blob: 05cbe0b4dae187dd38858fabc84b136a9df7ca6f [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>
Bram Moolenaar7cfea752010-06-22 06:07:12 +020089static guint32 clipboard_event_time = CurrentTime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
91
92/*
93 * Easy-to-use macro for multihead support.
94 */
95#ifdef HAVE_GTK_MULTIHEAD
96# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
97 gtk_widget_get_display(gui.mainwin), atom)
98#else
99# define GET_X_ATOM(atom) ((Atom)(atom))
100#endif
101
102/* Selection type distinguishers */
103enum
104{
105 TARGET_TYPE_NONE,
106 TARGET_UTF8_STRING,
107 TARGET_STRING,
108 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000109 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 TARGET_TEXT,
111 TARGET_TEXT_URI_LIST,
112 TARGET_TEXT_PLAIN,
113 TARGET_VIM,
114 TARGET_VIMENC
115};
116
117/*
118 * Table of selection targets supported by Vim.
119 * Note: Order matters, preferred types should come first.
120 */
121static const GtkTargetEntry selection_targets[] =
122{
123 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
124 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000125 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
128 {"TEXT", 0, TARGET_TEXT},
129 {"STRING", 0, TARGET_STRING}
130};
131#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
132
133#ifdef FEAT_DND
134/*
135 * Table of DnD targets supported by Vim.
136 * Note: Order matters, preferred types should come first.
137 */
138static const GtkTargetEntry dnd_targets[] =
139{
140 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000141 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {"STRING", 0, TARGET_STRING},
144 {"text/plain", 0, TARGET_TEXT_PLAIN}
145};
146# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
147#endif
148
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
151 * "Monospace" is a standard font alias that should be present
152 * on all proper Pango/fontconfig installations.
153 */
154# define DEFAULT_FONT "Monospace 10"
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
157/*
158 * Atoms used to communicate save-yourself from the X11 session manager. There
159 * is no need to move them into the GUI struct, since they should be constant.
160 */
161static GdkAtom wm_protocols_atom = GDK_NONE;
162static GdkAtom save_yourself_atom = GDK_NONE;
163#endif
164
165/*
166 * Atoms used to control/reference X11 selections.
167 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000168static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173/*
174 * Keycodes recognized by vim.
175 * NOTE: when changing this, the table in gui_x11.c probably needs the same
176 * change!
177 */
178static struct special_key
179{
180 guint key_sym;
181 char_u code0;
182 char_u code1;
183}
184const special_keys[] =
185{
186 {GDK_Up, 'k', 'u'},
187 {GDK_Down, 'k', 'd'},
188 {GDK_Left, 'k', 'l'},
189 {GDK_Right, 'k', 'r'},
190 {GDK_F1, 'k', '1'},
191 {GDK_F2, 'k', '2'},
192 {GDK_F3, 'k', '3'},
193 {GDK_F4, 'k', '4'},
194 {GDK_F5, 'k', '5'},
195 {GDK_F6, 'k', '6'},
196 {GDK_F7, 'k', '7'},
197 {GDK_F8, 'k', '8'},
198 {GDK_F9, 'k', '9'},
199 {GDK_F10, 'k', ';'},
200 {GDK_F11, 'F', '1'},
201 {GDK_F12, 'F', '2'},
202 {GDK_F13, 'F', '3'},
203 {GDK_F14, 'F', '4'},
204 {GDK_F15, 'F', '5'},
205 {GDK_F16, 'F', '6'},
206 {GDK_F17, 'F', '7'},
207 {GDK_F18, 'F', '8'},
208 {GDK_F19, 'F', '9'},
209 {GDK_F20, 'F', 'A'},
210 {GDK_F21, 'F', 'B'},
211 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
212 {GDK_F22, 'F', 'C'},
213 {GDK_F23, 'F', 'D'},
214 {GDK_F24, 'F', 'E'},
215 {GDK_F25, 'F', 'F'},
216 {GDK_F26, 'F', 'G'},
217 {GDK_F27, 'F', 'H'},
218 {GDK_F28, 'F', 'I'},
219 {GDK_F29, 'F', 'J'},
220 {GDK_F30, 'F', 'K'},
221 {GDK_F31, 'F', 'L'},
222 {GDK_F32, 'F', 'M'},
223 {GDK_F33, 'F', 'N'},
224 {GDK_F34, 'F', 'O'},
225 {GDK_F35, 'F', 'P'},
226#ifdef SunXK_F36
227 {SunXK_F36, 'F', 'Q'},
228 {SunXK_F37, 'F', 'R'},
229#endif
230 {GDK_Help, '%', '1'},
231 {GDK_Undo, '&', '8'},
232 {GDK_BackSpace, 'k', 'b'},
233 {GDK_Insert, 'k', 'I'},
234 {GDK_Delete, 'k', 'D'},
235 {GDK_3270_BackTab, 'k', 'B'},
236 {GDK_Clear, 'k', 'C'},
237 {GDK_Home, 'k', 'h'},
238 {GDK_End, '@', '7'},
239 {GDK_Prior, 'k', 'P'},
240 {GDK_Next, 'k', 'N'},
241 {GDK_Print, '%', '9'},
242 /* Keypad keys: */
243 {GDK_KP_Left, 'k', 'l'},
244 {GDK_KP_Right, 'k', 'r'},
245 {GDK_KP_Up, 'k', 'u'},
246 {GDK_KP_Down, 'k', 'd'},
247 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
248 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
249 {GDK_KP_Home, 'K', '1'},
250 {GDK_KP_End, 'K', '4'},
251 {GDK_KP_Prior, 'K', '3'}, /* page up */
252 {GDK_KP_Next, 'K', '5'}, /* page down */
253
254 {GDK_KP_Add, 'K', '6'},
255 {GDK_KP_Subtract, 'K', '7'},
256 {GDK_KP_Divide, 'K', '8'},
257 {GDK_KP_Multiply, 'K', '9'},
258 {GDK_KP_Enter, 'K', 'A'},
259 {GDK_KP_Decimal, 'K', 'B'},
260
261 {GDK_KP_0, 'K', 'C'},
262 {GDK_KP_1, 'K', 'D'},
263 {GDK_KP_2, 'K', 'E'},
264 {GDK_KP_3, 'K', 'F'},
265 {GDK_KP_4, 'K', 'G'},
266 {GDK_KP_5, 'K', 'H'},
267 {GDK_KP_6, 'K', 'I'},
268 {GDK_KP_7, 'K', 'J'},
269 {GDK_KP_8, 'K', 'K'},
270 {GDK_KP_9, 'K', 'L'},
271
272 /* End of list marker: */
273 {0, 0, 0}
274};
275
276/*
277 * Flags for command line options table below.
278 */
279#define ARG_FONT 1
280#define ARG_GEOMETRY 2
281#define ARG_REVERSE 3
282#define ARG_NOREVERSE 4
283#define ARG_BACKGROUND 5
284#define ARG_FOREGROUND 6
285#define ARG_ICONIC 7
286#define ARG_ROLE 8
287#define ARG_NETBEANS 9
288#define ARG_XRM 10 /* ignored */
289#define ARG_MENUFONT 11 /* ignored */
290#define ARG_INDEX_MASK 0x00ff
291#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
292#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
293#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
294#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
295#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
296
297/*
298 * This table holds all the X GUI command line options allowed. This includes
299 * the standard ones so that we can skip them when Vim is started without the
300 * GUI (but the GUI might start up later).
301 *
302 * When changing this, also update doc/gui_x11.txt and the usage message!!!
303 */
304typedef struct
305{
306 const char *name;
307 unsigned int flags;
308}
309cmdline_option_T;
310
311static const cmdline_option_T cmdline_options[] =
312{
313 /* We handle these options ourselves */
314 {"-fn", ARG_FONT|ARG_HAS_VALUE},
315 {"-font", ARG_FONT|ARG_HAS_VALUE},
316 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
317 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
318 {"-rv", ARG_REVERSE},
319 {"-reverse", ARG_REVERSE},
320 {"+rv", ARG_NOREVERSE},
321 {"+reverse", ARG_NOREVERSE},
322 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
323 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
324 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
325 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
326 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_NETBEANS_INTG
329 {"-nb", ARG_NETBEANS}, /* non-standard value format */
330 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
331 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
332 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* Arguments handled by GTK (and GNOME) internally. */
335 {"--g-fatal-warnings", ARG_FOR_GTK},
336 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
337 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
338 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
339 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
340 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
341 {"--sync", ARG_FOR_GTK},
342 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
343 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
344 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
346 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifdef FEAT_GUI_GNOME
349 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--sm-disable", ARG_FOR_GTK},
353 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
355 {"--oaf-private", ARG_FOR_GTK},
356 {"--enable-sound", ARG_FOR_GTK},
357 {"--disable-sound", ARG_FOR_GTK},
358 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
359 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
360 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
361 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
362# if 0 /* conflicts with Vim's own --version argument */
363 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
364# endif
365 {"--disable-crash-dialog", ARG_FOR_GTK},
366#endif
367 {NULL, 0}
368};
369
370static int gui_argc = 0;
371static char **gui_argv = NULL;
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
375static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000376static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378static int found_iconic_arg = FALSE;
379
380#ifdef FEAT_GUI_GNOME
381/*
382 * Can't use Gnome if --socketid given
383 */
384static int using_gnome = 0;
385#else
386# define using_gnome 0
387#endif
388
389/*
390 * Parse the GUI related command-line arguments. Any arguments used are
391 * deleted from argv, and *argc is decremented accordingly. This is called
392 * when vim is started, whether or not the GUI has been started.
393 */
394 void
395gui_mch_prepare(int *argc, char **argv)
396{
397 const cmdline_option_T *option;
398 int i = 0;
399 int len = 0;
400
401#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
402 /*
403 * Determine the command used to invoke Vim, to be passed as restart
404 * command to the session manager. If argv[0] contains any directory
405 * components try building an absolute path, otherwise leave it as is.
406 */
407 restart_command = argv[0];
408
409 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
410 {
411 char_u buf[MAXPATHL];
412
413 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000414 {
415 abs_restart_command = (char *)vim_strsave(buf);
416 restart_command = abs_restart_command;
417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
419#endif
420
421 /*
422 * Move all the entries in argv which are relevant to GTK+ and GNOME
423 * into gui_argv. Freed later in gui_mch_init().
424 */
425 gui_argc = 0;
426 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
427
428 g_return_if_fail(gui_argv != NULL);
429
430 gui_argv[gui_argc++] = argv[i++];
431
432 while (i < *argc)
433 {
434 /* Don't waste CPU cycles on non-option arguments. */
435 if (argv[i][0] != '-' && argv[i][0] != '+')
436 {
437 ++i;
438 continue;
439 }
440
441 /* Look for argv[i] in cmdline_options[] table. */
442 for (option = &cmdline_options[0]; option->name != NULL; ++option)
443 {
444 len = strlen(option->name);
445
446 if (strncmp(argv[i], option->name, len) == 0)
447 {
448 if (argv[i][len] == '\0')
449 break;
450 /* allow --foo=bar style */
451 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
452 break;
453#ifdef FEAT_NETBEANS_INTG
454 /* darn, -nb has non-standard syntax */
455 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
456 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
457 break;
458#endif
459 }
460 else if ((option->flags & ARG_COMPAT_LONG)
461 && strcmp(argv[i], option->name + 1) == 0)
462 {
463 /* Replace the standard X arguments "-name" and "-display"
464 * with their GNU-style long option counterparts. */
465 argv[i] = (char *)option->name;
466 break;
467 }
468 }
469 if (option->name == NULL) /* no match */
470 {
471 ++i;
472 continue;
473 }
474
475 if (option->flags & ARG_FOR_GTK)
476 {
477 /* Move the argument into gui_argv, which
478 * will later be passed to gtk_init_check() */
479 gui_argv[gui_argc++] = argv[i];
480 }
481 else
482 {
483 char *value = NULL;
484
485 /* Extract the option's value if there is one.
486 * Accept both "--foo bar" and "--foo=bar" style. */
487 if (option->flags & ARG_HAS_VALUE)
488 {
489 if (argv[i][len] == '=')
490 value = &argv[i][len + 1];
491 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
492 value = argv[i + 1];
493 }
494
495 /* Check for options handled by Vim itself */
496 switch (option->flags & ARG_INDEX_MASK)
497 {
498 case ARG_REVERSE:
499 found_reverse_arg = TRUE;
500 break;
501 case ARG_NOREVERSE:
502 found_reverse_arg = FALSE;
503 break;
504 case ARG_FONT:
505 font_argument = value;
506 break;
507 case ARG_GEOMETRY:
508 if (value != NULL)
509 gui.geom = vim_strsave((char_u *)value);
510 break;
511 case ARG_BACKGROUND:
512 background_argument = value;
513 break;
514 case ARG_FOREGROUND:
515 foreground_argument = value;
516 break;
517 case ARG_ICONIC:
518 found_iconic_arg = TRUE;
519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 case ARG_ROLE:
521 role_argument = value; /* used later in gui_mch_open() */
522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#ifdef FEAT_NETBEANS_INTG
524 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 gui.dofork = FALSE; /* don't fork() when starting GUI */
526 netbeansArg = argv[i];
527 break;
528#endif
529 default:
530 break;
531 }
532 }
533
534 /* These arguments make gnome_program_init() print a message and exit.
535 * Must start the GUI for this, otherwise ":gui" will exit later! */
536 if (option->flags & ARG_NEEDS_GUI)
537 gui.starting = TRUE;
538
539 if (option->flags & ARG_KEEP)
540 ++i;
541 else
542 {
543 /* Remove the flag from the argument vector. */
544 if (--*argc > i)
545 {
546 int n_strip = 1;
547
548 /* Move the argument's value as well, if there is one. */
549 if ((option->flags & ARG_HAS_VALUE)
550 && argv[i][len] != '='
551 && strcmp(argv[i + 1], "--") != 0)
552 {
553 ++n_strip;
554 --*argc;
555 if (option->flags & ARG_FOR_GTK)
556 gui_argv[gui_argc++] = argv[i + 1];
557 }
558
559 if (*argc > i)
560 mch_memmove(&argv[i], &argv[i + n_strip],
561 (*argc - i) * sizeof(char *));
562 }
563 argv[*argc] = NULL;
564 }
565 }
566
567 gui_argv[gui_argc] = NULL;
568}
569
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000570#if defined(EXITFREE) || defined(PROTO)
571 void
572gui_mch_free_all()
573{
574 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000575#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
576 vim_free(abs_restart_command);
577#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000578}
579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * This should be maybe completely removed.
583 * Doesn't seem possible, since check_copy_area() relies on
584 * this information. --danielk
585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000587visibility_event(GtkWidget *widget UNUSED,
588 GdkEventVisibility *event,
589 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 gui.visibility = event->state;
592 /*
593 * When we do an gdk_window_copy_area(), and the window is partially
594 * obscured, we want to receive an event to tell us whether it worked
595 * or not.
596 */
597 if (gui.text_gc != NULL)
598 gdk_gc_set_exposures(gui.text_gc,
599 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
600 return FALSE;
601}
602
603/*
604 * Redraw the corresponding portions of the screen.
605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000607expose_event(GtkWidget *widget UNUSED,
608 GdkEventExpose *event,
609 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 /* Skip this when the GUI isn't set up yet, will redraw later. */
612 if (gui.starting)
613 return FALSE;
614
615 out_flush(); /* make sure all output has been processed */
616 gui_redraw(event->area.x, event->area.y,
617 event->area.width, event->area.height);
618
619 /* Clear the border areas if needed */
620 if (event->area.x < FILL_X(0))
621 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
622 if (event->area.y < FILL_Y(0))
623 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
624 if (event->area.x > FILL_X(Columns))
625 gdk_window_clear_area(gui.drawarea->window,
626 FILL_X((int)Columns), 0, 0, 0);
627 if (event->area.y > FILL_Y(Rows))
628 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
629
630 return FALSE;
631}
632
633#ifdef FEAT_CLIENTSERVER
634/*
635 * Handle changes to the "Comm" property
636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000638property_event(GtkWidget *widget,
639 GdkEventProperty *event,
640 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 if (event->type == GDK_PROPERTY_NOTIFY
643 && event->state == (int)GDK_PROPERTY_NEW_VALUE
644 && GDK_WINDOW_XWINDOW(event->window) == commWindow
645 && GET_X_ATOM(event->atom) == commProperty)
646 {
647 XEvent xev;
648
649 /* Translate to XLib */
650 xev.xproperty.type = PropertyNotify;
651 xev.xproperty.atom = commProperty;
652 xev.xproperty.window = commWindow;
653 xev.xproperty.state = PropertyNewValue;
654 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656 return FALSE;
657}
658#endif
659
660
661/****************************************************************************
662 * Focus handlers:
663 */
664
665
666/*
667 * This is a simple state machine:
668 * BLINK_NONE not blinking at all
669 * BLINK_OFF blinking, cursor is not shown
670 * BLINK_ON blinking, cursor is shown
671 */
672
673#define BLINK_NONE 0
674#define BLINK_OFF 1
675#define BLINK_ON 2
676
677static int blink_state = BLINK_NONE;
678static long_u blink_waittime = 700;
679static long_u blink_ontime = 400;
680static long_u blink_offtime = 250;
681static guint blink_timer = 0;
682
683 void
684gui_mch_set_blinking(long waittime, long on, long off)
685{
686 blink_waittime = waittime;
687 blink_ontime = on;
688 blink_offtime = off;
689}
690
691/*
692 * Stop the cursor blinking. Show the cursor if it wasn't shown.
693 */
694 void
695gui_mch_stop_blink(void)
696{
697 if (blink_timer)
698 {
699 gtk_timeout_remove(blink_timer);
700 blink_timer = 0;
701 }
702 if (blink_state == BLINK_OFF)
703 gui_update_cursor(TRUE, FALSE);
704 blink_state = BLINK_NONE;
705}
706
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000708blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709{
710 if (blink_state == BLINK_ON)
711 {
712 gui_undraw_cursor();
713 blink_state = BLINK_OFF;
714 blink_timer = gtk_timeout_add((guint32)blink_offtime,
715 (GtkFunction) blink_cb, NULL);
716 }
717 else
718 {
719 gui_update_cursor(TRUE, FALSE);
720 blink_state = BLINK_ON;
721 blink_timer = gtk_timeout_add((guint32)blink_ontime,
722 (GtkFunction) blink_cb, NULL);
723 }
724
725 return FALSE; /* don't happen again */
726}
727
728/*
729 * Start the cursor blinking. If it was already blinking, this restarts the
730 * waiting time and shows the cursor.
731 */
732 void
733gui_mch_start_blink(void)
734{
735 if (blink_timer)
736 gtk_timeout_remove(blink_timer);
737 /* Only switch blinking on if none of the times is zero */
738 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
739 {
740 blink_timer = gtk_timeout_add((guint32)blink_waittime,
741 (GtkFunction) blink_cb, NULL);
742 blink_state = BLINK_ON;
743 gui_update_cursor(TRUE, FALSE);
744 }
745}
746
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000748enter_notify_event(GtkWidget *widget UNUSED,
749 GdkEventCrossing *event UNUSED,
750 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751{
752 if (blink_state == BLINK_NONE)
753 gui_mch_start_blink();
754
755 /* make sure keyboard input goes there */
756 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
757 gtk_widget_grab_focus(gui.drawarea);
758
759 return FALSE;
760}
761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000763leave_notify_event(GtkWidget *widget UNUSED,
764 GdkEventCrossing *event UNUSED,
765 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
767 if (blink_state != BLINK_NONE)
768 gui_mch_stop_blink();
769
770 return FALSE;
771}
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000774focus_in_event(GtkWidget *widget,
775 GdkEventFocus *event UNUSED,
776 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777{
778 gui_focus_change(TRUE);
779
780 if (blink_state == BLINK_NONE)
781 gui_mch_start_blink();
782
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000783 /* make sure keyboard input goes to the draw area (if this is focus for a
784 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000785 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000786 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
788 return TRUE;
789}
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000792focus_out_event(GtkWidget *widget UNUSED,
793 GdkEventFocus *event UNUSED,
794 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 gui_focus_change(FALSE);
797
798 if (blink_state != BLINK_NONE)
799 gui_mch_stop_blink();
800
801 return TRUE;
802}
803
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Translate a GDK key value to UTF-8 independently of the current locale.
807 * The output is written to string, which must have room for at least 6 bytes
808 * plus the NUL terminator. Returns the length in bytes.
809 *
810 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
811 * of GdkEventKey::string instead. But event->string is evil; see here why:
812 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
813 */
814 static int
815keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
816{
817 int len;
818 guint32 uc;
819
820 uc = gdk_keyval_to_unicode(keyval);
821 if (uc != 0)
822 {
823 /* Check for CTRL-foo */
824 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
825 {
826 /* These mappings look arbitrary at the first glance, but in fact
827 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
828 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
829 * more sense and is consistent with usual terminal behaviour). */
830 if (uc >= '@')
831 string[0] = uc & 0x1F;
832 else if (uc == '2')
833 string[0] = NUL;
834 else if (uc >= '3' && uc <= '7')
835 string[0] = uc ^ 0x28;
836 else if (uc == '8')
837 string[0] = BS;
838 else if (uc == '?')
839 string[0] = DEL;
840 else
841 string[0] = uc;
842 len = 1;
843 }
844 else
845 {
846 /* Translate a normal key to UTF-8. This doesn't work for dead
847 * keys of course, you _have_ to use an input method for that. */
848 len = utf_char2bytes((int)uc, string);
849 }
850 }
851 else
852 {
853 /* Translate keys which are represented by ASCII control codes in Vim.
854 * There are only a few of those; most control keys are translated to
855 * special terminal-like control sequences. */
856 len = 1;
857 switch (keyval)
858 {
859 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
860 string[0] = TAB;
861 break;
862 case GDK_Linefeed:
863 string[0] = NL;
864 break;
865 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
866 string[0] = CAR;
867 break;
868 case GDK_Escape:
869 string[0] = ESC;
870 break;
871 default:
872 len = 0;
873 break;
874 }
875 }
876 string[len] = NUL;
877
878 return len;
879}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000881 static int
882modifiers_gdk2vim(guint state)
883{
884 int modifiers = 0;
885
886 if (state & GDK_SHIFT_MASK)
887 modifiers |= MOD_MASK_SHIFT;
888 if (state & GDK_CONTROL_MASK)
889 modifiers |= MOD_MASK_CTRL;
890 if (state & GDK_MOD1_MASK)
891 modifiers |= MOD_MASK_ALT;
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200892#ifdef GDK_SUPER_MASK
893 if (state & GDK_SUPER_MASK)
894 modifiers |= MOD_MASK_META;
895#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000896 if (state & GDK_MOD4_MASK)
897 modifiers |= MOD_MASK_META;
898
899 return modifiers;
900}
901
902 static int
903modifiers_gdk2mouse(guint state)
904{
905 int modifiers = 0;
906
907 if (state & GDK_SHIFT_MASK)
908 modifiers |= MOUSE_SHIFT;
909 if (state & GDK_CONTROL_MASK)
910 modifiers |= MOUSE_CTRL;
911 if (state & GDK_MOD1_MASK)
912 modifiers |= MOUSE_ALT;
913
914 return modifiers;
915}
916
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917/*
918 * Main keyboard handler:
919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000921key_press_event(GtkWidget *widget UNUSED,
922 GdkEventKey *event,
923 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200925 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
927 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 guint key_sym;
929 int len;
930 int i;
931 int modifiers;
932 int key;
933 guint state;
934 char_u *s, *d;
935
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200936 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 key_sym = event->keyval;
938 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940#ifdef FEAT_XIM
941 if (xim_queue_key_press_event(event, TRUE))
942 return TRUE;
943#endif
944
945#ifdef FEAT_HANGULIN
946 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
947 {
948 hangul_input_state_toggle();
949 return TRUE;
950 }
951#endif
952
953#ifdef SunXK_F36
954 /*
955 * These keys have bogus lookup strings, and trapping them here is
956 * easier than trying to XRebindKeysym() on them with every possible
957 * combination of modifiers.
958 */
959 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
960 len = 0;
961 else
962#endif
963 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 len = keyval_to_string(key_sym, state, string2);
965
966 /* Careful: convert_input() doesn't handle the NUL character.
967 * No need to convert pure ASCII anyway, thus the len > 1 check. */
968 if (len > 1 && input_conv.vc_type != CONV_NONE)
969 len = convert_input(string2, len, sizeof(string2));
970
971 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 d = string;
973 for (i = 0; i < len; ++i)
974 {
975 *d++ = s[i];
976 if (d[-1] == CSI && d + 2 < string + sizeof(string))
977 {
978 /* Turn CSI into K_CSI. */
979 *d++ = KS_EXTRA;
980 *d++ = (int)KE_CSI;
981 }
982 }
983 len = d - string;
984 }
985
986 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
987 if (key_sym == GDK_ISO_Left_Tab)
988 {
989 key_sym = GDK_Tab;
990 state |= GDK_SHIFT_MASK;
991 }
992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_MENU
994 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
995 * is a menu shortcut, we ignore everything with the ALT modifier. */
996 if ((state & GDK_MOD1_MASK)
997 && gui.menu_is_active
998 && (*p_wak == 'y'
999 || (*p_wak == 'm'
1000 && len == 1
1001 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /* For GTK2 we return false to signify that we haven't handled the
1003 * keypress, so that gtk will handle the mnemonic or accelerator. */
1004 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005#endif
1006
1007 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1008 * that already has the 8th bit set.
1009 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1010 * Don't do this for double-byte encodings, it turns the char into a lead
1011 * byte. */
1012 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001013 && ((state & GDK_MOD1_MASK)
1014#ifdef GDK_SUPER_MASK
1015 || (state & GDK_SUPER_MASK)
1016#endif
1017 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1019 && (string[0] & 0x80) == 0
1020 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 )
1023 {
1024 string[0] |= 0x80;
1025 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 if (enc_utf8) /* convert to utf-8 */
1027 {
1028 string[1] = string[0] & 0xbf;
1029 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1030 if (string[1] == CSI)
1031 {
1032 string[2] = KS_EXTRA;
1033 string[3] = (int)KE_CSI;
1034 len = 4;
1035 }
1036 else
1037 len = 2;
1038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 }
1040
1041 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1042 * value) to detect backspace, delete and keypad keys. */
1043 if (len == 0 || len == 1)
1044 {
1045 for (i = 0; special_keys[i].key_sym != 0; i++)
1046 {
1047 if (special_keys[i].key_sym == key_sym)
1048 {
1049 string[0] = CSI;
1050 string[1] = special_keys[i].code0;
1051 string[2] = special_keys[i].code1;
1052 len = -3;
1053 break;
1054 }
1055 }
1056 }
1057
1058 if (len == 0) /* Unrecognized key */
1059 return TRUE;
1060
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 /* Special keys (and a few others) may have modifiers. Also when using a
1062 * double-byte encoding (can't set the 8th bit). */
1063 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1064 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1065 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1066 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001067 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001068#ifdef GDK_SUPER_MASK
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001069 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001071 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001073 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 /*
1076 * For some keys a shift modifier is translated into another key
1077 * code.
1078 */
1079 if (len == -3)
1080 key = TO_SPECIAL(string[1], string[2]);
1081 else
1082 key = string[0];
1083
1084 key = simplify_key(key, &modifiers);
1085 if (key == CSI)
1086 key = K_CSI;
1087 if (IS_SPECIAL(key))
1088 {
1089 string[0] = CSI;
1090 string[1] = K_SECOND(key);
1091 string[2] = K_THIRD(key);
1092 len = 3;
1093 }
1094 else
1095 {
1096 string[0] = key;
1097 len = 1;
1098 }
1099
1100 if (modifiers != 0)
1101 {
1102 string2[0] = CSI;
1103 string2[1] = KS_MODIFIER;
1104 string2[2] = modifiers;
1105 add_to_input_buf(string2, 3);
1106 }
1107 }
1108
1109 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1110 || (string[0] == intr_char && intr_char != Ctrl_C)))
1111 {
1112 trash_input_buf();
1113 got_int = TRUE;
1114 }
1115
1116 add_to_input_buf(string, len);
1117
1118 /* blank out the pointer if necessary */
1119 if (p_mh)
1120 gui_mch_mousehide(TRUE);
1121
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 return TRUE;
1123}
1124
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001125#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001127key_release_event(GtkWidget *widget UNUSED,
1128 GdkEventKey *event,
1129 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130{
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001131 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 /*
1133 * GTK+ 2 input methods may do fancy stuff on key release events too.
1134 * With the default IM for instance, you can enter any UCS code point
1135 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1136 */
1137 return xim_queue_key_press_event(event, FALSE);
1138}
1139#endif
1140
1141
1142/****************************************************************************
1143 * Selection handlers:
1144 */
1145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001147selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001149 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150{
1151 if (event->selection == clip_plus.gtk_sel_atom)
1152 clip_lose_selection(&clip_plus);
1153 else
1154 clip_lose_selection(&clip_star);
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 return TRUE;
1157}
1158
1159#define RS_NONE 0 /* selection_received_cb() not called yet */
1160#define RS_OK 1 /* selection_received_cb() called and OK */
1161#define RS_FAIL 2 /* selection_received_cb() called and failed */
1162static int received_selection = RS_NONE;
1163
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001165selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001167 guint time_ UNUSED,
1168 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169{
1170 VimClipboard *cbd;
1171 char_u *text;
1172 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 int len;
1175 int motion_type;
1176
1177 if (data->selection == clip_plus.gtk_sel_atom)
1178 cbd = &clip_plus;
1179 else
1180 cbd = &clip_star;
1181
1182 text = (char_u *)data->data;
1183 len = data->length;
1184 motion_type = MCHAR;
1185
1186 if (text == NULL || len <= 0)
1187 {
1188 received_selection = RS_FAIL;
1189 /* clip_free_selection(cbd); ??? */
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 return;
1192 }
1193
1194 if (data->type == vim_atom)
1195 {
1196 motion_type = *text++;
1197 --len;
1198 }
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else if (data->type == vimenc_atom)
1201 {
1202 char_u *enc;
1203 vimconv_T conv;
1204
1205 motion_type = *text++;
1206 --len;
1207
1208 enc = text;
1209 text += STRLEN(text) + 1;
1210 len -= text - enc;
1211
1212 /* If the encoding of the text is different from 'encoding', attempt
1213 * converting it. */
1214 conv.vc_type = CONV_NONE;
1215 convert_setup(&conv, enc, p_enc);
1216 if (conv.vc_type != CONV_NONE)
1217 {
1218 tmpbuf = string_convert(&conv, text, &len);
1219 if (tmpbuf != NULL)
1220 text = tmpbuf;
1221 convert_setup(&conv, NULL, NULL);
1222 }
1223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 /* gtk_selection_data_get_text() handles all the nasty details
1226 * and targets and encodings etc. This rocks so hard. */
1227 else
1228 {
1229 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1230 if (tmpbuf_utf8 != NULL)
1231 {
1232 len = STRLEN(tmpbuf_utf8);
1233 if (input_conv.vc_type != CONV_NONE)
1234 {
1235 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1236 if (tmpbuf != NULL)
1237 text = tmpbuf;
1238 }
1239 else
1240 text = tmpbuf_utf8;
1241 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001242 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1243 {
1244 vimconv_T conv;
1245
1246 /* UTF-16, we get this for HTML */
1247 conv.vc_type = CONV_NONE;
1248 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1249
1250 if (conv.vc_type != CONV_NONE)
1251 {
1252 text += 2;
1253 len -= 2;
1254 tmpbuf = string_convert(&conv, text, &len);
1255 convert_setup(&conv, NULL, NULL);
1256 }
1257 if (tmpbuf != NULL)
1258 text = tmpbuf;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261
Bram Moolenaara76638f2010-06-05 12:49:46 +02001262 /* Chop off any traiing NUL bytes. OpenOffice sends these. */
1263 while (len > 0 && text[len - 1] == NUL)
1264 --len;
1265
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 clip_yank_selection(motion_type, text, (long)len, cbd);
1267 received_selection = RS_OK;
1268 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270}
1271
1272/*
1273 * Prepare our selection data for passing it to the external selection
1274 * client.
1275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001277selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 GtkSelectionData *selection_data,
1279 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001280 guint time_ UNUSED,
1281 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282{
1283 char_u *string;
1284 char_u *tmpbuf;
1285 long_u tmplen;
1286 int length;
1287 int motion_type;
1288 GdkAtom type;
1289 VimClipboard *cbd;
1290
1291 if (selection_data->selection == clip_plus.gtk_sel_atom)
1292 cbd = &clip_plus;
1293 else
1294 cbd = &clip_star;
1295
1296 if (!cbd->owned)
1297 return; /* Shouldn't ever happen */
1298
1299 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001300 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 && info != (guint)TARGET_UTF8_STRING
1302 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 && info != (guint)TARGET_VIM
1304 && info != (guint)TARGET_COMPOUND_TEXT
1305 && info != (guint)TARGET_TEXT)
1306 return;
1307
1308 /* get the selection from the '*'/'+' register */
1309 clip_get_selection(cbd);
1310
1311 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1312 if (motion_type < 0 || string == NULL)
1313 return;
1314 /* Due to int arguments we can't handle more than G_MAXINT. Also
1315 * reserve one extra byte for NUL or the motion type; just in case.
1316 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1317 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1318
1319 if (info == (guint)TARGET_VIM)
1320 {
1321 tmpbuf = alloc((unsigned)length + 1);
1322 if (tmpbuf != NULL)
1323 {
1324 tmpbuf[0] = motion_type;
1325 mch_memmove(tmpbuf + 1, string, (size_t)length);
1326 }
1327 /* For our own format, the first byte contains the motion type */
1328 ++length;
1329 vim_free(string);
1330 string = tmpbuf;
1331 type = vim_atom;
1332 }
1333
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001334 else if (info == (guint)TARGET_HTML)
1335 {
1336 vimconv_T conv;
1337
1338 /* Since we get utf-16, we probably should set it as well. */
1339 conv.vc_type = CONV_NONE;
1340 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1341 if (conv.vc_type != CONV_NONE)
1342 {
1343 tmpbuf = string_convert(&conv, string, &length);
1344 convert_setup(&conv, NULL, NULL);
1345 vim_free(string);
1346 string = tmpbuf;
1347 }
1348
1349 /* Prepend the BOM: "fffe" */
1350 if (string != NULL)
1351 {
1352 tmpbuf = alloc(length + 2);
1353 tmpbuf[0] = 0xff;
1354 tmpbuf[1] = 0xfe;
1355 mch_memmove(tmpbuf + 2, string, (size_t)length);
1356 vim_free(string);
1357 string = tmpbuf;
1358 length += 2;
1359
1360 selection_data->type = selection_data->target;
1361 selection_data->format = 16; /* 16 bits per char */
1362 gtk_selection_data_set(selection_data, html_atom, 16,
1363 string, length);
1364 vim_free(string);
1365 }
1366 return;
1367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 else if (info == (guint)TARGET_VIMENC)
1369 {
1370 int l = STRLEN(p_enc);
1371
1372 /* contents: motion_type 'encoding' NUL text */
1373 tmpbuf = alloc((unsigned)length + l + 2);
1374 if (tmpbuf != NULL)
1375 {
1376 tmpbuf[0] = motion_type;
1377 STRCPY(tmpbuf + 1, p_enc);
1378 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1379 }
1380 length += l + 2;
1381 vim_free(string);
1382 string = tmpbuf;
1383 type = vimenc_atom;
1384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 /* gtk_selection_data_set_text() handles everything for us. This is
1387 * so easy and simple and cool, it'd be insane not to use it. */
1388 else
1389 {
1390 if (output_conv.vc_type != CONV_NONE)
1391 {
1392 tmpbuf = string_convert(&output_conv, string, &length);
1393 vim_free(string);
1394 if (tmpbuf == NULL)
1395 return;
1396 string = tmpbuf;
1397 }
1398 /* Validate the string to avoid runtime warnings */
1399 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1400 {
1401 gtk_selection_data_set_text(selection_data,
1402 (const char *)string, length);
1403 }
1404 vim_free(string);
1405 return;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 if (string != NULL)
1409 {
1410 selection_data->type = selection_data->target;
1411 selection_data->format = 8; /* 8 bits per char */
1412
1413 gtk_selection_data_set(selection_data, type, 8, string, length);
1414 vim_free(string);
1415 }
1416}
1417
1418/*
1419 * Check if the GUI can be started. Called before gvimrc is sourced.
1420 * Return OK or FAIL.
1421 */
1422 int
1423gui_mch_init_check(void)
1424{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425#ifdef FEAT_GUI_GNOME
1426 if (gtk_socket_id == 0)
1427 using_gnome = 1;
1428#endif
1429
1430 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1431 if (!gtk_init_check(&gui_argc, &gui_argv))
1432 {
1433 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001434 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 return FAIL;
1436 }
1437
1438 return OK;
1439}
1440
1441
1442/****************************************************************************
1443 * Mouse handling callbacks
1444 */
1445
1446
1447static guint mouse_click_timer = 0;
1448static int mouse_timed_out = TRUE;
1449
1450/*
1451 * Timer used to recognize multiple clicks of the mouse button
1452 */
1453 static gint
1454mouse_click_timer_cb(gpointer data)
1455{
1456 /* we don't use this information currently */
1457 int *timed_out = (int *) data;
1458
1459 *timed_out = TRUE;
1460 return FALSE; /* don't happen again */
1461}
1462
1463static guint motion_repeat_timer = 0;
1464static int motion_repeat_offset = FALSE;
1465static gint motion_repeat_timer_cb(gpointer);
1466
1467 static void
1468process_motion_notify(int x, int y, GdkModifierType state)
1469{
1470 int button;
1471 int_u vim_modifiers;
1472
1473 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1474 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1475 GDK_BUTTON5_MASK))
1476 ? MOUSE_DRAG : ' ';
1477
1478 /* If our pointer is currently hidden, then we should show it. */
1479 gui_mch_mousehide(FALSE);
1480
1481 /* Just moving the rodent above the drawing area without any button
1482 * being pressed. */
1483 if (button != MOUSE_DRAG)
1484 {
1485 gui_mouse_moved(x, y);
1486 return;
1487 }
1488
1489 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001490 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
Bram Moolenaar49325942007-05-10 19:19:59 +00001492 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 /*
1496 * Auto repeat timer handling.
1497 */
1498 if (x < 0 || y < 0
1499 || x >= gui.drawarea->allocation.width
1500 || y >= gui.drawarea->allocation.height)
1501 {
1502
1503 int dx;
1504 int dy;
1505 int offshoot;
1506 int delay = 10;
1507
1508 /* Calculate the maximal distance of the cursor from the drawing area.
1509 * (offshoot can't become negative here!).
1510 */
1511 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1512 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1513
1514 offshoot = dx > dy ? dx : dy;
1515
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001516 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 * distance of 127 pixels from the main window.
1518 *
1519 * One could think endlessly about the most ergonomic variant here.
1520 * For example it could make sense to calculate the distance from the
1521 * drags start instead...
1522 *
1523 * Maybe a parabolic interpolation would suite us better here too...
1524 */
1525 if (offshoot > 127)
1526 {
1527 /* 5 appears to be somehow near to my perceptual limits :-). */
1528 delay = 5;
1529 }
1530 else
1531 {
1532 delay = (130 * (127 - offshoot)) / 127 + 5;
1533 }
1534
1535 /* shoot again */
1536 if (!motion_repeat_timer)
1537 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1538 motion_repeat_timer_cb, NULL);
1539 }
1540}
1541
1542/*
1543 * Timer used to recognize multiple clicks of the mouse button.
1544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001546motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
1548 int x;
1549 int y;
1550 GdkModifierType state;
1551
1552 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1553
1554 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1555 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1556 GDK_BUTTON5_MASK)))
1557 {
1558 motion_repeat_timer = 0;
1559 return FALSE;
1560 }
1561
1562 /* If there already is a mouse click in the input buffer, wait another
1563 * time (otherwise we would create a backlog of clicks) */
1564 if (vim_used_in_input_buf() > 10)
1565 return TRUE;
1566
1567 motion_repeat_timer = 0;
1568
1569 /*
1570 * Fake a motion event.
1571 * Trick: Pretend the mouse moved to the next character on every other
1572 * event, otherwise drag events will be discarded, because they are still
1573 * in the same character.
1574 */
1575 if (motion_repeat_offset)
1576 x += gui.char_width;
1577
1578 motion_repeat_offset = !motion_repeat_offset;
1579 process_motion_notify(x, y, state);
1580
1581 /* Don't happen again. We will get reinstalled in the synthetic event
1582 * if needed -- thus repeating should still work. */
1583 return FALSE;
1584}
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001587motion_notify_event(GtkWidget *widget,
1588 GdkEventMotion *event,
1589 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590{
1591 if (event->is_hint)
1592 {
1593 int x;
1594 int y;
1595 GdkModifierType state;
1596
1597 gdk_window_get_pointer(widget->window, &x, &y, &state);
1598 process_motion_notify(x, y, state);
1599 }
1600 else
1601 {
1602 process_motion_notify((int)event->x, (int)event->y,
1603 (GdkModifierType)event->state);
1604 }
1605
1606 return TRUE; /* handled */
1607}
1608
1609
1610/*
1611 * Mouse button handling. Note please that we are capturing multiple click's
1612 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1613 * This is due to the way the generic VIM code is recognizing multiple clicks.
1614 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001616button_press_event(GtkWidget *widget,
1617 GdkEventButton *event,
1618 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619{
1620 int button;
1621 int repeated_click = FALSE;
1622 int x, y;
1623 int_u vim_modifiers;
1624
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001625 clipboard_event_time = event->time;
1626
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 /* Make sure we have focus now we've been selected */
1628 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1629 gtk_widget_grab_focus(widget);
1630
1631 /*
1632 * Don't let additional events about multiple clicks send by GTK to us
1633 * after the initial button press event confuse us.
1634 */
1635 if (event->type != GDK_BUTTON_PRESS)
1636 return FALSE;
1637
1638 x = event->x;
1639 y = event->y;
1640
1641 /* Handle multiple clicks */
1642 if (!mouse_timed_out && mouse_click_timer)
1643 {
1644 gtk_timeout_remove(mouse_click_timer);
1645 mouse_click_timer = 0;
1646 repeated_click = TRUE;
1647 }
1648
1649 mouse_timed_out = FALSE;
1650 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1651 mouse_click_timer_cb, &mouse_timed_out);
1652
1653 switch (event->button)
1654 {
1655 case 1:
1656 button = MOUSE_LEFT;
1657 break;
1658 case 2:
1659 button = MOUSE_MIDDLE;
1660 break;
1661 case 3:
1662 button = MOUSE_RIGHT;
1663 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 default:
1665 return FALSE; /* Unknown button */
1666 }
1667
1668#ifdef FEAT_XIM
1669 /* cancel any preediting */
1670 if (im_is_preediting())
1671 xim_reset();
1672#endif
1673
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001674 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675
1676 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 return TRUE;
1679}
1680
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001682 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001685scroll_event(GtkWidget *widget,
1686 GdkEventScroll *event,
1687 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688{
1689 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001690 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691
1692 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1693 gtk_widget_grab_focus(widget);
1694
1695 switch (event->direction)
1696 {
1697 case GDK_SCROLL_UP:
1698 button = MOUSE_4;
1699 break;
1700 case GDK_SCROLL_DOWN:
1701 button = MOUSE_5;
1702 break;
1703 default: /* We don't care about left and right... Yet. */
1704 return FALSE;
1705 }
1706
1707# ifdef FEAT_XIM
1708 /* cancel any preediting */
1709 if (im_is_preediting())
1710 xim_reset();
1711# endif
1712
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001713 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1716 FALSE, vim_modifiers);
1717
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 return TRUE;
1719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001723button_release_event(GtkWidget *widget UNUSED,
1724 GdkEventButton *event,
1725 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 int x, y;
1728 int_u vim_modifiers;
1729
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001730 clipboard_event_time = event->time;
1731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 /* Remove any motion "machine gun" timers used for automatic further
1733 extension of allocation areas if outside of the applications window
1734 area .*/
1735 if (motion_repeat_timer)
1736 {
1737 gtk_timeout_remove(motion_repeat_timer);
1738 motion_repeat_timer = 0;
1739 }
1740
1741 x = event->x;
1742 y = event->y;
1743
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001744 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
1748 return TRUE;
1749}
1750
1751
1752#ifdef FEAT_DND
1753/****************************************************************************
1754 * Drag aNd Drop support handlers.
1755 */
1756
1757/*
1758 * Count how many items there may be and separate them with a NUL.
1759 * Apparently the items are separated with \r\n. This is not documented,
1760 * thus be careful not to go past the end. Also allow separation with
1761 * NUL characters.
1762 */
1763 static int
1764count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1765{
1766 int i;
1767 char_u *p = out;
1768 int count = 0;
1769
1770 for (i = 0; i < len; ++i)
1771 {
1772 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1773 {
1774 if (p > out && p[-1] != NUL)
1775 {
1776 ++count;
1777 *p++ = NUL;
1778 }
1779 }
1780 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1781 {
1782 *p++ = hexhex2nr(raw + i + 1);
1783 i += 2;
1784 }
1785 else
1786 *p++ = raw[i];
1787 }
1788 if (p > out && p[-1] != NUL)
1789 {
1790 *p = NUL; /* last item didn't have \r or \n */
1791 ++count;
1792 }
1793 return count;
1794}
1795
1796/*
1797 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1798 * this process, URI which protocol is not "file:" are removed. Return
1799 * length of array (less than "max").
1800 */
1801 static int
1802filter_uri_list(char_u **outlist, int max, char_u *src)
1803{
1804 int i, j;
1805
1806 for (i = j = 0; i < max; ++i)
1807 {
1808 outlist[i] = NULL;
1809 if (STRNCMP(src, "file:", 5) == 0)
1810 {
1811 src += 5;
1812 if (STRNCMP(src, "//localhost", 11) == 0)
1813 src += 11;
1814 while (src[0] == '/' && src[1] == '/')
1815 ++src;
1816 outlist[j++] = vim_strsave(src);
1817 }
1818 src += STRLEN(src) + 1;
1819 }
1820 return j;
1821}
1822
1823 static char_u **
1824parse_uri_list(int *count, char_u *data, int len)
1825{
1826 int n = 0;
1827 char_u *tmp = NULL;
1828 char_u **array = NULL;;
1829
1830 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1831 {
1832 n = count_and_decode_uri_list(tmp, data, len);
1833 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1834 n = filter_uri_list(array, n, tmp);
1835 }
1836 vim_free(tmp);
1837 *count = n;
1838 return array;
1839}
1840
1841 static void
1842drag_handle_uri_list(GdkDragContext *context,
1843 GtkSelectionData *data,
1844 guint time_,
1845 GdkModifierType state,
1846 gint x,
1847 gint y)
1848{
1849 char_u **fnames;
1850 int nfiles = 0;
1851
1852 fnames = parse_uri_list(&nfiles, data->data, data->length);
1853
1854 if (fnames != NULL && nfiles > 0)
1855 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001856 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1859
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001860 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861
1862 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1863 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001864 else
1865 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866}
1867
1868 static void
1869drag_handle_text(GdkDragContext *context,
1870 GtkSelectionData *data,
1871 guint time_,
1872 GdkModifierType state)
1873{
1874 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1875 char_u *text;
1876 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878
1879 text = data->data;
1880 len = data->length;
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (data->type == utf8_string_atom)
1883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (input_conv.vc_type != CONV_NONE)
1885 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (tmpbuf != NULL)
1887 text = tmpbuf;
1888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 dnd_yank_drag_data(text, (long)len);
1891 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001894 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 if (dropkey[2] != 0)
1897 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1898 else
1899 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900}
1901
1902/*
1903 * DND receiver.
1904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 static void
1906drag_data_received_cb(GtkWidget *widget,
1907 GdkDragContext *context,
1908 gint x,
1909 gint y,
1910 GtkSelectionData *data,
1911 guint info,
1912 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001913 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914{
1915 GdkModifierType state;
1916
1917 /* Guard against trash */
1918 if (data->data == NULL
1919 || data->length <= 0
1920 || data->format != 8
1921 || data->data[data->length] != '\0')
1922 {
1923 gtk_drag_finish(context, FALSE, FALSE, time_);
1924 return;
1925 }
1926
1927 /* Get the current modifier state for proper distinguishment between
1928 * different operations later. */
1929 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1930
1931 /* Not sure about the role of "text/plain" here... */
1932 if (info == (guint)TARGET_TEXT_URI_LIST)
1933 drag_handle_uri_list(context, data, time_, state, x, y);
1934 else
1935 drag_handle_text(context, data, time_, state);
1936
1937}
1938#endif /* FEAT_DND */
1939
1940
1941#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1942/*
1943 * GnomeClient interact callback. Check for unsaved buffers that cannot
1944 * be abandoned and pop up a dialog asking the user for confirmation if
1945 * necessary.
1946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001948sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001950 GnomeDialogType type UNUSED,
1951 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
1953 cmdmod_T save_cmdmod;
1954 gboolean shutdown_cancelled;
1955
1956 save_cmdmod = cmdmod;
1957
1958# ifdef FEAT_BROWSE
1959 cmdmod.browse = TRUE;
1960# endif
1961# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1962 cmdmod.confirm = TRUE;
1963# endif
1964 /*
1965 * If there are changed buffers, present the user with
1966 * a dialog if possible, otherwise give an error message.
1967 */
1968 shutdown_cancelled = check_changed_any(FALSE);
1969
1970 exiting = FALSE;
1971 cmdmod = save_cmdmod;
1972 setcursor(); /* position the cursor */
1973 out_flush();
1974 /*
1975 * If the user hit the [Cancel] button the whole shutdown
1976 * will be cancelled. Wow, quite powerful feature (:
1977 */
1978 gnome_interaction_key_return(key, shutdown_cancelled);
1979}
1980
1981/*
1982 * Generate a script that can be used to restore the current editing session.
1983 * Save the value of v:this_session before running :mksession in order to make
1984 * automagic session save fully transparent. Return TRUE on success.
1985 */
1986 static int
1987write_session_file(char_u *filename)
1988{
1989 char_u *escaped_filename;
1990 char *mksession_cmdline;
1991 unsigned int save_ssop_flags;
1992 int failed;
1993
1994 /*
1995 * Build an ex command line to create a script that restores the current
1996 * session if executed. Escape the filename to avoid nasty surprises.
1997 */
1998 escaped_filename = vim_strsave_escaped(filename, escape_chars);
1999 if (escaped_filename == NULL)
2000 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002001 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2002 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 /*
2006 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2007 * unpredictable effects when the session is saved automatically. Also,
2008 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2009 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2010 * enormously large if the GNOME session feature is used regularly.
2011 */
2012 save_ssop_flags = ssop_flags;
2013 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002014 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2017 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2018 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002019 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021 ssop_flags = save_ssop_flags;
2022 g_free(mksession_cmdline);
2023 /*
2024 * Reopen the file and append a command to restore v:this_session,
2025 * as if this save never happened. This is to avoid conflicts with
2026 * the user's own sessions. FIXME: It's probably less hackish to add
2027 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2028 */
2029 if (!failed)
2030 {
2031 FILE *fd;
2032
2033 fd = open_exfile(filename, TRUE, APPENDBIN);
2034
2035 failed = (fd == NULL
2036 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2037 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2038
2039 if (fd != NULL && fclose(fd) != 0)
2040 failed = TRUE;
2041
2042 if (failed)
2043 mch_remove(filename);
2044 }
2045
2046 return !failed;
2047}
2048
2049/*
2050 * "save_yourself" signal handler. Initiate an interaction to ask the user
2051 * for confirmation if necessary. Save the current editing session and tell
2052 * the session manager how to restart Vim.
2053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 static gboolean
2055sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002056 gint phase UNUSED,
2057 GnomeSaveStyle save_style UNUSED,
2058 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002060 gboolean fast UNUSED,
2061 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062{
2063 static const char suffix[] = "-session.vim";
2064 char *session_file;
2065 unsigned int len;
2066 gboolean success;
2067
2068 /* Always request an interaction if possible. check_changed_any()
2069 * won't actually show a dialog unless any buffers have been modified.
2070 * There doesn't seem to be an obvious way to check that without
2071 * automatically firing the dialog. Anyway, it works just fine. */
2072 if (interact_style == GNOME_INTERACT_ANY)
2073 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2074 &sm_client_check_changed_any,
2075 NULL);
2076 out_flush();
2077 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2078
2079 /* The path is unique for each session save. We do neither know nor care
2080 * which session script will actually be used later. This decision is in
2081 * the domain of the session manager. */
2082 session_file = gnome_config_get_real_path(
2083 gnome_client_get_config_prefix(client));
2084 len = strlen(session_file);
2085
2086 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2087 --len; /* get rid of the superfluous trailing '/' */
2088
2089 session_file = g_renew(char, session_file, len + sizeof(suffix));
2090 memcpy(session_file + len, suffix, sizeof(suffix));
2091
2092 success = write_session_file((char_u *)session_file);
2093
2094 if (success)
2095 {
2096 const char *argv[8];
2097 int i;
2098
2099 /* Tell the session manager how to wipe out the stored session data.
2100 * This isn't as dangerous as it looks, don't worry :) session_file
2101 * is a unique absolute filename. Usually it'll be something like
2102 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2103 i = 0;
2104 argv[i++] = "rm";
2105 argv[i++] = session_file;
2106 argv[i] = NULL;
2107
2108 gnome_client_set_discard_command(client, i, (char **)argv);
2109
2110 /* Tell the session manager how to restore the just saved session.
2111 * This is easily done thanks to Vim's -S option. Pass the -f flag
2112 * since there's no need to fork -- it might even cause confusion.
2113 * Also pass the window role to give the WM something to match on.
2114 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2115 i = 0;
2116 argv[i++] = restart_command;
2117 argv[i++] = "-f";
2118 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 argv[i++] = "--role";
2120 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 argv[i++] = "-S";
2122 argv[i++] = session_file;
2123 argv[i] = NULL;
2124
2125 gnome_client_set_restart_command(client, i, (char **)argv);
2126 gnome_client_set_clone_command(client, 0, NULL);
2127 }
2128
2129 g_free(session_file);
2130
2131 return success;
2132}
2133
2134/*
2135 * Called when the session manager wants us to die. There isn't much to save
2136 * here since "save_yourself" has been emitted before (unless serious trouble
2137 * is happening).
2138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002140sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141{
2142 /* Don't write messages to the GUI anymore */
2143 full_screen = FALSE;
2144
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002145 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002146 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002147 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 preserve_exit();
2149}
2150
2151/*
2152 * Connect our signal handlers to be notified on session save and shutdown.
2153 */
2154 static void
2155setup_save_yourself(void)
2156{
2157 GnomeClient *client;
2158
2159 client = gnome_master_client();
2160
2161 if (client != NULL)
2162 {
2163 /* Must use the deprecated gtk_signal_connect() for compatibility
2164 * with GNOME 1. Arrgh, zombies! */
2165 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2166 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2167 gtk_signal_connect(GTK_OBJECT(client), "die",
2168 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2169 }
2170}
2171
2172#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2173
2174# ifdef USE_XSMP
2175/*
2176 * GTK tells us that XSMP needs attention
2177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 static gboolean
2179local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002180 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 GIOCondition condition;
2182 gpointer data;
2183{
2184 if (condition == G_IO_IN)
2185 {
2186 /* Do stuff; maybe close connection */
2187 if (xsmp_handle_requests() == FAIL)
2188 g_io_channel_unref((GIOChannel *)data);
2189 return TRUE;
2190 }
2191 /* Error */
2192 g_io_channel_unref((GIOChannel *)data);
2193 xsmp_close();
2194 return TRUE;
2195}
2196# endif /* USE_XSMP */
2197
2198/*
2199 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2200 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2201 */
2202 static void
2203setup_save_yourself(void)
2204{
2205 Atom *existing_atoms = NULL;
2206 int count = 0;
2207
2208#ifdef USE_XSMP
2209 if (xsmp_icefd != -1)
2210 {
2211 /*
2212 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2213 * set up GTK IO monitor
2214 */
2215 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2216
2217 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2218 local_xsmp_handle_requests, (gpointer)g_io);
2219 }
2220 else
2221#endif
2222 {
2223 /* Fall back to old method */
2224
2225 /* first get the existing value */
2226 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2227 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2228 &existing_atoms, &count))
2229 {
2230 Atom *new_atoms;
2231 Atom save_yourself_xatom;
2232 int i;
2233
2234 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2235
2236 /* check if WM_SAVE_YOURSELF isn't there yet */
2237 for (i = 0; i < count; ++i)
2238 if (existing_atoms[i] == save_yourself_xatom)
2239 break;
2240
2241 if (i == count)
2242 {
2243 /* allocate an Atoms array which is one item longer */
2244 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2245 * sizeof(Atom)));
2246 if (new_atoms != NULL)
2247 {
2248 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2249 new_atoms[count] = save_yourself_xatom;
2250 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2251 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2252 new_atoms, count + 1);
2253 vim_free(new_atoms);
2254 }
2255 }
2256 XFree(existing_atoms);
2257 }
2258 }
2259}
2260
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261/*
2262 * Installing a global event filter seems to be the only way to catch
2263 * client messages of type WM_PROTOCOLS without overriding GDK's own
2264 * client message event filter. Well, that's still better than trying
2265 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 *
2267 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2268 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2269 * I have the nasty feeling modern session managers just don't send this
2270 * deprecated message anymore. Addition: confirmed by several people.
2271 *
2272 * The GNOME session support is much cooler anyway. Unlike this ugly
2273 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2274 * it should work with KDE as well.
2275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002277global_event_filter(GdkXEvent *xev,
2278 GdkEvent *event UNUSED,
2279 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
2281 XEvent *xevent = (XEvent *)xev;
2282
2283 if (xevent != NULL
2284 && xevent->type == ClientMessage
2285 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002286 && (long_u)xevent->xclient.data.l[0]
2287 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 out_flush();
2290 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2291 /*
2292 * Set the window's WM_COMMAND property, to let the window manager
2293 * know we are done saving ourselves. We don't want to be
2294 * restarted, thus set argv to NULL.
2295 */
2296 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2297 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2298 NULL, 0);
2299 return GDK_FILTER_REMOVE;
2300 }
2301
2302 return GDK_FILTER_CONTINUE;
2303}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2305
2306
2307/*
2308 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002311mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312{
2313/* If you get an error message here, you still need to unpack the runtime
2314 * archive! */
2315#ifdef magick
2316# undef magick
2317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 /* A bit hackish, but avoids casting later and allows optimization */
2319# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320#define magick vim32x32
2321#include "../runtime/vim32x32.xpm"
2322#undef magick
2323#define magick vim16x16
2324#include "../runtime/vim16x16.xpm"
2325#undef magick
2326#define magick vim48x48
2327#include "../runtime/vim48x48.xpm"
2328#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
2331 /* When started with "--echo-wid" argument, write window ID on stdout. */
2332 if (echo_wid_arg)
2333 {
2334 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2335 fflush(stdout);
2336 }
2337
2338 if (vim_strchr(p_go, GO_ICON) != NULL)
2339 {
2340 /*
2341 * Add an icon to the main window. For fun and convenience of the user.
2342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 GList *icons = NULL;
2344
2345 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2346 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2347 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2348
2349 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2350
2351 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2352 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354
2355#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2356 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358#endif
2359 /* Setup to indicate to the window manager that we want to catch the
2360 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2361 * manager instead. */
2362#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2363 if (using_gnome)
2364#endif
2365 setup_save_yourself();
2366
2367#ifdef FEAT_CLIENTSERVER
2368 if (serverName == NULL && serverDelayedStartName != NULL)
2369 {
2370 /* This is a :gui command in a plain vim with no previous server */
2371 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2372
2373 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2374 serverDelayedStartName);
2375 }
2376 else
2377 {
2378 /*
2379 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2380 * have to change the "server" registration to that of the main window
2381 * If we have not registered a name yet, remember the window
2382 */
2383 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2384 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2385 }
2386 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2387 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2388 GTK_SIGNAL_FUNC(property_event), NULL);
2389#endif
2390}
2391
2392 static GdkCursor *
2393create_blank_pointer(void)
2394{
2395 GdkWindow *root_window = NULL;
2396 GdkPixmap *blank_mask;
2397 GdkCursor *cursor;
2398 GdkColor color = { 0, 0, 0, 0 };
2399 char blank_data[] = { 0x0 };
2400
2401#ifdef HAVE_GTK_MULTIHEAD
2402 root_window = gtk_widget_get_root_window(gui.mainwin);
2403#endif
2404
2405 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2406 * in size. */
2407 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2408 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2409 &color, &color, 0, 0);
2410 gdk_bitmap_unref(blank_mask);
2411
2412 return cursor;
2413}
2414
2415#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 static void
2417mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002418 GdkScreen *previous_screen UNUSED,
2419 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421 if (!gtk_widget_has_screen(widget))
2422 return;
2423
2424 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002425 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 */
2427 if (gui.blank_pointer != NULL)
2428 gdk_cursor_unref(gui.blank_pointer);
2429
2430 gui.blank_pointer = create_blank_pointer();
2431
2432 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2433 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2434
2435 /*
2436 * Create a new PangoContext for this screen, and initialize it
2437 * with the current font if necessary.
2438 */
2439 if (gui.text_context != NULL)
2440 g_object_unref(gui.text_context);
2441
2442 gui.text_context = gtk_widget_create_pango_context(widget);
2443 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2444
2445 if (gui.norm_font != NULL)
2446 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002447 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002448 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 }
2450}
2451#endif /* HAVE_GTK_MULTIHEAD */
2452
2453/*
2454 * After the drawing area comes up, we calculate all colors and create the
2455 * dummy blank cursor.
2456 *
2457 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2458 * fact that the main VIM engine doesn't take them into account anywhere.
2459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002461drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 GtkWidget *sbar;
2464
2465#ifdef FEAT_XIM
2466 xim_init();
2467#endif
2468 gui_mch_new_colors();
2469 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2470
2471 gui.blank_pointer = create_blank_pointer();
2472 if (gui.pointer_hidden)
2473 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2474
2475 /* get the actual size of the scrollbars, if they are realized */
2476 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2477 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2478 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2479 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2480 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2481 gui.scrollbar_width = sbar->allocation.width;
2482
2483 sbar = gui.bottom_sbar.id;
2484 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2485 gui.scrollbar_height = sbar->allocation.height;
2486}
2487
2488/*
2489 * Properly clean up on shutdown.
2490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002492drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 /* Don't write messages to the GUI anymore */
2495 full_screen = FALSE;
2496
2497#ifdef FEAT_XIM
2498 im_shutdown();
2499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (gui.ascii_glyphs != NULL)
2501 {
2502 pango_glyph_string_free(gui.ascii_glyphs);
2503 gui.ascii_glyphs = NULL;
2504 }
2505 if (gui.ascii_font != NULL)
2506 {
2507 g_object_unref(gui.ascii_font);
2508 gui.ascii_font = NULL;
2509 }
2510 g_object_unref(gui.text_context);
2511 gui.text_context = NULL;
2512
2513 g_object_unref(gui.text_gc);
2514 gui.text_gc = NULL;
2515
2516 gdk_cursor_unref(gui.blank_pointer);
2517 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518}
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002521drawarea_style_set_cb(GtkWidget *widget UNUSED,
2522 GtkStyle *previous_style UNUSED,
2523 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 gui_mch_new_colors();
2526}
2527
2528/*
2529 * Callback routine for the "delete_event" signal on the toplevel window.
2530 * Tries to vim gracefully, or refuses to exit with changed buffers.
2531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002533delete_event_cb(GtkWidget *widget UNUSED,
2534 GdkEventAny *event UNUSED,
2535 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 gui_shell_closed();
2538 return TRUE;
2539}
2540
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002541#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2542 static int
2543get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2544{
2545 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2546
2547#ifdef FEAT_GUI_GNOME
2548 if (using_gnome && widget != NULL)
2549 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002550 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002551 BonoboDockItem *dockitem;
2552
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002553 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002554 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2555 {
2556 /* Only menu & toolbar are dock items. Could tabline be?
2557 * Seem to be only the 2 defined in GNOME */
2558 widget = parent;
2559 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002560
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002561 if (dockitem == NULL || dockitem->is_floating)
2562 return 0;
2563 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2564 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002565 }
2566#endif
2567 if (widget != NULL
2568 && item_orientation == orientation
2569 && GTK_WIDGET_REALIZED(widget)
2570 && GTK_WIDGET_VISIBLE(widget))
2571 {
2572 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2573 return widget->allocation.height;
2574 else
2575 return widget->allocation.width;
2576 }
2577 return 0;
2578}
2579#endif
2580
2581 static int
2582get_menu_tool_width(void)
2583{
2584 int width = 0;
2585
2586#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2587# ifdef FEAT_MENU
2588 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2589# endif
2590# ifdef FEAT_TOOLBAR
2591 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2592# endif
2593# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002594 if (gui.tabline != NULL)
2595 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002596# endif
2597#endif
2598
2599 return width;
2600}
2601
2602 static int
2603get_menu_tool_height(void)
2604{
2605 int height = 0;
2606
2607#ifdef FEAT_MENU
2608 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2609#endif
2610#ifdef FEAT_TOOLBAR
2611 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2612#endif
2613#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002614 if (gui.tabline != NULL)
2615 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002616#endif
2617
2618 return height;
2619}
2620
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002621/* This controls whether we can set the real window hints at
2622 * start-up when in a GtkPlug.
2623 * 0 = normal processing (default)
2624 * 1 = init. hints set, no-one's tried to reset since last check
2625 * 2 = init. hints set, attempt made to change hints
2626 */
2627static int init_window_hints_state = 0;
2628
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002629 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002630update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002631{
2632 static int old_width = 0;
2633 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002634 static int old_min_width = 0;
2635 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002636 static int old_char_width = 0;
2637 static int old_char_height = 0;
2638
2639 int width;
2640 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002641 int min_width;
2642 int min_height;
2643
2644 /* At start-up, don't try to set the hints until the initial
2645 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002646 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002647 */
2648 if (!(force_width && force_height) && init_window_hints_state > 0)
2649 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002650 /* Don't do it! */
2651 init_window_hints_state = 2;
2652 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002653 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002654
2655 /* This also needs to be done when the main window isn't there yet,
2656 * otherwise the hints don't work. */
2657 width = gui_get_base_width();
2658 height = gui_get_base_height();
2659# ifdef FEAT_MENU
2660 height += tabline_height() * gui.char_height;
2661# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002662 width += get_menu_tool_width();
2663 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002664
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002665 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002666 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002667 * 'set columns=' or init. -geom) we briefly set the min. to the size
2668 * we wish to be instead of the legitimate minimum so that we actually
2669 * resize correctly.
2670 */
2671 if (force_width && force_height)
2672 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002673 min_width = force_width;
2674 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002675 }
2676 else
2677 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002678 min_width = width + MIN_COLUMNS * gui.char_width;
2679 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002680 }
2681
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002682 /* Avoid an expose event when the size didn't change. */
2683 if (width != old_width
2684 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002685 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002686 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002687 || gui.char_width != old_char_width
2688 || gui.char_height != old_char_height)
2689 {
2690 GdkGeometry geometry;
2691 GdkWindowHints geometry_mask;
2692
2693 geometry.width_inc = gui.char_width;
2694 geometry.height_inc = gui.char_height;
2695 geometry.base_width = width;
2696 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002697 geometry.min_width = min_width;
2698 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002699 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2700 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002701 /* Using gui.formwin as geometry widget doesn't work as expected
2702 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2703 * in Vim confuse GTK+. */
2704 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2705 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002706 old_width = width;
2707 old_height = height;
2708 old_min_width = min_width;
2709 old_min_height = min_height;
2710 old_char_width = gui.char_width;
2711 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002712 }
2713}
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715#ifdef FEAT_TOOLBAR
2716
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717/*
2718 * This extra effort wouldn't be necessary if we only used stock icons in the
2719 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2720 * shouldn't be treated differently, thus we do need this.
2721 */
2722 static void
2723icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2724{
2725 if (GTK_IS_IMAGE(widget))
2726 {
2727 GtkImage *image = (GtkImage *)widget;
2728
2729 /* User-defined icons are stored in a GtkIconSet */
2730 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2731 {
2732 GtkIconSet *icon_set;
2733 GtkIconSize icon_size;
2734
2735 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2736 icon_size = (GtkIconSize)(long)user_data;
2737
2738 gtk_icon_set_ref(icon_set);
2739 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2740 gtk_icon_set_unref(icon_set);
2741 }
2742 }
2743 else if (GTK_IS_CONTAINER(widget))
2744 {
2745 gtk_container_foreach((GtkContainer *)widget,
2746 &icon_size_changed_foreach,
2747 user_data);
2748 }
2749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
2751 static void
2752set_toolbar_style(GtkToolbar *toolbar)
2753{
2754 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 GtkIconSize size;
2756 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2759 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2760 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002761 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2763 style = GTK_TOOLBAR_BOTH;
2764 else if (toolbar_flags & TOOLBAR_TEXT)
2765 style = GTK_TOOLBAR_TEXT;
2766 else
2767 style = GTK_TOOLBAR_ICONS;
2768
2769 gtk_toolbar_set_style(toolbar, style);
2770 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 switch (tbis_flags)
2773 {
2774 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2775 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2776 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2777 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2778 default: size = GTK_ICON_SIZE_INVALID; break;
2779 }
2780 oldsize = gtk_toolbar_get_icon_size(toolbar);
2781
2782 if (size == GTK_ICON_SIZE_INVALID)
2783 {
2784 /* Let global user preferences decide the icon size. */
2785 gtk_toolbar_unset_icon_size(toolbar);
2786 size = gtk_toolbar_get_icon_size(toolbar);
2787 }
2788 if (size != oldsize)
2789 {
2790 gtk_container_foreach(GTK_CONTAINER(toolbar),
2791 &icon_size_changed_foreach,
2792 GINT_TO_POINTER((int)size));
2793 }
2794 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795}
2796
2797#endif /* FEAT_TOOLBAR */
2798
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002799#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2800static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002801static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002802static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002803static int clicked_page; /* page clicked in tab line */
2804
2805/*
2806 * Handle selecting an item in the tab line popup menu.
2807 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002808 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002809tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002810{
Bram Moolenaara5621492006-02-25 21:55:24 +00002811 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002812 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002813}
2814
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002815 static void
2816add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2817{
2818 GtkWidget *item;
2819 char_u *utf_text;
2820
2821 utf_text = CONVERT_TO_UTF8(text);
2822 item = gtk_menu_item_new_with_label((const char *)utf_text);
2823 gtk_widget_show(item);
2824 CONVERT_TO_UTF8_FREE(utf_text);
2825
2826 gtk_container_add(GTK_CONTAINER(menu), item);
2827 gtk_signal_connect(GTK_OBJECT(item), "activate",
2828 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002829 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002830}
2831
Bram Moolenaara5621492006-02-25 21:55:24 +00002832/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002833 * Create a menu for the tab line.
2834 */
2835 static GtkWidget *
2836create_tabline_menu(void)
2837{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002838 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002839
2840 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002841 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2842 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2843 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002844
2845 return menu;
2846}
2847
2848 static gboolean
2849on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2850{
2851 /* Was this button press event ? */
2852 if (event->type == GDK_BUTTON_PRESS)
2853 {
2854 GdkEventButton *bevent = (GdkEventButton *)event;
2855 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002856 int y = bevent->y;
2857 GtkWidget *tabwidget;
2858 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002859
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002860 /* When ignoring events return TRUE so that the selected page doesn't
2861 * change. */
2862 if (hold_gui_events
2863# ifdef FEAT_CMDWIN
2864 || cmdwin_type != 0
2865# endif
2866 )
2867 return TRUE;
2868
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002869 tabwin = gdk_window_at_pointer(&x, &y);
2870 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2871 clicked_page = (int)(long)gtk_object_get_user_data(
2872 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002873
2874 /* If the event was generated for 3rd button popup the menu. */
2875 if (bevent->button == 3)
2876 {
2877 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2878 bevent->button, bevent->time);
2879 /* We handled the event. */
2880 return TRUE;
2881 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002882 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002883 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002884 if (clicked_page == 0)
2885 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002886 /* Click after all tabs moves to next tab page. When "x" is
2887 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002888 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002889 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002890 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002891 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002892
Bram Moolenaara5621492006-02-25 21:55:24 +00002893 /* We didn't handle the event. */
2894 return FALSE;
2895}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002896
2897/*
2898 * Handle selecting one of the tabs.
2899 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002900 static void
2901on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002902 GtkNotebook *notebook UNUSED,
2903 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002904 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002905 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002906{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002907 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002908 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002909 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002910 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002911}
2912
2913/*
2914 * Show or hide the tabline.
2915 */
2916 void
2917gui_mch_show_tabline(int showit)
2918{
2919 if (gui.tabline == NULL)
2920 return;
2921
2922 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2923 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002924 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002925 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002926 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002927 if (showit)
2928 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002929 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002930
2931 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002932}
2933
2934/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002935 * Return TRUE when tabline is displayed.
2936 */
2937 int
2938gui_mch_showing_tabline(void)
2939{
2940 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002941 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002942}
2943
2944/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002945 * Update the labels of the tabline.
2946 */
2947 void
2948gui_mch_update_tabline(void)
2949{
2950 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002951 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002952 GtkWidget *label;
2953 tabpage_T *tp;
2954 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002955 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002956 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002957 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002958
2959 if (gui.tabline == NULL)
2960 return;
2961
2962 ignore_tabline_evt = TRUE;
2963
2964 /* Add a label for each tab page. They all contain the same text area. */
2965 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2966 {
2967 if (tp == curtab)
2968 curtabidx = nr;
2969
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002970 tab_num = nr + 1;
2971
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002972 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
2973 if (page == NULL)
2974 {
2975 /* Add notebook page */
2976 page = gtk_vbox_new(FALSE, 0);
2977 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002978 event_box = gtk_event_box_new();
2979 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002980 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002981 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002982 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002983 gtk_widget_show(label);
2984 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
2985 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002986 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002987 nr++);
2988 }
2989
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002990 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002991 gtk_object_set_user_data(GTK_OBJECT(event_box),
2992 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002993 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00002994 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002995 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002996 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002997 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002998
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002999 get_tabline_label(tp, TRUE);
3000 labeltext = CONVERT_TO_UTF8(NameBuff);
3001 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3002 (const char *)labeltext, NULL);
3003 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003004 }
3005
3006 /* Remove any old labels. */
3007 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3008 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3009
3010 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003011 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003012
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003013 /* Make sure everything is in place before drawing text. */
3014 gui_mch_update();
3015
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003016 ignore_tabline_evt = FALSE;
3017}
3018
3019/*
3020 * Set the current tab to "nr". First tab is 1.
3021 */
3022 void
3023gui_mch_set_curtab(nr)
3024 int nr;
3025{
3026 if (gui.tabline == NULL)
3027 return;
3028
3029 ignore_tabline_evt = TRUE;
3030 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003031 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003032 ignore_tabline_evt = FALSE;
3033}
3034
3035#endif /* FEAT_GUI_TABLINE */
3036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003038 * Add selection targets for PRIMARY and CLIPBOARD selections.
3039 */
3040 void
3041gui_gtk_set_selection_targets(void)
3042{
3043 int i, j = 0;
3044 int n_targets = N_SELECTION_TARGETS;
3045 GtkTargetEntry targets[N_SELECTION_TARGETS];
3046
3047 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3048 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003049 /* OpenOffice tries to use TARGET_HTML and fails when it doesn't
3050 * return something, instead of trying another target. Therefore only
3051 * offer TARGET_HTML when it works. */
3052 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3053 n_targets--;
3054 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003055 targets[j++] = selection_targets[i];
3056 }
3057
3058 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3059 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3060 gtk_selection_add_targets(gui.drawarea,
3061 (GdkAtom)GDK_SELECTION_PRIMARY,
3062 targets, n_targets);
3063 gtk_selection_add_targets(gui.drawarea,
3064 (GdkAtom)clip_plus.gtk_sel_atom,
3065 targets, n_targets);
3066}
3067
3068/*
3069 * Set up for receiving DND items.
3070 */
3071 void
3072gui_gtk_set_dnd_targets(void)
3073{
3074 int i, j = 0;
3075 int n_targets = N_DND_TARGETS;
3076 GtkTargetEntry targets[N_DND_TARGETS];
3077
3078 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3079 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003080 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3081 n_targets--;
3082 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003083 targets[j++] = dnd_targets[i];
3084 }
3085
3086 gtk_drag_dest_unset(gui.drawarea);
3087 gtk_drag_dest_set(gui.drawarea,
3088 GTK_DEST_DEFAULT_ALL,
3089 targets, n_targets,
3090 GDK_ACTION_COPY);
3091}
3092
3093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3095 * Returns OK for success, FAIL when the GUI can't be started.
3096 */
3097 int
3098gui_mch_init(void)
3099{
3100 GtkWidget *vbox;
3101
3102#ifdef FEAT_GUI_GNOME
3103 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3104 * exits on failure, but that's a non-issue because we already called
3105 * gtk_init_check() in gui_mch_init_check(). */
3106 if (using_gnome)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3108 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109#endif
3110 vim_free(gui_argv);
3111 gui_argv = NULL;
3112
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003113#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 /* Set the human-readable application name */
3115 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 /*
3118 * Force UTF-8 output no matter what the value of 'encoding' is.
3119 * did_set_string_option() in option.c prohibits changing 'termencoding'
3120 * to something else than UTF-8 if the GUI is in use.
3121 */
3122 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3123
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003124#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3128 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003129#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131#endif
3132
3133 /* Initialize values */
3134 gui.border_width = 2;
3135 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3136 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003137 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003139 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003141 /* LINTED: avoid warning: conversion to 'unsigned long' */
3142 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
3144 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003145 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148 /* Set default foreground and background colors. */
3149 gui.norm_pixel = gui.def_norm_pixel;
3150 gui.back_pixel = gui.def_back_pixel;
3151
3152 if (gtk_socket_id != 0)
3153 {
3154 GtkWidget *plug;
3155
3156 /* Use GtkSocket from another app. */
3157#ifdef HAVE_GTK_MULTIHEAD
3158 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3159 gtk_socket_id);
3160#else
3161 plug = gtk_plug_new(gtk_socket_id);
3162#endif
3163 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3164 {
3165 gui.mainwin = plug;
3166 }
3167 else
3168 {
3169 g_warning("Connection to GTK+ socket (ID %u) failed",
3170 (unsigned int)gtk_socket_id);
3171 /* Pretend we never wanted it if it failed (get own window) */
3172 gtk_socket_id = 0;
3173 }
3174 }
3175
3176 if (gtk_socket_id == 0)
3177 {
3178#ifdef FEAT_GUI_GNOME
3179 if (using_gnome)
3180 {
3181 gui.mainwin = gnome_app_new("Vim", NULL);
3182# ifdef USE_XSMP
3183 /* Use the GNOME save-yourself functionality now. */
3184 xsmp_close();
3185# endif
3186 }
3187 else
3188#endif
3189 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3190 }
3191
3192 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3193
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 /* Create the PangoContext used for drawing all text. */
3195 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3196 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3199 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3200
3201 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3202 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3203
3204 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3205 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3206#ifdef HAVE_GTK_MULTIHEAD
3207 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3208 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 gui.accel_group = gtk_accel_group_new();
3211 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003213 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 vbox = gtk_vbox_new(FALSE, 0);
3215
3216#ifdef FEAT_GUI_GNOME
3217 if (using_gnome)
3218 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003219# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /* automagically restore menubar/toolbar placement */
3221 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3222# endif
3223 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3224 }
3225 else
3226#endif
3227 {
3228 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3229 gtk_widget_show(vbox);
3230 }
3231
3232#ifdef FEAT_MENU
3233 /*
3234 * Create the menubar and handle
3235 */
3236 gui.menubar = gtk_menu_bar_new();
3237 gtk_widget_set_name(gui.menubar, "vim-menubar");
3238
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003239 /* Avoid that GTK takes <F10> away from us. */
3240 {
3241 GtkSettings *gtk_settings;
3242
3243 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3244 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3245 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003246
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248# ifdef FEAT_GUI_GNOME
3249 if (using_gnome)
3250 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 BonoboDockItem *dockitem;
3252
3253 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3254 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3255 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003256 /* We don't want the menu to float. */
3257 bonobo_dock_item_set_behavior(dockitem,
3258 bonobo_dock_item_get_behavior(dockitem)
3259 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 else
3263# endif /* FEAT_GUI_GNOME */
3264 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003265 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3266 * disabled in gui_init() later. */
3267 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3269 }
3270#endif /* FEAT_MENU */
3271
3272#ifdef FEAT_TOOLBAR
3273 /*
3274 * Create the toolbar and handle
3275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 /* some aesthetics on the toolbar */
3277 gtk_rc_parse_string(
3278 "style \"vim-toolbar-style\" {\n"
3279 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3280 "}\n"
3281 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3282 gui.toolbar = gtk_toolbar_new();
3283 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3285
3286# ifdef FEAT_GUI_GNOME
3287 if (using_gnome)
3288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 BonoboDockItem *dockitem;
3290
3291 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3292 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3293 GNOME_APP_TOOLBAR_NAME);
3294 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003295 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003296 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003297 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003298 bonobo_dock_item_get_behavior(dockitem)
3299 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 else
3303# endif /* FEAT_GUI_GNOME */
3304 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3306 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3307 gtk_widget_show(gui.toolbar);
3308 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3309 }
3310#endif /* FEAT_TOOLBAR */
3311
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003312#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003313 /*
3314 * Use a Notebook for the tab pages labels. The labels are hidden by
3315 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003316 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003317 gui.tabline = gtk_notebook_new();
3318 gtk_widget_show(gui.tabline);
3319 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3320 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3321 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003322 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003323 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003324
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003325 tabline_tooltip = gtk_tooltips_new();
3326 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3327
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003328 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003329 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003330
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003331 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003332 page = gtk_vbox_new(FALSE, 0);
3333 gtk_widget_show(page);
3334 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3335 label = gtk_label_new("-Empty-");
3336 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003337 event_box = gtk_event_box_new();
3338 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003339 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003340 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003341 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003342 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003343 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003344
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003345 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003346 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003347
3348 /* Create a popup menu for the tab line and connect it. */
3349 tabline_menu = create_tabline_menu();
3350 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003351 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003352#endif
3353
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 gui.formwin = gtk_form_new();
3355 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3356 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3357
3358 gui.drawarea = gtk_drawing_area_new();
3359
3360 /* Determine which events we will filter. */
3361 gtk_widget_set_events(gui.drawarea,
3362 GDK_EXPOSURE_MASK |
3363 GDK_ENTER_NOTIFY_MASK |
3364 GDK_LEAVE_NOTIFY_MASK |
3365 GDK_BUTTON_PRESS_MASK |
3366 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 GDK_KEY_PRESS_MASK |
3369 GDK_KEY_RELEASE_MASK |
3370 GDK_POINTER_MOTION_MASK |
3371 GDK_POINTER_MOTION_HINT_MASK);
3372
3373 gtk_widget_show(gui.drawarea);
3374 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3375 gtk_widget_show(gui.formwin);
3376 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3377
3378 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3379 * and not the window. */
3380 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3381 : GTK_OBJECT(gui.drawarea),
3382 "key_press_event",
3383 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003384#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 /* Also forward key release events for the benefit of GTK+ 2 input
3386 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3387 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3388 : G_OBJECT(gui.drawarea),
3389 "key_release_event",
3390 G_CALLBACK(&key_release_event), NULL);
3391#endif
3392 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3393 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3394 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3395 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3396
3397 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3398 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3399
3400 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3401
3402#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3403 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3404 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3405#endif
3406
3407 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003408 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3410
3411 /*
3412 * Set clipboard specific atoms
3413 */
3414 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3417 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3418
3419 /*
3420 * Start out by adding the configured border width into the border offset.
3421 */
3422 gui.border_offset = gui.border_width;
3423
3424 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3425 GTK_SIGNAL_FUNC(visibility_event), NULL);
3426 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3427 GTK_SIGNAL_FUNC(expose_event), NULL);
3428
3429 /*
3430 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3431 * Only needed for some window managers.
3432 */
3433 if (vim_strchr(p_go, GO_POINTER) != NULL)
3434 {
3435 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3436 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3437 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3438 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3439 }
3440
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003441 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3442 * only its widgets. Arguably, this could be common code and we not use
3443 * the window focus at all, but let's be safe.
3444 */
3445 if (gtk_socket_id == 0)
3446 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003447 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3448 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3449 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3450 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003451 }
3452 else
3453 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003454 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3455 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3456 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3457 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003458#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003459 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3460 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3461 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3462 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003463#endif /* FEAT_GUI_TABLINE */
3464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
3466 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3467 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3468 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3469 GTK_SIGNAL_FUNC(button_press_event), NULL);
3470 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3471 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3473 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
3475 /*
3476 * Add selection handler functions.
3477 */
3478 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3479 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3480 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3481 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3482
Bram Moolenaara76638f2010-06-05 12:49:46 +02003483 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
3485 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3486 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3487
3488 /* Pretend we don't have input focus, we will get an event if we do. */
3489 gui.in_focus = FALSE;
3490
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 return OK;
3492}
3493
3494#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3495/*
3496 * This is called from gui_start() after a fork() has been done.
3497 * We have to tell the session manager our new PID.
3498 */
3499 void
3500gui_mch_forked(void)
3501{
3502 if (using_gnome)
3503 {
3504 GnomeClient *client;
3505
3506 client = gnome_master_client();
3507
3508 if (client != NULL)
3509 gnome_client_set_process_id(client, getpid());
3510 }
3511}
3512#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3513
3514/*
3515 * Called when the foreground or background color has been changed.
3516 * This used to change the graphics contexts directly but we are
3517 * currently manipulating them where desired.
3518 */
3519 void
3520gui_mch_new_colors(void)
3521{
3522 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3523 {
3524 GdkColor color = { 0, 0, 0, 0 };
3525
3526 color.pixel = gui.back_pixel;
3527 gdk_window_set_background(gui.drawarea->window, &color);
3528 }
3529}
3530
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531/*
3532 * This signal informs us about the need to rearrange our sub-widgets.
3533 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003535form_configure_event(GtkWidget *widget UNUSED,
3536 GdkEventConfigure *event,
3537 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003539 int usable_height = event->height;
3540
3541 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3542 * no. of char-heights), so we have to manually sanitise them.
3543 * Widths seem to sort themselves out, don't ask me why.
3544 */
3545 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003546 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003547
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003549 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 gtk_form_thaw(GTK_FORM(gui.formwin));
3551
3552 return TRUE;
3553}
3554
3555/*
3556 * Function called when window already closed.
3557 * We can't do much more here than to trying to preserve what had been done,
3558 * since the window is already inevitably going away.
3559 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003561mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
3563 /* Don't write messages to the GUI anymore */
3564 full_screen = FALSE;
3565
3566 gui.mainwin = NULL;
3567 gui.drawarea = NULL;
3568
3569 if (!exiting) /* only do anything if the destroy was unexpected */
3570 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003571 vim_strncpy(IObuff,
3572 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3573 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 preserve_exit();
3575 }
3576}
3577
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003578
3579/*
3580 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3581 * hints (and thus the required size from -geom), but that after that we
3582 * put the hints back to normal (the actual minimum size) so we may
3583 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003584 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003585 * only way we have of making ourselves bigger (by set lines/columns).
3586 * Thus set hints at start-up to ensure correct init. size, then a
3587 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003588 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003589 * We'll not let the default hints be set while this timer's active.
3590 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003591 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003592check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003593{
3594 if (init_window_hints_state == 1)
3595 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003596 /* Safe to use normal hints now */
3597 init_window_hints_state = 0;
3598 update_window_manager_hints(0, 0);
3599 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003600 }
3601
3602 /* Keep on trying */
3603 init_window_hints_state = 1;
3604 return TRUE;
3605}
3606
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607/*
3608 * Open the GUI window which was created by a call to gui_mch_init().
3609 */
3610 int
3611gui_mch_open(void)
3612{
3613 guicolor_T fg_pixel = INVALCOLOR;
3614 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003615 guint pixel_width;
3616 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /*
3619 * Allow setting a window role on the command line, or invent one
3620 * if none was specified. This is mainly useful for GNOME session
3621 * support; allowing the WM to restore window placement.
3622 */
3623 if (role_argument != NULL)
3624 {
3625 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3626 }
3627 else
3628 {
3629 char *role;
3630
3631 /* Invent a unique-enough ID string for the role */
3632 role = g_strdup_printf("vim-%u-%u-%u",
3633 (unsigned)mch_get_pid(),
3634 (unsigned)g_random_int(),
3635 (unsigned)time(NULL));
3636
3637 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3638 g_free(role);
3639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
3644 /* Determine user specified geometry, if present. */
3645 if (gui.geom != NULL)
3646 {
3647 int mask;
3648 unsigned int w, h;
3649 int x = 0;
3650 int y = 0;
3651
3652 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3653
3654 if (mask & WidthValue)
3655 Columns = w;
3656 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003657 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003658 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003659 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003661 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003662
3663 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3664 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3665
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003666 pixel_width += get_menu_tool_width();
3667 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003668
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003670 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003671 int ww, hh;
3672 gui_mch_get_screen_dimensions(&ww, &hh);
3673 hh += p_ghr + get_menu_tool_height();
3674 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003675 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003676 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003677 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003678 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 vim_free(gui.geom);
3682 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003683
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003684 /* From now until everyone's stopped trying to set the window hints
3685 * to their correct minimum values, stop them being set as we need
3686 * them to remain at our required size for the parent GtkSocket to
3687 * give us the right initial size.
3688 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003689 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003690 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003691 update_window_manager_hints(pixel_width, pixel_height);
3692 init_window_hints_state = 1;
3693 g_timeout_add(1000, check_startup_plug_hints, NULL);
3694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003697 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3698 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003699 /* For GTK2 changing the size of the form widget doesn't cause window
3700 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003701 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003702 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003703 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 if (foreground_argument != NULL)
3706 fg_pixel = gui_get_color((char_u *)foreground_argument);
3707 if (fg_pixel == INVALCOLOR)
3708 fg_pixel = gui_get_color((char_u *)"Black");
3709
3710 if (background_argument != NULL)
3711 bg_pixel = gui_get_color((char_u *)background_argument);
3712 if (bg_pixel == INVALCOLOR)
3713 bg_pixel = gui_get_color((char_u *)"White");
3714
3715 if (found_reverse_arg)
3716 {
3717 gui.def_norm_pixel = bg_pixel;
3718 gui.def_back_pixel = fg_pixel;
3719 }
3720 else
3721 {
3722 gui.def_norm_pixel = fg_pixel;
3723 gui.def_back_pixel = bg_pixel;
3724 }
3725
3726 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3727 * in a vimrc file) */
3728 set_normal_colors();
3729
3730 /* Check that none of the colors are the same as the background color */
3731 gui_check_colors();
3732
3733 /* Get the colors for the highlight groups (gui_check_colors() might have
3734 * changed them). */
3735 highlight_gui_started(); /* re-init colors and fonts */
3736
3737 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3738 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3739
3740#ifdef FEAT_HANGULIN
3741 hangul_keyboard_set();
3742#endif
3743
3744 /*
3745 * Notify the fixed area about the need to resize the contents of the
3746 * gui.formwin, which we use for random positioning of the included
3747 * components.
3748 *
3749 * We connect this signal deferred finally after anything is in place,
3750 * since this is intended to handle resizements coming from the window
3751 * manager upon us and should not interfere with what VIM is requesting
3752 * upon startup.
3753 */
3754 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3755 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3756
3757#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003758 /* Set up for receiving DND items. */
3759 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760
3761 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3762 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3763#endif
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003766 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (found_iconic_arg && gtk_socket_id == 0)
3768 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003771#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 unsigned long menu_handler = 0;
3773# ifdef FEAT_TOOLBAR
3774 unsigned long tool_handler = 0;
3775# endif
3776 /*
3777 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3778 * show when restoring the saved layout configuration. We can't just
3779 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3780 * a toplevel window and thus will be realized immediately. Instead,
3781 * connect signal handlers to hide the widgets just after they've been
3782 * marked visible, but before the main window is realized.
3783 */
3784 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3785 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3786 G_CALLBACK(&gtk_widget_hide),
3787 NULL);
3788# ifdef FEAT_TOOLBAR
3789 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3790 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3791 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3792 G_CALLBACK(&gtk_widget_hide),
3793 NULL);
3794# endif
3795#endif
3796 gtk_widget_show(gui.mainwin);
3797
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003798#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 if (menu_handler != 0)
3800 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3801# ifdef FEAT_TOOLBAR
3802 if (tool_handler != 0)
3803 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3804# endif
3805#endif
3806 }
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 return OK;
3809}
3810
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003813gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 if (gui.mainwin != NULL)
3816 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817}
3818
3819/*
3820 * Get the position of the top left corner of the window.
3821 */
3822 int
3823gui_mch_get_winpos(int *x, int *y)
3824{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 return OK;
3827}
3828
3829/*
3830 * Set the position of the top left corner of the window to the given
3831 * coordinates.
3832 */
3833 void
3834gui_mch_set_winpos(int x, int y)
3835{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837}
3838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839#if 0
3840static int resize_idle_installed = FALSE;
3841/*
3842 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3843 * the shell size doesn't exceed the window size, i.e. if the window manager
3844 * ignored our size request. Usually this happens if the window is maximized.
3845 *
3846 * FIXME: It'd be nice if we could find a little more orthodox solution.
3847 * See also the remark below in gui_mch_set_shellsize().
3848 *
3849 * DISABLED: When doing ":set lines+=1" this function would first invoke
3850 * gui_resize_shell() with the old size, then the normal callback would
3851 * report the new size through form_configure_event(). That caused the window
3852 * layout to be messed up.
3853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 static gboolean
3855force_shell_resize_idle(gpointer data)
3856{
3857 if (gui.mainwin != NULL
3858 && GTK_WIDGET_REALIZED(gui.mainwin)
3859 && GTK_WIDGET_VISIBLE(gui.mainwin))
3860 {
3861 int width;
3862 int height;
3863
3864 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3865
3866 width -= get_menu_tool_width();
3867 height -= get_menu_tool_height();
3868
3869 gui_resize_shell(width, height);
3870 }
3871
3872 resize_idle_installed = FALSE;
3873 return FALSE; /* don't call me again */
3874}
3875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar09736232009-09-23 16:14:49 +00003877/*
3878 * Return TRUE if the main window is maximized.
3879 */
3880 int
3881gui_mch_maximized()
3882{
3883 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3884 && (gdk_window_get_state(gui.mainwin->window)
3885 & GDK_WINDOW_STATE_MAXIMIZED));
3886}
3887
3888/*
3889 * Unmaximize the main window
3890 */
3891 void
3892gui_mch_unmaximize()
3893{
3894 if (gui.mainwin != NULL)
3895 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3896}
Bram Moolenaar09736232009-09-23 16:14:49 +00003897
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898/*
3899 * Set the windows size.
3900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 void
3902gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003903 int min_width UNUSED, int min_height UNUSED,
3904 int base_width UNUSED, int base_height UNUSED,
3905 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 /* give GTK+ a chance to put all widget's into place */
3908 gui_mch_update();
3909
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003910 /* this will cause the proper resizement to happen too */
3911 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003912 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003915 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 * main window instead. */
3917 width += get_menu_tool_width();
3918 height += get_menu_tool_height();
3919
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003920 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003921 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003922 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003923 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003925# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (!resize_idle_installed)
3927 {
3928 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3929 &force_shell_resize_idle, NULL, NULL);
3930 resize_idle_installed = TRUE;
3931 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 /*
3934 * Wait until all events are processed to prevent a crash because the
3935 * real size of the drawing area doesn't reflect Vim's internal ideas.
3936 *
3937 * This is a bit of a hack, since Vim is a terminal application with a GUI
3938 * on top, while the GUI expects to be the boss.
3939 */
3940 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941}
3942
3943
3944/*
3945 * The screen size is used to make sure the initial window doesn't get bigger
3946 * than the screen. This subtracts some room for menubar, toolbar and window
3947 * decorations.
3948 */
3949 void
3950gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
3951{
3952#ifdef HAVE_GTK_MULTIHEAD
3953 GdkScreen* screen;
3954
3955 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
3956 screen = gtk_widget_get_screen(gui.mainwin);
3957 else
3958 screen = gdk_screen_get_default();
3959
3960 *screen_w = gdk_screen_get_width(screen);
3961 *screen_h = gdk_screen_get_height(screen) - p_ghr;
3962#else
3963 *screen_w = gdk_screen_width();
3964 /* Subtract 'guiheadroom' from the height to allow some room for the
3965 * window manager (task list and window title bar). */
3966 *screen_h = gdk_screen_height() - p_ghr;
3967#endif
3968
3969 /*
3970 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
3971 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02003972 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 * This should be completely changed later.
3974 */
3975 *screen_w -= get_menu_tool_width();
3976 *screen_h -= get_menu_tool_height();
3977}
3978
3979#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003981gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 if (title != NULL && output_conv.vc_type != CONV_NONE)
3984 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
3987
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (output_conv.vc_type != CONV_NONE)
3989 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990}
3991#endif /* FEAT_TITLE */
3992
3993#if defined(FEAT_MENU) || defined(PROTO)
3994 void
3995gui_mch_enable_menu(int showit)
3996{
3997 GtkWidget *widget;
3998
3999# ifdef FEAT_GUI_GNOME
4000 if (using_gnome)
4001 widget = gui.menubar_h;
4002 else
4003# endif
4004 widget = gui.menubar;
4005
Bram Moolenaar18144c82006-04-12 21:52:12 +00004006 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4007 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 if (showit)
4010 gtk_widget_show(widget);
4011 else
4012 gtk_widget_hide(widget);
4013
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004014 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016}
4017#endif /* FEAT_MENU */
4018
4019#if defined(FEAT_TOOLBAR) || defined(PROTO)
4020 void
4021gui_mch_show_toolbar(int showit)
4022{
4023 GtkWidget *widget;
4024
4025 if (gui.toolbar == NULL)
4026 return;
4027
4028# ifdef FEAT_GUI_GNOME
4029 if (using_gnome)
4030 widget = gui.toolbar_h;
4031 else
4032# endif
4033 widget = gui.toolbar;
4034
4035 if (showit)
4036 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4037
4038 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4039 {
4040 if (showit)
4041 gtk_widget_show(widget);
4042 else
4043 gtk_widget_hide(widget);
4044
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004045 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047}
4048#endif /* FEAT_TOOLBAR */
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050/*
4051 * Check if a given font is a CJK font. This is done in a very crude manner. It
4052 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4053 * font. Consequently, this function cannot be used as a general purpose check
4054 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4055 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4056 */
4057 static int
4058is_cjk_font(PangoFontDescription *font_desc)
4059{
4060 static const char * const cjk_langs[] =
4061 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4062
4063 PangoFont *font;
4064 unsigned i;
4065 int is_cjk = FALSE;
4066
4067 font = pango_context_load_font(gui.text_context, font_desc);
4068
4069 if (font == NULL)
4070 return FALSE;
4071
4072 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4073 {
4074 PangoCoverage *coverage;
4075 gunichar uc;
4076
4077 coverage = pango_font_get_coverage(
4078 font, pango_language_from_string(cjk_langs[i]));
4079
4080 if (coverage != NULL)
4081 {
4082 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4083 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4084 pango_coverage_unref(coverage);
4085 }
4086 }
4087
4088 g_object_unref(font);
4089
4090 return is_cjk;
4091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092
Bram Moolenaar231334e2005-07-25 20:46:57 +00004093/*
4094 * Adjust gui.char_height (after 'linespace' was changed).
4095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004097gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 PangoFontMetrics *metrics;
4100 int ascent;
4101 int descent;
4102
4103 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4104 pango_context_get_language(gui.text_context));
4105 ascent = pango_font_metrics_get_ascent(metrics);
4106 descent = pango_font_metrics_get_descent(metrics);
4107
4108 pango_font_metrics_unref(metrics);
4109
4110 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004111 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004112 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 /* A not-positive value of char_height may crash Vim. Only happens
4116 * if 'linespace' is negative (which does make sense sometimes). */
4117 gui.char_ascent = MAX(gui.char_ascent, 0);
4118 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4119
4120 return OK;
4121}
4122
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123/*
4124 * Put up a font dialog and return the selected font name in allocated memory.
4125 * "oldval" is the previous value. Return NULL when cancelled.
4126 * This should probably go into gui_gtk.c. Hmm.
4127 * FIXME:
4128 * The GTK2 font selection dialog has no filtering API. So we could either
4129 * a) implement our own (possibly copying the code from somewhere else) or
4130 * b) just live with it.
4131 */
4132 char_u *
4133gui_mch_font_dialog(char_u *oldval)
4134{
4135 GtkWidget *dialog;
4136 int response;
4137 char_u *fontname = NULL;
4138 char_u *oldname;
4139
4140 dialog = gtk_font_selection_dialog_new(NULL);
4141
4142 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4143 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4144
4145 if (oldval != NULL && oldval[0] != NUL)
4146 {
4147 if (output_conv.vc_type != CONV_NONE)
4148 oldname = string_convert(&output_conv, oldval, NULL);
4149 else
4150 oldname = oldval;
4151
4152 /* Annoying bug in GTK (or Pango): if the font name does not include a
4153 * size, zero is used. Use default point size ten. */
4154 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4155 {
4156 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4157
4158 if (p != NULL)
4159 {
4160 STRCPY(p + STRLEN(p), " 10");
4161 if (oldname != oldval)
4162 vim_free(oldname);
4163 oldname = p;
4164 }
4165 }
4166
4167 gtk_font_selection_dialog_set_font_name(
4168 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4169
4170 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004171 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004173 else
4174 gtk_font_selection_dialog_set_font_name(
4175 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176
4177 response = gtk_dialog_run(GTK_DIALOG(dialog));
4178
4179 if (response == GTK_RESPONSE_OK)
4180 {
4181 char *name;
4182
4183 name = gtk_font_selection_dialog_get_font_name(
4184 GTK_FONT_SELECTION_DIALOG(dialog));
4185 if (name != NULL)
4186 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004187 char_u *p;
4188
4189 /* Apparently some font names include a comma, need to escape
4190 * that, because in 'guifont' it separates names. */
4191 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004193 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004194 {
4195 fontname = string_convert(&input_conv, p, NULL);
4196 vim_free(p);
4197 }
4198 else
4199 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 if (response != GTK_RESPONSE_NONE)
4204 gtk_widget_destroy(dialog);
4205
4206 return fontname;
4207}
4208
4209/*
4210 * Some monospace fonts don't support a bold weight, and fall back
4211 * silently to the regular weight. But this is no good since our text
4212 * drawing function can emulate bold by overstriking. So let's try
4213 * to detect whether bold weight is actually available and emulate it
4214 * otherwise.
4215 *
4216 * Note that we don't need to check for italic style since Xft can
4217 * emulate italic on its own, provided you have a proper fontconfig
4218 * setup. We wouldn't be able to emulate it in Vim anyway.
4219 */
4220 static void
4221get_styled_font_variants(void)
4222{
4223 PangoFontDescription *bold_font_desc;
4224 PangoFont *plain_font;
4225 PangoFont *bold_font;
4226
4227 gui.font_can_bold = FALSE;
4228
4229 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4230
4231 if (plain_font == NULL)
4232 return;
4233
4234 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4235 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4236
4237 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4238 /*
4239 * The comparison relies on the unique handle nature of a PangoFont*,
4240 * i.e. it's assumed that a different PangoFont* won't refer to the
4241 * same font. Seems to work, and failing here isn't critical anyway.
4242 */
4243 if (bold_font != NULL)
4244 {
4245 gui.font_can_bold = (bold_font != plain_font);
4246 g_object_unref(bold_font);
4247 }
4248
4249 pango_font_description_free(bold_font_desc);
4250 g_object_unref(plain_font);
4251}
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253static PangoEngineShape *default_shape_engine = NULL;
4254
4255/*
4256 * Create a map from ASCII characters in the range [32,126] to glyphs
4257 * of the current font. This is used by gui_gtk2_draw_string() to skip
4258 * the itemize and shaping process for the most common case.
4259 */
4260 static void
4261ascii_glyph_table_init(void)
4262{
4263 char_u ascii_chars[128];
4264 PangoAttrList *attr_list;
4265 GList *item_list;
4266 int i;
4267
4268 if (gui.ascii_glyphs != NULL)
4269 pango_glyph_string_free(gui.ascii_glyphs);
4270 if (gui.ascii_font != NULL)
4271 g_object_unref(gui.ascii_font);
4272
4273 gui.ascii_glyphs = NULL;
4274 gui.ascii_font = NULL;
4275
4276 /* For safety, fill in question marks for the control characters. */
4277 for (i = 0; i < 32; ++i)
4278 ascii_chars[i] = '?';
4279 for (; i < 127; ++i)
4280 ascii_chars[i] = i;
4281 ascii_chars[i] = '?';
4282
4283 attr_list = pango_attr_list_new();
4284 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4285 0, sizeof(ascii_chars), attr_list, NULL);
4286
4287 if (item_list != NULL && item_list->next == NULL) /* play safe */
4288 {
4289 PangoItem *item;
4290 int width;
4291
4292 item = (PangoItem *)item_list->data;
4293 width = gui.char_width * PANGO_SCALE;
4294
4295 /* Remember the shape engine used for ASCII. */
4296 default_shape_engine = item->analysis.shape_engine;
4297
4298 gui.ascii_font = item->analysis.font;
4299 g_object_ref(gui.ascii_font);
4300
4301 gui.ascii_glyphs = pango_glyph_string_new();
4302
4303 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4304 &item->analysis, gui.ascii_glyphs);
4305
4306 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4307
4308 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4309 {
4310 PangoGlyphGeometry *geom;
4311
4312 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4313 geom->x_offset += MAX(0, width - geom->width) / 2;
4314 geom->width = width;
4315 }
4316 }
4317
4318 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4319 g_list_free(item_list);
4320 pango_attr_list_unref(attr_list);
4321}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323/*
4324 * Initialize Vim to use the font or fontset with the given name.
4325 * Return FAIL if the font could not be loaded, OK otherwise.
4326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004328gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 PangoFontDescription *font_desc;
4331 PangoLayout *layout;
4332 int width;
4333
4334 /* If font_name is NULL, this means to use the default, which should
4335 * be present on all proper Pango/fontconfig installations. */
4336 if (font_name == NULL)
4337 font_name = (char_u *)DEFAULT_FONT;
4338
4339 font_desc = gui_mch_get_font(font_name, FALSE);
4340
4341 if (font_desc == NULL)
4342 return FAIL;
4343
4344 gui_mch_free_font(gui.norm_font);
4345 gui.norm_font = font_desc;
4346
4347 pango_context_set_font_description(gui.text_context, font_desc);
4348
4349 layout = pango_layout_new(gui.text_context);
4350 pango_layout_set_text(layout, "MW", 2);
4351 pango_layout_get_size(layout, &width, NULL);
4352 /*
4353 * Set char_width to half the width obtained from pango_layout_get_size()
4354 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4355 * Pango to use the same width for both non-CJK characters (e.g. Latin
4356 * letters and numbers) and CJK characters. This results in 's p a c e d
4357 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4358 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4359 * width for CJK fonts.
4360 *
4361 * For related bugs, see:
4362 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4363 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4364 *
4365 * With this, for all four of the following cases, Vim works fine:
4366 * guifont=CJK_fixed_width_font
4367 * guifont=Non_CJK_fixed_font
4368 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4369 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4370 */
4371 if (is_cjk_font(gui.norm_font))
4372 {
4373 int cjk_width;
4374
4375 /* Measure the text extent of U+4E00 and U+4E8C */
4376 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4377 pango_layout_get_size(layout, &cjk_width, NULL);
4378
4379 if (width == cjk_width) /* Xft not patched */
4380 width /= 2;
4381 }
4382 g_object_unref(layout);
4383
4384 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4385
4386 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4387 if (gui.char_width <= 0)
4388 gui.char_width = 8;
4389
Bram Moolenaar231334e2005-07-25 20:46:57 +00004390 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 /* Set the fontname, which will be used for information purposes */
4393 hl_set_font_name(font_name);
4394
4395 get_styled_font_variants();
4396 ascii_glyph_table_init();
4397
4398 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4399 if (gui.wide_font != NULL
4400 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4401 {
4402 pango_font_description_free(gui.wide_font);
4403 gui.wide_font = NULL;
4404 }
4405
Bram Moolenaare161c792009-11-03 17:13:59 +00004406 if (gui_mch_maximized())
4407 {
4408 int w, h;
4409
4410 /* Update lines and columns in accordance with the new font, keep the
4411 * window maximized. */
4412 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4413 w -= get_menu_tool_width();
4414 h -= get_menu_tool_height();
4415 gui_resize_shell(w, h);
4416 }
4417 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004418 {
4419 /* Preserve the logical dimensions of the screen. */
4420 update_window_manager_hints(0, 0);
4421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422
4423 return OK;
4424}
4425
4426/*
4427 * Get a reference to the font "name".
4428 * Return zero for failure.
4429 */
4430 GuiFont
4431gui_mch_get_font(char_u *name, int report_error)
4432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 /* can't do this when GUI is not running */
4436 if (!gui.in_use || name == NULL)
4437 return NULL;
4438
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 if (output_conv.vc_type != CONV_NONE)
4440 {
4441 char_u *buf;
4442
4443 buf = string_convert(&output_conv, name, NULL);
4444 if (buf != NULL)
4445 {
4446 font = pango_font_description_from_string((const char *)buf);
4447 vim_free(buf);
4448 }
4449 else
4450 font = NULL;
4451 }
4452 else
4453 font = pango_font_description_from_string((const char *)name);
4454
4455 if (font != NULL)
4456 {
4457 PangoFont *real_font;
4458
4459 /* pango_context_load_font() bails out if no font size is set */
4460 if (pango_font_description_get_size(font) <= 0)
4461 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4462
4463 real_font = pango_context_load_font(gui.text_context, font);
4464
4465 if (real_font == NULL)
4466 {
4467 pango_font_description_free(font);
4468 font = NULL;
4469 }
4470 else
4471 g_object_unref(real_font);
4472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 if (font == NULL)
4475 {
4476 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004477 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 return NULL;
4479 }
4480
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /*
4482 * The fixed-width check has been disabled for GTK+ 2. Rationale:
4483 *
4484 * - The check tends to report false positives, particularly
4485 * in non-Latin locales or with old X fonts.
4486 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
4487 * GTK+ 2 Vim is actually capable of displaying variable width
4488 * fonts. Those will just be spaced out like in AA xterm.
4489 * - Failing here for the default font causes GUI startup to fail
4490 * even with wiped out configuration files.
4491 * - The font dialog displays all fonts unfiltered, and it's rather
4492 * annoying if 95% of the listed fonts produce an error message.
4493 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004494#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 /* Check that this is a mono-spaced font. Naturally, this is a bit
4497 * hackish -- fixed-width isn't really suitable for i18n text :/ */
4498 PangoLayout *layout;
4499 unsigned int i;
4500 int last_width = -1;
4501 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
4502
4503 layout = pango_layout_new(gui.text_context);
4504 pango_layout_set_font_description(layout, font);
4505
4506 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
4507 {
4508 int width;
4509
4510 pango_layout_set_text(layout, &test_chars[i], 1);
4511 pango_layout_get_size(layout, &width, NULL);
4512
4513 if (last_width >= 0 && width != last_width)
4514 {
4515 pango_font_description_free(font);
4516 font = NULL;
4517 break;
4518 }
4519
4520 last_width = width;
4521 }
4522
4523 g_object_unref(layout);
4524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525#endif
4526
4527 return font;
4528}
4529
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004530#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004531/*
4532 * Return the name of font "font" in allocated memory.
4533 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004534 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004535gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004536{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004537 if (font != NOFONT)
4538 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004539 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004540
Bram Moolenaar89d40322006-08-29 15:30:07 +00004541 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004543 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004544
Bram Moolenaar89d40322006-08-29 15:30:07 +00004545 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004546 return s;
4547 }
4548 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004549 return NULL;
4550}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004551#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553/*
4554 * If a font is not going to be used, free its structure.
4555 */
4556 void
4557gui_mch_free_font(GuiFont font)
4558{
4559 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561}
4562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563/*
4564 * Return the Pixel value (color) for the given color name. This routine was
4565 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4566 * Programmer's Guide.
4567 * Return INVALCOLOR for error.
4568 */
4569 guicolor_T
4570gui_mch_get_color(char_u *name)
4571{
4572 /* A number of colors that some X11 systems don't have */
4573 static const char *const vimnames[][2] =
4574 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004575 {"LightRed", "#FFBBBB"},
4576 {"LightGreen", "#88FF88"},
4577 {"LightMagenta","#FFBBFF"},
4578 {"DarkCyan", "#008888"},
4579 {"DarkBlue", "#0000BB"},
4580 {"DarkRed", "#BB0000"},
4581 {"DarkMagenta", "#BB00BB"},
4582 {"DarkGrey", "#BBBBBB"},
4583 {"DarkYellow", "#BBBB00"},
4584 {"Gray10", "#1A1A1A"},
4585 {"Grey10", "#1A1A1A"},
4586 {"Gray20", "#333333"},
4587 {"Grey20", "#333333"},
4588 {"Gray30", "#4D4D4D"},
4589 {"Grey30", "#4D4D4D"},
4590 {"Gray40", "#666666"},
4591 {"Grey40", "#666666"},
4592 {"Gray50", "#7F7F7F"},
4593 {"Grey50", "#7F7F7F"},
4594 {"Gray60", "#999999"},
4595 {"Grey60", "#999999"},
4596 {"Gray70", "#B3B3B3"},
4597 {"Grey70", "#B3B3B3"},
4598 {"Gray80", "#CCCCCC"},
4599 {"Grey80", "#CCCCCC"},
4600 {"Gray90", "#E5E5E5"},
4601 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 {NULL, NULL}
4603 };
4604
4605 if (!gui.in_use) /* can't do this when GUI not running */
4606 return INVALCOLOR;
4607
4608 while (name != NULL)
4609 {
4610 GdkColor color;
4611 int parsed;
4612 int i;
4613
4614 parsed = gdk_color_parse((const char *)name, &color);
4615
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (parsed)
4617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4619 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return (guicolor_T)color.pixel;
4621 }
4622 /* add a few builtin names and try again */
4623 for (i = 0; ; ++i)
4624 {
4625 if (vimnames[i][0] == NULL)
4626 {
4627 name = NULL;
4628 break;
4629 }
4630 if (STRICMP(name, vimnames[i][0]) == 0)
4631 {
4632 name = (char_u *)vimnames[i][1];
4633 break;
4634 }
4635 }
4636 }
4637
4638 return INVALCOLOR;
4639}
4640
4641/*
4642 * Set the current text foreground color.
4643 */
4644 void
4645gui_mch_set_fg_color(guicolor_T color)
4646{
4647 gui.fgcolor->pixel = (unsigned long)color;
4648}
4649
4650/*
4651 * Set the current text background color.
4652 */
4653 void
4654gui_mch_set_bg_color(guicolor_T color)
4655{
4656 gui.bgcolor->pixel = (unsigned long)color;
4657}
4658
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004659/*
4660 * Set the current text special color.
4661 */
4662 void
4663gui_mch_set_sp_color(guicolor_T color)
4664{
4665 gui.spcolor->pixel = (unsigned long)color;
4666}
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668/*
4669 * Function-like convenience macro for the sake of efficiency.
4670 */
4671#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4672 G_STMT_START{ \
4673 PangoAttribute *tmp_attr_; \
4674 tmp_attr_ = (Attribute); \
4675 tmp_attr_->start_index = (Start); \
4676 tmp_attr_->end_index = (End); \
4677 pango_attr_list_insert((AttrList), tmp_attr_); \
4678 }G_STMT_END
4679
4680 static void
4681apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4682{
4683 char_u *start = NULL;
4684 char_u *p;
4685 int uc;
4686
4687 for (p = s; p < s + len; p += utf_byte2len(*p))
4688 {
4689 uc = utf_ptr2char(p);
4690
4691 if (start == NULL)
4692 {
4693 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4694 start = p;
4695 }
4696 else if (uc < 0x80 /* optimization shortcut */
4697 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4698 {
4699 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4700 attr_list, start - s, p - s);
4701 start = NULL;
4702 }
4703 }
4704
4705 if (start != NULL)
4706 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4707 attr_list, start - s, len);
4708}
4709
4710 static int
4711count_cluster_cells(char_u *s, PangoItem *item,
4712 PangoGlyphString* glyphs, int i,
4713 int *cluster_width,
4714 int *last_glyph_rbearing)
4715{
4716 char_u *p;
4717 int next; /* glyph start index of next cluster */
4718 int start, end; /* string segment of current cluster */
4719 int width; /* real cluster width in Pango units */
4720 int uc;
4721 int cellcount = 0;
4722
4723 width = glyphs->glyphs[i].geometry.width;
4724
4725 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4726 {
4727 if (glyphs->glyphs[next].attr.is_cluster_start)
4728 break;
4729 else if (glyphs->glyphs[next].geometry.width > width)
4730 width = glyphs->glyphs[next].geometry.width;
4731 }
4732
4733 start = item->offset + glyphs->log_clusters[i];
4734 end = item->offset + ((next < glyphs->num_glyphs) ?
4735 glyphs->log_clusters[next] : item->length);
4736
4737 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4738 {
4739 uc = utf_ptr2char(p);
4740 if (uc < 0x80)
4741 ++cellcount;
4742 else if (!utf_iscomposing(uc))
4743 cellcount += utf_char2cells(uc);
4744 }
4745
4746 if (last_glyph_rbearing != NULL
4747 && cellcount > 0 && next == glyphs->num_glyphs)
4748 {
4749 PangoRectangle ink_rect;
4750 /*
4751 * If a certain combining mark had to be taken from a non-monospace
4752 * font, we have to compensate manually by adapting x_offset according
4753 * to the ink extents of the previous glyph.
4754 */
4755 pango_font_get_glyph_extents(item->analysis.font,
4756 glyphs->glyphs[i].glyph,
4757 &ink_rect, NULL);
4758
4759 if (PANGO_RBEARING(ink_rect) > 0)
4760 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4761 }
4762
4763 if (cellcount > 0)
4764 *cluster_width = width;
4765
4766 return cellcount;
4767}
4768
4769/*
4770 * If there are only combining characters in the cluster, we cannot just
4771 * change the width of the previous glyph since there is none. Therefore
4772 * some guesswork is needed.
4773 *
4774 * If ink_rect.x is negative Pango apparently has taken care of the composing
4775 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4776 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004777 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 *
4779 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4780 * the position of the previous glyph. Apparently this happens only with old
4781 * X fonts which don't provide the special combining information needed by
4782 * Pango.
4783 */
4784 static void
4785setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4786 int last_cellcount, int last_cluster_width,
4787 int last_glyph_rbearing)
4788{
4789 PangoRectangle ink_rect;
4790 PangoRectangle logical_rect;
4791 int width;
4792
4793 width = last_cellcount * gui.char_width * PANGO_SCALE;
4794 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4795 glyph->geometry.width = 0;
4796
4797 pango_font_get_glyph_extents(item->analysis.font,
4798 glyph->glyph,
4799 &ink_rect, &logical_rect);
4800 if (ink_rect.x < 0)
4801 {
4802 glyph->geometry.x_offset += last_glyph_rbearing;
4803 glyph->geometry.y_offset = logical_rect.height
4804 - (gui.char_height - p_linespace) * PANGO_SCALE;
4805 }
4806}
4807
4808 static void
4809draw_glyph_string(int row, int col, int num_cells, int flags,
4810 PangoFont *font, PangoGlyphString *glyphs)
4811{
4812 if (!(flags & DRAW_TRANSP))
4813 {
4814 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4815
4816 gdk_draw_rectangle(gui.drawarea->window,
4817 gui.text_gc,
4818 TRUE,
4819 FILL_X(col),
4820 FILL_Y(row),
4821 num_cells * gui.char_width,
4822 gui.char_height);
4823 }
4824
4825 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4826
4827 gdk_draw_glyphs(gui.drawarea->window,
4828 gui.text_gc,
4829 font,
4830 TEXT_X(col),
4831 TEXT_Y(row),
4832 glyphs);
4833
4834 /* redraw the contents with an offset of 1 to emulate bold */
4835 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4836 gdk_draw_glyphs(gui.drawarea->window,
4837 gui.text_gc,
4838 font,
4839 TEXT_X(col) + 1,
4840 TEXT_Y(row),
4841 glyphs);
4842}
4843
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004844/*
4845 * Draw underline and undercurl at the bottom of the character cell.
4846 */
4847 static void
4848draw_under(int flags, int row, int col, int cells)
4849{
4850 int i;
4851 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004852 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004853 int y = FILL_Y(row + 1) - 1;
4854
4855 /* Undercurl: draw curl at the bottom of the character cell. */
4856 if (flags & DRAW_UNDERC)
4857 {
4858 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4859 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4860 {
4861 offset = val[i % 8];
4862 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4863 }
4864 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4865 }
4866
4867 /* Underline: draw a line at the bottom of the character cell. */
4868 if (flags & DRAW_UNDERL)
4869 {
4870 /* When p_linespace is 0, overwrite the bottom row of pixels.
4871 * Otherwise put the line just below the character. */
4872 if (p_linespace > 1)
4873 y -= p_linespace - 1;
4874 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4875 FILL_X(col), y,
4876 FILL_X(col + cells) - 1, y);
4877 }
4878}
4879
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 int
4881gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4882{
4883 GdkRectangle area; /* area for clip mask */
4884 PangoGlyphString *glyphs; /* glyphs of current item */
4885 int column_offset = 0; /* column offset in cells */
4886 int i;
4887 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4888 char_u *new_conv_buf;
4889 int convlen;
4890 char_u *sp, *bp;
4891 int plen;
4892
4893 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4894 return len;
4895
4896 if (output_conv.vc_type != CONV_NONE)
4897 {
4898 /*
4899 * Convert characters from 'encoding' to 'termencoding', which is set
4900 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4901 * prohibits changing this to something else than UTF-8 if the GUI is
4902 * in use.
4903 */
4904 convlen = len;
4905 conv_buf = string_convert(&output_conv, s, &convlen);
4906 g_return_val_if_fail(conv_buf != NULL, len);
4907
4908 /* Correct for differences in char width: some chars are
4909 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4910 * compensate for that. */
4911 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4912 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004913 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4915 {
4916 new_conv_buf = alloc(convlen + 2);
4917 if (new_conv_buf == NULL)
4918 return len;
4919 plen += bp - conv_buf;
4920 mch_memmove(new_conv_buf, conv_buf, plen);
4921 new_conv_buf[plen] = ' ';
4922 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4923 convlen - plen + 1);
4924 vim_free(conv_buf);
4925 conv_buf = new_conv_buf;
4926 ++convlen;
4927 bp = conv_buf + plen;
4928 plen = 1;
4929 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004930 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 bp += plen;
4932 }
4933 s = conv_buf;
4934 len = convlen;
4935 }
4936
4937 /*
4938 * Restrict all drawing to the current screen line in order to prevent
4939 * fuzzy font lookups from messing up the screen.
4940 */
4941 area.x = gui.border_offset;
4942 area.y = FILL_Y(row);
4943 area.width = gui.num_cols * gui.char_width;
4944 area.height = gui.char_height;
4945
4946 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4947 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4948
4949 glyphs = pango_glyph_string_new();
4950
4951 /*
4952 * Optimization hack: If possible, skip the itemize and shaping process
4953 * for pure ASCII strings. This optimization is particularly effective
4954 * because Vim draws space characters to clear parts of the screen.
4955 */
4956 if (!(flags & DRAW_ITALIC)
4957 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4958 && gui.ascii_glyphs != NULL)
4959 {
4960 char_u *p;
4961
4962 for (p = s; p < s + len; ++p)
4963 if (*p & 0x80)
4964 goto not_ascii;
4965
4966 pango_glyph_string_set_size(glyphs, len);
4967
4968 for (i = 0; i < len; ++i)
4969 {
4970 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4971 glyphs->log_clusters[i] = i;
4972 }
4973
4974 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
4975
4976 column_offset = len;
4977 }
4978 else
4979not_ascii:
4980 {
4981 PangoAttrList *attr_list;
4982 GList *item_list;
4983 int cluster_width;
4984 int last_glyph_rbearing;
4985 int cells = 0; /* cells occupied by current cluster */
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004986#if 0
4987 int monospace13 = STRICMP(p_guifont, "monospace 13") == 0;
4988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004990 /* Safety check: pango crashes when invoked with invalid utf-8
4991 * characters. */
4992 if (!utf_valid_string(s, s + len))
4993 {
4994 column_offset = len;
4995 goto skipitall;
4996 }
4997
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /* original width of the current cluster */
4999 cluster_width = PANGO_SCALE * gui.char_width;
5000
5001 /* right bearing of the last non-composing glyph */
5002 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5003
5004 attr_list = pango_attr_list_new();
5005
5006 /* If 'guifontwide' is set then use that for double-width characters.
5007 * Otherwise just go with 'guifont' and let Pango do its thing. */
5008 if (gui.wide_font != NULL)
5009 apply_wide_font_attr(s, len, attr_list);
5010
5011 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5012 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5013 attr_list, 0, len);
5014 if (flags & DRAW_ITALIC)
5015 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5016 attr_list, 0, len);
5017 /*
5018 * Break the text into segments with consistent directional level
5019 * and shaping engine. Pure Latin text needs only a single segment,
5020 * so there's no need to worry about the loop's efficiency. Better
5021 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5022 */
5023 item_list = pango_itemize(gui.text_context,
5024 (const char *)s, 0, len, attr_list, NULL);
5025
5026 while (item_list != NULL)
5027 {
5028 PangoItem *item;
5029 int item_cells = 0; /* item length in cells */
5030
5031 item = (PangoItem *)item_list->data;
5032 item_list = g_list_delete_link(item_list, item_list);
5033 /*
5034 * Increment the bidirectional embedding level by 1 if it is not
5035 * even. An odd number means the output will be RTL, but we don't
5036 * want that since Vim handles right-to-left text on its own. It
5037 * would probably be sufficient to just set level = 0, but you can
5038 * never know :)
5039 *
5040 * Unfortunately we can't take advantage of Pango's ability to
5041 * render both LTR and RTL at the same time. In order to support
5042 * that, Vim's main screen engine would have to make use of Pango
5043 * functionality.
5044 */
5045 item->analysis.level = (item->analysis.level + 1) & (~1U);
5046
5047 /* HACK: Overrule the shape engine, we don't want shaping to be
5048 * done, because drawing the cursor would change the display. */
5049 item->analysis.shape_engine = default_shape_engine;
5050
5051 pango_shape((const char *)s + item->offset, item->length,
5052 &item->analysis, glyphs);
5053 /*
5054 * Fixed-width hack: iterate over the array and assign a fixed
5055 * width to each glyph, thus overriding the choice made by the
5056 * shaping engine. We use utf_char2cells() to determine the
5057 * number of cells needed.
5058 *
5059 * Also perform all kind of dark magic to get composing
5060 * characters right (and pretty too of course).
5061 */
5062 for (i = 0; i < glyphs->num_glyphs; ++i)
5063 {
5064 PangoGlyphInfo *glyph;
5065
5066 glyph = &glyphs->glyphs[i];
5067
5068 if (glyph->attr.is_cluster_start)
5069 {
5070 int cellcount;
5071
5072 cellcount = count_cluster_cells(
5073 s, item, glyphs, i, &cluster_width,
5074 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5075
5076 if (cellcount > 0)
5077 {
5078 int width;
5079
5080 width = cellcount * gui.char_width * PANGO_SCALE;
5081 glyph->geometry.x_offset +=
5082 MAX(0, width - cluster_width) / 2;
5083 glyph->geometry.width = width;
5084 }
5085 else
5086 {
5087 /* If there are only combining characters in the
5088 * cluster, we cannot just change the width of the
5089 * previous glyph since there is none. Therefore
5090 * some guesswork is needed. */
5091 setup_zero_width_cluster(item, glyph, cells,
5092 cluster_width,
5093 last_glyph_rbearing);
5094 }
5095
5096 item_cells += cellcount;
5097 cells = cellcount;
5098 }
5099 else if (i > 0)
5100 {
5101 int width;
5102
5103 /* There is a previous glyph, so we deal with combining
5104 * characters the canonical way. That is, setting the
5105 * width of the previous glyph to 0. */
5106 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 width = cells * gui.char_width * PANGO_SCALE;
5108 glyph->geometry.x_offset +=
5109 MAX(0, width - cluster_width) / 2;
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005110#if 0
5111 /* Dirty hack: for "monospace 13" font there is a bug that
5112 * draws composing chars in the wrong position. Add
5113 * "width" to the offset to work around that. */
5114 if (monospace13)
5115 glyph->geometry.x_offset = width;
5116#endif
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 glyph->geometry.width = width;
5119 }
5120 else /* i == 0 "cannot happen" */
5121 {
5122 glyph->geometry.width = 0;
5123 }
5124 }
5125
5126 /*** Aaaaand action! ***/
5127 draw_glyph_string(row, col + column_offset, item_cells,
5128 flags, item->analysis.font, glyphs);
5129
5130 pango_item_free(item);
5131
5132 column_offset += item_cells;
5133 }
5134
5135 pango_attr_list_unref(attr_list);
5136 }
5137
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005138skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005139 /* Draw underline and undercurl. */
5140 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
5142 pango_glyph_string_free(glyphs);
5143 vim_free(conv_buf);
5144
5145 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5146
5147 return column_offset;
5148}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150/*
5151 * Return OK if the key with the termcap name "name" is supported.
5152 */
5153 int
5154gui_mch_haskey(char_u *name)
5155{
5156 int i;
5157
5158 for (i = 0; special_keys[i].key_sym != 0; i++)
5159 if (name[0] == special_keys[i].code0
5160 && name[1] == special_keys[i].code1)
5161 return OK;
5162 return FAIL;
5163}
5164
5165#if defined(FEAT_TITLE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 || defined(PROTO)
5167/*
5168 * Return the text window-id and display. Only required for X-based GUI's
5169 */
5170 int
5171gui_get_x11_windis(Window *win, Display **dis)
5172{
5173 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5174 {
5175 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5176 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5177 return OK;
5178 }
5179
5180 *dis = NULL;
5181 *win = 0;
5182 return FAIL;
5183}
5184#endif
5185
5186#if defined(FEAT_CLIENTSERVER) \
5187 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5188
5189 Display *
5190gui_mch_get_display(void)
5191{
5192 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5193 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5194 else
5195 return NULL;
5196}
5197#endif
5198
5199 void
5200gui_mch_beep(void)
5201{
5202#ifdef HAVE_GTK_MULTIHEAD
5203 GdkDisplay *display;
5204
5205 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5206 display = gtk_widget_get_display(gui.mainwin);
5207 else
5208 display = gdk_display_get_default();
5209
5210 if (display != NULL)
5211 gdk_display_beep(display);
5212#else
5213 gdk_beep();
5214#endif
5215}
5216
5217 void
5218gui_mch_flash(int msec)
5219{
5220 GdkGCValues values;
5221 GdkGC *invert_gc;
5222
5223 if (gui.drawarea->window == NULL)
5224 return;
5225
5226 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5227 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5228 values.function = GDK_XOR;
5229 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5230 &values,
5231 GDK_GC_FOREGROUND |
5232 GDK_GC_BACKGROUND |
5233 GDK_GC_FUNCTION);
5234 gdk_gc_set_exposures(invert_gc,
5235 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5236 /*
5237 * Do a visual beep by changing back and forth in some undetermined way,
5238 * the foreground and background colors. This is due to the fact that
5239 * there can't be really any prediction about the effects of XOR on
5240 * arbitrary X11 servers. However this seems to be enough for what we
5241 * intend it to do.
5242 */
5243 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5244 TRUE,
5245 0, 0,
5246 FILL_X((int)Columns) + gui.border_offset,
5247 FILL_Y((int)Rows) + gui.border_offset);
5248
5249 gui_mch_flush();
5250 ui_delay((long)msec, TRUE); /* wait so many msec */
5251
5252 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5253 TRUE,
5254 0, 0,
5255 FILL_X((int)Columns) + gui.border_offset,
5256 FILL_Y((int)Rows) + gui.border_offset);
5257
5258 gdk_gc_destroy(invert_gc);
5259}
5260
5261/*
5262 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5263 */
5264 void
5265gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5266{
5267 GdkGCValues values;
5268 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 if (gui.drawarea->window == NULL)
5271 return;
5272
Bram Moolenaar89d40322006-08-29 15:30:07 +00005273 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5274 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 values.function = GDK_XOR;
5276 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5277 &values,
5278 GDK_GC_FOREGROUND |
5279 GDK_GC_BACKGROUND |
5280 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005281 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5282 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5284 TRUE,
5285 FILL_X(c), FILL_Y(r),
5286 (nc) * gui.char_width, (nr) * gui.char_height);
5287 gdk_gc_destroy(invert_gc);
5288}
5289
5290/*
5291 * Iconify the GUI window.
5292 */
5293 void
5294gui_mch_iconify(void)
5295{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297}
5298
5299#if defined(FEAT_EVAL) || defined(PROTO)
5300/*
5301 * Bring the Vim window to the foreground.
5302 */
5303 void
5304gui_mch_set_foreground(void)
5305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307}
5308#endif
5309
5310/*
5311 * Draw a cursor without focus.
5312 */
5313 void
5314gui_mch_draw_hollow_cursor(guicolor_T color)
5315{
5316 int i = 1;
5317
5318 if (gui.drawarea->window == NULL)
5319 return;
5320
5321 gui_mch_set_fg_color(color);
5322
5323 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 if (mb_lefthalve(gui.row, gui.col))
5325 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5327 FALSE,
5328 FILL_X(gui.col), FILL_Y(gui.row),
5329 i * gui.char_width - 1, gui.char_height - 1);
5330}
5331
5332/*
5333 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5334 * color "color".
5335 */
5336 void
5337gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5338{
5339 if (gui.drawarea->window == NULL)
5340 return;
5341
5342 gui_mch_set_fg_color(color);
5343
5344 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5345 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5346 TRUE,
5347#ifdef FEAT_RIGHTLEFT
5348 /* vertical line should be on the right of current point */
5349 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5350#endif
5351 FILL_X(gui.col),
5352 FILL_Y(gui.row) + gui.char_height - h,
5353 w, h);
5354}
5355
5356
5357/*
5358 * Catch up with any queued X11 events. This may put keyboard input into the
5359 * input buffer, call resize call-backs, trigger timers etc. If there is
5360 * nothing in the X11 event queue (& no timers pending), then we return
5361 * immediately.
5362 */
5363 void
5364gui_mch_update(void)
5365{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005366 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5367 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368}
5369
5370 static gint
5371input_timer_cb(gpointer data)
5372{
5373 int *timed_out = (int *) data;
5374
Bram Moolenaar49325942007-05-10 19:19:59 +00005375 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 *timed_out = TRUE;
5377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 return FALSE; /* don't happen again */
5379}
5380
5381#ifdef FEAT_SNIFF
5382/*
5383 * Callback function, used when data is available on the SNiFF connection.
5384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 static void
5386sniff_request_cb(
5387 gpointer data,
5388 gint source_fd,
5389 GdkInputCondition condition)
5390{
5391 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5392
5393 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395#endif
5396
5397/*
5398 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5399 * from the keyboard.
5400 * wtime == -1 Wait forever.
5401 * wtime == 0 This should never happen.
5402 * wtime > 0 Wait wtime milliseconds for a character.
5403 * Returns OK if a character was found to be available within the given time,
5404 * or FAIL otherwise.
5405 */
5406 int
5407gui_mch_wait_for_chars(long wtime)
5408{
5409 int focus;
5410 guint timer;
5411 static int timed_out;
5412#ifdef FEAT_SNIFF
5413 static int sniff_on = 0;
5414 static gint sniff_input_id = 0;
5415#endif
5416
5417#ifdef FEAT_SNIFF
5418 if (sniff_on && !want_sniff_request)
5419 {
5420 if (sniff_input_id)
5421 gdk_input_remove(sniff_input_id);
5422 sniff_on = 0;
5423 }
5424 else if (!sniff_on && want_sniff_request)
5425 {
5426 /* Add fd_from_sniff to watch for available data in main loop. */
5427 sniff_input_id = gdk_input_add(fd_from_sniff,
5428 GDK_INPUT_READ, sniff_request_cb, NULL);
5429 sniff_on = 1;
5430 }
5431#endif
5432
5433 timed_out = FALSE;
5434
5435 /* this timeout makes sure that we will return if no characters arrived in
5436 * time */
5437
5438 if (wtime > 0)
5439 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5440 else
5441 timer = 0;
5442
5443 focus = gui.in_focus;
5444
5445 do
5446 {
5447 /* Stop or start blinking when focus changes */
5448 if (gui.in_focus != focus)
5449 {
5450 if (gui.in_focus)
5451 gui_mch_start_blink();
5452 else
5453 gui_mch_stop_blink();
5454 focus = gui.in_focus;
5455 }
5456
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005457#if defined(FEAT_NETBEANS_INTG)
5458 /* Process the queued netbeans messages. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005459 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005460#endif
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 /*
5463 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005464 * Skip this if input is available anyway (can happen in rare
5465 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005467 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005468 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469
5470 /* Got char, return immediately */
5471 if (input_available())
5472 {
5473 if (timer != 0 && !timed_out)
5474 gtk_timeout_remove(timer);
5475 return OK;
5476 }
5477 } while (wtime < 0 || !timed_out);
5478
5479 /*
5480 * Flush all eventually pending (drawing) events.
5481 */
5482 gui_mch_update();
5483
5484 return FAIL;
5485}
5486
5487
5488/****************************************************************************
5489 * Output drawing routines.
5490 ****************************************************************************/
5491
5492
5493/* Flush any output to the screen */
5494 void
5495gui_mch_flush(void)
5496{
5497#ifdef HAVE_GTK_MULTIHEAD
5498 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5499 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5500#else
5501 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 /* This happens to actually do what gui_mch_flush() is supposed to do,
5504 * according to the comment above. */
5505 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5506 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507}
5508
5509/*
5510 * Clear a rectangular region of the screen from text pos (row1, col1) to
5511 * (row2, col2) inclusive.
5512 */
5513 void
5514gui_mch_clear_block(int row1, int col1, int row2, int col2)
5515{
5516 GdkColor color;
5517
5518 if (gui.drawarea->window == NULL)
5519 return;
5520
5521 color.pixel = gui.back_pixel;
5522
5523 gdk_gc_set_foreground(gui.text_gc, &color);
5524
5525 /* Clear one extra pixel at the far right, for when bold characters have
5526 * spilled over to the window border. */
5527 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5528 FILL_X(col1), FILL_Y(row1),
5529 (col2 - col1 + 1) * gui.char_width
5530 + (col2 == Columns - 1),
5531 (row2 - row1 + 1) * gui.char_height);
5532}
5533
5534 void
5535gui_mch_clear_all(void)
5536{
5537 if (gui.drawarea->window != NULL)
5538 gdk_window_clear(gui.drawarea->window);
5539}
5540
5541/*
5542 * Redraw any text revealed by scrolling up/down.
5543 */
5544 static void
5545check_copy_area(void)
5546{
5547 GdkEvent *event;
5548 int expose_count;
5549
5550 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5551 return;
5552
5553 /* Avoid redrawing the cursor while scrolling or it'll end up where
5554 * we don't want it to be. I'm not sure if it's correct to call
5555 * gui_dont_update_cursor() at this point but it works as a quick
5556 * fix for now. */
5557 gui_dont_update_cursor();
5558
5559 do
5560 {
5561 /* Wait to check whether the scroll worked or not. */
5562 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5563
5564 if (event == NULL)
5565 break; /* received NoExpose event */
5566
5567 gui_redraw(event->expose.area.x, event->expose.area.y,
5568 event->expose.area.width, event->expose.area.height);
5569
5570 expose_count = event->expose.count;
5571 gdk_event_free(event);
5572 }
5573 while (expose_count > 0); /* more events follow */
5574
5575 gui_can_update_cursor();
5576}
5577
5578/*
5579 * Delete the given number of lines from the given row, scrolling up any
5580 * text further down within the scroll region.
5581 */
5582 void
5583gui_mch_delete_lines(int row, int num_lines)
5584{
5585 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5586 return; /* Can't see the window */
5587
5588 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5589 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5590
5591 /* copy one extra pixel, for when bold has spilled over */
5592 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5593 FILL_X(gui.scroll_region_left), FILL_Y(row),
5594 gui.drawarea->window,
5595 FILL_X(gui.scroll_region_left),
5596 FILL_Y(row + num_lines),
5597 gui.char_width * (gui.scroll_region_right
5598 - gui.scroll_region_left + 1) + 1,
5599 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5600
5601 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5602 gui.scroll_region_left,
5603 gui.scroll_region_bot, gui.scroll_region_right);
5604 check_copy_area();
5605}
5606
5607/*
5608 * Insert the given number of lines before the given row, scrolling down any
5609 * following text within the scroll region.
5610 */
5611 void
5612gui_mch_insert_lines(int row, int num_lines)
5613{
5614 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5615 return; /* Can't see the window */
5616
5617 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5618 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5619
5620 /* copy one extra pixel, for when bold has spilled over */
5621 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5622 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5623 gui.drawarea->window,
5624 FILL_X(gui.scroll_region_left), FILL_Y(row),
5625 gui.char_width * (gui.scroll_region_right
5626 - gui.scroll_region_left + 1) + 1,
5627 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5628
5629 gui_clear_block(row, gui.scroll_region_left,
5630 row + num_lines - 1, gui.scroll_region_right);
5631 check_copy_area();
5632}
5633
5634/*
5635 * X Selection stuff, for cutting and pasting text to other windows.
5636 */
5637 void
5638clip_mch_request_selection(VimClipboard *cbd)
5639{
5640 GdkAtom target;
5641 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005642 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
5644 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5645 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005646 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5647 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 received_selection = RS_NONE;
5649 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5650
5651 gtk_selection_convert(gui.drawarea,
5652 cbd->gtk_sel_atom, target,
5653 (guint32)GDK_CURRENT_TIME);
5654
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005655 /* Hack: Wait up to three seconds for the selection. A hang was
5656 * noticed here when using the netrw plugin combined with ":gui"
5657 * during the FocusGained event. */
5658 start = time(NULL);
5659 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005660 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
5662 if (received_selection != RS_FAIL)
5663 return;
5664 }
5665
5666 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005667 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668}
5669
5670/*
5671 * Disown the selection.
5672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005674clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675{
5676 /* WEIRD: when using NULL to actually disown the selection, we lose the
5677 * selection the first time we own it. */
5678 /*
5679 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
5680 gui_mch_update();
5681 */
5682}
5683
5684/*
5685 * Own the selection and return OK if it worked.
5686 */
5687 int
5688clip_mch_own_selection(VimClipboard *cbd)
5689{
5690 int success;
5691
5692 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar7cfea752010-06-22 06:07:12 +02005693 clipboard_event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 gui_mch_update();
5695 return (success) ? OK : FAIL;
5696}
5697
5698/*
5699 * Send the current selection to the clipboard. Do nothing for X because we
5700 * will fill in the selection only when requested by another app.
5701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005703clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
5705}
5706
5707
5708#if defined(FEAT_MENU) || defined(PROTO)
5709/*
5710 * Make a menu item appear either active or not active (grey or not grey).
5711 */
5712 void
5713gui_mch_menu_grey(vimmenu_T *menu, int grey)
5714{
5715 if (menu->id == NULL)
5716 return;
5717
5718 if (menu_is_separator(menu->name))
5719 grey = TRUE;
5720
5721 gui_mch_menu_hidden(menu, FALSE);
5722 /* Be clever about bitfields versus true booleans here! */
5723 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5724 {
5725 gtk_widget_set_sensitive(menu->id, !grey);
5726 gui_mch_update();
5727 }
5728}
5729
5730/*
5731 * Make menu item hidden or not hidden.
5732 */
5733 void
5734gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5735{
5736 if (menu->id == 0)
5737 return;
5738
5739 if (hidden)
5740 {
5741 if (GTK_WIDGET_VISIBLE(menu->id))
5742 {
5743 gtk_widget_hide(menu->id);
5744 gui_mch_update();
5745 }
5746 }
5747 else
5748 {
5749 if (!GTK_WIDGET_VISIBLE(menu->id))
5750 {
5751 gtk_widget_show(menu->id);
5752 gui_mch_update();
5753 }
5754 }
5755}
5756
5757/*
5758 * This is called after setting all the menus to grey/hidden or not.
5759 */
5760 void
5761gui_mch_draw_menubar(void)
5762{
5763 /* just make sure that the visual changes get effect immediately */
5764 gui_mch_update();
5765}
5766#endif /* FEAT_MENU */
5767
5768/*
5769 * Scrollbar stuff.
5770 */
5771 void
5772gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5773{
5774 if (sb->id == NULL)
5775 return;
5776
5777 if (flag)
5778 gtk_widget_show(sb->id);
5779 else
5780 gtk_widget_hide(sb->id);
5781
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005782 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783}
5784
5785
5786/*
5787 * Return the RGB value of a pixel as long.
5788 */
5789 long_u
5790gui_mch_get_rgb(guicolor_T pixel)
5791{
5792 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5794 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 return (((unsigned)color.red & 0xff00) << 8)
5797 | ((unsigned)color.green & 0xff00)
5798 | (((unsigned)color.blue & 0xff00) >> 8);
5799}
5800
5801/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005802 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005804 void
5805gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005807 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808}
5809
5810 void
5811gui_mch_setmouse(int x, int y)
5812{
5813 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5814 * internal GDK mechanism present to accomplish this. (and for good
5815 * reason...) */
5816 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5817 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5818 0, 0, 0U, 0U, x, y);
5819}
5820
5821
5822#ifdef FEAT_MOUSESHAPE
5823/* The last set mouse pointer shape is remembered, to be used when it goes
5824 * from hidden to not hidden. */
5825static int last_shape = 0;
5826#endif
5827
5828/*
5829 * Use the blank mouse pointer or not.
5830 *
5831 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5832 */
5833 void
5834gui_mch_mousehide(int hide)
5835{
5836 if (gui.pointer_hidden != hide)
5837 {
5838 gui.pointer_hidden = hide;
5839 if (gui.drawarea->window && gui.blank_pointer != NULL)
5840 {
5841 if (hide)
5842 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5843 else
5844#ifdef FEAT_MOUSESHAPE
5845 mch_set_mouse_shape(last_shape);
5846#else
5847 gdk_window_set_cursor(gui.drawarea->window, NULL);
5848#endif
5849 }
5850 }
5851}
5852
5853#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5854
5855/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5856 * misc2.c! */
5857static const int mshape_ids[] =
5858{
5859 GDK_LEFT_PTR, /* arrow */
5860 GDK_CURSOR_IS_PIXMAP, /* blank */
5861 GDK_XTERM, /* beam */
5862 GDK_SB_V_DOUBLE_ARROW, /* updown */
5863 GDK_SIZING, /* udsizing */
5864 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5865 GDK_SIZING, /* lrsizing */
5866 GDK_WATCH, /* busy */
5867 GDK_X_CURSOR, /* no */
5868 GDK_CROSSHAIR, /* crosshair */
5869 GDK_HAND1, /* hand1 */
5870 GDK_HAND2, /* hand2 */
5871 GDK_PENCIL, /* pencil */
5872 GDK_QUESTION_ARROW, /* question */
5873 GDK_RIGHT_PTR, /* right-arrow */
5874 GDK_CENTER_PTR, /* up-arrow */
5875 GDK_LEFT_PTR /* last one */
5876};
5877
5878 void
5879mch_set_mouse_shape(int shape)
5880{
5881 int id;
5882 GdkCursor *c;
5883
5884 if (gui.drawarea->window == NULL)
5885 return;
5886
5887 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5888 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5889 else
5890 {
5891 if (shape >= MSHAPE_NUMBERED)
5892 {
5893 id = shape - MSHAPE_NUMBERED;
5894 if (id >= GDK_LAST_CURSOR)
5895 id = GDK_LEFT_PTR;
5896 else
5897 id &= ~1; /* they are always even (why?) */
5898 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005899 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005901 else
5902 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903# ifdef HAVE_GTK_MULTIHEAD
5904 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005905 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005907 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908# endif
5909 gdk_window_set_cursor(gui.drawarea->window, c);
5910 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5911 }
5912 if (shape != MSHAPE_HIDE)
5913 last_shape = shape;
5914}
5915#endif /* FEAT_MOUSESHAPE */
5916
5917
5918#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5919/*
5920 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5921 * scaled down if the current font is not big enough, or scaled up if the image
5922 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5923 * will be cut off if the current font is not big enough, or centered if it's
5924 * too small.
5925 */
5926# define SIGN_WIDTH (2 * gui.char_width)
5927# define SIGN_HEIGHT (gui.char_height)
5928# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 void
5931gui_mch_drawsign(int row, int col, int typenr)
5932{
5933 GdkPixbuf *sign;
5934
5935 sign = (GdkPixbuf *)sign_get_image(typenr);
5936
5937 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5938 {
5939 int width;
5940 int height;
5941 int xoffset;
5942 int yoffset;
5943 int need_scale;
5944
5945 width = gdk_pixbuf_get_width(sign);
5946 height = gdk_pixbuf_get_height(sign);
5947 /*
5948 * Decide whether we need to scale. Allow one pixel of border
5949 * width to be cut off, in order to avoid excessive scaling for
5950 * tiny differences in font size.
5951 */
5952 need_scale = (width > SIGN_WIDTH + 2
5953 || height > SIGN_HEIGHT + 2
5954 || (width < 3 * SIGN_WIDTH / 4
5955 && height < 3 * SIGN_HEIGHT / 4));
5956 if (need_scale)
5957 {
5958 double aspect;
5959
5960 /* Keep the original aspect ratio */
5961 aspect = (double)height / (double)width;
5962 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5963 width = MIN(width, SIGN_WIDTH);
5964 height = (double)width * aspect;
5965
5966 /* This doesn't seem to be worth caching, and doing so
5967 * would complicate the code quite a bit. */
5968 sign = gdk_pixbuf_scale_simple(sign, width, height,
5969 GDK_INTERP_BILINEAR);
5970 if (sign == NULL)
5971 return; /* out of memory */
5972 }
5973
5974 /* The origin is the upper-left corner of the pixmap. Therefore
5975 * these offset may become negative if the pixmap is smaller than
5976 * the 2x1 cells reserved for the sign icon. */
5977 xoffset = (width - SIGN_WIDTH) / 2;
5978 yoffset = (height - SIGN_HEIGHT) / 2;
5979
5980 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5981
5982 gdk_draw_rectangle(gui.drawarea->window,
5983 gui.text_gc,
5984 TRUE,
5985 FILL_X(col),
5986 FILL_Y(row),
5987 SIGN_WIDTH,
5988 SIGN_HEIGHT);
5989
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 gdk_pixbuf_render_to_drawable_alpha(sign,
5991 gui.drawarea->window,
5992 MAX(0, xoffset),
5993 MAX(0, yoffset),
5994 FILL_X(col) - MIN(0, xoffset),
5995 FILL_Y(row) - MIN(0, yoffset),
5996 MIN(width, SIGN_WIDTH),
5997 MIN(height, SIGN_HEIGHT),
5998 GDK_PIXBUF_ALPHA_BILEVEL,
5999 127,
6000 GDK_RGB_DITHER_NORMAL,
6001 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (need_scale)
6003 g_object_unref(sign);
6004 }
6005}
6006
6007 void *
6008gui_mch_register_sign(char_u *signfile)
6009{
6010 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6011 {
6012 GdkPixbuf *sign;
6013 GError *error = NULL;
6014 char_u *message;
6015
6016 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6017
6018 if (error == NULL)
6019 return sign;
6020
6021 message = (char_u *)error->message;
6022
6023 if (message != NULL && input_conv.vc_type != CONV_NONE)
6024 message = string_convert(&input_conv, message, NULL);
6025
6026 if (message != NULL)
6027 {
6028 /* The error message is already translated and will be more
6029 * descriptive than anything we could possibly do ourselves. */
6030 EMSG2("E255: %s", message);
6031
6032 if (input_conv.vc_type != CONV_NONE)
6033 vim_free(message);
6034 }
6035 g_error_free(error);
6036 }
6037
6038 return NULL;
6039}
6040
6041 void
6042gui_mch_destroy_sign(void *sign)
6043{
6044 if (sign != NULL)
6045 g_object_unref(sign);
6046}
6047
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#endif /* FEAT_SIGN_ICONS */