blob: 04b0ecb9d0b62b36940acde1b132553e4add0f27 [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,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200793 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000794 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;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001703 case GDK_SCROLL_LEFT:
1704 button = MOUSE_7;
1705 break;
1706 case GDK_SCROLL_RIGHT:
1707 button = MOUSE_6;
1708 break;
1709 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 return FALSE;
1711 }
1712
1713# ifdef FEAT_XIM
1714 /* cancel any preediting */
1715 if (im_is_preediting())
1716 xim_reset();
1717# endif
1718
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001719 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
1721 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1722 FALSE, vim_modifiers);
1723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 return TRUE;
1725}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726
1727
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001729button_release_event(GtkWidget *widget UNUSED,
1730 GdkEventButton *event,
1731 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732{
1733 int x, y;
1734 int_u vim_modifiers;
1735
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001736 clipboard_event_time = event->time;
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 /* Remove any motion "machine gun" timers used for automatic further
1739 extension of allocation areas if outside of the applications window
1740 area .*/
1741 if (motion_repeat_timer)
1742 {
1743 gtk_timeout_remove(motion_repeat_timer);
1744 motion_repeat_timer = 0;
1745 }
1746
1747 x = event->x;
1748 y = event->y;
1749
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001750 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753
1754 return TRUE;
1755}
1756
1757
1758#ifdef FEAT_DND
1759/****************************************************************************
1760 * Drag aNd Drop support handlers.
1761 */
1762
1763/*
1764 * Count how many items there may be and separate them with a NUL.
1765 * Apparently the items are separated with \r\n. This is not documented,
1766 * thus be careful not to go past the end. Also allow separation with
1767 * NUL characters.
1768 */
1769 static int
1770count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1771{
1772 int i;
1773 char_u *p = out;
1774 int count = 0;
1775
1776 for (i = 0; i < len; ++i)
1777 {
1778 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1779 {
1780 if (p > out && p[-1] != NUL)
1781 {
1782 ++count;
1783 *p++ = NUL;
1784 }
1785 }
1786 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1787 {
1788 *p++ = hexhex2nr(raw + i + 1);
1789 i += 2;
1790 }
1791 else
1792 *p++ = raw[i];
1793 }
1794 if (p > out && p[-1] != NUL)
1795 {
1796 *p = NUL; /* last item didn't have \r or \n */
1797 ++count;
1798 }
1799 return count;
1800}
1801
1802/*
1803 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1804 * this process, URI which protocol is not "file:" are removed. Return
1805 * length of array (less than "max").
1806 */
1807 static int
1808filter_uri_list(char_u **outlist, int max, char_u *src)
1809{
1810 int i, j;
1811
1812 for (i = j = 0; i < max; ++i)
1813 {
1814 outlist[i] = NULL;
1815 if (STRNCMP(src, "file:", 5) == 0)
1816 {
1817 src += 5;
1818 if (STRNCMP(src, "//localhost", 11) == 0)
1819 src += 11;
1820 while (src[0] == '/' && src[1] == '/')
1821 ++src;
1822 outlist[j++] = vim_strsave(src);
1823 }
1824 src += STRLEN(src) + 1;
1825 }
1826 return j;
1827}
1828
1829 static char_u **
1830parse_uri_list(int *count, char_u *data, int len)
1831{
1832 int n = 0;
1833 char_u *tmp = NULL;
1834 char_u **array = NULL;;
1835
1836 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1837 {
1838 n = count_and_decode_uri_list(tmp, data, len);
1839 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1840 n = filter_uri_list(array, n, tmp);
1841 }
1842 vim_free(tmp);
1843 *count = n;
1844 return array;
1845}
1846
1847 static void
1848drag_handle_uri_list(GdkDragContext *context,
1849 GtkSelectionData *data,
1850 guint time_,
1851 GdkModifierType state,
1852 gint x,
1853 gint y)
1854{
1855 char_u **fnames;
1856 int nfiles = 0;
1857
1858 fnames = parse_uri_list(&nfiles, data->data, data->length);
1859
1860 if (fnames != NULL && nfiles > 0)
1861 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001862 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
1864 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1865
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001866 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867
1868 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1869 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001870 else
1871 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872}
1873
1874 static void
1875drag_handle_text(GdkDragContext *context,
1876 GtkSelectionData *data,
1877 guint time_,
1878 GdkModifierType state)
1879{
1880 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1881 char_u *text;
1882 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 text = data->data;
1886 len = data->length;
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (data->type == utf8_string_atom)
1889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (input_conv.vc_type != CONV_NONE)
1891 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (tmpbuf != NULL)
1893 text = tmpbuf;
1894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 dnd_yank_drag_data(text, (long)len);
1897 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001900 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
1902 if (dropkey[2] != 0)
1903 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1904 else
1905 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906}
1907
1908/*
1909 * DND receiver.
1910 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 static void
1912drag_data_received_cb(GtkWidget *widget,
1913 GdkDragContext *context,
1914 gint x,
1915 gint y,
1916 GtkSelectionData *data,
1917 guint info,
1918 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001919 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920{
1921 GdkModifierType state;
1922
1923 /* Guard against trash */
1924 if (data->data == NULL
1925 || data->length <= 0
1926 || data->format != 8
1927 || data->data[data->length] != '\0')
1928 {
1929 gtk_drag_finish(context, FALSE, FALSE, time_);
1930 return;
1931 }
1932
1933 /* Get the current modifier state for proper distinguishment between
1934 * different operations later. */
1935 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1936
1937 /* Not sure about the role of "text/plain" here... */
1938 if (info == (guint)TARGET_TEXT_URI_LIST)
1939 drag_handle_uri_list(context, data, time_, state, x, y);
1940 else
1941 drag_handle_text(context, data, time_, state);
1942
1943}
1944#endif /* FEAT_DND */
1945
1946
1947#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1948/*
1949 * GnomeClient interact callback. Check for unsaved buffers that cannot
1950 * be abandoned and pop up a dialog asking the user for confirmation if
1951 * necessary.
1952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001954sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001956 GnomeDialogType type UNUSED,
1957 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958{
1959 cmdmod_T save_cmdmod;
1960 gboolean shutdown_cancelled;
1961
1962 save_cmdmod = cmdmod;
1963
1964# ifdef FEAT_BROWSE
1965 cmdmod.browse = TRUE;
1966# endif
1967# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1968 cmdmod.confirm = TRUE;
1969# endif
1970 /*
1971 * If there are changed buffers, present the user with
1972 * a dialog if possible, otherwise give an error message.
1973 */
1974 shutdown_cancelled = check_changed_any(FALSE);
1975
1976 exiting = FALSE;
1977 cmdmod = save_cmdmod;
1978 setcursor(); /* position the cursor */
1979 out_flush();
1980 /*
1981 * If the user hit the [Cancel] button the whole shutdown
1982 * will be cancelled. Wow, quite powerful feature (:
1983 */
1984 gnome_interaction_key_return(key, shutdown_cancelled);
1985}
1986
1987/*
1988 * Generate a script that can be used to restore the current editing session.
1989 * Save the value of v:this_session before running :mksession in order to make
1990 * automagic session save fully transparent. Return TRUE on success.
1991 */
1992 static int
1993write_session_file(char_u *filename)
1994{
1995 char_u *escaped_filename;
1996 char *mksession_cmdline;
1997 unsigned int save_ssop_flags;
1998 int failed;
1999
2000 /*
2001 * Build an ex command line to create a script that restores the current
2002 * session if executed. Escape the filename to avoid nasty surprises.
2003 */
2004 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2005 if (escaped_filename == NULL)
2006 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002007 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2008 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002010
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 /*
2012 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2013 * unpredictable effects when the session is saved automatically. Also,
2014 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2015 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2016 * enormously large if the GNOME session feature is used regularly.
2017 */
2018 save_ssop_flags = ssop_flags;
2019 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002020 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2023 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2024 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002025 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
2027 ssop_flags = save_ssop_flags;
2028 g_free(mksession_cmdline);
2029 /*
2030 * Reopen the file and append a command to restore v:this_session,
2031 * as if this save never happened. This is to avoid conflicts with
2032 * the user's own sessions. FIXME: It's probably less hackish to add
2033 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2034 */
2035 if (!failed)
2036 {
2037 FILE *fd;
2038
2039 fd = open_exfile(filename, TRUE, APPENDBIN);
2040
2041 failed = (fd == NULL
2042 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2043 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2044
2045 if (fd != NULL && fclose(fd) != 0)
2046 failed = TRUE;
2047
2048 if (failed)
2049 mch_remove(filename);
2050 }
2051
2052 return !failed;
2053}
2054
2055/*
2056 * "save_yourself" signal handler. Initiate an interaction to ask the user
2057 * for confirmation if necessary. Save the current editing session and tell
2058 * the session manager how to restart Vim.
2059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 static gboolean
2061sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002062 gint phase UNUSED,
2063 GnomeSaveStyle save_style UNUSED,
2064 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002066 gboolean fast UNUSED,
2067 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068{
2069 static const char suffix[] = "-session.vim";
2070 char *session_file;
2071 unsigned int len;
2072 gboolean success;
2073
2074 /* Always request an interaction if possible. check_changed_any()
2075 * won't actually show a dialog unless any buffers have been modified.
2076 * There doesn't seem to be an obvious way to check that without
2077 * automatically firing the dialog. Anyway, it works just fine. */
2078 if (interact_style == GNOME_INTERACT_ANY)
2079 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2080 &sm_client_check_changed_any,
2081 NULL);
2082 out_flush();
2083 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2084
2085 /* The path is unique for each session save. We do neither know nor care
2086 * which session script will actually be used later. This decision is in
2087 * the domain of the session manager. */
2088 session_file = gnome_config_get_real_path(
2089 gnome_client_get_config_prefix(client));
2090 len = strlen(session_file);
2091
2092 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2093 --len; /* get rid of the superfluous trailing '/' */
2094
2095 session_file = g_renew(char, session_file, len + sizeof(suffix));
2096 memcpy(session_file + len, suffix, sizeof(suffix));
2097
2098 success = write_session_file((char_u *)session_file);
2099
2100 if (success)
2101 {
2102 const char *argv[8];
2103 int i;
2104
2105 /* Tell the session manager how to wipe out the stored session data.
2106 * This isn't as dangerous as it looks, don't worry :) session_file
2107 * is a unique absolute filename. Usually it'll be something like
2108 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2109 i = 0;
2110 argv[i++] = "rm";
2111 argv[i++] = session_file;
2112 argv[i] = NULL;
2113
2114 gnome_client_set_discard_command(client, i, (char **)argv);
2115
2116 /* Tell the session manager how to restore the just saved session.
2117 * This is easily done thanks to Vim's -S option. Pass the -f flag
2118 * since there's no need to fork -- it might even cause confusion.
2119 * Also pass the window role to give the WM something to match on.
2120 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2121 i = 0;
2122 argv[i++] = restart_command;
2123 argv[i++] = "-f";
2124 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 argv[i++] = "--role";
2126 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 argv[i++] = "-S";
2128 argv[i++] = session_file;
2129 argv[i] = NULL;
2130
2131 gnome_client_set_restart_command(client, i, (char **)argv);
2132 gnome_client_set_clone_command(client, 0, NULL);
2133 }
2134
2135 g_free(session_file);
2136
2137 return success;
2138}
2139
2140/*
2141 * Called when the session manager wants us to die. There isn't much to save
2142 * here since "save_yourself" has been emitted before (unless serious trouble
2143 * is happening).
2144 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002146sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147{
2148 /* Don't write messages to the GUI anymore */
2149 full_screen = FALSE;
2150
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002151 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002152 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002153 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 preserve_exit();
2155}
2156
2157/*
2158 * Connect our signal handlers to be notified on session save and shutdown.
2159 */
2160 static void
2161setup_save_yourself(void)
2162{
2163 GnomeClient *client;
2164
2165 client = gnome_master_client();
2166
2167 if (client != NULL)
2168 {
2169 /* Must use the deprecated gtk_signal_connect() for compatibility
2170 * with GNOME 1. Arrgh, zombies! */
2171 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2172 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2173 gtk_signal_connect(GTK_OBJECT(client), "die",
2174 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2175 }
2176}
2177
2178#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2179
2180# ifdef USE_XSMP
2181/*
2182 * GTK tells us that XSMP needs attention
2183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 static gboolean
2185local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002186 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 GIOCondition condition;
2188 gpointer data;
2189{
2190 if (condition == G_IO_IN)
2191 {
2192 /* Do stuff; maybe close connection */
2193 if (xsmp_handle_requests() == FAIL)
2194 g_io_channel_unref((GIOChannel *)data);
2195 return TRUE;
2196 }
2197 /* Error */
2198 g_io_channel_unref((GIOChannel *)data);
2199 xsmp_close();
2200 return TRUE;
2201}
2202# endif /* USE_XSMP */
2203
2204/*
2205 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2206 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2207 */
2208 static void
2209setup_save_yourself(void)
2210{
2211 Atom *existing_atoms = NULL;
2212 int count = 0;
2213
2214#ifdef USE_XSMP
2215 if (xsmp_icefd != -1)
2216 {
2217 /*
2218 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2219 * set up GTK IO monitor
2220 */
2221 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2222
2223 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2224 local_xsmp_handle_requests, (gpointer)g_io);
2225 }
2226 else
2227#endif
2228 {
2229 /* Fall back to old method */
2230
2231 /* first get the existing value */
2232 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2233 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2234 &existing_atoms, &count))
2235 {
2236 Atom *new_atoms;
2237 Atom save_yourself_xatom;
2238 int i;
2239
2240 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2241
2242 /* check if WM_SAVE_YOURSELF isn't there yet */
2243 for (i = 0; i < count; ++i)
2244 if (existing_atoms[i] == save_yourself_xatom)
2245 break;
2246
2247 if (i == count)
2248 {
2249 /* allocate an Atoms array which is one item longer */
2250 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2251 * sizeof(Atom)));
2252 if (new_atoms != NULL)
2253 {
2254 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2255 new_atoms[count] = save_yourself_xatom;
2256 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2257 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2258 new_atoms, count + 1);
2259 vim_free(new_atoms);
2260 }
2261 }
2262 XFree(existing_atoms);
2263 }
2264 }
2265}
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267/*
2268 * Installing a global event filter seems to be the only way to catch
2269 * client messages of type WM_PROTOCOLS without overriding GDK's own
2270 * client message event filter. Well, that's still better than trying
2271 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 *
2273 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2274 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2275 * I have the nasty feeling modern session managers just don't send this
2276 * deprecated message anymore. Addition: confirmed by several people.
2277 *
2278 * The GNOME session support is much cooler anyway. Unlike this ugly
2279 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2280 * it should work with KDE as well.
2281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002283global_event_filter(GdkXEvent *xev,
2284 GdkEvent *event UNUSED,
2285 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286{
2287 XEvent *xevent = (XEvent *)xev;
2288
2289 if (xevent != NULL
2290 && xevent->type == ClientMessage
2291 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002292 && (long_u)xevent->xclient.data.l[0]
2293 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 out_flush();
2296 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2297 /*
2298 * Set the window's WM_COMMAND property, to let the window manager
2299 * know we are done saving ourselves. We don't want to be
2300 * restarted, thus set argv to NULL.
2301 */
2302 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2303 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2304 NULL, 0);
2305 return GDK_FILTER_REMOVE;
2306 }
2307
2308 return GDK_FILTER_CONTINUE;
2309}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2311
2312
2313/*
2314 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002317mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
2319/* If you get an error message here, you still need to unpack the runtime
2320 * archive! */
2321#ifdef magick
2322# undef magick
2323#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 /* A bit hackish, but avoids casting later and allows optimization */
2325# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326#define magick vim32x32
2327#include "../runtime/vim32x32.xpm"
2328#undef magick
2329#define magick vim16x16
2330#include "../runtime/vim16x16.xpm"
2331#undef magick
2332#define magick vim48x48
2333#include "../runtime/vim48x48.xpm"
2334#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /* When started with "--echo-wid" argument, write window ID on stdout. */
2338 if (echo_wid_arg)
2339 {
2340 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2341 fflush(stdout);
2342 }
2343
2344 if (vim_strchr(p_go, GO_ICON) != NULL)
2345 {
2346 /*
2347 * Add an icon to the main window. For fun and convenience of the user.
2348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 GList *icons = NULL;
2350
2351 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2352 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2353 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2354
2355 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2356
2357 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2358 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 }
2360
2361#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2362 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#endif
2365 /* Setup to indicate to the window manager that we want to catch the
2366 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2367 * manager instead. */
2368#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2369 if (using_gnome)
2370#endif
2371 setup_save_yourself();
2372
2373#ifdef FEAT_CLIENTSERVER
2374 if (serverName == NULL && serverDelayedStartName != NULL)
2375 {
2376 /* This is a :gui command in a plain vim with no previous server */
2377 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2378
2379 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2380 serverDelayedStartName);
2381 }
2382 else
2383 {
2384 /*
2385 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2386 * have to change the "server" registration to that of the main window
2387 * If we have not registered a name yet, remember the window
2388 */
2389 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2390 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2391 }
2392 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2393 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2394 GTK_SIGNAL_FUNC(property_event), NULL);
2395#endif
2396}
2397
2398 static GdkCursor *
2399create_blank_pointer(void)
2400{
2401 GdkWindow *root_window = NULL;
2402 GdkPixmap *blank_mask;
2403 GdkCursor *cursor;
2404 GdkColor color = { 0, 0, 0, 0 };
2405 char blank_data[] = { 0x0 };
2406
2407#ifdef HAVE_GTK_MULTIHEAD
2408 root_window = gtk_widget_get_root_window(gui.mainwin);
2409#endif
2410
2411 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2412 * in size. */
2413 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2414 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2415 &color, &color, 0, 0);
2416 gdk_bitmap_unref(blank_mask);
2417
2418 return cursor;
2419}
2420
2421#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 static void
2423mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002424 GdkScreen *previous_screen UNUSED,
2425 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426{
2427 if (!gtk_widget_has_screen(widget))
2428 return;
2429
2430 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002431 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 */
2433 if (gui.blank_pointer != NULL)
2434 gdk_cursor_unref(gui.blank_pointer);
2435
2436 gui.blank_pointer = create_blank_pointer();
2437
2438 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2439 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2440
2441 /*
2442 * Create a new PangoContext for this screen, and initialize it
2443 * with the current font if necessary.
2444 */
2445 if (gui.text_context != NULL)
2446 g_object_unref(gui.text_context);
2447
2448 gui.text_context = gtk_widget_create_pango_context(widget);
2449 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2450
2451 if (gui.norm_font != NULL)
2452 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002453 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002454 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456}
2457#endif /* HAVE_GTK_MULTIHEAD */
2458
2459/*
2460 * After the drawing area comes up, we calculate all colors and create the
2461 * dummy blank cursor.
2462 *
2463 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2464 * fact that the main VIM engine doesn't take them into account anywhere.
2465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002467drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 GtkWidget *sbar;
2470
2471#ifdef FEAT_XIM
2472 xim_init();
2473#endif
2474 gui_mch_new_colors();
2475 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2476
2477 gui.blank_pointer = create_blank_pointer();
2478 if (gui.pointer_hidden)
2479 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2480
2481 /* get the actual size of the scrollbars, if they are realized */
2482 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2483 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2484 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2485 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2486 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2487 gui.scrollbar_width = sbar->allocation.width;
2488
2489 sbar = gui.bottom_sbar.id;
2490 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2491 gui.scrollbar_height = sbar->allocation.height;
2492}
2493
2494/*
2495 * Properly clean up on shutdown.
2496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002498drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499{
2500 /* Don't write messages to the GUI anymore */
2501 full_screen = FALSE;
2502
2503#ifdef FEAT_XIM
2504 im_shutdown();
2505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 if (gui.ascii_glyphs != NULL)
2507 {
2508 pango_glyph_string_free(gui.ascii_glyphs);
2509 gui.ascii_glyphs = NULL;
2510 }
2511 if (gui.ascii_font != NULL)
2512 {
2513 g_object_unref(gui.ascii_font);
2514 gui.ascii_font = NULL;
2515 }
2516 g_object_unref(gui.text_context);
2517 gui.text_context = NULL;
2518
2519 g_object_unref(gui.text_gc);
2520 gui.text_gc = NULL;
2521
2522 gdk_cursor_unref(gui.blank_pointer);
2523 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524}
2525
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002527drawarea_style_set_cb(GtkWidget *widget UNUSED,
2528 GtkStyle *previous_style UNUSED,
2529 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 gui_mch_new_colors();
2532}
2533
2534/*
2535 * Callback routine for the "delete_event" signal on the toplevel window.
2536 * Tries to vim gracefully, or refuses to exit with changed buffers.
2537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002539delete_event_cb(GtkWidget *widget UNUSED,
2540 GdkEventAny *event UNUSED,
2541 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 gui_shell_closed();
2544 return TRUE;
2545}
2546
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002547#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2548 static int
2549get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2550{
2551 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2552
2553#ifdef FEAT_GUI_GNOME
2554 if (using_gnome && widget != NULL)
2555 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002556 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002557 BonoboDockItem *dockitem;
2558
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002559 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002560 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2561 {
2562 /* Only menu & toolbar are dock items. Could tabline be?
2563 * Seem to be only the 2 defined in GNOME */
2564 widget = parent;
2565 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002566
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002567 if (dockitem == NULL || dockitem->is_floating)
2568 return 0;
2569 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2570 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002571 }
2572#endif
2573 if (widget != NULL
2574 && item_orientation == orientation
2575 && GTK_WIDGET_REALIZED(widget)
2576 && GTK_WIDGET_VISIBLE(widget))
2577 {
2578 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2579 return widget->allocation.height;
2580 else
2581 return widget->allocation.width;
2582 }
2583 return 0;
2584}
2585#endif
2586
2587 static int
2588get_menu_tool_width(void)
2589{
2590 int width = 0;
2591
2592#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2593# ifdef FEAT_MENU
2594 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2595# endif
2596# ifdef FEAT_TOOLBAR
2597 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2598# endif
2599# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002600 if (gui.tabline != NULL)
2601 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002602# endif
2603#endif
2604
2605 return width;
2606}
2607
2608 static int
2609get_menu_tool_height(void)
2610{
2611 int height = 0;
2612
2613#ifdef FEAT_MENU
2614 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2615#endif
2616#ifdef FEAT_TOOLBAR
2617 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2618#endif
2619#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002620 if (gui.tabline != NULL)
2621 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002622#endif
2623
2624 return height;
2625}
2626
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002627/* This controls whether we can set the real window hints at
2628 * start-up when in a GtkPlug.
2629 * 0 = normal processing (default)
2630 * 1 = init. hints set, no-one's tried to reset since last check
2631 * 2 = init. hints set, attempt made to change hints
2632 */
2633static int init_window_hints_state = 0;
2634
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002635 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002636update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002637{
2638 static int old_width = 0;
2639 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002640 static int old_min_width = 0;
2641 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002642 static int old_char_width = 0;
2643 static int old_char_height = 0;
2644
2645 int width;
2646 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002647 int min_width;
2648 int min_height;
2649
2650 /* At start-up, don't try to set the hints until the initial
2651 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002652 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002653 */
2654 if (!(force_width && force_height) && init_window_hints_state > 0)
2655 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002656 /* Don't do it! */
2657 init_window_hints_state = 2;
2658 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002659 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002660
2661 /* This also needs to be done when the main window isn't there yet,
2662 * otherwise the hints don't work. */
2663 width = gui_get_base_width();
2664 height = gui_get_base_height();
2665# ifdef FEAT_MENU
2666 height += tabline_height() * gui.char_height;
2667# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002668 width += get_menu_tool_width();
2669 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002670
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002671 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002672 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002673 * 'set columns=' or init. -geom) we briefly set the min. to the size
2674 * we wish to be instead of the legitimate minimum so that we actually
2675 * resize correctly.
2676 */
2677 if (force_width && force_height)
2678 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002679 min_width = force_width;
2680 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002681 }
2682 else
2683 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002684 min_width = width + MIN_COLUMNS * gui.char_width;
2685 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002686 }
2687
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002688 /* Avoid an expose event when the size didn't change. */
2689 if (width != old_width
2690 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002691 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002692 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002693 || gui.char_width != old_char_width
2694 || gui.char_height != old_char_height)
2695 {
2696 GdkGeometry geometry;
2697 GdkWindowHints geometry_mask;
2698
2699 geometry.width_inc = gui.char_width;
2700 geometry.height_inc = gui.char_height;
2701 geometry.base_width = width;
2702 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002703 geometry.min_width = min_width;
2704 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002705 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2706 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002707 /* Using gui.formwin as geometry widget doesn't work as expected
2708 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2709 * in Vim confuse GTK+. */
2710 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2711 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002712 old_width = width;
2713 old_height = height;
2714 old_min_width = min_width;
2715 old_min_height = min_height;
2716 old_char_width = gui.char_width;
2717 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002718 }
2719}
2720
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721#ifdef FEAT_TOOLBAR
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723/*
2724 * This extra effort wouldn't be necessary if we only used stock icons in the
2725 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2726 * shouldn't be treated differently, thus we do need this.
2727 */
2728 static void
2729icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2730{
2731 if (GTK_IS_IMAGE(widget))
2732 {
2733 GtkImage *image = (GtkImage *)widget;
2734
2735 /* User-defined icons are stored in a GtkIconSet */
2736 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2737 {
2738 GtkIconSet *icon_set;
2739 GtkIconSize icon_size;
2740
2741 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2742 icon_size = (GtkIconSize)(long)user_data;
2743
2744 gtk_icon_set_ref(icon_set);
2745 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2746 gtk_icon_set_unref(icon_set);
2747 }
2748 }
2749 else if (GTK_IS_CONTAINER(widget))
2750 {
2751 gtk_container_foreach((GtkContainer *)widget,
2752 &icon_size_changed_foreach,
2753 user_data);
2754 }
2755}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 static void
2758set_toolbar_style(GtkToolbar *toolbar)
2759{
2760 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 GtkIconSize size;
2762 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2765 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2766 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002767 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2769 style = GTK_TOOLBAR_BOTH;
2770 else if (toolbar_flags & TOOLBAR_TEXT)
2771 style = GTK_TOOLBAR_TEXT;
2772 else
2773 style = GTK_TOOLBAR_ICONS;
2774
2775 gtk_toolbar_set_style(toolbar, style);
2776 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2777
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 switch (tbis_flags)
2779 {
2780 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2781 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2782 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2783 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2784 default: size = GTK_ICON_SIZE_INVALID; break;
2785 }
2786 oldsize = gtk_toolbar_get_icon_size(toolbar);
2787
2788 if (size == GTK_ICON_SIZE_INVALID)
2789 {
2790 /* Let global user preferences decide the icon size. */
2791 gtk_toolbar_unset_icon_size(toolbar);
2792 size = gtk_toolbar_get_icon_size(toolbar);
2793 }
2794 if (size != oldsize)
2795 {
2796 gtk_container_foreach(GTK_CONTAINER(toolbar),
2797 &icon_size_changed_foreach,
2798 GINT_TO_POINTER((int)size));
2799 }
2800 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801}
2802
2803#endif /* FEAT_TOOLBAR */
2804
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002805#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2806static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002807static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002808static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002809static int clicked_page; /* page clicked in tab line */
2810
2811/*
2812 * Handle selecting an item in the tab line popup menu.
2813 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002814 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002815tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002816{
Bram Moolenaara5621492006-02-25 21:55:24 +00002817 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002818 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002819}
2820
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002821 static void
2822add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2823{
2824 GtkWidget *item;
2825 char_u *utf_text;
2826
2827 utf_text = CONVERT_TO_UTF8(text);
2828 item = gtk_menu_item_new_with_label((const char *)utf_text);
2829 gtk_widget_show(item);
2830 CONVERT_TO_UTF8_FREE(utf_text);
2831
2832 gtk_container_add(GTK_CONTAINER(menu), item);
2833 gtk_signal_connect(GTK_OBJECT(item), "activate",
2834 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002835 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002836}
2837
Bram Moolenaara5621492006-02-25 21:55:24 +00002838/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002839 * Create a menu for the tab line.
2840 */
2841 static GtkWidget *
2842create_tabline_menu(void)
2843{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002844 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002845
2846 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002847 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2848 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2849 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002850
2851 return menu;
2852}
2853
2854 static gboolean
2855on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2856{
2857 /* Was this button press event ? */
2858 if (event->type == GDK_BUTTON_PRESS)
2859 {
2860 GdkEventButton *bevent = (GdkEventButton *)event;
2861 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002862 int y = bevent->y;
2863 GtkWidget *tabwidget;
2864 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002865
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002866 /* When ignoring events return TRUE so that the selected page doesn't
2867 * change. */
2868 if (hold_gui_events
2869# ifdef FEAT_CMDWIN
2870 || cmdwin_type != 0
2871# endif
2872 )
2873 return TRUE;
2874
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002875 tabwin = gdk_window_at_pointer(&x, &y);
2876 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2877 clicked_page = (int)(long)gtk_object_get_user_data(
2878 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002879
2880 /* If the event was generated for 3rd button popup the menu. */
2881 if (bevent->button == 3)
2882 {
2883 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2884 bevent->button, bevent->time);
2885 /* We handled the event. */
2886 return TRUE;
2887 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002888 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002889 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002890 if (clicked_page == 0)
2891 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002892 /* Click after all tabs moves to next tab page. When "x" is
2893 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002894 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002895 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002896 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002897 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002898
Bram Moolenaara5621492006-02-25 21:55:24 +00002899 /* We didn't handle the event. */
2900 return FALSE;
2901}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002902
2903/*
2904 * Handle selecting one of the tabs.
2905 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002906 static void
2907on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002908 GtkNotebook *notebook UNUSED,
2909 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002910 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002911 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002912{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002913 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002914 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002915 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002916 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002917}
2918
2919/*
2920 * Show or hide the tabline.
2921 */
2922 void
2923gui_mch_show_tabline(int showit)
2924{
2925 if (gui.tabline == NULL)
2926 return;
2927
2928 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2929 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002930 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002931 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002932 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002933 if (showit)
2934 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002935 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002936
2937 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002938}
2939
2940/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002941 * Return TRUE when tabline is displayed.
2942 */
2943 int
2944gui_mch_showing_tabline(void)
2945{
2946 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002947 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002948}
2949
2950/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002951 * Update the labels of the tabline.
2952 */
2953 void
2954gui_mch_update_tabline(void)
2955{
2956 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002957 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002958 GtkWidget *label;
2959 tabpage_T *tp;
2960 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002961 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002962 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002963 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002964
2965 if (gui.tabline == NULL)
2966 return;
2967
2968 ignore_tabline_evt = TRUE;
2969
2970 /* Add a label for each tab page. They all contain the same text area. */
2971 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2972 {
2973 if (tp == curtab)
2974 curtabidx = nr;
2975
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002976 tab_num = nr + 1;
2977
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002978 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
2979 if (page == NULL)
2980 {
2981 /* Add notebook page */
2982 page = gtk_vbox_new(FALSE, 0);
2983 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002984 event_box = gtk_event_box_new();
2985 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002986 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002987 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002988 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002989 gtk_widget_show(label);
2990 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
2991 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002992 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002993 nr++);
2994 }
2995
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002996 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002997 gtk_object_set_user_data(GTK_OBJECT(event_box),
2998 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002999 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003000 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003001 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003002 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003003 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003004
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003005 get_tabline_label(tp, TRUE);
3006 labeltext = CONVERT_TO_UTF8(NameBuff);
3007 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3008 (const char *)labeltext, NULL);
3009 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003010 }
3011
3012 /* Remove any old labels. */
3013 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3014 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3015
3016 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003017 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003018
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003019 /* Make sure everything is in place before drawing text. */
3020 gui_mch_update();
3021
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003022 ignore_tabline_evt = FALSE;
3023}
3024
3025/*
3026 * Set the current tab to "nr". First tab is 1.
3027 */
3028 void
3029gui_mch_set_curtab(nr)
3030 int nr;
3031{
3032 if (gui.tabline == NULL)
3033 return;
3034
3035 ignore_tabline_evt = TRUE;
3036 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003037 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003038 ignore_tabline_evt = FALSE;
3039}
3040
3041#endif /* FEAT_GUI_TABLINE */
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003044 * Add selection targets for PRIMARY and CLIPBOARD selections.
3045 */
3046 void
3047gui_gtk_set_selection_targets(void)
3048{
3049 int i, j = 0;
3050 int n_targets = N_SELECTION_TARGETS;
3051 GtkTargetEntry targets[N_SELECTION_TARGETS];
3052
3053 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3054 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003055 /* OpenOffice tries to use TARGET_HTML and fails when it doesn't
3056 * return something, instead of trying another target. Therefore only
3057 * offer TARGET_HTML when it works. */
3058 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3059 n_targets--;
3060 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003061 targets[j++] = selection_targets[i];
3062 }
3063
3064 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3065 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3066 gtk_selection_add_targets(gui.drawarea,
3067 (GdkAtom)GDK_SELECTION_PRIMARY,
3068 targets, n_targets);
3069 gtk_selection_add_targets(gui.drawarea,
3070 (GdkAtom)clip_plus.gtk_sel_atom,
3071 targets, n_targets);
3072}
3073
3074/*
3075 * Set up for receiving DND items.
3076 */
3077 void
3078gui_gtk_set_dnd_targets(void)
3079{
3080 int i, j = 0;
3081 int n_targets = N_DND_TARGETS;
3082 GtkTargetEntry targets[N_DND_TARGETS];
3083
3084 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3085 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003086 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3087 n_targets--;
3088 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003089 targets[j++] = dnd_targets[i];
3090 }
3091
3092 gtk_drag_dest_unset(gui.drawarea);
3093 gtk_drag_dest_set(gui.drawarea,
3094 GTK_DEST_DEFAULT_ALL,
3095 targets, n_targets,
3096 GDK_ACTION_COPY);
3097}
3098
3099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3101 * Returns OK for success, FAIL when the GUI can't be started.
3102 */
3103 int
3104gui_mch_init(void)
3105{
3106 GtkWidget *vbox;
3107
3108#ifdef FEAT_GUI_GNOME
3109 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3110 * exits on failure, but that's a non-issue because we already called
3111 * gtk_init_check() in gui_mch_init_check(). */
3112 if (using_gnome)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3114 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115#endif
3116 vim_free(gui_argv);
3117 gui_argv = NULL;
3118
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003119#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 /* Set the human-readable application name */
3121 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 /*
3124 * Force UTF-8 output no matter what the value of 'encoding' is.
3125 * did_set_string_option() in option.c prohibits changing 'termencoding'
3126 * to something else than UTF-8 if the GUI is in use.
3127 */
3128 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3129
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003130#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003132#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3134 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003135#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137#endif
3138
3139 /* Initialize values */
3140 gui.border_width = 2;
3141 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3142 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003143 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003145 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003147 /* LINTED: avoid warning: conversion to 'unsigned long' */
3148 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149
3150 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003151 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153
3154 /* Set default foreground and background colors. */
3155 gui.norm_pixel = gui.def_norm_pixel;
3156 gui.back_pixel = gui.def_back_pixel;
3157
3158 if (gtk_socket_id != 0)
3159 {
3160 GtkWidget *plug;
3161
3162 /* Use GtkSocket from another app. */
3163#ifdef HAVE_GTK_MULTIHEAD
3164 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3165 gtk_socket_id);
3166#else
3167 plug = gtk_plug_new(gtk_socket_id);
3168#endif
3169 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3170 {
3171 gui.mainwin = plug;
3172 }
3173 else
3174 {
3175 g_warning("Connection to GTK+ socket (ID %u) failed",
3176 (unsigned int)gtk_socket_id);
3177 /* Pretend we never wanted it if it failed (get own window) */
3178 gtk_socket_id = 0;
3179 }
3180 }
3181
3182 if (gtk_socket_id == 0)
3183 {
3184#ifdef FEAT_GUI_GNOME
3185 if (using_gnome)
3186 {
3187 gui.mainwin = gnome_app_new("Vim", NULL);
3188# ifdef USE_XSMP
3189 /* Use the GNOME save-yourself functionality now. */
3190 xsmp_close();
3191# endif
3192 }
3193 else
3194#endif
3195 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3196 }
3197
3198 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3199
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 /* Create the PangoContext used for drawing all text. */
3201 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3202 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3205 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3206
3207 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3208 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3209
3210 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3211 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3212#ifdef HAVE_GTK_MULTIHEAD
3213 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3214 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 gui.accel_group = gtk_accel_group_new();
3217 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003219 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 vbox = gtk_vbox_new(FALSE, 0);
3221
3222#ifdef FEAT_GUI_GNOME
3223 if (using_gnome)
3224 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003225# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /* automagically restore menubar/toolbar placement */
3227 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3228# endif
3229 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3230 }
3231 else
3232#endif
3233 {
3234 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3235 gtk_widget_show(vbox);
3236 }
3237
3238#ifdef FEAT_MENU
3239 /*
3240 * Create the menubar and handle
3241 */
3242 gui.menubar = gtk_menu_bar_new();
3243 gtk_widget_set_name(gui.menubar, "vim-menubar");
3244
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003245 /* Avoid that GTK takes <F10> away from us. */
3246 {
3247 GtkSettings *gtk_settings;
3248
3249 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3250 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3251 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003252
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254# ifdef FEAT_GUI_GNOME
3255 if (using_gnome)
3256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 BonoboDockItem *dockitem;
3258
3259 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3260 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3261 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003262 /* We don't want the menu to float. */
3263 bonobo_dock_item_set_behavior(dockitem,
3264 bonobo_dock_item_get_behavior(dockitem)
3265 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 else
3269# endif /* FEAT_GUI_GNOME */
3270 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003271 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3272 * disabled in gui_init() later. */
3273 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3275 }
3276#endif /* FEAT_MENU */
3277
3278#ifdef FEAT_TOOLBAR
3279 /*
3280 * Create the toolbar and handle
3281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 /* some aesthetics on the toolbar */
3283 gtk_rc_parse_string(
3284 "style \"vim-toolbar-style\" {\n"
3285 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3286 "}\n"
3287 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3288 gui.toolbar = gtk_toolbar_new();
3289 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3291
3292# ifdef FEAT_GUI_GNOME
3293 if (using_gnome)
3294 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 BonoboDockItem *dockitem;
3296
3297 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3298 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3299 GNOME_APP_TOOLBAR_NAME);
3300 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003301 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003302 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003303 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003304 bonobo_dock_item_get_behavior(dockitem)
3305 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else
3309# endif /* FEAT_GUI_GNOME */
3310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3312 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3313 gtk_widget_show(gui.toolbar);
3314 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3315 }
3316#endif /* FEAT_TOOLBAR */
3317
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003318#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003319 /*
3320 * Use a Notebook for the tab pages labels. The labels are hidden by
3321 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003322 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003323 gui.tabline = gtk_notebook_new();
3324 gtk_widget_show(gui.tabline);
3325 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3326 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3327 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003328 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003329 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003330
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003331 tabline_tooltip = gtk_tooltips_new();
3332 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3333
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003334 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003335 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003336
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003337 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003338 page = gtk_vbox_new(FALSE, 0);
3339 gtk_widget_show(page);
3340 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3341 label = gtk_label_new("-Empty-");
3342 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003343 event_box = gtk_event_box_new();
3344 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003345 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003346 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003347 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003348 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003349 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003350
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003351 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003352 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003353
3354 /* Create a popup menu for the tab line and connect it. */
3355 tabline_menu = create_tabline_menu();
3356 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003357 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358#endif
3359
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 gui.formwin = gtk_form_new();
3361 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3362 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3363
3364 gui.drawarea = gtk_drawing_area_new();
3365
3366 /* Determine which events we will filter. */
3367 gtk_widget_set_events(gui.drawarea,
3368 GDK_EXPOSURE_MASK |
3369 GDK_ENTER_NOTIFY_MASK |
3370 GDK_LEAVE_NOTIFY_MASK |
3371 GDK_BUTTON_PRESS_MASK |
3372 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 GDK_KEY_PRESS_MASK |
3375 GDK_KEY_RELEASE_MASK |
3376 GDK_POINTER_MOTION_MASK |
3377 GDK_POINTER_MOTION_HINT_MASK);
3378
3379 gtk_widget_show(gui.drawarea);
3380 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3381 gtk_widget_show(gui.formwin);
3382 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3383
3384 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3385 * and not the window. */
3386 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3387 : GTK_OBJECT(gui.drawarea),
3388 "key_press_event",
3389 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003390#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 /* Also forward key release events for the benefit of GTK+ 2 input
3392 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3393 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3394 : G_OBJECT(gui.drawarea),
3395 "key_release_event",
3396 G_CALLBACK(&key_release_event), NULL);
3397#endif
3398 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3399 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3400 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3401 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3402
3403 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3404 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3405
3406 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3407
3408#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3409 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3410 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3411#endif
3412
3413 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003414 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3416
3417 /*
3418 * Set clipboard specific atoms
3419 */
3420 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3423 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3424
3425 /*
3426 * Start out by adding the configured border width into the border offset.
3427 */
3428 gui.border_offset = gui.border_width;
3429
3430 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3431 GTK_SIGNAL_FUNC(visibility_event), NULL);
3432 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3433 GTK_SIGNAL_FUNC(expose_event), NULL);
3434
3435 /*
3436 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3437 * Only needed for some window managers.
3438 */
3439 if (vim_strchr(p_go, GO_POINTER) != NULL)
3440 {
3441 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3442 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3443 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3444 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3445 }
3446
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003447 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3448 * only its widgets. Arguably, this could be common code and we not use
3449 * the window focus at all, but let's be safe.
3450 */
3451 if (gtk_socket_id == 0)
3452 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003453 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3454 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3455 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3456 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003457 }
3458 else
3459 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003460 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3461 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3462 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3463 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003464#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003465 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3466 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3467 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3468 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003469#endif /* FEAT_GUI_TABLINE */
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
3472 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3473 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3474 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3475 GTK_SIGNAL_FUNC(button_press_event), NULL);
3476 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3477 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3479 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
3481 /*
3482 * Add selection handler functions.
3483 */
3484 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3485 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3486 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3487 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3488
Bram Moolenaara76638f2010-06-05 12:49:46 +02003489 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3492 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3493
3494 /* Pretend we don't have input focus, we will get an event if we do. */
3495 gui.in_focus = FALSE;
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 return OK;
3498}
3499
3500#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3501/*
3502 * This is called from gui_start() after a fork() has been done.
3503 * We have to tell the session manager our new PID.
3504 */
3505 void
3506gui_mch_forked(void)
3507{
3508 if (using_gnome)
3509 {
3510 GnomeClient *client;
3511
3512 client = gnome_master_client();
3513
3514 if (client != NULL)
3515 gnome_client_set_process_id(client, getpid());
3516 }
3517}
3518#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3519
3520/*
3521 * Called when the foreground or background color has been changed.
3522 * This used to change the graphics contexts directly but we are
3523 * currently manipulating them where desired.
3524 */
3525 void
3526gui_mch_new_colors(void)
3527{
3528 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3529 {
3530 GdkColor color = { 0, 0, 0, 0 };
3531
3532 color.pixel = gui.back_pixel;
3533 gdk_window_set_background(gui.drawarea->window, &color);
3534 }
3535}
3536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537/*
3538 * This signal informs us about the need to rearrange our sub-widgets.
3539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003541form_configure_event(GtkWidget *widget UNUSED,
3542 GdkEventConfigure *event,
3543 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003545 int usable_height = event->height;
3546
3547 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3548 * no. of char-heights), so we have to manually sanitise them.
3549 * Widths seem to sort themselves out, don't ask me why.
3550 */
3551 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003552 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003555 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 gtk_form_thaw(GTK_FORM(gui.formwin));
3557
3558 return TRUE;
3559}
3560
3561/*
3562 * Function called when window already closed.
3563 * We can't do much more here than to trying to preserve what had been done,
3564 * since the window is already inevitably going away.
3565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003567mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
3569 /* Don't write messages to the GUI anymore */
3570 full_screen = FALSE;
3571
3572 gui.mainwin = NULL;
3573 gui.drawarea = NULL;
3574
3575 if (!exiting) /* only do anything if the destroy was unexpected */
3576 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003577 vim_strncpy(IObuff,
3578 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3579 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 preserve_exit();
3581 }
3582}
3583
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003584
3585/*
3586 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3587 * hints (and thus the required size from -geom), but that after that we
3588 * put the hints back to normal (the actual minimum size) so we may
3589 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003590 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003591 * only way we have of making ourselves bigger (by set lines/columns).
3592 * Thus set hints at start-up to ensure correct init. size, then a
3593 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003594 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003595 * We'll not let the default hints be set while this timer's active.
3596 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003597 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003598check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003599{
3600 if (init_window_hints_state == 1)
3601 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003602 /* Safe to use normal hints now */
3603 init_window_hints_state = 0;
3604 update_window_manager_hints(0, 0);
3605 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003606 }
3607
3608 /* Keep on trying */
3609 init_window_hints_state = 1;
3610 return TRUE;
3611}
3612
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613/*
3614 * Open the GUI window which was created by a call to gui_mch_init().
3615 */
3616 int
3617gui_mch_open(void)
3618{
3619 guicolor_T fg_pixel = INVALCOLOR;
3620 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003621 guint pixel_width;
3622 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 /*
3625 * Allow setting a window role on the command line, or invent one
3626 * if none was specified. This is mainly useful for GNOME session
3627 * support; allowing the WM to restore window placement.
3628 */
3629 if (role_argument != NULL)
3630 {
3631 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3632 }
3633 else
3634 {
3635 char *role;
3636
3637 /* Invent a unique-enough ID string for the role */
3638 role = g_strdup_printf("vim-%u-%u-%u",
3639 (unsigned)mch_get_pid(),
3640 (unsigned)g_random_int(),
3641 (unsigned)time(NULL));
3642
3643 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3644 g_free(role);
3645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646
3647 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /* Determine user specified geometry, if present. */
3651 if (gui.geom != NULL)
3652 {
3653 int mask;
3654 unsigned int w, h;
3655 int x = 0;
3656 int y = 0;
3657
3658 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3659
3660 if (mask & WidthValue)
3661 Columns = w;
3662 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003663 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003664 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003665 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003667 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003668
3669 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3670 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3671
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003672 pixel_width += get_menu_tool_width();
3673 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003676 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003677 int ww, hh;
3678 gui_mch_get_screen_dimensions(&ww, &hh);
3679 hh += p_ghr + get_menu_tool_height();
3680 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003681 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003682 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003683 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003684 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 vim_free(gui.geom);
3688 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003689
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003690 /* From now until everyone's stopped trying to set the window hints
3691 * to their correct minimum values, stop them being set as we need
3692 * them to remain at our required size for the parent GtkSocket to
3693 * give us the right initial size.
3694 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003695 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003696 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003697 update_window_manager_hints(pixel_width, pixel_height);
3698 init_window_hints_state = 1;
3699 g_timeout_add(1000, check_startup_plug_hints, NULL);
3700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003703 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3704 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003705 /* For GTK2 changing the size of the form widget doesn't cause window
3706 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003707 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003708 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003709 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710
3711 if (foreground_argument != NULL)
3712 fg_pixel = gui_get_color((char_u *)foreground_argument);
3713 if (fg_pixel == INVALCOLOR)
3714 fg_pixel = gui_get_color((char_u *)"Black");
3715
3716 if (background_argument != NULL)
3717 bg_pixel = gui_get_color((char_u *)background_argument);
3718 if (bg_pixel == INVALCOLOR)
3719 bg_pixel = gui_get_color((char_u *)"White");
3720
3721 if (found_reverse_arg)
3722 {
3723 gui.def_norm_pixel = bg_pixel;
3724 gui.def_back_pixel = fg_pixel;
3725 }
3726 else
3727 {
3728 gui.def_norm_pixel = fg_pixel;
3729 gui.def_back_pixel = bg_pixel;
3730 }
3731
3732 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3733 * in a vimrc file) */
3734 set_normal_colors();
3735
3736 /* Check that none of the colors are the same as the background color */
3737 gui_check_colors();
3738
3739 /* Get the colors for the highlight groups (gui_check_colors() might have
3740 * changed them). */
3741 highlight_gui_started(); /* re-init colors and fonts */
3742
3743 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3744 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3745
3746#ifdef FEAT_HANGULIN
3747 hangul_keyboard_set();
3748#endif
3749
3750 /*
3751 * Notify the fixed area about the need to resize the contents of the
3752 * gui.formwin, which we use for random positioning of the included
3753 * components.
3754 *
3755 * We connect this signal deferred finally after anything is in place,
3756 * since this is intended to handle resizements coming from the window
3757 * manager upon us and should not interfere with what VIM is requesting
3758 * upon startup.
3759 */
3760 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3761 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3762
3763#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003764 /* Set up for receiving DND items. */
3765 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3768 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3769#endif
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003772 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (found_iconic_arg && gtk_socket_id == 0)
3774 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003777#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 unsigned long menu_handler = 0;
3779# ifdef FEAT_TOOLBAR
3780 unsigned long tool_handler = 0;
3781# endif
3782 /*
3783 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3784 * show when restoring the saved layout configuration. We can't just
3785 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3786 * a toplevel window and thus will be realized immediately. Instead,
3787 * connect signal handlers to hide the widgets just after they've been
3788 * marked visible, but before the main window is realized.
3789 */
3790 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3791 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3792 G_CALLBACK(&gtk_widget_hide),
3793 NULL);
3794# ifdef FEAT_TOOLBAR
3795 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3796 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3797 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3798 G_CALLBACK(&gtk_widget_hide),
3799 NULL);
3800# endif
3801#endif
3802 gtk_widget_show(gui.mainwin);
3803
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003804#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 if (menu_handler != 0)
3806 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3807# ifdef FEAT_TOOLBAR
3808 if (tool_handler != 0)
3809 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3810# endif
3811#endif
3812 }
3813
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 return OK;
3815}
3816
3817
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003819gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820{
3821 if (gui.mainwin != NULL)
3822 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824
3825/*
3826 * Get the position of the top left corner of the window.
3827 */
3828 int
3829gui_mch_get_winpos(int *x, int *y)
3830{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 return OK;
3833}
3834
3835/*
3836 * Set the position of the top left corner of the window to the given
3837 * coordinates.
3838 */
3839 void
3840gui_mch_set_winpos(int x, int y)
3841{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843}
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845#if 0
3846static int resize_idle_installed = FALSE;
3847/*
3848 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3849 * the shell size doesn't exceed the window size, i.e. if the window manager
3850 * ignored our size request. Usually this happens if the window is maximized.
3851 *
3852 * FIXME: It'd be nice if we could find a little more orthodox solution.
3853 * See also the remark below in gui_mch_set_shellsize().
3854 *
3855 * DISABLED: When doing ":set lines+=1" this function would first invoke
3856 * gui_resize_shell() with the old size, then the normal callback would
3857 * report the new size through form_configure_event(). That caused the window
3858 * layout to be messed up.
3859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 static gboolean
3861force_shell_resize_idle(gpointer data)
3862{
3863 if (gui.mainwin != NULL
3864 && GTK_WIDGET_REALIZED(gui.mainwin)
3865 && GTK_WIDGET_VISIBLE(gui.mainwin))
3866 {
3867 int width;
3868 int height;
3869
3870 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3871
3872 width -= get_menu_tool_width();
3873 height -= get_menu_tool_height();
3874
3875 gui_resize_shell(width, height);
3876 }
3877
3878 resize_idle_installed = FALSE;
3879 return FALSE; /* don't call me again */
3880}
3881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
Bram Moolenaar09736232009-09-23 16:14:49 +00003883/*
3884 * Return TRUE if the main window is maximized.
3885 */
3886 int
3887gui_mch_maximized()
3888{
3889 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3890 && (gdk_window_get_state(gui.mainwin->window)
3891 & GDK_WINDOW_STATE_MAXIMIZED));
3892}
3893
3894/*
3895 * Unmaximize the main window
3896 */
3897 void
3898gui_mch_unmaximize()
3899{
3900 if (gui.mainwin != NULL)
3901 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3902}
Bram Moolenaar09736232009-09-23 16:14:49 +00003903
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904/*
3905 * Set the windows size.
3906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 void
3908gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003909 int min_width UNUSED, int min_height UNUSED,
3910 int base_width UNUSED, int base_height UNUSED,
3911 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 /* give GTK+ a chance to put all widget's into place */
3914 gui_mch_update();
3915
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003916 /* this will cause the proper resizement to happen too */
3917 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003918 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003921 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 * main window instead. */
3923 width += get_menu_tool_width();
3924 height += get_menu_tool_height();
3925
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003926 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003927 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003928 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003929 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003931# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (!resize_idle_installed)
3933 {
3934 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3935 &force_shell_resize_idle, NULL, NULL);
3936 resize_idle_installed = TRUE;
3937 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003938# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 /*
3940 * Wait until all events are processed to prevent a crash because the
3941 * real size of the drawing area doesn't reflect Vim's internal ideas.
3942 *
3943 * This is a bit of a hack, since Vim is a terminal application with a GUI
3944 * on top, while the GUI expects to be the boss.
3945 */
3946 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947}
3948
3949
3950/*
3951 * The screen size is used to make sure the initial window doesn't get bigger
3952 * than the screen. This subtracts some room for menubar, toolbar and window
3953 * decorations.
3954 */
3955 void
3956gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
3957{
3958#ifdef HAVE_GTK_MULTIHEAD
3959 GdkScreen* screen;
3960
3961 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
3962 screen = gtk_widget_get_screen(gui.mainwin);
3963 else
3964 screen = gdk_screen_get_default();
3965
3966 *screen_w = gdk_screen_get_width(screen);
3967 *screen_h = gdk_screen_get_height(screen) - p_ghr;
3968#else
3969 *screen_w = gdk_screen_width();
3970 /* Subtract 'guiheadroom' from the height to allow some room for the
3971 * window manager (task list and window title bar). */
3972 *screen_h = gdk_screen_height() - p_ghr;
3973#endif
3974
3975 /*
3976 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
3977 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02003978 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 * This should be completely changed later.
3980 */
3981 *screen_w -= get_menu_tool_width();
3982 *screen_h -= get_menu_tool_height();
3983}
3984
3985#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003987gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 if (title != NULL && output_conv.vc_type != CONV_NONE)
3990 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
3993
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 if (output_conv.vc_type != CONV_NONE)
3995 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997#endif /* FEAT_TITLE */
3998
3999#if defined(FEAT_MENU) || defined(PROTO)
4000 void
4001gui_mch_enable_menu(int showit)
4002{
4003 GtkWidget *widget;
4004
4005# ifdef FEAT_GUI_GNOME
4006 if (using_gnome)
4007 widget = gui.menubar_h;
4008 else
4009# endif
4010 widget = gui.menubar;
4011
Bram Moolenaar18144c82006-04-12 21:52:12 +00004012 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4013 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 {
4015 if (showit)
4016 gtk_widget_show(widget);
4017 else
4018 gtk_widget_hide(widget);
4019
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004020 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022}
4023#endif /* FEAT_MENU */
4024
4025#if defined(FEAT_TOOLBAR) || defined(PROTO)
4026 void
4027gui_mch_show_toolbar(int showit)
4028{
4029 GtkWidget *widget;
4030
4031 if (gui.toolbar == NULL)
4032 return;
4033
4034# ifdef FEAT_GUI_GNOME
4035 if (using_gnome)
4036 widget = gui.toolbar_h;
4037 else
4038# endif
4039 widget = gui.toolbar;
4040
4041 if (showit)
4042 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4043
4044 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4045 {
4046 if (showit)
4047 gtk_widget_show(widget);
4048 else
4049 gtk_widget_hide(widget);
4050
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004051 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053}
4054#endif /* FEAT_TOOLBAR */
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056/*
4057 * Check if a given font is a CJK font. This is done in a very crude manner. It
4058 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4059 * font. Consequently, this function cannot be used as a general purpose check
4060 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4061 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4062 */
4063 static int
4064is_cjk_font(PangoFontDescription *font_desc)
4065{
4066 static const char * const cjk_langs[] =
4067 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4068
4069 PangoFont *font;
4070 unsigned i;
4071 int is_cjk = FALSE;
4072
4073 font = pango_context_load_font(gui.text_context, font_desc);
4074
4075 if (font == NULL)
4076 return FALSE;
4077
4078 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4079 {
4080 PangoCoverage *coverage;
4081 gunichar uc;
4082
4083 coverage = pango_font_get_coverage(
4084 font, pango_language_from_string(cjk_langs[i]));
4085
4086 if (coverage != NULL)
4087 {
4088 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4089 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4090 pango_coverage_unref(coverage);
4091 }
4092 }
4093
4094 g_object_unref(font);
4095
4096 return is_cjk;
4097}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar231334e2005-07-25 20:46:57 +00004099/*
4100 * Adjust gui.char_height (after 'linespace' was changed).
4101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004103gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 PangoFontMetrics *metrics;
4106 int ascent;
4107 int descent;
4108
4109 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4110 pango_context_get_language(gui.text_context));
4111 ascent = pango_font_metrics_get_ascent(metrics);
4112 descent = pango_font_metrics_get_descent(metrics);
4113
4114 pango_font_metrics_unref(metrics);
4115
4116 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004117 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004118 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 /* A not-positive value of char_height may crash Vim. Only happens
4122 * if 'linespace' is negative (which does make sense sometimes). */
4123 gui.char_ascent = MAX(gui.char_ascent, 0);
4124 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4125
4126 return OK;
4127}
4128
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129/*
4130 * Put up a font dialog and return the selected font name in allocated memory.
4131 * "oldval" is the previous value. Return NULL when cancelled.
4132 * This should probably go into gui_gtk.c. Hmm.
4133 * FIXME:
4134 * The GTK2 font selection dialog has no filtering API. So we could either
4135 * a) implement our own (possibly copying the code from somewhere else) or
4136 * b) just live with it.
4137 */
4138 char_u *
4139gui_mch_font_dialog(char_u *oldval)
4140{
4141 GtkWidget *dialog;
4142 int response;
4143 char_u *fontname = NULL;
4144 char_u *oldname;
4145
4146 dialog = gtk_font_selection_dialog_new(NULL);
4147
4148 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4149 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4150
4151 if (oldval != NULL && oldval[0] != NUL)
4152 {
4153 if (output_conv.vc_type != CONV_NONE)
4154 oldname = string_convert(&output_conv, oldval, NULL);
4155 else
4156 oldname = oldval;
4157
4158 /* Annoying bug in GTK (or Pango): if the font name does not include a
4159 * size, zero is used. Use default point size ten. */
4160 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4161 {
4162 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4163
4164 if (p != NULL)
4165 {
4166 STRCPY(p + STRLEN(p), " 10");
4167 if (oldname != oldval)
4168 vim_free(oldname);
4169 oldname = p;
4170 }
4171 }
4172
4173 gtk_font_selection_dialog_set_font_name(
4174 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4175
4176 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004177 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004179 else
4180 gtk_font_selection_dialog_set_font_name(
4181 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 response = gtk_dialog_run(GTK_DIALOG(dialog));
4184
4185 if (response == GTK_RESPONSE_OK)
4186 {
4187 char *name;
4188
4189 name = gtk_font_selection_dialog_get_font_name(
4190 GTK_FONT_SELECTION_DIALOG(dialog));
4191 if (name != NULL)
4192 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004193 char_u *p;
4194
4195 /* Apparently some font names include a comma, need to escape
4196 * that, because in 'guifont' it separates names. */
4197 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004199 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004200 {
4201 fontname = string_convert(&input_conv, p, NULL);
4202 vim_free(p);
4203 }
4204 else
4205 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207 }
4208
4209 if (response != GTK_RESPONSE_NONE)
4210 gtk_widget_destroy(dialog);
4211
4212 return fontname;
4213}
4214
4215/*
4216 * Some monospace fonts don't support a bold weight, and fall back
4217 * silently to the regular weight. But this is no good since our text
4218 * drawing function can emulate bold by overstriking. So let's try
4219 * to detect whether bold weight is actually available and emulate it
4220 * otherwise.
4221 *
4222 * Note that we don't need to check for italic style since Xft can
4223 * emulate italic on its own, provided you have a proper fontconfig
4224 * setup. We wouldn't be able to emulate it in Vim anyway.
4225 */
4226 static void
4227get_styled_font_variants(void)
4228{
4229 PangoFontDescription *bold_font_desc;
4230 PangoFont *plain_font;
4231 PangoFont *bold_font;
4232
4233 gui.font_can_bold = FALSE;
4234
4235 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4236
4237 if (plain_font == NULL)
4238 return;
4239
4240 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4241 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4242
4243 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4244 /*
4245 * The comparison relies on the unique handle nature of a PangoFont*,
4246 * i.e. it's assumed that a different PangoFont* won't refer to the
4247 * same font. Seems to work, and failing here isn't critical anyway.
4248 */
4249 if (bold_font != NULL)
4250 {
4251 gui.font_can_bold = (bold_font != plain_font);
4252 g_object_unref(bold_font);
4253 }
4254
4255 pango_font_description_free(bold_font_desc);
4256 g_object_unref(plain_font);
4257}
4258
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259static PangoEngineShape *default_shape_engine = NULL;
4260
4261/*
4262 * Create a map from ASCII characters in the range [32,126] to glyphs
4263 * of the current font. This is used by gui_gtk2_draw_string() to skip
4264 * the itemize and shaping process for the most common case.
4265 */
4266 static void
4267ascii_glyph_table_init(void)
4268{
4269 char_u ascii_chars[128];
4270 PangoAttrList *attr_list;
4271 GList *item_list;
4272 int i;
4273
4274 if (gui.ascii_glyphs != NULL)
4275 pango_glyph_string_free(gui.ascii_glyphs);
4276 if (gui.ascii_font != NULL)
4277 g_object_unref(gui.ascii_font);
4278
4279 gui.ascii_glyphs = NULL;
4280 gui.ascii_font = NULL;
4281
4282 /* For safety, fill in question marks for the control characters. */
4283 for (i = 0; i < 32; ++i)
4284 ascii_chars[i] = '?';
4285 for (; i < 127; ++i)
4286 ascii_chars[i] = i;
4287 ascii_chars[i] = '?';
4288
4289 attr_list = pango_attr_list_new();
4290 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4291 0, sizeof(ascii_chars), attr_list, NULL);
4292
4293 if (item_list != NULL && item_list->next == NULL) /* play safe */
4294 {
4295 PangoItem *item;
4296 int width;
4297
4298 item = (PangoItem *)item_list->data;
4299 width = gui.char_width * PANGO_SCALE;
4300
4301 /* Remember the shape engine used for ASCII. */
4302 default_shape_engine = item->analysis.shape_engine;
4303
4304 gui.ascii_font = item->analysis.font;
4305 g_object_ref(gui.ascii_font);
4306
4307 gui.ascii_glyphs = pango_glyph_string_new();
4308
4309 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4310 &item->analysis, gui.ascii_glyphs);
4311
4312 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4313
4314 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4315 {
4316 PangoGlyphGeometry *geom;
4317
4318 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4319 geom->x_offset += MAX(0, width - geom->width) / 2;
4320 geom->width = width;
4321 }
4322 }
4323
4324 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4325 g_list_free(item_list);
4326 pango_attr_list_unref(attr_list);
4327}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
4329/*
4330 * Initialize Vim to use the font or fontset with the given name.
4331 * Return FAIL if the font could not be loaded, OK otherwise.
4332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004334gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 PangoFontDescription *font_desc;
4337 PangoLayout *layout;
4338 int width;
4339
4340 /* If font_name is NULL, this means to use the default, which should
4341 * be present on all proper Pango/fontconfig installations. */
4342 if (font_name == NULL)
4343 font_name = (char_u *)DEFAULT_FONT;
4344
4345 font_desc = gui_mch_get_font(font_name, FALSE);
4346
4347 if (font_desc == NULL)
4348 return FAIL;
4349
4350 gui_mch_free_font(gui.norm_font);
4351 gui.norm_font = font_desc;
4352
4353 pango_context_set_font_description(gui.text_context, font_desc);
4354
4355 layout = pango_layout_new(gui.text_context);
4356 pango_layout_set_text(layout, "MW", 2);
4357 pango_layout_get_size(layout, &width, NULL);
4358 /*
4359 * Set char_width to half the width obtained from pango_layout_get_size()
4360 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4361 * Pango to use the same width for both non-CJK characters (e.g. Latin
4362 * letters and numbers) and CJK characters. This results in 's p a c e d
4363 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4364 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4365 * width for CJK fonts.
4366 *
4367 * For related bugs, see:
4368 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4369 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4370 *
4371 * With this, for all four of the following cases, Vim works fine:
4372 * guifont=CJK_fixed_width_font
4373 * guifont=Non_CJK_fixed_font
4374 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4375 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4376 */
4377 if (is_cjk_font(gui.norm_font))
4378 {
4379 int cjk_width;
4380
4381 /* Measure the text extent of U+4E00 and U+4E8C */
4382 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4383 pango_layout_get_size(layout, &cjk_width, NULL);
4384
4385 if (width == cjk_width) /* Xft not patched */
4386 width /= 2;
4387 }
4388 g_object_unref(layout);
4389
4390 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4391
4392 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4393 if (gui.char_width <= 0)
4394 gui.char_width = 8;
4395
Bram Moolenaar231334e2005-07-25 20:46:57 +00004396 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397
4398 /* Set the fontname, which will be used for information purposes */
4399 hl_set_font_name(font_name);
4400
4401 get_styled_font_variants();
4402 ascii_glyph_table_init();
4403
4404 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4405 if (gui.wide_font != NULL
4406 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4407 {
4408 pango_font_description_free(gui.wide_font);
4409 gui.wide_font = NULL;
4410 }
4411
Bram Moolenaare161c792009-11-03 17:13:59 +00004412 if (gui_mch_maximized())
4413 {
4414 int w, h;
4415
4416 /* Update lines and columns in accordance with the new font, keep the
4417 * window maximized. */
4418 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4419 w -= get_menu_tool_width();
4420 h -= get_menu_tool_height();
4421 gui_resize_shell(w, h);
4422 }
4423 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004424 {
4425 /* Preserve the logical dimensions of the screen. */
4426 update_window_manager_hints(0, 0);
4427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 return OK;
4430}
4431
4432/*
4433 * Get a reference to the font "name".
4434 * Return zero for failure.
4435 */
4436 GuiFont
4437gui_mch_get_font(char_u *name, int report_error)
4438{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
4441 /* can't do this when GUI is not running */
4442 if (!gui.in_use || name == NULL)
4443 return NULL;
4444
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (output_conv.vc_type != CONV_NONE)
4446 {
4447 char_u *buf;
4448
4449 buf = string_convert(&output_conv, name, NULL);
4450 if (buf != NULL)
4451 {
4452 font = pango_font_description_from_string((const char *)buf);
4453 vim_free(buf);
4454 }
4455 else
4456 font = NULL;
4457 }
4458 else
4459 font = pango_font_description_from_string((const char *)name);
4460
4461 if (font != NULL)
4462 {
4463 PangoFont *real_font;
4464
4465 /* pango_context_load_font() bails out if no font size is set */
4466 if (pango_font_description_get_size(font) <= 0)
4467 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4468
4469 real_font = pango_context_load_font(gui.text_context, font);
4470
4471 if (real_font == NULL)
4472 {
4473 pango_font_description_free(font);
4474 font = NULL;
4475 }
4476 else
4477 g_object_unref(real_font);
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 if (font == NULL)
4481 {
4482 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004483 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return NULL;
4485 }
4486
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 /*
4488 * The fixed-width check has been disabled for GTK+ 2. Rationale:
4489 *
4490 * - The check tends to report false positives, particularly
4491 * in non-Latin locales or with old X fonts.
4492 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
4493 * GTK+ 2 Vim is actually capable of displaying variable width
4494 * fonts. Those will just be spaced out like in AA xterm.
4495 * - Failing here for the default font causes GUI startup to fail
4496 * even with wiped out configuration files.
4497 * - The font dialog displays all fonts unfiltered, and it's rather
4498 * annoying if 95% of the listed fonts produce an error message.
4499 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004500#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
4502 /* Check that this is a mono-spaced font. Naturally, this is a bit
4503 * hackish -- fixed-width isn't really suitable for i18n text :/ */
4504 PangoLayout *layout;
4505 unsigned int i;
4506 int last_width = -1;
4507 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
4508
4509 layout = pango_layout_new(gui.text_context);
4510 pango_layout_set_font_description(layout, font);
4511
4512 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
4513 {
4514 int width;
4515
4516 pango_layout_set_text(layout, &test_chars[i], 1);
4517 pango_layout_get_size(layout, &width, NULL);
4518
4519 if (last_width >= 0 && width != last_width)
4520 {
4521 pango_font_description_free(font);
4522 font = NULL;
4523 break;
4524 }
4525
4526 last_width = width;
4527 }
4528
4529 g_object_unref(layout);
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531#endif
4532
4533 return font;
4534}
4535
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004536#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004537/*
4538 * Return the name of font "font" in allocated memory.
4539 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004540 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004541gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004542{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004543 if (font != NOFONT)
4544 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004545 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004546
Bram Moolenaar89d40322006-08-29 15:30:07 +00004547 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004548 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004549 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004550
Bram Moolenaar89d40322006-08-29 15:30:07 +00004551 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004552 return s;
4553 }
4554 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004555 return NULL;
4556}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004557#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559/*
4560 * If a font is not going to be used, free its structure.
4561 */
4562 void
4563gui_mch_free_font(GuiFont font)
4564{
4565 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567}
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569/*
4570 * Return the Pixel value (color) for the given color name. This routine was
4571 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4572 * Programmer's Guide.
4573 * Return INVALCOLOR for error.
4574 */
4575 guicolor_T
4576gui_mch_get_color(char_u *name)
4577{
4578 /* A number of colors that some X11 systems don't have */
4579 static const char *const vimnames[][2] =
4580 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004581 {"LightRed", "#FFBBBB"},
4582 {"LightGreen", "#88FF88"},
4583 {"LightMagenta","#FFBBFF"},
4584 {"DarkCyan", "#008888"},
4585 {"DarkBlue", "#0000BB"},
4586 {"DarkRed", "#BB0000"},
4587 {"DarkMagenta", "#BB00BB"},
4588 {"DarkGrey", "#BBBBBB"},
4589 {"DarkYellow", "#BBBB00"},
4590 {"Gray10", "#1A1A1A"},
4591 {"Grey10", "#1A1A1A"},
4592 {"Gray20", "#333333"},
4593 {"Grey20", "#333333"},
4594 {"Gray30", "#4D4D4D"},
4595 {"Grey30", "#4D4D4D"},
4596 {"Gray40", "#666666"},
4597 {"Grey40", "#666666"},
4598 {"Gray50", "#7F7F7F"},
4599 {"Grey50", "#7F7F7F"},
4600 {"Gray60", "#999999"},
4601 {"Grey60", "#999999"},
4602 {"Gray70", "#B3B3B3"},
4603 {"Grey70", "#B3B3B3"},
4604 {"Gray80", "#CCCCCC"},
4605 {"Grey80", "#CCCCCC"},
4606 {"Gray90", "#E5E5E5"},
4607 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 {NULL, NULL}
4609 };
4610
4611 if (!gui.in_use) /* can't do this when GUI not running */
4612 return INVALCOLOR;
4613
4614 while (name != NULL)
4615 {
4616 GdkColor color;
4617 int parsed;
4618 int i;
4619
4620 parsed = gdk_color_parse((const char *)name, &color);
4621
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (parsed)
4623 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4625 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 return (guicolor_T)color.pixel;
4627 }
4628 /* add a few builtin names and try again */
4629 for (i = 0; ; ++i)
4630 {
4631 if (vimnames[i][0] == NULL)
4632 {
4633 name = NULL;
4634 break;
4635 }
4636 if (STRICMP(name, vimnames[i][0]) == 0)
4637 {
4638 name = (char_u *)vimnames[i][1];
4639 break;
4640 }
4641 }
4642 }
4643
4644 return INVALCOLOR;
4645}
4646
4647/*
4648 * Set the current text foreground color.
4649 */
4650 void
4651gui_mch_set_fg_color(guicolor_T color)
4652{
4653 gui.fgcolor->pixel = (unsigned long)color;
4654}
4655
4656/*
4657 * Set the current text background color.
4658 */
4659 void
4660gui_mch_set_bg_color(guicolor_T color)
4661{
4662 gui.bgcolor->pixel = (unsigned long)color;
4663}
4664
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004665/*
4666 * Set the current text special color.
4667 */
4668 void
4669gui_mch_set_sp_color(guicolor_T color)
4670{
4671 gui.spcolor->pixel = (unsigned long)color;
4672}
4673
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674/*
4675 * Function-like convenience macro for the sake of efficiency.
4676 */
4677#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4678 G_STMT_START{ \
4679 PangoAttribute *tmp_attr_; \
4680 tmp_attr_ = (Attribute); \
4681 tmp_attr_->start_index = (Start); \
4682 tmp_attr_->end_index = (End); \
4683 pango_attr_list_insert((AttrList), tmp_attr_); \
4684 }G_STMT_END
4685
4686 static void
4687apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4688{
4689 char_u *start = NULL;
4690 char_u *p;
4691 int uc;
4692
4693 for (p = s; p < s + len; p += utf_byte2len(*p))
4694 {
4695 uc = utf_ptr2char(p);
4696
4697 if (start == NULL)
4698 {
4699 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4700 start = p;
4701 }
4702 else if (uc < 0x80 /* optimization shortcut */
4703 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4704 {
4705 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4706 attr_list, start - s, p - s);
4707 start = NULL;
4708 }
4709 }
4710
4711 if (start != NULL)
4712 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4713 attr_list, start - s, len);
4714}
4715
4716 static int
4717count_cluster_cells(char_u *s, PangoItem *item,
4718 PangoGlyphString* glyphs, int i,
4719 int *cluster_width,
4720 int *last_glyph_rbearing)
4721{
4722 char_u *p;
4723 int next; /* glyph start index of next cluster */
4724 int start, end; /* string segment of current cluster */
4725 int width; /* real cluster width in Pango units */
4726 int uc;
4727 int cellcount = 0;
4728
4729 width = glyphs->glyphs[i].geometry.width;
4730
4731 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4732 {
4733 if (glyphs->glyphs[next].attr.is_cluster_start)
4734 break;
4735 else if (glyphs->glyphs[next].geometry.width > width)
4736 width = glyphs->glyphs[next].geometry.width;
4737 }
4738
4739 start = item->offset + glyphs->log_clusters[i];
4740 end = item->offset + ((next < glyphs->num_glyphs) ?
4741 glyphs->log_clusters[next] : item->length);
4742
4743 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4744 {
4745 uc = utf_ptr2char(p);
4746 if (uc < 0x80)
4747 ++cellcount;
4748 else if (!utf_iscomposing(uc))
4749 cellcount += utf_char2cells(uc);
4750 }
4751
4752 if (last_glyph_rbearing != NULL
4753 && cellcount > 0 && next == glyphs->num_glyphs)
4754 {
4755 PangoRectangle ink_rect;
4756 /*
4757 * If a certain combining mark had to be taken from a non-monospace
4758 * font, we have to compensate manually by adapting x_offset according
4759 * to the ink extents of the previous glyph.
4760 */
4761 pango_font_get_glyph_extents(item->analysis.font,
4762 glyphs->glyphs[i].glyph,
4763 &ink_rect, NULL);
4764
4765 if (PANGO_RBEARING(ink_rect) > 0)
4766 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4767 }
4768
4769 if (cellcount > 0)
4770 *cluster_width = width;
4771
4772 return cellcount;
4773}
4774
4775/*
4776 * If there are only combining characters in the cluster, we cannot just
4777 * change the width of the previous glyph since there is none. Therefore
4778 * some guesswork is needed.
4779 *
4780 * If ink_rect.x is negative Pango apparently has taken care of the composing
4781 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4782 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004783 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 *
4785 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4786 * the position of the previous glyph. Apparently this happens only with old
4787 * X fonts which don't provide the special combining information needed by
4788 * Pango.
4789 */
4790 static void
4791setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4792 int last_cellcount, int last_cluster_width,
4793 int last_glyph_rbearing)
4794{
4795 PangoRectangle ink_rect;
4796 PangoRectangle logical_rect;
4797 int width;
4798
4799 width = last_cellcount * gui.char_width * PANGO_SCALE;
4800 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4801 glyph->geometry.width = 0;
4802
4803 pango_font_get_glyph_extents(item->analysis.font,
4804 glyph->glyph,
4805 &ink_rect, &logical_rect);
4806 if (ink_rect.x < 0)
4807 {
4808 glyph->geometry.x_offset += last_glyph_rbearing;
4809 glyph->geometry.y_offset = logical_rect.height
4810 - (gui.char_height - p_linespace) * PANGO_SCALE;
4811 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004812 else
4813 /* If the accent width is smaller than the cluster width, position it
4814 * in the middle. */
4815 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816}
4817
4818 static void
4819draw_glyph_string(int row, int col, int num_cells, int flags,
4820 PangoFont *font, PangoGlyphString *glyphs)
4821{
4822 if (!(flags & DRAW_TRANSP))
4823 {
4824 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4825
4826 gdk_draw_rectangle(gui.drawarea->window,
4827 gui.text_gc,
4828 TRUE,
4829 FILL_X(col),
4830 FILL_Y(row),
4831 num_cells * gui.char_width,
4832 gui.char_height);
4833 }
4834
4835 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4836
4837 gdk_draw_glyphs(gui.drawarea->window,
4838 gui.text_gc,
4839 font,
4840 TEXT_X(col),
4841 TEXT_Y(row),
4842 glyphs);
4843
4844 /* redraw the contents with an offset of 1 to emulate bold */
4845 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4846 gdk_draw_glyphs(gui.drawarea->window,
4847 gui.text_gc,
4848 font,
4849 TEXT_X(col) + 1,
4850 TEXT_Y(row),
4851 glyphs);
4852}
4853
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004854/*
4855 * Draw underline and undercurl at the bottom of the character cell.
4856 */
4857 static void
4858draw_under(int flags, int row, int col, int cells)
4859{
4860 int i;
4861 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004862 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004863 int y = FILL_Y(row + 1) - 1;
4864
4865 /* Undercurl: draw curl at the bottom of the character cell. */
4866 if (flags & DRAW_UNDERC)
4867 {
4868 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4869 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4870 {
4871 offset = val[i % 8];
4872 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4873 }
4874 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4875 }
4876
4877 /* Underline: draw a line at the bottom of the character cell. */
4878 if (flags & DRAW_UNDERL)
4879 {
4880 /* When p_linespace is 0, overwrite the bottom row of pixels.
4881 * Otherwise put the line just below the character. */
4882 if (p_linespace > 1)
4883 y -= p_linespace - 1;
4884 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4885 FILL_X(col), y,
4886 FILL_X(col + cells) - 1, y);
4887 }
4888}
4889
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 int
4891gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4892{
4893 GdkRectangle area; /* area for clip mask */
4894 PangoGlyphString *glyphs; /* glyphs of current item */
4895 int column_offset = 0; /* column offset in cells */
4896 int i;
4897 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4898 char_u *new_conv_buf;
4899 int convlen;
4900 char_u *sp, *bp;
4901 int plen;
4902
4903 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4904 return len;
4905
4906 if (output_conv.vc_type != CONV_NONE)
4907 {
4908 /*
4909 * Convert characters from 'encoding' to 'termencoding', which is set
4910 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4911 * prohibits changing this to something else than UTF-8 if the GUI is
4912 * in use.
4913 */
4914 convlen = len;
4915 conv_buf = string_convert(&output_conv, s, &convlen);
4916 g_return_val_if_fail(conv_buf != NULL, len);
4917
4918 /* Correct for differences in char width: some chars are
4919 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4920 * compensate for that. */
4921 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4922 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004923 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4925 {
4926 new_conv_buf = alloc(convlen + 2);
4927 if (new_conv_buf == NULL)
4928 return len;
4929 plen += bp - conv_buf;
4930 mch_memmove(new_conv_buf, conv_buf, plen);
4931 new_conv_buf[plen] = ' ';
4932 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4933 convlen - plen + 1);
4934 vim_free(conv_buf);
4935 conv_buf = new_conv_buf;
4936 ++convlen;
4937 bp = conv_buf + plen;
4938 plen = 1;
4939 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004940 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 bp += plen;
4942 }
4943 s = conv_buf;
4944 len = convlen;
4945 }
4946
4947 /*
4948 * Restrict all drawing to the current screen line in order to prevent
4949 * fuzzy font lookups from messing up the screen.
4950 */
4951 area.x = gui.border_offset;
4952 area.y = FILL_Y(row);
4953 area.width = gui.num_cols * gui.char_width;
4954 area.height = gui.char_height;
4955
4956 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4957 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4958
4959 glyphs = pango_glyph_string_new();
4960
4961 /*
4962 * Optimization hack: If possible, skip the itemize and shaping process
4963 * for pure ASCII strings. This optimization is particularly effective
4964 * because Vim draws space characters to clear parts of the screen.
4965 */
4966 if (!(flags & DRAW_ITALIC)
4967 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4968 && gui.ascii_glyphs != NULL)
4969 {
4970 char_u *p;
4971
4972 for (p = s; p < s + len; ++p)
4973 if (*p & 0x80)
4974 goto not_ascii;
4975
4976 pango_glyph_string_set_size(glyphs, len);
4977
4978 for (i = 0; i < len; ++i)
4979 {
4980 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4981 glyphs->log_clusters[i] = i;
4982 }
4983
4984 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
4985
4986 column_offset = len;
4987 }
4988 else
4989not_ascii:
4990 {
4991 PangoAttrList *attr_list;
4992 GList *item_list;
4993 int cluster_width;
4994 int last_glyph_rbearing;
4995 int cells = 0; /* cells occupied by current cluster */
4996
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004997 /* Safety check: pango crashes when invoked with invalid utf-8
4998 * characters. */
4999 if (!utf_valid_string(s, s + len))
5000 {
5001 column_offset = len;
5002 goto skipitall;
5003 }
5004
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 /* original width of the current cluster */
5006 cluster_width = PANGO_SCALE * gui.char_width;
5007
5008 /* right bearing of the last non-composing glyph */
5009 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5010
5011 attr_list = pango_attr_list_new();
5012
5013 /* If 'guifontwide' is set then use that for double-width characters.
5014 * Otherwise just go with 'guifont' and let Pango do its thing. */
5015 if (gui.wide_font != NULL)
5016 apply_wide_font_attr(s, len, attr_list);
5017
5018 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5019 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5020 attr_list, 0, len);
5021 if (flags & DRAW_ITALIC)
5022 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5023 attr_list, 0, len);
5024 /*
5025 * Break the text into segments with consistent directional level
5026 * and shaping engine. Pure Latin text needs only a single segment,
5027 * so there's no need to worry about the loop's efficiency. Better
5028 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5029 */
5030 item_list = pango_itemize(gui.text_context,
5031 (const char *)s, 0, len, attr_list, NULL);
5032
5033 while (item_list != NULL)
5034 {
5035 PangoItem *item;
5036 int item_cells = 0; /* item length in cells */
5037
5038 item = (PangoItem *)item_list->data;
5039 item_list = g_list_delete_link(item_list, item_list);
5040 /*
5041 * Increment the bidirectional embedding level by 1 if it is not
5042 * even. An odd number means the output will be RTL, but we don't
5043 * want that since Vim handles right-to-left text on its own. It
5044 * would probably be sufficient to just set level = 0, but you can
5045 * never know :)
5046 *
5047 * Unfortunately we can't take advantage of Pango's ability to
5048 * render both LTR and RTL at the same time. In order to support
5049 * that, Vim's main screen engine would have to make use of Pango
5050 * functionality.
5051 */
5052 item->analysis.level = (item->analysis.level + 1) & (~1U);
5053
5054 /* HACK: Overrule the shape engine, we don't want shaping to be
5055 * done, because drawing the cursor would change the display. */
5056 item->analysis.shape_engine = default_shape_engine;
5057
5058 pango_shape((const char *)s + item->offset, item->length,
5059 &item->analysis, glyphs);
5060 /*
5061 * Fixed-width hack: iterate over the array and assign a fixed
5062 * width to each glyph, thus overriding the choice made by the
5063 * shaping engine. We use utf_char2cells() to determine the
5064 * number of cells needed.
5065 *
5066 * Also perform all kind of dark magic to get composing
5067 * characters right (and pretty too of course).
5068 */
5069 for (i = 0; i < glyphs->num_glyphs; ++i)
5070 {
5071 PangoGlyphInfo *glyph;
5072
5073 glyph = &glyphs->glyphs[i];
5074
5075 if (glyph->attr.is_cluster_start)
5076 {
5077 int cellcount;
5078
5079 cellcount = count_cluster_cells(
5080 s, item, glyphs, i, &cluster_width,
5081 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5082
5083 if (cellcount > 0)
5084 {
5085 int width;
5086
5087 width = cellcount * gui.char_width * PANGO_SCALE;
5088 glyph->geometry.x_offset +=
5089 MAX(0, width - cluster_width) / 2;
5090 glyph->geometry.width = width;
5091 }
5092 else
5093 {
5094 /* If there are only combining characters in the
5095 * cluster, we cannot just change the width of the
5096 * previous glyph since there is none. Therefore
5097 * some guesswork is needed. */
5098 setup_zero_width_cluster(item, glyph, cells,
5099 cluster_width,
5100 last_glyph_rbearing);
5101 }
5102
5103 item_cells += cellcount;
5104 cells = cellcount;
5105 }
5106 else if (i > 0)
5107 {
5108 int width;
5109
5110 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005111 * characters the canonical way.
5112 * Older versions of Pango used a positive x_offset,
5113 * then set the width of the previous glyph to zero.
5114 * Newer versions of Pango use a negative x_offset.
5115 * For both adjust the x_offset to position the glyph in
5116 * the middle. */
5117 if (glyph->geometry.x_offset >= 0)
5118 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 width = cells * gui.char_width * PANGO_SCALE;
5120 glyph->geometry.x_offset +=
5121 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123 else /* i == 0 "cannot happen" */
5124 {
5125 glyph->geometry.width = 0;
5126 }
5127 }
5128
5129 /*** Aaaaand action! ***/
5130 draw_glyph_string(row, col + column_offset, item_cells,
5131 flags, item->analysis.font, glyphs);
5132
5133 pango_item_free(item);
5134
5135 column_offset += item_cells;
5136 }
5137
5138 pango_attr_list_unref(attr_list);
5139 }
5140
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005141skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005142 /* Draw underline and undercurl. */
5143 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
5145 pango_glyph_string_free(glyphs);
5146 vim_free(conv_buf);
5147
5148 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5149
5150 return column_offset;
5151}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152
5153/*
5154 * Return OK if the key with the termcap name "name" is supported.
5155 */
5156 int
5157gui_mch_haskey(char_u *name)
5158{
5159 int i;
5160
5161 for (i = 0; special_keys[i].key_sym != 0; i++)
5162 if (name[0] == special_keys[i].code0
5163 && name[1] == special_keys[i].code1)
5164 return OK;
5165 return FAIL;
5166}
5167
5168#if defined(FEAT_TITLE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 || defined(PROTO)
5170/*
5171 * Return the text window-id and display. Only required for X-based GUI's
5172 */
5173 int
5174gui_get_x11_windis(Window *win, Display **dis)
5175{
5176 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5177 {
5178 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5179 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5180 return OK;
5181 }
5182
5183 *dis = NULL;
5184 *win = 0;
5185 return FAIL;
5186}
5187#endif
5188
5189#if defined(FEAT_CLIENTSERVER) \
5190 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5191
5192 Display *
5193gui_mch_get_display(void)
5194{
5195 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5196 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5197 else
5198 return NULL;
5199}
5200#endif
5201
5202 void
5203gui_mch_beep(void)
5204{
5205#ifdef HAVE_GTK_MULTIHEAD
5206 GdkDisplay *display;
5207
5208 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5209 display = gtk_widget_get_display(gui.mainwin);
5210 else
5211 display = gdk_display_get_default();
5212
5213 if (display != NULL)
5214 gdk_display_beep(display);
5215#else
5216 gdk_beep();
5217#endif
5218}
5219
5220 void
5221gui_mch_flash(int msec)
5222{
5223 GdkGCValues values;
5224 GdkGC *invert_gc;
5225
5226 if (gui.drawarea->window == NULL)
5227 return;
5228
5229 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5230 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5231 values.function = GDK_XOR;
5232 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5233 &values,
5234 GDK_GC_FOREGROUND |
5235 GDK_GC_BACKGROUND |
5236 GDK_GC_FUNCTION);
5237 gdk_gc_set_exposures(invert_gc,
5238 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5239 /*
5240 * Do a visual beep by changing back and forth in some undetermined way,
5241 * the foreground and background colors. This is due to the fact that
5242 * there can't be really any prediction about the effects of XOR on
5243 * arbitrary X11 servers. However this seems to be enough for what we
5244 * intend it to do.
5245 */
5246 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5247 TRUE,
5248 0, 0,
5249 FILL_X((int)Columns) + gui.border_offset,
5250 FILL_Y((int)Rows) + gui.border_offset);
5251
5252 gui_mch_flush();
5253 ui_delay((long)msec, TRUE); /* wait so many msec */
5254
5255 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5256 TRUE,
5257 0, 0,
5258 FILL_X((int)Columns) + gui.border_offset,
5259 FILL_Y((int)Rows) + gui.border_offset);
5260
5261 gdk_gc_destroy(invert_gc);
5262}
5263
5264/*
5265 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5266 */
5267 void
5268gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5269{
5270 GdkGCValues values;
5271 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 if (gui.drawarea->window == NULL)
5274 return;
5275
Bram Moolenaar89d40322006-08-29 15:30:07 +00005276 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5277 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 values.function = GDK_XOR;
5279 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5280 &values,
5281 GDK_GC_FOREGROUND |
5282 GDK_GC_BACKGROUND |
5283 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005284 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5285 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5287 TRUE,
5288 FILL_X(c), FILL_Y(r),
5289 (nc) * gui.char_width, (nr) * gui.char_height);
5290 gdk_gc_destroy(invert_gc);
5291}
5292
5293/*
5294 * Iconify the GUI window.
5295 */
5296 void
5297gui_mch_iconify(void)
5298{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300}
5301
5302#if defined(FEAT_EVAL) || defined(PROTO)
5303/*
5304 * Bring the Vim window to the foreground.
5305 */
5306 void
5307gui_mch_set_foreground(void)
5308{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310}
5311#endif
5312
5313/*
5314 * Draw a cursor without focus.
5315 */
5316 void
5317gui_mch_draw_hollow_cursor(guicolor_T color)
5318{
5319 int i = 1;
5320
5321 if (gui.drawarea->window == NULL)
5322 return;
5323
5324 gui_mch_set_fg_color(color);
5325
5326 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 if (mb_lefthalve(gui.row, gui.col))
5328 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5330 FALSE,
5331 FILL_X(gui.col), FILL_Y(gui.row),
5332 i * gui.char_width - 1, gui.char_height - 1);
5333}
5334
5335/*
5336 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5337 * color "color".
5338 */
5339 void
5340gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5341{
5342 if (gui.drawarea->window == NULL)
5343 return;
5344
5345 gui_mch_set_fg_color(color);
5346
5347 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5348 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5349 TRUE,
5350#ifdef FEAT_RIGHTLEFT
5351 /* vertical line should be on the right of current point */
5352 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5353#endif
5354 FILL_X(gui.col),
5355 FILL_Y(gui.row) + gui.char_height - h,
5356 w, h);
5357}
5358
5359
5360/*
5361 * Catch up with any queued X11 events. This may put keyboard input into the
5362 * input buffer, call resize call-backs, trigger timers etc. If there is
5363 * nothing in the X11 event queue (& no timers pending), then we return
5364 * immediately.
5365 */
5366 void
5367gui_mch_update(void)
5368{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005369 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5370 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371}
5372
5373 static gint
5374input_timer_cb(gpointer data)
5375{
5376 int *timed_out = (int *) data;
5377
Bram Moolenaar49325942007-05-10 19:19:59 +00005378 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 *timed_out = TRUE;
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 return FALSE; /* don't happen again */
5382}
5383
5384#ifdef FEAT_SNIFF
5385/*
5386 * Callback function, used when data is available on the SNiFF connection.
5387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 static void
5389sniff_request_cb(
5390 gpointer data,
5391 gint source_fd,
5392 GdkInputCondition condition)
5393{
5394 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5395
5396 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397}
5398#endif
5399
5400/*
5401 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5402 * from the keyboard.
5403 * wtime == -1 Wait forever.
5404 * wtime == 0 This should never happen.
5405 * wtime > 0 Wait wtime milliseconds for a character.
5406 * Returns OK if a character was found to be available within the given time,
5407 * or FAIL otherwise.
5408 */
5409 int
5410gui_mch_wait_for_chars(long wtime)
5411{
5412 int focus;
5413 guint timer;
5414 static int timed_out;
5415#ifdef FEAT_SNIFF
5416 static int sniff_on = 0;
5417 static gint sniff_input_id = 0;
5418#endif
5419
5420#ifdef FEAT_SNIFF
5421 if (sniff_on && !want_sniff_request)
5422 {
5423 if (sniff_input_id)
5424 gdk_input_remove(sniff_input_id);
5425 sniff_on = 0;
5426 }
5427 else if (!sniff_on && want_sniff_request)
5428 {
5429 /* Add fd_from_sniff to watch for available data in main loop. */
5430 sniff_input_id = gdk_input_add(fd_from_sniff,
5431 GDK_INPUT_READ, sniff_request_cb, NULL);
5432 sniff_on = 1;
5433 }
5434#endif
5435
5436 timed_out = FALSE;
5437
5438 /* this timeout makes sure that we will return if no characters arrived in
5439 * time */
5440
5441 if (wtime > 0)
5442 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5443 else
5444 timer = 0;
5445
5446 focus = gui.in_focus;
5447
5448 do
5449 {
5450 /* Stop or start blinking when focus changes */
5451 if (gui.in_focus != focus)
5452 {
5453 if (gui.in_focus)
5454 gui_mch_start_blink();
5455 else
5456 gui_mch_stop_blink();
5457 focus = gui.in_focus;
5458 }
5459
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005460#if defined(FEAT_NETBEANS_INTG)
5461 /* Process the queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005462 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005463#endif
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 /*
5466 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005467 * Skip this if input is available anyway (can happen in rare
5468 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005470 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005471 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
5473 /* Got char, return immediately */
5474 if (input_available())
5475 {
5476 if (timer != 0 && !timed_out)
5477 gtk_timeout_remove(timer);
5478 return OK;
5479 }
5480 } while (wtime < 0 || !timed_out);
5481
5482 /*
5483 * Flush all eventually pending (drawing) events.
5484 */
5485 gui_mch_update();
5486
5487 return FAIL;
5488}
5489
5490
5491/****************************************************************************
5492 * Output drawing routines.
5493 ****************************************************************************/
5494
5495
5496/* Flush any output to the screen */
5497 void
5498gui_mch_flush(void)
5499{
5500#ifdef HAVE_GTK_MULTIHEAD
5501 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5502 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5503#else
5504 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 /* This happens to actually do what gui_mch_flush() is supposed to do,
5507 * according to the comment above. */
5508 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5509 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510}
5511
5512/*
5513 * Clear a rectangular region of the screen from text pos (row1, col1) to
5514 * (row2, col2) inclusive.
5515 */
5516 void
5517gui_mch_clear_block(int row1, int col1, int row2, int col2)
5518{
5519 GdkColor color;
5520
5521 if (gui.drawarea->window == NULL)
5522 return;
5523
5524 color.pixel = gui.back_pixel;
5525
5526 gdk_gc_set_foreground(gui.text_gc, &color);
5527
5528 /* Clear one extra pixel at the far right, for when bold characters have
5529 * spilled over to the window border. */
5530 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5531 FILL_X(col1), FILL_Y(row1),
5532 (col2 - col1 + 1) * gui.char_width
5533 + (col2 == Columns - 1),
5534 (row2 - row1 + 1) * gui.char_height);
5535}
5536
5537 void
5538gui_mch_clear_all(void)
5539{
5540 if (gui.drawarea->window != NULL)
5541 gdk_window_clear(gui.drawarea->window);
5542}
5543
5544/*
5545 * Redraw any text revealed by scrolling up/down.
5546 */
5547 static void
5548check_copy_area(void)
5549{
5550 GdkEvent *event;
5551 int expose_count;
5552
5553 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5554 return;
5555
5556 /* Avoid redrawing the cursor while scrolling or it'll end up where
5557 * we don't want it to be. I'm not sure if it's correct to call
5558 * gui_dont_update_cursor() at this point but it works as a quick
5559 * fix for now. */
5560 gui_dont_update_cursor();
5561
5562 do
5563 {
5564 /* Wait to check whether the scroll worked or not. */
5565 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5566
5567 if (event == NULL)
5568 break; /* received NoExpose event */
5569
5570 gui_redraw(event->expose.area.x, event->expose.area.y,
5571 event->expose.area.width, event->expose.area.height);
5572
5573 expose_count = event->expose.count;
5574 gdk_event_free(event);
5575 }
5576 while (expose_count > 0); /* more events follow */
5577
5578 gui_can_update_cursor();
5579}
5580
5581/*
5582 * Delete the given number of lines from the given row, scrolling up any
5583 * text further down within the scroll region.
5584 */
5585 void
5586gui_mch_delete_lines(int row, int num_lines)
5587{
5588 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5589 return; /* Can't see the window */
5590
5591 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5592 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5593
5594 /* copy one extra pixel, for when bold has spilled over */
5595 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5596 FILL_X(gui.scroll_region_left), FILL_Y(row),
5597 gui.drawarea->window,
5598 FILL_X(gui.scroll_region_left),
5599 FILL_Y(row + num_lines),
5600 gui.char_width * (gui.scroll_region_right
5601 - gui.scroll_region_left + 1) + 1,
5602 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5603
5604 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5605 gui.scroll_region_left,
5606 gui.scroll_region_bot, gui.scroll_region_right);
5607 check_copy_area();
5608}
5609
5610/*
5611 * Insert the given number of lines before the given row, scrolling down any
5612 * following text within the scroll region.
5613 */
5614 void
5615gui_mch_insert_lines(int row, int num_lines)
5616{
5617 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5618 return; /* Can't see the window */
5619
5620 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5621 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5622
5623 /* copy one extra pixel, for when bold has spilled over */
5624 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5625 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5626 gui.drawarea->window,
5627 FILL_X(gui.scroll_region_left), FILL_Y(row),
5628 gui.char_width * (gui.scroll_region_right
5629 - gui.scroll_region_left + 1) + 1,
5630 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5631
5632 gui_clear_block(row, gui.scroll_region_left,
5633 row + num_lines - 1, gui.scroll_region_right);
5634 check_copy_area();
5635}
5636
5637/*
5638 * X Selection stuff, for cutting and pasting text to other windows.
5639 */
5640 void
5641clip_mch_request_selection(VimClipboard *cbd)
5642{
5643 GdkAtom target;
5644 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005645 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5648 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005649 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5650 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 received_selection = RS_NONE;
5652 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5653
5654 gtk_selection_convert(gui.drawarea,
5655 cbd->gtk_sel_atom, target,
5656 (guint32)GDK_CURRENT_TIME);
5657
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005658 /* Hack: Wait up to three seconds for the selection. A hang was
5659 * noticed here when using the netrw plugin combined with ":gui"
5660 * during the FocusGained event. */
5661 start = time(NULL);
5662 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005663 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
5665 if (received_selection != RS_FAIL)
5666 return;
5667 }
5668
5669 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005670 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671}
5672
5673/*
5674 * Disown the selection.
5675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005677clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 /* WEIRD: when using NULL to actually disown the selection, we lose the
5680 * selection the first time we own it. */
5681 /*
5682 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
5683 gui_mch_update();
5684 */
5685}
5686
5687/*
5688 * Own the selection and return OK if it worked.
5689 */
5690 int
5691clip_mch_own_selection(VimClipboard *cbd)
5692{
5693 int success;
5694
5695 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar7cfea752010-06-22 06:07:12 +02005696 clipboard_event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 gui_mch_update();
5698 return (success) ? OK : FAIL;
5699}
5700
5701/*
5702 * Send the current selection to the clipboard. Do nothing for X because we
5703 * will fill in the selection only when requested by another app.
5704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005706clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708}
5709
5710
5711#if defined(FEAT_MENU) || defined(PROTO)
5712/*
5713 * Make a menu item appear either active or not active (grey or not grey).
5714 */
5715 void
5716gui_mch_menu_grey(vimmenu_T *menu, int grey)
5717{
5718 if (menu->id == NULL)
5719 return;
5720
5721 if (menu_is_separator(menu->name))
5722 grey = TRUE;
5723
5724 gui_mch_menu_hidden(menu, FALSE);
5725 /* Be clever about bitfields versus true booleans here! */
5726 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5727 {
5728 gtk_widget_set_sensitive(menu->id, !grey);
5729 gui_mch_update();
5730 }
5731}
5732
5733/*
5734 * Make menu item hidden or not hidden.
5735 */
5736 void
5737gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5738{
5739 if (menu->id == 0)
5740 return;
5741
5742 if (hidden)
5743 {
5744 if (GTK_WIDGET_VISIBLE(menu->id))
5745 {
5746 gtk_widget_hide(menu->id);
5747 gui_mch_update();
5748 }
5749 }
5750 else
5751 {
5752 if (!GTK_WIDGET_VISIBLE(menu->id))
5753 {
5754 gtk_widget_show(menu->id);
5755 gui_mch_update();
5756 }
5757 }
5758}
5759
5760/*
5761 * This is called after setting all the menus to grey/hidden or not.
5762 */
5763 void
5764gui_mch_draw_menubar(void)
5765{
5766 /* just make sure that the visual changes get effect immediately */
5767 gui_mch_update();
5768}
5769#endif /* FEAT_MENU */
5770
5771/*
5772 * Scrollbar stuff.
5773 */
5774 void
5775gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5776{
5777 if (sb->id == NULL)
5778 return;
5779
5780 if (flag)
5781 gtk_widget_show(sb->id);
5782 else
5783 gtk_widget_hide(sb->id);
5784
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005785 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786}
5787
5788
5789/*
5790 * Return the RGB value of a pixel as long.
5791 */
5792 long_u
5793gui_mch_get_rgb(guicolor_T pixel)
5794{
5795 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5797 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798
5799 return (((unsigned)color.red & 0xff00) << 8)
5800 | ((unsigned)color.green & 0xff00)
5801 | (((unsigned)color.blue & 0xff00) >> 8);
5802}
5803
5804/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005805 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005807 void
5808gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005810 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811}
5812
5813 void
5814gui_mch_setmouse(int x, int y)
5815{
5816 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5817 * internal GDK mechanism present to accomplish this. (and for good
5818 * reason...) */
5819 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5820 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5821 0, 0, 0U, 0U, x, y);
5822}
5823
5824
5825#ifdef FEAT_MOUSESHAPE
5826/* The last set mouse pointer shape is remembered, to be used when it goes
5827 * from hidden to not hidden. */
5828static int last_shape = 0;
5829#endif
5830
5831/*
5832 * Use the blank mouse pointer or not.
5833 *
5834 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5835 */
5836 void
5837gui_mch_mousehide(int hide)
5838{
5839 if (gui.pointer_hidden != hide)
5840 {
5841 gui.pointer_hidden = hide;
5842 if (gui.drawarea->window && gui.blank_pointer != NULL)
5843 {
5844 if (hide)
5845 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5846 else
5847#ifdef FEAT_MOUSESHAPE
5848 mch_set_mouse_shape(last_shape);
5849#else
5850 gdk_window_set_cursor(gui.drawarea->window, NULL);
5851#endif
5852 }
5853 }
5854}
5855
5856#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5857
5858/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5859 * misc2.c! */
5860static const int mshape_ids[] =
5861{
5862 GDK_LEFT_PTR, /* arrow */
5863 GDK_CURSOR_IS_PIXMAP, /* blank */
5864 GDK_XTERM, /* beam */
5865 GDK_SB_V_DOUBLE_ARROW, /* updown */
5866 GDK_SIZING, /* udsizing */
5867 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5868 GDK_SIZING, /* lrsizing */
5869 GDK_WATCH, /* busy */
5870 GDK_X_CURSOR, /* no */
5871 GDK_CROSSHAIR, /* crosshair */
5872 GDK_HAND1, /* hand1 */
5873 GDK_HAND2, /* hand2 */
5874 GDK_PENCIL, /* pencil */
5875 GDK_QUESTION_ARROW, /* question */
5876 GDK_RIGHT_PTR, /* right-arrow */
5877 GDK_CENTER_PTR, /* up-arrow */
5878 GDK_LEFT_PTR /* last one */
5879};
5880
5881 void
5882mch_set_mouse_shape(int shape)
5883{
5884 int id;
5885 GdkCursor *c;
5886
5887 if (gui.drawarea->window == NULL)
5888 return;
5889
5890 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5891 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5892 else
5893 {
5894 if (shape >= MSHAPE_NUMBERED)
5895 {
5896 id = shape - MSHAPE_NUMBERED;
5897 if (id >= GDK_LAST_CURSOR)
5898 id = GDK_LEFT_PTR;
5899 else
5900 id &= ~1; /* they are always even (why?) */
5901 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005902 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005904 else
5905 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906# ifdef HAVE_GTK_MULTIHEAD
5907 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005908 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005910 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911# endif
5912 gdk_window_set_cursor(gui.drawarea->window, c);
5913 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5914 }
5915 if (shape != MSHAPE_HIDE)
5916 last_shape = shape;
5917}
5918#endif /* FEAT_MOUSESHAPE */
5919
5920
5921#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5922/*
5923 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5924 * scaled down if the current font is not big enough, or scaled up if the image
5925 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5926 * will be cut off if the current font is not big enough, or centered if it's
5927 * too small.
5928 */
5929# define SIGN_WIDTH (2 * gui.char_width)
5930# define SIGN_HEIGHT (gui.char_height)
5931# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 void
5934gui_mch_drawsign(int row, int col, int typenr)
5935{
5936 GdkPixbuf *sign;
5937
5938 sign = (GdkPixbuf *)sign_get_image(typenr);
5939
5940 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5941 {
5942 int width;
5943 int height;
5944 int xoffset;
5945 int yoffset;
5946 int need_scale;
5947
5948 width = gdk_pixbuf_get_width(sign);
5949 height = gdk_pixbuf_get_height(sign);
5950 /*
5951 * Decide whether we need to scale. Allow one pixel of border
5952 * width to be cut off, in order to avoid excessive scaling for
5953 * tiny differences in font size.
5954 */
5955 need_scale = (width > SIGN_WIDTH + 2
5956 || height > SIGN_HEIGHT + 2
5957 || (width < 3 * SIGN_WIDTH / 4
5958 && height < 3 * SIGN_HEIGHT / 4));
5959 if (need_scale)
5960 {
5961 double aspect;
5962
5963 /* Keep the original aspect ratio */
5964 aspect = (double)height / (double)width;
5965 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5966 width = MIN(width, SIGN_WIDTH);
5967 height = (double)width * aspect;
5968
5969 /* This doesn't seem to be worth caching, and doing so
5970 * would complicate the code quite a bit. */
5971 sign = gdk_pixbuf_scale_simple(sign, width, height,
5972 GDK_INTERP_BILINEAR);
5973 if (sign == NULL)
5974 return; /* out of memory */
5975 }
5976
5977 /* The origin is the upper-left corner of the pixmap. Therefore
5978 * these offset may become negative if the pixmap is smaller than
5979 * the 2x1 cells reserved for the sign icon. */
5980 xoffset = (width - SIGN_WIDTH) / 2;
5981 yoffset = (height - SIGN_HEIGHT) / 2;
5982
5983 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5984
5985 gdk_draw_rectangle(gui.drawarea->window,
5986 gui.text_gc,
5987 TRUE,
5988 FILL_X(col),
5989 FILL_Y(row),
5990 SIGN_WIDTH,
5991 SIGN_HEIGHT);
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 gdk_pixbuf_render_to_drawable_alpha(sign,
5994 gui.drawarea->window,
5995 MAX(0, xoffset),
5996 MAX(0, yoffset),
5997 FILL_X(col) - MIN(0, xoffset),
5998 FILL_Y(row) - MIN(0, yoffset),
5999 MIN(width, SIGN_WIDTH),
6000 MIN(height, SIGN_HEIGHT),
6001 GDK_PIXBUF_ALPHA_BILEVEL,
6002 127,
6003 GDK_RGB_DITHER_NORMAL,
6004 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 if (need_scale)
6006 g_object_unref(sign);
6007 }
6008}
6009
6010 void *
6011gui_mch_register_sign(char_u *signfile)
6012{
6013 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6014 {
6015 GdkPixbuf *sign;
6016 GError *error = NULL;
6017 char_u *message;
6018
6019 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6020
6021 if (error == NULL)
6022 return sign;
6023
6024 message = (char_u *)error->message;
6025
6026 if (message != NULL && input_conv.vc_type != CONV_NONE)
6027 message = string_convert(&input_conv, message, NULL);
6028
6029 if (message != NULL)
6030 {
6031 /* The error message is already translated and will be more
6032 * descriptive than anything we could possibly do ourselves. */
6033 EMSG2("E255: %s", message);
6034
6035 if (input_conv.vc_type != CONV_NONE)
6036 vim_free(message);
6037 }
6038 g_error_free(error);
6039 }
6040
6041 return NULL;
6042}
6043
6044 void
6045gui_mch_destroy_sign(void *sign)
6046{
6047 if (sign != NULL)
6048 g_object_unref(sign);
6049}
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051#endif /* FEAT_SIGN_ICONS */