[7.2.444] Can't build with GTK 1, gtk_selection_clear_targets() is not
[vim_mainline.git] / src / gui_gtk_x11.c
blob81ce63c080657e8ac8faf7a0be5bb362a4c3c58e
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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 */
11 * Porting to GTK+ was done by:
13 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
18 * Support for GTK+ 2 was added by:
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
24 #include "vim.h"
26 #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
40 # ifdef bind_textdomain_codeset
41 # undef bind_textdomain_codeset
42 # endif
43 # 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"
48 # ifdef HAVE_GTK2
49 /* missing prototype in bonobo-dock-item.h */
50 extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
51 # endif
52 #endif
54 #if !defined(FEAT_GUI_GTK) && defined(PROTO)
55 /* When generating prototypes we don't want syntax errors. */
56 # define GdkAtom int
57 # define GdkEventExpose int
58 # define GdkEventFocus int
59 # define GdkEventVisibility int
60 # define GdkEventProperty int
61 # define GtkContainer int
62 # define GtkTargetEntry int
63 # define GtkType int
64 # define GtkWidget int
65 # define gint int
66 # define gpointer int
67 # define guint int
68 # define GdkEventKey int
69 # define GdkEventSelection int
70 # define GtkSelectionData int
71 # define GdkEventMotion int
72 # define GdkEventButton int
73 # define GdkDragContext int
74 # define GdkEventConfigure int
75 # define GdkEventClient int
76 #else
77 # include <gdk/gdkkeysyms.h>
78 # include <gdk/gdk.h>
79 # ifdef WIN3264
80 # include <gdk/gdkwin32.h>
81 # else
82 # include <gdk/gdkx.h>
83 # endif
85 # include <gtk/gtk.h>
86 # include "gui_gtk_f.h"
87 #endif
89 #ifdef HAVE_X11_SUNKEYSYM_H
90 # include <X11/Sunkeysym.h>
91 #endif
94 * Easy-to-use macro for multihead support.
96 #ifdef HAVE_GTK_MULTIHEAD
97 # define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
98 gtk_widget_get_display(gui.mainwin), atom)
99 #else
100 # define GET_X_ATOM(atom) ((Atom)(atom))
101 #endif
103 /* Selection type distinguishers */
104 enum
106 TARGET_TYPE_NONE,
107 TARGET_UTF8_STRING,
108 TARGET_STRING,
109 TARGET_COMPOUND_TEXT,
110 TARGET_HTML,
111 TARGET_TEXT,
112 TARGET_TEXT_URI_LIST,
113 TARGET_TEXT_PLAIN,
114 TARGET_VIM,
115 TARGET_VIMENC
119 * Table of selection targets supported by Vim.
120 * Note: Order matters, preferred types should come first.
122 static const GtkTargetEntry selection_targets[] =
124 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
125 {VIM_ATOM_NAME, 0, TARGET_VIM},
126 #ifdef FEAT_MBYTE
127 {"text/html", 0, TARGET_HTML},
128 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
129 #endif
130 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
131 {"TEXT", 0, TARGET_TEXT},
132 {"STRING", 0, TARGET_STRING}
134 #define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
136 #ifdef FEAT_DND
138 * Table of DnD targets supported by Vim.
139 * Note: Order matters, preferred types should come first.
141 static const GtkTargetEntry dnd_targets[] =
143 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
144 # ifdef FEAT_MBYTE
145 {"text/html", 0, TARGET_HTML},
146 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
147 # endif
148 {"STRING", 0, TARGET_STRING},
149 {"text/plain", 0, TARGET_TEXT_PLAIN}
151 # define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
152 #endif
155 #ifdef HAVE_GTK2
157 * "Monospace" is a standard font alias that should be present
158 * on all proper Pango/fontconfig installations.
160 # define DEFAULT_FONT "Monospace 10"
162 #else /* !HAVE_GTK2 */
164 * This is the single only fixed width font in X11, which seems to be present
165 * on all servers and available in all the variants we need.
167 # define DEFAULT_FONT "-adobe-courier-medium-r-normal-*-14-*-*-*-m-*-*-*"
169 #endif /* !HAVE_GTK2 */
171 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
173 * Atoms used to communicate save-yourself from the X11 session manager. There
174 * is no need to move them into the GUI struct, since they should be constant.
176 static GdkAtom wm_protocols_atom = GDK_NONE;
177 static GdkAtom save_yourself_atom = GDK_NONE;
178 #endif
181 * Atoms used to control/reference X11 selections.
183 #ifdef FEAT_MBYTE
184 static GdkAtom html_atom = GDK_NONE;
185 static GdkAtom utf8_string_atom = GDK_NONE;
186 #endif
187 #ifndef HAVE_GTK2
188 static GdkAtom compound_text_atom = GDK_NONE;
189 static GdkAtom text_atom = GDK_NONE;
190 #endif
191 static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
192 #ifdef FEAT_MBYTE
193 static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
194 #endif
197 * Keycodes recognized by vim.
198 * NOTE: when changing this, the table in gui_x11.c probably needs the same
199 * change!
201 static struct special_key
203 guint key_sym;
204 char_u code0;
205 char_u code1;
207 const special_keys[] =
209 {GDK_Up, 'k', 'u'},
210 {GDK_Down, 'k', 'd'},
211 {GDK_Left, 'k', 'l'},
212 {GDK_Right, 'k', 'r'},
213 {GDK_F1, 'k', '1'},
214 {GDK_F2, 'k', '2'},
215 {GDK_F3, 'k', '3'},
216 {GDK_F4, 'k', '4'},
217 {GDK_F5, 'k', '5'},
218 {GDK_F6, 'k', '6'},
219 {GDK_F7, 'k', '7'},
220 {GDK_F8, 'k', '8'},
221 {GDK_F9, 'k', '9'},
222 {GDK_F10, 'k', ';'},
223 {GDK_F11, 'F', '1'},
224 {GDK_F12, 'F', '2'},
225 {GDK_F13, 'F', '3'},
226 {GDK_F14, 'F', '4'},
227 {GDK_F15, 'F', '5'},
228 {GDK_F16, 'F', '6'},
229 {GDK_F17, 'F', '7'},
230 {GDK_F18, 'F', '8'},
231 {GDK_F19, 'F', '9'},
232 {GDK_F20, 'F', 'A'},
233 {GDK_F21, 'F', 'B'},
234 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
235 {GDK_F22, 'F', 'C'},
236 {GDK_F23, 'F', 'D'},
237 {GDK_F24, 'F', 'E'},
238 {GDK_F25, 'F', 'F'},
239 {GDK_F26, 'F', 'G'},
240 {GDK_F27, 'F', 'H'},
241 {GDK_F28, 'F', 'I'},
242 {GDK_F29, 'F', 'J'},
243 {GDK_F30, 'F', 'K'},
244 {GDK_F31, 'F', 'L'},
245 {GDK_F32, 'F', 'M'},
246 {GDK_F33, 'F', 'N'},
247 {GDK_F34, 'F', 'O'},
248 {GDK_F35, 'F', 'P'},
249 #ifdef SunXK_F36
250 {SunXK_F36, 'F', 'Q'},
251 {SunXK_F37, 'F', 'R'},
252 #endif
253 {GDK_Help, '%', '1'},
254 {GDK_Undo, '&', '8'},
255 {GDK_BackSpace, 'k', 'b'},
256 {GDK_Insert, 'k', 'I'},
257 {GDK_Delete, 'k', 'D'},
258 {GDK_3270_BackTab, 'k', 'B'},
259 {GDK_Clear, 'k', 'C'},
260 {GDK_Home, 'k', 'h'},
261 {GDK_End, '@', '7'},
262 {GDK_Prior, 'k', 'P'},
263 {GDK_Next, 'k', 'N'},
264 {GDK_Print, '%', '9'},
265 /* Keypad keys: */
266 {GDK_KP_Left, 'k', 'l'},
267 {GDK_KP_Right, 'k', 'r'},
268 {GDK_KP_Up, 'k', 'u'},
269 {GDK_KP_Down, 'k', 'd'},
270 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
271 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
272 {GDK_KP_Home, 'K', '1'},
273 {GDK_KP_End, 'K', '4'},
274 {GDK_KP_Prior, 'K', '3'}, /* page up */
275 {GDK_KP_Next, 'K', '5'}, /* page down */
277 {GDK_KP_Add, 'K', '6'},
278 {GDK_KP_Subtract, 'K', '7'},
279 {GDK_KP_Divide, 'K', '8'},
280 {GDK_KP_Multiply, 'K', '9'},
281 {GDK_KP_Enter, 'K', 'A'},
282 {GDK_KP_Decimal, 'K', 'B'},
284 {GDK_KP_0, 'K', 'C'},
285 {GDK_KP_1, 'K', 'D'},
286 {GDK_KP_2, 'K', 'E'},
287 {GDK_KP_3, 'K', 'F'},
288 {GDK_KP_4, 'K', 'G'},
289 {GDK_KP_5, 'K', 'H'},
290 {GDK_KP_6, 'K', 'I'},
291 {GDK_KP_7, 'K', 'J'},
292 {GDK_KP_8, 'K', 'K'},
293 {GDK_KP_9, 'K', 'L'},
295 /* End of list marker: */
296 {0, 0, 0}
300 * Flags for command line options table below.
302 #define ARG_FONT 1
303 #define ARG_GEOMETRY 2
304 #define ARG_REVERSE 3
305 #define ARG_NOREVERSE 4
306 #define ARG_BACKGROUND 5
307 #define ARG_FOREGROUND 6
308 #define ARG_ICONIC 7
309 #define ARG_ROLE 8
310 #define ARG_NETBEANS 9
311 #define ARG_XRM 10 /* ignored */
312 #define ARG_MENUFONT 11 /* ignored */
313 #define ARG_INDEX_MASK 0x00ff
314 #define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
315 #define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
316 #define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
317 #define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
318 #define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
321 * This table holds all the X GUI command line options allowed. This includes
322 * the standard ones so that we can skip them when Vim is started without the
323 * GUI (but the GUI might start up later).
325 * When changing this, also update doc/gui_x11.txt and the usage message!!!
327 typedef struct
329 const char *name;
330 unsigned int flags;
332 cmdline_option_T;
334 static const cmdline_option_T cmdline_options[] =
336 /* We handle these options ourselves */
337 {"-fn", ARG_FONT|ARG_HAS_VALUE},
338 {"-font", ARG_FONT|ARG_HAS_VALUE},
339 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
340 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
341 {"-rv", ARG_REVERSE},
342 {"-reverse", ARG_REVERSE},
343 {"+rv", ARG_NOREVERSE},
344 {"+reverse", ARG_NOREVERSE},
345 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
346 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
347 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
348 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
349 {"-iconic", ARG_ICONIC},
350 #ifdef HAVE_GTK2
351 {"--role", ARG_ROLE|ARG_HAS_VALUE},
352 #endif
353 #ifdef FEAT_NETBEANS_INTG
354 {"-nb", ARG_NETBEANS}, /* non-standard value format */
355 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
356 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
357 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
358 #endif
359 #if 0 /* not implemented; these arguments don't make sense for GTK+ */
360 {"-boldfont", ARG_HAS_VALUE},
361 {"-italicfont", ARG_HAS_VALUE},
362 {"-bw", ARG_HAS_VALUE},
363 {"-borderwidth", ARG_HAS_VALUE},
364 {"-sw", ARG_HAS_VALUE},
365 {"-scrollbarwidth", ARG_HAS_VALUE},
366 #endif
367 /* Arguments handled by GTK (and GNOME) internally. */
368 {"--g-fatal-warnings", ARG_FOR_GTK},
369 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
370 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
371 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
372 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
373 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
374 {"--sync", ARG_FOR_GTK},
375 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
376 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
377 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
378 #ifdef HAVE_GTK2
379 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
380 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
381 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
382 #else /* these don't seem to exist anymore */
383 {"--no-xshm", ARG_FOR_GTK},
384 {"--xim-preedit", ARG_FOR_GTK|ARG_HAS_VALUE},
385 {"--xim-status", ARG_FOR_GTK|ARG_HAS_VALUE},
386 {"--gxid_host", ARG_FOR_GTK|ARG_HAS_VALUE},
387 {"--gxid_port", ARG_FOR_GTK|ARG_HAS_VALUE},
388 #endif
389 #ifdef FEAT_GUI_GNOME
390 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
391 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
392 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
393 {"--sm-disable", ARG_FOR_GTK},
394 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
395 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
396 {"--oaf-private", ARG_FOR_GTK},
397 {"--enable-sound", ARG_FOR_GTK},
398 {"--disable-sound", ARG_FOR_GTK},
399 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
400 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
401 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
402 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
403 # if 0 /* conflicts with Vim's own --version argument */
404 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
405 # endif
406 {"--disable-crash-dialog", ARG_FOR_GTK},
407 #endif
408 {NULL, 0}
411 static int gui_argc = 0;
412 static char **gui_argv = NULL;
414 #ifdef HAVE_GTK2
415 static const char *role_argument = NULL;
416 #endif
417 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
418 static const char *restart_command = NULL;
419 static char *abs_restart_command = NULL;
420 #endif
421 static int found_iconic_arg = FALSE;
423 #ifdef FEAT_GUI_GNOME
425 * Can't use Gnome if --socketid given
427 static int using_gnome = 0;
428 #else
429 # define using_gnome 0
430 #endif
433 * Parse the GUI related command-line arguments. Any arguments used are
434 * deleted from argv, and *argc is decremented accordingly. This is called
435 * when vim is started, whether or not the GUI has been started.
437 void
438 gui_mch_prepare(int *argc, char **argv)
440 const cmdline_option_T *option;
441 int i = 0;
442 int len = 0;
444 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
446 * Determine the command used to invoke Vim, to be passed as restart
447 * command to the session manager. If argv[0] contains any directory
448 * components try building an absolute path, otherwise leave it as is.
450 restart_command = argv[0];
452 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
454 char_u buf[MAXPATHL];
456 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
458 abs_restart_command = (char *)vim_strsave(buf);
459 restart_command = abs_restart_command;
462 #endif
465 * Move all the entries in argv which are relevant to GTK+ and GNOME
466 * into gui_argv. Freed later in gui_mch_init().
468 gui_argc = 0;
469 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
471 g_return_if_fail(gui_argv != NULL);
473 gui_argv[gui_argc++] = argv[i++];
475 while (i < *argc)
477 /* Don't waste CPU cycles on non-option arguments. */
478 if (argv[i][0] != '-' && argv[i][0] != '+')
480 ++i;
481 continue;
484 /* Look for argv[i] in cmdline_options[] table. */
485 for (option = &cmdline_options[0]; option->name != NULL; ++option)
487 len = strlen(option->name);
489 if (strncmp(argv[i], option->name, len) == 0)
491 if (argv[i][len] == '\0')
492 break;
493 /* allow --foo=bar style */
494 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
495 break;
496 #ifdef FEAT_NETBEANS_INTG
497 /* darn, -nb has non-standard syntax */
498 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
499 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
500 break;
501 #endif
503 else if ((option->flags & ARG_COMPAT_LONG)
504 && strcmp(argv[i], option->name + 1) == 0)
506 /* Replace the standard X arguments "-name" and "-display"
507 * with their GNU-style long option counterparts. */
508 argv[i] = (char *)option->name;
509 break;
512 if (option->name == NULL) /* no match */
514 ++i;
515 continue;
518 if (option->flags & ARG_FOR_GTK)
520 /* Move the argument into gui_argv, which
521 * will later be passed to gtk_init_check() */
522 gui_argv[gui_argc++] = argv[i];
524 else
526 char *value = NULL;
528 /* Extract the option's value if there is one.
529 * Accept both "--foo bar" and "--foo=bar" style. */
530 if (option->flags & ARG_HAS_VALUE)
532 if (argv[i][len] == '=')
533 value = &argv[i][len + 1];
534 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
535 value = argv[i + 1];
538 /* Check for options handled by Vim itself */
539 switch (option->flags & ARG_INDEX_MASK)
541 case ARG_REVERSE:
542 found_reverse_arg = TRUE;
543 break;
544 case ARG_NOREVERSE:
545 found_reverse_arg = FALSE;
546 break;
547 case ARG_FONT:
548 font_argument = value;
549 break;
550 case ARG_GEOMETRY:
551 if (value != NULL)
552 gui.geom = vim_strsave((char_u *)value);
553 break;
554 case ARG_BACKGROUND:
555 background_argument = value;
556 break;
557 case ARG_FOREGROUND:
558 foreground_argument = value;
559 break;
560 case ARG_ICONIC:
561 found_iconic_arg = TRUE;
562 break;
563 #ifdef HAVE_GTK2
564 case ARG_ROLE:
565 role_argument = value; /* used later in gui_mch_open() */
566 break;
567 #endif
568 #ifdef FEAT_NETBEANS_INTG
569 case ARG_NETBEANS:
570 ++usingNetbeans;
571 gui.dofork = FALSE; /* don't fork() when starting GUI */
572 netbeansArg = argv[i];
573 break;
574 #endif
575 default:
576 break;
580 /* These arguments make gnome_program_init() print a message and exit.
581 * Must start the GUI for this, otherwise ":gui" will exit later! */
582 if (option->flags & ARG_NEEDS_GUI)
583 gui.starting = TRUE;
585 if (option->flags & ARG_KEEP)
586 ++i;
587 else
589 /* Remove the flag from the argument vector. */
590 if (--*argc > i)
592 int n_strip = 1;
594 /* Move the argument's value as well, if there is one. */
595 if ((option->flags & ARG_HAS_VALUE)
596 && argv[i][len] != '='
597 && strcmp(argv[i + 1], "--") != 0)
599 ++n_strip;
600 --*argc;
601 if (option->flags & ARG_FOR_GTK)
602 gui_argv[gui_argc++] = argv[i + 1];
605 if (*argc > i)
606 mch_memmove(&argv[i], &argv[i + n_strip],
607 (*argc - i) * sizeof(char *));
609 argv[*argc] = NULL;
613 gui_argv[gui_argc] = NULL;
616 #if defined(EXITFREE) || defined(PROTO)
617 void
618 gui_mch_free_all()
620 vim_free(gui_argv);
621 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
622 vim_free(abs_restart_command);
623 #endif
625 #endif
628 * This should be maybe completely removed.
629 * Doesn't seem possible, since check_copy_area() relies on
630 * this information. --danielk
632 static gint
633 visibility_event(GtkWidget *widget UNUSED,
634 GdkEventVisibility *event,
635 gpointer data UNUSED)
637 gui.visibility = event->state;
639 * When we do an gdk_window_copy_area(), and the window is partially
640 * obscured, we want to receive an event to tell us whether it worked
641 * or not.
643 if (gui.text_gc != NULL)
644 gdk_gc_set_exposures(gui.text_gc,
645 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
646 return FALSE;
650 * Redraw the corresponding portions of the screen.
652 static gint
653 expose_event(GtkWidget *widget UNUSED,
654 GdkEventExpose *event,
655 gpointer data UNUSED)
657 /* Skip this when the GUI isn't set up yet, will redraw later. */
658 if (gui.starting)
659 return FALSE;
661 out_flush(); /* make sure all output has been processed */
662 gui_redraw(event->area.x, event->area.y,
663 event->area.width, event->area.height);
665 /* Clear the border areas if needed */
666 if (event->area.x < FILL_X(0))
667 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
668 if (event->area.y < FILL_Y(0))
669 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
670 if (event->area.x > FILL_X(Columns))
671 gdk_window_clear_area(gui.drawarea->window,
672 FILL_X((int)Columns), 0, 0, 0);
673 if (event->area.y > FILL_Y(Rows))
674 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
676 return FALSE;
679 #ifdef FEAT_CLIENTSERVER
681 * Handle changes to the "Comm" property
683 static gint
684 property_event(GtkWidget *widget,
685 GdkEventProperty *event,
686 gpointer data UNUSED)
688 if (event->type == GDK_PROPERTY_NOTIFY
689 && event->state == (int)GDK_PROPERTY_NEW_VALUE
690 && GDK_WINDOW_XWINDOW(event->window) == commWindow
691 && GET_X_ATOM(event->atom) == commProperty)
693 XEvent xev;
695 /* Translate to XLib */
696 xev.xproperty.type = PropertyNotify;
697 xev.xproperty.atom = commProperty;
698 xev.xproperty.window = commWindow;
699 xev.xproperty.state = PropertyNewValue;
700 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
702 if (gtk_main_level() > 0)
703 gtk_main_quit();
705 return FALSE;
707 #endif
710 /****************************************************************************
711 * Focus handlers:
716 * This is a simple state machine:
717 * BLINK_NONE not blinking at all
718 * BLINK_OFF blinking, cursor is not shown
719 * BLINK_ON blinking, cursor is shown
722 #define BLINK_NONE 0
723 #define BLINK_OFF 1
724 #define BLINK_ON 2
726 static int blink_state = BLINK_NONE;
727 static long_u blink_waittime = 700;
728 static long_u blink_ontime = 400;
729 static long_u blink_offtime = 250;
730 static guint blink_timer = 0;
732 void
733 gui_mch_set_blinking(long waittime, long on, long off)
735 blink_waittime = waittime;
736 blink_ontime = on;
737 blink_offtime = off;
741 * Stop the cursor blinking. Show the cursor if it wasn't shown.
743 void
744 gui_mch_stop_blink(void)
746 if (blink_timer)
748 gtk_timeout_remove(blink_timer);
749 blink_timer = 0;
751 if (blink_state == BLINK_OFF)
752 gui_update_cursor(TRUE, FALSE);
753 blink_state = BLINK_NONE;
756 static gint
757 blink_cb(gpointer data UNUSED)
759 if (blink_state == BLINK_ON)
761 gui_undraw_cursor();
762 blink_state = BLINK_OFF;
763 blink_timer = gtk_timeout_add((guint32)blink_offtime,
764 (GtkFunction) blink_cb, NULL);
766 else
768 gui_update_cursor(TRUE, FALSE);
769 blink_state = BLINK_ON;
770 blink_timer = gtk_timeout_add((guint32)blink_ontime,
771 (GtkFunction) blink_cb, NULL);
774 return FALSE; /* don't happen again */
778 * Start the cursor blinking. If it was already blinking, this restarts the
779 * waiting time and shows the cursor.
781 void
782 gui_mch_start_blink(void)
784 if (blink_timer)
785 gtk_timeout_remove(blink_timer);
786 /* Only switch blinking on if none of the times is zero */
787 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
789 blink_timer = gtk_timeout_add((guint32)blink_waittime,
790 (GtkFunction) blink_cb, NULL);
791 blink_state = BLINK_ON;
792 gui_update_cursor(TRUE, FALSE);
796 static gint
797 enter_notify_event(GtkWidget *widget UNUSED,
798 GdkEventCrossing *event UNUSED,
799 gpointer data UNUSED)
801 if (blink_state == BLINK_NONE)
802 gui_mch_start_blink();
804 /* make sure keyboard input goes there */
805 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
806 gtk_widget_grab_focus(gui.drawarea);
808 return FALSE;
811 static gint
812 leave_notify_event(GtkWidget *widget UNUSED,
813 GdkEventCrossing *event UNUSED,
814 gpointer data UNUSED)
816 if (blink_state != BLINK_NONE)
817 gui_mch_stop_blink();
819 return FALSE;
822 static gint
823 focus_in_event(GtkWidget *widget,
824 GdkEventFocus *event UNUSED,
825 gpointer data UNUSED)
827 gui_focus_change(TRUE);
829 if (blink_state == BLINK_NONE)
830 gui_mch_start_blink();
832 /* make sure keyboard input goes to the draw area (if this is focus for a
833 * window) */
834 if (widget != gui.drawarea)
835 gtk_widget_grab_focus(gui.drawarea);
837 /* make sure the input buffer is read */
838 if (gtk_main_level() > 0)
839 gtk_main_quit();
841 return TRUE;
844 static gint
845 focus_out_event(GtkWidget *widget UNUSED,
846 GdkEventFocus *event UNUSED,
847 gpointer data UNUSED)
849 gui_focus_change(FALSE);
851 if (blink_state != BLINK_NONE)
852 gui_mch_stop_blink();
854 /* make sure the input buffer is read */
855 if (gtk_main_level() > 0)
856 gtk_main_quit();
858 return TRUE;
862 #ifdef HAVE_GTK2
864 * Translate a GDK key value to UTF-8 independently of the current locale.
865 * The output is written to string, which must have room for at least 6 bytes
866 * plus the NUL terminator. Returns the length in bytes.
868 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
869 * of GdkEventKey::string instead. But event->string is evil; see here why:
870 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
872 static int
873 keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
875 int len;
876 guint32 uc;
878 uc = gdk_keyval_to_unicode(keyval);
879 if (uc != 0)
881 /* Check for CTRL-foo */
882 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
884 /* These mappings look arbitrary at the first glance, but in fact
885 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
886 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
887 * more sense and is consistent with usual terminal behaviour). */
888 if (uc >= '@')
889 string[0] = uc & 0x1F;
890 else if (uc == '2')
891 string[0] = NUL;
892 else if (uc >= '3' && uc <= '7')
893 string[0] = uc ^ 0x28;
894 else if (uc == '8')
895 string[0] = BS;
896 else if (uc == '?')
897 string[0] = DEL;
898 else
899 string[0] = uc;
900 len = 1;
902 else
904 /* Translate a normal key to UTF-8. This doesn't work for dead
905 * keys of course, you _have_ to use an input method for that. */
906 len = utf_char2bytes((int)uc, string);
909 else
911 /* Translate keys which are represented by ASCII control codes in Vim.
912 * There are only a few of those; most control keys are translated to
913 * special terminal-like control sequences. */
914 len = 1;
915 switch (keyval)
917 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
918 string[0] = TAB;
919 break;
920 case GDK_Linefeed:
921 string[0] = NL;
922 break;
923 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
924 string[0] = CAR;
925 break;
926 case GDK_Escape:
927 string[0] = ESC;
928 break;
929 default:
930 len = 0;
931 break;
934 string[len] = NUL;
936 return len;
938 #endif /* HAVE_GTK2 */
940 static int
941 modifiers_gdk2vim(guint state)
943 int modifiers = 0;
945 if (state & GDK_SHIFT_MASK)
946 modifiers |= MOD_MASK_SHIFT;
947 if (state & GDK_CONTROL_MASK)
948 modifiers |= MOD_MASK_CTRL;
949 if (state & GDK_MOD1_MASK)
950 modifiers |= MOD_MASK_ALT;
951 if (state & GDK_MOD4_MASK)
952 modifiers |= MOD_MASK_META;
954 return modifiers;
957 static int
958 modifiers_gdk2mouse(guint state)
960 int modifiers = 0;
962 if (state & GDK_SHIFT_MASK)
963 modifiers |= MOUSE_SHIFT;
964 if (state & GDK_CONTROL_MASK)
965 modifiers |= MOUSE_CTRL;
966 if (state & GDK_MOD1_MASK)
967 modifiers |= MOUSE_ALT;
969 return modifiers;
973 * Main keyboard handler:
975 static gint
976 key_press_event(GtkWidget *widget UNUSED,
977 GdkEventKey *event,
978 gpointer data UNUSED)
980 #ifdef HAVE_GTK2
981 /* 256 bytes is way over the top, but for safety let's reduce it only
982 * for GTK+ 2 where we know for sure how large the string might get.
983 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
984 char_u string[32], string2[32];
985 #else
986 char_u string[256], string2[256];
987 #endif
988 guint key_sym;
989 int len;
990 int i;
991 int modifiers;
992 int key;
993 guint state;
994 char_u *s, *d;
996 key_sym = event->keyval;
997 state = event->state;
998 #ifndef HAVE_GTK2 /* deprecated */
999 len = event->length;
1000 g_assert(len <= sizeof(string));
1001 #endif
1003 #ifndef HAVE_GTK2
1005 * It appears as if we always want to consume a key-press (there currently
1006 * aren't any 'return FALSE's), so we always do this: when running in a
1007 * GtkPlug and not a window, we must prevent emission of the key_press
1008 * EVENT from continuing (which is 'beyond' the level of stopping mere
1009 * signals by returning FALSE), otherwise things like tab/cursor-keys are
1010 * processed by the GtkPlug default handler, which moves input focus away
1011 * from us!
1012 * Note: This should no longer be necessary with GTK+ 2.
1014 if (gtk_socket_id != 0)
1015 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1016 #endif
1018 #ifdef FEAT_XIM
1019 if (xim_queue_key_press_event(event, TRUE))
1020 return TRUE;
1021 #endif
1023 #ifdef FEAT_HANGULIN
1024 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1026 hangul_input_state_toggle();
1027 return TRUE;
1029 #endif
1031 #ifdef SunXK_F36
1033 * These keys have bogus lookup strings, and trapping them here is
1034 * easier than trying to XRebindKeysym() on them with every possible
1035 * combination of modifiers.
1037 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1038 len = 0;
1039 else
1040 #endif
1042 #ifdef HAVE_GTK2
1043 len = keyval_to_string(key_sym, state, string2);
1045 /* Careful: convert_input() doesn't handle the NUL character.
1046 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1047 if (len > 1 && input_conv.vc_type != CONV_NONE)
1048 len = convert_input(string2, len, sizeof(string2));
1050 s = string2;
1051 #else
1052 # ifdef FEAT_MBYTE
1053 if (input_conv.vc_type != CONV_NONE)
1055 mch_memmove(string2, event->string, len);
1056 len = convert_input(string2, len, sizeof(string2));
1057 s = string2;
1059 else
1060 # endif
1061 s = (char_u *)event->string;
1062 #endif
1064 d = string;
1065 for (i = 0; i < len; ++i)
1067 *d++ = s[i];
1068 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1070 /* Turn CSI into K_CSI. */
1071 *d++ = KS_EXTRA;
1072 *d++ = (int)KE_CSI;
1075 len = d - string;
1078 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1079 if (key_sym == GDK_ISO_Left_Tab)
1081 key_sym = GDK_Tab;
1082 state |= GDK_SHIFT_MASK;
1085 #ifndef HAVE_GTK2 /* for GTK+ 2, we handle this in keyval_to_string() */
1086 if ((key_sym == GDK_2 || key_sym == GDK_at) && (state & GDK_CONTROL_MASK))
1088 string[0] = NUL; /* CTRL-2 and CTRL-@ is NUL */
1089 len = 1;
1091 else if (len == 0 && (key_sym == GDK_space || key_sym == GDK_Tab))
1093 /* When there are modifiers, these keys get zero length; we need the
1094 * original key here to be able to add a modifier below. */
1095 string[0] = (key_sym & 0xff);
1096 len = 1;
1098 #endif
1100 #ifdef FEAT_MENU
1101 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1102 * is a menu shortcut, we ignore everything with the ALT modifier. */
1103 if ((state & GDK_MOD1_MASK)
1104 && gui.menu_is_active
1105 && (*p_wak == 'y'
1106 || (*p_wak == 'm'
1107 && len == 1
1108 && gui_is_menu_shortcut(string[0]))))
1109 # ifdef HAVE_GTK2
1110 /* For GTK2 we return false to signify that we haven't handled the
1111 * keypress, so that gtk will handle the mnemonic or accelerator. */
1112 return FALSE;
1113 # else
1114 return TRUE;
1115 # endif
1116 #endif
1118 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1119 * that already has the 8th bit set.
1120 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1121 * Don't do this for double-byte encodings, it turns the char into a lead
1122 * byte. */
1123 if (len == 1
1124 && (state & GDK_MOD1_MASK)
1125 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1126 && (string[0] & 0x80) == 0
1127 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
1128 #ifdef FEAT_MBYTE
1129 && !enc_dbcs
1130 #endif
1133 string[0] |= 0x80;
1134 state &= ~GDK_MOD1_MASK; /* don't use it again */
1135 #ifdef FEAT_MBYTE
1136 if (enc_utf8) /* convert to utf-8 */
1138 string[1] = string[0] & 0xbf;
1139 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1140 if (string[1] == CSI)
1142 string[2] = KS_EXTRA;
1143 string[3] = (int)KE_CSI;
1144 len = 4;
1146 else
1147 len = 2;
1149 #endif
1152 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1153 * value) to detect backspace, delete and keypad keys. */
1154 if (len == 0 || len == 1)
1156 for (i = 0; special_keys[i].key_sym != 0; i++)
1158 if (special_keys[i].key_sym == key_sym)
1160 string[0] = CSI;
1161 string[1] = special_keys[i].code0;
1162 string[2] = special_keys[i].code1;
1163 len = -3;
1164 break;
1169 if (len == 0) /* Unrecognized key */
1170 return TRUE;
1172 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) && !defined(HAVE_GTK2)
1173 /* Cancel or type backspace. For GTK2, im_commit_cb() does the same. */
1174 preedit_start_col = MAXCOL;
1175 xim_changed_while_preediting = TRUE;
1176 #endif
1178 /* Special keys (and a few others) may have modifiers. Also when using a
1179 * double-byte encoding (can't set the 8th bit). */
1180 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1181 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1182 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1183 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
1184 #ifdef FEAT_MBYTE
1185 || (enc_dbcs && len == 1 && (state & GDK_MOD1_MASK))
1186 #endif
1189 modifiers = modifiers_gdk2vim(state);
1192 * For some keys a shift modifier is translated into another key
1193 * code.
1195 if (len == -3)
1196 key = TO_SPECIAL(string[1], string[2]);
1197 else
1198 key = string[0];
1200 key = simplify_key(key, &modifiers);
1201 if (key == CSI)
1202 key = K_CSI;
1203 if (IS_SPECIAL(key))
1205 string[0] = CSI;
1206 string[1] = K_SECOND(key);
1207 string[2] = K_THIRD(key);
1208 len = 3;
1210 else
1212 string[0] = key;
1213 len = 1;
1216 if (modifiers != 0)
1218 string2[0] = CSI;
1219 string2[1] = KS_MODIFIER;
1220 string2[2] = modifiers;
1221 add_to_input_buf(string2, 3);
1225 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1226 || (string[0] == intr_char && intr_char != Ctrl_C)))
1228 trash_input_buf();
1229 got_int = TRUE;
1232 add_to_input_buf(string, len);
1234 /* blank out the pointer if necessary */
1235 if (p_mh)
1236 gui_mch_mousehide(TRUE);
1238 if (gtk_main_level() > 0)
1239 gtk_main_quit();
1241 return TRUE;
1244 #if defined(FEAT_XIM) && defined(HAVE_GTK2)
1245 static gboolean
1246 key_release_event(GtkWidget *widget UNUSED,
1247 GdkEventKey *event,
1248 gpointer data UNUSED)
1251 * GTK+ 2 input methods may do fancy stuff on key release events too.
1252 * With the default IM for instance, you can enter any UCS code point
1253 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1255 return xim_queue_key_press_event(event, FALSE);
1257 #endif
1260 /****************************************************************************
1261 * Selection handlers:
1264 static gint
1265 selection_clear_event(GtkWidget *widget UNUSED,
1266 GdkEventSelection *event,
1267 gpointer user_data UNUSED)
1269 if (event->selection == clip_plus.gtk_sel_atom)
1270 clip_lose_selection(&clip_plus);
1271 else
1272 clip_lose_selection(&clip_star);
1274 if (gtk_main_level() > 0)
1275 gtk_main_quit();
1277 return TRUE;
1280 #define RS_NONE 0 /* selection_received_cb() not called yet */
1281 #define RS_OK 1 /* selection_received_cb() called and OK */
1282 #define RS_FAIL 2 /* selection_received_cb() called and failed */
1283 static int received_selection = RS_NONE;
1285 static void
1286 selection_received_cb(GtkWidget *widget UNUSED,
1287 GtkSelectionData *data,
1288 guint time_ UNUSED,
1289 gpointer user_data UNUSED)
1291 VimClipboard *cbd;
1292 char_u *text;
1293 char_u *tmpbuf = NULL;
1294 #ifdef HAVE_GTK2
1295 guchar *tmpbuf_utf8 = NULL;
1296 #endif
1297 int len;
1298 int motion_type;
1300 if (data->selection == clip_plus.gtk_sel_atom)
1301 cbd = &clip_plus;
1302 else
1303 cbd = &clip_star;
1305 text = (char_u *)data->data;
1306 len = data->length;
1307 motion_type = MCHAR;
1309 if (text == NULL || len <= 0)
1311 received_selection = RS_FAIL;
1312 /* clip_free_selection(cbd); ??? */
1314 if (gtk_main_level() > 0)
1315 gtk_main_quit();
1317 return;
1320 if (data->type == vim_atom)
1322 motion_type = *text++;
1323 --len;
1326 #ifdef FEAT_MBYTE
1327 else if (data->type == vimenc_atom)
1329 char_u *enc;
1330 vimconv_T conv;
1332 motion_type = *text++;
1333 --len;
1335 enc = text;
1336 text += STRLEN(text) + 1;
1337 len -= text - enc;
1339 /* If the encoding of the text is different from 'encoding', attempt
1340 * converting it. */
1341 conv.vc_type = CONV_NONE;
1342 convert_setup(&conv, enc, p_enc);
1343 if (conv.vc_type != CONV_NONE)
1345 tmpbuf = string_convert(&conv, text, &len);
1346 if (tmpbuf != NULL)
1347 text = tmpbuf;
1348 convert_setup(&conv, NULL, NULL);
1351 #endif
1353 #ifdef HAVE_GTK2
1354 /* gtk_selection_data_get_text() handles all the nasty details
1355 * and targets and encodings etc. This rocks so hard. */
1356 else
1358 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1359 if (tmpbuf_utf8 != NULL)
1361 len = STRLEN(tmpbuf_utf8);
1362 if (input_conv.vc_type != CONV_NONE)
1364 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1365 if (tmpbuf != NULL)
1366 text = tmpbuf;
1368 else
1369 text = tmpbuf_utf8;
1371 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1373 vimconv_T conv;
1375 /* UTF-16, we get this for HTML */
1376 conv.vc_type = CONV_NONE;
1377 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1379 if (conv.vc_type != CONV_NONE)
1381 text += 2;
1382 len -= 2;
1383 tmpbuf = string_convert(&conv, text, &len);
1384 convert_setup(&conv, NULL, NULL);
1386 if (tmpbuf != NULL)
1387 text = tmpbuf;
1390 #else /* !HAVE_GTK2 */
1391 # ifdef FEAT_MBYTE
1392 else if (data->type == utf8_string_atom)
1394 vimconv_T conv;
1396 conv.vc_type = CONV_NONE;
1397 convert_setup(&conv, (char_u *)"utf-8", p_enc);
1399 if (conv.vc_type != CONV_NONE)
1401 tmpbuf = string_convert(&conv, text, &len);
1402 convert_setup(&conv, NULL, NULL);
1404 if (tmpbuf != NULL)
1405 text = tmpbuf;
1407 # endif
1408 else if (data->type == compound_text_atom || data->type == text_atom)
1410 char **list = NULL;
1411 int count;
1412 int i;
1413 unsigned tmplen = 0;
1415 count = gdk_text_property_to_text_list(data->type, data->format,
1416 data->data, data->length,
1417 &list);
1418 for (i = 0; i < count; ++i)
1419 tmplen += strlen(list[i]);
1421 tmpbuf = alloc(tmplen + 1);
1422 if (tmpbuf != NULL)
1424 tmpbuf[0] = NUL;
1425 for (i = 0; i < count; ++i)
1426 STRCAT(tmpbuf, list[i]);
1427 text = tmpbuf;
1428 len = tmplen;
1431 if (list != NULL)
1432 gdk_free_text_list(list);
1434 #endif /* !HAVE_GTK2 */
1436 /* Chop off any traiing NUL bytes. OpenOffice sends these. */
1437 while (len > 0 && text[len - 1] == NUL)
1438 --len;
1440 clip_yank_selection(motion_type, text, (long)len, cbd);
1441 received_selection = RS_OK;
1442 vim_free(tmpbuf);
1443 #ifdef HAVE_GTK2
1444 g_free(tmpbuf_utf8);
1445 #endif
1447 if (gtk_main_level() > 0)
1448 gtk_main_quit();
1452 * Prepare our selection data for passing it to the external selection
1453 * client.
1455 static void
1456 selection_get_cb(GtkWidget *widget UNUSED,
1457 GtkSelectionData *selection_data,
1458 guint info,
1459 guint time_ UNUSED,
1460 gpointer user_data UNUSED)
1462 char_u *string;
1463 char_u *tmpbuf;
1464 long_u tmplen;
1465 int length;
1466 int motion_type;
1467 GdkAtom type;
1468 VimClipboard *cbd;
1470 if (selection_data->selection == clip_plus.gtk_sel_atom)
1471 cbd = &clip_plus;
1472 else
1473 cbd = &clip_star;
1475 if (!cbd->owned)
1476 return; /* Shouldn't ever happen */
1478 if (info != (guint)TARGET_STRING
1479 #ifdef FEAT_MBYTE
1480 && (!clip_html || info != (guint)TARGET_HTML)
1481 && info != (guint)TARGET_UTF8_STRING
1482 && info != (guint)TARGET_VIMENC
1483 #endif
1484 && info != (guint)TARGET_VIM
1485 && info != (guint)TARGET_COMPOUND_TEXT
1486 && info != (guint)TARGET_TEXT)
1487 return;
1489 /* get the selection from the '*'/'+' register */
1490 clip_get_selection(cbd);
1492 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1493 if (motion_type < 0 || string == NULL)
1494 return;
1495 /* Due to int arguments we can't handle more than G_MAXINT. Also
1496 * reserve one extra byte for NUL or the motion type; just in case.
1497 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1498 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1500 if (info == (guint)TARGET_VIM)
1502 tmpbuf = alloc((unsigned)length + 1);
1503 if (tmpbuf != NULL)
1505 tmpbuf[0] = motion_type;
1506 mch_memmove(tmpbuf + 1, string, (size_t)length);
1508 /* For our own format, the first byte contains the motion type */
1509 ++length;
1510 vim_free(string);
1511 string = tmpbuf;
1512 type = vim_atom;
1515 #ifdef FEAT_MBYTE
1516 else if (info == (guint)TARGET_HTML)
1518 vimconv_T conv;
1520 /* Since we get utf-16, we probably should set it as well. */
1521 conv.vc_type = CONV_NONE;
1522 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1523 if (conv.vc_type != CONV_NONE)
1525 tmpbuf = string_convert(&conv, string, &length);
1526 convert_setup(&conv, NULL, NULL);
1527 vim_free(string);
1528 string = tmpbuf;
1531 /* Prepend the BOM: "fffe" */
1532 if (string != NULL)
1534 tmpbuf = alloc(length + 2);
1535 tmpbuf[0] = 0xff;
1536 tmpbuf[1] = 0xfe;
1537 mch_memmove(tmpbuf + 2, string, (size_t)length);
1538 vim_free(string);
1539 string = tmpbuf;
1540 length += 2;
1542 selection_data->type = selection_data->target;
1543 selection_data->format = 16; /* 16 bits per char */
1544 gtk_selection_data_set(selection_data, html_atom, 16,
1545 string, length);
1546 vim_free(string);
1548 return;
1550 else if (info == (guint)TARGET_VIMENC)
1552 int l = STRLEN(p_enc);
1554 /* contents: motion_type 'encoding' NUL text */
1555 tmpbuf = alloc((unsigned)length + l + 2);
1556 if (tmpbuf != NULL)
1558 tmpbuf[0] = motion_type;
1559 STRCPY(tmpbuf + 1, p_enc);
1560 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1562 length += l + 2;
1563 vim_free(string);
1564 string = tmpbuf;
1565 type = vimenc_atom;
1567 #endif
1569 #ifdef HAVE_GTK2
1570 /* gtk_selection_data_set_text() handles everything for us. This is
1571 * so easy and simple and cool, it'd be insane not to use it. */
1572 else
1574 if (output_conv.vc_type != CONV_NONE)
1576 tmpbuf = string_convert(&output_conv, string, &length);
1577 vim_free(string);
1578 if (tmpbuf == NULL)
1579 return;
1580 string = tmpbuf;
1582 /* Validate the string to avoid runtime warnings */
1583 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1585 gtk_selection_data_set_text(selection_data,
1586 (const char *)string, length);
1588 vim_free(string);
1589 return;
1591 #else /* !HAVE_GTK2 */
1592 # ifdef FEAT_MBYTE
1593 else if (info == (guint)TARGET_UTF8_STRING)
1595 vimconv_T conv;
1597 conv.vc_type = CONV_NONE;
1598 convert_setup(&conv, p_enc, (char_u *)"utf-8");
1600 if (conv.vc_type != CONV_NONE)
1602 tmpbuf = string_convert(&conv, string, &length);
1603 convert_setup(&conv, NULL, NULL);
1604 vim_free(string);
1605 string = tmpbuf;
1607 type = utf8_string_atom;
1609 # endif
1610 else if (info == (guint)TARGET_COMPOUND_TEXT
1611 || info == (guint)TARGET_TEXT)
1613 int format;
1615 /* Copy the string to ensure NUL-termination */
1616 tmpbuf = vim_strnsave(string, length);
1617 vim_free(string);
1618 if (tmpbuf != NULL)
1620 gdk_string_to_compound_text((const char *)tmpbuf,
1621 &type, &format, &string, &length);
1622 vim_free(tmpbuf);
1623 selection_data->type = type;
1624 selection_data->format = format;
1625 gtk_selection_data_set(selection_data, type, format, string, length);
1626 gdk_free_compound_text(string);
1628 return;
1630 else
1632 type = GDK_TARGET_STRING;
1634 #endif /* !HAVE_GTK2 */
1636 if (string != NULL)
1638 selection_data->type = selection_data->target;
1639 selection_data->format = 8; /* 8 bits per char */
1641 gtk_selection_data_set(selection_data, type, 8, string, length);
1642 vim_free(string);
1647 * Check if the GUI can be started. Called before gvimrc is sourced.
1648 * Return OK or FAIL.
1651 gui_mch_init_check(void)
1653 #ifndef HAVE_GTK2
1654 /* This is needed to make the locale handling consistent between the GUI
1655 * and the rest of VIM. */
1656 gtk_set_locale();
1657 #endif
1659 #ifdef FEAT_GUI_GNOME
1660 if (gtk_socket_id == 0)
1661 using_gnome = 1;
1662 #endif
1664 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1665 if (!gtk_init_check(&gui_argc, &gui_argv))
1667 gui.dying = TRUE;
1668 EMSG(_((char *)e_opendisp));
1669 return FAIL;
1672 return OK;
1676 /****************************************************************************
1677 * Mouse handling callbacks
1681 static guint mouse_click_timer = 0;
1682 static int mouse_timed_out = TRUE;
1685 * Timer used to recognize multiple clicks of the mouse button
1687 static gint
1688 mouse_click_timer_cb(gpointer data)
1690 /* we don't use this information currently */
1691 int *timed_out = (int *) data;
1693 *timed_out = TRUE;
1694 return FALSE; /* don't happen again */
1697 static guint motion_repeat_timer = 0;
1698 static int motion_repeat_offset = FALSE;
1699 static gint motion_repeat_timer_cb(gpointer);
1701 static void
1702 process_motion_notify(int x, int y, GdkModifierType state)
1704 int button;
1705 int_u vim_modifiers;
1707 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1708 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1709 GDK_BUTTON5_MASK))
1710 ? MOUSE_DRAG : ' ';
1712 /* If our pointer is currently hidden, then we should show it. */
1713 gui_mch_mousehide(FALSE);
1715 /* Just moving the rodent above the drawing area without any button
1716 * being pressed. */
1717 if (button != MOUSE_DRAG)
1719 gui_mouse_moved(x, y);
1720 return;
1723 /* translate modifier coding between the main engine and GTK */
1724 vim_modifiers = modifiers_gdk2mouse(state);
1726 /* inform the editor engine about the occurrence of this event */
1727 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1729 if (gtk_main_level() > 0)
1730 gtk_main_quit();
1733 * Auto repeat timer handling.
1735 if (x < 0 || y < 0
1736 || x >= gui.drawarea->allocation.width
1737 || y >= gui.drawarea->allocation.height)
1740 int dx;
1741 int dy;
1742 int offshoot;
1743 int delay = 10;
1745 /* Calculate the maximal distance of the cursor from the drawing area.
1746 * (offshoot can't become negative here!).
1748 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1749 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1751 offshoot = dx > dy ? dx : dy;
1753 /* Make a linearly decaying timer delay with a threshold of 5 at a
1754 * distance of 127 pixels from the main window.
1756 * One could think endlessly about the most ergonomic variant here.
1757 * For example it could make sense to calculate the distance from the
1758 * drags start instead...
1760 * Maybe a parabolic interpolation would suite us better here too...
1762 if (offshoot > 127)
1764 /* 5 appears to be somehow near to my perceptual limits :-). */
1765 delay = 5;
1767 else
1769 delay = (130 * (127 - offshoot)) / 127 + 5;
1772 /* shoot again */
1773 if (!motion_repeat_timer)
1774 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1775 motion_repeat_timer_cb, NULL);
1780 * Timer used to recognize multiple clicks of the mouse button.
1782 static gint
1783 motion_repeat_timer_cb(gpointer data UNUSED)
1785 int x;
1786 int y;
1787 GdkModifierType state;
1789 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1791 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1792 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1793 GDK_BUTTON5_MASK)))
1795 motion_repeat_timer = 0;
1796 return FALSE;
1799 /* If there already is a mouse click in the input buffer, wait another
1800 * time (otherwise we would create a backlog of clicks) */
1801 if (vim_used_in_input_buf() > 10)
1802 return TRUE;
1804 motion_repeat_timer = 0;
1807 * Fake a motion event.
1808 * Trick: Pretend the mouse moved to the next character on every other
1809 * event, otherwise drag events will be discarded, because they are still
1810 * in the same character.
1812 if (motion_repeat_offset)
1813 x += gui.char_width;
1815 motion_repeat_offset = !motion_repeat_offset;
1816 process_motion_notify(x, y, state);
1818 /* Don't happen again. We will get reinstalled in the synthetic event
1819 * if needed -- thus repeating should still work. */
1820 return FALSE;
1823 static gint
1824 motion_notify_event(GtkWidget *widget,
1825 GdkEventMotion *event,
1826 gpointer data UNUSED)
1828 if (event->is_hint)
1830 int x;
1831 int y;
1832 GdkModifierType state;
1834 gdk_window_get_pointer(widget->window, &x, &y, &state);
1835 process_motion_notify(x, y, state);
1837 else
1839 process_motion_notify((int)event->x, (int)event->y,
1840 (GdkModifierType)event->state);
1843 return TRUE; /* handled */
1848 * Mouse button handling. Note please that we are capturing multiple click's
1849 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1850 * This is due to the way the generic VIM code is recognizing multiple clicks.
1852 static gint
1853 button_press_event(GtkWidget *widget,
1854 GdkEventButton *event,
1855 gpointer data UNUSED)
1857 int button;
1858 int repeated_click = FALSE;
1859 int x, y;
1860 int_u vim_modifiers;
1862 /* Make sure we have focus now we've been selected */
1863 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1864 gtk_widget_grab_focus(widget);
1867 * Don't let additional events about multiple clicks send by GTK to us
1868 * after the initial button press event confuse us.
1870 if (event->type != GDK_BUTTON_PRESS)
1871 return FALSE;
1873 x = event->x;
1874 y = event->y;
1876 /* Handle multiple clicks */
1877 if (!mouse_timed_out && mouse_click_timer)
1879 gtk_timeout_remove(mouse_click_timer);
1880 mouse_click_timer = 0;
1881 repeated_click = TRUE;
1884 mouse_timed_out = FALSE;
1885 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1886 mouse_click_timer_cb, &mouse_timed_out);
1888 switch (event->button)
1890 case 1:
1891 button = MOUSE_LEFT;
1892 break;
1893 case 2:
1894 button = MOUSE_MIDDLE;
1895 break;
1896 case 3:
1897 button = MOUSE_RIGHT;
1898 break;
1899 #ifndef HAVE_GTK2
1900 case 4:
1901 button = MOUSE_4;
1902 break;
1903 case 5:
1904 button = MOUSE_5;
1905 break;
1906 #endif
1907 default:
1908 return FALSE; /* Unknown button */
1911 #ifdef FEAT_XIM
1912 /* cancel any preediting */
1913 if (im_is_preediting())
1914 xim_reset();
1915 #endif
1917 vim_modifiers = modifiers_gdk2mouse(event->state);
1919 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1920 if (gtk_main_level() > 0)
1921 gtk_main_quit(); /* make sure the above will be handled immediately */
1923 return TRUE;
1926 #ifdef HAVE_GTK2
1928 * GTK+ 2 doesn't handle mouse buttons 4, 5, 6 and 7 the same way as GTK+ 1.
1929 * Instead, it abstracts scrolling via the new GdkEventScroll.
1931 static gboolean
1932 scroll_event(GtkWidget *widget,
1933 GdkEventScroll *event,
1934 gpointer data UNUSED)
1936 int button;
1937 int_u vim_modifiers;
1939 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1940 gtk_widget_grab_focus(widget);
1942 switch (event->direction)
1944 case GDK_SCROLL_UP:
1945 button = MOUSE_4;
1946 break;
1947 case GDK_SCROLL_DOWN:
1948 button = MOUSE_5;
1949 break;
1950 default: /* We don't care about left and right... Yet. */
1951 return FALSE;
1954 # ifdef FEAT_XIM
1955 /* cancel any preediting */
1956 if (im_is_preediting())
1957 xim_reset();
1958 # endif
1960 vim_modifiers = modifiers_gdk2mouse(event->state);
1962 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1963 FALSE, vim_modifiers);
1965 if (gtk_main_level() > 0)
1966 gtk_main_quit(); /* make sure the above will be handled immediately */
1968 return TRUE;
1970 #endif /* HAVE_GTK2 */
1973 static gint
1974 button_release_event(GtkWidget *widget UNUSED,
1975 GdkEventButton *event,
1976 gpointer data UNUSED)
1978 int x, y;
1979 int_u vim_modifiers;
1981 /* Remove any motion "machine gun" timers used for automatic further
1982 extension of allocation areas if outside of the applications window
1983 area .*/
1984 if (motion_repeat_timer)
1986 gtk_timeout_remove(motion_repeat_timer);
1987 motion_repeat_timer = 0;
1990 x = event->x;
1991 y = event->y;
1993 vim_modifiers = modifiers_gdk2mouse(event->state);
1995 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
1996 if (gtk_main_level() > 0)
1997 gtk_main_quit(); /* make sure it will be handled immediately */
1999 return TRUE;
2003 #ifdef FEAT_DND
2004 /****************************************************************************
2005 * Drag aNd Drop support handlers.
2009 * Count how many items there may be and separate them with a NUL.
2010 * Apparently the items are separated with \r\n. This is not documented,
2011 * thus be careful not to go past the end. Also allow separation with
2012 * NUL characters.
2014 static int
2015 count_and_decode_uri_list(char_u *out, char_u *raw, int len)
2017 int i;
2018 char_u *p = out;
2019 int count = 0;
2021 for (i = 0; i < len; ++i)
2023 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
2025 if (p > out && p[-1] != NUL)
2027 ++count;
2028 *p++ = NUL;
2031 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
2033 *p++ = hexhex2nr(raw + i + 1);
2034 i += 2;
2036 else
2037 *p++ = raw[i];
2039 if (p > out && p[-1] != NUL)
2041 *p = NUL; /* last item didn't have \r or \n */
2042 ++count;
2044 return count;
2048 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2049 * this process, URI which protocol is not "file:" are removed. Return
2050 * length of array (less than "max").
2052 static int
2053 filter_uri_list(char_u **outlist, int max, char_u *src)
2055 int i, j;
2057 for (i = j = 0; i < max; ++i)
2059 outlist[i] = NULL;
2060 if (STRNCMP(src, "file:", 5) == 0)
2062 src += 5;
2063 if (STRNCMP(src, "//localhost", 11) == 0)
2064 src += 11;
2065 while (src[0] == '/' && src[1] == '/')
2066 ++src;
2067 outlist[j++] = vim_strsave(src);
2069 src += STRLEN(src) + 1;
2071 return j;
2074 static char_u **
2075 parse_uri_list(int *count, char_u *data, int len)
2077 int n = 0;
2078 char_u *tmp = NULL;
2079 char_u **array = NULL;;
2081 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2083 n = count_and_decode_uri_list(tmp, data, len);
2084 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2085 n = filter_uri_list(array, n, tmp);
2087 vim_free(tmp);
2088 *count = n;
2089 return array;
2092 static void
2093 drag_handle_uri_list(GdkDragContext *context,
2094 GtkSelectionData *data,
2095 guint time_,
2096 GdkModifierType state,
2097 gint x,
2098 gint y)
2100 char_u **fnames;
2101 int nfiles = 0;
2103 fnames = parse_uri_list(&nfiles, data->data, data->length);
2105 if (fnames != NULL && nfiles > 0)
2107 int_u modifiers;
2109 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2111 modifiers = modifiers_gdk2mouse(state);
2113 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2115 else
2116 vim_free(fnames);
2119 static void
2120 drag_handle_text(GdkDragContext *context,
2121 GtkSelectionData *data,
2122 guint time_,
2123 GdkModifierType state)
2125 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2126 char_u *text;
2127 int len;
2128 # ifdef FEAT_MBYTE
2129 char_u *tmpbuf = NULL;
2130 # endif
2132 text = data->data;
2133 len = data->length;
2135 # ifdef FEAT_MBYTE
2136 if (data->type == utf8_string_atom)
2138 # ifdef HAVE_GTK2
2139 if (input_conv.vc_type != CONV_NONE)
2140 tmpbuf = string_convert(&input_conv, text, &len);
2141 # else
2142 vimconv_T conv;
2144 conv.vc_type = CONV_NONE;
2145 convert_setup(&conv, (char_u *)"utf-8", p_enc);
2147 if (conv.vc_type != CONV_NONE)
2149 tmpbuf = string_convert(&conv, text, &len);
2150 convert_setup(&conv, NULL, NULL);
2152 # endif
2153 if (tmpbuf != NULL)
2154 text = tmpbuf;
2156 # endif /* FEAT_MBYTE */
2158 dnd_yank_drag_data(text, (long)len);
2159 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2160 # ifdef FEAT_MBYTE
2161 vim_free(tmpbuf);
2162 # endif
2164 dropkey[2] = modifiers_gdk2vim(state);
2166 if (dropkey[2] != 0)
2167 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2168 else
2169 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
2171 if (gtk_main_level() > 0)
2172 gtk_main_quit();
2176 * DND receiver.
2178 static void
2179 drag_data_received_cb(GtkWidget *widget,
2180 GdkDragContext *context,
2181 gint x,
2182 gint y,
2183 GtkSelectionData *data,
2184 guint info,
2185 guint time_,
2186 gpointer user_data UNUSED)
2188 GdkModifierType state;
2190 /* Guard against trash */
2191 if (data->data == NULL
2192 || data->length <= 0
2193 || data->format != 8
2194 || data->data[data->length] != '\0')
2196 gtk_drag_finish(context, FALSE, FALSE, time_);
2197 return;
2200 /* Get the current modifier state for proper distinguishment between
2201 * different operations later. */
2202 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
2204 /* Not sure about the role of "text/plain" here... */
2205 if (info == (guint)TARGET_TEXT_URI_LIST)
2206 drag_handle_uri_list(context, data, time_, state, x, y);
2207 else
2208 drag_handle_text(context, data, time_, state);
2211 #endif /* FEAT_DND */
2214 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2216 * GnomeClient interact callback. Check for unsaved buffers that cannot
2217 * be abandoned and pop up a dialog asking the user for confirmation if
2218 * necessary.
2220 static void
2221 sm_client_check_changed_any(GnomeClient *client,
2222 gint key,
2223 GnomeDialogType type,
2224 gpointer data)
2226 cmdmod_T save_cmdmod;
2227 gboolean shutdown_cancelled;
2229 save_cmdmod = cmdmod;
2231 # ifdef FEAT_BROWSE
2232 cmdmod.browse = TRUE;
2233 # endif
2234 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2235 cmdmod.confirm = TRUE;
2236 # endif
2238 * If there are changed buffers, present the user with
2239 * a dialog if possible, otherwise give an error message.
2241 shutdown_cancelled = check_changed_any(FALSE);
2243 exiting = FALSE;
2244 cmdmod = save_cmdmod;
2245 setcursor(); /* position the cursor */
2246 out_flush();
2248 * If the user hit the [Cancel] button the whole shutdown
2249 * will be cancelled. Wow, quite powerful feature (:
2251 gnome_interaction_key_return(key, shutdown_cancelled);
2255 * Generate a script that can be used to restore the current editing session.
2256 * Save the value of v:this_session before running :mksession in order to make
2257 * automagic session save fully transparent. Return TRUE on success.
2259 static int
2260 write_session_file(char_u *filename)
2262 char_u *escaped_filename;
2263 char *mksession_cmdline;
2264 unsigned int save_ssop_flags;
2265 int failed;
2268 * Build an ex command line to create a script that restores the current
2269 * session if executed. Escape the filename to avoid nasty surprises.
2271 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2272 if (escaped_filename == NULL)
2273 return FALSE;
2274 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2275 NULL);
2276 vim_free(escaped_filename);
2279 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2280 * unpredictable effects when the session is saved automatically. Also,
2281 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2282 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2283 * enormously large if the GNOME session feature is used regularly.
2285 save_ssop_flags = ssop_flags;
2286 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
2287 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
2289 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2290 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2291 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
2292 do_unlet((char_u *)"Save_VV_this_session", TRUE);
2294 ssop_flags = save_ssop_flags;
2295 g_free(mksession_cmdline);
2297 * Reopen the file and append a command to restore v:this_session,
2298 * as if this save never happened. This is to avoid conflicts with
2299 * the user's own sessions. FIXME: It's probably less hackish to add
2300 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2302 if (!failed)
2304 FILE *fd;
2306 fd = open_exfile(filename, TRUE, APPENDBIN);
2308 failed = (fd == NULL
2309 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2310 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2312 if (fd != NULL && fclose(fd) != 0)
2313 failed = TRUE;
2315 if (failed)
2316 mch_remove(filename);
2319 return !failed;
2323 * "save_yourself" signal handler. Initiate an interaction to ask the user
2324 * for confirmation if necessary. Save the current editing session and tell
2325 * the session manager how to restart Vim.
2327 static gboolean
2328 sm_client_save_yourself(GnomeClient *client,
2329 gint phase,
2330 GnomeSaveStyle save_style,
2331 gboolean shutdown,
2332 GnomeInteractStyle interact_style,
2333 gboolean fast,
2334 gpointer data)
2336 static const char suffix[] = "-session.vim";
2337 char *session_file;
2338 unsigned int len;
2339 gboolean success;
2341 /* Always request an interaction if possible. check_changed_any()
2342 * won't actually show a dialog unless any buffers have been modified.
2343 * There doesn't seem to be an obvious way to check that without
2344 * automatically firing the dialog. Anyway, it works just fine. */
2345 if (interact_style == GNOME_INTERACT_ANY)
2346 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2347 &sm_client_check_changed_any,
2348 NULL);
2349 out_flush();
2350 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2352 /* The path is unique for each session save. We do neither know nor care
2353 * which session script will actually be used later. This decision is in
2354 * the domain of the session manager. */
2355 session_file = gnome_config_get_real_path(
2356 gnome_client_get_config_prefix(client));
2357 len = strlen(session_file);
2359 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2360 --len; /* get rid of the superfluous trailing '/' */
2362 session_file = g_renew(char, session_file, len + sizeof(suffix));
2363 memcpy(session_file + len, suffix, sizeof(suffix));
2365 success = write_session_file((char_u *)session_file);
2367 if (success)
2369 const char *argv[8];
2370 int i;
2372 /* Tell the session manager how to wipe out the stored session data.
2373 * This isn't as dangerous as it looks, don't worry :) session_file
2374 * is a unique absolute filename. Usually it'll be something like
2375 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2376 i = 0;
2377 argv[i++] = "rm";
2378 argv[i++] = session_file;
2379 argv[i] = NULL;
2381 gnome_client_set_discard_command(client, i, (char **)argv);
2383 /* Tell the session manager how to restore the just saved session.
2384 * This is easily done thanks to Vim's -S option. Pass the -f flag
2385 * since there's no need to fork -- it might even cause confusion.
2386 * Also pass the window role to give the WM something to match on.
2387 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2388 i = 0;
2389 argv[i++] = restart_command;
2390 argv[i++] = "-f";
2391 argv[i++] = "-g";
2392 # ifdef HAVE_GTK2
2393 argv[i++] = "--role";
2394 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
2395 # endif
2396 argv[i++] = "-S";
2397 argv[i++] = session_file;
2398 argv[i] = NULL;
2400 gnome_client_set_restart_command(client, i, (char **)argv);
2401 gnome_client_set_clone_command(client, 0, NULL);
2404 g_free(session_file);
2406 return success;
2410 * Called when the session manager wants us to die. There isn't much to save
2411 * here since "save_yourself" has been emitted before (unless serious trouble
2412 * is happening).
2414 static void
2415 sm_client_die(GnomeClient *client, gpointer data)
2417 /* Don't write messages to the GUI anymore */
2418 full_screen = FALSE;
2420 vim_strncpy(IObuff, (char_u *)
2421 _("Vim: Received \"die\" request from session manager\n"),
2422 IOSIZE - 1);
2423 preserve_exit();
2427 * Connect our signal handlers to be notified on session save and shutdown.
2429 static void
2430 setup_save_yourself(void)
2432 GnomeClient *client;
2434 client = gnome_master_client();
2436 if (client != NULL)
2438 /* Must use the deprecated gtk_signal_connect() for compatibility
2439 * with GNOME 1. Arrgh, zombies! */
2440 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2441 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2442 gtk_signal_connect(GTK_OBJECT(client), "die",
2443 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2447 #else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2449 # ifdef USE_XSMP
2451 * GTK tells us that XSMP needs attention
2453 static gboolean
2454 local_xsmp_handle_requests(source, condition, data)
2455 GIOChannel *source UNUSED;
2456 GIOCondition condition;
2457 gpointer data;
2459 if (condition == G_IO_IN)
2461 /* Do stuff; maybe close connection */
2462 if (xsmp_handle_requests() == FAIL)
2463 g_io_channel_unref((GIOChannel *)data);
2464 return TRUE;
2466 /* Error */
2467 g_io_channel_unref((GIOChannel *)data);
2468 xsmp_close();
2469 return TRUE;
2471 # endif /* USE_XSMP */
2474 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2475 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2477 static void
2478 setup_save_yourself(void)
2480 Atom *existing_atoms = NULL;
2481 int count = 0;
2483 #ifdef USE_XSMP
2484 if (xsmp_icefd != -1)
2487 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2488 * set up GTK IO monitor
2490 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2492 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2493 local_xsmp_handle_requests, (gpointer)g_io);
2495 else
2496 #endif
2498 /* Fall back to old method */
2500 /* first get the existing value */
2501 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2502 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2503 &existing_atoms, &count))
2505 Atom *new_atoms;
2506 Atom save_yourself_xatom;
2507 int i;
2509 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2511 /* check if WM_SAVE_YOURSELF isn't there yet */
2512 for (i = 0; i < count; ++i)
2513 if (existing_atoms[i] == save_yourself_xatom)
2514 break;
2516 if (i == count)
2518 /* allocate an Atoms array which is one item longer */
2519 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2520 * sizeof(Atom)));
2521 if (new_atoms != NULL)
2523 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2524 new_atoms[count] = save_yourself_xatom;
2525 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2526 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2527 new_atoms, count + 1);
2528 vim_free(new_atoms);
2531 XFree(existing_atoms);
2536 # ifdef HAVE_GTK2
2538 * Installing a global event filter seems to be the only way to catch
2539 * client messages of type WM_PROTOCOLS without overriding GDK's own
2540 * client message event filter. Well, that's still better than trying
2541 * to guess what the GDK filter had done if it had been invoked instead
2542 * (This is what we did for GTK+ 1.2, see below).
2544 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2545 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2546 * I have the nasty feeling modern session managers just don't send this
2547 * deprecated message anymore. Addition: confirmed by several people.
2549 * The GNOME session support is much cooler anyway. Unlike this ugly
2550 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2551 * it should work with KDE as well.
2553 static GdkFilterReturn
2554 global_event_filter(GdkXEvent *xev,
2555 GdkEvent *event UNUSED,
2556 gpointer data UNUSED)
2558 XEvent *xevent = (XEvent *)xev;
2560 if (xevent != NULL
2561 && xevent->type == ClientMessage
2562 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
2563 && (long_u)xevent->xclient.data.l[0]
2564 == GET_X_ATOM(save_yourself_atom))
2566 out_flush();
2567 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2569 * Set the window's WM_COMMAND property, to let the window manager
2570 * know we are done saving ourselves. We don't want to be
2571 * restarted, thus set argv to NULL.
2573 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2574 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2575 NULL, 0);
2576 return GDK_FILTER_REMOVE;
2579 return GDK_FILTER_CONTINUE;
2582 # else /* !HAVE_GTK2 */
2585 * GDK handler for X ClientMessage events.
2587 static GdkFilterReturn
2588 gdk_wm_protocols_filter(GdkXEvent *xev, GdkEvent *event, gpointer data)
2590 /* From example in gdkevents.c/gdk_wm_protocols_filter */
2591 XEvent *xevent = (XEvent *)xev;
2593 if (xevent != NULL)
2595 if (xevent->xclient.data.l[0] == GET_X_ATOM(save_yourself_atom))
2597 out_flush();
2598 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2600 /* Set the window's WM_COMMAND property, to let the window manager
2601 * know we are done saving ourselves. We don't want to be
2602 * restarted, thus set argv to NULL. */
2603 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2604 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2605 NULL, 0);
2608 * Functionality from gdkevents.c/gdk_wm_protocols_filter;
2609 * Registering this filter apparently overrides the default GDK one,
2610 * so we need to perform its functionality. There seems no way to
2611 * register for WM_PROTOCOLS, and only process the WM_SAVE_YOURSELF
2612 * bit; it's all or nothing. Update: No, there is a way -- but it
2613 * only works with GTK+ 2 apparently. See above.
2615 else if (xevent->xclient.data.l[0] == GET_X_ATOM(gdk_wm_delete_window))
2617 event->any.type = GDK_DELETE;
2618 return GDK_FILTER_TRANSLATE;
2622 return GDK_FILTER_REMOVE;
2624 # endif /* !HAVE_GTK2 */
2626 #endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2630 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2632 static void
2633 mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
2635 /* If you get an error message here, you still need to unpack the runtime
2636 * archive! */
2637 #ifdef magick
2638 # undef magick
2639 #endif
2640 #ifdef HAVE_GTK2
2641 /* A bit hackish, but avoids casting later and allows optimization */
2642 # define static static const
2643 #endif
2644 #define magick vim32x32
2645 #include "../runtime/vim32x32.xpm"
2646 #undef magick
2647 #define magick vim16x16
2648 #include "../runtime/vim16x16.xpm"
2649 #undef magick
2650 #define magick vim48x48
2651 #include "../runtime/vim48x48.xpm"
2652 #undef magick
2653 #ifdef HAVE_GTK2
2654 # undef static
2655 #endif
2657 /* When started with "--echo-wid" argument, write window ID on stdout. */
2658 if (echo_wid_arg)
2660 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2661 fflush(stdout);
2664 if (vim_strchr(p_go, GO_ICON) != NULL)
2667 * Add an icon to the main window. For fun and convenience of the user.
2669 #ifdef HAVE_GTK2
2670 GList *icons = NULL;
2672 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2673 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2674 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2676 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2678 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2679 g_list_free(icons);
2681 #else /* !HAVE_GTK2 */
2683 GdkPixmap *icon;
2684 GdkBitmap *icon_mask = NULL;
2685 char **magick = vim32x32;
2686 Display *xdisplay;
2687 Window root_window;
2688 XIconSize *size;
2689 int number_sizes;
2691 * Adjust the icon to the preferences of the actual window manager.
2692 * This is once again a workaround for a deficiency in GTK+ 1.2.
2694 xdisplay = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
2695 root_window = XRootWindow(xdisplay, DefaultScreen(xdisplay));
2696 if (XGetIconSizes(xdisplay, root_window, &size, &number_sizes))
2698 if (number_sizes > 0)
2700 if (size->max_height >= 48 && size->max_height >= 48)
2701 magick = vim48x48;
2702 else if (size->max_height >= 32 && size->max_height >= 32)
2703 magick = vim32x32;
2704 else if (size->max_height >= 16 && size->max_height >= 16)
2705 magick = vim16x16;
2707 XFree(size);
2709 icon = gdk_pixmap_create_from_xpm_d(gui.mainwin->window,
2710 &icon_mask, NULL, magick);
2711 if (icon != NULL)
2712 /* Note: for some reason gdk_window_set_icon() doesn't acquire
2713 * a reference on the pixmap, thus we _have_ to leak it. */
2714 gdk_window_set_icon(gui.mainwin->window, NULL, icon, icon_mask);
2716 #endif /* !HAVE_GTK2 */
2719 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2720 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
2721 # ifdef HAVE_GTK2
2722 gdk_window_add_filter(NULL, &global_event_filter, NULL);
2723 # else
2724 gdk_add_client_message_filter(wm_protocols_atom,
2725 &gdk_wm_protocols_filter, NULL);
2726 # endif
2727 #endif
2728 /* Setup to indicate to the window manager that we want to catch the
2729 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2730 * manager instead. */
2731 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2732 if (using_gnome)
2733 #endif
2734 setup_save_yourself();
2736 #ifdef FEAT_CLIENTSERVER
2737 if (serverName == NULL && serverDelayedStartName != NULL)
2739 /* This is a :gui command in a plain vim with no previous server */
2740 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2742 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2743 serverDelayedStartName);
2745 else
2748 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2749 * have to change the "server" registration to that of the main window
2750 * If we have not registered a name yet, remember the window
2752 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2753 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2755 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2756 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2757 GTK_SIGNAL_FUNC(property_event), NULL);
2758 #endif
2761 static GdkCursor *
2762 create_blank_pointer(void)
2764 GdkWindow *root_window = NULL;
2765 GdkPixmap *blank_mask;
2766 GdkCursor *cursor;
2767 GdkColor color = { 0, 0, 0, 0 };
2768 char blank_data[] = { 0x0 };
2770 #ifdef HAVE_GTK_MULTIHEAD
2771 root_window = gtk_widget_get_root_window(gui.mainwin);
2772 #endif
2774 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2775 * in size. */
2776 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2777 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2778 &color, &color, 0, 0);
2779 gdk_bitmap_unref(blank_mask);
2781 return cursor;
2784 #ifdef HAVE_GTK_MULTIHEAD
2785 static void
2786 mainwin_screen_changed_cb(GtkWidget *widget,
2787 GdkScreen *previous_screen UNUSED,
2788 gpointer data UNUSED)
2790 if (!gtk_widget_has_screen(widget))
2791 return;
2794 * Recreate the invisible mouse cursor.
2796 if (gui.blank_pointer != NULL)
2797 gdk_cursor_unref(gui.blank_pointer);
2799 gui.blank_pointer = create_blank_pointer();
2801 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2802 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2805 * Create a new PangoContext for this screen, and initialize it
2806 * with the current font if necessary.
2808 if (gui.text_context != NULL)
2809 g_object_unref(gui.text_context);
2811 gui.text_context = gtk_widget_create_pango_context(widget);
2812 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2814 if (gui.norm_font != NULL)
2816 gui_mch_init_font(p_guifont, FALSE);
2817 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
2820 #endif /* HAVE_GTK_MULTIHEAD */
2823 * After the drawing area comes up, we calculate all colors and create the
2824 * dummy blank cursor.
2826 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2827 * fact that the main VIM engine doesn't take them into account anywhere.
2829 static void
2830 drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
2832 GtkWidget *sbar;
2834 #ifdef FEAT_XIM
2835 xim_init();
2836 #endif
2837 gui_mch_new_colors();
2838 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2840 gui.blank_pointer = create_blank_pointer();
2841 if (gui.pointer_hidden)
2842 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2844 /* get the actual size of the scrollbars, if they are realized */
2845 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2846 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2847 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2848 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2849 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2850 gui.scrollbar_width = sbar->allocation.width;
2852 sbar = gui.bottom_sbar.id;
2853 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2854 gui.scrollbar_height = sbar->allocation.height;
2858 * Properly clean up on shutdown.
2860 static void
2861 drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
2863 /* Don't write messages to the GUI anymore */
2864 full_screen = FALSE;
2866 #ifdef FEAT_XIM
2867 im_shutdown();
2868 #endif
2869 #ifdef HAVE_GTK2
2870 if (gui.ascii_glyphs != NULL)
2872 pango_glyph_string_free(gui.ascii_glyphs);
2873 gui.ascii_glyphs = NULL;
2875 if (gui.ascii_font != NULL)
2877 g_object_unref(gui.ascii_font);
2878 gui.ascii_font = NULL;
2880 g_object_unref(gui.text_context);
2881 gui.text_context = NULL;
2883 g_object_unref(gui.text_gc);
2884 gui.text_gc = NULL;
2886 gdk_cursor_unref(gui.blank_pointer);
2887 gui.blank_pointer = NULL;
2888 #else
2889 gdk_gc_unref(gui.text_gc);
2890 gui.text_gc = NULL;
2892 gdk_cursor_destroy(gui.blank_pointer);
2893 gui.blank_pointer = NULL;
2894 #endif
2897 static void
2898 drawarea_style_set_cb(GtkWidget *widget UNUSED,
2899 GtkStyle *previous_style UNUSED,
2900 gpointer data UNUSED)
2902 gui_mch_new_colors();
2906 * Callback routine for the "delete_event" signal on the toplevel window.
2907 * Tries to vim gracefully, or refuses to exit with changed buffers.
2909 static gint
2910 delete_event_cb(GtkWidget *widget UNUSED,
2911 GdkEventAny *event UNUSED,
2912 gpointer data UNUSED)
2914 gui_shell_closed();
2915 return TRUE;
2918 #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2919 static int
2920 get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2922 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2924 #ifdef FEAT_GUI_GNOME
2925 if (using_gnome && widget != NULL)
2927 # ifdef HAVE_GTK2
2928 GtkWidget *parent;
2929 BonoboDockItem *dockitem;
2931 parent = gtk_widget_get_parent(widget);
2932 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2934 /* Only menu & toolbar are dock items. Could tabline be?
2935 * Seem to be only the 2 defined in GNOME */
2936 widget = parent;
2937 dockitem = BONOBO_DOCK_ITEM(widget);
2939 if (dockitem == NULL || dockitem->is_floating)
2940 return 0;
2941 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2943 # else
2944 GnomeDockItem *dockitem;
2946 widget = widget->parent;
2947 dockitem = GNOME_DOCK_ITEM(widget);
2949 if (dockitem == NULL || dockitem->is_floating)
2950 return 0;
2951 item_orientation = gnome_dock_item_get_orientation(dockitem);
2952 # endif
2954 #endif
2955 if (widget != NULL
2956 && item_orientation == orientation
2957 && GTK_WIDGET_REALIZED(widget)
2958 && GTK_WIDGET_VISIBLE(widget))
2960 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2961 return widget->allocation.height;
2962 else
2963 return widget->allocation.width;
2965 return 0;
2967 #endif
2969 static int
2970 get_menu_tool_width(void)
2972 int width = 0;
2974 #ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2975 # ifdef FEAT_MENU
2976 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2977 # endif
2978 # ifdef FEAT_TOOLBAR
2979 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2980 # endif
2981 # ifdef FEAT_GUI_TABLINE
2982 if (gui.tabline != NULL)
2983 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
2984 # endif
2985 #endif
2987 return width;
2990 static int
2991 get_menu_tool_height(void)
2993 int height = 0;
2995 #ifdef FEAT_MENU
2996 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2997 #endif
2998 #ifdef FEAT_TOOLBAR
2999 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
3000 #endif
3001 #ifdef FEAT_GUI_TABLINE
3002 if (gui.tabline != NULL)
3003 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
3004 #endif
3006 return height;
3009 /* This controls whether we can set the real window hints at
3010 * start-up when in a GtkPlug.
3011 * 0 = normal processing (default)
3012 * 1 = init. hints set, no-one's tried to reset since last check
3013 * 2 = init. hints set, attempt made to change hints
3015 static int init_window_hints_state = 0;
3017 static void
3018 update_window_manager_hints(int force_width, int force_height)
3020 static int old_width = 0;
3021 static int old_height = 0;
3022 static int old_min_width = 0;
3023 static int old_min_height = 0;
3024 static int old_char_width = 0;
3025 static int old_char_height = 0;
3027 int width;
3028 int height;
3029 int min_width;
3030 int min_height;
3032 /* At start-up, don't try to set the hints until the initial
3033 * values have been used (those that dictate our initial size)
3034 * Let forced (i.e., correct) values through always.
3036 if (!(force_width && force_height) && init_window_hints_state > 0)
3038 /* Don't do it! */
3039 init_window_hints_state = 2;
3040 return;
3043 /* This also needs to be done when the main window isn't there yet,
3044 * otherwise the hints don't work. */
3045 width = gui_get_base_width();
3046 height = gui_get_base_height();
3047 # ifdef FEAT_MENU
3048 height += tabline_height() * gui.char_height;
3049 # endif
3050 # ifdef HAVE_GTK2
3051 width += get_menu_tool_width();
3052 height += get_menu_tool_height();
3053 # endif
3055 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
3056 * their actual widget size. When we set our size ourselves (e.g.,
3057 * 'set columns=' or init. -geom) we briefly set the min. to the size
3058 * we wish to be instead of the legitimate minimum so that we actually
3059 * resize correctly.
3061 if (force_width && force_height)
3063 min_width = force_width;
3064 min_height = force_height;
3066 else
3068 min_width = width + MIN_COLUMNS * gui.char_width;
3069 min_height = height + MIN_LINES * gui.char_height;
3072 /* Avoid an expose event when the size didn't change. */
3073 if (width != old_width
3074 || height != old_height
3075 || min_width != old_min_width
3076 || min_height != old_min_height
3077 || gui.char_width != old_char_width
3078 || gui.char_height != old_char_height)
3080 GdkGeometry geometry;
3081 GdkWindowHints geometry_mask;
3083 geometry.width_inc = gui.char_width;
3084 geometry.height_inc = gui.char_height;
3085 geometry.base_width = width;
3086 geometry.base_height = height;
3087 geometry.min_width = min_width;
3088 geometry.min_height = min_height;
3089 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3090 |GDK_HINT_MIN_SIZE;
3091 # ifdef HAVE_GTK2
3092 /* Using gui.formwin as geometry widget doesn't work as expected
3093 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3094 * in Vim confuse GTK+. */
3095 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3096 &geometry, geometry_mask);
3097 # else
3098 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.formwin,
3099 &geometry, geometry_mask);
3100 # endif
3101 old_width = width;
3102 old_height = height;
3103 old_min_width = min_width;
3104 old_min_height = min_height;
3105 old_char_width = gui.char_width;
3106 old_char_height = gui.char_height;
3110 #ifdef FEAT_TOOLBAR
3112 # ifdef HAVE_GTK2
3114 * This extra effort wouldn't be necessary if we only used stock icons in the
3115 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3116 * shouldn't be treated differently, thus we do need this.
3118 static void
3119 icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3121 if (GTK_IS_IMAGE(widget))
3123 GtkImage *image = (GtkImage *)widget;
3125 /* User-defined icons are stored in a GtkIconSet */
3126 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3128 GtkIconSet *icon_set;
3129 GtkIconSize icon_size;
3131 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3132 icon_size = (GtkIconSize)(long)user_data;
3134 gtk_icon_set_ref(icon_set);
3135 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3136 gtk_icon_set_unref(icon_set);
3139 else if (GTK_IS_CONTAINER(widget))
3141 gtk_container_foreach((GtkContainer *)widget,
3142 &icon_size_changed_foreach,
3143 user_data);
3146 # endif /* HAVE_GTK2 */
3148 static void
3149 set_toolbar_style(GtkToolbar *toolbar)
3151 GtkToolbarStyle style;
3152 # ifdef HAVE_GTK2
3153 GtkIconSize size;
3154 GtkIconSize oldsize;
3155 # endif
3157 # ifdef HAVE_GTK2
3158 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3159 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3160 style = GTK_TOOLBAR_BOTH_HORIZ;
3161 else
3162 # endif
3163 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
3164 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3165 style = GTK_TOOLBAR_BOTH;
3166 else if (toolbar_flags & TOOLBAR_TEXT)
3167 style = GTK_TOOLBAR_TEXT;
3168 else
3169 style = GTK_TOOLBAR_ICONS;
3171 gtk_toolbar_set_style(toolbar, style);
3172 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
3174 # ifdef HAVE_GTK2
3175 switch (tbis_flags)
3177 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3178 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3179 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3180 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
3181 default: size = GTK_ICON_SIZE_INVALID; break;
3183 oldsize = gtk_toolbar_get_icon_size(toolbar);
3185 if (size == GTK_ICON_SIZE_INVALID)
3187 /* Let global user preferences decide the icon size. */
3188 gtk_toolbar_unset_icon_size(toolbar);
3189 size = gtk_toolbar_get_icon_size(toolbar);
3191 if (size != oldsize)
3193 gtk_container_foreach(GTK_CONTAINER(toolbar),
3194 &icon_size_changed_foreach,
3195 GINT_TO_POINTER((int)size));
3197 gtk_toolbar_set_icon_size(toolbar, size);
3198 # endif
3201 #endif /* FEAT_TOOLBAR */
3203 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3204 static int ignore_tabline_evt = FALSE;
3205 static GtkWidget *tabline_menu;
3206 static GtkTooltips *tabline_tooltip;
3207 static int clicked_page; /* page clicked in tab line */
3210 * Handle selecting an item in the tab line popup menu.
3212 static void
3213 tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
3215 /* Add the string cmd into input buffer */
3216 send_tabline_menu_event(clicked_page, (int)(long)user_data);
3218 if (gtk_main_level() > 0)
3219 gtk_main_quit();
3222 static void
3223 add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3225 GtkWidget *item;
3226 char_u *utf_text;
3228 utf_text = CONVERT_TO_UTF8(text);
3229 item = gtk_menu_item_new_with_label((const char *)utf_text);
3230 gtk_widget_show(item);
3231 CONVERT_TO_UTF8_FREE(utf_text);
3233 gtk_container_add(GTK_CONTAINER(menu), item);
3234 gtk_signal_connect(GTK_OBJECT(item), "activate",
3235 GTK_SIGNAL_FUNC(tabline_menu_handler),
3236 (gpointer)(long)resp);
3240 * Create a menu for the tab line.
3242 static GtkWidget *
3243 create_tabline_menu(void)
3245 GtkWidget *menu;
3247 menu = gtk_menu_new();
3248 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
3249 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3250 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
3252 return menu;
3255 static gboolean
3256 on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3258 /* Was this button press event ? */
3259 if (event->type == GDK_BUTTON_PRESS)
3261 GdkEventButton *bevent = (GdkEventButton *)event;
3262 int x = bevent->x;
3263 int y = bevent->y;
3264 GtkWidget *tabwidget;
3265 GdkWindow *tabwin;
3267 /* When ignoring events return TRUE so that the selected page doesn't
3268 * change. */
3269 if (hold_gui_events
3270 # ifdef FEAT_CMDWIN
3271 || cmdwin_type != 0
3272 # endif
3274 return TRUE;
3276 tabwin = gdk_window_at_pointer(&x, &y);
3277 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
3278 clicked_page = (int)(long)gtk_object_get_user_data(
3279 GTK_OBJECT(tabwidget));
3281 /* If the event was generated for 3rd button popup the menu. */
3282 if (bevent->button == 3)
3284 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3285 bevent->button, bevent->time);
3286 /* We handled the event. */
3287 return TRUE;
3289 else if (bevent->button == 1)
3291 if (clicked_page == 0)
3293 /* Click after all tabs moves to next tab page. When "x" is
3294 * small guess it's the left button. */
3295 if (send_tabline_event(x < 50 ? -1 : 0) && gtk_main_level() > 0)
3296 gtk_main_quit();
3298 #ifndef HAVE_GTK2
3299 else
3300 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline),
3301 clicked_page - 1);
3302 #endif
3306 /* We didn't handle the event. */
3307 return FALSE;
3311 * Handle selecting one of the tabs.
3313 static void
3314 on_select_tab(
3315 GtkNotebook *notebook UNUSED,
3316 GtkNotebookPage *page UNUSED,
3317 gint idx,
3318 gpointer data UNUSED)
3320 if (!ignore_tabline_evt)
3322 if (send_tabline_event(idx + 1) && gtk_main_level() > 0)
3323 gtk_main_quit();
3327 #ifndef HAVE_GTK2
3328 static int showing_tabline = 0;
3329 #endif
3332 * Show or hide the tabline.
3334 void
3335 gui_mch_show_tabline(int showit)
3337 if (gui.tabline == NULL)
3338 return;
3340 #ifdef HAVE_GTK2
3341 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3342 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
3343 #else
3344 if (!showit != !showing_tabline)
3345 #endif
3347 /* Note: this may cause a resize event */
3348 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
3349 update_window_manager_hints(0, 0);
3350 #ifndef HAVE_GTK2
3351 showing_tabline = showit;
3352 #endif
3353 if (showit)
3354 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
3357 gui_mch_update();
3361 * Return TRUE when tabline is displayed.
3364 gui_mch_showing_tabline(void)
3366 return gui.tabline != NULL
3367 #ifdef HAVE_GTK2
3368 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3369 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))
3370 #else
3371 && showing_tabline
3372 #endif
3377 * Update the labels of the tabline.
3379 void
3380 gui_mch_update_tabline(void)
3382 GtkWidget *page;
3383 GtkWidget *event_box;
3384 GtkWidget *label;
3385 tabpage_T *tp;
3386 int nr = 0;
3387 int tab_num;
3388 int curtabidx = 0;
3389 char_u *labeltext;
3391 if (gui.tabline == NULL)
3392 return;
3394 ignore_tabline_evt = TRUE;
3396 /* Add a label for each tab page. They all contain the same text area. */
3397 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3399 if (tp == curtab)
3400 curtabidx = nr;
3402 tab_num = nr + 1;
3404 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3405 if (page == NULL)
3407 /* Add notebook page */
3408 page = gtk_vbox_new(FALSE, 0);
3409 gtk_widget_show(page);
3410 event_box = gtk_event_box_new();
3411 gtk_widget_show(event_box);
3412 label = gtk_label_new("-Empty-");
3413 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
3414 gtk_container_add(GTK_CONTAINER(event_box), label);
3415 gtk_widget_show(label);
3416 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3417 page,
3418 event_box,
3419 nr++);
3422 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
3423 gtk_object_set_user_data(GTK_OBJECT(event_box),
3424 (gpointer)(long)tab_num);
3425 label = GTK_BIN(event_box)->child;
3426 get_tabline_label(tp, FALSE);
3427 labeltext = CONVERT_TO_UTF8(NameBuff);
3428 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
3429 CONVERT_TO_UTF8_FREE(labeltext);
3431 get_tabline_label(tp, TRUE);
3432 labeltext = CONVERT_TO_UTF8(NameBuff);
3433 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3434 (const char *)labeltext, NULL);
3435 CONVERT_TO_UTF8_FREE(labeltext);
3438 /* Remove any old labels. */
3439 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3440 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3442 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
3443 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
3445 /* Make sure everything is in place before drawing text. */
3446 gui_mch_update();
3448 ignore_tabline_evt = FALSE;
3452 * Set the current tab to "nr". First tab is 1.
3454 void
3455 gui_mch_set_curtab(nr)
3456 int nr;
3458 if (gui.tabline == NULL)
3459 return;
3461 ignore_tabline_evt = TRUE;
3462 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
3463 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
3464 ignore_tabline_evt = FALSE;
3467 #endif /* FEAT_GUI_TABLINE */
3470 * Add selection targets for PRIMARY and CLIPBOARD selections.
3472 void
3473 gui_gtk_set_selection_targets(void)
3475 int i, j = 0;
3476 int n_targets = N_SELECTION_TARGETS;
3477 GtkTargetEntry targets[N_SELECTION_TARGETS];
3479 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3481 #if defined(FEAT_MBYTE) && defined(HAVE_GTK2)
3482 /* OpenOffice tries to use TARGET_HTML and fails when it doesn't
3483 * return something, instead of trying another target. Therefore only
3484 * offer TARGET_HTML when it works. */
3485 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3486 n_targets--;
3487 else
3488 #endif
3489 targets[j++] = selection_targets[i];
3492 #ifdef HAVE_GTK2 /* GTK 1 doesn't have this function */
3493 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3494 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3495 #endif
3496 gtk_selection_add_targets(gui.drawarea,
3497 (GdkAtom)GDK_SELECTION_PRIMARY,
3498 targets, n_targets);
3499 gtk_selection_add_targets(gui.drawarea,
3500 (GdkAtom)clip_plus.gtk_sel_atom,
3501 targets, n_targets);
3505 * Set up for receiving DND items.
3507 void
3508 gui_gtk_set_dnd_targets(void)
3510 int i, j = 0;
3511 int n_targets = N_DND_TARGETS;
3512 GtkTargetEntry targets[N_DND_TARGETS];
3514 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3516 #ifdef FEAT_MBYTE
3517 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3518 n_targets--;
3519 else
3520 #endif
3521 targets[j++] = dnd_targets[i];
3524 gtk_drag_dest_unset(gui.drawarea);
3525 gtk_drag_dest_set(gui.drawarea,
3526 GTK_DEST_DEFAULT_ALL,
3527 targets, n_targets,
3528 GDK_ACTION_COPY);
3532 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3533 * Returns OK for success, FAIL when the GUI can't be started.
3536 gui_mch_init(void)
3538 GtkWidget *vbox;
3540 #ifdef FEAT_GUI_GNOME
3541 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3542 * exits on failure, but that's a non-issue because we already called
3543 * gtk_init_check() in gui_mch_init_check(). */
3544 if (using_gnome)
3545 # ifdef HAVE_GTK2
3546 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3547 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
3548 # else
3549 gnome_init(VIMPACKAGE, VIM_VERSION_SHORT, gui_argc, gui_argv);
3550 # endif
3551 #endif
3552 vim_free(gui_argv);
3553 gui_argv = NULL;
3555 #ifdef HAVE_GTK2
3556 # if GLIB_CHECK_VERSION(2,1,3)
3557 /* Set the human-readable application name */
3558 g_set_application_name("Vim");
3559 # endif
3561 * Force UTF-8 output no matter what the value of 'encoding' is.
3562 * did_set_string_option() in option.c prohibits changing 'termencoding'
3563 * to something else than UTF-8 if the GUI is in use.
3565 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3567 # ifdef FEAT_TOOLBAR
3568 gui_gtk_register_stock_icons();
3569 # endif
3570 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3571 * The hard part is deciding install locations and the Makefile magic. */
3572 # if 0
3573 gtk_rc_parse("gtkrc");
3574 # endif
3575 #endif
3577 /* Initialize values */
3578 gui.border_width = 2;
3579 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3580 gui.scrollbar_height = SB_DEFAULT_WIDTH;
3581 /* LINTED: avoid warning: conversion to 'unsigned long' */
3582 gui.fgcolor = g_new0(GdkColor, 1);
3583 /* LINTED: avoid warning: conversion to 'unsigned long' */
3584 gui.bgcolor = g_new0(GdkColor, 1);
3585 /* LINTED: avoid warning: conversion to 'unsigned long' */
3586 gui.spcolor = g_new0(GdkColor, 1);
3588 /* Initialise atoms */
3589 #ifdef FEAT_MBYTE
3590 html_atom = gdk_atom_intern("text/html", FALSE);
3591 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3592 #endif
3593 #ifndef HAVE_GTK2
3594 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3595 text_atom = gdk_atom_intern("TEXT", FALSE);
3596 #endif
3598 /* Set default foreground and background colors. */
3599 gui.norm_pixel = gui.def_norm_pixel;
3600 gui.back_pixel = gui.def_back_pixel;
3602 if (gtk_socket_id != 0)
3604 GtkWidget *plug;
3606 /* Use GtkSocket from another app. */
3607 #ifdef HAVE_GTK_MULTIHEAD
3608 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3609 gtk_socket_id);
3610 #else
3611 plug = gtk_plug_new(gtk_socket_id);
3612 #endif
3613 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3615 gui.mainwin = plug;
3617 else
3619 g_warning("Connection to GTK+ socket (ID %u) failed",
3620 (unsigned int)gtk_socket_id);
3621 /* Pretend we never wanted it if it failed (get own window) */
3622 gtk_socket_id = 0;
3626 if (gtk_socket_id == 0)
3628 #ifdef FEAT_GUI_GNOME
3629 if (using_gnome)
3631 gui.mainwin = gnome_app_new("Vim", NULL);
3632 # ifdef USE_XSMP
3633 /* Use the GNOME save-yourself functionality now. */
3634 xsmp_close();
3635 # endif
3637 else
3638 #endif
3639 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3642 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3644 #ifdef HAVE_GTK2
3645 /* Create the PangoContext used for drawing all text. */
3646 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3647 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
3648 #endif
3650 #ifndef HAVE_GTK2
3651 gtk_window_set_policy(GTK_WINDOW(gui.mainwin), TRUE, TRUE, TRUE);
3652 #endif
3653 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3654 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3656 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3657 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3659 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3660 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3661 #ifdef HAVE_GTK_MULTIHEAD
3662 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3663 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3664 #endif
3665 #ifdef HAVE_GTK2
3666 gui.accel_group = gtk_accel_group_new();
3667 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
3668 #else
3669 gui.accel_group = gtk_accel_group_get_default();
3670 #endif
3672 /* A vertical box holds the menubar, toolbar and main text window. */
3673 vbox = gtk_vbox_new(FALSE, 0);
3675 #ifdef FEAT_GUI_GNOME
3676 if (using_gnome)
3678 # if defined(HAVE_GTK2) && defined(FEAT_MENU)
3679 /* automagically restore menubar/toolbar placement */
3680 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3681 # endif
3682 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3684 else
3685 #endif
3687 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3688 gtk_widget_show(vbox);
3691 #ifdef FEAT_MENU
3693 * Create the menubar and handle
3695 gui.menubar = gtk_menu_bar_new();
3696 gtk_widget_set_name(gui.menubar, "vim-menubar");
3698 # ifdef HAVE_GTK2
3699 /* Avoid that GTK takes <F10> away from us. */
3701 GtkSettings *gtk_settings;
3703 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3704 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3706 # endif
3709 # ifdef FEAT_GUI_GNOME
3710 if (using_gnome)
3712 # ifdef HAVE_GTK2
3713 BonoboDockItem *dockitem;
3715 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3716 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3717 GNOME_APP_MENUBAR_NAME);
3718 /* We don't want the menu to float. */
3719 bonobo_dock_item_set_behavior(dockitem,
3720 bonobo_dock_item_get_behavior(dockitem)
3721 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
3722 gui.menubar_h = GTK_WIDGET(dockitem);
3723 # else
3724 gui.menubar_h = gnome_dock_item_new("VimMainMenu",
3725 GNOME_DOCK_ITEM_BEH_EXCLUSIVE |
3726 GNOME_DOCK_ITEM_BEH_NEVER_VERTICAL);
3727 gtk_container_add(GTK_CONTAINER(gui.menubar_h), gui.menubar);
3729 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3730 GNOME_DOCK_ITEM(gui.menubar_h),
3731 GNOME_DOCK_TOP, /* placement */
3732 1, /* band_num */
3733 0, /* band_position */
3734 0, /* offset */
3735 TRUE);
3736 gtk_widget_show(gui.menubar);
3737 # endif
3739 else
3740 # endif /* FEAT_GUI_GNOME */
3742 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3743 * disabled in gui_init() later. */
3744 gtk_widget_show(gui.menubar);
3745 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3747 #endif /* FEAT_MENU */
3749 #ifdef FEAT_TOOLBAR
3751 * Create the toolbar and handle
3753 # ifdef HAVE_GTK2
3754 /* some aesthetics on the toolbar */
3755 gtk_rc_parse_string(
3756 "style \"vim-toolbar-style\" {\n"
3757 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3758 "}\n"
3759 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3760 gui.toolbar = gtk_toolbar_new();
3761 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
3762 # else
3763 gui.toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3764 GTK_TOOLBAR_ICONS);
3765 gtk_toolbar_set_button_relief(GTK_TOOLBAR(gui.toolbar), GTK_RELIEF_NONE);
3766 # endif
3767 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3769 # ifdef FEAT_GUI_GNOME
3770 if (using_gnome)
3772 # ifdef HAVE_GTK2
3773 BonoboDockItem *dockitem;
3775 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3776 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3777 GNOME_APP_TOOLBAR_NAME);
3778 gui.toolbar_h = GTK_WIDGET(dockitem);
3779 /* When the toolbar is floating it gets stuck. So long as that isn't
3780 * fixed let's disallow floating. */
3781 bonobo_dock_item_set_behavior(dockitem,
3782 bonobo_dock_item_get_behavior(dockitem)
3783 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
3784 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
3785 # else
3786 GtkWidget *dockitem;
3788 dockitem = gnome_dock_item_new("VimToolBar",
3789 GNOME_DOCK_ITEM_BEH_EXCLUSIVE);
3790 gtk_container_add(GTK_CONTAINER(dockitem), GTK_WIDGET(gui.toolbar));
3791 gui.toolbar_h = dockitem;
3793 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3794 GNOME_DOCK_ITEM(dockitem),
3795 GNOME_DOCK_TOP, /* placement */
3796 1, /* band_num */
3797 1, /* band_position */
3798 0, /* offset */
3799 TRUE);
3800 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 2);
3801 gtk_widget_show(gui.toolbar);
3802 # endif
3804 else
3805 # endif /* FEAT_GUI_GNOME */
3807 # ifndef HAVE_GTK2
3808 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 1);
3809 # endif
3810 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3811 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3812 gtk_widget_show(gui.toolbar);
3813 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3815 #endif /* FEAT_TOOLBAR */
3817 #ifdef FEAT_GUI_TABLINE
3819 * Use a Notebook for the tab pages labels. The labels are hidden by
3820 * default.
3822 gui.tabline = gtk_notebook_new();
3823 gtk_widget_show(gui.tabline);
3824 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3825 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3826 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
3827 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
3828 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3830 tabline_tooltip = gtk_tooltips_new();
3831 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3834 GtkWidget *page, *label, *event_box;
3836 /* Add the first tab. */
3837 page = gtk_vbox_new(FALSE, 0);
3838 gtk_widget_show(page);
3839 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3840 label = gtk_label_new("-Empty-");
3841 gtk_widget_show(label);
3842 event_box = gtk_event_box_new();
3843 gtk_widget_show(event_box);
3844 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
3845 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
3846 gtk_container_add(GTK_CONTAINER(event_box), label);
3847 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
3850 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
3851 GTK_SIGNAL_FUNC(on_select_tab), NULL);
3853 /* Create a popup menu for the tab line and connect it. */
3854 tabline_menu = create_tabline_menu();
3855 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
3856 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
3857 #endif
3859 gui.formwin = gtk_form_new();
3860 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3861 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3863 gui.drawarea = gtk_drawing_area_new();
3865 /* Determine which events we will filter. */
3866 gtk_widget_set_events(gui.drawarea,
3867 GDK_EXPOSURE_MASK |
3868 GDK_ENTER_NOTIFY_MASK |
3869 GDK_LEAVE_NOTIFY_MASK |
3870 GDK_BUTTON_PRESS_MASK |
3871 GDK_BUTTON_RELEASE_MASK |
3872 #ifdef HAVE_GTK2
3873 GDK_SCROLL_MASK |
3874 #endif
3875 GDK_KEY_PRESS_MASK |
3876 GDK_KEY_RELEASE_MASK |
3877 GDK_POINTER_MOTION_MASK |
3878 GDK_POINTER_MOTION_HINT_MASK);
3880 gtk_widget_show(gui.drawarea);
3881 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3882 gtk_widget_show(gui.formwin);
3883 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3885 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3886 * and not the window. */
3887 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3888 : GTK_OBJECT(gui.drawarea),
3889 "key_press_event",
3890 GTK_SIGNAL_FUNC(key_press_event), NULL);
3891 #if defined(FEAT_XIM) && defined(HAVE_GTK2)
3892 /* Also forward key release events for the benefit of GTK+ 2 input
3893 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3894 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3895 : G_OBJECT(gui.drawarea),
3896 "key_release_event",
3897 G_CALLBACK(&key_release_event), NULL);
3898 #endif
3899 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3900 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3901 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3902 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3904 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3905 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3907 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3909 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3910 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3911 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3912 #endif
3914 if (gtk_socket_id != 0)
3915 /* make sure keyboard input can go to the drawarea */
3916 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3919 * Set clipboard specific atoms
3921 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
3922 #ifdef FEAT_MBYTE
3923 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
3924 #endif
3925 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3926 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3929 * Start out by adding the configured border width into the border offset.
3931 gui.border_offset = gui.border_width;
3933 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3934 GTK_SIGNAL_FUNC(visibility_event), NULL);
3935 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3936 GTK_SIGNAL_FUNC(expose_event), NULL);
3939 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3940 * Only needed for some window managers.
3942 if (vim_strchr(p_go, GO_POINTER) != NULL)
3944 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3945 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3946 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3947 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3950 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3951 * only its widgets. Arguably, this could be common code and we not use
3952 * the window focus at all, but let's be safe.
3954 if (gtk_socket_id == 0)
3956 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3957 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3958 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3959 GTK_SIGNAL_FUNC(focus_in_event), NULL);
3961 else
3963 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3964 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3965 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3966 GTK_SIGNAL_FUNC(focus_in_event), NULL);
3967 #ifdef FEAT_GUI_TABLINE
3968 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3969 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3970 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3971 GTK_SIGNAL_FUNC(focus_in_event), NULL);
3972 #endif /* FEAT_GUI_TABLINE */
3975 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3976 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3977 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3978 GTK_SIGNAL_FUNC(button_press_event), NULL);
3979 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3980 GTK_SIGNAL_FUNC(button_release_event), NULL);
3981 #ifdef HAVE_GTK2
3982 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3983 G_CALLBACK(&scroll_event), NULL);
3984 #endif
3987 * Add selection handler functions.
3989 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3990 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3991 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3992 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3994 gui_gtk_set_selection_targets();
3996 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3997 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3999 /* Pretend we don't have input focus, we will get an event if we do. */
4000 gui.in_focus = FALSE;
4002 return OK;
4005 #if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
4007 * This is called from gui_start() after a fork() has been done.
4008 * We have to tell the session manager our new PID.
4010 void
4011 gui_mch_forked(void)
4013 if (using_gnome)
4015 GnomeClient *client;
4017 client = gnome_master_client();
4019 if (client != NULL)
4020 gnome_client_set_process_id(client, getpid());
4023 #endif /* FEAT_GUI_GNOME && FEAT_SESSION */
4026 * Called when the foreground or background color has been changed.
4027 * This used to change the graphics contexts directly but we are
4028 * currently manipulating them where desired.
4030 void
4031 gui_mch_new_colors(void)
4033 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
4035 GdkColor color = { 0, 0, 0, 0 };
4037 color.pixel = gui.back_pixel;
4038 gdk_window_set_background(gui.drawarea->window, &color);
4043 * This signal informs us about the need to rearrange our sub-widgets.
4045 static gint
4046 form_configure_event(GtkWidget *widget UNUSED,
4047 GdkEventConfigure *event,
4048 gpointer data UNUSED)
4050 int usable_height = event->height;
4052 /* When in a GtkPlug, we can't guarantee valid heights (as a round
4053 * no. of char-heights), so we have to manually sanitise them.
4054 * Widths seem to sort themselves out, don't ask me why.
4056 if (gtk_socket_id != 0)
4057 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
4059 gtk_form_freeze(GTK_FORM(gui.formwin));
4060 gui_resize_shell(event->width, usable_height);
4061 gtk_form_thaw(GTK_FORM(gui.formwin));
4063 return TRUE;
4067 * Function called when window already closed.
4068 * We can't do much more here than to trying to preserve what had been done,
4069 * since the window is already inevitably going away.
4071 static void
4072 mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
4074 /* Don't write messages to the GUI anymore */
4075 full_screen = FALSE;
4077 gui.mainwin = NULL;
4078 gui.drawarea = NULL;
4080 if (!exiting) /* only do anything if the destroy was unexpected */
4082 vim_strncpy(IObuff,
4083 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
4084 IOSIZE - 1);
4085 preserve_exit();
4091 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4092 * hints (and thus the required size from -geom), but that after that we
4093 * put the hints back to normal (the actual minimum size) so we may
4094 * subsequently be resized smaller. GtkSocket (the parent end) uses the
4095 * plug's window 'min hints to set *it's* minimum size, but that's also the
4096 * only way we have of making ourselves bigger (by set lines/columns).
4097 * Thus set hints at start-up to ensure correct init. size, then a
4098 * second after the final attempt to reset the real minimum hinst (done by
4099 * scrollbar init.), actually do the standard hinst and stop the timer.
4100 * We'll not let the default hints be set while this timer's active.
4102 static gboolean
4103 check_startup_plug_hints(gpointer data UNUSED)
4105 if (init_window_hints_state == 1)
4107 /* Safe to use normal hints now */
4108 init_window_hints_state = 0;
4109 update_window_manager_hints(0, 0);
4110 return FALSE; /* stop timer */
4113 /* Keep on trying */
4114 init_window_hints_state = 1;
4115 return TRUE;
4119 * Open the GUI window which was created by a call to gui_mch_init().
4122 gui_mch_open(void)
4124 guicolor_T fg_pixel = INVALCOLOR;
4125 guicolor_T bg_pixel = INVALCOLOR;
4126 guint pixel_width;
4127 guint pixel_height;
4129 #ifdef HAVE_GTK2
4131 * Allow setting a window role on the command line, or invent one
4132 * if none was specified. This is mainly useful for GNOME session
4133 * support; allowing the WM to restore window placement.
4135 if (role_argument != NULL)
4137 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4139 else
4141 char *role;
4143 /* Invent a unique-enough ID string for the role */
4144 role = g_strdup_printf("vim-%u-%u-%u",
4145 (unsigned)mch_get_pid(),
4146 (unsigned)g_random_int(),
4147 (unsigned)time(NULL));
4149 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4150 g_free(role);
4152 #endif
4154 if (gui_win_x != -1 && gui_win_y != -1)
4155 #ifdef HAVE_GTK2
4156 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
4157 #else
4158 gtk_widget_set_uposition(gui.mainwin, gui_win_x, gui_win_y);
4159 #endif
4161 /* Determine user specified geometry, if present. */
4162 if (gui.geom != NULL)
4164 int mask;
4165 unsigned int w, h;
4166 int x = 0;
4167 int y = 0;
4169 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4171 if (mask & WidthValue)
4172 Columns = w;
4173 if (mask & HeightValue)
4175 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
4176 p_window = h - 1;
4177 Rows = h;
4180 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4181 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4183 #ifdef HAVE_GTK2
4184 pixel_width += get_menu_tool_width();
4185 pixel_height += get_menu_tool_height();
4186 #endif
4188 if (mask & (XValue | YValue))
4190 int ww, hh;
4191 gui_mch_get_screen_dimensions(&ww, &hh);
4192 hh += p_ghr + get_menu_tool_height();
4193 ww += get_menu_tool_width();
4194 if (mask & XNegative)
4195 x += ww - pixel_width;
4196 if (mask & YNegative)
4197 y += hh - pixel_height;
4198 #ifdef HAVE_GTK2
4199 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4200 #else
4201 gtk_widget_set_uposition(gui.mainwin, x, y);
4202 #endif
4204 vim_free(gui.geom);
4205 gui.geom = NULL;
4207 /* From now until everyone's stopped trying to set the window hints
4208 * to their correct minimum values, stop them being set as we need
4209 * them to remain at our required size for the parent GtkSocket to
4210 * give us the right initial size.
4212 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
4214 update_window_manager_hints(pixel_width, pixel_height);
4215 init_window_hints_state = 1;
4216 g_timeout_add(1000, check_startup_plug_hints, NULL);
4220 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4221 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4222 #ifdef HAVE_GTK2
4223 /* For GTK2 changing the size of the form widget doesn't cause window
4224 * resizing. */
4225 if (gtk_socket_id == 0)
4226 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
4227 #else
4228 gtk_form_set_size(GTK_FORM(gui.formwin), pixel_width, pixel_height);
4229 #endif
4230 update_window_manager_hints(0, 0);
4232 if (foreground_argument != NULL)
4233 fg_pixel = gui_get_color((char_u *)foreground_argument);
4234 if (fg_pixel == INVALCOLOR)
4235 fg_pixel = gui_get_color((char_u *)"Black");
4237 if (background_argument != NULL)
4238 bg_pixel = gui_get_color((char_u *)background_argument);
4239 if (bg_pixel == INVALCOLOR)
4240 bg_pixel = gui_get_color((char_u *)"White");
4242 if (found_reverse_arg)
4244 gui.def_norm_pixel = bg_pixel;
4245 gui.def_back_pixel = fg_pixel;
4247 else
4249 gui.def_norm_pixel = fg_pixel;
4250 gui.def_back_pixel = bg_pixel;
4253 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4254 * in a vimrc file) */
4255 set_normal_colors();
4257 /* Check that none of the colors are the same as the background color */
4258 gui_check_colors();
4260 /* Get the colors for the highlight groups (gui_check_colors() might have
4261 * changed them). */
4262 highlight_gui_started(); /* re-init colors and fonts */
4264 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4265 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
4267 #ifdef FEAT_HANGULIN
4268 hangul_keyboard_set();
4269 #endif
4272 * Notify the fixed area about the need to resize the contents of the
4273 * gui.formwin, which we use for random positioning of the included
4274 * components.
4276 * We connect this signal deferred finally after anything is in place,
4277 * since this is intended to handle resizements coming from the window
4278 * manager upon us and should not interfere with what VIM is requesting
4279 * upon startup.
4281 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4282 GTK_SIGNAL_FUNC(form_configure_event), NULL);
4284 #ifdef FEAT_DND
4285 /* Set up for receiving DND items. */
4286 gui_gtk_set_dnd_targets();
4288 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4289 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
4290 #endif
4292 #ifdef HAVE_GTK2
4293 /* With GTK+ 2, we need to iconify the window before calling show()
4294 * to avoid mapping the window for a short time. This is just as one
4295 * would expect it to work, but it's different in GTK+ 1. The funny
4296 * thing is that iconifying after show() _does_ work with GTK+ 1.
4297 * (BTW doing this in the "realize" handler makes no difference.) */
4298 if (found_iconic_arg && gtk_socket_id == 0)
4299 gui_mch_iconify();
4300 #endif
4303 #if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4304 unsigned long menu_handler = 0;
4305 # ifdef FEAT_TOOLBAR
4306 unsigned long tool_handler = 0;
4307 # endif
4309 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4310 * show when restoring the saved layout configuration. We can't just
4311 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4312 * a toplevel window and thus will be realized immediately. Instead,
4313 * connect signal handlers to hide the widgets just after they've been
4314 * marked visible, but before the main window is realized.
4316 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4317 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4318 G_CALLBACK(&gtk_widget_hide),
4319 NULL);
4320 # ifdef FEAT_TOOLBAR
4321 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4322 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4323 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4324 G_CALLBACK(&gtk_widget_hide),
4325 NULL);
4326 # endif
4327 #endif
4328 gtk_widget_show(gui.mainwin);
4330 #if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4331 if (menu_handler != 0)
4332 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4333 # ifdef FEAT_TOOLBAR
4334 if (tool_handler != 0)
4335 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4336 # endif
4337 #endif
4340 #ifndef HAVE_GTK2
4341 /* With GTK+ 1, we need to iconify the window after calling show().
4342 * See the comment above for details. */
4343 if (found_iconic_arg && gtk_socket_id == 0)
4344 gui_mch_iconify();
4345 #endif
4347 return OK;
4351 void
4352 gui_mch_exit(int rc UNUSED)
4354 if (gui.mainwin != NULL)
4355 gtk_widget_destroy(gui.mainwin);
4357 if (gtk_main_level() > 0)
4358 gtk_main_quit();
4362 * Get the position of the top left corner of the window.
4365 gui_mch_get_winpos(int *x, int *y)
4367 #ifdef HAVE_GTK2
4368 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
4369 #else
4370 /* For some people this must be gdk_window_get_origin() for a correct
4371 * result. Where is the documentation! */
4372 gdk_window_get_root_origin(gui.mainwin->window, x, y);
4373 #endif
4374 return OK;
4378 * Set the position of the top left corner of the window to the given
4379 * coordinates.
4381 void
4382 gui_mch_set_winpos(int x, int y)
4384 #ifdef HAVE_GTK2
4385 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4386 #else
4387 gdk_window_move(gui.mainwin->window, x, y);
4388 #endif
4391 #ifdef HAVE_GTK2
4392 #if 0
4393 static int resize_idle_installed = FALSE;
4395 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4396 * the shell size doesn't exceed the window size, i.e. if the window manager
4397 * ignored our size request. Usually this happens if the window is maximized.
4399 * FIXME: It'd be nice if we could find a little more orthodox solution.
4400 * See also the remark below in gui_mch_set_shellsize().
4402 * DISABLED: When doing ":set lines+=1" this function would first invoke
4403 * gui_resize_shell() with the old size, then the normal callback would
4404 * report the new size through form_configure_event(). That caused the window
4405 * layout to be messed up.
4407 static gboolean
4408 force_shell_resize_idle(gpointer data)
4410 if (gui.mainwin != NULL
4411 && GTK_WIDGET_REALIZED(gui.mainwin)
4412 && GTK_WIDGET_VISIBLE(gui.mainwin))
4414 int width;
4415 int height;
4417 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4419 width -= get_menu_tool_width();
4420 height -= get_menu_tool_height();
4422 gui_resize_shell(width, height);
4425 resize_idle_installed = FALSE;
4426 return FALSE; /* don't call me again */
4428 #endif
4429 #endif /* HAVE_GTK2 */
4431 #if defined(HAVE_GTK2) || defined(PROTO)
4433 * Return TRUE if the main window is maximized.
4436 gui_mch_maximized()
4438 return (gui.mainwin != NULL && gui.mainwin->window != NULL
4439 && (gdk_window_get_state(gui.mainwin->window)
4440 & GDK_WINDOW_STATE_MAXIMIZED));
4444 * Unmaximize the main window
4446 void
4447 gui_mch_unmaximize()
4449 if (gui.mainwin != NULL)
4450 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
4452 #endif
4455 * Set the windows size.
4457 void
4458 gui_mch_set_shellsize(int width, int height,
4459 int min_width UNUSED, int min_height UNUSED,
4460 int base_width UNUSED, int base_height UNUSED,
4461 int direction UNUSED)
4463 #ifndef HAVE_GTK2
4464 /* Hack: When the form already is at the desired size, the window might
4465 * have been resized with the mouse. Force a resize by setting a
4466 * different size first. */
4467 if (GTK_FORM(gui.formwin)->width == width
4468 && GTK_FORM(gui.formwin)->height == height)
4470 gtk_form_set_size(GTK_FORM(gui.formwin), width + 1, height + 1);
4471 gui_mch_update();
4473 gtk_form_set_size(GTK_FORM(gui.formwin), width, height);
4474 #endif
4476 /* give GTK+ a chance to put all widget's into place */
4477 gui_mch_update();
4479 #ifndef HAVE_GTK2
4480 /* this will cause the proper resizement to happen too */
4481 update_window_manager_hints(0, 0);
4483 #else
4484 /* this will cause the proper resizement to happen too */
4485 if (gtk_socket_id == 0)
4486 update_window_manager_hints(0, 0);
4488 /* With GTK+ 2, changing the size of the form widget doesn't resize
4489 * the window. So let's do it the other way around and resize the
4490 * main window instead. */
4491 width += get_menu_tool_width();
4492 height += get_menu_tool_height();
4494 if (gtk_socket_id == 0)
4495 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
4496 else
4497 update_window_manager_hints(width, height);
4499 # if 0
4500 if (!resize_idle_installed)
4502 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4503 &force_shell_resize_idle, NULL, NULL);
4504 resize_idle_installed = TRUE;
4506 # endif
4508 * Wait until all events are processed to prevent a crash because the
4509 * real size of the drawing area doesn't reflect Vim's internal ideas.
4511 * This is a bit of a hack, since Vim is a terminal application with a GUI
4512 * on top, while the GUI expects to be the boss.
4514 gui_mch_update();
4515 #endif
4520 * The screen size is used to make sure the initial window doesn't get bigger
4521 * than the screen. This subtracts some room for menubar, toolbar and window
4522 * decorations.
4524 void
4525 gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4527 #ifdef HAVE_GTK_MULTIHEAD
4528 GdkScreen* screen;
4530 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4531 screen = gtk_widget_get_screen(gui.mainwin);
4532 else
4533 screen = gdk_screen_get_default();
4535 *screen_w = gdk_screen_get_width(screen);
4536 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4537 #else
4538 *screen_w = gdk_screen_width();
4539 /* Subtract 'guiheadroom' from the height to allow some room for the
4540 * window manager (task list and window title bar). */
4541 *screen_h = gdk_screen_height() - p_ghr;
4542 #endif
4545 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4546 * the toolbar and menubar for GTK, we subtract them from the screen
4547 * hight, so that the window size can be made to fit on the screen.
4548 * This should be completely changed later.
4550 *screen_w -= get_menu_tool_width();
4551 *screen_h -= get_menu_tool_height();
4554 #if defined(FEAT_TITLE) || defined(PROTO)
4555 void
4556 gui_mch_settitle(char_u *title, char_u *icon UNUSED)
4558 # ifdef HAVE_GTK2
4559 if (title != NULL && output_conv.vc_type != CONV_NONE)
4560 title = string_convert(&output_conv, title, NULL);
4561 # endif
4563 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4565 # ifdef HAVE_GTK2
4566 if (output_conv.vc_type != CONV_NONE)
4567 vim_free(title);
4568 # endif
4570 #endif /* FEAT_TITLE */
4572 #if defined(FEAT_MENU) || defined(PROTO)
4573 void
4574 gui_mch_enable_menu(int showit)
4576 GtkWidget *widget;
4578 # ifdef FEAT_GUI_GNOME
4579 if (using_gnome)
4580 widget = gui.menubar_h;
4581 else
4582 # endif
4583 widget = gui.menubar;
4585 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4586 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
4588 if (showit)
4589 gtk_widget_show(widget);
4590 else
4591 gtk_widget_hide(widget);
4593 update_window_manager_hints(0, 0);
4596 #endif /* FEAT_MENU */
4598 #if defined(FEAT_TOOLBAR) || defined(PROTO)
4599 void
4600 gui_mch_show_toolbar(int showit)
4602 GtkWidget *widget;
4604 if (gui.toolbar == NULL)
4605 return;
4607 # ifdef FEAT_GUI_GNOME
4608 if (using_gnome)
4609 widget = gui.toolbar_h;
4610 else
4611 # endif
4612 widget = gui.toolbar;
4614 if (showit)
4615 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4617 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4619 if (showit)
4620 gtk_widget_show(widget);
4621 else
4622 gtk_widget_hide(widget);
4624 update_window_manager_hints(0, 0);
4627 #endif /* FEAT_TOOLBAR */
4629 #ifndef HAVE_GTK2
4631 * Get a font structure for highlighting.
4632 * "cbdata" is a pointer to the global gui structure.
4634 static void
4635 font_sel_ok(GtkWidget *wgt, gpointer cbdata)
4637 gui_T *vw = (gui_T *)cbdata;
4638 GtkFontSelectionDialog *fs = (GtkFontSelectionDialog *)vw->fontdlg;
4640 if (vw->fontname)
4641 g_free(vw->fontname);
4643 vw->fontname = (char_u *)gtk_font_selection_dialog_get_font_name(fs);
4644 gtk_widget_hide(vw->fontdlg);
4645 if (gtk_main_level() > 0)
4646 gtk_main_quit();
4649 static void
4650 font_sel_cancel(GtkWidget *wgt, gpointer cbdata)
4652 gui_T *vw = (gui_T *)cbdata;
4654 gtk_widget_hide(vw->fontdlg);
4655 if (gtk_main_level() > 0)
4656 gtk_main_quit();
4659 static void
4660 font_sel_destroy(GtkWidget *wgt, gpointer cbdata)
4662 gui_T *vw = (gui_T *)cbdata;
4664 vw->fontdlg = NULL;
4665 if (gtk_main_level() > 0)
4666 gtk_main_quit();
4668 #endif /* !HAVE_GTK2 */
4670 #ifdef HAVE_GTK2
4672 * Check if a given font is a CJK font. This is done in a very crude manner. It
4673 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4674 * font. Consequently, this function cannot be used as a general purpose check
4675 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4676 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4678 static int
4679 is_cjk_font(PangoFontDescription *font_desc)
4681 static const char * const cjk_langs[] =
4682 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4684 PangoFont *font;
4685 unsigned i;
4686 int is_cjk = FALSE;
4688 font = pango_context_load_font(gui.text_context, font_desc);
4690 if (font == NULL)
4691 return FALSE;
4693 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4695 PangoCoverage *coverage;
4696 gunichar uc;
4698 coverage = pango_font_get_coverage(
4699 font, pango_language_from_string(cjk_langs[i]));
4701 if (coverage != NULL)
4703 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4704 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4705 pango_coverage_unref(coverage);
4709 g_object_unref(font);
4711 return is_cjk;
4713 #endif /* HAVE_GTK2 */
4716 * Adjust gui.char_height (after 'linespace' was changed).
4719 gui_mch_adjust_charheight(void)
4721 #ifdef HAVE_GTK2
4722 PangoFontMetrics *metrics;
4723 int ascent;
4724 int descent;
4726 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4727 pango_context_get_language(gui.text_context));
4728 ascent = pango_font_metrics_get_ascent(metrics);
4729 descent = pango_font_metrics_get_descent(metrics);
4731 pango_font_metrics_unref(metrics);
4733 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
4734 + p_linespace;
4735 /* LINTED: avoid warning: bitwise operation on signed value */
4736 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4738 #else /* !HAVE_GTK2 */
4740 gui.char_height = gui.current_font->ascent + gui.current_font->descent
4741 + p_linespace;
4742 gui.char_ascent = gui.current_font->ascent + p_linespace / 2;
4744 #endif /* !HAVE_GTK2 */
4746 /* A not-positive value of char_height may crash Vim. Only happens
4747 * if 'linespace' is negative (which does make sense sometimes). */
4748 gui.char_ascent = MAX(gui.char_ascent, 0);
4749 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4751 return OK;
4754 #if defined(FEAT_XFONTSET) || defined(PROTO)
4756 * Try to load the requested fontset.
4758 GuiFontset
4759 gui_mch_get_fontset(char_u *name, int report_error, int fixed_width)
4761 GdkFont *font;
4763 if (!gui.in_use || name == NULL)
4764 return NOFONT;
4766 font = gdk_fontset_load((gchar *)name);
4768 if (font == NULL)
4770 if (report_error)
4771 EMSG2(_(e_fontset), name);
4772 return NOFONT;
4774 /* TODO: check if the font is fixed width. */
4776 /* reference this font as being in use */
4777 gdk_font_ref(font);
4779 return (GuiFontset)font;
4781 #endif /* FEAT_XFONTSET */
4783 #ifndef HAVE_GTK2
4785 * Put up a font dialog and return the selected font name in allocated memory.
4786 * "oldval" is the previous value.
4787 * Return NULL when cancelled.
4789 char_u *
4790 gui_mch_font_dialog(char_u *oldval)
4792 char_u *fontname = NULL;
4794 if (!gui.fontdlg)
4796 GtkFontSelectionDialog *fsd = NULL;
4798 gui.fontdlg = gtk_font_selection_dialog_new(_("Font Selection"));
4799 fsd = GTK_FONT_SELECTION_DIALOG(gui.fontdlg);
4800 gtk_window_set_modal(GTK_WINDOW(gui.fontdlg), TRUE);
4801 gtk_window_set_transient_for(GTK_WINDOW(gui.fontdlg),
4802 GTK_WINDOW(gui.mainwin));
4803 gtk_signal_connect(GTK_OBJECT(gui.fontdlg), "destroy",
4804 GTK_SIGNAL_FUNC(font_sel_destroy), &gui);
4805 gtk_signal_connect(GTK_OBJECT(fsd->ok_button), "clicked",
4806 GTK_SIGNAL_FUNC(font_sel_ok), &gui);
4807 gtk_signal_connect(GTK_OBJECT(fsd->cancel_button), "clicked",
4808 GTK_SIGNAL_FUNC(font_sel_cancel), &gui);
4811 if (oldval != NULL && *oldval != NUL)
4812 gtk_font_selection_dialog_set_font_name(
4813 GTK_FONT_SELECTION_DIALOG(gui.fontdlg), (char *)oldval);
4814 else
4815 gtk_font_selection_dialog_set_font_name(
4816 GTK_FONT_SELECTION_DIALOG(gui.fontdlg), DEFAULT_FONT);
4818 if (gui.fontname)
4820 g_free(gui.fontname);
4821 gui.fontname = NULL;
4823 gtk_window_position(GTK_WINDOW(gui.fontdlg), GTK_WIN_POS_MOUSE);
4824 gtk_widget_show(gui.fontdlg);
4826 static gchar *spacings[] = {"c", "m", NULL};
4828 /* In GTK 1.2.3 this must be after the gtk_widget_show() call,
4829 * otherwise everything is blocked for ten seconds. */
4830 gtk_font_selection_dialog_set_filter(
4831 GTK_FONT_SELECTION_DIALOG(gui.fontdlg),
4832 GTK_FONT_FILTER_BASE,
4833 GTK_FONT_ALL, NULL, NULL,
4834 NULL, NULL, spacings, NULL);
4837 /* Wait for the font dialog to be closed. */
4838 while (gui.fontdlg && GTK_WIDGET_DRAWABLE(gui.fontdlg))
4839 gtk_main_iteration_do(TRUE);
4841 if (gui.fontname != NULL)
4843 /* Apparently some font names include a comma, need to escape that,
4844 * because in 'guifont' it separates names. */
4845 fontname = vim_strsave_escaped(gui.fontname, (char_u *)",");
4846 g_free(gui.fontname);
4847 gui.fontname = NULL;
4849 return fontname;
4851 #endif /* !HAVE_GTK2 */
4853 #ifdef HAVE_GTK2
4855 * Put up a font dialog and return the selected font name in allocated memory.
4856 * "oldval" is the previous value. Return NULL when cancelled.
4857 * This should probably go into gui_gtk.c. Hmm.
4858 * FIXME:
4859 * The GTK2 font selection dialog has no filtering API. So we could either
4860 * a) implement our own (possibly copying the code from somewhere else) or
4861 * b) just live with it.
4863 char_u *
4864 gui_mch_font_dialog(char_u *oldval)
4866 GtkWidget *dialog;
4867 int response;
4868 char_u *fontname = NULL;
4869 char_u *oldname;
4871 dialog = gtk_font_selection_dialog_new(NULL);
4873 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4874 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4876 if (oldval != NULL && oldval[0] != NUL)
4878 if (output_conv.vc_type != CONV_NONE)
4879 oldname = string_convert(&output_conv, oldval, NULL);
4880 else
4881 oldname = oldval;
4883 /* Annoying bug in GTK (or Pango): if the font name does not include a
4884 * size, zero is used. Use default point size ten. */
4885 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4887 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4889 if (p != NULL)
4891 STRCPY(p + STRLEN(p), " 10");
4892 if (oldname != oldval)
4893 vim_free(oldname);
4894 oldname = p;
4898 gtk_font_selection_dialog_set_font_name(
4899 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4901 if (oldname != oldval)
4902 vim_free(oldname);
4904 else
4905 gtk_font_selection_dialog_set_font_name(
4906 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
4908 response = gtk_dialog_run(GTK_DIALOG(dialog));
4910 if (response == GTK_RESPONSE_OK)
4912 char *name;
4914 name = gtk_font_selection_dialog_get_font_name(
4915 GTK_FONT_SELECTION_DIALOG(dialog));
4916 if (name != NULL)
4918 char_u *p;
4920 /* Apparently some font names include a comma, need to escape
4921 * that, because in 'guifont' it separates names. */
4922 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
4923 g_free(name);
4924 if (p != NULL && input_conv.vc_type != CONV_NONE)
4926 fontname = string_convert(&input_conv, p, NULL);
4927 vim_free(p);
4929 else
4930 fontname = p;
4934 if (response != GTK_RESPONSE_NONE)
4935 gtk_widget_destroy(dialog);
4937 return fontname;
4941 * Some monospace fonts don't support a bold weight, and fall back
4942 * silently to the regular weight. But this is no good since our text
4943 * drawing function can emulate bold by overstriking. So let's try
4944 * to detect whether bold weight is actually available and emulate it
4945 * otherwise.
4947 * Note that we don't need to check for italic style since Xft can
4948 * emulate italic on its own, provided you have a proper fontconfig
4949 * setup. We wouldn't be able to emulate it in Vim anyway.
4951 static void
4952 get_styled_font_variants(void)
4954 PangoFontDescription *bold_font_desc;
4955 PangoFont *plain_font;
4956 PangoFont *bold_font;
4958 gui.font_can_bold = FALSE;
4960 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4962 if (plain_font == NULL)
4963 return;
4965 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4966 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4968 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4970 * The comparison relies on the unique handle nature of a PangoFont*,
4971 * i.e. it's assumed that a different PangoFont* won't refer to the
4972 * same font. Seems to work, and failing here isn't critical anyway.
4974 if (bold_font != NULL)
4976 gui.font_can_bold = (bold_font != plain_font);
4977 g_object_unref(bold_font);
4980 pango_font_description_free(bold_font_desc);
4981 g_object_unref(plain_font);
4984 #else /* !HAVE_GTK2 */
4987 * There is only one excuse I can give for the following attempt to manage font
4988 * styles:
4990 * I HATE THE BRAIN DEAD WAY X11 IS HANDLING FONTS (--mdcki)
4991 * (Me too. --danielk)
4993 static void
4994 get_styled_font_variants(char_u * font_name)
4996 char *chunk[32];
4997 char *sdup;
4998 char *tmp;
4999 int len, i;
5000 GuiFont *styled_font[3];
5002 styled_font[0] = &gui.bold_font;
5003 styled_font[1] = &gui.ital_font;
5004 styled_font[2] = &gui.boldital_font;
5006 /* First free whatever was previously there. */
5007 for (i = 0; i < 3; ++i)
5008 if (*styled_font[i])
5010 gdk_font_unref(*styled_font[i]);
5011 *styled_font[i] = NULL;
5014 if ((sdup = g_strdup((const char *)font_name)) == NULL)
5015 return;
5017 /* split up the whole */
5018 i = 0;
5019 for (tmp = sdup; *tmp != '\0'; ++tmp)
5021 if (*tmp == '-')
5023 *tmp = '\0';
5025 if (i == 32)
5026 break;
5028 chunk[i] = tmp + 1;
5029 ++i;
5033 if (i == 14)
5035 GdkFont *font = NULL;
5036 const char *bold_chunk[3] = { "bold", NULL, "bold" };
5037 const char *italic_chunk[3] = { NULL, "o", "o" };
5039 /* font name was complete */
5040 len = strlen((const char *)font_name) + 32;
5042 for (i = 0; i < 3; ++i)
5044 char *styled_name;
5045 int j;
5047 styled_name = (char *)alloc(len);
5048 if (styled_name == NULL)
5050 g_free(sdup);
5051 return;
5054 *styled_name = '\0';
5056 for (j = 0; j < 14; ++j)
5058 strcat(styled_name, "-");
5059 if (j == 2 && bold_chunk[i] != NULL)
5060 strcat(styled_name, bold_chunk[i]);
5061 else if (j == 3 && italic_chunk[i] != NULL)
5062 strcat(styled_name, italic_chunk[i]);
5063 else
5064 strcat(styled_name, chunk[j]);
5067 font = gui_mch_get_font((char_u *)styled_name, FALSE);
5068 if (font != NULL)
5069 *styled_font[i] = font;
5071 vim_free(styled_name);
5075 g_free(sdup);
5077 #endif /* !HAVE_GTK2 */
5079 #ifdef HAVE_GTK2
5080 static PangoEngineShape *default_shape_engine = NULL;
5083 * Create a map from ASCII characters in the range [32,126] to glyphs
5084 * of the current font. This is used by gui_gtk2_draw_string() to skip
5085 * the itemize and shaping process for the most common case.
5087 static void
5088 ascii_glyph_table_init(void)
5090 char_u ascii_chars[128];
5091 PangoAttrList *attr_list;
5092 GList *item_list;
5093 int i;
5095 if (gui.ascii_glyphs != NULL)
5096 pango_glyph_string_free(gui.ascii_glyphs);
5097 if (gui.ascii_font != NULL)
5098 g_object_unref(gui.ascii_font);
5100 gui.ascii_glyphs = NULL;
5101 gui.ascii_font = NULL;
5103 /* For safety, fill in question marks for the control characters. */
5104 for (i = 0; i < 32; ++i)
5105 ascii_chars[i] = '?';
5106 for (; i < 127; ++i)
5107 ascii_chars[i] = i;
5108 ascii_chars[i] = '?';
5110 attr_list = pango_attr_list_new();
5111 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
5112 0, sizeof(ascii_chars), attr_list, NULL);
5114 if (item_list != NULL && item_list->next == NULL) /* play safe */
5116 PangoItem *item;
5117 int width;
5119 item = (PangoItem *)item_list->data;
5120 width = gui.char_width * PANGO_SCALE;
5122 /* Remember the shape engine used for ASCII. */
5123 default_shape_engine = item->analysis.shape_engine;
5125 gui.ascii_font = item->analysis.font;
5126 g_object_ref(gui.ascii_font);
5128 gui.ascii_glyphs = pango_glyph_string_new();
5130 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
5131 &item->analysis, gui.ascii_glyphs);
5133 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
5135 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
5137 PangoGlyphGeometry *geom;
5139 geom = &gui.ascii_glyphs->glyphs[i].geometry;
5140 geom->x_offset += MAX(0, width - geom->width) / 2;
5141 geom->width = width;
5145 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5146 g_list_free(item_list);
5147 pango_attr_list_unref(attr_list);
5149 #endif /* HAVE_GTK2 */
5152 * Initialize Vim to use the font or fontset with the given name.
5153 * Return FAIL if the font could not be loaded, OK otherwise.
5156 gui_mch_init_font(char_u *font_name, int fontset UNUSED)
5158 #ifdef HAVE_GTK2
5159 PangoFontDescription *font_desc;
5160 PangoLayout *layout;
5161 int width;
5163 /* If font_name is NULL, this means to use the default, which should
5164 * be present on all proper Pango/fontconfig installations. */
5165 if (font_name == NULL)
5166 font_name = (char_u *)DEFAULT_FONT;
5168 font_desc = gui_mch_get_font(font_name, FALSE);
5170 if (font_desc == NULL)
5171 return FAIL;
5173 gui_mch_free_font(gui.norm_font);
5174 gui.norm_font = font_desc;
5176 pango_context_set_font_description(gui.text_context, font_desc);
5178 layout = pango_layout_new(gui.text_context);
5179 pango_layout_set_text(layout, "MW", 2);
5180 pango_layout_get_size(layout, &width, NULL);
5182 * Set char_width to half the width obtained from pango_layout_get_size()
5183 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5184 * Pango to use the same width for both non-CJK characters (e.g. Latin
5185 * letters and numbers) and CJK characters. This results in 's p a c e d
5186 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5187 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5188 * width for CJK fonts.
5190 * For related bugs, see:
5191 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5192 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5194 * With this, for all four of the following cases, Vim works fine:
5195 * guifont=CJK_fixed_width_font
5196 * guifont=Non_CJK_fixed_font
5197 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5198 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5200 if (is_cjk_font(gui.norm_font))
5202 int cjk_width;
5204 /* Measure the text extent of U+4E00 and U+4E8C */
5205 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5206 pango_layout_get_size(layout, &cjk_width, NULL);
5208 if (width == cjk_width) /* Xft not patched */
5209 width /= 2;
5211 g_object_unref(layout);
5213 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5215 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5216 if (gui.char_width <= 0)
5217 gui.char_width = 8;
5219 gui_mch_adjust_charheight();
5221 /* Set the fontname, which will be used for information purposes */
5222 hl_set_font_name(font_name);
5224 get_styled_font_variants();
5225 ascii_glyph_table_init();
5227 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5228 if (gui.wide_font != NULL
5229 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5231 pango_font_description_free(gui.wide_font);
5232 gui.wide_font = NULL;
5235 #else /* !HAVE_GTK2 */
5237 GdkFont *font = NULL;
5239 # ifdef FEAT_XFONTSET
5240 /* Try loading a fontset. If this fails we try loading a normal font. */
5241 if (fontset && font_name != NULL)
5242 font = gui_mch_get_fontset(font_name, TRUE, TRUE);
5244 if (font == NULL)
5245 # endif
5247 /* If font_name is NULL, this means to use the default, which should
5248 * be present on all X11 servers. */
5249 if (font_name == NULL)
5250 font_name = (char_u *)DEFAULT_FONT;
5251 font = gui_mch_get_font(font_name, FALSE);
5254 if (font == NULL)
5255 return FAIL;
5257 gui_mch_free_font(gui.norm_font);
5258 # ifdef FEAT_XFONTSET
5259 gui_mch_free_fontset(gui.fontset);
5260 if (font->type == GDK_FONT_FONTSET)
5262 gui.norm_font = NOFONT;
5263 gui.fontset = (GuiFontset)font;
5264 /* Use two bytes, this works around the problem that the result would
5265 * be zero if no 8-bit font was found. */
5266 gui.char_width = gdk_string_width(font, "xW") / 2;
5268 else
5269 # endif
5271 gui.norm_font = font;
5272 # ifdef FEAT_XFONTSET
5273 gui.fontset = NOFONTSET;
5274 # endif
5275 gui.char_width = ((XFontStruct *)
5276 GDK_FONT_XFONT(font))->max_bounds.width;
5279 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5280 if (gui.char_width <= 0)
5281 gui.char_width = 8;
5283 gui.char_height = font->ascent + font->descent + p_linespace;
5284 gui.char_ascent = font->ascent + p_linespace / 2;
5286 /* A not-positive value of char_height may crash Vim. Only happens
5287 * if 'linespace' is negative (which does make sense sometimes). */
5288 gui.char_ascent = MAX(gui.char_ascent, 0);
5289 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5291 /* Set the fontname, which will be used for information purposes */
5292 hl_set_font_name(font_name);
5294 if (font->type != GDK_FONT_FONTSET)
5295 get_styled_font_variants(font_name);
5297 /* Synchronize the fonts used in user input dialogs, since otherwise
5298 * search/replace will be esp. annoying in case of international font
5299 * usage.
5301 gui_gtk_synch_fonts();
5303 # ifdef FEAT_XIM
5304 /* Adjust input management behaviour to the capabilities of the new
5305 * fontset */
5306 xim_decide_input_style();
5307 if (xim_get_status_area_height())
5309 /* Status area is required. Just create the empty container so that
5310 * mainwin will allocate the extra space for status area. */
5311 GtkWidget *alignment = gtk_alignment_new((gfloat)0.5, (gfloat)0.5,
5312 (gfloat)1.0, (gfloat)1.0);
5314 gtk_widget_set_usize(alignment, 20, gui.char_height + 2);
5315 gtk_box_pack_end(GTK_BOX(GTK_BIN(gui.mainwin)->child),
5316 alignment, FALSE, FALSE, 0);
5317 gtk_widget_show(alignment);
5319 # endif
5320 #endif /* !HAVE_GTK2 */
5322 #ifdef HAVE_GTK2
5323 if (gui_mch_maximized())
5325 int w, h;
5327 /* Update lines and columns in accordance with the new font, keep the
5328 * window maximized. */
5329 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
5330 w -= get_menu_tool_width();
5331 h -= get_menu_tool_height();
5332 gui_resize_shell(w, h);
5334 else
5335 #endif
5337 /* Preserve the logical dimensions of the screen. */
5338 update_window_manager_hints(0, 0);
5341 return OK;
5345 * Get a reference to the font "name".
5346 * Return zero for failure.
5348 GuiFont
5349 gui_mch_get_font(char_u *name, int report_error)
5351 #ifdef HAVE_GTK2
5352 PangoFontDescription *font;
5353 #else
5354 GdkFont *font;
5355 #endif
5357 /* can't do this when GUI is not running */
5358 if (!gui.in_use || name == NULL)
5359 return NULL;
5361 #ifdef HAVE_GTK2
5362 if (output_conv.vc_type != CONV_NONE)
5364 char_u *buf;
5366 buf = string_convert(&output_conv, name, NULL);
5367 if (buf != NULL)
5369 font = pango_font_description_from_string((const char *)buf);
5370 vim_free(buf);
5372 else
5373 font = NULL;
5375 else
5376 font = pango_font_description_from_string((const char *)name);
5378 if (font != NULL)
5380 PangoFont *real_font;
5382 /* pango_context_load_font() bails out if no font size is set */
5383 if (pango_font_description_get_size(font) <= 0)
5384 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5386 real_font = pango_context_load_font(gui.text_context, font);
5388 if (real_font == NULL)
5390 pango_font_description_free(font);
5391 font = NULL;
5393 else
5394 g_object_unref(real_font);
5396 #else
5397 font = gdk_font_load((const gchar *)name);
5398 #endif
5400 if (font == NULL)
5402 if (report_error)
5403 EMSG2(_((char *)e_font), name);
5404 return NULL;
5407 #ifdef HAVE_GTK2
5409 * The fixed-width check has been disabled for GTK+ 2. Rationale:
5411 * - The check tends to report false positives, particularly
5412 * in non-Latin locales or with old X fonts.
5413 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
5414 * GTK+ 2 Vim is actually capable of displaying variable width
5415 * fonts. Those will just be spaced out like in AA xterm.
5416 * - Failing here for the default font causes GUI startup to fail
5417 * even with wiped out configuration files.
5418 * - The font dialog displays all fonts unfiltered, and it's rather
5419 * annoying if 95% of the listed fonts produce an error message.
5421 # if 0
5423 /* Check that this is a mono-spaced font. Naturally, this is a bit
5424 * hackish -- fixed-width isn't really suitable for i18n text :/ */
5425 PangoLayout *layout;
5426 unsigned int i;
5427 int last_width = -1;
5428 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
5430 layout = pango_layout_new(gui.text_context);
5431 pango_layout_set_font_description(layout, font);
5433 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
5435 int width;
5437 pango_layout_set_text(layout, &test_chars[i], 1);
5438 pango_layout_get_size(layout, &width, NULL);
5440 if (last_width >= 0 && width != last_width)
5442 pango_font_description_free(font);
5443 font = NULL;
5444 break;
5447 last_width = width;
5450 g_object_unref(layout);
5452 # endif
5453 #else /* !HAVE_GTK2 */
5455 XFontStruct *xfont;
5457 /* reference this font as being in use */
5458 gdk_font_ref(font);
5460 /* Check that this is a mono-spaced font.
5462 xfont = (XFontStruct *) GDK_FONT_XFONT(font);
5464 if (xfont->max_bounds.width != xfont->min_bounds.width)
5466 gdk_font_unref(font);
5467 font = NULL;
5470 #endif /* !HAVE_GTK2 */
5472 #if !defined(HAVE_GTK2) || 0 /* disabled for GTK+ 2, see above */
5473 if (font == NULL && report_error)
5474 EMSG2(_(e_fontwidth), name);
5475 #endif
5477 return font;
5480 #if defined(FEAT_EVAL) || defined(PROTO)
5482 * Return the name of font "font" in allocated memory.
5484 char_u *
5485 gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
5487 # ifdef HAVE_GTK2
5488 if (font != NOFONT)
5490 char *pangoname = pango_font_description_to_string(font);
5492 if (pangoname != NULL)
5494 char_u *s = vim_strsave((char_u *)pangoname);
5496 g_free(pangoname);
5497 return s;
5500 # else
5501 /* Don't know how to get the name, return what we got. */
5502 if (name != NULL)
5503 return vim_strsave(name);
5504 # endif
5505 return NULL;
5507 #endif
5509 #if !defined(HAVE_GTK2) || defined(PROTO)
5511 * Set the current text font.
5512 * Since we create all GC on demand, we use just gui.current_font to
5513 * indicate the desired current font.
5515 void
5516 gui_mch_set_font(GuiFont font)
5518 gui.current_font = font;
5520 #endif
5522 #if defined(FEAT_XFONTSET) || defined(PROTO)
5524 * Set the current text fontset.
5526 void
5527 gui_mch_set_fontset(GuiFontset fontset)
5529 gui.current_font = fontset;
5531 #endif
5534 * If a font is not going to be used, free its structure.
5536 void
5537 gui_mch_free_font(GuiFont font)
5539 if (font != NOFONT)
5540 #ifdef HAVE_GTK2
5541 pango_font_description_free(font);
5542 #else
5543 gdk_font_unref(font);
5544 #endif
5547 #if defined(FEAT_XFONTSET) || defined(PROTO)
5549 * If a fontset is not going to be used, free its structure.
5551 void
5552 gui_mch_free_fontset(GuiFontset fontset)
5554 if (fontset != NOFONTSET)
5555 gdk_font_unref(fontset);
5557 #endif
5561 * Return the Pixel value (color) for the given color name. This routine was
5562 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5563 * Programmer's Guide.
5564 * Return INVALCOLOR for error.
5566 guicolor_T
5567 gui_mch_get_color(char_u *name)
5569 /* A number of colors that some X11 systems don't have */
5570 static const char *const vimnames[][2] =
5572 {"LightRed", "#FFBBBB"},
5573 {"LightGreen", "#88FF88"},
5574 {"LightMagenta","#FFBBFF"},
5575 {"DarkCyan", "#008888"},
5576 {"DarkBlue", "#0000BB"},
5577 {"DarkRed", "#BB0000"},
5578 {"DarkMagenta", "#BB00BB"},
5579 {"DarkGrey", "#BBBBBB"},
5580 {"DarkYellow", "#BBBB00"},
5581 {"Gray10", "#1A1A1A"},
5582 {"Grey10", "#1A1A1A"},
5583 {"Gray20", "#333333"},
5584 {"Grey20", "#333333"},
5585 {"Gray30", "#4D4D4D"},
5586 {"Grey30", "#4D4D4D"},
5587 {"Gray40", "#666666"},
5588 {"Grey40", "#666666"},
5589 {"Gray50", "#7F7F7F"},
5590 {"Grey50", "#7F7F7F"},
5591 {"Gray60", "#999999"},
5592 {"Grey60", "#999999"},
5593 {"Gray70", "#B3B3B3"},
5594 {"Grey70", "#B3B3B3"},
5595 {"Gray80", "#CCCCCC"},
5596 {"Grey80", "#CCCCCC"},
5597 {"Gray90", "#E5E5E5"},
5598 {"Grey90", "#E5E5E5"},
5599 {NULL, NULL}
5602 if (!gui.in_use) /* can't do this when GUI not running */
5603 return INVALCOLOR;
5605 while (name != NULL)
5607 GdkColor color;
5608 int parsed;
5609 int i;
5611 parsed = gdk_color_parse((const char *)name, &color);
5613 #ifndef HAVE_GTK2 /* ohh, lovely GTK+ 2, eases our pain :) */
5615 * Since we have already called gtk_set_locale here the bugger
5616 * XParseColor will accept only explicit color names in the language
5617 * of the current locale. However this will interfere with:
5618 * 1. Vim's global startup files
5619 * 2. Explicit color names in .vimrc
5621 * Therefore we first try to parse the color in the current locale and
5622 * if it fails, we fall back to the portable "C" one.
5624 if (!parsed)
5626 char *current;
5628 current = setlocale(LC_ALL, NULL);
5629 if (current != NULL)
5631 char *saved;
5633 saved = g_strdup(current);
5634 setlocale(LC_ALL, "C");
5636 parsed = gdk_color_parse((const gchar *)name, &color);
5638 setlocale(LC_ALL, saved);
5639 gtk_set_locale();
5641 g_free(saved);
5644 #endif /* !HAVE_GTK2 */
5646 if (parsed)
5648 #ifdef HAVE_GTK2
5649 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5650 &color, FALSE, TRUE);
5651 #else
5652 gdk_color_alloc(gtk_widget_get_colormap(gui.drawarea), &color);
5653 #endif
5654 return (guicolor_T)color.pixel;
5656 /* add a few builtin names and try again */
5657 for (i = 0; ; ++i)
5659 if (vimnames[i][0] == NULL)
5661 name = NULL;
5662 break;
5664 if (STRICMP(name, vimnames[i][0]) == 0)
5666 name = (char_u *)vimnames[i][1];
5667 break;
5672 return INVALCOLOR;
5676 * Set the current text foreground color.
5678 void
5679 gui_mch_set_fg_color(guicolor_T color)
5681 gui.fgcolor->pixel = (unsigned long)color;
5685 * Set the current text background color.
5687 void
5688 gui_mch_set_bg_color(guicolor_T color)
5690 gui.bgcolor->pixel = (unsigned long)color;
5694 * Set the current text special color.
5696 void
5697 gui_mch_set_sp_color(guicolor_T color)
5699 gui.spcolor->pixel = (unsigned long)color;
5702 #ifdef HAVE_GTK2
5704 * Function-like convenience macro for the sake of efficiency.
5706 #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5707 G_STMT_START{ \
5708 PangoAttribute *tmp_attr_; \
5709 tmp_attr_ = (Attribute); \
5710 tmp_attr_->start_index = (Start); \
5711 tmp_attr_->end_index = (End); \
5712 pango_attr_list_insert((AttrList), tmp_attr_); \
5713 }G_STMT_END
5715 static void
5716 apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5718 char_u *start = NULL;
5719 char_u *p;
5720 int uc;
5722 for (p = s; p < s + len; p += utf_byte2len(*p))
5724 uc = utf_ptr2char(p);
5726 if (start == NULL)
5728 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5729 start = p;
5731 else if (uc < 0x80 /* optimization shortcut */
5732 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5734 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5735 attr_list, start - s, p - s);
5736 start = NULL;
5740 if (start != NULL)
5741 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5742 attr_list, start - s, len);
5745 static int
5746 count_cluster_cells(char_u *s, PangoItem *item,
5747 PangoGlyphString* glyphs, int i,
5748 int *cluster_width,
5749 int *last_glyph_rbearing)
5751 char_u *p;
5752 int next; /* glyph start index of next cluster */
5753 int start, end; /* string segment of current cluster */
5754 int width; /* real cluster width in Pango units */
5755 int uc;
5756 int cellcount = 0;
5758 width = glyphs->glyphs[i].geometry.width;
5760 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5762 if (glyphs->glyphs[next].attr.is_cluster_start)
5763 break;
5764 else if (glyphs->glyphs[next].geometry.width > width)
5765 width = glyphs->glyphs[next].geometry.width;
5768 start = item->offset + glyphs->log_clusters[i];
5769 end = item->offset + ((next < glyphs->num_glyphs) ?
5770 glyphs->log_clusters[next] : item->length);
5772 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5774 uc = utf_ptr2char(p);
5775 if (uc < 0x80)
5776 ++cellcount;
5777 else if (!utf_iscomposing(uc))
5778 cellcount += utf_char2cells(uc);
5781 if (last_glyph_rbearing != NULL
5782 && cellcount > 0 && next == glyphs->num_glyphs)
5784 PangoRectangle ink_rect;
5786 * If a certain combining mark had to be taken from a non-monospace
5787 * font, we have to compensate manually by adapting x_offset according
5788 * to the ink extents of the previous glyph.
5790 pango_font_get_glyph_extents(item->analysis.font,
5791 glyphs->glyphs[i].glyph,
5792 &ink_rect, NULL);
5794 if (PANGO_RBEARING(ink_rect) > 0)
5795 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5798 if (cellcount > 0)
5799 *cluster_width = width;
5801 return cellcount;
5805 * If there are only combining characters in the cluster, we cannot just
5806 * change the width of the previous glyph since there is none. Therefore
5807 * some guesswork is needed.
5809 * If ink_rect.x is negative Pango apparently has taken care of the composing
5810 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5811 * to problems with composing from different fonts we still need to fine-tune
5812 * x_offset to avoid uglyness.
5814 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5815 * the position of the previous glyph. Apparently this happens only with old
5816 * X fonts which don't provide the special combining information needed by
5817 * Pango.
5819 static void
5820 setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5821 int last_cellcount, int last_cluster_width,
5822 int last_glyph_rbearing)
5824 PangoRectangle ink_rect;
5825 PangoRectangle logical_rect;
5826 int width;
5828 width = last_cellcount * gui.char_width * PANGO_SCALE;
5829 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5830 glyph->geometry.width = 0;
5832 pango_font_get_glyph_extents(item->analysis.font,
5833 glyph->glyph,
5834 &ink_rect, &logical_rect);
5835 if (ink_rect.x < 0)
5837 glyph->geometry.x_offset += last_glyph_rbearing;
5838 glyph->geometry.y_offset = logical_rect.height
5839 - (gui.char_height - p_linespace) * PANGO_SCALE;
5843 static void
5844 draw_glyph_string(int row, int col, int num_cells, int flags,
5845 PangoFont *font, PangoGlyphString *glyphs)
5847 if (!(flags & DRAW_TRANSP))
5849 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5851 gdk_draw_rectangle(gui.drawarea->window,
5852 gui.text_gc,
5853 TRUE,
5854 FILL_X(col),
5855 FILL_Y(row),
5856 num_cells * gui.char_width,
5857 gui.char_height);
5860 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5862 gdk_draw_glyphs(gui.drawarea->window,
5863 gui.text_gc,
5864 font,
5865 TEXT_X(col),
5866 TEXT_Y(row),
5867 glyphs);
5869 /* redraw the contents with an offset of 1 to emulate bold */
5870 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
5871 gdk_draw_glyphs(gui.drawarea->window,
5872 gui.text_gc,
5873 font,
5874 TEXT_X(col) + 1,
5875 TEXT_Y(row),
5876 glyphs);
5879 #endif /* HAVE_GTK2 */
5882 * Draw underline and undercurl at the bottom of the character cell.
5884 static void
5885 draw_under(int flags, int row, int col, int cells)
5887 int i;
5888 int offset;
5889 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
5890 int y = FILL_Y(row + 1) - 1;
5892 /* Undercurl: draw curl at the bottom of the character cell. */
5893 if (flags & DRAW_UNDERC)
5895 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5896 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5898 offset = val[i % 8];
5899 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5901 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5904 /* Underline: draw a line at the bottom of the character cell. */
5905 if (flags & DRAW_UNDERL)
5907 /* When p_linespace is 0, overwrite the bottom row of pixels.
5908 * Otherwise put the line just below the character. */
5909 if (p_linespace > 1)
5910 y -= p_linespace - 1;
5911 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5912 FILL_X(col), y,
5913 FILL_X(col + cells) - 1, y);
5917 #if defined(HAVE_GTK2) || defined(PROTO)
5919 gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5921 GdkRectangle area; /* area for clip mask */
5922 PangoGlyphString *glyphs; /* glyphs of current item */
5923 int column_offset = 0; /* column offset in cells */
5924 int i;
5925 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5926 char_u *new_conv_buf;
5927 int convlen;
5928 char_u *sp, *bp;
5929 int plen;
5931 if (gui.text_context == NULL || gui.drawarea->window == NULL)
5932 return len;
5934 if (output_conv.vc_type != CONV_NONE)
5937 * Convert characters from 'encoding' to 'termencoding', which is set
5938 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5939 * prohibits changing this to something else than UTF-8 if the GUI is
5940 * in use.
5942 convlen = len;
5943 conv_buf = string_convert(&output_conv, s, &convlen);
5944 g_return_val_if_fail(conv_buf != NULL, len);
5946 /* Correct for differences in char width: some chars are
5947 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5948 * compensate for that. */
5949 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5951 plen = utf_ptr2len(bp);
5952 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5954 new_conv_buf = alloc(convlen + 2);
5955 if (new_conv_buf == NULL)
5956 return len;
5957 plen += bp - conv_buf;
5958 mch_memmove(new_conv_buf, conv_buf, plen);
5959 new_conv_buf[plen] = ' ';
5960 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5961 convlen - plen + 1);
5962 vim_free(conv_buf);
5963 conv_buf = new_conv_buf;
5964 ++convlen;
5965 bp = conv_buf + plen;
5966 plen = 1;
5968 sp += (*mb_ptr2len)(sp);
5969 bp += plen;
5971 s = conv_buf;
5972 len = convlen;
5976 * Restrict all drawing to the current screen line in order to prevent
5977 * fuzzy font lookups from messing up the screen.
5979 area.x = gui.border_offset;
5980 area.y = FILL_Y(row);
5981 area.width = gui.num_cols * gui.char_width;
5982 area.height = gui.char_height;
5984 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5985 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
5987 glyphs = pango_glyph_string_new();
5990 * Optimization hack: If possible, skip the itemize and shaping process
5991 * for pure ASCII strings. This optimization is particularly effective
5992 * because Vim draws space characters to clear parts of the screen.
5994 if (!(flags & DRAW_ITALIC)
5995 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5996 && gui.ascii_glyphs != NULL)
5998 char_u *p;
6000 for (p = s; p < s + len; ++p)
6001 if (*p & 0x80)
6002 goto not_ascii;
6004 pango_glyph_string_set_size(glyphs, len);
6006 for (i = 0; i < len; ++i)
6008 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
6009 glyphs->log_clusters[i] = i;
6012 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
6014 column_offset = len;
6016 else
6017 not_ascii:
6019 PangoAttrList *attr_list;
6020 GList *item_list;
6021 int cluster_width;
6022 int last_glyph_rbearing;
6023 int cells = 0; /* cells occupied by current cluster */
6024 #if 0
6025 int monospace13 = STRICMP(p_guifont, "monospace 13") == 0;
6026 #endif
6028 /* Safety check: pango crashes when invoked with invalid utf-8
6029 * characters. */
6030 if (!utf_valid_string(s, s + len))
6032 column_offset = len;
6033 goto skipitall;
6036 /* original width of the current cluster */
6037 cluster_width = PANGO_SCALE * gui.char_width;
6039 /* right bearing of the last non-composing glyph */
6040 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
6042 attr_list = pango_attr_list_new();
6044 /* If 'guifontwide' is set then use that for double-width characters.
6045 * Otherwise just go with 'guifont' and let Pango do its thing. */
6046 if (gui.wide_font != NULL)
6047 apply_wide_font_attr(s, len, attr_list);
6049 if ((flags & DRAW_BOLD) && gui.font_can_bold)
6050 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
6051 attr_list, 0, len);
6052 if (flags & DRAW_ITALIC)
6053 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
6054 attr_list, 0, len);
6056 * Break the text into segments with consistent directional level
6057 * and shaping engine. Pure Latin text needs only a single segment,
6058 * so there's no need to worry about the loop's efficiency. Better
6059 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6061 item_list = pango_itemize(gui.text_context,
6062 (const char *)s, 0, len, attr_list, NULL);
6064 while (item_list != NULL)
6066 PangoItem *item;
6067 int item_cells = 0; /* item length in cells */
6069 item = (PangoItem *)item_list->data;
6070 item_list = g_list_delete_link(item_list, item_list);
6072 * Increment the bidirectional embedding level by 1 if it is not
6073 * even. An odd number means the output will be RTL, but we don't
6074 * want that since Vim handles right-to-left text on its own. It
6075 * would probably be sufficient to just set level = 0, but you can
6076 * never know :)
6078 * Unfortunately we can't take advantage of Pango's ability to
6079 * render both LTR and RTL at the same time. In order to support
6080 * that, Vim's main screen engine would have to make use of Pango
6081 * functionality.
6083 item->analysis.level = (item->analysis.level + 1) & (~1U);
6085 /* HACK: Overrule the shape engine, we don't want shaping to be
6086 * done, because drawing the cursor would change the display. */
6087 item->analysis.shape_engine = default_shape_engine;
6089 pango_shape((const char *)s + item->offset, item->length,
6090 &item->analysis, glyphs);
6092 * Fixed-width hack: iterate over the array and assign a fixed
6093 * width to each glyph, thus overriding the choice made by the
6094 * shaping engine. We use utf_char2cells() to determine the
6095 * number of cells needed.
6097 * Also perform all kind of dark magic to get composing
6098 * characters right (and pretty too of course).
6100 for (i = 0; i < glyphs->num_glyphs; ++i)
6102 PangoGlyphInfo *glyph;
6104 glyph = &glyphs->glyphs[i];
6106 if (glyph->attr.is_cluster_start)
6108 int cellcount;
6110 cellcount = count_cluster_cells(
6111 s, item, glyphs, i, &cluster_width,
6112 (item_list != NULL) ? &last_glyph_rbearing : NULL);
6114 if (cellcount > 0)
6116 int width;
6118 width = cellcount * gui.char_width * PANGO_SCALE;
6119 glyph->geometry.x_offset +=
6120 MAX(0, width - cluster_width) / 2;
6121 glyph->geometry.width = width;
6123 else
6125 /* If there are only combining characters in the
6126 * cluster, we cannot just change the width of the
6127 * previous glyph since there is none. Therefore
6128 * some guesswork is needed. */
6129 setup_zero_width_cluster(item, glyph, cells,
6130 cluster_width,
6131 last_glyph_rbearing);
6134 item_cells += cellcount;
6135 cells = cellcount;
6137 else if (i > 0)
6139 int width;
6141 /* There is a previous glyph, so we deal with combining
6142 * characters the canonical way. That is, setting the
6143 * width of the previous glyph to 0. */
6144 glyphs->glyphs[i - 1].geometry.width = 0;
6145 width = cells * gui.char_width * PANGO_SCALE;
6146 glyph->geometry.x_offset +=
6147 MAX(0, width - cluster_width) / 2;
6148 #if 0
6149 /* Dirty hack: for "monospace 13" font there is a bug that
6150 * draws composing chars in the wrong position. Add
6151 * "width" to the offset to work around that. */
6152 if (monospace13)
6153 glyph->geometry.x_offset = width;
6154 #endif
6156 glyph->geometry.width = width;
6158 else /* i == 0 "cannot happen" */
6160 glyph->geometry.width = 0;
6164 /*** Aaaaand action! ***/
6165 draw_glyph_string(row, col + column_offset, item_cells,
6166 flags, item->analysis.font, glyphs);
6168 pango_item_free(item);
6170 column_offset += item_cells;
6173 pango_attr_list_unref(attr_list);
6176 skipitall:
6177 /* Draw underline and undercurl. */
6178 draw_under(flags, row, col, column_offset);
6180 pango_glyph_string_free(glyphs);
6181 vim_free(conv_buf);
6183 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
6185 return column_offset;
6187 #endif /* HAVE_GTK2 */
6189 #if !defined(HAVE_GTK2) || defined(PROTO)
6190 void
6191 gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
6193 static XChar2b *buf = NULL;
6194 static int buflen = 0;
6195 int is_wide;
6196 XChar2b *text;
6197 int textlen;
6198 XFontStruct *xfont;
6199 char_u *p;
6200 # ifdef FEAT_MBYTE
6201 unsigned c;
6202 # endif
6203 int width;
6205 if (gui.current_font == NULL || gui.drawarea->window == NULL)
6206 return;
6209 * Yeah yeah apparently the font support in GTK+ 1.2 only cares for either:
6210 * asians or 8-bit fonts. It is broken there, but no wonder the whole font
6211 * stuff is broken in X11 in first place. And the internationalization API
6212 * isn't something you would really like to use.
6215 xfont = (XFontStruct *)((GdkFontPrivate*)gui.current_font)->xfont;
6216 is_wide = ((xfont->min_byte1 != 0 || xfont->max_byte1 != 0)
6217 # ifdef FEAT_XFONTSET
6218 && gui.fontset == NOFONTSET
6219 # endif
6222 if (is_wide)
6224 /* Convert a byte sequence to 16 bit characters for the Gdk functions.
6225 * Need a buffer for the 16 bit characters. Keep it between calls,
6226 * because allocating it each time is slow. */
6227 if (buflen < len)
6229 XtFree((char *)buf);
6230 buf = (XChar2b *)XtMalloc(len * sizeof(XChar2b));
6231 buflen = len;
6234 p = s;
6235 textlen = 0;
6236 width = 0;
6237 while (p < s + len)
6239 # ifdef FEAT_MBYTE
6240 if (enc_utf8)
6242 int pcc[MAX_MCO];
6244 /* TODO: use the composing characters */
6245 c = utfc_ptr2char_len(p, pcc, len - (p - s));
6246 if (c >= 0x10000) /* show chars > 0xffff as ? */
6247 c = 0xbf;
6248 buf[textlen].byte1 = c >> 8;
6249 buf[textlen].byte2 = c;
6250 p += utfc_ptr2len_len(p, len - (p - s));
6251 width += utf_char2cells(c);
6253 else
6254 # endif
6256 buf[textlen].byte1 = '\0'; /* high eight bits */
6257 buf[textlen].byte2 = *p; /* low eight bits */
6258 ++p;
6259 ++width;
6261 ++textlen;
6263 text = buf;
6264 textlen = textlen * 2;
6266 else
6268 text = (XChar2b *)s;
6269 textlen = len;
6270 # ifdef FEAT_MBYTE
6271 if (has_mbyte)
6273 width = 0;
6274 for (p = s; p < s + len; p += (*mb_ptr2len_len)(p, len - (p - s)))
6275 width += (*mb_ptr2cells_len)(p, len - (p - s));
6277 else
6278 # endif
6279 width = len;
6282 if (!(flags & DRAW_TRANSP))
6284 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6285 gdk_draw_rectangle(gui.drawarea->window,
6286 gui.text_gc,
6287 TRUE,
6288 FILL_X(col), FILL_Y(row),
6289 width * gui.char_width, gui.char_height);
6291 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6292 gdk_draw_text(gui.drawarea->window,
6293 gui.current_font,
6294 gui.text_gc,
6295 TEXT_X(col), TEXT_Y(row),
6296 (const gchar *)text, textlen);
6298 /* redraw the contents with an offset of 1 to emulate bold */
6299 if (flags & DRAW_BOLD)
6300 gdk_draw_text(gui.drawarea->window,
6301 gui.current_font,
6302 gui.text_gc,
6303 TEXT_X(col) + 1, TEXT_Y(row),
6304 (const gchar *)text, textlen);
6306 /* Draw underline and undercurl. */
6307 draw_under(flags, row, col, width);
6309 #endif /* !HAVE_GTK2 */
6312 * Return OK if the key with the termcap name "name" is supported.
6315 gui_mch_haskey(char_u *name)
6317 int i;
6319 for (i = 0; special_keys[i].key_sym != 0; i++)
6320 if (name[0] == special_keys[i].code0
6321 && name[1] == special_keys[i].code1)
6322 return OK;
6323 return FAIL;
6326 #if defined(FEAT_TITLE) \
6327 || (defined(FEAT_XIM) && !defined(HAVE_GTK2)) \
6328 || defined(PROTO)
6330 * Return the text window-id and display. Only required for X-based GUI's
6333 gui_get_x11_windis(Window *win, Display **dis)
6335 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6337 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6338 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
6339 return OK;
6342 *dis = NULL;
6343 *win = 0;
6344 return FAIL;
6346 #endif
6348 #if defined(FEAT_CLIENTSERVER) \
6349 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6351 Display *
6352 gui_mch_get_display(void)
6354 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6355 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6356 else
6357 return NULL;
6359 #endif
6361 void
6362 gui_mch_beep(void)
6364 #ifdef HAVE_GTK_MULTIHEAD
6365 GdkDisplay *display;
6367 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6368 display = gtk_widget_get_display(gui.mainwin);
6369 else
6370 display = gdk_display_get_default();
6372 if (display != NULL)
6373 gdk_display_beep(display);
6374 #else
6375 gdk_beep();
6376 #endif
6379 void
6380 gui_mch_flash(int msec)
6382 GdkGCValues values;
6383 GdkGC *invert_gc;
6385 if (gui.drawarea->window == NULL)
6386 return;
6388 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6389 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6390 values.function = GDK_XOR;
6391 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6392 &values,
6393 GDK_GC_FOREGROUND |
6394 GDK_GC_BACKGROUND |
6395 GDK_GC_FUNCTION);
6396 gdk_gc_set_exposures(invert_gc,
6397 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6399 * Do a visual beep by changing back and forth in some undetermined way,
6400 * the foreground and background colors. This is due to the fact that
6401 * there can't be really any prediction about the effects of XOR on
6402 * arbitrary X11 servers. However this seems to be enough for what we
6403 * intend it to do.
6405 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6406 TRUE,
6407 0, 0,
6408 FILL_X((int)Columns) + gui.border_offset,
6409 FILL_Y((int)Rows) + gui.border_offset);
6411 gui_mch_flush();
6412 ui_delay((long)msec, TRUE); /* wait so many msec */
6414 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6415 TRUE,
6416 0, 0,
6417 FILL_X((int)Columns) + gui.border_offset,
6418 FILL_Y((int)Rows) + gui.border_offset);
6420 gdk_gc_destroy(invert_gc);
6424 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6426 void
6427 gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6429 GdkGCValues values;
6430 GdkGC *invert_gc;
6432 if (gui.drawarea->window == NULL)
6433 return;
6435 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6436 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6437 values.function = GDK_XOR;
6438 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6439 &values,
6440 GDK_GC_FOREGROUND |
6441 GDK_GC_BACKGROUND |
6442 GDK_GC_FUNCTION);
6443 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6444 GDK_VISIBILITY_UNOBSCURED);
6445 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6446 TRUE,
6447 FILL_X(c), FILL_Y(r),
6448 (nc) * gui.char_width, (nr) * gui.char_height);
6449 gdk_gc_destroy(invert_gc);
6453 * Iconify the GUI window.
6455 void
6456 gui_mch_iconify(void)
6458 #ifdef HAVE_GTK2
6459 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
6460 #else
6461 XIconifyWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
6462 GDK_WINDOW_XWINDOW(gui.mainwin->window),
6463 DefaultScreen(GDK_WINDOW_XDISPLAY(gui.mainwin->window)));
6464 #endif
6467 #if defined(FEAT_EVAL) || defined(PROTO)
6469 * Bring the Vim window to the foreground.
6471 void
6472 gui_mch_set_foreground(void)
6474 # ifdef HAVE_GTK2
6475 gtk_window_present(GTK_WINDOW(gui.mainwin));
6476 # else
6477 gdk_window_raise(gui.mainwin->window);
6478 # endif
6480 #endif
6483 * Draw a cursor without focus.
6485 void
6486 gui_mch_draw_hollow_cursor(guicolor_T color)
6488 int i = 1;
6490 if (gui.drawarea->window == NULL)
6491 return;
6493 gui_mch_set_fg_color(color);
6495 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6496 #ifdef FEAT_MBYTE
6497 if (mb_lefthalve(gui.row, gui.col))
6498 i = 2;
6499 #endif
6500 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6501 FALSE,
6502 FILL_X(gui.col), FILL_Y(gui.row),
6503 i * gui.char_width - 1, gui.char_height - 1);
6507 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6508 * color "color".
6510 void
6511 gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6513 if (gui.drawarea->window == NULL)
6514 return;
6516 gui_mch_set_fg_color(color);
6518 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6519 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6520 TRUE,
6521 #ifdef FEAT_RIGHTLEFT
6522 /* vertical line should be on the right of current point */
6523 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6524 #endif
6525 FILL_X(gui.col),
6526 FILL_Y(gui.row) + gui.char_height - h,
6527 w, h);
6532 * Catch up with any queued X11 events. This may put keyboard input into the
6533 * input buffer, call resize call-backs, trigger timers etc. If there is
6534 * nothing in the X11 event queue (& no timers pending), then we return
6535 * immediately.
6537 void
6538 gui_mch_update(void)
6540 while (gtk_events_pending() && !vim_is_input_buf_full())
6541 gtk_main_iteration_do(FALSE);
6544 static gint
6545 input_timer_cb(gpointer data)
6547 int *timed_out = (int *) data;
6549 /* Just inform the caller about the occurrence of it */
6550 *timed_out = TRUE;
6552 if (gtk_main_level() > 0)
6553 gtk_main_quit();
6555 return FALSE; /* don't happen again */
6558 #ifdef FEAT_SNIFF
6560 * Callback function, used when data is available on the SNiFF connection.
6562 static void
6563 sniff_request_cb(
6564 gpointer data,
6565 gint source_fd,
6566 GdkInputCondition condition)
6568 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
6570 add_to_input_buf(bytes, 3);
6572 if (gtk_main_level() > 0)
6573 gtk_main_quit();
6575 #endif
6578 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6579 * from the keyboard.
6580 * wtime == -1 Wait forever.
6581 * wtime == 0 This should never happen.
6582 * wtime > 0 Wait wtime milliseconds for a character.
6583 * Returns OK if a character was found to be available within the given time,
6584 * or FAIL otherwise.
6587 gui_mch_wait_for_chars(long wtime)
6589 int focus;
6590 guint timer;
6591 static int timed_out;
6592 #ifdef FEAT_SNIFF
6593 static int sniff_on = 0;
6594 static gint sniff_input_id = 0;
6595 #endif
6597 #ifdef FEAT_SNIFF
6598 if (sniff_on && !want_sniff_request)
6600 if (sniff_input_id)
6601 gdk_input_remove(sniff_input_id);
6602 sniff_on = 0;
6604 else if (!sniff_on && want_sniff_request)
6606 /* Add fd_from_sniff to watch for available data in main loop. */
6607 sniff_input_id = gdk_input_add(fd_from_sniff,
6608 GDK_INPUT_READ, sniff_request_cb, NULL);
6609 sniff_on = 1;
6611 #endif
6613 timed_out = FALSE;
6615 /* this timeout makes sure that we will return if no characters arrived in
6616 * time */
6618 if (wtime > 0)
6619 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
6620 else
6621 timer = 0;
6623 focus = gui.in_focus;
6627 /* Stop or start blinking when focus changes */
6628 if (gui.in_focus != focus)
6630 if (gui.in_focus)
6631 gui_mch_start_blink();
6632 else
6633 gui_mch_stop_blink();
6634 focus = gui.in_focus;
6637 #if defined(FEAT_NETBEANS_INTG)
6638 /* Process the queued netbeans messages. */
6639 netbeans_parse_messages();
6640 #endif
6643 * Loop in GTK+ processing until a timeout or input occurs.
6644 * Skip this if input is available anyway (can happen in rare
6645 * situations, sort of race condition).
6647 if (!input_available())
6648 gtk_main();
6650 /* Got char, return immediately */
6651 if (input_available())
6653 if (timer != 0 && !timed_out)
6654 gtk_timeout_remove(timer);
6655 return OK;
6657 } while (wtime < 0 || !timed_out);
6660 * Flush all eventually pending (drawing) events.
6662 gui_mch_update();
6664 return FAIL;
6668 /****************************************************************************
6669 * Output drawing routines.
6670 ****************************************************************************/
6673 /* Flush any output to the screen */
6674 void
6675 gui_mch_flush(void)
6677 #ifdef HAVE_GTK_MULTIHEAD
6678 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6679 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6680 #else
6681 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6682 #endif
6683 #ifdef HAVE_GTK2
6684 /* This happens to actually do what gui_mch_flush() is supposed to do,
6685 * according to the comment above. */
6686 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
6687 gdk_window_process_updates(gui.drawarea->window, FALSE);
6688 #endif
6692 * Clear a rectangular region of the screen from text pos (row1, col1) to
6693 * (row2, col2) inclusive.
6695 void
6696 gui_mch_clear_block(int row1, int col1, int row2, int col2)
6698 GdkColor color;
6700 if (gui.drawarea->window == NULL)
6701 return;
6703 color.pixel = gui.back_pixel;
6705 gdk_gc_set_foreground(gui.text_gc, &color);
6707 /* Clear one extra pixel at the far right, for when bold characters have
6708 * spilled over to the window border. */
6709 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6710 FILL_X(col1), FILL_Y(row1),
6711 (col2 - col1 + 1) * gui.char_width
6712 + (col2 == Columns - 1),
6713 (row2 - row1 + 1) * gui.char_height);
6716 void
6717 gui_mch_clear_all(void)
6719 if (gui.drawarea->window != NULL)
6720 gdk_window_clear(gui.drawarea->window);
6724 * Redraw any text revealed by scrolling up/down.
6726 static void
6727 check_copy_area(void)
6729 GdkEvent *event;
6730 int expose_count;
6732 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6733 return;
6735 /* Avoid redrawing the cursor while scrolling or it'll end up where
6736 * we don't want it to be. I'm not sure if it's correct to call
6737 * gui_dont_update_cursor() at this point but it works as a quick
6738 * fix for now. */
6739 gui_dont_update_cursor();
6743 /* Wait to check whether the scroll worked or not. */
6744 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6746 if (event == NULL)
6747 break; /* received NoExpose event */
6749 gui_redraw(event->expose.area.x, event->expose.area.y,
6750 event->expose.area.width, event->expose.area.height);
6752 expose_count = event->expose.count;
6753 gdk_event_free(event);
6755 while (expose_count > 0); /* more events follow */
6757 gui_can_update_cursor();
6761 * Delete the given number of lines from the given row, scrolling up any
6762 * text further down within the scroll region.
6764 void
6765 gui_mch_delete_lines(int row, int num_lines)
6767 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6768 return; /* Can't see the window */
6770 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6771 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6773 /* copy one extra pixel, for when bold has spilled over */
6774 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6775 FILL_X(gui.scroll_region_left), FILL_Y(row),
6776 gui.drawarea->window,
6777 FILL_X(gui.scroll_region_left),
6778 FILL_Y(row + num_lines),
6779 gui.char_width * (gui.scroll_region_right
6780 - gui.scroll_region_left + 1) + 1,
6781 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6783 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6784 gui.scroll_region_left,
6785 gui.scroll_region_bot, gui.scroll_region_right);
6786 check_copy_area();
6790 * Insert the given number of lines before the given row, scrolling down any
6791 * following text within the scroll region.
6793 void
6794 gui_mch_insert_lines(int row, int num_lines)
6796 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6797 return; /* Can't see the window */
6799 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6800 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6802 /* copy one extra pixel, for when bold has spilled over */
6803 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6804 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6805 gui.drawarea->window,
6806 FILL_X(gui.scroll_region_left), FILL_Y(row),
6807 gui.char_width * (gui.scroll_region_right
6808 - gui.scroll_region_left + 1) + 1,
6809 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6811 gui_clear_block(row, gui.scroll_region_left,
6812 row + num_lines - 1, gui.scroll_region_right);
6813 check_copy_area();
6817 * X Selection stuff, for cutting and pasting text to other windows.
6819 void
6820 clip_mch_request_selection(VimClipboard *cbd)
6822 GdkAtom target;
6823 unsigned i;
6824 time_t start;
6826 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6828 #ifdef FEAT_MBYTE
6829 if (!clip_html && selection_targets[i].info == TARGET_HTML)
6830 continue;
6831 #endif
6832 received_selection = RS_NONE;
6833 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6835 gtk_selection_convert(gui.drawarea,
6836 cbd->gtk_sel_atom, target,
6837 (guint32)GDK_CURRENT_TIME);
6839 /* Hack: Wait up to three seconds for the selection. A hang was
6840 * noticed here when using the netrw plugin combined with ":gui"
6841 * during the FocusGained event. */
6842 start = time(NULL);
6843 while (received_selection == RS_NONE && time(NULL) < start + 3)
6844 gtk_main(); /* wait for selection_received_cb */
6846 if (received_selection != RS_FAIL)
6847 return;
6850 /* Final fallback position - use the X CUT_BUFFER0 store */
6851 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
6855 * Disown the selection.
6857 void
6858 clip_mch_lose_selection(VimClipboard *cbd UNUSED)
6860 /* WEIRD: when using NULL to actually disown the selection, we lose the
6861 * selection the first time we own it. */
6863 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
6864 gui_mch_update();
6869 * Own the selection and return OK if it worked.
6872 clip_mch_own_selection(VimClipboard *cbd)
6874 int success;
6876 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
6877 (guint32)GDK_CURRENT_TIME);
6878 gui_mch_update();
6879 return (success) ? OK : FAIL;
6883 * Send the current selection to the clipboard. Do nothing for X because we
6884 * will fill in the selection only when requested by another app.
6886 void
6887 clip_mch_set_selection(VimClipboard *cbd UNUSED)
6892 #if defined(FEAT_MENU) || defined(PROTO)
6894 * Make a menu item appear either active or not active (grey or not grey).
6896 void
6897 gui_mch_menu_grey(vimmenu_T *menu, int grey)
6899 if (menu->id == NULL)
6900 return;
6902 if (menu_is_separator(menu->name))
6903 grey = TRUE;
6905 gui_mch_menu_hidden(menu, FALSE);
6906 /* Be clever about bitfields versus true booleans here! */
6907 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
6909 gtk_widget_set_sensitive(menu->id, !grey);
6910 gui_mch_update();
6915 * Make menu item hidden or not hidden.
6917 void
6918 gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6920 if (menu->id == 0)
6921 return;
6923 if (hidden)
6925 if (GTK_WIDGET_VISIBLE(menu->id))
6927 gtk_widget_hide(menu->id);
6928 gui_mch_update();
6931 else
6933 if (!GTK_WIDGET_VISIBLE(menu->id))
6935 gtk_widget_show(menu->id);
6936 gui_mch_update();
6942 * This is called after setting all the menus to grey/hidden or not.
6944 void
6945 gui_mch_draw_menubar(void)
6947 /* just make sure that the visual changes get effect immediately */
6948 gui_mch_update();
6950 #endif /* FEAT_MENU */
6953 * Scrollbar stuff.
6955 void
6956 gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
6958 if (sb->id == NULL)
6959 return;
6961 if (flag)
6962 gtk_widget_show(sb->id);
6963 else
6964 gtk_widget_hide(sb->id);
6966 update_window_manager_hints(0, 0);
6971 * Return the RGB value of a pixel as long.
6973 long_u
6974 gui_mch_get_rgb(guicolor_T pixel)
6976 GdkColor color;
6977 #ifndef HAVE_GTK2
6978 GdkColorContext *cc;
6980 cc = gdk_color_context_new(gtk_widget_get_visual(gui.drawarea),
6981 gtk_widget_get_colormap(gui.drawarea));
6982 color.pixel = pixel;
6983 gdk_color_context_query_color(cc, &color);
6985 gdk_color_context_free(cc);
6986 #else
6987 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
6988 (unsigned long)pixel, &color);
6989 #endif
6991 return (((unsigned)color.red & 0xff00) << 8)
6992 | ((unsigned)color.green & 0xff00)
6993 | (((unsigned)color.blue & 0xff00) >> 8);
6997 * Get current mouse coordinates in text window.
6999 void
7000 gui_mch_getmouse(int *x, int *y)
7002 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
7005 void
7006 gui_mch_setmouse(int x, int y)
7008 /* Sorry for the Xlib call, but we can't avoid it, since there is no
7009 * internal GDK mechanism present to accomplish this. (and for good
7010 * reason...) */
7011 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
7012 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
7013 0, 0, 0U, 0U, x, y);
7017 #ifdef FEAT_MOUSESHAPE
7018 /* The last set mouse pointer shape is remembered, to be used when it goes
7019 * from hidden to not hidden. */
7020 static int last_shape = 0;
7021 #endif
7024 * Use the blank mouse pointer or not.
7026 * hide: TRUE = use blank ptr, FALSE = use parent ptr
7028 void
7029 gui_mch_mousehide(int hide)
7031 if (gui.pointer_hidden != hide)
7033 gui.pointer_hidden = hide;
7034 if (gui.drawarea->window && gui.blank_pointer != NULL)
7036 if (hide)
7037 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
7038 else
7039 #ifdef FEAT_MOUSESHAPE
7040 mch_set_mouse_shape(last_shape);
7041 #else
7042 gdk_window_set_cursor(gui.drawarea->window, NULL);
7043 #endif
7048 #if defined(FEAT_MOUSESHAPE) || defined(PROTO)
7050 /* Table for shape IDs. Keep in sync with the mshape_names[] table in
7051 * misc2.c! */
7052 static const int mshape_ids[] =
7054 GDK_LEFT_PTR, /* arrow */
7055 GDK_CURSOR_IS_PIXMAP, /* blank */
7056 GDK_XTERM, /* beam */
7057 GDK_SB_V_DOUBLE_ARROW, /* updown */
7058 GDK_SIZING, /* udsizing */
7059 GDK_SB_H_DOUBLE_ARROW, /* leftright */
7060 GDK_SIZING, /* lrsizing */
7061 GDK_WATCH, /* busy */
7062 GDK_X_CURSOR, /* no */
7063 GDK_CROSSHAIR, /* crosshair */
7064 GDK_HAND1, /* hand1 */
7065 GDK_HAND2, /* hand2 */
7066 GDK_PENCIL, /* pencil */
7067 GDK_QUESTION_ARROW, /* question */
7068 GDK_RIGHT_PTR, /* right-arrow */
7069 GDK_CENTER_PTR, /* up-arrow */
7070 GDK_LEFT_PTR /* last one */
7073 void
7074 mch_set_mouse_shape(int shape)
7076 int id;
7077 GdkCursor *c;
7079 if (gui.drawarea->window == NULL)
7080 return;
7082 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
7083 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
7084 else
7086 if (shape >= MSHAPE_NUMBERED)
7088 id = shape - MSHAPE_NUMBERED;
7089 if (id >= GDK_LAST_CURSOR)
7090 id = GDK_LEFT_PTR;
7091 else
7092 id &= ~1; /* they are always even (why?) */
7094 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
7095 id = mshape_ids[shape];
7096 else
7097 return;
7098 # ifdef HAVE_GTK_MULTIHEAD
7099 c = gdk_cursor_new_for_display(
7100 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
7101 # else
7102 c = gdk_cursor_new((GdkCursorType)id);
7103 # endif
7104 gdk_window_set_cursor(gui.drawarea->window, c);
7105 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
7107 if (shape != MSHAPE_HIDE)
7108 last_shape = shape;
7110 #endif /* FEAT_MOUSESHAPE */
7113 #if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7115 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7116 * scaled down if the current font is not big enough, or scaled up if the image
7117 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7118 * will be cut off if the current font is not big enough, or centered if it's
7119 * too small.
7121 # define SIGN_WIDTH (2 * gui.char_width)
7122 # define SIGN_HEIGHT (gui.char_height)
7123 # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7125 # ifdef HAVE_GTK2
7127 void
7128 gui_mch_drawsign(int row, int col, int typenr)
7130 GdkPixbuf *sign;
7132 sign = (GdkPixbuf *)sign_get_image(typenr);
7134 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
7136 int width;
7137 int height;
7138 int xoffset;
7139 int yoffset;
7140 int need_scale;
7142 width = gdk_pixbuf_get_width(sign);
7143 height = gdk_pixbuf_get_height(sign);
7145 * Decide whether we need to scale. Allow one pixel of border
7146 * width to be cut off, in order to avoid excessive scaling for
7147 * tiny differences in font size.
7149 need_scale = (width > SIGN_WIDTH + 2
7150 || height > SIGN_HEIGHT + 2
7151 || (width < 3 * SIGN_WIDTH / 4
7152 && height < 3 * SIGN_HEIGHT / 4));
7153 if (need_scale)
7155 double aspect;
7157 /* Keep the original aspect ratio */
7158 aspect = (double)height / (double)width;
7159 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7160 width = MIN(width, SIGN_WIDTH);
7161 height = (double)width * aspect;
7163 /* This doesn't seem to be worth caching, and doing so
7164 * would complicate the code quite a bit. */
7165 sign = gdk_pixbuf_scale_simple(sign, width, height,
7166 GDK_INTERP_BILINEAR);
7167 if (sign == NULL)
7168 return; /* out of memory */
7171 /* The origin is the upper-left corner of the pixmap. Therefore
7172 * these offset may become negative if the pixmap is smaller than
7173 * the 2x1 cells reserved for the sign icon. */
7174 xoffset = (width - SIGN_WIDTH) / 2;
7175 yoffset = (height - SIGN_HEIGHT) / 2;
7177 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7179 gdk_draw_rectangle(gui.drawarea->window,
7180 gui.text_gc,
7181 TRUE,
7182 FILL_X(col),
7183 FILL_Y(row),
7184 SIGN_WIDTH,
7185 SIGN_HEIGHT);
7187 # if GTK_CHECK_VERSION(2,1,1)
7188 gdk_draw_pixbuf(gui.drawarea->window,
7189 NULL,
7190 sign,
7191 MAX(0, xoffset),
7192 MAX(0, yoffset),
7193 FILL_X(col) - MIN(0, xoffset),
7194 FILL_Y(row) - MIN(0, yoffset),
7195 MIN(width, SIGN_WIDTH),
7196 MIN(height, SIGN_HEIGHT),
7197 GDK_RGB_DITHER_NORMAL,
7198 0, 0);
7199 # else
7200 gdk_pixbuf_render_to_drawable_alpha(sign,
7201 gui.drawarea->window,
7202 MAX(0, xoffset),
7203 MAX(0, yoffset),
7204 FILL_X(col) - MIN(0, xoffset),
7205 FILL_Y(row) - MIN(0, yoffset),
7206 MIN(width, SIGN_WIDTH),
7207 MIN(height, SIGN_HEIGHT),
7208 GDK_PIXBUF_ALPHA_BILEVEL,
7209 127,
7210 GDK_RGB_DITHER_NORMAL,
7211 0, 0);
7212 # endif
7213 if (need_scale)
7214 g_object_unref(sign);
7218 void *
7219 gui_mch_register_sign(char_u *signfile)
7221 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7223 GdkPixbuf *sign;
7224 GError *error = NULL;
7225 char_u *message;
7227 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7229 if (error == NULL)
7230 return sign;
7232 message = (char_u *)error->message;
7234 if (message != NULL && input_conv.vc_type != CONV_NONE)
7235 message = string_convert(&input_conv, message, NULL);
7237 if (message != NULL)
7239 /* The error message is already translated and will be more
7240 * descriptive than anything we could possibly do ourselves. */
7241 EMSG2("E255: %s", message);
7243 if (input_conv.vc_type != CONV_NONE)
7244 vim_free(message);
7246 g_error_free(error);
7249 return NULL;
7252 void
7253 gui_mch_destroy_sign(void *sign)
7255 if (sign != NULL)
7256 g_object_unref(sign);
7259 # else /* !HAVE_GTK2 */
7261 typedef struct
7263 GdkPixmap *pixmap;
7264 GdkBitmap *mask;
7266 signicon_T;
7268 void
7269 gui_mch_drawsign(int row, int col, int typenr)
7271 signicon_T *sign;
7273 sign = (signicon_T *)sign_get_image(typenr);
7275 if (sign != NULL && sign->pixmap != NULL
7276 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7278 int width;
7279 int height;
7280 int xoffset;
7281 int yoffset;
7283 gdk_window_get_size(sign->pixmap, &width, &height);
7285 /* The origin is the upper-left corner of the pixmap. Therefore
7286 * these offset may become negative if the pixmap is smaller than
7287 * the 2x1 cells reserved for the sign icon. */
7288 xoffset = (width - SIGN_WIDTH) / 2;
7289 yoffset = (height - SIGN_HEIGHT) / 2;
7291 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7293 gdk_draw_rectangle(gui.drawarea->window,
7294 gui.text_gc,
7295 TRUE,
7296 FILL_X(col),
7297 FILL_Y(row),
7298 SIGN_WIDTH,
7299 SIGN_HEIGHT);
7301 /* Set the clip mask for bilevel transparency */
7302 if (sign->mask != NULL)
7304 gdk_gc_set_clip_origin(gui.text_gc,
7305 FILL_X(col) - xoffset,
7306 FILL_Y(row) - yoffset);
7307 gdk_gc_set_clip_mask(gui.text_gc, sign->mask);
7310 gdk_draw_pixmap(gui.drawarea->window,
7311 gui.text_gc,
7312 sign->pixmap,
7313 MAX(0, xoffset),
7314 MAX(0, yoffset),
7315 FILL_X(col) - MIN(0, xoffset),
7316 FILL_Y(row) - MIN(0, yoffset),
7317 MIN(width, SIGN_WIDTH),
7318 MIN(height, SIGN_HEIGHT));
7320 gdk_gc_set_clip_mask(gui.text_gc, NULL);
7324 void *
7325 gui_mch_register_sign(char_u *signfile)
7327 signicon_T *sign = NULL;
7329 if (signfile[0] != NUL && signfile[0] != '-'
7330 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7332 sign = (signicon_T *)alloc(sizeof(signicon_T));
7334 if (sign != NULL) /* NULL == OOM == "cannot really happen" */
7336 sign->mask = NULL;
7337 sign->pixmap = gdk_pixmap_colormap_create_from_xpm(
7338 gui.drawarea->window, NULL,
7339 &sign->mask, NULL,
7340 (const char *)signfile);
7342 if (sign->pixmap == NULL)
7344 vim_free(sign);
7345 sign = NULL;
7346 EMSG(_(e_signdata));
7350 return sign;
7353 void
7354 gui_mch_destroy_sign(void *sign)
7356 if (sign != NULL)
7358 signicon_T *signicon = (signicon_T *)sign;
7360 if (signicon->pixmap != NULL)
7361 gdk_pixmap_unref(signicon->pixmap);
7362 if (signicon->mask != NULL)
7363 gdk_bitmap_unref(signicon->mask);
7365 vim_free(signicon);
7368 # endif /* !HAVE_GTK2 */
7370 #endif /* FEAT_SIGN_ICONS */