blob: e543b0a3754dfe9eb64970272e1e246431fa5c80 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
22 */
23
24#include "vim.h"
Bram Moolenaar36e294c2015-12-29 18:55:46 +010025#ifdef USE_GRESOURCE
26#include "auto/gui_gtk_gresources.h"
27#endif
Bram Moolenaarc93e7912008-07-08 10:46:08 +000028
Bram Moolenaar071d4272004-06-13 20:20:40 +000029#ifdef FEAT_GUI_GNOME
30/* Gnome redefines _() and N_(). Grrr... */
31# ifdef _
32# undef _
33# endif
34# ifdef N_
35# undef N_
36# endif
37# ifdef textdomain
38# undef textdomain
39# endif
40# ifdef bindtextdomain
41# undef bindtextdomain
42# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000043# ifdef bind_textdomain_codeset
44# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000046# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
47# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
48# endif
49# include <gnome.h>
50# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000051/* missing prototype in bonobo-dock-item.h */
52extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000053#endif
54
55#if !defined(FEAT_GUI_GTK) && defined(PROTO)
56/* When generating prototypes we don't want syntax errors. */
57# define GdkAtom int
58# define GdkEventExpose int
59# define GdkEventFocus int
60# define GdkEventVisibility int
61# define GdkEventProperty int
62# define GtkContainer int
63# define GtkTargetEntry int
64# define GtkType int
65# define GtkWidget int
66# define gint int
67# define gpointer int
68# define guint int
69# define GdkEventKey int
70# define GdkEventSelection int
71# define GtkSelectionData int
72# define GdkEventMotion int
73# define GdkEventButton int
74# define GdkDragContext int
75# define GdkEventConfigure int
76# define GdkEventClient int
77#else
78# include <gdk/gdkkeysyms.h>
79# include <gdk/gdk.h>
80# ifdef WIN3264
81# include <gdk/gdkwin32.h>
82# else
83# include <gdk/gdkx.h>
84# endif
85
86# include <gtk/gtk.h>
87# include "gui_gtk_f.h"
88#endif
89
90#ifdef HAVE_X11_SUNKEYSYM_H
91# include <X11/Sunkeysym.h>
92#endif
93
94/*
95 * Easy-to-use macro for multihead support.
96 */
97#ifdef HAVE_GTK_MULTIHEAD
98# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
99 gtk_widget_get_display(gui.mainwin), atom)
100#else
101# define GET_X_ATOM(atom) ((Atom)(atom))
102#endif
103
104/* Selection type distinguishers */
105enum
106{
107 TARGET_TYPE_NONE,
108 TARGET_UTF8_STRING,
109 TARGET_STRING,
110 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000111 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 TARGET_TEXT,
113 TARGET_TEXT_URI_LIST,
114 TARGET_TEXT_PLAIN,
115 TARGET_VIM,
116 TARGET_VIMENC
117};
118
119/*
120 * Table of selection targets supported by Vim.
121 * Note: Order matters, preferred types should come first.
122 */
123static const GtkTargetEntry selection_targets[] =
124{
125 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
126 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000127 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
130 {"TEXT", 0, TARGET_TEXT},
131 {"STRING", 0, TARGET_STRING}
132};
133#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
134
135#ifdef FEAT_DND
136/*
137 * Table of DnD targets supported by Vim.
138 * Note: Order matters, preferred types should come first.
139 */
140static const GtkTargetEntry dnd_targets[] =
141{
142 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000143 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 {"STRING", 0, TARGET_STRING},
146 {"text/plain", 0, TARGET_TEXT_PLAIN}
147};
148# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
149#endif
150
151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
153 * "Monospace" is a standard font alias that should be present
154 * on all proper Pango/fontconfig installations.
155 */
156# define DEFAULT_FONT "Monospace 10"
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
159/*
160 * Atoms used to communicate save-yourself from the X11 session manager. There
161 * is no need to move them into the GUI struct, since they should be constant.
162 */
163static GdkAtom wm_protocols_atom = GDK_NONE;
164static GdkAtom save_yourself_atom = GDK_NONE;
165#endif
166
167/*
168 * Atoms used to control/reference X11 selections.
169 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000170static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174
175/*
176 * Keycodes recognized by vim.
177 * NOTE: when changing this, the table in gui_x11.c probably needs the same
178 * change!
179 */
180static struct special_key
181{
182 guint key_sym;
183 char_u code0;
184 char_u code1;
185}
186const special_keys[] =
187{
188 {GDK_Up, 'k', 'u'},
189 {GDK_Down, 'k', 'd'},
190 {GDK_Left, 'k', 'l'},
191 {GDK_Right, 'k', 'r'},
192 {GDK_F1, 'k', '1'},
193 {GDK_F2, 'k', '2'},
194 {GDK_F3, 'k', '3'},
195 {GDK_F4, 'k', '4'},
196 {GDK_F5, 'k', '5'},
197 {GDK_F6, 'k', '6'},
198 {GDK_F7, 'k', '7'},
199 {GDK_F8, 'k', '8'},
200 {GDK_F9, 'k', '9'},
201 {GDK_F10, 'k', ';'},
202 {GDK_F11, 'F', '1'},
203 {GDK_F12, 'F', '2'},
204 {GDK_F13, 'F', '3'},
205 {GDK_F14, 'F', '4'},
206 {GDK_F15, 'F', '5'},
207 {GDK_F16, 'F', '6'},
208 {GDK_F17, 'F', '7'},
209 {GDK_F18, 'F', '8'},
210 {GDK_F19, 'F', '9'},
211 {GDK_F20, 'F', 'A'},
212 {GDK_F21, 'F', 'B'},
213 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
214 {GDK_F22, 'F', 'C'},
215 {GDK_F23, 'F', 'D'},
216 {GDK_F24, 'F', 'E'},
217 {GDK_F25, 'F', 'F'},
218 {GDK_F26, 'F', 'G'},
219 {GDK_F27, 'F', 'H'},
220 {GDK_F28, 'F', 'I'},
221 {GDK_F29, 'F', 'J'},
222 {GDK_F30, 'F', 'K'},
223 {GDK_F31, 'F', 'L'},
224 {GDK_F32, 'F', 'M'},
225 {GDK_F33, 'F', 'N'},
226 {GDK_F34, 'F', 'O'},
227 {GDK_F35, 'F', 'P'},
228#ifdef SunXK_F36
229 {SunXK_F36, 'F', 'Q'},
230 {SunXK_F37, 'F', 'R'},
231#endif
232 {GDK_Help, '%', '1'},
233 {GDK_Undo, '&', '8'},
234 {GDK_BackSpace, 'k', 'b'},
235 {GDK_Insert, 'k', 'I'},
236 {GDK_Delete, 'k', 'D'},
237 {GDK_3270_BackTab, 'k', 'B'},
238 {GDK_Clear, 'k', 'C'},
239 {GDK_Home, 'k', 'h'},
240 {GDK_End, '@', '7'},
241 {GDK_Prior, 'k', 'P'},
242 {GDK_Next, 'k', 'N'},
243 {GDK_Print, '%', '9'},
244 /* Keypad keys: */
245 {GDK_KP_Left, 'k', 'l'},
246 {GDK_KP_Right, 'k', 'r'},
247 {GDK_KP_Up, 'k', 'u'},
248 {GDK_KP_Down, 'k', 'd'},
249 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
250 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
251 {GDK_KP_Home, 'K', '1'},
252 {GDK_KP_End, 'K', '4'},
253 {GDK_KP_Prior, 'K', '3'}, /* page up */
254 {GDK_KP_Next, 'K', '5'}, /* page down */
255
256 {GDK_KP_Add, 'K', '6'},
257 {GDK_KP_Subtract, 'K', '7'},
258 {GDK_KP_Divide, 'K', '8'},
259 {GDK_KP_Multiply, 'K', '9'},
260 {GDK_KP_Enter, 'K', 'A'},
261 {GDK_KP_Decimal, 'K', 'B'},
262
263 {GDK_KP_0, 'K', 'C'},
264 {GDK_KP_1, 'K', 'D'},
265 {GDK_KP_2, 'K', 'E'},
266 {GDK_KP_3, 'K', 'F'},
267 {GDK_KP_4, 'K', 'G'},
268 {GDK_KP_5, 'K', 'H'},
269 {GDK_KP_6, 'K', 'I'},
270 {GDK_KP_7, 'K', 'J'},
271 {GDK_KP_8, 'K', 'K'},
272 {GDK_KP_9, 'K', 'L'},
273
274 /* End of list marker: */
275 {0, 0, 0}
276};
277
278/*
279 * Flags for command line options table below.
280 */
281#define ARG_FONT 1
282#define ARG_GEOMETRY 2
283#define ARG_REVERSE 3
284#define ARG_NOREVERSE 4
285#define ARG_BACKGROUND 5
286#define ARG_FOREGROUND 6
287#define ARG_ICONIC 7
288#define ARG_ROLE 8
289#define ARG_NETBEANS 9
290#define ARG_XRM 10 /* ignored */
291#define ARG_MENUFONT 11 /* ignored */
292#define ARG_INDEX_MASK 0x00ff
293#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
294#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
295#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
296#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
297#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
298
299/*
300 * This table holds all the X GUI command line options allowed. This includes
301 * the standard ones so that we can skip them when Vim is started without the
302 * GUI (but the GUI might start up later).
303 *
304 * When changing this, also update doc/gui_x11.txt and the usage message!!!
305 */
306typedef struct
307{
308 const char *name;
309 unsigned int flags;
310}
311cmdline_option_T;
312
313static const cmdline_option_T cmdline_options[] =
314{
315 /* We handle these options ourselves */
316 {"-fn", ARG_FONT|ARG_HAS_VALUE},
317 {"-font", ARG_FONT|ARG_HAS_VALUE},
318 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
319 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
320 {"-rv", ARG_REVERSE},
321 {"-reverse", ARG_REVERSE},
322 {"+rv", ARG_NOREVERSE},
323 {"+reverse", ARG_NOREVERSE},
324 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
325 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
326 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
327 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
328 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_NETBEANS_INTG
331 {"-nb", ARG_NETBEANS}, /* non-standard value format */
332 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
333 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
334 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 /* Arguments handled by GTK (and GNOME) internally. */
337 {"--g-fatal-warnings", ARG_FOR_GTK},
338 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
339 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
340 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
341 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
342 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
343 {"--sync", ARG_FOR_GTK},
344 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
345 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
346 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
348 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
349 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#ifdef FEAT_GUI_GNOME
351 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--sm-disable", ARG_FOR_GTK},
355 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
357 {"--oaf-private", ARG_FOR_GTK},
358 {"--enable-sound", ARG_FOR_GTK},
359 {"--disable-sound", ARG_FOR_GTK},
360 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
361 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
362 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
363 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
364# if 0 /* conflicts with Vim's own --version argument */
365 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
366# endif
367 {"--disable-crash-dialog", ARG_FOR_GTK},
368#endif
369 {NULL, 0}
370};
371
372static int gui_argc = 0;
373static char **gui_argv = NULL;
374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
377static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000378static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379#endif
380static int found_iconic_arg = FALSE;
381
382#ifdef FEAT_GUI_GNOME
383/*
384 * Can't use Gnome if --socketid given
385 */
386static int using_gnome = 0;
387#else
388# define using_gnome 0
389#endif
390
391/*
392 * Parse the GUI related command-line arguments. Any arguments used are
393 * deleted from argv, and *argc is decremented accordingly. This is called
394 * when vim is started, whether or not the GUI has been started.
395 */
396 void
397gui_mch_prepare(int *argc, char **argv)
398{
399 const cmdline_option_T *option;
400 int i = 0;
401 int len = 0;
402
403#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
404 /*
405 * Determine the command used to invoke Vim, to be passed as restart
406 * command to the session manager. If argv[0] contains any directory
407 * components try building an absolute path, otherwise leave it as is.
408 */
409 restart_command = argv[0];
410
411 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
412 {
413 char_u buf[MAXPATHL];
414
415 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000416 {
417 abs_restart_command = (char *)vim_strsave(buf);
418 restart_command = abs_restart_command;
419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421#endif
422
423 /*
424 * Move all the entries in argv which are relevant to GTK+ and GNOME
425 * into gui_argv. Freed later in gui_mch_init().
426 */
427 gui_argc = 0;
428 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
429
430 g_return_if_fail(gui_argv != NULL);
431
432 gui_argv[gui_argc++] = argv[i++];
433
434 while (i < *argc)
435 {
436 /* Don't waste CPU cycles on non-option arguments. */
437 if (argv[i][0] != '-' && argv[i][0] != '+')
438 {
439 ++i;
440 continue;
441 }
442
443 /* Look for argv[i] in cmdline_options[] table. */
444 for (option = &cmdline_options[0]; option->name != NULL; ++option)
445 {
446 len = strlen(option->name);
447
448 if (strncmp(argv[i], option->name, len) == 0)
449 {
450 if (argv[i][len] == '\0')
451 break;
452 /* allow --foo=bar style */
453 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
454 break;
455#ifdef FEAT_NETBEANS_INTG
456 /* darn, -nb has non-standard syntax */
457 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
458 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
459 break;
460#endif
461 }
462 else if ((option->flags & ARG_COMPAT_LONG)
463 && strcmp(argv[i], option->name + 1) == 0)
464 {
465 /* Replace the standard X arguments "-name" and "-display"
466 * with their GNU-style long option counterparts. */
467 argv[i] = (char *)option->name;
468 break;
469 }
470 }
471 if (option->name == NULL) /* no match */
472 {
473 ++i;
474 continue;
475 }
476
477 if (option->flags & ARG_FOR_GTK)
478 {
479 /* Move the argument into gui_argv, which
480 * will later be passed to gtk_init_check() */
481 gui_argv[gui_argc++] = argv[i];
482 }
483 else
484 {
485 char *value = NULL;
486
487 /* Extract the option's value if there is one.
488 * Accept both "--foo bar" and "--foo=bar" style. */
489 if (option->flags & ARG_HAS_VALUE)
490 {
491 if (argv[i][len] == '=')
492 value = &argv[i][len + 1];
493 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
494 value = argv[i + 1];
495 }
496
497 /* Check for options handled by Vim itself */
498 switch (option->flags & ARG_INDEX_MASK)
499 {
500 case ARG_REVERSE:
501 found_reverse_arg = TRUE;
502 break;
503 case ARG_NOREVERSE:
504 found_reverse_arg = FALSE;
505 break;
506 case ARG_FONT:
507 font_argument = value;
508 break;
509 case ARG_GEOMETRY:
510 if (value != NULL)
511 gui.geom = vim_strsave((char_u *)value);
512 break;
513 case ARG_BACKGROUND:
514 background_argument = value;
515 break;
516 case ARG_FOREGROUND:
517 foreground_argument = value;
518 break;
519 case ARG_ICONIC:
520 found_iconic_arg = TRUE;
521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 case ARG_ROLE:
523 role_argument = value; /* used later in gui_mch_open() */
524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#ifdef FEAT_NETBEANS_INTG
526 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 gui.dofork = FALSE; /* don't fork() when starting GUI */
528 netbeansArg = argv[i];
529 break;
530#endif
531 default:
532 break;
533 }
534 }
535
536 /* These arguments make gnome_program_init() print a message and exit.
537 * Must start the GUI for this, otherwise ":gui" will exit later! */
538 if (option->flags & ARG_NEEDS_GUI)
539 gui.starting = TRUE;
540
541 if (option->flags & ARG_KEEP)
542 ++i;
543 else
544 {
545 /* Remove the flag from the argument vector. */
546 if (--*argc > i)
547 {
548 int n_strip = 1;
549
550 /* Move the argument's value as well, if there is one. */
551 if ((option->flags & ARG_HAS_VALUE)
552 && argv[i][len] != '='
553 && strcmp(argv[i + 1], "--") != 0)
554 {
555 ++n_strip;
556 --*argc;
557 if (option->flags & ARG_FOR_GTK)
558 gui_argv[gui_argc++] = argv[i + 1];
559 }
560
561 if (*argc > i)
562 mch_memmove(&argv[i], &argv[i + n_strip],
563 (*argc - i) * sizeof(char *));
564 }
565 argv[*argc] = NULL;
566 }
567 }
568
569 gui_argv[gui_argc] = NULL;
570}
571
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000572#if defined(EXITFREE) || defined(PROTO)
573 void
574gui_mch_free_all()
575{
576 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000577#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
578 vim_free(abs_restart_command);
579#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000580}
581#endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583/*
584 * This should be maybe completely removed.
585 * Doesn't seem possible, since check_copy_area() relies on
586 * this information. --danielk
587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000589visibility_event(GtkWidget *widget UNUSED,
590 GdkEventVisibility *event,
591 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 gui.visibility = event->state;
594 /*
595 * When we do an gdk_window_copy_area(), and the window is partially
596 * obscured, we want to receive an event to tell us whether it worked
597 * or not.
598 */
599 if (gui.text_gc != NULL)
600 gdk_gc_set_exposures(gui.text_gc,
601 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
602 return FALSE;
603}
604
605/*
606 * Redraw the corresponding portions of the screen.
607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000609expose_event(GtkWidget *widget UNUSED,
610 GdkEventExpose *event,
611 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612{
613 /* Skip this when the GUI isn't set up yet, will redraw later. */
614 if (gui.starting)
615 return FALSE;
616
617 out_flush(); /* make sure all output has been processed */
618 gui_redraw(event->area.x, event->area.y,
619 event->area.width, event->area.height);
620
621 /* Clear the border areas if needed */
622 if (event->area.x < FILL_X(0))
623 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
624 if (event->area.y < FILL_Y(0))
625 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
626 if (event->area.x > FILL_X(Columns))
627 gdk_window_clear_area(gui.drawarea->window,
628 FILL_X((int)Columns), 0, 0, 0);
629 if (event->area.y > FILL_Y(Rows))
630 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
631
632 return FALSE;
633}
634
635#ifdef FEAT_CLIENTSERVER
636/*
637 * Handle changes to the "Comm" property
638 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000640property_event(GtkWidget *widget,
641 GdkEventProperty *event,
642 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643{
644 if (event->type == GDK_PROPERTY_NOTIFY
645 && event->state == (int)GDK_PROPERTY_NEW_VALUE
646 && GDK_WINDOW_XWINDOW(event->window) == commWindow
647 && GET_X_ATOM(event->atom) == commProperty)
648 {
649 XEvent xev;
650
651 /* Translate to XLib */
652 xev.xproperty.type = PropertyNotify;
653 xev.xproperty.atom = commProperty;
654 xev.xproperty.window = commWindow;
655 xev.xproperty.state = PropertyNewValue;
Bram Moolenaar93c88e02015-09-15 14:12:05 +0200656 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 }
658 return FALSE;
659}
660#endif
661
662
663/****************************************************************************
664 * Focus handlers:
665 */
666
667
668/*
669 * This is a simple state machine:
670 * BLINK_NONE not blinking at all
671 * BLINK_OFF blinking, cursor is not shown
672 * BLINK_ON blinking, cursor is shown
673 */
674
675#define BLINK_NONE 0
676#define BLINK_OFF 1
677#define BLINK_ON 2
678
679static int blink_state = BLINK_NONE;
680static long_u blink_waittime = 700;
681static long_u blink_ontime = 400;
682static long_u blink_offtime = 250;
683static guint blink_timer = 0;
684
685 void
686gui_mch_set_blinking(long waittime, long on, long off)
687{
688 blink_waittime = waittime;
689 blink_ontime = on;
690 blink_offtime = off;
691}
692
693/*
694 * Stop the cursor blinking. Show the cursor if it wasn't shown.
695 */
696 void
697gui_mch_stop_blink(void)
698{
699 if (blink_timer)
700 {
701 gtk_timeout_remove(blink_timer);
702 blink_timer = 0;
703 }
704 if (blink_state == BLINK_OFF)
705 gui_update_cursor(TRUE, FALSE);
706 blink_state = BLINK_NONE;
707}
708
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000710blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711{
712 if (blink_state == BLINK_ON)
713 {
714 gui_undraw_cursor();
715 blink_state = BLINK_OFF;
716 blink_timer = gtk_timeout_add((guint32)blink_offtime,
717 (GtkFunction) blink_cb, NULL);
718 }
719 else
720 {
721 gui_update_cursor(TRUE, FALSE);
722 blink_state = BLINK_ON;
723 blink_timer = gtk_timeout_add((guint32)blink_ontime,
724 (GtkFunction) blink_cb, NULL);
725 }
726
727 return FALSE; /* don't happen again */
728}
729
730/*
731 * Start the cursor blinking. If it was already blinking, this restarts the
732 * waiting time and shows the cursor.
733 */
734 void
735gui_mch_start_blink(void)
736{
737 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200738 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 gtk_timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200740 blink_timer = 0;
741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 /* Only switch blinking on if none of the times is zero */
743 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
744 {
745 blink_timer = gtk_timeout_add((guint32)blink_waittime,
746 (GtkFunction) blink_cb, NULL);
747 blink_state = BLINK_ON;
748 gui_update_cursor(TRUE, FALSE);
749 }
750}
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000753enter_notify_event(GtkWidget *widget UNUSED,
754 GdkEventCrossing *event UNUSED,
755 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756{
757 if (blink_state == BLINK_NONE)
758 gui_mch_start_blink();
759
760 /* make sure keyboard input goes there */
761 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
762 gtk_widget_grab_focus(gui.drawarea);
763
764 return FALSE;
765}
766
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000768leave_notify_event(GtkWidget *widget UNUSED,
769 GdkEventCrossing *event UNUSED,
770 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771{
772 if (blink_state != BLINK_NONE)
773 gui_mch_stop_blink();
774
775 return FALSE;
776}
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000779focus_in_event(GtkWidget *widget,
780 GdkEventFocus *event UNUSED,
781 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 gui_focus_change(TRUE);
784
785 if (blink_state == BLINK_NONE)
786 gui_mch_start_blink();
787
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000788 /* make sure keyboard input goes to the draw area (if this is focus for a
789 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000790 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000791 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 return TRUE;
794}
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000797focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200798 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000799 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
801 gui_focus_change(FALSE);
802
803 if (blink_state != BLINK_NONE)
804 gui_mch_stop_blink();
805
806 return TRUE;
807}
808
809
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810/*
811 * Translate a GDK key value to UTF-8 independently of the current locale.
812 * The output is written to string, which must have room for at least 6 bytes
813 * plus the NUL terminator. Returns the length in bytes.
814 *
815 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
816 * of GdkEventKey::string instead. But event->string is evil; see here why:
817 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
818 */
819 static int
820keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
821{
822 int len;
823 guint32 uc;
824
825 uc = gdk_keyval_to_unicode(keyval);
826 if (uc != 0)
827 {
828 /* Check for CTRL-foo */
829 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
830 {
831 /* These mappings look arbitrary at the first glance, but in fact
832 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
833 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
834 * more sense and is consistent with usual terminal behaviour). */
835 if (uc >= '@')
836 string[0] = uc & 0x1F;
837 else if (uc == '2')
838 string[0] = NUL;
839 else if (uc >= '3' && uc <= '7')
840 string[0] = uc ^ 0x28;
841 else if (uc == '8')
842 string[0] = BS;
843 else if (uc == '?')
844 string[0] = DEL;
845 else
846 string[0] = uc;
847 len = 1;
848 }
849 else
850 {
851 /* Translate a normal key to UTF-8. This doesn't work for dead
852 * keys of course, you _have_ to use an input method for that. */
853 len = utf_char2bytes((int)uc, string);
854 }
855 }
856 else
857 {
858 /* Translate keys which are represented by ASCII control codes in Vim.
859 * There are only a few of those; most control keys are translated to
860 * special terminal-like control sequences. */
861 len = 1;
862 switch (keyval)
863 {
864 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
865 string[0] = TAB;
866 break;
867 case GDK_Linefeed:
868 string[0] = NL;
869 break;
870 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
871 string[0] = CAR;
872 break;
873 case GDK_Escape:
874 string[0] = ESC;
875 break;
876 default:
877 len = 0;
878 break;
879 }
880 }
881 string[len] = NUL;
882
883 return len;
884}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000886 static int
887modifiers_gdk2vim(guint state)
888{
889 int modifiers = 0;
890
891 if (state & GDK_SHIFT_MASK)
892 modifiers |= MOD_MASK_SHIFT;
893 if (state & GDK_CONTROL_MASK)
894 modifiers |= MOD_MASK_CTRL;
895 if (state & GDK_MOD1_MASK)
896 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +0200897#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200898 if (state & GDK_SUPER_MASK)
899 modifiers |= MOD_MASK_META;
900#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000901 if (state & GDK_MOD4_MASK)
902 modifiers |= MOD_MASK_META;
903
904 return modifiers;
905}
906
907 static int
908modifiers_gdk2mouse(guint state)
909{
910 int modifiers = 0;
911
912 if (state & GDK_SHIFT_MASK)
913 modifiers |= MOUSE_SHIFT;
914 if (state & GDK_CONTROL_MASK)
915 modifiers |= MOUSE_CTRL;
916 if (state & GDK_MOD1_MASK)
917 modifiers |= MOUSE_ALT;
918
919 return modifiers;
920}
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922/*
923 * Main keyboard handler:
924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000926key_press_event(GtkWidget *widget UNUSED,
927 GdkEventKey *event,
928 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200930 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
932 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 guint key_sym;
934 int len;
935 int i;
936 int modifiers;
937 int key;
938 guint state;
939 char_u *s, *d;
940
Bram Moolenaar20892c12011-06-26 04:49:00 +0200941 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 key_sym = event->keyval;
943 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944
945#ifdef FEAT_XIM
946 if (xim_queue_key_press_event(event, TRUE))
947 return TRUE;
948#endif
949
950#ifdef FEAT_HANGULIN
951 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
952 {
953 hangul_input_state_toggle();
954 return TRUE;
955 }
956#endif
957
958#ifdef SunXK_F36
959 /*
960 * These keys have bogus lookup strings, and trapping them here is
961 * easier than trying to XRebindKeysym() on them with every possible
962 * combination of modifiers.
963 */
964 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
965 len = 0;
966 else
967#endif
968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 len = keyval_to_string(key_sym, state, string2);
970
971 /* Careful: convert_input() doesn't handle the NUL character.
972 * No need to convert pure ASCII anyway, thus the len > 1 check. */
973 if (len > 1 && input_conv.vc_type != CONV_NONE)
974 len = convert_input(string2, len, sizeof(string2));
975
976 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 d = string;
978 for (i = 0; i < len; ++i)
979 {
980 *d++ = s[i];
981 if (d[-1] == CSI && d + 2 < string + sizeof(string))
982 {
983 /* Turn CSI into K_CSI. */
984 *d++ = KS_EXTRA;
985 *d++ = (int)KE_CSI;
986 }
987 }
988 len = d - string;
989 }
990
991 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
992 if (key_sym == GDK_ISO_Left_Tab)
993 {
994 key_sym = GDK_Tab;
995 state |= GDK_SHIFT_MASK;
996 }
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#ifdef FEAT_MENU
999 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1000 * is a menu shortcut, we ignore everything with the ALT modifier. */
1001 if ((state & GDK_MOD1_MASK)
1002 && gui.menu_is_active
1003 && (*p_wak == 'y'
1004 || (*p_wak == 'm'
1005 && len == 1
1006 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 /* For GTK2 we return false to signify that we haven't handled the
1008 * keypress, so that gtk will handle the mnemonic or accelerator. */
1009 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010#endif
1011
1012 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1013 * that already has the 8th bit set.
1014 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1015 * Don't do this for double-byte encodings, it turns the char into a lead
1016 * byte. */
1017 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001018 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001019#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001020 || (state & GDK_SUPER_MASK)
1021#endif
1022 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1024 && (string[0] & 0x80) == 0
1025 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 )
1028 {
1029 string[0] |= 0x80;
1030 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (enc_utf8) /* convert to utf-8 */
1032 {
1033 string[1] = string[0] & 0xbf;
1034 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1035 if (string[1] == CSI)
1036 {
1037 string[2] = KS_EXTRA;
1038 string[3] = (int)KE_CSI;
1039 len = 4;
1040 }
1041 else
1042 len = 2;
1043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045
1046 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1047 * value) to detect backspace, delete and keypad keys. */
1048 if (len == 0 || len == 1)
1049 {
1050 for (i = 0; special_keys[i].key_sym != 0; i++)
1051 {
1052 if (special_keys[i].key_sym == key_sym)
1053 {
1054 string[0] = CSI;
1055 string[1] = special_keys[i].code0;
1056 string[2] = special_keys[i].code1;
1057 len = -3;
1058 break;
1059 }
1060 }
1061 }
1062
1063 if (len == 0) /* Unrecognized key */
1064 return TRUE;
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 /* Special keys (and a few others) may have modifiers. Also when using a
1067 * double-byte encoding (can't set the 8th bit). */
1068 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1069 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1070 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1071 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001072 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001073#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001074 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001076 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001078 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080 /*
1081 * For some keys a shift modifier is translated into another key
1082 * code.
1083 */
1084 if (len == -3)
1085 key = TO_SPECIAL(string[1], string[2]);
1086 else
1087 key = string[0];
1088
1089 key = simplify_key(key, &modifiers);
1090 if (key == CSI)
1091 key = K_CSI;
1092 if (IS_SPECIAL(key))
1093 {
1094 string[0] = CSI;
1095 string[1] = K_SECOND(key);
1096 string[2] = K_THIRD(key);
1097 len = 3;
1098 }
1099 else
1100 {
1101 string[0] = key;
1102 len = 1;
1103 }
1104
1105 if (modifiers != 0)
1106 {
1107 string2[0] = CSI;
1108 string2[1] = KS_MODIFIER;
1109 string2[2] = modifiers;
1110 add_to_input_buf(string2, 3);
1111 }
1112 }
1113
1114 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1115 || (string[0] == intr_char && intr_char != Ctrl_C)))
1116 {
1117 trash_input_buf();
1118 got_int = TRUE;
1119 }
1120
1121 add_to_input_buf(string, len);
1122
1123 /* blank out the pointer if necessary */
1124 if (p_mh)
1125 gui_mch_mousehide(TRUE);
1126
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 return TRUE;
1128}
1129
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001130#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001132key_release_event(GtkWidget *widget UNUSED,
1133 GdkEventKey *event,
1134 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
Bram Moolenaar20892c12011-06-26 04:49:00 +02001136 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 /*
1138 * GTK+ 2 input methods may do fancy stuff on key release events too.
1139 * With the default IM for instance, you can enter any UCS code point
1140 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1141 */
1142 return xim_queue_key_press_event(event, FALSE);
1143}
1144#endif
1145
1146
1147/****************************************************************************
1148 * Selection handlers:
1149 */
1150
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001152selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001154 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155{
1156 if (event->selection == clip_plus.gtk_sel_atom)
1157 clip_lose_selection(&clip_plus);
1158 else
1159 clip_lose_selection(&clip_star);
1160
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 return TRUE;
1162}
1163
1164#define RS_NONE 0 /* selection_received_cb() not called yet */
1165#define RS_OK 1 /* selection_received_cb() called and OK */
1166#define RS_FAIL 2 /* selection_received_cb() called and failed */
1167static int received_selection = RS_NONE;
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001170selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001172 guint time_ UNUSED,
1173 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174{
1175 VimClipboard *cbd;
1176 char_u *text;
1177 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001180 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182 if (data->selection == clip_plus.gtk_sel_atom)
1183 cbd = &clip_plus;
1184 else
1185 cbd = &clip_star;
1186
1187 text = (char_u *)data->data;
1188 len = data->length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190 if (text == NULL || len <= 0)
1191 {
1192 received_selection = RS_FAIL;
1193 /* clip_free_selection(cbd); ??? */
1194
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 return;
1196 }
1197
1198 if (data->type == vim_atom)
1199 {
1200 motion_type = *text++;
1201 --len;
1202 }
1203
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else if (data->type == vimenc_atom)
1205 {
1206 char_u *enc;
1207 vimconv_T conv;
1208
1209 motion_type = *text++;
1210 --len;
1211
1212 enc = text;
1213 text += STRLEN(text) + 1;
1214 len -= text - enc;
1215
1216 /* If the encoding of the text is different from 'encoding', attempt
1217 * converting it. */
1218 conv.vc_type = CONV_NONE;
1219 convert_setup(&conv, enc, p_enc);
1220 if (conv.vc_type != CONV_NONE)
1221 {
1222 tmpbuf = string_convert(&conv, text, &len);
1223 if (tmpbuf != NULL)
1224 text = tmpbuf;
1225 convert_setup(&conv, NULL, NULL);
1226 }
1227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 /* gtk_selection_data_get_text() handles all the nasty details
1230 * and targets and encodings etc. This rocks so hard. */
1231 else
1232 {
1233 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1234 if (tmpbuf_utf8 != NULL)
1235 {
1236 len = STRLEN(tmpbuf_utf8);
1237 if (input_conv.vc_type != CONV_NONE)
1238 {
1239 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1240 if (tmpbuf != NULL)
1241 text = tmpbuf;
1242 }
1243 else
1244 text = tmpbuf_utf8;
1245 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001246 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1247 {
1248 vimconv_T conv;
1249
1250 /* UTF-16, we get this for HTML */
1251 conv.vc_type = CONV_NONE;
1252 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1253
1254 if (conv.vc_type != CONV_NONE)
1255 {
1256 text += 2;
1257 len -= 2;
1258 tmpbuf = string_convert(&conv, text, &len);
1259 convert_setup(&conv, NULL, NULL);
1260 }
1261 if (tmpbuf != NULL)
1262 text = tmpbuf;
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001266 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001267 while (len > 0 && text[len - 1] == NUL)
1268 --len;
1269
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 clip_yank_selection(motion_type, text, (long)len, cbd);
1271 received_selection = RS_OK;
1272 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274}
1275
1276/*
1277 * Prepare our selection data for passing it to the external selection
1278 * client.
1279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001281selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 GtkSelectionData *selection_data,
1283 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001284 guint time_ UNUSED,
1285 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 char_u *string;
1288 char_u *tmpbuf;
1289 long_u tmplen;
1290 int length;
1291 int motion_type;
1292 GdkAtom type;
1293 VimClipboard *cbd;
1294
1295 if (selection_data->selection == clip_plus.gtk_sel_atom)
1296 cbd = &clip_plus;
1297 else
1298 cbd = &clip_star;
1299
1300 if (!cbd->owned)
1301 return; /* Shouldn't ever happen */
1302
1303 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001304 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 && info != (guint)TARGET_UTF8_STRING
1306 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 && info != (guint)TARGET_VIM
1308 && info != (guint)TARGET_COMPOUND_TEXT
1309 && info != (guint)TARGET_TEXT)
1310 return;
1311
1312 /* get the selection from the '*'/'+' register */
1313 clip_get_selection(cbd);
1314
1315 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1316 if (motion_type < 0 || string == NULL)
1317 return;
1318 /* Due to int arguments we can't handle more than G_MAXINT. Also
1319 * reserve one extra byte for NUL or the motion type; just in case.
1320 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1321 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1322
1323 if (info == (guint)TARGET_VIM)
1324 {
1325 tmpbuf = alloc((unsigned)length + 1);
1326 if (tmpbuf != NULL)
1327 {
1328 tmpbuf[0] = motion_type;
1329 mch_memmove(tmpbuf + 1, string, (size_t)length);
1330 }
1331 /* For our own format, the first byte contains the motion type */
1332 ++length;
1333 vim_free(string);
1334 string = tmpbuf;
1335 type = vim_atom;
1336 }
1337
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001338 else if (info == (guint)TARGET_HTML)
1339 {
1340 vimconv_T conv;
1341
1342 /* Since we get utf-16, we probably should set it as well. */
1343 conv.vc_type = CONV_NONE;
1344 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1345 if (conv.vc_type != CONV_NONE)
1346 {
1347 tmpbuf = string_convert(&conv, string, &length);
1348 convert_setup(&conv, NULL, NULL);
1349 vim_free(string);
1350 string = tmpbuf;
1351 }
1352
1353 /* Prepend the BOM: "fffe" */
1354 if (string != NULL)
1355 {
1356 tmpbuf = alloc(length + 2);
1357 tmpbuf[0] = 0xff;
1358 tmpbuf[1] = 0xfe;
1359 mch_memmove(tmpbuf + 2, string, (size_t)length);
1360 vim_free(string);
1361 string = tmpbuf;
1362 length += 2;
1363
1364 selection_data->type = selection_data->target;
1365 selection_data->format = 16; /* 16 bits per char */
1366 gtk_selection_data_set(selection_data, html_atom, 16,
1367 string, length);
1368 vim_free(string);
1369 }
1370 return;
1371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 else if (info == (guint)TARGET_VIMENC)
1373 {
1374 int l = STRLEN(p_enc);
1375
1376 /* contents: motion_type 'encoding' NUL text */
1377 tmpbuf = alloc((unsigned)length + l + 2);
1378 if (tmpbuf != NULL)
1379 {
1380 tmpbuf[0] = motion_type;
1381 STRCPY(tmpbuf + 1, p_enc);
1382 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1383 }
1384 length += l + 2;
1385 vim_free(string);
1386 string = tmpbuf;
1387 type = vimenc_atom;
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /* gtk_selection_data_set_text() handles everything for us. This is
1391 * so easy and simple and cool, it'd be insane not to use it. */
1392 else
1393 {
1394 if (output_conv.vc_type != CONV_NONE)
1395 {
1396 tmpbuf = string_convert(&output_conv, string, &length);
1397 vim_free(string);
1398 if (tmpbuf == NULL)
1399 return;
1400 string = tmpbuf;
1401 }
1402 /* Validate the string to avoid runtime warnings */
1403 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1404 {
1405 gtk_selection_data_set_text(selection_data,
1406 (const char *)string, length);
1407 }
1408 vim_free(string);
1409 return;
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
1412 if (string != NULL)
1413 {
1414 selection_data->type = selection_data->target;
1415 selection_data->format = 8; /* 8 bits per char */
1416
1417 gtk_selection_data_set(selection_data, type, 8, string, length);
1418 vim_free(string);
1419 }
1420}
1421
1422/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001423 * Check if the GUI can be started. Called before gvimrc is sourced and
1424 * before fork().
1425 * Return OK or FAIL.
1426 */
1427 int
1428gui_mch_early_init_check(void)
1429{
1430 char_u *p;
1431
1432 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1433 p = mch_getenv((char_u *)"DISPLAY");
1434 if (p == NULL || *p == NUL)
1435 {
1436 gui.dying = TRUE;
1437 EMSG(_((char *)e_opendisp));
1438 return FAIL;
1439 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01001440#ifdef USE_GRESOURCE
1441 gui_gtk_register_resource();
1442#endif
Bram Moolenaar29695702012-05-18 17:03:18 +02001443 return OK;
1444}
1445
1446/*
1447 * Check if the GUI can be started. Called before gvimrc is sourced but after
1448 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 * Return OK or FAIL.
1450 */
1451 int
1452gui_mch_init_check(void)
1453{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454#ifdef FEAT_GUI_GNOME
1455 if (gtk_socket_id == 0)
1456 using_gnome = 1;
1457#endif
1458
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001459 /* This defaults to argv[0], but we want it to match the name of the
1460 * shipped gvim.desktop so that Vim's windows can be associated with this
1461 * file. */
1462 g_set_prgname("gvim");
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1465 if (!gtk_init_check(&gui_argc, &gui_argv))
1466 {
1467 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001468 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return FAIL;
1470 }
1471
1472 return OK;
1473}
1474
1475
1476/****************************************************************************
1477 * Mouse handling callbacks
1478 */
1479
1480
1481static guint mouse_click_timer = 0;
1482static int mouse_timed_out = TRUE;
1483
1484/*
1485 * Timer used to recognize multiple clicks of the mouse button
1486 */
1487 static gint
1488mouse_click_timer_cb(gpointer data)
1489{
1490 /* we don't use this information currently */
1491 int *timed_out = (int *) data;
1492
1493 *timed_out = TRUE;
1494 return FALSE; /* don't happen again */
1495}
1496
1497static guint motion_repeat_timer = 0;
1498static int motion_repeat_offset = FALSE;
1499static gint motion_repeat_timer_cb(gpointer);
1500
1501 static void
1502process_motion_notify(int x, int y, GdkModifierType state)
1503{
1504 int button;
1505 int_u vim_modifiers;
1506
1507 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1508 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1509 GDK_BUTTON5_MASK))
1510 ? MOUSE_DRAG : ' ';
1511
1512 /* If our pointer is currently hidden, then we should show it. */
1513 gui_mch_mousehide(FALSE);
1514
1515 /* Just moving the rodent above the drawing area without any button
1516 * being pressed. */
1517 if (button != MOUSE_DRAG)
1518 {
1519 gui_mouse_moved(x, y);
1520 return;
1521 }
1522
1523 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001524 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar49325942007-05-10 19:19:59 +00001526 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1528
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 /*
1530 * Auto repeat timer handling.
1531 */
1532 if (x < 0 || y < 0
1533 || x >= gui.drawarea->allocation.width
1534 || y >= gui.drawarea->allocation.height)
1535 {
1536
1537 int dx;
1538 int dy;
1539 int offshoot;
1540 int delay = 10;
1541
1542 /* Calculate the maximal distance of the cursor from the drawing area.
1543 * (offshoot can't become negative here!).
1544 */
1545 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1546 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1547
1548 offshoot = dx > dy ? dx : dy;
1549
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001550 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * distance of 127 pixels from the main window.
1552 *
1553 * One could think endlessly about the most ergonomic variant here.
1554 * For example it could make sense to calculate the distance from the
1555 * drags start instead...
1556 *
1557 * Maybe a parabolic interpolation would suite us better here too...
1558 */
1559 if (offshoot > 127)
1560 {
1561 /* 5 appears to be somehow near to my perceptual limits :-). */
1562 delay = 5;
1563 }
1564 else
1565 {
1566 delay = (130 * (127 - offshoot)) / 127 + 5;
1567 }
1568
1569 /* shoot again */
1570 if (!motion_repeat_timer)
1571 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1572 motion_repeat_timer_cb, NULL);
1573 }
1574}
1575
1576/*
1577 * Timer used to recognize multiple clicks of the mouse button.
1578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001580motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581{
1582 int x;
1583 int y;
1584 GdkModifierType state;
1585
1586 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1587
1588 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1589 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1590 GDK_BUTTON5_MASK)))
1591 {
1592 motion_repeat_timer = 0;
1593 return FALSE;
1594 }
1595
1596 /* If there already is a mouse click in the input buffer, wait another
1597 * time (otherwise we would create a backlog of clicks) */
1598 if (vim_used_in_input_buf() > 10)
1599 return TRUE;
1600
1601 motion_repeat_timer = 0;
1602
1603 /*
1604 * Fake a motion event.
1605 * Trick: Pretend the mouse moved to the next character on every other
1606 * event, otherwise drag events will be discarded, because they are still
1607 * in the same character.
1608 */
1609 if (motion_repeat_offset)
1610 x += gui.char_width;
1611
1612 motion_repeat_offset = !motion_repeat_offset;
1613 process_motion_notify(x, y, state);
1614
1615 /* Don't happen again. We will get reinstalled in the synthetic event
1616 * if needed -- thus repeating should still work. */
1617 return FALSE;
1618}
1619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001621motion_notify_event(GtkWidget *widget,
1622 GdkEventMotion *event,
1623 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 if (event->is_hint)
1626 {
1627 int x;
1628 int y;
1629 GdkModifierType state;
1630
1631 gdk_window_get_pointer(widget->window, &x, &y, &state);
1632 process_motion_notify(x, y, state);
1633 }
1634 else
1635 {
1636 process_motion_notify((int)event->x, (int)event->y,
1637 (GdkModifierType)event->state);
1638 }
1639
1640 return TRUE; /* handled */
1641}
1642
1643
1644/*
1645 * Mouse button handling. Note please that we are capturing multiple click's
1646 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1647 * This is due to the way the generic VIM code is recognizing multiple clicks.
1648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001650button_press_event(GtkWidget *widget,
1651 GdkEventButton *event,
1652 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653{
1654 int button;
1655 int repeated_click = FALSE;
1656 int x, y;
1657 int_u vim_modifiers;
1658
Bram Moolenaar20892c12011-06-26 04:49:00 +02001659 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 /* Make sure we have focus now we've been selected */
1662 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1663 gtk_widget_grab_focus(widget);
1664
1665 /*
1666 * Don't let additional events about multiple clicks send by GTK to us
1667 * after the initial button press event confuse us.
1668 */
1669 if (event->type != GDK_BUTTON_PRESS)
1670 return FALSE;
1671
1672 x = event->x;
1673 y = event->y;
1674
1675 /* Handle multiple clicks */
1676 if (!mouse_timed_out && mouse_click_timer)
1677 {
1678 gtk_timeout_remove(mouse_click_timer);
1679 mouse_click_timer = 0;
1680 repeated_click = TRUE;
1681 }
1682
1683 mouse_timed_out = FALSE;
1684 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1685 mouse_click_timer_cb, &mouse_timed_out);
1686
1687 switch (event->button)
1688 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001689 /* Keep in sync with gui_x11.c.
1690 * Buttons 4-7 are handled in scroll_event() */
1691 case 1: button = MOUSE_LEFT; break;
1692 case 2: button = MOUSE_MIDDLE; break;
1693 case 3: button = MOUSE_RIGHT; break;
1694 case 8: button = MOUSE_X1; break;
1695 case 9: button = MOUSE_X2; break;
1696 default:
1697 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699
1700#ifdef FEAT_XIM
1701 /* cancel any preediting */
1702 if (im_is_preediting())
1703 xim_reset();
1704#endif
1705
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001706 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
1710 return TRUE;
1711}
1712
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001714 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001717scroll_event(GtkWidget *widget,
1718 GdkEventScroll *event,
1719 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001722 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
1724 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1725 gtk_widget_grab_focus(widget);
1726
1727 switch (event->direction)
1728 {
1729 case GDK_SCROLL_UP:
1730 button = MOUSE_4;
1731 break;
1732 case GDK_SCROLL_DOWN:
1733 button = MOUSE_5;
1734 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001735 case GDK_SCROLL_LEFT:
1736 button = MOUSE_7;
1737 break;
1738 case GDK_SCROLL_RIGHT:
1739 button = MOUSE_6;
1740 break;
1741 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return FALSE;
1743 }
1744
1745# ifdef FEAT_XIM
1746 /* cancel any preediting */
1747 if (im_is_preediting())
1748 xim_reset();
1749# endif
1750
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001751 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1754 FALSE, vim_modifiers);
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 return TRUE;
1757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001761button_release_event(GtkWidget *widget UNUSED,
1762 GdkEventButton *event,
1763 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764{
1765 int x, y;
1766 int_u vim_modifiers;
1767
Bram Moolenaar20892c12011-06-26 04:49:00 +02001768 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001769
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 /* Remove any motion "machine gun" timers used for automatic further
1771 extension of allocation areas if outside of the applications window
1772 area .*/
1773 if (motion_repeat_timer)
1774 {
1775 gtk_timeout_remove(motion_repeat_timer);
1776 motion_repeat_timer = 0;
1777 }
1778
1779 x = event->x;
1780 y = event->y;
1781
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001782 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
1786 return TRUE;
1787}
1788
1789
1790#ifdef FEAT_DND
1791/****************************************************************************
1792 * Drag aNd Drop support handlers.
1793 */
1794
1795/*
1796 * Count how many items there may be and separate them with a NUL.
1797 * Apparently the items are separated with \r\n. This is not documented,
1798 * thus be careful not to go past the end. Also allow separation with
1799 * NUL characters.
1800 */
1801 static int
1802count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1803{
1804 int i;
1805 char_u *p = out;
1806 int count = 0;
1807
1808 for (i = 0; i < len; ++i)
1809 {
1810 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1811 {
1812 if (p > out && p[-1] != NUL)
1813 {
1814 ++count;
1815 *p++ = NUL;
1816 }
1817 }
1818 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1819 {
1820 *p++ = hexhex2nr(raw + i + 1);
1821 i += 2;
1822 }
1823 else
1824 *p++ = raw[i];
1825 }
1826 if (p > out && p[-1] != NUL)
1827 {
1828 *p = NUL; /* last item didn't have \r or \n */
1829 ++count;
1830 }
1831 return count;
1832}
1833
1834/*
1835 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1836 * this process, URI which protocol is not "file:" are removed. Return
1837 * length of array (less than "max").
1838 */
1839 static int
1840filter_uri_list(char_u **outlist, int max, char_u *src)
1841{
1842 int i, j;
1843
1844 for (i = j = 0; i < max; ++i)
1845 {
1846 outlist[i] = NULL;
1847 if (STRNCMP(src, "file:", 5) == 0)
1848 {
1849 src += 5;
1850 if (STRNCMP(src, "//localhost", 11) == 0)
1851 src += 11;
1852 while (src[0] == '/' && src[1] == '/')
1853 ++src;
1854 outlist[j++] = vim_strsave(src);
1855 }
1856 src += STRLEN(src) + 1;
1857 }
1858 return j;
1859}
1860
1861 static char_u **
1862parse_uri_list(int *count, char_u *data, int len)
1863{
1864 int n = 0;
1865 char_u *tmp = NULL;
1866 char_u **array = NULL;;
1867
1868 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1869 {
1870 n = count_and_decode_uri_list(tmp, data, len);
1871 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1872 n = filter_uri_list(array, n, tmp);
1873 }
1874 vim_free(tmp);
1875 *count = n;
1876 return array;
1877}
1878
1879 static void
1880drag_handle_uri_list(GdkDragContext *context,
1881 GtkSelectionData *data,
1882 guint time_,
1883 GdkModifierType state,
1884 gint x,
1885 gint y)
1886{
1887 char_u **fnames;
1888 int nfiles = 0;
1889
1890 fnames = parse_uri_list(&nfiles, data->data, data->length);
1891
1892 if (fnames != NULL && nfiles > 0)
1893 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001894 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1897
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001898 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
1900 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1901 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001902 else
1903 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904}
1905
1906 static void
1907drag_handle_text(GdkDragContext *context,
1908 GtkSelectionData *data,
1909 guint time_,
1910 GdkModifierType state)
1911{
1912 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1913 char_u *text;
1914 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
1917 text = data->data;
1918 len = data->length;
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (data->type == utf8_string_atom)
1921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (input_conv.vc_type != CONV_NONE)
1923 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (tmpbuf != NULL)
1925 text = tmpbuf;
1926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 dnd_yank_drag_data(text, (long)len);
1929 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001932 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934 if (dropkey[2] != 0)
1935 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1936 else
1937 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938}
1939
1940/*
1941 * DND receiver.
1942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 static void
1944drag_data_received_cb(GtkWidget *widget,
1945 GdkDragContext *context,
1946 gint x,
1947 gint y,
1948 GtkSelectionData *data,
1949 guint info,
1950 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001951 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 GdkModifierType state;
1954
1955 /* Guard against trash */
1956 if (data->data == NULL
1957 || data->length <= 0
1958 || data->format != 8
1959 || data->data[data->length] != '\0')
1960 {
1961 gtk_drag_finish(context, FALSE, FALSE, time_);
1962 return;
1963 }
1964
1965 /* Get the current modifier state for proper distinguishment between
1966 * different operations later. */
1967 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1968
1969 /* Not sure about the role of "text/plain" here... */
1970 if (info == (guint)TARGET_TEXT_URI_LIST)
1971 drag_handle_uri_list(context, data, time_, state, x, y);
1972 else
1973 drag_handle_text(context, data, time_, state);
1974
1975}
1976#endif /* FEAT_DND */
1977
1978
1979#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1980/*
1981 * GnomeClient interact callback. Check for unsaved buffers that cannot
1982 * be abandoned and pop up a dialog asking the user for confirmation if
1983 * necessary.
1984 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001986sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001988 GnomeDialogType type UNUSED,
1989 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990{
1991 cmdmod_T save_cmdmod;
1992 gboolean shutdown_cancelled;
1993
1994 save_cmdmod = cmdmod;
1995
1996# ifdef FEAT_BROWSE
1997 cmdmod.browse = TRUE;
1998# endif
1999# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2000 cmdmod.confirm = TRUE;
2001# endif
2002 /*
2003 * If there are changed buffers, present the user with
2004 * a dialog if possible, otherwise give an error message.
2005 */
2006 shutdown_cancelled = check_changed_any(FALSE);
2007
2008 exiting = FALSE;
2009 cmdmod = save_cmdmod;
2010 setcursor(); /* position the cursor */
2011 out_flush();
2012 /*
2013 * If the user hit the [Cancel] button the whole shutdown
2014 * will be cancelled. Wow, quite powerful feature (:
2015 */
2016 gnome_interaction_key_return(key, shutdown_cancelled);
2017}
2018
2019/*
2020 * Generate a script that can be used to restore the current editing session.
2021 * Save the value of v:this_session before running :mksession in order to make
2022 * automagic session save fully transparent. Return TRUE on success.
2023 */
2024 static int
2025write_session_file(char_u *filename)
2026{
2027 char_u *escaped_filename;
2028 char *mksession_cmdline;
2029 unsigned int save_ssop_flags;
2030 int failed;
2031
2032 /*
2033 * Build an ex command line to create a script that restores the current
2034 * session if executed. Escape the filename to avoid nasty surprises.
2035 */
2036 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2037 if (escaped_filename == NULL)
2038 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002039 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2040 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 /*
2044 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2045 * unpredictable effects when the session is saved automatically. Also,
2046 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2047 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2048 * enormously large if the GNOME session feature is used regularly.
2049 */
2050 save_ssop_flags = ssop_flags;
2051 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002052 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053
2054 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2055 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2056 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002057 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 ssop_flags = save_ssop_flags;
2060 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 /*
2063 * Reopen the file and append a command to restore v:this_session,
2064 * as if this save never happened. This is to avoid conflicts with
2065 * the user's own sessions. FIXME: It's probably less hackish to add
2066 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2067 */
2068 if (!failed)
2069 {
2070 FILE *fd;
2071
2072 fd = open_exfile(filename, TRUE, APPENDBIN);
2073
2074 failed = (fd == NULL
2075 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2076 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2077
2078 if (fd != NULL && fclose(fd) != 0)
2079 failed = TRUE;
2080
2081 if (failed)
2082 mch_remove(filename);
2083 }
2084
2085 return !failed;
2086}
2087
2088/*
2089 * "save_yourself" signal handler. Initiate an interaction to ask the user
2090 * for confirmation if necessary. Save the current editing session and tell
2091 * the session manager how to restart Vim.
2092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 static gboolean
2094sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002095 gint phase UNUSED,
2096 GnomeSaveStyle save_style UNUSED,
2097 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002099 gboolean fast UNUSED,
2100 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 static const char suffix[] = "-session.vim";
2103 char *session_file;
2104 unsigned int len;
2105 gboolean success;
2106
2107 /* Always request an interaction if possible. check_changed_any()
2108 * won't actually show a dialog unless any buffers have been modified.
2109 * There doesn't seem to be an obvious way to check that without
2110 * automatically firing the dialog. Anyway, it works just fine. */
2111 if (interact_style == GNOME_INTERACT_ANY)
2112 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2113 &sm_client_check_changed_any,
2114 NULL);
2115 out_flush();
2116 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2117
2118 /* The path is unique for each session save. We do neither know nor care
2119 * which session script will actually be used later. This decision is in
2120 * the domain of the session manager. */
2121 session_file = gnome_config_get_real_path(
2122 gnome_client_get_config_prefix(client));
2123 len = strlen(session_file);
2124
2125 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2126 --len; /* get rid of the superfluous trailing '/' */
2127
2128 session_file = g_renew(char, session_file, len + sizeof(suffix));
2129 memcpy(session_file + len, suffix, sizeof(suffix));
2130
2131 success = write_session_file((char_u *)session_file);
2132
2133 if (success)
2134 {
2135 const char *argv[8];
2136 int i;
2137
2138 /* Tell the session manager how to wipe out the stored session data.
2139 * This isn't as dangerous as it looks, don't worry :) session_file
2140 * is a unique absolute filename. Usually it'll be something like
2141 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2142 i = 0;
2143 argv[i++] = "rm";
2144 argv[i++] = session_file;
2145 argv[i] = NULL;
2146
2147 gnome_client_set_discard_command(client, i, (char **)argv);
2148
2149 /* Tell the session manager how to restore the just saved session.
2150 * This is easily done thanks to Vim's -S option. Pass the -f flag
2151 * since there's no need to fork -- it might even cause confusion.
2152 * Also pass the window role to give the WM something to match on.
2153 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2154 i = 0;
2155 argv[i++] = restart_command;
2156 argv[i++] = "-f";
2157 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 argv[i++] = "--role";
2159 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 argv[i++] = "-S";
2161 argv[i++] = session_file;
2162 argv[i] = NULL;
2163
2164 gnome_client_set_restart_command(client, i, (char **)argv);
2165 gnome_client_set_clone_command(client, 0, NULL);
2166 }
2167
2168 g_free(session_file);
2169
2170 return success;
2171}
2172
2173/*
2174 * Called when the session manager wants us to die. There isn't much to save
2175 * here since "save_yourself" has been emitted before (unless serious trouble
2176 * is happening).
2177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002179sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180{
2181 /* Don't write messages to the GUI anymore */
2182 full_screen = FALSE;
2183
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002184 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002185 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002186 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 preserve_exit();
2188}
2189
2190/*
2191 * Connect our signal handlers to be notified on session save and shutdown.
2192 */
2193 static void
2194setup_save_yourself(void)
2195{
2196 GnomeClient *client;
2197
2198 client = gnome_master_client();
2199
2200 if (client != NULL)
2201 {
2202 /* Must use the deprecated gtk_signal_connect() for compatibility
2203 * with GNOME 1. Arrgh, zombies! */
2204 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2205 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2206 gtk_signal_connect(GTK_OBJECT(client), "die",
2207 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2208 }
2209}
2210
2211#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2212
2213# ifdef USE_XSMP
2214/*
2215 * GTK tells us that XSMP needs attention
2216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 static gboolean
2218local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002219 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 GIOCondition condition;
2221 gpointer data;
2222{
2223 if (condition == G_IO_IN)
2224 {
2225 /* Do stuff; maybe close connection */
2226 if (xsmp_handle_requests() == FAIL)
2227 g_io_channel_unref((GIOChannel *)data);
2228 return TRUE;
2229 }
2230 /* Error */
2231 g_io_channel_unref((GIOChannel *)data);
2232 xsmp_close();
2233 return TRUE;
2234}
2235# endif /* USE_XSMP */
2236
2237/*
2238 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2239 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2240 */
2241 static void
2242setup_save_yourself(void)
2243{
2244 Atom *existing_atoms = NULL;
2245 int count = 0;
2246
2247#ifdef USE_XSMP
2248 if (xsmp_icefd != -1)
2249 {
2250 /*
2251 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2252 * set up GTK IO monitor
2253 */
2254 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2255
2256 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2257 local_xsmp_handle_requests, (gpointer)g_io);
2258 }
2259 else
2260#endif
2261 {
2262 /* Fall back to old method */
2263
2264 /* first get the existing value */
2265 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2266 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2267 &existing_atoms, &count))
2268 {
2269 Atom *new_atoms;
2270 Atom save_yourself_xatom;
2271 int i;
2272
2273 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2274
2275 /* check if WM_SAVE_YOURSELF isn't there yet */
2276 for (i = 0; i < count; ++i)
2277 if (existing_atoms[i] == save_yourself_xatom)
2278 break;
2279
2280 if (i == count)
2281 {
2282 /* allocate an Atoms array which is one item longer */
2283 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2284 * sizeof(Atom)));
2285 if (new_atoms != NULL)
2286 {
2287 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2288 new_atoms[count] = save_yourself_xatom;
2289 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2290 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2291 new_atoms, count + 1);
2292 vim_free(new_atoms);
2293 }
2294 }
2295 XFree(existing_atoms);
2296 }
2297 }
2298}
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300/*
2301 * Installing a global event filter seems to be the only way to catch
2302 * client messages of type WM_PROTOCOLS without overriding GDK's own
2303 * client message event filter. Well, that's still better than trying
2304 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 *
2306 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2307 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2308 * I have the nasty feeling modern session managers just don't send this
2309 * deprecated message anymore. Addition: confirmed by several people.
2310 *
2311 * The GNOME session support is much cooler anyway. Unlike this ugly
2312 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2313 * it should work with KDE as well.
2314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002316global_event_filter(GdkXEvent *xev,
2317 GdkEvent *event UNUSED,
2318 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320 XEvent *xevent = (XEvent *)xev;
2321
2322 if (xevent != NULL
2323 && xevent->type == ClientMessage
2324 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002325 && (long_u)xevent->xclient.data.l[0]
2326 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 out_flush();
2329 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2330 /*
2331 * Set the window's WM_COMMAND property, to let the window manager
2332 * know we are done saving ourselves. We don't want to be
2333 * restarted, thus set argv to NULL.
2334 */
2335 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2336 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2337 NULL, 0);
2338 return GDK_FILTER_REMOVE;
2339 }
2340
2341 return GDK_FILTER_CONTINUE;
2342}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2344
2345
2346/*
2347 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002350mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351{
2352/* If you get an error message here, you still need to unpack the runtime
2353 * archive! */
2354#ifdef magick
2355# undef magick
2356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 /* A bit hackish, but avoids casting later and allows optimization */
2358# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359#define magick vim32x32
2360#include "../runtime/vim32x32.xpm"
2361#undef magick
2362#define magick vim16x16
2363#include "../runtime/vim16x16.xpm"
2364#undef magick
2365#define magick vim48x48
2366#include "../runtime/vim48x48.xpm"
2367#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /* When started with "--echo-wid" argument, write window ID on stdout. */
2371 if (echo_wid_arg)
2372 {
2373 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2374 fflush(stdout);
2375 }
2376
2377 if (vim_strchr(p_go, GO_ICON) != NULL)
2378 {
2379 /*
2380 * Add an icon to the main window. For fun and convenience of the user.
2381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 GList *icons = NULL;
2383
2384 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2385 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2386 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2387
2388 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2389
2390 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2391 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393
2394#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2395 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#endif
2398 /* Setup to indicate to the window manager that we want to catch the
2399 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2400 * manager instead. */
2401#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2402 if (using_gnome)
2403#endif
2404 setup_save_yourself();
2405
2406#ifdef FEAT_CLIENTSERVER
2407 if (serverName == NULL && serverDelayedStartName != NULL)
2408 {
2409 /* This is a :gui command in a plain vim with no previous server */
2410 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2411
2412 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2413 serverDelayedStartName);
2414 }
2415 else
2416 {
2417 /*
2418 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2419 * have to change the "server" registration to that of the main window
2420 * If we have not registered a name yet, remember the window
2421 */
2422 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2423 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2424 }
2425 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2426 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2427 GTK_SIGNAL_FUNC(property_event), NULL);
2428#endif
2429}
2430
2431 static GdkCursor *
2432create_blank_pointer(void)
2433{
2434 GdkWindow *root_window = NULL;
2435 GdkPixmap *blank_mask;
2436 GdkCursor *cursor;
2437 GdkColor color = { 0, 0, 0, 0 };
2438 char blank_data[] = { 0x0 };
2439
2440#ifdef HAVE_GTK_MULTIHEAD
2441 root_window = gtk_widget_get_root_window(gui.mainwin);
2442#endif
2443
2444 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2445 * in size. */
2446 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2447 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2448 &color, &color, 0, 0);
2449 gdk_bitmap_unref(blank_mask);
2450
2451 return cursor;
2452}
2453
2454#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 static void
2456mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002457 GdkScreen *previous_screen UNUSED,
2458 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459{
2460 if (!gtk_widget_has_screen(widget))
2461 return;
2462
2463 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002464 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
2466 if (gui.blank_pointer != NULL)
2467 gdk_cursor_unref(gui.blank_pointer);
2468
2469 gui.blank_pointer = create_blank_pointer();
2470
2471 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2472 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2473
2474 /*
2475 * Create a new PangoContext for this screen, and initialize it
2476 * with the current font if necessary.
2477 */
2478 if (gui.text_context != NULL)
2479 g_object_unref(gui.text_context);
2480
2481 gui.text_context = gtk_widget_create_pango_context(widget);
2482 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2483
2484 if (gui.norm_font != NULL)
2485 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002486 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002487 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489}
2490#endif /* HAVE_GTK_MULTIHEAD */
2491
2492/*
2493 * After the drawing area comes up, we calculate all colors and create the
2494 * dummy blank cursor.
2495 *
2496 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2497 * fact that the main VIM engine doesn't take them into account anywhere.
2498 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002500drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501{
2502 GtkWidget *sbar;
2503
2504#ifdef FEAT_XIM
2505 xim_init();
2506#endif
2507 gui_mch_new_colors();
2508 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2509
2510 gui.blank_pointer = create_blank_pointer();
2511 if (gui.pointer_hidden)
2512 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2513
2514 /* get the actual size of the scrollbars, if they are realized */
2515 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2516 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2517 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2518 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2519 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2520 gui.scrollbar_width = sbar->allocation.width;
2521
2522 sbar = gui.bottom_sbar.id;
2523 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2524 gui.scrollbar_height = sbar->allocation.height;
2525}
2526
2527/*
2528 * Properly clean up on shutdown.
2529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002531drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 /* Don't write messages to the GUI anymore */
2534 full_screen = FALSE;
2535
2536#ifdef FEAT_XIM
2537 im_shutdown();
2538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (gui.ascii_glyphs != NULL)
2540 {
2541 pango_glyph_string_free(gui.ascii_glyphs);
2542 gui.ascii_glyphs = NULL;
2543 }
2544 if (gui.ascii_font != NULL)
2545 {
2546 g_object_unref(gui.ascii_font);
2547 gui.ascii_font = NULL;
2548 }
2549 g_object_unref(gui.text_context);
2550 gui.text_context = NULL;
2551
2552 g_object_unref(gui.text_gc);
2553 gui.text_gc = NULL;
2554
2555 gdk_cursor_unref(gui.blank_pointer);
2556 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557}
2558
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002560drawarea_style_set_cb(GtkWidget *widget UNUSED,
2561 GtkStyle *previous_style UNUSED,
2562 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 gui_mch_new_colors();
2565}
2566
2567/*
2568 * Callback routine for the "delete_event" signal on the toplevel window.
2569 * Tries to vim gracefully, or refuses to exit with changed buffers.
2570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002572delete_event_cb(GtkWidget *widget UNUSED,
2573 GdkEventAny *event UNUSED,
2574 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 gui_shell_closed();
2577 return TRUE;
2578}
2579
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002580#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2581 static int
2582get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2583{
2584 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2585
2586#ifdef FEAT_GUI_GNOME
2587 if (using_gnome && widget != NULL)
2588 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002589 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002590 BonoboDockItem *dockitem;
2591
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002592 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002593 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2594 {
2595 /* Only menu & toolbar are dock items. Could tabline be?
2596 * Seem to be only the 2 defined in GNOME */
2597 widget = parent;
2598 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002599
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002600 if (dockitem == NULL || dockitem->is_floating)
2601 return 0;
2602 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2603 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002604 }
2605#endif
2606 if (widget != NULL
2607 && item_orientation == orientation
2608 && GTK_WIDGET_REALIZED(widget)
2609 && GTK_WIDGET_VISIBLE(widget))
2610 {
2611 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2612 return widget->allocation.height;
2613 else
2614 return widget->allocation.width;
2615 }
2616 return 0;
2617}
2618#endif
2619
2620 static int
2621get_menu_tool_width(void)
2622{
2623 int width = 0;
2624
2625#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2626# ifdef FEAT_MENU
2627 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2628# endif
2629# ifdef FEAT_TOOLBAR
2630 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2631# endif
2632# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002633 if (gui.tabline != NULL)
2634 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002635# endif
2636#endif
2637
2638 return width;
2639}
2640
2641 static int
2642get_menu_tool_height(void)
2643{
2644 int height = 0;
2645
2646#ifdef FEAT_MENU
2647 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2648#endif
2649#ifdef FEAT_TOOLBAR
2650 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2651#endif
2652#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002653 if (gui.tabline != NULL)
2654 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002655#endif
2656
2657 return height;
2658}
2659
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002660/* This controls whether we can set the real window hints at
2661 * start-up when in a GtkPlug.
2662 * 0 = normal processing (default)
2663 * 1 = init. hints set, no-one's tried to reset since last check
2664 * 2 = init. hints set, attempt made to change hints
2665 */
2666static int init_window_hints_state = 0;
2667
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002668 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002669update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002670{
2671 static int old_width = 0;
2672 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002673 static int old_min_width = 0;
2674 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002675 static int old_char_width = 0;
2676 static int old_char_height = 0;
2677
2678 int width;
2679 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002680 int min_width;
2681 int min_height;
2682
2683 /* At start-up, don't try to set the hints until the initial
2684 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002685 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002686 */
2687 if (!(force_width && force_height) && init_window_hints_state > 0)
2688 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002689 /* Don't do it! */
2690 init_window_hints_state = 2;
2691 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002692 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002693
2694 /* This also needs to be done when the main window isn't there yet,
2695 * otherwise the hints don't work. */
2696 width = gui_get_base_width();
2697 height = gui_get_base_height();
2698# ifdef FEAT_MENU
2699 height += tabline_height() * gui.char_height;
2700# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002701 width += get_menu_tool_width();
2702 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002703
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002704 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002705 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002706 * 'set columns=' or init. -geom) we briefly set the min. to the size
2707 * we wish to be instead of the legitimate minimum so that we actually
2708 * resize correctly.
2709 */
2710 if (force_width && force_height)
2711 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002712 min_width = force_width;
2713 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002714 }
2715 else
2716 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002717 min_width = width + MIN_COLUMNS * gui.char_width;
2718 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002719 }
2720
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002721 /* Avoid an expose event when the size didn't change. */
2722 if (width != old_width
2723 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002724 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002725 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002726 || gui.char_width != old_char_width
2727 || gui.char_height != old_char_height)
2728 {
2729 GdkGeometry geometry;
2730 GdkWindowHints geometry_mask;
2731
2732 geometry.width_inc = gui.char_width;
2733 geometry.height_inc = gui.char_height;
2734 geometry.base_width = width;
2735 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002736 geometry.min_width = min_width;
2737 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002738 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2739 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002740 /* Using gui.formwin as geometry widget doesn't work as expected
2741 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2742 * in Vim confuse GTK+. */
2743 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2744 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002745 old_width = width;
2746 old_height = height;
2747 old_min_width = min_width;
2748 old_min_height = min_height;
2749 old_char_width = gui.char_width;
2750 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002751 }
2752}
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754#ifdef FEAT_TOOLBAR
2755
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756/*
2757 * This extra effort wouldn't be necessary if we only used stock icons in the
2758 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2759 * shouldn't be treated differently, thus we do need this.
2760 */
2761 static void
2762icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2763{
2764 if (GTK_IS_IMAGE(widget))
2765 {
2766 GtkImage *image = (GtkImage *)widget;
2767
2768 /* User-defined icons are stored in a GtkIconSet */
2769 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2770 {
2771 GtkIconSet *icon_set;
2772 GtkIconSize icon_size;
2773
2774 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2775 icon_size = (GtkIconSize)(long)user_data;
2776
2777 gtk_icon_set_ref(icon_set);
2778 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2779 gtk_icon_set_unref(icon_set);
2780 }
2781 }
2782 else if (GTK_IS_CONTAINER(widget))
2783 {
2784 gtk_container_foreach((GtkContainer *)widget,
2785 &icon_size_changed_foreach,
2786 user_data);
2787 }
2788}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789
2790 static void
2791set_toolbar_style(GtkToolbar *toolbar)
2792{
2793 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 GtkIconSize size;
2795 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2798 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2799 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002800 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2802 style = GTK_TOOLBAR_BOTH;
2803 else if (toolbar_flags & TOOLBAR_TEXT)
2804 style = GTK_TOOLBAR_TEXT;
2805 else
2806 style = GTK_TOOLBAR_ICONS;
2807
2808 gtk_toolbar_set_style(toolbar, style);
2809 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2810
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 switch (tbis_flags)
2812 {
2813 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2814 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2815 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2816 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2817 default: size = GTK_ICON_SIZE_INVALID; break;
2818 }
2819 oldsize = gtk_toolbar_get_icon_size(toolbar);
2820
2821 if (size == GTK_ICON_SIZE_INVALID)
2822 {
2823 /* Let global user preferences decide the icon size. */
2824 gtk_toolbar_unset_icon_size(toolbar);
2825 size = gtk_toolbar_get_icon_size(toolbar);
2826 }
2827 if (size != oldsize)
2828 {
2829 gtk_container_foreach(GTK_CONTAINER(toolbar),
2830 &icon_size_changed_foreach,
2831 GINT_TO_POINTER((int)size));
2832 }
2833 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834}
2835
2836#endif /* FEAT_TOOLBAR */
2837
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002838#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2839static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002840static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002841static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002842static int clicked_page; /* page clicked in tab line */
2843
2844/*
2845 * Handle selecting an item in the tab line popup menu.
2846 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002847 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002848tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002849{
Bram Moolenaara5621492006-02-25 21:55:24 +00002850 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002851 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002852}
2853
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002854 static void
2855add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2856{
2857 GtkWidget *item;
2858 char_u *utf_text;
2859
2860 utf_text = CONVERT_TO_UTF8(text);
2861 item = gtk_menu_item_new_with_label((const char *)utf_text);
2862 gtk_widget_show(item);
2863 CONVERT_TO_UTF8_FREE(utf_text);
2864
2865 gtk_container_add(GTK_CONTAINER(menu), item);
2866 gtk_signal_connect(GTK_OBJECT(item), "activate",
2867 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002868 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002869}
2870
Bram Moolenaara5621492006-02-25 21:55:24 +00002871/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002872 * Create a menu for the tab line.
2873 */
2874 static GtkWidget *
2875create_tabline_menu(void)
2876{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002877 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002878
2879 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02002880 if (first_tabpage->tp_next != NULL)
2881 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
2882 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002883 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2884 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002885
2886 return menu;
2887}
2888
2889 static gboolean
2890on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2891{
2892 /* Was this button press event ? */
2893 if (event->type == GDK_BUTTON_PRESS)
2894 {
2895 GdkEventButton *bevent = (GdkEventButton *)event;
2896 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002897 int y = bevent->y;
2898 GtkWidget *tabwidget;
2899 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002900
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002901 /* When ignoring events return TRUE so that the selected page doesn't
2902 * change. */
2903 if (hold_gui_events
2904# ifdef FEAT_CMDWIN
2905 || cmdwin_type != 0
2906# endif
2907 )
2908 return TRUE;
2909
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002910 tabwin = gdk_window_at_pointer(&x, &y);
2911 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2912 clicked_page = (int)(long)gtk_object_get_user_data(
2913 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002914
2915 /* If the event was generated for 3rd button popup the menu. */
2916 if (bevent->button == 3)
2917 {
2918 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2919 bevent->button, bevent->time);
2920 /* We handled the event. */
2921 return TRUE;
2922 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002923 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002924 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002925 if (clicked_page == 0)
2926 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002927 /* Click after all tabs moves to next tab page. When "x" is
2928 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002929 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002930 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002931 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002932 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002933
Bram Moolenaara5621492006-02-25 21:55:24 +00002934 /* We didn't handle the event. */
2935 return FALSE;
2936}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002937
2938/*
2939 * Handle selecting one of the tabs.
2940 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002941 static void
2942on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002943 GtkNotebook *notebook UNUSED,
2944 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002945 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002946 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002947{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002948 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002949 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002950 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002951 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002952}
2953
2954/*
2955 * Show or hide the tabline.
2956 */
2957 void
2958gui_mch_show_tabline(int showit)
2959{
2960 if (gui.tabline == NULL)
2961 return;
2962
2963 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2964 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002965 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002966 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002967 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002968 if (showit)
2969 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002970 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002971
2972 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002973}
2974
2975/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002976 * Return TRUE when tabline is displayed.
2977 */
2978 int
2979gui_mch_showing_tabline(void)
2980{
2981 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002982 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002983}
2984
2985/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002986 * Update the labels of the tabline.
2987 */
2988 void
2989gui_mch_update_tabline(void)
2990{
2991 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002992 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002993 GtkWidget *label;
2994 tabpage_T *tp;
2995 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002996 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002997 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002998 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002999
3000 if (gui.tabline == NULL)
3001 return;
3002
3003 ignore_tabline_evt = TRUE;
3004
3005 /* Add a label for each tab page. They all contain the same text area. */
3006 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3007 {
3008 if (tp == curtab)
3009 curtabidx = nr;
3010
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003011 tab_num = nr + 1;
3012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003013 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3014 if (page == NULL)
3015 {
3016 /* Add notebook page */
3017 page = gtk_vbox_new(FALSE, 0);
3018 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003019 event_box = gtk_event_box_new();
3020 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003021 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003022 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003023 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003024 gtk_widget_show(label);
3025 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3026 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003027 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003028 nr++);
3029 }
3030
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003031 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003032 gtk_object_set_user_data(GTK_OBJECT(event_box),
3033 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003034 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003035 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003036 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003037 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003038 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003039
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003040 get_tabline_label(tp, TRUE);
3041 labeltext = CONVERT_TO_UTF8(NameBuff);
3042 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3043 (const char *)labeltext, NULL);
3044 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003045 }
3046
3047 /* Remove any old labels. */
3048 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3049 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3050
3051 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003052 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003053
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003054 /* Make sure everything is in place before drawing text. */
3055 gui_mch_update();
3056
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003057 ignore_tabline_evt = FALSE;
3058}
3059
3060/*
3061 * Set the current tab to "nr". First tab is 1.
3062 */
3063 void
3064gui_mch_set_curtab(nr)
3065 int nr;
3066{
3067 if (gui.tabline == NULL)
3068 return;
3069
3070 ignore_tabline_evt = TRUE;
3071 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003072 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 ignore_tabline_evt = FALSE;
3074}
3075
3076#endif /* FEAT_GUI_TABLINE */
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003079 * Add selection targets for PRIMARY and CLIPBOARD selections.
3080 */
3081 void
3082gui_gtk_set_selection_targets(void)
3083{
3084 int i, j = 0;
3085 int n_targets = N_SELECTION_TARGETS;
3086 GtkTargetEntry targets[N_SELECTION_TARGETS];
3087
3088 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3089 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003090 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003091 * return something, instead of trying another target. Therefore only
3092 * offer TARGET_HTML when it works. */
3093 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3094 n_targets--;
3095 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003096 targets[j++] = selection_targets[i];
3097 }
3098
3099 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3100 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3101 gtk_selection_add_targets(gui.drawarea,
3102 (GdkAtom)GDK_SELECTION_PRIMARY,
3103 targets, n_targets);
3104 gtk_selection_add_targets(gui.drawarea,
3105 (GdkAtom)clip_plus.gtk_sel_atom,
3106 targets, n_targets);
3107}
3108
3109/*
3110 * Set up for receiving DND items.
3111 */
3112 void
3113gui_gtk_set_dnd_targets(void)
3114{
3115 int i, j = 0;
3116 int n_targets = N_DND_TARGETS;
3117 GtkTargetEntry targets[N_DND_TARGETS];
3118
3119 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3120 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003121 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003122 n_targets--;
3123 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003124 targets[j++] = dnd_targets[i];
3125 }
3126
3127 gtk_drag_dest_unset(gui.drawarea);
3128 gtk_drag_dest_set(gui.drawarea,
3129 GTK_DEST_DEFAULT_ALL,
3130 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003131 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003132}
3133
3134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3136 * Returns OK for success, FAIL when the GUI can't be started.
3137 */
3138 int
3139gui_mch_init(void)
3140{
3141 GtkWidget *vbox;
3142
3143#ifdef FEAT_GUI_GNOME
3144 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3145 * exits on failure, but that's a non-issue because we already called
3146 * gtk_init_check() in gui_mch_init_check(). */
3147 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3150 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003151# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003152 {
3153 char *p = setlocale(LC_NUMERIC, NULL);
3154
3155 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3156 * init may change it. */
3157 if (p == NULL || strcmp(p, "C") != 0)
3158 setlocale(LC_NUMERIC, "C");
3159 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003160# endif
3161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162#endif
3163 vim_free(gui_argv);
3164 gui_argv = NULL;
3165
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003166#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 /* Set the human-readable application name */
3168 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 /*
3171 * Force UTF-8 output no matter what the value of 'encoding' is.
3172 * did_set_string_option() in option.c prohibits changing 'termencoding'
3173 * to something else than UTF-8 if the GUI is in use.
3174 */
3175 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3176
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003177#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3181 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003182#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184#endif
3185
3186 /* Initialize values */
3187 gui.border_width = 2;
3188 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3189 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003190 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003192 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003194 /* LINTED: avoid warning: conversion to 'unsigned long' */
3195 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003198 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200
3201 /* Set default foreground and background colors. */
3202 gui.norm_pixel = gui.def_norm_pixel;
3203 gui.back_pixel = gui.def_back_pixel;
3204
3205 if (gtk_socket_id != 0)
3206 {
3207 GtkWidget *plug;
3208
3209 /* Use GtkSocket from another app. */
3210#ifdef HAVE_GTK_MULTIHEAD
3211 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3212 gtk_socket_id);
3213#else
3214 plug = gtk_plug_new(gtk_socket_id);
3215#endif
3216 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3217 {
3218 gui.mainwin = plug;
3219 }
3220 else
3221 {
3222 g_warning("Connection to GTK+ socket (ID %u) failed",
3223 (unsigned int)gtk_socket_id);
3224 /* Pretend we never wanted it if it failed (get own window) */
3225 gtk_socket_id = 0;
3226 }
3227 }
3228
3229 if (gtk_socket_id == 0)
3230 {
3231#ifdef FEAT_GUI_GNOME
3232 if (using_gnome)
3233 {
3234 gui.mainwin = gnome_app_new("Vim", NULL);
3235# ifdef USE_XSMP
3236 /* Use the GNOME save-yourself functionality now. */
3237 xsmp_close();
3238# endif
3239 }
3240 else
3241#endif
3242 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3243 }
3244
3245 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 /* Create the PangoContext used for drawing all text. */
3248 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3249 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3252 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3253
3254 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3255 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3256
3257 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3258 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3259#ifdef HAVE_GTK_MULTIHEAD
3260 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3261 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 gui.accel_group = gtk_accel_group_new();
3264 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003266 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 vbox = gtk_vbox_new(FALSE, 0);
3268
3269#ifdef FEAT_GUI_GNOME
3270 if (using_gnome)
3271 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003272# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /* automagically restore menubar/toolbar placement */
3274 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3275# endif
3276 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3277 }
3278 else
3279#endif
3280 {
3281 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3282 gtk_widget_show(vbox);
3283 }
3284
3285#ifdef FEAT_MENU
3286 /*
3287 * Create the menubar and handle
3288 */
3289 gui.menubar = gtk_menu_bar_new();
3290 gtk_widget_set_name(gui.menubar, "vim-menubar");
3291
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003292 /* Avoid that GTK takes <F10> away from us. */
3293 {
3294 GtkSettings *gtk_settings;
3295
3296 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3297 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3298 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003299
3300
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301# ifdef FEAT_GUI_GNOME
3302 if (using_gnome)
3303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 BonoboDockItem *dockitem;
3305
3306 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3307 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3308 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003309 /* We don't want the menu to float. */
3310 bonobo_dock_item_set_behavior(dockitem,
3311 bonobo_dock_item_get_behavior(dockitem)
3312 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315 else
3316# endif /* FEAT_GUI_GNOME */
3317 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003318 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3319 * disabled in gui_init() later. */
3320 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3322 }
3323#endif /* FEAT_MENU */
3324
3325#ifdef FEAT_TOOLBAR
3326 /*
3327 * Create the toolbar and handle
3328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 /* some aesthetics on the toolbar */
3330 gtk_rc_parse_string(
3331 "style \"vim-toolbar-style\" {\n"
3332 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3333 "}\n"
3334 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3335 gui.toolbar = gtk_toolbar_new();
3336 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3338
3339# ifdef FEAT_GUI_GNOME
3340 if (using_gnome)
3341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 BonoboDockItem *dockitem;
3343
3344 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3345 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3346 GNOME_APP_TOOLBAR_NAME);
3347 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003348 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003349 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003350 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003351 bonobo_dock_item_get_behavior(dockitem)
3352 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 }
3355 else
3356# endif /* FEAT_GUI_GNOME */
3357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3359 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3360 gtk_widget_show(gui.toolbar);
3361 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3362 }
3363#endif /* FEAT_TOOLBAR */
3364
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003365#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003366 /*
3367 * Use a Notebook for the tab pages labels. The labels are hidden by
3368 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003369 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003370 gui.tabline = gtk_notebook_new();
3371 gtk_widget_show(gui.tabline);
3372 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3373 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3374 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003375 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003376 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003377
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003378 tabline_tooltip = gtk_tooltips_new();
3379 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3380
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003381 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003382 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003383
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003384 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003385 page = gtk_vbox_new(FALSE, 0);
3386 gtk_widget_show(page);
3387 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3388 label = gtk_label_new("-Empty-");
3389 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003390 event_box = gtk_event_box_new();
3391 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003392 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003393 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003394 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003395 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003396 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003397
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003398 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003399 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003400
3401 /* Create a popup menu for the tab line and connect it. */
3402 tabline_menu = create_tabline_menu();
3403 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003404 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003405#endif
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 gui.formwin = gtk_form_new();
3408 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3409 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3410
3411 gui.drawarea = gtk_drawing_area_new();
3412
3413 /* Determine which events we will filter. */
3414 gtk_widget_set_events(gui.drawarea,
3415 GDK_EXPOSURE_MASK |
3416 GDK_ENTER_NOTIFY_MASK |
3417 GDK_LEAVE_NOTIFY_MASK |
3418 GDK_BUTTON_PRESS_MASK |
3419 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 GDK_KEY_PRESS_MASK |
3422 GDK_KEY_RELEASE_MASK |
3423 GDK_POINTER_MOTION_MASK |
3424 GDK_POINTER_MOTION_HINT_MASK);
3425
3426 gtk_widget_show(gui.drawarea);
3427 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3428 gtk_widget_show(gui.formwin);
3429 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3430
3431 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3432 * and not the window. */
3433 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3434 : GTK_OBJECT(gui.drawarea),
3435 "key_press_event",
3436 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003437#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 /* Also forward key release events for the benefit of GTK+ 2 input
3439 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3440 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3441 : G_OBJECT(gui.drawarea),
3442 "key_release_event",
3443 G_CALLBACK(&key_release_event), NULL);
3444#endif
3445 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3446 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3447 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3448 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3449
3450 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3451 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3452
3453 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3454
3455#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3456 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3457 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3458#endif
3459
3460 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003461 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3463
3464 /*
3465 * Set clipboard specific atoms
3466 */
3467 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3470 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3471
3472 /*
3473 * Start out by adding the configured border width into the border offset.
3474 */
3475 gui.border_offset = gui.border_width;
3476
3477 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3478 GTK_SIGNAL_FUNC(visibility_event), NULL);
3479 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3480 GTK_SIGNAL_FUNC(expose_event), NULL);
3481
3482 /*
3483 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3484 * Only needed for some window managers.
3485 */
3486 if (vim_strchr(p_go, GO_POINTER) != NULL)
3487 {
3488 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3489 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3490 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3491 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3492 }
3493
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003494 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3495 * only its widgets. Arguably, this could be common code and we not use
3496 * the window focus at all, but let's be safe.
3497 */
3498 if (gtk_socket_id == 0)
3499 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003500 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3501 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3502 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3503 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003504 }
3505 else
3506 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003507 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3508 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3509 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3510 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003511#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003512 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3513 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3514 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3515 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003516#endif /* FEAT_GUI_TABLINE */
3517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3520 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3521 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3522 GTK_SIGNAL_FUNC(button_press_event), NULL);
3523 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3524 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3526 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
3528 /*
3529 * Add selection handler functions.
3530 */
3531 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3532 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3533 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3534 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3535
Bram Moolenaara76638f2010-06-05 12:49:46 +02003536 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3539 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3540
3541 /* Pretend we don't have input focus, we will get an event if we do. */
3542 gui.in_focus = FALSE;
3543
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 return OK;
3545}
3546
3547#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3548/*
3549 * This is called from gui_start() after a fork() has been done.
3550 * We have to tell the session manager our new PID.
3551 */
3552 void
3553gui_mch_forked(void)
3554{
3555 if (using_gnome)
3556 {
3557 GnomeClient *client;
3558
3559 client = gnome_master_client();
3560
3561 if (client != NULL)
3562 gnome_client_set_process_id(client, getpid());
3563 }
3564}
3565#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3566
3567/*
3568 * Called when the foreground or background color has been changed.
3569 * This used to change the graphics contexts directly but we are
3570 * currently manipulating them where desired.
3571 */
3572 void
3573gui_mch_new_colors(void)
3574{
3575 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3576 {
3577 GdkColor color = { 0, 0, 0, 0 };
3578
3579 color.pixel = gui.back_pixel;
3580 gdk_window_set_background(gui.drawarea->window, &color);
3581 }
3582}
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584/*
3585 * This signal informs us about the need to rearrange our sub-widgets.
3586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003588form_configure_event(GtkWidget *widget UNUSED,
3589 GdkEventConfigure *event,
3590 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003592 int usable_height = event->height;
3593
3594 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3595 * no. of char-heights), so we have to manually sanitise them.
3596 * Widths seem to sort themselves out, don't ask me why.
3597 */
3598 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003599 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003602 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 gtk_form_thaw(GTK_FORM(gui.formwin));
3604
3605 return TRUE;
3606}
3607
3608/*
3609 * Function called when window already closed.
3610 * We can't do much more here than to trying to preserve what had been done,
3611 * since the window is already inevitably going away.
3612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003614mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 /* Don't write messages to the GUI anymore */
3617 full_screen = FALSE;
3618
3619 gui.mainwin = NULL;
3620 gui.drawarea = NULL;
3621
3622 if (!exiting) /* only do anything if the destroy was unexpected */
3623 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003624 vim_strncpy(IObuff,
3625 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3626 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 preserve_exit();
3628 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01003629#ifdef USE_GRESOURCE
3630 gui_gtk_unregister_resource();
3631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632}
3633
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003634
3635/*
3636 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3637 * hints (and thus the required size from -geom), but that after that we
3638 * put the hints back to normal (the actual minimum size) so we may
3639 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003640 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003641 * only way we have of making ourselves bigger (by set lines/columns).
3642 * Thus set hints at start-up to ensure correct init. size, then a
3643 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003644 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003645 * We'll not let the default hints be set while this timer's active.
3646 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003647 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003648check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003649{
3650 if (init_window_hints_state == 1)
3651 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003652 /* Safe to use normal hints now */
3653 init_window_hints_state = 0;
3654 update_window_manager_hints(0, 0);
3655 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003656 }
3657
3658 /* Keep on trying */
3659 init_window_hints_state = 1;
3660 return TRUE;
3661}
3662
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663/*
3664 * Open the GUI window which was created by a call to gui_mch_init().
3665 */
3666 int
3667gui_mch_open(void)
3668{
3669 guicolor_T fg_pixel = INVALCOLOR;
3670 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003671 guint pixel_width;
3672 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 /*
3675 * Allow setting a window role on the command line, or invent one
3676 * if none was specified. This is mainly useful for GNOME session
3677 * support; allowing the WM to restore window placement.
3678 */
3679 if (role_argument != NULL)
3680 {
3681 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3682 }
3683 else
3684 {
3685 char *role;
3686
3687 /* Invent a unique-enough ID string for the role */
3688 role = g_strdup_printf("vim-%u-%u-%u",
3689 (unsigned)mch_get_pid(),
3690 (unsigned)g_random_int(),
3691 (unsigned)time(NULL));
3692
3693 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3694 g_free(role);
3695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696
3697 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
3700 /* Determine user specified geometry, if present. */
3701 if (gui.geom != NULL)
3702 {
3703 int mask;
3704 unsigned int w, h;
3705 int x = 0;
3706 int y = 0;
3707
3708 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3709
3710 if (mask & WidthValue)
3711 Columns = w;
3712 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003713 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003714 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003715 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003717 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003718 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003719
3720 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3721 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3722
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003723 pixel_width += get_menu_tool_width();
3724 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003727 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003728 int ww, hh;
3729 gui_mch_get_screen_dimensions(&ww, &hh);
3730 hh += p_ghr + get_menu_tool_height();
3731 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003732 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003733 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003734 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003735 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 vim_free(gui.geom);
3739 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003740
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003741 /* From now until everyone's stopped trying to set the window hints
3742 * to their correct minimum values, stop them being set as we need
3743 * them to remain at our required size for the parent GtkSocket to
3744 * give us the right initial size.
3745 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003746 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003747 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003748 update_window_manager_hints(pixel_width, pixel_height);
3749 init_window_hints_state = 1;
3750 g_timeout_add(1000, check_startup_plug_hints, NULL);
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003754 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3755 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003756 /* For GTK2 changing the size of the form widget doesn't cause window
3757 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003758 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003759 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003760 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 if (foreground_argument != NULL)
3763 fg_pixel = gui_get_color((char_u *)foreground_argument);
3764 if (fg_pixel == INVALCOLOR)
3765 fg_pixel = gui_get_color((char_u *)"Black");
3766
3767 if (background_argument != NULL)
3768 bg_pixel = gui_get_color((char_u *)background_argument);
3769 if (bg_pixel == INVALCOLOR)
3770 bg_pixel = gui_get_color((char_u *)"White");
3771
3772 if (found_reverse_arg)
3773 {
3774 gui.def_norm_pixel = bg_pixel;
3775 gui.def_back_pixel = fg_pixel;
3776 }
3777 else
3778 {
3779 gui.def_norm_pixel = fg_pixel;
3780 gui.def_back_pixel = bg_pixel;
3781 }
3782
3783 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3784 * in a vimrc file) */
3785 set_normal_colors();
3786
3787 /* Check that none of the colors are the same as the background color */
3788 gui_check_colors();
3789
3790 /* Get the colors for the highlight groups (gui_check_colors() might have
3791 * changed them). */
3792 highlight_gui_started(); /* re-init colors and fonts */
3793
3794 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3795 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3796
3797#ifdef FEAT_HANGULIN
3798 hangul_keyboard_set();
3799#endif
3800
3801 /*
3802 * Notify the fixed area about the need to resize the contents of the
3803 * gui.formwin, which we use for random positioning of the included
3804 * components.
3805 *
3806 * We connect this signal deferred finally after anything is in place,
3807 * since this is intended to handle resizements coming from the window
3808 * manager upon us and should not interfere with what VIM is requesting
3809 * upon startup.
3810 */
3811 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3812 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3813
3814#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003815 /* Set up for receiving DND items. */
3816 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3819 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3820#endif
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003823 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (found_iconic_arg && gtk_socket_id == 0)
3825 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003828#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 unsigned long menu_handler = 0;
3830# ifdef FEAT_TOOLBAR
3831 unsigned long tool_handler = 0;
3832# endif
3833 /*
3834 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3835 * show when restoring the saved layout configuration. We can't just
3836 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3837 * a toplevel window and thus will be realized immediately. Instead,
3838 * connect signal handlers to hide the widgets just after they've been
3839 * marked visible, but before the main window is realized.
3840 */
3841 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3842 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3843 G_CALLBACK(&gtk_widget_hide),
3844 NULL);
3845# ifdef FEAT_TOOLBAR
3846 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3847 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3848 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3849 G_CALLBACK(&gtk_widget_hide),
3850 NULL);
3851# endif
3852#endif
3853 gtk_widget_show(gui.mainwin);
3854
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003855#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (menu_handler != 0)
3857 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3858# ifdef FEAT_TOOLBAR
3859 if (tool_handler != 0)
3860 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3861# endif
3862#endif
3863 }
3864
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 return OK;
3866}
3867
3868
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003870gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871{
3872 if (gui.mainwin != NULL)
3873 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874}
3875
3876/*
3877 * Get the position of the top left corner of the window.
3878 */
3879 int
3880gui_mch_get_winpos(int *x, int *y)
3881{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 return OK;
3884}
3885
3886/*
3887 * Set the position of the top left corner of the window to the given
3888 * coordinates.
3889 */
3890 void
3891gui_mch_set_winpos(int x, int y)
3892{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894}
3895
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896#if 0
3897static int resize_idle_installed = FALSE;
3898/*
3899 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3900 * the shell size doesn't exceed the window size, i.e. if the window manager
3901 * ignored our size request. Usually this happens if the window is maximized.
3902 *
3903 * FIXME: It'd be nice if we could find a little more orthodox solution.
3904 * See also the remark below in gui_mch_set_shellsize().
3905 *
3906 * DISABLED: When doing ":set lines+=1" this function would first invoke
3907 * gui_resize_shell() with the old size, then the normal callback would
3908 * report the new size through form_configure_event(). That caused the window
3909 * layout to be messed up.
3910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 static gboolean
3912force_shell_resize_idle(gpointer data)
3913{
3914 if (gui.mainwin != NULL
3915 && GTK_WIDGET_REALIZED(gui.mainwin)
3916 && GTK_WIDGET_VISIBLE(gui.mainwin))
3917 {
3918 int width;
3919 int height;
3920
3921 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3922
3923 width -= get_menu_tool_width();
3924 height -= get_menu_tool_height();
3925
3926 gui_resize_shell(width, height);
3927 }
3928
3929 resize_idle_installed = FALSE;
3930 return FALSE; /* don't call me again */
3931}
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
Bram Moolenaar09736232009-09-23 16:14:49 +00003934/*
3935 * Return TRUE if the main window is maximized.
3936 */
3937 int
3938gui_mch_maximized()
3939{
3940 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3941 && (gdk_window_get_state(gui.mainwin->window)
3942 & GDK_WINDOW_STATE_MAXIMIZED));
3943}
3944
3945/*
3946 * Unmaximize the main window
3947 */
3948 void
3949gui_mch_unmaximize()
3950{
3951 if (gui.mainwin != NULL)
3952 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3953}
Bram Moolenaar09736232009-09-23 16:14:49 +00003954
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02003956 * Called when the font changed while the window is maximized. Compute the
3957 * new Rows and Columns. This is like resizing the window.
3958 */
3959 void
3960gui_mch_newfont()
3961{
3962 int w, h;
3963
3964 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
3965 w -= get_menu_tool_width();
3966 h -= get_menu_tool_height();
3967 gui_resize_shell(w, h);
3968}
3969
3970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * Set the windows size.
3972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 void
3974gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003975 int min_width UNUSED, int min_height UNUSED,
3976 int base_width UNUSED, int base_height UNUSED,
3977 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 /* give GTK+ a chance to put all widget's into place */
3980 gui_mch_update();
3981
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003982 /* this will cause the proper resizement to happen too */
3983 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003984 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003985
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003987 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 * main window instead. */
3989 width += get_menu_tool_width();
3990 height += get_menu_tool_height();
3991
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003992 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003993 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003994 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003995 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003997# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 if (!resize_idle_installed)
3999 {
4000 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4001 &force_shell_resize_idle, NULL, NULL);
4002 resize_idle_installed = TRUE;
4003 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02004004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 /*
4006 * Wait until all events are processed to prevent a crash because the
4007 * real size of the drawing area doesn't reflect Vim's internal ideas.
4008 *
4009 * This is a bit of a hack, since Vim is a terminal application with a GUI
4010 * on top, while the GUI expects to be the boss.
4011 */
4012 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014
4015
4016/*
4017 * The screen size is used to make sure the initial window doesn't get bigger
4018 * than the screen. This subtracts some room for menubar, toolbar and window
4019 * decorations.
4020 */
4021 void
4022gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4023{
4024#ifdef HAVE_GTK_MULTIHEAD
4025 GdkScreen* screen;
4026
4027 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4028 screen = gtk_widget_get_screen(gui.mainwin);
4029 else
4030 screen = gdk_screen_get_default();
4031
4032 *screen_w = gdk_screen_get_width(screen);
4033 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4034#else
4035 *screen_w = gdk_screen_width();
4036 /* Subtract 'guiheadroom' from the height to allow some room for the
4037 * window manager (task list and window title bar). */
4038 *screen_h = gdk_screen_height() - p_ghr;
4039#endif
4040
4041 /*
4042 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4043 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004044 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * This should be completely changed later.
4046 */
4047 *screen_w -= get_menu_tool_width();
4048 *screen_h -= get_menu_tool_height();
4049}
4050
4051#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004053gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 if (title != NULL && output_conv.vc_type != CONV_NONE)
4056 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057
4058 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4059
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 if (output_conv.vc_type != CONV_NONE)
4061 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062}
4063#endif /* FEAT_TITLE */
4064
4065#if defined(FEAT_MENU) || defined(PROTO)
4066 void
4067gui_mch_enable_menu(int showit)
4068{
4069 GtkWidget *widget;
4070
4071# ifdef FEAT_GUI_GNOME
4072 if (using_gnome)
4073 widget = gui.menubar_h;
4074 else
4075# endif
4076 widget = gui.menubar;
4077
Bram Moolenaar18144c82006-04-12 21:52:12 +00004078 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4079 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 if (showit)
4082 gtk_widget_show(widget);
4083 else
4084 gtk_widget_hide(widget);
4085
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004086 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088}
4089#endif /* FEAT_MENU */
4090
4091#if defined(FEAT_TOOLBAR) || defined(PROTO)
4092 void
4093gui_mch_show_toolbar(int showit)
4094{
4095 GtkWidget *widget;
4096
4097 if (gui.toolbar == NULL)
4098 return;
4099
4100# ifdef FEAT_GUI_GNOME
4101 if (using_gnome)
4102 widget = gui.toolbar_h;
4103 else
4104# endif
4105 widget = gui.toolbar;
4106
4107 if (showit)
4108 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4109
4110 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4111 {
4112 if (showit)
4113 gtk_widget_show(widget);
4114 else
4115 gtk_widget_hide(widget);
4116
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004117 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
4119}
4120#endif /* FEAT_TOOLBAR */
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122/*
4123 * Check if a given font is a CJK font. This is done in a very crude manner. It
4124 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4125 * font. Consequently, this function cannot be used as a general purpose check
4126 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4127 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4128 */
4129 static int
4130is_cjk_font(PangoFontDescription *font_desc)
4131{
4132 static const char * const cjk_langs[] =
4133 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4134
4135 PangoFont *font;
4136 unsigned i;
4137 int is_cjk = FALSE;
4138
4139 font = pango_context_load_font(gui.text_context, font_desc);
4140
4141 if (font == NULL)
4142 return FALSE;
4143
4144 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4145 {
4146 PangoCoverage *coverage;
4147 gunichar uc;
4148
4149 coverage = pango_font_get_coverage(
4150 font, pango_language_from_string(cjk_langs[i]));
4151
4152 if (coverage != NULL)
4153 {
4154 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4155 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4156 pango_coverage_unref(coverage);
4157 }
4158 }
4159
4160 g_object_unref(font);
4161
4162 return is_cjk;
4163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164
Bram Moolenaar231334e2005-07-25 20:46:57 +00004165/*
4166 * Adjust gui.char_height (after 'linespace' was changed).
4167 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004169gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 PangoFontMetrics *metrics;
4172 int ascent;
4173 int descent;
4174
4175 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4176 pango_context_get_language(gui.text_context));
4177 ascent = pango_font_metrics_get_ascent(metrics);
4178 descent = pango_font_metrics_get_descent(metrics);
4179
4180 pango_font_metrics_unref(metrics);
4181
4182 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004183 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004184 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4186
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /* A not-positive value of char_height may crash Vim. Only happens
4188 * if 'linespace' is negative (which does make sense sometimes). */
4189 gui.char_ascent = MAX(gui.char_ascent, 0);
4190 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4191
4192 return OK;
4193}
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195/*
4196 * Put up a font dialog and return the selected font name in allocated memory.
4197 * "oldval" is the previous value. Return NULL when cancelled.
4198 * This should probably go into gui_gtk.c. Hmm.
4199 * FIXME:
4200 * The GTK2 font selection dialog has no filtering API. So we could either
4201 * a) implement our own (possibly copying the code from somewhere else) or
4202 * b) just live with it.
4203 */
4204 char_u *
4205gui_mch_font_dialog(char_u *oldval)
4206{
4207 GtkWidget *dialog;
4208 int response;
4209 char_u *fontname = NULL;
4210 char_u *oldname;
4211
4212 dialog = gtk_font_selection_dialog_new(NULL);
4213
4214 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4215 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4216
4217 if (oldval != NULL && oldval[0] != NUL)
4218 {
4219 if (output_conv.vc_type != CONV_NONE)
4220 oldname = string_convert(&output_conv, oldval, NULL);
4221 else
4222 oldname = oldval;
4223
4224 /* Annoying bug in GTK (or Pango): if the font name does not include a
4225 * size, zero is used. Use default point size ten. */
4226 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4227 {
4228 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4229
4230 if (p != NULL)
4231 {
4232 STRCPY(p + STRLEN(p), " 10");
4233 if (oldname != oldval)
4234 vim_free(oldname);
4235 oldname = p;
4236 }
4237 }
4238
4239 gtk_font_selection_dialog_set_font_name(
4240 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4241
4242 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004243 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004245 else
4246 gtk_font_selection_dialog_set_font_name(
4247 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 response = gtk_dialog_run(GTK_DIALOG(dialog));
4250
4251 if (response == GTK_RESPONSE_OK)
4252 {
4253 char *name;
4254
4255 name = gtk_font_selection_dialog_get_font_name(
4256 GTK_FONT_SELECTION_DIALOG(dialog));
4257 if (name != NULL)
4258 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259 char_u *p;
4260
4261 /* Apparently some font names include a comma, need to escape
4262 * that, because in 'guifont' it separates names. */
4263 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004265 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004266 {
4267 fontname = string_convert(&input_conv, p, NULL);
4268 vim_free(p);
4269 }
4270 else
4271 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
4273 }
4274
4275 if (response != GTK_RESPONSE_NONE)
4276 gtk_widget_destroy(dialog);
4277
4278 return fontname;
4279}
4280
4281/*
4282 * Some monospace fonts don't support a bold weight, and fall back
4283 * silently to the regular weight. But this is no good since our text
4284 * drawing function can emulate bold by overstriking. So let's try
4285 * to detect whether bold weight is actually available and emulate it
4286 * otherwise.
4287 *
4288 * Note that we don't need to check for italic style since Xft can
4289 * emulate italic on its own, provided you have a proper fontconfig
4290 * setup. We wouldn't be able to emulate it in Vim anyway.
4291 */
4292 static void
4293get_styled_font_variants(void)
4294{
4295 PangoFontDescription *bold_font_desc;
4296 PangoFont *plain_font;
4297 PangoFont *bold_font;
4298
4299 gui.font_can_bold = FALSE;
4300
4301 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4302
4303 if (plain_font == NULL)
4304 return;
4305
4306 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4307 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4308
4309 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4310 /*
4311 * The comparison relies on the unique handle nature of a PangoFont*,
4312 * i.e. it's assumed that a different PangoFont* won't refer to the
4313 * same font. Seems to work, and failing here isn't critical anyway.
4314 */
4315 if (bold_font != NULL)
4316 {
4317 gui.font_can_bold = (bold_font != plain_font);
4318 g_object_unref(bold_font);
4319 }
4320
4321 pango_font_description_free(bold_font_desc);
4322 g_object_unref(plain_font);
4323}
4324
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325static PangoEngineShape *default_shape_engine = NULL;
4326
4327/*
4328 * Create a map from ASCII characters in the range [32,126] to glyphs
4329 * of the current font. This is used by gui_gtk2_draw_string() to skip
4330 * the itemize and shaping process for the most common case.
4331 */
4332 static void
4333ascii_glyph_table_init(void)
4334{
4335 char_u ascii_chars[128];
4336 PangoAttrList *attr_list;
4337 GList *item_list;
4338 int i;
4339
4340 if (gui.ascii_glyphs != NULL)
4341 pango_glyph_string_free(gui.ascii_glyphs);
4342 if (gui.ascii_font != NULL)
4343 g_object_unref(gui.ascii_font);
4344
4345 gui.ascii_glyphs = NULL;
4346 gui.ascii_font = NULL;
4347
4348 /* For safety, fill in question marks for the control characters. */
4349 for (i = 0; i < 32; ++i)
4350 ascii_chars[i] = '?';
4351 for (; i < 127; ++i)
4352 ascii_chars[i] = i;
4353 ascii_chars[i] = '?';
4354
4355 attr_list = pango_attr_list_new();
4356 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4357 0, sizeof(ascii_chars), attr_list, NULL);
4358
4359 if (item_list != NULL && item_list->next == NULL) /* play safe */
4360 {
4361 PangoItem *item;
4362 int width;
4363
4364 item = (PangoItem *)item_list->data;
4365 width = gui.char_width * PANGO_SCALE;
4366
4367 /* Remember the shape engine used for ASCII. */
4368 default_shape_engine = item->analysis.shape_engine;
4369
4370 gui.ascii_font = item->analysis.font;
4371 g_object_ref(gui.ascii_font);
4372
4373 gui.ascii_glyphs = pango_glyph_string_new();
4374
4375 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4376 &item->analysis, gui.ascii_glyphs);
4377
4378 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4379
4380 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4381 {
4382 PangoGlyphGeometry *geom;
4383
4384 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4385 geom->x_offset += MAX(0, width - geom->width) / 2;
4386 geom->width = width;
4387 }
4388 }
4389
4390 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4391 g_list_free(item_list);
4392 pango_attr_list_unref(attr_list);
4393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395/*
4396 * Initialize Vim to use the font or fontset with the given name.
4397 * Return FAIL if the font could not be loaded, OK otherwise.
4398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004400gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 PangoFontDescription *font_desc;
4403 PangoLayout *layout;
4404 int width;
4405
4406 /* If font_name is NULL, this means to use the default, which should
4407 * be present on all proper Pango/fontconfig installations. */
4408 if (font_name == NULL)
4409 font_name = (char_u *)DEFAULT_FONT;
4410
4411 font_desc = gui_mch_get_font(font_name, FALSE);
4412
4413 if (font_desc == NULL)
4414 return FAIL;
4415
4416 gui_mch_free_font(gui.norm_font);
4417 gui.norm_font = font_desc;
4418
4419 pango_context_set_font_description(gui.text_context, font_desc);
4420
4421 layout = pango_layout_new(gui.text_context);
4422 pango_layout_set_text(layout, "MW", 2);
4423 pango_layout_get_size(layout, &width, NULL);
4424 /*
4425 * Set char_width to half the width obtained from pango_layout_get_size()
4426 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4427 * Pango to use the same width for both non-CJK characters (e.g. Latin
4428 * letters and numbers) and CJK characters. This results in 's p a c e d
4429 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4430 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4431 * width for CJK fonts.
4432 *
4433 * For related bugs, see:
4434 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4435 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4436 *
4437 * With this, for all four of the following cases, Vim works fine:
4438 * guifont=CJK_fixed_width_font
4439 * guifont=Non_CJK_fixed_font
4440 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4441 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4442 */
4443 if (is_cjk_font(gui.norm_font))
4444 {
4445 int cjk_width;
4446
4447 /* Measure the text extent of U+4E00 and U+4E8C */
4448 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4449 pango_layout_get_size(layout, &cjk_width, NULL);
4450
4451 if (width == cjk_width) /* Xft not patched */
4452 width /= 2;
4453 }
4454 g_object_unref(layout);
4455
4456 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4457
4458 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4459 if (gui.char_width <= 0)
4460 gui.char_width = 8;
4461
Bram Moolenaar231334e2005-07-25 20:46:57 +00004462 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463
4464 /* Set the fontname, which will be used for information purposes */
4465 hl_set_font_name(font_name);
4466
4467 get_styled_font_variants();
4468 ascii_glyph_table_init();
4469
4470 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4471 if (gui.wide_font != NULL
4472 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4473 {
4474 pango_font_description_free(gui.wide_font);
4475 gui.wide_font = NULL;
4476 }
4477
Bram Moolenaare161c792009-11-03 17:13:59 +00004478 if (gui_mch_maximized())
4479 {
Bram Moolenaare161c792009-11-03 17:13:59 +00004480 /* Update lines and columns in accordance with the new font, keep the
4481 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004482 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00004483 }
4484 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004485 {
4486 /* Preserve the logical dimensions of the screen. */
4487 update_window_manager_hints(0, 0);
4488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489
4490 return OK;
4491}
4492
4493/*
4494 * Get a reference to the font "name".
4495 * Return zero for failure.
4496 */
4497 GuiFont
4498gui_mch_get_font(char_u *name, int report_error)
4499{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 /* can't do this when GUI is not running */
4503 if (!gui.in_use || name == NULL)
4504 return NULL;
4505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 if (output_conv.vc_type != CONV_NONE)
4507 {
4508 char_u *buf;
4509
4510 buf = string_convert(&output_conv, name, NULL);
4511 if (buf != NULL)
4512 {
4513 font = pango_font_description_from_string((const char *)buf);
4514 vim_free(buf);
4515 }
4516 else
4517 font = NULL;
4518 }
4519 else
4520 font = pango_font_description_from_string((const char *)name);
4521
4522 if (font != NULL)
4523 {
4524 PangoFont *real_font;
4525
4526 /* pango_context_load_font() bails out if no font size is set */
4527 if (pango_font_description_get_size(font) <= 0)
4528 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4529
4530 real_font = pango_context_load_font(gui.text_context, font);
4531
4532 if (real_font == NULL)
4533 {
4534 pango_font_description_free(font);
4535 font = NULL;
4536 }
4537 else
4538 g_object_unref(real_font);
4539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540
4541 if (font == NULL)
4542 {
4543 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004544 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 return NULL;
4546 }
4547
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return font;
4549}
4550
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004551#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004552/*
4553 * Return the name of font "font" in allocated memory.
4554 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004555 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004556gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004557{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004558 if (font != NOFONT)
4559 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004560 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004561
Bram Moolenaar89d40322006-08-29 15:30:07 +00004562 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004563 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004564 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004565
Bram Moolenaar89d40322006-08-29 15:30:07 +00004566 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004567 return s;
4568 }
4569 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004570 return NULL;
4571}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004572#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574/*
4575 * If a font is not going to be used, free its structure.
4576 */
4577 void
4578gui_mch_free_font(GuiFont font)
4579{
4580 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582}
4583
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584/*
4585 * Return the Pixel value (color) for the given color name. This routine was
4586 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4587 * Programmer's Guide.
4588 * Return INVALCOLOR for error.
4589 */
4590 guicolor_T
4591gui_mch_get_color(char_u *name)
4592{
4593 /* A number of colors that some X11 systems don't have */
4594 static const char *const vimnames[][2] =
4595 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004596 {"LightRed", "#FFBBBB"},
4597 {"LightGreen", "#88FF88"},
4598 {"LightMagenta","#FFBBFF"},
4599 {"DarkCyan", "#008888"},
4600 {"DarkBlue", "#0000BB"},
4601 {"DarkRed", "#BB0000"},
4602 {"DarkMagenta", "#BB00BB"},
4603 {"DarkGrey", "#BBBBBB"},
4604 {"DarkYellow", "#BBBB00"},
4605 {"Gray10", "#1A1A1A"},
4606 {"Grey10", "#1A1A1A"},
4607 {"Gray20", "#333333"},
4608 {"Grey20", "#333333"},
4609 {"Gray30", "#4D4D4D"},
4610 {"Grey30", "#4D4D4D"},
4611 {"Gray40", "#666666"},
4612 {"Grey40", "#666666"},
4613 {"Gray50", "#7F7F7F"},
4614 {"Grey50", "#7F7F7F"},
4615 {"Gray60", "#999999"},
4616 {"Grey60", "#999999"},
4617 {"Gray70", "#B3B3B3"},
4618 {"Grey70", "#B3B3B3"},
4619 {"Gray80", "#CCCCCC"},
4620 {"Grey80", "#CCCCCC"},
4621 {"Gray90", "#E5E5E5"},
4622 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 {NULL, NULL}
4624 };
4625
4626 if (!gui.in_use) /* can't do this when GUI not running */
4627 return INVALCOLOR;
4628
4629 while (name != NULL)
4630 {
4631 GdkColor color;
4632 int parsed;
4633 int i;
4634
4635 parsed = gdk_color_parse((const char *)name, &color);
4636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (parsed)
4638 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4640 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 return (guicolor_T)color.pixel;
4642 }
4643 /* add a few builtin names and try again */
4644 for (i = 0; ; ++i)
4645 {
4646 if (vimnames[i][0] == NULL)
4647 {
4648 name = NULL;
4649 break;
4650 }
4651 if (STRICMP(name, vimnames[i][0]) == 0)
4652 {
4653 name = (char_u *)vimnames[i][1];
4654 break;
4655 }
4656 }
4657 }
4658
4659 return INVALCOLOR;
4660}
4661
4662/*
4663 * Set the current text foreground color.
4664 */
4665 void
4666gui_mch_set_fg_color(guicolor_T color)
4667{
4668 gui.fgcolor->pixel = (unsigned long)color;
4669}
4670
4671/*
4672 * Set the current text background color.
4673 */
4674 void
4675gui_mch_set_bg_color(guicolor_T color)
4676{
4677 gui.bgcolor->pixel = (unsigned long)color;
4678}
4679
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004680/*
4681 * Set the current text special color.
4682 */
4683 void
4684gui_mch_set_sp_color(guicolor_T color)
4685{
4686 gui.spcolor->pixel = (unsigned long)color;
4687}
4688
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689/*
4690 * Function-like convenience macro for the sake of efficiency.
4691 */
4692#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4693 G_STMT_START{ \
4694 PangoAttribute *tmp_attr_; \
4695 tmp_attr_ = (Attribute); \
4696 tmp_attr_->start_index = (Start); \
4697 tmp_attr_->end_index = (End); \
4698 pango_attr_list_insert((AttrList), tmp_attr_); \
4699 }G_STMT_END
4700
4701 static void
4702apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4703{
4704 char_u *start = NULL;
4705 char_u *p;
4706 int uc;
4707
4708 for (p = s; p < s + len; p += utf_byte2len(*p))
4709 {
4710 uc = utf_ptr2char(p);
4711
4712 if (start == NULL)
4713 {
4714 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4715 start = p;
4716 }
4717 else if (uc < 0x80 /* optimization shortcut */
4718 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4719 {
4720 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4721 attr_list, start - s, p - s);
4722 start = NULL;
4723 }
4724 }
4725
4726 if (start != NULL)
4727 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4728 attr_list, start - s, len);
4729}
4730
4731 static int
4732count_cluster_cells(char_u *s, PangoItem *item,
4733 PangoGlyphString* glyphs, int i,
4734 int *cluster_width,
4735 int *last_glyph_rbearing)
4736{
4737 char_u *p;
4738 int next; /* glyph start index of next cluster */
4739 int start, end; /* string segment of current cluster */
4740 int width; /* real cluster width in Pango units */
4741 int uc;
4742 int cellcount = 0;
4743
4744 width = glyphs->glyphs[i].geometry.width;
4745
4746 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4747 {
4748 if (glyphs->glyphs[next].attr.is_cluster_start)
4749 break;
4750 else if (glyphs->glyphs[next].geometry.width > width)
4751 width = glyphs->glyphs[next].geometry.width;
4752 }
4753
4754 start = item->offset + glyphs->log_clusters[i];
4755 end = item->offset + ((next < glyphs->num_glyphs) ?
4756 glyphs->log_clusters[next] : item->length);
4757
4758 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4759 {
4760 uc = utf_ptr2char(p);
4761 if (uc < 0x80)
4762 ++cellcount;
4763 else if (!utf_iscomposing(uc))
4764 cellcount += utf_char2cells(uc);
4765 }
4766
4767 if (last_glyph_rbearing != NULL
4768 && cellcount > 0 && next == glyphs->num_glyphs)
4769 {
4770 PangoRectangle ink_rect;
4771 /*
4772 * If a certain combining mark had to be taken from a non-monospace
4773 * font, we have to compensate manually by adapting x_offset according
4774 * to the ink extents of the previous glyph.
4775 */
4776 pango_font_get_glyph_extents(item->analysis.font,
4777 glyphs->glyphs[i].glyph,
4778 &ink_rect, NULL);
4779
4780 if (PANGO_RBEARING(ink_rect) > 0)
4781 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4782 }
4783
4784 if (cellcount > 0)
4785 *cluster_width = width;
4786
4787 return cellcount;
4788}
4789
4790/*
4791 * If there are only combining characters in the cluster, we cannot just
4792 * change the width of the previous glyph since there is none. Therefore
4793 * some guesswork is needed.
4794 *
4795 * If ink_rect.x is negative Pango apparently has taken care of the composing
4796 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4797 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004798 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 *
4800 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4801 * the position of the previous glyph. Apparently this happens only with old
4802 * X fonts which don't provide the special combining information needed by
4803 * Pango.
4804 */
4805 static void
4806setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4807 int last_cellcount, int last_cluster_width,
4808 int last_glyph_rbearing)
4809{
4810 PangoRectangle ink_rect;
4811 PangoRectangle logical_rect;
4812 int width;
4813
4814 width = last_cellcount * gui.char_width * PANGO_SCALE;
4815 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4816 glyph->geometry.width = 0;
4817
4818 pango_font_get_glyph_extents(item->analysis.font,
4819 glyph->glyph,
4820 &ink_rect, &logical_rect);
4821 if (ink_rect.x < 0)
4822 {
4823 glyph->geometry.x_offset += last_glyph_rbearing;
4824 glyph->geometry.y_offset = logical_rect.height
4825 - (gui.char_height - p_linespace) * PANGO_SCALE;
4826 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004827 else
4828 /* If the accent width is smaller than the cluster width, position it
4829 * in the middle. */
4830 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831}
4832
4833 static void
4834draw_glyph_string(int row, int col, int num_cells, int flags,
4835 PangoFont *font, PangoGlyphString *glyphs)
4836{
4837 if (!(flags & DRAW_TRANSP))
4838 {
4839 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4840
4841 gdk_draw_rectangle(gui.drawarea->window,
4842 gui.text_gc,
4843 TRUE,
4844 FILL_X(col),
4845 FILL_Y(row),
4846 num_cells * gui.char_width,
4847 gui.char_height);
4848 }
4849
4850 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4851
4852 gdk_draw_glyphs(gui.drawarea->window,
4853 gui.text_gc,
4854 font,
4855 TEXT_X(col),
4856 TEXT_Y(row),
4857 glyphs);
4858
4859 /* redraw the contents with an offset of 1 to emulate bold */
4860 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4861 gdk_draw_glyphs(gui.drawarea->window,
4862 gui.text_gc,
4863 font,
4864 TEXT_X(col) + 1,
4865 TEXT_Y(row),
4866 glyphs);
4867}
4868
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004869/*
4870 * Draw underline and undercurl at the bottom of the character cell.
4871 */
4872 static void
4873draw_under(int flags, int row, int col, int cells)
4874{
4875 int i;
4876 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004877 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004878 int y = FILL_Y(row + 1) - 1;
4879
4880 /* Undercurl: draw curl at the bottom of the character cell. */
4881 if (flags & DRAW_UNDERC)
4882 {
4883 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4884 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4885 {
4886 offset = val[i % 8];
4887 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4888 }
4889 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4890 }
4891
4892 /* Underline: draw a line at the bottom of the character cell. */
4893 if (flags & DRAW_UNDERL)
4894 {
4895 /* When p_linespace is 0, overwrite the bottom row of pixels.
4896 * Otherwise put the line just below the character. */
4897 if (p_linespace > 1)
4898 y -= p_linespace - 1;
4899 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4900 FILL_X(col), y,
4901 FILL_X(col + cells) - 1, y);
4902 }
4903}
4904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 int
4906gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4907{
4908 GdkRectangle area; /* area for clip mask */
4909 PangoGlyphString *glyphs; /* glyphs of current item */
4910 int column_offset = 0; /* column offset in cells */
4911 int i;
4912 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4913 char_u *new_conv_buf;
4914 int convlen;
4915 char_u *sp, *bp;
4916 int plen;
4917
4918 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4919 return len;
4920
4921 if (output_conv.vc_type != CONV_NONE)
4922 {
4923 /*
4924 * Convert characters from 'encoding' to 'termencoding', which is set
4925 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4926 * prohibits changing this to something else than UTF-8 if the GUI is
4927 * in use.
4928 */
4929 convlen = len;
4930 conv_buf = string_convert(&output_conv, s, &convlen);
4931 g_return_val_if_fail(conv_buf != NULL, len);
4932
4933 /* Correct for differences in char width: some chars are
4934 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4935 * compensate for that. */
4936 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4937 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4940 {
4941 new_conv_buf = alloc(convlen + 2);
4942 if (new_conv_buf == NULL)
4943 return len;
4944 plen += bp - conv_buf;
4945 mch_memmove(new_conv_buf, conv_buf, plen);
4946 new_conv_buf[plen] = ' ';
4947 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4948 convlen - plen + 1);
4949 vim_free(conv_buf);
4950 conv_buf = new_conv_buf;
4951 ++convlen;
4952 bp = conv_buf + plen;
4953 plen = 1;
4954 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004955 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 bp += plen;
4957 }
4958 s = conv_buf;
4959 len = convlen;
4960 }
4961
4962 /*
4963 * Restrict all drawing to the current screen line in order to prevent
4964 * fuzzy font lookups from messing up the screen.
4965 */
4966 area.x = gui.border_offset;
4967 area.y = FILL_Y(row);
4968 area.width = gui.num_cols * gui.char_width;
4969 area.height = gui.char_height;
4970
4971 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4972 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4973
4974 glyphs = pango_glyph_string_new();
4975
4976 /*
4977 * Optimization hack: If possible, skip the itemize and shaping process
4978 * for pure ASCII strings. This optimization is particularly effective
4979 * because Vim draws space characters to clear parts of the screen.
4980 */
4981 if (!(flags & DRAW_ITALIC)
4982 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4983 && gui.ascii_glyphs != NULL)
4984 {
4985 char_u *p;
4986
4987 for (p = s; p < s + len; ++p)
4988 if (*p & 0x80)
4989 goto not_ascii;
4990
4991 pango_glyph_string_set_size(glyphs, len);
4992
4993 for (i = 0; i < len; ++i)
4994 {
4995 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4996 glyphs->log_clusters[i] = i;
4997 }
4998
4999 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5000
5001 column_offset = len;
5002 }
5003 else
5004not_ascii:
5005 {
5006 PangoAttrList *attr_list;
5007 GList *item_list;
5008 int cluster_width;
5009 int last_glyph_rbearing;
5010 int cells = 0; /* cells occupied by current cluster */
5011
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005012 /* Safety check: pango crashes when invoked with invalid utf-8
5013 * characters. */
5014 if (!utf_valid_string(s, s + len))
5015 {
5016 column_offset = len;
5017 goto skipitall;
5018 }
5019
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 /* original width of the current cluster */
5021 cluster_width = PANGO_SCALE * gui.char_width;
5022
5023 /* right bearing of the last non-composing glyph */
5024 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5025
5026 attr_list = pango_attr_list_new();
5027
5028 /* If 'guifontwide' is set then use that for double-width characters.
5029 * Otherwise just go with 'guifont' and let Pango do its thing. */
5030 if (gui.wide_font != NULL)
5031 apply_wide_font_attr(s, len, attr_list);
5032
5033 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5034 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5035 attr_list, 0, len);
5036 if (flags & DRAW_ITALIC)
5037 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5038 attr_list, 0, len);
5039 /*
5040 * Break the text into segments with consistent directional level
5041 * and shaping engine. Pure Latin text needs only a single segment,
5042 * so there's no need to worry about the loop's efficiency. Better
5043 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5044 */
5045 item_list = pango_itemize(gui.text_context,
5046 (const char *)s, 0, len, attr_list, NULL);
5047
5048 while (item_list != NULL)
5049 {
5050 PangoItem *item;
5051 int item_cells = 0; /* item length in cells */
5052
5053 item = (PangoItem *)item_list->data;
5054 item_list = g_list_delete_link(item_list, item_list);
5055 /*
5056 * Increment the bidirectional embedding level by 1 if it is not
5057 * even. An odd number means the output will be RTL, but we don't
5058 * want that since Vim handles right-to-left text on its own. It
5059 * would probably be sufficient to just set level = 0, but you can
5060 * never know :)
5061 *
5062 * Unfortunately we can't take advantage of Pango's ability to
5063 * render both LTR and RTL at the same time. In order to support
5064 * that, Vim's main screen engine would have to make use of Pango
5065 * functionality.
5066 */
5067 item->analysis.level = (item->analysis.level + 1) & (~1U);
5068
5069 /* HACK: Overrule the shape engine, we don't want shaping to be
5070 * done, because drawing the cursor would change the display. */
5071 item->analysis.shape_engine = default_shape_engine;
5072
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02005073#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02005074 pango_shape_full((const char *)s + item->offset, item->length,
5075 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02005076#else
5077 pango_shape((const char *)s + item->offset, item->length,
5078 &item->analysis, glyphs);
5079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 /*
5081 * Fixed-width hack: iterate over the array and assign a fixed
5082 * width to each glyph, thus overriding the choice made by the
5083 * shaping engine. We use utf_char2cells() to determine the
5084 * number of cells needed.
5085 *
5086 * Also perform all kind of dark magic to get composing
5087 * characters right (and pretty too of course).
5088 */
5089 for (i = 0; i < glyphs->num_glyphs; ++i)
5090 {
5091 PangoGlyphInfo *glyph;
5092
5093 glyph = &glyphs->glyphs[i];
5094
5095 if (glyph->attr.is_cluster_start)
5096 {
5097 int cellcount;
5098
5099 cellcount = count_cluster_cells(
5100 s, item, glyphs, i, &cluster_width,
5101 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5102
5103 if (cellcount > 0)
5104 {
5105 int width;
5106
5107 width = cellcount * gui.char_width * PANGO_SCALE;
5108 glyph->geometry.x_offset +=
5109 MAX(0, width - cluster_width) / 2;
5110 glyph->geometry.width = width;
5111 }
5112 else
5113 {
5114 /* If there are only combining characters in the
5115 * cluster, we cannot just change the width of the
5116 * previous glyph since there is none. Therefore
5117 * some guesswork is needed. */
5118 setup_zero_width_cluster(item, glyph, cells,
5119 cluster_width,
5120 last_glyph_rbearing);
5121 }
5122
5123 item_cells += cellcount;
5124 cells = cellcount;
5125 }
5126 else if (i > 0)
5127 {
5128 int width;
5129
5130 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005131 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02005132 * In some circumstances Pango uses a positive x_offset,
5133 * then use the width of the previous glyph for this one
5134 * and set the previous width to zero.
5135 * Otherwise we get a negative x_offset, Pango has already
5136 * positioned the combining char, keep the widths as they
5137 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005138 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02005139 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005140 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02005141 {
5142 glyphs->glyphs[i].geometry.width =
5143 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005144 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02005145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 width = cells * gui.char_width * PANGO_SCALE;
5147 glyph->geometry.x_offset +=
5148 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 }
5150 else /* i == 0 "cannot happen" */
5151 {
5152 glyph->geometry.width = 0;
5153 }
5154 }
5155
5156 /*** Aaaaand action! ***/
5157 draw_glyph_string(row, col + column_offset, item_cells,
5158 flags, item->analysis.font, glyphs);
5159
5160 pango_item_free(item);
5161
5162 column_offset += item_cells;
5163 }
5164
5165 pango_attr_list_unref(attr_list);
5166 }
5167
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005168skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005169 /* Draw underline and undercurl. */
5170 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
5172 pango_glyph_string_free(glyphs);
5173 vim_free(conv_buf);
5174
5175 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5176
5177 return column_offset;
5178}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180/*
5181 * Return OK if the key with the termcap name "name" is supported.
5182 */
5183 int
5184gui_mch_haskey(char_u *name)
5185{
5186 int i;
5187
5188 for (i = 0; special_keys[i].key_sym != 0; i++)
5189 if (name[0] == special_keys[i].code0
5190 && name[1] == special_keys[i].code1)
5191 return OK;
5192 return FAIL;
5193}
5194
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005195#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196/*
5197 * Return the text window-id and display. Only required for X-based GUI's
5198 */
5199 int
5200gui_get_x11_windis(Window *win, Display **dis)
5201{
5202 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5203 {
5204 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5205 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5206 return OK;
5207 }
5208
5209 *dis = NULL;
5210 *win = 0;
5211 return FAIL;
5212}
5213#endif
5214
5215#if defined(FEAT_CLIENTSERVER) \
5216 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5217
5218 Display *
5219gui_mch_get_display(void)
5220{
5221 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5222 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5223 else
5224 return NULL;
5225}
5226#endif
5227
5228 void
5229gui_mch_beep(void)
5230{
5231#ifdef HAVE_GTK_MULTIHEAD
5232 GdkDisplay *display;
5233
5234 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5235 display = gtk_widget_get_display(gui.mainwin);
5236 else
5237 display = gdk_display_get_default();
5238
5239 if (display != NULL)
5240 gdk_display_beep(display);
5241#else
5242 gdk_beep();
5243#endif
5244}
5245
5246 void
5247gui_mch_flash(int msec)
5248{
5249 GdkGCValues values;
5250 GdkGC *invert_gc;
5251
5252 if (gui.drawarea->window == NULL)
5253 return;
5254
5255 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5256 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5257 values.function = GDK_XOR;
5258 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5259 &values,
5260 GDK_GC_FOREGROUND |
5261 GDK_GC_BACKGROUND |
5262 GDK_GC_FUNCTION);
5263 gdk_gc_set_exposures(invert_gc,
5264 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5265 /*
5266 * Do a visual beep by changing back and forth in some undetermined way,
5267 * the foreground and background colors. This is due to the fact that
5268 * there can't be really any prediction about the effects of XOR on
5269 * arbitrary X11 servers. However this seems to be enough for what we
5270 * intend it to do.
5271 */
5272 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5273 TRUE,
5274 0, 0,
5275 FILL_X((int)Columns) + gui.border_offset,
5276 FILL_Y((int)Rows) + gui.border_offset);
5277
5278 gui_mch_flush();
5279 ui_delay((long)msec, TRUE); /* wait so many msec */
5280
5281 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5282 TRUE,
5283 0, 0,
5284 FILL_X((int)Columns) + gui.border_offset,
5285 FILL_Y((int)Rows) + gui.border_offset);
5286
5287 gdk_gc_destroy(invert_gc);
5288}
5289
5290/*
5291 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5292 */
5293 void
5294gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5295{
5296 GdkGCValues values;
5297 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 if (gui.drawarea->window == NULL)
5300 return;
5301
Bram Moolenaar89d40322006-08-29 15:30:07 +00005302 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5303 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 values.function = GDK_XOR;
5305 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5306 &values,
5307 GDK_GC_FOREGROUND |
5308 GDK_GC_BACKGROUND |
5309 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005310 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5311 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5313 TRUE,
5314 FILL_X(c), FILL_Y(r),
5315 (nc) * gui.char_width, (nr) * gui.char_height);
5316 gdk_gc_destroy(invert_gc);
5317}
5318
5319/*
5320 * Iconify the GUI window.
5321 */
5322 void
5323gui_mch_iconify(void)
5324{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326}
5327
5328#if defined(FEAT_EVAL) || defined(PROTO)
5329/*
5330 * Bring the Vim window to the foreground.
5331 */
5332 void
5333gui_mch_set_foreground(void)
5334{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336}
5337#endif
5338
5339/*
5340 * Draw a cursor without focus.
5341 */
5342 void
5343gui_mch_draw_hollow_cursor(guicolor_T color)
5344{
5345 int i = 1;
5346
5347 if (gui.drawarea->window == NULL)
5348 return;
5349
5350 gui_mch_set_fg_color(color);
5351
5352 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 if (mb_lefthalve(gui.row, gui.col))
5354 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5356 FALSE,
5357 FILL_X(gui.col), FILL_Y(gui.row),
5358 i * gui.char_width - 1, gui.char_height - 1);
5359}
5360
5361/*
5362 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5363 * color "color".
5364 */
5365 void
5366gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5367{
5368 if (gui.drawarea->window == NULL)
5369 return;
5370
5371 gui_mch_set_fg_color(color);
5372
5373 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5374 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5375 TRUE,
5376#ifdef FEAT_RIGHTLEFT
5377 /* vertical line should be on the right of current point */
5378 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5379#endif
5380 FILL_X(gui.col),
5381 FILL_Y(gui.row) + gui.char_height - h,
5382 w, h);
5383}
5384
5385
5386/*
5387 * Catch up with any queued X11 events. This may put keyboard input into the
5388 * input buffer, call resize call-backs, trigger timers etc. If there is
5389 * nothing in the X11 event queue (& no timers pending), then we return
5390 * immediately.
5391 */
5392 void
5393gui_mch_update(void)
5394{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005395 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5396 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397}
5398
5399 static gint
5400input_timer_cb(gpointer data)
5401{
5402 int *timed_out = (int *) data;
5403
Bram Moolenaar49325942007-05-10 19:19:59 +00005404 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 *timed_out = TRUE;
5406
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 return FALSE; /* don't happen again */
5408}
5409
5410#ifdef FEAT_SNIFF
5411/*
5412 * Callback function, used when data is available on the SNiFF connection.
5413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 static void
5415sniff_request_cb(
5416 gpointer data,
5417 gint source_fd,
5418 GdkInputCondition condition)
5419{
5420 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5421
5422 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424#endif
5425
5426/*
5427 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5428 * from the keyboard.
5429 * wtime == -1 Wait forever.
5430 * wtime == 0 This should never happen.
5431 * wtime > 0 Wait wtime milliseconds for a character.
5432 * Returns OK if a character was found to be available within the given time,
5433 * or FAIL otherwise.
5434 */
5435 int
5436gui_mch_wait_for_chars(long wtime)
5437{
5438 int focus;
5439 guint timer;
5440 static int timed_out;
5441#ifdef FEAT_SNIFF
5442 static int sniff_on = 0;
5443 static gint sniff_input_id = 0;
5444#endif
5445
5446#ifdef FEAT_SNIFF
5447 if (sniff_on && !want_sniff_request)
5448 {
5449 if (sniff_input_id)
5450 gdk_input_remove(sniff_input_id);
5451 sniff_on = 0;
5452 }
5453 else if (!sniff_on && want_sniff_request)
5454 {
5455 /* Add fd_from_sniff to watch for available data in main loop. */
5456 sniff_input_id = gdk_input_add(fd_from_sniff,
5457 GDK_INPUT_READ, sniff_request_cb, NULL);
5458 sniff_on = 1;
5459 }
5460#endif
5461
5462 timed_out = FALSE;
5463
5464 /* this timeout makes sure that we will return if no characters arrived in
5465 * time */
5466
5467 if (wtime > 0)
5468 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5469 else
5470 timer = 0;
5471
5472 focus = gui.in_focus;
5473
5474 do
5475 {
5476 /* Stop or start blinking when focus changes */
5477 if (gui.in_focus != focus)
5478 {
5479 if (gui.in_focus)
5480 gui_mch_start_blink();
5481 else
5482 gui_mch_stop_blink();
5483 focus = gui.in_focus;
5484 }
5485
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005486#ifdef MESSAGE_QUEUE
5487 parse_queued_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005488#endif
5489
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 /*
5491 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005492 * Skip this if input is available anyway (can happen in rare
5493 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005495 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005496 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
5498 /* Got char, return immediately */
5499 if (input_available())
5500 {
5501 if (timer != 0 && !timed_out)
5502 gtk_timeout_remove(timer);
5503 return OK;
5504 }
5505 } while (wtime < 0 || !timed_out);
5506
5507 /*
5508 * Flush all eventually pending (drawing) events.
5509 */
5510 gui_mch_update();
5511
5512 return FAIL;
5513}
5514
5515
5516/****************************************************************************
5517 * Output drawing routines.
5518 ****************************************************************************/
5519
5520
5521/* Flush any output to the screen */
5522 void
5523gui_mch_flush(void)
5524{
5525#ifdef HAVE_GTK_MULTIHEAD
5526 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5527 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5528#else
5529 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 /* This happens to actually do what gui_mch_flush() is supposed to do,
5532 * according to the comment above. */
5533 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5534 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535}
5536
5537/*
5538 * Clear a rectangular region of the screen from text pos (row1, col1) to
5539 * (row2, col2) inclusive.
5540 */
5541 void
5542gui_mch_clear_block(int row1, int col1, int row2, int col2)
5543{
5544 GdkColor color;
5545
5546 if (gui.drawarea->window == NULL)
5547 return;
5548
5549 color.pixel = gui.back_pixel;
5550
5551 gdk_gc_set_foreground(gui.text_gc, &color);
5552
5553 /* Clear one extra pixel at the far right, for when bold characters have
5554 * spilled over to the window border. */
5555 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5556 FILL_X(col1), FILL_Y(row1),
5557 (col2 - col1 + 1) * gui.char_width
5558 + (col2 == Columns - 1),
5559 (row2 - row1 + 1) * gui.char_height);
5560}
5561
5562 void
5563gui_mch_clear_all(void)
5564{
5565 if (gui.drawarea->window != NULL)
5566 gdk_window_clear(gui.drawarea->window);
5567}
5568
5569/*
5570 * Redraw any text revealed by scrolling up/down.
5571 */
5572 static void
5573check_copy_area(void)
5574{
5575 GdkEvent *event;
5576 int expose_count;
5577
5578 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5579 return;
5580
5581 /* Avoid redrawing the cursor while scrolling or it'll end up where
5582 * we don't want it to be. I'm not sure if it's correct to call
5583 * gui_dont_update_cursor() at this point but it works as a quick
5584 * fix for now. */
5585 gui_dont_update_cursor();
5586
5587 do
5588 {
5589 /* Wait to check whether the scroll worked or not. */
5590 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5591
5592 if (event == NULL)
5593 break; /* received NoExpose event */
5594
5595 gui_redraw(event->expose.area.x, event->expose.area.y,
5596 event->expose.area.width, event->expose.area.height);
5597
5598 expose_count = event->expose.count;
5599 gdk_event_free(event);
5600 }
5601 while (expose_count > 0); /* more events follow */
5602
5603 gui_can_update_cursor();
5604}
5605
5606/*
5607 * Delete the given number of lines from the given row, scrolling up any
5608 * text further down within the scroll region.
5609 */
5610 void
5611gui_mch_delete_lines(int row, int num_lines)
5612{
5613 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5614 return; /* Can't see the window */
5615
5616 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5617 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5618
5619 /* copy one extra pixel, for when bold has spilled over */
5620 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5621 FILL_X(gui.scroll_region_left), FILL_Y(row),
5622 gui.drawarea->window,
5623 FILL_X(gui.scroll_region_left),
5624 FILL_Y(row + num_lines),
5625 gui.char_width * (gui.scroll_region_right
5626 - gui.scroll_region_left + 1) + 1,
5627 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5628
5629 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5630 gui.scroll_region_left,
5631 gui.scroll_region_bot, gui.scroll_region_right);
5632 check_copy_area();
5633}
5634
5635/*
5636 * Insert the given number of lines before the given row, scrolling down any
5637 * following text within the scroll region.
5638 */
5639 void
5640gui_mch_insert_lines(int row, int num_lines)
5641{
5642 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5643 return; /* Can't see the window */
5644
5645 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5646 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5647
5648 /* copy one extra pixel, for when bold has spilled over */
5649 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5650 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5651 gui.drawarea->window,
5652 FILL_X(gui.scroll_region_left), FILL_Y(row),
5653 gui.char_width * (gui.scroll_region_right
5654 - gui.scroll_region_left + 1) + 1,
5655 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5656
5657 gui_clear_block(row, gui.scroll_region_left,
5658 row + num_lines - 1, gui.scroll_region_right);
5659 check_copy_area();
5660}
5661
5662/*
5663 * X Selection stuff, for cutting and pasting text to other windows.
5664 */
5665 void
5666clip_mch_request_selection(VimClipboard *cbd)
5667{
5668 GdkAtom target;
5669 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005670 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5673 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005674 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5675 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 received_selection = RS_NONE;
5677 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5678
5679 gtk_selection_convert(gui.drawarea,
5680 cbd->gtk_sel_atom, target,
5681 (guint32)GDK_CURRENT_TIME);
5682
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005683 /* Hack: Wait up to three seconds for the selection. A hang was
5684 * noticed here when using the netrw plugin combined with ":gui"
5685 * during the FocusGained event. */
5686 start = time(NULL);
5687 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005688 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
5690 if (received_selection != RS_FAIL)
5691 return;
5692 }
5693
5694 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005695 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696}
5697
5698/*
5699 * Disown the selection.
5700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005702clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005704 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706}
5707
5708/*
5709 * Own the selection and return OK if it worked.
5710 */
5711 int
5712clip_mch_own_selection(VimClipboard *cbd)
5713{
5714 int success;
5715
5716 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02005717 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 gui_mch_update();
5719 return (success) ? OK : FAIL;
5720}
5721
5722/*
5723 * Send the current selection to the clipboard. Do nothing for X because we
5724 * will fill in the selection only when requested by another app.
5725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005727clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729}
5730
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005731 int
5732clip_gtk_owner_exists(VimClipboard *cbd)
5733{
5734 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
5735}
5736
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737
5738#if defined(FEAT_MENU) || defined(PROTO)
5739/*
5740 * Make a menu item appear either active or not active (grey or not grey).
5741 */
5742 void
5743gui_mch_menu_grey(vimmenu_T *menu, int grey)
5744{
5745 if (menu->id == NULL)
5746 return;
5747
5748 if (menu_is_separator(menu->name))
5749 grey = TRUE;
5750
5751 gui_mch_menu_hidden(menu, FALSE);
5752 /* Be clever about bitfields versus true booleans here! */
5753 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5754 {
5755 gtk_widget_set_sensitive(menu->id, !grey);
5756 gui_mch_update();
5757 }
5758}
5759
5760/*
5761 * Make menu item hidden or not hidden.
5762 */
5763 void
5764gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5765{
5766 if (menu->id == 0)
5767 return;
5768
5769 if (hidden)
5770 {
5771 if (GTK_WIDGET_VISIBLE(menu->id))
5772 {
5773 gtk_widget_hide(menu->id);
5774 gui_mch_update();
5775 }
5776 }
5777 else
5778 {
5779 if (!GTK_WIDGET_VISIBLE(menu->id))
5780 {
5781 gtk_widget_show(menu->id);
5782 gui_mch_update();
5783 }
5784 }
5785}
5786
5787/*
5788 * This is called after setting all the menus to grey/hidden or not.
5789 */
5790 void
5791gui_mch_draw_menubar(void)
5792{
5793 /* just make sure that the visual changes get effect immediately */
5794 gui_mch_update();
5795}
5796#endif /* FEAT_MENU */
5797
5798/*
5799 * Scrollbar stuff.
5800 */
5801 void
5802gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5803{
5804 if (sb->id == NULL)
5805 return;
5806
5807 if (flag)
5808 gtk_widget_show(sb->id);
5809 else
5810 gtk_widget_hide(sb->id);
5811
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005812 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815
5816/*
5817 * Return the RGB value of a pixel as long.
5818 */
5819 long_u
5820gui_mch_get_rgb(guicolor_T pixel)
5821{
5822 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5824 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825
5826 return (((unsigned)color.red & 0xff00) << 8)
5827 | ((unsigned)color.green & 0xff00)
5828 | (((unsigned)color.blue & 0xff00) >> 8);
5829}
5830
5831/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005832 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005834 void
5835gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005837 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
5839
5840 void
5841gui_mch_setmouse(int x, int y)
5842{
5843 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5844 * internal GDK mechanism present to accomplish this. (and for good
5845 * reason...) */
5846 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5847 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5848 0, 0, 0U, 0U, x, y);
5849}
5850
5851
5852#ifdef FEAT_MOUSESHAPE
5853/* The last set mouse pointer shape is remembered, to be used when it goes
5854 * from hidden to not hidden. */
5855static int last_shape = 0;
5856#endif
5857
5858/*
5859 * Use the blank mouse pointer or not.
5860 *
5861 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5862 */
5863 void
5864gui_mch_mousehide(int hide)
5865{
5866 if (gui.pointer_hidden != hide)
5867 {
5868 gui.pointer_hidden = hide;
5869 if (gui.drawarea->window && gui.blank_pointer != NULL)
5870 {
5871 if (hide)
5872 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5873 else
5874#ifdef FEAT_MOUSESHAPE
5875 mch_set_mouse_shape(last_shape);
5876#else
5877 gdk_window_set_cursor(gui.drawarea->window, NULL);
5878#endif
5879 }
5880 }
5881}
5882
5883#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5884
5885/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5886 * misc2.c! */
5887static const int mshape_ids[] =
5888{
5889 GDK_LEFT_PTR, /* arrow */
5890 GDK_CURSOR_IS_PIXMAP, /* blank */
5891 GDK_XTERM, /* beam */
5892 GDK_SB_V_DOUBLE_ARROW, /* updown */
5893 GDK_SIZING, /* udsizing */
5894 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5895 GDK_SIZING, /* lrsizing */
5896 GDK_WATCH, /* busy */
5897 GDK_X_CURSOR, /* no */
5898 GDK_CROSSHAIR, /* crosshair */
5899 GDK_HAND1, /* hand1 */
5900 GDK_HAND2, /* hand2 */
5901 GDK_PENCIL, /* pencil */
5902 GDK_QUESTION_ARROW, /* question */
5903 GDK_RIGHT_PTR, /* right-arrow */
5904 GDK_CENTER_PTR, /* up-arrow */
5905 GDK_LEFT_PTR /* last one */
5906};
5907
5908 void
5909mch_set_mouse_shape(int shape)
5910{
5911 int id;
5912 GdkCursor *c;
5913
5914 if (gui.drawarea->window == NULL)
5915 return;
5916
5917 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5918 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5919 else
5920 {
5921 if (shape >= MSHAPE_NUMBERED)
5922 {
5923 id = shape - MSHAPE_NUMBERED;
5924 if (id >= GDK_LAST_CURSOR)
5925 id = GDK_LEFT_PTR;
5926 else
5927 id &= ~1; /* they are always even (why?) */
5928 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005929 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005931 else
5932 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933# ifdef HAVE_GTK_MULTIHEAD
5934 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005935 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005937 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938# endif
5939 gdk_window_set_cursor(gui.drawarea->window, c);
5940 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5941 }
5942 if (shape != MSHAPE_HIDE)
5943 last_shape = shape;
5944}
5945#endif /* FEAT_MOUSESHAPE */
5946
5947
5948#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5949/*
5950 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5951 * scaled down if the current font is not big enough, or scaled up if the image
5952 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5953 * will be cut off if the current font is not big enough, or centered if it's
5954 * too small.
5955 */
5956# define SIGN_WIDTH (2 * gui.char_width)
5957# define SIGN_HEIGHT (gui.char_height)
5958# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 void
5961gui_mch_drawsign(int row, int col, int typenr)
5962{
5963 GdkPixbuf *sign;
5964
5965 sign = (GdkPixbuf *)sign_get_image(typenr);
5966
5967 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5968 {
5969 int width;
5970 int height;
5971 int xoffset;
5972 int yoffset;
5973 int need_scale;
5974
5975 width = gdk_pixbuf_get_width(sign);
5976 height = gdk_pixbuf_get_height(sign);
5977 /*
5978 * Decide whether we need to scale. Allow one pixel of border
5979 * width to be cut off, in order to avoid excessive scaling for
5980 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005981 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 */
5983 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005984 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 || (width < 3 * SIGN_WIDTH / 4
5986 && height < 3 * SIGN_HEIGHT / 4));
5987 if (need_scale)
5988 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005989 double aspect;
5990 int w = width;
5991 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
5993 /* Keep the original aspect ratio */
5994 aspect = (double)height / (double)width;
5995 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5996 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005997 if (((double)(MAX(height, SIGN_HEIGHT)) /
5998 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
5999 {
6000 /* Change the aspect ratio by at most 15% to fill the
6001 * available space completly. */
6002 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
6003 height = MIN(height, SIGN_HEIGHT);
6004 }
6005 else
6006 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
Bram Moolenaar58cbc912014-06-17 18:47:02 +02006008 if (w == width && h == height)
6009 {
6010 /* no change in dimensions; don't decrease reference counter
6011 * (below) */
6012 need_scale = FALSE;
6013 }
6014 else
6015 {
6016 /* This doesn't seem to be worth caching, and doing so would
6017 * complicate the code quite a bit. */
6018 sign = gdk_pixbuf_scale_simple(sign, width, height,
6019 GDK_INTERP_BILINEAR);
6020 if (sign == NULL)
6021 return; /* out of memory */
6022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 }
6024
6025 /* The origin is the upper-left corner of the pixmap. Therefore
6026 * these offset may become negative if the pixmap is smaller than
6027 * the 2x1 cells reserved for the sign icon. */
6028 xoffset = (width - SIGN_WIDTH) / 2;
6029 yoffset = (height - SIGN_HEIGHT) / 2;
6030
6031 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6032
6033 gdk_draw_rectangle(gui.drawarea->window,
6034 gui.text_gc,
6035 TRUE,
6036 FILL_X(col),
6037 FILL_Y(row),
6038 SIGN_WIDTH,
6039 SIGN_HEIGHT);
6040
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 gdk_pixbuf_render_to_drawable_alpha(sign,
6042 gui.drawarea->window,
6043 MAX(0, xoffset),
6044 MAX(0, yoffset),
6045 FILL_X(col) - MIN(0, xoffset),
6046 FILL_Y(row) - MIN(0, yoffset),
6047 MIN(width, SIGN_WIDTH),
6048 MIN(height, SIGN_HEIGHT),
6049 GDK_PIXBUF_ALPHA_BILEVEL,
6050 127,
6051 GDK_RGB_DITHER_NORMAL,
6052 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 if (need_scale)
6054 g_object_unref(sign);
6055 }
6056}
6057
6058 void *
6059gui_mch_register_sign(char_u *signfile)
6060{
6061 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6062 {
6063 GdkPixbuf *sign;
6064 GError *error = NULL;
6065 char_u *message;
6066
6067 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6068
6069 if (error == NULL)
6070 return sign;
6071
6072 message = (char_u *)error->message;
6073
6074 if (message != NULL && input_conv.vc_type != CONV_NONE)
6075 message = string_convert(&input_conv, message, NULL);
6076
6077 if (message != NULL)
6078 {
6079 /* The error message is already translated and will be more
6080 * descriptive than anything we could possibly do ourselves. */
6081 EMSG2("E255: %s", message);
6082
6083 if (input_conv.vc_type != CONV_NONE)
6084 vim_free(message);
6085 }
6086 g_error_free(error);
6087 }
6088
6089 return NULL;
6090}
6091
6092 void
6093gui_mch_destroy_sign(void *sign)
6094{
6095 if (sign != NULL)
6096 g_object_unref(sign);
6097}
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099#endif /* FEAT_SIGN_ICONS */