blob: 5c6e831a750fb6f647cf0f8bec217225f6a331df [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 Moolenaarc93e7912008-07-08 10:46:08 +000025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef FEAT_GUI_GNOME
27/* Gnome redefines _() and N_(). Grrr... */
28# ifdef _
29# undef _
30# endif
31# ifdef N_
32# undef N_
33# endif
34# ifdef textdomain
35# undef textdomain
36# endif
37# ifdef bindtextdomain
38# undef bindtextdomain
39# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000040# ifdef bind_textdomain_codeset
41# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
44# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
45# endif
46# include <gnome.h>
47# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000048/* missing prototype in bonobo-dock-item.h */
49extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
51
52#if !defined(FEAT_GUI_GTK) && defined(PROTO)
53/* When generating prototypes we don't want syntax errors. */
54# define GdkAtom int
55# define GdkEventExpose int
56# define GdkEventFocus int
57# define GdkEventVisibility int
58# define GdkEventProperty int
59# define GtkContainer int
60# define GtkTargetEntry int
61# define GtkType int
62# define GtkWidget int
63# define gint int
64# define gpointer int
65# define guint int
66# define GdkEventKey int
67# define GdkEventSelection int
68# define GtkSelectionData int
69# define GdkEventMotion int
70# define GdkEventButton int
71# define GdkDragContext int
72# define GdkEventConfigure int
73# define GdkEventClient int
74#else
75# include <gdk/gdkkeysyms.h>
76# include <gdk/gdk.h>
77# ifdef WIN3264
78# include <gdk/gdkwin32.h>
79# else
80# include <gdk/gdkx.h>
81# endif
82
83# include <gtk/gtk.h>
84# include "gui_gtk_f.h"
85#endif
86
87#ifdef HAVE_X11_SUNKEYSYM_H
88# include <X11/Sunkeysym.h>
89#endif
90
Bram Moolenaarede547a2011-02-25 15:15:50 +010091static guint32 clipboard_event_time = CurrentTime;
92
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/*
94 * Easy-to-use macro for multihead support.
95 */
96#ifdef HAVE_GTK_MULTIHEAD
97# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
98 gtk_widget_get_display(gui.mainwin), atom)
99#else
100# define GET_X_ATOM(atom) ((Atom)(atom))
101#endif
102
103/* Selection type distinguishers */
104enum
105{
106 TARGET_TYPE_NONE,
107 TARGET_UTF8_STRING,
108 TARGET_STRING,
109 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000110 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 TARGET_TEXT,
112 TARGET_TEXT_URI_LIST,
113 TARGET_TEXT_PLAIN,
114 TARGET_VIM,
115 TARGET_VIMENC
116};
117
118/*
119 * Table of selection targets supported by Vim.
120 * Note: Order matters, preferred types should come first.
121 */
122static const GtkTargetEntry selection_targets[] =
123{
124 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
125 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000126 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
129 {"TEXT", 0, TARGET_TEXT},
130 {"STRING", 0, TARGET_STRING}
131};
132#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
133
134#ifdef FEAT_DND
135/*
136 * Table of DnD targets supported by Vim.
137 * Note: Order matters, preferred types should come first.
138 */
139static const GtkTargetEntry dnd_targets[] =
140{
141 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000142 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144 {"STRING", 0, TARGET_STRING},
145 {"text/plain", 0, TARGET_TEXT_PLAIN}
146};
147# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
148#endif
149
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * "Monospace" is a standard font alias that should be present
153 * on all proper Pango/fontconfig installations.
154 */
155# define DEFAULT_FONT "Monospace 10"
156
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
158/*
159 * Atoms used to communicate save-yourself from the X11 session manager. There
160 * is no need to move them into the GUI struct, since they should be constant.
161 */
162static GdkAtom wm_protocols_atom = GDK_NONE;
163static GdkAtom save_yourself_atom = GDK_NONE;
164#endif
165
166/*
167 * Atoms used to control/reference X11 selections.
168 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000169static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174/*
175 * Keycodes recognized by vim.
176 * NOTE: when changing this, the table in gui_x11.c probably needs the same
177 * change!
178 */
179static struct special_key
180{
181 guint key_sym;
182 char_u code0;
183 char_u code1;
184}
185const special_keys[] =
186{
187 {GDK_Up, 'k', 'u'},
188 {GDK_Down, 'k', 'd'},
189 {GDK_Left, 'k', 'l'},
190 {GDK_Right, 'k', 'r'},
191 {GDK_F1, 'k', '1'},
192 {GDK_F2, 'k', '2'},
193 {GDK_F3, 'k', '3'},
194 {GDK_F4, 'k', '4'},
195 {GDK_F5, 'k', '5'},
196 {GDK_F6, 'k', '6'},
197 {GDK_F7, 'k', '7'},
198 {GDK_F8, 'k', '8'},
199 {GDK_F9, 'k', '9'},
200 {GDK_F10, 'k', ';'},
201 {GDK_F11, 'F', '1'},
202 {GDK_F12, 'F', '2'},
203 {GDK_F13, 'F', '3'},
204 {GDK_F14, 'F', '4'},
205 {GDK_F15, 'F', '5'},
206 {GDK_F16, 'F', '6'},
207 {GDK_F17, 'F', '7'},
208 {GDK_F18, 'F', '8'},
209 {GDK_F19, 'F', '9'},
210 {GDK_F20, 'F', 'A'},
211 {GDK_F21, 'F', 'B'},
212 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
213 {GDK_F22, 'F', 'C'},
214 {GDK_F23, 'F', 'D'},
215 {GDK_F24, 'F', 'E'},
216 {GDK_F25, 'F', 'F'},
217 {GDK_F26, 'F', 'G'},
218 {GDK_F27, 'F', 'H'},
219 {GDK_F28, 'F', 'I'},
220 {GDK_F29, 'F', 'J'},
221 {GDK_F30, 'F', 'K'},
222 {GDK_F31, 'F', 'L'},
223 {GDK_F32, 'F', 'M'},
224 {GDK_F33, 'F', 'N'},
225 {GDK_F34, 'F', 'O'},
226 {GDK_F35, 'F', 'P'},
227#ifdef SunXK_F36
228 {SunXK_F36, 'F', 'Q'},
229 {SunXK_F37, 'F', 'R'},
230#endif
231 {GDK_Help, '%', '1'},
232 {GDK_Undo, '&', '8'},
233 {GDK_BackSpace, 'k', 'b'},
234 {GDK_Insert, 'k', 'I'},
235 {GDK_Delete, 'k', 'D'},
236 {GDK_3270_BackTab, 'k', 'B'},
237 {GDK_Clear, 'k', 'C'},
238 {GDK_Home, 'k', 'h'},
239 {GDK_End, '@', '7'},
240 {GDK_Prior, 'k', 'P'},
241 {GDK_Next, 'k', 'N'},
242 {GDK_Print, '%', '9'},
243 /* Keypad keys: */
244 {GDK_KP_Left, 'k', 'l'},
245 {GDK_KP_Right, 'k', 'r'},
246 {GDK_KP_Up, 'k', 'u'},
247 {GDK_KP_Down, 'k', 'd'},
248 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
249 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
250 {GDK_KP_Home, 'K', '1'},
251 {GDK_KP_End, 'K', '4'},
252 {GDK_KP_Prior, 'K', '3'}, /* page up */
253 {GDK_KP_Next, 'K', '5'}, /* page down */
254
255 {GDK_KP_Add, 'K', '6'},
256 {GDK_KP_Subtract, 'K', '7'},
257 {GDK_KP_Divide, 'K', '8'},
258 {GDK_KP_Multiply, 'K', '9'},
259 {GDK_KP_Enter, 'K', 'A'},
260 {GDK_KP_Decimal, 'K', 'B'},
261
262 {GDK_KP_0, 'K', 'C'},
263 {GDK_KP_1, 'K', 'D'},
264 {GDK_KP_2, 'K', 'E'},
265 {GDK_KP_3, 'K', 'F'},
266 {GDK_KP_4, 'K', 'G'},
267 {GDK_KP_5, 'K', 'H'},
268 {GDK_KP_6, 'K', 'I'},
269 {GDK_KP_7, 'K', 'J'},
270 {GDK_KP_8, 'K', 'K'},
271 {GDK_KP_9, 'K', 'L'},
272
273 /* End of list marker: */
274 {0, 0, 0}
275};
276
277/*
278 * Flags for command line options table below.
279 */
280#define ARG_FONT 1
281#define ARG_GEOMETRY 2
282#define ARG_REVERSE 3
283#define ARG_NOREVERSE 4
284#define ARG_BACKGROUND 5
285#define ARG_FOREGROUND 6
286#define ARG_ICONIC 7
287#define ARG_ROLE 8
288#define ARG_NETBEANS 9
289#define ARG_XRM 10 /* ignored */
290#define ARG_MENUFONT 11 /* ignored */
291#define ARG_INDEX_MASK 0x00ff
292#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
293#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
294#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
295#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
296#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
297
298/*
299 * This table holds all the X GUI command line options allowed. This includes
300 * the standard ones so that we can skip them when Vim is started without the
301 * GUI (but the GUI might start up later).
302 *
303 * When changing this, also update doc/gui_x11.txt and the usage message!!!
304 */
305typedef struct
306{
307 const char *name;
308 unsigned int flags;
309}
310cmdline_option_T;
311
312static const cmdline_option_T cmdline_options[] =
313{
314 /* We handle these options ourselves */
315 {"-fn", ARG_FONT|ARG_HAS_VALUE},
316 {"-font", ARG_FONT|ARG_HAS_VALUE},
317 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
318 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
319 {"-rv", ARG_REVERSE},
320 {"-reverse", ARG_REVERSE},
321 {"+rv", ARG_NOREVERSE},
322 {"+reverse", ARG_NOREVERSE},
323 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
324 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
325 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
326 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
327 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_NETBEANS_INTG
330 {"-nb", ARG_NETBEANS}, /* non-standard value format */
331 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
332 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
333 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
334#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 /* Arguments handled by GTK (and GNOME) internally. */
336 {"--g-fatal-warnings", ARG_FOR_GTK},
337 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
338 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
339 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
340 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
341 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
342 {"--sync", ARG_FOR_GTK},
343 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
344 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
345 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
348 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#ifdef FEAT_GUI_GNOME
350 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--sm-disable", ARG_FOR_GTK},
354 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
355 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
356 {"--oaf-private", ARG_FOR_GTK},
357 {"--enable-sound", ARG_FOR_GTK},
358 {"--disable-sound", ARG_FOR_GTK},
359 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
360 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
361 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
362 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
363# if 0 /* conflicts with Vim's own --version argument */
364 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
365# endif
366 {"--disable-crash-dialog", ARG_FOR_GTK},
367#endif
368 {NULL, 0}
369};
370
371static int gui_argc = 0;
372static char **gui_argv = NULL;
373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
376static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000377static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378#endif
379static int found_iconic_arg = FALSE;
380
381#ifdef FEAT_GUI_GNOME
382/*
383 * Can't use Gnome if --socketid given
384 */
385static int using_gnome = 0;
386#else
387# define using_gnome 0
388#endif
389
390/*
391 * Parse the GUI related command-line arguments. Any arguments used are
392 * deleted from argv, and *argc is decremented accordingly. This is called
393 * when vim is started, whether or not the GUI has been started.
394 */
395 void
396gui_mch_prepare(int *argc, char **argv)
397{
398 const cmdline_option_T *option;
399 int i = 0;
400 int len = 0;
401
402#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
403 /*
404 * Determine the command used to invoke Vim, to be passed as restart
405 * command to the session manager. If argv[0] contains any directory
406 * components try building an absolute path, otherwise leave it as is.
407 */
408 restart_command = argv[0];
409
410 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
411 {
412 char_u buf[MAXPATHL];
413
414 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000415 {
416 abs_restart_command = (char *)vim_strsave(buf);
417 restart_command = abs_restart_command;
418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 }
420#endif
421
422 /*
423 * Move all the entries in argv which are relevant to GTK+ and GNOME
424 * into gui_argv. Freed later in gui_mch_init().
425 */
426 gui_argc = 0;
427 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
428
429 g_return_if_fail(gui_argv != NULL);
430
431 gui_argv[gui_argc++] = argv[i++];
432
433 while (i < *argc)
434 {
435 /* Don't waste CPU cycles on non-option arguments. */
436 if (argv[i][0] != '-' && argv[i][0] != '+')
437 {
438 ++i;
439 continue;
440 }
441
442 /* Look for argv[i] in cmdline_options[] table. */
443 for (option = &cmdline_options[0]; option->name != NULL; ++option)
444 {
445 len = strlen(option->name);
446
447 if (strncmp(argv[i], option->name, len) == 0)
448 {
449 if (argv[i][len] == '\0')
450 break;
451 /* allow --foo=bar style */
452 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
453 break;
454#ifdef FEAT_NETBEANS_INTG
455 /* darn, -nb has non-standard syntax */
456 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
457 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
458 break;
459#endif
460 }
461 else if ((option->flags & ARG_COMPAT_LONG)
462 && strcmp(argv[i], option->name + 1) == 0)
463 {
464 /* Replace the standard X arguments "-name" and "-display"
465 * with their GNU-style long option counterparts. */
466 argv[i] = (char *)option->name;
467 break;
468 }
469 }
470 if (option->name == NULL) /* no match */
471 {
472 ++i;
473 continue;
474 }
475
476 if (option->flags & ARG_FOR_GTK)
477 {
478 /* Move the argument into gui_argv, which
479 * will later be passed to gtk_init_check() */
480 gui_argv[gui_argc++] = argv[i];
481 }
482 else
483 {
484 char *value = NULL;
485
486 /* Extract the option's value if there is one.
487 * Accept both "--foo bar" and "--foo=bar" style. */
488 if (option->flags & ARG_HAS_VALUE)
489 {
490 if (argv[i][len] == '=')
491 value = &argv[i][len + 1];
492 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
493 value = argv[i + 1];
494 }
495
496 /* Check for options handled by Vim itself */
497 switch (option->flags & ARG_INDEX_MASK)
498 {
499 case ARG_REVERSE:
500 found_reverse_arg = TRUE;
501 break;
502 case ARG_NOREVERSE:
503 found_reverse_arg = FALSE;
504 break;
505 case ARG_FONT:
506 font_argument = value;
507 break;
508 case ARG_GEOMETRY:
509 if (value != NULL)
510 gui.geom = vim_strsave((char_u *)value);
511 break;
512 case ARG_BACKGROUND:
513 background_argument = value;
514 break;
515 case ARG_FOREGROUND:
516 foreground_argument = value;
517 break;
518 case ARG_ICONIC:
519 found_iconic_arg = TRUE;
520 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 case ARG_ROLE:
522 role_argument = value; /* used later in gui_mch_open() */
523 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524#ifdef FEAT_NETBEANS_INTG
525 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 gui.dofork = FALSE; /* don't fork() when starting GUI */
527 netbeansArg = argv[i];
528 break;
529#endif
530 default:
531 break;
532 }
533 }
534
535 /* These arguments make gnome_program_init() print a message and exit.
536 * Must start the GUI for this, otherwise ":gui" will exit later! */
537 if (option->flags & ARG_NEEDS_GUI)
538 gui.starting = TRUE;
539
540 if (option->flags & ARG_KEEP)
541 ++i;
542 else
543 {
544 /* Remove the flag from the argument vector. */
545 if (--*argc > i)
546 {
547 int n_strip = 1;
548
549 /* Move the argument's value as well, if there is one. */
550 if ((option->flags & ARG_HAS_VALUE)
551 && argv[i][len] != '='
552 && strcmp(argv[i + 1], "--") != 0)
553 {
554 ++n_strip;
555 --*argc;
556 if (option->flags & ARG_FOR_GTK)
557 gui_argv[gui_argc++] = argv[i + 1];
558 }
559
560 if (*argc > i)
561 mch_memmove(&argv[i], &argv[i + n_strip],
562 (*argc - i) * sizeof(char *));
563 }
564 argv[*argc] = NULL;
565 }
566 }
567
568 gui_argv[gui_argc] = NULL;
569}
570
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000571#if defined(EXITFREE) || defined(PROTO)
572 void
573gui_mch_free_all()
574{
575 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000576#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
577 vim_free(abs_restart_command);
578#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000579}
580#endif
581
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582/*
583 * This should be maybe completely removed.
584 * Doesn't seem possible, since check_copy_area() relies on
585 * this information. --danielk
586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000588visibility_event(GtkWidget *widget UNUSED,
589 GdkEventVisibility *event,
590 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 gui.visibility = event->state;
593 /*
594 * When we do an gdk_window_copy_area(), and the window is partially
595 * obscured, we want to receive an event to tell us whether it worked
596 * or not.
597 */
598 if (gui.text_gc != NULL)
599 gdk_gc_set_exposures(gui.text_gc,
600 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
601 return FALSE;
602}
603
604/*
605 * Redraw the corresponding portions of the screen.
606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000608expose_event(GtkWidget *widget UNUSED,
609 GdkEventExpose *event,
610 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611{
612 /* Skip this when the GUI isn't set up yet, will redraw later. */
613 if (gui.starting)
614 return FALSE;
615
616 out_flush(); /* make sure all output has been processed */
617 gui_redraw(event->area.x, event->area.y,
618 event->area.width, event->area.height);
619
620 /* Clear the border areas if needed */
621 if (event->area.x < FILL_X(0))
622 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
623 if (event->area.y < FILL_Y(0))
624 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
625 if (event->area.x > FILL_X(Columns))
626 gdk_window_clear_area(gui.drawarea->window,
627 FILL_X((int)Columns), 0, 0, 0);
628 if (event->area.y > FILL_Y(Rows))
629 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
630
631 return FALSE;
632}
633
634#ifdef FEAT_CLIENTSERVER
635/*
636 * Handle changes to the "Comm" property
637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000639property_event(GtkWidget *widget,
640 GdkEventProperty *event,
641 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 if (event->type == GDK_PROPERTY_NOTIFY
644 && event->state == (int)GDK_PROPERTY_NEW_VALUE
645 && GDK_WINDOW_XWINDOW(event->window) == commWindow
646 && GET_X_ATOM(event->atom) == commProperty)
647 {
648 XEvent xev;
649
650 /* Translate to XLib */
651 xev.xproperty.type = PropertyNotify;
652 xev.xproperty.atom = commProperty;
653 xev.xproperty.window = commWindow;
654 xev.xproperty.state = PropertyNewValue;
655 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 }
657 return FALSE;
658}
659#endif
660
661
662/****************************************************************************
663 * Focus handlers:
664 */
665
666
667/*
668 * This is a simple state machine:
669 * BLINK_NONE not blinking at all
670 * BLINK_OFF blinking, cursor is not shown
671 * BLINK_ON blinking, cursor is shown
672 */
673
674#define BLINK_NONE 0
675#define BLINK_OFF 1
676#define BLINK_ON 2
677
678static int blink_state = BLINK_NONE;
679static long_u blink_waittime = 700;
680static long_u blink_ontime = 400;
681static long_u blink_offtime = 250;
682static guint blink_timer = 0;
683
684 void
685gui_mch_set_blinking(long waittime, long on, long off)
686{
687 blink_waittime = waittime;
688 blink_ontime = on;
689 blink_offtime = off;
690}
691
692/*
693 * Stop the cursor blinking. Show the cursor if it wasn't shown.
694 */
695 void
696gui_mch_stop_blink(void)
697{
698 if (blink_timer)
699 {
700 gtk_timeout_remove(blink_timer);
701 blink_timer = 0;
702 }
703 if (blink_state == BLINK_OFF)
704 gui_update_cursor(TRUE, FALSE);
705 blink_state = BLINK_NONE;
706}
707
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000709blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711 if (blink_state == BLINK_ON)
712 {
713 gui_undraw_cursor();
714 blink_state = BLINK_OFF;
715 blink_timer = gtk_timeout_add((guint32)blink_offtime,
716 (GtkFunction) blink_cb, NULL);
717 }
718 else
719 {
720 gui_update_cursor(TRUE, FALSE);
721 blink_state = BLINK_ON;
722 blink_timer = gtk_timeout_add((guint32)blink_ontime,
723 (GtkFunction) blink_cb, NULL);
724 }
725
726 return FALSE; /* don't happen again */
727}
728
729/*
730 * Start the cursor blinking. If it was already blinking, this restarts the
731 * waiting time and shows the cursor.
732 */
733 void
734gui_mch_start_blink(void)
735{
736 if (blink_timer)
737 gtk_timeout_remove(blink_timer);
738 /* Only switch blinking on if none of the times is zero */
739 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
740 {
741 blink_timer = gtk_timeout_add((guint32)blink_waittime,
742 (GtkFunction) blink_cb, NULL);
743 blink_state = BLINK_ON;
744 gui_update_cursor(TRUE, FALSE);
745 }
746}
747
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000749enter_notify_event(GtkWidget *widget UNUSED,
750 GdkEventCrossing *event UNUSED,
751 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752{
753 if (blink_state == BLINK_NONE)
754 gui_mch_start_blink();
755
756 /* make sure keyboard input goes there */
757 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
758 gtk_widget_grab_focus(gui.drawarea);
759
760 return FALSE;
761}
762
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000764leave_notify_event(GtkWidget *widget UNUSED,
765 GdkEventCrossing *event UNUSED,
766 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767{
768 if (blink_state != BLINK_NONE)
769 gui_mch_stop_blink();
770
771 return FALSE;
772}
773
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000775focus_in_event(GtkWidget *widget,
776 GdkEventFocus *event UNUSED,
777 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778{
779 gui_focus_change(TRUE);
780
781 if (blink_state == BLINK_NONE)
782 gui_mch_start_blink();
783
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000784 /* make sure keyboard input goes to the draw area (if this is focus for a
785 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000786 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000787 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 return TRUE;
790}
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000793focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200794 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000795 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 gui_focus_change(FALSE);
798
799 if (blink_state != BLINK_NONE)
800 gui_mch_stop_blink();
801
802 return TRUE;
803}
804
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
807 * Translate a GDK key value to UTF-8 independently of the current locale.
808 * The output is written to string, which must have room for at least 6 bytes
809 * plus the NUL terminator. Returns the length in bytes.
810 *
811 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
812 * of GdkEventKey::string instead. But event->string is evil; see here why:
813 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
814 */
815 static int
816keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
817{
818 int len;
819 guint32 uc;
820
821 uc = gdk_keyval_to_unicode(keyval);
822 if (uc != 0)
823 {
824 /* Check for CTRL-foo */
825 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
826 {
827 /* These mappings look arbitrary at the first glance, but in fact
828 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
829 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
830 * more sense and is consistent with usual terminal behaviour). */
831 if (uc >= '@')
832 string[0] = uc & 0x1F;
833 else if (uc == '2')
834 string[0] = NUL;
835 else if (uc >= '3' && uc <= '7')
836 string[0] = uc ^ 0x28;
837 else if (uc == '8')
838 string[0] = BS;
839 else if (uc == '?')
840 string[0] = DEL;
841 else
842 string[0] = uc;
843 len = 1;
844 }
845 else
846 {
847 /* Translate a normal key to UTF-8. This doesn't work for dead
848 * keys of course, you _have_ to use an input method for that. */
849 len = utf_char2bytes((int)uc, string);
850 }
851 }
852 else
853 {
854 /* Translate keys which are represented by ASCII control codes in Vim.
855 * There are only a few of those; most control keys are translated to
856 * special terminal-like control sequences. */
857 len = 1;
858 switch (keyval)
859 {
860 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
861 string[0] = TAB;
862 break;
863 case GDK_Linefeed:
864 string[0] = NL;
865 break;
866 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
867 string[0] = CAR;
868 break;
869 case GDK_Escape:
870 string[0] = ESC;
871 break;
872 default:
873 len = 0;
874 break;
875 }
876 }
877 string[len] = NUL;
878
879 return len;
880}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000882 static int
883modifiers_gdk2vim(guint state)
884{
885 int modifiers = 0;
886
887 if (state & GDK_SHIFT_MASK)
888 modifiers |= MOD_MASK_SHIFT;
889 if (state & GDK_CONTROL_MASK)
890 modifiers |= MOD_MASK_CTRL;
891 if (state & GDK_MOD1_MASK)
892 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +0200893#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200894 if (state & GDK_SUPER_MASK)
895 modifiers |= MOD_MASK_META;
896#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000897 if (state & GDK_MOD4_MASK)
898 modifiers |= MOD_MASK_META;
899
900 return modifiers;
901}
902
903 static int
904modifiers_gdk2mouse(guint state)
905{
906 int modifiers = 0;
907
908 if (state & GDK_SHIFT_MASK)
909 modifiers |= MOUSE_SHIFT;
910 if (state & GDK_CONTROL_MASK)
911 modifiers |= MOUSE_CTRL;
912 if (state & GDK_MOD1_MASK)
913 modifiers |= MOUSE_ALT;
914
915 return modifiers;
916}
917
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918/*
919 * Main keyboard handler:
920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000922key_press_event(GtkWidget *widget UNUSED,
923 GdkEventKey *event,
924 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200926 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
928 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 guint key_sym;
930 int len;
931 int i;
932 int modifiers;
933 int key;
934 guint state;
935 char_u *s, *d;
936
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200937 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 key_sym = event->keyval;
939 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941#ifdef FEAT_XIM
942 if (xim_queue_key_press_event(event, TRUE))
943 return TRUE;
944#endif
945
946#ifdef FEAT_HANGULIN
947 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
948 {
949 hangul_input_state_toggle();
950 return TRUE;
951 }
952#endif
953
954#ifdef SunXK_F36
955 /*
956 * These keys have bogus lookup strings, and trapping them here is
957 * easier than trying to XRebindKeysym() on them with every possible
958 * combination of modifiers.
959 */
960 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
961 len = 0;
962 else
963#endif
964 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 len = keyval_to_string(key_sym, state, string2);
966
967 /* Careful: convert_input() doesn't handle the NUL character.
968 * No need to convert pure ASCII anyway, thus the len > 1 check. */
969 if (len > 1 && input_conv.vc_type != CONV_NONE)
970 len = convert_input(string2, len, sizeof(string2));
971
972 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 d = string;
974 for (i = 0; i < len; ++i)
975 {
976 *d++ = s[i];
977 if (d[-1] == CSI && d + 2 < string + sizeof(string))
978 {
979 /* Turn CSI into K_CSI. */
980 *d++ = KS_EXTRA;
981 *d++ = (int)KE_CSI;
982 }
983 }
984 len = d - string;
985 }
986
987 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
988 if (key_sym == GDK_ISO_Left_Tab)
989 {
990 key_sym = GDK_Tab;
991 state |= GDK_SHIFT_MASK;
992 }
993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#ifdef FEAT_MENU
995 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
996 * is a menu shortcut, we ignore everything with the ALT modifier. */
997 if ((state & GDK_MOD1_MASK)
998 && gui.menu_is_active
999 && (*p_wak == 'y'
1000 || (*p_wak == 'm'
1001 && len == 1
1002 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /* For GTK2 we return false to signify that we haven't handled the
1004 * keypress, so that gtk will handle the mnemonic or accelerator. */
1005 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#endif
1007
1008 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1009 * that already has the 8th bit set.
1010 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1011 * Don't do this for double-byte encodings, it turns the char into a lead
1012 * byte. */
1013 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001014 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001015#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001016 || (state & GDK_SUPER_MASK)
1017#endif
1018 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1020 && (string[0] & 0x80) == 0
1021 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 )
1024 {
1025 string[0] |= 0x80;
1026 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (enc_utf8) /* convert to utf-8 */
1028 {
1029 string[1] = string[0] & 0xbf;
1030 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1031 if (string[1] == CSI)
1032 {
1033 string[2] = KS_EXTRA;
1034 string[3] = (int)KE_CSI;
1035 len = 4;
1036 }
1037 else
1038 len = 2;
1039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 }
1041
1042 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1043 * value) to detect backspace, delete and keypad keys. */
1044 if (len == 0 || len == 1)
1045 {
1046 for (i = 0; special_keys[i].key_sym != 0; i++)
1047 {
1048 if (special_keys[i].key_sym == key_sym)
1049 {
1050 string[0] = CSI;
1051 string[1] = special_keys[i].code0;
1052 string[2] = special_keys[i].code1;
1053 len = -3;
1054 break;
1055 }
1056 }
1057 }
1058
1059 if (len == 0) /* Unrecognized key */
1060 return TRUE;
1061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 /* Special keys (and a few others) may have modifiers. Also when using a
1063 * double-byte encoding (can't set the 8th bit). */
1064 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1065 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1066 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1067 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001068 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001069#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001070 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001072 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001074 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075
1076 /*
1077 * For some keys a shift modifier is translated into another key
1078 * code.
1079 */
1080 if (len == -3)
1081 key = TO_SPECIAL(string[1], string[2]);
1082 else
1083 key = string[0];
1084
1085 key = simplify_key(key, &modifiers);
1086 if (key == CSI)
1087 key = K_CSI;
1088 if (IS_SPECIAL(key))
1089 {
1090 string[0] = CSI;
1091 string[1] = K_SECOND(key);
1092 string[2] = K_THIRD(key);
1093 len = 3;
1094 }
1095 else
1096 {
1097 string[0] = key;
1098 len = 1;
1099 }
1100
1101 if (modifiers != 0)
1102 {
1103 string2[0] = CSI;
1104 string2[1] = KS_MODIFIER;
1105 string2[2] = modifiers;
1106 add_to_input_buf(string2, 3);
1107 }
1108 }
1109
1110 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1111 || (string[0] == intr_char && intr_char != Ctrl_C)))
1112 {
1113 trash_input_buf();
1114 got_int = TRUE;
1115 }
1116
1117 add_to_input_buf(string, len);
1118
1119 /* blank out the pointer if necessary */
1120 if (p_mh)
1121 gui_mch_mousehide(TRUE);
1122
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 return TRUE;
1124}
1125
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001126#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001128key_release_event(GtkWidget *widget UNUSED,
1129 GdkEventKey *event,
1130 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131{
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001132 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 /*
1134 * GTK+ 2 input methods may do fancy stuff on key release events too.
1135 * With the default IM for instance, you can enter any UCS code point
1136 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1137 */
1138 return xim_queue_key_press_event(event, FALSE);
1139}
1140#endif
1141
1142
1143/****************************************************************************
1144 * Selection handlers:
1145 */
1146
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001148selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001150 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151{
1152 if (event->selection == clip_plus.gtk_sel_atom)
1153 clip_lose_selection(&clip_plus);
1154 else
1155 clip_lose_selection(&clip_star);
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 return TRUE;
1158}
1159
1160#define RS_NONE 0 /* selection_received_cb() not called yet */
1161#define RS_OK 1 /* selection_received_cb() called and OK */
1162#define RS_FAIL 2 /* selection_received_cb() called and failed */
1163static int received_selection = RS_NONE;
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001166selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001168 guint time_ UNUSED,
1169 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170{
1171 VimClipboard *cbd;
1172 char_u *text;
1173 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 int len;
1176 int motion_type;
1177
1178 if (data->selection == clip_plus.gtk_sel_atom)
1179 cbd = &clip_plus;
1180 else
1181 cbd = &clip_star;
1182
1183 text = (char_u *)data->data;
1184 len = data->length;
1185 motion_type = MCHAR;
1186
1187 if (text == NULL || len <= 0)
1188 {
1189 received_selection = RS_FAIL;
1190 /* clip_free_selection(cbd); ??? */
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 return;
1193 }
1194
1195 if (data->type == vim_atom)
1196 {
1197 motion_type = *text++;
1198 --len;
1199 }
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (data->type == vimenc_atom)
1202 {
1203 char_u *enc;
1204 vimconv_T conv;
1205
1206 motion_type = *text++;
1207 --len;
1208
1209 enc = text;
1210 text += STRLEN(text) + 1;
1211 len -= text - enc;
1212
1213 /* If the encoding of the text is different from 'encoding', attempt
1214 * converting it. */
1215 conv.vc_type = CONV_NONE;
1216 convert_setup(&conv, enc, p_enc);
1217 if (conv.vc_type != CONV_NONE)
1218 {
1219 tmpbuf = string_convert(&conv, text, &len);
1220 if (tmpbuf != NULL)
1221 text = tmpbuf;
1222 convert_setup(&conv, NULL, NULL);
1223 }
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* gtk_selection_data_get_text() handles all the nasty details
1227 * and targets and encodings etc. This rocks so hard. */
1228 else
1229 {
1230 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1231 if (tmpbuf_utf8 != NULL)
1232 {
1233 len = STRLEN(tmpbuf_utf8);
1234 if (input_conv.vc_type != CONV_NONE)
1235 {
1236 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1237 if (tmpbuf != NULL)
1238 text = tmpbuf;
1239 }
1240 else
1241 text = tmpbuf_utf8;
1242 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001243 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1244 {
1245 vimconv_T conv;
1246
1247 /* UTF-16, we get this for HTML */
1248 conv.vc_type = CONV_NONE;
1249 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1250
1251 if (conv.vc_type != CONV_NONE)
1252 {
1253 text += 2;
1254 len -= 2;
1255 tmpbuf = string_convert(&conv, text, &len);
1256 convert_setup(&conv, NULL, NULL);
1257 }
1258 if (tmpbuf != NULL)
1259 text = tmpbuf;
1260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaara76638f2010-06-05 12:49:46 +02001263 /* Chop off any traiing NUL bytes. OpenOffice sends these. */
1264 while (len > 0 && text[len - 1] == NUL)
1265 --len;
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 clip_yank_selection(motion_type, text, (long)len, cbd);
1268 received_selection = RS_OK;
1269 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273/*
1274 * Prepare our selection data for passing it to the external selection
1275 * client.
1276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001278selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 GtkSelectionData *selection_data,
1280 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001281 guint time_ UNUSED,
1282 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 char_u *string;
1285 char_u *tmpbuf;
1286 long_u tmplen;
1287 int length;
1288 int motion_type;
1289 GdkAtom type;
1290 VimClipboard *cbd;
1291
1292 if (selection_data->selection == clip_plus.gtk_sel_atom)
1293 cbd = &clip_plus;
1294 else
1295 cbd = &clip_star;
1296
1297 if (!cbd->owned)
1298 return; /* Shouldn't ever happen */
1299
1300 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001301 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && info != (guint)TARGET_UTF8_STRING
1303 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && info != (guint)TARGET_VIM
1305 && info != (guint)TARGET_COMPOUND_TEXT
1306 && info != (guint)TARGET_TEXT)
1307 return;
1308
1309 /* get the selection from the '*'/'+' register */
1310 clip_get_selection(cbd);
1311
1312 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1313 if (motion_type < 0 || string == NULL)
1314 return;
1315 /* Due to int arguments we can't handle more than G_MAXINT. Also
1316 * reserve one extra byte for NUL or the motion type; just in case.
1317 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1318 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1319
1320 if (info == (guint)TARGET_VIM)
1321 {
1322 tmpbuf = alloc((unsigned)length + 1);
1323 if (tmpbuf != NULL)
1324 {
1325 tmpbuf[0] = motion_type;
1326 mch_memmove(tmpbuf + 1, string, (size_t)length);
1327 }
1328 /* For our own format, the first byte contains the motion type */
1329 ++length;
1330 vim_free(string);
1331 string = tmpbuf;
1332 type = vim_atom;
1333 }
1334
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001335 else if (info == (guint)TARGET_HTML)
1336 {
1337 vimconv_T conv;
1338
1339 /* Since we get utf-16, we probably should set it as well. */
1340 conv.vc_type = CONV_NONE;
1341 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1342 if (conv.vc_type != CONV_NONE)
1343 {
1344 tmpbuf = string_convert(&conv, string, &length);
1345 convert_setup(&conv, NULL, NULL);
1346 vim_free(string);
1347 string = tmpbuf;
1348 }
1349
1350 /* Prepend the BOM: "fffe" */
1351 if (string != NULL)
1352 {
1353 tmpbuf = alloc(length + 2);
1354 tmpbuf[0] = 0xff;
1355 tmpbuf[1] = 0xfe;
1356 mch_memmove(tmpbuf + 2, string, (size_t)length);
1357 vim_free(string);
1358 string = tmpbuf;
1359 length += 2;
1360
1361 selection_data->type = selection_data->target;
1362 selection_data->format = 16; /* 16 bits per char */
1363 gtk_selection_data_set(selection_data, html_atom, 16,
1364 string, length);
1365 vim_free(string);
1366 }
1367 return;
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 else if (info == (guint)TARGET_VIMENC)
1370 {
1371 int l = STRLEN(p_enc);
1372
1373 /* contents: motion_type 'encoding' NUL text */
1374 tmpbuf = alloc((unsigned)length + l + 2);
1375 if (tmpbuf != NULL)
1376 {
1377 tmpbuf[0] = motion_type;
1378 STRCPY(tmpbuf + 1, p_enc);
1379 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1380 }
1381 length += l + 2;
1382 vim_free(string);
1383 string = tmpbuf;
1384 type = vimenc_atom;
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /* gtk_selection_data_set_text() handles everything for us. This is
1388 * so easy and simple and cool, it'd be insane not to use it. */
1389 else
1390 {
1391 if (output_conv.vc_type != CONV_NONE)
1392 {
1393 tmpbuf = string_convert(&output_conv, string, &length);
1394 vim_free(string);
1395 if (tmpbuf == NULL)
1396 return;
1397 string = tmpbuf;
1398 }
1399 /* Validate the string to avoid runtime warnings */
1400 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1401 {
1402 gtk_selection_data_set_text(selection_data,
1403 (const char *)string, length);
1404 }
1405 vim_free(string);
1406 return;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 if (string != NULL)
1410 {
1411 selection_data->type = selection_data->target;
1412 selection_data->format = 8; /* 8 bits per char */
1413
1414 gtk_selection_data_set(selection_data, type, 8, string, length);
1415 vim_free(string);
1416 }
1417}
1418
1419/*
1420 * Check if the GUI can be started. Called before gvimrc is sourced.
1421 * Return OK or FAIL.
1422 */
1423 int
1424gui_mch_init_check(void)
1425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#ifdef FEAT_GUI_GNOME
1427 if (gtk_socket_id == 0)
1428 using_gnome = 1;
1429#endif
1430
1431 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1432 if (!gtk_init_check(&gui_argc, &gui_argv))
1433 {
1434 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001435 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 return FAIL;
1437 }
1438
1439 return OK;
1440}
1441
1442
1443/****************************************************************************
1444 * Mouse handling callbacks
1445 */
1446
1447
1448static guint mouse_click_timer = 0;
1449static int mouse_timed_out = TRUE;
1450
1451/*
1452 * Timer used to recognize multiple clicks of the mouse button
1453 */
1454 static gint
1455mouse_click_timer_cb(gpointer data)
1456{
1457 /* we don't use this information currently */
1458 int *timed_out = (int *) data;
1459
1460 *timed_out = TRUE;
1461 return FALSE; /* don't happen again */
1462}
1463
1464static guint motion_repeat_timer = 0;
1465static int motion_repeat_offset = FALSE;
1466static gint motion_repeat_timer_cb(gpointer);
1467
1468 static void
1469process_motion_notify(int x, int y, GdkModifierType state)
1470{
1471 int button;
1472 int_u vim_modifiers;
1473
1474 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1475 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1476 GDK_BUTTON5_MASK))
1477 ? MOUSE_DRAG : ' ';
1478
1479 /* If our pointer is currently hidden, then we should show it. */
1480 gui_mch_mousehide(FALSE);
1481
1482 /* Just moving the rodent above the drawing area without any button
1483 * being pressed. */
1484 if (button != MOUSE_DRAG)
1485 {
1486 gui_mouse_moved(x, y);
1487 return;
1488 }
1489
1490 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001491 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
Bram Moolenaar49325942007-05-10 19:19:59 +00001493 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1495
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 /*
1497 * Auto repeat timer handling.
1498 */
1499 if (x < 0 || y < 0
1500 || x >= gui.drawarea->allocation.width
1501 || y >= gui.drawarea->allocation.height)
1502 {
1503
1504 int dx;
1505 int dy;
1506 int offshoot;
1507 int delay = 10;
1508
1509 /* Calculate the maximal distance of the cursor from the drawing area.
1510 * (offshoot can't become negative here!).
1511 */
1512 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1513 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1514
1515 offshoot = dx > dy ? dx : dy;
1516
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001517 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * distance of 127 pixels from the main window.
1519 *
1520 * One could think endlessly about the most ergonomic variant here.
1521 * For example it could make sense to calculate the distance from the
1522 * drags start instead...
1523 *
1524 * Maybe a parabolic interpolation would suite us better here too...
1525 */
1526 if (offshoot > 127)
1527 {
1528 /* 5 appears to be somehow near to my perceptual limits :-). */
1529 delay = 5;
1530 }
1531 else
1532 {
1533 delay = (130 * (127 - offshoot)) / 127 + 5;
1534 }
1535
1536 /* shoot again */
1537 if (!motion_repeat_timer)
1538 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1539 motion_repeat_timer_cb, NULL);
1540 }
1541}
1542
1543/*
1544 * Timer used to recognize multiple clicks of the mouse button.
1545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001547motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548{
1549 int x;
1550 int y;
1551 GdkModifierType state;
1552
1553 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1554
1555 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1556 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1557 GDK_BUTTON5_MASK)))
1558 {
1559 motion_repeat_timer = 0;
1560 return FALSE;
1561 }
1562
1563 /* If there already is a mouse click in the input buffer, wait another
1564 * time (otherwise we would create a backlog of clicks) */
1565 if (vim_used_in_input_buf() > 10)
1566 return TRUE;
1567
1568 motion_repeat_timer = 0;
1569
1570 /*
1571 * Fake a motion event.
1572 * Trick: Pretend the mouse moved to the next character on every other
1573 * event, otherwise drag events will be discarded, because they are still
1574 * in the same character.
1575 */
1576 if (motion_repeat_offset)
1577 x += gui.char_width;
1578
1579 motion_repeat_offset = !motion_repeat_offset;
1580 process_motion_notify(x, y, state);
1581
1582 /* Don't happen again. We will get reinstalled in the synthetic event
1583 * if needed -- thus repeating should still work. */
1584 return FALSE;
1585}
1586
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001588motion_notify_event(GtkWidget *widget,
1589 GdkEventMotion *event,
1590 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591{
1592 if (event->is_hint)
1593 {
1594 int x;
1595 int y;
1596 GdkModifierType state;
1597
1598 gdk_window_get_pointer(widget->window, &x, &y, &state);
1599 process_motion_notify(x, y, state);
1600 }
1601 else
1602 {
1603 process_motion_notify((int)event->x, (int)event->y,
1604 (GdkModifierType)event->state);
1605 }
1606
1607 return TRUE; /* handled */
1608}
1609
1610
1611/*
1612 * Mouse button handling. Note please that we are capturing multiple click's
1613 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1614 * This is due to the way the generic VIM code is recognizing multiple clicks.
1615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001617button_press_event(GtkWidget *widget,
1618 GdkEventButton *event,
1619 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620{
1621 int button;
1622 int repeated_click = FALSE;
1623 int x, y;
1624 int_u vim_modifiers;
1625
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001626 clipboard_event_time = event->time;
1627
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 /* Make sure we have focus now we've been selected */
1629 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1630 gtk_widget_grab_focus(widget);
1631
1632 /*
1633 * Don't let additional events about multiple clicks send by GTK to us
1634 * after the initial button press event confuse us.
1635 */
1636 if (event->type != GDK_BUTTON_PRESS)
1637 return FALSE;
1638
1639 x = event->x;
1640 y = event->y;
1641
1642 /* Handle multiple clicks */
1643 if (!mouse_timed_out && mouse_click_timer)
1644 {
1645 gtk_timeout_remove(mouse_click_timer);
1646 mouse_click_timer = 0;
1647 repeated_click = TRUE;
1648 }
1649
1650 mouse_timed_out = FALSE;
1651 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1652 mouse_click_timer_cb, &mouse_timed_out);
1653
1654 switch (event->button)
1655 {
1656 case 1:
1657 button = MOUSE_LEFT;
1658 break;
1659 case 2:
1660 button = MOUSE_MIDDLE;
1661 break;
1662 case 3:
1663 button = MOUSE_RIGHT;
1664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 default:
1666 return FALSE; /* Unknown button */
1667 }
1668
1669#ifdef FEAT_XIM
1670 /* cancel any preediting */
1671 if (im_is_preediting())
1672 xim_reset();
1673#endif
1674
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001675 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676
1677 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 return TRUE;
1680}
1681
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001683 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001686scroll_event(GtkWidget *widget,
1687 GdkEventScroll *event,
1688 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689{
1690 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001691 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1694 gtk_widget_grab_focus(widget);
1695
1696 switch (event->direction)
1697 {
1698 case GDK_SCROLL_UP:
1699 button = MOUSE_4;
1700 break;
1701 case GDK_SCROLL_DOWN:
1702 button = MOUSE_5;
1703 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001704 case GDK_SCROLL_LEFT:
1705 button = MOUSE_7;
1706 break;
1707 case GDK_SCROLL_RIGHT:
1708 button = MOUSE_6;
1709 break;
1710 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 return FALSE;
1712 }
1713
1714# ifdef FEAT_XIM
1715 /* cancel any preediting */
1716 if (im_is_preediting())
1717 xim_reset();
1718# endif
1719
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001720 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1723 FALSE, vim_modifiers);
1724
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 return TRUE;
1726}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727
1728
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001730button_release_event(GtkWidget *widget UNUSED,
1731 GdkEventButton *event,
1732 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 int x, y;
1735 int_u vim_modifiers;
1736
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001737 clipboard_event_time = event->time;
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Remove any motion "machine gun" timers used for automatic further
1740 extension of allocation areas if outside of the applications window
1741 area .*/
1742 if (motion_repeat_timer)
1743 {
1744 gtk_timeout_remove(motion_repeat_timer);
1745 motion_repeat_timer = 0;
1746 }
1747
1748 x = event->x;
1749 y = event->y;
1750
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001751 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752
1753 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755 return TRUE;
1756}
1757
1758
1759#ifdef FEAT_DND
1760/****************************************************************************
1761 * Drag aNd Drop support handlers.
1762 */
1763
1764/*
1765 * Count how many items there may be and separate them with a NUL.
1766 * Apparently the items are separated with \r\n. This is not documented,
1767 * thus be careful not to go past the end. Also allow separation with
1768 * NUL characters.
1769 */
1770 static int
1771count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1772{
1773 int i;
1774 char_u *p = out;
1775 int count = 0;
1776
1777 for (i = 0; i < len; ++i)
1778 {
1779 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1780 {
1781 if (p > out && p[-1] != NUL)
1782 {
1783 ++count;
1784 *p++ = NUL;
1785 }
1786 }
1787 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1788 {
1789 *p++ = hexhex2nr(raw + i + 1);
1790 i += 2;
1791 }
1792 else
1793 *p++ = raw[i];
1794 }
1795 if (p > out && p[-1] != NUL)
1796 {
1797 *p = NUL; /* last item didn't have \r or \n */
1798 ++count;
1799 }
1800 return count;
1801}
1802
1803/*
1804 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1805 * this process, URI which protocol is not "file:" are removed. Return
1806 * length of array (less than "max").
1807 */
1808 static int
1809filter_uri_list(char_u **outlist, int max, char_u *src)
1810{
1811 int i, j;
1812
1813 for (i = j = 0; i < max; ++i)
1814 {
1815 outlist[i] = NULL;
1816 if (STRNCMP(src, "file:", 5) == 0)
1817 {
1818 src += 5;
1819 if (STRNCMP(src, "//localhost", 11) == 0)
1820 src += 11;
1821 while (src[0] == '/' && src[1] == '/')
1822 ++src;
1823 outlist[j++] = vim_strsave(src);
1824 }
1825 src += STRLEN(src) + 1;
1826 }
1827 return j;
1828}
1829
1830 static char_u **
1831parse_uri_list(int *count, char_u *data, int len)
1832{
1833 int n = 0;
1834 char_u *tmp = NULL;
1835 char_u **array = NULL;;
1836
1837 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1838 {
1839 n = count_and_decode_uri_list(tmp, data, len);
1840 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1841 n = filter_uri_list(array, n, tmp);
1842 }
1843 vim_free(tmp);
1844 *count = n;
1845 return array;
1846}
1847
1848 static void
1849drag_handle_uri_list(GdkDragContext *context,
1850 GtkSelectionData *data,
1851 guint time_,
1852 GdkModifierType state,
1853 gint x,
1854 gint y)
1855{
1856 char_u **fnames;
1857 int nfiles = 0;
1858
1859 fnames = parse_uri_list(&nfiles, data->data, data->length);
1860
1861 if (fnames != NULL && nfiles > 0)
1862 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001863 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864
1865 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1866
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001867 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
1869 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1870 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001871 else
1872 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873}
1874
1875 static void
1876drag_handle_text(GdkDragContext *context,
1877 GtkSelectionData *data,
1878 guint time_,
1879 GdkModifierType state)
1880{
1881 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1882 char_u *text;
1883 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 text = data->data;
1887 len = data->length;
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (data->type == utf8_string_atom)
1890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (input_conv.vc_type != CONV_NONE)
1892 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (tmpbuf != NULL)
1894 text = tmpbuf;
1895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
1897 dnd_yank_drag_data(text, (long)len);
1898 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001901 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902
1903 if (dropkey[2] != 0)
1904 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1905 else
1906 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907}
1908
1909/*
1910 * DND receiver.
1911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 static void
1913drag_data_received_cb(GtkWidget *widget,
1914 GdkDragContext *context,
1915 gint x,
1916 gint y,
1917 GtkSelectionData *data,
1918 guint info,
1919 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001920 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 GdkModifierType state;
1923
1924 /* Guard against trash */
1925 if (data->data == NULL
1926 || data->length <= 0
1927 || data->format != 8
1928 || data->data[data->length] != '\0')
1929 {
1930 gtk_drag_finish(context, FALSE, FALSE, time_);
1931 return;
1932 }
1933
1934 /* Get the current modifier state for proper distinguishment between
1935 * different operations later. */
1936 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1937
1938 /* Not sure about the role of "text/plain" here... */
1939 if (info == (guint)TARGET_TEXT_URI_LIST)
1940 drag_handle_uri_list(context, data, time_, state, x, y);
1941 else
1942 drag_handle_text(context, data, time_, state);
1943
1944}
1945#endif /* FEAT_DND */
1946
1947
1948#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1949/*
1950 * GnomeClient interact callback. Check for unsaved buffers that cannot
1951 * be abandoned and pop up a dialog asking the user for confirmation if
1952 * necessary.
1953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001955sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001957 GnomeDialogType type UNUSED,
1958 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959{
1960 cmdmod_T save_cmdmod;
1961 gboolean shutdown_cancelled;
1962
1963 save_cmdmod = cmdmod;
1964
1965# ifdef FEAT_BROWSE
1966 cmdmod.browse = TRUE;
1967# endif
1968# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1969 cmdmod.confirm = TRUE;
1970# endif
1971 /*
1972 * If there are changed buffers, present the user with
1973 * a dialog if possible, otherwise give an error message.
1974 */
1975 shutdown_cancelled = check_changed_any(FALSE);
1976
1977 exiting = FALSE;
1978 cmdmod = save_cmdmod;
1979 setcursor(); /* position the cursor */
1980 out_flush();
1981 /*
1982 * If the user hit the [Cancel] button the whole shutdown
1983 * will be cancelled. Wow, quite powerful feature (:
1984 */
1985 gnome_interaction_key_return(key, shutdown_cancelled);
1986}
1987
1988/*
1989 * Generate a script that can be used to restore the current editing session.
1990 * Save the value of v:this_session before running :mksession in order to make
1991 * automagic session save fully transparent. Return TRUE on success.
1992 */
1993 static int
1994write_session_file(char_u *filename)
1995{
1996 char_u *escaped_filename;
1997 char *mksession_cmdline;
1998 unsigned int save_ssop_flags;
1999 int failed;
2000
2001 /*
2002 * Build an ex command line to create a script that restores the current
2003 * session if executed. Escape the filename to avoid nasty surprises.
2004 */
2005 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2006 if (escaped_filename == NULL)
2007 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002008 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2009 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 /*
2013 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2014 * unpredictable effects when the session is saved automatically. Also,
2015 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2016 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2017 * enormously large if the GNOME session feature is used regularly.
2018 */
2019 save_ssop_flags = ssop_flags;
2020 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002021 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
2023 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2024 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2025 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002026 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 ssop_flags = save_ssop_flags;
2029 g_free(mksession_cmdline);
2030 /*
2031 * Reopen the file and append a command to restore v:this_session,
2032 * as if this save never happened. This is to avoid conflicts with
2033 * the user's own sessions. FIXME: It's probably less hackish to add
2034 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2035 */
2036 if (!failed)
2037 {
2038 FILE *fd;
2039
2040 fd = open_exfile(filename, TRUE, APPENDBIN);
2041
2042 failed = (fd == NULL
2043 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2044 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2045
2046 if (fd != NULL && fclose(fd) != 0)
2047 failed = TRUE;
2048
2049 if (failed)
2050 mch_remove(filename);
2051 }
2052
2053 return !failed;
2054}
2055
2056/*
2057 * "save_yourself" signal handler. Initiate an interaction to ask the user
2058 * for confirmation if necessary. Save the current editing session and tell
2059 * the session manager how to restart Vim.
2060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 static gboolean
2062sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002063 gint phase UNUSED,
2064 GnomeSaveStyle save_style UNUSED,
2065 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002067 gboolean fast UNUSED,
2068 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 static const char suffix[] = "-session.vim";
2071 char *session_file;
2072 unsigned int len;
2073 gboolean success;
2074
2075 /* Always request an interaction if possible. check_changed_any()
2076 * won't actually show a dialog unless any buffers have been modified.
2077 * There doesn't seem to be an obvious way to check that without
2078 * automatically firing the dialog. Anyway, it works just fine. */
2079 if (interact_style == GNOME_INTERACT_ANY)
2080 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2081 &sm_client_check_changed_any,
2082 NULL);
2083 out_flush();
2084 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2085
2086 /* The path is unique for each session save. We do neither know nor care
2087 * which session script will actually be used later. This decision is in
2088 * the domain of the session manager. */
2089 session_file = gnome_config_get_real_path(
2090 gnome_client_get_config_prefix(client));
2091 len = strlen(session_file);
2092
2093 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2094 --len; /* get rid of the superfluous trailing '/' */
2095
2096 session_file = g_renew(char, session_file, len + sizeof(suffix));
2097 memcpy(session_file + len, suffix, sizeof(suffix));
2098
2099 success = write_session_file((char_u *)session_file);
2100
2101 if (success)
2102 {
2103 const char *argv[8];
2104 int i;
2105
2106 /* Tell the session manager how to wipe out the stored session data.
2107 * This isn't as dangerous as it looks, don't worry :) session_file
2108 * is a unique absolute filename. Usually it'll be something like
2109 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2110 i = 0;
2111 argv[i++] = "rm";
2112 argv[i++] = session_file;
2113 argv[i] = NULL;
2114
2115 gnome_client_set_discard_command(client, i, (char **)argv);
2116
2117 /* Tell the session manager how to restore the just saved session.
2118 * This is easily done thanks to Vim's -S option. Pass the -f flag
2119 * since there's no need to fork -- it might even cause confusion.
2120 * Also pass the window role to give the WM something to match on.
2121 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2122 i = 0;
2123 argv[i++] = restart_command;
2124 argv[i++] = "-f";
2125 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 argv[i++] = "--role";
2127 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 argv[i++] = "-S";
2129 argv[i++] = session_file;
2130 argv[i] = NULL;
2131
2132 gnome_client_set_restart_command(client, i, (char **)argv);
2133 gnome_client_set_clone_command(client, 0, NULL);
2134 }
2135
2136 g_free(session_file);
2137
2138 return success;
2139}
2140
2141/*
2142 * Called when the session manager wants us to die. There isn't much to save
2143 * here since "save_yourself" has been emitted before (unless serious trouble
2144 * is happening).
2145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002147sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
2149 /* Don't write messages to the GUI anymore */
2150 full_screen = FALSE;
2151
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002152 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002153 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002154 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 preserve_exit();
2156}
2157
2158/*
2159 * Connect our signal handlers to be notified on session save and shutdown.
2160 */
2161 static void
2162setup_save_yourself(void)
2163{
2164 GnomeClient *client;
2165
2166 client = gnome_master_client();
2167
2168 if (client != NULL)
2169 {
2170 /* Must use the deprecated gtk_signal_connect() for compatibility
2171 * with GNOME 1. Arrgh, zombies! */
2172 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2173 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2174 gtk_signal_connect(GTK_OBJECT(client), "die",
2175 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2176 }
2177}
2178
2179#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2180
2181# ifdef USE_XSMP
2182/*
2183 * GTK tells us that XSMP needs attention
2184 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 static gboolean
2186local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002187 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 GIOCondition condition;
2189 gpointer data;
2190{
2191 if (condition == G_IO_IN)
2192 {
2193 /* Do stuff; maybe close connection */
2194 if (xsmp_handle_requests() == FAIL)
2195 g_io_channel_unref((GIOChannel *)data);
2196 return TRUE;
2197 }
2198 /* Error */
2199 g_io_channel_unref((GIOChannel *)data);
2200 xsmp_close();
2201 return TRUE;
2202}
2203# endif /* USE_XSMP */
2204
2205/*
2206 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2207 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2208 */
2209 static void
2210setup_save_yourself(void)
2211{
2212 Atom *existing_atoms = NULL;
2213 int count = 0;
2214
2215#ifdef USE_XSMP
2216 if (xsmp_icefd != -1)
2217 {
2218 /*
2219 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2220 * set up GTK IO monitor
2221 */
2222 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2223
2224 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2225 local_xsmp_handle_requests, (gpointer)g_io);
2226 }
2227 else
2228#endif
2229 {
2230 /* Fall back to old method */
2231
2232 /* first get the existing value */
2233 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2234 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2235 &existing_atoms, &count))
2236 {
2237 Atom *new_atoms;
2238 Atom save_yourself_xatom;
2239 int i;
2240
2241 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2242
2243 /* check if WM_SAVE_YOURSELF isn't there yet */
2244 for (i = 0; i < count; ++i)
2245 if (existing_atoms[i] == save_yourself_xatom)
2246 break;
2247
2248 if (i == count)
2249 {
2250 /* allocate an Atoms array which is one item longer */
2251 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2252 * sizeof(Atom)));
2253 if (new_atoms != NULL)
2254 {
2255 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2256 new_atoms[count] = save_yourself_xatom;
2257 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2258 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2259 new_atoms, count + 1);
2260 vim_free(new_atoms);
2261 }
2262 }
2263 XFree(existing_atoms);
2264 }
2265 }
2266}
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268/*
2269 * Installing a global event filter seems to be the only way to catch
2270 * client messages of type WM_PROTOCOLS without overriding GDK's own
2271 * client message event filter. Well, that's still better than trying
2272 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 *
2274 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2275 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2276 * I have the nasty feeling modern session managers just don't send this
2277 * deprecated message anymore. Addition: confirmed by several people.
2278 *
2279 * The GNOME session support is much cooler anyway. Unlike this ugly
2280 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2281 * it should work with KDE as well.
2282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002284global_event_filter(GdkXEvent *xev,
2285 GdkEvent *event UNUSED,
2286 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287{
2288 XEvent *xevent = (XEvent *)xev;
2289
2290 if (xevent != NULL
2291 && xevent->type == ClientMessage
2292 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002293 && (long_u)xevent->xclient.data.l[0]
2294 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
2296 out_flush();
2297 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2298 /*
2299 * Set the window's WM_COMMAND property, to let the window manager
2300 * know we are done saving ourselves. We don't want to be
2301 * restarted, thus set argv to NULL.
2302 */
2303 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2304 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2305 NULL, 0);
2306 return GDK_FILTER_REMOVE;
2307 }
2308
2309 return GDK_FILTER_CONTINUE;
2310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2312
2313
2314/*
2315 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002318mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319{
2320/* If you get an error message here, you still need to unpack the runtime
2321 * archive! */
2322#ifdef magick
2323# undef magick
2324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 /* A bit hackish, but avoids casting later and allows optimization */
2326# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327#define magick vim32x32
2328#include "../runtime/vim32x32.xpm"
2329#undef magick
2330#define magick vim16x16
2331#include "../runtime/vim16x16.xpm"
2332#undef magick
2333#define magick vim48x48
2334#include "../runtime/vim48x48.xpm"
2335#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /* When started with "--echo-wid" argument, write window ID on stdout. */
2339 if (echo_wid_arg)
2340 {
2341 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2342 fflush(stdout);
2343 }
2344
2345 if (vim_strchr(p_go, GO_ICON) != NULL)
2346 {
2347 /*
2348 * Add an icon to the main window. For fun and convenience of the user.
2349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 GList *icons = NULL;
2351
2352 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2353 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2354 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2355
2356 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2357
2358 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2359 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361
2362#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2363 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366 /* Setup to indicate to the window manager that we want to catch the
2367 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2368 * manager instead. */
2369#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2370 if (using_gnome)
2371#endif
2372 setup_save_yourself();
2373
2374#ifdef FEAT_CLIENTSERVER
2375 if (serverName == NULL && serverDelayedStartName != NULL)
2376 {
2377 /* This is a :gui command in a plain vim with no previous server */
2378 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2379
2380 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2381 serverDelayedStartName);
2382 }
2383 else
2384 {
2385 /*
2386 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2387 * have to change the "server" registration to that of the main window
2388 * If we have not registered a name yet, remember the window
2389 */
2390 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2391 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2392 }
2393 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2394 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2395 GTK_SIGNAL_FUNC(property_event), NULL);
2396#endif
2397}
2398
2399 static GdkCursor *
2400create_blank_pointer(void)
2401{
2402 GdkWindow *root_window = NULL;
2403 GdkPixmap *blank_mask;
2404 GdkCursor *cursor;
2405 GdkColor color = { 0, 0, 0, 0 };
2406 char blank_data[] = { 0x0 };
2407
2408#ifdef HAVE_GTK_MULTIHEAD
2409 root_window = gtk_widget_get_root_window(gui.mainwin);
2410#endif
2411
2412 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2413 * in size. */
2414 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2415 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2416 &color, &color, 0, 0);
2417 gdk_bitmap_unref(blank_mask);
2418
2419 return cursor;
2420}
2421
2422#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 static void
2424mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002425 GdkScreen *previous_screen UNUSED,
2426 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 if (!gtk_widget_has_screen(widget))
2429 return;
2430
2431 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002432 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 */
2434 if (gui.blank_pointer != NULL)
2435 gdk_cursor_unref(gui.blank_pointer);
2436
2437 gui.blank_pointer = create_blank_pointer();
2438
2439 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2440 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2441
2442 /*
2443 * Create a new PangoContext for this screen, and initialize it
2444 * with the current font if necessary.
2445 */
2446 if (gui.text_context != NULL)
2447 g_object_unref(gui.text_context);
2448
2449 gui.text_context = gtk_widget_create_pango_context(widget);
2450 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2451
2452 if (gui.norm_font != NULL)
2453 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002454 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002455 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 }
2457}
2458#endif /* HAVE_GTK_MULTIHEAD */
2459
2460/*
2461 * After the drawing area comes up, we calculate all colors and create the
2462 * dummy blank cursor.
2463 *
2464 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2465 * fact that the main VIM engine doesn't take them into account anywhere.
2466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002468drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 GtkWidget *sbar;
2471
2472#ifdef FEAT_XIM
2473 xim_init();
2474#endif
2475 gui_mch_new_colors();
2476 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2477
2478 gui.blank_pointer = create_blank_pointer();
2479 if (gui.pointer_hidden)
2480 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2481
2482 /* get the actual size of the scrollbars, if they are realized */
2483 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2484 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2485 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2486 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2487 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2488 gui.scrollbar_width = sbar->allocation.width;
2489
2490 sbar = gui.bottom_sbar.id;
2491 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2492 gui.scrollbar_height = sbar->allocation.height;
2493}
2494
2495/*
2496 * Properly clean up on shutdown.
2497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002499drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 /* Don't write messages to the GUI anymore */
2502 full_screen = FALSE;
2503
2504#ifdef FEAT_XIM
2505 im_shutdown();
2506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (gui.ascii_glyphs != NULL)
2508 {
2509 pango_glyph_string_free(gui.ascii_glyphs);
2510 gui.ascii_glyphs = NULL;
2511 }
2512 if (gui.ascii_font != NULL)
2513 {
2514 g_object_unref(gui.ascii_font);
2515 gui.ascii_font = NULL;
2516 }
2517 g_object_unref(gui.text_context);
2518 gui.text_context = NULL;
2519
2520 g_object_unref(gui.text_gc);
2521 gui.text_gc = NULL;
2522
2523 gdk_cursor_unref(gui.blank_pointer);
2524 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525}
2526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002528drawarea_style_set_cb(GtkWidget *widget UNUSED,
2529 GtkStyle *previous_style UNUSED,
2530 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 gui_mch_new_colors();
2533}
2534
2535/*
2536 * Callback routine for the "delete_event" signal on the toplevel window.
2537 * Tries to vim gracefully, or refuses to exit with changed buffers.
2538 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002540delete_event_cb(GtkWidget *widget UNUSED,
2541 GdkEventAny *event UNUSED,
2542 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543{
2544 gui_shell_closed();
2545 return TRUE;
2546}
2547
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002548#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2549 static int
2550get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2551{
2552 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2553
2554#ifdef FEAT_GUI_GNOME
2555 if (using_gnome && widget != NULL)
2556 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002557 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002558 BonoboDockItem *dockitem;
2559
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002560 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002561 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2562 {
2563 /* Only menu & toolbar are dock items. Could tabline be?
2564 * Seem to be only the 2 defined in GNOME */
2565 widget = parent;
2566 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002567
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002568 if (dockitem == NULL || dockitem->is_floating)
2569 return 0;
2570 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2571 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002572 }
2573#endif
2574 if (widget != NULL
2575 && item_orientation == orientation
2576 && GTK_WIDGET_REALIZED(widget)
2577 && GTK_WIDGET_VISIBLE(widget))
2578 {
2579 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2580 return widget->allocation.height;
2581 else
2582 return widget->allocation.width;
2583 }
2584 return 0;
2585}
2586#endif
2587
2588 static int
2589get_menu_tool_width(void)
2590{
2591 int width = 0;
2592
2593#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2594# ifdef FEAT_MENU
2595 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2596# endif
2597# ifdef FEAT_TOOLBAR
2598 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2599# endif
2600# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002601 if (gui.tabline != NULL)
2602 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002603# endif
2604#endif
2605
2606 return width;
2607}
2608
2609 static int
2610get_menu_tool_height(void)
2611{
2612 int height = 0;
2613
2614#ifdef FEAT_MENU
2615 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2616#endif
2617#ifdef FEAT_TOOLBAR
2618 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2619#endif
2620#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002621 if (gui.tabline != NULL)
2622 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002623#endif
2624
2625 return height;
2626}
2627
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002628/* This controls whether we can set the real window hints at
2629 * start-up when in a GtkPlug.
2630 * 0 = normal processing (default)
2631 * 1 = init. hints set, no-one's tried to reset since last check
2632 * 2 = init. hints set, attempt made to change hints
2633 */
2634static int init_window_hints_state = 0;
2635
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002636 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002637update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002638{
2639 static int old_width = 0;
2640 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002641 static int old_min_width = 0;
2642 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002643 static int old_char_width = 0;
2644 static int old_char_height = 0;
2645
2646 int width;
2647 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002648 int min_width;
2649 int min_height;
2650
2651 /* At start-up, don't try to set the hints until the initial
2652 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002653 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002654 */
2655 if (!(force_width && force_height) && init_window_hints_state > 0)
2656 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002657 /* Don't do it! */
2658 init_window_hints_state = 2;
2659 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002660 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002661
2662 /* This also needs to be done when the main window isn't there yet,
2663 * otherwise the hints don't work. */
2664 width = gui_get_base_width();
2665 height = gui_get_base_height();
2666# ifdef FEAT_MENU
2667 height += tabline_height() * gui.char_height;
2668# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002669 width += get_menu_tool_width();
2670 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002671
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002672 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002673 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002674 * 'set columns=' or init. -geom) we briefly set the min. to the size
2675 * we wish to be instead of the legitimate minimum so that we actually
2676 * resize correctly.
2677 */
2678 if (force_width && force_height)
2679 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002680 min_width = force_width;
2681 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002682 }
2683 else
2684 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002685 min_width = width + MIN_COLUMNS * gui.char_width;
2686 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002687 }
2688
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002689 /* Avoid an expose event when the size didn't change. */
2690 if (width != old_width
2691 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002692 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002693 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002694 || gui.char_width != old_char_width
2695 || gui.char_height != old_char_height)
2696 {
2697 GdkGeometry geometry;
2698 GdkWindowHints geometry_mask;
2699
2700 geometry.width_inc = gui.char_width;
2701 geometry.height_inc = gui.char_height;
2702 geometry.base_width = width;
2703 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002704 geometry.min_width = min_width;
2705 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002706 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2707 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002708 /* Using gui.formwin as geometry widget doesn't work as expected
2709 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2710 * in Vim confuse GTK+. */
2711 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2712 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002713 old_width = width;
2714 old_height = height;
2715 old_min_width = min_width;
2716 old_min_height = min_height;
2717 old_char_width = gui.char_width;
2718 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002719 }
2720}
2721
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722#ifdef FEAT_TOOLBAR
2723
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724/*
2725 * This extra effort wouldn't be necessary if we only used stock icons in the
2726 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2727 * shouldn't be treated differently, thus we do need this.
2728 */
2729 static void
2730icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2731{
2732 if (GTK_IS_IMAGE(widget))
2733 {
2734 GtkImage *image = (GtkImage *)widget;
2735
2736 /* User-defined icons are stored in a GtkIconSet */
2737 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2738 {
2739 GtkIconSet *icon_set;
2740 GtkIconSize icon_size;
2741
2742 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2743 icon_size = (GtkIconSize)(long)user_data;
2744
2745 gtk_icon_set_ref(icon_set);
2746 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2747 gtk_icon_set_unref(icon_set);
2748 }
2749 }
2750 else if (GTK_IS_CONTAINER(widget))
2751 {
2752 gtk_container_foreach((GtkContainer *)widget,
2753 &icon_size_changed_foreach,
2754 user_data);
2755 }
2756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
2758 static void
2759set_toolbar_style(GtkToolbar *toolbar)
2760{
2761 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 GtkIconSize size;
2763 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2766 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2767 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002768 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2770 style = GTK_TOOLBAR_BOTH;
2771 else if (toolbar_flags & TOOLBAR_TEXT)
2772 style = GTK_TOOLBAR_TEXT;
2773 else
2774 style = GTK_TOOLBAR_ICONS;
2775
2776 gtk_toolbar_set_style(toolbar, style);
2777 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2778
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 switch (tbis_flags)
2780 {
2781 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2782 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2783 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2784 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2785 default: size = GTK_ICON_SIZE_INVALID; break;
2786 }
2787 oldsize = gtk_toolbar_get_icon_size(toolbar);
2788
2789 if (size == GTK_ICON_SIZE_INVALID)
2790 {
2791 /* Let global user preferences decide the icon size. */
2792 gtk_toolbar_unset_icon_size(toolbar);
2793 size = gtk_toolbar_get_icon_size(toolbar);
2794 }
2795 if (size != oldsize)
2796 {
2797 gtk_container_foreach(GTK_CONTAINER(toolbar),
2798 &icon_size_changed_foreach,
2799 GINT_TO_POINTER((int)size));
2800 }
2801 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802}
2803
2804#endif /* FEAT_TOOLBAR */
2805
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002806#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2807static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002808static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002809static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002810static int clicked_page; /* page clicked in tab line */
2811
2812/*
2813 * Handle selecting an item in the tab line popup menu.
2814 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002815 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002816tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002817{
Bram Moolenaara5621492006-02-25 21:55:24 +00002818 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002819 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002820}
2821
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002822 static void
2823add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2824{
2825 GtkWidget *item;
2826 char_u *utf_text;
2827
2828 utf_text = CONVERT_TO_UTF8(text);
2829 item = gtk_menu_item_new_with_label((const char *)utf_text);
2830 gtk_widget_show(item);
2831 CONVERT_TO_UTF8_FREE(utf_text);
2832
2833 gtk_container_add(GTK_CONTAINER(menu), item);
2834 gtk_signal_connect(GTK_OBJECT(item), "activate",
2835 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002836 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002837}
2838
Bram Moolenaara5621492006-02-25 21:55:24 +00002839/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002840 * Create a menu for the tab line.
2841 */
2842 static GtkWidget *
2843create_tabline_menu(void)
2844{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002845 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002846
2847 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002848 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2849 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2850 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002851
2852 return menu;
2853}
2854
2855 static gboolean
2856on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2857{
2858 /* Was this button press event ? */
2859 if (event->type == GDK_BUTTON_PRESS)
2860 {
2861 GdkEventButton *bevent = (GdkEventButton *)event;
2862 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002863 int y = bevent->y;
2864 GtkWidget *tabwidget;
2865 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002866
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002867 /* When ignoring events return TRUE so that the selected page doesn't
2868 * change. */
2869 if (hold_gui_events
2870# ifdef FEAT_CMDWIN
2871 || cmdwin_type != 0
2872# endif
2873 )
2874 return TRUE;
2875
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002876 tabwin = gdk_window_at_pointer(&x, &y);
2877 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2878 clicked_page = (int)(long)gtk_object_get_user_data(
2879 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002880
2881 /* If the event was generated for 3rd button popup the menu. */
2882 if (bevent->button == 3)
2883 {
2884 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2885 bevent->button, bevent->time);
2886 /* We handled the event. */
2887 return TRUE;
2888 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002889 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002890 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002891 if (clicked_page == 0)
2892 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002893 /* Click after all tabs moves to next tab page. When "x" is
2894 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002895 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002896 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002897 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002898 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002899
Bram Moolenaara5621492006-02-25 21:55:24 +00002900 /* We didn't handle the event. */
2901 return FALSE;
2902}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002903
2904/*
2905 * Handle selecting one of the tabs.
2906 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002907 static void
2908on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002909 GtkNotebook *notebook UNUSED,
2910 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002911 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002912 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002913{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002914 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002915 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002916 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002917 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002918}
2919
2920/*
2921 * Show or hide the tabline.
2922 */
2923 void
2924gui_mch_show_tabline(int showit)
2925{
2926 if (gui.tabline == NULL)
2927 return;
2928
2929 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2930 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002931 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002932 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002933 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002934 if (showit)
2935 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002936 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002937
2938 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002939}
2940
2941/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002942 * Return TRUE when tabline is displayed.
2943 */
2944 int
2945gui_mch_showing_tabline(void)
2946{
2947 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002948 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002949}
2950
2951/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002952 * Update the labels of the tabline.
2953 */
2954 void
2955gui_mch_update_tabline(void)
2956{
2957 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002958 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002959 GtkWidget *label;
2960 tabpage_T *tp;
2961 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002962 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002963 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002964 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002965
2966 if (gui.tabline == NULL)
2967 return;
2968
2969 ignore_tabline_evt = TRUE;
2970
2971 /* Add a label for each tab page. They all contain the same text area. */
2972 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2973 {
2974 if (tp == curtab)
2975 curtabidx = nr;
2976
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002977 tab_num = nr + 1;
2978
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002979 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
2980 if (page == NULL)
2981 {
2982 /* Add notebook page */
2983 page = gtk_vbox_new(FALSE, 0);
2984 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002985 event_box = gtk_event_box_new();
2986 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002987 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002988 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002989 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002990 gtk_widget_show(label);
2991 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
2992 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002993 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002994 nr++);
2995 }
2996
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002997 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002998 gtk_object_set_user_data(GTK_OBJECT(event_box),
2999 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003000 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003001 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003002 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003003 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003004 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003005
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003006 get_tabline_label(tp, TRUE);
3007 labeltext = CONVERT_TO_UTF8(NameBuff);
3008 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3009 (const char *)labeltext, NULL);
3010 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003011 }
3012
3013 /* Remove any old labels. */
3014 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3015 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3016
3017 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003018 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003019
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003020 /* Make sure everything is in place before drawing text. */
3021 gui_mch_update();
3022
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003023 ignore_tabline_evt = FALSE;
3024}
3025
3026/*
3027 * Set the current tab to "nr". First tab is 1.
3028 */
3029 void
3030gui_mch_set_curtab(nr)
3031 int nr;
3032{
3033 if (gui.tabline == NULL)
3034 return;
3035
3036 ignore_tabline_evt = TRUE;
3037 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003038 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003039 ignore_tabline_evt = FALSE;
3040}
3041
3042#endif /* FEAT_GUI_TABLINE */
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003045 * Add selection targets for PRIMARY and CLIPBOARD selections.
3046 */
3047 void
3048gui_gtk_set_selection_targets(void)
3049{
3050 int i, j = 0;
3051 int n_targets = N_SELECTION_TARGETS;
3052 GtkTargetEntry targets[N_SELECTION_TARGETS];
3053
3054 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3055 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003056 /* OpenOffice tries to use TARGET_HTML and fails when it doesn't
3057 * return something, instead of trying another target. Therefore only
3058 * offer TARGET_HTML when it works. */
3059 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3060 n_targets--;
3061 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003062 targets[j++] = selection_targets[i];
3063 }
3064
3065 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3066 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3067 gtk_selection_add_targets(gui.drawarea,
3068 (GdkAtom)GDK_SELECTION_PRIMARY,
3069 targets, n_targets);
3070 gtk_selection_add_targets(gui.drawarea,
3071 (GdkAtom)clip_plus.gtk_sel_atom,
3072 targets, n_targets);
3073}
3074
3075/*
3076 * Set up for receiving DND items.
3077 */
3078 void
3079gui_gtk_set_dnd_targets(void)
3080{
3081 int i, j = 0;
3082 int n_targets = N_DND_TARGETS;
3083 GtkTargetEntry targets[N_DND_TARGETS];
3084
3085 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3086 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003087 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3088 n_targets--;
3089 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003090 targets[j++] = dnd_targets[i];
3091 }
3092
3093 gtk_drag_dest_unset(gui.drawarea);
3094 gtk_drag_dest_set(gui.drawarea,
3095 GTK_DEST_DEFAULT_ALL,
3096 targets, n_targets,
3097 GDK_ACTION_COPY);
3098}
3099
3100/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3102 * Returns OK for success, FAIL when the GUI can't be started.
3103 */
3104 int
3105gui_mch_init(void)
3106{
3107 GtkWidget *vbox;
3108
3109#ifdef FEAT_GUI_GNOME
3110 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3111 * exits on failure, but that's a non-issue because we already called
3112 * gtk_init_check() in gui_mch_init_check(). */
3113 if (using_gnome)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3115 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116#endif
3117 vim_free(gui_argv);
3118 gui_argv = NULL;
3119
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003120#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 /* Set the human-readable application name */
3122 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 /*
3125 * Force UTF-8 output no matter what the value of 'encoding' is.
3126 * did_set_string_option() in option.c prohibits changing 'termencoding'
3127 * to something else than UTF-8 if the GUI is in use.
3128 */
3129 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3130
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003131#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3135 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003136#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138#endif
3139
3140 /* Initialize values */
3141 gui.border_width = 2;
3142 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3143 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003144 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003146 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003148 /* LINTED: avoid warning: conversion to 'unsigned long' */
3149 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
3151 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003152 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154
3155 /* Set default foreground and background colors. */
3156 gui.norm_pixel = gui.def_norm_pixel;
3157 gui.back_pixel = gui.def_back_pixel;
3158
3159 if (gtk_socket_id != 0)
3160 {
3161 GtkWidget *plug;
3162
3163 /* Use GtkSocket from another app. */
3164#ifdef HAVE_GTK_MULTIHEAD
3165 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3166 gtk_socket_id);
3167#else
3168 plug = gtk_plug_new(gtk_socket_id);
3169#endif
3170 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3171 {
3172 gui.mainwin = plug;
3173 }
3174 else
3175 {
3176 g_warning("Connection to GTK+ socket (ID %u) failed",
3177 (unsigned int)gtk_socket_id);
3178 /* Pretend we never wanted it if it failed (get own window) */
3179 gtk_socket_id = 0;
3180 }
3181 }
3182
3183 if (gtk_socket_id == 0)
3184 {
3185#ifdef FEAT_GUI_GNOME
3186 if (using_gnome)
3187 {
3188 gui.mainwin = gnome_app_new("Vim", NULL);
3189# ifdef USE_XSMP
3190 /* Use the GNOME save-yourself functionality now. */
3191 xsmp_close();
3192# endif
3193 }
3194 else
3195#endif
3196 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3197 }
3198
3199 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 /* Create the PangoContext used for drawing all text. */
3202 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3203 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3206 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3207
3208 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3209 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3210
3211 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3212 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3213#ifdef HAVE_GTK_MULTIHEAD
3214 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3215 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 gui.accel_group = gtk_accel_group_new();
3218 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003220 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 vbox = gtk_vbox_new(FALSE, 0);
3222
3223#ifdef FEAT_GUI_GNOME
3224 if (using_gnome)
3225 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003226# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 /* automagically restore menubar/toolbar placement */
3228 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3229# endif
3230 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3231 }
3232 else
3233#endif
3234 {
3235 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3236 gtk_widget_show(vbox);
3237 }
3238
3239#ifdef FEAT_MENU
3240 /*
3241 * Create the menubar and handle
3242 */
3243 gui.menubar = gtk_menu_bar_new();
3244 gtk_widget_set_name(gui.menubar, "vim-menubar");
3245
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003246 /* Avoid that GTK takes <F10> away from us. */
3247 {
3248 GtkSettings *gtk_settings;
3249
3250 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3251 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3252 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003253
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255# ifdef FEAT_GUI_GNOME
3256 if (using_gnome)
3257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 BonoboDockItem *dockitem;
3259
3260 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3261 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3262 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003263 /* We don't want the menu to float. */
3264 bonobo_dock_item_set_behavior(dockitem,
3265 bonobo_dock_item_get_behavior(dockitem)
3266 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 }
3269 else
3270# endif /* FEAT_GUI_GNOME */
3271 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003272 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3273 * disabled in gui_init() later. */
3274 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3276 }
3277#endif /* FEAT_MENU */
3278
3279#ifdef FEAT_TOOLBAR
3280 /*
3281 * Create the toolbar and handle
3282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 /* some aesthetics on the toolbar */
3284 gtk_rc_parse_string(
3285 "style \"vim-toolbar-style\" {\n"
3286 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3287 "}\n"
3288 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3289 gui.toolbar = gtk_toolbar_new();
3290 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3292
3293# ifdef FEAT_GUI_GNOME
3294 if (using_gnome)
3295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 BonoboDockItem *dockitem;
3297
3298 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3299 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3300 GNOME_APP_TOOLBAR_NAME);
3301 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003302 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003303 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003304 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003305 bonobo_dock_item_get_behavior(dockitem)
3306 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 else
3310# endif /* FEAT_GUI_GNOME */
3311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3313 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3314 gtk_widget_show(gui.toolbar);
3315 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3316 }
3317#endif /* FEAT_TOOLBAR */
3318
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003319#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003320 /*
3321 * Use a Notebook for the tab pages labels. The labels are hidden by
3322 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003323 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003324 gui.tabline = gtk_notebook_new();
3325 gtk_widget_show(gui.tabline);
3326 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3327 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3328 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003329 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003330 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003331
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003332 tabline_tooltip = gtk_tooltips_new();
3333 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3334
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003335 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003336 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003337
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003338 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003339 page = gtk_vbox_new(FALSE, 0);
3340 gtk_widget_show(page);
3341 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3342 label = gtk_label_new("-Empty-");
3343 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003344 event_box = gtk_event_box_new();
3345 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003346 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003347 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003348 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003349 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003350 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003351
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003352 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003353 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003354
3355 /* Create a popup menu for the tab line and connect it. */
3356 tabline_menu = create_tabline_menu();
3357 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003358 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003359#endif
3360
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 gui.formwin = gtk_form_new();
3362 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3363 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3364
3365 gui.drawarea = gtk_drawing_area_new();
3366
3367 /* Determine which events we will filter. */
3368 gtk_widget_set_events(gui.drawarea,
3369 GDK_EXPOSURE_MASK |
3370 GDK_ENTER_NOTIFY_MASK |
3371 GDK_LEAVE_NOTIFY_MASK |
3372 GDK_BUTTON_PRESS_MASK |
3373 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 GDK_KEY_PRESS_MASK |
3376 GDK_KEY_RELEASE_MASK |
3377 GDK_POINTER_MOTION_MASK |
3378 GDK_POINTER_MOTION_HINT_MASK);
3379
3380 gtk_widget_show(gui.drawarea);
3381 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3382 gtk_widget_show(gui.formwin);
3383 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3384
3385 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3386 * and not the window. */
3387 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3388 : GTK_OBJECT(gui.drawarea),
3389 "key_press_event",
3390 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003391#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 /* Also forward key release events for the benefit of GTK+ 2 input
3393 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3394 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3395 : G_OBJECT(gui.drawarea),
3396 "key_release_event",
3397 G_CALLBACK(&key_release_event), NULL);
3398#endif
3399 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3400 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3401 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3402 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3403
3404 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3405 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3406
3407 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3408
3409#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3410 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3411 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3412#endif
3413
3414 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003415 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3417
3418 /*
3419 * Set clipboard specific atoms
3420 */
3421 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3424 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3425
3426 /*
3427 * Start out by adding the configured border width into the border offset.
3428 */
3429 gui.border_offset = gui.border_width;
3430
3431 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3432 GTK_SIGNAL_FUNC(visibility_event), NULL);
3433 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3434 GTK_SIGNAL_FUNC(expose_event), NULL);
3435
3436 /*
3437 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3438 * Only needed for some window managers.
3439 */
3440 if (vim_strchr(p_go, GO_POINTER) != NULL)
3441 {
3442 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3443 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3444 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3445 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3446 }
3447
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003448 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3449 * only its widgets. Arguably, this could be common code and we not use
3450 * the window focus at all, but let's be safe.
3451 */
3452 if (gtk_socket_id == 0)
3453 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003454 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3455 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3456 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3457 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003458 }
3459 else
3460 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003461 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3462 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3463 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3464 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003465#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003466 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3467 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3468 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3469 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003470#endif /* FEAT_GUI_TABLINE */
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3474 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3475 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3476 GTK_SIGNAL_FUNC(button_press_event), NULL);
3477 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3478 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3480 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
3482 /*
3483 * Add selection handler functions.
3484 */
3485 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3486 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3487 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3488 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3489
Bram Moolenaara76638f2010-06-05 12:49:46 +02003490 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
3492 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3493 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3494
3495 /* Pretend we don't have input focus, we will get an event if we do. */
3496 gui.in_focus = FALSE;
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return OK;
3499}
3500
3501#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3502/*
3503 * This is called from gui_start() after a fork() has been done.
3504 * We have to tell the session manager our new PID.
3505 */
3506 void
3507gui_mch_forked(void)
3508{
3509 if (using_gnome)
3510 {
3511 GnomeClient *client;
3512
3513 client = gnome_master_client();
3514
3515 if (client != NULL)
3516 gnome_client_set_process_id(client, getpid());
3517 }
3518}
3519#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3520
3521/*
3522 * Called when the foreground or background color has been changed.
3523 * This used to change the graphics contexts directly but we are
3524 * currently manipulating them where desired.
3525 */
3526 void
3527gui_mch_new_colors(void)
3528{
3529 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3530 {
3531 GdkColor color = { 0, 0, 0, 0 };
3532
3533 color.pixel = gui.back_pixel;
3534 gdk_window_set_background(gui.drawarea->window, &color);
3535 }
3536}
3537
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538/*
3539 * This signal informs us about the need to rearrange our sub-widgets.
3540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003542form_configure_event(GtkWidget *widget UNUSED,
3543 GdkEventConfigure *event,
3544 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003546 int usable_height = event->height;
3547
3548 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3549 * no. of char-heights), so we have to manually sanitise them.
3550 * Widths seem to sort themselves out, don't ask me why.
3551 */
3552 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003553 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003556 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 gtk_form_thaw(GTK_FORM(gui.formwin));
3558
3559 return TRUE;
3560}
3561
3562/*
3563 * Function called when window already closed.
3564 * We can't do much more here than to trying to preserve what had been done,
3565 * since the window is already inevitably going away.
3566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003568mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 /* Don't write messages to the GUI anymore */
3571 full_screen = FALSE;
3572
3573 gui.mainwin = NULL;
3574 gui.drawarea = NULL;
3575
3576 if (!exiting) /* only do anything if the destroy was unexpected */
3577 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003578 vim_strncpy(IObuff,
3579 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3580 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 preserve_exit();
3582 }
3583}
3584
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003585
3586/*
3587 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3588 * hints (and thus the required size from -geom), but that after that we
3589 * put the hints back to normal (the actual minimum size) so we may
3590 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003591 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003592 * only way we have of making ourselves bigger (by set lines/columns).
3593 * Thus set hints at start-up to ensure correct init. size, then a
3594 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003595 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003596 * We'll not let the default hints be set while this timer's active.
3597 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003598 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003599check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003600{
3601 if (init_window_hints_state == 1)
3602 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003603 /* Safe to use normal hints now */
3604 init_window_hints_state = 0;
3605 update_window_manager_hints(0, 0);
3606 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003607 }
3608
3609 /* Keep on trying */
3610 init_window_hints_state = 1;
3611 return TRUE;
3612}
3613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614/*
3615 * Open the GUI window which was created by a call to gui_mch_init().
3616 */
3617 int
3618gui_mch_open(void)
3619{
3620 guicolor_T fg_pixel = INVALCOLOR;
3621 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003622 guint pixel_width;
3623 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 /*
3626 * Allow setting a window role on the command line, or invent one
3627 * if none was specified. This is mainly useful for GNOME session
3628 * support; allowing the WM to restore window placement.
3629 */
3630 if (role_argument != NULL)
3631 {
3632 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3633 }
3634 else
3635 {
3636 char *role;
3637
3638 /* Invent a unique-enough ID string for the role */
3639 role = g_strdup_printf("vim-%u-%u-%u",
3640 (unsigned)mch_get_pid(),
3641 (unsigned)g_random_int(),
3642 (unsigned)time(NULL));
3643
3644 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3645 g_free(role);
3646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647
3648 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /* Determine user specified geometry, if present. */
3652 if (gui.geom != NULL)
3653 {
3654 int mask;
3655 unsigned int w, h;
3656 int x = 0;
3657 int y = 0;
3658
3659 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3660
3661 if (mask & WidthValue)
3662 Columns = w;
3663 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003664 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003665 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003666 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003668 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003669
3670 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3671 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3672
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003673 pixel_width += get_menu_tool_width();
3674 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003677 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003678 int ww, hh;
3679 gui_mch_get_screen_dimensions(&ww, &hh);
3680 hh += p_ghr + get_menu_tool_height();
3681 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003682 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003683 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003684 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003685 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 vim_free(gui.geom);
3689 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003690
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003691 /* From now until everyone's stopped trying to set the window hints
3692 * to their correct minimum values, stop them being set as we need
3693 * them to remain at our required size for the parent GtkSocket to
3694 * give us the right initial size.
3695 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003696 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003697 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003698 update_window_manager_hints(pixel_width, pixel_height);
3699 init_window_hints_state = 1;
3700 g_timeout_add(1000, check_startup_plug_hints, NULL);
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003704 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3705 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003706 /* For GTK2 changing the size of the form widget doesn't cause window
3707 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003708 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003709 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003710 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 if (foreground_argument != NULL)
3713 fg_pixel = gui_get_color((char_u *)foreground_argument);
3714 if (fg_pixel == INVALCOLOR)
3715 fg_pixel = gui_get_color((char_u *)"Black");
3716
3717 if (background_argument != NULL)
3718 bg_pixel = gui_get_color((char_u *)background_argument);
3719 if (bg_pixel == INVALCOLOR)
3720 bg_pixel = gui_get_color((char_u *)"White");
3721
3722 if (found_reverse_arg)
3723 {
3724 gui.def_norm_pixel = bg_pixel;
3725 gui.def_back_pixel = fg_pixel;
3726 }
3727 else
3728 {
3729 gui.def_norm_pixel = fg_pixel;
3730 gui.def_back_pixel = bg_pixel;
3731 }
3732
3733 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3734 * in a vimrc file) */
3735 set_normal_colors();
3736
3737 /* Check that none of the colors are the same as the background color */
3738 gui_check_colors();
3739
3740 /* Get the colors for the highlight groups (gui_check_colors() might have
3741 * changed them). */
3742 highlight_gui_started(); /* re-init colors and fonts */
3743
3744 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3745 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3746
3747#ifdef FEAT_HANGULIN
3748 hangul_keyboard_set();
3749#endif
3750
3751 /*
3752 * Notify the fixed area about the need to resize the contents of the
3753 * gui.formwin, which we use for random positioning of the included
3754 * components.
3755 *
3756 * We connect this signal deferred finally after anything is in place,
3757 * since this is intended to handle resizements coming from the window
3758 * manager upon us and should not interfere with what VIM is requesting
3759 * upon startup.
3760 */
3761 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3762 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3763
3764#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003765 /* Set up for receiving DND items. */
3766 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3769 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3770#endif
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003773 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (found_iconic_arg && gtk_socket_id == 0)
3775 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003778#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 unsigned long menu_handler = 0;
3780# ifdef FEAT_TOOLBAR
3781 unsigned long tool_handler = 0;
3782# endif
3783 /*
3784 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3785 * show when restoring the saved layout configuration. We can't just
3786 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3787 * a toplevel window and thus will be realized immediately. Instead,
3788 * connect signal handlers to hide the widgets just after they've been
3789 * marked visible, but before the main window is realized.
3790 */
3791 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3792 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3793 G_CALLBACK(&gtk_widget_hide),
3794 NULL);
3795# ifdef FEAT_TOOLBAR
3796 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3797 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3798 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3799 G_CALLBACK(&gtk_widget_hide),
3800 NULL);
3801# endif
3802#endif
3803 gtk_widget_show(gui.mainwin);
3804
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003805#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 if (menu_handler != 0)
3807 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3808# ifdef FEAT_TOOLBAR
3809 if (tool_handler != 0)
3810 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3811# endif
3812#endif
3813 }
3814
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 return OK;
3816}
3817
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003820gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 if (gui.mainwin != NULL)
3823 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824}
3825
3826/*
3827 * Get the position of the top left corner of the window.
3828 */
3829 int
3830gui_mch_get_winpos(int *x, int *y)
3831{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 return OK;
3834}
3835
3836/*
3837 * Set the position of the top left corner of the window to the given
3838 * coordinates.
3839 */
3840 void
3841gui_mch_set_winpos(int x, int y)
3842{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844}
3845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#if 0
3847static int resize_idle_installed = FALSE;
3848/*
3849 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3850 * the shell size doesn't exceed the window size, i.e. if the window manager
3851 * ignored our size request. Usually this happens if the window is maximized.
3852 *
3853 * FIXME: It'd be nice if we could find a little more orthodox solution.
3854 * See also the remark below in gui_mch_set_shellsize().
3855 *
3856 * DISABLED: When doing ":set lines+=1" this function would first invoke
3857 * gui_resize_shell() with the old size, then the normal callback would
3858 * report the new size through form_configure_event(). That caused the window
3859 * layout to be messed up.
3860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 static gboolean
3862force_shell_resize_idle(gpointer data)
3863{
3864 if (gui.mainwin != NULL
3865 && GTK_WIDGET_REALIZED(gui.mainwin)
3866 && GTK_WIDGET_VISIBLE(gui.mainwin))
3867 {
3868 int width;
3869 int height;
3870
3871 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3872
3873 width -= get_menu_tool_width();
3874 height -= get_menu_tool_height();
3875
3876 gui_resize_shell(width, height);
3877 }
3878
3879 resize_idle_installed = FALSE;
3880 return FALSE; /* don't call me again */
3881}
3882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar09736232009-09-23 16:14:49 +00003884/*
3885 * Return TRUE if the main window is maximized.
3886 */
3887 int
3888gui_mch_maximized()
3889{
3890 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3891 && (gdk_window_get_state(gui.mainwin->window)
3892 & GDK_WINDOW_STATE_MAXIMIZED));
3893}
3894
3895/*
3896 * Unmaximize the main window
3897 */
3898 void
3899gui_mch_unmaximize()
3900{
3901 if (gui.mainwin != NULL)
3902 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3903}
Bram Moolenaar09736232009-09-23 16:14:49 +00003904
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905/*
3906 * Set the windows size.
3907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 void
3909gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003910 int min_width UNUSED, int min_height UNUSED,
3911 int base_width UNUSED, int base_height UNUSED,
3912 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 /* give GTK+ a chance to put all widget's into place */
3915 gui_mch_update();
3916
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003917 /* this will cause the proper resizement to happen too */
3918 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003919 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003920
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003922 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 * main window instead. */
3924 width += get_menu_tool_width();
3925 height += get_menu_tool_height();
3926
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003927 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003928 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003929 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003930 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003932# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 if (!resize_idle_installed)
3934 {
3935 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3936 &force_shell_resize_idle, NULL, NULL);
3937 resize_idle_installed = TRUE;
3938 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 /*
3941 * Wait until all events are processed to prevent a crash because the
3942 * real size of the drawing area doesn't reflect Vim's internal ideas.
3943 *
3944 * This is a bit of a hack, since Vim is a terminal application with a GUI
3945 * on top, while the GUI expects to be the boss.
3946 */
3947 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948}
3949
3950
3951/*
3952 * The screen size is used to make sure the initial window doesn't get bigger
3953 * than the screen. This subtracts some room for menubar, toolbar and window
3954 * decorations.
3955 */
3956 void
3957gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
3958{
3959#ifdef HAVE_GTK_MULTIHEAD
3960 GdkScreen* screen;
3961
3962 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
3963 screen = gtk_widget_get_screen(gui.mainwin);
3964 else
3965 screen = gdk_screen_get_default();
3966
3967 *screen_w = gdk_screen_get_width(screen);
3968 *screen_h = gdk_screen_get_height(screen) - p_ghr;
3969#else
3970 *screen_w = gdk_screen_width();
3971 /* Subtract 'guiheadroom' from the height to allow some room for the
3972 * window manager (task list and window title bar). */
3973 *screen_h = gdk_screen_height() - p_ghr;
3974#endif
3975
3976 /*
3977 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
3978 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02003979 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 * This should be completely changed later.
3981 */
3982 *screen_w -= get_menu_tool_width();
3983 *screen_h -= get_menu_tool_height();
3984}
3985
3986#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003988gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (title != NULL && output_conv.vc_type != CONV_NONE)
3991 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
3993 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
3994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (output_conv.vc_type != CONV_NONE)
3996 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998#endif /* FEAT_TITLE */
3999
4000#if defined(FEAT_MENU) || defined(PROTO)
4001 void
4002gui_mch_enable_menu(int showit)
4003{
4004 GtkWidget *widget;
4005
4006# ifdef FEAT_GUI_GNOME
4007 if (using_gnome)
4008 widget = gui.menubar_h;
4009 else
4010# endif
4011 widget = gui.menubar;
4012
Bram Moolenaar18144c82006-04-12 21:52:12 +00004013 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4014 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 if (showit)
4017 gtk_widget_show(widget);
4018 else
4019 gtk_widget_hide(widget);
4020
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004021 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 }
4023}
4024#endif /* FEAT_MENU */
4025
4026#if defined(FEAT_TOOLBAR) || defined(PROTO)
4027 void
4028gui_mch_show_toolbar(int showit)
4029{
4030 GtkWidget *widget;
4031
4032 if (gui.toolbar == NULL)
4033 return;
4034
4035# ifdef FEAT_GUI_GNOME
4036 if (using_gnome)
4037 widget = gui.toolbar_h;
4038 else
4039# endif
4040 widget = gui.toolbar;
4041
4042 if (showit)
4043 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4044
4045 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4046 {
4047 if (showit)
4048 gtk_widget_show(widget);
4049 else
4050 gtk_widget_hide(widget);
4051
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004052 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054}
4055#endif /* FEAT_TOOLBAR */
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057/*
4058 * Check if a given font is a CJK font. This is done in a very crude manner. It
4059 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4060 * font. Consequently, this function cannot be used as a general purpose check
4061 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4062 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4063 */
4064 static int
4065is_cjk_font(PangoFontDescription *font_desc)
4066{
4067 static const char * const cjk_langs[] =
4068 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4069
4070 PangoFont *font;
4071 unsigned i;
4072 int is_cjk = FALSE;
4073
4074 font = pango_context_load_font(gui.text_context, font_desc);
4075
4076 if (font == NULL)
4077 return FALSE;
4078
4079 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4080 {
4081 PangoCoverage *coverage;
4082 gunichar uc;
4083
4084 coverage = pango_font_get_coverage(
4085 font, pango_language_from_string(cjk_langs[i]));
4086
4087 if (coverage != NULL)
4088 {
4089 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4090 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4091 pango_coverage_unref(coverage);
4092 }
4093 }
4094
4095 g_object_unref(font);
4096
4097 return is_cjk;
4098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
Bram Moolenaar231334e2005-07-25 20:46:57 +00004100/*
4101 * Adjust gui.char_height (after 'linespace' was changed).
4102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004104gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 PangoFontMetrics *metrics;
4107 int ascent;
4108 int descent;
4109
4110 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4111 pango_context_get_language(gui.text_context));
4112 ascent = pango_font_metrics_get_ascent(metrics);
4113 descent = pango_font_metrics_get_descent(metrics);
4114
4115 pango_font_metrics_unref(metrics);
4116
4117 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004118 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004119 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 /* A not-positive value of char_height may crash Vim. Only happens
4123 * if 'linespace' is negative (which does make sense sometimes). */
4124 gui.char_ascent = MAX(gui.char_ascent, 0);
4125 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4126
4127 return OK;
4128}
4129
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130/*
4131 * Put up a font dialog and return the selected font name in allocated memory.
4132 * "oldval" is the previous value. Return NULL when cancelled.
4133 * This should probably go into gui_gtk.c. Hmm.
4134 * FIXME:
4135 * The GTK2 font selection dialog has no filtering API. So we could either
4136 * a) implement our own (possibly copying the code from somewhere else) or
4137 * b) just live with it.
4138 */
4139 char_u *
4140gui_mch_font_dialog(char_u *oldval)
4141{
4142 GtkWidget *dialog;
4143 int response;
4144 char_u *fontname = NULL;
4145 char_u *oldname;
4146
4147 dialog = gtk_font_selection_dialog_new(NULL);
4148
4149 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4150 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4151
4152 if (oldval != NULL && oldval[0] != NUL)
4153 {
4154 if (output_conv.vc_type != CONV_NONE)
4155 oldname = string_convert(&output_conv, oldval, NULL);
4156 else
4157 oldname = oldval;
4158
4159 /* Annoying bug in GTK (or Pango): if the font name does not include a
4160 * size, zero is used. Use default point size ten. */
4161 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4162 {
4163 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4164
4165 if (p != NULL)
4166 {
4167 STRCPY(p + STRLEN(p), " 10");
4168 if (oldname != oldval)
4169 vim_free(oldname);
4170 oldname = p;
4171 }
4172 }
4173
4174 gtk_font_selection_dialog_set_font_name(
4175 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4176
4177 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004178 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004180 else
4181 gtk_font_selection_dialog_set_font_name(
4182 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183
4184 response = gtk_dialog_run(GTK_DIALOG(dialog));
4185
4186 if (response == GTK_RESPONSE_OK)
4187 {
4188 char *name;
4189
4190 name = gtk_font_selection_dialog_get_font_name(
4191 GTK_FONT_SELECTION_DIALOG(dialog));
4192 if (name != NULL)
4193 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004194 char_u *p;
4195
4196 /* Apparently some font names include a comma, need to escape
4197 * that, because in 'guifont' it separates names. */
4198 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004200 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004201 {
4202 fontname = string_convert(&input_conv, p, NULL);
4203 vim_free(p);
4204 }
4205 else
4206 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208 }
4209
4210 if (response != GTK_RESPONSE_NONE)
4211 gtk_widget_destroy(dialog);
4212
4213 return fontname;
4214}
4215
4216/*
4217 * Some monospace fonts don't support a bold weight, and fall back
4218 * silently to the regular weight. But this is no good since our text
4219 * drawing function can emulate bold by overstriking. So let's try
4220 * to detect whether bold weight is actually available and emulate it
4221 * otherwise.
4222 *
4223 * Note that we don't need to check for italic style since Xft can
4224 * emulate italic on its own, provided you have a proper fontconfig
4225 * setup. We wouldn't be able to emulate it in Vim anyway.
4226 */
4227 static void
4228get_styled_font_variants(void)
4229{
4230 PangoFontDescription *bold_font_desc;
4231 PangoFont *plain_font;
4232 PangoFont *bold_font;
4233
4234 gui.font_can_bold = FALSE;
4235
4236 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4237
4238 if (plain_font == NULL)
4239 return;
4240
4241 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4242 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4243
4244 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4245 /*
4246 * The comparison relies on the unique handle nature of a PangoFont*,
4247 * i.e. it's assumed that a different PangoFont* won't refer to the
4248 * same font. Seems to work, and failing here isn't critical anyway.
4249 */
4250 if (bold_font != NULL)
4251 {
4252 gui.font_can_bold = (bold_font != plain_font);
4253 g_object_unref(bold_font);
4254 }
4255
4256 pango_font_description_free(bold_font_desc);
4257 g_object_unref(plain_font);
4258}
4259
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260static PangoEngineShape *default_shape_engine = NULL;
4261
4262/*
4263 * Create a map from ASCII characters in the range [32,126] to glyphs
4264 * of the current font. This is used by gui_gtk2_draw_string() to skip
4265 * the itemize and shaping process for the most common case.
4266 */
4267 static void
4268ascii_glyph_table_init(void)
4269{
4270 char_u ascii_chars[128];
4271 PangoAttrList *attr_list;
4272 GList *item_list;
4273 int i;
4274
4275 if (gui.ascii_glyphs != NULL)
4276 pango_glyph_string_free(gui.ascii_glyphs);
4277 if (gui.ascii_font != NULL)
4278 g_object_unref(gui.ascii_font);
4279
4280 gui.ascii_glyphs = NULL;
4281 gui.ascii_font = NULL;
4282
4283 /* For safety, fill in question marks for the control characters. */
4284 for (i = 0; i < 32; ++i)
4285 ascii_chars[i] = '?';
4286 for (; i < 127; ++i)
4287 ascii_chars[i] = i;
4288 ascii_chars[i] = '?';
4289
4290 attr_list = pango_attr_list_new();
4291 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4292 0, sizeof(ascii_chars), attr_list, NULL);
4293
4294 if (item_list != NULL && item_list->next == NULL) /* play safe */
4295 {
4296 PangoItem *item;
4297 int width;
4298
4299 item = (PangoItem *)item_list->data;
4300 width = gui.char_width * PANGO_SCALE;
4301
4302 /* Remember the shape engine used for ASCII. */
4303 default_shape_engine = item->analysis.shape_engine;
4304
4305 gui.ascii_font = item->analysis.font;
4306 g_object_ref(gui.ascii_font);
4307
4308 gui.ascii_glyphs = pango_glyph_string_new();
4309
4310 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4311 &item->analysis, gui.ascii_glyphs);
4312
4313 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4314
4315 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4316 {
4317 PangoGlyphGeometry *geom;
4318
4319 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4320 geom->x_offset += MAX(0, width - geom->width) / 2;
4321 geom->width = width;
4322 }
4323 }
4324
4325 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4326 g_list_free(item_list);
4327 pango_attr_list_unref(attr_list);
4328}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329
4330/*
4331 * Initialize Vim to use the font or fontset with the given name.
4332 * Return FAIL if the font could not be loaded, OK otherwise.
4333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004335gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 PangoFontDescription *font_desc;
4338 PangoLayout *layout;
4339 int width;
4340
4341 /* If font_name is NULL, this means to use the default, which should
4342 * be present on all proper Pango/fontconfig installations. */
4343 if (font_name == NULL)
4344 font_name = (char_u *)DEFAULT_FONT;
4345
4346 font_desc = gui_mch_get_font(font_name, FALSE);
4347
4348 if (font_desc == NULL)
4349 return FAIL;
4350
4351 gui_mch_free_font(gui.norm_font);
4352 gui.norm_font = font_desc;
4353
4354 pango_context_set_font_description(gui.text_context, font_desc);
4355
4356 layout = pango_layout_new(gui.text_context);
4357 pango_layout_set_text(layout, "MW", 2);
4358 pango_layout_get_size(layout, &width, NULL);
4359 /*
4360 * Set char_width to half the width obtained from pango_layout_get_size()
4361 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4362 * Pango to use the same width for both non-CJK characters (e.g. Latin
4363 * letters and numbers) and CJK characters. This results in 's p a c e d
4364 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4365 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4366 * width for CJK fonts.
4367 *
4368 * For related bugs, see:
4369 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4370 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4371 *
4372 * With this, for all four of the following cases, Vim works fine:
4373 * guifont=CJK_fixed_width_font
4374 * guifont=Non_CJK_fixed_font
4375 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4376 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4377 */
4378 if (is_cjk_font(gui.norm_font))
4379 {
4380 int cjk_width;
4381
4382 /* Measure the text extent of U+4E00 and U+4E8C */
4383 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4384 pango_layout_get_size(layout, &cjk_width, NULL);
4385
4386 if (width == cjk_width) /* Xft not patched */
4387 width /= 2;
4388 }
4389 g_object_unref(layout);
4390
4391 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4392
4393 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4394 if (gui.char_width <= 0)
4395 gui.char_width = 8;
4396
Bram Moolenaar231334e2005-07-25 20:46:57 +00004397 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 /* Set the fontname, which will be used for information purposes */
4400 hl_set_font_name(font_name);
4401
4402 get_styled_font_variants();
4403 ascii_glyph_table_init();
4404
4405 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4406 if (gui.wide_font != NULL
4407 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4408 {
4409 pango_font_description_free(gui.wide_font);
4410 gui.wide_font = NULL;
4411 }
4412
Bram Moolenaare161c792009-11-03 17:13:59 +00004413 if (gui_mch_maximized())
4414 {
4415 int w, h;
4416
4417 /* Update lines and columns in accordance with the new font, keep the
4418 * window maximized. */
4419 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4420 w -= get_menu_tool_width();
4421 h -= get_menu_tool_height();
4422 gui_resize_shell(w, h);
4423 }
4424 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004425 {
4426 /* Preserve the logical dimensions of the screen. */
4427 update_window_manager_hints(0, 0);
4428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
4430 return OK;
4431}
4432
4433/*
4434 * Get a reference to the font "name".
4435 * Return zero for failure.
4436 */
4437 GuiFont
4438gui_mch_get_font(char_u *name, int report_error)
4439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
4442 /* can't do this when GUI is not running */
4443 if (!gui.in_use || name == NULL)
4444 return NULL;
4445
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (output_conv.vc_type != CONV_NONE)
4447 {
4448 char_u *buf;
4449
4450 buf = string_convert(&output_conv, name, NULL);
4451 if (buf != NULL)
4452 {
4453 font = pango_font_description_from_string((const char *)buf);
4454 vim_free(buf);
4455 }
4456 else
4457 font = NULL;
4458 }
4459 else
4460 font = pango_font_description_from_string((const char *)name);
4461
4462 if (font != NULL)
4463 {
4464 PangoFont *real_font;
4465
4466 /* pango_context_load_font() bails out if no font size is set */
4467 if (pango_font_description_get_size(font) <= 0)
4468 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4469
4470 real_font = pango_context_load_font(gui.text_context, font);
4471
4472 if (real_font == NULL)
4473 {
4474 pango_font_description_free(font);
4475 font = NULL;
4476 }
4477 else
4478 g_object_unref(real_font);
4479 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480
4481 if (font == NULL)
4482 {
4483 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004484 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 return NULL;
4486 }
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 return font;
4489}
4490
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004491#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004492/*
4493 * Return the name of font "font" in allocated memory.
4494 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004495 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004496gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004497{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004498 if (font != NOFONT)
4499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004500 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004501
Bram Moolenaar89d40322006-08-29 15:30:07 +00004502 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004504 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004505
Bram Moolenaar89d40322006-08-29 15:30:07 +00004506 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004507 return s;
4508 }
4509 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004510 return NULL;
4511}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004512#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514/*
4515 * If a font is not going to be used, free its structure.
4516 */
4517 void
4518gui_mch_free_font(GuiFont font)
4519{
4520 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522}
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524/*
4525 * Return the Pixel value (color) for the given color name. This routine was
4526 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4527 * Programmer's Guide.
4528 * Return INVALCOLOR for error.
4529 */
4530 guicolor_T
4531gui_mch_get_color(char_u *name)
4532{
4533 /* A number of colors that some X11 systems don't have */
4534 static const char *const vimnames[][2] =
4535 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004536 {"LightRed", "#FFBBBB"},
4537 {"LightGreen", "#88FF88"},
4538 {"LightMagenta","#FFBBFF"},
4539 {"DarkCyan", "#008888"},
4540 {"DarkBlue", "#0000BB"},
4541 {"DarkRed", "#BB0000"},
4542 {"DarkMagenta", "#BB00BB"},
4543 {"DarkGrey", "#BBBBBB"},
4544 {"DarkYellow", "#BBBB00"},
4545 {"Gray10", "#1A1A1A"},
4546 {"Grey10", "#1A1A1A"},
4547 {"Gray20", "#333333"},
4548 {"Grey20", "#333333"},
4549 {"Gray30", "#4D4D4D"},
4550 {"Grey30", "#4D4D4D"},
4551 {"Gray40", "#666666"},
4552 {"Grey40", "#666666"},
4553 {"Gray50", "#7F7F7F"},
4554 {"Grey50", "#7F7F7F"},
4555 {"Gray60", "#999999"},
4556 {"Grey60", "#999999"},
4557 {"Gray70", "#B3B3B3"},
4558 {"Grey70", "#B3B3B3"},
4559 {"Gray80", "#CCCCCC"},
4560 {"Grey80", "#CCCCCC"},
4561 {"Gray90", "#E5E5E5"},
4562 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {NULL, NULL}
4564 };
4565
4566 if (!gui.in_use) /* can't do this when GUI not running */
4567 return INVALCOLOR;
4568
4569 while (name != NULL)
4570 {
4571 GdkColor color;
4572 int parsed;
4573 int i;
4574
4575 parsed = gdk_color_parse((const char *)name, &color);
4576
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (parsed)
4578 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4580 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 return (guicolor_T)color.pixel;
4582 }
4583 /* add a few builtin names and try again */
4584 for (i = 0; ; ++i)
4585 {
4586 if (vimnames[i][0] == NULL)
4587 {
4588 name = NULL;
4589 break;
4590 }
4591 if (STRICMP(name, vimnames[i][0]) == 0)
4592 {
4593 name = (char_u *)vimnames[i][1];
4594 break;
4595 }
4596 }
4597 }
4598
4599 return INVALCOLOR;
4600}
4601
4602/*
4603 * Set the current text foreground color.
4604 */
4605 void
4606gui_mch_set_fg_color(guicolor_T color)
4607{
4608 gui.fgcolor->pixel = (unsigned long)color;
4609}
4610
4611/*
4612 * Set the current text background color.
4613 */
4614 void
4615gui_mch_set_bg_color(guicolor_T color)
4616{
4617 gui.bgcolor->pixel = (unsigned long)color;
4618}
4619
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004620/*
4621 * Set the current text special color.
4622 */
4623 void
4624gui_mch_set_sp_color(guicolor_T color)
4625{
4626 gui.spcolor->pixel = (unsigned long)color;
4627}
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629/*
4630 * Function-like convenience macro for the sake of efficiency.
4631 */
4632#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4633 G_STMT_START{ \
4634 PangoAttribute *tmp_attr_; \
4635 tmp_attr_ = (Attribute); \
4636 tmp_attr_->start_index = (Start); \
4637 tmp_attr_->end_index = (End); \
4638 pango_attr_list_insert((AttrList), tmp_attr_); \
4639 }G_STMT_END
4640
4641 static void
4642apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4643{
4644 char_u *start = NULL;
4645 char_u *p;
4646 int uc;
4647
4648 for (p = s; p < s + len; p += utf_byte2len(*p))
4649 {
4650 uc = utf_ptr2char(p);
4651
4652 if (start == NULL)
4653 {
4654 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4655 start = p;
4656 }
4657 else if (uc < 0x80 /* optimization shortcut */
4658 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4659 {
4660 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4661 attr_list, start - s, p - s);
4662 start = NULL;
4663 }
4664 }
4665
4666 if (start != NULL)
4667 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4668 attr_list, start - s, len);
4669}
4670
4671 static int
4672count_cluster_cells(char_u *s, PangoItem *item,
4673 PangoGlyphString* glyphs, int i,
4674 int *cluster_width,
4675 int *last_glyph_rbearing)
4676{
4677 char_u *p;
4678 int next; /* glyph start index of next cluster */
4679 int start, end; /* string segment of current cluster */
4680 int width; /* real cluster width in Pango units */
4681 int uc;
4682 int cellcount = 0;
4683
4684 width = glyphs->glyphs[i].geometry.width;
4685
4686 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4687 {
4688 if (glyphs->glyphs[next].attr.is_cluster_start)
4689 break;
4690 else if (glyphs->glyphs[next].geometry.width > width)
4691 width = glyphs->glyphs[next].geometry.width;
4692 }
4693
4694 start = item->offset + glyphs->log_clusters[i];
4695 end = item->offset + ((next < glyphs->num_glyphs) ?
4696 glyphs->log_clusters[next] : item->length);
4697
4698 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4699 {
4700 uc = utf_ptr2char(p);
4701 if (uc < 0x80)
4702 ++cellcount;
4703 else if (!utf_iscomposing(uc))
4704 cellcount += utf_char2cells(uc);
4705 }
4706
4707 if (last_glyph_rbearing != NULL
4708 && cellcount > 0 && next == glyphs->num_glyphs)
4709 {
4710 PangoRectangle ink_rect;
4711 /*
4712 * If a certain combining mark had to be taken from a non-monospace
4713 * font, we have to compensate manually by adapting x_offset according
4714 * to the ink extents of the previous glyph.
4715 */
4716 pango_font_get_glyph_extents(item->analysis.font,
4717 glyphs->glyphs[i].glyph,
4718 &ink_rect, NULL);
4719
4720 if (PANGO_RBEARING(ink_rect) > 0)
4721 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4722 }
4723
4724 if (cellcount > 0)
4725 *cluster_width = width;
4726
4727 return cellcount;
4728}
4729
4730/*
4731 * If there are only combining characters in the cluster, we cannot just
4732 * change the width of the previous glyph since there is none. Therefore
4733 * some guesswork is needed.
4734 *
4735 * If ink_rect.x is negative Pango apparently has taken care of the composing
4736 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4737 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004738 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 *
4740 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4741 * the position of the previous glyph. Apparently this happens only with old
4742 * X fonts which don't provide the special combining information needed by
4743 * Pango.
4744 */
4745 static void
4746setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4747 int last_cellcount, int last_cluster_width,
4748 int last_glyph_rbearing)
4749{
4750 PangoRectangle ink_rect;
4751 PangoRectangle logical_rect;
4752 int width;
4753
4754 width = last_cellcount * gui.char_width * PANGO_SCALE;
4755 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4756 glyph->geometry.width = 0;
4757
4758 pango_font_get_glyph_extents(item->analysis.font,
4759 glyph->glyph,
4760 &ink_rect, &logical_rect);
4761 if (ink_rect.x < 0)
4762 {
4763 glyph->geometry.x_offset += last_glyph_rbearing;
4764 glyph->geometry.y_offset = logical_rect.height
4765 - (gui.char_height - p_linespace) * PANGO_SCALE;
4766 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004767 else
4768 /* If the accent width is smaller than the cluster width, position it
4769 * in the middle. */
4770 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771}
4772
4773 static void
4774draw_glyph_string(int row, int col, int num_cells, int flags,
4775 PangoFont *font, PangoGlyphString *glyphs)
4776{
4777 if (!(flags & DRAW_TRANSP))
4778 {
4779 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4780
4781 gdk_draw_rectangle(gui.drawarea->window,
4782 gui.text_gc,
4783 TRUE,
4784 FILL_X(col),
4785 FILL_Y(row),
4786 num_cells * gui.char_width,
4787 gui.char_height);
4788 }
4789
4790 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4791
4792 gdk_draw_glyphs(gui.drawarea->window,
4793 gui.text_gc,
4794 font,
4795 TEXT_X(col),
4796 TEXT_Y(row),
4797 glyphs);
4798
4799 /* redraw the contents with an offset of 1 to emulate bold */
4800 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4801 gdk_draw_glyphs(gui.drawarea->window,
4802 gui.text_gc,
4803 font,
4804 TEXT_X(col) + 1,
4805 TEXT_Y(row),
4806 glyphs);
4807}
4808
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004809/*
4810 * Draw underline and undercurl at the bottom of the character cell.
4811 */
4812 static void
4813draw_under(int flags, int row, int col, int cells)
4814{
4815 int i;
4816 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004817 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004818 int y = FILL_Y(row + 1) - 1;
4819
4820 /* Undercurl: draw curl at the bottom of the character cell. */
4821 if (flags & DRAW_UNDERC)
4822 {
4823 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4824 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4825 {
4826 offset = val[i % 8];
4827 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4828 }
4829 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4830 }
4831
4832 /* Underline: draw a line at the bottom of the character cell. */
4833 if (flags & DRAW_UNDERL)
4834 {
4835 /* When p_linespace is 0, overwrite the bottom row of pixels.
4836 * Otherwise put the line just below the character. */
4837 if (p_linespace > 1)
4838 y -= p_linespace - 1;
4839 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4840 FILL_X(col), y,
4841 FILL_X(col + cells) - 1, y);
4842 }
4843}
4844
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 int
4846gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4847{
4848 GdkRectangle area; /* area for clip mask */
4849 PangoGlyphString *glyphs; /* glyphs of current item */
4850 int column_offset = 0; /* column offset in cells */
4851 int i;
4852 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4853 char_u *new_conv_buf;
4854 int convlen;
4855 char_u *sp, *bp;
4856 int plen;
4857
4858 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4859 return len;
4860
4861 if (output_conv.vc_type != CONV_NONE)
4862 {
4863 /*
4864 * Convert characters from 'encoding' to 'termencoding', which is set
4865 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4866 * prohibits changing this to something else than UTF-8 if the GUI is
4867 * in use.
4868 */
4869 convlen = len;
4870 conv_buf = string_convert(&output_conv, s, &convlen);
4871 g_return_val_if_fail(conv_buf != NULL, len);
4872
4873 /* Correct for differences in char width: some chars are
4874 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4875 * compensate for that. */
4876 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4877 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004878 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4880 {
4881 new_conv_buf = alloc(convlen + 2);
4882 if (new_conv_buf == NULL)
4883 return len;
4884 plen += bp - conv_buf;
4885 mch_memmove(new_conv_buf, conv_buf, plen);
4886 new_conv_buf[plen] = ' ';
4887 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4888 convlen - plen + 1);
4889 vim_free(conv_buf);
4890 conv_buf = new_conv_buf;
4891 ++convlen;
4892 bp = conv_buf + plen;
4893 plen = 1;
4894 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004895 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 bp += plen;
4897 }
4898 s = conv_buf;
4899 len = convlen;
4900 }
4901
4902 /*
4903 * Restrict all drawing to the current screen line in order to prevent
4904 * fuzzy font lookups from messing up the screen.
4905 */
4906 area.x = gui.border_offset;
4907 area.y = FILL_Y(row);
4908 area.width = gui.num_cols * gui.char_width;
4909 area.height = gui.char_height;
4910
4911 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4912 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4913
4914 glyphs = pango_glyph_string_new();
4915
4916 /*
4917 * Optimization hack: If possible, skip the itemize and shaping process
4918 * for pure ASCII strings. This optimization is particularly effective
4919 * because Vim draws space characters to clear parts of the screen.
4920 */
4921 if (!(flags & DRAW_ITALIC)
4922 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4923 && gui.ascii_glyphs != NULL)
4924 {
4925 char_u *p;
4926
4927 for (p = s; p < s + len; ++p)
4928 if (*p & 0x80)
4929 goto not_ascii;
4930
4931 pango_glyph_string_set_size(glyphs, len);
4932
4933 for (i = 0; i < len; ++i)
4934 {
4935 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4936 glyphs->log_clusters[i] = i;
4937 }
4938
4939 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
4940
4941 column_offset = len;
4942 }
4943 else
4944not_ascii:
4945 {
4946 PangoAttrList *attr_list;
4947 GList *item_list;
4948 int cluster_width;
4949 int last_glyph_rbearing;
4950 int cells = 0; /* cells occupied by current cluster */
4951
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004952 /* Safety check: pango crashes when invoked with invalid utf-8
4953 * characters. */
4954 if (!utf_valid_string(s, s + len))
4955 {
4956 column_offset = len;
4957 goto skipitall;
4958 }
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 /* original width of the current cluster */
4961 cluster_width = PANGO_SCALE * gui.char_width;
4962
4963 /* right bearing of the last non-composing glyph */
4964 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
4965
4966 attr_list = pango_attr_list_new();
4967
4968 /* If 'guifontwide' is set then use that for double-width characters.
4969 * Otherwise just go with 'guifont' and let Pango do its thing. */
4970 if (gui.wide_font != NULL)
4971 apply_wide_font_attr(s, len, attr_list);
4972
4973 if ((flags & DRAW_BOLD) && gui.font_can_bold)
4974 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
4975 attr_list, 0, len);
4976 if (flags & DRAW_ITALIC)
4977 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
4978 attr_list, 0, len);
4979 /*
4980 * Break the text into segments with consistent directional level
4981 * and shaping engine. Pure Latin text needs only a single segment,
4982 * so there's no need to worry about the loop's efficiency. Better
4983 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
4984 */
4985 item_list = pango_itemize(gui.text_context,
4986 (const char *)s, 0, len, attr_list, NULL);
4987
4988 while (item_list != NULL)
4989 {
4990 PangoItem *item;
4991 int item_cells = 0; /* item length in cells */
4992
4993 item = (PangoItem *)item_list->data;
4994 item_list = g_list_delete_link(item_list, item_list);
4995 /*
4996 * Increment the bidirectional embedding level by 1 if it is not
4997 * even. An odd number means the output will be RTL, but we don't
4998 * want that since Vim handles right-to-left text on its own. It
4999 * would probably be sufficient to just set level = 0, but you can
5000 * never know :)
5001 *
5002 * Unfortunately we can't take advantage of Pango's ability to
5003 * render both LTR and RTL at the same time. In order to support
5004 * that, Vim's main screen engine would have to make use of Pango
5005 * functionality.
5006 */
5007 item->analysis.level = (item->analysis.level + 1) & (~1U);
5008
5009 /* HACK: Overrule the shape engine, we don't want shaping to be
5010 * done, because drawing the cursor would change the display. */
5011 item->analysis.shape_engine = default_shape_engine;
5012
5013 pango_shape((const char *)s + item->offset, item->length,
5014 &item->analysis, glyphs);
5015 /*
5016 * Fixed-width hack: iterate over the array and assign a fixed
5017 * width to each glyph, thus overriding the choice made by the
5018 * shaping engine. We use utf_char2cells() to determine the
5019 * number of cells needed.
5020 *
5021 * Also perform all kind of dark magic to get composing
5022 * characters right (and pretty too of course).
5023 */
5024 for (i = 0; i < glyphs->num_glyphs; ++i)
5025 {
5026 PangoGlyphInfo *glyph;
5027
5028 glyph = &glyphs->glyphs[i];
5029
5030 if (glyph->attr.is_cluster_start)
5031 {
5032 int cellcount;
5033
5034 cellcount = count_cluster_cells(
5035 s, item, glyphs, i, &cluster_width,
5036 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5037
5038 if (cellcount > 0)
5039 {
5040 int width;
5041
5042 width = cellcount * gui.char_width * PANGO_SCALE;
5043 glyph->geometry.x_offset +=
5044 MAX(0, width - cluster_width) / 2;
5045 glyph->geometry.width = width;
5046 }
5047 else
5048 {
5049 /* If there are only combining characters in the
5050 * cluster, we cannot just change the width of the
5051 * previous glyph since there is none. Therefore
5052 * some guesswork is needed. */
5053 setup_zero_width_cluster(item, glyph, cells,
5054 cluster_width,
5055 last_glyph_rbearing);
5056 }
5057
5058 item_cells += cellcount;
5059 cells = cellcount;
5060 }
5061 else if (i > 0)
5062 {
5063 int width;
5064
5065 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005066 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02005067 * In some circumstances Pango uses a positive x_offset,
5068 * then use the width of the previous glyph for this one
5069 * and set the previous width to zero.
5070 * Otherwise we get a negative x_offset, Pango has already
5071 * positioned the combining char, keep the widths as they
5072 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005073 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02005074 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005075 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02005076 {
5077 glyphs->glyphs[i].geometry.width =
5078 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005079 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02005080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 width = cells * gui.char_width * PANGO_SCALE;
5082 glyph->geometry.x_offset +=
5083 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
5085 else /* i == 0 "cannot happen" */
5086 {
5087 glyph->geometry.width = 0;
5088 }
5089 }
5090
5091 /*** Aaaaand action! ***/
5092 draw_glyph_string(row, col + column_offset, item_cells,
5093 flags, item->analysis.font, glyphs);
5094
5095 pango_item_free(item);
5096
5097 column_offset += item_cells;
5098 }
5099
5100 pango_attr_list_unref(attr_list);
5101 }
5102
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005103skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005104 /* Draw underline and undercurl. */
5105 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 pango_glyph_string_free(glyphs);
5108 vim_free(conv_buf);
5109
5110 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5111
5112 return column_offset;
5113}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115/*
5116 * Return OK if the key with the termcap name "name" is supported.
5117 */
5118 int
5119gui_mch_haskey(char_u *name)
5120{
5121 int i;
5122
5123 for (i = 0; special_keys[i].key_sym != 0; i++)
5124 if (name[0] == special_keys[i].code0
5125 && name[1] == special_keys[i].code1)
5126 return OK;
5127 return FAIL;
5128}
5129
5130#if defined(FEAT_TITLE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 || defined(PROTO)
5132/*
5133 * Return the text window-id and display. Only required for X-based GUI's
5134 */
5135 int
5136gui_get_x11_windis(Window *win, Display **dis)
5137{
5138 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5139 {
5140 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5141 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5142 return OK;
5143 }
5144
5145 *dis = NULL;
5146 *win = 0;
5147 return FAIL;
5148}
5149#endif
5150
5151#if defined(FEAT_CLIENTSERVER) \
5152 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5153
5154 Display *
5155gui_mch_get_display(void)
5156{
5157 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5158 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5159 else
5160 return NULL;
5161}
5162#endif
5163
5164 void
5165gui_mch_beep(void)
5166{
5167#ifdef HAVE_GTK_MULTIHEAD
5168 GdkDisplay *display;
5169
5170 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5171 display = gtk_widget_get_display(gui.mainwin);
5172 else
5173 display = gdk_display_get_default();
5174
5175 if (display != NULL)
5176 gdk_display_beep(display);
5177#else
5178 gdk_beep();
5179#endif
5180}
5181
5182 void
5183gui_mch_flash(int msec)
5184{
5185 GdkGCValues values;
5186 GdkGC *invert_gc;
5187
5188 if (gui.drawarea->window == NULL)
5189 return;
5190
5191 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5192 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5193 values.function = GDK_XOR;
5194 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5195 &values,
5196 GDK_GC_FOREGROUND |
5197 GDK_GC_BACKGROUND |
5198 GDK_GC_FUNCTION);
5199 gdk_gc_set_exposures(invert_gc,
5200 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5201 /*
5202 * Do a visual beep by changing back and forth in some undetermined way,
5203 * the foreground and background colors. This is due to the fact that
5204 * there can't be really any prediction about the effects of XOR on
5205 * arbitrary X11 servers. However this seems to be enough for what we
5206 * intend it to do.
5207 */
5208 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5209 TRUE,
5210 0, 0,
5211 FILL_X((int)Columns) + gui.border_offset,
5212 FILL_Y((int)Rows) + gui.border_offset);
5213
5214 gui_mch_flush();
5215 ui_delay((long)msec, TRUE); /* wait so many msec */
5216
5217 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5218 TRUE,
5219 0, 0,
5220 FILL_X((int)Columns) + gui.border_offset,
5221 FILL_Y((int)Rows) + gui.border_offset);
5222
5223 gdk_gc_destroy(invert_gc);
5224}
5225
5226/*
5227 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5228 */
5229 void
5230gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5231{
5232 GdkGCValues values;
5233 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234
5235 if (gui.drawarea->window == NULL)
5236 return;
5237
Bram Moolenaar89d40322006-08-29 15:30:07 +00005238 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5239 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 values.function = GDK_XOR;
5241 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5242 &values,
5243 GDK_GC_FOREGROUND |
5244 GDK_GC_BACKGROUND |
5245 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005246 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5247 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5249 TRUE,
5250 FILL_X(c), FILL_Y(r),
5251 (nc) * gui.char_width, (nr) * gui.char_height);
5252 gdk_gc_destroy(invert_gc);
5253}
5254
5255/*
5256 * Iconify the GUI window.
5257 */
5258 void
5259gui_mch_iconify(void)
5260{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262}
5263
5264#if defined(FEAT_EVAL) || defined(PROTO)
5265/*
5266 * Bring the Vim window to the foreground.
5267 */
5268 void
5269gui_mch_set_foreground(void)
5270{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272}
5273#endif
5274
5275/*
5276 * Draw a cursor without focus.
5277 */
5278 void
5279gui_mch_draw_hollow_cursor(guicolor_T color)
5280{
5281 int i = 1;
5282
5283 if (gui.drawarea->window == NULL)
5284 return;
5285
5286 gui_mch_set_fg_color(color);
5287
5288 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (mb_lefthalve(gui.row, gui.col))
5290 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5292 FALSE,
5293 FILL_X(gui.col), FILL_Y(gui.row),
5294 i * gui.char_width - 1, gui.char_height - 1);
5295}
5296
5297/*
5298 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5299 * color "color".
5300 */
5301 void
5302gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5303{
5304 if (gui.drawarea->window == NULL)
5305 return;
5306
5307 gui_mch_set_fg_color(color);
5308
5309 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5310 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5311 TRUE,
5312#ifdef FEAT_RIGHTLEFT
5313 /* vertical line should be on the right of current point */
5314 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5315#endif
5316 FILL_X(gui.col),
5317 FILL_Y(gui.row) + gui.char_height - h,
5318 w, h);
5319}
5320
5321
5322/*
5323 * Catch up with any queued X11 events. This may put keyboard input into the
5324 * input buffer, call resize call-backs, trigger timers etc. If there is
5325 * nothing in the X11 event queue (& no timers pending), then we return
5326 * immediately.
5327 */
5328 void
5329gui_mch_update(void)
5330{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005331 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5332 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333}
5334
5335 static gint
5336input_timer_cb(gpointer data)
5337{
5338 int *timed_out = (int *) data;
5339
Bram Moolenaar49325942007-05-10 19:19:59 +00005340 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 *timed_out = TRUE;
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 return FALSE; /* don't happen again */
5344}
5345
5346#ifdef FEAT_SNIFF
5347/*
5348 * Callback function, used when data is available on the SNiFF connection.
5349 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 static void
5351sniff_request_cb(
5352 gpointer data,
5353 gint source_fd,
5354 GdkInputCondition condition)
5355{
5356 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5357
5358 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359}
5360#endif
5361
5362/*
5363 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5364 * from the keyboard.
5365 * wtime == -1 Wait forever.
5366 * wtime == 0 This should never happen.
5367 * wtime > 0 Wait wtime milliseconds for a character.
5368 * Returns OK if a character was found to be available within the given time,
5369 * or FAIL otherwise.
5370 */
5371 int
5372gui_mch_wait_for_chars(long wtime)
5373{
5374 int focus;
5375 guint timer;
5376 static int timed_out;
5377#ifdef FEAT_SNIFF
5378 static int sniff_on = 0;
5379 static gint sniff_input_id = 0;
5380#endif
5381
5382#ifdef FEAT_SNIFF
5383 if (sniff_on && !want_sniff_request)
5384 {
5385 if (sniff_input_id)
5386 gdk_input_remove(sniff_input_id);
5387 sniff_on = 0;
5388 }
5389 else if (!sniff_on && want_sniff_request)
5390 {
5391 /* Add fd_from_sniff to watch for available data in main loop. */
5392 sniff_input_id = gdk_input_add(fd_from_sniff,
5393 GDK_INPUT_READ, sniff_request_cb, NULL);
5394 sniff_on = 1;
5395 }
5396#endif
5397
5398 timed_out = FALSE;
5399
5400 /* this timeout makes sure that we will return if no characters arrived in
5401 * time */
5402
5403 if (wtime > 0)
5404 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5405 else
5406 timer = 0;
5407
5408 focus = gui.in_focus;
5409
5410 do
5411 {
5412 /* Stop or start blinking when focus changes */
5413 if (gui.in_focus != focus)
5414 {
5415 if (gui.in_focus)
5416 gui_mch_start_blink();
5417 else
5418 gui_mch_stop_blink();
5419 focus = gui.in_focus;
5420 }
5421
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005422#if defined(FEAT_NETBEANS_INTG)
Bram Moolenaarede547a2011-02-25 15:15:50 +01005423 /* Process any queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005424 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005425#endif
5426
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 /*
5428 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005429 * Skip this if input is available anyway (can happen in rare
5430 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005432 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005433 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434
5435 /* Got char, return immediately */
5436 if (input_available())
5437 {
5438 if (timer != 0 && !timed_out)
5439 gtk_timeout_remove(timer);
5440 return OK;
5441 }
5442 } while (wtime < 0 || !timed_out);
5443
5444 /*
5445 * Flush all eventually pending (drawing) events.
5446 */
5447 gui_mch_update();
5448
5449 return FAIL;
5450}
5451
5452
5453/****************************************************************************
5454 * Output drawing routines.
5455 ****************************************************************************/
5456
5457
5458/* Flush any output to the screen */
5459 void
5460gui_mch_flush(void)
5461{
5462#ifdef HAVE_GTK_MULTIHEAD
5463 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5464 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5465#else
5466 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 /* This happens to actually do what gui_mch_flush() is supposed to do,
5469 * according to the comment above. */
5470 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5471 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472}
5473
5474/*
5475 * Clear a rectangular region of the screen from text pos (row1, col1) to
5476 * (row2, col2) inclusive.
5477 */
5478 void
5479gui_mch_clear_block(int row1, int col1, int row2, int col2)
5480{
5481 GdkColor color;
5482
5483 if (gui.drawarea->window == NULL)
5484 return;
5485
5486 color.pixel = gui.back_pixel;
5487
5488 gdk_gc_set_foreground(gui.text_gc, &color);
5489
5490 /* Clear one extra pixel at the far right, for when bold characters have
5491 * spilled over to the window border. */
5492 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5493 FILL_X(col1), FILL_Y(row1),
5494 (col2 - col1 + 1) * gui.char_width
5495 + (col2 == Columns - 1),
5496 (row2 - row1 + 1) * gui.char_height);
5497}
5498
5499 void
5500gui_mch_clear_all(void)
5501{
5502 if (gui.drawarea->window != NULL)
5503 gdk_window_clear(gui.drawarea->window);
5504}
5505
5506/*
5507 * Redraw any text revealed by scrolling up/down.
5508 */
5509 static void
5510check_copy_area(void)
5511{
5512 GdkEvent *event;
5513 int expose_count;
5514
5515 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5516 return;
5517
5518 /* Avoid redrawing the cursor while scrolling or it'll end up where
5519 * we don't want it to be. I'm not sure if it's correct to call
5520 * gui_dont_update_cursor() at this point but it works as a quick
5521 * fix for now. */
5522 gui_dont_update_cursor();
5523
5524 do
5525 {
5526 /* Wait to check whether the scroll worked or not. */
5527 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5528
5529 if (event == NULL)
5530 break; /* received NoExpose event */
5531
5532 gui_redraw(event->expose.area.x, event->expose.area.y,
5533 event->expose.area.width, event->expose.area.height);
5534
5535 expose_count = event->expose.count;
5536 gdk_event_free(event);
5537 }
5538 while (expose_count > 0); /* more events follow */
5539
5540 gui_can_update_cursor();
5541}
5542
5543/*
5544 * Delete the given number of lines from the given row, scrolling up any
5545 * text further down within the scroll region.
5546 */
5547 void
5548gui_mch_delete_lines(int row, int num_lines)
5549{
5550 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5551 return; /* Can't see the window */
5552
5553 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5554 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5555
5556 /* copy one extra pixel, for when bold has spilled over */
5557 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5558 FILL_X(gui.scroll_region_left), FILL_Y(row),
5559 gui.drawarea->window,
5560 FILL_X(gui.scroll_region_left),
5561 FILL_Y(row + num_lines),
5562 gui.char_width * (gui.scroll_region_right
5563 - gui.scroll_region_left + 1) + 1,
5564 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5565
5566 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5567 gui.scroll_region_left,
5568 gui.scroll_region_bot, gui.scroll_region_right);
5569 check_copy_area();
5570}
5571
5572/*
5573 * Insert the given number of lines before the given row, scrolling down any
5574 * following text within the scroll region.
5575 */
5576 void
5577gui_mch_insert_lines(int row, int num_lines)
5578{
5579 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5580 return; /* Can't see the window */
5581
5582 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5583 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5584
5585 /* copy one extra pixel, for when bold has spilled over */
5586 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5587 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5588 gui.drawarea->window,
5589 FILL_X(gui.scroll_region_left), FILL_Y(row),
5590 gui.char_width * (gui.scroll_region_right
5591 - gui.scroll_region_left + 1) + 1,
5592 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5593
5594 gui_clear_block(row, gui.scroll_region_left,
5595 row + num_lines - 1, gui.scroll_region_right);
5596 check_copy_area();
5597}
5598
5599/*
5600 * X Selection stuff, for cutting and pasting text to other windows.
5601 */
5602 void
5603clip_mch_request_selection(VimClipboard *cbd)
5604{
5605 GdkAtom target;
5606 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005607 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608
5609 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5610 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005611 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5612 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 received_selection = RS_NONE;
5614 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5615
5616 gtk_selection_convert(gui.drawarea,
5617 cbd->gtk_sel_atom, target,
5618 (guint32)GDK_CURRENT_TIME);
5619
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005620 /* Hack: Wait up to three seconds for the selection. A hang was
5621 * noticed here when using the netrw plugin combined with ":gui"
5622 * during the FocusGained event. */
5623 start = time(NULL);
5624 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005625 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 if (received_selection != RS_FAIL)
5628 return;
5629 }
5630
5631 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005632 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
5634
5635/*
5636 * Disown the selection.
5637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005639clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 /* WEIRD: when using NULL to actually disown the selection, we lose the
5642 * selection the first time we own it. */
5643 /*
5644 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
5645 gui_mch_update();
5646 */
5647}
5648
5649/*
5650 * Own the selection and return OK if it worked.
5651 */
5652 int
5653clip_mch_own_selection(VimClipboard *cbd)
5654{
5655 int success;
5656
5657 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar7cfea752010-06-22 06:07:12 +02005658 clipboard_event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 gui_mch_update();
5660 return (success) ? OK : FAIL;
5661}
5662
5663/*
5664 * Send the current selection to the clipboard. Do nothing for X because we
5665 * will fill in the selection only when requested by another app.
5666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005668clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669{
5670}
5671
5672
5673#if defined(FEAT_MENU) || defined(PROTO)
5674/*
5675 * Make a menu item appear either active or not active (grey or not grey).
5676 */
5677 void
5678gui_mch_menu_grey(vimmenu_T *menu, int grey)
5679{
5680 if (menu->id == NULL)
5681 return;
5682
5683 if (menu_is_separator(menu->name))
5684 grey = TRUE;
5685
5686 gui_mch_menu_hidden(menu, FALSE);
5687 /* Be clever about bitfields versus true booleans here! */
5688 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5689 {
5690 gtk_widget_set_sensitive(menu->id, !grey);
5691 gui_mch_update();
5692 }
5693}
5694
5695/*
5696 * Make menu item hidden or not hidden.
5697 */
5698 void
5699gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5700{
5701 if (menu->id == 0)
5702 return;
5703
5704 if (hidden)
5705 {
5706 if (GTK_WIDGET_VISIBLE(menu->id))
5707 {
5708 gtk_widget_hide(menu->id);
5709 gui_mch_update();
5710 }
5711 }
5712 else
5713 {
5714 if (!GTK_WIDGET_VISIBLE(menu->id))
5715 {
5716 gtk_widget_show(menu->id);
5717 gui_mch_update();
5718 }
5719 }
5720}
5721
5722/*
5723 * This is called after setting all the menus to grey/hidden or not.
5724 */
5725 void
5726gui_mch_draw_menubar(void)
5727{
5728 /* just make sure that the visual changes get effect immediately */
5729 gui_mch_update();
5730}
5731#endif /* FEAT_MENU */
5732
5733/*
5734 * Scrollbar stuff.
5735 */
5736 void
5737gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5738{
5739 if (sb->id == NULL)
5740 return;
5741
5742 if (flag)
5743 gtk_widget_show(sb->id);
5744 else
5745 gtk_widget_hide(sb->id);
5746
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005747 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748}
5749
5750
5751/*
5752 * Return the RGB value of a pixel as long.
5753 */
5754 long_u
5755gui_mch_get_rgb(guicolor_T pixel)
5756{
5757 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5759 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
5761 return (((unsigned)color.red & 0xff00) << 8)
5762 | ((unsigned)color.green & 0xff00)
5763 | (((unsigned)color.blue & 0xff00) >> 8);
5764}
5765
5766/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005767 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005769 void
5770gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005772 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773}
5774
5775 void
5776gui_mch_setmouse(int x, int y)
5777{
5778 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5779 * internal GDK mechanism present to accomplish this. (and for good
5780 * reason...) */
5781 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5782 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5783 0, 0, 0U, 0U, x, y);
5784}
5785
5786
5787#ifdef FEAT_MOUSESHAPE
5788/* The last set mouse pointer shape is remembered, to be used when it goes
5789 * from hidden to not hidden. */
5790static int last_shape = 0;
5791#endif
5792
5793/*
5794 * Use the blank mouse pointer or not.
5795 *
5796 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5797 */
5798 void
5799gui_mch_mousehide(int hide)
5800{
5801 if (gui.pointer_hidden != hide)
5802 {
5803 gui.pointer_hidden = hide;
5804 if (gui.drawarea->window && gui.blank_pointer != NULL)
5805 {
5806 if (hide)
5807 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5808 else
5809#ifdef FEAT_MOUSESHAPE
5810 mch_set_mouse_shape(last_shape);
5811#else
5812 gdk_window_set_cursor(gui.drawarea->window, NULL);
5813#endif
5814 }
5815 }
5816}
5817
5818#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5819
5820/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5821 * misc2.c! */
5822static const int mshape_ids[] =
5823{
5824 GDK_LEFT_PTR, /* arrow */
5825 GDK_CURSOR_IS_PIXMAP, /* blank */
5826 GDK_XTERM, /* beam */
5827 GDK_SB_V_DOUBLE_ARROW, /* updown */
5828 GDK_SIZING, /* udsizing */
5829 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5830 GDK_SIZING, /* lrsizing */
5831 GDK_WATCH, /* busy */
5832 GDK_X_CURSOR, /* no */
5833 GDK_CROSSHAIR, /* crosshair */
5834 GDK_HAND1, /* hand1 */
5835 GDK_HAND2, /* hand2 */
5836 GDK_PENCIL, /* pencil */
5837 GDK_QUESTION_ARROW, /* question */
5838 GDK_RIGHT_PTR, /* right-arrow */
5839 GDK_CENTER_PTR, /* up-arrow */
5840 GDK_LEFT_PTR /* last one */
5841};
5842
5843 void
5844mch_set_mouse_shape(int shape)
5845{
5846 int id;
5847 GdkCursor *c;
5848
5849 if (gui.drawarea->window == NULL)
5850 return;
5851
5852 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5853 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5854 else
5855 {
5856 if (shape >= MSHAPE_NUMBERED)
5857 {
5858 id = shape - MSHAPE_NUMBERED;
5859 if (id >= GDK_LAST_CURSOR)
5860 id = GDK_LEFT_PTR;
5861 else
5862 id &= ~1; /* they are always even (why?) */
5863 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005864 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005866 else
5867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868# ifdef HAVE_GTK_MULTIHEAD
5869 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005870 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005872 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873# endif
5874 gdk_window_set_cursor(gui.drawarea->window, c);
5875 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5876 }
5877 if (shape != MSHAPE_HIDE)
5878 last_shape = shape;
5879}
5880#endif /* FEAT_MOUSESHAPE */
5881
5882
5883#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5884/*
5885 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5886 * scaled down if the current font is not big enough, or scaled up if the image
5887 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5888 * will be cut off if the current font is not big enough, or centered if it's
5889 * too small.
5890 */
5891# define SIGN_WIDTH (2 * gui.char_width)
5892# define SIGN_HEIGHT (gui.char_height)
5893# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 void
5896gui_mch_drawsign(int row, int col, int typenr)
5897{
5898 GdkPixbuf *sign;
5899
5900 sign = (GdkPixbuf *)sign_get_image(typenr);
5901
5902 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5903 {
5904 int width;
5905 int height;
5906 int xoffset;
5907 int yoffset;
5908 int need_scale;
5909
5910 width = gdk_pixbuf_get_width(sign);
5911 height = gdk_pixbuf_get_height(sign);
5912 /*
5913 * Decide whether we need to scale. Allow one pixel of border
5914 * width to be cut off, in order to avoid excessive scaling for
5915 * tiny differences in font size.
5916 */
5917 need_scale = (width > SIGN_WIDTH + 2
5918 || height > SIGN_HEIGHT + 2
5919 || (width < 3 * SIGN_WIDTH / 4
5920 && height < 3 * SIGN_HEIGHT / 4));
5921 if (need_scale)
5922 {
5923 double aspect;
5924
5925 /* Keep the original aspect ratio */
5926 aspect = (double)height / (double)width;
5927 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5928 width = MIN(width, SIGN_WIDTH);
5929 height = (double)width * aspect;
5930
5931 /* This doesn't seem to be worth caching, and doing so
5932 * would complicate the code quite a bit. */
5933 sign = gdk_pixbuf_scale_simple(sign, width, height,
5934 GDK_INTERP_BILINEAR);
5935 if (sign == NULL)
5936 return; /* out of memory */
5937 }
5938
5939 /* The origin is the upper-left corner of the pixmap. Therefore
5940 * these offset may become negative if the pixmap is smaller than
5941 * the 2x1 cells reserved for the sign icon. */
5942 xoffset = (width - SIGN_WIDTH) / 2;
5943 yoffset = (height - SIGN_HEIGHT) / 2;
5944
5945 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5946
5947 gdk_draw_rectangle(gui.drawarea->window,
5948 gui.text_gc,
5949 TRUE,
5950 FILL_X(col),
5951 FILL_Y(row),
5952 SIGN_WIDTH,
5953 SIGN_HEIGHT);
5954
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 gdk_pixbuf_render_to_drawable_alpha(sign,
5956 gui.drawarea->window,
5957 MAX(0, xoffset),
5958 MAX(0, yoffset),
5959 FILL_X(col) - MIN(0, xoffset),
5960 FILL_Y(row) - MIN(0, yoffset),
5961 MIN(width, SIGN_WIDTH),
5962 MIN(height, SIGN_HEIGHT),
5963 GDK_PIXBUF_ALPHA_BILEVEL,
5964 127,
5965 GDK_RGB_DITHER_NORMAL,
5966 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 if (need_scale)
5968 g_object_unref(sign);
5969 }
5970}
5971
5972 void *
5973gui_mch_register_sign(char_u *signfile)
5974{
5975 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
5976 {
5977 GdkPixbuf *sign;
5978 GError *error = NULL;
5979 char_u *message;
5980
5981 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
5982
5983 if (error == NULL)
5984 return sign;
5985
5986 message = (char_u *)error->message;
5987
5988 if (message != NULL && input_conv.vc_type != CONV_NONE)
5989 message = string_convert(&input_conv, message, NULL);
5990
5991 if (message != NULL)
5992 {
5993 /* The error message is already translated and will be more
5994 * descriptive than anything we could possibly do ourselves. */
5995 EMSG2("E255: %s", message);
5996
5997 if (input_conv.vc_type != CONV_NONE)
5998 vim_free(message);
5999 }
6000 g_error_free(error);
6001 }
6002
6003 return NULL;
6004}
6005
6006 void
6007gui_mch_destroy_sign(void *sign)
6008{
6009 if (sign != NULL)
6010 g_object_unref(sign);
6011}
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013#endif /* FEAT_SIGN_ICONS */