blob: e44ae0628eb33697c790c45ba87b4a5c9eb0f222 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaar0a56cb82005-01-04 21:45:14 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
22 */
23
24#include "vim.h"
Bram Moolenaarc93e7912008-07-08 10:46:08 +000025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef FEAT_GUI_GNOME
27/* Gnome redefines _() and N_(). Grrr... */
28# ifdef _
29# undef _
30# endif
31# ifdef N_
32# undef N_
33# endif
34# ifdef textdomain
35# undef textdomain
36# endif
37# ifdef bindtextdomain
38# undef bindtextdomain
39# endif
Bram Moolenaara2dd9002007-05-14 17:38:30 +000040# ifdef bind_textdomain_codeset
41# undef bind_textdomain_codeset
Bram Moolenaar49325942007-05-10 19:19:59 +000042# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000043# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
44# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
45# endif
46# include <gnome.h>
47# include "version.h"
Bram Moolenaardb552d602006-03-23 22:59:57 +000048# ifdef HAVE_GTK2
49/* missing prototype in bonobo-dock-item.h */
50extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
51# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000052#endif
53
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
84
85# include <gtk/gtk.h>
86# include "gui_gtk_f.h"
87#endif
88
89#ifdef HAVE_X11_SUNKEYSYM_H
90# include <X11/Sunkeysym.h>
91#endif
92
93/*
94 * Easy-to-use macro for multihead support.
95 */
96#ifdef HAVE_GTK_MULTIHEAD
97# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
98 gtk_widget_get_display(gui.mainwin), atom)
99#else
100# define GET_X_ATOM(atom) ((Atom)(atom))
101#endif
102
103/* Selection type distinguishers */
104enum
105{
106 TARGET_TYPE_NONE,
107 TARGET_UTF8_STRING,
108 TARGET_STRING,
109 TARGET_COMPOUND_TEXT,
110 TARGET_TEXT,
111 TARGET_TEXT_URI_LIST,
112 TARGET_TEXT_PLAIN,
113 TARGET_VIM,
114 TARGET_VIMENC
115};
116
117/*
118 * Table of selection targets supported by Vim.
119 * Note: Order matters, preferred types should come first.
120 */
121static const GtkTargetEntry selection_targets[] =
122{
123 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
124 {VIM_ATOM_NAME, 0, TARGET_VIM},
125#ifdef FEAT_MBYTE
126 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
127#endif
128 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
129 {"TEXT", 0, TARGET_TEXT},
130 {"STRING", 0, TARGET_STRING}
131};
132#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
133
134#ifdef FEAT_DND
135/*
136 * Table of DnD targets supported by Vim.
137 * Note: Order matters, preferred types should come first.
138 */
139static const GtkTargetEntry dnd_targets[] =
140{
141 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
142# ifdef FEAT_MBYTE
143 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
144# endif
145 {"STRING", 0, TARGET_STRING},
146 {"text/plain", 0, TARGET_TEXT_PLAIN}
147};
148# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
149#endif
150
151
152#ifdef HAVE_GTK2
153/*
154 * "Monospace" is a standard font alias that should be present
155 * on all proper Pango/fontconfig installations.
156 */
157# define DEFAULT_FONT "Monospace 10"
158
159#else /* !HAVE_GTK2 */
160/*
161 * This is the single only fixed width font in X11, which seems to be present
162 * on all servers and available in all the variants we need.
163 */
164# define DEFAULT_FONT "-adobe-courier-medium-r-normal-*-14-*-*-*-m-*-*-*"
165
166#endif /* !HAVE_GTK2 */
167
168#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
169/*
170 * Atoms used to communicate save-yourself from the X11 session manager. There
171 * is no need to move them into the GUI struct, since they should be constant.
172 */
173static GdkAtom wm_protocols_atom = GDK_NONE;
174static GdkAtom save_yourself_atom = GDK_NONE;
175#endif
176
177/*
178 * Atoms used to control/reference X11 selections.
179 */
180#ifdef FEAT_MBYTE
181static GdkAtom utf8_string_atom = GDK_NONE;
182#endif
183#ifndef HAVE_GTK2
184static GdkAtom compound_text_atom = GDK_NONE;
185static GdkAtom text_atom = GDK_NONE;
186#endif
187static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
188#ifdef FEAT_MBYTE
189static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
190#endif
191
192/*
193 * Keycodes recognized by vim.
194 * NOTE: when changing this, the table in gui_x11.c probably needs the same
195 * change!
196 */
197static struct special_key
198{
199 guint key_sym;
200 char_u code0;
201 char_u code1;
202}
203const special_keys[] =
204{
205 {GDK_Up, 'k', 'u'},
206 {GDK_Down, 'k', 'd'},
207 {GDK_Left, 'k', 'l'},
208 {GDK_Right, 'k', 'r'},
209 {GDK_F1, 'k', '1'},
210 {GDK_F2, 'k', '2'},
211 {GDK_F3, 'k', '3'},
212 {GDK_F4, 'k', '4'},
213 {GDK_F5, 'k', '5'},
214 {GDK_F6, 'k', '6'},
215 {GDK_F7, 'k', '7'},
216 {GDK_F8, 'k', '8'},
217 {GDK_F9, 'k', '9'},
218 {GDK_F10, 'k', ';'},
219 {GDK_F11, 'F', '1'},
220 {GDK_F12, 'F', '2'},
221 {GDK_F13, 'F', '3'},
222 {GDK_F14, 'F', '4'},
223 {GDK_F15, 'F', '5'},
224 {GDK_F16, 'F', '6'},
225 {GDK_F17, 'F', '7'},
226 {GDK_F18, 'F', '8'},
227 {GDK_F19, 'F', '9'},
228 {GDK_F20, 'F', 'A'},
229 {GDK_F21, 'F', 'B'},
230 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
231 {GDK_F22, 'F', 'C'},
232 {GDK_F23, 'F', 'D'},
233 {GDK_F24, 'F', 'E'},
234 {GDK_F25, 'F', 'F'},
235 {GDK_F26, 'F', 'G'},
236 {GDK_F27, 'F', 'H'},
237 {GDK_F28, 'F', 'I'},
238 {GDK_F29, 'F', 'J'},
239 {GDK_F30, 'F', 'K'},
240 {GDK_F31, 'F', 'L'},
241 {GDK_F32, 'F', 'M'},
242 {GDK_F33, 'F', 'N'},
243 {GDK_F34, 'F', 'O'},
244 {GDK_F35, 'F', 'P'},
245#ifdef SunXK_F36
246 {SunXK_F36, 'F', 'Q'},
247 {SunXK_F37, 'F', 'R'},
248#endif
249 {GDK_Help, '%', '1'},
250 {GDK_Undo, '&', '8'},
251 {GDK_BackSpace, 'k', 'b'},
252 {GDK_Insert, 'k', 'I'},
253 {GDK_Delete, 'k', 'D'},
254 {GDK_3270_BackTab, 'k', 'B'},
255 {GDK_Clear, 'k', 'C'},
256 {GDK_Home, 'k', 'h'},
257 {GDK_End, '@', '7'},
258 {GDK_Prior, 'k', 'P'},
259 {GDK_Next, 'k', 'N'},
260 {GDK_Print, '%', '9'},
261 /* Keypad keys: */
262 {GDK_KP_Left, 'k', 'l'},
263 {GDK_KP_Right, 'k', 'r'},
264 {GDK_KP_Up, 'k', 'u'},
265 {GDK_KP_Down, 'k', 'd'},
266 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
267 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
268 {GDK_KP_Home, 'K', '1'},
269 {GDK_KP_End, 'K', '4'},
270 {GDK_KP_Prior, 'K', '3'}, /* page up */
271 {GDK_KP_Next, 'K', '5'}, /* page down */
272
273 {GDK_KP_Add, 'K', '6'},
274 {GDK_KP_Subtract, 'K', '7'},
275 {GDK_KP_Divide, 'K', '8'},
276 {GDK_KP_Multiply, 'K', '9'},
277 {GDK_KP_Enter, 'K', 'A'},
278 {GDK_KP_Decimal, 'K', 'B'},
279
280 {GDK_KP_0, 'K', 'C'},
281 {GDK_KP_1, 'K', 'D'},
282 {GDK_KP_2, 'K', 'E'},
283 {GDK_KP_3, 'K', 'F'},
284 {GDK_KP_4, 'K', 'G'},
285 {GDK_KP_5, 'K', 'H'},
286 {GDK_KP_6, 'K', 'I'},
287 {GDK_KP_7, 'K', 'J'},
288 {GDK_KP_8, 'K', 'K'},
289 {GDK_KP_9, 'K', 'L'},
290
291 /* End of list marker: */
292 {0, 0, 0}
293};
294
295/*
296 * Flags for command line options table below.
297 */
298#define ARG_FONT 1
299#define ARG_GEOMETRY 2
300#define ARG_REVERSE 3
301#define ARG_NOREVERSE 4
302#define ARG_BACKGROUND 5
303#define ARG_FOREGROUND 6
304#define ARG_ICONIC 7
305#define ARG_ROLE 8
306#define ARG_NETBEANS 9
307#define ARG_XRM 10 /* ignored */
308#define ARG_MENUFONT 11 /* ignored */
309#define ARG_INDEX_MASK 0x00ff
310#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
311#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
312#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
313#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
314#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
315
316/*
317 * This table holds all the X GUI command line options allowed. This includes
318 * the standard ones so that we can skip them when Vim is started without the
319 * GUI (but the GUI might start up later).
320 *
321 * When changing this, also update doc/gui_x11.txt and the usage message!!!
322 */
323typedef struct
324{
325 const char *name;
326 unsigned int flags;
327}
328cmdline_option_T;
329
330static const cmdline_option_T cmdline_options[] =
331{
332 /* We handle these options ourselves */
333 {"-fn", ARG_FONT|ARG_HAS_VALUE},
334 {"-font", ARG_FONT|ARG_HAS_VALUE},
335 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
336 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
337 {"-rv", ARG_REVERSE},
338 {"-reverse", ARG_REVERSE},
339 {"+rv", ARG_NOREVERSE},
340 {"+reverse", ARG_NOREVERSE},
341 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
342 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
343 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
344 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
345 {"-iconic", ARG_ICONIC},
346#ifdef HAVE_GTK2
347 {"--role", ARG_ROLE|ARG_HAS_VALUE},
348#endif
349#ifdef FEAT_NETBEANS_INTG
350 {"-nb", ARG_NETBEANS}, /* non-standard value format */
351 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
352 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
353 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
354#endif
355#if 0 /* not implemented; these arguments don't make sense for GTK+ */
356 {"-boldfont", ARG_HAS_VALUE},
357 {"-italicfont", ARG_HAS_VALUE},
358 {"-bw", ARG_HAS_VALUE},
359 {"-borderwidth", ARG_HAS_VALUE},
360 {"-sw", ARG_HAS_VALUE},
361 {"-scrollbarwidth", ARG_HAS_VALUE},
362#endif
363 /* Arguments handled by GTK (and GNOME) internally. */
364 {"--g-fatal-warnings", ARG_FOR_GTK},
365 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
366 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
367 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
368 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
369 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
370 {"--sync", ARG_FOR_GTK},
371 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
372 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
373 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
374#ifdef HAVE_GTK2
375 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
376 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
377 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
378#else /* these don't seem to exist anymore */
379 {"--no-xshm", ARG_FOR_GTK},
380 {"--xim-preedit", ARG_FOR_GTK|ARG_HAS_VALUE},
381 {"--xim-status", ARG_FOR_GTK|ARG_HAS_VALUE},
382 {"--gxid_host", ARG_FOR_GTK|ARG_HAS_VALUE},
383 {"--gxid_port", ARG_FOR_GTK|ARG_HAS_VALUE},
384#endif
385#ifdef FEAT_GUI_GNOME
386 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
387 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
388 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
389 {"--sm-disable", ARG_FOR_GTK},
390 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
391 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
392 {"--oaf-private", ARG_FOR_GTK},
393 {"--enable-sound", ARG_FOR_GTK},
394 {"--disable-sound", ARG_FOR_GTK},
395 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
396 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
397 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
398 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
399# if 0 /* conflicts with Vim's own --version argument */
400 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
401# endif
402 {"--disable-crash-dialog", ARG_FOR_GTK},
403#endif
404 {NULL, 0}
405};
406
407static int gui_argc = 0;
408static char **gui_argv = NULL;
409
410#ifdef HAVE_GTK2
411static const char *role_argument = NULL;
412#endif
413#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
414static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000415static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416#endif
417static int found_iconic_arg = FALSE;
418
419#ifdef FEAT_GUI_GNOME
420/*
421 * Can't use Gnome if --socketid given
422 */
423static int using_gnome = 0;
424#else
425# define using_gnome 0
426#endif
427
428/*
429 * Parse the GUI related command-line arguments. Any arguments used are
430 * deleted from argv, and *argc is decremented accordingly. This is called
431 * when vim is started, whether or not the GUI has been started.
432 */
433 void
434gui_mch_prepare(int *argc, char **argv)
435{
436 const cmdline_option_T *option;
437 int i = 0;
438 int len = 0;
439
440#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
441 /*
442 * Determine the command used to invoke Vim, to be passed as restart
443 * command to the session manager. If argv[0] contains any directory
444 * components try building an absolute path, otherwise leave it as is.
445 */
446 restart_command = argv[0];
447
448 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
449 {
450 char_u buf[MAXPATHL];
451
452 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000453 {
454 abs_restart_command = (char *)vim_strsave(buf);
455 restart_command = abs_restart_command;
456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 }
458#endif
459
460 /*
461 * Move all the entries in argv which are relevant to GTK+ and GNOME
462 * into gui_argv. Freed later in gui_mch_init().
463 */
464 gui_argc = 0;
465 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
466
467 g_return_if_fail(gui_argv != NULL);
468
469 gui_argv[gui_argc++] = argv[i++];
470
471 while (i < *argc)
472 {
473 /* Don't waste CPU cycles on non-option arguments. */
474 if (argv[i][0] != '-' && argv[i][0] != '+')
475 {
476 ++i;
477 continue;
478 }
479
480 /* Look for argv[i] in cmdline_options[] table. */
481 for (option = &cmdline_options[0]; option->name != NULL; ++option)
482 {
483 len = strlen(option->name);
484
485 if (strncmp(argv[i], option->name, len) == 0)
486 {
487 if (argv[i][len] == '\0')
488 break;
489 /* allow --foo=bar style */
490 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
491 break;
492#ifdef FEAT_NETBEANS_INTG
493 /* darn, -nb has non-standard syntax */
494 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
495 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
496 break;
497#endif
498 }
499 else if ((option->flags & ARG_COMPAT_LONG)
500 && strcmp(argv[i], option->name + 1) == 0)
501 {
502 /* Replace the standard X arguments "-name" and "-display"
503 * with their GNU-style long option counterparts. */
504 argv[i] = (char *)option->name;
505 break;
506 }
507 }
508 if (option->name == NULL) /* no match */
509 {
510 ++i;
511 continue;
512 }
513
514 if (option->flags & ARG_FOR_GTK)
515 {
516 /* Move the argument into gui_argv, which
517 * will later be passed to gtk_init_check() */
518 gui_argv[gui_argc++] = argv[i];
519 }
520 else
521 {
522 char *value = NULL;
523
524 /* Extract the option's value if there is one.
525 * Accept both "--foo bar" and "--foo=bar" style. */
526 if (option->flags & ARG_HAS_VALUE)
527 {
528 if (argv[i][len] == '=')
529 value = &argv[i][len + 1];
530 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
531 value = argv[i + 1];
532 }
533
534 /* Check for options handled by Vim itself */
535 switch (option->flags & ARG_INDEX_MASK)
536 {
537 case ARG_REVERSE:
538 found_reverse_arg = TRUE;
539 break;
540 case ARG_NOREVERSE:
541 found_reverse_arg = FALSE;
542 break;
543 case ARG_FONT:
544 font_argument = value;
545 break;
546 case ARG_GEOMETRY:
547 if (value != NULL)
548 gui.geom = vim_strsave((char_u *)value);
549 break;
550 case ARG_BACKGROUND:
551 background_argument = value;
552 break;
553 case ARG_FOREGROUND:
554 foreground_argument = value;
555 break;
556 case ARG_ICONIC:
557 found_iconic_arg = TRUE;
558 break;
559#ifdef HAVE_GTK2
560 case ARG_ROLE:
561 role_argument = value; /* used later in gui_mch_open() */
562 break;
563#endif
564#ifdef FEAT_NETBEANS_INTG
565 case ARG_NETBEANS:
566 ++usingNetbeans;
567 gui.dofork = FALSE; /* don't fork() when starting GUI */
568 netbeansArg = argv[i];
569 break;
570#endif
571 default:
572 break;
573 }
574 }
575
576 /* These arguments make gnome_program_init() print a message and exit.
577 * Must start the GUI for this, otherwise ":gui" will exit later! */
578 if (option->flags & ARG_NEEDS_GUI)
579 gui.starting = TRUE;
580
581 if (option->flags & ARG_KEEP)
582 ++i;
583 else
584 {
585 /* Remove the flag from the argument vector. */
586 if (--*argc > i)
587 {
588 int n_strip = 1;
589
590 /* Move the argument's value as well, if there is one. */
591 if ((option->flags & ARG_HAS_VALUE)
592 && argv[i][len] != '='
593 && strcmp(argv[i + 1], "--") != 0)
594 {
595 ++n_strip;
596 --*argc;
597 if (option->flags & ARG_FOR_GTK)
598 gui_argv[gui_argc++] = argv[i + 1];
599 }
600
601 if (*argc > i)
602 mch_memmove(&argv[i], &argv[i + n_strip],
603 (*argc - i) * sizeof(char *));
604 }
605 argv[*argc] = NULL;
606 }
607 }
608
609 gui_argv[gui_argc] = NULL;
610}
611
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000612#if defined(EXITFREE) || defined(PROTO)
613 void
614gui_mch_free_all()
615{
616 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000617#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
618 vim_free(abs_restart_command);
619#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000620}
621#endif
622
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623/*
624 * This should be maybe completely removed.
625 * Doesn't seem possible, since check_copy_area() relies on
626 * this information. --danielk
627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000629visibility_event(GtkWidget *widget UNUSED,
630 GdkEventVisibility *event,
631 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 gui.visibility = event->state;
634 /*
635 * When we do an gdk_window_copy_area(), and the window is partially
636 * obscured, we want to receive an event to tell us whether it worked
637 * or not.
638 */
639 if (gui.text_gc != NULL)
640 gdk_gc_set_exposures(gui.text_gc,
641 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
642 return FALSE;
643}
644
645/*
646 * Redraw the corresponding portions of the screen.
647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000649expose_event(GtkWidget *widget UNUSED,
650 GdkEventExpose *event,
651 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652{
653 /* Skip this when the GUI isn't set up yet, will redraw later. */
654 if (gui.starting)
655 return FALSE;
656
657 out_flush(); /* make sure all output has been processed */
658 gui_redraw(event->area.x, event->area.y,
659 event->area.width, event->area.height);
660
661 /* Clear the border areas if needed */
662 if (event->area.x < FILL_X(0))
663 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
664 if (event->area.y < FILL_Y(0))
665 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
666 if (event->area.x > FILL_X(Columns))
667 gdk_window_clear_area(gui.drawarea->window,
668 FILL_X((int)Columns), 0, 0, 0);
669 if (event->area.y > FILL_Y(Rows))
670 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
671
672 return FALSE;
673}
674
675#ifdef FEAT_CLIENTSERVER
676/*
677 * Handle changes to the "Comm" property
678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000680property_event(GtkWidget *widget,
681 GdkEventProperty *event,
682 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 if (event->type == GDK_PROPERTY_NOTIFY
685 && event->state == (int)GDK_PROPERTY_NEW_VALUE
686 && GDK_WINDOW_XWINDOW(event->window) == commWindow
687 && GET_X_ATOM(event->atom) == commProperty)
688 {
689 XEvent xev;
690
691 /* Translate to XLib */
692 xev.xproperty.type = PropertyNotify;
693 xev.xproperty.atom = commProperty;
694 xev.xproperty.window = commWindow;
695 xev.xproperty.state = PropertyNewValue;
696 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
697
698 if (gtk_main_level() > 0)
699 gtk_main_quit();
700 }
701 return FALSE;
702}
703#endif
704
705
706/****************************************************************************
707 * Focus handlers:
708 */
709
710
711/*
712 * This is a simple state machine:
713 * BLINK_NONE not blinking at all
714 * BLINK_OFF blinking, cursor is not shown
715 * BLINK_ON blinking, cursor is shown
716 */
717
718#define BLINK_NONE 0
719#define BLINK_OFF 1
720#define BLINK_ON 2
721
722static int blink_state = BLINK_NONE;
723static long_u blink_waittime = 700;
724static long_u blink_ontime = 400;
725static long_u blink_offtime = 250;
726static guint blink_timer = 0;
727
728 void
729gui_mch_set_blinking(long waittime, long on, long off)
730{
731 blink_waittime = waittime;
732 blink_ontime = on;
733 blink_offtime = off;
734}
735
736/*
737 * Stop the cursor blinking. Show the cursor if it wasn't shown.
738 */
739 void
740gui_mch_stop_blink(void)
741{
742 if (blink_timer)
743 {
744 gtk_timeout_remove(blink_timer);
745 blink_timer = 0;
746 }
747 if (blink_state == BLINK_OFF)
748 gui_update_cursor(TRUE, FALSE);
749 blink_state = BLINK_NONE;
750}
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000753blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 if (blink_state == BLINK_ON)
756 {
757 gui_undraw_cursor();
758 blink_state = BLINK_OFF;
759 blink_timer = gtk_timeout_add((guint32)blink_offtime,
760 (GtkFunction) blink_cb, NULL);
761 }
762 else
763 {
764 gui_update_cursor(TRUE, FALSE);
765 blink_state = BLINK_ON;
766 blink_timer = gtk_timeout_add((guint32)blink_ontime,
767 (GtkFunction) blink_cb, NULL);
768 }
769
770 return FALSE; /* don't happen again */
771}
772
773/*
774 * Start the cursor blinking. If it was already blinking, this restarts the
775 * waiting time and shows the cursor.
776 */
777 void
778gui_mch_start_blink(void)
779{
780 if (blink_timer)
781 gtk_timeout_remove(blink_timer);
782 /* Only switch blinking on if none of the times is zero */
783 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
784 {
785 blink_timer = gtk_timeout_add((guint32)blink_waittime,
786 (GtkFunction) blink_cb, NULL);
787 blink_state = BLINK_ON;
788 gui_update_cursor(TRUE, FALSE);
789 }
790}
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000793enter_notify_event(GtkWidget *widget UNUSED,
794 GdkEventCrossing *event UNUSED,
795 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796{
797 if (blink_state == BLINK_NONE)
798 gui_mch_start_blink();
799
800 /* make sure keyboard input goes there */
801 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
802 gtk_widget_grab_focus(gui.drawarea);
803
804 return FALSE;
805}
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000808leave_notify_event(GtkWidget *widget UNUSED,
809 GdkEventCrossing *event UNUSED,
810 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811{
812 if (blink_state != BLINK_NONE)
813 gui_mch_stop_blink();
814
815 return FALSE;
816}
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000819focus_in_event(GtkWidget *widget,
820 GdkEventFocus *event UNUSED,
821 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 gui_focus_change(TRUE);
824
825 if (blink_state == BLINK_NONE)
826 gui_mch_start_blink();
827
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000828 /* make sure keyboard input goes to the draw area (if this is focus for a
829 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000830 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000831 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000833 /* make sure the input buffer is read */
834 if (gtk_main_level() > 0)
835 gtk_main_quit();
836
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 return TRUE;
838}
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000841focus_out_event(GtkWidget *widget UNUSED,
842 GdkEventFocus *event UNUSED,
843 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
845 gui_focus_change(FALSE);
846
847 if (blink_state != BLINK_NONE)
848 gui_mch_stop_blink();
849
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000850 /* make sure the input buffer is read */
851 if (gtk_main_level() > 0)
852 gtk_main_quit();
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 return TRUE;
855}
856
857
858#ifdef HAVE_GTK2
859/*
860 * Translate a GDK key value to UTF-8 independently of the current locale.
861 * The output is written to string, which must have room for at least 6 bytes
862 * plus the NUL terminator. Returns the length in bytes.
863 *
864 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
865 * of GdkEventKey::string instead. But event->string is evil; see here why:
866 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
867 */
868 static int
869keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
870{
871 int len;
872 guint32 uc;
873
874 uc = gdk_keyval_to_unicode(keyval);
875 if (uc != 0)
876 {
877 /* Check for CTRL-foo */
878 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
879 {
880 /* These mappings look arbitrary at the first glance, but in fact
881 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
882 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
883 * more sense and is consistent with usual terminal behaviour). */
884 if (uc >= '@')
885 string[0] = uc & 0x1F;
886 else if (uc == '2')
887 string[0] = NUL;
888 else if (uc >= '3' && uc <= '7')
889 string[0] = uc ^ 0x28;
890 else if (uc == '8')
891 string[0] = BS;
892 else if (uc == '?')
893 string[0] = DEL;
894 else
895 string[0] = uc;
896 len = 1;
897 }
898 else
899 {
900 /* Translate a normal key to UTF-8. This doesn't work for dead
901 * keys of course, you _have_ to use an input method for that. */
902 len = utf_char2bytes((int)uc, string);
903 }
904 }
905 else
906 {
907 /* Translate keys which are represented by ASCII control codes in Vim.
908 * There are only a few of those; most control keys are translated to
909 * special terminal-like control sequences. */
910 len = 1;
911 switch (keyval)
912 {
913 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
914 string[0] = TAB;
915 break;
916 case GDK_Linefeed:
917 string[0] = NL;
918 break;
919 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
920 string[0] = CAR;
921 break;
922 case GDK_Escape:
923 string[0] = ESC;
924 break;
925 default:
926 len = 0;
927 break;
928 }
929 }
930 string[len] = NUL;
931
932 return len;
933}
934#endif /* HAVE_GTK2 */
935
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000936 static int
937modifiers_gdk2vim(guint state)
938{
939 int modifiers = 0;
940
941 if (state & GDK_SHIFT_MASK)
942 modifiers |= MOD_MASK_SHIFT;
943 if (state & GDK_CONTROL_MASK)
944 modifiers |= MOD_MASK_CTRL;
945 if (state & GDK_MOD1_MASK)
946 modifiers |= MOD_MASK_ALT;
947 if (state & GDK_MOD4_MASK)
948 modifiers |= MOD_MASK_META;
949
950 return modifiers;
951}
952
953 static int
954modifiers_gdk2mouse(guint state)
955{
956 int modifiers = 0;
957
958 if (state & GDK_SHIFT_MASK)
959 modifiers |= MOUSE_SHIFT;
960 if (state & GDK_CONTROL_MASK)
961 modifiers |= MOUSE_CTRL;
962 if (state & GDK_MOD1_MASK)
963 modifiers |= MOUSE_ALT;
964
965 return modifiers;
966}
967
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968/*
969 * Main keyboard handler:
970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000972key_press_event(GtkWidget *widget UNUSED,
973 GdkEventKey *event,
974 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975{
976#ifdef HAVE_GTK2
977 /* 256 bytes is way over the top, but for safety let's reduce it only
978 * for GTK+ 2 where we know for sure how large the string might get.
979 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
980 char_u string[32], string2[32];
981#else
982 char_u string[256], string2[256];
983#endif
984 guint key_sym;
985 int len;
986 int i;
987 int modifiers;
988 int key;
989 guint state;
990 char_u *s, *d;
991
992 key_sym = event->keyval;
993 state = event->state;
994#ifndef HAVE_GTK2 /* deprecated */
995 len = event->length;
996 g_assert(len <= sizeof(string));
997#endif
998
999#ifndef HAVE_GTK2
1000 /*
1001 * It appears as if we always want to consume a key-press (there currently
1002 * aren't any 'return FALSE's), so we always do this: when running in a
1003 * GtkPlug and not a window, we must prevent emission of the key_press
1004 * EVENT from continuing (which is 'beyond' the level of stopping mere
1005 * signals by returning FALSE), otherwise things like tab/cursor-keys are
1006 * processed by the GtkPlug default handler, which moves input focus away
1007 * from us!
1008 * Note: This should no longer be necessary with GTK+ 2.
1009 */
1010 if (gtk_socket_id != 0)
1011 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1012#endif
1013
1014#ifdef FEAT_XIM
1015 if (xim_queue_key_press_event(event, TRUE))
1016 return TRUE;
1017#endif
1018
1019#ifdef FEAT_HANGULIN
1020 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1021 {
1022 hangul_input_state_toggle();
1023 return TRUE;
1024 }
1025#endif
1026
1027#ifdef SunXK_F36
1028 /*
1029 * These keys have bogus lookup strings, and trapping them here is
1030 * easier than trying to XRebindKeysym() on them with every possible
1031 * combination of modifiers.
1032 */
1033 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1034 len = 0;
1035 else
1036#endif
1037 {
1038#ifdef HAVE_GTK2
1039 len = keyval_to_string(key_sym, state, string2);
1040
1041 /* Careful: convert_input() doesn't handle the NUL character.
1042 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1043 if (len > 1 && input_conv.vc_type != CONV_NONE)
1044 len = convert_input(string2, len, sizeof(string2));
1045
1046 s = string2;
1047#else
1048# ifdef FEAT_MBYTE
1049 if (input_conv.vc_type != CONV_NONE)
1050 {
1051 mch_memmove(string2, event->string, len);
1052 len = convert_input(string2, len, sizeof(string2));
1053 s = string2;
1054 }
1055 else
1056# endif
1057 s = (char_u *)event->string;
1058#endif
1059
1060 d = string;
1061 for (i = 0; i < len; ++i)
1062 {
1063 *d++ = s[i];
1064 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1065 {
1066 /* Turn CSI into K_CSI. */
1067 *d++ = KS_EXTRA;
1068 *d++ = (int)KE_CSI;
1069 }
1070 }
1071 len = d - string;
1072 }
1073
1074 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1075 if (key_sym == GDK_ISO_Left_Tab)
1076 {
1077 key_sym = GDK_Tab;
1078 state |= GDK_SHIFT_MASK;
1079 }
1080
1081#ifndef HAVE_GTK2 /* for GTK+ 2, we handle this in keyval_to_string() */
1082 if ((key_sym == GDK_2 || key_sym == GDK_at) && (state & GDK_CONTROL_MASK))
1083 {
1084 string[0] = NUL; /* CTRL-2 and CTRL-@ is NUL */
1085 len = 1;
1086 }
1087 else if (len == 0 && (key_sym == GDK_space || key_sym == GDK_Tab))
1088 {
1089 /* When there are modifiers, these keys get zero length; we need the
1090 * original key here to be able to add a modifier below. */
1091 string[0] = (key_sym & 0xff);
1092 len = 1;
1093 }
1094#endif
1095
1096#ifdef FEAT_MENU
1097 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1098 * is a menu shortcut, we ignore everything with the ALT modifier. */
1099 if ((state & GDK_MOD1_MASK)
1100 && gui.menu_is_active
1101 && (*p_wak == 'y'
1102 || (*p_wak == 'm'
1103 && len == 1
1104 && gui_is_menu_shortcut(string[0]))))
1105# ifdef HAVE_GTK2
1106 /* For GTK2 we return false to signify that we haven't handled the
1107 * keypress, so that gtk will handle the mnemonic or accelerator. */
1108 return FALSE;
1109# else
1110 return TRUE;
1111# endif
1112#endif
1113
1114 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1115 * that already has the 8th bit set.
1116 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1117 * Don't do this for double-byte encodings, it turns the char into a lead
1118 * byte. */
1119 if (len == 1
1120 && (state & GDK_MOD1_MASK)
1121 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1122 && (string[0] & 0x80) == 0
1123 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
1124#ifdef FEAT_MBYTE
1125 && !enc_dbcs
1126#endif
1127 )
1128 {
1129 string[0] |= 0x80;
1130 state &= ~GDK_MOD1_MASK; /* don't use it again */
1131#ifdef FEAT_MBYTE
1132 if (enc_utf8) /* convert to utf-8 */
1133 {
1134 string[1] = string[0] & 0xbf;
1135 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1136 if (string[1] == CSI)
1137 {
1138 string[2] = KS_EXTRA;
1139 string[3] = (int)KE_CSI;
1140 len = 4;
1141 }
1142 else
1143 len = 2;
1144 }
1145#endif
1146 }
1147
1148 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1149 * value) to detect backspace, delete and keypad keys. */
1150 if (len == 0 || len == 1)
1151 {
1152 for (i = 0; special_keys[i].key_sym != 0; i++)
1153 {
1154 if (special_keys[i].key_sym == key_sym)
1155 {
1156 string[0] = CSI;
1157 string[1] = special_keys[i].code0;
1158 string[2] = special_keys[i].code1;
1159 len = -3;
1160 break;
1161 }
1162 }
1163 }
1164
1165 if (len == 0) /* Unrecognized key */
1166 return TRUE;
1167
1168#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) && !defined(HAVE_GTK2)
1169 /* Cancel or type backspace. For GTK2, im_commit_cb() does the same. */
1170 preedit_start_col = MAXCOL;
1171 xim_changed_while_preediting = TRUE;
1172#endif
1173
1174 /* Special keys (and a few others) may have modifiers. Also when using a
1175 * double-byte encoding (can't set the 8th bit). */
1176 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1177 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1178 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1179 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
1180#ifdef FEAT_MBYTE
1181 || (enc_dbcs && len == 1 && (state & GDK_MOD1_MASK))
1182#endif
1183 )
1184 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001185 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
1187 /*
1188 * For some keys a shift modifier is translated into another key
1189 * code.
1190 */
1191 if (len == -3)
1192 key = TO_SPECIAL(string[1], string[2]);
1193 else
1194 key = string[0];
1195
1196 key = simplify_key(key, &modifiers);
1197 if (key == CSI)
1198 key = K_CSI;
1199 if (IS_SPECIAL(key))
1200 {
1201 string[0] = CSI;
1202 string[1] = K_SECOND(key);
1203 string[2] = K_THIRD(key);
1204 len = 3;
1205 }
1206 else
1207 {
1208 string[0] = key;
1209 len = 1;
1210 }
1211
1212 if (modifiers != 0)
1213 {
1214 string2[0] = CSI;
1215 string2[1] = KS_MODIFIER;
1216 string2[2] = modifiers;
1217 add_to_input_buf(string2, 3);
1218 }
1219 }
1220
1221 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1222 || (string[0] == intr_char && intr_char != Ctrl_C)))
1223 {
1224 trash_input_buf();
1225 got_int = TRUE;
1226 }
1227
1228 add_to_input_buf(string, len);
1229
1230 /* blank out the pointer if necessary */
1231 if (p_mh)
1232 gui_mch_mousehide(TRUE);
1233
1234 if (gtk_main_level() > 0)
1235 gtk_main_quit();
1236
1237 return TRUE;
1238}
1239
1240#if defined(FEAT_XIM) && defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001242key_release_event(GtkWidget *widget UNUSED,
1243 GdkEventKey *event,
1244 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245{
1246 /*
1247 * GTK+ 2 input methods may do fancy stuff on key release events too.
1248 * With the default IM for instance, you can enter any UCS code point
1249 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1250 */
1251 return xim_queue_key_press_event(event, FALSE);
1252}
1253#endif
1254
1255
1256/****************************************************************************
1257 * Selection handlers:
1258 */
1259
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001261selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001263 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264{
1265 if (event->selection == clip_plus.gtk_sel_atom)
1266 clip_lose_selection(&clip_plus);
1267 else
1268 clip_lose_selection(&clip_star);
1269
1270 if (gtk_main_level() > 0)
1271 gtk_main_quit();
1272
1273 return TRUE;
1274}
1275
1276#define RS_NONE 0 /* selection_received_cb() not called yet */
1277#define RS_OK 1 /* selection_received_cb() called and OK */
1278#define RS_FAIL 2 /* selection_received_cb() called and failed */
1279static int received_selection = RS_NONE;
1280
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001282selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001284 guint time_ UNUSED,
1285 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286{
1287 VimClipboard *cbd;
1288 char_u *text;
1289 char_u *tmpbuf = NULL;
1290#ifdef HAVE_GTK2
1291 guchar *tmpbuf_utf8 = NULL;
1292#endif
1293 int len;
1294 int motion_type;
1295
1296 if (data->selection == clip_plus.gtk_sel_atom)
1297 cbd = &clip_plus;
1298 else
1299 cbd = &clip_star;
1300
1301 text = (char_u *)data->data;
1302 len = data->length;
1303 motion_type = MCHAR;
1304
1305 if (text == NULL || len <= 0)
1306 {
1307 received_selection = RS_FAIL;
1308 /* clip_free_selection(cbd); ??? */
1309
1310 if (gtk_main_level() > 0)
1311 gtk_main_quit();
1312
1313 return;
1314 }
1315
1316 if (data->type == vim_atom)
1317 {
1318 motion_type = *text++;
1319 --len;
1320 }
1321
1322#ifdef FEAT_MBYTE
1323 else if (data->type == vimenc_atom)
1324 {
1325 char_u *enc;
1326 vimconv_T conv;
1327
1328 motion_type = *text++;
1329 --len;
1330
1331 enc = text;
1332 text += STRLEN(text) + 1;
1333 len -= text - enc;
1334
1335 /* If the encoding of the text is different from 'encoding', attempt
1336 * converting it. */
1337 conv.vc_type = CONV_NONE;
1338 convert_setup(&conv, enc, p_enc);
1339 if (conv.vc_type != CONV_NONE)
1340 {
1341 tmpbuf = string_convert(&conv, text, &len);
1342 if (tmpbuf != NULL)
1343 text = tmpbuf;
1344 convert_setup(&conv, NULL, NULL);
1345 }
1346 }
1347#endif
1348
1349#ifdef HAVE_GTK2
1350 /* gtk_selection_data_get_text() handles all the nasty details
1351 * and targets and encodings etc. This rocks so hard. */
1352 else
1353 {
1354 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1355 if (tmpbuf_utf8 != NULL)
1356 {
1357 len = STRLEN(tmpbuf_utf8);
1358 if (input_conv.vc_type != CONV_NONE)
1359 {
1360 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1361 if (tmpbuf != NULL)
1362 text = tmpbuf;
1363 }
1364 else
1365 text = tmpbuf_utf8;
1366 }
1367 }
1368#else /* !HAVE_GTK2 */
1369# ifdef FEAT_MBYTE
1370 else if (data->type == utf8_string_atom)
1371 {
1372 vimconv_T conv;
1373
1374 conv.vc_type = CONV_NONE;
1375 convert_setup(&conv, (char_u *)"utf-8", p_enc);
1376
1377 if (conv.vc_type != CONV_NONE)
1378 {
1379 tmpbuf = string_convert(&conv, text, &len);
1380 convert_setup(&conv, NULL, NULL);
1381 }
1382 if (tmpbuf != NULL)
1383 text = tmpbuf;
1384 }
1385# endif
1386 else if (data->type == compound_text_atom || data->type == text_atom)
1387 {
1388 char **list = NULL;
1389 int count;
1390 int i;
1391 unsigned tmplen = 0;
1392
1393 count = gdk_text_property_to_text_list(data->type, data->format,
1394 data->data, data->length,
1395 &list);
1396 for (i = 0; i < count; ++i)
1397 tmplen += strlen(list[i]);
1398
1399 tmpbuf = alloc(tmplen + 1);
1400 if (tmpbuf != NULL)
1401 {
1402 tmpbuf[0] = NUL;
1403 for (i = 0; i < count; ++i)
1404 STRCAT(tmpbuf, list[i]);
1405 text = tmpbuf;
1406 len = tmplen;
1407 }
1408
1409 if (list != NULL)
1410 gdk_free_text_list(list);
1411 }
1412#endif /* !HAVE_GTK2 */
1413
1414 clip_yank_selection(motion_type, text, (long)len, cbd);
1415 received_selection = RS_OK;
1416 vim_free(tmpbuf);
1417#ifdef HAVE_GTK2
1418 g_free(tmpbuf_utf8);
1419#endif
1420
1421 if (gtk_main_level() > 0)
1422 gtk_main_quit();
1423}
1424
1425/*
1426 * Prepare our selection data for passing it to the external selection
1427 * client.
1428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001430selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 GtkSelectionData *selection_data,
1432 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001433 guint time_ UNUSED,
1434 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 char_u *string;
1437 char_u *tmpbuf;
1438 long_u tmplen;
1439 int length;
1440 int motion_type;
1441 GdkAtom type;
1442 VimClipboard *cbd;
1443
1444 if (selection_data->selection == clip_plus.gtk_sel_atom)
1445 cbd = &clip_plus;
1446 else
1447 cbd = &clip_star;
1448
1449 if (!cbd->owned)
1450 return; /* Shouldn't ever happen */
1451
1452 if (info != (guint)TARGET_STRING
1453#ifdef FEAT_MBYTE
1454 && info != (guint)TARGET_UTF8_STRING
1455 && info != (guint)TARGET_VIMENC
1456#endif
1457 && info != (guint)TARGET_VIM
1458 && info != (guint)TARGET_COMPOUND_TEXT
1459 && info != (guint)TARGET_TEXT)
1460 return;
1461
1462 /* get the selection from the '*'/'+' register */
1463 clip_get_selection(cbd);
1464
1465 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1466 if (motion_type < 0 || string == NULL)
1467 return;
1468 /* Due to int arguments we can't handle more than G_MAXINT. Also
1469 * reserve one extra byte for NUL or the motion type; just in case.
1470 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1471 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1472
1473 if (info == (guint)TARGET_VIM)
1474 {
1475 tmpbuf = alloc((unsigned)length + 1);
1476 if (tmpbuf != NULL)
1477 {
1478 tmpbuf[0] = motion_type;
1479 mch_memmove(tmpbuf + 1, string, (size_t)length);
1480 }
1481 /* For our own format, the first byte contains the motion type */
1482 ++length;
1483 vim_free(string);
1484 string = tmpbuf;
1485 type = vim_atom;
1486 }
1487
1488#ifdef FEAT_MBYTE
1489 else if (info == (guint)TARGET_VIMENC)
1490 {
1491 int l = STRLEN(p_enc);
1492
1493 /* contents: motion_type 'encoding' NUL text */
1494 tmpbuf = alloc((unsigned)length + l + 2);
1495 if (tmpbuf != NULL)
1496 {
1497 tmpbuf[0] = motion_type;
1498 STRCPY(tmpbuf + 1, p_enc);
1499 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1500 }
1501 length += l + 2;
1502 vim_free(string);
1503 string = tmpbuf;
1504 type = vimenc_atom;
1505 }
1506#endif
1507
1508#ifdef HAVE_GTK2
1509 /* gtk_selection_data_set_text() handles everything for us. This is
1510 * so easy and simple and cool, it'd be insane not to use it. */
1511 else
1512 {
1513 if (output_conv.vc_type != CONV_NONE)
1514 {
1515 tmpbuf = string_convert(&output_conv, string, &length);
1516 vim_free(string);
1517 if (tmpbuf == NULL)
1518 return;
1519 string = tmpbuf;
1520 }
1521 /* Validate the string to avoid runtime warnings */
1522 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1523 {
1524 gtk_selection_data_set_text(selection_data,
1525 (const char *)string, length);
1526 }
1527 vim_free(string);
1528 return;
1529 }
1530#else /* !HAVE_GTK2 */
1531# ifdef FEAT_MBYTE
1532 else if (info == (guint)TARGET_UTF8_STRING)
1533 {
1534 vimconv_T conv;
1535
1536 conv.vc_type = CONV_NONE;
1537 convert_setup(&conv, p_enc, (char_u *)"utf-8");
1538
1539 if (conv.vc_type != CONV_NONE)
1540 {
1541 tmpbuf = string_convert(&conv, string, &length);
1542 convert_setup(&conv, NULL, NULL);
1543 vim_free(string);
1544 string = tmpbuf;
1545 }
1546 type = utf8_string_atom;
1547 }
1548# endif
1549 else if (info == (guint)TARGET_COMPOUND_TEXT
1550 || info == (guint)TARGET_TEXT)
1551 {
1552 int format;
1553
1554 /* Copy the string to ensure NUL-termination */
1555 tmpbuf = vim_strnsave(string, length);
1556 vim_free(string);
1557 if (tmpbuf != NULL)
1558 {
1559 gdk_string_to_compound_text((const char *)tmpbuf,
1560 &type, &format, &string, &length);
1561 vim_free(tmpbuf);
1562 selection_data->type = type;
1563 selection_data->format = format;
1564 gtk_selection_data_set(selection_data, type, format, string, length);
1565 gdk_free_compound_text(string);
1566 }
1567 return;
1568 }
1569 else
1570 {
1571 type = GDK_TARGET_STRING;
1572 }
1573#endif /* !HAVE_GTK2 */
1574
1575 if (string != NULL)
1576 {
1577 selection_data->type = selection_data->target;
1578 selection_data->format = 8; /* 8 bits per char */
1579
1580 gtk_selection_data_set(selection_data, type, 8, string, length);
1581 vim_free(string);
1582 }
1583}
1584
1585/*
1586 * Check if the GUI can be started. Called before gvimrc is sourced.
1587 * Return OK or FAIL.
1588 */
1589 int
1590gui_mch_init_check(void)
1591{
1592#ifndef HAVE_GTK2
Bram Moolenaar49325942007-05-10 19:19:59 +00001593 /* This is needed to make the locale handling consistent between the GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 * and the rest of VIM. */
1595 gtk_set_locale();
1596#endif
1597
1598#ifdef FEAT_GUI_GNOME
1599 if (gtk_socket_id == 0)
1600 using_gnome = 1;
1601#endif
1602
1603 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1604 if (!gtk_init_check(&gui_argc, &gui_argv))
1605 {
1606 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001607 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return FAIL;
1609 }
1610
1611 return OK;
1612}
1613
1614
1615/****************************************************************************
1616 * Mouse handling callbacks
1617 */
1618
1619
1620static guint mouse_click_timer = 0;
1621static int mouse_timed_out = TRUE;
1622
1623/*
1624 * Timer used to recognize multiple clicks of the mouse button
1625 */
1626 static gint
1627mouse_click_timer_cb(gpointer data)
1628{
1629 /* we don't use this information currently */
1630 int *timed_out = (int *) data;
1631
1632 *timed_out = TRUE;
1633 return FALSE; /* don't happen again */
1634}
1635
1636static guint motion_repeat_timer = 0;
1637static int motion_repeat_offset = FALSE;
1638static gint motion_repeat_timer_cb(gpointer);
1639
1640 static void
1641process_motion_notify(int x, int y, GdkModifierType state)
1642{
1643 int button;
1644 int_u vim_modifiers;
1645
1646 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1647 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1648 GDK_BUTTON5_MASK))
1649 ? MOUSE_DRAG : ' ';
1650
1651 /* If our pointer is currently hidden, then we should show it. */
1652 gui_mch_mousehide(FALSE);
1653
1654 /* Just moving the rodent above the drawing area without any button
1655 * being pressed. */
1656 if (button != MOUSE_DRAG)
1657 {
1658 gui_mouse_moved(x, y);
1659 return;
1660 }
1661
1662 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001663 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaar49325942007-05-10 19:19:59 +00001665 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1667
1668 if (gtk_main_level() > 0)
1669 gtk_main_quit();
1670
1671 /*
1672 * Auto repeat timer handling.
1673 */
1674 if (x < 0 || y < 0
1675 || x >= gui.drawarea->allocation.width
1676 || y >= gui.drawarea->allocation.height)
1677 {
1678
1679 int dx;
1680 int dy;
1681 int offshoot;
1682 int delay = 10;
1683
1684 /* Calculate the maximal distance of the cursor from the drawing area.
1685 * (offshoot can't become negative here!).
1686 */
1687 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1688 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1689
1690 offshoot = dx > dy ? dx : dy;
1691
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001692 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 * distance of 127 pixels from the main window.
1694 *
1695 * One could think endlessly about the most ergonomic variant here.
1696 * For example it could make sense to calculate the distance from the
1697 * drags start instead...
1698 *
1699 * Maybe a parabolic interpolation would suite us better here too...
1700 */
1701 if (offshoot > 127)
1702 {
1703 /* 5 appears to be somehow near to my perceptual limits :-). */
1704 delay = 5;
1705 }
1706 else
1707 {
1708 delay = (130 * (127 - offshoot)) / 127 + 5;
1709 }
1710
1711 /* shoot again */
1712 if (!motion_repeat_timer)
1713 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1714 motion_repeat_timer_cb, NULL);
1715 }
1716}
1717
1718/*
1719 * Timer used to recognize multiple clicks of the mouse button.
1720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001722motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int x;
1725 int y;
1726 GdkModifierType state;
1727
1728 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1729
1730 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1731 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1732 GDK_BUTTON5_MASK)))
1733 {
1734 motion_repeat_timer = 0;
1735 return FALSE;
1736 }
1737
1738 /* If there already is a mouse click in the input buffer, wait another
1739 * time (otherwise we would create a backlog of clicks) */
1740 if (vim_used_in_input_buf() > 10)
1741 return TRUE;
1742
1743 motion_repeat_timer = 0;
1744
1745 /*
1746 * Fake a motion event.
1747 * Trick: Pretend the mouse moved to the next character on every other
1748 * event, otherwise drag events will be discarded, because they are still
1749 * in the same character.
1750 */
1751 if (motion_repeat_offset)
1752 x += gui.char_width;
1753
1754 motion_repeat_offset = !motion_repeat_offset;
1755 process_motion_notify(x, y, state);
1756
1757 /* Don't happen again. We will get reinstalled in the synthetic event
1758 * if needed -- thus repeating should still work. */
1759 return FALSE;
1760}
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001763motion_notify_event(GtkWidget *widget,
1764 GdkEventMotion *event,
1765 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766{
1767 if (event->is_hint)
1768 {
1769 int x;
1770 int y;
1771 GdkModifierType state;
1772
1773 gdk_window_get_pointer(widget->window, &x, &y, &state);
1774 process_motion_notify(x, y, state);
1775 }
1776 else
1777 {
1778 process_motion_notify((int)event->x, (int)event->y,
1779 (GdkModifierType)event->state);
1780 }
1781
1782 return TRUE; /* handled */
1783}
1784
1785
1786/*
1787 * Mouse button handling. Note please that we are capturing multiple click's
1788 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1789 * This is due to the way the generic VIM code is recognizing multiple clicks.
1790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001792button_press_event(GtkWidget *widget,
1793 GdkEventButton *event,
1794 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
1796 int button;
1797 int repeated_click = FALSE;
1798 int x, y;
1799 int_u vim_modifiers;
1800
1801 /* Make sure we have focus now we've been selected */
1802 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1803 gtk_widget_grab_focus(widget);
1804
1805 /*
1806 * Don't let additional events about multiple clicks send by GTK to us
1807 * after the initial button press event confuse us.
1808 */
1809 if (event->type != GDK_BUTTON_PRESS)
1810 return FALSE;
1811
1812 x = event->x;
1813 y = event->y;
1814
1815 /* Handle multiple clicks */
1816 if (!mouse_timed_out && mouse_click_timer)
1817 {
1818 gtk_timeout_remove(mouse_click_timer);
1819 mouse_click_timer = 0;
1820 repeated_click = TRUE;
1821 }
1822
1823 mouse_timed_out = FALSE;
1824 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1825 mouse_click_timer_cb, &mouse_timed_out);
1826
1827 switch (event->button)
1828 {
1829 case 1:
1830 button = MOUSE_LEFT;
1831 break;
1832 case 2:
1833 button = MOUSE_MIDDLE;
1834 break;
1835 case 3:
1836 button = MOUSE_RIGHT;
1837 break;
1838#ifndef HAVE_GTK2
1839 case 4:
1840 button = MOUSE_4;
1841 break;
1842 case 5:
1843 button = MOUSE_5;
1844 break;
1845#endif
1846 default:
1847 return FALSE; /* Unknown button */
1848 }
1849
1850#ifdef FEAT_XIM
1851 /* cancel any preediting */
1852 if (im_is_preediting())
1853 xim_reset();
1854#endif
1855
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001856 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857
1858 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1859 if (gtk_main_level() > 0)
1860 gtk_main_quit(); /* make sure the above will be handled immediately */
1861
1862 return TRUE;
1863}
1864
1865#ifdef HAVE_GTK2
1866/*
1867 * GTK+ 2 doesn't handle mouse buttons 4, 5, 6 and 7 the same way as GTK+ 1.
1868 * Instead, it abstracts scrolling via the new GdkEventScroll.
1869 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001871scroll_event(GtkWidget *widget,
1872 GdkEventScroll *event,
1873 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874{
1875 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001876 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1879 gtk_widget_grab_focus(widget);
1880
1881 switch (event->direction)
1882 {
1883 case GDK_SCROLL_UP:
1884 button = MOUSE_4;
1885 break;
1886 case GDK_SCROLL_DOWN:
1887 button = MOUSE_5;
1888 break;
1889 default: /* We don't care about left and right... Yet. */
1890 return FALSE;
1891 }
1892
1893# ifdef FEAT_XIM
1894 /* cancel any preediting */
1895 if (im_is_preediting())
1896 xim_reset();
1897# endif
1898
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001899 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1902 FALSE, vim_modifiers);
1903
1904 if (gtk_main_level() > 0)
1905 gtk_main_quit(); /* make sure the above will be handled immediately */
1906
1907 return TRUE;
1908}
1909#endif /* HAVE_GTK2 */
1910
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001913button_release_event(GtkWidget *widget UNUSED,
1914 GdkEventButton *event,
1915 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 int x, y;
1918 int_u vim_modifiers;
1919
1920 /* Remove any motion "machine gun" timers used for automatic further
1921 extension of allocation areas if outside of the applications window
1922 area .*/
1923 if (motion_repeat_timer)
1924 {
1925 gtk_timeout_remove(motion_repeat_timer);
1926 motion_repeat_timer = 0;
1927 }
1928
1929 x = event->x;
1930 y = event->y;
1931
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001932 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
1934 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
1935 if (gtk_main_level() > 0)
1936 gtk_main_quit(); /* make sure it will be handled immediately */
1937
1938 return TRUE;
1939}
1940
1941
1942#ifdef FEAT_DND
1943/****************************************************************************
1944 * Drag aNd Drop support handlers.
1945 */
1946
1947/*
1948 * Count how many items there may be and separate them with a NUL.
1949 * Apparently the items are separated with \r\n. This is not documented,
1950 * thus be careful not to go past the end. Also allow separation with
1951 * NUL characters.
1952 */
1953 static int
1954count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1955{
1956 int i;
1957 char_u *p = out;
1958 int count = 0;
1959
1960 for (i = 0; i < len; ++i)
1961 {
1962 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1963 {
1964 if (p > out && p[-1] != NUL)
1965 {
1966 ++count;
1967 *p++ = NUL;
1968 }
1969 }
1970 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1971 {
1972 *p++ = hexhex2nr(raw + i + 1);
1973 i += 2;
1974 }
1975 else
1976 *p++ = raw[i];
1977 }
1978 if (p > out && p[-1] != NUL)
1979 {
1980 *p = NUL; /* last item didn't have \r or \n */
1981 ++count;
1982 }
1983 return count;
1984}
1985
1986/*
1987 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1988 * this process, URI which protocol is not "file:" are removed. Return
1989 * length of array (less than "max").
1990 */
1991 static int
1992filter_uri_list(char_u **outlist, int max, char_u *src)
1993{
1994 int i, j;
1995
1996 for (i = j = 0; i < max; ++i)
1997 {
1998 outlist[i] = NULL;
1999 if (STRNCMP(src, "file:", 5) == 0)
2000 {
2001 src += 5;
2002 if (STRNCMP(src, "//localhost", 11) == 0)
2003 src += 11;
2004 while (src[0] == '/' && src[1] == '/')
2005 ++src;
2006 outlist[j++] = vim_strsave(src);
2007 }
2008 src += STRLEN(src) + 1;
2009 }
2010 return j;
2011}
2012
2013 static char_u **
2014parse_uri_list(int *count, char_u *data, int len)
2015{
2016 int n = 0;
2017 char_u *tmp = NULL;
2018 char_u **array = NULL;;
2019
2020 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2021 {
2022 n = count_and_decode_uri_list(tmp, data, len);
2023 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2024 n = filter_uri_list(array, n, tmp);
2025 }
2026 vim_free(tmp);
2027 *count = n;
2028 return array;
2029}
2030
2031 static void
2032drag_handle_uri_list(GdkDragContext *context,
2033 GtkSelectionData *data,
2034 guint time_,
2035 GdkModifierType state,
2036 gint x,
2037 gint y)
2038{
2039 char_u **fnames;
2040 int nfiles = 0;
2041
2042 fnames = parse_uri_list(&nfiles, data->data, data->length);
2043
2044 if (fnames != NULL && nfiles > 0)
2045 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002046 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2049
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002050 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2053 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002054 else
2055 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056}
2057
2058 static void
2059drag_handle_text(GdkDragContext *context,
2060 GtkSelectionData *data,
2061 guint time_,
2062 GdkModifierType state)
2063{
2064 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2065 char_u *text;
2066 int len;
2067# ifdef FEAT_MBYTE
2068 char_u *tmpbuf = NULL;
2069# endif
2070
2071 text = data->data;
2072 len = data->length;
2073
2074# ifdef FEAT_MBYTE
2075 if (data->type == utf8_string_atom)
2076 {
2077# ifdef HAVE_GTK2
2078 if (input_conv.vc_type != CONV_NONE)
2079 tmpbuf = string_convert(&input_conv, text, &len);
2080# else
2081 vimconv_T conv;
2082
2083 conv.vc_type = CONV_NONE;
2084 convert_setup(&conv, (char_u *)"utf-8", p_enc);
2085
2086 if (conv.vc_type != CONV_NONE)
2087 {
2088 tmpbuf = string_convert(&conv, text, &len);
2089 convert_setup(&conv, NULL, NULL);
2090 }
2091# endif
2092 if (tmpbuf != NULL)
2093 text = tmpbuf;
2094 }
2095# endif /* FEAT_MBYTE */
2096
2097 dnd_yank_drag_data(text, (long)len);
2098 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2099# ifdef FEAT_MBYTE
2100 vim_free(tmpbuf);
2101# endif
2102
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002103 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
2105 if (dropkey[2] != 0)
2106 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2107 else
2108 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
2109
2110 if (gtk_main_level() > 0)
2111 gtk_main_quit();
2112}
2113
2114/*
2115 * DND receiver.
2116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 static void
2118drag_data_received_cb(GtkWidget *widget,
2119 GdkDragContext *context,
2120 gint x,
2121 gint y,
2122 GtkSelectionData *data,
2123 guint info,
2124 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002125 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126{
2127 GdkModifierType state;
2128
2129 /* Guard against trash */
2130 if (data->data == NULL
2131 || data->length <= 0
2132 || data->format != 8
2133 || data->data[data->length] != '\0')
2134 {
2135 gtk_drag_finish(context, FALSE, FALSE, time_);
2136 return;
2137 }
2138
2139 /* Get the current modifier state for proper distinguishment between
2140 * different operations later. */
2141 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
2142
2143 /* Not sure about the role of "text/plain" here... */
2144 if (info == (guint)TARGET_TEXT_URI_LIST)
2145 drag_handle_uri_list(context, data, time_, state, x, y);
2146 else
2147 drag_handle_text(context, data, time_, state);
2148
2149}
2150#endif /* FEAT_DND */
2151
2152
2153#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2154/*
2155 * GnomeClient interact callback. Check for unsaved buffers that cannot
2156 * be abandoned and pop up a dialog asking the user for confirmation if
2157 * necessary.
2158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 static void
2160sm_client_check_changed_any(GnomeClient *client,
2161 gint key,
2162 GnomeDialogType type,
2163 gpointer data)
2164{
2165 cmdmod_T save_cmdmod;
2166 gboolean shutdown_cancelled;
2167
2168 save_cmdmod = cmdmod;
2169
2170# ifdef FEAT_BROWSE
2171 cmdmod.browse = TRUE;
2172# endif
2173# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2174 cmdmod.confirm = TRUE;
2175# endif
2176 /*
2177 * If there are changed buffers, present the user with
2178 * a dialog if possible, otherwise give an error message.
2179 */
2180 shutdown_cancelled = check_changed_any(FALSE);
2181
2182 exiting = FALSE;
2183 cmdmod = save_cmdmod;
2184 setcursor(); /* position the cursor */
2185 out_flush();
2186 /*
2187 * If the user hit the [Cancel] button the whole shutdown
2188 * will be cancelled. Wow, quite powerful feature (:
2189 */
2190 gnome_interaction_key_return(key, shutdown_cancelled);
2191}
2192
2193/*
2194 * Generate a script that can be used to restore the current editing session.
2195 * Save the value of v:this_session before running :mksession in order to make
2196 * automagic session save fully transparent. Return TRUE on success.
2197 */
2198 static int
2199write_session_file(char_u *filename)
2200{
2201 char_u *escaped_filename;
2202 char *mksession_cmdline;
2203 unsigned int save_ssop_flags;
2204 int failed;
2205
2206 /*
2207 * Build an ex command line to create a script that restores the current
2208 * session if executed. Escape the filename to avoid nasty surprises.
2209 */
2210 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2211 if (escaped_filename == NULL)
2212 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002213 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2214 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 /*
2218 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2219 * unpredictable effects when the session is saved automatically. Also,
2220 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2221 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2222 * enormously large if the GNOME session feature is used regularly.
2223 */
2224 save_ssop_flags = ssop_flags;
2225 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002226 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227
2228 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2229 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2230 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002231 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
2233 ssop_flags = save_ssop_flags;
2234 g_free(mksession_cmdline);
2235 /*
2236 * Reopen the file and append a command to restore v:this_session,
2237 * as if this save never happened. This is to avoid conflicts with
2238 * the user's own sessions. FIXME: It's probably less hackish to add
2239 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2240 */
2241 if (!failed)
2242 {
2243 FILE *fd;
2244
2245 fd = open_exfile(filename, TRUE, APPENDBIN);
2246
2247 failed = (fd == NULL
2248 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2249 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2250
2251 if (fd != NULL && fclose(fd) != 0)
2252 failed = TRUE;
2253
2254 if (failed)
2255 mch_remove(filename);
2256 }
2257
2258 return !failed;
2259}
2260
2261/*
2262 * "save_yourself" signal handler. Initiate an interaction to ask the user
2263 * for confirmation if necessary. Save the current editing session and tell
2264 * the session manager how to restart Vim.
2265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 static gboolean
2267sm_client_save_yourself(GnomeClient *client,
2268 gint phase,
2269 GnomeSaveStyle save_style,
2270 gboolean shutdown,
2271 GnomeInteractStyle interact_style,
2272 gboolean fast,
2273 gpointer data)
2274{
2275 static const char suffix[] = "-session.vim";
2276 char *session_file;
2277 unsigned int len;
2278 gboolean success;
2279
2280 /* Always request an interaction if possible. check_changed_any()
2281 * won't actually show a dialog unless any buffers have been modified.
2282 * There doesn't seem to be an obvious way to check that without
2283 * automatically firing the dialog. Anyway, it works just fine. */
2284 if (interact_style == GNOME_INTERACT_ANY)
2285 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2286 &sm_client_check_changed_any,
2287 NULL);
2288 out_flush();
2289 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2290
2291 /* The path is unique for each session save. We do neither know nor care
2292 * which session script will actually be used later. This decision is in
2293 * the domain of the session manager. */
2294 session_file = gnome_config_get_real_path(
2295 gnome_client_get_config_prefix(client));
2296 len = strlen(session_file);
2297
2298 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2299 --len; /* get rid of the superfluous trailing '/' */
2300
2301 session_file = g_renew(char, session_file, len + sizeof(suffix));
2302 memcpy(session_file + len, suffix, sizeof(suffix));
2303
2304 success = write_session_file((char_u *)session_file);
2305
2306 if (success)
2307 {
2308 const char *argv[8];
2309 int i;
2310
2311 /* Tell the session manager how to wipe out the stored session data.
2312 * This isn't as dangerous as it looks, don't worry :) session_file
2313 * is a unique absolute filename. Usually it'll be something like
2314 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2315 i = 0;
2316 argv[i++] = "rm";
2317 argv[i++] = session_file;
2318 argv[i] = NULL;
2319
2320 gnome_client_set_discard_command(client, i, (char **)argv);
2321
2322 /* Tell the session manager how to restore the just saved session.
2323 * This is easily done thanks to Vim's -S option. Pass the -f flag
2324 * since there's no need to fork -- it might even cause confusion.
2325 * Also pass the window role to give the WM something to match on.
2326 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2327 i = 0;
2328 argv[i++] = restart_command;
2329 argv[i++] = "-f";
2330 argv[i++] = "-g";
2331# ifdef HAVE_GTK2
2332 argv[i++] = "--role";
2333 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
2334# endif
2335 argv[i++] = "-S";
2336 argv[i++] = session_file;
2337 argv[i] = NULL;
2338
2339 gnome_client_set_restart_command(client, i, (char **)argv);
2340 gnome_client_set_clone_command(client, 0, NULL);
2341 }
2342
2343 g_free(session_file);
2344
2345 return success;
2346}
2347
2348/*
2349 * Called when the session manager wants us to die. There isn't much to save
2350 * here since "save_yourself" has been emitted before (unless serious trouble
2351 * is happening).
2352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 static void
2354sm_client_die(GnomeClient *client, gpointer data)
2355{
2356 /* Don't write messages to the GUI anymore */
2357 full_screen = FALSE;
2358
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002359 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002360 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002361 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 preserve_exit();
2363}
2364
2365/*
2366 * Connect our signal handlers to be notified on session save and shutdown.
2367 */
2368 static void
2369setup_save_yourself(void)
2370{
2371 GnomeClient *client;
2372
2373 client = gnome_master_client();
2374
2375 if (client != NULL)
2376 {
2377 /* Must use the deprecated gtk_signal_connect() for compatibility
2378 * with GNOME 1. Arrgh, zombies! */
2379 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2380 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2381 gtk_signal_connect(GTK_OBJECT(client), "die",
2382 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2383 }
2384}
2385
2386#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2387
2388# ifdef USE_XSMP
2389/*
2390 * GTK tells us that XSMP needs attention
2391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 static gboolean
2393local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002394 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 GIOCondition condition;
2396 gpointer data;
2397{
2398 if (condition == G_IO_IN)
2399 {
2400 /* Do stuff; maybe close connection */
2401 if (xsmp_handle_requests() == FAIL)
2402 g_io_channel_unref((GIOChannel *)data);
2403 return TRUE;
2404 }
2405 /* Error */
2406 g_io_channel_unref((GIOChannel *)data);
2407 xsmp_close();
2408 return TRUE;
2409}
2410# endif /* USE_XSMP */
2411
2412/*
2413 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2414 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2415 */
2416 static void
2417setup_save_yourself(void)
2418{
2419 Atom *existing_atoms = NULL;
2420 int count = 0;
2421
2422#ifdef USE_XSMP
2423 if (xsmp_icefd != -1)
2424 {
2425 /*
2426 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2427 * set up GTK IO monitor
2428 */
2429 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2430
2431 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2432 local_xsmp_handle_requests, (gpointer)g_io);
2433 }
2434 else
2435#endif
2436 {
2437 /* Fall back to old method */
2438
2439 /* first get the existing value */
2440 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2441 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2442 &existing_atoms, &count))
2443 {
2444 Atom *new_atoms;
2445 Atom save_yourself_xatom;
2446 int i;
2447
2448 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2449
2450 /* check if WM_SAVE_YOURSELF isn't there yet */
2451 for (i = 0; i < count; ++i)
2452 if (existing_atoms[i] == save_yourself_xatom)
2453 break;
2454
2455 if (i == count)
2456 {
2457 /* allocate an Atoms array which is one item longer */
2458 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2459 * sizeof(Atom)));
2460 if (new_atoms != NULL)
2461 {
2462 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2463 new_atoms[count] = save_yourself_xatom;
2464 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2465 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2466 new_atoms, count + 1);
2467 vim_free(new_atoms);
2468 }
2469 }
2470 XFree(existing_atoms);
2471 }
2472 }
2473}
2474
2475# ifdef HAVE_GTK2
2476/*
2477 * Installing a global event filter seems to be the only way to catch
2478 * client messages of type WM_PROTOCOLS without overriding GDK's own
2479 * client message event filter. Well, that's still better than trying
2480 * to guess what the GDK filter had done if it had been invoked instead
2481 * (This is what we did for GTK+ 1.2, see below).
2482 *
2483 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2484 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2485 * I have the nasty feeling modern session managers just don't send this
2486 * deprecated message anymore. Addition: confirmed by several people.
2487 *
2488 * The GNOME session support is much cooler anyway. Unlike this ugly
2489 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2490 * it should work with KDE as well.
2491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002493global_event_filter(GdkXEvent *xev,
2494 GdkEvent *event UNUSED,
2495 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 XEvent *xevent = (XEvent *)xev;
2498
2499 if (xevent != NULL
2500 && xevent->type == ClientMessage
2501 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002502 && (long_u)xevent->xclient.data.l[0]
2503 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 out_flush();
2506 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2507 /*
2508 * Set the window's WM_COMMAND property, to let the window manager
2509 * know we are done saving ourselves. We don't want to be
2510 * restarted, thus set argv to NULL.
2511 */
2512 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2513 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2514 NULL, 0);
2515 return GDK_FILTER_REMOVE;
2516 }
2517
2518 return GDK_FILTER_CONTINUE;
2519}
2520
2521# else /* !HAVE_GTK2 */
2522
2523/*
2524 * GDK handler for X ClientMessage events.
2525 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 static GdkFilterReturn
2527gdk_wm_protocols_filter(GdkXEvent *xev, GdkEvent *event, gpointer data)
2528{
2529 /* From example in gdkevents.c/gdk_wm_protocols_filter */
2530 XEvent *xevent = (XEvent *)xev;
2531
2532 if (xevent != NULL)
2533 {
2534 if (xevent->xclient.data.l[0] == GET_X_ATOM(save_yourself_atom))
2535 {
2536 out_flush();
2537 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2538
2539 /* Set the window's WM_COMMAND property, to let the window manager
2540 * know we are done saving ourselves. We don't want to be
2541 * restarted, thus set argv to NULL. */
2542 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2543 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2544 NULL, 0);
2545 }
2546 /*
2547 * Functionality from gdkevents.c/gdk_wm_protocols_filter;
2548 * Registering this filter apparently overrides the default GDK one,
2549 * so we need to perform its functionality. There seems no way to
2550 * register for WM_PROTOCOLS, and only process the WM_SAVE_YOURSELF
2551 * bit; it's all or nothing. Update: No, there is a way -- but it
2552 * only works with GTK+ 2 apparently. See above.
2553 */
2554 else if (xevent->xclient.data.l[0] == GET_X_ATOM(gdk_wm_delete_window))
2555 {
2556 event->any.type = GDK_DELETE;
2557 return GDK_FILTER_TRANSLATE;
2558 }
2559 }
2560
2561 return GDK_FILTER_REMOVE;
2562}
2563# endif /* !HAVE_GTK2 */
2564
2565#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2566
2567
2568/*
2569 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002572mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574/* If you get an error message here, you still need to unpack the runtime
2575 * archive! */
2576#ifdef magick
2577# undef magick
2578#endif
2579#ifdef HAVE_GTK2
2580 /* A bit hackish, but avoids casting later and allows optimization */
2581# define static static const
2582#endif
2583#define magick vim32x32
2584#include "../runtime/vim32x32.xpm"
2585#undef magick
2586#define magick vim16x16
2587#include "../runtime/vim16x16.xpm"
2588#undef magick
2589#define magick vim48x48
2590#include "../runtime/vim48x48.xpm"
2591#undef magick
2592#ifdef HAVE_GTK2
2593# undef static
2594#endif
2595
2596 /* When started with "--echo-wid" argument, write window ID on stdout. */
2597 if (echo_wid_arg)
2598 {
2599 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2600 fflush(stdout);
2601 }
2602
2603 if (vim_strchr(p_go, GO_ICON) != NULL)
2604 {
2605 /*
2606 * Add an icon to the main window. For fun and convenience of the user.
2607 */
2608#ifdef HAVE_GTK2
2609 GList *icons = NULL;
2610
2611 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2612 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2613 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2614
2615 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2616
2617 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2618 g_list_free(icons);
2619
2620#else /* !HAVE_GTK2 */
2621
2622 GdkPixmap *icon;
2623 GdkBitmap *icon_mask = NULL;
2624 char **magick = vim32x32;
2625 Display *xdisplay;
2626 Window root_window;
2627 XIconSize *size;
2628 int number_sizes;
2629 /*
2630 * Adjust the icon to the preferences of the actual window manager.
Bram Moolenaar49325942007-05-10 19:19:59 +00002631 * This is once again a workaround for a deficiency in GTK+ 1.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 */
2633 xdisplay = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
2634 root_window = XRootWindow(xdisplay, DefaultScreen(xdisplay));
2635 if (XGetIconSizes(xdisplay, root_window, &size, &number_sizes))
2636 {
2637 if (number_sizes > 0)
2638 {
2639 if (size->max_height >= 48 && size->max_height >= 48)
2640 magick = vim48x48;
2641 else if (size->max_height >= 32 && size->max_height >= 32)
2642 magick = vim32x32;
2643 else if (size->max_height >= 16 && size->max_height >= 16)
2644 magick = vim16x16;
2645 }
2646 XFree(size);
2647 }
2648 icon = gdk_pixmap_create_from_xpm_d(gui.mainwin->window,
2649 &icon_mask, NULL, magick);
2650 if (icon != NULL)
2651 /* Note: for some reason gdk_window_set_icon() doesn't acquire
2652 * a reference on the pixmap, thus we _have_ to leak it. */
2653 gdk_window_set_icon(gui.mainwin->window, NULL, icon, icon_mask);
2654
2655#endif /* !HAVE_GTK2 */
2656 }
2657
2658#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2659 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
2660# ifdef HAVE_GTK2
2661 gdk_window_add_filter(NULL, &global_event_filter, NULL);
2662# else
2663 gdk_add_client_message_filter(wm_protocols_atom,
2664 &gdk_wm_protocols_filter, NULL);
2665# endif
2666#endif
2667 /* Setup to indicate to the window manager that we want to catch the
2668 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2669 * manager instead. */
2670#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2671 if (using_gnome)
2672#endif
2673 setup_save_yourself();
2674
2675#ifdef FEAT_CLIENTSERVER
2676 if (serverName == NULL && serverDelayedStartName != NULL)
2677 {
2678 /* This is a :gui command in a plain vim with no previous server */
2679 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2680
2681 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2682 serverDelayedStartName);
2683 }
2684 else
2685 {
2686 /*
2687 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2688 * have to change the "server" registration to that of the main window
2689 * If we have not registered a name yet, remember the window
2690 */
2691 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2692 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2693 }
2694 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2695 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2696 GTK_SIGNAL_FUNC(property_event), NULL);
2697#endif
2698}
2699
2700 static GdkCursor *
2701create_blank_pointer(void)
2702{
2703 GdkWindow *root_window = NULL;
2704 GdkPixmap *blank_mask;
2705 GdkCursor *cursor;
2706 GdkColor color = { 0, 0, 0, 0 };
2707 char blank_data[] = { 0x0 };
2708
2709#ifdef HAVE_GTK_MULTIHEAD
2710 root_window = gtk_widget_get_root_window(gui.mainwin);
2711#endif
2712
2713 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2714 * in size. */
2715 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2716 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2717 &color, &color, 0, 0);
2718 gdk_bitmap_unref(blank_mask);
2719
2720 return cursor;
2721}
2722
2723#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 static void
2725mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002726 GdkScreen *previous_screen UNUSED,
2727 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 if (!gtk_widget_has_screen(widget))
2730 return;
2731
2732 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002733 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 */
2735 if (gui.blank_pointer != NULL)
2736 gdk_cursor_unref(gui.blank_pointer);
2737
2738 gui.blank_pointer = create_blank_pointer();
2739
2740 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2741 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2742
2743 /*
2744 * Create a new PangoContext for this screen, and initialize it
2745 * with the current font if necessary.
2746 */
2747 if (gui.text_context != NULL)
2748 g_object_unref(gui.text_context);
2749
2750 gui.text_context = gtk_widget_create_pango_context(widget);
2751 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2752
2753 if (gui.norm_font != NULL)
2754 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002755 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002756 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758}
2759#endif /* HAVE_GTK_MULTIHEAD */
2760
2761/*
2762 * After the drawing area comes up, we calculate all colors and create the
2763 * dummy blank cursor.
2764 *
2765 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2766 * fact that the main VIM engine doesn't take them into account anywhere.
2767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002769drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770{
2771 GtkWidget *sbar;
2772
2773#ifdef FEAT_XIM
2774 xim_init();
2775#endif
2776 gui_mch_new_colors();
2777 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2778
2779 gui.blank_pointer = create_blank_pointer();
2780 if (gui.pointer_hidden)
2781 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2782
2783 /* get the actual size of the scrollbars, if they are realized */
2784 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2785 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2786 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2787 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2788 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2789 gui.scrollbar_width = sbar->allocation.width;
2790
2791 sbar = gui.bottom_sbar.id;
2792 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2793 gui.scrollbar_height = sbar->allocation.height;
2794}
2795
2796/*
2797 * Properly clean up on shutdown.
2798 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002800drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 /* Don't write messages to the GUI anymore */
2803 full_screen = FALSE;
2804
2805#ifdef FEAT_XIM
2806 im_shutdown();
2807#endif
2808#ifdef HAVE_GTK2
2809 if (gui.ascii_glyphs != NULL)
2810 {
2811 pango_glyph_string_free(gui.ascii_glyphs);
2812 gui.ascii_glyphs = NULL;
2813 }
2814 if (gui.ascii_font != NULL)
2815 {
2816 g_object_unref(gui.ascii_font);
2817 gui.ascii_font = NULL;
2818 }
2819 g_object_unref(gui.text_context);
2820 gui.text_context = NULL;
2821
2822 g_object_unref(gui.text_gc);
2823 gui.text_gc = NULL;
2824
2825 gdk_cursor_unref(gui.blank_pointer);
2826 gui.blank_pointer = NULL;
2827#else
2828 gdk_gc_unref(gui.text_gc);
2829 gui.text_gc = NULL;
2830
2831 gdk_cursor_destroy(gui.blank_pointer);
2832 gui.blank_pointer = NULL;
2833#endif
2834}
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002837drawarea_style_set_cb(GtkWidget *widget UNUSED,
2838 GtkStyle *previous_style UNUSED,
2839 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 gui_mch_new_colors();
2842}
2843
2844/*
2845 * Callback routine for the "delete_event" signal on the toplevel window.
2846 * Tries to vim gracefully, or refuses to exit with changed buffers.
2847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002849delete_event_cb(GtkWidget *widget UNUSED,
2850 GdkEventAny *event UNUSED,
2851 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852{
2853 gui_shell_closed();
2854 return TRUE;
2855}
2856
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002857#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2858 static int
2859get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2860{
2861 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2862
2863#ifdef FEAT_GUI_GNOME
2864 if (using_gnome && widget != NULL)
2865 {
2866# ifdef HAVE_GTK2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002867 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002868 BonoboDockItem *dockitem;
2869
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002870 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002871 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2872 {
2873 /* Only menu & toolbar are dock items. Could tabline be?
2874 * Seem to be only the 2 defined in GNOME */
2875 widget = parent;
2876 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002877
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002878 if (dockitem == NULL || dockitem->is_floating)
2879 return 0;
2880 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2881 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002882# else
2883 GnomeDockItem *dockitem;
2884
2885 widget = widget->parent;
2886 dockitem = GNOME_DOCK_ITEM(widget);
2887
2888 if (dockitem == NULL || dockitem->is_floating)
2889 return 0;
2890 item_orientation = gnome_dock_item_get_orientation(dockitem);
2891# endif
2892 }
2893#endif
2894 if (widget != NULL
2895 && item_orientation == orientation
2896 && GTK_WIDGET_REALIZED(widget)
2897 && GTK_WIDGET_VISIBLE(widget))
2898 {
2899 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2900 return widget->allocation.height;
2901 else
2902 return widget->allocation.width;
2903 }
2904 return 0;
2905}
2906#endif
2907
2908 static int
2909get_menu_tool_width(void)
2910{
2911 int width = 0;
2912
2913#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2914# ifdef FEAT_MENU
2915 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2916# endif
2917# ifdef FEAT_TOOLBAR
2918 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2919# endif
2920# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002921 if (gui.tabline != NULL)
2922 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002923# endif
2924#endif
2925
2926 return width;
2927}
2928
2929 static int
2930get_menu_tool_height(void)
2931{
2932 int height = 0;
2933
2934#ifdef FEAT_MENU
2935 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2936#endif
2937#ifdef FEAT_TOOLBAR
2938 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2939#endif
2940#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002941 if (gui.tabline != NULL)
2942 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002943#endif
2944
2945 return height;
2946}
2947
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002948/* This controls whether we can set the real window hints at
2949 * start-up when in a GtkPlug.
2950 * 0 = normal processing (default)
2951 * 1 = init. hints set, no-one's tried to reset since last check
2952 * 2 = init. hints set, attempt made to change hints
2953 */
2954static int init_window_hints_state = 0;
2955
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002956 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002957update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002958{
2959 static int old_width = 0;
2960 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002961 static int old_min_width = 0;
2962 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002963 static int old_char_width = 0;
2964 static int old_char_height = 0;
2965
2966 int width;
2967 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002968 int min_width;
2969 int min_height;
2970
2971 /* At start-up, don't try to set the hints until the initial
2972 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002973 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002974 */
2975 if (!(force_width && force_height) && init_window_hints_state > 0)
2976 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002977 /* Don't do it! */
2978 init_window_hints_state = 2;
2979 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002980 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002981
2982 /* This also needs to be done when the main window isn't there yet,
2983 * otherwise the hints don't work. */
2984 width = gui_get_base_width();
2985 height = gui_get_base_height();
2986# ifdef FEAT_MENU
2987 height += tabline_height() * gui.char_height;
2988# endif
2989# ifdef HAVE_GTK2
2990 width += get_menu_tool_width();
2991 height += get_menu_tool_height();
2992# endif
2993
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002994 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002995 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002996 * 'set columns=' or init. -geom) we briefly set the min. to the size
2997 * we wish to be instead of the legitimate minimum so that we actually
2998 * resize correctly.
2999 */
3000 if (force_width && force_height)
3001 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003002 min_width = force_width;
3003 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003004 }
3005 else
3006 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003007 min_width = width + MIN_COLUMNS * gui.char_width;
3008 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003009 }
3010
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003011 /* Avoid an expose event when the size didn't change. */
3012 if (width != old_width
3013 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003014 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003015 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003016 || gui.char_width != old_char_width
3017 || gui.char_height != old_char_height)
3018 {
3019 GdkGeometry geometry;
3020 GdkWindowHints geometry_mask;
3021
3022 geometry.width_inc = gui.char_width;
3023 geometry.height_inc = gui.char_height;
3024 geometry.base_width = width;
3025 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003026 geometry.min_width = min_width;
3027 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003028 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3029 |GDK_HINT_MIN_SIZE;
3030# ifdef HAVE_GTK2
3031 /* Using gui.formwin as geometry widget doesn't work as expected
3032 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3033 * in Vim confuse GTK+. */
3034 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3035 &geometry, geometry_mask);
3036# else
3037 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.formwin,
3038 &geometry, geometry_mask);
3039# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003040 old_width = width;
3041 old_height = height;
3042 old_min_width = min_width;
3043 old_min_height = min_height;
3044 old_char_width = gui.char_width;
3045 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003046 }
3047}
3048
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049#ifdef FEAT_TOOLBAR
3050
3051# ifdef HAVE_GTK2
3052/*
3053 * This extra effort wouldn't be necessary if we only used stock icons in the
3054 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3055 * shouldn't be treated differently, thus we do need this.
3056 */
3057 static void
3058icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3059{
3060 if (GTK_IS_IMAGE(widget))
3061 {
3062 GtkImage *image = (GtkImage *)widget;
3063
3064 /* User-defined icons are stored in a GtkIconSet */
3065 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3066 {
3067 GtkIconSet *icon_set;
3068 GtkIconSize icon_size;
3069
3070 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3071 icon_size = (GtkIconSize)(long)user_data;
3072
3073 gtk_icon_set_ref(icon_set);
3074 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3075 gtk_icon_set_unref(icon_set);
3076 }
3077 }
3078 else if (GTK_IS_CONTAINER(widget))
3079 {
3080 gtk_container_foreach((GtkContainer *)widget,
3081 &icon_size_changed_foreach,
3082 user_data);
3083 }
3084}
3085# endif /* HAVE_GTK2 */
3086
3087 static void
3088set_toolbar_style(GtkToolbar *toolbar)
3089{
3090 GtkToolbarStyle style;
3091# ifdef HAVE_GTK2
3092 GtkIconSize size;
3093 GtkIconSize oldsize;
3094# endif
3095
3096# ifdef HAVE_GTK2
3097 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3098 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3099 style = GTK_TOOLBAR_BOTH_HORIZ;
3100 else
3101# endif
3102 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
3103 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3104 style = GTK_TOOLBAR_BOTH;
3105 else if (toolbar_flags & TOOLBAR_TEXT)
3106 style = GTK_TOOLBAR_TEXT;
3107 else
3108 style = GTK_TOOLBAR_ICONS;
3109
3110 gtk_toolbar_set_style(toolbar, style);
3111 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
3112
3113# ifdef HAVE_GTK2
3114 switch (tbis_flags)
3115 {
3116 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3117 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3118 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3119 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
3120 default: size = GTK_ICON_SIZE_INVALID; break;
3121 }
3122 oldsize = gtk_toolbar_get_icon_size(toolbar);
3123
3124 if (size == GTK_ICON_SIZE_INVALID)
3125 {
3126 /* Let global user preferences decide the icon size. */
3127 gtk_toolbar_unset_icon_size(toolbar);
3128 size = gtk_toolbar_get_icon_size(toolbar);
3129 }
3130 if (size != oldsize)
3131 {
3132 gtk_container_foreach(GTK_CONTAINER(toolbar),
3133 &icon_size_changed_foreach,
3134 GINT_TO_POINTER((int)size));
3135 }
3136 gtk_toolbar_set_icon_size(toolbar, size);
3137# endif
3138}
3139
3140#endif /* FEAT_TOOLBAR */
3141
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003142#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3143static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003144static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003145static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00003146static int clicked_page; /* page clicked in tab line */
3147
3148/*
3149 * Handle selecting an item in the tab line popup menu.
3150 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003151 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003152tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003153{
Bram Moolenaara5621492006-02-25 21:55:24 +00003154 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003155 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003156
3157 if (gtk_main_level() > 0)
3158 gtk_main_quit();
3159}
3160
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003161 static void
3162add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3163{
3164 GtkWidget *item;
3165 char_u *utf_text;
3166
3167 utf_text = CONVERT_TO_UTF8(text);
3168 item = gtk_menu_item_new_with_label((const char *)utf_text);
3169 gtk_widget_show(item);
3170 CONVERT_TO_UTF8_FREE(utf_text);
3171
3172 gtk_container_add(GTK_CONTAINER(menu), item);
3173 gtk_signal_connect(GTK_OBJECT(item), "activate",
3174 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003175 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003176}
3177
Bram Moolenaara5621492006-02-25 21:55:24 +00003178/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003179 * Create a menu for the tab line.
3180 */
3181 static GtkWidget *
3182create_tabline_menu(void)
3183{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003184 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003185
3186 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003187 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
3188 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3189 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003190
3191 return menu;
3192}
3193
3194 static gboolean
3195on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3196{
3197 /* Was this button press event ? */
3198 if (event->type == GDK_BUTTON_PRESS)
3199 {
3200 GdkEventButton *bevent = (GdkEventButton *)event;
3201 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003202 int y = bevent->y;
3203 GtkWidget *tabwidget;
3204 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003205
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003206 /* When ignoring events return TRUE so that the selected page doesn't
3207 * change. */
3208 if (hold_gui_events
3209# ifdef FEAT_CMDWIN
3210 || cmdwin_type != 0
3211# endif
3212 )
3213 return TRUE;
3214
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003215 tabwin = gdk_window_at_pointer(&x, &y);
3216 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
3217 clicked_page = (int)(long)gtk_object_get_user_data(
3218 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00003219
3220 /* If the event was generated for 3rd button popup the menu. */
3221 if (bevent->button == 3)
3222 {
3223 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3224 bevent->button, bevent->time);
3225 /* We handled the event. */
3226 return TRUE;
3227 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003228 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003229 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003230 if (clicked_page == 0)
3231 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003232 /* Click after all tabs moves to next tab page. When "x" is
3233 * small guess it's the left button. */
3234 if (send_tabline_event(x < 50 ? -1 : 0) && gtk_main_level() > 0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003235 gtk_main_quit();
3236 }
3237#ifndef HAVE_GTK2
3238 else
3239 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline),
3240 clicked_page - 1);
3241#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003242 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003243 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003244
Bram Moolenaara5621492006-02-25 21:55:24 +00003245 /* We didn't handle the event. */
3246 return FALSE;
3247}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003248
3249/*
3250 * Handle selecting one of the tabs.
3251 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252 static void
3253on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003254 GtkNotebook *notebook UNUSED,
3255 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003256 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003257 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003258{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003259 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003260 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003261 if (send_tabline_event(idx + 1) && gtk_main_level() > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003262 gtk_main_quit();
3263 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003264}
3265
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003266#ifndef HAVE_GTK2
3267static int showing_tabline = 0;
3268#endif
3269
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003270/*
3271 * Show or hide the tabline.
3272 */
3273 void
3274gui_mch_show_tabline(int showit)
3275{
3276 if (gui.tabline == NULL)
3277 return;
3278
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003279#ifdef HAVE_GTK2
3280 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003281 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003282#else
3283 if (!showit != !showing_tabline)
3284#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003285 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003286 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003287 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003288 update_window_manager_hints(0, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003289#ifndef HAVE_GTK2
3290 showing_tabline = showit;
3291#endif
Bram Moolenaar96351572006-05-05 21:16:59 +00003292 if (showit)
3293 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003294 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003295
3296 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003297}
3298
3299/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003300 * Return TRUE when tabline is displayed.
3301 */
3302 int
3303gui_mch_showing_tabline(void)
3304{
3305 return gui.tabline != NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003306#ifdef HAVE_GTK2
3307 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3308 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))
3309#else
3310 && showing_tabline
3311#endif
3312 ;
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003313}
3314
3315/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003316 * Update the labels of the tabline.
3317 */
3318 void
3319gui_mch_update_tabline(void)
3320{
3321 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003322 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003323 GtkWidget *label;
3324 tabpage_T *tp;
3325 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003326 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003327 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003328 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003329
3330 if (gui.tabline == NULL)
3331 return;
3332
3333 ignore_tabline_evt = TRUE;
3334
3335 /* Add a label for each tab page. They all contain the same text area. */
3336 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3337 {
3338 if (tp == curtab)
3339 curtabidx = nr;
3340
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003341 tab_num = nr + 1;
3342
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003343 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3344 if (page == NULL)
3345 {
3346 /* Add notebook page */
3347 page = gtk_vbox_new(FALSE, 0);
3348 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003349 event_box = gtk_event_box_new();
3350 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003351 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003352 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003353 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003354 gtk_widget_show(label);
3355 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3356 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003357 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358 nr++);
3359 }
3360
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003361 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003362 gtk_object_set_user_data(GTK_OBJECT(event_box),
3363 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003364 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003365 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003366 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003367 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003368 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003369
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003370 get_tabline_label(tp, TRUE);
3371 labeltext = CONVERT_TO_UTF8(NameBuff);
3372 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3373 (const char *)labeltext, NULL);
3374 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003375 }
3376
3377 /* Remove any old labels. */
3378 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3379 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3380
3381 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003382 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003383
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003384 /* Make sure everything is in place before drawing text. */
3385 gui_mch_update();
3386
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003387 ignore_tabline_evt = FALSE;
3388}
3389
3390/*
3391 * Set the current tab to "nr". First tab is 1.
3392 */
3393 void
3394gui_mch_set_curtab(nr)
3395 int nr;
3396{
3397 if (gui.tabline == NULL)
3398 return;
3399
3400 ignore_tabline_evt = TRUE;
3401 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003402 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003403 ignore_tabline_evt = FALSE;
3404}
3405
3406#endif /* FEAT_GUI_TABLINE */
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408/*
3409 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3410 * Returns OK for success, FAIL when the GUI can't be started.
3411 */
3412 int
3413gui_mch_init(void)
3414{
3415 GtkWidget *vbox;
3416
3417#ifdef FEAT_GUI_GNOME
3418 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3419 * exits on failure, but that's a non-issue because we already called
3420 * gtk_init_check() in gui_mch_init_check(). */
3421 if (using_gnome)
3422# ifdef HAVE_GTK2
3423 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3424 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
3425# else
3426 gnome_init(VIMPACKAGE, VIM_VERSION_SHORT, gui_argc, gui_argv);
3427# endif
3428#endif
3429 vim_free(gui_argv);
3430 gui_argv = NULL;
3431
3432#ifdef HAVE_GTK2
3433# if GLIB_CHECK_VERSION(2,1,3)
3434 /* Set the human-readable application name */
3435 g_set_application_name("Vim");
3436# endif
3437 /*
3438 * Force UTF-8 output no matter what the value of 'encoding' is.
3439 * did_set_string_option() in option.c prohibits changing 'termencoding'
3440 * to something else than UTF-8 if the GUI is in use.
3441 */
3442 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3443
3444# ifdef FEAT_TOOLBAR
3445 gui_gtk_register_stock_icons();
3446# endif
3447 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3448 * The hard part is deciding install locations and the Makefile magic. */
3449# if 0
3450 gtk_rc_parse("gtkrc");
3451# endif
3452#endif
3453
3454 /* Initialize values */
3455 gui.border_width = 2;
3456 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3457 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003458 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003460 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003462 /* LINTED: avoid warning: conversion to 'unsigned long' */
3463 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464
3465 /* Initialise atoms */
3466#ifdef FEAT_MBYTE
3467 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3468#endif
3469#ifndef HAVE_GTK2
3470 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3471 text_atom = gdk_atom_intern("TEXT", FALSE);
3472#endif
3473
3474 /* Set default foreground and background colors. */
3475 gui.norm_pixel = gui.def_norm_pixel;
3476 gui.back_pixel = gui.def_back_pixel;
3477
3478 if (gtk_socket_id != 0)
3479 {
3480 GtkWidget *plug;
3481
3482 /* Use GtkSocket from another app. */
3483#ifdef HAVE_GTK_MULTIHEAD
3484 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3485 gtk_socket_id);
3486#else
3487 plug = gtk_plug_new(gtk_socket_id);
3488#endif
3489 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3490 {
3491 gui.mainwin = plug;
3492 }
3493 else
3494 {
3495 g_warning("Connection to GTK+ socket (ID %u) failed",
3496 (unsigned int)gtk_socket_id);
3497 /* Pretend we never wanted it if it failed (get own window) */
3498 gtk_socket_id = 0;
3499 }
3500 }
3501
3502 if (gtk_socket_id == 0)
3503 {
3504#ifdef FEAT_GUI_GNOME
3505 if (using_gnome)
3506 {
3507 gui.mainwin = gnome_app_new("Vim", NULL);
3508# ifdef USE_XSMP
3509 /* Use the GNOME save-yourself functionality now. */
3510 xsmp_close();
3511# endif
3512 }
3513 else
3514#endif
3515 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3516 }
3517
3518 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3519
3520#ifdef HAVE_GTK2
3521 /* Create the PangoContext used for drawing all text. */
3522 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3523 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
3524#endif
3525
3526#ifndef HAVE_GTK2
3527 gtk_window_set_policy(GTK_WINDOW(gui.mainwin), TRUE, TRUE, TRUE);
3528#endif
3529 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3530 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3531
3532 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3533 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3534
3535 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3536 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3537#ifdef HAVE_GTK_MULTIHEAD
3538 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3539 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3540#endif
3541#ifdef HAVE_GTK2
3542 gui.accel_group = gtk_accel_group_new();
3543 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
3544#else
3545 gui.accel_group = gtk_accel_group_get_default();
3546#endif
3547
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003548 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 vbox = gtk_vbox_new(FALSE, 0);
3550
3551#ifdef FEAT_GUI_GNOME
3552 if (using_gnome)
3553 {
3554# if defined(HAVE_GTK2) && defined(FEAT_MENU)
3555 /* automagically restore menubar/toolbar placement */
3556 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3557# endif
3558 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3559 }
3560 else
3561#endif
3562 {
3563 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3564 gtk_widget_show(vbox);
3565 }
3566
3567#ifdef FEAT_MENU
3568 /*
3569 * Create the menubar and handle
3570 */
3571 gui.menubar = gtk_menu_bar_new();
3572 gtk_widget_set_name(gui.menubar, "vim-menubar");
3573
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003574# ifdef HAVE_GTK2
3575 /* Avoid that GTK takes <F10> away from us. */
3576 {
3577 GtkSettings *gtk_settings;
3578
3579 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3580 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3581 }
3582# endif
3583
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585# ifdef FEAT_GUI_GNOME
3586 if (using_gnome)
3587 {
3588# ifdef HAVE_GTK2
3589 BonoboDockItem *dockitem;
3590
3591 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3592 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3593 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003594 /* We don't want the menu to float. */
3595 bonobo_dock_item_set_behavior(dockitem,
3596 bonobo_dock_item_get_behavior(dockitem)
3597 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 gui.menubar_h = GTK_WIDGET(dockitem);
3599# else
3600 gui.menubar_h = gnome_dock_item_new("VimMainMenu",
3601 GNOME_DOCK_ITEM_BEH_EXCLUSIVE |
3602 GNOME_DOCK_ITEM_BEH_NEVER_VERTICAL);
3603 gtk_container_add(GTK_CONTAINER(gui.menubar_h), gui.menubar);
3604
3605 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3606 GNOME_DOCK_ITEM(gui.menubar_h),
3607 GNOME_DOCK_TOP, /* placement */
3608 1, /* band_num */
3609 0, /* band_position */
3610 0, /* offset */
3611 TRUE);
3612 gtk_widget_show(gui.menubar);
3613# endif
3614 }
3615 else
3616# endif /* FEAT_GUI_GNOME */
3617 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003618 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3619 * disabled in gui_init() later. */
3620 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3622 }
3623#endif /* FEAT_MENU */
3624
3625#ifdef FEAT_TOOLBAR
3626 /*
3627 * Create the toolbar and handle
3628 */
3629# ifdef HAVE_GTK2
3630 /* some aesthetics on the toolbar */
3631 gtk_rc_parse_string(
3632 "style \"vim-toolbar-style\" {\n"
3633 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3634 "}\n"
3635 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3636 gui.toolbar = gtk_toolbar_new();
3637 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
3638# else
3639 gui.toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3640 GTK_TOOLBAR_ICONS);
3641 gtk_toolbar_set_button_relief(GTK_TOOLBAR(gui.toolbar), GTK_RELIEF_NONE);
3642# endif
3643 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3644
3645# ifdef FEAT_GUI_GNOME
3646 if (using_gnome)
3647 {
3648# ifdef HAVE_GTK2
3649 BonoboDockItem *dockitem;
3650
3651 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3652 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3653 GNOME_APP_TOOLBAR_NAME);
3654 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003655 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003656 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003657 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003658 bonobo_dock_item_get_behavior(dockitem)
3659 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
3661# else
3662 GtkWidget *dockitem;
3663
3664 dockitem = gnome_dock_item_new("VimToolBar",
3665 GNOME_DOCK_ITEM_BEH_EXCLUSIVE);
3666 gtk_container_add(GTK_CONTAINER(dockitem), GTK_WIDGET(gui.toolbar));
3667 gui.toolbar_h = dockitem;
3668
3669 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3670 GNOME_DOCK_ITEM(dockitem),
3671 GNOME_DOCK_TOP, /* placement */
3672 1, /* band_num */
3673 1, /* band_position */
3674 0, /* offset */
3675 TRUE);
3676 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 2);
3677 gtk_widget_show(gui.toolbar);
3678# endif
3679 }
3680 else
3681# endif /* FEAT_GUI_GNOME */
3682 {
3683# ifndef HAVE_GTK2
3684 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 1);
3685# endif
3686 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3687 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3688 gtk_widget_show(gui.toolbar);
3689 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3690 }
3691#endif /* FEAT_TOOLBAR */
3692
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003693#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003694 /*
3695 * Use a Notebook for the tab pages labels. The labels are hidden by
3696 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003697 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003698 gui.tabline = gtk_notebook_new();
3699 gtk_widget_show(gui.tabline);
3700 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3701 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3702 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003703 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003704 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003705
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003706 tabline_tooltip = gtk_tooltips_new();
3707 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3708
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003709 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003710 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003711
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003712 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003713 page = gtk_vbox_new(FALSE, 0);
3714 gtk_widget_show(page);
3715 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3716 label = gtk_label_new("-Empty-");
3717 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003718 event_box = gtk_event_box_new();
3719 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003720 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003721 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003722 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003723 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003724 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003725
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003726 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003727 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003728
3729 /* Create a popup menu for the tab line and connect it. */
3730 tabline_menu = create_tabline_menu();
3731 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003732 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003733#endif
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 gui.formwin = gtk_form_new();
3736 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3737 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3738
3739 gui.drawarea = gtk_drawing_area_new();
3740
3741 /* Determine which events we will filter. */
3742 gtk_widget_set_events(gui.drawarea,
3743 GDK_EXPOSURE_MASK |
3744 GDK_ENTER_NOTIFY_MASK |
3745 GDK_LEAVE_NOTIFY_MASK |
3746 GDK_BUTTON_PRESS_MASK |
3747 GDK_BUTTON_RELEASE_MASK |
3748#ifdef HAVE_GTK2
3749 GDK_SCROLL_MASK |
3750#endif
3751 GDK_KEY_PRESS_MASK |
3752 GDK_KEY_RELEASE_MASK |
3753 GDK_POINTER_MOTION_MASK |
3754 GDK_POINTER_MOTION_HINT_MASK);
3755
3756 gtk_widget_show(gui.drawarea);
3757 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3758 gtk_widget_show(gui.formwin);
3759 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3760
3761 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3762 * and not the window. */
3763 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3764 : GTK_OBJECT(gui.drawarea),
3765 "key_press_event",
3766 GTK_SIGNAL_FUNC(key_press_event), NULL);
3767#if defined(FEAT_XIM) && defined(HAVE_GTK2)
3768 /* Also forward key release events for the benefit of GTK+ 2 input
3769 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3770 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3771 : G_OBJECT(gui.drawarea),
3772 "key_release_event",
3773 G_CALLBACK(&key_release_event), NULL);
3774#endif
3775 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3776 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3777 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3778 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3779
3780 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3781 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3782
3783 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3784
3785#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3786 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3787 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3788#endif
3789
3790 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003791 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3793
3794 /*
3795 * Set clipboard specific atoms
3796 */
3797 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
3798#ifdef FEAT_MBYTE
3799 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
3800#endif
3801 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3802 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3803
3804 /*
3805 * Start out by adding the configured border width into the border offset.
3806 */
3807 gui.border_offset = gui.border_width;
3808
3809 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3810 GTK_SIGNAL_FUNC(visibility_event), NULL);
3811 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3812 GTK_SIGNAL_FUNC(expose_event), NULL);
3813
3814 /*
3815 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3816 * Only needed for some window managers.
3817 */
3818 if (vim_strchr(p_go, GO_POINTER) != NULL)
3819 {
3820 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3821 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3822 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3823 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3824 }
3825
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003826 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3827 * only its widgets. Arguably, this could be common code and we not use
3828 * the window focus at all, but let's be safe.
3829 */
3830 if (gtk_socket_id == 0)
3831 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003832 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3833 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3834 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3835 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003836 }
3837 else
3838 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003839 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3840 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3841 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3842 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003843#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003844 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3845 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3846 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3847 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003848#endif /* FEAT_GUI_TABLINE */
3849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3852 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3853 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3854 GTK_SIGNAL_FUNC(button_press_event), NULL);
3855 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3856 GTK_SIGNAL_FUNC(button_release_event), NULL);
3857#ifdef HAVE_GTK2
3858 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3859 G_CALLBACK(&scroll_event), NULL);
3860#endif
3861
3862 /*
3863 * Add selection handler functions.
3864 */
3865 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3866 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3867 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3868 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3869
3870 /*
3871 * Add selection targets for PRIMARY and CLIPBOARD selections.
3872 */
3873 gtk_selection_add_targets(gui.drawarea,
3874 (GdkAtom)GDK_SELECTION_PRIMARY,
3875 selection_targets, N_SELECTION_TARGETS);
3876 gtk_selection_add_targets(gui.drawarea,
3877 (GdkAtom)clip_plus.gtk_sel_atom,
3878 selection_targets, N_SELECTION_TARGETS);
3879
3880 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3881 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3882
3883 /* Pretend we don't have input focus, we will get an event if we do. */
3884 gui.in_focus = FALSE;
3885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 return OK;
3887}
3888
3889#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3890/*
3891 * This is called from gui_start() after a fork() has been done.
3892 * We have to tell the session manager our new PID.
3893 */
3894 void
3895gui_mch_forked(void)
3896{
3897 if (using_gnome)
3898 {
3899 GnomeClient *client;
3900
3901 client = gnome_master_client();
3902
3903 if (client != NULL)
3904 gnome_client_set_process_id(client, getpid());
3905 }
3906}
3907#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3908
3909/*
3910 * Called when the foreground or background color has been changed.
3911 * This used to change the graphics contexts directly but we are
3912 * currently manipulating them where desired.
3913 */
3914 void
3915gui_mch_new_colors(void)
3916{
3917 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3918 {
3919 GdkColor color = { 0, 0, 0, 0 };
3920
3921 color.pixel = gui.back_pixel;
3922 gdk_window_set_background(gui.drawarea->window, &color);
3923 }
3924}
3925
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926/*
3927 * This signal informs us about the need to rearrange our sub-widgets.
3928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003930form_configure_event(GtkWidget *widget UNUSED,
3931 GdkEventConfigure *event,
3932 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003934 int usable_height = event->height;
3935
3936 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3937 * no. of char-heights), so we have to manually sanitise them.
3938 * Widths seem to sort themselves out, don't ask me why.
3939 */
3940 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003941 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003944 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 gtk_form_thaw(GTK_FORM(gui.formwin));
3946
3947 return TRUE;
3948}
3949
3950/*
3951 * Function called when window already closed.
3952 * We can't do much more here than to trying to preserve what had been done,
3953 * since the window is already inevitably going away.
3954 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003956mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 /* Don't write messages to the GUI anymore */
3959 full_screen = FALSE;
3960
3961 gui.mainwin = NULL;
3962 gui.drawarea = NULL;
3963
3964 if (!exiting) /* only do anything if the destroy was unexpected */
3965 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003966 vim_strncpy(IObuff,
3967 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3968 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 preserve_exit();
3970 }
3971}
3972
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003973
3974/*
3975 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3976 * hints (and thus the required size from -geom), but that after that we
3977 * put the hints back to normal (the actual minimum size) so we may
3978 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003979 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003980 * only way we have of making ourselves bigger (by set lines/columns).
3981 * Thus set hints at start-up to ensure correct init. size, then a
3982 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003983 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003984 * We'll not let the default hints be set while this timer's active.
3985 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003986 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003987check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003988{
3989 if (init_window_hints_state == 1)
3990 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003991 /* Safe to use normal hints now */
3992 init_window_hints_state = 0;
3993 update_window_manager_hints(0, 0);
3994 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003995 }
3996
3997 /* Keep on trying */
3998 init_window_hints_state = 1;
3999 return TRUE;
4000}
4001
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003/*
4004 * Open the GUI window which was created by a call to gui_mch_init().
4005 */
4006 int
4007gui_mch_open(void)
4008{
4009 guicolor_T fg_pixel = INVALCOLOR;
4010 guicolor_T bg_pixel = INVALCOLOR;
4011
4012#ifdef HAVE_GTK2
4013 /*
4014 * Allow setting a window role on the command line, or invent one
4015 * if none was specified. This is mainly useful for GNOME session
4016 * support; allowing the WM to restore window placement.
4017 */
4018 if (role_argument != NULL)
4019 {
4020 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4021 }
4022 else
4023 {
4024 char *role;
4025
4026 /* Invent a unique-enough ID string for the role */
4027 role = g_strdup_printf("vim-%u-%u-%u",
4028 (unsigned)mch_get_pid(),
4029 (unsigned)g_random_int(),
4030 (unsigned)time(NULL));
4031
4032 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4033 g_free(role);
4034 }
4035#endif
4036
4037 if (gui_win_x != -1 && gui_win_y != -1)
4038#ifdef HAVE_GTK2
4039 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
4040#else
4041 gtk_widget_set_uposition(gui.mainwin, gui_win_x, gui_win_y);
4042#endif
4043
4044 /* Determine user specified geometry, if present. */
4045 if (gui.geom != NULL)
4046 {
4047 int mask;
4048 unsigned int w, h;
4049 int x = 0;
4050 int y = 0;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004051 guint pixel_width;
4052 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053
4054 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4055
4056 if (mask & WidthValue)
4057 Columns = w;
4058 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004059 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004060 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004061 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004063 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004064
4065 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4066 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4067
4068#ifdef HAVE_GTK2
4069 pixel_width += get_menu_tool_width();
4070 pixel_height += get_menu_tool_height();
4071#endif
4072
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004074 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004075 int ww, hh;
4076 gui_mch_get_screen_dimensions(&ww, &hh);
4077 hh += p_ghr + get_menu_tool_height();
4078 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004079 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004080 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004081 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004082 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083#ifdef HAVE_GTK2
4084 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4085#else
4086 gtk_widget_set_uposition(gui.mainwin, x, y);
4087#endif
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 vim_free(gui.geom);
4090 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004091
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004092 /* From now until everyone's stopped trying to set the window hints
4093 * to their correct minimum values, stop them being set as we need
4094 * them to remain at our required size for the parent GtkSocket to
4095 * give us the right initial size.
4096 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004097 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004098 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004099 update_window_manager_hints(pixel_width, pixel_height);
4100 init_window_hints_state = 1;
4101 g_timeout_add(1000, check_startup_plug_hints, NULL);
4102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104
4105 gtk_form_set_size(GTK_FORM(gui.formwin),
4106 (guint)(gui_get_base_width() + Columns * gui.char_width),
4107 (guint)(gui_get_base_height() + Rows * gui.char_height));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004108 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 if (foreground_argument != NULL)
4111 fg_pixel = gui_get_color((char_u *)foreground_argument);
4112 if (fg_pixel == INVALCOLOR)
4113 fg_pixel = gui_get_color((char_u *)"Black");
4114
4115 if (background_argument != NULL)
4116 bg_pixel = gui_get_color((char_u *)background_argument);
4117 if (bg_pixel == INVALCOLOR)
4118 bg_pixel = gui_get_color((char_u *)"White");
4119
4120 if (found_reverse_arg)
4121 {
4122 gui.def_norm_pixel = bg_pixel;
4123 gui.def_back_pixel = fg_pixel;
4124 }
4125 else
4126 {
4127 gui.def_norm_pixel = fg_pixel;
4128 gui.def_back_pixel = bg_pixel;
4129 }
4130
4131 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4132 * in a vimrc file) */
4133 set_normal_colors();
4134
4135 /* Check that none of the colors are the same as the background color */
4136 gui_check_colors();
4137
4138 /* Get the colors for the highlight groups (gui_check_colors() might have
4139 * changed them). */
4140 highlight_gui_started(); /* re-init colors and fonts */
4141
4142 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4143 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
4144
4145#ifdef FEAT_HANGULIN
4146 hangul_keyboard_set();
4147#endif
4148
4149 /*
4150 * Notify the fixed area about the need to resize the contents of the
4151 * gui.formwin, which we use for random positioning of the included
4152 * components.
4153 *
4154 * We connect this signal deferred finally after anything is in place,
4155 * since this is intended to handle resizements coming from the window
4156 * manager upon us and should not interfere with what VIM is requesting
4157 * upon startup.
4158 */
4159 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4160 GTK_SIGNAL_FUNC(form_configure_event), NULL);
4161
4162#ifdef FEAT_DND
4163 /*
4164 * Set up for receiving DND items.
4165 */
4166 gtk_drag_dest_set(gui.drawarea,
4167 GTK_DEST_DEFAULT_ALL,
4168 dnd_targets, N_DND_TARGETS,
4169 GDK_ACTION_COPY);
4170
4171 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4172 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
4173#endif
4174
4175#ifdef HAVE_GTK2
4176 /* With GTK+ 2, we need to iconify the window before calling show()
4177 * to avoid mapping the window for a short time. This is just as one
4178 * would expect it to work, but it's different in GTK+ 1. The funny
4179 * thing is that iconifying after show() _does_ work with GTK+ 1.
4180 * (BTW doing this in the "realize" handler makes no difference.) */
4181 if (found_iconic_arg && gtk_socket_id == 0)
4182 gui_mch_iconify();
4183#endif
4184
4185 {
4186#if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4187 unsigned long menu_handler = 0;
4188# ifdef FEAT_TOOLBAR
4189 unsigned long tool_handler = 0;
4190# endif
4191 /*
4192 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4193 * show when restoring the saved layout configuration. We can't just
4194 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4195 * a toplevel window and thus will be realized immediately. Instead,
4196 * connect signal handlers to hide the widgets just after they've been
4197 * marked visible, but before the main window is realized.
4198 */
4199 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4200 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4201 G_CALLBACK(&gtk_widget_hide),
4202 NULL);
4203# ifdef FEAT_TOOLBAR
4204 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4205 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4206 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4207 G_CALLBACK(&gtk_widget_hide),
4208 NULL);
4209# endif
4210#endif
4211 gtk_widget_show(gui.mainwin);
4212
4213#if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4214 if (menu_handler != 0)
4215 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4216# ifdef FEAT_TOOLBAR
4217 if (tool_handler != 0)
4218 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4219# endif
4220#endif
4221 }
4222
4223#ifndef HAVE_GTK2
4224 /* With GTK+ 1, we need to iconify the window after calling show().
4225 * See the comment above for details. */
4226 if (found_iconic_arg && gtk_socket_id == 0)
4227 gui_mch_iconify();
4228#endif
4229
4230 return OK;
4231}
4232
4233
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004235gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 if (gui.mainwin != NULL)
4238 gtk_widget_destroy(gui.mainwin);
4239
4240 if (gtk_main_level() > 0)
4241 gtk_main_quit();
4242}
4243
4244/*
4245 * Get the position of the top left corner of the window.
4246 */
4247 int
4248gui_mch_get_winpos(int *x, int *y)
4249{
4250#ifdef HAVE_GTK2
4251 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
4252#else
4253 /* For some people this must be gdk_window_get_origin() for a correct
4254 * result. Where is the documentation! */
4255 gdk_window_get_root_origin(gui.mainwin->window, x, y);
4256#endif
4257 return OK;
4258}
4259
4260/*
4261 * Set the position of the top left corner of the window to the given
4262 * coordinates.
4263 */
4264 void
4265gui_mch_set_winpos(int x, int y)
4266{
4267#ifdef HAVE_GTK2
4268 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4269#else
4270 gdk_window_move(gui.mainwin->window, x, y);
4271#endif
4272}
4273
4274#ifdef HAVE_GTK2
4275#if 0
4276static int resize_idle_installed = FALSE;
4277/*
4278 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4279 * the shell size doesn't exceed the window size, i.e. if the window manager
4280 * ignored our size request. Usually this happens if the window is maximized.
4281 *
4282 * FIXME: It'd be nice if we could find a little more orthodox solution.
4283 * See also the remark below in gui_mch_set_shellsize().
4284 *
4285 * DISABLED: When doing ":set lines+=1" this function would first invoke
4286 * gui_resize_shell() with the old size, then the normal callback would
4287 * report the new size through form_configure_event(). That caused the window
4288 * layout to be messed up.
4289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 static gboolean
4291force_shell_resize_idle(gpointer data)
4292{
4293 if (gui.mainwin != NULL
4294 && GTK_WIDGET_REALIZED(gui.mainwin)
4295 && GTK_WIDGET_VISIBLE(gui.mainwin))
4296 {
4297 int width;
4298 int height;
4299
4300 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4301
4302 width -= get_menu_tool_width();
4303 height -= get_menu_tool_height();
4304
4305 gui_resize_shell(width, height);
4306 }
4307
4308 resize_idle_installed = FALSE;
4309 return FALSE; /* don't call me again */
4310}
4311#endif
4312#endif /* HAVE_GTK2 */
4313
4314/*
4315 * Set the windows size.
4316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 void
4318gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004319 int min_width UNUSED, int min_height UNUSED,
4320 int base_width UNUSED, int base_height UNUSED,
4321 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
4323#ifndef HAVE_GTK2
4324 /* Hack: When the form already is at the desired size, the window might
4325 * have been resized with the mouse. Force a resize by setting a
4326 * different size first. */
4327 if (GTK_FORM(gui.formwin)->width == width
4328 && GTK_FORM(gui.formwin)->height == height)
4329 {
4330 gtk_form_set_size(GTK_FORM(gui.formwin), width + 1, height + 1);
4331 gui_mch_update();
4332 }
4333 gtk_form_set_size(GTK_FORM(gui.formwin), width, height);
4334#endif
4335
4336 /* give GTK+ a chance to put all widget's into place */
4337 gui_mch_update();
4338
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004339#ifndef HAVE_GTK2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 /* this will cause the proper resizement to happen too */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004341 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004343#else /* HAVE_GTK2 */
4344 /* this will cause the proper resizement to happen too */
4345 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004346 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004349 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 * main window instead. */
4351 width += get_menu_tool_width();
4352 height += get_menu_tool_height();
4353
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004354 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004355 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004356 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004357 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358
4359#if 0
4360 if (!resize_idle_installed)
4361 {
4362 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4363 &force_shell_resize_idle, NULL, NULL);
4364 resize_idle_installed = TRUE;
4365 }
4366#endif
4367 /*
4368 * Wait until all events are processed to prevent a crash because the
4369 * real size of the drawing area doesn't reflect Vim's internal ideas.
4370 *
4371 * This is a bit of a hack, since Vim is a terminal application with a GUI
4372 * on top, while the GUI expects to be the boss.
4373 */
4374 gui_mch_update();
4375#endif
4376}
4377
4378
4379/*
4380 * The screen size is used to make sure the initial window doesn't get bigger
4381 * than the screen. This subtracts some room for menubar, toolbar and window
4382 * decorations.
4383 */
4384 void
4385gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4386{
4387#ifdef HAVE_GTK_MULTIHEAD
4388 GdkScreen* screen;
4389
4390 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4391 screen = gtk_widget_get_screen(gui.mainwin);
4392 else
4393 screen = gdk_screen_get_default();
4394
4395 *screen_w = gdk_screen_get_width(screen);
4396 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4397#else
4398 *screen_w = gdk_screen_width();
4399 /* Subtract 'guiheadroom' from the height to allow some room for the
4400 * window manager (task list and window title bar). */
4401 *screen_h = gdk_screen_height() - p_ghr;
4402#endif
4403
4404 /*
4405 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4406 * the toolbar and menubar for GTK, we subtract them from the screen
4407 * hight, so that the window size can be made to fit on the screen.
4408 * This should be completely changed later.
4409 */
4410 *screen_w -= get_menu_tool_width();
4411 *screen_h -= get_menu_tool_height();
4412}
4413
4414#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004416gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
4418# ifdef HAVE_GTK2
4419 if (title != NULL && output_conv.vc_type != CONV_NONE)
4420 title = string_convert(&output_conv, title, NULL);
4421# endif
4422
4423 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4424
4425# ifdef HAVE_GTK2
4426 if (output_conv.vc_type != CONV_NONE)
4427 vim_free(title);
4428# endif
4429}
4430#endif /* FEAT_TITLE */
4431
4432#if defined(FEAT_MENU) || defined(PROTO)
4433 void
4434gui_mch_enable_menu(int showit)
4435{
4436 GtkWidget *widget;
4437
4438# ifdef FEAT_GUI_GNOME
4439 if (using_gnome)
4440 widget = gui.menubar_h;
4441 else
4442# endif
4443 widget = gui.menubar;
4444
Bram Moolenaar18144c82006-04-12 21:52:12 +00004445 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4446 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 if (showit)
4449 gtk_widget_show(widget);
4450 else
4451 gtk_widget_hide(widget);
4452
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004453 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455}
4456#endif /* FEAT_MENU */
4457
4458#if defined(FEAT_TOOLBAR) || defined(PROTO)
4459 void
4460gui_mch_show_toolbar(int showit)
4461{
4462 GtkWidget *widget;
4463
4464 if (gui.toolbar == NULL)
4465 return;
4466
4467# ifdef FEAT_GUI_GNOME
4468 if (using_gnome)
4469 widget = gui.toolbar_h;
4470 else
4471# endif
4472 widget = gui.toolbar;
4473
4474 if (showit)
4475 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4476
4477 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4478 {
4479 if (showit)
4480 gtk_widget_show(widget);
4481 else
4482 gtk_widget_hide(widget);
4483
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004484 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486}
4487#endif /* FEAT_TOOLBAR */
4488
4489#ifndef HAVE_GTK2
4490/*
4491 * Get a font structure for highlighting.
4492 * "cbdata" is a pointer to the global gui structure.
4493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 static void
4495font_sel_ok(GtkWidget *wgt, gpointer cbdata)
4496{
4497 gui_T *vw = (gui_T *)cbdata;
4498 GtkFontSelectionDialog *fs = (GtkFontSelectionDialog *)vw->fontdlg;
4499
4500 if (vw->fontname)
4501 g_free(vw->fontname);
4502
4503 vw->fontname = (char_u *)gtk_font_selection_dialog_get_font_name(fs);
4504 gtk_widget_hide(vw->fontdlg);
4505 if (gtk_main_level() > 0)
4506 gtk_main_quit();
4507}
4508
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 static void
4510font_sel_cancel(GtkWidget *wgt, gpointer cbdata)
4511{
4512 gui_T *vw = (gui_T *)cbdata;
4513
4514 gtk_widget_hide(vw->fontdlg);
4515 if (gtk_main_level() > 0)
4516 gtk_main_quit();
4517}
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 static void
4520font_sel_destroy(GtkWidget *wgt, gpointer cbdata)
4521{
4522 gui_T *vw = (gui_T *)cbdata;
4523
4524 vw->fontdlg = NULL;
4525 if (gtk_main_level() > 0)
4526 gtk_main_quit();
4527}
4528#endif /* !HAVE_GTK2 */
4529
4530#ifdef HAVE_GTK2
4531/*
4532 * Check if a given font is a CJK font. This is done in a very crude manner. It
4533 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4534 * font. Consequently, this function cannot be used as a general purpose check
4535 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4536 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4537 */
4538 static int
4539is_cjk_font(PangoFontDescription *font_desc)
4540{
4541 static const char * const cjk_langs[] =
4542 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4543
4544 PangoFont *font;
4545 unsigned i;
4546 int is_cjk = FALSE;
4547
4548 font = pango_context_load_font(gui.text_context, font_desc);
4549
4550 if (font == NULL)
4551 return FALSE;
4552
4553 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4554 {
4555 PangoCoverage *coverage;
4556 gunichar uc;
4557
4558 coverage = pango_font_get_coverage(
4559 font, pango_language_from_string(cjk_langs[i]));
4560
4561 if (coverage != NULL)
4562 {
4563 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4564 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4565 pango_coverage_unref(coverage);
4566 }
4567 }
4568
4569 g_object_unref(font);
4570
4571 return is_cjk;
4572}
4573#endif /* HAVE_GTK2 */
4574
Bram Moolenaar231334e2005-07-25 20:46:57 +00004575/*
4576 * Adjust gui.char_height (after 'linespace' was changed).
4577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004579gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580{
4581#ifdef HAVE_GTK2
4582 PangoFontMetrics *metrics;
4583 int ascent;
4584 int descent;
4585
4586 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4587 pango_context_get_language(gui.text_context));
4588 ascent = pango_font_metrics_get_ascent(metrics);
4589 descent = pango_font_metrics_get_descent(metrics);
4590
4591 pango_font_metrics_unref(metrics);
4592
4593 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004594 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004595 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4597
4598#else /* !HAVE_GTK2 */
4599
4600 gui.char_height = gui.current_font->ascent + gui.current_font->descent
Bram Moolenaar231334e2005-07-25 20:46:57 +00004601 + p_linespace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 gui.char_ascent = gui.current_font->ascent + p_linespace / 2;
4603
4604#endif /* !HAVE_GTK2 */
4605
4606 /* A not-positive value of char_height may crash Vim. Only happens
4607 * if 'linespace' is negative (which does make sense sometimes). */
4608 gui.char_ascent = MAX(gui.char_ascent, 0);
4609 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4610
4611 return OK;
4612}
4613
4614#if defined(FEAT_XFONTSET) || defined(PROTO)
4615/*
4616 * Try to load the requested fontset.
4617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 GuiFontset
4619gui_mch_get_fontset(char_u *name, int report_error, int fixed_width)
4620{
4621 GdkFont *font;
4622
4623 if (!gui.in_use || name == NULL)
4624 return NOFONT;
4625
4626 font = gdk_fontset_load((gchar *)name);
4627
4628 if (font == NULL)
4629 {
4630 if (report_error)
4631 EMSG2(_(e_fontset), name);
4632 return NOFONT;
4633 }
4634 /* TODO: check if the font is fixed width. */
4635
4636 /* reference this font as being in use */
4637 gdk_font_ref(font);
4638
4639 return (GuiFontset)font;
4640}
4641#endif /* FEAT_XFONTSET */
4642
4643#ifndef HAVE_GTK2
4644/*
4645 * Put up a font dialog and return the selected font name in allocated memory.
4646 * "oldval" is the previous value.
4647 * Return NULL when cancelled.
4648 */
4649 char_u *
4650gui_mch_font_dialog(char_u *oldval)
4651{
4652 char_u *fontname = NULL;
4653
4654 if (!gui.fontdlg)
4655 {
4656 GtkFontSelectionDialog *fsd = NULL;
4657
4658 gui.fontdlg = gtk_font_selection_dialog_new(_("Font Selection"));
4659 fsd = GTK_FONT_SELECTION_DIALOG(gui.fontdlg);
4660 gtk_window_set_modal(GTK_WINDOW(gui.fontdlg), TRUE);
4661 gtk_window_set_transient_for(GTK_WINDOW(gui.fontdlg),
4662 GTK_WINDOW(gui.mainwin));
4663 gtk_signal_connect(GTK_OBJECT(gui.fontdlg), "destroy",
4664 GTK_SIGNAL_FUNC(font_sel_destroy), &gui);
4665 gtk_signal_connect(GTK_OBJECT(fsd->ok_button), "clicked",
4666 GTK_SIGNAL_FUNC(font_sel_ok), &gui);
4667 gtk_signal_connect(GTK_OBJECT(fsd->cancel_button), "clicked",
4668 GTK_SIGNAL_FUNC(font_sel_cancel), &gui);
4669 }
4670
4671 if (oldval != NULL && *oldval != NUL)
4672 gtk_font_selection_dialog_set_font_name(
4673 GTK_FONT_SELECTION_DIALOG(gui.fontdlg), (char *)oldval);
4674
4675 if (gui.fontname)
4676 {
4677 g_free(gui.fontname);
4678 gui.fontname = NULL;
4679 }
4680 gtk_window_position(GTK_WINDOW(gui.fontdlg), GTK_WIN_POS_MOUSE);
4681 gtk_widget_show(gui.fontdlg);
4682 {
4683 static gchar *spacings[] = {"c", "m", NULL};
4684
4685 /* In GTK 1.2.3 this must be after the gtk_widget_show() call,
4686 * otherwise everything is blocked for ten seconds. */
4687 gtk_font_selection_dialog_set_filter(
4688 GTK_FONT_SELECTION_DIALOG(gui.fontdlg),
4689 GTK_FONT_FILTER_BASE,
4690 GTK_FONT_ALL, NULL, NULL,
4691 NULL, NULL, spacings, NULL);
4692 }
4693
4694 /* Wait for the font dialog to be closed. */
4695 while (gui.fontdlg && GTK_WIDGET_DRAWABLE(gui.fontdlg))
4696 gtk_main_iteration_do(TRUE);
4697
4698 if (gui.fontname != NULL)
4699 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004700 /* Apparently some font names include a comma, need to escape that,
4701 * because in 'guifont' it separates names. */
4702 fontname = vim_strsave_escaped(gui.fontname, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 g_free(gui.fontname);
4704 gui.fontname = NULL;
4705 }
4706 return fontname;
4707}
4708#endif /* !HAVE_GTK2 */
4709
4710#ifdef HAVE_GTK2
4711/*
4712 * Put up a font dialog and return the selected font name in allocated memory.
4713 * "oldval" is the previous value. Return NULL when cancelled.
4714 * This should probably go into gui_gtk.c. Hmm.
4715 * FIXME:
4716 * The GTK2 font selection dialog has no filtering API. So we could either
4717 * a) implement our own (possibly copying the code from somewhere else) or
4718 * b) just live with it.
4719 */
4720 char_u *
4721gui_mch_font_dialog(char_u *oldval)
4722{
4723 GtkWidget *dialog;
4724 int response;
4725 char_u *fontname = NULL;
4726 char_u *oldname;
4727
4728 dialog = gtk_font_selection_dialog_new(NULL);
4729
4730 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4731 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4732
4733 if (oldval != NULL && oldval[0] != NUL)
4734 {
4735 if (output_conv.vc_type != CONV_NONE)
4736 oldname = string_convert(&output_conv, oldval, NULL);
4737 else
4738 oldname = oldval;
4739
4740 /* Annoying bug in GTK (or Pango): if the font name does not include a
4741 * size, zero is used. Use default point size ten. */
4742 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4743 {
4744 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4745
4746 if (p != NULL)
4747 {
4748 STRCPY(p + STRLEN(p), " 10");
4749 if (oldname != oldval)
4750 vim_free(oldname);
4751 oldname = p;
4752 }
4753 }
4754
4755 gtk_font_selection_dialog_set_font_name(
4756 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4757
4758 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004759 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761
4762 response = gtk_dialog_run(GTK_DIALOG(dialog));
4763
4764 if (response == GTK_RESPONSE_OK)
4765 {
4766 char *name;
4767
4768 name = gtk_font_selection_dialog_get_font_name(
4769 GTK_FONT_SELECTION_DIALOG(dialog));
4770 if (name != NULL)
4771 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004772 char_u *p;
4773
4774 /* Apparently some font names include a comma, need to escape
4775 * that, because in 'guifont' it separates names. */
4776 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004778 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004779 {
4780 fontname = string_convert(&input_conv, p, NULL);
4781 vim_free(p);
4782 }
4783 else
4784 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 }
4786 }
4787
4788 if (response != GTK_RESPONSE_NONE)
4789 gtk_widget_destroy(dialog);
4790
4791 return fontname;
4792}
4793
4794/*
4795 * Some monospace fonts don't support a bold weight, and fall back
4796 * silently to the regular weight. But this is no good since our text
4797 * drawing function can emulate bold by overstriking. So let's try
4798 * to detect whether bold weight is actually available and emulate it
4799 * otherwise.
4800 *
4801 * Note that we don't need to check for italic style since Xft can
4802 * emulate italic on its own, provided you have a proper fontconfig
4803 * setup. We wouldn't be able to emulate it in Vim anyway.
4804 */
4805 static void
4806get_styled_font_variants(void)
4807{
4808 PangoFontDescription *bold_font_desc;
4809 PangoFont *plain_font;
4810 PangoFont *bold_font;
4811
4812 gui.font_can_bold = FALSE;
4813
4814 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4815
4816 if (plain_font == NULL)
4817 return;
4818
4819 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4820 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4821
4822 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4823 /*
4824 * The comparison relies on the unique handle nature of a PangoFont*,
4825 * i.e. it's assumed that a different PangoFont* won't refer to the
4826 * same font. Seems to work, and failing here isn't critical anyway.
4827 */
4828 if (bold_font != NULL)
4829 {
4830 gui.font_can_bold = (bold_font != plain_font);
4831 g_object_unref(bold_font);
4832 }
4833
4834 pango_font_description_free(bold_font_desc);
4835 g_object_unref(plain_font);
4836}
4837
4838#else /* !HAVE_GTK2 */
4839
4840/*
4841 * There is only one excuse I can give for the following attempt to manage font
4842 * styles:
4843 *
4844 * I HATE THE BRAIN DEAD WAY X11 IS HANDLING FONTS (--mdcki)
4845 * (Me too. --danielk)
4846 */
4847 static void
4848get_styled_font_variants(char_u * font_name)
4849{
4850 char *chunk[32];
4851 char *sdup;
4852 char *tmp;
4853 int len, i;
4854 GuiFont *styled_font[3];
4855
4856 styled_font[0] = &gui.bold_font;
4857 styled_font[1] = &gui.ital_font;
4858 styled_font[2] = &gui.boldital_font;
4859
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004860 /* First free whatever was previously there. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 for (i = 0; i < 3; ++i)
4862 if (*styled_font[i])
4863 {
4864 gdk_font_unref(*styled_font[i]);
4865 *styled_font[i] = NULL;
4866 }
4867
4868 if ((sdup = g_strdup((const char *)font_name)) == NULL)
4869 return;
4870
4871 /* split up the whole */
4872 i = 0;
4873 for (tmp = sdup; *tmp != '\0'; ++tmp)
4874 {
4875 if (*tmp == '-')
4876 {
4877 *tmp = '\0';
4878
4879 if (i == 32)
4880 break;
4881
4882 chunk[i] = tmp + 1;
4883 ++i;
4884 }
4885 }
4886
4887 if (i == 14)
4888 {
4889 GdkFont *font = NULL;
4890 const char *bold_chunk[3] = { "bold", NULL, "bold" };
4891 const char *italic_chunk[3] = { NULL, "o", "o" };
4892
4893 /* font name was complete */
4894 len = strlen((const char *)font_name) + 32;
4895
4896 for (i = 0; i < 3; ++i)
4897 {
4898 char *styled_name;
4899 int j;
4900
4901 styled_name = (char *)alloc(len);
4902 if (styled_name == NULL)
4903 {
4904 g_free(sdup);
4905 return;
4906 }
4907
4908 *styled_name = '\0';
4909
4910 for (j = 0; j < 14; ++j)
4911 {
4912 strcat(styled_name, "-");
4913 if (j == 2 && bold_chunk[i] != NULL)
4914 strcat(styled_name, bold_chunk[i]);
4915 else if (j == 3 && italic_chunk[i] != NULL)
4916 strcat(styled_name, italic_chunk[i]);
4917 else
4918 strcat(styled_name, chunk[j]);
4919 }
4920
4921 font = gui_mch_get_font((char_u *)styled_name, FALSE);
4922 if (font != NULL)
4923 *styled_font[i] = font;
4924
4925 vim_free(styled_name);
4926 }
4927 }
4928
4929 g_free(sdup);
4930}
4931#endif /* !HAVE_GTK2 */
4932
4933#ifdef HAVE_GTK2
4934static PangoEngineShape *default_shape_engine = NULL;
4935
4936/*
4937 * Create a map from ASCII characters in the range [32,126] to glyphs
4938 * of the current font. This is used by gui_gtk2_draw_string() to skip
4939 * the itemize and shaping process for the most common case.
4940 */
4941 static void
4942ascii_glyph_table_init(void)
4943{
4944 char_u ascii_chars[128];
4945 PangoAttrList *attr_list;
4946 GList *item_list;
4947 int i;
4948
4949 if (gui.ascii_glyphs != NULL)
4950 pango_glyph_string_free(gui.ascii_glyphs);
4951 if (gui.ascii_font != NULL)
4952 g_object_unref(gui.ascii_font);
4953
4954 gui.ascii_glyphs = NULL;
4955 gui.ascii_font = NULL;
4956
4957 /* For safety, fill in question marks for the control characters. */
4958 for (i = 0; i < 32; ++i)
4959 ascii_chars[i] = '?';
4960 for (; i < 127; ++i)
4961 ascii_chars[i] = i;
4962 ascii_chars[i] = '?';
4963
4964 attr_list = pango_attr_list_new();
4965 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4966 0, sizeof(ascii_chars), attr_list, NULL);
4967
4968 if (item_list != NULL && item_list->next == NULL) /* play safe */
4969 {
4970 PangoItem *item;
4971 int width;
4972
4973 item = (PangoItem *)item_list->data;
4974 width = gui.char_width * PANGO_SCALE;
4975
4976 /* Remember the shape engine used for ASCII. */
4977 default_shape_engine = item->analysis.shape_engine;
4978
4979 gui.ascii_font = item->analysis.font;
4980 g_object_ref(gui.ascii_font);
4981
4982 gui.ascii_glyphs = pango_glyph_string_new();
4983
4984 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4985 &item->analysis, gui.ascii_glyphs);
4986
4987 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4988
4989 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4990 {
4991 PangoGlyphGeometry *geom;
4992
4993 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4994 geom->x_offset += MAX(0, width - geom->width) / 2;
4995 geom->width = width;
4996 }
4997 }
4998
4999 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
5000 g_list_free(item_list);
5001 pango_attr_list_unref(attr_list);
5002}
5003#endif /* HAVE_GTK2 */
5004
5005/*
5006 * Initialize Vim to use the font or fontset with the given name.
5007 * Return FAIL if the font could not be loaded, OK otherwise.
5008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005010gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012#ifdef HAVE_GTK2
5013 PangoFontDescription *font_desc;
5014 PangoLayout *layout;
5015 int width;
5016
5017 /* If font_name is NULL, this means to use the default, which should
5018 * be present on all proper Pango/fontconfig installations. */
5019 if (font_name == NULL)
5020 font_name = (char_u *)DEFAULT_FONT;
5021
5022 font_desc = gui_mch_get_font(font_name, FALSE);
5023
5024 if (font_desc == NULL)
5025 return FAIL;
5026
5027 gui_mch_free_font(gui.norm_font);
5028 gui.norm_font = font_desc;
5029
5030 pango_context_set_font_description(gui.text_context, font_desc);
5031
5032 layout = pango_layout_new(gui.text_context);
5033 pango_layout_set_text(layout, "MW", 2);
5034 pango_layout_get_size(layout, &width, NULL);
5035 /*
5036 * Set char_width to half the width obtained from pango_layout_get_size()
5037 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5038 * Pango to use the same width for both non-CJK characters (e.g. Latin
5039 * letters and numbers) and CJK characters. This results in 's p a c e d
5040 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5041 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5042 * width for CJK fonts.
5043 *
5044 * For related bugs, see:
5045 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5046 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5047 *
5048 * With this, for all four of the following cases, Vim works fine:
5049 * guifont=CJK_fixed_width_font
5050 * guifont=Non_CJK_fixed_font
5051 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5052 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5053 */
5054 if (is_cjk_font(gui.norm_font))
5055 {
5056 int cjk_width;
5057
5058 /* Measure the text extent of U+4E00 and U+4E8C */
5059 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5060 pango_layout_get_size(layout, &cjk_width, NULL);
5061
5062 if (width == cjk_width) /* Xft not patched */
5063 width /= 2;
5064 }
5065 g_object_unref(layout);
5066
5067 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5068
5069 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5070 if (gui.char_width <= 0)
5071 gui.char_width = 8;
5072
Bram Moolenaar231334e2005-07-25 20:46:57 +00005073 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
5075 /* Set the fontname, which will be used for information purposes */
5076 hl_set_font_name(font_name);
5077
5078 get_styled_font_variants();
5079 ascii_glyph_table_init();
5080
5081 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5082 if (gui.wide_font != NULL
5083 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5084 {
5085 pango_font_description_free(gui.wide_font);
5086 gui.wide_font = NULL;
5087 }
5088
5089#else /* !HAVE_GTK2 */
5090
5091 GdkFont *font = NULL;
5092
5093# ifdef FEAT_XFONTSET
5094 /* Try loading a fontset. If this fails we try loading a normal font. */
5095 if (fontset && font_name != NULL)
5096 font = gui_mch_get_fontset(font_name, TRUE, TRUE);
5097
5098 if (font == NULL)
5099# endif
5100 {
5101 /* If font_name is NULL, this means to use the default, which should
5102 * be present on all X11 servers. */
5103 if (font_name == NULL)
5104 font_name = (char_u *)DEFAULT_FONT;
5105 font = gui_mch_get_font(font_name, FALSE);
5106 }
5107
5108 if (font == NULL)
5109 return FAIL;
5110
5111 gui_mch_free_font(gui.norm_font);
5112# ifdef FEAT_XFONTSET
5113 gui_mch_free_fontset(gui.fontset);
5114 if (font->type == GDK_FONT_FONTSET)
5115 {
5116 gui.norm_font = NOFONT;
5117 gui.fontset = (GuiFontset)font;
5118 /* Use two bytes, this works around the problem that the result would
5119 * be zero if no 8-bit font was found. */
5120 gui.char_width = gdk_string_width(font, "xW") / 2;
5121 }
5122 else
5123# endif
5124 {
5125 gui.norm_font = font;
5126# ifdef FEAT_XFONTSET
5127 gui.fontset = NOFONTSET;
5128# endif
5129 gui.char_width = ((XFontStruct *)
5130 GDK_FONT_XFONT(font))->max_bounds.width;
5131 }
5132
5133 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5134 if (gui.char_width <= 0)
5135 gui.char_width = 8;
5136
5137 gui.char_height = font->ascent + font->descent + p_linespace;
5138 gui.char_ascent = font->ascent + p_linespace / 2;
5139
5140 /* A not-positive value of char_height may crash Vim. Only happens
5141 * if 'linespace' is negative (which does make sense sometimes). */
5142 gui.char_ascent = MAX(gui.char_ascent, 0);
5143 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5144
5145 /* Set the fontname, which will be used for information purposes */
5146 hl_set_font_name(font_name);
5147
5148 if (font->type != GDK_FONT_FONTSET)
5149 get_styled_font_variants(font_name);
5150
5151 /* Synchronize the fonts used in user input dialogs, since otherwise
5152 * search/replace will be esp. annoying in case of international font
5153 * usage.
5154 */
5155 gui_gtk_synch_fonts();
5156
5157# ifdef FEAT_XIM
5158 /* Adjust input management behaviour to the capabilities of the new
5159 * fontset */
5160 xim_decide_input_style();
5161 if (xim_get_status_area_height())
5162 {
5163 /* Status area is required. Just create the empty container so that
5164 * mainwin will allocate the extra space for status area. */
5165 GtkWidget *alignment = gtk_alignment_new((gfloat)0.5, (gfloat)0.5,
5166 (gfloat)1.0, (gfloat)1.0);
5167
5168 gtk_widget_set_usize(alignment, 20, gui.char_height + 2);
5169 gtk_box_pack_end(GTK_BOX(GTK_BIN(gui.mainwin)->child),
5170 alignment, FALSE, FALSE, 0);
5171 gtk_widget_show(alignment);
5172 }
5173# endif
5174#endif /* !HAVE_GTK2 */
5175
5176 /* Preserve the logical dimensions of the screen. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005177 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
5179 return OK;
5180}
5181
5182/*
5183 * Get a reference to the font "name".
5184 * Return zero for failure.
5185 */
5186 GuiFont
5187gui_mch_get_font(char_u *name, int report_error)
5188{
5189#ifdef HAVE_GTK2
5190 PangoFontDescription *font;
5191#else
5192 GdkFont *font;
5193#endif
5194
5195 /* can't do this when GUI is not running */
5196 if (!gui.in_use || name == NULL)
5197 return NULL;
5198
5199#ifdef HAVE_GTK2
5200 if (output_conv.vc_type != CONV_NONE)
5201 {
5202 char_u *buf;
5203
5204 buf = string_convert(&output_conv, name, NULL);
5205 if (buf != NULL)
5206 {
5207 font = pango_font_description_from_string((const char *)buf);
5208 vim_free(buf);
5209 }
5210 else
5211 font = NULL;
5212 }
5213 else
5214 font = pango_font_description_from_string((const char *)name);
5215
5216 if (font != NULL)
5217 {
5218 PangoFont *real_font;
5219
5220 /* pango_context_load_font() bails out if no font size is set */
5221 if (pango_font_description_get_size(font) <= 0)
5222 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5223
5224 real_font = pango_context_load_font(gui.text_context, font);
5225
5226 if (real_font == NULL)
5227 {
5228 pango_font_description_free(font);
5229 font = NULL;
5230 }
5231 else
5232 g_object_unref(real_font);
5233 }
5234#else
5235 font = gdk_font_load((const gchar *)name);
5236#endif
5237
5238 if (font == NULL)
5239 {
5240 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005241 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 return NULL;
5243 }
5244
5245#ifdef HAVE_GTK2
5246 /*
5247 * The fixed-width check has been disabled for GTK+ 2. Rationale:
5248 *
5249 * - The check tends to report false positives, particularly
5250 * in non-Latin locales or with old X fonts.
5251 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
5252 * GTK+ 2 Vim is actually capable of displaying variable width
5253 * fonts. Those will just be spaced out like in AA xterm.
5254 * - Failing here for the default font causes GUI startup to fail
5255 * even with wiped out configuration files.
5256 * - The font dialog displays all fonts unfiltered, and it's rather
5257 * annoying if 95% of the listed fonts produce an error message.
5258 */
5259# if 0
5260 {
5261 /* Check that this is a mono-spaced font. Naturally, this is a bit
5262 * hackish -- fixed-width isn't really suitable for i18n text :/ */
5263 PangoLayout *layout;
5264 unsigned int i;
5265 int last_width = -1;
5266 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
5267
5268 layout = pango_layout_new(gui.text_context);
5269 pango_layout_set_font_description(layout, font);
5270
5271 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
5272 {
5273 int width;
5274
5275 pango_layout_set_text(layout, &test_chars[i], 1);
5276 pango_layout_get_size(layout, &width, NULL);
5277
5278 if (last_width >= 0 && width != last_width)
5279 {
5280 pango_font_description_free(font);
5281 font = NULL;
5282 break;
5283 }
5284
5285 last_width = width;
5286 }
5287
5288 g_object_unref(layout);
5289 }
5290# endif
5291#else /* !HAVE_GTK2 */
5292 {
5293 XFontStruct *xfont;
5294
5295 /* reference this font as being in use */
5296 gdk_font_ref(font);
5297
5298 /* Check that this is a mono-spaced font.
5299 */
5300 xfont = (XFontStruct *) GDK_FONT_XFONT(font);
5301
5302 if (xfont->max_bounds.width != xfont->min_bounds.width)
5303 {
5304 gdk_font_unref(font);
5305 font = NULL;
5306 }
5307 }
5308#endif /* !HAVE_GTK2 */
5309
5310#if !defined(HAVE_GTK2) || 0 /* disabled for GTK+ 2, see above */
5311 if (font == NULL && report_error)
5312 EMSG2(_(e_fontwidth), name);
5313#endif
5314
5315 return font;
5316}
5317
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005318#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005319/*
5320 * Return the name of font "font" in allocated memory.
5321 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005322 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005323gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005324{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005325# ifdef HAVE_GTK2
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005326 if (font != NOFONT)
5327 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005328 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005329
Bram Moolenaar89d40322006-08-29 15:30:07 +00005330 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005331 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005332 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005333
Bram Moolenaar89d40322006-08-29 15:30:07 +00005334 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005335 return s;
5336 }
5337 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005338# else
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005339 /* Don't know how to get the name, return what we got. */
5340 if (name != NULL)
5341 return vim_strsave(name);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005342# endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005343 return NULL;
5344}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005345#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005346
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347#if !defined(HAVE_GTK2) || defined(PROTO)
5348/*
5349 * Set the current text font.
5350 * Since we create all GC on demand, we use just gui.current_font to
5351 * indicate the desired current font.
5352 */
5353 void
5354gui_mch_set_font(GuiFont font)
5355{
5356 gui.current_font = font;
5357}
5358#endif
5359
5360#if defined(FEAT_XFONTSET) || defined(PROTO)
5361/*
5362 * Set the current text fontset.
5363 */
5364 void
5365gui_mch_set_fontset(GuiFontset fontset)
5366{
5367 gui.current_font = fontset;
5368}
5369#endif
5370
5371/*
5372 * If a font is not going to be used, free its structure.
5373 */
5374 void
5375gui_mch_free_font(GuiFont font)
5376{
5377 if (font != NOFONT)
5378#ifdef HAVE_GTK2
5379 pango_font_description_free(font);
5380#else
5381 gdk_font_unref(font);
5382#endif
5383}
5384
5385#if defined(FEAT_XFONTSET) || defined(PROTO)
5386/*
5387 * If a fontset is not going to be used, free its structure.
5388 */
5389 void
5390gui_mch_free_fontset(GuiFontset fontset)
5391{
5392 if (fontset != NOFONTSET)
5393 gdk_font_unref(fontset);
5394}
5395#endif
5396
5397
5398/*
5399 * Return the Pixel value (color) for the given color name. This routine was
5400 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5401 * Programmer's Guide.
5402 * Return INVALCOLOR for error.
5403 */
5404 guicolor_T
5405gui_mch_get_color(char_u *name)
5406{
5407 /* A number of colors that some X11 systems don't have */
5408 static const char *const vimnames[][2] =
5409 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00005410 {"LightRed", "#FFBBBB"},
5411 {"LightGreen", "#88FF88"},
5412 {"LightMagenta","#FFBBFF"},
5413 {"DarkCyan", "#008888"},
5414 {"DarkBlue", "#0000BB"},
5415 {"DarkRed", "#BB0000"},
5416 {"DarkMagenta", "#BB00BB"},
5417 {"DarkGrey", "#BBBBBB"},
5418 {"DarkYellow", "#BBBB00"},
5419 {"Gray10", "#1A1A1A"},
5420 {"Grey10", "#1A1A1A"},
5421 {"Gray20", "#333333"},
5422 {"Grey20", "#333333"},
5423 {"Gray30", "#4D4D4D"},
5424 {"Grey30", "#4D4D4D"},
5425 {"Gray40", "#666666"},
5426 {"Grey40", "#666666"},
5427 {"Gray50", "#7F7F7F"},
5428 {"Grey50", "#7F7F7F"},
5429 {"Gray60", "#999999"},
5430 {"Grey60", "#999999"},
5431 {"Gray70", "#B3B3B3"},
5432 {"Grey70", "#B3B3B3"},
5433 {"Gray80", "#CCCCCC"},
5434 {"Grey80", "#CCCCCC"},
5435 {"Gray90", "#E5E5E5"},
5436 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {NULL, NULL}
5438 };
5439
5440 if (!gui.in_use) /* can't do this when GUI not running */
5441 return INVALCOLOR;
5442
5443 while (name != NULL)
5444 {
5445 GdkColor color;
5446 int parsed;
5447 int i;
5448
5449 parsed = gdk_color_parse((const char *)name, &color);
5450
5451#ifndef HAVE_GTK2 /* ohh, lovely GTK+ 2, eases our pain :) */
5452 /*
5453 * Since we have already called gtk_set_locale here the bugger
5454 * XParseColor will accept only explicit color names in the language
Bram Moolenaar49325942007-05-10 19:19:59 +00005455 * of the current locale. However this will interfere with:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * 1. Vim's global startup files
5457 * 2. Explicit color names in .vimrc
5458 *
5459 * Therefore we first try to parse the color in the current locale and
5460 * if it fails, we fall back to the portable "C" one.
5461 */
5462 if (!parsed)
5463 {
5464 char *current;
5465
5466 current = setlocale(LC_ALL, NULL);
5467 if (current != NULL)
5468 {
5469 char *saved;
5470
5471 saved = g_strdup(current);
5472 setlocale(LC_ALL, "C");
5473
5474 parsed = gdk_color_parse((const gchar *)name, &color);
5475
5476 setlocale(LC_ALL, saved);
5477 gtk_set_locale();
5478
5479 g_free(saved);
5480 }
5481 }
5482#endif /* !HAVE_GTK2 */
5483
5484 if (parsed)
5485 {
5486#ifdef HAVE_GTK2
5487 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5488 &color, FALSE, TRUE);
5489#else
5490 gdk_color_alloc(gtk_widget_get_colormap(gui.drawarea), &color);
5491#endif
5492 return (guicolor_T)color.pixel;
5493 }
5494 /* add a few builtin names and try again */
5495 for (i = 0; ; ++i)
5496 {
5497 if (vimnames[i][0] == NULL)
5498 {
5499 name = NULL;
5500 break;
5501 }
5502 if (STRICMP(name, vimnames[i][0]) == 0)
5503 {
5504 name = (char_u *)vimnames[i][1];
5505 break;
5506 }
5507 }
5508 }
5509
5510 return INVALCOLOR;
5511}
5512
5513/*
5514 * Set the current text foreground color.
5515 */
5516 void
5517gui_mch_set_fg_color(guicolor_T color)
5518{
5519 gui.fgcolor->pixel = (unsigned long)color;
5520}
5521
5522/*
5523 * Set the current text background color.
5524 */
5525 void
5526gui_mch_set_bg_color(guicolor_T color)
5527{
5528 gui.bgcolor->pixel = (unsigned long)color;
5529}
5530
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005531/*
5532 * Set the current text special color.
5533 */
5534 void
5535gui_mch_set_sp_color(guicolor_T color)
5536{
5537 gui.spcolor->pixel = (unsigned long)color;
5538}
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#ifdef HAVE_GTK2
5541/*
5542 * Function-like convenience macro for the sake of efficiency.
5543 */
5544#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5545 G_STMT_START{ \
5546 PangoAttribute *tmp_attr_; \
5547 tmp_attr_ = (Attribute); \
5548 tmp_attr_->start_index = (Start); \
5549 tmp_attr_->end_index = (End); \
5550 pango_attr_list_insert((AttrList), tmp_attr_); \
5551 }G_STMT_END
5552
5553 static void
5554apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5555{
5556 char_u *start = NULL;
5557 char_u *p;
5558 int uc;
5559
5560 for (p = s; p < s + len; p += utf_byte2len(*p))
5561 {
5562 uc = utf_ptr2char(p);
5563
5564 if (start == NULL)
5565 {
5566 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5567 start = p;
5568 }
5569 else if (uc < 0x80 /* optimization shortcut */
5570 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5571 {
5572 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5573 attr_list, start - s, p - s);
5574 start = NULL;
5575 }
5576 }
5577
5578 if (start != NULL)
5579 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5580 attr_list, start - s, len);
5581}
5582
5583 static int
5584count_cluster_cells(char_u *s, PangoItem *item,
5585 PangoGlyphString* glyphs, int i,
5586 int *cluster_width,
5587 int *last_glyph_rbearing)
5588{
5589 char_u *p;
5590 int next; /* glyph start index of next cluster */
5591 int start, end; /* string segment of current cluster */
5592 int width; /* real cluster width in Pango units */
5593 int uc;
5594 int cellcount = 0;
5595
5596 width = glyphs->glyphs[i].geometry.width;
5597
5598 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5599 {
5600 if (glyphs->glyphs[next].attr.is_cluster_start)
5601 break;
5602 else if (glyphs->glyphs[next].geometry.width > width)
5603 width = glyphs->glyphs[next].geometry.width;
5604 }
5605
5606 start = item->offset + glyphs->log_clusters[i];
5607 end = item->offset + ((next < glyphs->num_glyphs) ?
5608 glyphs->log_clusters[next] : item->length);
5609
5610 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5611 {
5612 uc = utf_ptr2char(p);
5613 if (uc < 0x80)
5614 ++cellcount;
5615 else if (!utf_iscomposing(uc))
5616 cellcount += utf_char2cells(uc);
5617 }
5618
5619 if (last_glyph_rbearing != NULL
5620 && cellcount > 0 && next == glyphs->num_glyphs)
5621 {
5622 PangoRectangle ink_rect;
5623 /*
5624 * If a certain combining mark had to be taken from a non-monospace
5625 * font, we have to compensate manually by adapting x_offset according
5626 * to the ink extents of the previous glyph.
5627 */
5628 pango_font_get_glyph_extents(item->analysis.font,
5629 glyphs->glyphs[i].glyph,
5630 &ink_rect, NULL);
5631
5632 if (PANGO_RBEARING(ink_rect) > 0)
5633 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5634 }
5635
5636 if (cellcount > 0)
5637 *cluster_width = width;
5638
5639 return cellcount;
5640}
5641
5642/*
5643 * If there are only combining characters in the cluster, we cannot just
5644 * change the width of the previous glyph since there is none. Therefore
5645 * some guesswork is needed.
5646 *
5647 * If ink_rect.x is negative Pango apparently has taken care of the composing
5648 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5649 * to problems with composing from different fonts we still need to fine-tune
5650 * x_offset to avoid uglyness.
5651 *
5652 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5653 * the position of the previous glyph. Apparently this happens only with old
5654 * X fonts which don't provide the special combining information needed by
5655 * Pango.
5656 */
5657 static void
5658setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5659 int last_cellcount, int last_cluster_width,
5660 int last_glyph_rbearing)
5661{
5662 PangoRectangle ink_rect;
5663 PangoRectangle logical_rect;
5664 int width;
5665
5666 width = last_cellcount * gui.char_width * PANGO_SCALE;
5667 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5668 glyph->geometry.width = 0;
5669
5670 pango_font_get_glyph_extents(item->analysis.font,
5671 glyph->glyph,
5672 &ink_rect, &logical_rect);
5673 if (ink_rect.x < 0)
5674 {
5675 glyph->geometry.x_offset += last_glyph_rbearing;
5676 glyph->geometry.y_offset = logical_rect.height
5677 - (gui.char_height - p_linespace) * PANGO_SCALE;
5678 }
5679}
5680
5681 static void
5682draw_glyph_string(int row, int col, int num_cells, int flags,
5683 PangoFont *font, PangoGlyphString *glyphs)
5684{
5685 if (!(flags & DRAW_TRANSP))
5686 {
5687 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5688
5689 gdk_draw_rectangle(gui.drawarea->window,
5690 gui.text_gc,
5691 TRUE,
5692 FILL_X(col),
5693 FILL_Y(row),
5694 num_cells * gui.char_width,
5695 gui.char_height);
5696 }
5697
5698 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5699
5700 gdk_draw_glyphs(gui.drawarea->window,
5701 gui.text_gc,
5702 font,
5703 TEXT_X(col),
5704 TEXT_Y(row),
5705 glyphs);
5706
5707 /* redraw the contents with an offset of 1 to emulate bold */
5708 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
5709 gdk_draw_glyphs(gui.drawarea->window,
5710 gui.text_gc,
5711 font,
5712 TEXT_X(col) + 1,
5713 TEXT_Y(row),
5714 glyphs);
5715}
5716
5717#endif /* HAVE_GTK2 */
5718
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005719/*
5720 * Draw underline and undercurl at the bottom of the character cell.
5721 */
5722 static void
5723draw_under(int flags, int row, int col, int cells)
5724{
5725 int i;
5726 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005727 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005728 int y = FILL_Y(row + 1) - 1;
5729
5730 /* Undercurl: draw curl at the bottom of the character cell. */
5731 if (flags & DRAW_UNDERC)
5732 {
5733 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5734 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5735 {
5736 offset = val[i % 8];
5737 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5738 }
5739 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5740 }
5741
5742 /* Underline: draw a line at the bottom of the character cell. */
5743 if (flags & DRAW_UNDERL)
5744 {
5745 /* When p_linespace is 0, overwrite the bottom row of pixels.
5746 * Otherwise put the line just below the character. */
5747 if (p_linespace > 1)
5748 y -= p_linespace - 1;
5749 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5750 FILL_X(col), y,
5751 FILL_X(col + cells) - 1, y);
5752 }
5753}
5754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#if defined(HAVE_GTK2) || defined(PROTO)
5756 int
5757gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5758{
5759 GdkRectangle area; /* area for clip mask */
5760 PangoGlyphString *glyphs; /* glyphs of current item */
5761 int column_offset = 0; /* column offset in cells */
5762 int i;
5763 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5764 char_u *new_conv_buf;
5765 int convlen;
5766 char_u *sp, *bp;
5767 int plen;
5768
5769 if (gui.text_context == NULL || gui.drawarea->window == NULL)
5770 return len;
5771
5772 if (output_conv.vc_type != CONV_NONE)
5773 {
5774 /*
5775 * Convert characters from 'encoding' to 'termencoding', which is set
5776 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5777 * prohibits changing this to something else than UTF-8 if the GUI is
5778 * in use.
5779 */
5780 convlen = len;
5781 conv_buf = string_convert(&output_conv, s, &convlen);
5782 g_return_val_if_fail(conv_buf != NULL, len);
5783
5784 /* Correct for differences in char width: some chars are
5785 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5786 * compensate for that. */
5787 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5788 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005789 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5791 {
5792 new_conv_buf = alloc(convlen + 2);
5793 if (new_conv_buf == NULL)
5794 return len;
5795 plen += bp - conv_buf;
5796 mch_memmove(new_conv_buf, conv_buf, plen);
5797 new_conv_buf[plen] = ' ';
5798 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5799 convlen - plen + 1);
5800 vim_free(conv_buf);
5801 conv_buf = new_conv_buf;
5802 ++convlen;
5803 bp = conv_buf + plen;
5804 plen = 1;
5805 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005806 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 bp += plen;
5808 }
5809 s = conv_buf;
5810 len = convlen;
5811 }
5812
5813 /*
5814 * Restrict all drawing to the current screen line in order to prevent
5815 * fuzzy font lookups from messing up the screen.
5816 */
5817 area.x = gui.border_offset;
5818 area.y = FILL_Y(row);
5819 area.width = gui.num_cols * gui.char_width;
5820 area.height = gui.char_height;
5821
5822 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5823 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
5824
5825 glyphs = pango_glyph_string_new();
5826
5827 /*
5828 * Optimization hack: If possible, skip the itemize and shaping process
5829 * for pure ASCII strings. This optimization is particularly effective
5830 * because Vim draws space characters to clear parts of the screen.
5831 */
5832 if (!(flags & DRAW_ITALIC)
5833 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5834 && gui.ascii_glyphs != NULL)
5835 {
5836 char_u *p;
5837
5838 for (p = s; p < s + len; ++p)
5839 if (*p & 0x80)
5840 goto not_ascii;
5841
5842 pango_glyph_string_set_size(glyphs, len);
5843
5844 for (i = 0; i < len; ++i)
5845 {
5846 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5847 glyphs->log_clusters[i] = i;
5848 }
5849
5850 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5851
5852 column_offset = len;
5853 }
5854 else
5855not_ascii:
5856 {
5857 PangoAttrList *attr_list;
5858 GList *item_list;
5859 int cluster_width;
5860 int last_glyph_rbearing;
5861 int cells = 0; /* cells occupied by current cluster */
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005862#if 0
5863 int monospace13 = STRICMP(p_guifont, "monospace 13") == 0;
5864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005866 /* Safety check: pango crashes when invoked with invalid utf-8
5867 * characters. */
5868 if (!utf_valid_string(s, s + len))
5869 {
5870 column_offset = len;
5871 goto skipitall;
5872 }
5873
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 /* original width of the current cluster */
5875 cluster_width = PANGO_SCALE * gui.char_width;
5876
5877 /* right bearing of the last non-composing glyph */
5878 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5879
5880 attr_list = pango_attr_list_new();
5881
5882 /* If 'guifontwide' is set then use that for double-width characters.
5883 * Otherwise just go with 'guifont' and let Pango do its thing. */
5884 if (gui.wide_font != NULL)
5885 apply_wide_font_attr(s, len, attr_list);
5886
5887 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5888 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5889 attr_list, 0, len);
5890 if (flags & DRAW_ITALIC)
5891 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5892 attr_list, 0, len);
5893 /*
5894 * Break the text into segments with consistent directional level
5895 * and shaping engine. Pure Latin text needs only a single segment,
5896 * so there's no need to worry about the loop's efficiency. Better
5897 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5898 */
5899 item_list = pango_itemize(gui.text_context,
5900 (const char *)s, 0, len, attr_list, NULL);
5901
5902 while (item_list != NULL)
5903 {
5904 PangoItem *item;
5905 int item_cells = 0; /* item length in cells */
5906
5907 item = (PangoItem *)item_list->data;
5908 item_list = g_list_delete_link(item_list, item_list);
5909 /*
5910 * Increment the bidirectional embedding level by 1 if it is not
5911 * even. An odd number means the output will be RTL, but we don't
5912 * want that since Vim handles right-to-left text on its own. It
5913 * would probably be sufficient to just set level = 0, but you can
5914 * never know :)
5915 *
5916 * Unfortunately we can't take advantage of Pango's ability to
5917 * render both LTR and RTL at the same time. In order to support
5918 * that, Vim's main screen engine would have to make use of Pango
5919 * functionality.
5920 */
5921 item->analysis.level = (item->analysis.level + 1) & (~1U);
5922
5923 /* HACK: Overrule the shape engine, we don't want shaping to be
5924 * done, because drawing the cursor would change the display. */
5925 item->analysis.shape_engine = default_shape_engine;
5926
5927 pango_shape((const char *)s + item->offset, item->length,
5928 &item->analysis, glyphs);
5929 /*
5930 * Fixed-width hack: iterate over the array and assign a fixed
5931 * width to each glyph, thus overriding the choice made by the
5932 * shaping engine. We use utf_char2cells() to determine the
5933 * number of cells needed.
5934 *
5935 * Also perform all kind of dark magic to get composing
5936 * characters right (and pretty too of course).
5937 */
5938 for (i = 0; i < glyphs->num_glyphs; ++i)
5939 {
5940 PangoGlyphInfo *glyph;
5941
5942 glyph = &glyphs->glyphs[i];
5943
5944 if (glyph->attr.is_cluster_start)
5945 {
5946 int cellcount;
5947
5948 cellcount = count_cluster_cells(
5949 s, item, glyphs, i, &cluster_width,
5950 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5951
5952 if (cellcount > 0)
5953 {
5954 int width;
5955
5956 width = cellcount * gui.char_width * PANGO_SCALE;
5957 glyph->geometry.x_offset +=
5958 MAX(0, width - cluster_width) / 2;
5959 glyph->geometry.width = width;
5960 }
5961 else
5962 {
5963 /* If there are only combining characters in the
5964 * cluster, we cannot just change the width of the
5965 * previous glyph since there is none. Therefore
5966 * some guesswork is needed. */
5967 setup_zero_width_cluster(item, glyph, cells,
5968 cluster_width,
5969 last_glyph_rbearing);
5970 }
5971
5972 item_cells += cellcount;
5973 cells = cellcount;
5974 }
5975 else if (i > 0)
5976 {
5977 int width;
5978
5979 /* There is a previous glyph, so we deal with combining
5980 * characters the canonical way. That is, setting the
5981 * width of the previous glyph to 0. */
5982 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 width = cells * gui.char_width * PANGO_SCALE;
5984 glyph->geometry.x_offset +=
5985 MAX(0, width - cluster_width) / 2;
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005986#if 0
5987 /* Dirty hack: for "monospace 13" font there is a bug that
5988 * draws composing chars in the wrong position. Add
5989 * "width" to the offset to work around that. */
5990 if (monospace13)
5991 glyph->geometry.x_offset = width;
5992#endif
5993
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 glyph->geometry.width = width;
5995 }
5996 else /* i == 0 "cannot happen" */
5997 {
5998 glyph->geometry.width = 0;
5999 }
6000 }
6001
6002 /*** Aaaaand action! ***/
6003 draw_glyph_string(row, col + column_offset, item_cells,
6004 flags, item->analysis.font, glyphs);
6005
6006 pango_item_free(item);
6007
6008 column_offset += item_cells;
6009 }
6010
6011 pango_attr_list_unref(attr_list);
6012 }
6013
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006014skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006015 /* Draw underline and undercurl. */
6016 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017
6018 pango_glyph_string_free(glyphs);
6019 vim_free(conv_buf);
6020
6021 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
6022
6023 return column_offset;
6024}
6025#endif /* HAVE_GTK2 */
6026
6027#if !defined(HAVE_GTK2) || defined(PROTO)
6028 void
6029gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
6030{
6031 static XChar2b *buf = NULL;
6032 static int buflen = 0;
6033 int is_wide;
6034 XChar2b *text;
6035 int textlen;
6036 XFontStruct *xfont;
6037 char_u *p;
6038# ifdef FEAT_MBYTE
6039 unsigned c;
6040# endif
6041 int width;
6042
6043 if (gui.current_font == NULL || gui.drawarea->window == NULL)
6044 return;
6045
6046 /*
6047 * Yeah yeah apparently the font support in GTK+ 1.2 only cares for either:
6048 * asians or 8-bit fonts. It is broken there, but no wonder the whole font
6049 * stuff is broken in X11 in first place. And the internationalization API
6050 * isn't something you would really like to use.
6051 */
6052
6053 xfont = (XFontStruct *)((GdkFontPrivate*)gui.current_font)->xfont;
6054 is_wide = ((xfont->min_byte1 != 0 || xfont->max_byte1 != 0)
6055# ifdef FEAT_XFONTSET
6056 && gui.fontset == NOFONTSET
6057# endif
6058 );
6059
6060 if (is_wide)
6061 {
6062 /* Convert a byte sequence to 16 bit characters for the Gdk functions.
6063 * Need a buffer for the 16 bit characters. Keep it between calls,
6064 * because allocating it each time is slow. */
6065 if (buflen < len)
6066 {
6067 XtFree((char *)buf);
6068 buf = (XChar2b *)XtMalloc(len * sizeof(XChar2b));
6069 buflen = len;
6070 }
6071
6072 p = s;
6073 textlen = 0;
6074 width = 0;
6075 while (p < s + len)
6076 {
6077# ifdef FEAT_MBYTE
6078 if (enc_utf8)
6079 {
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00006080 int pcc[MAX_MCO];
6081
6082 /* TODO: use the composing characters */
6083 c = utfc_ptr2char_len(p, &pcc, len - (p - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 if (c >= 0x10000) /* show chars > 0xffff as ? */
6085 c = 0xbf;
6086 buf[textlen].byte1 = c >> 8;
6087 buf[textlen].byte2 = c;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00006088 p += utfc_ptr2len_len(p, len - (p - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 width += utf_char2cells(c);
6090 }
6091 else
6092# endif
6093 {
6094 buf[textlen].byte1 = '\0'; /* high eight bits */
6095 buf[textlen].byte2 = *p; /* low eight bits */
6096 ++p;
6097 ++width;
6098 }
6099 ++textlen;
6100 }
6101 text = buf;
6102 textlen = textlen * 2;
6103 }
6104 else
6105 {
6106 text = (XChar2b *)s;
6107 textlen = len;
6108# ifdef FEAT_MBYTE
6109 if (has_mbyte)
6110 {
6111 width = 0;
Bram Moolenaarfeba08b2009-06-16 13:12:07 +00006112 for (p = s; p < s + len; p += (*mb_ptr2len_len)(p, len - (p - s)))
6113 width += (*mb_ptr2cells_len)(p, len - (p - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 }
6115 else
6116# endif
6117 width = len;
6118 }
6119
6120 if (!(flags & DRAW_TRANSP))
6121 {
6122 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6123 gdk_draw_rectangle(gui.drawarea->window,
6124 gui.text_gc,
6125 TRUE,
6126 FILL_X(col), FILL_Y(row),
6127 width * gui.char_width, gui.char_height);
6128 }
6129 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6130 gdk_draw_text(gui.drawarea->window,
6131 gui.current_font,
6132 gui.text_gc,
6133 TEXT_X(col), TEXT_Y(row),
6134 (const gchar *)text, textlen);
6135
6136 /* redraw the contents with an offset of 1 to emulate bold */
6137 if (flags & DRAW_BOLD)
6138 gdk_draw_text(gui.drawarea->window,
6139 gui.current_font,
6140 gui.text_gc,
6141 TEXT_X(col) + 1, TEXT_Y(row),
6142 (const gchar *)text, textlen);
6143
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006144 /* Draw underline and undercurl. */
6145 draw_under(flags, row, col, width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146}
6147#endif /* !HAVE_GTK2 */
6148
6149/*
6150 * Return OK if the key with the termcap name "name" is supported.
6151 */
6152 int
6153gui_mch_haskey(char_u *name)
6154{
6155 int i;
6156
6157 for (i = 0; special_keys[i].key_sym != 0; i++)
6158 if (name[0] == special_keys[i].code0
6159 && name[1] == special_keys[i].code1)
6160 return OK;
6161 return FAIL;
6162}
6163
6164#if defined(FEAT_TITLE) \
6165 || (defined(FEAT_XIM) && !defined(HAVE_GTK2)) \
6166 || defined(PROTO)
6167/*
6168 * Return the text window-id and display. Only required for X-based GUI's
6169 */
6170 int
6171gui_get_x11_windis(Window *win, Display **dis)
6172{
6173 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6174 {
6175 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6176 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
6177 return OK;
6178 }
6179
6180 *dis = NULL;
6181 *win = 0;
6182 return FAIL;
6183}
6184#endif
6185
6186#if defined(FEAT_CLIENTSERVER) \
6187 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6188
6189 Display *
6190gui_mch_get_display(void)
6191{
6192 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6193 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6194 else
6195 return NULL;
6196}
6197#endif
6198
6199 void
6200gui_mch_beep(void)
6201{
6202#ifdef HAVE_GTK_MULTIHEAD
6203 GdkDisplay *display;
6204
6205 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6206 display = gtk_widget_get_display(gui.mainwin);
6207 else
6208 display = gdk_display_get_default();
6209
6210 if (display != NULL)
6211 gdk_display_beep(display);
6212#else
6213 gdk_beep();
6214#endif
6215}
6216
6217 void
6218gui_mch_flash(int msec)
6219{
6220 GdkGCValues values;
6221 GdkGC *invert_gc;
6222
6223 if (gui.drawarea->window == NULL)
6224 return;
6225
6226 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6227 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6228 values.function = GDK_XOR;
6229 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6230 &values,
6231 GDK_GC_FOREGROUND |
6232 GDK_GC_BACKGROUND |
6233 GDK_GC_FUNCTION);
6234 gdk_gc_set_exposures(invert_gc,
6235 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6236 /*
6237 * Do a visual beep by changing back and forth in some undetermined way,
6238 * the foreground and background colors. This is due to the fact that
6239 * there can't be really any prediction about the effects of XOR on
6240 * arbitrary X11 servers. However this seems to be enough for what we
6241 * intend it to do.
6242 */
6243 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6244 TRUE,
6245 0, 0,
6246 FILL_X((int)Columns) + gui.border_offset,
6247 FILL_Y((int)Rows) + gui.border_offset);
6248
6249 gui_mch_flush();
6250 ui_delay((long)msec, TRUE); /* wait so many msec */
6251
6252 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6253 TRUE,
6254 0, 0,
6255 FILL_X((int)Columns) + gui.border_offset,
6256 FILL_Y((int)Rows) + gui.border_offset);
6257
6258 gdk_gc_destroy(invert_gc);
6259}
6260
6261/*
6262 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6263 */
6264 void
6265gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6266{
6267 GdkGCValues values;
6268 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 if (gui.drawarea->window == NULL)
6271 return;
6272
Bram Moolenaar89d40322006-08-29 15:30:07 +00006273 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6274 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 values.function = GDK_XOR;
6276 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6277 &values,
6278 GDK_GC_FOREGROUND |
6279 GDK_GC_BACKGROUND |
6280 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006281 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6282 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6284 TRUE,
6285 FILL_X(c), FILL_Y(r),
6286 (nc) * gui.char_width, (nr) * gui.char_height);
6287 gdk_gc_destroy(invert_gc);
6288}
6289
6290/*
6291 * Iconify the GUI window.
6292 */
6293 void
6294gui_mch_iconify(void)
6295{
6296#ifdef HAVE_GTK2
6297 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
6298#else
6299 XIconifyWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
6300 GDK_WINDOW_XWINDOW(gui.mainwin->window),
6301 DefaultScreen(GDK_WINDOW_XDISPLAY(gui.mainwin->window)));
6302#endif
6303}
6304
6305#if defined(FEAT_EVAL) || defined(PROTO)
6306/*
6307 * Bring the Vim window to the foreground.
6308 */
6309 void
6310gui_mch_set_foreground(void)
6311{
6312# ifdef HAVE_GTK2
6313 gtk_window_present(GTK_WINDOW(gui.mainwin));
6314# else
6315 gdk_window_raise(gui.mainwin->window);
6316# endif
6317}
6318#endif
6319
6320/*
6321 * Draw a cursor without focus.
6322 */
6323 void
6324gui_mch_draw_hollow_cursor(guicolor_T color)
6325{
6326 int i = 1;
6327
6328 if (gui.drawarea->window == NULL)
6329 return;
6330
6331 gui_mch_set_fg_color(color);
6332
6333 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6334#ifdef FEAT_MBYTE
6335 if (mb_lefthalve(gui.row, gui.col))
6336 i = 2;
6337#endif
6338 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6339 FALSE,
6340 FILL_X(gui.col), FILL_Y(gui.row),
6341 i * gui.char_width - 1, gui.char_height - 1);
6342}
6343
6344/*
6345 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6346 * color "color".
6347 */
6348 void
6349gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6350{
6351 if (gui.drawarea->window == NULL)
6352 return;
6353
6354 gui_mch_set_fg_color(color);
6355
6356 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6357 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6358 TRUE,
6359#ifdef FEAT_RIGHTLEFT
6360 /* vertical line should be on the right of current point */
6361 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6362#endif
6363 FILL_X(gui.col),
6364 FILL_Y(gui.row) + gui.char_height - h,
6365 w, h);
6366}
6367
6368
6369/*
6370 * Catch up with any queued X11 events. This may put keyboard input into the
6371 * input buffer, call resize call-backs, trigger timers etc. If there is
6372 * nothing in the X11 event queue (& no timers pending), then we return
6373 * immediately.
6374 */
6375 void
6376gui_mch_update(void)
6377{
6378 while (gtk_events_pending() && !vim_is_input_buf_full())
6379 gtk_main_iteration_do(FALSE);
6380}
6381
6382 static gint
6383input_timer_cb(gpointer data)
6384{
6385 int *timed_out = (int *) data;
6386
Bram Moolenaar49325942007-05-10 19:19:59 +00006387 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 *timed_out = TRUE;
6389
6390 if (gtk_main_level() > 0)
6391 gtk_main_quit();
6392
6393 return FALSE; /* don't happen again */
6394}
6395
6396#ifdef FEAT_SNIFF
6397/*
6398 * Callback function, used when data is available on the SNiFF connection.
6399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 static void
6401sniff_request_cb(
6402 gpointer data,
6403 gint source_fd,
6404 GdkInputCondition condition)
6405{
6406 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
6407
6408 add_to_input_buf(bytes, 3);
6409
6410 if (gtk_main_level() > 0)
6411 gtk_main_quit();
6412}
6413#endif
6414
6415/*
6416 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6417 * from the keyboard.
6418 * wtime == -1 Wait forever.
6419 * wtime == 0 This should never happen.
6420 * wtime > 0 Wait wtime milliseconds for a character.
6421 * Returns OK if a character was found to be available within the given time,
6422 * or FAIL otherwise.
6423 */
6424 int
6425gui_mch_wait_for_chars(long wtime)
6426{
6427 int focus;
6428 guint timer;
6429 static int timed_out;
6430#ifdef FEAT_SNIFF
6431 static int sniff_on = 0;
6432 static gint sniff_input_id = 0;
6433#endif
6434
6435#ifdef FEAT_SNIFF
6436 if (sniff_on && !want_sniff_request)
6437 {
6438 if (sniff_input_id)
6439 gdk_input_remove(sniff_input_id);
6440 sniff_on = 0;
6441 }
6442 else if (!sniff_on && want_sniff_request)
6443 {
6444 /* Add fd_from_sniff to watch for available data in main loop. */
6445 sniff_input_id = gdk_input_add(fd_from_sniff,
6446 GDK_INPUT_READ, sniff_request_cb, NULL);
6447 sniff_on = 1;
6448 }
6449#endif
6450
6451 timed_out = FALSE;
6452
6453 /* this timeout makes sure that we will return if no characters arrived in
6454 * time */
6455
6456 if (wtime > 0)
6457 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
6458 else
6459 timer = 0;
6460
6461 focus = gui.in_focus;
6462
6463 do
6464 {
6465 /* Stop or start blinking when focus changes */
6466 if (gui.in_focus != focus)
6467 {
6468 if (gui.in_focus)
6469 gui_mch_start_blink();
6470 else
6471 gui_mch_stop_blink();
6472 focus = gui.in_focus;
6473 }
6474
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006475#if defined(FEAT_NETBEANS_INTG)
6476 /* Process the queued netbeans messages. */
6477 netbeans_parse_messages();
6478#endif
6479
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 /*
6481 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006482 * Skip this if input is available anyway (can happen in rare
6483 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006485 if (!input_available())
6486 gtk_main();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487
6488 /* Got char, return immediately */
6489 if (input_available())
6490 {
6491 if (timer != 0 && !timed_out)
6492 gtk_timeout_remove(timer);
6493 return OK;
6494 }
6495 } while (wtime < 0 || !timed_out);
6496
6497 /*
6498 * Flush all eventually pending (drawing) events.
6499 */
6500 gui_mch_update();
6501
6502 return FAIL;
6503}
6504
6505
6506/****************************************************************************
6507 * Output drawing routines.
6508 ****************************************************************************/
6509
6510
6511/* Flush any output to the screen */
6512 void
6513gui_mch_flush(void)
6514{
6515#ifdef HAVE_GTK_MULTIHEAD
6516 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6517 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6518#else
6519 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6520#endif
6521#ifdef HAVE_GTK2
6522 /* This happens to actually do what gui_mch_flush() is supposed to do,
6523 * according to the comment above. */
6524 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
6525 gdk_window_process_updates(gui.drawarea->window, FALSE);
6526#endif
6527}
6528
6529/*
6530 * Clear a rectangular region of the screen from text pos (row1, col1) to
6531 * (row2, col2) inclusive.
6532 */
6533 void
6534gui_mch_clear_block(int row1, int col1, int row2, int col2)
6535{
6536 GdkColor color;
6537
6538 if (gui.drawarea->window == NULL)
6539 return;
6540
6541 color.pixel = gui.back_pixel;
6542
6543 gdk_gc_set_foreground(gui.text_gc, &color);
6544
6545 /* Clear one extra pixel at the far right, for when bold characters have
6546 * spilled over to the window border. */
6547 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6548 FILL_X(col1), FILL_Y(row1),
6549 (col2 - col1 + 1) * gui.char_width
6550 + (col2 == Columns - 1),
6551 (row2 - row1 + 1) * gui.char_height);
6552}
6553
6554 void
6555gui_mch_clear_all(void)
6556{
6557 if (gui.drawarea->window != NULL)
6558 gdk_window_clear(gui.drawarea->window);
6559}
6560
6561/*
6562 * Redraw any text revealed by scrolling up/down.
6563 */
6564 static void
6565check_copy_area(void)
6566{
6567 GdkEvent *event;
6568 int expose_count;
6569
6570 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6571 return;
6572
6573 /* Avoid redrawing the cursor while scrolling or it'll end up where
6574 * we don't want it to be. I'm not sure if it's correct to call
6575 * gui_dont_update_cursor() at this point but it works as a quick
6576 * fix for now. */
6577 gui_dont_update_cursor();
6578
6579 do
6580 {
6581 /* Wait to check whether the scroll worked or not. */
6582 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6583
6584 if (event == NULL)
6585 break; /* received NoExpose event */
6586
6587 gui_redraw(event->expose.area.x, event->expose.area.y,
6588 event->expose.area.width, event->expose.area.height);
6589
6590 expose_count = event->expose.count;
6591 gdk_event_free(event);
6592 }
6593 while (expose_count > 0); /* more events follow */
6594
6595 gui_can_update_cursor();
6596}
6597
6598/*
6599 * Delete the given number of lines from the given row, scrolling up any
6600 * text further down within the scroll region.
6601 */
6602 void
6603gui_mch_delete_lines(int row, int num_lines)
6604{
6605 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6606 return; /* Can't see the window */
6607
6608 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6609 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6610
6611 /* copy one extra pixel, for when bold has spilled over */
6612 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6613 FILL_X(gui.scroll_region_left), FILL_Y(row),
6614 gui.drawarea->window,
6615 FILL_X(gui.scroll_region_left),
6616 FILL_Y(row + num_lines),
6617 gui.char_width * (gui.scroll_region_right
6618 - gui.scroll_region_left + 1) + 1,
6619 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6620
6621 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6622 gui.scroll_region_left,
6623 gui.scroll_region_bot, gui.scroll_region_right);
6624 check_copy_area();
6625}
6626
6627/*
6628 * Insert the given number of lines before the given row, scrolling down any
6629 * following text within the scroll region.
6630 */
6631 void
6632gui_mch_insert_lines(int row, int num_lines)
6633{
6634 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6635 return; /* Can't see the window */
6636
6637 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6638 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6639
6640 /* copy one extra pixel, for when bold has spilled over */
6641 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6642 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6643 gui.drawarea->window,
6644 FILL_X(gui.scroll_region_left), FILL_Y(row),
6645 gui.char_width * (gui.scroll_region_right
6646 - gui.scroll_region_left + 1) + 1,
6647 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6648
6649 gui_clear_block(row, gui.scroll_region_left,
6650 row + num_lines - 1, gui.scroll_region_right);
6651 check_copy_area();
6652}
6653
6654/*
6655 * X Selection stuff, for cutting and pasting text to other windows.
6656 */
6657 void
6658clip_mch_request_selection(VimClipboard *cbd)
6659{
6660 GdkAtom target;
6661 unsigned i;
6662 int nbytes;
6663 char_u *buffer;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006664 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665
6666 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6667 {
6668 received_selection = RS_NONE;
6669 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6670
6671 gtk_selection_convert(gui.drawarea,
6672 cbd->gtk_sel_atom, target,
6673 (guint32)GDK_CURRENT_TIME);
6674
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006675 /* Hack: Wait up to three seconds for the selection. A hang was
6676 * noticed here when using the netrw plugin combined with ":gui"
6677 * during the FocusGained event. */
6678 start = time(NULL);
6679 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 gtk_main(); /* wait for selection_received_cb */
6681
6682 if (received_selection != RS_FAIL)
6683 return;
6684 }
6685
6686 /* Final fallback position - use the X CUT_BUFFER0 store */
6687 nbytes = 0;
6688 buffer = (char_u *)XFetchBuffer(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
6689 &nbytes, 0);
6690 if (nbytes > 0)
6691 {
6692 /* Got something */
6693 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
6694 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00006695 {
6696 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 smsg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaar54ee7752005-05-31 22:22:17 +00006698 verbose_leave();
6699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701 if (buffer != NULL)
6702 XFree(buffer);
6703}
6704
6705/*
6706 * Disown the selection.
6707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006709clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710{
6711 /* WEIRD: when using NULL to actually disown the selection, we lose the
6712 * selection the first time we own it. */
6713 /*
6714 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
6715 gui_mch_update();
6716 */
6717}
6718
6719/*
6720 * Own the selection and return OK if it worked.
6721 */
6722 int
6723clip_mch_own_selection(VimClipboard *cbd)
6724{
6725 int success;
6726
6727 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
6728 (guint32)GDK_CURRENT_TIME);
6729 gui_mch_update();
6730 return (success) ? OK : FAIL;
6731}
6732
6733/*
6734 * Send the current selection to the clipboard. Do nothing for X because we
6735 * will fill in the selection only when requested by another app.
6736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006738clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740}
6741
6742
6743#if defined(FEAT_MENU) || defined(PROTO)
6744/*
6745 * Make a menu item appear either active or not active (grey or not grey).
6746 */
6747 void
6748gui_mch_menu_grey(vimmenu_T *menu, int grey)
6749{
6750 if (menu->id == NULL)
6751 return;
6752
6753 if (menu_is_separator(menu->name))
6754 grey = TRUE;
6755
6756 gui_mch_menu_hidden(menu, FALSE);
6757 /* Be clever about bitfields versus true booleans here! */
6758 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
6759 {
6760 gtk_widget_set_sensitive(menu->id, !grey);
6761 gui_mch_update();
6762 }
6763}
6764
6765/*
6766 * Make menu item hidden or not hidden.
6767 */
6768 void
6769gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6770{
6771 if (menu->id == 0)
6772 return;
6773
6774 if (hidden)
6775 {
6776 if (GTK_WIDGET_VISIBLE(menu->id))
6777 {
6778 gtk_widget_hide(menu->id);
6779 gui_mch_update();
6780 }
6781 }
6782 else
6783 {
6784 if (!GTK_WIDGET_VISIBLE(menu->id))
6785 {
6786 gtk_widget_show(menu->id);
6787 gui_mch_update();
6788 }
6789 }
6790}
6791
6792/*
6793 * This is called after setting all the menus to grey/hidden or not.
6794 */
6795 void
6796gui_mch_draw_menubar(void)
6797{
6798 /* just make sure that the visual changes get effect immediately */
6799 gui_mch_update();
6800}
6801#endif /* FEAT_MENU */
6802
6803/*
6804 * Scrollbar stuff.
6805 */
6806 void
6807gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
6808{
6809 if (sb->id == NULL)
6810 return;
6811
6812 if (flag)
6813 gtk_widget_show(sb->id);
6814 else
6815 gtk_widget_hide(sb->id);
6816
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00006817 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818}
6819
6820
6821/*
6822 * Return the RGB value of a pixel as long.
6823 */
6824 long_u
6825gui_mch_get_rgb(guicolor_T pixel)
6826{
6827 GdkColor color;
6828#ifndef HAVE_GTK2
6829 GdkColorContext *cc;
6830
6831 cc = gdk_color_context_new(gtk_widget_get_visual(gui.drawarea),
6832 gtk_widget_get_colormap(gui.drawarea));
6833 color.pixel = pixel;
6834 gdk_color_context_query_color(cc, &color);
6835
6836 gdk_color_context_free(cc);
6837#else
6838 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
6839 (unsigned long)pixel, &color);
6840#endif
6841
6842 return (((unsigned)color.red & 0xff00) << 8)
6843 | ((unsigned)color.green & 0xff00)
6844 | (((unsigned)color.blue & 0xff00) >> 8);
6845}
6846
6847/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006848 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006850 void
6851gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006853 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854}
6855
6856 void
6857gui_mch_setmouse(int x, int y)
6858{
6859 /* Sorry for the Xlib call, but we can't avoid it, since there is no
6860 * internal GDK mechanism present to accomplish this. (and for good
6861 * reason...) */
6862 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
6863 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
6864 0, 0, 0U, 0U, x, y);
6865}
6866
6867
6868#ifdef FEAT_MOUSESHAPE
6869/* The last set mouse pointer shape is remembered, to be used when it goes
6870 * from hidden to not hidden. */
6871static int last_shape = 0;
6872#endif
6873
6874/*
6875 * Use the blank mouse pointer or not.
6876 *
6877 * hide: TRUE = use blank ptr, FALSE = use parent ptr
6878 */
6879 void
6880gui_mch_mousehide(int hide)
6881{
6882 if (gui.pointer_hidden != hide)
6883 {
6884 gui.pointer_hidden = hide;
6885 if (gui.drawarea->window && gui.blank_pointer != NULL)
6886 {
6887 if (hide)
6888 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
6889 else
6890#ifdef FEAT_MOUSESHAPE
6891 mch_set_mouse_shape(last_shape);
6892#else
6893 gdk_window_set_cursor(gui.drawarea->window, NULL);
6894#endif
6895 }
6896 }
6897}
6898
6899#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
6900
6901/* Table for shape IDs. Keep in sync with the mshape_names[] table in
6902 * misc2.c! */
6903static const int mshape_ids[] =
6904{
6905 GDK_LEFT_PTR, /* arrow */
6906 GDK_CURSOR_IS_PIXMAP, /* blank */
6907 GDK_XTERM, /* beam */
6908 GDK_SB_V_DOUBLE_ARROW, /* updown */
6909 GDK_SIZING, /* udsizing */
6910 GDK_SB_H_DOUBLE_ARROW, /* leftright */
6911 GDK_SIZING, /* lrsizing */
6912 GDK_WATCH, /* busy */
6913 GDK_X_CURSOR, /* no */
6914 GDK_CROSSHAIR, /* crosshair */
6915 GDK_HAND1, /* hand1 */
6916 GDK_HAND2, /* hand2 */
6917 GDK_PENCIL, /* pencil */
6918 GDK_QUESTION_ARROW, /* question */
6919 GDK_RIGHT_PTR, /* right-arrow */
6920 GDK_CENTER_PTR, /* up-arrow */
6921 GDK_LEFT_PTR /* last one */
6922};
6923
6924 void
6925mch_set_mouse_shape(int shape)
6926{
6927 int id;
6928 GdkCursor *c;
6929
6930 if (gui.drawarea->window == NULL)
6931 return;
6932
6933 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
6934 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
6935 else
6936 {
6937 if (shape >= MSHAPE_NUMBERED)
6938 {
6939 id = shape - MSHAPE_NUMBERED;
6940 if (id >= GDK_LAST_CURSOR)
6941 id = GDK_LEFT_PTR;
6942 else
6943 id &= ~1; /* they are always even (why?) */
6944 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006945 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006947 else
6948 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949# ifdef HAVE_GTK_MULTIHEAD
6950 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00006951 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00006953 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954# endif
6955 gdk_window_set_cursor(gui.drawarea->window, c);
6956 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
6957 }
6958 if (shape != MSHAPE_HIDE)
6959 last_shape = shape;
6960}
6961#endif /* FEAT_MOUSESHAPE */
6962
6963
6964#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6965/*
6966 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
6967 * scaled down if the current font is not big enough, or scaled up if the image
6968 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
6969 * will be cut off if the current font is not big enough, or centered if it's
6970 * too small.
6971 */
6972# define SIGN_WIDTH (2 * gui.char_width)
6973# define SIGN_HEIGHT (gui.char_height)
6974# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
6975
6976# ifdef HAVE_GTK2
6977
6978 void
6979gui_mch_drawsign(int row, int col, int typenr)
6980{
6981 GdkPixbuf *sign;
6982
6983 sign = (GdkPixbuf *)sign_get_image(typenr);
6984
6985 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
6986 {
6987 int width;
6988 int height;
6989 int xoffset;
6990 int yoffset;
6991 int need_scale;
6992
6993 width = gdk_pixbuf_get_width(sign);
6994 height = gdk_pixbuf_get_height(sign);
6995 /*
6996 * Decide whether we need to scale. Allow one pixel of border
6997 * width to be cut off, in order to avoid excessive scaling for
6998 * tiny differences in font size.
6999 */
7000 need_scale = (width > SIGN_WIDTH + 2
7001 || height > SIGN_HEIGHT + 2
7002 || (width < 3 * SIGN_WIDTH / 4
7003 && height < 3 * SIGN_HEIGHT / 4));
7004 if (need_scale)
7005 {
7006 double aspect;
7007
7008 /* Keep the original aspect ratio */
7009 aspect = (double)height / (double)width;
7010 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7011 width = MIN(width, SIGN_WIDTH);
7012 height = (double)width * aspect;
7013
7014 /* This doesn't seem to be worth caching, and doing so
7015 * would complicate the code quite a bit. */
7016 sign = gdk_pixbuf_scale_simple(sign, width, height,
7017 GDK_INTERP_BILINEAR);
7018 if (sign == NULL)
7019 return; /* out of memory */
7020 }
7021
7022 /* The origin is the upper-left corner of the pixmap. Therefore
7023 * these offset may become negative if the pixmap is smaller than
7024 * the 2x1 cells reserved for the sign icon. */
7025 xoffset = (width - SIGN_WIDTH) / 2;
7026 yoffset = (height - SIGN_HEIGHT) / 2;
7027
7028 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7029
7030 gdk_draw_rectangle(gui.drawarea->window,
7031 gui.text_gc,
7032 TRUE,
7033 FILL_X(col),
7034 FILL_Y(row),
7035 SIGN_WIDTH,
7036 SIGN_HEIGHT);
7037
7038# if GTK_CHECK_VERSION(2,1,1)
7039 gdk_draw_pixbuf(gui.drawarea->window,
7040 NULL,
7041 sign,
7042 MAX(0, xoffset),
7043 MAX(0, yoffset),
7044 FILL_X(col) - MIN(0, xoffset),
7045 FILL_Y(row) - MIN(0, yoffset),
7046 MIN(width, SIGN_WIDTH),
7047 MIN(height, SIGN_HEIGHT),
7048 GDK_RGB_DITHER_NORMAL,
7049 0, 0);
7050# else
7051 gdk_pixbuf_render_to_drawable_alpha(sign,
7052 gui.drawarea->window,
7053 MAX(0, xoffset),
7054 MAX(0, yoffset),
7055 FILL_X(col) - MIN(0, xoffset),
7056 FILL_Y(row) - MIN(0, yoffset),
7057 MIN(width, SIGN_WIDTH),
7058 MIN(height, SIGN_HEIGHT),
7059 GDK_PIXBUF_ALPHA_BILEVEL,
7060 127,
7061 GDK_RGB_DITHER_NORMAL,
7062 0, 0);
7063# endif
7064 if (need_scale)
7065 g_object_unref(sign);
7066 }
7067}
7068
7069 void *
7070gui_mch_register_sign(char_u *signfile)
7071{
7072 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7073 {
7074 GdkPixbuf *sign;
7075 GError *error = NULL;
7076 char_u *message;
7077
7078 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7079
7080 if (error == NULL)
7081 return sign;
7082
7083 message = (char_u *)error->message;
7084
7085 if (message != NULL && input_conv.vc_type != CONV_NONE)
7086 message = string_convert(&input_conv, message, NULL);
7087
7088 if (message != NULL)
7089 {
7090 /* The error message is already translated and will be more
7091 * descriptive than anything we could possibly do ourselves. */
7092 EMSG2("E255: %s", message);
7093
7094 if (input_conv.vc_type != CONV_NONE)
7095 vim_free(message);
7096 }
7097 g_error_free(error);
7098 }
7099
7100 return NULL;
7101}
7102
7103 void
7104gui_mch_destroy_sign(void *sign)
7105{
7106 if (sign != NULL)
7107 g_object_unref(sign);
7108}
7109
7110# else /* !HAVE_GTK2 */
7111
7112typedef struct
7113{
7114 GdkPixmap *pixmap;
7115 GdkBitmap *mask;
7116}
7117signicon_T;
7118
7119 void
7120gui_mch_drawsign(int row, int col, int typenr)
7121{
7122 signicon_T *sign;
7123
7124 sign = (signicon_T *)sign_get_image(typenr);
7125
7126 if (sign != NULL && sign->pixmap != NULL
7127 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7128 {
7129 int width;
7130 int height;
7131 int xoffset;
7132 int yoffset;
7133
7134 gdk_window_get_size(sign->pixmap, &width, &height);
7135
7136 /* The origin is the upper-left corner of the pixmap. Therefore
7137 * these offset may become negative if the pixmap is smaller than
7138 * the 2x1 cells reserved for the sign icon. */
7139 xoffset = (width - SIGN_WIDTH) / 2;
7140 yoffset = (height - SIGN_HEIGHT) / 2;
7141
7142 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7143
7144 gdk_draw_rectangle(gui.drawarea->window,
7145 gui.text_gc,
7146 TRUE,
7147 FILL_X(col),
7148 FILL_Y(row),
7149 SIGN_WIDTH,
7150 SIGN_HEIGHT);
7151
7152 /* Set the clip mask for bilevel transparency */
7153 if (sign->mask != NULL)
7154 {
7155 gdk_gc_set_clip_origin(gui.text_gc,
7156 FILL_X(col) - xoffset,
7157 FILL_Y(row) - yoffset);
7158 gdk_gc_set_clip_mask(gui.text_gc, sign->mask);
7159 }
7160
7161 gdk_draw_pixmap(gui.drawarea->window,
7162 gui.text_gc,
7163 sign->pixmap,
7164 MAX(0, xoffset),
7165 MAX(0, yoffset),
7166 FILL_X(col) - MIN(0, xoffset),
7167 FILL_Y(row) - MIN(0, yoffset),
7168 MIN(width, SIGN_WIDTH),
7169 MIN(height, SIGN_HEIGHT));
7170
7171 gdk_gc_set_clip_mask(gui.text_gc, NULL);
7172 }
7173}
7174
7175 void *
7176gui_mch_register_sign(char_u *signfile)
7177{
7178 signicon_T *sign = NULL;
7179
7180 if (signfile[0] != NUL && signfile[0] != '-'
7181 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7182 {
7183 sign = (signicon_T *)alloc(sizeof(signicon_T));
7184
7185 if (sign != NULL) /* NULL == OOM == "cannot really happen" */
7186 {
7187 sign->mask = NULL;
7188 sign->pixmap = gdk_pixmap_colormap_create_from_xpm(
7189 gui.drawarea->window, NULL,
7190 &sign->mask, NULL,
7191 (const char *)signfile);
7192
7193 if (sign->pixmap == NULL)
7194 {
7195 vim_free(sign);
7196 sign = NULL;
7197 EMSG(_(e_signdata));
7198 }
7199 }
7200 }
7201 return sign;
7202}
7203
7204 void
7205gui_mch_destroy_sign(void *sign)
7206{
7207 if (sign != NULL)
7208 {
7209 signicon_T *signicon = (signicon_T *)sign;
7210
7211 if (signicon->pixmap != NULL)
7212 gdk_pixmap_unref(signicon->pixmap);
7213 if (signicon->mask != NULL)
7214 gdk_bitmap_unref(signicon->mask);
7215
7216 vim_free(signicon);
7217 }
7218}
7219# endif /* !HAVE_GTK2 */
7220
7221#endif /* FEAT_SIGN_ICONS */