blob: 7541d79072fe73dabe9d29e1f2a38d8bd2b4b3ca [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
Bram Moolenaar98921892016-02-23 17:14:37 +010022 *
23 * Support for GTK+ 3 was added by:
24 *
25 * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com>
Bram Moolenaar071d4272004-06-13 20:20:40 +000026 */
27
28#include "vim.h"
Bram Moolenaar36e294c2015-12-29 18:55:46 +010029#ifdef USE_GRESOURCE
30#include "auto/gui_gtk_gresources.h"
31#endif
Bram Moolenaarc93e7912008-07-08 10:46:08 +000032
Bram Moolenaar071d4272004-06-13 20:20:40 +000033#ifdef FEAT_GUI_GNOME
34/* Gnome redefines _() and N_(). Grrr... */
35# ifdef _
36# undef _
37# endif
38# ifdef N_
39# undef N_
40# endif
41# ifdef textdomain
42# undef textdomain
43# endif
44# ifdef bindtextdomain
45# undef bindtextdomain
46# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000047# ifdef bind_textdomain_codeset
48# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000049# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
51# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
52# endif
53# include <gnome.h>
54# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000055/* missing prototype in bonobo-dock-item.h */
56extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#endif
58
59#if !defined(FEAT_GUI_GTK) && defined(PROTO)
60/* When generating prototypes we don't want syntax errors. */
61# define GdkAtom int
62# define GdkEventExpose int
63# define GdkEventFocus int
64# define GdkEventVisibility int
65# define GdkEventProperty int
66# define GtkContainer int
67# define GtkTargetEntry int
68# define GtkType int
69# define GtkWidget int
70# define gint int
71# define gpointer int
72# define guint int
73# define GdkEventKey int
74# define GdkEventSelection int
75# define GtkSelectionData int
76# define GdkEventMotion int
77# define GdkEventButton int
78# define GdkDragContext int
79# define GdkEventConfigure int
80# define GdkEventClient int
81#else
Bram Moolenaar98921892016-02-23 17:14:37 +010082# if GTK_CHECK_VERSION(3,0,0)
83# include <gdk/gdkkeysyms-compat.h>
84# include <gtk/gtkx.h>
85# else
86# include <gdk/gdkkeysyms.h>
87# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088# include <gdk/gdk.h>
89# ifdef WIN3264
90# include <gdk/gdkwin32.h>
91# else
92# include <gdk/gdkx.h>
93# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000094# include <gtk/gtk.h>
95# include "gui_gtk_f.h"
96#endif
97
98#ifdef HAVE_X11_SUNKEYSYM_H
99# include <X11/Sunkeysym.h>
100#endif
101
102/*
103 * Easy-to-use macro for multihead support.
104 */
Bram Moolenaarb463e8d2017-06-05 15:07:09 +0200105#define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 gtk_widget_get_display(gui.mainwin), atom)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
108/* Selection type distinguishers */
109enum
110{
111 TARGET_TYPE_NONE,
112 TARGET_UTF8_STRING,
113 TARGET_STRING,
114 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000115 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 TARGET_TEXT,
117 TARGET_TEXT_URI_LIST,
118 TARGET_TEXT_PLAIN,
119 TARGET_VIM,
120 TARGET_VIMENC
121};
122
123/*
124 * Table of selection targets supported by Vim.
125 * Note: Order matters, preferred types should come first.
126 */
127static const GtkTargetEntry selection_targets[] =
128{
129 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
130 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000131 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
134 {"TEXT", 0, TARGET_TEXT},
135 {"STRING", 0, TARGET_STRING}
136};
137#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
138
139#ifdef FEAT_DND
140/*
141 * Table of DnD targets supported by Vim.
142 * Note: Order matters, preferred types should come first.
143 */
144static const GtkTargetEntry dnd_targets[] =
145{
146 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000147 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 {"STRING", 0, TARGET_STRING},
150 {"text/plain", 0, TARGET_TEXT_PLAIN}
151};
152# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
153#endif
154
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
157 * "Monospace" is a standard font alias that should be present
158 * on all proper Pango/fontconfig installations.
159 */
160# define DEFAULT_FONT "Monospace 10"
161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
163/*
164 * Atoms used to communicate save-yourself from the X11 session manager. There
165 * is no need to move them into the GUI struct, since they should be constant.
166 */
167static GdkAtom wm_protocols_atom = GDK_NONE;
168static GdkAtom save_yourself_atom = GDK_NONE;
169#endif
170
171/*
172 * Atoms used to control/reference X11 selections.
173 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000174static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178
179/*
180 * Keycodes recognized by vim.
181 * NOTE: when changing this, the table in gui_x11.c probably needs the same
182 * change!
183 */
184static struct special_key
185{
186 guint key_sym;
187 char_u code0;
188 char_u code1;
189}
190const special_keys[] =
191{
192 {GDK_Up, 'k', 'u'},
193 {GDK_Down, 'k', 'd'},
194 {GDK_Left, 'k', 'l'},
195 {GDK_Right, 'k', 'r'},
196 {GDK_F1, 'k', '1'},
197 {GDK_F2, 'k', '2'},
198 {GDK_F3, 'k', '3'},
199 {GDK_F4, 'k', '4'},
200 {GDK_F5, 'k', '5'},
201 {GDK_F6, 'k', '6'},
202 {GDK_F7, 'k', '7'},
203 {GDK_F8, 'k', '8'},
204 {GDK_F9, 'k', '9'},
205 {GDK_F10, 'k', ';'},
206 {GDK_F11, 'F', '1'},
207 {GDK_F12, 'F', '2'},
208 {GDK_F13, 'F', '3'},
209 {GDK_F14, 'F', '4'},
210 {GDK_F15, 'F', '5'},
211 {GDK_F16, 'F', '6'},
212 {GDK_F17, 'F', '7'},
213 {GDK_F18, 'F', '8'},
214 {GDK_F19, 'F', '9'},
215 {GDK_F20, 'F', 'A'},
216 {GDK_F21, 'F', 'B'},
217 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
218 {GDK_F22, 'F', 'C'},
219 {GDK_F23, 'F', 'D'},
220 {GDK_F24, 'F', 'E'},
221 {GDK_F25, 'F', 'F'},
222 {GDK_F26, 'F', 'G'},
223 {GDK_F27, 'F', 'H'},
224 {GDK_F28, 'F', 'I'},
225 {GDK_F29, 'F', 'J'},
226 {GDK_F30, 'F', 'K'},
227 {GDK_F31, 'F', 'L'},
228 {GDK_F32, 'F', 'M'},
229 {GDK_F33, 'F', 'N'},
230 {GDK_F34, 'F', 'O'},
231 {GDK_F35, 'F', 'P'},
232#ifdef SunXK_F36
233 {SunXK_F36, 'F', 'Q'},
234 {SunXK_F37, 'F', 'R'},
235#endif
236 {GDK_Help, '%', '1'},
237 {GDK_Undo, '&', '8'},
238 {GDK_BackSpace, 'k', 'b'},
239 {GDK_Insert, 'k', 'I'},
240 {GDK_Delete, 'k', 'D'},
241 {GDK_3270_BackTab, 'k', 'B'},
242 {GDK_Clear, 'k', 'C'},
243 {GDK_Home, 'k', 'h'},
244 {GDK_End, '@', '7'},
245 {GDK_Prior, 'k', 'P'},
246 {GDK_Next, 'k', 'N'},
247 {GDK_Print, '%', '9'},
248 /* Keypad keys: */
249 {GDK_KP_Left, 'k', 'l'},
250 {GDK_KP_Right, 'k', 'r'},
251 {GDK_KP_Up, 'k', 'u'},
252 {GDK_KP_Down, 'k', 'd'},
253 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
254 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
255 {GDK_KP_Home, 'K', '1'},
256 {GDK_KP_End, 'K', '4'},
257 {GDK_KP_Prior, 'K', '3'}, /* page up */
258 {GDK_KP_Next, 'K', '5'}, /* page down */
259
260 {GDK_KP_Add, 'K', '6'},
261 {GDK_KP_Subtract, 'K', '7'},
262 {GDK_KP_Divide, 'K', '8'},
263 {GDK_KP_Multiply, 'K', '9'},
264 {GDK_KP_Enter, 'K', 'A'},
265 {GDK_KP_Decimal, 'K', 'B'},
266
267 {GDK_KP_0, 'K', 'C'},
268 {GDK_KP_1, 'K', 'D'},
269 {GDK_KP_2, 'K', 'E'},
270 {GDK_KP_3, 'K', 'F'},
271 {GDK_KP_4, 'K', 'G'},
272 {GDK_KP_5, 'K', 'H'},
273 {GDK_KP_6, 'K', 'I'},
274 {GDK_KP_7, 'K', 'J'},
275 {GDK_KP_8, 'K', 'K'},
276 {GDK_KP_9, 'K', 'L'},
277
278 /* End of list marker: */
279 {0, 0, 0}
280};
281
282/*
283 * Flags for command line options table below.
284 */
285#define ARG_FONT 1
286#define ARG_GEOMETRY 2
287#define ARG_REVERSE 3
288#define ARG_NOREVERSE 4
289#define ARG_BACKGROUND 5
290#define ARG_FOREGROUND 6
291#define ARG_ICONIC 7
292#define ARG_ROLE 8
293#define ARG_NETBEANS 9
294#define ARG_XRM 10 /* ignored */
295#define ARG_MENUFONT 11 /* ignored */
296#define ARG_INDEX_MASK 0x00ff
297#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
298#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
299#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
300#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
301#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
302
303/*
304 * This table holds all the X GUI command line options allowed. This includes
305 * the standard ones so that we can skip them when Vim is started without the
306 * GUI (but the GUI might start up later).
307 *
308 * When changing this, also update doc/gui_x11.txt and the usage message!!!
309 */
310typedef struct
311{
312 const char *name;
313 unsigned int flags;
314}
315cmdline_option_T;
316
317static const cmdline_option_T cmdline_options[] =
318{
319 /* We handle these options ourselves */
320 {"-fn", ARG_FONT|ARG_HAS_VALUE},
321 {"-font", ARG_FONT|ARG_HAS_VALUE},
322 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
323 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
324 {"-rv", ARG_REVERSE},
325 {"-reverse", ARG_REVERSE},
326 {"+rv", ARG_NOREVERSE},
327 {"+reverse", ARG_NOREVERSE},
328 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
329 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
330 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
331 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
332 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#ifdef FEAT_NETBEANS_INTG
335 {"-nb", ARG_NETBEANS}, /* non-standard value format */
336 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
337 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
338 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
339#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 /* Arguments handled by GTK (and GNOME) internally. */
341 {"--g-fatal-warnings", ARG_FOR_GTK},
342 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
343 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
344 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
345 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
346 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--sync", ARG_FOR_GTK},
348 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
349 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
350 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef FEAT_GUI_GNOME
355 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
358 {"--sm-disable", ARG_FOR_GTK},
359 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"--oaf-private", ARG_FOR_GTK},
362 {"--enable-sound", ARG_FOR_GTK},
363 {"--disable-sound", ARG_FOR_GTK},
364 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
365 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
366 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
367 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
368# if 0 /* conflicts with Vim's own --version argument */
369 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
370# endif
371 {"--disable-crash-dialog", ARG_FOR_GTK},
372#endif
373 {NULL, 0}
374};
375
376static int gui_argc = 0;
377static char **gui_argv = NULL;
378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
381static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000382static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000383#endif
384static int found_iconic_arg = FALSE;
385
386#ifdef FEAT_GUI_GNOME
387/*
388 * Can't use Gnome if --socketid given
389 */
390static int using_gnome = 0;
391#else
392# define using_gnome 0
393#endif
394
395/*
396 * Parse the GUI related command-line arguments. Any arguments used are
397 * deleted from argv, and *argc is decremented accordingly. This is called
398 * when vim is started, whether or not the GUI has been started.
399 */
400 void
401gui_mch_prepare(int *argc, char **argv)
402{
403 const cmdline_option_T *option;
404 int i = 0;
405 int len = 0;
406
407#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
408 /*
409 * Determine the command used to invoke Vim, to be passed as restart
410 * command to the session manager. If argv[0] contains any directory
411 * components try building an absolute path, otherwise leave it as is.
412 */
413 restart_command = argv[0];
414
415 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
416 {
417 char_u buf[MAXPATHL];
418
419 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000420 {
421 abs_restart_command = (char *)vim_strsave(buf);
422 restart_command = abs_restart_command;
423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425#endif
426
427 /*
428 * Move all the entries in argv which are relevant to GTK+ and GNOME
429 * into gui_argv. Freed later in gui_mch_init().
430 */
431 gui_argc = 0;
432 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
433
434 g_return_if_fail(gui_argv != NULL);
435
436 gui_argv[gui_argc++] = argv[i++];
437
438 while (i < *argc)
439 {
440 /* Don't waste CPU cycles on non-option arguments. */
441 if (argv[i][0] != '-' && argv[i][0] != '+')
442 {
443 ++i;
444 continue;
445 }
446
447 /* Look for argv[i] in cmdline_options[] table. */
448 for (option = &cmdline_options[0]; option->name != NULL; ++option)
449 {
450 len = strlen(option->name);
451
452 if (strncmp(argv[i], option->name, len) == 0)
453 {
454 if (argv[i][len] == '\0')
455 break;
456 /* allow --foo=bar style */
457 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
458 break;
459#ifdef FEAT_NETBEANS_INTG
460 /* darn, -nb has non-standard syntax */
461 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
462 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
463 break;
464#endif
465 }
466 else if ((option->flags & ARG_COMPAT_LONG)
467 && strcmp(argv[i], option->name + 1) == 0)
468 {
469 /* Replace the standard X arguments "-name" and "-display"
470 * with their GNU-style long option counterparts. */
471 argv[i] = (char *)option->name;
472 break;
473 }
474 }
475 if (option->name == NULL) /* no match */
476 {
477 ++i;
478 continue;
479 }
480
481 if (option->flags & ARG_FOR_GTK)
482 {
483 /* Move the argument into gui_argv, which
484 * will later be passed to gtk_init_check() */
485 gui_argv[gui_argc++] = argv[i];
486 }
487 else
488 {
489 char *value = NULL;
490
491 /* Extract the option's value if there is one.
492 * Accept both "--foo bar" and "--foo=bar" style. */
493 if (option->flags & ARG_HAS_VALUE)
494 {
495 if (argv[i][len] == '=')
496 value = &argv[i][len + 1];
497 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
498 value = argv[i + 1];
499 }
500
501 /* Check for options handled by Vim itself */
502 switch (option->flags & ARG_INDEX_MASK)
503 {
504 case ARG_REVERSE:
505 found_reverse_arg = TRUE;
506 break;
507 case ARG_NOREVERSE:
508 found_reverse_arg = FALSE;
509 break;
510 case ARG_FONT:
511 font_argument = value;
512 break;
513 case ARG_GEOMETRY:
514 if (value != NULL)
515 gui.geom = vim_strsave((char_u *)value);
516 break;
517 case ARG_BACKGROUND:
518 background_argument = value;
519 break;
520 case ARG_FOREGROUND:
521 foreground_argument = value;
522 break;
523 case ARG_ICONIC:
524 found_iconic_arg = TRUE;
525 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 case ARG_ROLE:
527 role_argument = value; /* used later in gui_mch_open() */
528 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529#ifdef FEAT_NETBEANS_INTG
530 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 gui.dofork = FALSE; /* don't fork() when starting GUI */
532 netbeansArg = argv[i];
533 break;
534#endif
535 default:
536 break;
537 }
538 }
539
540 /* These arguments make gnome_program_init() print a message and exit.
Bram Moolenaar717e1962016-08-10 21:28:44 +0200541 * Must start the GUI for this, otherwise ":gui" will exit later!
542 * Only when the GUI can start. */
543 if ((option->flags & ARG_NEEDS_GUI)
544 && gui_mch_early_init_check(FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 gui.starting = TRUE;
546
547 if (option->flags & ARG_KEEP)
548 ++i;
549 else
550 {
551 /* Remove the flag from the argument vector. */
552 if (--*argc > i)
553 {
554 int n_strip = 1;
555
556 /* Move the argument's value as well, if there is one. */
557 if ((option->flags & ARG_HAS_VALUE)
558 && argv[i][len] != '='
559 && strcmp(argv[i + 1], "--") != 0)
560 {
561 ++n_strip;
562 --*argc;
563 if (option->flags & ARG_FOR_GTK)
564 gui_argv[gui_argc++] = argv[i + 1];
565 }
566
567 if (*argc > i)
568 mch_memmove(&argv[i], &argv[i + n_strip],
569 (*argc - i) * sizeof(char *));
570 }
571 argv[*argc] = NULL;
572 }
573 }
574
575 gui_argv[gui_argc] = NULL;
576}
577
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000578#if defined(EXITFREE) || defined(PROTO)
579 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100580gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000581{
582 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000583#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
584 vim_free(abs_restart_command);
585#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000586}
587#endif
588
Bram Moolenaar98921892016-02-23 17:14:37 +0100589#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590/*
591 * This should be maybe completely removed.
592 * Doesn't seem possible, since check_copy_area() relies on
593 * this information. --danielk
594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000596visibility_event(GtkWidget *widget UNUSED,
597 GdkEventVisibility *event,
598 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599{
600 gui.visibility = event->state;
601 /*
602 * When we do an gdk_window_copy_area(), and the window is partially
603 * obscured, we want to receive an event to tell us whether it worked
604 * or not.
605 */
606 if (gui.text_gc != NULL)
607 gdk_gc_set_exposures(gui.text_gc,
608 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
609 return FALSE;
610}
Bram Moolenaar98921892016-02-23 17:14:37 +0100611#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613/*
614 * Redraw the corresponding portions of the screen.
615 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100616#if GTK_CHECK_VERSION(3,0,0)
617static gboolean is_key_pressed = FALSE;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100618static gboolean blink_mode = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100619
620static gboolean gui_gtk_is_blink_on(void);
Bram Moolenaar98921892016-02-23 17:14:37 +0100621static void gui_gtk_window_clear(GdkWindow *win);
622
623 static void
624gui_gtk3_redraw(int x, int y, int width, int height)
625{
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100626 /* Range checks are left to gui_redraw_block() */
Bram Moolenaar98921892016-02-23 17:14:37 +0100627 gui_redraw_block(Y_2_ROW(y), X_2_COL(x),
628 Y_2_ROW(y + height - 1), X_2_COL(x + width - 1),
629 GUI_MON_NOCLEAR);
630}
631
632 static void
633gui_gtk3_update_cursor(cairo_t *cr)
634{
635 if (gui.row == gui.cursor_row)
636 {
637 gui.by_signal = TRUE;
Bram Moolenaar4fc563b2016-03-12 12:40:58 +0100638 if (State & CMDLINE)
639 gui_update_cursor(TRUE, FALSE);
640 else
641 gui_update_cursor(TRUE, TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +0100642 gui.by_signal = FALSE;
643 cairo_paint(cr);
644 }
645}
646
647 static gboolean
648gui_gtk3_should_draw_cursor(void)
649{
650 unsigned int cond = 0;
651 cond |= gui_gtk_is_blink_on();
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100652 if (gui.cursor_col >= gui.col)
653 cond |= is_key_pressed;
Bram Moolenaar98921892016-02-23 17:14:37 +0100654 cond |= gui.in_focus == FALSE;
Bram Moolenaar98921892016-02-23 17:14:37 +0100655 return cond;
656}
657
658 static gboolean
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200659draw_event(GtkWidget *widget UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +0100660 cairo_t *cr,
661 gpointer user_data UNUSED)
662{
663 /* Skip this when the GUI isn't set up yet, will redraw later. */
664 if (gui.starting)
665 return FALSE;
666
667 out_flush(); /* make sure all output has been processed */
668 /* for GTK+ 3, may induce other draw events. */
669
670 cairo_set_source_surface(cr, gui.surface, 0, 0);
671
672 /* Draw the window without the cursor. */
673 gui.by_signal = TRUE;
674 {
675 cairo_rectangle_list_t *list = NULL;
676
Bram Moolenaar98921892016-02-23 17:14:37 +0100677 list = cairo_copy_clip_rectangle_list(cr);
678 if (list->status != CAIRO_STATUS_CLIP_NOT_REPRESENTABLE)
679 {
680 int i;
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100681
682 /* First clear all the blocks and then redraw them. Just in case
683 * some blocks overlap. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100684 for (i = 0; i < list->num_rectangles; i++)
685 {
686 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200687
Bram Moolenaarfe344a92017-02-23 12:20:35 +0100688 gui_mch_clear_block(Y_2_ROW((int)rect.y), 0,
689 Y_2_ROW((int)(rect.y + rect.height)) - 1, Columns - 1);
690 }
691
692 for (i = 0; i < list->num_rectangles; i++)
693 {
694 const cairo_rectangle_t rect = list->rectangles[i];
Bram Moolenaar8e31fd52016-06-04 22:18:13 +0200695
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100696 if (blink_mode)
697 gui_gtk3_redraw(rect.x, rect.y, rect.width, rect.height);
698 else
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100699 {
700 if (get_real_state() & VISUAL)
701 gui_gtk3_redraw(rect.x, rect.y,
702 rect.width, rect.height);
703 else
704 gui_redraw(rect.x, rect.y, rect.width, rect.height);
705 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100706 }
707 }
708 cairo_rectangle_list_destroy(list);
709
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100710 if (get_real_state() & VISUAL)
711 {
712 if (gui.cursor_row == gui.row && gui.cursor_col >= gui.col)
713 gui_update_cursor(TRUE, TRUE);
714 }
715
Bram Moolenaar98921892016-02-23 17:14:37 +0100716 cairo_paint(cr);
717 }
718 gui.by_signal = FALSE;
719
720 /* Add the cursor to the window if necessary.*/
Bram Moolenaarb4ebf9a2016-03-12 16:28:18 +0100721 if (gui_gtk3_should_draw_cursor() && blink_mode)
Bram Moolenaar98921892016-02-23 17:14:37 +0100722 gui_gtk3_update_cursor(cr);
723
724 return FALSE;
725}
726#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000728expose_event(GtkWidget *widget UNUSED,
729 GdkEventExpose *event,
730 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731{
732 /* Skip this when the GUI isn't set up yet, will redraw later. */
733 if (gui.starting)
734 return FALSE;
735
736 out_flush(); /* make sure all output has been processed */
737 gui_redraw(event->area.x, event->area.y,
738 event->area.width, event->area.height);
739
740 /* Clear the border areas if needed */
741 if (event->area.x < FILL_X(0))
742 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
743 if (event->area.y < FILL_Y(0))
744 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
745 if (event->area.x > FILL_X(Columns))
746 gdk_window_clear_area(gui.drawarea->window,
747 FILL_X((int)Columns), 0, 0, 0);
748 if (event->area.y > FILL_Y(Rows))
749 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
750
751 return FALSE;
752}
Bram Moolenaar98921892016-02-23 17:14:37 +0100753#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755#ifdef FEAT_CLIENTSERVER
756/*
757 * Handle changes to the "Comm" property
758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000760property_event(GtkWidget *widget,
761 GdkEventProperty *event,
762 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763{
764 if (event->type == GDK_PROPERTY_NOTIFY
765 && event->state == (int)GDK_PROPERTY_NEW_VALUE
Bram Moolenaar98921892016-02-23 17:14:37 +0100766# if GTK_CHECK_VERSION(3,0,0)
767 && GDK_WINDOW_XID(event->window) == commWindow
768# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 && GDK_WINDOW_XWINDOW(event->window) == commWindow
Bram Moolenaar98921892016-02-23 17:14:37 +0100770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 && GET_X_ATOM(event->atom) == commProperty)
772 {
773 XEvent xev;
774
775 /* Translate to XLib */
776 xev.xproperty.type = PropertyNotify;
777 xev.xproperty.atom = commProperty;
778 xev.xproperty.window = commWindow;
779 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar98921892016-02-23 17:14:37 +0100780# if GTK_CHECK_VERSION(3,0,0)
781 serverEventProc(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(widget)),
782 &xev, 0);
783# else
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200784 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +0100785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 }
787 return FALSE;
788}
Bram Moolenaar98921892016-02-23 17:14:37 +0100789#endif /* defined(FEAT_CLIENTSERVER) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +0200791/*
792 * Handle changes to the "Xft/DPI" setting
793 */
794 static void
795gtk_settings_xft_dpi_changed_cb(GtkSettings *gtk_settings UNUSED,
796 GParamSpec *pspec UNUSED,
797 gpointer data UNUSED)
798{
799 // Create a new PangoContext for this screen, and initialize it
800 // with the current font if necessary.
801 if (gui.text_context != NULL)
802 g_object_unref(gui.text_context);
803
804 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
805 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
806
807 if (gui.norm_font != NULL)
808 {
809 // force default font
810 gui_mch_init_font(*p_guifont == NUL ? NULL : p_guifont, FALSE);
811 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
812 }
813}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200815#if GTK_CHECK_VERSION(3,0,0)
816typedef gboolean timeout_cb_type;
817#else
818typedef gint timeout_cb_type;
819#endif
820
821/*
822 * Start a timer that will invoke the specified callback.
823 * Returns the ID of the timer.
824 */
825 static guint
826timeout_add(int time, timeout_cb_type (*callback)(gpointer), int *flagp)
827{
828#if GTK_CHECK_VERSION(3,0,0)
829 return g_timeout_add((guint)time, (GSourceFunc)callback, flagp);
830#else
831 return gtk_timeout_add((guint32)time, (GtkFunction)callback, flagp);
832#endif
833}
834
835 static void
836timeout_remove(guint timer)
837{
838#if GTK_CHECK_VERSION(3,0,0)
839 g_source_remove(timer);
840#else
841 gtk_timeout_remove(timer);
842#endif
843}
844
845
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846/****************************************************************************
847 * Focus handlers:
848 */
849
850
851/*
852 * This is a simple state machine:
853 * BLINK_NONE not blinking at all
854 * BLINK_OFF blinking, cursor is not shown
855 * BLINK_ON blinking, cursor is shown
856 */
857
858#define BLINK_NONE 0
859#define BLINK_OFF 1
860#define BLINK_ON 2
861
862static int blink_state = BLINK_NONE;
863static long_u blink_waittime = 700;
864static long_u blink_ontime = 400;
865static long_u blink_offtime = 250;
866static guint blink_timer = 0;
867
Bram Moolenaar98921892016-02-23 17:14:37 +0100868#if GTK_CHECK_VERSION(3,0,0)
869 static gboolean
870gui_gtk_is_blink_on(void)
871{
872 return blink_state == BLINK_ON;
873}
Bram Moolenaar98921892016-02-23 17:14:37 +0100874#endif
875
Bram Moolenaar703a8042016-06-04 16:24:32 +0200876 int
877gui_mch_is_blinking(void)
878{
879 return blink_state != BLINK_NONE;
880}
881
Bram Moolenaar9d5d3c92016-07-07 16:43:02 +0200882 int
883gui_mch_is_blink_off(void)
884{
885 return blink_state == BLINK_OFF;
886}
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 void
889gui_mch_set_blinking(long waittime, long on, long off)
890{
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100891#if GTK_CHECK_VERSION(3,0,0)
892 if (waittime == 0 || on == 0 || off == 0)
893 {
894 blink_mode = FALSE;
895
896 blink_waittime = 700;
897 blink_ontime = 400;
898 blink_offtime = 250;
899 }
900 else
901 {
902 blink_mode = TRUE;
903
904 blink_waittime = waittime;
905 blink_ontime = on;
906 blink_offtime = off;
907 }
908#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 blink_waittime = waittime;
910 blink_ontime = on;
911 blink_offtime = off;
Bram Moolenaar0ecbe332016-03-05 22:40:52 +0100912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913}
914
915/*
916 * Stop the cursor blinking. Show the cursor if it wasn't shown.
917 */
918 void
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100919gui_mch_stop_blink(int may_call_gui_update_cursor)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920{
921 if (blink_timer)
922 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200923 timeout_remove(blink_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 blink_timer = 0;
925 }
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +0100926 if (blink_state == BLINK_OFF && may_call_gui_update_cursor)
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200929 gui_mch_flush();
930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 blink_state = BLINK_NONE;
932}
933
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200934 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000935blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936{
937 if (blink_state == BLINK_ON)
938 {
939 gui_undraw_cursor();
940 blink_state = BLINK_OFF;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200941 blink_timer = timeout_add(blink_offtime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 }
943 else
944 {
945 gui_update_cursor(TRUE, FALSE);
946 blink_state = BLINK_ON;
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200947 blink_timer = timeout_add(blink_ontime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200949 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951 return FALSE; /* don't happen again */
952}
953
954/*
955 * Start the cursor blinking. If it was already blinking, this restarts the
956 * waiting time and shows the cursor.
957 */
958 void
959gui_mch_start_blink(void)
960{
961 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200962 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200963 timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200964 blink_timer = 0;
965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /* Only switch blinking on if none of the times is zero */
967 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
968 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +0200969 blink_timer = timeout_add(blink_waittime, blink_cb, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 blink_state = BLINK_ON;
971 gui_update_cursor(TRUE, FALSE);
Bram Moolenaarda3a77d2016-07-10 23:16:09 +0200972 gui_mch_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 }
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000977enter_notify_event(GtkWidget *widget UNUSED,
978 GdkEventCrossing *event UNUSED,
979 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
981 if (blink_state == BLINK_NONE)
982 gui_mch_start_blink();
983
984 /* make sure keyboard input goes there */
Bram Moolenaar98921892016-02-23 17:14:37 +0100985#if GTK_CHECK_VERSION(3,0,0)
986 if (gtk_socket_id == 0 || !gtk_widget_has_focus(gui.drawarea))
987#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
Bram Moolenaar98921892016-02-23 17:14:37 +0100989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 gtk_widget_grab_focus(gui.drawarea);
991
992 return FALSE;
993}
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000996leave_notify_event(GtkWidget *widget UNUSED,
997 GdkEventCrossing *event UNUSED,
998 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999{
1000 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001001 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 return FALSE;
1004}
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001007focus_in_event(GtkWidget *widget,
1008 GdkEventFocus *event UNUSED,
1009 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010{
1011 gui_focus_change(TRUE);
1012
1013 if (blink_state == BLINK_NONE)
1014 gui_mch_start_blink();
1015
Bram Moolenaar9c8791f2007-09-05 19:47:23 +00001016 /* make sure keyboard input goes to the draw area (if this is focus for a
1017 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00001018 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001019 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020
1021 return TRUE;
1022}
1023
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001025focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +02001026 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001027 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
1029 gui_focus_change(FALSE);
1030
1031 if (blink_state != BLINK_NONE)
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001032 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 return TRUE;
1035}
1036
1037
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038/*
1039 * Translate a GDK key value to UTF-8 independently of the current locale.
1040 * The output is written to string, which must have room for at least 6 bytes
1041 * plus the NUL terminator. Returns the length in bytes.
1042 *
1043 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
1044 * of GdkEventKey::string instead. But event->string is evil; see here why:
1045 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
1046 */
1047 static int
1048keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
1049{
1050 int len;
1051 guint32 uc;
1052
1053 uc = gdk_keyval_to_unicode(keyval);
1054 if (uc != 0)
1055 {
1056 /* Check for CTRL-foo */
1057 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
1058 {
1059 /* These mappings look arbitrary at the first glance, but in fact
1060 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
1061 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
1062 * more sense and is consistent with usual terminal behaviour). */
1063 if (uc >= '@')
1064 string[0] = uc & 0x1F;
1065 else if (uc == '2')
1066 string[0] = NUL;
1067 else if (uc >= '3' && uc <= '7')
1068 string[0] = uc ^ 0x28;
1069 else if (uc == '8')
1070 string[0] = BS;
1071 else if (uc == '?')
1072 string[0] = DEL;
1073 else
1074 string[0] = uc;
1075 len = 1;
1076 }
1077 else
1078 {
1079 /* Translate a normal key to UTF-8. This doesn't work for dead
1080 * keys of course, you _have_ to use an input method for that. */
1081 len = utf_char2bytes((int)uc, string);
1082 }
1083 }
1084 else
1085 {
1086 /* Translate keys which are represented by ASCII control codes in Vim.
1087 * There are only a few of those; most control keys are translated to
1088 * special terminal-like control sequences. */
1089 len = 1;
1090 switch (keyval)
1091 {
1092 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
1093 string[0] = TAB;
1094 break;
1095 case GDK_Linefeed:
1096 string[0] = NL;
1097 break;
1098 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
1099 string[0] = CAR;
1100 break;
1101 case GDK_Escape:
1102 string[0] = ESC;
1103 break;
1104 default:
1105 len = 0;
1106 break;
1107 }
1108 }
1109 string[len] = NUL;
1110
1111 return len;
1112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001114 static int
1115modifiers_gdk2vim(guint state)
1116{
1117 int modifiers = 0;
1118
1119 if (state & GDK_SHIFT_MASK)
1120 modifiers |= MOD_MASK_SHIFT;
1121 if (state & GDK_CONTROL_MASK)
1122 modifiers |= MOD_MASK_CTRL;
1123 if (state & GDK_MOD1_MASK)
1124 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +02001125#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001126 if (state & GDK_SUPER_MASK)
1127 modifiers |= MOD_MASK_META;
1128#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001129 if (state & GDK_MOD4_MASK)
1130 modifiers |= MOD_MASK_META;
1131
1132 return modifiers;
1133}
1134
1135 static int
1136modifiers_gdk2mouse(guint state)
1137{
1138 int modifiers = 0;
1139
1140 if (state & GDK_SHIFT_MASK)
1141 modifiers |= MOUSE_SHIFT;
1142 if (state & GDK_CONTROL_MASK)
1143 modifiers |= MOUSE_CTRL;
1144 if (state & GDK_MOD1_MASK)
1145 modifiers |= MOUSE_ALT;
1146
1147 return modifiers;
1148}
1149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150/*
1151 * Main keyboard handler:
1152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001154key_press_event(GtkWidget *widget UNUSED,
1155 GdkEventKey *event,
1156 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001158 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
1160 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 guint key_sym;
1162 int len;
1163 int i;
1164 int modifiers;
1165 int key;
1166 guint state;
1167 char_u *s, *d;
1168
Bram Moolenaar98921892016-02-23 17:14:37 +01001169#if GTK_CHECK_VERSION(3,0,0)
1170 is_key_pressed = TRUE;
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01001171 gui_mch_stop_blink(TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001172#endif
1173
Bram Moolenaar20892c12011-06-26 04:49:00 +02001174 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 key_sym = event->keyval;
1176 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178#ifdef FEAT_XIM
1179 if (xim_queue_key_press_event(event, TRUE))
1180 return TRUE;
1181#endif
1182
1183#ifdef FEAT_HANGULIN
1184 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1185 {
1186 hangul_input_state_toggle();
1187 return TRUE;
1188 }
1189#endif
1190
1191#ifdef SunXK_F36
1192 /*
1193 * These keys have bogus lookup strings, and trapping them here is
1194 * easier than trying to XRebindKeysym() on them with every possible
1195 * combination of modifiers.
1196 */
1197 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1198 len = 0;
1199 else
1200#endif
1201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 len = keyval_to_string(key_sym, state, string2);
1203
1204 /* Careful: convert_input() doesn't handle the NUL character.
1205 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1206 if (len > 1 && input_conv.vc_type != CONV_NONE)
1207 len = convert_input(string2, len, sizeof(string2));
1208
1209 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 d = string;
1211 for (i = 0; i < len; ++i)
1212 {
1213 *d++ = s[i];
1214 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1215 {
1216 /* Turn CSI into K_CSI. */
1217 *d++ = KS_EXTRA;
1218 *d++ = (int)KE_CSI;
1219 }
1220 }
1221 len = d - string;
1222 }
1223
1224 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1225 if (key_sym == GDK_ISO_Left_Tab)
1226 {
1227 key_sym = GDK_Tab;
1228 state |= GDK_SHIFT_MASK;
1229 }
1230
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#ifdef FEAT_MENU
1232 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1233 * is a menu shortcut, we ignore everything with the ALT modifier. */
1234 if ((state & GDK_MOD1_MASK)
1235 && gui.menu_is_active
1236 && (*p_wak == 'y'
1237 || (*p_wak == 'm'
1238 && len == 1
1239 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 /* For GTK2 we return false to signify that we haven't handled the
1241 * keypress, so that gtk will handle the mnemonic or accelerator. */
1242 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244
1245 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1246 * that already has the 8th bit set.
1247 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1248 * Don't do this for double-byte encodings, it turns the char into a lead
1249 * byte. */
1250 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001251 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001252#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001253 || (state & GDK_SUPER_MASK)
1254#endif
1255 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1257 && (string[0] & 0x80) == 0
1258 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 )
1261 {
1262 string[0] |= 0x80;
1263 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 if (enc_utf8) /* convert to utf-8 */
1265 {
1266 string[1] = string[0] & 0xbf;
1267 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1268 if (string[1] == CSI)
1269 {
1270 string[2] = KS_EXTRA;
1271 string[3] = (int)KE_CSI;
1272 len = 4;
1273 }
1274 else
1275 len = 2;
1276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278
1279 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1280 * value) to detect backspace, delete and keypad keys. */
1281 if (len == 0 || len == 1)
1282 {
1283 for (i = 0; special_keys[i].key_sym != 0; i++)
1284 {
1285 if (special_keys[i].key_sym == key_sym)
1286 {
1287 string[0] = CSI;
1288 string[1] = special_keys[i].code0;
1289 string[2] = special_keys[i].code1;
1290 len = -3;
1291 break;
1292 }
1293 }
1294 }
1295
1296 if (len == 0) /* Unrecognized key */
1297 return TRUE;
1298
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 /* Special keys (and a few others) may have modifiers. Also when using a
1300 * double-byte encoding (can't set the 8th bit). */
1301 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1302 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1303 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1304 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001305 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001306#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001307 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001309 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001311 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312
1313 /*
1314 * For some keys a shift modifier is translated into another key
1315 * code.
1316 */
1317 if (len == -3)
1318 key = TO_SPECIAL(string[1], string[2]);
1319 else
1320 key = string[0];
1321
1322 key = simplify_key(key, &modifiers);
1323 if (key == CSI)
1324 key = K_CSI;
1325 if (IS_SPECIAL(key))
1326 {
1327 string[0] = CSI;
1328 string[1] = K_SECOND(key);
1329 string[2] = K_THIRD(key);
1330 len = 3;
1331 }
1332 else
1333 {
1334 string[0] = key;
1335 len = 1;
1336 }
1337
1338 if (modifiers != 0)
1339 {
1340 string2[0] = CSI;
1341 string2[1] = KS_MODIFIER;
1342 string2[2] = modifiers;
1343 add_to_input_buf(string2, 3);
1344 }
1345 }
1346
1347 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1348 || (string[0] == intr_char && intr_char != Ctrl_C)))
1349 {
1350 trash_input_buf();
1351 got_int = TRUE;
1352 }
1353
1354 add_to_input_buf(string, len);
1355
1356 /* blank out the pointer if necessary */
1357 if (p_mh)
1358 gui_mch_mousehide(TRUE);
1359
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 return TRUE;
1361}
1362
Bram Moolenaar98921892016-02-23 17:14:37 +01001363#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001365key_release_event(GtkWidget *widget UNUSED,
1366 GdkEventKey *event,
1367 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368{
Bram Moolenaar98921892016-02-23 17:14:37 +01001369# if GTK_CHECK_VERSION(3,0,0)
1370 is_key_pressed = FALSE;
1371 gui_mch_start_blink();
1372# endif
1373# if defined(FEAT_XIM)
Bram Moolenaar20892c12011-06-26 04:49:00 +02001374 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 /*
1376 * GTK+ 2 input methods may do fancy stuff on key release events too.
1377 * With the default IM for instance, you can enter any UCS code point
1378 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1379 */
1380 return xim_queue_key_press_event(event, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01001381# else
1382 return TRUE;
1383# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384}
1385#endif
1386
1387
1388/****************************************************************************
1389 * Selection handlers:
1390 */
1391
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001392/* Remember when clip_lose_selection was called from here, we must not call
1393 * gtk_selection_owner_set() then. */
1394static int in_selection_clear_event = FALSE;
1395
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001397selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001399 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001401 in_selection_clear_event = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (event->selection == clip_plus.gtk_sel_atom)
1403 clip_lose_selection(&clip_plus);
1404 else
1405 clip_lose_selection(&clip_star);
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01001406 in_selection_clear_event = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 return TRUE;
1409}
1410
1411#define RS_NONE 0 /* selection_received_cb() not called yet */
1412#define RS_OK 1 /* selection_received_cb() called and OK */
1413#define RS_FAIL 2 /* selection_received_cb() called and failed */
1414static int received_selection = RS_NONE;
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001417selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001419 guint time_ UNUSED,
1420 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
1422 VimClipboard *cbd;
1423 char_u *text;
1424 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001427 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaar98921892016-02-23 17:14:37 +01001429#if GTK_CHECK_VERSION(3,0,0)
1430 if (gtk_selection_data_get_selection(data) == clip_plus.gtk_sel_atom)
1431#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 cbd = &clip_plus;
1435 else
1436 cbd = &clip_star;
1437
Bram Moolenaar98921892016-02-23 17:14:37 +01001438#if GTK_CHECK_VERSION(3,0,0)
1439 text = (char_u *)gtk_selection_data_get_data(data);
1440 len = gtk_selection_data_get_length(data);
1441#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 text = (char_u *)data->data;
1443 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01001444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445
1446 if (text == NULL || len <= 0)
1447 {
1448 received_selection = RS_FAIL;
1449 /* clip_free_selection(cbd); ??? */
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return;
1452 }
1453
Bram Moolenaar98921892016-02-23 17:14:37 +01001454#if GTK_CHECK_VERSION(3,0,0)
1455 if (gtk_selection_data_get_data_type(data) == vim_atom)
1456#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (data->type == vim_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 {
1460 motion_type = *text++;
1461 --len;
1462 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001463#if GTK_CHECK_VERSION(3,0,0)
1464 else if (gtk_selection_data_get_data_type(data) == vimenc_atom)
1465#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else if (data->type == vimenc_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 char_u *enc;
1470 vimconv_T conv;
1471
1472 motion_type = *text++;
1473 --len;
1474
1475 enc = text;
1476 text += STRLEN(text) + 1;
1477 len -= text - enc;
1478
1479 /* If the encoding of the text is different from 'encoding', attempt
1480 * converting it. */
1481 conv.vc_type = CONV_NONE;
1482 convert_setup(&conv, enc, p_enc);
1483 if (conv.vc_type != CONV_NONE)
1484 {
1485 tmpbuf = string_convert(&conv, text, &len);
1486 if (tmpbuf != NULL)
1487 text = tmpbuf;
1488 convert_setup(&conv, NULL, NULL);
1489 }
1490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /* gtk_selection_data_get_text() handles all the nasty details
1493 * and targets and encodings etc. This rocks so hard. */
1494 else
1495 {
1496 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1497 if (tmpbuf_utf8 != NULL)
1498 {
1499 len = STRLEN(tmpbuf_utf8);
1500 if (input_conv.vc_type != CONV_NONE)
1501 {
1502 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1503 if (tmpbuf != NULL)
1504 text = tmpbuf;
1505 }
1506 else
1507 text = tmpbuf_utf8;
1508 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001509 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1510 {
1511 vimconv_T conv;
1512
1513 /* UTF-16, we get this for HTML */
1514 conv.vc_type = CONV_NONE;
1515 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1516
1517 if (conv.vc_type != CONV_NONE)
1518 {
1519 text += 2;
1520 len -= 2;
1521 tmpbuf = string_convert(&conv, text, &len);
1522 convert_setup(&conv, NULL, NULL);
1523 }
1524 if (tmpbuf != NULL)
1525 text = tmpbuf;
1526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001529 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001530 while (len > 0 && text[len - 1] == NUL)
1531 --len;
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 clip_yank_selection(motion_type, text, (long)len, cbd);
1534 received_selection = RS_OK;
1535 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537}
1538
1539/*
1540 * Prepare our selection data for passing it to the external selection
1541 * client.
1542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001544selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 GtkSelectionData *selection_data,
1546 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001547 guint time_ UNUSED,
1548 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 char_u *string;
1551 char_u *tmpbuf;
1552 long_u tmplen;
1553 int length;
1554 int motion_type;
1555 GdkAtom type;
1556 VimClipboard *cbd;
1557
Bram Moolenaar98921892016-02-23 17:14:37 +01001558#if GTK_CHECK_VERSION(3,0,0)
1559 if (gtk_selection_data_get_selection(selection_data)
1560 == clip_plus.gtk_sel_atom)
1561#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 if (selection_data->selection == clip_plus.gtk_sel_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01001563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 cbd = &clip_plus;
1565 else
1566 cbd = &clip_star;
1567
1568 if (!cbd->owned)
1569 return; /* Shouldn't ever happen */
1570
1571 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001572 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 && info != (guint)TARGET_UTF8_STRING
1574 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 && info != (guint)TARGET_VIM
1576 && info != (guint)TARGET_COMPOUND_TEXT
1577 && info != (guint)TARGET_TEXT)
1578 return;
1579
1580 /* get the selection from the '*'/'+' register */
1581 clip_get_selection(cbd);
1582
1583 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1584 if (motion_type < 0 || string == NULL)
1585 return;
1586 /* Due to int arguments we can't handle more than G_MAXINT. Also
1587 * reserve one extra byte for NUL or the motion type; just in case.
1588 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1589 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1590
1591 if (info == (guint)TARGET_VIM)
1592 {
1593 tmpbuf = alloc((unsigned)length + 1);
1594 if (tmpbuf != NULL)
1595 {
1596 tmpbuf[0] = motion_type;
1597 mch_memmove(tmpbuf + 1, string, (size_t)length);
1598 }
1599 /* For our own format, the first byte contains the motion type */
1600 ++length;
1601 vim_free(string);
1602 string = tmpbuf;
1603 type = vim_atom;
1604 }
1605
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001606 else if (info == (guint)TARGET_HTML)
1607 {
1608 vimconv_T conv;
1609
1610 /* Since we get utf-16, we probably should set it as well. */
1611 conv.vc_type = CONV_NONE;
1612 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1613 if (conv.vc_type != CONV_NONE)
1614 {
1615 tmpbuf = string_convert(&conv, string, &length);
1616 convert_setup(&conv, NULL, NULL);
1617 vim_free(string);
1618 string = tmpbuf;
1619 }
1620
1621 /* Prepend the BOM: "fffe" */
1622 if (string != NULL)
1623 {
1624 tmpbuf = alloc(length + 2);
1625 tmpbuf[0] = 0xff;
1626 tmpbuf[1] = 0xfe;
1627 mch_memmove(tmpbuf + 2, string, (size_t)length);
1628 vim_free(string);
1629 string = tmpbuf;
1630 length += 2;
1631
Bram Moolenaar98921892016-02-23 17:14:37 +01001632#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001633 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001634 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001635 selection_data->type = selection_data->target;
1636 selection_data->format = 16; /* 16 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001637#endif
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001638 gtk_selection_data_set(selection_data, html_atom, 16,
1639 string, length);
1640 vim_free(string);
1641 }
1642 return;
1643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 else if (info == (guint)TARGET_VIMENC)
1645 {
1646 int l = STRLEN(p_enc);
1647
1648 /* contents: motion_type 'encoding' NUL text */
1649 tmpbuf = alloc((unsigned)length + l + 2);
1650 if (tmpbuf != NULL)
1651 {
1652 tmpbuf[0] = motion_type;
1653 STRCPY(tmpbuf + 1, p_enc);
1654 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1655 }
1656 length += l + 2;
1657 vim_free(string);
1658 string = tmpbuf;
1659 type = vimenc_atom;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 /* gtk_selection_data_set_text() handles everything for us. This is
1663 * so easy and simple and cool, it'd be insane not to use it. */
1664 else
1665 {
1666 if (output_conv.vc_type != CONV_NONE)
1667 {
1668 tmpbuf = string_convert(&output_conv, string, &length);
1669 vim_free(string);
1670 if (tmpbuf == NULL)
1671 return;
1672 string = tmpbuf;
1673 }
1674 /* Validate the string to avoid runtime warnings */
1675 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1676 {
1677 gtk_selection_data_set_text(selection_data,
1678 (const char *)string, length);
1679 }
1680 vim_free(string);
1681 return;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683
1684 if (string != NULL)
1685 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001686#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01001687 /* Looks redundant even for GTK2 because these values are
Bram Moolenaar98921892016-02-23 17:14:37 +01001688 * overwritten by gtk_selection_data_set() that follows. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 selection_data->type = selection_data->target;
1690 selection_data->format = 8; /* 8 bits per char */
Bram Moolenaar98921892016-02-23 17:14:37 +01001691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 gtk_selection_data_set(selection_data, type, 8, string, length);
1693 vim_free(string);
1694 }
1695}
1696
1697/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001698 * Check if the GUI can be started. Called before gvimrc is sourced and
1699 * before fork().
1700 * Return OK or FAIL.
1701 */
1702 int
Bram Moolenaar717e1962016-08-10 21:28:44 +02001703gui_mch_early_init_check(int give_message)
Bram Moolenaar29695702012-05-18 17:03:18 +02001704{
1705 char_u *p;
1706
1707 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1708 p = mch_getenv((char_u *)"DISPLAY");
1709 if (p == NULL || *p == NUL)
1710 {
1711 gui.dying = TRUE;
Bram Moolenaar717e1962016-08-10 21:28:44 +02001712 if (give_message)
1713 EMSG(_((char *)e_opendisp));
Bram Moolenaar29695702012-05-18 17:03:18 +02001714 return FAIL;
1715 }
1716 return OK;
1717}
1718
1719/*
1720 * Check if the GUI can be started. Called before gvimrc is sourced but after
1721 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 * Return OK or FAIL.
1723 */
1724 int
1725gui_mch_init_check(void)
1726{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001727#ifdef USE_GRESOURCE
1728 static int res_registered = FALSE;
1729
1730 if (!res_registered)
1731 {
1732 /* Call this function in the GUI process; otherwise, the resources
1733 * won't be available. Don't call it twice. */
1734 res_registered = TRUE;
1735 gui_gtk_register_resource();
1736 }
1737#endif
1738
Bram Moolenaarf80663f2016-04-05 21:56:06 +02001739#if GTK_CHECK_VERSION(3,10,0)
1740 /* Vim currently assumes that Gtk means X11, so it cannot use native Gtk
1741 * support for other backends such as Wayland. */
1742 gdk_set_allowed_backends ("x11");
1743#endif
1744
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#ifdef FEAT_GUI_GNOME
1746 if (gtk_socket_id == 0)
1747 using_gnome = 1;
1748#endif
1749
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001750 /* This defaults to argv[0], but we want it to match the name of the
1751 * shipped gvim.desktop so that Vim's windows can be associated with this
1752 * file. */
1753 g_set_prgname("gvim");
1754
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1756 if (!gtk_init_check(&gui_argc, &gui_argv))
1757 {
1758 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001759 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 return FAIL;
1761 }
1762
1763 return OK;
1764}
1765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766/****************************************************************************
1767 * Mouse handling callbacks
1768 */
1769
1770
1771static guint mouse_click_timer = 0;
1772static int mouse_timed_out = TRUE;
1773
1774/*
1775 * Timer used to recognize multiple clicks of the mouse button
1776 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001777 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778mouse_click_timer_cb(gpointer data)
1779{
1780 /* we don't use this information currently */
1781 int *timed_out = (int *) data;
1782
1783 *timed_out = TRUE;
1784 return FALSE; /* don't happen again */
1785}
1786
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001787static guint motion_repeat_timer = 0;
1788static int motion_repeat_offset = FALSE;
1789static timeout_cb_type motion_repeat_timer_cb(gpointer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
1791 static void
1792process_motion_notify(int x, int y, GdkModifierType state)
1793{
1794 int button;
1795 int_u vim_modifiers;
Bram Moolenaar98921892016-02-23 17:14:37 +01001796#if GTK_CHECK_VERSION(3,0,0)
1797 GtkAllocation allocation;
1798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
1800 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1801 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1802 GDK_BUTTON5_MASK))
1803 ? MOUSE_DRAG : ' ';
1804
1805 /* If our pointer is currently hidden, then we should show it. */
1806 gui_mch_mousehide(FALSE);
1807
1808 /* Just moving the rodent above the drawing area without any button
1809 * being pressed. */
1810 if (button != MOUSE_DRAG)
1811 {
1812 gui_mouse_moved(x, y);
1813 return;
1814 }
1815
1816 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001817 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar49325942007-05-10 19:19:59 +00001819 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
1823 * Auto repeat timer handling.
1824 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001825#if GTK_CHECK_VERSION(3,0,0)
1826 gtk_widget_get_allocation(gui.drawarea, &allocation);
1827
1828 if (x < 0 || y < 0
1829 || x >= allocation.width
1830 || y >= allocation.height)
1831#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if (x < 0 || y < 0
1833 || x >= gui.drawarea->allocation.width
1834 || y >= gui.drawarea->allocation.height)
Bram Moolenaar98921892016-02-23 17:14:37 +01001835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 {
1837
1838 int dx;
1839 int dy;
1840 int offshoot;
1841 int delay = 10;
1842
1843 /* Calculate the maximal distance of the cursor from the drawing area.
1844 * (offshoot can't become negative here!).
1845 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001846#if GTK_CHECK_VERSION(3,0,0)
1847 dx = x < 0 ? -x : x - allocation.width;
1848 dy = y < 0 ? -y : y - allocation.height;
1849#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1851 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01001852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 offshoot = dx > dy ? dx : dy;
1855
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001856 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 * distance of 127 pixels from the main window.
1858 *
1859 * One could think endlessly about the most ergonomic variant here.
1860 * For example it could make sense to calculate the distance from the
1861 * drags start instead...
1862 *
1863 * Maybe a parabolic interpolation would suite us better here too...
1864 */
1865 if (offshoot > 127)
1866 {
1867 /* 5 appears to be somehow near to my perceptual limits :-). */
1868 delay = 5;
1869 }
1870 else
1871 {
1872 delay = (130 * (127 - offshoot)) / 127 + 5;
1873 }
1874
1875 /* shoot again */
1876 if (!motion_repeat_timer)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001877 motion_repeat_timer = timeout_add(delay, motion_repeat_timer_cb,
1878 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880}
1881
Bram Moolenaar98921892016-02-23 17:14:37 +01001882#if GTK_CHECK_VERSION(3,0,0)
1883 static GdkDevice *
1884gui_gtk_get_pointer_device(GtkWidget *widget)
1885{
1886 GdkWindow * const win = gtk_widget_get_window(widget);
1887 GdkDisplay * const dpy = gdk_window_get_display(win);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001888# if GTK_CHECK_VERSION(3,20,0)
1889 GdkSeat * const seat = gdk_display_get_default_seat(dpy);
1890 return gdk_seat_get_pointer(seat);
1891# else
Bram Moolenaar98921892016-02-23 17:14:37 +01001892 GdkDeviceManager * const mngr = gdk_display_get_device_manager(dpy);
1893 return gdk_device_manager_get_client_pointer(mngr);
Bram Moolenaar30e12d22016-04-17 20:49:53 +02001894# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001895}
1896
1897 static GdkWindow *
1898gui_gtk_get_pointer(GtkWidget *widget,
1899 gint *x,
1900 gint *y,
1901 GdkModifierType *state)
1902{
1903 GdkWindow * const win = gtk_widget_get_window(widget);
1904 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1905 return gdk_window_get_device_position(win, dev , x, y, state);
1906}
1907
Bram Moolenaar2369c152016-03-04 23:08:25 +01001908# if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar98921892016-02-23 17:14:37 +01001909 static GdkWindow *
1910gui_gtk_window_at_position(GtkWidget *widget,
1911 gint *x,
1912 gint *y)
1913{
1914 GdkDevice * const dev = gui_gtk_get_pointer_device(widget);
1915 return gdk_device_get_window_at_position(dev, x, y);
1916}
Bram Moolenaar2369c152016-03-04 23:08:25 +01001917# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01001918#endif
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920/*
1921 * Timer used to recognize multiple clicks of the mouse button.
1922 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02001923 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001924motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925{
1926 int x;
1927 int y;
1928 GdkModifierType state;
1929
Bram Moolenaar98921892016-02-23 17:14:37 +01001930#if GTK_CHECK_VERSION(3,0,0)
1931 gui_gtk_get_pointer(gui.drawarea, &x, &y, &state);
1932#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935
1936 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1937 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1938 GDK_BUTTON5_MASK)))
1939 {
1940 motion_repeat_timer = 0;
1941 return FALSE;
1942 }
1943
1944 /* If there already is a mouse click in the input buffer, wait another
1945 * time (otherwise we would create a backlog of clicks) */
1946 if (vim_used_in_input_buf() > 10)
1947 return TRUE;
1948
1949 motion_repeat_timer = 0;
1950
1951 /*
1952 * Fake a motion event.
1953 * Trick: Pretend the mouse moved to the next character on every other
1954 * event, otherwise drag events will be discarded, because they are still
1955 * in the same character.
1956 */
1957 if (motion_repeat_offset)
1958 x += gui.char_width;
1959
1960 motion_repeat_offset = !motion_repeat_offset;
1961 process_motion_notify(x, y, state);
1962
1963 /* Don't happen again. We will get reinstalled in the synthetic event
1964 * if needed -- thus repeating should still work. */
1965 return FALSE;
1966}
1967
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001969motion_notify_event(GtkWidget *widget,
1970 GdkEventMotion *event,
1971 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972{
1973 if (event->is_hint)
1974 {
1975 int x;
1976 int y;
1977 GdkModifierType state;
1978
Bram Moolenaar98921892016-02-23 17:14:37 +01001979#if GTK_CHECK_VERSION(3,0,0)
1980 gui_gtk_get_pointer(widget, &x, &y, &state);
1981#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 gdk_window_get_pointer(widget->window, &x, &y, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01001983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 process_motion_notify(x, y, state);
1985 }
1986 else
1987 {
1988 process_motion_notify((int)event->x, (int)event->y,
1989 (GdkModifierType)event->state);
1990 }
1991
1992 return TRUE; /* handled */
1993}
1994
1995
1996/*
1997 * Mouse button handling. Note please that we are capturing multiple click's
1998 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1999 * This is due to the way the generic VIM code is recognizing multiple clicks.
2000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002002button_press_event(GtkWidget *widget,
2003 GdkEventButton *event,
2004 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005{
2006 int button;
2007 int repeated_click = FALSE;
2008 int x, y;
2009 int_u vim_modifiers;
2010
Bram Moolenaar20892c12011-06-26 04:49:00 +02002011 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 /* Make sure we have focus now we've been selected */
Bram Moolenaar98921892016-02-23 17:14:37 +01002014#if GTK_CHECK_VERSION(3,0,0)
2015 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2016#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 gtk_widget_grab_focus(widget);
2020
2021 /*
2022 * Don't let additional events about multiple clicks send by GTK to us
2023 * after the initial button press event confuse us.
2024 */
2025 if (event->type != GDK_BUTTON_PRESS)
2026 return FALSE;
2027
2028 x = event->x;
2029 y = event->y;
2030
2031 /* Handle multiple clicks */
2032 if (!mouse_timed_out && mouse_click_timer)
2033 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002034 timeout_remove(mouse_click_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 mouse_click_timer = 0;
2036 repeated_click = TRUE;
2037 }
2038
2039 mouse_timed_out = FALSE;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002040 mouse_click_timer = timeout_add(p_mouset, mouse_click_timer_cb,
2041 &mouse_timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042
2043 switch (event->button)
2044 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01002045 /* Keep in sync with gui_x11.c.
2046 * Buttons 4-7 are handled in scroll_event() */
2047 case 1: button = MOUSE_LEFT; break;
2048 case 2: button = MOUSE_MIDDLE; break;
2049 case 3: button = MOUSE_RIGHT; break;
2050 case 8: button = MOUSE_X1; break;
2051 case 9: button = MOUSE_X2; break;
2052 default:
2053 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055
2056#ifdef FEAT_XIM
2057 /* cancel any preediting */
2058 if (im_is_preediting())
2059 xim_reset();
2060#endif
2061
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002062 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063
2064 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 return TRUE;
2067}
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002070 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002073scroll_event(GtkWidget *widget,
2074 GdkEventScroll *event,
2075 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076{
2077 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002078 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079
Bram Moolenaar98921892016-02-23 17:14:37 +01002080#if GTK_CHECK_VERSION(3,0,0)
2081 if (gtk_socket_id != 0 && !gtk_widget_has_focus(widget))
2082#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01002084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 gtk_widget_grab_focus(widget);
2086
2087 switch (event->direction)
2088 {
2089 case GDK_SCROLL_UP:
2090 button = MOUSE_4;
2091 break;
2092 case GDK_SCROLL_DOWN:
2093 button = MOUSE_5;
2094 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002095 case GDK_SCROLL_LEFT:
2096 button = MOUSE_7;
2097 break;
2098 case GDK_SCROLL_RIGHT:
2099 button = MOUSE_6;
2100 break;
2101 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 return FALSE;
2103 }
2104
2105# ifdef FEAT_XIM
2106 /* cancel any preediting */
2107 if (im_is_preediting())
2108 xim_reset();
2109# endif
2110
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002111 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112
2113 gui_send_mouse_event(button, (int)event->x, (int)event->y,
2114 FALSE, vim_modifiers);
2115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 return TRUE;
2117}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002121button_release_event(GtkWidget *widget UNUSED,
2122 GdkEventButton *event,
2123 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124{
2125 int x, y;
2126 int_u vim_modifiers;
2127
Bram Moolenaar20892c12011-06-26 04:49:00 +02002128 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02002129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 /* Remove any motion "machine gun" timers used for automatic further
2131 extension of allocation areas if outside of the applications window
2132 area .*/
2133 if (motion_repeat_timer)
2134 {
Bram Moolenaar4ab79682017-08-27 14:50:47 +02002135 timeout_remove(motion_repeat_timer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 motion_repeat_timer = 0;
2137 }
2138
2139 x = event->x;
2140 y = event->y;
2141
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002142 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143
2144 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145
2146 return TRUE;
2147}
2148
2149
2150#ifdef FEAT_DND
2151/****************************************************************************
2152 * Drag aNd Drop support handlers.
2153 */
2154
2155/*
2156 * Count how many items there may be and separate them with a NUL.
2157 * Apparently the items are separated with \r\n. This is not documented,
2158 * thus be careful not to go past the end. Also allow separation with
2159 * NUL characters.
2160 */
2161 static int
2162count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2163{
2164 int i;
2165 char_u *p = out;
2166 int count = 0;
2167
2168 for (i = 0; i < len; ++i)
2169 {
2170 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2171 {
2172 if (p > out && p[-1] != NUL)
2173 {
2174 ++count;
2175 *p++ = NUL;
2176 }
2177 }
2178 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2179 {
2180 *p++ = hexhex2nr(raw + i + 1);
2181 i += 2;
2182 }
2183 else
2184 *p++ = raw[i];
2185 }
2186 if (p > out && p[-1] != NUL)
2187 {
2188 *p = NUL; /* last item didn't have \r or \n */
2189 ++count;
2190 }
2191 return count;
2192}
2193
2194/*
2195 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2196 * this process, URI which protocol is not "file:" are removed. Return
2197 * length of array (less than "max").
2198 */
2199 static int
2200filter_uri_list(char_u **outlist, int max, char_u *src)
2201{
2202 int i, j;
2203
2204 for (i = j = 0; i < max; ++i)
2205 {
2206 outlist[i] = NULL;
2207 if (STRNCMP(src, "file:", 5) == 0)
2208 {
2209 src += 5;
2210 if (STRNCMP(src, "//localhost", 11) == 0)
2211 src += 11;
2212 while (src[0] == '/' && src[1] == '/')
2213 ++src;
2214 outlist[j++] = vim_strsave(src);
2215 }
2216 src += STRLEN(src) + 1;
2217 }
2218 return j;
2219}
2220
2221 static char_u **
2222parse_uri_list(int *count, char_u *data, int len)
2223{
2224 int n = 0;
2225 char_u *tmp = NULL;
Bram Moolenaar945ec092016-06-08 21:17:43 +02002226 char_u **array = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2229 {
2230 n = count_and_decode_uri_list(tmp, data, len);
2231 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2232 n = filter_uri_list(array, n, tmp);
2233 }
2234 vim_free(tmp);
2235 *count = n;
2236 return array;
2237}
2238
2239 static void
2240drag_handle_uri_list(GdkDragContext *context,
2241 GtkSelectionData *data,
2242 guint time_,
2243 GdkModifierType state,
2244 gint x,
2245 gint y)
2246{
2247 char_u **fnames;
2248 int nfiles = 0;
2249
Bram Moolenaar98921892016-02-23 17:14:37 +01002250# if GTK_CHECK_VERSION(3,0,0)
2251 fnames = parse_uri_list(&nfiles,
2252 (char_u *)gtk_selection_data_get_data(data),
2253 gtk_selection_data_get_length(data));
2254# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 fnames = parse_uri_list(&nfiles, data->data, data->length);
Bram Moolenaar98921892016-02-23 17:14:37 +01002256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257
2258 if (fnames != NULL && nfiles > 0)
2259 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002260 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261
2262 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2263
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002264 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265
2266 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2267 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002268 else
2269 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270}
2271
2272 static void
2273drag_handle_text(GdkDragContext *context,
2274 GtkSelectionData *data,
2275 guint time_,
2276 GdkModifierType state)
2277{
2278 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2279 char_u *text;
2280 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
Bram Moolenaar98921892016-02-23 17:14:37 +01002283# if GTK_CHECK_VERSION(3,0,0)
2284 text = (char_u *)gtk_selection_data_get_data(data);
2285 len = gtk_selection_data_get_length(data);
2286# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 text = data->data;
2288 len = data->length;
Bram Moolenaar98921892016-02-23 17:14:37 +01002289# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
Bram Moolenaar98921892016-02-23 17:14:37 +01002291# if GTK_CHECK_VERSION(3,0,0)
2292 if (gtk_selection_data_get_data_type(data) == utf8_string_atom)
2293# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (data->type == utf8_string_atom)
Bram Moolenaar98921892016-02-23 17:14:37 +01002295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (input_conv.vc_type != CONV_NONE)
2298 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 if (tmpbuf != NULL)
2300 text = tmpbuf;
2301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303 dnd_yank_drag_data(text, (long)len);
2304 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002307 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
2309 if (dropkey[2] != 0)
2310 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2311 else
2312 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313}
2314
2315/*
2316 * DND receiver.
2317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 static void
2319drag_data_received_cb(GtkWidget *widget,
2320 GdkDragContext *context,
2321 gint x,
2322 gint y,
2323 GtkSelectionData *data,
2324 guint info,
2325 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002326 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 GdkModifierType state;
2329
2330 /* Guard against trash */
Bram Moolenaar98921892016-02-23 17:14:37 +01002331# if GTK_CHECK_VERSION(3,0,0)
2332 const guchar * const data_data = gtk_selection_data_get_data(data);
2333 const gint data_length = gtk_selection_data_get_length(data);
2334 const gint data_format = gtk_selection_data_get_format(data);
2335
2336 if (data_data == NULL
2337 || data_length <= 0
2338 || data_format != 8
2339 || data_data[data_length] != '\0')
2340# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 if (data->data == NULL
2342 || data->length <= 0
2343 || data->format != 8
2344 || data->data[data->length] != '\0')
Bram Moolenaar98921892016-02-23 17:14:37 +01002345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 gtk_drag_finish(context, FALSE, FALSE, time_);
2348 return;
2349 }
2350
2351 /* Get the current modifier state for proper distinguishment between
2352 * different operations later. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002353#if GTK_CHECK_VERSION(3,0,0)
2354 gui_gtk_get_pointer(widget, NULL, NULL, &state);
2355# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
Bram Moolenaar98921892016-02-23 17:14:37 +01002357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358
2359 /* Not sure about the role of "text/plain" here... */
2360 if (info == (guint)TARGET_TEXT_URI_LIST)
2361 drag_handle_uri_list(context, data, time_, state, x, y);
2362 else
2363 drag_handle_text(context, data, time_, state);
2364
2365}
2366#endif /* FEAT_DND */
2367
2368
2369#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2370/*
2371 * GnomeClient interact callback. Check for unsaved buffers that cannot
2372 * be abandoned and pop up a dialog asking the user for confirmation if
2373 * necessary.
2374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002376sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002378 GnomeDialogType type UNUSED,
2379 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380{
2381 cmdmod_T save_cmdmod;
2382 gboolean shutdown_cancelled;
2383
2384 save_cmdmod = cmdmod;
2385
2386# ifdef FEAT_BROWSE
2387 cmdmod.browse = TRUE;
2388# endif
2389# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2390 cmdmod.confirm = TRUE;
2391# endif
2392 /*
2393 * If there are changed buffers, present the user with
2394 * a dialog if possible, otherwise give an error message.
2395 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002396 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 exiting = FALSE;
2399 cmdmod = save_cmdmod;
2400 setcursor(); /* position the cursor */
2401 out_flush();
2402 /*
2403 * If the user hit the [Cancel] button the whole shutdown
2404 * will be cancelled. Wow, quite powerful feature (:
2405 */
2406 gnome_interaction_key_return(key, shutdown_cancelled);
2407}
2408
2409/*
2410 * Generate a script that can be used to restore the current editing session.
2411 * Save the value of v:this_session before running :mksession in order to make
2412 * automagic session save fully transparent. Return TRUE on success.
2413 */
2414 static int
2415write_session_file(char_u *filename)
2416{
2417 char_u *escaped_filename;
2418 char *mksession_cmdline;
2419 unsigned int save_ssop_flags;
2420 int failed;
2421
2422 /*
2423 * Build an ex command line to create a script that restores the current
2424 * session if executed. Escape the filename to avoid nasty surprises.
2425 */
2426 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2427 if (escaped_filename == NULL)
2428 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002429 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2430 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 /*
2434 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2435 * unpredictable effects when the session is saved automatically. Also,
2436 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2437 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2438 * enormously large if the GNOME session feature is used regularly.
2439 */
2440 save_ssop_flags = ssop_flags;
2441 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002442 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2445 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2446 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002447 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448
2449 ssop_flags = save_ssop_flags;
2450 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002451
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 /*
2453 * Reopen the file and append a command to restore v:this_session,
2454 * as if this save never happened. This is to avoid conflicts with
2455 * the user's own sessions. FIXME: It's probably less hackish to add
2456 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2457 */
2458 if (!failed)
2459 {
2460 FILE *fd;
2461
2462 fd = open_exfile(filename, TRUE, APPENDBIN);
2463
2464 failed = (fd == NULL
2465 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2466 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2467
2468 if (fd != NULL && fclose(fd) != 0)
2469 failed = TRUE;
2470
2471 if (failed)
2472 mch_remove(filename);
2473 }
2474
2475 return !failed;
2476}
2477
2478/*
2479 * "save_yourself" signal handler. Initiate an interaction to ask the user
2480 * for confirmation if necessary. Save the current editing session and tell
2481 * the session manager how to restart Vim.
2482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 static gboolean
2484sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002485 gint phase UNUSED,
2486 GnomeSaveStyle save_style UNUSED,
2487 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002489 gboolean fast UNUSED,
2490 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
2492 static const char suffix[] = "-session.vim";
2493 char *session_file;
2494 unsigned int len;
2495 gboolean success;
2496
2497 /* Always request an interaction if possible. check_changed_any()
2498 * won't actually show a dialog unless any buffers have been modified.
2499 * There doesn't seem to be an obvious way to check that without
2500 * automatically firing the dialog. Anyway, it works just fine. */
2501 if (interact_style == GNOME_INTERACT_ANY)
2502 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2503 &sm_client_check_changed_any,
2504 NULL);
2505 out_flush();
2506 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2507
2508 /* The path is unique for each session save. We do neither know nor care
2509 * which session script will actually be used later. This decision is in
2510 * the domain of the session manager. */
2511 session_file = gnome_config_get_real_path(
2512 gnome_client_get_config_prefix(client));
2513 len = strlen(session_file);
2514
2515 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2516 --len; /* get rid of the superfluous trailing '/' */
2517
2518 session_file = g_renew(char, session_file, len + sizeof(suffix));
2519 memcpy(session_file + len, suffix, sizeof(suffix));
2520
2521 success = write_session_file((char_u *)session_file);
2522
2523 if (success)
2524 {
2525 const char *argv[8];
2526 int i;
2527
2528 /* Tell the session manager how to wipe out the stored session data.
2529 * This isn't as dangerous as it looks, don't worry :) session_file
2530 * is a unique absolute filename. Usually it'll be something like
2531 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2532 i = 0;
2533 argv[i++] = "rm";
2534 argv[i++] = session_file;
2535 argv[i] = NULL;
2536
2537 gnome_client_set_discard_command(client, i, (char **)argv);
2538
2539 /* Tell the session manager how to restore the just saved session.
2540 * This is easily done thanks to Vim's -S option. Pass the -f flag
2541 * since there's no need to fork -- it might even cause confusion.
2542 * Also pass the window role to give the WM something to match on.
2543 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2544 i = 0;
2545 argv[i++] = restart_command;
2546 argv[i++] = "-f";
2547 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 argv[i++] = "--role";
2549 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 argv[i++] = "-S";
2551 argv[i++] = session_file;
2552 argv[i] = NULL;
2553
2554 gnome_client_set_restart_command(client, i, (char **)argv);
2555 gnome_client_set_clone_command(client, 0, NULL);
2556 }
2557
2558 g_free(session_file);
2559
2560 return success;
2561}
2562
2563/*
2564 * Called when the session manager wants us to die. There isn't much to save
2565 * here since "save_yourself" has been emitted before (unless serious trouble
2566 * is happening).
2567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002569sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 /* Don't write messages to the GUI anymore */
2572 full_screen = FALSE;
2573
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002574 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002575 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002576 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 preserve_exit();
2578}
2579
2580/*
2581 * Connect our signal handlers to be notified on session save and shutdown.
2582 */
2583 static void
2584setup_save_yourself(void)
2585{
2586 GnomeClient *client;
2587
2588 client = gnome_master_client();
2589
2590 if (client != NULL)
2591 {
2592 /* Must use the deprecated gtk_signal_connect() for compatibility
2593 * with GNOME 1. Arrgh, zombies! */
2594 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2595 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2596 gtk_signal_connect(GTK_OBJECT(client), "die",
2597 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2598 }
2599}
2600
2601#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2602
2603# ifdef USE_XSMP
2604/*
2605 * GTK tells us that XSMP needs attention
2606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002608local_xsmp_handle_requests(
2609 GIOChannel *source UNUSED,
2610 GIOCondition condition,
2611 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612{
2613 if (condition == G_IO_IN)
2614 {
2615 /* Do stuff; maybe close connection */
2616 if (xsmp_handle_requests() == FAIL)
2617 g_io_channel_unref((GIOChannel *)data);
2618 return TRUE;
2619 }
2620 /* Error */
2621 g_io_channel_unref((GIOChannel *)data);
2622 xsmp_close();
2623 return TRUE;
2624}
2625# endif /* USE_XSMP */
2626
2627/*
2628 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2629 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2630 */
2631 static void
2632setup_save_yourself(void)
2633{
2634 Atom *existing_atoms = NULL;
2635 int count = 0;
2636
Bram Moolenaar98921892016-02-23 17:14:37 +01002637# ifdef USE_XSMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (xsmp_icefd != -1)
2639 {
2640 /*
2641 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2642 * set up GTK IO monitor
2643 */
2644 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2645
2646 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2647 local_xsmp_handle_requests, (gpointer)g_io);
Bram Moolenaar98921892016-02-23 17:14:37 +01002648 g_io_channel_unref(g_io);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002651# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
2653 /* Fall back to old method */
2654
2655 /* first get the existing value */
Bram Moolenaar98921892016-02-23 17:14:37 +01002656# if GTK_CHECK_VERSION(3,0,0)
2657 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2658
2659 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2660 GDK_WINDOW_XID(mainwin_win),
2661 &existing_atoms, &count))
2662# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2664 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2665 &existing_atoms, &count))
Bram Moolenaar98921892016-02-23 17:14:37 +01002666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 {
2668 Atom *new_atoms;
2669 Atom save_yourself_xatom;
2670 int i;
2671
2672 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2673
2674 /* check if WM_SAVE_YOURSELF isn't there yet */
2675 for (i = 0; i < count; ++i)
2676 if (existing_atoms[i] == save_yourself_xatom)
2677 break;
2678
2679 if (i == count)
2680 {
2681 /* allocate an Atoms array which is one item longer */
2682 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2683 * sizeof(Atom)));
2684 if (new_atoms != NULL)
2685 {
2686 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2687 new_atoms[count] = save_yourself_xatom;
Bram Moolenaar98921892016-02-23 17:14:37 +01002688# if GTK_CHECK_VERSION(3,0,0)
2689 XSetWMProtocols(GDK_WINDOW_XDISPLAY(mainwin_win),
2690 GDK_WINDOW_XID(mainwin_win),
2691# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2693 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 new_atoms, count + 1);
2696 vim_free(new_atoms);
2697 }
2698 }
2699 XFree(existing_atoms);
2700 }
2701 }
2702}
2703
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704/*
2705 * Installing a global event filter seems to be the only way to catch
2706 * client messages of type WM_PROTOCOLS without overriding GDK's own
2707 * client message event filter. Well, that's still better than trying
2708 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 *
2710 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2711 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2712 * I have the nasty feeling modern session managers just don't send this
2713 * deprecated message anymore. Addition: confirmed by several people.
2714 *
2715 * The GNOME session support is much cooler anyway. Unlike this ugly
2716 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2717 * it should work with KDE as well.
2718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002720global_event_filter(GdkXEvent *xev,
2721 GdkEvent *event UNUSED,
2722 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 XEvent *xevent = (XEvent *)xev;
2725
2726 if (xevent != NULL
2727 && xevent->type == ClientMessage
2728 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002729 && (long_u)xevent->xclient.data.l[0]
2730 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
2732 out_flush();
2733 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2734 /*
2735 * Set the window's WM_COMMAND property, to let the window manager
2736 * know we are done saving ourselves. We don't want to be
2737 * restarted, thus set argv to NULL.
2738 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002739# if GTK_CHECK_VERSION(3,0,0)
2740 XSetCommand(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
2741 GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin)),
2742# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2744 GDK_WINDOW_XWINDOW(gui.mainwin->window),
Bram Moolenaar98921892016-02-23 17:14:37 +01002745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 NULL, 0);
2747 return GDK_FILTER_REMOVE;
2748 }
2749
2750 return GDK_FILTER_CONTINUE;
2751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2753
2754
2755/*
2756 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002759mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760{
2761/* If you get an error message here, you still need to unpack the runtime
2762 * archive! */
2763#ifdef magick
2764# undef magick
2765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 /* A bit hackish, but avoids casting later and allows optimization */
2767# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768#define magick vim32x32
2769#include "../runtime/vim32x32.xpm"
2770#undef magick
2771#define magick vim16x16
2772#include "../runtime/vim16x16.xpm"
2773#undef magick
2774#define magick vim48x48
2775#include "../runtime/vim48x48.xpm"
2776#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
Bram Moolenaar98921892016-02-23 17:14:37 +01002779#if GTK_CHECK_VERSION(3,0,0)
2780 GdkWindow * const mainwin_win = gtk_widget_get_window(gui.mainwin);
2781#endif
2782
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 /* When started with "--echo-wid" argument, write window ID on stdout. */
2784 if (echo_wid_arg)
2785 {
Bram Moolenaar98921892016-02-23 17:14:37 +01002786#if GTK_CHECK_VERSION(3,0,0)
2787 printf("WID: %ld\n", (long)GDK_WINDOW_XID(mainwin_win));
2788#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002790#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 fflush(stdout);
2792 }
2793
2794 if (vim_strchr(p_go, GO_ICON) != NULL)
2795 {
2796 /*
2797 * Add an icon to the main window. For fun and convenience of the user.
2798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 GList *icons = NULL;
2800
2801 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2802 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2803 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2804
2805 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2806
2807 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2808 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
2810
2811#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2812 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814#endif
2815 /* Setup to indicate to the window manager that we want to catch the
2816 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2817 * manager instead. */
2818#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2819 if (using_gnome)
2820#endif
2821 setup_save_yourself();
2822
2823#ifdef FEAT_CLIENTSERVER
2824 if (serverName == NULL && serverDelayedStartName != NULL)
2825 {
2826 /* This is a :gui command in a plain vim with no previous server */
Bram Moolenaar98921892016-02-23 17:14:37 +01002827# if GTK_CHECK_VERSION(3,0,0)
2828 commWindow = GDK_WINDOW_XID(mainwin_win);
2829
2830 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(mainwin_win),
2831 serverDelayedStartName);
2832# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2834
2835 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2836 serverDelayedStartName);
Bram Moolenaar98921892016-02-23 17:14:37 +01002837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
2839 else
2840 {
2841 /*
2842 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2843 * have to change the "server" registration to that of the main window
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002844 * If we have not registered a name yet, remember the window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002846# if GTK_CHECK_VERSION(3,0,0)
2847 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(mainwin_win),
2848 GDK_WINDOW_XID(mainwin_win));
2849# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2851 GDK_WINDOW_XWINDOW(gui.mainwin->window));
Bram Moolenaar98921892016-02-23 17:14:37 +01002852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
2854 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01002855# if GTK_CHECK_VERSION(3,0,0)
2856 g_signal_connect(G_OBJECT(gui.mainwin), "property-notify-event",
2857 G_CALLBACK(property_event), NULL);
2858# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2860 GTK_SIGNAL_FUNC(property_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01002861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862#endif
2863}
2864
2865 static GdkCursor *
2866create_blank_pointer(void)
2867{
2868 GdkWindow *root_window = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01002869#if GTK_CHECK_VERSION(3,0,0)
2870 GdkPixbuf *blank_mask;
2871#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 GdkPixmap *blank_mask;
Bram Moolenaar98921892016-02-23 17:14:37 +01002873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 GdkCursor *cursor;
Bram Moolenaar36edf062016-07-21 22:10:12 +02002875#if GTK_CHECK_VERSION(3,0,0)
2876 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
2877#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 GdkColor color = { 0, 0, 0, 0 };
2879 char blank_data[] = { 0x0 };
Bram Moolenaar98921892016-02-23 17:14:37 +01002880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002882#if GTK_CHECK_VERSION(3,12,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002883 {
2884 GdkWindow * const win = gtk_widget_get_window(gui.mainwin);
2885 GdkScreen * const scrn = gdk_window_get_screen(win);
2886 root_window = gdk_screen_get_root_window(scrn);
2887 }
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002888#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 root_window = gtk_widget_get_root_window(gui.mainwin);
2890#endif
2891
2892 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2893 * in size. */
Bram Moolenaar98921892016-02-23 17:14:37 +01002894#if GTK_CHECK_VERSION(3,0,0)
2895 {
2896 cairo_surface_t *surf;
2897 cairo_t *cr;
2898
2899 surf = cairo_image_surface_create(CAIRO_FORMAT_A1, 1, 1);
2900 cr = cairo_create(surf);
2901
Bram Moolenaar36edf062016-07-21 22:10:12 +02002902 cairo_set_source_rgba(cr,
2903 color.red,
2904 color.green,
2905 color.blue,
2906 color.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01002907 cairo_rectangle(cr, 0, 0, 1, 1);
2908 cairo_fill(cr);
2909 cairo_destroy(cr);
2910
2911 blank_mask = gdk_pixbuf_get_from_surface(surf, 0, 0, 1, 1);
2912 cairo_surface_destroy(surf);
2913
2914 cursor = gdk_cursor_new_from_pixbuf(gdk_window_get_display(root_window),
2915 blank_mask, 0, 0);
2916 g_object_unref(blank_mask);
2917 }
2918#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2920 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2921 &color, &color, 0, 0);
2922 gdk_bitmap_unref(blank_mask);
Bram Moolenaar98921892016-02-23 17:14:37 +01002923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924
2925 return cursor;
2926}
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 static void
2929mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002930 GdkScreen *previous_screen UNUSED,
2931 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932{
2933 if (!gtk_widget_has_screen(widget))
2934 return;
2935
2936 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002937 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 */
2939 if (gui.blank_pointer != NULL)
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002940#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002941 g_object_unref(G_OBJECT(gui.blank_pointer));
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002942#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
2946 gui.blank_pointer = create_blank_pointer();
2947
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002948#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01002949 if (gui.pointer_hidden && gtk_widget_get_window(gui.drawarea) != NULL)
2950 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
2951 gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002952#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2954 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02002955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956
2957 /*
2958 * Create a new PangoContext for this screen, and initialize it
2959 * with the current font if necessary.
2960 */
2961 if (gui.text_context != NULL)
2962 g_object_unref(gui.text_context);
2963
2964 gui.text_context = gtk_widget_create_pango_context(widget);
2965 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2966
2967 if (gui.norm_font != NULL)
2968 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002969 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar8ac44152017-11-09 18:33:29 +01002970 gui_set_shellsize(TRUE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973
2974/*
2975 * After the drawing area comes up, we calculate all colors and create the
2976 * dummy blank cursor.
2977 *
2978 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2979 * fact that the main VIM engine doesn't take them into account anywhere.
2980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002982drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983{
2984 GtkWidget *sbar;
Bram Moolenaar98921892016-02-23 17:14:37 +01002985#if GTK_CHECK_VERSION(3,0,0)
2986 GtkAllocation allocation;
2987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
2989#ifdef FEAT_XIM
2990 xim_init();
2991#endif
2992 gui_mch_new_colors();
Bram Moolenaar98921892016-02-23 17:14:37 +01002993#if GTK_CHECK_VERSION(3,0,0)
2994 gui.surface = gdk_window_create_similar_surface(
2995 gtk_widget_get_window(widget),
2996 CAIRO_CONTENT_COLOR_ALPHA,
2997 gtk_widget_get_allocated_width(widget),
2998 gtk_widget_get_allocated_height(widget));
2999#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 gui.text_gc = gdk_gc_new(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01003001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002
3003 gui.blank_pointer = create_blank_pointer();
3004 if (gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01003005#if GTK_CHECK_VERSION(3,0,0)
3006 gdk_window_set_cursor(gtk_widget_get_window(widget), gui.blank_pointer);
3007#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 gdk_window_set_cursor(widget->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 /* get the actual size of the scrollbars, if they are realized */
3012 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
3013 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
3014 && firstwin->w_scrollbars[SBAR_RIGHT].id))
3015 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003016#if GTK_CHECK_VERSION(3,0,0)
3017 gtk_widget_get_allocation(sbar, &allocation);
3018 if (sbar && gtk_widget_get_realized(sbar) && allocation.width)
3019 gui.scrollbar_width = allocation.width;
3020#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
3022 gui.scrollbar_width = sbar->allocation.width;
Bram Moolenaar98921892016-02-23 17:14:37 +01003023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
3025 sbar = gui.bottom_sbar.id;
Bram Moolenaar98921892016-02-23 17:14:37 +01003026#if GTK_CHECK_VERSION(3,0,0)
3027 if (sbar && gtk_widget_get_realized(sbar) && allocation.height)
3028 gui.scrollbar_height = allocation.height;
3029#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
3031 gui.scrollbar_height = sbar->allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033}
3034
3035/*
3036 * Properly clean up on shutdown.
3037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003039drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 /* Don't write messages to the GUI anymore */
3042 full_screen = FALSE;
3043
3044#ifdef FEAT_XIM
3045 im_shutdown();
3046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 if (gui.ascii_glyphs != NULL)
3048 {
3049 pango_glyph_string_free(gui.ascii_glyphs);
3050 gui.ascii_glyphs = NULL;
3051 }
3052 if (gui.ascii_font != NULL)
3053 {
3054 g_object_unref(gui.ascii_font);
3055 gui.ascii_font = NULL;
3056 }
3057 g_object_unref(gui.text_context);
3058 gui.text_context = NULL;
3059
Bram Moolenaar98921892016-02-23 17:14:37 +01003060#if GTK_CHECK_VERSION(3,0,0)
3061 if (gui.surface != NULL)
3062 {
3063 cairo_surface_destroy(gui.surface);
3064 gui.surface = NULL;
3065 }
3066#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 g_object_unref(gui.text_gc);
3068 gui.text_gc = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +01003069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070
Bram Moolenaar98921892016-02-23 17:14:37 +01003071#if GTK_CHECK_VERSION(3,0,0)
3072 g_object_unref(G_OBJECT(gui.blank_pointer));
3073#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 gdk_cursor_unref(gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01003075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077}
3078
Bram Moolenaara859f042016-11-17 19:11:55 +01003079#if GTK_CHECK_VERSION(3,22,2)
3080 static void
3081drawarea_style_updated_cb(GtkWidget *widget UNUSED,
3082 gpointer data UNUSED)
3083#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003085drawarea_style_set_cb(GtkWidget *widget UNUSED,
3086 GtkStyle *previous_style UNUSED,
3087 gpointer data UNUSED)
Bram Moolenaara859f042016-11-17 19:11:55 +01003088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 gui_mch_new_colors();
3091}
3092
Bram Moolenaar98921892016-02-23 17:14:37 +01003093#if GTK_CHECK_VERSION(3,0,0)
3094 static gboolean
3095drawarea_configure_event_cb(GtkWidget *widget,
3096 GdkEventConfigure *event,
3097 gpointer data UNUSED)
3098{
3099 static int cur_width = 0;
3100 static int cur_height = 0;
3101
3102 g_return_val_if_fail(event
3103 && event->width >= 1 && event->height >= 1, TRUE);
3104
Bram Moolenaar182707a2016-11-21 20:55:58 +01003105# if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01003106 /* As of 3.22.2, GdkWindows have started distributing configure events to
3107 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
3108 *
3109 * As can be seen from the implementation of move_native_children() and
3110 * configure_native_child() in gdkwindow.c, those functions actually
3111 * propagate configure events to every child, failing to distinguish
3112 * "native" one from non-native one.
3113 *
3114 * Naturally, configure events propagated to here like that are fallacious
3115 * and, as a matter of fact, they trigger a geometric collapse of
3116 * gui.drawarea in fullscreen and miximized modes.
3117 *
3118 * To filter out such nuisance events, we are making use of the fact that
3119 * the field send_event of such GdkEventConfigures is set to FALSE in
3120 * configure_native_child().
3121 *
3122 * Obviously, this is a terrible hack making GVim depend on GTK's
3123 * implementation details. Therefore, watch out any relevant internal
3124 * changes happening in GTK in the feature (sigh).
3125 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01003126 /* Follow-up
3127 * After a few weeks later, the GdkWindow change mentioned above was
3128 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
3129 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01003130 if (event->send_event == FALSE)
3131 return TRUE;
3132# endif
3133
Bram Moolenaar98921892016-02-23 17:14:37 +01003134 if (event->width == cur_width && event->height == cur_height)
3135 return TRUE;
3136
3137 cur_width = event->width;
3138 cur_height = event->height;
3139
3140 if (gui.surface != NULL)
3141 cairo_surface_destroy(gui.surface);
3142
3143 gui.surface = gdk_window_create_similar_surface(
3144 gtk_widget_get_window(widget),
3145 CAIRO_CONTENT_COLOR_ALPHA,
3146 event->width, event->height);
3147
3148 gtk_widget_queue_draw(widget);
3149
3150 return TRUE;
3151}
3152#endif
3153
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154/*
3155 * Callback routine for the "delete_event" signal on the toplevel window.
3156 * Tries to vim gracefully, or refuses to exit with changed buffers.
3157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003159delete_event_cb(GtkWidget *widget UNUSED,
3160 GdkEventAny *event UNUSED,
3161 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162{
3163 gui_shell_closed();
3164 return TRUE;
3165}
3166
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003167#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
3168 static int
3169get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
3170{
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003171# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003172 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
3173
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003174 if (using_gnome && widget != NULL)
3175 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003176 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003177 BonoboDockItem *dockitem;
3178
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003179 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003180 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
3181 {
3182 /* Only menu & toolbar are dock items. Could tabline be?
3183 * Seem to be only the 2 defined in GNOME */
3184 widget = parent;
3185 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003186
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003187 if (dockitem == NULL || dockitem->is_floating)
3188 return 0;
3189 item_orientation = bonobo_dock_item_get_orientation(dockitem);
3190 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003191 }
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003192# else
3193# define item_orientation GTK_ORIENTATION_HORIZONTAL
Bram Moolenaar98921892016-02-23 17:14:37 +01003194# endif
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003195
Bram Moolenaar98921892016-02-23 17:14:37 +01003196# if GTK_CHECK_VERSION(3,0,0)
3197 if (widget != NULL
3198 && item_orientation == orientation
3199 && gtk_widget_get_realized(widget)
3200 && gtk_widget_get_visible(widget))
3201# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003202 if (widget != NULL
3203 && item_orientation == orientation
3204 && GTK_WIDGET_REALIZED(widget)
3205 && GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01003206# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003207 {
Bram Moolenaar98921892016-02-23 17:14:37 +01003208# if GTK_CHECK_VERSION(3,0,0)
3209 GtkAllocation allocation;
3210
3211 gtk_widget_get_allocation(widget, &allocation);
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003212 return allocation.height;
Bram Moolenaar98921892016-02-23 17:14:37 +01003213# else
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003214# ifdef FEAT_GUI_GNOME
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003215 if (orientation == GTK_ORIENTATION_HORIZONTAL)
3216 return widget->allocation.height;
3217 else
3218 return widget->allocation.width;
Bram Moolenaarc4a249a2017-01-30 22:56:48 +01003219# else
3220 return widget->allocation.height;
3221# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01003222# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003223 }
3224 return 0;
3225}
3226#endif
3227
3228 static int
3229get_menu_tool_width(void)
3230{
3231 int width = 0;
3232
3233#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
3234# ifdef FEAT_MENU
3235 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
3236# endif
3237# ifdef FEAT_TOOLBAR
3238 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
3239# endif
3240# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003241 if (gui.tabline != NULL)
3242 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003243# endif
3244#endif
3245
3246 return width;
3247}
3248
3249 static int
3250get_menu_tool_height(void)
3251{
3252 int height = 0;
3253
3254#ifdef FEAT_MENU
3255 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
3256#endif
3257#ifdef FEAT_TOOLBAR
3258 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3259#endif
3260#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003261 if (gui.tabline != NULL)
3262 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003263#endif
3264
3265 return height;
3266}
3267
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003268/* This controls whether we can set the real window hints at
3269 * start-up when in a GtkPlug.
3270 * 0 = normal processing (default)
3271 * 1 = init. hints set, no-one's tried to reset since last check
3272 * 2 = init. hints set, attempt made to change hints
3273 */
3274static int init_window_hints_state = 0;
3275
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003276 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003277update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003278{
3279 static int old_width = 0;
3280 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003281 static int old_min_width = 0;
3282 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003283 static int old_char_width = 0;
3284 static int old_char_height = 0;
3285
3286 int width;
3287 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003288 int min_width;
3289 int min_height;
3290
3291 /* At start-up, don't try to set the hints until the initial
3292 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003293 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003294 */
3295 if (!(force_width && force_height) && init_window_hints_state > 0)
3296 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003297 /* Don't do it! */
3298 init_window_hints_state = 2;
3299 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003300 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003301
3302 /* This also needs to be done when the main window isn't there yet,
3303 * otherwise the hints don't work. */
3304 width = gui_get_base_width();
3305 height = gui_get_base_height();
3306# ifdef FEAT_MENU
3307 height += tabline_height() * gui.char_height;
3308# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003309 width += get_menu_tool_width();
3310 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003311
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003312 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00003313 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003314 * 'set columns=' or init. -geom) we briefly set the min. to the size
3315 * we wish to be instead of the legitimate minimum so that we actually
3316 * resize correctly.
3317 */
3318 if (force_width && force_height)
3319 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003320 min_width = force_width;
3321 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003322 }
3323 else
3324 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003325 min_width = width + MIN_COLUMNS * gui.char_width;
3326 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003327 }
3328
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003329 /* Avoid an expose event when the size didn't change. */
3330 if (width != old_width
3331 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003332 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003333 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003334 || gui.char_width != old_char_width
3335 || gui.char_height != old_char_height)
3336 {
3337 GdkGeometry geometry;
3338 GdkWindowHints geometry_mask;
3339
3340 geometry.width_inc = gui.char_width;
3341 geometry.height_inc = gui.char_height;
3342 geometry.base_width = width;
3343 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003344 geometry.min_width = min_width;
3345 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003346 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3347 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003348 /* Using gui.formwin as geometry widget doesn't work as expected
3349 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3350 * in Vim confuse GTK+. */
3351 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3352 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003353 old_width = width;
3354 old_height = height;
3355 old_min_width = min_width;
3356 old_min_height = min_height;
3357 old_char_width = gui.char_width;
3358 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003359 }
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#ifdef FEAT_TOOLBAR
3363
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364/*
3365 * This extra effort wouldn't be necessary if we only used stock icons in the
3366 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3367 * shouldn't be treated differently, thus we do need this.
3368 */
3369 static void
3370icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3371{
3372 if (GTK_IS_IMAGE(widget))
3373 {
3374 GtkImage *image = (GtkImage *)widget;
3375
Bram Moolenaar98921892016-02-23 17:14:37 +01003376# if GTK_CHECK_VERSION(3,10,0)
3377 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_NAME)
3378 {
3379 const GtkIconSize icon_size = GPOINTER_TO_INT(user_data);
3380 const gchar *icon_name;
3381
3382 gtk_image_get_icon_name(image, &icon_name, NULL);
3383
3384 gtk_image_set_from_icon_name(image, icon_name, icon_size);
3385 }
3386# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 /* User-defined icons are stored in a GtkIconSet */
3388 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3389 {
3390 GtkIconSet *icon_set;
3391 GtkIconSize icon_size;
3392
3393 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3394 icon_size = (GtkIconSize)(long)user_data;
3395
3396 gtk_icon_set_ref(icon_set);
3397 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3398 gtk_icon_set_unref(icon_set);
3399 }
Bram Moolenaar98921892016-02-23 17:14:37 +01003400# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
3402 else if (GTK_IS_CONTAINER(widget))
3403 {
3404 gtk_container_foreach((GtkContainer *)widget,
3405 &icon_size_changed_foreach,
3406 user_data);
3407 }
3408}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409
3410 static void
3411set_toolbar_style(GtkToolbar *toolbar)
3412{
3413 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 GtkIconSize size;
3415 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3418 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3419 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003420 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3422 style = GTK_TOOLBAR_BOTH;
3423 else if (toolbar_flags & TOOLBAR_TEXT)
3424 style = GTK_TOOLBAR_TEXT;
3425 else
3426 style = GTK_TOOLBAR_ICONS;
3427
3428 gtk_toolbar_set_style(toolbar, style);
Bram Moolenaar98921892016-02-23 17:14:37 +01003429# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003431# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 switch (tbis_flags)
3434 {
3435 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3436 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3437 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3438 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
Bram Moolenaarbeb003b2016-03-08 22:47:17 +01003439 case TBIS_HUGE: size = GTK_ICON_SIZE_DND; break;
3440 case TBIS_GIANT: size = GTK_ICON_SIZE_DIALOG; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 default: size = GTK_ICON_SIZE_INVALID; break;
3442 }
3443 oldsize = gtk_toolbar_get_icon_size(toolbar);
3444
3445 if (size == GTK_ICON_SIZE_INVALID)
3446 {
3447 /* Let global user preferences decide the icon size. */
3448 gtk_toolbar_unset_icon_size(toolbar);
3449 size = gtk_toolbar_get_icon_size(toolbar);
3450 }
3451 if (size != oldsize)
3452 {
3453 gtk_container_foreach(GTK_CONTAINER(toolbar),
3454 &icon_size_changed_foreach,
3455 GINT_TO_POINTER((int)size));
3456 }
3457 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458}
3459
3460#endif /* FEAT_TOOLBAR */
3461
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003462#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3463static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003464static GtkWidget *tabline_menu;
Bram Moolenaar98921892016-02-23 17:14:37 +01003465# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003466static GtkTooltips *tabline_tooltip;
Bram Moolenaar98921892016-02-23 17:14:37 +01003467# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003468static int clicked_page; /* page clicked in tab line */
3469
3470/*
3471 * Handle selecting an item in the tab line popup menu.
3472 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003473 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003474tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003475{
Bram Moolenaara5621492006-02-25 21:55:24 +00003476 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003477 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003478}
3479
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003480 static void
3481add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3482{
3483 GtkWidget *item;
3484 char_u *utf_text;
3485
3486 utf_text = CONVERT_TO_UTF8(text);
3487 item = gtk_menu_item_new_with_label((const char *)utf_text);
3488 gtk_widget_show(item);
3489 CONVERT_TO_UTF8_FREE(utf_text);
3490
3491 gtk_container_add(GTK_CONTAINER(menu), item);
Bram Moolenaar98921892016-02-23 17:14:37 +01003492# if GTK_CHECK_VERSION(3,0,0)
3493 g_signal_connect(G_OBJECT(item), "activate",
3494 G_CALLBACK(tabline_menu_handler),
3495 GINT_TO_POINTER(resp));
3496# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003497 gtk_signal_connect(GTK_OBJECT(item), "activate",
3498 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003499 (gpointer)(long)resp);
Bram Moolenaar98921892016-02-23 17:14:37 +01003500# endif
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003501}
3502
Bram Moolenaara5621492006-02-25 21:55:24 +00003503/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003504 * Create a menu for the tab line.
3505 */
3506 static GtkWidget *
3507create_tabline_menu(void)
3508{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003509 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003510
3511 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02003512 if (first_tabpage->tp_next != NULL)
3513 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
3514 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003515 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3516 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003517
3518 return menu;
3519}
3520
3521 static gboolean
3522on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3523{
3524 /* Was this button press event ? */
3525 if (event->type == GDK_BUTTON_PRESS)
3526 {
3527 GdkEventButton *bevent = (GdkEventButton *)event;
3528 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003529 int y = bevent->y;
3530 GtkWidget *tabwidget;
3531 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003532
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003533 /* When ignoring events return TRUE so that the selected page doesn't
3534 * change. */
3535 if (hold_gui_events
3536# ifdef FEAT_CMDWIN
3537 || cmdwin_type != 0
3538# endif
3539 )
3540 return TRUE;
3541
Bram Moolenaar98921892016-02-23 17:14:37 +01003542# if GTK_CHECK_VERSION(3,0,0)
3543 tabwin = gui_gtk_window_at_position(gui.mainwin, &x, &y);
3544# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003545 tabwin = gdk_window_at_pointer(&x, &y);
Bram Moolenaar98921892016-02-23 17:14:37 +01003546# endif
3547
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003548 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
Bram Moolenaar98921892016-02-23 17:14:37 +01003549# if GTK_CHECK_VERSION(3,0,0)
3550 clicked_page = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tabwidget),
3551 "tab_num"));
3552# else
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003553 clicked_page = (int)(long)gtk_object_get_user_data(
3554 GTK_OBJECT(tabwidget));
Bram Moolenaar98921892016-02-23 17:14:37 +01003555# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003556
3557 /* If the event was generated for 3rd button popup the menu. */
3558 if (bevent->button == 3)
3559 {
Bram Moolenaara859f042016-11-17 19:11:55 +01003560# if GTK_CHECK_VERSION(3,22,2)
3561 gtk_menu_popup_at_pointer(GTK_MENU(widget), event);
3562# else
Bram Moolenaara5621492006-02-25 21:55:24 +00003563 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3564 bevent->button, bevent->time);
Bram Moolenaara859f042016-11-17 19:11:55 +01003565# endif
Bram Moolenaara5621492006-02-25 21:55:24 +00003566 /* We handled the event. */
3567 return TRUE;
3568 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003569 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003570 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003571 if (clicked_page == 0)
3572 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003573 /* Click after all tabs moves to next tab page. When "x" is
3574 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02003575 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003576 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003577 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003578 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003579
Bram Moolenaara5621492006-02-25 21:55:24 +00003580 /* We didn't handle the event. */
3581 return FALSE;
3582}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003583
3584/*
3585 * Handle selecting one of the tabs.
3586 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003587 static void
3588on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003589 GtkNotebook *notebook UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003590# if GTK_CHECK_VERSION(3,0,0)
3591 gpointer *page UNUSED,
3592# else
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003593 GtkNotebookPage *page UNUSED,
Bram Moolenaar98921892016-02-23 17:14:37 +01003594# endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00003595 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003596 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003597{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003598 if (!ignore_tabline_evt)
Bram Moolenaara3f41662010-07-11 19:01:06 +02003599 send_tabline_event(idx + 1);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003600}
3601
3602/*
3603 * Handle reordering the tabs (using D&D).
3604 */
3605 static void
3606on_tab_reordered(
3607 GtkNotebook *notebook UNUSED,
3608# if GTK_CHECK_VERSION(3,0,0)
3609 gpointer *page UNUSED,
3610# else
3611 GtkNotebookPage *page UNUSED,
3612# endif
3613 gint idx,
3614 gpointer data UNUSED)
3615{
3616 if (!ignore_tabline_evt)
3617 {
3618 if ((tabpage_index(curtab) - 1) < idx)
3619 tabpage_move(idx + 1);
3620 else
3621 tabpage_move(idx);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003622 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003623}
3624
3625/*
3626 * Show or hide the tabline.
3627 */
3628 void
3629gui_mch_show_tabline(int showit)
3630{
3631 if (gui.tabline == NULL)
3632 return;
3633
3634 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3635 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003636 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003637 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003638 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00003639 if (showit)
Bram Moolenaar98921892016-02-23 17:14:37 +01003640# if GTK_CHECK_VERSION(3,0,0)
3641 gtk_widget_set_can_focus(GTK_WIDGET(gui.tabline), FALSE);
3642# else
Bram Moolenaar96351572006-05-05 21:16:59 +00003643 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01003644# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003645 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003646
3647 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003648}
3649
3650/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003651 * Return TRUE when tabline is displayed.
3652 */
3653 int
3654gui_mch_showing_tabline(void)
3655{
3656 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003657 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003658}
3659
3660/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003661 * Update the labels of the tabline.
3662 */
3663 void
3664gui_mch_update_tabline(void)
3665{
3666 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003667 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003668 GtkWidget *label;
3669 tabpage_T *tp;
3670 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003671 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003672 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003673 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003674
3675 if (gui.tabline == NULL)
3676 return;
3677
3678 ignore_tabline_evt = TRUE;
3679
3680 /* Add a label for each tab page. They all contain the same text area. */
3681 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3682 {
3683 if (tp == curtab)
3684 curtabidx = nr;
3685
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003686 tab_num = nr + 1;
3687
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003688 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3689 if (page == NULL)
3690 {
3691 /* Add notebook page */
Bram Moolenaar98921892016-02-23 17:14:37 +01003692# if GTK_CHECK_VERSION(3,2,0)
3693 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3694 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
3695# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003696 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003697# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003698 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003699 event_box = gtk_event_box_new();
3700 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003701 label = gtk_label_new("-Empty-");
Bram Moolenaar98921892016-02-23 17:14:37 +01003702# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003703 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01003704# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003705 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003706 gtk_widget_show(label);
3707 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3708 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003709 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003710 nr++);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02003711 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline),
3712 page,
3713 TRUE);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003714 }
3715
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003716 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar98921892016-02-23 17:14:37 +01003717# if GTK_CHECK_VERSION(3,0,0)
3718 g_object_set_data(G_OBJECT(event_box), "tab_num",
3719 GINT_TO_POINTER(tab_num));
3720# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003721 gtk_object_set_user_data(GTK_OBJECT(event_box),
3722 (gpointer)(long)tab_num);
Bram Moolenaar98921892016-02-23 17:14:37 +01003723# endif
3724# if GTK_CHECK_VERSION(3,0,0)
3725 label = gtk_bin_get_child(GTK_BIN(event_box));
3726# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003727 label = GTK_BIN(event_box)->child;
Bram Moolenaar98921892016-02-23 17:14:37 +01003728# endif
Bram Moolenaar57657d82006-04-21 22:12:41 +00003729 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003730 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003731 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003732 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003733
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003734 get_tabline_label(tp, TRUE);
3735 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar98921892016-02-23 17:14:37 +01003736# if GTK_CHECK_VERSION(3,0,0)
3737 gtk_widget_set_tooltip_text(event_box, (const gchar *)labeltext);
3738# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003739 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3740 (const char *)labeltext, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003741# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003742 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003743 }
3744
3745 /* Remove any old labels. */
3746 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3747 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3748
Bram Moolenaar98921892016-02-23 17:14:37 +01003749# if GTK_CHECK_VERSION(3,0,0)
3750 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3751 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3752# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003753 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003754 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar98921892016-02-23 17:14:37 +01003755# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003756
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003757 /* Make sure everything is in place before drawing text. */
3758 gui_mch_update();
3759
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003760 ignore_tabline_evt = FALSE;
3761}
3762
3763/*
3764 * Set the current tab to "nr". First tab is 1.
3765 */
3766 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003767gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003768{
3769 if (gui.tabline == NULL)
3770 return;
3771
3772 ignore_tabline_evt = TRUE;
Bram Moolenaar98921892016-02-23 17:14:37 +01003773# if GTK_CHECK_VERSION(3,0,0)
3774 if (gtk_notebook_get_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3775 gtk_notebook_set_current_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3776# else
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003777 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003778 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01003779# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003780 ignore_tabline_evt = FALSE;
3781}
3782
3783#endif /* FEAT_GUI_TABLINE */
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003786 * Add selection targets for PRIMARY and CLIPBOARD selections.
3787 */
3788 void
3789gui_gtk_set_selection_targets(void)
3790{
3791 int i, j = 0;
3792 int n_targets = N_SELECTION_TARGETS;
3793 GtkTargetEntry targets[N_SELECTION_TARGETS];
3794
3795 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3796 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003797 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003798 * return something, instead of trying another target. Therefore only
3799 * offer TARGET_HTML when it works. */
3800 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3801 n_targets--;
3802 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003803 targets[j++] = selection_targets[i];
3804 }
3805
3806 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3807 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3808 gtk_selection_add_targets(gui.drawarea,
3809 (GdkAtom)GDK_SELECTION_PRIMARY,
3810 targets, n_targets);
3811 gtk_selection_add_targets(gui.drawarea,
3812 (GdkAtom)clip_plus.gtk_sel_atom,
3813 targets, n_targets);
3814}
3815
3816/*
3817 * Set up for receiving DND items.
3818 */
3819 void
3820gui_gtk_set_dnd_targets(void)
3821{
3822 int i, j = 0;
3823 int n_targets = N_DND_TARGETS;
3824 GtkTargetEntry targets[N_DND_TARGETS];
3825
3826 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3827 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003828 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003829 n_targets--;
3830 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003831 targets[j++] = dnd_targets[i];
3832 }
3833
3834 gtk_drag_dest_unset(gui.drawarea);
3835 gtk_drag_dest_set(gui.drawarea,
3836 GTK_DEST_DEFAULT_ALL,
3837 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003838 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003839}
3840
3841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3843 * Returns OK for success, FAIL when the GUI can't be started.
3844 */
3845 int
3846gui_mch_init(void)
3847{
3848 GtkWidget *vbox;
3849
3850#ifdef FEAT_GUI_GNOME
3851 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3852 * exits on failure, but that's a non-issue because we already called
3853 * gtk_init_check() in gui_mch_init_check(). */
3854 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3857 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003858# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003859 {
3860 char *p = setlocale(LC_NUMERIC, NULL);
3861
3862 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3863 * init may change it. */
3864 if (p == NULL || strcmp(p, "C") != 0)
3865 setlocale(LC_NUMERIC, "C");
3866 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003867# endif
3868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869#endif
Bram Moolenaard23a8232018-02-10 18:45:26 +01003870 VIM_CLEAR(gui_argv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003872#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 /* Set the human-readable application name */
3874 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 /*
3877 * Force UTF-8 output no matter what the value of 'encoding' is.
3878 * did_set_string_option() in option.c prohibits changing 'termencoding'
3879 * to something else than UTF-8 if the GUI is in use.
3880 */
3881 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3882
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003883#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3887 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003888#if !GTK_CHECK_VERSION(3,0,0)
3889# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 gtk_rc_parse("gtkrc");
Bram Moolenaar98921892016-02-23 17:14:37 +01003891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892#endif
3893
3894 /* Initialize values */
3895 gui.border_width = 2;
3896 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3897 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaar36edf062016-07-21 22:10:12 +02003898#if GTK_CHECK_VERSION(3,0,0)
3899 gui.fgcolor = g_new(GdkRGBA, 1);
3900 gui.bgcolor = g_new(GdkRGBA, 1);
3901 gui.spcolor = g_new(GdkRGBA, 1);
3902#else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003903 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003905 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003907 /* LINTED: avoid warning: conversion to 'unsigned long' */
3908 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar36edf062016-07-21 22:10:12 +02003909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
3911 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003912 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 /* Set default foreground and background colors. */
3916 gui.norm_pixel = gui.def_norm_pixel;
3917 gui.back_pixel = gui.def_back_pixel;
3918
3919 if (gtk_socket_id != 0)
3920 {
3921 GtkWidget *plug;
3922
3923 /* Use GtkSocket from another app. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3925 gtk_socket_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01003926#if GTK_CHECK_VERSION(3,0,0)
3927 if (plug != NULL && gtk_plug_get_socket_window(GTK_PLUG(plug)) != NULL)
3928#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01003930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 {
3932 gui.mainwin = plug;
3933 }
3934 else
3935 {
3936 g_warning("Connection to GTK+ socket (ID %u) failed",
3937 (unsigned int)gtk_socket_id);
3938 /* Pretend we never wanted it if it failed (get own window) */
3939 gtk_socket_id = 0;
3940 }
3941 }
3942
3943 if (gtk_socket_id == 0)
3944 {
3945#ifdef FEAT_GUI_GNOME
3946 if (using_gnome)
3947 {
3948 gui.mainwin = gnome_app_new("Vim", NULL);
3949# ifdef USE_XSMP
3950 /* Use the GNOME save-yourself functionality now. */
3951 xsmp_close();
3952# endif
3953 }
3954 else
3955#endif
3956 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3957 }
3958
3959 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3960
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 /* Create the PangoContext used for drawing all text. */
3962 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3963 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
Bram Moolenaar98921892016-02-23 17:14:37 +01003965#if GTK_CHECK_VERSION(3,0,0)
3966 gtk_container_set_border_width(GTK_CONTAINER(gui.mainwin), 0);
3967#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3971
Bram Moolenaar98921892016-02-23 17:14:37 +01003972#if GTK_CHECK_VERSION(3,0,0)
3973 g_signal_connect(G_OBJECT(gui.mainwin), "delete-event",
3974 G_CALLBACK(&delete_event_cb), NULL);
3975
3976 g_signal_connect(G_OBJECT(gui.mainwin), "realize",
3977 G_CALLBACK(&mainwin_realize), NULL);
3978#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3980 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3981
3982 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3983 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01003984#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003985
3986 g_signal_connect(G_OBJECT(gui.mainwin), "screen-changed",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02003988
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 gui.accel_group = gtk_accel_group_new();
3990 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003992 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01003993#if GTK_CHECK_VERSION(3,2,0)
3994 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
3995 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
3996#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01003998#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999
4000#ifdef FEAT_GUI_GNOME
4001 if (using_gnome)
4002 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004003# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 /* automagically restore menubar/toolbar placement */
4005 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
4006# endif
4007 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
4008 }
4009 else
4010#endif
4011 {
4012 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
4013 gtk_widget_show(vbox);
4014 }
4015
4016#ifdef FEAT_MENU
4017 /*
4018 * Create the menubar and handle
4019 */
4020 gui.menubar = gtk_menu_bar_new();
4021 gtk_widget_set_name(gui.menubar, "vim-menubar");
4022
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004023 /* Avoid that GTK takes <F10> away from us. */
4024 {
4025 GtkSettings *gtk_settings;
4026
4027 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
4028 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
4029 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004030
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032# ifdef FEAT_GUI_GNOME
4033 if (using_gnome)
4034 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 BonoboDockItem *dockitem;
4036
4037 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
4038 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4039 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00004040 /* We don't want the menu to float. */
4041 bonobo_dock_item_set_behavior(dockitem,
4042 bonobo_dock_item_get_behavior(dockitem)
4043 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 else
4047# endif /* FEAT_GUI_GNOME */
4048 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00004049 /* Always show the menubar, otherwise <F10> doesn't work. It may be
4050 * disabled in gui_init() later. */
4051 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
4053 }
4054#endif /* FEAT_MENU */
4055
4056#ifdef FEAT_TOOLBAR
4057 /*
4058 * Create the toolbar and handle
4059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 /* some aesthetics on the toolbar */
Bram Moolenaar98921892016-02-23 17:14:37 +01004061# ifdef USE_GTK3
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004062 /* TODO: Add GTK+ 3 code here using GtkCssProvider if necessary. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004063 /* N.B. Since the default value of GtkToolbar::button-relief is
4064 * GTK_RELIEF_NONE, there's no need to specify that, probably. */
4065# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 gtk_rc_parse_string(
4067 "style \"vim-toolbar-style\" {\n"
4068 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
4069 "}\n"
4070 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
Bram Moolenaar98921892016-02-23 17:14:37 +01004071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 gui.toolbar = gtk_toolbar_new();
4073 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4075
4076# ifdef FEAT_GUI_GNOME
4077 if (using_gnome)
4078 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 BonoboDockItem *dockitem;
4080
4081 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
4082 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
4083 GNOME_APP_TOOLBAR_NAME);
4084 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004085 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00004086 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00004087 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00004088 bonobo_dock_item_get_behavior(dockitem)
4089 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 }
4092 else
4093# endif /* FEAT_GUI_GNOME */
4094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
4096 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4097 gtk_widget_show(gui.toolbar);
4098 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
4099 }
4100#endif /* FEAT_TOOLBAR */
4101
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004102#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004103 /*
4104 * Use a Notebook for the tab pages labels. The labels are hidden by
4105 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004106 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004107 gui.tabline = gtk_notebook_new();
4108 gtk_widget_show(gui.tabline);
4109 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
4110 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
4111 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004112 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004113# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar96351572006-05-05 21:16:59 +00004114 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01004115# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004116
Bram Moolenaar98921892016-02-23 17:14:37 +01004117# if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004118 tabline_tooltip = gtk_tooltips_new();
4119 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
Bram Moolenaar98921892016-02-23 17:14:37 +01004120# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004121
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004122 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004123 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004124
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004125 /* Add the first tab. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004126# if GTK_CHECK_VERSION(3,2,0)
4127 page = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
4128 gtk_box_set_homogeneous(GTK_BOX(page), FALSE);
4129# else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004130 page = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004131# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004132 gtk_widget_show(page);
4133 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
4134 label = gtk_label_new("-Empty-");
4135 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004136 event_box = gtk_event_box_new();
4137 gtk_widget_show(event_box);
Bram Moolenaar98921892016-02-23 17:14:37 +01004138# if GTK_CHECK_VERSION(3,0,0)
4139 g_object_set_data(G_OBJECT(event_box), "tab_num", GINT_TO_POINTER(1L));
4140# else
Bram Moolenaar47b46d72008-07-02 19:05:48 +00004141 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar98921892016-02-23 17:14:37 +01004142# endif
4143# if !GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar8ea91232006-04-28 22:41:43 +00004144 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01004145# endif
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004146 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00004147 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004148 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(gui.tabline), page, TRUE);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00004149 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00004150
Bram Moolenaar98921892016-02-23 17:14:37 +01004151# if GTK_CHECK_VERSION(3,0,0)
4152 g_signal_connect(G_OBJECT(gui.tabline), "switch-page",
4153 G_CALLBACK(on_select_tab), NULL);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004154 g_signal_connect(G_OBJECT(gui.tabline), "page-reordered",
4155 G_CALLBACK(on_tab_reordered), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004156# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004157 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004158 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarca05aa22017-10-22 15:36:14 +02004159 gtk_signal_connect(GTK_OBJECT(gui.tabline), "page-reordered",
4160 GTK_SIGNAL_FUNC(on_tab_reordered), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004161# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004162
4163 /* Create a popup menu for the tab line and connect it. */
4164 tabline_menu = create_tabline_menu();
Bram Moolenaar98921892016-02-23 17:14:37 +01004165# if GTK_CHECK_VERSION(3,0,0)
4166 g_signal_connect_swapped(G_OBJECT(gui.tabline), "button-press-event",
4167 G_CALLBACK(on_tabline_menu), G_OBJECT(tabline_menu));
4168# else
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004169 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004170 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar98921892016-02-23 17:14:37 +01004171# endif
4172#endif /* FEAT_GUI_TABLINE */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004173
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 gui.formwin = gtk_form_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01004175#if GTK_CHECK_VERSION(3,0,0)
4176 gtk_container_set_border_width(GTK_CONTAINER(gui.formwin), 0);
4177#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01004179#endif
4180#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
Bram Moolenaar98921892016-02-23 17:14:37 +01004182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 gui.drawarea = gtk_drawing_area_new();
Bram Moolenaara859f042016-11-17 19:11:55 +01004185#if GTK_CHECK_VERSION(3,22,2)
4186 gtk_widget_set_name(gui.drawarea, "vim-gui-drawarea");
4187#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004188#if GTK_CHECK_VERSION(3,0,0)
4189 gui.surface = NULL;
4190 gui.by_signal = FALSE;
4191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192
4193 /* Determine which events we will filter. */
4194 gtk_widget_set_events(gui.drawarea,
4195 GDK_EXPOSURE_MASK |
4196 GDK_ENTER_NOTIFY_MASK |
4197 GDK_LEAVE_NOTIFY_MASK |
4198 GDK_BUTTON_PRESS_MASK |
4199 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 GDK_KEY_PRESS_MASK |
4202 GDK_KEY_RELEASE_MASK |
4203 GDK_POINTER_MOTION_MASK |
4204 GDK_POINTER_MOTION_HINT_MASK);
4205
4206 gtk_widget_show(gui.drawarea);
4207 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
4208 gtk_widget_show(gui.formwin);
4209 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
4210
4211 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
4212 * and not the window. */
Bram Moolenaar98921892016-02-23 17:14:37 +01004213#if GTK_CHECK_VERSION(3,0,0)
4214 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4215 : G_OBJECT(gui.drawarea),
4216 "key-press-event",
4217 G_CALLBACK(key_press_event), NULL);
4218#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
4220 : GTK_OBJECT(gui.drawarea),
4221 "key_press_event",
4222 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004223#endif
4224#if defined(FEAT_XIM) || GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 /* Also forward key release events for the benefit of GTK+ 2 input
4226 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
4227 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
4228 : G_OBJECT(gui.drawarea),
Bram Moolenaar98921892016-02-23 17:14:37 +01004229 "key-release-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 G_CALLBACK(&key_release_event), NULL);
4231#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004232#if GTK_CHECK_VERSION(3,0,0)
4233 g_signal_connect(G_OBJECT(gui.drawarea), "realize",
4234 G_CALLBACK(drawarea_realize_cb), NULL);
4235 g_signal_connect(G_OBJECT(gui.drawarea), "unrealize",
4236 G_CALLBACK(drawarea_unrealize_cb), NULL);
4237 g_signal_connect(G_OBJECT(gui.drawarea), "configure-event",
4238 G_CALLBACK(drawarea_configure_event_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004239# if GTK_CHECK_VERSION(3,22,2)
4240 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-updated",
4241 G_CALLBACK(&drawarea_style_updated_cb), NULL);
4242# else
Bram Moolenaar98921892016-02-23 17:14:37 +01004243 g_signal_connect_after(G_OBJECT(gui.drawarea), "style-set",
4244 G_CALLBACK(&drawarea_style_set_cb), NULL);
Bram Moolenaara859f042016-11-17 19:11:55 +01004245# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004246#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
4248 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
4249 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
4250 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
4251
4252 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
4253 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004254#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar98921892016-02-23 17:14:37 +01004256#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
Bram Moolenaar98921892016-02-23 17:14:37 +01004258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
4261 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
4262 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
4263#endif
4264
4265 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004266 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar98921892016-02-23 17:14:37 +01004267#if GTK_CHECK_VERSION(3,0,0)
4268 gtk_widget_set_can_focus(gui.drawarea, TRUE);
4269#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01004271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272
4273 /*
4274 * Set clipboard specific atoms
4275 */
4276 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
4279 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
4280
4281 /*
4282 * Start out by adding the configured border width into the border offset.
4283 */
4284 gui.border_offset = gui.border_width;
4285
Bram Moolenaar98921892016-02-23 17:14:37 +01004286#if GTK_CHECK_VERSION(3,0,0)
4287 g_signal_connect(G_OBJECT(gui.drawarea), "draw",
4288 G_CALLBACK(draw_event), NULL);
4289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
4291 GTK_SIGNAL_FUNC(visibility_event), NULL);
4292 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
4293 GTK_SIGNAL_FUNC(expose_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 /*
4297 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
4298 * Only needed for some window managers.
4299 */
4300 if (vim_strchr(p_go, GO_POINTER) != NULL)
4301 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004302#if GTK_CHECK_VERSION(3,0,0)
4303 g_signal_connect(G_OBJECT(gui.drawarea), "leave-notify-event",
4304 G_CALLBACK(leave_notify_event), NULL);
4305 g_signal_connect(G_OBJECT(gui.drawarea), "enter-notify-event",
4306 G_CALLBACK(enter_notify_event), NULL);
4307#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
4309 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
4310 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
4311 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004315 /* Real windows can get focus ... GtkPlug, being a mere container can't,
4316 * only its widgets. Arguably, this could be common code and we not use
4317 * the window focus at all, but let's be safe.
4318 */
4319 if (gtk_socket_id == 0)
4320 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004321#if GTK_CHECK_VERSION(3,0,0)
4322 g_signal_connect(G_OBJECT(gui.mainwin), "focus-out-event",
4323 G_CALLBACK(focus_out_event), NULL);
4324 g_signal_connect(G_OBJECT(gui.mainwin), "focus-in-event",
4325 G_CALLBACK(focus_in_event), NULL);
4326#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004327 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
4328 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4329 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
4330 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004331#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004332 }
4333 else
4334 {
Bram Moolenaar98921892016-02-23 17:14:37 +01004335#if GTK_CHECK_VERSION(3,0,0)
4336 g_signal_connect(G_OBJECT(gui.drawarea), "focus-out-event",
4337 G_CALLBACK(focus_out_event), NULL);
4338 g_signal_connect(G_OBJECT(gui.drawarea), "focus-in-event",
4339 G_CALLBACK(focus_in_event), NULL);
4340#else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004341 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
4342 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4343 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
4344 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004345#endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004346#ifdef FEAT_GUI_TABLINE
Bram Moolenaar98921892016-02-23 17:14:37 +01004347# if GTK_CHECK_VERSION(3,0,0)
4348 g_signal_connect(G_OBJECT(gui.tabline), "focus-out-event",
4349 G_CALLBACK(focus_out_event), NULL);
4350 g_signal_connect(G_OBJECT(gui.tabline), "focus-in-event",
4351 G_CALLBACK(focus_in_event), NULL);
4352# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004353 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
4354 GTK_SIGNAL_FUNC(focus_out_event), NULL);
4355 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
4356 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004357# endif
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004358#endif /* FEAT_GUI_TABLINE */
4359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaar98921892016-02-23 17:14:37 +01004361#if GTK_CHECK_VERSION(3,0,0)
4362 g_signal_connect(G_OBJECT(gui.drawarea), "motion-notify-event",
4363 G_CALLBACK(motion_notify_event), NULL);
4364 g_signal_connect(G_OBJECT(gui.drawarea), "button-press-event",
4365 G_CALLBACK(button_press_event), NULL);
4366 g_signal_connect(G_OBJECT(gui.drawarea), "button-release-event",
4367 G_CALLBACK(button_release_event), NULL);
4368 g_signal_connect(G_OBJECT(gui.drawarea), "scroll-event",
4369 G_CALLBACK(&scroll_event), NULL);
4370#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
4372 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
4373 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
4374 GTK_SIGNAL_FUNC(button_press_event), NULL);
4375 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
4376 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
4378 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
4381 /*
4382 * Add selection handler functions.
4383 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004384#if GTK_CHECK_VERSION(3,0,0)
4385 g_signal_connect(G_OBJECT(gui.drawarea), "selection-clear-event",
4386 G_CALLBACK(selection_clear_event), NULL);
4387 g_signal_connect(G_OBJECT(gui.drawarea), "selection-received",
4388 G_CALLBACK(selection_received_cb), NULL);
4389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
4391 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
4392 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
4393 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaara76638f2010-06-05 12:49:46 +02004396 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
Bram Moolenaar98921892016-02-23 17:14:37 +01004398#if GTK_CHECK_VERSION(3,0,0)
4399 g_signal_connect(G_OBJECT(gui.drawarea), "selection-get",
4400 G_CALLBACK(selection_get_cb), NULL);
4401#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
4403 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405
4406 /* Pretend we don't have input focus, we will get an event if we do. */
4407 gui.in_focus = FALSE;
4408
Bram Moolenaar7ebf4e12018-08-07 20:01:40 +02004409 // Handle changes to the "Xft/DPI" setting.
4410 {
4411 GtkSettings *gtk_settings =
4412 gtk_settings_get_for_screen(gdk_screen_get_default());
4413
4414 g_signal_connect(gtk_settings, "notify::gtk-xft-dpi",
4415 G_CALLBACK(gtk_settings_xft_dpi_changed_cb), NULL);
4416 }
4417
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 return OK;
4419}
4420
4421#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4422/*
4423 * This is called from gui_start() after a fork() has been done.
4424 * We have to tell the session manager our new PID.
4425 */
4426 void
4427gui_mch_forked(void)
4428{
4429 if (using_gnome)
4430 {
4431 GnomeClient *client;
4432
4433 client = gnome_master_client();
4434
4435 if (client != NULL)
4436 gnome_client_set_process_id(client, getpid());
4437 }
4438}
4439#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4440
Bram Moolenaar98921892016-02-23 17:14:37 +01004441#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02004442 static GdkRGBA
4443color_to_rgba(guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004444{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004445 GdkRGBA rgba;
4446 rgba.red = ((color & 0xff0000) >> 16) / 255.0;
4447 rgba.green = ((color & 0xff00) >> 8) / 255.0;
4448 rgba.blue = ((color & 0xff)) / 255.0;
4449 rgba.alpha = 1.0;
4450 return rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004451}
4452
4453 static void
Bram Moolenaar36edf062016-07-21 22:10:12 +02004454set_cairo_source_rgba_from_color(cairo_t *cr, guicolor_T color)
Bram Moolenaar98921892016-02-23 17:14:37 +01004455{
Bram Moolenaar36edf062016-07-21 22:10:12 +02004456 const GdkRGBA rgba = color_to_rgba(color);
4457 cairo_set_source_rgba(cr, rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004458}
4459#endif /* GTK_CHECK_VERSION(3,0,0) */
4460
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461/*
4462 * Called when the foreground or background color has been changed.
4463 * This used to change the graphics contexts directly but we are
4464 * currently manipulating them where desired.
4465 */
4466 void
4467gui_mch_new_colors(void)
4468{
Bram Moolenaar98921892016-02-23 17:14:37 +01004469#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaara859f042016-11-17 19:11:55 +01004470# if !GTK_CHECK_VERSION(3,22,2)
Bram Moolenaar98921892016-02-23 17:14:37 +01004471 GdkWindow * const da_win = gtk_widget_get_window(gui.drawarea);
Bram Moolenaara859f042016-11-17 19:11:55 +01004472# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01004473
4474 if (gui.drawarea != NULL && gtk_widget_get_window(gui.drawarea) != NULL)
4475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01004477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaara859f042016-11-17 19:11:55 +01004479#if GTK_CHECK_VERSION(3,22,2)
4480 GtkStyleContext * const context
4481 = gtk_widget_get_style_context(gui.drawarea);
4482 GtkCssProvider * const provider = gtk_css_provider_new();
4483 gchar * const css = g_strdup_printf(
4484 "widget#vim-gui-drawarea {\n"
4485 " background-color: #%.2lx%.2lx%.2lx;\n"
4486 "}\n",
4487 (gui.back_pixel >> 16) & 0xff,
4488 (gui.back_pixel >> 8) & 0xff,
4489 gui.back_pixel & 0xff);
4490
4491 gtk_css_provider_load_from_data(provider, css, -1, NULL);
4492 gtk_style_context_add_provider(context,
4493 GTK_STYLE_PROVIDER(provider), G_MAXUINT);
4494
4495 g_free(css);
4496 g_object_unref(provider);
4497#elif GTK_CHECK_VERSION(3,4,0) /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar36edf062016-07-21 22:10:12 +02004498 GdkRGBA rgba;
Bram Moolenaar98921892016-02-23 17:14:37 +01004499
Bram Moolenaar36edf062016-07-21 22:10:12 +02004500 rgba = color_to_rgba(gui.back_pixel);
Bram Moolenaar98921892016-02-23 17:14:37 +01004501 {
4502 cairo_pattern_t * const pat = cairo_pattern_create_rgba(
Bram Moolenaar36edf062016-07-21 22:10:12 +02004503 rgba.red, rgba.green, rgba.blue, rgba.alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01004504 if (pat != NULL)
4505 {
4506 gdk_window_set_background_pattern(da_win, pat);
4507 cairo_pattern_destroy(pat);
4508 }
4509 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02004510 gdk_window_set_background_rgba(da_win, &rgba);
Bram Moolenaar98921892016-02-23 17:14:37 +01004511 }
4512#else /* !GTK_CHECK_VERSION(3,4,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 GdkColor color = { 0, 0, 0, 0 };
4514
4515 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01004516# if GTK_CHECK_VERSION(3,0,0)
4517 gdk_window_set_background(da_win, &color);
4518# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 gdk_window_set_background(gui.drawarea->window, &color);
Bram Moolenaar98921892016-02-23 17:14:37 +01004520# endif
Bram Moolenaara859f042016-11-17 19:11:55 +01004521#endif /* !GTK_CHECK_VERSION(3,22,2) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523}
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525/*
4526 * This signal informs us about the need to rearrange our sub-widgets.
4527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004529form_configure_event(GtkWidget *widget UNUSED,
4530 GdkEventConfigure *event,
4531 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004533 int usable_height = event->height;
4534
Bram Moolenaar182707a2016-11-21 20:55:58 +01004535#if GTK_CHECK_VERSION(3,22,2) && !GTK_CHECK_VERSION(3,22,4)
Bram Moolenaara859f042016-11-17 19:11:55 +01004536 /* As of 3.22.2, GdkWindows have started distributing configure events to
4537 * their "native" children (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=12579fe71b3b8f79eb9c1b80e429443bcc437dd0).
4538 *
4539 * As can be seen from the implementation of move_native_children() and
4540 * configure_native_child() in gdkwindow.c, those functions actually
4541 * propagate configure events to every child, failing to distinguish
4542 * "native" one from non-native one.
4543 *
4544 * Naturally, configure events propagated to here like that are fallacious
4545 * and, as a matter of fact, they trigger a geometric collapse of
4546 * gui.formwin.
4547 *
4548 * To filter out such fallacious events, check if the given event is the
4549 * one that was sent out to the right place. Ignore it if not.
4550 */
Bram Moolenaar182707a2016-11-21 20:55:58 +01004551 /* Follow-up
4552 * After a few weeks later, the GdkWindow change mentioned above was
4553 * reverted (https://git.gnome.org/browse/gtk+/commit/?h=gtk-3-22&id=f70039cb9603a02d2369fec4038abf40a1711155).
4554 * The corresponding official release is 3.22.4. */
Bram Moolenaara859f042016-11-17 19:11:55 +01004555 if (event->window != gtk_widget_get_window(gui.formwin))
4556 return TRUE;
4557#endif
4558
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004559 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4560 * no. of char-heights), so we have to manually sanitise them.
4561 * Widths seem to sort themselves out, don't ask me why.
4562 */
4563 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004564 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004565
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004567 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 gtk_form_thaw(GTK_FORM(gui.formwin));
4569
4570 return TRUE;
4571}
4572
4573/*
4574 * Function called when window already closed.
4575 * We can't do much more here than to trying to preserve what had been done,
4576 * since the window is already inevitably going away.
4577 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004578#if GTK_CHECK_VERSION(3,0,0)
4579 static void
4580mainwin_destroy_cb(GObject *object UNUSED, gpointer data UNUSED)
4581#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004583mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar98921892016-02-23 17:14:37 +01004584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585{
4586 /* Don't write messages to the GUI anymore */
4587 full_screen = FALSE;
4588
4589 gui.mainwin = NULL;
4590 gui.drawarea = NULL;
4591
4592 if (!exiting) /* only do anything if the destroy was unexpected */
4593 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004594 vim_strncpy(IObuff,
4595 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4596 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 preserve_exit();
4598 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01004599#ifdef USE_GRESOURCE
4600 gui_gtk_unregister_resource();
4601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602}
4603
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004604
4605/*
4606 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4607 * hints (and thus the required size from -geom), but that after that we
4608 * put the hints back to normal (the actual minimum size) so we may
4609 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00004610 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004611 * only way we have of making ourselves bigger (by set lines/columns).
4612 * Thus set hints at start-up to ensure correct init. size, then a
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01004613 * second after the final attempt to reset the real minimum hints (done by
4614 * scrollbar init.), actually do the standard hints and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004615 * We'll not let the default hints be set while this timer's active.
4616 */
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004617 static timeout_cb_type
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004618check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004619{
4620 if (init_window_hints_state == 1)
4621 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004622 /* Safe to use normal hints now */
4623 init_window_hints_state = 0;
4624 update_window_manager_hints(0, 0);
4625 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004626 }
4627
4628 /* Keep on trying */
4629 init_window_hints_state = 1;
4630 return TRUE;
4631}
4632
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633/*
4634 * Open the GUI window which was created by a call to gui_mch_init().
4635 */
4636 int
4637gui_mch_open(void)
4638{
4639 guicolor_T fg_pixel = INVALCOLOR;
4640 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004641 guint pixel_width;
4642 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 /*
4645 * Allow setting a window role on the command line, or invent one
4646 * if none was specified. This is mainly useful for GNOME session
4647 * support; allowing the WM to restore window placement.
4648 */
4649 if (role_argument != NULL)
4650 {
4651 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4652 }
4653 else
4654 {
4655 char *role;
4656
4657 /* Invent a unique-enough ID string for the role */
4658 role = g_strdup_printf("vim-%u-%u-%u",
4659 (unsigned)mch_get_pid(),
4660 (unsigned)g_random_int(),
4661 (unsigned)time(NULL));
4662
4663 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4664 g_free(role);
4665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666
4667 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 /* Determine user specified geometry, if present. */
4671 if (gui.geom != NULL)
4672 {
4673 int mask;
4674 unsigned int w, h;
4675 int x = 0;
4676 int y = 0;
4677
4678 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4679
4680 if (mask & WidthValue)
4681 Columns = w;
4682 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004683 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004684 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004685 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004687 }
Bram Moolenaare057d402013-06-30 17:51:51 +02004688 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004689
4690 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4691 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4692
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004693 pixel_width += get_menu_tool_width();
4694 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004695
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004697 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004698 int ww, hh;
4699 gui_mch_get_screen_dimensions(&ww, &hh);
4700 hh += p_ghr + get_menu_tool_height();
4701 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004702 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004703 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004704 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004705 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004707 }
Bram Moolenaard23a8232018-02-10 18:45:26 +01004708 VIM_CLEAR(gui.geom);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004709
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004710 /* From now until everyone's stopped trying to set the window hints
4711 * to their correct minimum values, stop them being set as we need
4712 * them to remain at our required size for the parent GtkSocket to
4713 * give us the right initial size.
4714 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004715 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004716 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004717 update_window_manager_hints(pixel_width, pixel_height);
4718 init_window_hints_state = 1;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02004719 timeout_add(1000, check_startup_plug_hints, NULL);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004723 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4724 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004725 /* For GTK2 changing the size of the form widget doesn't cause window
4726 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02004727 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00004728 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004729 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730
4731 if (foreground_argument != NULL)
4732 fg_pixel = gui_get_color((char_u *)foreground_argument);
4733 if (fg_pixel == INVALCOLOR)
4734 fg_pixel = gui_get_color((char_u *)"Black");
4735
4736 if (background_argument != NULL)
4737 bg_pixel = gui_get_color((char_u *)background_argument);
4738 if (bg_pixel == INVALCOLOR)
4739 bg_pixel = gui_get_color((char_u *)"White");
4740
4741 if (found_reverse_arg)
4742 {
4743 gui.def_norm_pixel = bg_pixel;
4744 gui.def_back_pixel = fg_pixel;
4745 }
4746 else
4747 {
4748 gui.def_norm_pixel = fg_pixel;
4749 gui.def_back_pixel = bg_pixel;
4750 }
4751
4752 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4753 * in a vimrc file) */
4754 set_normal_colors();
4755
4756 /* Check that none of the colors are the same as the background color */
4757 gui_check_colors();
4758
4759 /* Get the colors for the highlight groups (gui_check_colors() might have
4760 * changed them). */
4761 highlight_gui_started(); /* re-init colors and fonts */
4762
Bram Moolenaar98921892016-02-23 17:14:37 +01004763#if GTK_CHECK_VERSION(3,0,0)
4764 g_signal_connect(G_OBJECT(gui.mainwin), "destroy",
4765 G_CALLBACK(mainwin_destroy_cb), NULL);
4766#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4768 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770
4771#ifdef FEAT_HANGULIN
4772 hangul_keyboard_set();
4773#endif
4774
4775 /*
4776 * Notify the fixed area about the need to resize the contents of the
4777 * gui.formwin, which we use for random positioning of the included
4778 * components.
4779 *
4780 * We connect this signal deferred finally after anything is in place,
4781 * since this is intended to handle resizements coming from the window
4782 * manager upon us and should not interfere with what VIM is requesting
4783 * upon startup.
4784 */
Bram Moolenaar98921892016-02-23 17:14:37 +01004785#if GTK_CHECK_VERSION(3,0,0)
4786 g_signal_connect(G_OBJECT(gui.formwin), "configure-event",
4787 G_CALLBACK(form_configure_event), NULL);
4788#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4790 GTK_SIGNAL_FUNC(form_configure_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
4793#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02004794 /* Set up for receiving DND items. */
4795 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796
Bram Moolenaar98921892016-02-23 17:14:37 +01004797# if GTK_CHECK_VERSION(3,0,0)
4798 g_signal_connect(G_OBJECT(gui.drawarea), "drag-data-received",
4799 G_CALLBACK(drag_data_received_cb), NULL);
4800# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4802 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01004803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804#endif
4805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004807 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (found_iconic_arg && gtk_socket_id == 0)
4809 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810
4811 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004812#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 unsigned long menu_handler = 0;
4814# ifdef FEAT_TOOLBAR
4815 unsigned long tool_handler = 0;
4816# endif
4817 /*
4818 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4819 * show when restoring the saved layout configuration. We can't just
4820 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4821 * a toplevel window and thus will be realized immediately. Instead,
4822 * connect signal handlers to hide the widgets just after they've been
4823 * marked visible, but before the main window is realized.
4824 */
4825 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4826 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4827 G_CALLBACK(&gtk_widget_hide),
4828 NULL);
4829# ifdef FEAT_TOOLBAR
4830 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4831 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4832 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4833 G_CALLBACK(&gtk_widget_hide),
4834 NULL);
4835# endif
4836#endif
4837 gtk_widget_show(gui.mainwin);
4838
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004839#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 if (menu_handler != 0)
4841 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4842# ifdef FEAT_TOOLBAR
4843 if (tool_handler != 0)
4844 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4845# endif
4846#endif
4847 }
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 return OK;
4850}
4851
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004854gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 if (gui.mainwin != NULL)
4857 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858}
4859
4860/*
4861 * Get the position of the top left corner of the window.
4862 */
4863 int
4864gui_mch_get_winpos(int *x, int *y)
4865{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return OK;
4868}
4869
4870/*
4871 * Set the position of the top left corner of the window to the given
4872 * coordinates.
4873 */
4874 void
4875gui_mch_set_winpos(int x, int y)
4876{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878}
4879
Bram Moolenaar98921892016-02-23 17:14:37 +01004880#if !GTK_CHECK_VERSION(3,0,0)
4881# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882static int resize_idle_installed = FALSE;
4883/*
4884 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4885 * the shell size doesn't exceed the window size, i.e. if the window manager
4886 * ignored our size request. Usually this happens if the window is maximized.
4887 *
4888 * FIXME: It'd be nice if we could find a little more orthodox solution.
4889 * See also the remark below in gui_mch_set_shellsize().
4890 *
4891 * DISABLED: When doing ":set lines+=1" this function would first invoke
4892 * gui_resize_shell() with the old size, then the normal callback would
4893 * report the new size through form_configure_event(). That caused the window
4894 * layout to be messed up.
4895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 static gboolean
4897force_shell_resize_idle(gpointer data)
4898{
4899 if (gui.mainwin != NULL
4900 && GTK_WIDGET_REALIZED(gui.mainwin)
4901 && GTK_WIDGET_VISIBLE(gui.mainwin))
4902 {
4903 int width;
4904 int height;
4905
4906 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4907
4908 width -= get_menu_tool_width();
4909 height -= get_menu_tool_height();
4910
4911 gui_resize_shell(width, height);
4912 }
4913
4914 resize_idle_installed = FALSE;
4915 return FALSE; /* don't call me again */
4916}
Bram Moolenaar98921892016-02-23 17:14:37 +01004917# endif
4918#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919
Bram Moolenaar09736232009-09-23 16:14:49 +00004920/*
4921 * Return TRUE if the main window is maximized.
4922 */
4923 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004924gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004925{
Bram Moolenaar98921892016-02-23 17:14:37 +01004926#if GTK_CHECK_VERSION(3,0,0)
4927 return (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL
4928 && (gdk_window_get_state(gtk_widget_get_window(gui.mainwin))
4929 & GDK_WINDOW_STATE_MAXIMIZED));
4930#else
Bram Moolenaar09736232009-09-23 16:14:49 +00004931 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4932 && (gdk_window_get_state(gui.mainwin->window)
4933 & GDK_WINDOW_STATE_MAXIMIZED));
Bram Moolenaar98921892016-02-23 17:14:37 +01004934#endif
Bram Moolenaar09736232009-09-23 16:14:49 +00004935}
4936
4937/*
4938 * Unmaximize the main window
4939 */
4940 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004941gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00004942{
4943 if (gui.mainwin != NULL)
4944 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4945}
Bram Moolenaar09736232009-09-23 16:14:49 +00004946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947/*
Bram Moolenaar8ac44152017-11-09 18:33:29 +01004948 * Called when the font changed while the window is maximized or GO_KEEPWINSIZE
4949 * is set. Compute the new Rows and Columns. This is like resizing the
4950 * window.
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004951 */
4952 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01004953gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004954{
4955 int w, h;
4956
4957 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4958 w -= get_menu_tool_width();
4959 h -= get_menu_tool_height();
4960 gui_resize_shell(w, h);
4961}
4962
4963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * Set the windows size.
4965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 void
4967gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004968 int min_width UNUSED, int min_height UNUSED,
4969 int base_width UNUSED, int base_height UNUSED,
4970 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* give GTK+ a chance to put all widget's into place */
4973 gui_mch_update();
4974
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004975 /* this will cause the proper resizement to happen too */
4976 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004977 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004980 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 * main window instead. */
4982 width += get_menu_tool_width();
4983 height += get_menu_tool_height();
4984
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004985 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004986 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004987 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004988 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
Bram Moolenaar98921892016-02-23 17:14:37 +01004990# if !GTK_CHECK_VERSION(3,0,0)
4991# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (!resize_idle_installed)
4993 {
4994 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4995 &force_shell_resize_idle, NULL, NULL);
4996 resize_idle_installed = TRUE;
4997 }
Bram Moolenaar98921892016-02-23 17:14:37 +01004998# endif
4999# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 /*
5001 * Wait until all events are processed to prevent a crash because the
5002 * real size of the drawing area doesn't reflect Vim's internal ideas.
5003 *
5004 * This is a bit of a hack, since Vim is a terminal application with a GUI
5005 * on top, while the GUI expects to be the boss.
5006 */
5007 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008}
5009
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005010 void
Bram Moolenaar8696bba2017-09-09 23:00:56 +02005011gui_gtk_get_screen_size_of_win(GtkWidget *wid, int *width, int *height)
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005012{
5013#if GTK_CHECK_VERSION(3,22,0)
Bram Moolenaar8696bba2017-09-09 23:00:56 +02005014 GdkDisplay *dpy = gtk_widget_get_display(wid);
5015 GdkWindow *win = gtk_widget_get_window(wid);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005016 GdkMonitor *monitor = gdk_display_get_monitor_at_window(dpy, win);
5017 GdkRectangle geometry;
5018
5019 gdk_monitor_get_geometry(monitor, &geometry);
5020 *width = geometry.width;
5021 *height = geometry.height;
5022#else
5023 GdkScreen* screen;
5024
Bram Moolenaar8696bba2017-09-09 23:00:56 +02005025 if (wid != NULL && gtk_widget_has_screen(wid))
5026 screen = gtk_widget_get_screen(wid);
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005027 else
5028 screen = gdk_screen_get_default();
5029 *width = gdk_screen_get_width(screen);
5030 *height = gdk_screen_get_height(screen);
5031#endif
5032}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
5034/*
5035 * The screen size is used to make sure the initial window doesn't get bigger
5036 * than the screen. This subtracts some room for menubar, toolbar and window
5037 * decorations.
5038 */
5039 void
5040gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
5041{
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005042 gui_gtk_get_screen_size_of_win(gui.mainwin, screen_w, screen_h);
Bram Moolenaara859f042016-11-17 19:11:55 +01005043
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02005044 /* Subtract 'guiheadroom' from the height to allow some room for the
5045 * window manager (task list and window title bar). */
Bram Moolenaar7be9b502017-09-09 18:45:26 +02005046 *screen_h -= p_ghr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
5048 /*
5049 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
5050 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02005051 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 * This should be completely changed later.
5053 */
5054 *screen_w -= get_menu_tool_width();
5055 *screen_h -= get_menu_tool_height();
5056}
5057
5058#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005060gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 if (title != NULL && output_conv.vc_type != CONV_NONE)
5063 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
5065 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
5066
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 if (output_conv.vc_type != CONV_NONE)
5068 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069}
5070#endif /* FEAT_TITLE */
5071
5072#if defined(FEAT_MENU) || defined(PROTO)
5073 void
5074gui_mch_enable_menu(int showit)
5075{
5076 GtkWidget *widget;
5077
5078# ifdef FEAT_GUI_GNOME
5079 if (using_gnome)
5080 widget = gui.menubar_h;
5081 else
5082# endif
5083 widget = gui.menubar;
5084
Bram Moolenaar18144c82006-04-12 21:52:12 +00005085 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
Bram Moolenaar98921892016-02-23 17:14:37 +01005086# if GTK_CHECK_VERSION(3,0,0)
5087 if (!showit != !gtk_widget_get_visible(widget) && !gui.starting)
5088# else
Bram Moolenaar18144c82006-04-12 21:52:12 +00005089 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar98921892016-02-23 17:14:37 +01005090# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 if (showit)
5093 gtk_widget_show(widget);
5094 else
5095 gtk_widget_hide(widget);
5096
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005097 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 }
5099}
5100#endif /* FEAT_MENU */
5101
5102#if defined(FEAT_TOOLBAR) || defined(PROTO)
5103 void
5104gui_mch_show_toolbar(int showit)
5105{
5106 GtkWidget *widget;
5107
5108 if (gui.toolbar == NULL)
5109 return;
5110
5111# ifdef FEAT_GUI_GNOME
5112 if (using_gnome)
5113 widget = gui.toolbar_h;
5114 else
5115# endif
5116 widget = gui.toolbar;
5117
5118 if (showit)
5119 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
5120
Bram Moolenaar98921892016-02-23 17:14:37 +01005121# if GTK_CHECK_VERSION(3,0,0)
5122 if (!showit != !gtk_widget_get_visible(widget))
5123# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (!showit != !GTK_WIDGET_VISIBLE(widget))
Bram Moolenaar98921892016-02-23 17:14:37 +01005125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
5127 if (showit)
5128 gtk_widget_show(widget);
5129 else
5130 gtk_widget_hide(widget);
5131
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005132 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134}
5135#endif /* FEAT_TOOLBAR */
5136
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137/*
5138 * Check if a given font is a CJK font. This is done in a very crude manner. It
5139 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
5140 * font. Consequently, this function cannot be used as a general purpose check
5141 * for CJK-ness for which fontconfig APIs should be used. This is only used by
5142 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
5143 */
5144 static int
5145is_cjk_font(PangoFontDescription *font_desc)
5146{
5147 static const char * const cjk_langs[] =
5148 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
5149
5150 PangoFont *font;
5151 unsigned i;
5152 int is_cjk = FALSE;
5153
5154 font = pango_context_load_font(gui.text_context, font_desc);
5155
5156 if (font == NULL)
5157 return FALSE;
5158
5159 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
5160 {
5161 PangoCoverage *coverage;
5162 gunichar uc;
5163
5164 coverage = pango_font_get_coverage(
5165 font, pango_language_from_string(cjk_langs[i]));
5166
5167 if (coverage != NULL)
5168 {
5169 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
5170 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
5171 pango_coverage_unref(coverage);
5172 }
5173 }
5174
5175 g_object_unref(font);
5176
5177 return is_cjk;
5178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
Bram Moolenaar231334e2005-07-25 20:46:57 +00005180/*
5181 * Adjust gui.char_height (after 'linespace' was changed).
5182 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00005184gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 PangoFontMetrics *metrics;
5187 int ascent;
5188 int descent;
5189
5190 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
5191 pango_context_get_language(gui.text_context));
5192 ascent = pango_font_metrics_get_ascent(metrics);
5193 descent = pango_font_metrics_get_descent(metrics);
5194
5195 pango_font_metrics_unref(metrics);
5196
5197 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00005198 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005199 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
5201
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 /* A not-positive value of char_height may crash Vim. Only happens
5203 * if 'linespace' is negative (which does make sense sometimes). */
5204 gui.char_ascent = MAX(gui.char_ascent, 0);
5205 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5206
5207 return OK;
5208}
5209
Bram Moolenaar98921892016-02-23 17:14:37 +01005210#if GTK_CHECK_VERSION(3,0,0)
5211/* Callback function used in gui_mch_font_dialog() */
5212 static gboolean
5213font_filter(const PangoFontFamily *family,
5214 const PangoFontFace *face UNUSED,
5215 gpointer data UNUSED)
5216{
5217 return pango_font_family_is_monospace((PangoFontFamily *)family);
5218}
5219#endif
5220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221/*
5222 * Put up a font dialog and return the selected font name in allocated memory.
5223 * "oldval" is the previous value. Return NULL when cancelled.
5224 * This should probably go into gui_gtk.c. Hmm.
5225 * FIXME:
5226 * The GTK2 font selection dialog has no filtering API. So we could either
5227 * a) implement our own (possibly copying the code from somewhere else) or
5228 * b) just live with it.
5229 */
5230 char_u *
5231gui_mch_font_dialog(char_u *oldval)
5232{
5233 GtkWidget *dialog;
5234 int response;
5235 char_u *fontname = NULL;
5236 char_u *oldname;
5237
Bram Moolenaar98921892016-02-23 17:14:37 +01005238#if GTK_CHECK_VERSION(3,2,0)
5239 dialog = gtk_font_chooser_dialog_new(NULL, NULL);
5240 gtk_font_chooser_set_filter_func(GTK_FONT_CHOOSER(dialog), font_filter,
5241 NULL, NULL);
5242#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 dialog = gtk_font_selection_dialog_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01005244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
5247 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
5248
5249 if (oldval != NULL && oldval[0] != NUL)
5250 {
5251 if (output_conv.vc_type != CONV_NONE)
5252 oldname = string_convert(&output_conv, oldval, NULL);
5253 else
5254 oldname = oldval;
5255
5256 /* Annoying bug in GTK (or Pango): if the font name does not include a
5257 * size, zero is used. Use default point size ten. */
5258 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
5259 {
5260 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
5261
5262 if (p != NULL)
5263 {
5264 STRCPY(p + STRLEN(p), " 10");
5265 if (oldname != oldval)
5266 vim_free(oldname);
5267 oldname = p;
5268 }
5269 }
5270
Bram Moolenaar98921892016-02-23 17:14:37 +01005271#if GTK_CHECK_VERSION(3,2,0)
5272 gtk_font_chooser_set_font(
5273 GTK_FONT_CHOOSER(dialog), (const gchar *)oldname);
5274#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 gtk_font_selection_dialog_set_font_name(
5276 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
Bram Moolenaar98921892016-02-23 17:14:37 +01005277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005282 else
Bram Moolenaar98921892016-02-23 17:14:37 +01005283#if GTK_CHECK_VERSION(3,2,0)
5284 gtk_font_chooser_set_font(
5285 GTK_FONT_CHOOSER(dialog), DEFAULT_FONT);
5286#else
Bram Moolenaarbef9d832009-09-11 13:46:41 +00005287 gtk_font_selection_dialog_set_font_name(
5288 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar98921892016-02-23 17:14:37 +01005289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 response = gtk_dialog_run(GTK_DIALOG(dialog));
5292
5293 if (response == GTK_RESPONSE_OK)
5294 {
5295 char *name;
5296
Bram Moolenaar98921892016-02-23 17:14:37 +01005297#if GTK_CHECK_VERSION(3,2,0)
5298 name = gtk_font_chooser_get_font(GTK_FONT_CHOOSER(dialog));
5299#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 name = gtk_font_selection_dialog_get_font_name(
5301 GTK_FONT_SELECTION_DIALOG(dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01005302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (name != NULL)
5304 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005305 char_u *p;
5306
5307 /* Apparently some font names include a comma, need to escape
5308 * that, because in 'guifont' it separates names. */
5309 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00005311 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005312 {
5313 fontname = string_convert(&input_conv, p, NULL);
5314 vim_free(p);
5315 }
5316 else
5317 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 }
5319 }
5320
5321 if (response != GTK_RESPONSE_NONE)
5322 gtk_widget_destroy(dialog);
5323
5324 return fontname;
5325}
5326
5327/*
5328 * Some monospace fonts don't support a bold weight, and fall back
5329 * silently to the regular weight. But this is no good since our text
5330 * drawing function can emulate bold by overstriking. So let's try
5331 * to detect whether bold weight is actually available and emulate it
5332 * otherwise.
5333 *
5334 * Note that we don't need to check for italic style since Xft can
5335 * emulate italic on its own, provided you have a proper fontconfig
5336 * setup. We wouldn't be able to emulate it in Vim anyway.
5337 */
5338 static void
5339get_styled_font_variants(void)
5340{
5341 PangoFontDescription *bold_font_desc;
5342 PangoFont *plain_font;
5343 PangoFont *bold_font;
5344
5345 gui.font_can_bold = FALSE;
5346
5347 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
5348
5349 if (plain_font == NULL)
5350 return;
5351
5352 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
5353 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
5354
5355 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
5356 /*
5357 * The comparison relies on the unique handle nature of a PangoFont*,
5358 * i.e. it's assumed that a different PangoFont* won't refer to the
5359 * same font. Seems to work, and failing here isn't critical anyway.
5360 */
5361 if (bold_font != NULL)
5362 {
5363 gui.font_can_bold = (bold_font != plain_font);
5364 g_object_unref(bold_font);
5365 }
5366
5367 pango_font_description_free(bold_font_desc);
5368 g_object_unref(plain_font);
5369}
5370
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371static PangoEngineShape *default_shape_engine = NULL;
5372
5373/*
5374 * Create a map from ASCII characters in the range [32,126] to glyphs
5375 * of the current font. This is used by gui_gtk2_draw_string() to skip
5376 * the itemize and shaping process for the most common case.
5377 */
5378 static void
5379ascii_glyph_table_init(void)
5380{
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005381 char_u ascii_chars[2 * 128];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 PangoAttrList *attr_list;
5383 GList *item_list;
5384 int i;
5385
5386 if (gui.ascii_glyphs != NULL)
5387 pango_glyph_string_free(gui.ascii_glyphs);
5388 if (gui.ascii_font != NULL)
5389 g_object_unref(gui.ascii_font);
5390
5391 gui.ascii_glyphs = NULL;
5392 gui.ascii_font = NULL;
5393
Bram Moolenaar16350cb2016-08-14 20:27:34 +02005394 /* For safety, fill in question marks for the control characters.
5395 * Put a space between characters to avoid shaping. */
5396 for (i = 0; i < 128; ++i)
5397 {
5398 if (i >= 32 && i < 127)
5399 ascii_chars[2 * i] = i;
5400 else
5401 ascii_chars[2 * i] = '?';
5402 ascii_chars[2 * i + 1] = ' ';
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 attr_list = pango_attr_list_new();
5406 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5407 0, sizeof(ascii_chars), attr_list, NULL);
5408
5409 if (item_list != NULL && item_list->next == NULL) /* play safe */
5410 {
5411 PangoItem *item;
5412 int width;
5413
5414 item = (PangoItem *)item_list->data;
5415 width = gui.char_width * PANGO_SCALE;
5416
5417 /* Remember the shape engine used for ASCII. */
5418 default_shape_engine = item->analysis.shape_engine;
5419
5420 gui.ascii_font = item->analysis.font;
5421 g_object_ref(gui.ascii_font);
5422
5423 gui.ascii_glyphs = pango_glyph_string_new();
5424
5425 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5426 &item->analysis, gui.ascii_glyphs);
5427
5428 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5429
5430 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5431 {
5432 PangoGlyphGeometry *geom;
5433
5434 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5435 geom->x_offset += MAX(0, width - geom->width) / 2;
5436 geom->width = width;
5437 }
5438 }
5439
5440 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5441 g_list_free(item_list);
5442 pango_attr_list_unref(attr_list);
5443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445/*
5446 * Initialize Vim to use the font or fontset with the given name.
5447 * Return FAIL if the font could not be loaded, OK otherwise.
5448 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005450gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 PangoFontDescription *font_desc;
5453 PangoLayout *layout;
5454 int width;
5455
5456 /* If font_name is NULL, this means to use the default, which should
5457 * be present on all proper Pango/fontconfig installations. */
5458 if (font_name == NULL)
5459 font_name = (char_u *)DEFAULT_FONT;
5460
5461 font_desc = gui_mch_get_font(font_name, FALSE);
5462
5463 if (font_desc == NULL)
5464 return FAIL;
5465
5466 gui_mch_free_font(gui.norm_font);
5467 gui.norm_font = font_desc;
5468
5469 pango_context_set_font_description(gui.text_context, font_desc);
5470
5471 layout = pango_layout_new(gui.text_context);
5472 pango_layout_set_text(layout, "MW", 2);
5473 pango_layout_get_size(layout, &width, NULL);
5474 /*
5475 * Set char_width to half the width obtained from pango_layout_get_size()
5476 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5477 * Pango to use the same width for both non-CJK characters (e.g. Latin
5478 * letters and numbers) and CJK characters. This results in 's p a c e d
5479 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5480 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5481 * width for CJK fonts.
5482 *
5483 * For related bugs, see:
5484 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5485 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5486 *
5487 * With this, for all four of the following cases, Vim works fine:
5488 * guifont=CJK_fixed_width_font
5489 * guifont=Non_CJK_fixed_font
5490 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5491 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5492 */
5493 if (is_cjk_font(gui.norm_font))
5494 {
5495 int cjk_width;
5496
5497 /* Measure the text extent of U+4E00 and U+4E8C */
5498 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5499 pango_layout_get_size(layout, &cjk_width, NULL);
5500
5501 if (width == cjk_width) /* Xft not patched */
5502 width /= 2;
5503 }
5504 g_object_unref(layout);
5505
5506 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5507
5508 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5509 if (gui.char_width <= 0)
5510 gui.char_width = 8;
5511
Bram Moolenaar231334e2005-07-25 20:46:57 +00005512 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
5514 /* Set the fontname, which will be used for information purposes */
5515 hl_set_font_name(font_name);
5516
5517 get_styled_font_variants();
5518 ascii_glyph_table_init();
5519
5520 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5521 if (gui.wide_font != NULL
5522 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5523 {
5524 pango_font_description_free(gui.wide_font);
5525 gui.wide_font = NULL;
5526 }
5527
Bram Moolenaare161c792009-11-03 17:13:59 +00005528 if (gui_mch_maximized())
5529 {
Bram Moolenaare161c792009-11-03 17:13:59 +00005530 /* Update lines and columns in accordance with the new font, keep the
5531 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02005532 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00005533 }
5534 else
Bram Moolenaare161c792009-11-03 17:13:59 +00005535 {
5536 /* Preserve the logical dimensions of the screen. */
5537 update_window_manager_hints(0, 0);
5538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
5540 return OK;
5541}
5542
5543/*
5544 * Get a reference to the font "name".
5545 * Return zero for failure.
5546 */
5547 GuiFont
5548gui_mch_get_font(char_u *name, int report_error)
5549{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
5552 /* can't do this when GUI is not running */
5553 if (!gui.in_use || name == NULL)
5554 return NULL;
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (output_conv.vc_type != CONV_NONE)
5557 {
5558 char_u *buf;
5559
5560 buf = string_convert(&output_conv, name, NULL);
5561 if (buf != NULL)
5562 {
5563 font = pango_font_description_from_string((const char *)buf);
5564 vim_free(buf);
5565 }
5566 else
5567 font = NULL;
5568 }
5569 else
5570 font = pango_font_description_from_string((const char *)name);
5571
5572 if (font != NULL)
5573 {
5574 PangoFont *real_font;
5575
5576 /* pango_context_load_font() bails out if no font size is set */
5577 if (pango_font_description_get_size(font) <= 0)
5578 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5579
5580 real_font = pango_context_load_font(gui.text_context, font);
5581
5582 if (real_font == NULL)
5583 {
5584 pango_font_description_free(font);
5585 font = NULL;
5586 }
5587 else
5588 g_object_unref(real_font);
5589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590
5591 if (font == NULL)
5592 {
5593 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005594 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 return NULL;
5596 }
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 return font;
5599}
5600
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005601#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005602/*
5603 * Return the name of font "font" in allocated memory.
5604 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005605 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005606gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005607{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005608 if (font != NOFONT)
5609 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005610 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005611
Bram Moolenaar89d40322006-08-29 15:30:07 +00005612 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005613 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005614 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005615
Bram Moolenaar89d40322006-08-29 15:30:07 +00005616 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005617 return s;
5618 }
5619 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005620 return NULL;
5621}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005622#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624/*
5625 * If a font is not going to be used, free its structure.
5626 */
5627 void
5628gui_mch_free_font(GuiFont font)
5629{
5630 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632}
5633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634/*
Bram Moolenaar36edf062016-07-21 22:10:12 +02005635 * Return the Pixel value (color) for the given color name.
5636 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 * Return INVALCOLOR for error.
5638 */
5639 guicolor_T
5640gui_mch_get_color(char_u *name)
5641{
Bram Moolenaar2e324952018-04-14 14:37:07 +02005642 guicolor_T color = INVALCOLOR;
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 if (!gui.in_use) /* can't do this when GUI not running */
Bram Moolenaar2e324952018-04-14 14:37:07 +02005645 return color;
5646
5647 if (name != NULL)
5648 color = gui_get_color_cmn(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar98921892016-02-23 17:14:37 +01005650#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar2e324952018-04-14 14:37:07 +02005651 return color;
Bram Moolenaar98921892016-02-23 17:14:37 +01005652#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02005653 if (color == INVALCOLOR)
5654 return INVALCOLOR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
Bram Moolenaar26af85d2017-07-23 16:45:10 +02005656 return gui_mch_get_rgb_color(
5657 (color & 0xff0000) >> 16,
5658 (color & 0xff00) >> 8,
5659 color & 0xff);
5660#endif
5661}
5662
5663/*
5664 * Return the Pixel value (color) for the given RGB values.
5665 * Return INVALCOLOR for error.
5666 */
5667 guicolor_T
5668gui_mch_get_rgb_color(int r, int g, int b)
5669{
5670#if GTK_CHECK_VERSION(3,0,0)
5671 return gui_get_rgb_color_cmn(r, g, b);
5672#else
5673 GdkColor gcolor;
5674 int ret;
5675
5676 gcolor.red = (guint16)(r / 255.0 * 65535 + 0.5);
5677 gcolor.green = (guint16)(g / 255.0 * 65535 + 0.5);
5678 gcolor.blue = (guint16)(b / 255.0 * 65535 + 0.5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaar36edf062016-07-21 22:10:12 +02005680 ret = gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5681 &gcolor, FALSE, TRUE);
5682
5683 return ret != 0 ? (guicolor_T)gcolor.pixel : INVALCOLOR;
5684#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687/*
5688 * Set the current text foreground color.
5689 */
5690 void
5691gui_mch_set_fg_color(guicolor_T color)
5692{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005693#if GTK_CHECK_VERSION(3,0,0)
5694 *gui.fgcolor = color_to_rgba(color);
5695#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 gui.fgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698}
5699
5700/*
5701 * Set the current text background color.
5702 */
5703 void
5704gui_mch_set_bg_color(guicolor_T color)
5705{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005706#if GTK_CHECK_VERSION(3,0,0)
5707 *gui.bgcolor = color_to_rgba(color);
5708#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 gui.bgcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711}
5712
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005713/*
5714 * Set the current text special color.
5715 */
5716 void
5717gui_mch_set_sp_color(guicolor_T color)
5718{
Bram Moolenaar36edf062016-07-21 22:10:12 +02005719#if GTK_CHECK_VERSION(3,0,0)
5720 *gui.spcolor = color_to_rgba(color);
5721#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005722 gui.spcolor->pixel = (unsigned long)color;
Bram Moolenaar36edf062016-07-21 22:10:12 +02005723#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005724}
5725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726/*
5727 * Function-like convenience macro for the sake of efficiency.
5728 */
5729#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5730 G_STMT_START{ \
5731 PangoAttribute *tmp_attr_; \
5732 tmp_attr_ = (Attribute); \
5733 tmp_attr_->start_index = (Start); \
5734 tmp_attr_->end_index = (End); \
5735 pango_attr_list_insert((AttrList), tmp_attr_); \
5736 }G_STMT_END
5737
5738 static void
5739apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5740{
5741 char_u *start = NULL;
5742 char_u *p;
5743 int uc;
5744
5745 for (p = s; p < s + len; p += utf_byte2len(*p))
5746 {
5747 uc = utf_ptr2char(p);
5748
5749 if (start == NULL)
5750 {
5751 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5752 start = p;
5753 }
5754 else if (uc < 0x80 /* optimization shortcut */
5755 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5756 {
5757 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5758 attr_list, start - s, p - s);
5759 start = NULL;
5760 }
5761 }
5762
5763 if (start != NULL)
5764 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5765 attr_list, start - s, len);
5766}
5767
5768 static int
5769count_cluster_cells(char_u *s, PangoItem *item,
5770 PangoGlyphString* glyphs, int i,
5771 int *cluster_width,
5772 int *last_glyph_rbearing)
5773{
5774 char_u *p;
5775 int next; /* glyph start index of next cluster */
5776 int start, end; /* string segment of current cluster */
5777 int width; /* real cluster width in Pango units */
5778 int uc;
5779 int cellcount = 0;
5780
5781 width = glyphs->glyphs[i].geometry.width;
5782
5783 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5784 {
5785 if (glyphs->glyphs[next].attr.is_cluster_start)
5786 break;
5787 else if (glyphs->glyphs[next].geometry.width > width)
5788 width = glyphs->glyphs[next].geometry.width;
5789 }
5790
5791 start = item->offset + glyphs->log_clusters[i];
5792 end = item->offset + ((next < glyphs->num_glyphs) ?
5793 glyphs->log_clusters[next] : item->length);
5794
5795 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5796 {
5797 uc = utf_ptr2char(p);
5798 if (uc < 0x80)
5799 ++cellcount;
5800 else if (!utf_iscomposing(uc))
5801 cellcount += utf_char2cells(uc);
5802 }
5803
5804 if (last_glyph_rbearing != NULL
5805 && cellcount > 0 && next == glyphs->num_glyphs)
5806 {
5807 PangoRectangle ink_rect;
5808 /*
5809 * If a certain combining mark had to be taken from a non-monospace
5810 * font, we have to compensate manually by adapting x_offset according
5811 * to the ink extents of the previous glyph.
5812 */
5813 pango_font_get_glyph_extents(item->analysis.font,
5814 glyphs->glyphs[i].glyph,
5815 &ink_rect, NULL);
5816
5817 if (PANGO_RBEARING(ink_rect) > 0)
5818 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5819 }
5820
5821 if (cellcount > 0)
5822 *cluster_width = width;
5823
5824 return cellcount;
5825}
5826
5827/*
5828 * If there are only combining characters in the cluster, we cannot just
5829 * change the width of the previous glyph since there is none. Therefore
5830 * some guesswork is needed.
5831 *
5832 * If ink_rect.x is negative Pango apparently has taken care of the composing
5833 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5834 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02005835 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 *
5837 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5838 * the position of the previous glyph. Apparently this happens only with old
5839 * X fonts which don't provide the special combining information needed by
5840 * Pango.
5841 */
5842 static void
5843setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5844 int last_cellcount, int last_cluster_width,
5845 int last_glyph_rbearing)
5846{
5847 PangoRectangle ink_rect;
5848 PangoRectangle logical_rect;
5849 int width;
5850
5851 width = last_cellcount * gui.char_width * PANGO_SCALE;
5852 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5853 glyph->geometry.width = 0;
5854
5855 pango_font_get_glyph_extents(item->analysis.font,
5856 glyph->glyph,
5857 &ink_rect, &logical_rect);
5858 if (ink_rect.x < 0)
5859 {
5860 glyph->geometry.x_offset += last_glyph_rbearing;
5861 glyph->geometry.y_offset = logical_rect.height
5862 - (gui.char_height - p_linespace) * PANGO_SCALE;
5863 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005864 else
5865 /* If the accent width is smaller than the cluster width, position it
5866 * in the middle. */
5867 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868}
5869
Bram Moolenaar98921892016-02-23 17:14:37 +01005870#if GTK_CHECK_VERSION(3,0,0)
5871 static void
5872draw_glyph_string(int row, int col, int num_cells, int flags,
5873 PangoFont *font, PangoGlyphString *glyphs,
5874 cairo_t *cr)
5875#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 static void
5877draw_glyph_string(int row, int col, int num_cells, int flags,
5878 PangoFont *font, PangoGlyphString *glyphs)
Bram Moolenaar98921892016-02-23 17:14:37 +01005879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 if (!(flags & DRAW_TRANSP))
5882 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005883#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005884 cairo_set_source_rgba(cr,
5885 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
5886 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005887 cairo_rectangle(cr,
5888 FILL_X(col), FILL_Y(row),
5889 num_cells * gui.char_width, gui.char_height);
5890 cairo_fill(cr);
5891#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5893
5894 gdk_draw_rectangle(gui.drawarea->window,
5895 gui.text_gc,
5896 TRUE,
5897 FILL_X(col),
5898 FILL_Y(row),
5899 num_cells * gui.char_width,
5900 gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01005901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 }
5903
Bram Moolenaar98921892016-02-23 17:14:37 +01005904#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02005905 cairo_set_source_rgba(cr,
5906 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5907 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005908 cairo_move_to(cr, TEXT_X(col), TEXT_Y(row));
5909 pango_cairo_show_glyph_string(cr, font, glyphs);
5910#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5912
5913 gdk_draw_glyphs(gui.drawarea->window,
5914 gui.text_gc,
5915 font,
5916 TEXT_X(col),
5917 TEXT_Y(row),
5918 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920
5921 /* redraw the contents with an offset of 1 to emulate bold */
5922 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
Bram Moolenaar98921892016-02-23 17:14:37 +01005923#if GTK_CHECK_VERSION(3,0,0)
5924 {
Bram Moolenaar36edf062016-07-21 22:10:12 +02005925 cairo_set_source_rgba(cr,
5926 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
5927 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005928 cairo_move_to(cr, TEXT_X(col) + 1, TEXT_Y(row));
5929 pango_cairo_show_glyph_string(cr, font, glyphs);
5930 }
5931#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 gdk_draw_glyphs(gui.drawarea->window,
5933 gui.text_gc,
5934 font,
5935 TEXT_X(col) + 1,
5936 TEXT_Y(row),
5937 glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01005938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939}
5940
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005941/*
5942 * Draw underline and undercurl at the bottom of the character cell.
5943 */
Bram Moolenaar98921892016-02-23 17:14:37 +01005944#if GTK_CHECK_VERSION(3,0,0)
5945 static void
5946draw_under(int flags, int row, int col, int cells, cairo_t *cr)
5947#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005948 static void
5949draw_under(int flags, int row, int col, int cells)
Bram Moolenaar98921892016-02-23 17:14:37 +01005950#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005951{
5952 int i;
5953 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005954 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005955 int y = FILL_Y(row + 1) - 1;
5956
5957 /* Undercurl: draw curl at the bottom of the character cell. */
5958 if (flags & DRAW_UNDERC)
5959 {
Bram Moolenaar98921892016-02-23 17:14:37 +01005960#if GTK_CHECK_VERSION(3,0,0)
5961 cairo_set_line_width(cr, 1.0);
5962 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
Bram Moolenaar36edf062016-07-21 22:10:12 +02005963 cairo_set_source_rgba(cr,
5964 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5965 gui.spcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01005966 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5967 {
5968 offset = val[i % 8];
5969 cairo_line_to(cr, i, y - offset + 0.5);
5970 }
5971 cairo_stroke(cr);
5972#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005973 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5974 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5975 {
5976 offset = val[i % 8];
5977 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5978 }
5979 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01005980#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005981 }
5982
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02005983 /* Draw a strikethrough line */
5984 if (flags & DRAW_STRIKE)
5985 {
5986#if GTK_CHECK_VERSION(3,0,0)
5987 cairo_set_line_width(cr, 1.0);
5988 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
5989 cairo_set_source_rgba(cr,
5990 gui.spcolor->red, gui.spcolor->green, gui.spcolor->blue,
5991 gui.spcolor->alpha);
5992 cairo_move_to(cr, FILL_X(col), y + 1 - gui.char_height/2 + 0.5);
5993 cairo_line_to(cr, FILL_X(col + cells), y + 1 - gui.char_height/2 + 0.5);
5994 cairo_stroke(cr);
5995#else
5996 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5997 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5998 FILL_X(col), y + 1 - gui.char_height/2,
5999 FILL_X(col + cells), y + 1 - gui.char_height/2);
6000 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6001#endif
6002 }
6003
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006004 /* Underline: draw a line at the bottom of the character cell. */
6005 if (flags & DRAW_UNDERL)
6006 {
6007 /* When p_linespace is 0, overwrite the bottom row of pixels.
6008 * Otherwise put the line just below the character. */
6009 if (p_linespace > 1)
6010 y -= p_linespace - 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006011#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaarcf4b00c2017-09-02 18:33:56 +02006012 cairo_set_line_width(cr, 1.0);
6013 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6014 cairo_set_source_rgba(cr,
6015 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6016 gui.fgcolor->alpha);
6017 cairo_move_to(cr, FILL_X(col), y + 0.5);
6018 cairo_line_to(cr, FILL_X(col + cells), y + 0.5);
6019 cairo_stroke(cr);
Bram Moolenaar98921892016-02-23 17:14:37 +01006020#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006021 gdk_draw_line(gui.drawarea->window, gui.text_gc,
6022 FILL_X(col), y,
6023 FILL_X(col + cells) - 1, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01006024#endif
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006025 }
6026}
6027
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 int
6029gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
6030{
6031 GdkRectangle area; /* area for clip mask */
6032 PangoGlyphString *glyphs; /* glyphs of current item */
6033 int column_offset = 0; /* column offset in cells */
6034 int i;
6035 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
6036 char_u *new_conv_buf;
6037 int convlen;
6038 char_u *sp, *bp;
6039 int plen;
Bram Moolenaar98921892016-02-23 17:14:37 +01006040#if GTK_CHECK_VERSION(3,0,0)
6041 cairo_t *cr;
6042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
Bram Moolenaar98921892016-02-23 17:14:37 +01006044#if GTK_CHECK_VERSION(3,0,0)
6045 if (gui.text_context == NULL || gtk_widget_get_window(gui.drawarea) == NULL)
6046#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 if (gui.text_context == NULL || gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 return len;
6050
6051 if (output_conv.vc_type != CONV_NONE)
6052 {
6053 /*
6054 * Convert characters from 'encoding' to 'termencoding', which is set
6055 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
6056 * prohibits changing this to something else than UTF-8 if the GUI is
6057 * in use.
6058 */
6059 convlen = len;
6060 conv_buf = string_convert(&output_conv, s, &convlen);
6061 g_return_val_if_fail(conv_buf != NULL, len);
6062
6063 /* Correct for differences in char width: some chars are
6064 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
6065 * compensate for that. */
6066 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
6067 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006068 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
6070 {
6071 new_conv_buf = alloc(convlen + 2);
6072 if (new_conv_buf == NULL)
6073 return len;
6074 plen += bp - conv_buf;
6075 mch_memmove(new_conv_buf, conv_buf, plen);
6076 new_conv_buf[plen] = ' ';
6077 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
6078 convlen - plen + 1);
6079 vim_free(conv_buf);
6080 conv_buf = new_conv_buf;
6081 ++convlen;
6082 bp = conv_buf + plen;
6083 plen = 1;
6084 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006085 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 bp += plen;
6087 }
6088 s = conv_buf;
6089 len = convlen;
6090 }
6091
6092 /*
6093 * Restrict all drawing to the current screen line in order to prevent
6094 * fuzzy font lookups from messing up the screen.
6095 */
6096 area.x = gui.border_offset;
6097 area.y = FILL_Y(row);
6098 area.width = gui.num_cols * gui.char_width;
6099 area.height = gui.char_height;
6100
Bram Moolenaar98921892016-02-23 17:14:37 +01006101#if GTK_CHECK_VERSION(3,0,0)
6102 cr = cairo_create(gui.surface);
6103 cairo_rectangle(cr, area.x, area.y, area.width, area.height);
6104 cairo_clip(cr);
6105#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
6107 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
Bram Moolenaar98921892016-02-23 17:14:37 +01006108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
6110 glyphs = pango_glyph_string_new();
6111
6112 /*
6113 * Optimization hack: If possible, skip the itemize and shaping process
6114 * for pure ASCII strings. This optimization is particularly effective
6115 * because Vim draws space characters to clear parts of the screen.
6116 */
6117 if (!(flags & DRAW_ITALIC)
6118 && !((flags & DRAW_BOLD) && gui.font_can_bold)
6119 && gui.ascii_glyphs != NULL)
6120 {
6121 char_u *p;
6122
6123 for (p = s; p < s + len; ++p)
6124 if (*p & 0x80)
6125 goto not_ascii;
6126
6127 pango_glyph_string_set_size(glyphs, len);
6128
6129 for (i = 0; i < len; ++i)
6130 {
Bram Moolenaar16350cb2016-08-14 20:27:34 +02006131 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[2 * s[i]];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 glyphs->log_clusters[i] = i;
6133 }
6134
Bram Moolenaar98921892016-02-23 17:14:37 +01006135#if GTK_CHECK_VERSION(3,0,0)
6136 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs, cr);
6137#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141 column_offset = len;
6142 }
6143 else
6144not_ascii:
6145 {
6146 PangoAttrList *attr_list;
6147 GList *item_list;
6148 int cluster_width;
6149 int last_glyph_rbearing;
6150 int cells = 0; /* cells occupied by current cluster */
6151
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006152 /* Safety check: pango crashes when invoked with invalid utf-8
6153 * characters. */
6154 if (!utf_valid_string(s, s + len))
6155 {
6156 column_offset = len;
6157 goto skipitall;
6158 }
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 /* original width of the current cluster */
6161 cluster_width = PANGO_SCALE * gui.char_width;
6162
6163 /* right bearing of the last non-composing glyph */
6164 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6165
6166 attr_list = pango_attr_list_new();
6167
6168 /* If 'guifontwide' is set then use that for double-width characters.
6169 * Otherwise just go with 'guifont' and let Pango do its thing. */
6170 if (gui.wide_font != NULL)
6171 apply_wide_font_attr(s, len, attr_list);
6172
6173 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6174 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6175 attr_list, 0, len);
6176 if (flags & DRAW_ITALIC)
6177 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6178 attr_list, 0, len);
6179 /*
6180 * Break the text into segments with consistent directional level
6181 * and shaping engine. Pure Latin text needs only a single segment,
6182 * so there's no need to worry about the loop's efficiency. Better
6183 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6184 */
6185 item_list = pango_itemize(gui.text_context,
6186 (const char *)s, 0, len, attr_list, NULL);
6187
6188 while (item_list != NULL)
6189 {
6190 PangoItem *item;
6191 int item_cells = 0; /* item length in cells */
6192
6193 item = (PangoItem *)item_list->data;
6194 item_list = g_list_delete_link(item_list, item_list);
6195 /*
6196 * Increment the bidirectional embedding level by 1 if it is not
6197 * even. An odd number means the output will be RTL, but we don't
6198 * want that since Vim handles right-to-left text on its own. It
6199 * would probably be sufficient to just set level = 0, but you can
6200 * never know :)
6201 *
6202 * Unfortunately we can't take advantage of Pango's ability to
6203 * render both LTR and RTL at the same time. In order to support
6204 * that, Vim's main screen engine would have to make use of Pango
6205 * functionality.
6206 */
6207 item->analysis.level = (item->analysis.level + 1) & (~1U);
6208
6209 /* HACK: Overrule the shape engine, we don't want shaping to be
6210 * done, because drawing the cursor would change the display. */
6211 item->analysis.shape_engine = default_shape_engine;
6212
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006213#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02006214 pango_shape_full((const char *)s + item->offset, item->length,
6215 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02006216#else
6217 pango_shape((const char *)s + item->offset, item->length,
6218 &item->analysis, glyphs);
6219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 /*
6221 * Fixed-width hack: iterate over the array and assign a fixed
6222 * width to each glyph, thus overriding the choice made by the
6223 * shaping engine. We use utf_char2cells() to determine the
6224 * number of cells needed.
6225 *
6226 * Also perform all kind of dark magic to get composing
6227 * characters right (and pretty too of course).
6228 */
6229 for (i = 0; i < glyphs->num_glyphs; ++i)
6230 {
6231 PangoGlyphInfo *glyph;
6232
6233 glyph = &glyphs->glyphs[i];
6234
6235 if (glyph->attr.is_cluster_start)
6236 {
6237 int cellcount;
6238
6239 cellcount = count_cluster_cells(
6240 s, item, glyphs, i, &cluster_width,
6241 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6242
6243 if (cellcount > 0)
6244 {
6245 int width;
6246
6247 width = cellcount * gui.char_width * PANGO_SCALE;
6248 glyph->geometry.x_offset +=
6249 MAX(0, width - cluster_width) / 2;
6250 glyph->geometry.width = width;
6251 }
6252 else
6253 {
6254 /* If there are only combining characters in the
6255 * cluster, we cannot just change the width of the
6256 * previous glyph since there is none. Therefore
6257 * some guesswork is needed. */
6258 setup_zero_width_cluster(item, glyph, cells,
6259 cluster_width,
6260 last_glyph_rbearing);
6261 }
6262
6263 item_cells += cellcount;
6264 cells = cellcount;
6265 }
6266 else if (i > 0)
6267 {
6268 int width;
6269
6270 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006271 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02006272 * In some circumstances Pango uses a positive x_offset,
6273 * then use the width of the previous glyph for this one
6274 * and set the previous width to zero.
6275 * Otherwise we get a negative x_offset, Pango has already
6276 * positioned the combining char, keep the widths as they
6277 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006278 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02006279 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006280 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02006281 {
6282 glyphs->glyphs[i].geometry.width =
6283 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02006284 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02006285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 width = cells * gui.char_width * PANGO_SCALE;
6287 glyph->geometry.x_offset +=
6288 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 else /* i == 0 "cannot happen" */
6291 {
6292 glyph->geometry.width = 0;
6293 }
6294 }
6295
6296 /*** Aaaaand action! ***/
Bram Moolenaar98921892016-02-23 17:14:37 +01006297#if GTK_CHECK_VERSION(3,0,0)
6298 draw_glyph_string(row, col + column_offset, item_cells,
6299 flags, item->analysis.font, glyphs,
6300 cr);
6301#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 draw_glyph_string(row, col + column_offset, item_cells,
6303 flags, item->analysis.font, glyphs);
Bram Moolenaar98921892016-02-23 17:14:37 +01006304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 pango_item_free(item);
6307
6308 column_offset += item_cells;
6309 }
6310
6311 pango_attr_list_unref(attr_list);
6312 }
6313
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006314skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006315 /* Draw underline and undercurl. */
Bram Moolenaar98921892016-02-23 17:14:37 +01006316#if GTK_CHECK_VERSION(3,0,0)
6317 draw_under(flags, row, col, column_offset, cr);
6318#else
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006319 draw_under(flags, row, col, column_offset);
Bram Moolenaar98921892016-02-23 17:14:37 +01006320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321
6322 pango_glyph_string_free(glyphs);
6323 vim_free(conv_buf);
6324
Bram Moolenaar98921892016-02-23 17:14:37 +01006325#if GTK_CHECK_VERSION(3,0,0)
6326 cairo_destroy(cr);
6327 if (!gui.by_signal)
6328 gdk_window_invalidate_rect(gtk_widget_get_window(gui.drawarea),
6329 &area, FALSE);
6330#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01006332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 return column_offset;
6335}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337/*
6338 * Return OK if the key with the termcap name "name" is supported.
6339 */
6340 int
6341gui_mch_haskey(char_u *name)
6342{
6343 int i;
6344
6345 for (i = 0; special_keys[i].key_sym != 0; i++)
6346 if (name[0] == special_keys[i].code0
6347 && name[1] == special_keys[i].code1)
6348 return OK;
6349 return FAIL;
6350}
6351
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01006352#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353/*
6354 * Return the text window-id and display. Only required for X-based GUI's
6355 */
6356 int
6357gui_get_x11_windis(Window *win, Display **dis)
6358{
Bram Moolenaar98921892016-02-23 17:14:37 +01006359#if GTK_CHECK_VERSION(3,0,0)
6360 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6361#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 {
Bram Moolenaar98921892016-02-23 17:14:37 +01006365#if GTK_CHECK_VERSION(3,0,0)
6366 *dis = GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6367 *win = GDK_WINDOW_XID(gtk_widget_get_window(gui.mainwin));
6368#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6370 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 return OK;
6373 }
6374
6375 *dis = NULL;
6376 *win = 0;
6377 return FAIL;
6378}
6379#endif
6380
6381#if defined(FEAT_CLIENTSERVER) \
6382 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6383
6384 Display *
6385gui_mch_get_display(void)
6386{
Bram Moolenaar98921892016-02-23 17:14:37 +01006387#if GTK_CHECK_VERSION(3,0,0)
6388 if (gui.mainwin != NULL && gtk_widget_get_window(gui.mainwin) != NULL)
6389 return GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin));
6390#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6392 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 else
6395 return NULL;
6396}
6397#endif
6398
6399 void
6400gui_mch_beep(void)
6401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 GdkDisplay *display;
6403
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006404#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006405 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006406#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 display = gtk_widget_get_display(gui.mainwin);
6410 else
6411 display = gdk_display_get_default();
6412
6413 if (display != NULL)
6414 gdk_display_beep(display);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415}
6416
6417 void
6418gui_mch_flash(int msec)
6419{
Bram Moolenaar98921892016-02-23 17:14:37 +01006420#if GTK_CHECK_VERSION(3,0,0)
6421 /* TODO Replace GdkGC with Cairo */
6422 (void)msec;
6423#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 GdkGCValues values;
6425 GdkGC *invert_gc;
6426
6427 if (gui.drawarea->window == NULL)
6428 return;
6429
6430 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6431 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6432 values.function = GDK_XOR;
6433 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6434 &values,
6435 GDK_GC_FOREGROUND |
6436 GDK_GC_BACKGROUND |
6437 GDK_GC_FUNCTION);
6438 gdk_gc_set_exposures(invert_gc,
6439 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6440 /*
6441 * Do a visual beep by changing back and forth in some undetermined way,
6442 * the foreground and background colors. This is due to the fact that
6443 * there can't be really any prediction about the effects of XOR on
6444 * arbitrary X11 servers. However this seems to be enough for what we
6445 * intend it to do.
6446 */
6447 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6448 TRUE,
6449 0, 0,
6450 FILL_X((int)Columns) + gui.border_offset,
6451 FILL_Y((int)Rows) + gui.border_offset);
6452
6453 gui_mch_flush();
6454 ui_delay((long)msec, TRUE); /* wait so many msec */
6455
6456 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6457 TRUE,
6458 0, 0,
6459 FILL_X((int)Columns) + gui.border_offset,
6460 FILL_Y((int)Rows) + gui.border_offset);
6461
6462 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466/*
6467 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6468 */
6469 void
6470gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6471{
Bram Moolenaar98921892016-02-23 17:14:37 +01006472#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006473 const GdkRectangle rect = {
6474 FILL_X(c), FILL_Y(r), nc * gui.char_width, nr * gui.char_height
6475 };
6476 cairo_t * const cr = cairo_create(gui.surface);
6477
Bram Moolenaar36edf062016-07-21 22:10:12 +02006478 set_cairo_source_rgba_from_color(cr, gui.norm_pixel ^ gui.back_pixel);
Bram Moolenaar4fc563b2016-03-12 12:40:58 +01006479# if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,9,2)
6480 cairo_set_operator(cr, CAIRO_OPERATOR_DIFFERENCE);
6481# else
6482 /* Give an implementation for older cairo versions if necessary. */
6483# endif
6484 gdk_cairo_rectangle(cr, &rect);
6485 cairo_fill(cr);
6486
6487 cairo_destroy(cr);
6488
6489 if (!gui.by_signal)
6490 gtk_widget_queue_draw_area(gui.drawarea, rect.x, rect.y,
6491 rect.width, rect.height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006492#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 GdkGCValues values;
6494 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496 if (gui.drawarea->window == NULL)
6497 return;
6498
Bram Moolenaar89d40322006-08-29 15:30:07 +00006499 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6500 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 values.function = GDK_XOR;
6502 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6503 &values,
6504 GDK_GC_FOREGROUND |
6505 GDK_GC_BACKGROUND |
6506 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006507 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6508 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6510 TRUE,
6511 FILL_X(c), FILL_Y(r),
6512 (nc) * gui.char_width, (nr) * gui.char_height);
6513 gdk_gc_destroy(invert_gc);
Bram Moolenaar98921892016-02-23 17:14:37 +01006514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515}
6516
6517/*
6518 * Iconify the GUI window.
6519 */
6520 void
6521gui_mch_iconify(void)
6522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524}
6525
6526#if defined(FEAT_EVAL) || defined(PROTO)
6527/*
6528 * Bring the Vim window to the foreground.
6529 */
6530 void
6531gui_mch_set_foreground(void)
6532{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534}
6535#endif
6536
6537/*
6538 * Draw a cursor without focus.
6539 */
6540 void
6541gui_mch_draw_hollow_cursor(guicolor_T color)
6542{
6543 int i = 1;
Bram Moolenaar98921892016-02-23 17:14:37 +01006544#if GTK_CHECK_VERSION(3,0,0)
6545 cairo_t *cr;
6546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
Bram Moolenaar98921892016-02-23 17:14:37 +01006548#if GTK_CHECK_VERSION(3,0,0)
6549 if (gtk_widget_get_window(gui.drawarea) == NULL)
6550#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 return;
6554
Bram Moolenaar98921892016-02-23 17:14:37 +01006555#if GTK_CHECK_VERSION(3,0,0)
6556 cr = cairo_create(gui.surface);
6557#endif
6558
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 gui_mch_set_fg_color(color);
6560
Bram Moolenaar98921892016-02-23 17:14:37 +01006561#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02006562 cairo_set_source_rgba(cr,
6563 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6564 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006565#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar98921892016-02-23 17:14:37 +01006567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 if (mb_lefthalve(gui.row, gui.col))
6569 i = 2;
Bram Moolenaar98921892016-02-23 17:14:37 +01006570#if GTK_CHECK_VERSION(3,0,0)
6571 cairo_set_line_width(cr, 1.0);
6572 cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
6573 cairo_rectangle(cr,
6574 FILL_X(gui.col) + 0.5, FILL_Y(gui.row) + 0.5,
6575 i * gui.char_width - 1, gui.char_height - 1);
6576 cairo_stroke(cr);
6577 cairo_destroy(cr);
6578#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6580 FALSE,
6581 FILL_X(gui.col), FILL_Y(gui.row),
6582 i * gui.char_width - 1, gui.char_height - 1);
Bram Moolenaar98921892016-02-23 17:14:37 +01006583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584}
6585
6586/*
6587 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6588 * color "color".
6589 */
6590 void
6591gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6592{
Bram Moolenaar98921892016-02-23 17:14:37 +01006593#if GTK_CHECK_VERSION(3,0,0)
6594 if (gtk_widget_get_window(gui.drawarea) == NULL)
6595#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01006597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 return;
6599
6600 gui_mch_set_fg_color(color);
6601
Bram Moolenaar98921892016-02-23 17:14:37 +01006602#if GTK_CHECK_VERSION(3,0,0)
6603 {
6604 cairo_t *cr;
6605
6606 cr = cairo_create(gui.surface);
Bram Moolenaar36edf062016-07-21 22:10:12 +02006607 cairo_set_source_rgba(cr,
6608 gui.fgcolor->red, gui.fgcolor->green, gui.fgcolor->blue,
6609 gui.fgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01006610 cairo_rectangle(cr,
6611# ifdef FEAT_RIGHTLEFT
6612 /* vertical line should be on the right of current point */
6613 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6614# endif
6615 FILL_X(gui.col), FILL_Y(gui.row) + gui.char_height - h,
6616 w, h);
6617 cairo_fill(cr);
6618 cairo_destroy(cr);
6619 }
6620#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6622 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6623 TRUE,
Bram Moolenaar98921892016-02-23 17:14:37 +01006624# ifdef FEAT_RIGHTLEFT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 /* vertical line should be on the right of current point */
6626 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
Bram Moolenaar98921892016-02-23 17:14:37 +01006627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 FILL_X(gui.col),
6629 FILL_Y(gui.row) + gui.char_height - h,
6630 w, h);
Bram Moolenaar98921892016-02-23 17:14:37 +01006631#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632}
6633
6634
6635/*
6636 * Catch up with any queued X11 events. This may put keyboard input into the
6637 * input buffer, call resize call-backs, trigger timers etc. If there is
6638 * nothing in the X11 event queue (& no timers pending), then we return
6639 * immediately.
6640 */
6641 void
6642gui_mch_update(void)
6643{
Bram Moolenaara3f41662010-07-11 19:01:06 +02006644 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
6645 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006648 static timeout_cb_type
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649input_timer_cb(gpointer data)
6650{
6651 int *timed_out = (int *) data;
6652
Bram Moolenaar49325942007-05-10 19:19:59 +00006653 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 *timed_out = TRUE;
6655
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 return FALSE; /* don't happen again */
6657}
6658
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006659#ifdef FEAT_JOB_CHANNEL
6660 static timeout_cb_type
6661channel_poll_cb(gpointer data UNUSED)
6662{
6663 /* Using an event handler for a channel that may be disconnected does
6664 * not work, it hangs. Instead poll for messages. */
6665 channel_handle_events(TRUE);
6666 parse_queued_messages();
6667
6668 return TRUE; /* repeat */
6669}
6670#endif
6671
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672/*
6673 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6674 * from the keyboard.
6675 * wtime == -1 Wait forever.
6676 * wtime == 0 This should never happen.
6677 * wtime > 0 Wait wtime milliseconds for a character.
6678 * Returns OK if a character was found to be available within the given time,
6679 * or FAIL otherwise.
6680 */
6681 int
6682gui_mch_wait_for_chars(long wtime)
6683{
Bram Moolenaar4231da42016-06-02 14:30:04 +02006684 int focus;
6685 guint timer;
6686 static int timed_out;
6687 int retval = FAIL;
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006688#ifdef FEAT_JOB_CHANNEL
6689 guint channel_timer = 0;
6690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 timed_out = FALSE;
6693
6694 /* this timeout makes sure that we will return if no characters arrived in
6695 * time */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 if (wtime > 0)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006697 timer = timeout_add(wtime, input_timer_cb, &timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
6699 timer = 0;
6700
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006701#ifdef FEAT_JOB_CHANNEL
6702 /* If there is a channel with the keep_open flag we need to poll for input
6703 * on them. */
6704 if (channel_any_keep_open())
6705 channel_timer = timeout_add(20, channel_poll_cb, NULL);
6706#endif
6707
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 focus = gui.in_focus;
6709
6710 do
6711 {
6712 /* Stop or start blinking when focus changes */
6713 if (gui.in_focus != focus)
6714 {
6715 if (gui.in_focus)
6716 gui_mch_start_blink();
6717 else
Bram Moolenaar1dd45fb2018-01-31 21:10:01 +01006718 gui_mch_stop_blink(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 focus = gui.in_focus;
6720 }
6721
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006722#ifdef MESSAGE_QUEUE
Bram Moolenaar4231da42016-06-02 14:30:04 +02006723# ifdef FEAT_TIMERS
6724 did_add_timer = FALSE;
6725# endif
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006726 parse_queued_messages();
Bram Moolenaar4231da42016-06-02 14:30:04 +02006727# ifdef FEAT_TIMERS
6728 if (did_add_timer)
6729 /* Need to recompute the waiting time. */
6730 goto theend;
6731# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006732#endif
6733
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 /*
6735 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006736 * Skip this if input is available anyway (can happen in rare
6737 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006739 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02006740 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 /* Got char, return immediately */
6743 if (input_available())
6744 {
Bram Moolenaar4231da42016-06-02 14:30:04 +02006745 retval = OK;
6746 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 }
6748 } while (wtime < 0 || !timed_out);
6749
6750 /*
6751 * Flush all eventually pending (drawing) events.
6752 */
6753 gui_mch_update();
6754
Bram Moolenaar4231da42016-06-02 14:30:04 +02006755theend:
6756 if (timer != 0 && !timed_out)
Bram Moolenaar4ab79682017-08-27 14:50:47 +02006757 timeout_remove(timer);
6758#ifdef FEAT_JOB_CHANNEL
6759 if (channel_timer != 0)
6760 timeout_remove(channel_timer);
Bram Moolenaar4231da42016-06-02 14:30:04 +02006761#endif
6762
6763 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764}
6765
6766
6767/****************************************************************************
6768 * Output drawing routines.
6769 ****************************************************************************/
6770
6771
6772/* Flush any output to the screen */
6773 void
6774gui_mch_flush(void)
6775{
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006776#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar98921892016-02-23 17:14:37 +01006777 if (gui.mainwin != NULL && gtk_widget_get_realized(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778#else
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006779 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780#endif
Bram Moolenaarb463e8d2017-06-05 15:07:09 +02006781 gdk_display_flush(gtk_widget_get_display(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782}
6783
6784/*
6785 * Clear a rectangular region of the screen from text pos (row1, col1) to
6786 * (row2, col2) inclusive.
6787 */
6788 void
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006789gui_mch_clear_block(int row1arg, int col1arg, int row2arg, int col2arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
Bram Moolenaarfe344a92017-02-23 12:20:35 +01006791
6792 int col1 = check_col(col1arg);
6793 int col2 = check_col(col2arg);
6794 int row1 = check_row(row1arg);
6795 int row2 = check_row(row2arg);
6796
Bram Moolenaar98921892016-02-23 17:14:37 +01006797#if GTK_CHECK_VERSION(3,0,0)
6798 if (gtk_widget_get_window(gui.drawarea) == NULL)
6799 return;
6800#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 GdkColor color;
6802
6803 if (gui.drawarea->window == NULL)
6804 return;
6805
6806 color.pixel = gui.back_pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01006807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
Bram Moolenaar98921892016-02-23 17:14:37 +01006809#if GTK_CHECK_VERSION(3,0,0)
6810 {
6811 /* Add one pixel to the far right column in case a double-stroked
6812 * bold glyph may sit there. */
6813 const GdkRectangle rect = {
6814 FILL_X(col1), FILL_Y(row1),
6815 (col2 - col1 + 1) * gui.char_width + (col2 == Columns - 1),
6816 (row2 - row1 + 1) * gui.char_height
6817 };
6818 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
6819 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006820# if GTK_CHECK_VERSION(3,22,2)
6821 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6822# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006823 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6824 if (pat != NULL)
6825 cairo_set_source(cr, pat);
6826 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006827 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006828# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006829 gdk_cairo_rectangle(cr, &rect);
6830 cairo_fill(cr);
6831 cairo_destroy(cr);
6832
6833 if (!gui.by_signal)
6834 gdk_window_invalidate_rect(win, &rect, FALSE);
6835 }
6836#else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 gdk_gc_set_foreground(gui.text_gc, &color);
6838
6839 /* Clear one extra pixel at the far right, for when bold characters have
6840 * spilled over to the window border. */
6841 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6842 FILL_X(col1), FILL_Y(row1),
6843 (col2 - col1 + 1) * gui.char_width
6844 + (col2 == Columns - 1),
6845 (row2 - row1 + 1) * gui.char_height);
Bram Moolenaar98921892016-02-23 17:14:37 +01006846#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847}
6848
Bram Moolenaar98921892016-02-23 17:14:37 +01006849#if GTK_CHECK_VERSION(3,0,0)
6850 static void
6851gui_gtk_window_clear(GdkWindow *win)
6852{
6853 const GdkRectangle rect = {
6854 0, 0, gdk_window_get_width(win), gdk_window_get_height(win)
6855 };
6856 cairo_t * const cr = cairo_create(gui.surface);
Bram Moolenaara859f042016-11-17 19:11:55 +01006857# if GTK_CHECK_VERSION(3,22,2)
6858 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
6859# else
Bram Moolenaar98921892016-02-23 17:14:37 +01006860 cairo_pattern_t * const pat = gdk_window_get_background_pattern(win);
6861 if (pat != NULL)
6862 cairo_set_source(cr, pat);
6863 else
Bram Moolenaar36edf062016-07-21 22:10:12 +02006864 set_cairo_source_rgba_from_color(cr, gui.back_pixel);
Bram Moolenaara859f042016-11-17 19:11:55 +01006865# endif
Bram Moolenaar98921892016-02-23 17:14:37 +01006866 gdk_cairo_rectangle(cr, &rect);
6867 cairo_fill(cr);
6868 cairo_destroy(cr);
6869
6870 if (!gui.by_signal)
6871 gdk_window_invalidate_rect(win, &rect, FALSE);
6872}
6873#endif
6874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 void
6876gui_mch_clear_all(void)
6877{
Bram Moolenaar98921892016-02-23 17:14:37 +01006878#if GTK_CHECK_VERSION(3,0,0)
6879 if (gtk_widget_get_window(gui.drawarea) != NULL)
6880 gui_gtk_window_clear(gtk_widget_get_window(gui.drawarea));
6881#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 if (gui.drawarea->window != NULL)
6883 gdk_window_clear(gui.drawarea->window);
Bram Moolenaar98921892016-02-23 17:14:37 +01006884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885}
6886
Bram Moolenaar98921892016-02-23 17:14:37 +01006887#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888/*
6889 * Redraw any text revealed by scrolling up/down.
6890 */
6891 static void
6892check_copy_area(void)
6893{
6894 GdkEvent *event;
6895 int expose_count;
6896
6897 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6898 return;
6899
6900 /* Avoid redrawing the cursor while scrolling or it'll end up where
6901 * we don't want it to be. I'm not sure if it's correct to call
6902 * gui_dont_update_cursor() at this point but it works as a quick
6903 * fix for now. */
Bram Moolenaar107abd22016-08-12 14:08:25 +02006904 gui_dont_update_cursor(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905
6906 do
6907 {
6908 /* Wait to check whether the scroll worked or not. */
6909 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6910
6911 if (event == NULL)
6912 break; /* received NoExpose event */
6913
6914 gui_redraw(event->expose.area.x, event->expose.area.y,
6915 event->expose.area.width, event->expose.area.height);
6916
6917 expose_count = event->expose.count;
6918 gdk_event_free(event);
6919 }
6920 while (expose_count > 0); /* more events follow */
6921
6922 gui_can_update_cursor();
6923}
Bram Moolenaar98921892016-02-23 17:14:37 +01006924#endif /* !GTK_CHECK_VERSION(3,0,0) */
6925
6926#if GTK_CHECK_VERSION(3,0,0)
6927 static void
6928gui_gtk_surface_copy_rect(int dest_x, int dest_y,
6929 int src_x, int src_y,
6930 int width, int height)
6931{
6932 cairo_t * const cr = cairo_create(gui.surface);
6933
6934 cairo_rectangle(cr, dest_x, dest_y, width, height);
6935 cairo_clip(cr);
6936 cairo_push_group(cr);
6937 cairo_set_source_surface(cr, gui.surface, dest_x - src_x, dest_y - src_y);
6938 cairo_paint(cr);
6939 cairo_pop_group_to_source(cr);
6940 cairo_paint(cr);
6941
6942 cairo_destroy(cr);
6943}
6944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946/*
6947 * Delete the given number of lines from the given row, scrolling up any
6948 * text further down within the scroll region.
6949 */
6950 void
6951gui_mch_delete_lines(int row, int num_lines)
6952{
Bram Moolenaar98921892016-02-23 17:14:37 +01006953#if GTK_CHECK_VERSION(3,0,0)
6954 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
6955 const int nrows = gui.scroll_region_bot - row + 1;
6956 const int src_nrows = nrows - num_lines;
6957
6958 gui_gtk_surface_copy_rect(
6959 FILL_X(gui.scroll_region_left), FILL_Y(row),
6960 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6961 gui.char_width * ncols + 1, gui.char_height * src_nrows);
6962 gui_clear_block(
6963 gui.scroll_region_bot - num_lines + 1, gui.scroll_region_left,
6964 gui.scroll_region_bot, gui.scroll_region_right);
6965 gui_gtk3_redraw(
6966 FILL_X(gui.scroll_region_left), FILL_Y(row),
6967 gui.char_width * ncols + 1, gui.char_height * nrows);
6968 if (!gui.by_signal)
6969 gtk_widget_queue_draw_area(gui.drawarea,
6970 FILL_X(gui.scroll_region_left), FILL_Y(row),
6971 gui.char_width * ncols + 1, gui.char_height * nrows);
6972#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6974 return; /* Can't see the window */
6975
6976 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6977 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6978
6979 /* copy one extra pixel, for when bold has spilled over */
6980 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6981 FILL_X(gui.scroll_region_left), FILL_Y(row),
6982 gui.drawarea->window,
6983 FILL_X(gui.scroll_region_left),
6984 FILL_Y(row + num_lines),
6985 gui.char_width * (gui.scroll_region_right
6986 - gui.scroll_region_left + 1) + 1,
6987 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6988
6989 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6990 gui.scroll_region_left,
6991 gui.scroll_region_bot, gui.scroll_region_right);
6992 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01006993#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994}
6995
6996/*
6997 * Insert the given number of lines before the given row, scrolling down any
6998 * following text within the scroll region.
6999 */
7000 void
7001gui_mch_insert_lines(int row, int num_lines)
7002{
Bram Moolenaar98921892016-02-23 17:14:37 +01007003#if GTK_CHECK_VERSION(3,0,0)
7004 const int ncols = gui.scroll_region_right - gui.scroll_region_left + 1;
7005 const int nrows = gui.scroll_region_bot - row + 1;
7006 const int src_nrows = nrows - num_lines;
7007
7008 gui_gtk_surface_copy_rect(
7009 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
7010 FILL_X(gui.scroll_region_left), FILL_Y(row),
7011 gui.char_width * ncols + 1, gui.char_height * src_nrows);
7012 gui_mch_clear_block(
7013 row, gui.scroll_region_left,
7014 row + num_lines - 1, gui.scroll_region_right);
7015 gui_gtk3_redraw(
7016 FILL_X(gui.scroll_region_left), FILL_Y(row),
7017 gui.char_width * ncols + 1, gui.char_height * nrows);
7018 if (!gui.by_signal)
7019 gtk_widget_queue_draw_area(gui.drawarea,
7020 FILL_X(gui.scroll_region_left), FILL_Y(row),
7021 gui.char_width * ncols + 1, gui.char_height * nrows);
7022#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
7024 return; /* Can't see the window */
7025
7026 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
7027 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
7028
7029 /* copy one extra pixel, for when bold has spilled over */
7030 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
7031 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
7032 gui.drawarea->window,
7033 FILL_X(gui.scroll_region_left), FILL_Y(row),
7034 gui.char_width * (gui.scroll_region_right
7035 - gui.scroll_region_left + 1) + 1,
7036 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
7037
7038 gui_clear_block(row, gui.scroll_region_left,
7039 row + num_lines - 1, gui.scroll_region_right);
7040 check_copy_area();
Bram Moolenaar98921892016-02-23 17:14:37 +01007041#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044/*
7045 * X Selection stuff, for cutting and pasting text to other windows.
7046 */
7047 void
7048clip_mch_request_selection(VimClipboard *cbd)
7049{
7050 GdkAtom target;
7051 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00007052 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054 for (i = 0; i < N_SELECTION_TARGETS; ++i)
7055 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00007056 if (!clip_html && selection_targets[i].info == TARGET_HTML)
7057 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 received_selection = RS_NONE;
7059 target = gdk_atom_intern(selection_targets[i].target, FALSE);
7060
7061 gtk_selection_convert(gui.drawarea,
7062 cbd->gtk_sel_atom, target,
7063 (guint32)GDK_CURRENT_TIME);
7064
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00007065 /* Hack: Wait up to three seconds for the selection. A hang was
7066 * noticed here when using the netrw plugin combined with ":gui"
7067 * during the FocusGained event. */
7068 start = time(NULL);
7069 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02007070 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071
7072 if (received_selection != RS_FAIL)
7073 return;
7074 }
7075
7076 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaar98921892016-02-23 17:14:37 +01007077#if GTK_CHECK_VERSION(3,0,0)
7078 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.mainwin)),
7079 cbd);
7080#else
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00007081 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar98921892016-02-23 17:14:37 +01007082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083}
7084
7085/*
7086 * Disown the selection.
7087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007089clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar29dfa5a2018-03-20 21:24:45 +01007091 if (!in_selection_clear_event)
7092 {
7093 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
7094 gui_mch_update();
7095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
7099 * Own the selection and return OK if it worked.
7100 */
7101 int
7102clip_mch_own_selection(VimClipboard *cbd)
7103{
7104 int success;
7105
7106 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02007107 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 gui_mch_update();
7109 return (success) ? OK : FAIL;
7110}
7111
7112/*
7113 * Send the current selection to the clipboard. Do nothing for X because we
7114 * will fill in the selection only when requested by another app.
7115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007117clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
7119}
7120
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01007121 int
7122clip_gtk_owner_exists(VimClipboard *cbd)
7123{
7124 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
7125}
7126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128#if defined(FEAT_MENU) || defined(PROTO)
7129/*
7130 * Make a menu item appear either active or not active (grey or not grey).
7131 */
7132 void
7133gui_mch_menu_grey(vimmenu_T *menu, int grey)
7134{
7135 if (menu->id == NULL)
7136 return;
7137
7138 if (menu_is_separator(menu->name))
7139 grey = TRUE;
7140
7141 gui_mch_menu_hidden(menu, FALSE);
7142 /* Be clever about bitfields versus true booleans here! */
Bram Moolenaar98921892016-02-23 17:14:37 +01007143# if GTK_CHECK_VERSION(3,0,0)
7144 if (!gtk_widget_get_sensitive(menu->id) == !grey)
7145# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
Bram Moolenaar98921892016-02-23 17:14:37 +01007147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
7149 gtk_widget_set_sensitive(menu->id, !grey);
7150 gui_mch_update();
7151 }
7152}
7153
7154/*
7155 * Make menu item hidden or not hidden.
7156 */
7157 void
7158gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
7159{
7160 if (menu->id == 0)
7161 return;
7162
7163 if (hidden)
7164 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007165# if GTK_CHECK_VERSION(3,0,0)
7166 if (gtk_widget_get_visible(menu->id))
7167# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 gtk_widget_hide(menu->id);
7172 gui_mch_update();
7173 }
7174 }
7175 else
7176 {
Bram Moolenaar98921892016-02-23 17:14:37 +01007177# if GTK_CHECK_VERSION(3,0,0)
7178 if (!gtk_widget_get_visible(menu->id))
7179# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (!GTK_WIDGET_VISIBLE(menu->id))
Bram Moolenaar98921892016-02-23 17:14:37 +01007181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 {
7183 gtk_widget_show(menu->id);
7184 gui_mch_update();
7185 }
7186 }
7187}
7188
7189/*
7190 * This is called after setting all the menus to grey/hidden or not.
7191 */
7192 void
7193gui_mch_draw_menubar(void)
7194{
7195 /* just make sure that the visual changes get effect immediately */
7196 gui_mch_update();
7197}
7198#endif /* FEAT_MENU */
7199
7200/*
7201 * Scrollbar stuff.
7202 */
7203 void
7204gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
7205{
7206 if (sb->id == NULL)
7207 return;
7208
Bram Moolenaar98921892016-02-23 17:14:37 +01007209#if GTK_CHECK_VERSION(3,0,0)
7210 gtk_widget_set_visible(sb->id, flag);
7211#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (flag)
7213 gtk_widget_show(sb->id);
7214 else
7215 gtk_widget_hide(sb->id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00007218 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219}
7220
7221
7222/*
7223 * Return the RGB value of a pixel as long.
7224 */
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007225 guicolor_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226gui_mch_get_rgb(guicolor_T pixel)
7227{
Bram Moolenaar98921892016-02-23 17:14:37 +01007228#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar36edf062016-07-21 22:10:12 +02007229 return (long_u)pixel;
Bram Moolenaar98921892016-02-23 17:14:37 +01007230#else
Bram Moolenaar36edf062016-07-21 22:10:12 +02007231 GdkColor color;
7232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
7234 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007236 return (guicolor_T)(
7237 (((unsigned)color.red & 0xff00) << 8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 | ((unsigned)color.green & 0xff00)
Bram Moolenaar1b58cdd2016-08-22 23:04:33 +02007239 | (((unsigned)color.blue & 0xff00) >> 8));
Bram Moolenaar36edf062016-07-21 22:10:12 +02007240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
7243/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007244 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007246 void
7247gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaar98921892016-02-23 17:14:37 +01007249#if GTK_CHECK_VERSION(3,0,0)
7250 gui_gtk_get_pointer(gui.drawarea, x, y, NULL);
7251#else
Bram Moolenaar6cc16192005-01-08 21:49:45 +00007252 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01007253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
7256 void
7257gui_mch_setmouse(int x, int y)
7258{
7259 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7260 * internal GDK mechanism present to accomplish this. (and for good
7261 * reason...) */
Bram Moolenaar98921892016-02-23 17:14:37 +01007262#if GTK_CHECK_VERSION(3,0,0)
7263 XWarpPointer(GDK_WINDOW_XDISPLAY(gtk_widget_get_window(gui.drawarea)),
7264 (Window)0, GDK_WINDOW_XID(gtk_widget_get_window(gui.drawarea)),
7265 0, 0, 0U, 0U, x, y);
7266#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7268 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7269 0, 0, 0U, 0U, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01007270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
7272
7273
7274#ifdef FEAT_MOUSESHAPE
7275/* The last set mouse pointer shape is remembered, to be used when it goes
7276 * from hidden to not hidden. */
7277static int last_shape = 0;
7278#endif
7279
7280/*
7281 * Use the blank mouse pointer or not.
7282 *
7283 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7284 */
7285 void
7286gui_mch_mousehide(int hide)
7287{
7288 if (gui.pointer_hidden != hide)
7289 {
7290 gui.pointer_hidden = hide;
Bram Moolenaar98921892016-02-23 17:14:37 +01007291#if GTK_CHECK_VERSION(3,0,0)
7292 if (gtk_widget_get_window(gui.drawarea) && gui.blank_pointer != NULL)
7293#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (gui.drawarea->window && gui.blank_pointer != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007295#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 {
7297 if (hide)
Bram Moolenaar98921892016-02-23 17:14:37 +01007298#if GTK_CHECK_VERSION(3,0,0)
7299 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7300 gui.blank_pointer);
7301#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
7305#ifdef FEAT_MOUSESHAPE
7306 mch_set_mouse_shape(last_shape);
Bram Moolenaar3f2a5d82016-02-27 22:08:16 +01007307#elif GTK_CHECK_VERSION(3,0,0)
7308 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309#else
7310 gdk_window_set_cursor(gui.drawarea->window, NULL);
7311#endif
7312 }
7313 }
7314}
7315
7316#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7317
7318/* Table for shape IDs. Keep in sync with the mshape_names[] table in
7319 * misc2.c! */
7320static const int mshape_ids[] =
7321{
7322 GDK_LEFT_PTR, /* arrow */
7323 GDK_CURSOR_IS_PIXMAP, /* blank */
7324 GDK_XTERM, /* beam */
7325 GDK_SB_V_DOUBLE_ARROW, /* updown */
7326 GDK_SIZING, /* udsizing */
7327 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7328 GDK_SIZING, /* lrsizing */
7329 GDK_WATCH, /* busy */
7330 GDK_X_CURSOR, /* no */
7331 GDK_CROSSHAIR, /* crosshair */
7332 GDK_HAND1, /* hand1 */
7333 GDK_HAND2, /* hand2 */
7334 GDK_PENCIL, /* pencil */
7335 GDK_QUESTION_ARROW, /* question */
7336 GDK_RIGHT_PTR, /* right-arrow */
7337 GDK_CENTER_PTR, /* up-arrow */
7338 GDK_LEFT_PTR /* last one */
7339};
7340
7341 void
7342mch_set_mouse_shape(int shape)
7343{
7344 int id;
7345 GdkCursor *c;
7346
Bram Moolenaar98921892016-02-23 17:14:37 +01007347# if GTK_CHECK_VERSION(3,0,0)
7348 if (gtk_widget_get_window(gui.drawarea) == NULL)
7349# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (gui.drawarea->window == NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 return;
7353
7354 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
Bram Moolenaar98921892016-02-23 17:14:37 +01007355# if GTK_CHECK_VERSION(3,0,0)
7356 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea),
7357 gui.blank_pointer);
7358# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
Bram Moolenaar98921892016-02-23 17:14:37 +01007360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 else
7362 {
7363 if (shape >= MSHAPE_NUMBERED)
7364 {
7365 id = shape - MSHAPE_NUMBERED;
7366 if (id >= GDK_LAST_CURSOR)
7367 id = GDK_LEFT_PTR;
7368 else
7369 id &= ~1; /* they are always even (why?) */
7370 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00007371 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007373 else
7374 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00007376 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar98921892016-02-23 17:14:37 +01007377# if GTK_CHECK_VERSION(3,0,0)
7378 gdk_window_set_cursor(gtk_widget_get_window(gui.drawarea), c);
7379# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 gdk_window_set_cursor(gui.drawarea->window, c);
Bram Moolenaar98921892016-02-23 17:14:37 +01007381# endif
7382# if GTK_CHECK_VERSION(3,0,0)
7383 g_object_unref(G_OBJECT(c));
7384# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
Bram Moolenaar98921892016-02-23 17:14:37 +01007386# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
7388 if (shape != MSHAPE_HIDE)
7389 last_shape = shape;
7390}
7391#endif /* FEAT_MOUSESHAPE */
7392
7393
7394#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7395/*
7396 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7397 * scaled down if the current font is not big enough, or scaled up if the image
7398 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7399 * will be cut off if the current font is not big enough, or centered if it's
7400 * too small.
7401 */
7402# define SIGN_WIDTH (2 * gui.char_width)
7403# define SIGN_HEIGHT (gui.char_height)
7404# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7405
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 void
7407gui_mch_drawsign(int row, int col, int typenr)
7408{
7409 GdkPixbuf *sign;
7410
7411 sign = (GdkPixbuf *)sign_get_image(typenr);
7412
Bram Moolenaar98921892016-02-23 17:14:37 +01007413# if GTK_CHECK_VERSION(3,0,0)
7414 if (sign != NULL && gui.drawarea != NULL
7415 && gtk_widget_get_window(gui.drawarea) != NULL)
7416# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01007418# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 {
7420 int width;
7421 int height;
7422 int xoffset;
7423 int yoffset;
7424 int need_scale;
7425
7426 width = gdk_pixbuf_get_width(sign);
7427 height = gdk_pixbuf_get_height(sign);
7428 /*
7429 * Decide whether we need to scale. Allow one pixel of border
7430 * width to be cut off, in order to avoid excessive scaling for
7431 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007432 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 */
7434 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007435 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 || (width < 3 * SIGN_WIDTH / 4
7437 && height < 3 * SIGN_HEIGHT / 4));
7438 if (need_scale)
7439 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007440 double aspect;
7441 int w = width;
7442 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 /* Keep the original aspect ratio */
7445 aspect = (double)height / (double)width;
7446 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7447 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007448 if (((double)(MAX(height, SIGN_HEIGHT)) /
7449 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
7450 {
7451 /* Change the aspect ratio by at most 15% to fill the
7452 * available space completly. */
7453 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
7454 height = MIN(height, SIGN_HEIGHT);
7455 }
7456 else
7457 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
Bram Moolenaar58cbc912014-06-17 18:47:02 +02007459 if (w == width && h == height)
7460 {
7461 /* no change in dimensions; don't decrease reference counter
7462 * (below) */
7463 need_scale = FALSE;
7464 }
7465 else
7466 {
7467 /* This doesn't seem to be worth caching, and doing so would
7468 * complicate the code quite a bit. */
7469 sign = gdk_pixbuf_scale_simple(sign, width, height,
7470 GDK_INTERP_BILINEAR);
7471 if (sign == NULL)
7472 return; /* out of memory */
7473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 }
7475
7476 /* The origin is the upper-left corner of the pixmap. Therefore
7477 * these offset may become negative if the pixmap is smaller than
7478 * the 2x1 cells reserved for the sign icon. */
7479 xoffset = (width - SIGN_WIDTH) / 2;
7480 yoffset = (height - SIGN_HEIGHT) / 2;
7481
Bram Moolenaar98921892016-02-23 17:14:37 +01007482# if GTK_CHECK_VERSION(3,0,0)
7483 {
7484 cairo_t *cr;
7485 cairo_surface_t *bg_surf;
7486 cairo_t *bg_cr;
7487 cairo_surface_t *sign_surf;
7488 cairo_t *sign_cr;
7489
7490 cr = cairo_create(gui.surface);
7491
7492 bg_surf = cairo_surface_create_similar(gui.surface,
7493 cairo_surface_get_content(gui.surface),
7494 SIGN_WIDTH, SIGN_HEIGHT);
7495 bg_cr = cairo_create(bg_surf);
Bram Moolenaar36edf062016-07-21 22:10:12 +02007496 cairo_set_source_rgba(bg_cr,
7497 gui.bgcolor->red, gui.bgcolor->green, gui.bgcolor->blue,
7498 gui.bgcolor->alpha);
Bram Moolenaar98921892016-02-23 17:14:37 +01007499 cairo_paint(bg_cr);
7500
7501 sign_surf = cairo_surface_create_similar(gui.surface,
7502 cairo_surface_get_content(gui.surface),
7503 SIGN_WIDTH, SIGN_HEIGHT);
7504 sign_cr = cairo_create(sign_surf);
7505 gdk_cairo_set_source_pixbuf(sign_cr, sign, -xoffset, -yoffset);
7506 cairo_paint(sign_cr);
7507
7508 cairo_set_operator(sign_cr, CAIRO_OPERATOR_DEST_OVER);
7509 cairo_set_source_surface(sign_cr, bg_surf, 0, 0);
7510 cairo_paint(sign_cr);
7511
7512 cairo_set_source_surface(cr, sign_surf, FILL_X(col), FILL_Y(row));
7513 cairo_paint(cr);
7514
7515 cairo_destroy(sign_cr);
7516 cairo_surface_destroy(sign_surf);
7517 cairo_destroy(bg_cr);
7518 cairo_surface_destroy(bg_surf);
7519 cairo_destroy(cr);
7520
7521 if (!gui.by_signal)
7522 gtk_widget_queue_draw_area(gui.drawarea,
7523 FILL_X(col), FILL_Y(col), width, height);
7524
7525 }
7526# else /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7528
7529 gdk_draw_rectangle(gui.drawarea->window,
7530 gui.text_gc,
7531 TRUE,
7532 FILL_X(col),
7533 FILL_Y(row),
7534 SIGN_WIDTH,
7535 SIGN_HEIGHT);
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 gdk_pixbuf_render_to_drawable_alpha(sign,
7538 gui.drawarea->window,
7539 MAX(0, xoffset),
7540 MAX(0, yoffset),
7541 FILL_X(col) - MIN(0, xoffset),
7542 FILL_Y(row) - MIN(0, yoffset),
7543 MIN(width, SIGN_WIDTH),
7544 MIN(height, SIGN_HEIGHT),
7545 GDK_PIXBUF_ALPHA_BILEVEL,
7546 127,
7547 GDK_RGB_DITHER_NORMAL,
7548 0, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01007549# endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 if (need_scale)
7551 g_object_unref(sign);
7552 }
7553}
7554
7555 void *
7556gui_mch_register_sign(char_u *signfile)
7557{
7558 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7559 {
7560 GdkPixbuf *sign;
7561 GError *error = NULL;
7562 char_u *message;
7563
7564 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7565
7566 if (error == NULL)
7567 return sign;
7568
7569 message = (char_u *)error->message;
7570
7571 if (message != NULL && input_conv.vc_type != CONV_NONE)
7572 message = string_convert(&input_conv, message, NULL);
7573
7574 if (message != NULL)
7575 {
7576 /* The error message is already translated and will be more
7577 * descriptive than anything we could possibly do ourselves. */
7578 EMSG2("E255: %s", message);
7579
7580 if (input_conv.vc_type != CONV_NONE)
7581 vim_free(message);
7582 }
7583 g_error_free(error);
7584 }
7585
7586 return NULL;
7587}
7588
7589 void
7590gui_mch_destroy_sign(void *sign)
7591{
7592 if (sign != NULL)
7593 g_object_unref(sign);
7594}
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596#endif /* FEAT_SIGN_ICONS */