blob: 9f6775fb8c83adc7b5d29709b9c3af7a18eb25c0 [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
Bram Moolenaar66f948e2016-01-30 16:39:25 +0100574gui_mch_free_all(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000575{
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 }
1440 return OK;
1441}
1442
1443/*
1444 * Check if the GUI can be started. Called before gvimrc is sourced but after
1445 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 * Return OK or FAIL.
1447 */
1448 int
1449gui_mch_init_check(void)
1450{
Bram Moolenaar3a466a82016-01-19 17:47:25 +01001451#ifdef USE_GRESOURCE
1452 static int res_registered = FALSE;
1453
1454 if (!res_registered)
1455 {
1456 /* Call this function in the GUI process; otherwise, the resources
1457 * won't be available. Don't call it twice. */
1458 res_registered = TRUE;
1459 gui_gtk_register_resource();
1460 }
1461#endif
1462
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#ifdef FEAT_GUI_GNOME
1464 if (gtk_socket_id == 0)
1465 using_gnome = 1;
1466#endif
1467
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001468 /* This defaults to argv[0], but we want it to match the name of the
1469 * shipped gvim.desktop so that Vim's windows can be associated with this
1470 * file. */
1471 g_set_prgname("gvim");
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1474 if (!gtk_init_check(&gui_argc, &gui_argv))
1475 {
1476 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001477 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 return FAIL;
1479 }
1480
1481 return OK;
1482}
1483
1484
1485/****************************************************************************
1486 * Mouse handling callbacks
1487 */
1488
1489
1490static guint mouse_click_timer = 0;
1491static int mouse_timed_out = TRUE;
1492
1493/*
1494 * Timer used to recognize multiple clicks of the mouse button
1495 */
1496 static gint
1497mouse_click_timer_cb(gpointer data)
1498{
1499 /* we don't use this information currently */
1500 int *timed_out = (int *) data;
1501
1502 *timed_out = TRUE;
1503 return FALSE; /* don't happen again */
1504}
1505
1506static guint motion_repeat_timer = 0;
1507static int motion_repeat_offset = FALSE;
1508static gint motion_repeat_timer_cb(gpointer);
1509
1510 static void
1511process_motion_notify(int x, int y, GdkModifierType state)
1512{
1513 int button;
1514 int_u vim_modifiers;
1515
1516 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1517 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1518 GDK_BUTTON5_MASK))
1519 ? MOUSE_DRAG : ' ';
1520
1521 /* If our pointer is currently hidden, then we should show it. */
1522 gui_mch_mousehide(FALSE);
1523
1524 /* Just moving the rodent above the drawing area without any button
1525 * being pressed. */
1526 if (button != MOUSE_DRAG)
1527 {
1528 gui_mouse_moved(x, y);
1529 return;
1530 }
1531
1532 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001533 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaar49325942007-05-10 19:19:59 +00001535 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /*
1539 * Auto repeat timer handling.
1540 */
1541 if (x < 0 || y < 0
1542 || x >= gui.drawarea->allocation.width
1543 || y >= gui.drawarea->allocation.height)
1544 {
1545
1546 int dx;
1547 int dy;
1548 int offshoot;
1549 int delay = 10;
1550
1551 /* Calculate the maximal distance of the cursor from the drawing area.
1552 * (offshoot can't become negative here!).
1553 */
1554 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1555 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1556
1557 offshoot = dx > dy ? dx : dy;
1558
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001559 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 * distance of 127 pixels from the main window.
1561 *
1562 * One could think endlessly about the most ergonomic variant here.
1563 * For example it could make sense to calculate the distance from the
1564 * drags start instead...
1565 *
1566 * Maybe a parabolic interpolation would suite us better here too...
1567 */
1568 if (offshoot > 127)
1569 {
1570 /* 5 appears to be somehow near to my perceptual limits :-). */
1571 delay = 5;
1572 }
1573 else
1574 {
1575 delay = (130 * (127 - offshoot)) / 127 + 5;
1576 }
1577
1578 /* shoot again */
1579 if (!motion_repeat_timer)
1580 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1581 motion_repeat_timer_cb, NULL);
1582 }
1583}
1584
1585/*
1586 * Timer used to recognize multiple clicks of the mouse button.
1587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001589motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
1591 int x;
1592 int y;
1593 GdkModifierType state;
1594
1595 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1596
1597 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1598 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1599 GDK_BUTTON5_MASK)))
1600 {
1601 motion_repeat_timer = 0;
1602 return FALSE;
1603 }
1604
1605 /* If there already is a mouse click in the input buffer, wait another
1606 * time (otherwise we would create a backlog of clicks) */
1607 if (vim_used_in_input_buf() > 10)
1608 return TRUE;
1609
1610 motion_repeat_timer = 0;
1611
1612 /*
1613 * Fake a motion event.
1614 * Trick: Pretend the mouse moved to the next character on every other
1615 * event, otherwise drag events will be discarded, because they are still
1616 * in the same character.
1617 */
1618 if (motion_repeat_offset)
1619 x += gui.char_width;
1620
1621 motion_repeat_offset = !motion_repeat_offset;
1622 process_motion_notify(x, y, state);
1623
1624 /* Don't happen again. We will get reinstalled in the synthetic event
1625 * if needed -- thus repeating should still work. */
1626 return FALSE;
1627}
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001630motion_notify_event(GtkWidget *widget,
1631 GdkEventMotion *event,
1632 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
1634 if (event->is_hint)
1635 {
1636 int x;
1637 int y;
1638 GdkModifierType state;
1639
1640 gdk_window_get_pointer(widget->window, &x, &y, &state);
1641 process_motion_notify(x, y, state);
1642 }
1643 else
1644 {
1645 process_motion_notify((int)event->x, (int)event->y,
1646 (GdkModifierType)event->state);
1647 }
1648
1649 return TRUE; /* handled */
1650}
1651
1652
1653/*
1654 * Mouse button handling. Note please that we are capturing multiple click's
1655 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1656 * This is due to the way the generic VIM code is recognizing multiple clicks.
1657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001659button_press_event(GtkWidget *widget,
1660 GdkEventButton *event,
1661 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662{
1663 int button;
1664 int repeated_click = FALSE;
1665 int x, y;
1666 int_u vim_modifiers;
1667
Bram Moolenaar20892c12011-06-26 04:49:00 +02001668 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001669
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 /* Make sure we have focus now we've been selected */
1671 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1672 gtk_widget_grab_focus(widget);
1673
1674 /*
1675 * Don't let additional events about multiple clicks send by GTK to us
1676 * after the initial button press event confuse us.
1677 */
1678 if (event->type != GDK_BUTTON_PRESS)
1679 return FALSE;
1680
1681 x = event->x;
1682 y = event->y;
1683
1684 /* Handle multiple clicks */
1685 if (!mouse_timed_out && mouse_click_timer)
1686 {
1687 gtk_timeout_remove(mouse_click_timer);
1688 mouse_click_timer = 0;
1689 repeated_click = TRUE;
1690 }
1691
1692 mouse_timed_out = FALSE;
1693 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1694 mouse_click_timer_cb, &mouse_timed_out);
1695
1696 switch (event->button)
1697 {
Bram Moolenaar88e484b2015-11-24 15:38:44 +01001698 /* Keep in sync with gui_x11.c.
1699 * Buttons 4-7 are handled in scroll_event() */
1700 case 1: button = MOUSE_LEFT; break;
1701 case 2: button = MOUSE_MIDDLE; break;
1702 case 3: button = MOUSE_RIGHT; break;
1703 case 8: button = MOUSE_X1; break;
1704 case 9: button = MOUSE_X2; break;
1705 default:
1706 return FALSE; /* Unknown button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 }
1708
1709#ifdef FEAT_XIM
1710 /* cancel any preediting */
1711 if (im_is_preediting())
1712 xim_reset();
1713#endif
1714
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001715 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
1719 return TRUE;
1720}
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001723 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001726scroll_event(GtkWidget *widget,
1727 GdkEventScroll *event,
1728 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
1730 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001731 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1734 gtk_widget_grab_focus(widget);
1735
1736 switch (event->direction)
1737 {
1738 case GDK_SCROLL_UP:
1739 button = MOUSE_4;
1740 break;
1741 case GDK_SCROLL_DOWN:
1742 button = MOUSE_5;
1743 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001744 case GDK_SCROLL_LEFT:
1745 button = MOUSE_7;
1746 break;
1747 case GDK_SCROLL_RIGHT:
1748 button = MOUSE_6;
1749 break;
1750 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 return FALSE;
1752 }
1753
1754# ifdef FEAT_XIM
1755 /* cancel any preediting */
1756 if (im_is_preediting())
1757 xim_reset();
1758# endif
1759
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001760 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
1762 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1763 FALSE, vim_modifiers);
1764
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 return TRUE;
1766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767
1768
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001770button_release_event(GtkWidget *widget UNUSED,
1771 GdkEventButton *event,
1772 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773{
1774 int x, y;
1775 int_u vim_modifiers;
1776
Bram Moolenaar20892c12011-06-26 04:49:00 +02001777 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /* Remove any motion "machine gun" timers used for automatic further
1780 extension of allocation areas if outside of the applications window
1781 area .*/
1782 if (motion_repeat_timer)
1783 {
1784 gtk_timeout_remove(motion_repeat_timer);
1785 motion_repeat_timer = 0;
1786 }
1787
1788 x = event->x;
1789 y = event->y;
1790
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001791 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795 return TRUE;
1796}
1797
1798
1799#ifdef FEAT_DND
1800/****************************************************************************
1801 * Drag aNd Drop support handlers.
1802 */
1803
1804/*
1805 * Count how many items there may be and separate them with a NUL.
1806 * Apparently the items are separated with \r\n. This is not documented,
1807 * thus be careful not to go past the end. Also allow separation with
1808 * NUL characters.
1809 */
1810 static int
1811count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1812{
1813 int i;
1814 char_u *p = out;
1815 int count = 0;
1816
1817 for (i = 0; i < len; ++i)
1818 {
1819 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1820 {
1821 if (p > out && p[-1] != NUL)
1822 {
1823 ++count;
1824 *p++ = NUL;
1825 }
1826 }
1827 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1828 {
1829 *p++ = hexhex2nr(raw + i + 1);
1830 i += 2;
1831 }
1832 else
1833 *p++ = raw[i];
1834 }
1835 if (p > out && p[-1] != NUL)
1836 {
1837 *p = NUL; /* last item didn't have \r or \n */
1838 ++count;
1839 }
1840 return count;
1841}
1842
1843/*
1844 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1845 * this process, URI which protocol is not "file:" are removed. Return
1846 * length of array (less than "max").
1847 */
1848 static int
1849filter_uri_list(char_u **outlist, int max, char_u *src)
1850{
1851 int i, j;
1852
1853 for (i = j = 0; i < max; ++i)
1854 {
1855 outlist[i] = NULL;
1856 if (STRNCMP(src, "file:", 5) == 0)
1857 {
1858 src += 5;
1859 if (STRNCMP(src, "//localhost", 11) == 0)
1860 src += 11;
1861 while (src[0] == '/' && src[1] == '/')
1862 ++src;
1863 outlist[j++] = vim_strsave(src);
1864 }
1865 src += STRLEN(src) + 1;
1866 }
1867 return j;
1868}
1869
1870 static char_u **
1871parse_uri_list(int *count, char_u *data, int len)
1872{
1873 int n = 0;
1874 char_u *tmp = NULL;
1875 char_u **array = NULL;;
1876
1877 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1878 {
1879 n = count_and_decode_uri_list(tmp, data, len);
1880 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1881 n = filter_uri_list(array, n, tmp);
1882 }
1883 vim_free(tmp);
1884 *count = n;
1885 return array;
1886}
1887
1888 static void
1889drag_handle_uri_list(GdkDragContext *context,
1890 GtkSelectionData *data,
1891 guint time_,
1892 GdkModifierType state,
1893 gint x,
1894 gint y)
1895{
1896 char_u **fnames;
1897 int nfiles = 0;
1898
1899 fnames = parse_uri_list(&nfiles, data->data, data->length);
1900
1901 if (fnames != NULL && nfiles > 0)
1902 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001903 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1906
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001907 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1910 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001911 else
1912 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913}
1914
1915 static void
1916drag_handle_text(GdkDragContext *context,
1917 GtkSelectionData *data,
1918 guint time_,
1919 GdkModifierType state)
1920{
1921 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1922 char_u *text;
1923 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 text = data->data;
1927 len = data->length;
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (data->type == utf8_string_atom)
1930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (input_conv.vc_type != CONV_NONE)
1932 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (tmpbuf != NULL)
1934 text = tmpbuf;
1935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 dnd_yank_drag_data(text, (long)len);
1938 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001941 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943 if (dropkey[2] != 0)
1944 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1945 else
1946 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947}
1948
1949/*
1950 * DND receiver.
1951 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 static void
1953drag_data_received_cb(GtkWidget *widget,
1954 GdkDragContext *context,
1955 gint x,
1956 gint y,
1957 GtkSelectionData *data,
1958 guint info,
1959 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001960 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961{
1962 GdkModifierType state;
1963
1964 /* Guard against trash */
1965 if (data->data == NULL
1966 || data->length <= 0
1967 || data->format != 8
1968 || data->data[data->length] != '\0')
1969 {
1970 gtk_drag_finish(context, FALSE, FALSE, time_);
1971 return;
1972 }
1973
1974 /* Get the current modifier state for proper distinguishment between
1975 * different operations later. */
1976 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1977
1978 /* Not sure about the role of "text/plain" here... */
1979 if (info == (guint)TARGET_TEXT_URI_LIST)
1980 drag_handle_uri_list(context, data, time_, state, x, y);
1981 else
1982 drag_handle_text(context, data, time_, state);
1983
1984}
1985#endif /* FEAT_DND */
1986
1987
1988#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1989/*
1990 * GnomeClient interact callback. Check for unsaved buffers that cannot
1991 * be abandoned and pop up a dialog asking the user for confirmation if
1992 * necessary.
1993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001995sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001997 GnomeDialogType type UNUSED,
1998 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999{
2000 cmdmod_T save_cmdmod;
2001 gboolean shutdown_cancelled;
2002
2003 save_cmdmod = cmdmod;
2004
2005# ifdef FEAT_BROWSE
2006 cmdmod.browse = TRUE;
2007# endif
2008# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2009 cmdmod.confirm = TRUE;
2010# endif
2011 /*
2012 * If there are changed buffers, present the user with
2013 * a dialog if possible, otherwise give an error message.
2014 */
Bram Moolenaar027387f2016-01-02 22:25:52 +01002015 shutdown_cancelled = check_changed_any(FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017 exiting = FALSE;
2018 cmdmod = save_cmdmod;
2019 setcursor(); /* position the cursor */
2020 out_flush();
2021 /*
2022 * If the user hit the [Cancel] button the whole shutdown
2023 * will be cancelled. Wow, quite powerful feature (:
2024 */
2025 gnome_interaction_key_return(key, shutdown_cancelled);
2026}
2027
2028/*
2029 * Generate a script that can be used to restore the current editing session.
2030 * Save the value of v:this_session before running :mksession in order to make
2031 * automagic session save fully transparent. Return TRUE on success.
2032 */
2033 static int
2034write_session_file(char_u *filename)
2035{
2036 char_u *escaped_filename;
2037 char *mksession_cmdline;
2038 unsigned int save_ssop_flags;
2039 int failed;
2040
2041 /*
2042 * Build an ex command line to create a script that restores the current
2043 * session if executed. Escape the filename to avoid nasty surprises.
2044 */
2045 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2046 if (escaped_filename == NULL)
2047 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002048 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2049 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 /*
2053 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2054 * unpredictable effects when the session is saved automatically. Also,
2055 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2056 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2057 * enormously large if the GNOME session feature is used regularly.
2058 */
2059 save_ssop_flags = ssop_flags;
2060 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002061 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062
2063 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2064 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2065 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002066 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 ssop_flags = save_ssop_flags;
2069 g_free(mksession_cmdline);
Bram Moolenaar24dc2302014-05-13 20:19:58 +02002070
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 /*
2072 * Reopen the file and append a command to restore v:this_session,
2073 * as if this save never happened. This is to avoid conflicts with
2074 * the user's own sessions. FIXME: It's probably less hackish to add
2075 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2076 */
2077 if (!failed)
2078 {
2079 FILE *fd;
2080
2081 fd = open_exfile(filename, TRUE, APPENDBIN);
2082
2083 failed = (fd == NULL
2084 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2085 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2086
2087 if (fd != NULL && fclose(fd) != 0)
2088 failed = TRUE;
2089
2090 if (failed)
2091 mch_remove(filename);
2092 }
2093
2094 return !failed;
2095}
2096
2097/*
2098 * "save_yourself" signal handler. Initiate an interaction to ask the user
2099 * for confirmation if necessary. Save the current editing session and tell
2100 * the session manager how to restart Vim.
2101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 static gboolean
2103sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002104 gint phase UNUSED,
2105 GnomeSaveStyle save_style UNUSED,
2106 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002108 gboolean fast UNUSED,
2109 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 static const char suffix[] = "-session.vim";
2112 char *session_file;
2113 unsigned int len;
2114 gboolean success;
2115
2116 /* Always request an interaction if possible. check_changed_any()
2117 * won't actually show a dialog unless any buffers have been modified.
2118 * There doesn't seem to be an obvious way to check that without
2119 * automatically firing the dialog. Anyway, it works just fine. */
2120 if (interact_style == GNOME_INTERACT_ANY)
2121 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2122 &sm_client_check_changed_any,
2123 NULL);
2124 out_flush();
2125 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2126
2127 /* The path is unique for each session save. We do neither know nor care
2128 * which session script will actually be used later. This decision is in
2129 * the domain of the session manager. */
2130 session_file = gnome_config_get_real_path(
2131 gnome_client_get_config_prefix(client));
2132 len = strlen(session_file);
2133
2134 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2135 --len; /* get rid of the superfluous trailing '/' */
2136
2137 session_file = g_renew(char, session_file, len + sizeof(suffix));
2138 memcpy(session_file + len, suffix, sizeof(suffix));
2139
2140 success = write_session_file((char_u *)session_file);
2141
2142 if (success)
2143 {
2144 const char *argv[8];
2145 int i;
2146
2147 /* Tell the session manager how to wipe out the stored session data.
2148 * This isn't as dangerous as it looks, don't worry :) session_file
2149 * is a unique absolute filename. Usually it'll be something like
2150 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2151 i = 0;
2152 argv[i++] = "rm";
2153 argv[i++] = session_file;
2154 argv[i] = NULL;
2155
2156 gnome_client_set_discard_command(client, i, (char **)argv);
2157
2158 /* Tell the session manager how to restore the just saved session.
2159 * This is easily done thanks to Vim's -S option. Pass the -f flag
2160 * since there's no need to fork -- it might even cause confusion.
2161 * Also pass the window role to give the WM something to match on.
2162 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2163 i = 0;
2164 argv[i++] = restart_command;
2165 argv[i++] = "-f";
2166 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 argv[i++] = "--role";
2168 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 argv[i++] = "-S";
2170 argv[i++] = session_file;
2171 argv[i] = NULL;
2172
2173 gnome_client_set_restart_command(client, i, (char **)argv);
2174 gnome_client_set_clone_command(client, 0, NULL);
2175 }
2176
2177 g_free(session_file);
2178
2179 return success;
2180}
2181
2182/*
2183 * Called when the session manager wants us to die. There isn't much to save
2184 * here since "save_yourself" has been emitted before (unless serious trouble
2185 * is happening).
2186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002188sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189{
2190 /* Don't write messages to the GUI anymore */
2191 full_screen = FALSE;
2192
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002193 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002194 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002195 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 preserve_exit();
2197}
2198
2199/*
2200 * Connect our signal handlers to be notified on session save and shutdown.
2201 */
2202 static void
2203setup_save_yourself(void)
2204{
2205 GnomeClient *client;
2206
2207 client = gnome_master_client();
2208
2209 if (client != NULL)
2210 {
2211 /* Must use the deprecated gtk_signal_connect() for compatibility
2212 * with GNOME 1. Arrgh, zombies! */
2213 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2214 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2215 gtk_signal_connect(GTK_OBJECT(client), "die",
2216 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2217 }
2218}
2219
2220#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2221
2222# ifdef USE_XSMP
2223/*
2224 * GTK tells us that XSMP needs attention
2225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 static gboolean
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002227local_xsmp_handle_requests(
2228 GIOChannel *source UNUSED,
2229 GIOCondition condition,
2230 gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231{
2232 if (condition == G_IO_IN)
2233 {
2234 /* Do stuff; maybe close connection */
2235 if (xsmp_handle_requests() == FAIL)
2236 g_io_channel_unref((GIOChannel *)data);
2237 return TRUE;
2238 }
2239 /* Error */
2240 g_io_channel_unref((GIOChannel *)data);
2241 xsmp_close();
2242 return TRUE;
2243}
2244# endif /* USE_XSMP */
2245
2246/*
2247 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2248 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2249 */
2250 static void
2251setup_save_yourself(void)
2252{
2253 Atom *existing_atoms = NULL;
2254 int count = 0;
2255
2256#ifdef USE_XSMP
2257 if (xsmp_icefd != -1)
2258 {
2259 /*
2260 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2261 * set up GTK IO monitor
2262 */
2263 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2264
2265 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2266 local_xsmp_handle_requests, (gpointer)g_io);
2267 }
2268 else
2269#endif
2270 {
2271 /* Fall back to old method */
2272
2273 /* first get the existing value */
2274 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2275 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2276 &existing_atoms, &count))
2277 {
2278 Atom *new_atoms;
2279 Atom save_yourself_xatom;
2280 int i;
2281
2282 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2283
2284 /* check if WM_SAVE_YOURSELF isn't there yet */
2285 for (i = 0; i < count; ++i)
2286 if (existing_atoms[i] == save_yourself_xatom)
2287 break;
2288
2289 if (i == count)
2290 {
2291 /* allocate an Atoms array which is one item longer */
2292 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2293 * sizeof(Atom)));
2294 if (new_atoms != NULL)
2295 {
2296 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2297 new_atoms[count] = save_yourself_xatom;
2298 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2299 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2300 new_atoms, count + 1);
2301 vim_free(new_atoms);
2302 }
2303 }
2304 XFree(existing_atoms);
2305 }
2306 }
2307}
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309/*
2310 * Installing a global event filter seems to be the only way to catch
2311 * client messages of type WM_PROTOCOLS without overriding GDK's own
2312 * client message event filter. Well, that's still better than trying
2313 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 *
2315 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2316 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2317 * I have the nasty feeling modern session managers just don't send this
2318 * deprecated message anymore. Addition: confirmed by several people.
2319 *
2320 * The GNOME session support is much cooler anyway. Unlike this ugly
2321 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2322 * it should work with KDE as well.
2323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002325global_event_filter(GdkXEvent *xev,
2326 GdkEvent *event UNUSED,
2327 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
2329 XEvent *xevent = (XEvent *)xev;
2330
2331 if (xevent != NULL
2332 && xevent->type == ClientMessage
2333 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002334 && (long_u)xevent->xclient.data.l[0]
2335 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 out_flush();
2338 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2339 /*
2340 * Set the window's WM_COMMAND property, to let the window manager
2341 * know we are done saving ourselves. We don't want to be
2342 * restarted, thus set argv to NULL.
2343 */
2344 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2345 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2346 NULL, 0);
2347 return GDK_FILTER_REMOVE;
2348 }
2349
2350 return GDK_FILTER_CONTINUE;
2351}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2353
2354
2355/*
2356 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002359mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
2361/* If you get an error message here, you still need to unpack the runtime
2362 * archive! */
2363#ifdef magick
2364# undef magick
2365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 /* A bit hackish, but avoids casting later and allows optimization */
2367# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#define magick vim32x32
2369#include "../runtime/vim32x32.xpm"
2370#undef magick
2371#define magick vim16x16
2372#include "../runtime/vim16x16.xpm"
2373#undef magick
2374#define magick vim48x48
2375#include "../runtime/vim48x48.xpm"
2376#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 /* When started with "--echo-wid" argument, write window ID on stdout. */
2380 if (echo_wid_arg)
2381 {
2382 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2383 fflush(stdout);
2384 }
2385
2386 if (vim_strchr(p_go, GO_ICON) != NULL)
2387 {
2388 /*
2389 * Add an icon to the main window. For fun and convenience of the user.
2390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 GList *icons = NULL;
2392
2393 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2394 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2395 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2396
2397 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2398
2399 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2400 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
2402
2403#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2404 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406#endif
2407 /* Setup to indicate to the window manager that we want to catch the
2408 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2409 * manager instead. */
2410#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2411 if (using_gnome)
2412#endif
2413 setup_save_yourself();
2414
2415#ifdef FEAT_CLIENTSERVER
2416 if (serverName == NULL && serverDelayedStartName != NULL)
2417 {
2418 /* This is a :gui command in a plain vim with no previous server */
2419 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2420
2421 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2422 serverDelayedStartName);
2423 }
2424 else
2425 {
2426 /*
2427 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2428 * have to change the "server" registration to that of the main window
2429 * If we have not registered a name yet, remember the window
2430 */
2431 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2432 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2433 }
2434 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2435 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2436 GTK_SIGNAL_FUNC(property_event), NULL);
2437#endif
2438}
2439
2440 static GdkCursor *
2441create_blank_pointer(void)
2442{
2443 GdkWindow *root_window = NULL;
2444 GdkPixmap *blank_mask;
2445 GdkCursor *cursor;
2446 GdkColor color = { 0, 0, 0, 0 };
2447 char blank_data[] = { 0x0 };
2448
2449#ifdef HAVE_GTK_MULTIHEAD
2450 root_window = gtk_widget_get_root_window(gui.mainwin);
2451#endif
2452
2453 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2454 * in size. */
2455 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2456 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2457 &color, &color, 0, 0);
2458 gdk_bitmap_unref(blank_mask);
2459
2460 return cursor;
2461}
2462
2463#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 static void
2465mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002466 GdkScreen *previous_screen UNUSED,
2467 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 if (!gtk_widget_has_screen(widget))
2470 return;
2471
2472 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002473 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
2475 if (gui.blank_pointer != NULL)
2476 gdk_cursor_unref(gui.blank_pointer);
2477
2478 gui.blank_pointer = create_blank_pointer();
2479
2480 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2481 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2482
2483 /*
2484 * Create a new PangoContext for this screen, and initialize it
2485 * with the current font if necessary.
2486 */
2487 if (gui.text_context != NULL)
2488 g_object_unref(gui.text_context);
2489
2490 gui.text_context = gtk_widget_create_pango_context(widget);
2491 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2492
2493 if (gui.norm_font != NULL)
2494 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002495 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002496 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 }
2498}
2499#endif /* HAVE_GTK_MULTIHEAD */
2500
2501/*
2502 * After the drawing area comes up, we calculate all colors and create the
2503 * dummy blank cursor.
2504 *
2505 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2506 * fact that the main VIM engine doesn't take them into account anywhere.
2507 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002509drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510{
2511 GtkWidget *sbar;
2512
2513#ifdef FEAT_XIM
2514 xim_init();
2515#endif
2516 gui_mch_new_colors();
2517 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2518
2519 gui.blank_pointer = create_blank_pointer();
2520 if (gui.pointer_hidden)
2521 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2522
2523 /* get the actual size of the scrollbars, if they are realized */
2524 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2525 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2526 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2527 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2528 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2529 gui.scrollbar_width = sbar->allocation.width;
2530
2531 sbar = gui.bottom_sbar.id;
2532 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2533 gui.scrollbar_height = sbar->allocation.height;
2534}
2535
2536/*
2537 * Properly clean up on shutdown.
2538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002540drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
2542 /* Don't write messages to the GUI anymore */
2543 full_screen = FALSE;
2544
2545#ifdef FEAT_XIM
2546 im_shutdown();
2547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (gui.ascii_glyphs != NULL)
2549 {
2550 pango_glyph_string_free(gui.ascii_glyphs);
2551 gui.ascii_glyphs = NULL;
2552 }
2553 if (gui.ascii_font != NULL)
2554 {
2555 g_object_unref(gui.ascii_font);
2556 gui.ascii_font = NULL;
2557 }
2558 g_object_unref(gui.text_context);
2559 gui.text_context = NULL;
2560
2561 g_object_unref(gui.text_gc);
2562 gui.text_gc = NULL;
2563
2564 gdk_cursor_unref(gui.blank_pointer);
2565 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566}
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002569drawarea_style_set_cb(GtkWidget *widget UNUSED,
2570 GtkStyle *previous_style UNUSED,
2571 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 gui_mch_new_colors();
2574}
2575
2576/*
2577 * Callback routine for the "delete_event" signal on the toplevel window.
2578 * Tries to vim gracefully, or refuses to exit with changed buffers.
2579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002581delete_event_cb(GtkWidget *widget UNUSED,
2582 GdkEventAny *event UNUSED,
2583 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585 gui_shell_closed();
2586 return TRUE;
2587}
2588
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002589#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2590 static int
2591get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2592{
2593 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2594
2595#ifdef FEAT_GUI_GNOME
2596 if (using_gnome && widget != NULL)
2597 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002598 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002599 BonoboDockItem *dockitem;
2600
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002601 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002602 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2603 {
2604 /* Only menu & toolbar are dock items. Could tabline be?
2605 * Seem to be only the 2 defined in GNOME */
2606 widget = parent;
2607 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002608
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002609 if (dockitem == NULL || dockitem->is_floating)
2610 return 0;
2611 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2612 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002613 }
2614#endif
2615 if (widget != NULL
2616 && item_orientation == orientation
2617 && GTK_WIDGET_REALIZED(widget)
2618 && GTK_WIDGET_VISIBLE(widget))
2619 {
2620 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2621 return widget->allocation.height;
2622 else
2623 return widget->allocation.width;
2624 }
2625 return 0;
2626}
2627#endif
2628
2629 static int
2630get_menu_tool_width(void)
2631{
2632 int width = 0;
2633
2634#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2635# ifdef FEAT_MENU
2636 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2637# endif
2638# ifdef FEAT_TOOLBAR
2639 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2640# endif
2641# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002642 if (gui.tabline != NULL)
2643 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002644# endif
2645#endif
2646
2647 return width;
2648}
2649
2650 static int
2651get_menu_tool_height(void)
2652{
2653 int height = 0;
2654
2655#ifdef FEAT_MENU
2656 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2657#endif
2658#ifdef FEAT_TOOLBAR
2659 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2660#endif
2661#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002662 if (gui.tabline != NULL)
2663 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002664#endif
2665
2666 return height;
2667}
2668
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002669/* This controls whether we can set the real window hints at
2670 * start-up when in a GtkPlug.
2671 * 0 = normal processing (default)
2672 * 1 = init. hints set, no-one's tried to reset since last check
2673 * 2 = init. hints set, attempt made to change hints
2674 */
2675static int init_window_hints_state = 0;
2676
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002677 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002678update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002679{
2680 static int old_width = 0;
2681 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002682 static int old_min_width = 0;
2683 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002684 static int old_char_width = 0;
2685 static int old_char_height = 0;
2686
2687 int width;
2688 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002689 int min_width;
2690 int min_height;
2691
2692 /* At start-up, don't try to set the hints until the initial
2693 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002694 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002695 */
2696 if (!(force_width && force_height) && init_window_hints_state > 0)
2697 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002698 /* Don't do it! */
2699 init_window_hints_state = 2;
2700 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002701 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002702
2703 /* This also needs to be done when the main window isn't there yet,
2704 * otherwise the hints don't work. */
2705 width = gui_get_base_width();
2706 height = gui_get_base_height();
2707# ifdef FEAT_MENU
2708 height += tabline_height() * gui.char_height;
2709# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002710 width += get_menu_tool_width();
2711 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002712
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002713 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002714 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002715 * 'set columns=' or init. -geom) we briefly set the min. to the size
2716 * we wish to be instead of the legitimate minimum so that we actually
2717 * resize correctly.
2718 */
2719 if (force_width && force_height)
2720 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002721 min_width = force_width;
2722 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002723 }
2724 else
2725 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002726 min_width = width + MIN_COLUMNS * gui.char_width;
2727 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002728 }
2729
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002730 /* Avoid an expose event when the size didn't change. */
2731 if (width != old_width
2732 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002733 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002734 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002735 || gui.char_width != old_char_width
2736 || gui.char_height != old_char_height)
2737 {
2738 GdkGeometry geometry;
2739 GdkWindowHints geometry_mask;
2740
2741 geometry.width_inc = gui.char_width;
2742 geometry.height_inc = gui.char_height;
2743 geometry.base_width = width;
2744 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002745 geometry.min_width = min_width;
2746 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002747 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2748 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002749 /* Using gui.formwin as geometry widget doesn't work as expected
2750 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2751 * in Vim confuse GTK+. */
2752 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2753 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002754 old_width = width;
2755 old_height = height;
2756 old_min_width = min_width;
2757 old_min_height = min_height;
2758 old_char_width = gui.char_width;
2759 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002760 }
2761}
2762
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763#ifdef FEAT_TOOLBAR
2764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765/*
2766 * This extra effort wouldn't be necessary if we only used stock icons in the
2767 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2768 * shouldn't be treated differently, thus we do need this.
2769 */
2770 static void
2771icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2772{
2773 if (GTK_IS_IMAGE(widget))
2774 {
2775 GtkImage *image = (GtkImage *)widget;
2776
2777 /* User-defined icons are stored in a GtkIconSet */
2778 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2779 {
2780 GtkIconSet *icon_set;
2781 GtkIconSize icon_size;
2782
2783 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2784 icon_size = (GtkIconSize)(long)user_data;
2785
2786 gtk_icon_set_ref(icon_set);
2787 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2788 gtk_icon_set_unref(icon_set);
2789 }
2790 }
2791 else if (GTK_IS_CONTAINER(widget))
2792 {
2793 gtk_container_foreach((GtkContainer *)widget,
2794 &icon_size_changed_foreach,
2795 user_data);
2796 }
2797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
2799 static void
2800set_toolbar_style(GtkToolbar *toolbar)
2801{
2802 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 GtkIconSize size;
2804 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2807 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2808 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002809 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2811 style = GTK_TOOLBAR_BOTH;
2812 else if (toolbar_flags & TOOLBAR_TEXT)
2813 style = GTK_TOOLBAR_TEXT;
2814 else
2815 style = GTK_TOOLBAR_ICONS;
2816
2817 gtk_toolbar_set_style(toolbar, style);
2818 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2819
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 switch (tbis_flags)
2821 {
2822 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2823 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2824 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2825 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2826 default: size = GTK_ICON_SIZE_INVALID; break;
2827 }
2828 oldsize = gtk_toolbar_get_icon_size(toolbar);
2829
2830 if (size == GTK_ICON_SIZE_INVALID)
2831 {
2832 /* Let global user preferences decide the icon size. */
2833 gtk_toolbar_unset_icon_size(toolbar);
2834 size = gtk_toolbar_get_icon_size(toolbar);
2835 }
2836 if (size != oldsize)
2837 {
2838 gtk_container_foreach(GTK_CONTAINER(toolbar),
2839 &icon_size_changed_foreach,
2840 GINT_TO_POINTER((int)size));
2841 }
2842 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843}
2844
2845#endif /* FEAT_TOOLBAR */
2846
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002847#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2848static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002849static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002850static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002851static int clicked_page; /* page clicked in tab line */
2852
2853/*
2854 * Handle selecting an item in the tab line popup menu.
2855 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002856 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002857tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002858{
Bram Moolenaara5621492006-02-25 21:55:24 +00002859 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002860 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002861}
2862
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002863 static void
2864add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2865{
2866 GtkWidget *item;
2867 char_u *utf_text;
2868
2869 utf_text = CONVERT_TO_UTF8(text);
2870 item = gtk_menu_item_new_with_label((const char *)utf_text);
2871 gtk_widget_show(item);
2872 CONVERT_TO_UTF8_FREE(utf_text);
2873
2874 gtk_container_add(GTK_CONTAINER(menu), item);
2875 gtk_signal_connect(GTK_OBJECT(item), "activate",
2876 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002877 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002878}
2879
Bram Moolenaara5621492006-02-25 21:55:24 +00002880/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002881 * Create a menu for the tab line.
2882 */
2883 static GtkWidget *
2884create_tabline_menu(void)
2885{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002886 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002887
2888 menu = gtk_menu_new();
Bram Moolenaar7098ee52015-06-09 19:14:24 +02002889 if (first_tabpage->tp_next != NULL)
2890 add_tabline_menu_item(menu, (char_u *)_("Close tab"),
2891 TABLINE_MENU_CLOSE);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002892 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2893 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002894
2895 return menu;
2896}
2897
2898 static gboolean
2899on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2900{
2901 /* Was this button press event ? */
2902 if (event->type == GDK_BUTTON_PRESS)
2903 {
2904 GdkEventButton *bevent = (GdkEventButton *)event;
2905 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002906 int y = bevent->y;
2907 GtkWidget *tabwidget;
2908 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002909
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002910 /* When ignoring events return TRUE so that the selected page doesn't
2911 * change. */
2912 if (hold_gui_events
2913# ifdef FEAT_CMDWIN
2914 || cmdwin_type != 0
2915# endif
2916 )
2917 return TRUE;
2918
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002919 tabwin = gdk_window_at_pointer(&x, &y);
2920 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2921 clicked_page = (int)(long)gtk_object_get_user_data(
2922 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002923
2924 /* If the event was generated for 3rd button popup the menu. */
2925 if (bevent->button == 3)
2926 {
2927 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2928 bevent->button, bevent->time);
2929 /* We handled the event. */
2930 return TRUE;
2931 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002932 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002933 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002934 if (clicked_page == 0)
2935 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002936 /* Click after all tabs moves to next tab page. When "x" is
2937 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002938 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002939 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002940 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002941 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002942
Bram Moolenaara5621492006-02-25 21:55:24 +00002943 /* We didn't handle the event. */
2944 return FALSE;
2945}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002946
2947/*
2948 * Handle selecting one of the tabs.
2949 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002950 static void
2951on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002952 GtkNotebook *notebook UNUSED,
2953 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002954 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002955 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002956{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002957 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002958 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002959 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002960 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002961}
2962
2963/*
2964 * Show or hide the tabline.
2965 */
2966 void
2967gui_mch_show_tabline(int showit)
2968{
2969 if (gui.tabline == NULL)
2970 return;
2971
2972 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2973 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002974 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002975 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002976 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002977 if (showit)
2978 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002979 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002980
2981 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002982}
2983
2984/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002985 * Return TRUE when tabline is displayed.
2986 */
2987 int
2988gui_mch_showing_tabline(void)
2989{
2990 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002991 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002992}
2993
2994/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002995 * Update the labels of the tabline.
2996 */
2997 void
2998gui_mch_update_tabline(void)
2999{
3000 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003001 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003002 GtkWidget *label;
3003 tabpage_T *tp;
3004 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003005 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003006 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003007 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003008
3009 if (gui.tabline == NULL)
3010 return;
3011
3012 ignore_tabline_evt = TRUE;
3013
3014 /* Add a label for each tab page. They all contain the same text area. */
3015 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3016 {
3017 if (tp == curtab)
3018 curtabidx = nr;
3019
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003020 tab_num = nr + 1;
3021
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003022 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3023 if (page == NULL)
3024 {
3025 /* Add notebook page */
3026 page = gtk_vbox_new(FALSE, 0);
3027 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003028 event_box = gtk_event_box_new();
3029 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003030 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003031 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003032 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003033 gtk_widget_show(label);
3034 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3035 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003036 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003037 nr++);
3038 }
3039
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003040 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003041 gtk_object_set_user_data(GTK_OBJECT(event_box),
3042 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003043 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003044 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003045 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003046 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003047 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003048
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003049 get_tabline_label(tp, TRUE);
3050 labeltext = CONVERT_TO_UTF8(NameBuff);
3051 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3052 (const char *)labeltext, NULL);
3053 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003054 }
3055
3056 /* Remove any old labels. */
3057 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3058 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3059
3060 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003061 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003062
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003063 /* Make sure everything is in place before drawing text. */
3064 gui_mch_update();
3065
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003066 ignore_tabline_evt = FALSE;
3067}
3068
3069/*
3070 * Set the current tab to "nr". First tab is 1.
3071 */
3072 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003073gui_mch_set_curtab(int nr)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003074{
3075 if (gui.tabline == NULL)
3076 return;
3077
3078 ignore_tabline_evt = TRUE;
3079 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003080 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003081 ignore_tabline_evt = FALSE;
3082}
3083
3084#endif /* FEAT_GUI_TABLINE */
3085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003087 * Add selection targets for PRIMARY and CLIPBOARD selections.
3088 */
3089 void
3090gui_gtk_set_selection_targets(void)
3091{
3092 int i, j = 0;
3093 int n_targets = N_SELECTION_TARGETS;
3094 GtkTargetEntry targets[N_SELECTION_TARGETS];
3095
3096 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3097 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003098 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003099 * return something, instead of trying another target. Therefore only
3100 * offer TARGET_HTML when it works. */
3101 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3102 n_targets--;
3103 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003104 targets[j++] = selection_targets[i];
3105 }
3106
3107 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3108 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3109 gtk_selection_add_targets(gui.drawarea,
3110 (GdkAtom)GDK_SELECTION_PRIMARY,
3111 targets, n_targets);
3112 gtk_selection_add_targets(gui.drawarea,
3113 (GdkAtom)clip_plus.gtk_sel_atom,
3114 targets, n_targets);
3115}
3116
3117/*
3118 * Set up for receiving DND items.
3119 */
3120 void
3121gui_gtk_set_dnd_targets(void)
3122{
3123 int i, j = 0;
3124 int n_targets = N_DND_TARGETS;
3125 GtkTargetEntry targets[N_DND_TARGETS];
3126
3127 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3128 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003129 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003130 n_targets--;
3131 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003132 targets[j++] = dnd_targets[i];
3133 }
3134
3135 gtk_drag_dest_unset(gui.drawarea);
3136 gtk_drag_dest_set(gui.drawarea,
3137 GTK_DEST_DEFAULT_ALL,
3138 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003139 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003140}
3141
3142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3144 * Returns OK for success, FAIL when the GUI can't be started.
3145 */
3146 int
3147gui_mch_init(void)
3148{
3149 GtkWidget *vbox;
3150
3151#ifdef FEAT_GUI_GNOME
3152 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3153 * exits on failure, but that's a non-issue because we already called
3154 * gtk_init_check() in gui_mch_init_check(). */
3155 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3158 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003159# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003160 {
3161 char *p = setlocale(LC_NUMERIC, NULL);
3162
3163 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3164 * init may change it. */
3165 if (p == NULL || strcmp(p, "C") != 0)
3166 setlocale(LC_NUMERIC, "C");
3167 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003168# endif
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
3171 vim_free(gui_argv);
3172 gui_argv = NULL;
3173
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003174#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 /* Set the human-readable application name */
3176 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 /*
3179 * Force UTF-8 output no matter what the value of 'encoding' is.
3180 * did_set_string_option() in option.c prohibits changing 'termencoding'
3181 * to something else than UTF-8 if the GUI is in use.
3182 */
3183 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3184
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003185#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3189 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003190#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192#endif
3193
3194 /* Initialize values */
3195 gui.border_width = 2;
3196 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3197 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003198 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003200 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003202 /* LINTED: avoid warning: conversion to 'unsigned long' */
3203 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
3205 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003206 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /* Set default foreground and background colors. */
3210 gui.norm_pixel = gui.def_norm_pixel;
3211 gui.back_pixel = gui.def_back_pixel;
3212
3213 if (gtk_socket_id != 0)
3214 {
3215 GtkWidget *plug;
3216
3217 /* Use GtkSocket from another app. */
3218#ifdef HAVE_GTK_MULTIHEAD
3219 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3220 gtk_socket_id);
3221#else
3222 plug = gtk_plug_new(gtk_socket_id);
3223#endif
3224 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3225 {
3226 gui.mainwin = plug;
3227 }
3228 else
3229 {
3230 g_warning("Connection to GTK+ socket (ID %u) failed",
3231 (unsigned int)gtk_socket_id);
3232 /* Pretend we never wanted it if it failed (get own window) */
3233 gtk_socket_id = 0;
3234 }
3235 }
3236
3237 if (gtk_socket_id == 0)
3238 {
3239#ifdef FEAT_GUI_GNOME
3240 if (using_gnome)
3241 {
3242 gui.mainwin = gnome_app_new("Vim", NULL);
3243# ifdef USE_XSMP
3244 /* Use the GNOME save-yourself functionality now. */
3245 xsmp_close();
3246# endif
3247 }
3248 else
3249#endif
3250 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3251 }
3252
3253 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 /* Create the PangoContext used for drawing all text. */
3256 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3257 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3260 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3261
3262 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3263 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3264
3265 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3266 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3267#ifdef HAVE_GTK_MULTIHEAD
3268 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3269 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 gui.accel_group = gtk_accel_group_new();
3272 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003274 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 vbox = gtk_vbox_new(FALSE, 0);
3276
3277#ifdef FEAT_GUI_GNOME
3278 if (using_gnome)
3279 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003280# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 /* automagically restore menubar/toolbar placement */
3282 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3283# endif
3284 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3285 }
3286 else
3287#endif
3288 {
3289 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3290 gtk_widget_show(vbox);
3291 }
3292
3293#ifdef FEAT_MENU
3294 /*
3295 * Create the menubar and handle
3296 */
3297 gui.menubar = gtk_menu_bar_new();
3298 gtk_widget_set_name(gui.menubar, "vim-menubar");
3299
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003300 /* Avoid that GTK takes <F10> away from us. */
3301 {
3302 GtkSettings *gtk_settings;
3303
3304 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3305 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3306 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003307
3308
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309# ifdef FEAT_GUI_GNOME
3310 if (using_gnome)
3311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 BonoboDockItem *dockitem;
3313
3314 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3315 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3316 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003317 /* We don't want the menu to float. */
3318 bonobo_dock_item_set_behavior(dockitem,
3319 bonobo_dock_item_get_behavior(dockitem)
3320 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 else
3324# endif /* FEAT_GUI_GNOME */
3325 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003326 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3327 * disabled in gui_init() later. */
3328 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3330 }
3331#endif /* FEAT_MENU */
3332
3333#ifdef FEAT_TOOLBAR
3334 /*
3335 * Create the toolbar and handle
3336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 /* some aesthetics on the toolbar */
3338 gtk_rc_parse_string(
3339 "style \"vim-toolbar-style\" {\n"
3340 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3341 "}\n"
3342 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3343 gui.toolbar = gtk_toolbar_new();
3344 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3346
3347# ifdef FEAT_GUI_GNOME
3348 if (using_gnome)
3349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 BonoboDockItem *dockitem;
3351
3352 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3353 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3354 GNOME_APP_TOOLBAR_NAME);
3355 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003356 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003357 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003358 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003359 bonobo_dock_item_get_behavior(dockitem)
3360 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 }
3363 else
3364# endif /* FEAT_GUI_GNOME */
3365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3367 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3368 gtk_widget_show(gui.toolbar);
3369 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3370 }
3371#endif /* FEAT_TOOLBAR */
3372
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003373#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003374 /*
3375 * Use a Notebook for the tab pages labels. The labels are hidden by
3376 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003377 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003378 gui.tabline = gtk_notebook_new();
3379 gtk_widget_show(gui.tabline);
3380 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3381 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3382 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003383 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003384 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003385
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003386 tabline_tooltip = gtk_tooltips_new();
3387 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3388
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003389 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003390 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003391
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003392 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003393 page = gtk_vbox_new(FALSE, 0);
3394 gtk_widget_show(page);
3395 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3396 label = gtk_label_new("-Empty-");
3397 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003398 event_box = gtk_event_box_new();
3399 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003400 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003401 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003402 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003403 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003404 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003405
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003406 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003407 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003408
3409 /* Create a popup menu for the tab line and connect it. */
3410 tabline_menu = create_tabline_menu();
3411 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003412 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003413#endif
3414
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 gui.formwin = gtk_form_new();
3416 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3417 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3418
3419 gui.drawarea = gtk_drawing_area_new();
3420
3421 /* Determine which events we will filter. */
3422 gtk_widget_set_events(gui.drawarea,
3423 GDK_EXPOSURE_MASK |
3424 GDK_ENTER_NOTIFY_MASK |
3425 GDK_LEAVE_NOTIFY_MASK |
3426 GDK_BUTTON_PRESS_MASK |
3427 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 GDK_KEY_PRESS_MASK |
3430 GDK_KEY_RELEASE_MASK |
3431 GDK_POINTER_MOTION_MASK |
3432 GDK_POINTER_MOTION_HINT_MASK);
3433
3434 gtk_widget_show(gui.drawarea);
3435 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3436 gtk_widget_show(gui.formwin);
3437 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3438
3439 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3440 * and not the window. */
3441 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3442 : GTK_OBJECT(gui.drawarea),
3443 "key_press_event",
3444 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003445#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 /* Also forward key release events for the benefit of GTK+ 2 input
3447 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3448 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3449 : G_OBJECT(gui.drawarea),
3450 "key_release_event",
3451 G_CALLBACK(&key_release_event), NULL);
3452#endif
3453 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3454 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3455 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3456 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3457
3458 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3459 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3460
3461 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3462
3463#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3464 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3465 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3466#endif
3467
3468 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003469 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3471
3472 /*
3473 * Set clipboard specific atoms
3474 */
3475 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3478 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3479
3480 /*
3481 * Start out by adding the configured border width into the border offset.
3482 */
3483 gui.border_offset = gui.border_width;
3484
3485 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3486 GTK_SIGNAL_FUNC(visibility_event), NULL);
3487 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3488 GTK_SIGNAL_FUNC(expose_event), NULL);
3489
3490 /*
3491 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3492 * Only needed for some window managers.
3493 */
3494 if (vim_strchr(p_go, GO_POINTER) != NULL)
3495 {
3496 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3497 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3498 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3499 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3500 }
3501
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003502 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3503 * only its widgets. Arguably, this could be common code and we not use
3504 * the window focus at all, but let's be safe.
3505 */
3506 if (gtk_socket_id == 0)
3507 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003508 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3509 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3510 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3511 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003512 }
3513 else
3514 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003515 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3516 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3517 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3518 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003519#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003520 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3521 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3522 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3523 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003524#endif /* FEAT_GUI_TABLINE */
3525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526
3527 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3528 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3529 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3530 GTK_SIGNAL_FUNC(button_press_event), NULL);
3531 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3532 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3534 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
3536 /*
3537 * Add selection handler functions.
3538 */
3539 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3540 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3541 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3542 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3543
Bram Moolenaara76638f2010-06-05 12:49:46 +02003544 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3547 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3548
3549 /* Pretend we don't have input focus, we will get an event if we do. */
3550 gui.in_focus = FALSE;
3551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 return OK;
3553}
3554
3555#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3556/*
3557 * This is called from gui_start() after a fork() has been done.
3558 * We have to tell the session manager our new PID.
3559 */
3560 void
3561gui_mch_forked(void)
3562{
3563 if (using_gnome)
3564 {
3565 GnomeClient *client;
3566
3567 client = gnome_master_client();
3568
3569 if (client != NULL)
3570 gnome_client_set_process_id(client, getpid());
3571 }
3572}
3573#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3574
3575/*
3576 * Called when the foreground or background color has been changed.
3577 * This used to change the graphics contexts directly but we are
3578 * currently manipulating them where desired.
3579 */
3580 void
3581gui_mch_new_colors(void)
3582{
3583 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3584 {
3585 GdkColor color = { 0, 0, 0, 0 };
3586
3587 color.pixel = gui.back_pixel;
3588 gdk_window_set_background(gui.drawarea->window, &color);
3589 }
3590}
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592/*
3593 * This signal informs us about the need to rearrange our sub-widgets.
3594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003596form_configure_event(GtkWidget *widget UNUSED,
3597 GdkEventConfigure *event,
3598 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003600 int usable_height = event->height;
3601
3602 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3603 * no. of char-heights), so we have to manually sanitise them.
3604 * Widths seem to sort themselves out, don't ask me why.
3605 */
3606 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003607 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003608
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003610 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 gtk_form_thaw(GTK_FORM(gui.formwin));
3612
3613 return TRUE;
3614}
3615
3616/*
3617 * Function called when window already closed.
3618 * We can't do much more here than to trying to preserve what had been done,
3619 * since the window is already inevitably going away.
3620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003622mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623{
3624 /* Don't write messages to the GUI anymore */
3625 full_screen = FALSE;
3626
3627 gui.mainwin = NULL;
3628 gui.drawarea = NULL;
3629
3630 if (!exiting) /* only do anything if the destroy was unexpected */
3631 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003632 vim_strncpy(IObuff,
3633 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3634 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 preserve_exit();
3636 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +01003637#ifdef USE_GRESOURCE
3638 gui_gtk_unregister_resource();
3639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640}
3641
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003642
3643/*
3644 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3645 * hints (and thus the required size from -geom), but that after that we
3646 * put the hints back to normal (the actual minimum size) so we may
3647 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003648 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003649 * only way we have of making ourselves bigger (by set lines/columns).
3650 * Thus set hints at start-up to ensure correct init. size, then a
3651 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003652 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003653 * We'll not let the default hints be set while this timer's active.
3654 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003655 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003656check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003657{
3658 if (init_window_hints_state == 1)
3659 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003660 /* Safe to use normal hints now */
3661 init_window_hints_state = 0;
3662 update_window_manager_hints(0, 0);
3663 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003664 }
3665
3666 /* Keep on trying */
3667 init_window_hints_state = 1;
3668 return TRUE;
3669}
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671/*
3672 * Open the GUI window which was created by a call to gui_mch_init().
3673 */
3674 int
3675gui_mch_open(void)
3676{
3677 guicolor_T fg_pixel = INVALCOLOR;
3678 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003679 guint pixel_width;
3680 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 /*
3683 * Allow setting a window role on the command line, or invent one
3684 * if none was specified. This is mainly useful for GNOME session
3685 * support; allowing the WM to restore window placement.
3686 */
3687 if (role_argument != NULL)
3688 {
3689 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3690 }
3691 else
3692 {
3693 char *role;
3694
3695 /* Invent a unique-enough ID string for the role */
3696 role = g_strdup_printf("vim-%u-%u-%u",
3697 (unsigned)mch_get_pid(),
3698 (unsigned)g_random_int(),
3699 (unsigned)time(NULL));
3700
3701 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3702 g_free(role);
3703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707
3708 /* Determine user specified geometry, if present. */
3709 if (gui.geom != NULL)
3710 {
3711 int mask;
3712 unsigned int w, h;
3713 int x = 0;
3714 int y = 0;
3715
3716 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3717
3718 if (mask & WidthValue)
3719 Columns = w;
3720 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003721 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003722 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003723 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003725 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003726 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003727
3728 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3729 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3730
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003731 pixel_width += get_menu_tool_width();
3732 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003735 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003736 int ww, hh;
3737 gui_mch_get_screen_dimensions(&ww, &hh);
3738 hh += p_ghr + get_menu_tool_height();
3739 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003740 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003741 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003742 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003743 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 vim_free(gui.geom);
3747 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003748
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003749 /* From now until everyone's stopped trying to set the window hints
3750 * to their correct minimum values, stop them being set as we need
3751 * them to remain at our required size for the parent GtkSocket to
3752 * give us the right initial size.
3753 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003754 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003755 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003756 update_window_manager_hints(pixel_width, pixel_height);
3757 init_window_hints_state = 1;
3758 g_timeout_add(1000, check_startup_plug_hints, NULL);
3759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 }
3761
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003762 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3763 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003764 /* For GTK2 changing the size of the form widget doesn't cause window
3765 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003766 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003767 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003768 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 if (foreground_argument != NULL)
3771 fg_pixel = gui_get_color((char_u *)foreground_argument);
3772 if (fg_pixel == INVALCOLOR)
3773 fg_pixel = gui_get_color((char_u *)"Black");
3774
3775 if (background_argument != NULL)
3776 bg_pixel = gui_get_color((char_u *)background_argument);
3777 if (bg_pixel == INVALCOLOR)
3778 bg_pixel = gui_get_color((char_u *)"White");
3779
3780 if (found_reverse_arg)
3781 {
3782 gui.def_norm_pixel = bg_pixel;
3783 gui.def_back_pixel = fg_pixel;
3784 }
3785 else
3786 {
3787 gui.def_norm_pixel = fg_pixel;
3788 gui.def_back_pixel = bg_pixel;
3789 }
3790
3791 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3792 * in a vimrc file) */
3793 set_normal_colors();
3794
3795 /* Check that none of the colors are the same as the background color */
3796 gui_check_colors();
3797
3798 /* Get the colors for the highlight groups (gui_check_colors() might have
3799 * changed them). */
3800 highlight_gui_started(); /* re-init colors and fonts */
3801
3802 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3803 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3804
3805#ifdef FEAT_HANGULIN
3806 hangul_keyboard_set();
3807#endif
3808
3809 /*
3810 * Notify the fixed area about the need to resize the contents of the
3811 * gui.formwin, which we use for random positioning of the included
3812 * components.
3813 *
3814 * We connect this signal deferred finally after anything is in place,
3815 * since this is intended to handle resizements coming from the window
3816 * manager upon us and should not interfere with what VIM is requesting
3817 * upon startup.
3818 */
3819 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3820 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3821
3822#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003823 /* Set up for receiving DND items. */
3824 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825
3826 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3827 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3828#endif
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003831 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (found_iconic_arg && gtk_socket_id == 0)
3833 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003836#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 unsigned long menu_handler = 0;
3838# ifdef FEAT_TOOLBAR
3839 unsigned long tool_handler = 0;
3840# endif
3841 /*
3842 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3843 * show when restoring the saved layout configuration. We can't just
3844 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3845 * a toplevel window and thus will be realized immediately. Instead,
3846 * connect signal handlers to hide the widgets just after they've been
3847 * marked visible, but before the main window is realized.
3848 */
3849 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3850 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3851 G_CALLBACK(&gtk_widget_hide),
3852 NULL);
3853# ifdef FEAT_TOOLBAR
3854 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3855 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3856 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3857 G_CALLBACK(&gtk_widget_hide),
3858 NULL);
3859# endif
3860#endif
3861 gtk_widget_show(gui.mainwin);
3862
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003863#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (menu_handler != 0)
3865 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3866# ifdef FEAT_TOOLBAR
3867 if (tool_handler != 0)
3868 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3869# endif
3870#endif
3871 }
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return OK;
3874}
3875
3876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003878gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879{
3880 if (gui.mainwin != NULL)
3881 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882}
3883
3884/*
3885 * Get the position of the top left corner of the window.
3886 */
3887 int
3888gui_mch_get_winpos(int *x, int *y)
3889{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 return OK;
3892}
3893
3894/*
3895 * Set the position of the top left corner of the window to the given
3896 * coordinates.
3897 */
3898 void
3899gui_mch_set_winpos(int x, int y)
3900{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902}
3903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904#if 0
3905static int resize_idle_installed = FALSE;
3906/*
3907 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3908 * the shell size doesn't exceed the window size, i.e. if the window manager
3909 * ignored our size request. Usually this happens if the window is maximized.
3910 *
3911 * FIXME: It'd be nice if we could find a little more orthodox solution.
3912 * See also the remark below in gui_mch_set_shellsize().
3913 *
3914 * DISABLED: When doing ":set lines+=1" this function would first invoke
3915 * gui_resize_shell() with the old size, then the normal callback would
3916 * report the new size through form_configure_event(). That caused the window
3917 * layout to be messed up.
3918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 static gboolean
3920force_shell_resize_idle(gpointer data)
3921{
3922 if (gui.mainwin != NULL
3923 && GTK_WIDGET_REALIZED(gui.mainwin)
3924 && GTK_WIDGET_VISIBLE(gui.mainwin))
3925 {
3926 int width;
3927 int height;
3928
3929 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3930
3931 width -= get_menu_tool_width();
3932 height -= get_menu_tool_height();
3933
3934 gui_resize_shell(width, height);
3935 }
3936
3937 resize_idle_installed = FALSE;
3938 return FALSE; /* don't call me again */
3939}
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
Bram Moolenaar09736232009-09-23 16:14:49 +00003942/*
3943 * Return TRUE if the main window is maximized.
3944 */
3945 int
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003946gui_mch_maximized(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00003947{
3948 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3949 && (gdk_window_get_state(gui.mainwin->window)
3950 & GDK_WINDOW_STATE_MAXIMIZED));
3951}
3952
3953/*
3954 * Unmaximize the main window
3955 */
3956 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003957gui_mch_unmaximize(void)
Bram Moolenaar09736232009-09-23 16:14:49 +00003958{
3959 if (gui.mainwin != NULL)
3960 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3961}
Bram Moolenaar09736232009-09-23 16:14:49 +00003962
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02003964 * Called when the font changed while the window is maximized. Compute the
3965 * new Rows and Columns. This is like resizing the window.
3966 */
3967 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01003968gui_mch_newfont(void)
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02003969{
3970 int w, h;
3971
3972 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
3973 w -= get_menu_tool_width();
3974 h -= get_menu_tool_height();
3975 gui_resize_shell(w, h);
3976}
3977
3978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * Set the windows size.
3980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 void
3982gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003983 int min_width UNUSED, int min_height UNUSED,
3984 int base_width UNUSED, int base_height UNUSED,
3985 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 /* give GTK+ a chance to put all widget's into place */
3988 gui_mch_update();
3989
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003990 /* this will cause the proper resizement to happen too */
3991 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003992 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003995 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 * main window instead. */
3997 width += get_menu_tool_width();
3998 height += get_menu_tool_height();
3999
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004000 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004001 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004002 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004003 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02004005# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (!resize_idle_installed)
4007 {
4008 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4009 &force_shell_resize_idle, NULL, NULL);
4010 resize_idle_installed = TRUE;
4011 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02004012# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 /*
4014 * Wait until all events are processed to prevent a crash because the
4015 * real size of the drawing area doesn't reflect Vim's internal ideas.
4016 *
4017 * This is a bit of a hack, since Vim is a terminal application with a GUI
4018 * on top, while the GUI expects to be the boss.
4019 */
4020 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021}
4022
4023
4024/*
4025 * The screen size is used to make sure the initial window doesn't get bigger
4026 * than the screen. This subtracts some room for menubar, toolbar and window
4027 * decorations.
4028 */
4029 void
4030gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4031{
4032#ifdef HAVE_GTK_MULTIHEAD
4033 GdkScreen* screen;
4034
4035 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4036 screen = gtk_widget_get_screen(gui.mainwin);
4037 else
4038 screen = gdk_screen_get_default();
4039
4040 *screen_w = gdk_screen_get_width(screen);
4041 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4042#else
4043 *screen_w = gdk_screen_width();
4044 /* Subtract 'guiheadroom' from the height to allow some room for the
4045 * window manager (task list and window title bar). */
4046 *screen_h = gdk_screen_height() - p_ghr;
4047#endif
4048
4049 /*
4050 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4051 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004052 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 * This should be completely changed later.
4054 */
4055 *screen_w -= get_menu_tool_width();
4056 *screen_h -= get_menu_tool_height();
4057}
4058
4059#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004061gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 if (title != NULL && output_conv.vc_type != CONV_NONE)
4064 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4067
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 if (output_conv.vc_type != CONV_NONE)
4069 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070}
4071#endif /* FEAT_TITLE */
4072
4073#if defined(FEAT_MENU) || defined(PROTO)
4074 void
4075gui_mch_enable_menu(int showit)
4076{
4077 GtkWidget *widget;
4078
4079# ifdef FEAT_GUI_GNOME
4080 if (using_gnome)
4081 widget = gui.menubar_h;
4082 else
4083# endif
4084 widget = gui.menubar;
4085
Bram Moolenaar18144c82006-04-12 21:52:12 +00004086 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4087 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 {
4089 if (showit)
4090 gtk_widget_show(widget);
4091 else
4092 gtk_widget_hide(widget);
4093
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004094 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096}
4097#endif /* FEAT_MENU */
4098
4099#if defined(FEAT_TOOLBAR) || defined(PROTO)
4100 void
4101gui_mch_show_toolbar(int showit)
4102{
4103 GtkWidget *widget;
4104
4105 if (gui.toolbar == NULL)
4106 return;
4107
4108# ifdef FEAT_GUI_GNOME
4109 if (using_gnome)
4110 widget = gui.toolbar_h;
4111 else
4112# endif
4113 widget = gui.toolbar;
4114
4115 if (showit)
4116 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4117
4118 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4119 {
4120 if (showit)
4121 gtk_widget_show(widget);
4122 else
4123 gtk_widget_hide(widget);
4124
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004125 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127}
4128#endif /* FEAT_TOOLBAR */
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130/*
4131 * Check if a given font is a CJK font. This is done in a very crude manner. It
4132 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4133 * font. Consequently, this function cannot be used as a general purpose check
4134 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4135 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4136 */
4137 static int
4138is_cjk_font(PangoFontDescription *font_desc)
4139{
4140 static const char * const cjk_langs[] =
4141 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4142
4143 PangoFont *font;
4144 unsigned i;
4145 int is_cjk = FALSE;
4146
4147 font = pango_context_load_font(gui.text_context, font_desc);
4148
4149 if (font == NULL)
4150 return FALSE;
4151
4152 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4153 {
4154 PangoCoverage *coverage;
4155 gunichar uc;
4156
4157 coverage = pango_font_get_coverage(
4158 font, pango_language_from_string(cjk_langs[i]));
4159
4160 if (coverage != NULL)
4161 {
4162 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4163 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4164 pango_coverage_unref(coverage);
4165 }
4166 }
4167
4168 g_object_unref(font);
4169
4170 return is_cjk;
4171}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar231334e2005-07-25 20:46:57 +00004173/*
4174 * Adjust gui.char_height (after 'linespace' was changed).
4175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004177gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 PangoFontMetrics *metrics;
4180 int ascent;
4181 int descent;
4182
4183 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4184 pango_context_get_language(gui.text_context));
4185 ascent = pango_font_metrics_get_ascent(metrics);
4186 descent = pango_font_metrics_get_descent(metrics);
4187
4188 pango_font_metrics_unref(metrics);
4189
4190 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004191 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004192 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 /* A not-positive value of char_height may crash Vim. Only happens
4196 * if 'linespace' is negative (which does make sense sometimes). */
4197 gui.char_ascent = MAX(gui.char_ascent, 0);
4198 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4199
4200 return OK;
4201}
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203/*
4204 * Put up a font dialog and return the selected font name in allocated memory.
4205 * "oldval" is the previous value. Return NULL when cancelled.
4206 * This should probably go into gui_gtk.c. Hmm.
4207 * FIXME:
4208 * The GTK2 font selection dialog has no filtering API. So we could either
4209 * a) implement our own (possibly copying the code from somewhere else) or
4210 * b) just live with it.
4211 */
4212 char_u *
4213gui_mch_font_dialog(char_u *oldval)
4214{
4215 GtkWidget *dialog;
4216 int response;
4217 char_u *fontname = NULL;
4218 char_u *oldname;
4219
4220 dialog = gtk_font_selection_dialog_new(NULL);
4221
4222 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4223 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4224
4225 if (oldval != NULL && oldval[0] != NUL)
4226 {
4227 if (output_conv.vc_type != CONV_NONE)
4228 oldname = string_convert(&output_conv, oldval, NULL);
4229 else
4230 oldname = oldval;
4231
4232 /* Annoying bug in GTK (or Pango): if the font name does not include a
4233 * size, zero is used. Use default point size ten. */
4234 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4235 {
4236 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4237
4238 if (p != NULL)
4239 {
4240 STRCPY(p + STRLEN(p), " 10");
4241 if (oldname != oldval)
4242 vim_free(oldname);
4243 oldname = p;
4244 }
4245 }
4246
4247 gtk_font_selection_dialog_set_font_name(
4248 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4249
4250 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004251 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004253 else
4254 gtk_font_selection_dialog_set_font_name(
4255 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 response = gtk_dialog_run(GTK_DIALOG(dialog));
4258
4259 if (response == GTK_RESPONSE_OK)
4260 {
4261 char *name;
4262
4263 name = gtk_font_selection_dialog_get_font_name(
4264 GTK_FONT_SELECTION_DIALOG(dialog));
4265 if (name != NULL)
4266 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004267 char_u *p;
4268
4269 /* Apparently some font names include a comma, need to escape
4270 * that, because in 'guifont' it separates names. */
4271 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004273 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004274 {
4275 fontname = string_convert(&input_conv, p, NULL);
4276 vim_free(p);
4277 }
4278 else
4279 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 }
4282
4283 if (response != GTK_RESPONSE_NONE)
4284 gtk_widget_destroy(dialog);
4285
4286 return fontname;
4287}
4288
4289/*
4290 * Some monospace fonts don't support a bold weight, and fall back
4291 * silently to the regular weight. But this is no good since our text
4292 * drawing function can emulate bold by overstriking. So let's try
4293 * to detect whether bold weight is actually available and emulate it
4294 * otherwise.
4295 *
4296 * Note that we don't need to check for italic style since Xft can
4297 * emulate italic on its own, provided you have a proper fontconfig
4298 * setup. We wouldn't be able to emulate it in Vim anyway.
4299 */
4300 static void
4301get_styled_font_variants(void)
4302{
4303 PangoFontDescription *bold_font_desc;
4304 PangoFont *plain_font;
4305 PangoFont *bold_font;
4306
4307 gui.font_can_bold = FALSE;
4308
4309 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4310
4311 if (plain_font == NULL)
4312 return;
4313
4314 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4315 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4316
4317 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4318 /*
4319 * The comparison relies on the unique handle nature of a PangoFont*,
4320 * i.e. it's assumed that a different PangoFont* won't refer to the
4321 * same font. Seems to work, and failing here isn't critical anyway.
4322 */
4323 if (bold_font != NULL)
4324 {
4325 gui.font_can_bold = (bold_font != plain_font);
4326 g_object_unref(bold_font);
4327 }
4328
4329 pango_font_description_free(bold_font_desc);
4330 g_object_unref(plain_font);
4331}
4332
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333static PangoEngineShape *default_shape_engine = NULL;
4334
4335/*
4336 * Create a map from ASCII characters in the range [32,126] to glyphs
4337 * of the current font. This is used by gui_gtk2_draw_string() to skip
4338 * the itemize and shaping process for the most common case.
4339 */
4340 static void
4341ascii_glyph_table_init(void)
4342{
4343 char_u ascii_chars[128];
4344 PangoAttrList *attr_list;
4345 GList *item_list;
4346 int i;
4347
4348 if (gui.ascii_glyphs != NULL)
4349 pango_glyph_string_free(gui.ascii_glyphs);
4350 if (gui.ascii_font != NULL)
4351 g_object_unref(gui.ascii_font);
4352
4353 gui.ascii_glyphs = NULL;
4354 gui.ascii_font = NULL;
4355
4356 /* For safety, fill in question marks for the control characters. */
4357 for (i = 0; i < 32; ++i)
4358 ascii_chars[i] = '?';
4359 for (; i < 127; ++i)
4360 ascii_chars[i] = i;
4361 ascii_chars[i] = '?';
4362
4363 attr_list = pango_attr_list_new();
4364 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4365 0, sizeof(ascii_chars), attr_list, NULL);
4366
4367 if (item_list != NULL && item_list->next == NULL) /* play safe */
4368 {
4369 PangoItem *item;
4370 int width;
4371
4372 item = (PangoItem *)item_list->data;
4373 width = gui.char_width * PANGO_SCALE;
4374
4375 /* Remember the shape engine used for ASCII. */
4376 default_shape_engine = item->analysis.shape_engine;
4377
4378 gui.ascii_font = item->analysis.font;
4379 g_object_ref(gui.ascii_font);
4380
4381 gui.ascii_glyphs = pango_glyph_string_new();
4382
4383 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4384 &item->analysis, gui.ascii_glyphs);
4385
4386 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4387
4388 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4389 {
4390 PangoGlyphGeometry *geom;
4391
4392 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4393 geom->x_offset += MAX(0, width - geom->width) / 2;
4394 geom->width = width;
4395 }
4396 }
4397
4398 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4399 g_list_free(item_list);
4400 pango_attr_list_unref(attr_list);
4401}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403/*
4404 * Initialize Vim to use the font or fontset with the given name.
4405 * Return FAIL if the font could not be loaded, OK otherwise.
4406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004408gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 PangoFontDescription *font_desc;
4411 PangoLayout *layout;
4412 int width;
4413
4414 /* If font_name is NULL, this means to use the default, which should
4415 * be present on all proper Pango/fontconfig installations. */
4416 if (font_name == NULL)
4417 font_name = (char_u *)DEFAULT_FONT;
4418
4419 font_desc = gui_mch_get_font(font_name, FALSE);
4420
4421 if (font_desc == NULL)
4422 return FAIL;
4423
4424 gui_mch_free_font(gui.norm_font);
4425 gui.norm_font = font_desc;
4426
4427 pango_context_set_font_description(gui.text_context, font_desc);
4428
4429 layout = pango_layout_new(gui.text_context);
4430 pango_layout_set_text(layout, "MW", 2);
4431 pango_layout_get_size(layout, &width, NULL);
4432 /*
4433 * Set char_width to half the width obtained from pango_layout_get_size()
4434 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4435 * Pango to use the same width for both non-CJK characters (e.g. Latin
4436 * letters and numbers) and CJK characters. This results in 's p a c e d
4437 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4438 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4439 * width for CJK fonts.
4440 *
4441 * For related bugs, see:
4442 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4443 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4444 *
4445 * With this, for all four of the following cases, Vim works fine:
4446 * guifont=CJK_fixed_width_font
4447 * guifont=Non_CJK_fixed_font
4448 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4449 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4450 */
4451 if (is_cjk_font(gui.norm_font))
4452 {
4453 int cjk_width;
4454
4455 /* Measure the text extent of U+4E00 and U+4E8C */
4456 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4457 pango_layout_get_size(layout, &cjk_width, NULL);
4458
4459 if (width == cjk_width) /* Xft not patched */
4460 width /= 2;
4461 }
4462 g_object_unref(layout);
4463
4464 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4465
4466 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4467 if (gui.char_width <= 0)
4468 gui.char_width = 8;
4469
Bram Moolenaar231334e2005-07-25 20:46:57 +00004470 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /* Set the fontname, which will be used for information purposes */
4473 hl_set_font_name(font_name);
4474
4475 get_styled_font_variants();
4476 ascii_glyph_table_init();
4477
4478 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4479 if (gui.wide_font != NULL
4480 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4481 {
4482 pango_font_description_free(gui.wide_font);
4483 gui.wide_font = NULL;
4484 }
4485
Bram Moolenaare161c792009-11-03 17:13:59 +00004486 if (gui_mch_maximized())
4487 {
Bram Moolenaare161c792009-11-03 17:13:59 +00004488 /* Update lines and columns in accordance with the new font, keep the
4489 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004490 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00004491 }
4492 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004493 {
4494 /* Preserve the logical dimensions of the screen. */
4495 update_window_manager_hints(0, 0);
4496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 return OK;
4499}
4500
4501/*
4502 * Get a reference to the font "name".
4503 * Return zero for failure.
4504 */
4505 GuiFont
4506gui_mch_get_font(char_u *name, int report_error)
4507{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509
4510 /* can't do this when GUI is not running */
4511 if (!gui.in_use || name == NULL)
4512 return NULL;
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 if (output_conv.vc_type != CONV_NONE)
4515 {
4516 char_u *buf;
4517
4518 buf = string_convert(&output_conv, name, NULL);
4519 if (buf != NULL)
4520 {
4521 font = pango_font_description_from_string((const char *)buf);
4522 vim_free(buf);
4523 }
4524 else
4525 font = NULL;
4526 }
4527 else
4528 font = pango_font_description_from_string((const char *)name);
4529
4530 if (font != NULL)
4531 {
4532 PangoFont *real_font;
4533
4534 /* pango_context_load_font() bails out if no font size is set */
4535 if (pango_font_description_get_size(font) <= 0)
4536 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4537
4538 real_font = pango_context_load_font(gui.text_context, font);
4539
4540 if (real_font == NULL)
4541 {
4542 pango_font_description_free(font);
4543 font = NULL;
4544 }
4545 else
4546 g_object_unref(real_font);
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 if (font == NULL)
4550 {
4551 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004552 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 return NULL;
4554 }
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 return font;
4557}
4558
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004559#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004560/*
4561 * Return the name of font "font" in allocated memory.
4562 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004563 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004564gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004565{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004566 if (font != NOFONT)
4567 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004568 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004569
Bram Moolenaar89d40322006-08-29 15:30:07 +00004570 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004571 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004572 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004573
Bram Moolenaar89d40322006-08-29 15:30:07 +00004574 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004575 return s;
4576 }
4577 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004578 return NULL;
4579}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004580#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582/*
4583 * If a font is not going to be used, free its structure.
4584 */
4585 void
4586gui_mch_free_font(GuiFont font)
4587{
4588 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590}
4591
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592/*
4593 * Return the Pixel value (color) for the given color name. This routine was
4594 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4595 * Programmer's Guide.
4596 * Return INVALCOLOR for error.
4597 */
4598 guicolor_T
4599gui_mch_get_color(char_u *name)
4600{
4601 /* A number of colors that some X11 systems don't have */
4602 static const char *const vimnames[][2] =
4603 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004604 {"LightRed", "#FFBBBB"},
4605 {"LightGreen", "#88FF88"},
4606 {"LightMagenta","#FFBBFF"},
4607 {"DarkCyan", "#008888"},
4608 {"DarkBlue", "#0000BB"},
4609 {"DarkRed", "#BB0000"},
4610 {"DarkMagenta", "#BB00BB"},
4611 {"DarkGrey", "#BBBBBB"},
4612 {"DarkYellow", "#BBBB00"},
4613 {"Gray10", "#1A1A1A"},
4614 {"Grey10", "#1A1A1A"},
4615 {"Gray20", "#333333"},
4616 {"Grey20", "#333333"},
4617 {"Gray30", "#4D4D4D"},
4618 {"Grey30", "#4D4D4D"},
4619 {"Gray40", "#666666"},
4620 {"Grey40", "#666666"},
4621 {"Gray50", "#7F7F7F"},
4622 {"Grey50", "#7F7F7F"},
4623 {"Gray60", "#999999"},
4624 {"Grey60", "#999999"},
4625 {"Gray70", "#B3B3B3"},
4626 {"Grey70", "#B3B3B3"},
4627 {"Gray80", "#CCCCCC"},
4628 {"Grey80", "#CCCCCC"},
4629 {"Gray90", "#E5E5E5"},
4630 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {NULL, NULL}
4632 };
4633
4634 if (!gui.in_use) /* can't do this when GUI not running */
4635 return INVALCOLOR;
4636
4637 while (name != NULL)
4638 {
4639 GdkColor color;
4640 int parsed;
4641 int i;
4642
4643 parsed = gdk_color_parse((const char *)name, &color);
4644
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (parsed)
4646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4648 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return (guicolor_T)color.pixel;
4650 }
4651 /* add a few builtin names and try again */
4652 for (i = 0; ; ++i)
4653 {
4654 if (vimnames[i][0] == NULL)
4655 {
4656 name = NULL;
4657 break;
4658 }
4659 if (STRICMP(name, vimnames[i][0]) == 0)
4660 {
4661 name = (char_u *)vimnames[i][1];
4662 break;
4663 }
4664 }
4665 }
4666
4667 return INVALCOLOR;
4668}
4669
4670/*
4671 * Set the current text foreground color.
4672 */
4673 void
4674gui_mch_set_fg_color(guicolor_T color)
4675{
4676 gui.fgcolor->pixel = (unsigned long)color;
4677}
4678
4679/*
4680 * Set the current text background color.
4681 */
4682 void
4683gui_mch_set_bg_color(guicolor_T color)
4684{
4685 gui.bgcolor->pixel = (unsigned long)color;
4686}
4687
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004688/*
4689 * Set the current text special color.
4690 */
4691 void
4692gui_mch_set_sp_color(guicolor_T color)
4693{
4694 gui.spcolor->pixel = (unsigned long)color;
4695}
4696
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697/*
4698 * Function-like convenience macro for the sake of efficiency.
4699 */
4700#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4701 G_STMT_START{ \
4702 PangoAttribute *tmp_attr_; \
4703 tmp_attr_ = (Attribute); \
4704 tmp_attr_->start_index = (Start); \
4705 tmp_attr_->end_index = (End); \
4706 pango_attr_list_insert((AttrList), tmp_attr_); \
4707 }G_STMT_END
4708
4709 static void
4710apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4711{
4712 char_u *start = NULL;
4713 char_u *p;
4714 int uc;
4715
4716 for (p = s; p < s + len; p += utf_byte2len(*p))
4717 {
4718 uc = utf_ptr2char(p);
4719
4720 if (start == NULL)
4721 {
4722 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4723 start = p;
4724 }
4725 else if (uc < 0x80 /* optimization shortcut */
4726 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4727 {
4728 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4729 attr_list, start - s, p - s);
4730 start = NULL;
4731 }
4732 }
4733
4734 if (start != NULL)
4735 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4736 attr_list, start - s, len);
4737}
4738
4739 static int
4740count_cluster_cells(char_u *s, PangoItem *item,
4741 PangoGlyphString* glyphs, int i,
4742 int *cluster_width,
4743 int *last_glyph_rbearing)
4744{
4745 char_u *p;
4746 int next; /* glyph start index of next cluster */
4747 int start, end; /* string segment of current cluster */
4748 int width; /* real cluster width in Pango units */
4749 int uc;
4750 int cellcount = 0;
4751
4752 width = glyphs->glyphs[i].geometry.width;
4753
4754 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4755 {
4756 if (glyphs->glyphs[next].attr.is_cluster_start)
4757 break;
4758 else if (glyphs->glyphs[next].geometry.width > width)
4759 width = glyphs->glyphs[next].geometry.width;
4760 }
4761
4762 start = item->offset + glyphs->log_clusters[i];
4763 end = item->offset + ((next < glyphs->num_glyphs) ?
4764 glyphs->log_clusters[next] : item->length);
4765
4766 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4767 {
4768 uc = utf_ptr2char(p);
4769 if (uc < 0x80)
4770 ++cellcount;
4771 else if (!utf_iscomposing(uc))
4772 cellcount += utf_char2cells(uc);
4773 }
4774
4775 if (last_glyph_rbearing != NULL
4776 && cellcount > 0 && next == glyphs->num_glyphs)
4777 {
4778 PangoRectangle ink_rect;
4779 /*
4780 * If a certain combining mark had to be taken from a non-monospace
4781 * font, we have to compensate manually by adapting x_offset according
4782 * to the ink extents of the previous glyph.
4783 */
4784 pango_font_get_glyph_extents(item->analysis.font,
4785 glyphs->glyphs[i].glyph,
4786 &ink_rect, NULL);
4787
4788 if (PANGO_RBEARING(ink_rect) > 0)
4789 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4790 }
4791
4792 if (cellcount > 0)
4793 *cluster_width = width;
4794
4795 return cellcount;
4796}
4797
4798/*
4799 * If there are only combining characters in the cluster, we cannot just
4800 * change the width of the previous glyph since there is none. Therefore
4801 * some guesswork is needed.
4802 *
4803 * If ink_rect.x is negative Pango apparently has taken care of the composing
4804 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4805 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004806 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 *
4808 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4809 * the position of the previous glyph. Apparently this happens only with old
4810 * X fonts which don't provide the special combining information needed by
4811 * Pango.
4812 */
4813 static void
4814setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4815 int last_cellcount, int last_cluster_width,
4816 int last_glyph_rbearing)
4817{
4818 PangoRectangle ink_rect;
4819 PangoRectangle logical_rect;
4820 int width;
4821
4822 width = last_cellcount * gui.char_width * PANGO_SCALE;
4823 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4824 glyph->geometry.width = 0;
4825
4826 pango_font_get_glyph_extents(item->analysis.font,
4827 glyph->glyph,
4828 &ink_rect, &logical_rect);
4829 if (ink_rect.x < 0)
4830 {
4831 glyph->geometry.x_offset += last_glyph_rbearing;
4832 glyph->geometry.y_offset = logical_rect.height
4833 - (gui.char_height - p_linespace) * PANGO_SCALE;
4834 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004835 else
4836 /* If the accent width is smaller than the cluster width, position it
4837 * in the middle. */
4838 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839}
4840
4841 static void
4842draw_glyph_string(int row, int col, int num_cells, int flags,
4843 PangoFont *font, PangoGlyphString *glyphs)
4844{
4845 if (!(flags & DRAW_TRANSP))
4846 {
4847 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4848
4849 gdk_draw_rectangle(gui.drawarea->window,
4850 gui.text_gc,
4851 TRUE,
4852 FILL_X(col),
4853 FILL_Y(row),
4854 num_cells * gui.char_width,
4855 gui.char_height);
4856 }
4857
4858 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4859
4860 gdk_draw_glyphs(gui.drawarea->window,
4861 gui.text_gc,
4862 font,
4863 TEXT_X(col),
4864 TEXT_Y(row),
4865 glyphs);
4866
4867 /* redraw the contents with an offset of 1 to emulate bold */
4868 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4869 gdk_draw_glyphs(gui.drawarea->window,
4870 gui.text_gc,
4871 font,
4872 TEXT_X(col) + 1,
4873 TEXT_Y(row),
4874 glyphs);
4875}
4876
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004877/*
4878 * Draw underline and undercurl at the bottom of the character cell.
4879 */
4880 static void
4881draw_under(int flags, int row, int col, int cells)
4882{
4883 int i;
4884 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004885 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004886 int y = FILL_Y(row + 1) - 1;
4887
4888 /* Undercurl: draw curl at the bottom of the character cell. */
4889 if (flags & DRAW_UNDERC)
4890 {
4891 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4892 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4893 {
4894 offset = val[i % 8];
4895 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4896 }
4897 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4898 }
4899
4900 /* Underline: draw a line at the bottom of the character cell. */
4901 if (flags & DRAW_UNDERL)
4902 {
4903 /* When p_linespace is 0, overwrite the bottom row of pixels.
4904 * Otherwise put the line just below the character. */
4905 if (p_linespace > 1)
4906 y -= p_linespace - 1;
4907 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4908 FILL_X(col), y,
4909 FILL_X(col + cells) - 1, y);
4910 }
4911}
4912
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 int
4914gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4915{
4916 GdkRectangle area; /* area for clip mask */
4917 PangoGlyphString *glyphs; /* glyphs of current item */
4918 int column_offset = 0; /* column offset in cells */
4919 int i;
4920 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4921 char_u *new_conv_buf;
4922 int convlen;
4923 char_u *sp, *bp;
4924 int plen;
4925
4926 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4927 return len;
4928
4929 if (output_conv.vc_type != CONV_NONE)
4930 {
4931 /*
4932 * Convert characters from 'encoding' to 'termencoding', which is set
4933 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4934 * prohibits changing this to something else than UTF-8 if the GUI is
4935 * in use.
4936 */
4937 convlen = len;
4938 conv_buf = string_convert(&output_conv, s, &convlen);
4939 g_return_val_if_fail(conv_buf != NULL, len);
4940
4941 /* Correct for differences in char width: some chars are
4942 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4943 * compensate for that. */
4944 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004946 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4948 {
4949 new_conv_buf = alloc(convlen + 2);
4950 if (new_conv_buf == NULL)
4951 return len;
4952 plen += bp - conv_buf;
4953 mch_memmove(new_conv_buf, conv_buf, plen);
4954 new_conv_buf[plen] = ' ';
4955 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4956 convlen - plen + 1);
4957 vim_free(conv_buf);
4958 conv_buf = new_conv_buf;
4959 ++convlen;
4960 bp = conv_buf + plen;
4961 plen = 1;
4962 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004963 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 bp += plen;
4965 }
4966 s = conv_buf;
4967 len = convlen;
4968 }
4969
4970 /*
4971 * Restrict all drawing to the current screen line in order to prevent
4972 * fuzzy font lookups from messing up the screen.
4973 */
4974 area.x = gui.border_offset;
4975 area.y = FILL_Y(row);
4976 area.width = gui.num_cols * gui.char_width;
4977 area.height = gui.char_height;
4978
4979 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4980 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4981
4982 glyphs = pango_glyph_string_new();
4983
4984 /*
4985 * Optimization hack: If possible, skip the itemize and shaping process
4986 * for pure ASCII strings. This optimization is particularly effective
4987 * because Vim draws space characters to clear parts of the screen.
4988 */
4989 if (!(flags & DRAW_ITALIC)
4990 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4991 && gui.ascii_glyphs != NULL)
4992 {
4993 char_u *p;
4994
4995 for (p = s; p < s + len; ++p)
4996 if (*p & 0x80)
4997 goto not_ascii;
4998
4999 pango_glyph_string_set_size(glyphs, len);
5000
5001 for (i = 0; i < len; ++i)
5002 {
5003 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5004 glyphs->log_clusters[i] = i;
5005 }
5006
5007 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5008
5009 column_offset = len;
5010 }
5011 else
5012not_ascii:
5013 {
5014 PangoAttrList *attr_list;
5015 GList *item_list;
5016 int cluster_width;
5017 int last_glyph_rbearing;
5018 int cells = 0; /* cells occupied by current cluster */
5019
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005020 /* Safety check: pango crashes when invoked with invalid utf-8
5021 * characters. */
5022 if (!utf_valid_string(s, s + len))
5023 {
5024 column_offset = len;
5025 goto skipitall;
5026 }
5027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 /* original width of the current cluster */
5029 cluster_width = PANGO_SCALE * gui.char_width;
5030
5031 /* right bearing of the last non-composing glyph */
5032 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5033
5034 attr_list = pango_attr_list_new();
5035
5036 /* If 'guifontwide' is set then use that for double-width characters.
5037 * Otherwise just go with 'guifont' and let Pango do its thing. */
5038 if (gui.wide_font != NULL)
5039 apply_wide_font_attr(s, len, attr_list);
5040
5041 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5042 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5043 attr_list, 0, len);
5044 if (flags & DRAW_ITALIC)
5045 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5046 attr_list, 0, len);
5047 /*
5048 * Break the text into segments with consistent directional level
5049 * and shaping engine. Pure Latin text needs only a single segment,
5050 * so there's no need to worry about the loop's efficiency. Better
5051 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5052 */
5053 item_list = pango_itemize(gui.text_context,
5054 (const char *)s, 0, len, attr_list, NULL);
5055
5056 while (item_list != NULL)
5057 {
5058 PangoItem *item;
5059 int item_cells = 0; /* item length in cells */
5060
5061 item = (PangoItem *)item_list->data;
5062 item_list = g_list_delete_link(item_list, item_list);
5063 /*
5064 * Increment the bidirectional embedding level by 1 if it is not
5065 * even. An odd number means the output will be RTL, but we don't
5066 * want that since Vim handles right-to-left text on its own. It
5067 * would probably be sufficient to just set level = 0, but you can
5068 * never know :)
5069 *
5070 * Unfortunately we can't take advantage of Pango's ability to
5071 * render both LTR and RTL at the same time. In order to support
5072 * that, Vim's main screen engine would have to make use of Pango
5073 * functionality.
5074 */
5075 item->analysis.level = (item->analysis.level + 1) & (~1U);
5076
5077 /* HACK: Overrule the shape engine, we don't want shaping to be
5078 * done, because drawing the cursor would change the display. */
5079 item->analysis.shape_engine = default_shape_engine;
5080
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02005081#ifdef HAVE_PANGO_SHAPE_FULL
Bram Moolenaar7e2ec002015-09-08 16:31:06 +02005082 pango_shape_full((const char *)s + item->offset, item->length,
5083 (const char *)s, len, &item->analysis, glyphs);
Bram Moolenaar3cbe0c02015-09-08 20:00:22 +02005084#else
5085 pango_shape((const char *)s + item->offset, item->length,
5086 &item->analysis, glyphs);
5087#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 /*
5089 * Fixed-width hack: iterate over the array and assign a fixed
5090 * width to each glyph, thus overriding the choice made by the
5091 * shaping engine. We use utf_char2cells() to determine the
5092 * number of cells needed.
5093 *
5094 * Also perform all kind of dark magic to get composing
5095 * characters right (and pretty too of course).
5096 */
5097 for (i = 0; i < glyphs->num_glyphs; ++i)
5098 {
5099 PangoGlyphInfo *glyph;
5100
5101 glyph = &glyphs->glyphs[i];
5102
5103 if (glyph->attr.is_cluster_start)
5104 {
5105 int cellcount;
5106
5107 cellcount = count_cluster_cells(
5108 s, item, glyphs, i, &cluster_width,
5109 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5110
5111 if (cellcount > 0)
5112 {
5113 int width;
5114
5115 width = cellcount * gui.char_width * PANGO_SCALE;
5116 glyph->geometry.x_offset +=
5117 MAX(0, width - cluster_width) / 2;
5118 glyph->geometry.width = width;
5119 }
5120 else
5121 {
5122 /* If there are only combining characters in the
5123 * cluster, we cannot just change the width of the
5124 * previous glyph since there is none. Therefore
5125 * some guesswork is needed. */
5126 setup_zero_width_cluster(item, glyph, cells,
5127 cluster_width,
5128 last_glyph_rbearing);
5129 }
5130
5131 item_cells += cellcount;
5132 cells = cellcount;
5133 }
5134 else if (i > 0)
5135 {
5136 int width;
5137
5138 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005139 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02005140 * In some circumstances Pango uses a positive x_offset,
5141 * then use the width of the previous glyph for this one
5142 * and set the previous width to zero.
5143 * Otherwise we get a negative x_offset, Pango has already
5144 * positioned the combining char, keep the widths as they
5145 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005146 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02005147 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005148 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02005149 {
5150 glyphs->glyphs[i].geometry.width =
5151 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005152 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02005153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 width = cells * gui.char_width * PANGO_SCALE;
5155 glyph->geometry.x_offset +=
5156 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 else /* i == 0 "cannot happen" */
5159 {
5160 glyph->geometry.width = 0;
5161 }
5162 }
5163
5164 /*** Aaaaand action! ***/
5165 draw_glyph_string(row, col + column_offset, item_cells,
5166 flags, item->analysis.font, glyphs);
5167
5168 pango_item_free(item);
5169
5170 column_offset += item_cells;
5171 }
5172
5173 pango_attr_list_unref(attr_list);
5174 }
5175
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005176skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005177 /* Draw underline and undercurl. */
5178 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 pango_glyph_string_free(glyphs);
5181 vim_free(conv_buf);
5182
5183 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5184
5185 return column_offset;
5186}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187
5188/*
5189 * Return OK if the key with the termcap name "name" is supported.
5190 */
5191 int
5192gui_mch_haskey(char_u *name)
5193{
5194 int i;
5195
5196 for (i = 0; special_keys[i].key_sym != 0; i++)
5197 if (name[0] == special_keys[i].code0
5198 && name[1] == special_keys[i].code1)
5199 return OK;
5200 return FAIL;
5201}
5202
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005203#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204/*
5205 * Return the text window-id and display. Only required for X-based GUI's
5206 */
5207 int
5208gui_get_x11_windis(Window *win, Display **dis)
5209{
5210 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5211 {
5212 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5213 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5214 return OK;
5215 }
5216
5217 *dis = NULL;
5218 *win = 0;
5219 return FAIL;
5220}
5221#endif
5222
5223#if defined(FEAT_CLIENTSERVER) \
5224 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5225
5226 Display *
5227gui_mch_get_display(void)
5228{
5229 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5230 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5231 else
5232 return NULL;
5233}
5234#endif
5235
5236 void
5237gui_mch_beep(void)
5238{
5239#ifdef HAVE_GTK_MULTIHEAD
5240 GdkDisplay *display;
5241
5242 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5243 display = gtk_widget_get_display(gui.mainwin);
5244 else
5245 display = gdk_display_get_default();
5246
5247 if (display != NULL)
5248 gdk_display_beep(display);
5249#else
5250 gdk_beep();
5251#endif
5252}
5253
5254 void
5255gui_mch_flash(int msec)
5256{
5257 GdkGCValues values;
5258 GdkGC *invert_gc;
5259
5260 if (gui.drawarea->window == NULL)
5261 return;
5262
5263 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5264 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5265 values.function = GDK_XOR;
5266 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5267 &values,
5268 GDK_GC_FOREGROUND |
5269 GDK_GC_BACKGROUND |
5270 GDK_GC_FUNCTION);
5271 gdk_gc_set_exposures(invert_gc,
5272 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5273 /*
5274 * Do a visual beep by changing back and forth in some undetermined way,
5275 * the foreground and background colors. This is due to the fact that
5276 * there can't be really any prediction about the effects of XOR on
5277 * arbitrary X11 servers. However this seems to be enough for what we
5278 * intend it to do.
5279 */
5280 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5281 TRUE,
5282 0, 0,
5283 FILL_X((int)Columns) + gui.border_offset,
5284 FILL_Y((int)Rows) + gui.border_offset);
5285
5286 gui_mch_flush();
5287 ui_delay((long)msec, TRUE); /* wait so many msec */
5288
5289 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5290 TRUE,
5291 0, 0,
5292 FILL_X((int)Columns) + gui.border_offset,
5293 FILL_Y((int)Rows) + gui.border_offset);
5294
5295 gdk_gc_destroy(invert_gc);
5296}
5297
5298/*
5299 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5300 */
5301 void
5302gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5303{
5304 GdkGCValues values;
5305 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 if (gui.drawarea->window == NULL)
5308 return;
5309
Bram Moolenaar89d40322006-08-29 15:30:07 +00005310 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5311 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 values.function = GDK_XOR;
5313 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5314 &values,
5315 GDK_GC_FOREGROUND |
5316 GDK_GC_BACKGROUND |
5317 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005318 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5319 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5321 TRUE,
5322 FILL_X(c), FILL_Y(r),
5323 (nc) * gui.char_width, (nr) * gui.char_height);
5324 gdk_gc_destroy(invert_gc);
5325}
5326
5327/*
5328 * Iconify the GUI window.
5329 */
5330 void
5331gui_mch_iconify(void)
5332{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334}
5335
5336#if defined(FEAT_EVAL) || defined(PROTO)
5337/*
5338 * Bring the Vim window to the foreground.
5339 */
5340 void
5341gui_mch_set_foreground(void)
5342{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344}
5345#endif
5346
5347/*
5348 * Draw a cursor without focus.
5349 */
5350 void
5351gui_mch_draw_hollow_cursor(guicolor_T color)
5352{
5353 int i = 1;
5354
5355 if (gui.drawarea->window == NULL)
5356 return;
5357
5358 gui_mch_set_fg_color(color);
5359
5360 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 if (mb_lefthalve(gui.row, gui.col))
5362 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5364 FALSE,
5365 FILL_X(gui.col), FILL_Y(gui.row),
5366 i * gui.char_width - 1, gui.char_height - 1);
5367}
5368
5369/*
5370 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5371 * color "color".
5372 */
5373 void
5374gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5375{
5376 if (gui.drawarea->window == NULL)
5377 return;
5378
5379 gui_mch_set_fg_color(color);
5380
5381 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5382 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5383 TRUE,
5384#ifdef FEAT_RIGHTLEFT
5385 /* vertical line should be on the right of current point */
5386 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5387#endif
5388 FILL_X(gui.col),
5389 FILL_Y(gui.row) + gui.char_height - h,
5390 w, h);
5391}
5392
5393
5394/*
5395 * Catch up with any queued X11 events. This may put keyboard input into the
5396 * input buffer, call resize call-backs, trigger timers etc. If there is
5397 * nothing in the X11 event queue (& no timers pending), then we return
5398 * immediately.
5399 */
5400 void
5401gui_mch_update(void)
5402{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005403 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5404 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405}
5406
5407 static gint
5408input_timer_cb(gpointer data)
5409{
5410 int *timed_out = (int *) data;
5411
Bram Moolenaar49325942007-05-10 19:19:59 +00005412 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 *timed_out = TRUE;
5414
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return FALSE; /* don't happen again */
5416}
5417
5418#ifdef FEAT_SNIFF
5419/*
5420 * Callback function, used when data is available on the SNiFF connection.
5421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 static void
5423sniff_request_cb(
Bram Moolenaar1357caf2016-02-01 21:40:14 +01005424 gpointer data UNUSED,
5425 gint source_fd UNUSED,
5426 GdkInputCondition condition UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427{
5428 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5429
5430 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431}
5432#endif
5433
5434/*
5435 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5436 * from the keyboard.
5437 * wtime == -1 Wait forever.
5438 * wtime == 0 This should never happen.
5439 * wtime > 0 Wait wtime milliseconds for a character.
5440 * Returns OK if a character was found to be available within the given time,
5441 * or FAIL otherwise.
5442 */
5443 int
5444gui_mch_wait_for_chars(long wtime)
5445{
5446 int focus;
5447 guint timer;
5448 static int timed_out;
5449#ifdef FEAT_SNIFF
5450 static int sniff_on = 0;
5451 static gint sniff_input_id = 0;
5452#endif
5453
5454#ifdef FEAT_SNIFF
5455 if (sniff_on && !want_sniff_request)
5456 {
5457 if (sniff_input_id)
5458 gdk_input_remove(sniff_input_id);
5459 sniff_on = 0;
5460 }
5461 else if (!sniff_on && want_sniff_request)
5462 {
5463 /* Add fd_from_sniff to watch for available data in main loop. */
5464 sniff_input_id = gdk_input_add(fd_from_sniff,
5465 GDK_INPUT_READ, sniff_request_cb, NULL);
5466 sniff_on = 1;
5467 }
5468#endif
5469
5470 timed_out = FALSE;
5471
5472 /* this timeout makes sure that we will return if no characters arrived in
5473 * time */
5474
5475 if (wtime > 0)
5476 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5477 else
5478 timer = 0;
5479
5480 focus = gui.in_focus;
5481
5482 do
5483 {
5484 /* Stop or start blinking when focus changes */
5485 if (gui.in_focus != focus)
5486 {
5487 if (gui.in_focus)
5488 gui_mch_start_blink();
5489 else
5490 gui_mch_stop_blink();
5491 focus = gui.in_focus;
5492 }
5493
Bram Moolenaar93c88e02015-09-15 14:12:05 +02005494#ifdef MESSAGE_QUEUE
5495 parse_queued_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005496#endif
5497
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 /*
5499 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005500 * Skip this if input is available anyway (can happen in rare
5501 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005503 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005504 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
5506 /* Got char, return immediately */
5507 if (input_available())
5508 {
5509 if (timer != 0 && !timed_out)
5510 gtk_timeout_remove(timer);
5511 return OK;
5512 }
5513 } while (wtime < 0 || !timed_out);
5514
5515 /*
5516 * Flush all eventually pending (drawing) events.
5517 */
5518 gui_mch_update();
5519
5520 return FAIL;
5521}
5522
5523
5524/****************************************************************************
5525 * Output drawing routines.
5526 ****************************************************************************/
5527
5528
5529/* Flush any output to the screen */
5530 void
5531gui_mch_flush(void)
5532{
5533#ifdef HAVE_GTK_MULTIHEAD
5534 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5535 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5536#else
5537 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 /* This happens to actually do what gui_mch_flush() is supposed to do,
5540 * according to the comment above. */
5541 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5542 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543}
5544
5545/*
5546 * Clear a rectangular region of the screen from text pos (row1, col1) to
5547 * (row2, col2) inclusive.
5548 */
5549 void
5550gui_mch_clear_block(int row1, int col1, int row2, int col2)
5551{
5552 GdkColor color;
5553
5554 if (gui.drawarea->window == NULL)
5555 return;
5556
5557 color.pixel = gui.back_pixel;
5558
5559 gdk_gc_set_foreground(gui.text_gc, &color);
5560
5561 /* Clear one extra pixel at the far right, for when bold characters have
5562 * spilled over to the window border. */
5563 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5564 FILL_X(col1), FILL_Y(row1),
5565 (col2 - col1 + 1) * gui.char_width
5566 + (col2 == Columns - 1),
5567 (row2 - row1 + 1) * gui.char_height);
5568}
5569
5570 void
5571gui_mch_clear_all(void)
5572{
5573 if (gui.drawarea->window != NULL)
5574 gdk_window_clear(gui.drawarea->window);
5575}
5576
5577/*
5578 * Redraw any text revealed by scrolling up/down.
5579 */
5580 static void
5581check_copy_area(void)
5582{
5583 GdkEvent *event;
5584 int expose_count;
5585
5586 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5587 return;
5588
5589 /* Avoid redrawing the cursor while scrolling or it'll end up where
5590 * we don't want it to be. I'm not sure if it's correct to call
5591 * gui_dont_update_cursor() at this point but it works as a quick
5592 * fix for now. */
5593 gui_dont_update_cursor();
5594
5595 do
5596 {
5597 /* Wait to check whether the scroll worked or not. */
5598 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5599
5600 if (event == NULL)
5601 break; /* received NoExpose event */
5602
5603 gui_redraw(event->expose.area.x, event->expose.area.y,
5604 event->expose.area.width, event->expose.area.height);
5605
5606 expose_count = event->expose.count;
5607 gdk_event_free(event);
5608 }
5609 while (expose_count > 0); /* more events follow */
5610
5611 gui_can_update_cursor();
5612}
5613
5614/*
5615 * Delete the given number of lines from the given row, scrolling up any
5616 * text further down within the scroll region.
5617 */
5618 void
5619gui_mch_delete_lines(int row, int num_lines)
5620{
5621 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5622 return; /* Can't see the window */
5623
5624 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5625 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5626
5627 /* copy one extra pixel, for when bold has spilled over */
5628 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5629 FILL_X(gui.scroll_region_left), FILL_Y(row),
5630 gui.drawarea->window,
5631 FILL_X(gui.scroll_region_left),
5632 FILL_Y(row + num_lines),
5633 gui.char_width * (gui.scroll_region_right
5634 - gui.scroll_region_left + 1) + 1,
5635 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5636
5637 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5638 gui.scroll_region_left,
5639 gui.scroll_region_bot, gui.scroll_region_right);
5640 check_copy_area();
5641}
5642
5643/*
5644 * Insert the given number of lines before the given row, scrolling down any
5645 * following text within the scroll region.
5646 */
5647 void
5648gui_mch_insert_lines(int row, int num_lines)
5649{
5650 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5651 return; /* Can't see the window */
5652
5653 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5654 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5655
5656 /* copy one extra pixel, for when bold has spilled over */
5657 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5658 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5659 gui.drawarea->window,
5660 FILL_X(gui.scroll_region_left), FILL_Y(row),
5661 gui.char_width * (gui.scroll_region_right
5662 - gui.scroll_region_left + 1) + 1,
5663 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5664
5665 gui_clear_block(row, gui.scroll_region_left,
5666 row + num_lines - 1, gui.scroll_region_right);
5667 check_copy_area();
5668}
5669
5670/*
5671 * X Selection stuff, for cutting and pasting text to other windows.
5672 */
5673 void
5674clip_mch_request_selection(VimClipboard *cbd)
5675{
5676 GdkAtom target;
5677 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005678 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5681 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005682 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5683 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 received_selection = RS_NONE;
5685 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5686
5687 gtk_selection_convert(gui.drawarea,
5688 cbd->gtk_sel_atom, target,
5689 (guint32)GDK_CURRENT_TIME);
5690
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005691 /* Hack: Wait up to three seconds for the selection. A hang was
5692 * noticed here when using the netrw plugin combined with ":gui"
5693 * during the FocusGained event. */
5694 start = time(NULL);
5695 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005696 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 if (received_selection != RS_FAIL)
5699 return;
5700 }
5701
5702 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005703 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704}
5705
5706/*
5707 * Disown the selection.
5708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005710clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005712 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714}
5715
5716/*
5717 * Own the selection and return OK if it worked.
5718 */
5719 int
5720clip_mch_own_selection(VimClipboard *cbd)
5721{
5722 int success;
5723
5724 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02005725 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 gui_mch_update();
5727 return (success) ? OK : FAIL;
5728}
5729
5730/*
5731 * Send the current selection to the clipboard. Do nothing for X because we
5732 * will fill in the selection only when requested by another app.
5733 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005735clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737}
5738
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005739 int
5740clip_gtk_owner_exists(VimClipboard *cbd)
5741{
5742 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
5743}
5744
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745
5746#if defined(FEAT_MENU) || defined(PROTO)
5747/*
5748 * Make a menu item appear either active or not active (grey or not grey).
5749 */
5750 void
5751gui_mch_menu_grey(vimmenu_T *menu, int grey)
5752{
5753 if (menu->id == NULL)
5754 return;
5755
5756 if (menu_is_separator(menu->name))
5757 grey = TRUE;
5758
5759 gui_mch_menu_hidden(menu, FALSE);
5760 /* Be clever about bitfields versus true booleans here! */
5761 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5762 {
5763 gtk_widget_set_sensitive(menu->id, !grey);
5764 gui_mch_update();
5765 }
5766}
5767
5768/*
5769 * Make menu item hidden or not hidden.
5770 */
5771 void
5772gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5773{
5774 if (menu->id == 0)
5775 return;
5776
5777 if (hidden)
5778 {
5779 if (GTK_WIDGET_VISIBLE(menu->id))
5780 {
5781 gtk_widget_hide(menu->id);
5782 gui_mch_update();
5783 }
5784 }
5785 else
5786 {
5787 if (!GTK_WIDGET_VISIBLE(menu->id))
5788 {
5789 gtk_widget_show(menu->id);
5790 gui_mch_update();
5791 }
5792 }
5793}
5794
5795/*
5796 * This is called after setting all the menus to grey/hidden or not.
5797 */
5798 void
5799gui_mch_draw_menubar(void)
5800{
5801 /* just make sure that the visual changes get effect immediately */
5802 gui_mch_update();
5803}
5804#endif /* FEAT_MENU */
5805
5806/*
5807 * Scrollbar stuff.
5808 */
5809 void
5810gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5811{
5812 if (sb->id == NULL)
5813 return;
5814
5815 if (flag)
5816 gtk_widget_show(sb->id);
5817 else
5818 gtk_widget_hide(sb->id);
5819
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005820 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821}
5822
5823
5824/*
5825 * Return the RGB value of a pixel as long.
5826 */
5827 long_u
5828gui_mch_get_rgb(guicolor_T pixel)
5829{
5830 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5832 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834 return (((unsigned)color.red & 0xff00) << 8)
5835 | ((unsigned)color.green & 0xff00)
5836 | (((unsigned)color.blue & 0xff00) >> 8);
5837}
5838
5839/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005840 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005842 void
5843gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005845 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846}
5847
5848 void
5849gui_mch_setmouse(int x, int y)
5850{
5851 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5852 * internal GDK mechanism present to accomplish this. (and for good
5853 * reason...) */
5854 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5855 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5856 0, 0, 0U, 0U, x, y);
5857}
5858
5859
5860#ifdef FEAT_MOUSESHAPE
5861/* The last set mouse pointer shape is remembered, to be used when it goes
5862 * from hidden to not hidden. */
5863static int last_shape = 0;
5864#endif
5865
5866/*
5867 * Use the blank mouse pointer or not.
5868 *
5869 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5870 */
5871 void
5872gui_mch_mousehide(int hide)
5873{
5874 if (gui.pointer_hidden != hide)
5875 {
5876 gui.pointer_hidden = hide;
5877 if (gui.drawarea->window && gui.blank_pointer != NULL)
5878 {
5879 if (hide)
5880 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5881 else
5882#ifdef FEAT_MOUSESHAPE
5883 mch_set_mouse_shape(last_shape);
5884#else
5885 gdk_window_set_cursor(gui.drawarea->window, NULL);
5886#endif
5887 }
5888 }
5889}
5890
5891#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5892
5893/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5894 * misc2.c! */
5895static const int mshape_ids[] =
5896{
5897 GDK_LEFT_PTR, /* arrow */
5898 GDK_CURSOR_IS_PIXMAP, /* blank */
5899 GDK_XTERM, /* beam */
5900 GDK_SB_V_DOUBLE_ARROW, /* updown */
5901 GDK_SIZING, /* udsizing */
5902 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5903 GDK_SIZING, /* lrsizing */
5904 GDK_WATCH, /* busy */
5905 GDK_X_CURSOR, /* no */
5906 GDK_CROSSHAIR, /* crosshair */
5907 GDK_HAND1, /* hand1 */
5908 GDK_HAND2, /* hand2 */
5909 GDK_PENCIL, /* pencil */
5910 GDK_QUESTION_ARROW, /* question */
5911 GDK_RIGHT_PTR, /* right-arrow */
5912 GDK_CENTER_PTR, /* up-arrow */
5913 GDK_LEFT_PTR /* last one */
5914};
5915
5916 void
5917mch_set_mouse_shape(int shape)
5918{
5919 int id;
5920 GdkCursor *c;
5921
5922 if (gui.drawarea->window == NULL)
5923 return;
5924
5925 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5926 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5927 else
5928 {
5929 if (shape >= MSHAPE_NUMBERED)
5930 {
5931 id = shape - MSHAPE_NUMBERED;
5932 if (id >= GDK_LAST_CURSOR)
5933 id = GDK_LEFT_PTR;
5934 else
5935 id &= ~1; /* they are always even (why?) */
5936 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005937 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005939 else
5940 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941# ifdef HAVE_GTK_MULTIHEAD
5942 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005943 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005945 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946# endif
5947 gdk_window_set_cursor(gui.drawarea->window, c);
5948 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5949 }
5950 if (shape != MSHAPE_HIDE)
5951 last_shape = shape;
5952}
5953#endif /* FEAT_MOUSESHAPE */
5954
5955
5956#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5957/*
5958 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5959 * scaled down if the current font is not big enough, or scaled up if the image
5960 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5961 * will be cut off if the current font is not big enough, or centered if it's
5962 * too small.
5963 */
5964# define SIGN_WIDTH (2 * gui.char_width)
5965# define SIGN_HEIGHT (gui.char_height)
5966# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5967
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968 void
5969gui_mch_drawsign(int row, int col, int typenr)
5970{
5971 GdkPixbuf *sign;
5972
5973 sign = (GdkPixbuf *)sign_get_image(typenr);
5974
5975 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5976 {
5977 int width;
5978 int height;
5979 int xoffset;
5980 int yoffset;
5981 int need_scale;
5982
5983 width = gdk_pixbuf_get_width(sign);
5984 height = gdk_pixbuf_get_height(sign);
5985 /*
5986 * Decide whether we need to scale. Allow one pixel of border
5987 * width to be cut off, in order to avoid excessive scaling for
5988 * tiny differences in font size.
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005989 * Do scale to fit the height to avoid gaps because of linespacing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991 need_scale = (width > SIGN_WIDTH + 2
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005992 || height != SIGN_HEIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 || (width < 3 * SIGN_WIDTH / 4
5994 && height < 3 * SIGN_HEIGHT / 4));
5995 if (need_scale)
5996 {
Bram Moolenaar58cbc912014-06-17 18:47:02 +02005997 double aspect;
5998 int w = width;
5999 int h = height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 /* Keep the original aspect ratio */
6002 aspect = (double)height / (double)width;
6003 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
6004 width = MIN(width, SIGN_WIDTH);
Bram Moolenaar58cbc912014-06-17 18:47:02 +02006005 if (((double)(MAX(height, SIGN_HEIGHT)) /
6006 (double)(MIN(height, SIGN_HEIGHT))) < 1.15)
6007 {
6008 /* Change the aspect ratio by at most 15% to fill the
6009 * available space completly. */
6010 height = (double)SIGN_HEIGHT * SIGN_ASPECT / aspect;
6011 height = MIN(height, SIGN_HEIGHT);
6012 }
6013 else
6014 height = (double)width * aspect;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar58cbc912014-06-17 18:47:02 +02006016 if (w == width && h == height)
6017 {
6018 /* no change in dimensions; don't decrease reference counter
6019 * (below) */
6020 need_scale = FALSE;
6021 }
6022 else
6023 {
6024 /* This doesn't seem to be worth caching, and doing so would
6025 * complicate the code quite a bit. */
6026 sign = gdk_pixbuf_scale_simple(sign, width, height,
6027 GDK_INTERP_BILINEAR);
6028 if (sign == NULL)
6029 return; /* out of memory */
6030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032
6033 /* The origin is the upper-left corner of the pixmap. Therefore
6034 * these offset may become negative if the pixmap is smaller than
6035 * the 2x1 cells reserved for the sign icon. */
6036 xoffset = (width - SIGN_WIDTH) / 2;
6037 yoffset = (height - SIGN_HEIGHT) / 2;
6038
6039 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6040
6041 gdk_draw_rectangle(gui.drawarea->window,
6042 gui.text_gc,
6043 TRUE,
6044 FILL_X(col),
6045 FILL_Y(row),
6046 SIGN_WIDTH,
6047 SIGN_HEIGHT);
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 gdk_pixbuf_render_to_drawable_alpha(sign,
6050 gui.drawarea->window,
6051 MAX(0, xoffset),
6052 MAX(0, yoffset),
6053 FILL_X(col) - MIN(0, xoffset),
6054 FILL_Y(row) - MIN(0, yoffset),
6055 MIN(width, SIGN_WIDTH),
6056 MIN(height, SIGN_HEIGHT),
6057 GDK_PIXBUF_ALPHA_BILEVEL,
6058 127,
6059 GDK_RGB_DITHER_NORMAL,
6060 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 if (need_scale)
6062 g_object_unref(sign);
6063 }
6064}
6065
6066 void *
6067gui_mch_register_sign(char_u *signfile)
6068{
6069 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6070 {
6071 GdkPixbuf *sign;
6072 GError *error = NULL;
6073 char_u *message;
6074
6075 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6076
6077 if (error == NULL)
6078 return sign;
6079
6080 message = (char_u *)error->message;
6081
6082 if (message != NULL && input_conv.vc_type != CONV_NONE)
6083 message = string_convert(&input_conv, message, NULL);
6084
6085 if (message != NULL)
6086 {
6087 /* The error message is already translated and will be more
6088 * descriptive than anything we could possibly do ourselves. */
6089 EMSG2("E255: %s", message);
6090
6091 if (input_conv.vc_type != CONV_NONE)
6092 vim_free(message);
6093 }
6094 g_error_free(error);
6095 }
6096
6097 return NULL;
6098}
6099
6100 void
6101gui_mch_destroy_sign(void *sign)
6102{
6103 if (sign != NULL)
6104 g_object_unref(sign);
6105}
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107#endif /* FEAT_SIGN_ICONS */