blob: d7f5103e1ebcb46eb460be2aea29cb925ca990e8 [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;
415#endif
416static int found_iconic_arg = FALSE;
417
418#ifdef FEAT_GUI_GNOME
419/*
420 * Can't use Gnome if --socketid given
421 */
422static int using_gnome = 0;
423#else
424# define using_gnome 0
425#endif
426
427/*
428 * Parse the GUI related command-line arguments. Any arguments used are
429 * deleted from argv, and *argc is decremented accordingly. This is called
430 * when vim is started, whether or not the GUI has been started.
431 */
432 void
433gui_mch_prepare(int *argc, char **argv)
434{
435 const cmdline_option_T *option;
436 int i = 0;
437 int len = 0;
438
439#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
440 /*
441 * Determine the command used to invoke Vim, to be passed as restart
442 * command to the session manager. If argv[0] contains any directory
443 * components try building an absolute path, otherwise leave it as is.
444 */
445 restart_command = argv[0];
446
447 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
448 {
449 char_u buf[MAXPATHL];
450
451 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
452 /* Tiny leak; doesn't matter, and usually we don't even get here */
453 restart_command = (char *)vim_strsave(buf);
454 }
455#endif
456
457 /*
458 * Move all the entries in argv which are relevant to GTK+ and GNOME
459 * into gui_argv. Freed later in gui_mch_init().
460 */
461 gui_argc = 0;
462 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
463
464 g_return_if_fail(gui_argv != NULL);
465
466 gui_argv[gui_argc++] = argv[i++];
467
468 while (i < *argc)
469 {
470 /* Don't waste CPU cycles on non-option arguments. */
471 if (argv[i][0] != '-' && argv[i][0] != '+')
472 {
473 ++i;
474 continue;
475 }
476
477 /* Look for argv[i] in cmdline_options[] table. */
478 for (option = &cmdline_options[0]; option->name != NULL; ++option)
479 {
480 len = strlen(option->name);
481
482 if (strncmp(argv[i], option->name, len) == 0)
483 {
484 if (argv[i][len] == '\0')
485 break;
486 /* allow --foo=bar style */
487 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
488 break;
489#ifdef FEAT_NETBEANS_INTG
490 /* darn, -nb has non-standard syntax */
491 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
492 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
493 break;
494#endif
495 }
496 else if ((option->flags & ARG_COMPAT_LONG)
497 && strcmp(argv[i], option->name + 1) == 0)
498 {
499 /* Replace the standard X arguments "-name" and "-display"
500 * with their GNU-style long option counterparts. */
501 argv[i] = (char *)option->name;
502 break;
503 }
504 }
505 if (option->name == NULL) /* no match */
506 {
507 ++i;
508 continue;
509 }
510
511 if (option->flags & ARG_FOR_GTK)
512 {
513 /* Move the argument into gui_argv, which
514 * will later be passed to gtk_init_check() */
515 gui_argv[gui_argc++] = argv[i];
516 }
517 else
518 {
519 char *value = NULL;
520
521 /* Extract the option's value if there is one.
522 * Accept both "--foo bar" and "--foo=bar" style. */
523 if (option->flags & ARG_HAS_VALUE)
524 {
525 if (argv[i][len] == '=')
526 value = &argv[i][len + 1];
527 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
528 value = argv[i + 1];
529 }
530
531 /* Check for options handled by Vim itself */
532 switch (option->flags & ARG_INDEX_MASK)
533 {
534 case ARG_REVERSE:
535 found_reverse_arg = TRUE;
536 break;
537 case ARG_NOREVERSE:
538 found_reverse_arg = FALSE;
539 break;
540 case ARG_FONT:
541 font_argument = value;
542 break;
543 case ARG_GEOMETRY:
544 if (value != NULL)
545 gui.geom = vim_strsave((char_u *)value);
546 break;
547 case ARG_BACKGROUND:
548 background_argument = value;
549 break;
550 case ARG_FOREGROUND:
551 foreground_argument = value;
552 break;
553 case ARG_ICONIC:
554 found_iconic_arg = TRUE;
555 break;
556#ifdef HAVE_GTK2
557 case ARG_ROLE:
558 role_argument = value; /* used later in gui_mch_open() */
559 break;
560#endif
561#ifdef FEAT_NETBEANS_INTG
562 case ARG_NETBEANS:
563 ++usingNetbeans;
564 gui.dofork = FALSE; /* don't fork() when starting GUI */
565 netbeansArg = argv[i];
566 break;
567#endif
568 default:
569 break;
570 }
571 }
572
573 /* These arguments make gnome_program_init() print a message and exit.
574 * Must start the GUI for this, otherwise ":gui" will exit later! */
575 if (option->flags & ARG_NEEDS_GUI)
576 gui.starting = TRUE;
577
578 if (option->flags & ARG_KEEP)
579 ++i;
580 else
581 {
582 /* Remove the flag from the argument vector. */
583 if (--*argc > i)
584 {
585 int n_strip = 1;
586
587 /* Move the argument's value as well, if there is one. */
588 if ((option->flags & ARG_HAS_VALUE)
589 && argv[i][len] != '='
590 && strcmp(argv[i + 1], "--") != 0)
591 {
592 ++n_strip;
593 --*argc;
594 if (option->flags & ARG_FOR_GTK)
595 gui_argv[gui_argc++] = argv[i + 1];
596 }
597
598 if (*argc > i)
599 mch_memmove(&argv[i], &argv[i + n_strip],
600 (*argc - i) * sizeof(char *));
601 }
602 argv[*argc] = NULL;
603 }
604 }
605
606 gui_argv[gui_argc] = NULL;
607}
608
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000609#if defined(EXITFREE) || defined(PROTO)
610 void
611gui_mch_free_all()
612{
613 vim_free(gui_argv);
614}
615#endif
616
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617/*
618 * This should be maybe completely removed.
619 * Doesn't seem possible, since check_copy_area() relies on
620 * this information. --danielk
621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000623visibility_event(GtkWidget *widget UNUSED,
624 GdkEventVisibility *event,
625 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626{
627 gui.visibility = event->state;
628 /*
629 * When we do an gdk_window_copy_area(), and the window is partially
630 * obscured, we want to receive an event to tell us whether it worked
631 * or not.
632 */
633 if (gui.text_gc != NULL)
634 gdk_gc_set_exposures(gui.text_gc,
635 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
636 return FALSE;
637}
638
639/*
640 * Redraw the corresponding portions of the screen.
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000643expose_event(GtkWidget *widget UNUSED,
644 GdkEventExpose *event,
645 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646{
647 /* Skip this when the GUI isn't set up yet, will redraw later. */
648 if (gui.starting)
649 return FALSE;
650
651 out_flush(); /* make sure all output has been processed */
652 gui_redraw(event->area.x, event->area.y,
653 event->area.width, event->area.height);
654
655 /* Clear the border areas if needed */
656 if (event->area.x < FILL_X(0))
657 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
658 if (event->area.y < FILL_Y(0))
659 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
660 if (event->area.x > FILL_X(Columns))
661 gdk_window_clear_area(gui.drawarea->window,
662 FILL_X((int)Columns), 0, 0, 0);
663 if (event->area.y > FILL_Y(Rows))
664 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
665
666 return FALSE;
667}
668
669#ifdef FEAT_CLIENTSERVER
670/*
671 * Handle changes to the "Comm" property
672 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000674property_event(GtkWidget *widget,
675 GdkEventProperty *event,
676 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678 if (event->type == GDK_PROPERTY_NOTIFY
679 && event->state == (int)GDK_PROPERTY_NEW_VALUE
680 && GDK_WINDOW_XWINDOW(event->window) == commWindow
681 && GET_X_ATOM(event->atom) == commProperty)
682 {
683 XEvent xev;
684
685 /* Translate to XLib */
686 xev.xproperty.type = PropertyNotify;
687 xev.xproperty.atom = commProperty;
688 xev.xproperty.window = commWindow;
689 xev.xproperty.state = PropertyNewValue;
690 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
691
692 if (gtk_main_level() > 0)
693 gtk_main_quit();
694 }
695 return FALSE;
696}
697#endif
698
699
700/****************************************************************************
701 * Focus handlers:
702 */
703
704
705/*
706 * This is a simple state machine:
707 * BLINK_NONE not blinking at all
708 * BLINK_OFF blinking, cursor is not shown
709 * BLINK_ON blinking, cursor is shown
710 */
711
712#define BLINK_NONE 0
713#define BLINK_OFF 1
714#define BLINK_ON 2
715
716static int blink_state = BLINK_NONE;
717static long_u blink_waittime = 700;
718static long_u blink_ontime = 400;
719static long_u blink_offtime = 250;
720static guint blink_timer = 0;
721
722 void
723gui_mch_set_blinking(long waittime, long on, long off)
724{
725 blink_waittime = waittime;
726 blink_ontime = on;
727 blink_offtime = off;
728}
729
730/*
731 * Stop the cursor blinking. Show the cursor if it wasn't shown.
732 */
733 void
734gui_mch_stop_blink(void)
735{
736 if (blink_timer)
737 {
738 gtk_timeout_remove(blink_timer);
739 blink_timer = 0;
740 }
741 if (blink_state == BLINK_OFF)
742 gui_update_cursor(TRUE, FALSE);
743 blink_state = BLINK_NONE;
744}
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000747blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748{
749 if (blink_state == BLINK_ON)
750 {
751 gui_undraw_cursor();
752 blink_state = BLINK_OFF;
753 blink_timer = gtk_timeout_add((guint32)blink_offtime,
754 (GtkFunction) blink_cb, NULL);
755 }
756 else
757 {
758 gui_update_cursor(TRUE, FALSE);
759 blink_state = BLINK_ON;
760 blink_timer = gtk_timeout_add((guint32)blink_ontime,
761 (GtkFunction) blink_cb, NULL);
762 }
763
764 return FALSE; /* don't happen again */
765}
766
767/*
768 * Start the cursor blinking. If it was already blinking, this restarts the
769 * waiting time and shows the cursor.
770 */
771 void
772gui_mch_start_blink(void)
773{
774 if (blink_timer)
775 gtk_timeout_remove(blink_timer);
776 /* Only switch blinking on if none of the times is zero */
777 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
778 {
779 blink_timer = gtk_timeout_add((guint32)blink_waittime,
780 (GtkFunction) blink_cb, NULL);
781 blink_state = BLINK_ON;
782 gui_update_cursor(TRUE, FALSE);
783 }
784}
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000787enter_notify_event(GtkWidget *widget UNUSED,
788 GdkEventCrossing *event UNUSED,
789 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
791 if (blink_state == BLINK_NONE)
792 gui_mch_start_blink();
793
794 /* make sure keyboard input goes there */
795 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
796 gtk_widget_grab_focus(gui.drawarea);
797
798 return FALSE;
799}
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000802leave_notify_event(GtkWidget *widget UNUSED,
803 GdkEventCrossing *event UNUSED,
804 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 if (blink_state != BLINK_NONE)
807 gui_mch_stop_blink();
808
809 return FALSE;
810}
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000813focus_in_event(GtkWidget *widget,
814 GdkEventFocus *event UNUSED,
815 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816{
817 gui_focus_change(TRUE);
818
819 if (blink_state == BLINK_NONE)
820 gui_mch_start_blink();
821
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000822 /* make sure keyboard input goes to the draw area (if this is focus for a
823 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000824 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000825 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000827 /* make sure the input buffer is read */
828 if (gtk_main_level() > 0)
829 gtk_main_quit();
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 return TRUE;
832}
833
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000835focus_out_event(GtkWidget *widget UNUSED,
836 GdkEventFocus *event UNUSED,
837 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838{
839 gui_focus_change(FALSE);
840
841 if (blink_state != BLINK_NONE)
842 gui_mch_stop_blink();
843
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000844 /* make sure the input buffer is read */
845 if (gtk_main_level() > 0)
846 gtk_main_quit();
847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 return TRUE;
849}
850
851
852#ifdef HAVE_GTK2
853/*
854 * Translate a GDK key value to UTF-8 independently of the current locale.
855 * The output is written to string, which must have room for at least 6 bytes
856 * plus the NUL terminator. Returns the length in bytes.
857 *
858 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
859 * of GdkEventKey::string instead. But event->string is evil; see here why:
860 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
861 */
862 static int
863keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
864{
865 int len;
866 guint32 uc;
867
868 uc = gdk_keyval_to_unicode(keyval);
869 if (uc != 0)
870 {
871 /* Check for CTRL-foo */
872 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
873 {
874 /* These mappings look arbitrary at the first glance, but in fact
875 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
876 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
877 * more sense and is consistent with usual terminal behaviour). */
878 if (uc >= '@')
879 string[0] = uc & 0x1F;
880 else if (uc == '2')
881 string[0] = NUL;
882 else if (uc >= '3' && uc <= '7')
883 string[0] = uc ^ 0x28;
884 else if (uc == '8')
885 string[0] = BS;
886 else if (uc == '?')
887 string[0] = DEL;
888 else
889 string[0] = uc;
890 len = 1;
891 }
892 else
893 {
894 /* Translate a normal key to UTF-8. This doesn't work for dead
895 * keys of course, you _have_ to use an input method for that. */
896 len = utf_char2bytes((int)uc, string);
897 }
898 }
899 else
900 {
901 /* Translate keys which are represented by ASCII control codes in Vim.
902 * There are only a few of those; most control keys are translated to
903 * special terminal-like control sequences. */
904 len = 1;
905 switch (keyval)
906 {
907 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
908 string[0] = TAB;
909 break;
910 case GDK_Linefeed:
911 string[0] = NL;
912 break;
913 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
914 string[0] = CAR;
915 break;
916 case GDK_Escape:
917 string[0] = ESC;
918 break;
919 default:
920 len = 0;
921 break;
922 }
923 }
924 string[len] = NUL;
925
926 return len;
927}
928#endif /* HAVE_GTK2 */
929
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000930 static int
931modifiers_gdk2vim(guint state)
932{
933 int modifiers = 0;
934
935 if (state & GDK_SHIFT_MASK)
936 modifiers |= MOD_MASK_SHIFT;
937 if (state & GDK_CONTROL_MASK)
938 modifiers |= MOD_MASK_CTRL;
939 if (state & GDK_MOD1_MASK)
940 modifiers |= MOD_MASK_ALT;
941 if (state & GDK_MOD4_MASK)
942 modifiers |= MOD_MASK_META;
943
944 return modifiers;
945}
946
947 static int
948modifiers_gdk2mouse(guint state)
949{
950 int modifiers = 0;
951
952 if (state & GDK_SHIFT_MASK)
953 modifiers |= MOUSE_SHIFT;
954 if (state & GDK_CONTROL_MASK)
955 modifiers |= MOUSE_CTRL;
956 if (state & GDK_MOD1_MASK)
957 modifiers |= MOUSE_ALT;
958
959 return modifiers;
960}
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
963 * Main keyboard handler:
964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000966key_press_event(GtkWidget *widget UNUSED,
967 GdkEventKey *event,
968 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
970#ifdef HAVE_GTK2
971 /* 256 bytes is way over the top, but for safety let's reduce it only
972 * for GTK+ 2 where we know for sure how large the string might get.
973 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
974 char_u string[32], string2[32];
975#else
976 char_u string[256], string2[256];
977#endif
978 guint key_sym;
979 int len;
980 int i;
981 int modifiers;
982 int key;
983 guint state;
984 char_u *s, *d;
985
986 key_sym = event->keyval;
987 state = event->state;
988#ifndef HAVE_GTK2 /* deprecated */
989 len = event->length;
990 g_assert(len <= sizeof(string));
991#endif
992
993#ifndef HAVE_GTK2
994 /*
995 * It appears as if we always want to consume a key-press (there currently
996 * aren't any 'return FALSE's), so we always do this: when running in a
997 * GtkPlug and not a window, we must prevent emission of the key_press
998 * EVENT from continuing (which is 'beyond' the level of stopping mere
999 * signals by returning FALSE), otherwise things like tab/cursor-keys are
1000 * processed by the GtkPlug default handler, which moves input focus away
1001 * from us!
1002 * Note: This should no longer be necessary with GTK+ 2.
1003 */
1004 if (gtk_socket_id != 0)
1005 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
1006#endif
1007
1008#ifdef FEAT_XIM
1009 if (xim_queue_key_press_event(event, TRUE))
1010 return TRUE;
1011#endif
1012
1013#ifdef FEAT_HANGULIN
1014 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
1015 {
1016 hangul_input_state_toggle();
1017 return TRUE;
1018 }
1019#endif
1020
1021#ifdef SunXK_F36
1022 /*
1023 * These keys have bogus lookup strings, and trapping them here is
1024 * easier than trying to XRebindKeysym() on them with every possible
1025 * combination of modifiers.
1026 */
1027 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
1028 len = 0;
1029 else
1030#endif
1031 {
1032#ifdef HAVE_GTK2
1033 len = keyval_to_string(key_sym, state, string2);
1034
1035 /* Careful: convert_input() doesn't handle the NUL character.
1036 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1037 if (len > 1 && input_conv.vc_type != CONV_NONE)
1038 len = convert_input(string2, len, sizeof(string2));
1039
1040 s = string2;
1041#else
1042# ifdef FEAT_MBYTE
1043 if (input_conv.vc_type != CONV_NONE)
1044 {
1045 mch_memmove(string2, event->string, len);
1046 len = convert_input(string2, len, sizeof(string2));
1047 s = string2;
1048 }
1049 else
1050# endif
1051 s = (char_u *)event->string;
1052#endif
1053
1054 d = string;
1055 for (i = 0; i < len; ++i)
1056 {
1057 *d++ = s[i];
1058 if (d[-1] == CSI && d + 2 < string + sizeof(string))
1059 {
1060 /* Turn CSI into K_CSI. */
1061 *d++ = KS_EXTRA;
1062 *d++ = (int)KE_CSI;
1063 }
1064 }
1065 len = d - string;
1066 }
1067
1068 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1069 if (key_sym == GDK_ISO_Left_Tab)
1070 {
1071 key_sym = GDK_Tab;
1072 state |= GDK_SHIFT_MASK;
1073 }
1074
1075#ifndef HAVE_GTK2 /* for GTK+ 2, we handle this in keyval_to_string() */
1076 if ((key_sym == GDK_2 || key_sym == GDK_at) && (state & GDK_CONTROL_MASK))
1077 {
1078 string[0] = NUL; /* CTRL-2 and CTRL-@ is NUL */
1079 len = 1;
1080 }
1081 else if (len == 0 && (key_sym == GDK_space || key_sym == GDK_Tab))
1082 {
1083 /* When there are modifiers, these keys get zero length; we need the
1084 * original key here to be able to add a modifier below. */
1085 string[0] = (key_sym & 0xff);
1086 len = 1;
1087 }
1088#endif
1089
1090#ifdef FEAT_MENU
1091 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1092 * is a menu shortcut, we ignore everything with the ALT modifier. */
1093 if ((state & GDK_MOD1_MASK)
1094 && gui.menu_is_active
1095 && (*p_wak == 'y'
1096 || (*p_wak == 'm'
1097 && len == 1
1098 && gui_is_menu_shortcut(string[0]))))
1099# ifdef HAVE_GTK2
1100 /* For GTK2 we return false to signify that we haven't handled the
1101 * keypress, so that gtk will handle the mnemonic or accelerator. */
1102 return FALSE;
1103# else
1104 return TRUE;
1105# endif
1106#endif
1107
1108 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1109 * that already has the 8th bit set.
1110 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1111 * Don't do this for double-byte encodings, it turns the char into a lead
1112 * byte. */
1113 if (len == 1
1114 && (state & GDK_MOD1_MASK)
1115 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1116 && (string[0] & 0x80) == 0
1117 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
1118#ifdef FEAT_MBYTE
1119 && !enc_dbcs
1120#endif
1121 )
1122 {
1123 string[0] |= 0x80;
1124 state &= ~GDK_MOD1_MASK; /* don't use it again */
1125#ifdef FEAT_MBYTE
1126 if (enc_utf8) /* convert to utf-8 */
1127 {
1128 string[1] = string[0] & 0xbf;
1129 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1130 if (string[1] == CSI)
1131 {
1132 string[2] = KS_EXTRA;
1133 string[3] = (int)KE_CSI;
1134 len = 4;
1135 }
1136 else
1137 len = 2;
1138 }
1139#endif
1140 }
1141
1142 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1143 * value) to detect backspace, delete and keypad keys. */
1144 if (len == 0 || len == 1)
1145 {
1146 for (i = 0; special_keys[i].key_sym != 0; i++)
1147 {
1148 if (special_keys[i].key_sym == key_sym)
1149 {
1150 string[0] = CSI;
1151 string[1] = special_keys[i].code0;
1152 string[2] = special_keys[i].code1;
1153 len = -3;
1154 break;
1155 }
1156 }
1157 }
1158
1159 if (len == 0) /* Unrecognized key */
1160 return TRUE;
1161
1162#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) && !defined(HAVE_GTK2)
1163 /* Cancel or type backspace. For GTK2, im_commit_cb() does the same. */
1164 preedit_start_col = MAXCOL;
1165 xim_changed_while_preediting = TRUE;
1166#endif
1167
1168 /* Special keys (and a few others) may have modifiers. Also when using a
1169 * double-byte encoding (can't set the 8th bit). */
1170 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1171 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1172 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1173 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
1174#ifdef FEAT_MBYTE
1175 || (enc_dbcs && len == 1 && (state & GDK_MOD1_MASK))
1176#endif
1177 )
1178 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001179 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
1181 /*
1182 * For some keys a shift modifier is translated into another key
1183 * code.
1184 */
1185 if (len == -3)
1186 key = TO_SPECIAL(string[1], string[2]);
1187 else
1188 key = string[0];
1189
1190 key = simplify_key(key, &modifiers);
1191 if (key == CSI)
1192 key = K_CSI;
1193 if (IS_SPECIAL(key))
1194 {
1195 string[0] = CSI;
1196 string[1] = K_SECOND(key);
1197 string[2] = K_THIRD(key);
1198 len = 3;
1199 }
1200 else
1201 {
1202 string[0] = key;
1203 len = 1;
1204 }
1205
1206 if (modifiers != 0)
1207 {
1208 string2[0] = CSI;
1209 string2[1] = KS_MODIFIER;
1210 string2[2] = modifiers;
1211 add_to_input_buf(string2, 3);
1212 }
1213 }
1214
1215 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1216 || (string[0] == intr_char && intr_char != Ctrl_C)))
1217 {
1218 trash_input_buf();
1219 got_int = TRUE;
1220 }
1221
1222 add_to_input_buf(string, len);
1223
1224 /* blank out the pointer if necessary */
1225 if (p_mh)
1226 gui_mch_mousehide(TRUE);
1227
1228 if (gtk_main_level() > 0)
1229 gtk_main_quit();
1230
1231 return TRUE;
1232}
1233
1234#if defined(FEAT_XIM) && defined(HAVE_GTK2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001236key_release_event(GtkWidget *widget UNUSED,
1237 GdkEventKey *event,
1238 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239{
1240 /*
1241 * GTK+ 2 input methods may do fancy stuff on key release events too.
1242 * With the default IM for instance, you can enter any UCS code point
1243 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1244 */
1245 return xim_queue_key_press_event(event, FALSE);
1246}
1247#endif
1248
1249
1250/****************************************************************************
1251 * Selection handlers:
1252 */
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001255selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001257 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258{
1259 if (event->selection == clip_plus.gtk_sel_atom)
1260 clip_lose_selection(&clip_plus);
1261 else
1262 clip_lose_selection(&clip_star);
1263
1264 if (gtk_main_level() > 0)
1265 gtk_main_quit();
1266
1267 return TRUE;
1268}
1269
1270#define RS_NONE 0 /* selection_received_cb() not called yet */
1271#define RS_OK 1 /* selection_received_cb() called and OK */
1272#define RS_FAIL 2 /* selection_received_cb() called and failed */
1273static int received_selection = RS_NONE;
1274
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001276selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001278 guint time_ UNUSED,
1279 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 VimClipboard *cbd;
1282 char_u *text;
1283 char_u *tmpbuf = NULL;
1284#ifdef HAVE_GTK2
1285 guchar *tmpbuf_utf8 = NULL;
1286#endif
1287 int len;
1288 int motion_type;
1289
1290 if (data->selection == clip_plus.gtk_sel_atom)
1291 cbd = &clip_plus;
1292 else
1293 cbd = &clip_star;
1294
1295 text = (char_u *)data->data;
1296 len = data->length;
1297 motion_type = MCHAR;
1298
1299 if (text == NULL || len <= 0)
1300 {
1301 received_selection = RS_FAIL;
1302 /* clip_free_selection(cbd); ??? */
1303
1304 if (gtk_main_level() > 0)
1305 gtk_main_quit();
1306
1307 return;
1308 }
1309
1310 if (data->type == vim_atom)
1311 {
1312 motion_type = *text++;
1313 --len;
1314 }
1315
1316#ifdef FEAT_MBYTE
1317 else if (data->type == vimenc_atom)
1318 {
1319 char_u *enc;
1320 vimconv_T conv;
1321
1322 motion_type = *text++;
1323 --len;
1324
1325 enc = text;
1326 text += STRLEN(text) + 1;
1327 len -= text - enc;
1328
1329 /* If the encoding of the text is different from 'encoding', attempt
1330 * converting it. */
1331 conv.vc_type = CONV_NONE;
1332 convert_setup(&conv, enc, p_enc);
1333 if (conv.vc_type != CONV_NONE)
1334 {
1335 tmpbuf = string_convert(&conv, text, &len);
1336 if (tmpbuf != NULL)
1337 text = tmpbuf;
1338 convert_setup(&conv, NULL, NULL);
1339 }
1340 }
1341#endif
1342
1343#ifdef HAVE_GTK2
1344 /* gtk_selection_data_get_text() handles all the nasty details
1345 * and targets and encodings etc. This rocks so hard. */
1346 else
1347 {
1348 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1349 if (tmpbuf_utf8 != NULL)
1350 {
1351 len = STRLEN(tmpbuf_utf8);
1352 if (input_conv.vc_type != CONV_NONE)
1353 {
1354 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1355 if (tmpbuf != NULL)
1356 text = tmpbuf;
1357 }
1358 else
1359 text = tmpbuf_utf8;
1360 }
1361 }
1362#else /* !HAVE_GTK2 */
1363# ifdef FEAT_MBYTE
1364 else if (data->type == utf8_string_atom)
1365 {
1366 vimconv_T conv;
1367
1368 conv.vc_type = CONV_NONE;
1369 convert_setup(&conv, (char_u *)"utf-8", p_enc);
1370
1371 if (conv.vc_type != CONV_NONE)
1372 {
1373 tmpbuf = string_convert(&conv, text, &len);
1374 convert_setup(&conv, NULL, NULL);
1375 }
1376 if (tmpbuf != NULL)
1377 text = tmpbuf;
1378 }
1379# endif
1380 else if (data->type == compound_text_atom || data->type == text_atom)
1381 {
1382 char **list = NULL;
1383 int count;
1384 int i;
1385 unsigned tmplen = 0;
1386
1387 count = gdk_text_property_to_text_list(data->type, data->format,
1388 data->data, data->length,
1389 &list);
1390 for (i = 0; i < count; ++i)
1391 tmplen += strlen(list[i]);
1392
1393 tmpbuf = alloc(tmplen + 1);
1394 if (tmpbuf != NULL)
1395 {
1396 tmpbuf[0] = NUL;
1397 for (i = 0; i < count; ++i)
1398 STRCAT(tmpbuf, list[i]);
1399 text = tmpbuf;
1400 len = tmplen;
1401 }
1402
1403 if (list != NULL)
1404 gdk_free_text_list(list);
1405 }
1406#endif /* !HAVE_GTK2 */
1407
1408 clip_yank_selection(motion_type, text, (long)len, cbd);
1409 received_selection = RS_OK;
1410 vim_free(tmpbuf);
1411#ifdef HAVE_GTK2
1412 g_free(tmpbuf_utf8);
1413#endif
1414
1415 if (gtk_main_level() > 0)
1416 gtk_main_quit();
1417}
1418
1419/*
1420 * Prepare our selection data for passing it to the external selection
1421 * client.
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001424selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 GtkSelectionData *selection_data,
1426 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001427 guint time_ UNUSED,
1428 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
1430 char_u *string;
1431 char_u *tmpbuf;
1432 long_u tmplen;
1433 int length;
1434 int motion_type;
1435 GdkAtom type;
1436 VimClipboard *cbd;
1437
1438 if (selection_data->selection == clip_plus.gtk_sel_atom)
1439 cbd = &clip_plus;
1440 else
1441 cbd = &clip_star;
1442
1443 if (!cbd->owned)
1444 return; /* Shouldn't ever happen */
1445
1446 if (info != (guint)TARGET_STRING
1447#ifdef FEAT_MBYTE
1448 && info != (guint)TARGET_UTF8_STRING
1449 && info != (guint)TARGET_VIMENC
1450#endif
1451 && info != (guint)TARGET_VIM
1452 && info != (guint)TARGET_COMPOUND_TEXT
1453 && info != (guint)TARGET_TEXT)
1454 return;
1455
1456 /* get the selection from the '*'/'+' register */
1457 clip_get_selection(cbd);
1458
1459 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1460 if (motion_type < 0 || string == NULL)
1461 return;
1462 /* Due to int arguments we can't handle more than G_MAXINT. Also
1463 * reserve one extra byte for NUL or the motion type; just in case.
1464 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1465 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1466
1467 if (info == (guint)TARGET_VIM)
1468 {
1469 tmpbuf = alloc((unsigned)length + 1);
1470 if (tmpbuf != NULL)
1471 {
1472 tmpbuf[0] = motion_type;
1473 mch_memmove(tmpbuf + 1, string, (size_t)length);
1474 }
1475 /* For our own format, the first byte contains the motion type */
1476 ++length;
1477 vim_free(string);
1478 string = tmpbuf;
1479 type = vim_atom;
1480 }
1481
1482#ifdef FEAT_MBYTE
1483 else if (info == (guint)TARGET_VIMENC)
1484 {
1485 int l = STRLEN(p_enc);
1486
1487 /* contents: motion_type 'encoding' NUL text */
1488 tmpbuf = alloc((unsigned)length + l + 2);
1489 if (tmpbuf != NULL)
1490 {
1491 tmpbuf[0] = motion_type;
1492 STRCPY(tmpbuf + 1, p_enc);
1493 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1494 }
1495 length += l + 2;
1496 vim_free(string);
1497 string = tmpbuf;
1498 type = vimenc_atom;
1499 }
1500#endif
1501
1502#ifdef HAVE_GTK2
1503 /* gtk_selection_data_set_text() handles everything for us. This is
1504 * so easy and simple and cool, it'd be insane not to use it. */
1505 else
1506 {
1507 if (output_conv.vc_type != CONV_NONE)
1508 {
1509 tmpbuf = string_convert(&output_conv, string, &length);
1510 vim_free(string);
1511 if (tmpbuf == NULL)
1512 return;
1513 string = tmpbuf;
1514 }
1515 /* Validate the string to avoid runtime warnings */
1516 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1517 {
1518 gtk_selection_data_set_text(selection_data,
1519 (const char *)string, length);
1520 }
1521 vim_free(string);
1522 return;
1523 }
1524#else /* !HAVE_GTK2 */
1525# ifdef FEAT_MBYTE
1526 else if (info == (guint)TARGET_UTF8_STRING)
1527 {
1528 vimconv_T conv;
1529
1530 conv.vc_type = CONV_NONE;
1531 convert_setup(&conv, p_enc, (char_u *)"utf-8");
1532
1533 if (conv.vc_type != CONV_NONE)
1534 {
1535 tmpbuf = string_convert(&conv, string, &length);
1536 convert_setup(&conv, NULL, NULL);
1537 vim_free(string);
1538 string = tmpbuf;
1539 }
1540 type = utf8_string_atom;
1541 }
1542# endif
1543 else if (info == (guint)TARGET_COMPOUND_TEXT
1544 || info == (guint)TARGET_TEXT)
1545 {
1546 int format;
1547
1548 /* Copy the string to ensure NUL-termination */
1549 tmpbuf = vim_strnsave(string, length);
1550 vim_free(string);
1551 if (tmpbuf != NULL)
1552 {
1553 gdk_string_to_compound_text((const char *)tmpbuf,
1554 &type, &format, &string, &length);
1555 vim_free(tmpbuf);
1556 selection_data->type = type;
1557 selection_data->format = format;
1558 gtk_selection_data_set(selection_data, type, format, string, length);
1559 gdk_free_compound_text(string);
1560 }
1561 return;
1562 }
1563 else
1564 {
1565 type = GDK_TARGET_STRING;
1566 }
1567#endif /* !HAVE_GTK2 */
1568
1569 if (string != NULL)
1570 {
1571 selection_data->type = selection_data->target;
1572 selection_data->format = 8; /* 8 bits per char */
1573
1574 gtk_selection_data_set(selection_data, type, 8, string, length);
1575 vim_free(string);
1576 }
1577}
1578
1579/*
1580 * Check if the GUI can be started. Called before gvimrc is sourced.
1581 * Return OK or FAIL.
1582 */
1583 int
1584gui_mch_init_check(void)
1585{
1586#ifndef HAVE_GTK2
Bram Moolenaar49325942007-05-10 19:19:59 +00001587 /* This is needed to make the locale handling consistent between the GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 * and the rest of VIM. */
1589 gtk_set_locale();
1590#endif
1591
1592#ifdef FEAT_GUI_GNOME
1593 if (gtk_socket_id == 0)
1594 using_gnome = 1;
1595#endif
1596
1597 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1598 if (!gtk_init_check(&gui_argc, &gui_argv))
1599 {
1600 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001601 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 return FAIL;
1603 }
1604
1605 return OK;
1606}
1607
1608
1609/****************************************************************************
1610 * Mouse handling callbacks
1611 */
1612
1613
1614static guint mouse_click_timer = 0;
1615static int mouse_timed_out = TRUE;
1616
1617/*
1618 * Timer used to recognize multiple clicks of the mouse button
1619 */
1620 static gint
1621mouse_click_timer_cb(gpointer data)
1622{
1623 /* we don't use this information currently */
1624 int *timed_out = (int *) data;
1625
1626 *timed_out = TRUE;
1627 return FALSE; /* don't happen again */
1628}
1629
1630static guint motion_repeat_timer = 0;
1631static int motion_repeat_offset = FALSE;
1632static gint motion_repeat_timer_cb(gpointer);
1633
1634 static void
1635process_motion_notify(int x, int y, GdkModifierType state)
1636{
1637 int button;
1638 int_u vim_modifiers;
1639
1640 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1641 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1642 GDK_BUTTON5_MASK))
1643 ? MOUSE_DRAG : ' ';
1644
1645 /* If our pointer is currently hidden, then we should show it. */
1646 gui_mch_mousehide(FALSE);
1647
1648 /* Just moving the rodent above the drawing area without any button
1649 * being pressed. */
1650 if (button != MOUSE_DRAG)
1651 {
1652 gui_mouse_moved(x, y);
1653 return;
1654 }
1655
1656 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001657 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
Bram Moolenaar49325942007-05-10 19:19:59 +00001659 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1661
1662 if (gtk_main_level() > 0)
1663 gtk_main_quit();
1664
1665 /*
1666 * Auto repeat timer handling.
1667 */
1668 if (x < 0 || y < 0
1669 || x >= gui.drawarea->allocation.width
1670 || y >= gui.drawarea->allocation.height)
1671 {
1672
1673 int dx;
1674 int dy;
1675 int offshoot;
1676 int delay = 10;
1677
1678 /* Calculate the maximal distance of the cursor from the drawing area.
1679 * (offshoot can't become negative here!).
1680 */
1681 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1682 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1683
1684 offshoot = dx > dy ? dx : dy;
1685
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001686 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 * distance of 127 pixels from the main window.
1688 *
1689 * One could think endlessly about the most ergonomic variant here.
1690 * For example it could make sense to calculate the distance from the
1691 * drags start instead...
1692 *
1693 * Maybe a parabolic interpolation would suite us better here too...
1694 */
1695 if (offshoot > 127)
1696 {
1697 /* 5 appears to be somehow near to my perceptual limits :-). */
1698 delay = 5;
1699 }
1700 else
1701 {
1702 delay = (130 * (127 - offshoot)) / 127 + 5;
1703 }
1704
1705 /* shoot again */
1706 if (!motion_repeat_timer)
1707 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1708 motion_repeat_timer_cb, NULL);
1709 }
1710}
1711
1712/*
1713 * Timer used to recognize multiple clicks of the mouse button.
1714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001716motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
1718 int x;
1719 int y;
1720 GdkModifierType state;
1721
1722 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1723
1724 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1725 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1726 GDK_BUTTON5_MASK)))
1727 {
1728 motion_repeat_timer = 0;
1729 return FALSE;
1730 }
1731
1732 /* If there already is a mouse click in the input buffer, wait another
1733 * time (otherwise we would create a backlog of clicks) */
1734 if (vim_used_in_input_buf() > 10)
1735 return TRUE;
1736
1737 motion_repeat_timer = 0;
1738
1739 /*
1740 * Fake a motion event.
1741 * Trick: Pretend the mouse moved to the next character on every other
1742 * event, otherwise drag events will be discarded, because they are still
1743 * in the same character.
1744 */
1745 if (motion_repeat_offset)
1746 x += gui.char_width;
1747
1748 motion_repeat_offset = !motion_repeat_offset;
1749 process_motion_notify(x, y, state);
1750
1751 /* Don't happen again. We will get reinstalled in the synthetic event
1752 * if needed -- thus repeating should still work. */
1753 return FALSE;
1754}
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001757motion_notify_event(GtkWidget *widget,
1758 GdkEventMotion *event,
1759 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 if (event->is_hint)
1762 {
1763 int x;
1764 int y;
1765 GdkModifierType state;
1766
1767 gdk_window_get_pointer(widget->window, &x, &y, &state);
1768 process_motion_notify(x, y, state);
1769 }
1770 else
1771 {
1772 process_motion_notify((int)event->x, (int)event->y,
1773 (GdkModifierType)event->state);
1774 }
1775
1776 return TRUE; /* handled */
1777}
1778
1779
1780/*
1781 * Mouse button handling. Note please that we are capturing multiple click's
1782 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1783 * This is due to the way the generic VIM code is recognizing multiple clicks.
1784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001786button_press_event(GtkWidget *widget,
1787 GdkEventButton *event,
1788 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 int button;
1791 int repeated_click = FALSE;
1792 int x, y;
1793 int_u vim_modifiers;
1794
1795 /* Make sure we have focus now we've been selected */
1796 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1797 gtk_widget_grab_focus(widget);
1798
1799 /*
1800 * Don't let additional events about multiple clicks send by GTK to us
1801 * after the initial button press event confuse us.
1802 */
1803 if (event->type != GDK_BUTTON_PRESS)
1804 return FALSE;
1805
1806 x = event->x;
1807 y = event->y;
1808
1809 /* Handle multiple clicks */
1810 if (!mouse_timed_out && mouse_click_timer)
1811 {
1812 gtk_timeout_remove(mouse_click_timer);
1813 mouse_click_timer = 0;
1814 repeated_click = TRUE;
1815 }
1816
1817 mouse_timed_out = FALSE;
1818 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1819 mouse_click_timer_cb, &mouse_timed_out);
1820
1821 switch (event->button)
1822 {
1823 case 1:
1824 button = MOUSE_LEFT;
1825 break;
1826 case 2:
1827 button = MOUSE_MIDDLE;
1828 break;
1829 case 3:
1830 button = MOUSE_RIGHT;
1831 break;
1832#ifndef HAVE_GTK2
1833 case 4:
1834 button = MOUSE_4;
1835 break;
1836 case 5:
1837 button = MOUSE_5;
1838 break;
1839#endif
1840 default:
1841 return FALSE; /* Unknown button */
1842 }
1843
1844#ifdef FEAT_XIM
1845 /* cancel any preediting */
1846 if (im_is_preediting())
1847 xim_reset();
1848#endif
1849
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001850 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
1852 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1853 if (gtk_main_level() > 0)
1854 gtk_main_quit(); /* make sure the above will be handled immediately */
1855
1856 return TRUE;
1857}
1858
1859#ifdef HAVE_GTK2
1860/*
1861 * GTK+ 2 doesn't handle mouse buttons 4, 5, 6 and 7 the same way as GTK+ 1.
1862 * Instead, it abstracts scrolling via the new GdkEventScroll.
1863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001865scroll_event(GtkWidget *widget,
1866 GdkEventScroll *event,
1867 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868{
1869 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001870 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871
1872 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1873 gtk_widget_grab_focus(widget);
1874
1875 switch (event->direction)
1876 {
1877 case GDK_SCROLL_UP:
1878 button = MOUSE_4;
1879 break;
1880 case GDK_SCROLL_DOWN:
1881 button = MOUSE_5;
1882 break;
1883 default: /* We don't care about left and right... Yet. */
1884 return FALSE;
1885 }
1886
1887# ifdef FEAT_XIM
1888 /* cancel any preediting */
1889 if (im_is_preediting())
1890 xim_reset();
1891# endif
1892
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001893 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1896 FALSE, vim_modifiers);
1897
1898 if (gtk_main_level() > 0)
1899 gtk_main_quit(); /* make sure the above will be handled immediately */
1900
1901 return TRUE;
1902}
1903#endif /* HAVE_GTK2 */
1904
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001907button_release_event(GtkWidget *widget UNUSED,
1908 GdkEventButton *event,
1909 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 int x, y;
1912 int_u vim_modifiers;
1913
1914 /* Remove any motion "machine gun" timers used for automatic further
1915 extension of allocation areas if outside of the applications window
1916 area .*/
1917 if (motion_repeat_timer)
1918 {
1919 gtk_timeout_remove(motion_repeat_timer);
1920 motion_repeat_timer = 0;
1921 }
1922
1923 x = event->x;
1924 y = event->y;
1925
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001926 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
1929 if (gtk_main_level() > 0)
1930 gtk_main_quit(); /* make sure it will be handled immediately */
1931
1932 return TRUE;
1933}
1934
1935
1936#ifdef FEAT_DND
1937/****************************************************************************
1938 * Drag aNd Drop support handlers.
1939 */
1940
1941/*
1942 * Count how many items there may be and separate them with a NUL.
1943 * Apparently the items are separated with \r\n. This is not documented,
1944 * thus be careful not to go past the end. Also allow separation with
1945 * NUL characters.
1946 */
1947 static int
1948count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1949{
1950 int i;
1951 char_u *p = out;
1952 int count = 0;
1953
1954 for (i = 0; i < len; ++i)
1955 {
1956 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1957 {
1958 if (p > out && p[-1] != NUL)
1959 {
1960 ++count;
1961 *p++ = NUL;
1962 }
1963 }
1964 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1965 {
1966 *p++ = hexhex2nr(raw + i + 1);
1967 i += 2;
1968 }
1969 else
1970 *p++ = raw[i];
1971 }
1972 if (p > out && p[-1] != NUL)
1973 {
1974 *p = NUL; /* last item didn't have \r or \n */
1975 ++count;
1976 }
1977 return count;
1978}
1979
1980/*
1981 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1982 * this process, URI which protocol is not "file:" are removed. Return
1983 * length of array (less than "max").
1984 */
1985 static int
1986filter_uri_list(char_u **outlist, int max, char_u *src)
1987{
1988 int i, j;
1989
1990 for (i = j = 0; i < max; ++i)
1991 {
1992 outlist[i] = NULL;
1993 if (STRNCMP(src, "file:", 5) == 0)
1994 {
1995 src += 5;
1996 if (STRNCMP(src, "//localhost", 11) == 0)
1997 src += 11;
1998 while (src[0] == '/' && src[1] == '/')
1999 ++src;
2000 outlist[j++] = vim_strsave(src);
2001 }
2002 src += STRLEN(src) + 1;
2003 }
2004 return j;
2005}
2006
2007 static char_u **
2008parse_uri_list(int *count, char_u *data, int len)
2009{
2010 int n = 0;
2011 char_u *tmp = NULL;
2012 char_u **array = NULL;;
2013
2014 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
2015 {
2016 n = count_and_decode_uri_list(tmp, data, len);
2017 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
2018 n = filter_uri_list(array, n, tmp);
2019 }
2020 vim_free(tmp);
2021 *count = n;
2022 return array;
2023}
2024
2025 static void
2026drag_handle_uri_list(GdkDragContext *context,
2027 GtkSelectionData *data,
2028 guint time_,
2029 GdkModifierType state,
2030 gint x,
2031 gint y)
2032{
2033 char_u **fnames;
2034 int nfiles = 0;
2035
2036 fnames = parse_uri_list(&nfiles, data->data, data->length);
2037
2038 if (fnames != NULL && nfiles > 0)
2039 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002040 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
2042 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2043
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002044 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045
2046 gui_handle_drop(x, y, modifiers, fnames, nfiles);
2047 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00002048 else
2049 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050}
2051
2052 static void
2053drag_handle_text(GdkDragContext *context,
2054 GtkSelectionData *data,
2055 guint time_,
2056 GdkModifierType state)
2057{
2058 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
2059 char_u *text;
2060 int len;
2061# ifdef FEAT_MBYTE
2062 char_u *tmpbuf = NULL;
2063# endif
2064
2065 text = data->data;
2066 len = data->length;
2067
2068# ifdef FEAT_MBYTE
2069 if (data->type == utf8_string_atom)
2070 {
2071# ifdef HAVE_GTK2
2072 if (input_conv.vc_type != CONV_NONE)
2073 tmpbuf = string_convert(&input_conv, text, &len);
2074# else
2075 vimconv_T conv;
2076
2077 conv.vc_type = CONV_NONE;
2078 convert_setup(&conv, (char_u *)"utf-8", p_enc);
2079
2080 if (conv.vc_type != CONV_NONE)
2081 {
2082 tmpbuf = string_convert(&conv, text, &len);
2083 convert_setup(&conv, NULL, NULL);
2084 }
2085# endif
2086 if (tmpbuf != NULL)
2087 text = tmpbuf;
2088 }
2089# endif /* FEAT_MBYTE */
2090
2091 dnd_yank_drag_data(text, (long)len);
2092 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
2093# ifdef FEAT_MBYTE
2094 vim_free(tmpbuf);
2095# endif
2096
Bram Moolenaar19a09a12005-03-04 23:39:37 +00002097 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098
2099 if (dropkey[2] != 0)
2100 add_to_input_buf(dropkey, (int)sizeof(dropkey));
2101 else
2102 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
2103
2104 if (gtk_main_level() > 0)
2105 gtk_main_quit();
2106}
2107
2108/*
2109 * DND receiver.
2110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 static void
2112drag_data_received_cb(GtkWidget *widget,
2113 GdkDragContext *context,
2114 gint x,
2115 gint y,
2116 GtkSelectionData *data,
2117 guint info,
2118 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002119 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 GdkModifierType state;
2122
2123 /* Guard against trash */
2124 if (data->data == NULL
2125 || data->length <= 0
2126 || data->format != 8
2127 || data->data[data->length] != '\0')
2128 {
2129 gtk_drag_finish(context, FALSE, FALSE, time_);
2130 return;
2131 }
2132
2133 /* Get the current modifier state for proper distinguishment between
2134 * different operations later. */
2135 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
2136
2137 /* Not sure about the role of "text/plain" here... */
2138 if (info == (guint)TARGET_TEXT_URI_LIST)
2139 drag_handle_uri_list(context, data, time_, state, x, y);
2140 else
2141 drag_handle_text(context, data, time_, state);
2142
2143}
2144#endif /* FEAT_DND */
2145
2146
2147#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2148/*
2149 * GnomeClient interact callback. Check for unsaved buffers that cannot
2150 * be abandoned and pop up a dialog asking the user for confirmation if
2151 * necessary.
2152 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 static void
2154sm_client_check_changed_any(GnomeClient *client,
2155 gint key,
2156 GnomeDialogType type,
2157 gpointer data)
2158{
2159 cmdmod_T save_cmdmod;
2160 gboolean shutdown_cancelled;
2161
2162 save_cmdmod = cmdmod;
2163
2164# ifdef FEAT_BROWSE
2165 cmdmod.browse = TRUE;
2166# endif
2167# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2168 cmdmod.confirm = TRUE;
2169# endif
2170 /*
2171 * If there are changed buffers, present the user with
2172 * a dialog if possible, otherwise give an error message.
2173 */
2174 shutdown_cancelled = check_changed_any(FALSE);
2175
2176 exiting = FALSE;
2177 cmdmod = save_cmdmod;
2178 setcursor(); /* position the cursor */
2179 out_flush();
2180 /*
2181 * If the user hit the [Cancel] button the whole shutdown
2182 * will be cancelled. Wow, quite powerful feature (:
2183 */
2184 gnome_interaction_key_return(key, shutdown_cancelled);
2185}
2186
2187/*
2188 * Generate a script that can be used to restore the current editing session.
2189 * Save the value of v:this_session before running :mksession in order to make
2190 * automagic session save fully transparent. Return TRUE on success.
2191 */
2192 static int
2193write_session_file(char_u *filename)
2194{
2195 char_u *escaped_filename;
2196 char *mksession_cmdline;
2197 unsigned int save_ssop_flags;
2198 int failed;
2199
2200 /*
2201 * Build an ex command line to create a script that restores the current
2202 * session if executed. Escape the filename to avoid nasty surprises.
2203 */
2204 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2205 if (escaped_filename == NULL)
2206 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002207 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2208 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002210
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 /*
2212 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2213 * unpredictable effects when the session is saved automatically. Also,
2214 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2215 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2216 * enormously large if the GNOME session feature is used regularly.
2217 */
2218 save_ssop_flags = ssop_flags;
2219 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002220 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221
2222 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2223 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2224 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002225 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226
2227 ssop_flags = save_ssop_flags;
2228 g_free(mksession_cmdline);
2229 /*
2230 * Reopen the file and append a command to restore v:this_session,
2231 * as if this save never happened. This is to avoid conflicts with
2232 * the user's own sessions. FIXME: It's probably less hackish to add
2233 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2234 */
2235 if (!failed)
2236 {
2237 FILE *fd;
2238
2239 fd = open_exfile(filename, TRUE, APPENDBIN);
2240
2241 failed = (fd == NULL
2242 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2243 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2244
2245 if (fd != NULL && fclose(fd) != 0)
2246 failed = TRUE;
2247
2248 if (failed)
2249 mch_remove(filename);
2250 }
2251
2252 return !failed;
2253}
2254
2255/*
2256 * "save_yourself" signal handler. Initiate an interaction to ask the user
2257 * for confirmation if necessary. Save the current editing session and tell
2258 * the session manager how to restart Vim.
2259 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 static gboolean
2261sm_client_save_yourself(GnomeClient *client,
2262 gint phase,
2263 GnomeSaveStyle save_style,
2264 gboolean shutdown,
2265 GnomeInteractStyle interact_style,
2266 gboolean fast,
2267 gpointer data)
2268{
2269 static const char suffix[] = "-session.vim";
2270 char *session_file;
2271 unsigned int len;
2272 gboolean success;
2273
2274 /* Always request an interaction if possible. check_changed_any()
2275 * won't actually show a dialog unless any buffers have been modified.
2276 * There doesn't seem to be an obvious way to check that without
2277 * automatically firing the dialog. Anyway, it works just fine. */
2278 if (interact_style == GNOME_INTERACT_ANY)
2279 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2280 &sm_client_check_changed_any,
2281 NULL);
2282 out_flush();
2283 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2284
2285 /* The path is unique for each session save. We do neither know nor care
2286 * which session script will actually be used later. This decision is in
2287 * the domain of the session manager. */
2288 session_file = gnome_config_get_real_path(
2289 gnome_client_get_config_prefix(client));
2290 len = strlen(session_file);
2291
2292 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2293 --len; /* get rid of the superfluous trailing '/' */
2294
2295 session_file = g_renew(char, session_file, len + sizeof(suffix));
2296 memcpy(session_file + len, suffix, sizeof(suffix));
2297
2298 success = write_session_file((char_u *)session_file);
2299
2300 if (success)
2301 {
2302 const char *argv[8];
2303 int i;
2304
2305 /* Tell the session manager how to wipe out the stored session data.
2306 * This isn't as dangerous as it looks, don't worry :) session_file
2307 * is a unique absolute filename. Usually it'll be something like
2308 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2309 i = 0;
2310 argv[i++] = "rm";
2311 argv[i++] = session_file;
2312 argv[i] = NULL;
2313
2314 gnome_client_set_discard_command(client, i, (char **)argv);
2315
2316 /* Tell the session manager how to restore the just saved session.
2317 * This is easily done thanks to Vim's -S option. Pass the -f flag
2318 * since there's no need to fork -- it might even cause confusion.
2319 * Also pass the window role to give the WM something to match on.
2320 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2321 i = 0;
2322 argv[i++] = restart_command;
2323 argv[i++] = "-f";
2324 argv[i++] = "-g";
2325# ifdef HAVE_GTK2
2326 argv[i++] = "--role";
2327 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
2328# endif
2329 argv[i++] = "-S";
2330 argv[i++] = session_file;
2331 argv[i] = NULL;
2332
2333 gnome_client_set_restart_command(client, i, (char **)argv);
2334 gnome_client_set_clone_command(client, 0, NULL);
2335 }
2336
2337 g_free(session_file);
2338
2339 return success;
2340}
2341
2342/*
2343 * Called when the session manager wants us to die. There isn't much to save
2344 * here since "save_yourself" has been emitted before (unless serious trouble
2345 * is happening).
2346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 static void
2348sm_client_die(GnomeClient *client, gpointer data)
2349{
2350 /* Don't write messages to the GUI anymore */
2351 full_screen = FALSE;
2352
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002353 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002354 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002355 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 preserve_exit();
2357}
2358
2359/*
2360 * Connect our signal handlers to be notified on session save and shutdown.
2361 */
2362 static void
2363setup_save_yourself(void)
2364{
2365 GnomeClient *client;
2366
2367 client = gnome_master_client();
2368
2369 if (client != NULL)
2370 {
2371 /* Must use the deprecated gtk_signal_connect() for compatibility
2372 * with GNOME 1. Arrgh, zombies! */
2373 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2374 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2375 gtk_signal_connect(GTK_OBJECT(client), "die",
2376 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2377 }
2378}
2379
2380#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2381
2382# ifdef USE_XSMP
2383/*
2384 * GTK tells us that XSMP needs attention
2385 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 static gboolean
2387local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002388 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 GIOCondition condition;
2390 gpointer data;
2391{
2392 if (condition == G_IO_IN)
2393 {
2394 /* Do stuff; maybe close connection */
2395 if (xsmp_handle_requests() == FAIL)
2396 g_io_channel_unref((GIOChannel *)data);
2397 return TRUE;
2398 }
2399 /* Error */
2400 g_io_channel_unref((GIOChannel *)data);
2401 xsmp_close();
2402 return TRUE;
2403}
2404# endif /* USE_XSMP */
2405
2406/*
2407 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2408 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2409 */
2410 static void
2411setup_save_yourself(void)
2412{
2413 Atom *existing_atoms = NULL;
2414 int count = 0;
2415
2416#ifdef USE_XSMP
2417 if (xsmp_icefd != -1)
2418 {
2419 /*
2420 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2421 * set up GTK IO monitor
2422 */
2423 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2424
2425 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2426 local_xsmp_handle_requests, (gpointer)g_io);
2427 }
2428 else
2429#endif
2430 {
2431 /* Fall back to old method */
2432
2433 /* first get the existing value */
2434 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2435 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2436 &existing_atoms, &count))
2437 {
2438 Atom *new_atoms;
2439 Atom save_yourself_xatom;
2440 int i;
2441
2442 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2443
2444 /* check if WM_SAVE_YOURSELF isn't there yet */
2445 for (i = 0; i < count; ++i)
2446 if (existing_atoms[i] == save_yourself_xatom)
2447 break;
2448
2449 if (i == count)
2450 {
2451 /* allocate an Atoms array which is one item longer */
2452 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2453 * sizeof(Atom)));
2454 if (new_atoms != NULL)
2455 {
2456 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2457 new_atoms[count] = save_yourself_xatom;
2458 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2459 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2460 new_atoms, count + 1);
2461 vim_free(new_atoms);
2462 }
2463 }
2464 XFree(existing_atoms);
2465 }
2466 }
2467}
2468
2469# ifdef HAVE_GTK2
2470/*
2471 * Installing a global event filter seems to be the only way to catch
2472 * client messages of type WM_PROTOCOLS without overriding GDK's own
2473 * client message event filter. Well, that's still better than trying
2474 * to guess what the GDK filter had done if it had been invoked instead
2475 * (This is what we did for GTK+ 1.2, see below).
2476 *
2477 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2478 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2479 * I have the nasty feeling modern session managers just don't send this
2480 * deprecated message anymore. Addition: confirmed by several people.
2481 *
2482 * The GNOME session support is much cooler anyway. Unlike this ugly
2483 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2484 * it should work with KDE as well.
2485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002487global_event_filter(GdkXEvent *xev,
2488 GdkEvent *event UNUSED,
2489 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 XEvent *xevent = (XEvent *)xev;
2492
2493 if (xevent != NULL
2494 && xevent->type == ClientMessage
2495 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002496 && (long_u)xevent->xclient.data.l[0]
2497 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 out_flush();
2500 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2501 /*
2502 * Set the window's WM_COMMAND property, to let the window manager
2503 * know we are done saving ourselves. We don't want to be
2504 * restarted, thus set argv to NULL.
2505 */
2506 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2507 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2508 NULL, 0);
2509 return GDK_FILTER_REMOVE;
2510 }
2511
2512 return GDK_FILTER_CONTINUE;
2513}
2514
2515# else /* !HAVE_GTK2 */
2516
2517/*
2518 * GDK handler for X ClientMessage events.
2519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 static GdkFilterReturn
2521gdk_wm_protocols_filter(GdkXEvent *xev, GdkEvent *event, gpointer data)
2522{
2523 /* From example in gdkevents.c/gdk_wm_protocols_filter */
2524 XEvent *xevent = (XEvent *)xev;
2525
2526 if (xevent != NULL)
2527 {
2528 if (xevent->xclient.data.l[0] == GET_X_ATOM(save_yourself_atom))
2529 {
2530 out_flush();
2531 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2532
2533 /* Set the window's WM_COMMAND property, to let the window manager
2534 * know we are done saving ourselves. We don't want to be
2535 * restarted, thus set argv to NULL. */
2536 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2537 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2538 NULL, 0);
2539 }
2540 /*
2541 * Functionality from gdkevents.c/gdk_wm_protocols_filter;
2542 * Registering this filter apparently overrides the default GDK one,
2543 * so we need to perform its functionality. There seems no way to
2544 * register for WM_PROTOCOLS, and only process the WM_SAVE_YOURSELF
2545 * bit; it's all or nothing. Update: No, there is a way -- but it
2546 * only works with GTK+ 2 apparently. See above.
2547 */
2548 else if (xevent->xclient.data.l[0] == GET_X_ATOM(gdk_wm_delete_window))
2549 {
2550 event->any.type = GDK_DELETE;
2551 return GDK_FILTER_TRANSLATE;
2552 }
2553 }
2554
2555 return GDK_FILTER_REMOVE;
2556}
2557# endif /* !HAVE_GTK2 */
2558
2559#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2560
2561
2562/*
2563 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002566mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568/* If you get an error message here, you still need to unpack the runtime
2569 * archive! */
2570#ifdef magick
2571# undef magick
2572#endif
2573#ifdef HAVE_GTK2
2574 /* A bit hackish, but avoids casting later and allows optimization */
2575# define static static const
2576#endif
2577#define magick vim32x32
2578#include "../runtime/vim32x32.xpm"
2579#undef magick
2580#define magick vim16x16
2581#include "../runtime/vim16x16.xpm"
2582#undef magick
2583#define magick vim48x48
2584#include "../runtime/vim48x48.xpm"
2585#undef magick
2586#ifdef HAVE_GTK2
2587# undef static
2588#endif
2589
2590 /* When started with "--echo-wid" argument, write window ID on stdout. */
2591 if (echo_wid_arg)
2592 {
2593 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2594 fflush(stdout);
2595 }
2596
2597 if (vim_strchr(p_go, GO_ICON) != NULL)
2598 {
2599 /*
2600 * Add an icon to the main window. For fun and convenience of the user.
2601 */
2602#ifdef HAVE_GTK2
2603 GList *icons = NULL;
2604
2605 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2606 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2607 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2608
2609 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2610
2611 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2612 g_list_free(icons);
2613
2614#else /* !HAVE_GTK2 */
2615
2616 GdkPixmap *icon;
2617 GdkBitmap *icon_mask = NULL;
2618 char **magick = vim32x32;
2619 Display *xdisplay;
2620 Window root_window;
2621 XIconSize *size;
2622 int number_sizes;
2623 /*
2624 * Adjust the icon to the preferences of the actual window manager.
Bram Moolenaar49325942007-05-10 19:19:59 +00002625 * This is once again a workaround for a deficiency in GTK+ 1.2.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 */
2627 xdisplay = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
2628 root_window = XRootWindow(xdisplay, DefaultScreen(xdisplay));
2629 if (XGetIconSizes(xdisplay, root_window, &size, &number_sizes))
2630 {
2631 if (number_sizes > 0)
2632 {
2633 if (size->max_height >= 48 && size->max_height >= 48)
2634 magick = vim48x48;
2635 else if (size->max_height >= 32 && size->max_height >= 32)
2636 magick = vim32x32;
2637 else if (size->max_height >= 16 && size->max_height >= 16)
2638 magick = vim16x16;
2639 }
2640 XFree(size);
2641 }
2642 icon = gdk_pixmap_create_from_xpm_d(gui.mainwin->window,
2643 &icon_mask, NULL, magick);
2644 if (icon != NULL)
2645 /* Note: for some reason gdk_window_set_icon() doesn't acquire
2646 * a reference on the pixmap, thus we _have_ to leak it. */
2647 gdk_window_set_icon(gui.mainwin->window, NULL, icon, icon_mask);
2648
2649#endif /* !HAVE_GTK2 */
2650 }
2651
2652#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2653 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
2654# ifdef HAVE_GTK2
2655 gdk_window_add_filter(NULL, &global_event_filter, NULL);
2656# else
2657 gdk_add_client_message_filter(wm_protocols_atom,
2658 &gdk_wm_protocols_filter, NULL);
2659# endif
2660#endif
2661 /* Setup to indicate to the window manager that we want to catch the
2662 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2663 * manager instead. */
2664#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2665 if (using_gnome)
2666#endif
2667 setup_save_yourself();
2668
2669#ifdef FEAT_CLIENTSERVER
2670 if (serverName == NULL && serverDelayedStartName != NULL)
2671 {
2672 /* This is a :gui command in a plain vim with no previous server */
2673 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2674
2675 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2676 serverDelayedStartName);
2677 }
2678 else
2679 {
2680 /*
2681 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2682 * have to change the "server" registration to that of the main window
2683 * If we have not registered a name yet, remember the window
2684 */
2685 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2686 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2687 }
2688 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2689 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2690 GTK_SIGNAL_FUNC(property_event), NULL);
2691#endif
2692}
2693
2694 static GdkCursor *
2695create_blank_pointer(void)
2696{
2697 GdkWindow *root_window = NULL;
2698 GdkPixmap *blank_mask;
2699 GdkCursor *cursor;
2700 GdkColor color = { 0, 0, 0, 0 };
2701 char blank_data[] = { 0x0 };
2702
2703#ifdef HAVE_GTK_MULTIHEAD
2704 root_window = gtk_widget_get_root_window(gui.mainwin);
2705#endif
2706
2707 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2708 * in size. */
2709 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2710 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2711 &color, &color, 0, 0);
2712 gdk_bitmap_unref(blank_mask);
2713
2714 return cursor;
2715}
2716
2717#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 static void
2719mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002720 GdkScreen *previous_screen UNUSED,
2721 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722{
2723 if (!gtk_widget_has_screen(widget))
2724 return;
2725
2726 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002727 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 */
2729 if (gui.blank_pointer != NULL)
2730 gdk_cursor_unref(gui.blank_pointer);
2731
2732 gui.blank_pointer = create_blank_pointer();
2733
2734 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2735 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2736
2737 /*
2738 * Create a new PangoContext for this screen, and initialize it
2739 * with the current font if necessary.
2740 */
2741 if (gui.text_context != NULL)
2742 g_object_unref(gui.text_context);
2743
2744 gui.text_context = gtk_widget_create_pango_context(widget);
2745 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2746
2747 if (gui.norm_font != NULL)
2748 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002749 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002750 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752}
2753#endif /* HAVE_GTK_MULTIHEAD */
2754
2755/*
2756 * After the drawing area comes up, we calculate all colors and create the
2757 * dummy blank cursor.
2758 *
2759 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2760 * fact that the main VIM engine doesn't take them into account anywhere.
2761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002763drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764{
2765 GtkWidget *sbar;
2766
2767#ifdef FEAT_XIM
2768 xim_init();
2769#endif
2770 gui_mch_new_colors();
2771 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2772
2773 gui.blank_pointer = create_blank_pointer();
2774 if (gui.pointer_hidden)
2775 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2776
2777 /* get the actual size of the scrollbars, if they are realized */
2778 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2779 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2780 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2781 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2782 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2783 gui.scrollbar_width = sbar->allocation.width;
2784
2785 sbar = gui.bottom_sbar.id;
2786 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2787 gui.scrollbar_height = sbar->allocation.height;
2788}
2789
2790/*
2791 * Properly clean up on shutdown.
2792 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002794drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 /* Don't write messages to the GUI anymore */
2797 full_screen = FALSE;
2798
2799#ifdef FEAT_XIM
2800 im_shutdown();
2801#endif
2802#ifdef HAVE_GTK2
2803 if (gui.ascii_glyphs != NULL)
2804 {
2805 pango_glyph_string_free(gui.ascii_glyphs);
2806 gui.ascii_glyphs = NULL;
2807 }
2808 if (gui.ascii_font != NULL)
2809 {
2810 g_object_unref(gui.ascii_font);
2811 gui.ascii_font = NULL;
2812 }
2813 g_object_unref(gui.text_context);
2814 gui.text_context = NULL;
2815
2816 g_object_unref(gui.text_gc);
2817 gui.text_gc = NULL;
2818
2819 gdk_cursor_unref(gui.blank_pointer);
2820 gui.blank_pointer = NULL;
2821#else
2822 gdk_gc_unref(gui.text_gc);
2823 gui.text_gc = NULL;
2824
2825 gdk_cursor_destroy(gui.blank_pointer);
2826 gui.blank_pointer = NULL;
2827#endif
2828}
2829
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002831drawarea_style_set_cb(GtkWidget *widget UNUSED,
2832 GtkStyle *previous_style UNUSED,
2833 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 gui_mch_new_colors();
2836}
2837
2838/*
2839 * Callback routine for the "delete_event" signal on the toplevel window.
2840 * Tries to vim gracefully, or refuses to exit with changed buffers.
2841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002843delete_event_cb(GtkWidget *widget UNUSED,
2844 GdkEventAny *event UNUSED,
2845 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846{
2847 gui_shell_closed();
2848 return TRUE;
2849}
2850
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002851#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2852 static int
2853get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2854{
2855 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2856
2857#ifdef FEAT_GUI_GNOME
2858 if (using_gnome && widget != NULL)
2859 {
2860# ifdef HAVE_GTK2
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002861 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002862 BonoboDockItem *dockitem;
2863
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002864 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002865 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2866 {
2867 /* Only menu & toolbar are dock items. Could tabline be?
2868 * Seem to be only the 2 defined in GNOME */
2869 widget = parent;
2870 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002871
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002872 if (dockitem == NULL || dockitem->is_floating)
2873 return 0;
2874 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2875 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002876# else
2877 GnomeDockItem *dockitem;
2878
2879 widget = widget->parent;
2880 dockitem = GNOME_DOCK_ITEM(widget);
2881
2882 if (dockitem == NULL || dockitem->is_floating)
2883 return 0;
2884 item_orientation = gnome_dock_item_get_orientation(dockitem);
2885# endif
2886 }
2887#endif
2888 if (widget != NULL
2889 && item_orientation == orientation
2890 && GTK_WIDGET_REALIZED(widget)
2891 && GTK_WIDGET_VISIBLE(widget))
2892 {
2893 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2894 return widget->allocation.height;
2895 else
2896 return widget->allocation.width;
2897 }
2898 return 0;
2899}
2900#endif
2901
2902 static int
2903get_menu_tool_width(void)
2904{
2905 int width = 0;
2906
2907#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2908# ifdef FEAT_MENU
2909 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2910# endif
2911# ifdef FEAT_TOOLBAR
2912 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2913# endif
2914# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002915 if (gui.tabline != NULL)
2916 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002917# endif
2918#endif
2919
2920 return width;
2921}
2922
2923 static int
2924get_menu_tool_height(void)
2925{
2926 int height = 0;
2927
2928#ifdef FEAT_MENU
2929 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2930#endif
2931#ifdef FEAT_TOOLBAR
2932 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2933#endif
2934#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002935 if (gui.tabline != NULL)
2936 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002937#endif
2938
2939 return height;
2940}
2941
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002942/* This controls whether we can set the real window hints at
2943 * start-up when in a GtkPlug.
2944 * 0 = normal processing (default)
2945 * 1 = init. hints set, no-one's tried to reset since last check
2946 * 2 = init. hints set, attempt made to change hints
2947 */
2948static int init_window_hints_state = 0;
2949
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002950 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002951update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002952{
2953 static int old_width = 0;
2954 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002955 static int old_min_width = 0;
2956 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002957 static int old_char_width = 0;
2958 static int old_char_height = 0;
2959
2960 int width;
2961 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002962 int min_width;
2963 int min_height;
2964
2965 /* At start-up, don't try to set the hints until the initial
2966 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002967 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002968 */
2969 if (!(force_width && force_height) && init_window_hints_state > 0)
2970 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002971 /* Don't do it! */
2972 init_window_hints_state = 2;
2973 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002974 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002975
2976 /* This also needs to be done when the main window isn't there yet,
2977 * otherwise the hints don't work. */
2978 width = gui_get_base_width();
2979 height = gui_get_base_height();
2980# ifdef FEAT_MENU
2981 height += tabline_height() * gui.char_height;
2982# endif
2983# ifdef HAVE_GTK2
2984 width += get_menu_tool_width();
2985 height += get_menu_tool_height();
2986# endif
2987
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002988 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002989 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002990 * 'set columns=' or init. -geom) we briefly set the min. to the size
2991 * we wish to be instead of the legitimate minimum so that we actually
2992 * resize correctly.
2993 */
2994 if (force_width && force_height)
2995 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002996 min_width = force_width;
2997 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002998 }
2999 else
3000 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003001 min_width = width + MIN_COLUMNS * gui.char_width;
3002 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003003 }
3004
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003005 /* Avoid an expose event when the size didn't change. */
3006 if (width != old_width
3007 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003008 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003009 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003010 || gui.char_width != old_char_width
3011 || gui.char_height != old_char_height)
3012 {
3013 GdkGeometry geometry;
3014 GdkWindowHints geometry_mask;
3015
3016 geometry.width_inc = gui.char_width;
3017 geometry.height_inc = gui.char_height;
3018 geometry.base_width = width;
3019 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003020 geometry.min_width = min_width;
3021 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003022 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
3023 |GDK_HINT_MIN_SIZE;
3024# ifdef HAVE_GTK2
3025 /* Using gui.formwin as geometry widget doesn't work as expected
3026 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3027 * in Vim confuse GTK+. */
3028 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
3029 &geometry, geometry_mask);
3030# else
3031 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.formwin,
3032 &geometry, geometry_mask);
3033# endif
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003034 old_width = width;
3035 old_height = height;
3036 old_min_width = min_width;
3037 old_min_height = min_height;
3038 old_char_width = gui.char_width;
3039 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003040 }
3041}
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043#ifdef FEAT_TOOLBAR
3044
3045# ifdef HAVE_GTK2
3046/*
3047 * This extra effort wouldn't be necessary if we only used stock icons in the
3048 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3049 * shouldn't be treated differently, thus we do need this.
3050 */
3051 static void
3052icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
3053{
3054 if (GTK_IS_IMAGE(widget))
3055 {
3056 GtkImage *image = (GtkImage *)widget;
3057
3058 /* User-defined icons are stored in a GtkIconSet */
3059 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
3060 {
3061 GtkIconSet *icon_set;
3062 GtkIconSize icon_size;
3063
3064 gtk_image_get_icon_set(image, &icon_set, &icon_size);
3065 icon_size = (GtkIconSize)(long)user_data;
3066
3067 gtk_icon_set_ref(icon_set);
3068 gtk_image_set_from_icon_set(image, icon_set, icon_size);
3069 gtk_icon_set_unref(icon_set);
3070 }
3071 }
3072 else if (GTK_IS_CONTAINER(widget))
3073 {
3074 gtk_container_foreach((GtkContainer *)widget,
3075 &icon_size_changed_foreach,
3076 user_data);
3077 }
3078}
3079# endif /* HAVE_GTK2 */
3080
3081 static void
3082set_toolbar_style(GtkToolbar *toolbar)
3083{
3084 GtkToolbarStyle style;
3085# ifdef HAVE_GTK2
3086 GtkIconSize size;
3087 GtkIconSize oldsize;
3088# endif
3089
3090# ifdef HAVE_GTK2
3091 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3092 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
3093 style = GTK_TOOLBAR_BOTH_HORIZ;
3094 else
3095# endif
3096 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
3097 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
3098 style = GTK_TOOLBAR_BOTH;
3099 else if (toolbar_flags & TOOLBAR_TEXT)
3100 style = GTK_TOOLBAR_TEXT;
3101 else
3102 style = GTK_TOOLBAR_ICONS;
3103
3104 gtk_toolbar_set_style(toolbar, style);
3105 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
3106
3107# ifdef HAVE_GTK2
3108 switch (tbis_flags)
3109 {
3110 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
3111 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
3112 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
3113 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
3114 default: size = GTK_ICON_SIZE_INVALID; break;
3115 }
3116 oldsize = gtk_toolbar_get_icon_size(toolbar);
3117
3118 if (size == GTK_ICON_SIZE_INVALID)
3119 {
3120 /* Let global user preferences decide the icon size. */
3121 gtk_toolbar_unset_icon_size(toolbar);
3122 size = gtk_toolbar_get_icon_size(toolbar);
3123 }
3124 if (size != oldsize)
3125 {
3126 gtk_container_foreach(GTK_CONTAINER(toolbar),
3127 &icon_size_changed_foreach,
3128 GINT_TO_POINTER((int)size));
3129 }
3130 gtk_toolbar_set_icon_size(toolbar, size);
3131# endif
3132}
3133
3134#endif /* FEAT_TOOLBAR */
3135
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003136#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3137static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00003138static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003139static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00003140static int clicked_page; /* page clicked in tab line */
3141
3142/*
3143 * Handle selecting an item in the tab line popup menu.
3144 */
Bram Moolenaara5621492006-02-25 21:55:24 +00003145 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003146tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00003147{
Bram Moolenaara5621492006-02-25 21:55:24 +00003148 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00003149 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00003150
3151 if (gtk_main_level() > 0)
3152 gtk_main_quit();
3153}
3154
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003155 static void
3156add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
3157{
3158 GtkWidget *item;
3159 char_u *utf_text;
3160
3161 utf_text = CONVERT_TO_UTF8(text);
3162 item = gtk_menu_item_new_with_label((const char *)utf_text);
3163 gtk_widget_show(item);
3164 CONVERT_TO_UTF8_FREE(utf_text);
3165
3166 gtk_container_add(GTK_CONTAINER(menu), item);
3167 gtk_signal_connect(GTK_OBJECT(item), "activate",
3168 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003169 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003170}
3171
Bram Moolenaara5621492006-02-25 21:55:24 +00003172/*
Bram Moolenaara5621492006-02-25 21:55:24 +00003173 * Create a menu for the tab line.
3174 */
3175 static GtkWidget *
3176create_tabline_menu(void)
3177{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003178 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00003179
3180 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003181 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
3182 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
3183 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00003184
3185 return menu;
3186}
3187
3188 static gboolean
3189on_tabline_menu(GtkWidget *widget, GdkEvent *event)
3190{
3191 /* Was this button press event ? */
3192 if (event->type == GDK_BUTTON_PRESS)
3193 {
3194 GdkEventButton *bevent = (GdkEventButton *)event;
3195 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003196 int y = bevent->y;
3197 GtkWidget *tabwidget;
3198 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00003199
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003200 /* When ignoring events return TRUE so that the selected page doesn't
3201 * change. */
3202 if (hold_gui_events
3203# ifdef FEAT_CMDWIN
3204 || cmdwin_type != 0
3205# endif
3206 )
3207 return TRUE;
3208
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003209 tabwin = gdk_window_at_pointer(&x, &y);
3210 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
3211 clicked_page = (int)(long)gtk_object_get_user_data(
3212 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00003213
3214 /* If the event was generated for 3rd button popup the menu. */
3215 if (bevent->button == 3)
3216 {
3217 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
3218 bevent->button, bevent->time);
3219 /* We handled the event. */
3220 return TRUE;
3221 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003222 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003223 {
Bram Moolenaar96351572006-05-05 21:16:59 +00003224 if (clicked_page == 0)
3225 {
Bram Moolenaar07354542007-09-15 12:07:46 +00003226 /* Click after all tabs moves to next tab page. When "x" is
3227 * small guess it's the left button. */
3228 if (send_tabline_event(x < 50 ? -1 : 0) && gtk_main_level() > 0)
Bram Moolenaar96351572006-05-05 21:16:59 +00003229 gtk_main_quit();
3230 }
3231#ifndef HAVE_GTK2
3232 else
3233 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline),
3234 clicked_page - 1);
3235#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003236 }
Bram Moolenaara5621492006-02-25 21:55:24 +00003237 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003238
Bram Moolenaara5621492006-02-25 21:55:24 +00003239 /* We didn't handle the event. */
3240 return FALSE;
3241}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003242
3243/*
3244 * Handle selecting one of the tabs.
3245 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003246 static void
3247on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003248 GtkNotebook *notebook UNUSED,
3249 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00003250 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003251 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003253 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003254 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00003255 if (send_tabline_event(idx + 1) && gtk_main_level() > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00003256 gtk_main_quit();
3257 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003258}
3259
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003260#ifndef HAVE_GTK2
3261static int showing_tabline = 0;
3262#endif
3263
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003264/*
3265 * Show or hide the tabline.
3266 */
3267 void
3268gui_mch_show_tabline(int showit)
3269{
3270 if (gui.tabline == NULL)
3271 return;
3272
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003273#ifdef HAVE_GTK2
3274 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003275 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003276#else
3277 if (!showit != !showing_tabline)
3278#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003279 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003280 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003281 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003282 update_window_manager_hints(0, 0);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003283#ifndef HAVE_GTK2
3284 showing_tabline = showit;
3285#endif
Bram Moolenaar96351572006-05-05 21:16:59 +00003286 if (showit)
3287 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003288 }
Bram Moolenaar96351572006-05-05 21:16:59 +00003289
3290 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003291}
3292
3293/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003294 * Return TRUE when tabline is displayed.
3295 */
3296 int
3297gui_mch_showing_tabline(void)
3298{
3299 return gui.tabline != NULL
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003300#ifdef HAVE_GTK2
3301 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3302 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline))
3303#else
3304 && showing_tabline
3305#endif
3306 ;
Bram Moolenaar3517bb12006-03-03 22:58:45 +00003307}
3308
3309/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003310 * Update the labels of the tabline.
3311 */
3312 void
3313gui_mch_update_tabline(void)
3314{
3315 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003316 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003317 GtkWidget *label;
3318 tabpage_T *tp;
3319 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003320 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003321 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003322 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003323
3324 if (gui.tabline == NULL)
3325 return;
3326
3327 ignore_tabline_evt = TRUE;
3328
3329 /* Add a label for each tab page. They all contain the same text area. */
3330 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3331 {
3332 if (tp == curtab)
3333 curtabidx = nr;
3334
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003335 tab_num = nr + 1;
3336
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003337 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3338 if (page == NULL)
3339 {
3340 /* Add notebook page */
3341 page = gtk_vbox_new(FALSE, 0);
3342 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003343 event_box = gtk_event_box_new();
3344 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003345 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003346 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003347 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003348 gtk_widget_show(label);
3349 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3350 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003351 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003352 nr++);
3353 }
3354
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003355 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003356 gtk_object_set_user_data(GTK_OBJECT(event_box),
3357 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003358 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003359 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003360 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003361 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003362 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003363
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003364 get_tabline_label(tp, TRUE);
3365 labeltext = CONVERT_TO_UTF8(NameBuff);
3366 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3367 (const char *)labeltext, NULL);
3368 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003369 }
3370
3371 /* Remove any old labels. */
3372 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3373 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3374
3375 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003376 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003377
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003378 /* Make sure everything is in place before drawing text. */
3379 gui_mch_update();
3380
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003381 ignore_tabline_evt = FALSE;
3382}
3383
3384/*
3385 * Set the current tab to "nr". First tab is 1.
3386 */
3387 void
3388gui_mch_set_curtab(nr)
3389 int nr;
3390{
3391 if (gui.tabline == NULL)
3392 return;
3393
3394 ignore_tabline_evt = TRUE;
3395 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003396 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003397 ignore_tabline_evt = FALSE;
3398}
3399
3400#endif /* FEAT_GUI_TABLINE */
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402/*
3403 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3404 * Returns OK for success, FAIL when the GUI can't be started.
3405 */
3406 int
3407gui_mch_init(void)
3408{
3409 GtkWidget *vbox;
3410
3411#ifdef FEAT_GUI_GNOME
3412 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3413 * exits on failure, but that's a non-issue because we already called
3414 * gtk_init_check() in gui_mch_init_check(). */
3415 if (using_gnome)
3416# ifdef HAVE_GTK2
3417 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3418 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
3419# else
3420 gnome_init(VIMPACKAGE, VIM_VERSION_SHORT, gui_argc, gui_argv);
3421# endif
3422#endif
3423 vim_free(gui_argv);
3424 gui_argv = NULL;
3425
3426#ifdef HAVE_GTK2
3427# if GLIB_CHECK_VERSION(2,1,3)
3428 /* Set the human-readable application name */
3429 g_set_application_name("Vim");
3430# endif
3431 /*
3432 * Force UTF-8 output no matter what the value of 'encoding' is.
3433 * did_set_string_option() in option.c prohibits changing 'termencoding'
3434 * to something else than UTF-8 if the GUI is in use.
3435 */
3436 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3437
3438# ifdef FEAT_TOOLBAR
3439 gui_gtk_register_stock_icons();
3440# endif
3441 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3442 * The hard part is deciding install locations and the Makefile magic. */
3443# if 0
3444 gtk_rc_parse("gtkrc");
3445# endif
3446#endif
3447
3448 /* Initialize values */
3449 gui.border_width = 2;
3450 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3451 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003452 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003454 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003456 /* LINTED: avoid warning: conversion to 'unsigned long' */
3457 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 /* Initialise atoms */
3460#ifdef FEAT_MBYTE
3461 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
3462#endif
3463#ifndef HAVE_GTK2
3464 compound_text_atom = gdk_atom_intern("COMPOUND_TEXT", FALSE);
3465 text_atom = gdk_atom_intern("TEXT", FALSE);
3466#endif
3467
3468 /* Set default foreground and background colors. */
3469 gui.norm_pixel = gui.def_norm_pixel;
3470 gui.back_pixel = gui.def_back_pixel;
3471
3472 if (gtk_socket_id != 0)
3473 {
3474 GtkWidget *plug;
3475
3476 /* Use GtkSocket from another app. */
3477#ifdef HAVE_GTK_MULTIHEAD
3478 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3479 gtk_socket_id);
3480#else
3481 plug = gtk_plug_new(gtk_socket_id);
3482#endif
3483 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3484 {
3485 gui.mainwin = plug;
3486 }
3487 else
3488 {
3489 g_warning("Connection to GTK+ socket (ID %u) failed",
3490 (unsigned int)gtk_socket_id);
3491 /* Pretend we never wanted it if it failed (get own window) */
3492 gtk_socket_id = 0;
3493 }
3494 }
3495
3496 if (gtk_socket_id == 0)
3497 {
3498#ifdef FEAT_GUI_GNOME
3499 if (using_gnome)
3500 {
3501 gui.mainwin = gnome_app_new("Vim", NULL);
3502# ifdef USE_XSMP
3503 /* Use the GNOME save-yourself functionality now. */
3504 xsmp_close();
3505# endif
3506 }
3507 else
3508#endif
3509 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3510 }
3511
3512 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3513
3514#ifdef HAVE_GTK2
3515 /* Create the PangoContext used for drawing all text. */
3516 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3517 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
3518#endif
3519
3520#ifndef HAVE_GTK2
3521 gtk_window_set_policy(GTK_WINDOW(gui.mainwin), TRUE, TRUE, TRUE);
3522#endif
3523 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3524 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3525
3526 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3527 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3528
3529 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3530 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3531#ifdef HAVE_GTK_MULTIHEAD
3532 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3533 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3534#endif
3535#ifdef HAVE_GTK2
3536 gui.accel_group = gtk_accel_group_new();
3537 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
3538#else
3539 gui.accel_group = gtk_accel_group_get_default();
3540#endif
3541
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003542 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 vbox = gtk_vbox_new(FALSE, 0);
3544
3545#ifdef FEAT_GUI_GNOME
3546 if (using_gnome)
3547 {
3548# if defined(HAVE_GTK2) && defined(FEAT_MENU)
3549 /* automagically restore menubar/toolbar placement */
3550 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3551# endif
3552 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3553 }
3554 else
3555#endif
3556 {
3557 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3558 gtk_widget_show(vbox);
3559 }
3560
3561#ifdef FEAT_MENU
3562 /*
3563 * Create the menubar and handle
3564 */
3565 gui.menubar = gtk_menu_bar_new();
3566 gtk_widget_set_name(gui.menubar, "vim-menubar");
3567
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003568# ifdef HAVE_GTK2
3569 /* Avoid that GTK takes <F10> away from us. */
3570 {
3571 GtkSettings *gtk_settings;
3572
3573 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3574 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3575 }
3576# endif
3577
3578
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579# ifdef FEAT_GUI_GNOME
3580 if (using_gnome)
3581 {
3582# ifdef HAVE_GTK2
3583 BonoboDockItem *dockitem;
3584
3585 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3586 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3587 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003588 /* We don't want the menu to float. */
3589 bonobo_dock_item_set_behavior(dockitem,
3590 bonobo_dock_item_get_behavior(dockitem)
3591 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 gui.menubar_h = GTK_WIDGET(dockitem);
3593# else
3594 gui.menubar_h = gnome_dock_item_new("VimMainMenu",
3595 GNOME_DOCK_ITEM_BEH_EXCLUSIVE |
3596 GNOME_DOCK_ITEM_BEH_NEVER_VERTICAL);
3597 gtk_container_add(GTK_CONTAINER(gui.menubar_h), gui.menubar);
3598
3599 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3600 GNOME_DOCK_ITEM(gui.menubar_h),
3601 GNOME_DOCK_TOP, /* placement */
3602 1, /* band_num */
3603 0, /* band_position */
3604 0, /* offset */
3605 TRUE);
3606 gtk_widget_show(gui.menubar);
3607# endif
3608 }
3609 else
3610# endif /* FEAT_GUI_GNOME */
3611 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003612 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3613 * disabled in gui_init() later. */
3614 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3616 }
3617#endif /* FEAT_MENU */
3618
3619#ifdef FEAT_TOOLBAR
3620 /*
3621 * Create the toolbar and handle
3622 */
3623# ifdef HAVE_GTK2
3624 /* some aesthetics on the toolbar */
3625 gtk_rc_parse_string(
3626 "style \"vim-toolbar-style\" {\n"
3627 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3628 "}\n"
3629 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3630 gui.toolbar = gtk_toolbar_new();
3631 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
3632# else
3633 gui.toolbar = gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL,
3634 GTK_TOOLBAR_ICONS);
3635 gtk_toolbar_set_button_relief(GTK_TOOLBAR(gui.toolbar), GTK_RELIEF_NONE);
3636# endif
3637 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3638
3639# ifdef FEAT_GUI_GNOME
3640 if (using_gnome)
3641 {
3642# ifdef HAVE_GTK2
3643 BonoboDockItem *dockitem;
3644
3645 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3646 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3647 GNOME_APP_TOOLBAR_NAME);
3648 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003649 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003650 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003651 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003652 bonobo_dock_item_get_behavior(dockitem)
3653 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
3655# else
3656 GtkWidget *dockitem;
3657
3658 dockitem = gnome_dock_item_new("VimToolBar",
3659 GNOME_DOCK_ITEM_BEH_EXCLUSIVE);
3660 gtk_container_add(GTK_CONTAINER(dockitem), GTK_WIDGET(gui.toolbar));
3661 gui.toolbar_h = dockitem;
3662
3663 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui.mainwin)->dock),
3664 GNOME_DOCK_ITEM(dockitem),
3665 GNOME_DOCK_TOP, /* placement */
3666 1, /* band_num */
3667 1, /* band_position */
3668 0, /* offset */
3669 TRUE);
3670 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 2);
3671 gtk_widget_show(gui.toolbar);
3672# endif
3673 }
3674 else
3675# endif /* FEAT_GUI_GNOME */
3676 {
3677# ifndef HAVE_GTK2
3678 gtk_container_border_width(GTK_CONTAINER(gui.toolbar), 1);
3679# endif
3680 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3681 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3682 gtk_widget_show(gui.toolbar);
3683 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3684 }
3685#endif /* FEAT_TOOLBAR */
3686
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003687#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003688 /*
3689 * Use a Notebook for the tab pages labels. The labels are hidden by
3690 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003691 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003692 gui.tabline = gtk_notebook_new();
3693 gtk_widget_show(gui.tabline);
3694 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3695 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3696 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003697 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003698 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003699
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003700 tabline_tooltip = gtk_tooltips_new();
3701 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3702
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003703 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003704 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003705
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003706 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003707 page = gtk_vbox_new(FALSE, 0);
3708 gtk_widget_show(page);
3709 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3710 label = gtk_label_new("-Empty-");
3711 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003712 event_box = gtk_event_box_new();
3713 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003714 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003715 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003716 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003717 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003718 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003719
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003720 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003721 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003722
3723 /* Create a popup menu for the tab line and connect it. */
3724 tabline_menu = create_tabline_menu();
3725 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003726 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003727#endif
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 gui.formwin = gtk_form_new();
3730 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3731 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3732
3733 gui.drawarea = gtk_drawing_area_new();
3734
3735 /* Determine which events we will filter. */
3736 gtk_widget_set_events(gui.drawarea,
3737 GDK_EXPOSURE_MASK |
3738 GDK_ENTER_NOTIFY_MASK |
3739 GDK_LEAVE_NOTIFY_MASK |
3740 GDK_BUTTON_PRESS_MASK |
3741 GDK_BUTTON_RELEASE_MASK |
3742#ifdef HAVE_GTK2
3743 GDK_SCROLL_MASK |
3744#endif
3745 GDK_KEY_PRESS_MASK |
3746 GDK_KEY_RELEASE_MASK |
3747 GDK_POINTER_MOTION_MASK |
3748 GDK_POINTER_MOTION_HINT_MASK);
3749
3750 gtk_widget_show(gui.drawarea);
3751 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3752 gtk_widget_show(gui.formwin);
3753 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3754
3755 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3756 * and not the window. */
3757 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3758 : GTK_OBJECT(gui.drawarea),
3759 "key_press_event",
3760 GTK_SIGNAL_FUNC(key_press_event), NULL);
3761#if defined(FEAT_XIM) && defined(HAVE_GTK2)
3762 /* Also forward key release events for the benefit of GTK+ 2 input
3763 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3764 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3765 : G_OBJECT(gui.drawarea),
3766 "key_release_event",
3767 G_CALLBACK(&key_release_event), NULL);
3768#endif
3769 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3770 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3771 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3772 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3773
3774 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3775 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3776
3777 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3778
3779#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3780 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3781 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3782#endif
3783
3784 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003785 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3787
3788 /*
3789 * Set clipboard specific atoms
3790 */
3791 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
3792#ifdef FEAT_MBYTE
3793 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
3794#endif
3795 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3796 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3797
3798 /*
3799 * Start out by adding the configured border width into the border offset.
3800 */
3801 gui.border_offset = gui.border_width;
3802
3803 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3804 GTK_SIGNAL_FUNC(visibility_event), NULL);
3805 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3806 GTK_SIGNAL_FUNC(expose_event), NULL);
3807
3808 /*
3809 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3810 * Only needed for some window managers.
3811 */
3812 if (vim_strchr(p_go, GO_POINTER) != NULL)
3813 {
3814 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3815 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3816 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3817 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3818 }
3819
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003820 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3821 * only its widgets. Arguably, this could be common code and we not use
3822 * the window focus at all, but let's be safe.
3823 */
3824 if (gtk_socket_id == 0)
3825 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003826 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3827 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3828 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3829 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003830 }
3831 else
3832 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003833 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3834 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3835 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3836 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003837#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003838 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3839 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3840 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3841 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003842#endif /* FEAT_GUI_TABLINE */
3843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844
3845 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3846 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3847 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3848 GTK_SIGNAL_FUNC(button_press_event), NULL);
3849 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3850 GTK_SIGNAL_FUNC(button_release_event), NULL);
3851#ifdef HAVE_GTK2
3852 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3853 G_CALLBACK(&scroll_event), NULL);
3854#endif
3855
3856 /*
3857 * Add selection handler functions.
3858 */
3859 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3860 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3861 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3862 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3863
3864 /*
3865 * Add selection targets for PRIMARY and CLIPBOARD selections.
3866 */
3867 gtk_selection_add_targets(gui.drawarea,
3868 (GdkAtom)GDK_SELECTION_PRIMARY,
3869 selection_targets, N_SELECTION_TARGETS);
3870 gtk_selection_add_targets(gui.drawarea,
3871 (GdkAtom)clip_plus.gtk_sel_atom,
3872 selection_targets, N_SELECTION_TARGETS);
3873
3874 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3875 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3876
3877 /* Pretend we don't have input focus, we will get an event if we do. */
3878 gui.in_focus = FALSE;
3879
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 return OK;
3881}
3882
3883#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3884/*
3885 * This is called from gui_start() after a fork() has been done.
3886 * We have to tell the session manager our new PID.
3887 */
3888 void
3889gui_mch_forked(void)
3890{
3891 if (using_gnome)
3892 {
3893 GnomeClient *client;
3894
3895 client = gnome_master_client();
3896
3897 if (client != NULL)
3898 gnome_client_set_process_id(client, getpid());
3899 }
3900}
3901#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3902
3903/*
3904 * Called when the foreground or background color has been changed.
3905 * This used to change the graphics contexts directly but we are
3906 * currently manipulating them where desired.
3907 */
3908 void
3909gui_mch_new_colors(void)
3910{
3911 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3912 {
3913 GdkColor color = { 0, 0, 0, 0 };
3914
3915 color.pixel = gui.back_pixel;
3916 gdk_window_set_background(gui.drawarea->window, &color);
3917 }
3918}
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920/*
3921 * This signal informs us about the need to rearrange our sub-widgets.
3922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003924form_configure_event(GtkWidget *widget UNUSED,
3925 GdkEventConfigure *event,
3926 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003928 int usable_height = event->height;
3929
3930 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3931 * no. of char-heights), so we have to manually sanitise them.
3932 * Widths seem to sort themselves out, don't ask me why.
3933 */
3934 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003935 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003938 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 gtk_form_thaw(GTK_FORM(gui.formwin));
3940
3941 return TRUE;
3942}
3943
3944/*
3945 * Function called when window already closed.
3946 * We can't do much more here than to trying to preserve what had been done,
3947 * since the window is already inevitably going away.
3948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003950mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951{
3952 /* Don't write messages to the GUI anymore */
3953 full_screen = FALSE;
3954
3955 gui.mainwin = NULL;
3956 gui.drawarea = NULL;
3957
3958 if (!exiting) /* only do anything if the destroy was unexpected */
3959 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003960 vim_strncpy(IObuff,
3961 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3962 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 preserve_exit();
3964 }
3965}
3966
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003967
3968/*
3969 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3970 * hints (and thus the required size from -geom), but that after that we
3971 * put the hints back to normal (the actual minimum size) so we may
3972 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003973 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003974 * only way we have of making ourselves bigger (by set lines/columns).
3975 * Thus set hints at start-up to ensure correct init. size, then a
3976 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003977 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003978 * We'll not let the default hints be set while this timer's active.
3979 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003980 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003981check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003982{
3983 if (init_window_hints_state == 1)
3984 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003985 /* Safe to use normal hints now */
3986 init_window_hints_state = 0;
3987 update_window_manager_hints(0, 0);
3988 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003989 }
3990
3991 /* Keep on trying */
3992 init_window_hints_state = 1;
3993 return TRUE;
3994}
3995
3996
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997/*
3998 * Open the GUI window which was created by a call to gui_mch_init().
3999 */
4000 int
4001gui_mch_open(void)
4002{
4003 guicolor_T fg_pixel = INVALCOLOR;
4004 guicolor_T bg_pixel = INVALCOLOR;
4005
4006#ifdef HAVE_GTK2
4007 /*
4008 * Allow setting a window role on the command line, or invent one
4009 * if none was specified. This is mainly useful for GNOME session
4010 * support; allowing the WM to restore window placement.
4011 */
4012 if (role_argument != NULL)
4013 {
4014 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
4015 }
4016 else
4017 {
4018 char *role;
4019
4020 /* Invent a unique-enough ID string for the role */
4021 role = g_strdup_printf("vim-%u-%u-%u",
4022 (unsigned)mch_get_pid(),
4023 (unsigned)g_random_int(),
4024 (unsigned)time(NULL));
4025
4026 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
4027 g_free(role);
4028 }
4029#endif
4030
4031 if (gui_win_x != -1 && gui_win_y != -1)
4032#ifdef HAVE_GTK2
4033 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
4034#else
4035 gtk_widget_set_uposition(gui.mainwin, gui_win_x, gui_win_y);
4036#endif
4037
4038 /* Determine user specified geometry, if present. */
4039 if (gui.geom != NULL)
4040 {
4041 int mask;
4042 unsigned int w, h;
4043 int x = 0;
4044 int y = 0;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004045 guint pixel_width;
4046 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
4049
4050 if (mask & WidthValue)
4051 Columns = w;
4052 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00004053 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004054 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00004055 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00004057 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004058
4059 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
4060 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
4061
4062#ifdef HAVE_GTK2
4063 pixel_width += get_menu_tool_width();
4064 pixel_height += get_menu_tool_height();
4065#endif
4066
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004068 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004069 int ww, hh;
4070 gui_mch_get_screen_dimensions(&ww, &hh);
4071 hh += p_ghr + get_menu_tool_height();
4072 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004073 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004074 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004075 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004076 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077#ifdef HAVE_GTK2
4078 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4079#else
4080 gtk_widget_set_uposition(gui.mainwin, x, y);
4081#endif
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00004082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 vim_free(gui.geom);
4084 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004085
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004086 /* From now until everyone's stopped trying to set the window hints
4087 * to their correct minimum values, stop them being set as we need
4088 * them to remain at our required size for the parent GtkSocket to
4089 * give us the right initial size.
4090 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004091 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004092 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004093 update_window_manager_hints(pixel_width, pixel_height);
4094 init_window_hints_state = 1;
4095 g_timeout_add(1000, check_startup_plug_hints, NULL);
4096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 }
4098
4099 gtk_form_set_size(GTK_FORM(gui.formwin),
4100 (guint)(gui_get_base_width() + Columns * gui.char_width),
4101 (guint)(gui_get_base_height() + Rows * gui.char_height));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004102 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103
4104 if (foreground_argument != NULL)
4105 fg_pixel = gui_get_color((char_u *)foreground_argument);
4106 if (fg_pixel == INVALCOLOR)
4107 fg_pixel = gui_get_color((char_u *)"Black");
4108
4109 if (background_argument != NULL)
4110 bg_pixel = gui_get_color((char_u *)background_argument);
4111 if (bg_pixel == INVALCOLOR)
4112 bg_pixel = gui_get_color((char_u *)"White");
4113
4114 if (found_reverse_arg)
4115 {
4116 gui.def_norm_pixel = bg_pixel;
4117 gui.def_back_pixel = fg_pixel;
4118 }
4119 else
4120 {
4121 gui.def_norm_pixel = fg_pixel;
4122 gui.def_back_pixel = bg_pixel;
4123 }
4124
4125 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4126 * in a vimrc file) */
4127 set_normal_colors();
4128
4129 /* Check that none of the colors are the same as the background color */
4130 gui_check_colors();
4131
4132 /* Get the colors for the highlight groups (gui_check_colors() might have
4133 * changed them). */
4134 highlight_gui_started(); /* re-init colors and fonts */
4135
4136 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
4137 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
4138
4139#ifdef FEAT_HANGULIN
4140 hangul_keyboard_set();
4141#endif
4142
4143 /*
4144 * Notify the fixed area about the need to resize the contents of the
4145 * gui.formwin, which we use for random positioning of the included
4146 * components.
4147 *
4148 * We connect this signal deferred finally after anything is in place,
4149 * since this is intended to handle resizements coming from the window
4150 * manager upon us and should not interfere with what VIM is requesting
4151 * upon startup.
4152 */
4153 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
4154 GTK_SIGNAL_FUNC(form_configure_event), NULL);
4155
4156#ifdef FEAT_DND
4157 /*
4158 * Set up for receiving DND items.
4159 */
4160 gtk_drag_dest_set(gui.drawarea,
4161 GTK_DEST_DEFAULT_ALL,
4162 dnd_targets, N_DND_TARGETS,
4163 GDK_ACTION_COPY);
4164
4165 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
4166 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
4167#endif
4168
4169#ifdef HAVE_GTK2
4170 /* With GTK+ 2, we need to iconify the window before calling show()
4171 * to avoid mapping the window for a short time. This is just as one
4172 * would expect it to work, but it's different in GTK+ 1. The funny
4173 * thing is that iconifying after show() _does_ work with GTK+ 1.
4174 * (BTW doing this in the "realize" handler makes no difference.) */
4175 if (found_iconic_arg && gtk_socket_id == 0)
4176 gui_mch_iconify();
4177#endif
4178
4179 {
4180#if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4181 unsigned long menu_handler = 0;
4182# ifdef FEAT_TOOLBAR
4183 unsigned long tool_handler = 0;
4184# endif
4185 /*
4186 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4187 * show when restoring the saved layout configuration. We can't just
4188 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4189 * a toplevel window and thus will be realized immediately. Instead,
4190 * connect signal handlers to hide the widgets just after they've been
4191 * marked visible, but before the main window is realized.
4192 */
4193 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
4194 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
4195 G_CALLBACK(&gtk_widget_hide),
4196 NULL);
4197# ifdef FEAT_TOOLBAR
4198 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
4199 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
4200 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
4201 G_CALLBACK(&gtk_widget_hide),
4202 NULL);
4203# endif
4204#endif
4205 gtk_widget_show(gui.mainwin);
4206
4207#if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4208 if (menu_handler != 0)
4209 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
4210# ifdef FEAT_TOOLBAR
4211 if (tool_handler != 0)
4212 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
4213# endif
4214#endif
4215 }
4216
4217#ifndef HAVE_GTK2
4218 /* With GTK+ 1, we need to iconify the window after calling show().
4219 * See the comment above for details. */
4220 if (found_iconic_arg && gtk_socket_id == 0)
4221 gui_mch_iconify();
4222#endif
4223
4224 return OK;
4225}
4226
4227
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004229gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 if (gui.mainwin != NULL)
4232 gtk_widget_destroy(gui.mainwin);
4233
4234 if (gtk_main_level() > 0)
4235 gtk_main_quit();
4236}
4237
4238/*
4239 * Get the position of the top left corner of the window.
4240 */
4241 int
4242gui_mch_get_winpos(int *x, int *y)
4243{
4244#ifdef HAVE_GTK2
4245 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
4246#else
4247 /* For some people this must be gdk_window_get_origin() for a correct
4248 * result. Where is the documentation! */
4249 gdk_window_get_root_origin(gui.mainwin->window, x, y);
4250#endif
4251 return OK;
4252}
4253
4254/*
4255 * Set the position of the top left corner of the window to the given
4256 * coordinates.
4257 */
4258 void
4259gui_mch_set_winpos(int x, int y)
4260{
4261#ifdef HAVE_GTK2
4262 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
4263#else
4264 gdk_window_move(gui.mainwin->window, x, y);
4265#endif
4266}
4267
4268#ifdef HAVE_GTK2
4269#if 0
4270static int resize_idle_installed = FALSE;
4271/*
4272 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4273 * the shell size doesn't exceed the window size, i.e. if the window manager
4274 * ignored our size request. Usually this happens if the window is maximized.
4275 *
4276 * FIXME: It'd be nice if we could find a little more orthodox solution.
4277 * See also the remark below in gui_mch_set_shellsize().
4278 *
4279 * DISABLED: When doing ":set lines+=1" this function would first invoke
4280 * gui_resize_shell() with the old size, then the normal callback would
4281 * report the new size through form_configure_event(). That caused the window
4282 * layout to be messed up.
4283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 static gboolean
4285force_shell_resize_idle(gpointer data)
4286{
4287 if (gui.mainwin != NULL
4288 && GTK_WIDGET_REALIZED(gui.mainwin)
4289 && GTK_WIDGET_VISIBLE(gui.mainwin))
4290 {
4291 int width;
4292 int height;
4293
4294 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
4295
4296 width -= get_menu_tool_width();
4297 height -= get_menu_tool_height();
4298
4299 gui_resize_shell(width, height);
4300 }
4301
4302 resize_idle_installed = FALSE;
4303 return FALSE; /* don't call me again */
4304}
4305#endif
4306#endif /* HAVE_GTK2 */
4307
4308/*
4309 * Set the windows size.
4310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 void
4312gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004313 int min_width UNUSED, int min_height UNUSED,
4314 int base_width UNUSED, int base_height UNUSED,
4315 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316{
4317#ifndef HAVE_GTK2
4318 /* Hack: When the form already is at the desired size, the window might
4319 * have been resized with the mouse. Force a resize by setting a
4320 * different size first. */
4321 if (GTK_FORM(gui.formwin)->width == width
4322 && GTK_FORM(gui.formwin)->height == height)
4323 {
4324 gtk_form_set_size(GTK_FORM(gui.formwin), width + 1, height + 1);
4325 gui_mch_update();
4326 }
4327 gtk_form_set_size(GTK_FORM(gui.formwin), width, height);
4328#endif
4329
4330 /* give GTK+ a chance to put all widget's into place */
4331 gui_mch_update();
4332
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004333#ifndef HAVE_GTK2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* this will cause the proper resizement to happen too */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004335 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004337#else /* HAVE_GTK2 */
4338 /* this will cause the proper resizement to happen too */
4339 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004340 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004341
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004343 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 * main window instead. */
4345 width += get_menu_tool_width();
4346 height += get_menu_tool_height();
4347
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004348 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004349 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004350 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004351 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352
4353#if 0
4354 if (!resize_idle_installed)
4355 {
4356 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
4357 &force_shell_resize_idle, NULL, NULL);
4358 resize_idle_installed = TRUE;
4359 }
4360#endif
4361 /*
4362 * Wait until all events are processed to prevent a crash because the
4363 * real size of the drawing area doesn't reflect Vim's internal ideas.
4364 *
4365 * This is a bit of a hack, since Vim is a terminal application with a GUI
4366 * on top, while the GUI expects to be the boss.
4367 */
4368 gui_mch_update();
4369#endif
4370}
4371
4372
4373/*
4374 * The screen size is used to make sure the initial window doesn't get bigger
4375 * than the screen. This subtracts some room for menubar, toolbar and window
4376 * decorations.
4377 */
4378 void
4379gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4380{
4381#ifdef HAVE_GTK_MULTIHEAD
4382 GdkScreen* screen;
4383
4384 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4385 screen = gtk_widget_get_screen(gui.mainwin);
4386 else
4387 screen = gdk_screen_get_default();
4388
4389 *screen_w = gdk_screen_get_width(screen);
4390 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4391#else
4392 *screen_w = gdk_screen_width();
4393 /* Subtract 'guiheadroom' from the height to allow some room for the
4394 * window manager (task list and window title bar). */
4395 *screen_h = gdk_screen_height() - p_ghr;
4396#endif
4397
4398 /*
4399 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4400 * the toolbar and menubar for GTK, we subtract them from the screen
4401 * hight, so that the window size can be made to fit on the screen.
4402 * This should be completely changed later.
4403 */
4404 *screen_w -= get_menu_tool_width();
4405 *screen_h -= get_menu_tool_height();
4406}
4407
4408#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004410gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411{
4412# ifdef HAVE_GTK2
4413 if (title != NULL && output_conv.vc_type != CONV_NONE)
4414 title = string_convert(&output_conv, title, NULL);
4415# endif
4416
4417 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4418
4419# ifdef HAVE_GTK2
4420 if (output_conv.vc_type != CONV_NONE)
4421 vim_free(title);
4422# endif
4423}
4424#endif /* FEAT_TITLE */
4425
4426#if defined(FEAT_MENU) || defined(PROTO)
4427 void
4428gui_mch_enable_menu(int showit)
4429{
4430 GtkWidget *widget;
4431
4432# ifdef FEAT_GUI_GNOME
4433 if (using_gnome)
4434 widget = gui.menubar_h;
4435 else
4436# endif
4437 widget = gui.menubar;
4438
Bram Moolenaar18144c82006-04-12 21:52:12 +00004439 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4440 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
4442 if (showit)
4443 gtk_widget_show(widget);
4444 else
4445 gtk_widget_hide(widget);
4446
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004447 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449}
4450#endif /* FEAT_MENU */
4451
4452#if defined(FEAT_TOOLBAR) || defined(PROTO)
4453 void
4454gui_mch_show_toolbar(int showit)
4455{
4456 GtkWidget *widget;
4457
4458 if (gui.toolbar == NULL)
4459 return;
4460
4461# ifdef FEAT_GUI_GNOME
4462 if (using_gnome)
4463 widget = gui.toolbar_h;
4464 else
4465# endif
4466 widget = gui.toolbar;
4467
4468 if (showit)
4469 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4470
4471 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4472 {
4473 if (showit)
4474 gtk_widget_show(widget);
4475 else
4476 gtk_widget_hide(widget);
4477
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004478 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 }
4480}
4481#endif /* FEAT_TOOLBAR */
4482
4483#ifndef HAVE_GTK2
4484/*
4485 * Get a font structure for highlighting.
4486 * "cbdata" is a pointer to the global gui structure.
4487 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 static void
4489font_sel_ok(GtkWidget *wgt, gpointer cbdata)
4490{
4491 gui_T *vw = (gui_T *)cbdata;
4492 GtkFontSelectionDialog *fs = (GtkFontSelectionDialog *)vw->fontdlg;
4493
4494 if (vw->fontname)
4495 g_free(vw->fontname);
4496
4497 vw->fontname = (char_u *)gtk_font_selection_dialog_get_font_name(fs);
4498 gtk_widget_hide(vw->fontdlg);
4499 if (gtk_main_level() > 0)
4500 gtk_main_quit();
4501}
4502
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 static void
4504font_sel_cancel(GtkWidget *wgt, gpointer cbdata)
4505{
4506 gui_T *vw = (gui_T *)cbdata;
4507
4508 gtk_widget_hide(vw->fontdlg);
4509 if (gtk_main_level() > 0)
4510 gtk_main_quit();
4511}
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 static void
4514font_sel_destroy(GtkWidget *wgt, gpointer cbdata)
4515{
4516 gui_T *vw = (gui_T *)cbdata;
4517
4518 vw->fontdlg = NULL;
4519 if (gtk_main_level() > 0)
4520 gtk_main_quit();
4521}
4522#endif /* !HAVE_GTK2 */
4523
4524#ifdef HAVE_GTK2
4525/*
4526 * Check if a given font is a CJK font. This is done in a very crude manner. It
4527 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4528 * font. Consequently, this function cannot be used as a general purpose check
4529 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4530 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4531 */
4532 static int
4533is_cjk_font(PangoFontDescription *font_desc)
4534{
4535 static const char * const cjk_langs[] =
4536 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4537
4538 PangoFont *font;
4539 unsigned i;
4540 int is_cjk = FALSE;
4541
4542 font = pango_context_load_font(gui.text_context, font_desc);
4543
4544 if (font == NULL)
4545 return FALSE;
4546
4547 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4548 {
4549 PangoCoverage *coverage;
4550 gunichar uc;
4551
4552 coverage = pango_font_get_coverage(
4553 font, pango_language_from_string(cjk_langs[i]));
4554
4555 if (coverage != NULL)
4556 {
4557 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4558 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4559 pango_coverage_unref(coverage);
4560 }
4561 }
4562
4563 g_object_unref(font);
4564
4565 return is_cjk;
4566}
4567#endif /* HAVE_GTK2 */
4568
Bram Moolenaar231334e2005-07-25 20:46:57 +00004569/*
4570 * Adjust gui.char_height (after 'linespace' was changed).
4571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004573gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574{
4575#ifdef HAVE_GTK2
4576 PangoFontMetrics *metrics;
4577 int ascent;
4578 int descent;
4579
4580 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4581 pango_context_get_language(gui.text_context));
4582 ascent = pango_font_metrics_get_ascent(metrics);
4583 descent = pango_font_metrics_get_descent(metrics);
4584
4585 pango_font_metrics_unref(metrics);
4586
4587 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004588 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004589 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4591
4592#else /* !HAVE_GTK2 */
4593
4594 gui.char_height = gui.current_font->ascent + gui.current_font->descent
Bram Moolenaar231334e2005-07-25 20:46:57 +00004595 + p_linespace;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 gui.char_ascent = gui.current_font->ascent + p_linespace / 2;
4597
4598#endif /* !HAVE_GTK2 */
4599
4600 /* A not-positive value of char_height may crash Vim. Only happens
4601 * if 'linespace' is negative (which does make sense sometimes). */
4602 gui.char_ascent = MAX(gui.char_ascent, 0);
4603 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4604
4605 return OK;
4606}
4607
4608#if defined(FEAT_XFONTSET) || defined(PROTO)
4609/*
4610 * Try to load the requested fontset.
4611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 GuiFontset
4613gui_mch_get_fontset(char_u *name, int report_error, int fixed_width)
4614{
4615 GdkFont *font;
4616
4617 if (!gui.in_use || name == NULL)
4618 return NOFONT;
4619
4620 font = gdk_fontset_load((gchar *)name);
4621
4622 if (font == NULL)
4623 {
4624 if (report_error)
4625 EMSG2(_(e_fontset), name);
4626 return NOFONT;
4627 }
4628 /* TODO: check if the font is fixed width. */
4629
4630 /* reference this font as being in use */
4631 gdk_font_ref(font);
4632
4633 return (GuiFontset)font;
4634}
4635#endif /* FEAT_XFONTSET */
4636
4637#ifndef HAVE_GTK2
4638/*
4639 * Put up a font dialog and return the selected font name in allocated memory.
4640 * "oldval" is the previous value.
4641 * Return NULL when cancelled.
4642 */
4643 char_u *
4644gui_mch_font_dialog(char_u *oldval)
4645{
4646 char_u *fontname = NULL;
4647
4648 if (!gui.fontdlg)
4649 {
4650 GtkFontSelectionDialog *fsd = NULL;
4651
4652 gui.fontdlg = gtk_font_selection_dialog_new(_("Font Selection"));
4653 fsd = GTK_FONT_SELECTION_DIALOG(gui.fontdlg);
4654 gtk_window_set_modal(GTK_WINDOW(gui.fontdlg), TRUE);
4655 gtk_window_set_transient_for(GTK_WINDOW(gui.fontdlg),
4656 GTK_WINDOW(gui.mainwin));
4657 gtk_signal_connect(GTK_OBJECT(gui.fontdlg), "destroy",
4658 GTK_SIGNAL_FUNC(font_sel_destroy), &gui);
4659 gtk_signal_connect(GTK_OBJECT(fsd->ok_button), "clicked",
4660 GTK_SIGNAL_FUNC(font_sel_ok), &gui);
4661 gtk_signal_connect(GTK_OBJECT(fsd->cancel_button), "clicked",
4662 GTK_SIGNAL_FUNC(font_sel_cancel), &gui);
4663 }
4664
4665 if (oldval != NULL && *oldval != NUL)
4666 gtk_font_selection_dialog_set_font_name(
4667 GTK_FONT_SELECTION_DIALOG(gui.fontdlg), (char *)oldval);
4668
4669 if (gui.fontname)
4670 {
4671 g_free(gui.fontname);
4672 gui.fontname = NULL;
4673 }
4674 gtk_window_position(GTK_WINDOW(gui.fontdlg), GTK_WIN_POS_MOUSE);
4675 gtk_widget_show(gui.fontdlg);
4676 {
4677 static gchar *spacings[] = {"c", "m", NULL};
4678
4679 /* In GTK 1.2.3 this must be after the gtk_widget_show() call,
4680 * otherwise everything is blocked for ten seconds. */
4681 gtk_font_selection_dialog_set_filter(
4682 GTK_FONT_SELECTION_DIALOG(gui.fontdlg),
4683 GTK_FONT_FILTER_BASE,
4684 GTK_FONT_ALL, NULL, NULL,
4685 NULL, NULL, spacings, NULL);
4686 }
4687
4688 /* Wait for the font dialog to be closed. */
4689 while (gui.fontdlg && GTK_WIDGET_DRAWABLE(gui.fontdlg))
4690 gtk_main_iteration_do(TRUE);
4691
4692 if (gui.fontname != NULL)
4693 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004694 /* Apparently some font names include a comma, need to escape that,
4695 * because in 'guifont' it separates names. */
4696 fontname = vim_strsave_escaped(gui.fontname, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 g_free(gui.fontname);
4698 gui.fontname = NULL;
4699 }
4700 return fontname;
4701}
4702#endif /* !HAVE_GTK2 */
4703
4704#ifdef HAVE_GTK2
4705/*
4706 * Put up a font dialog and return the selected font name in allocated memory.
4707 * "oldval" is the previous value. Return NULL when cancelled.
4708 * This should probably go into gui_gtk.c. Hmm.
4709 * FIXME:
4710 * The GTK2 font selection dialog has no filtering API. So we could either
4711 * a) implement our own (possibly copying the code from somewhere else) or
4712 * b) just live with it.
4713 */
4714 char_u *
4715gui_mch_font_dialog(char_u *oldval)
4716{
4717 GtkWidget *dialog;
4718 int response;
4719 char_u *fontname = NULL;
4720 char_u *oldname;
4721
4722 dialog = gtk_font_selection_dialog_new(NULL);
4723
4724 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4725 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4726
4727 if (oldval != NULL && oldval[0] != NUL)
4728 {
4729 if (output_conv.vc_type != CONV_NONE)
4730 oldname = string_convert(&output_conv, oldval, NULL);
4731 else
4732 oldname = oldval;
4733
4734 /* Annoying bug in GTK (or Pango): if the font name does not include a
4735 * size, zero is used. Use default point size ten. */
4736 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4737 {
4738 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4739
4740 if (p != NULL)
4741 {
4742 STRCPY(p + STRLEN(p), " 10");
4743 if (oldname != oldval)
4744 vim_free(oldname);
4745 oldname = p;
4746 }
4747 }
4748
4749 gtk_font_selection_dialog_set_font_name(
4750 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4751
4752 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004753 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
4755
4756 response = gtk_dialog_run(GTK_DIALOG(dialog));
4757
4758 if (response == GTK_RESPONSE_OK)
4759 {
4760 char *name;
4761
4762 name = gtk_font_selection_dialog_get_font_name(
4763 GTK_FONT_SELECTION_DIALOG(dialog));
4764 if (name != NULL)
4765 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004766 char_u *p;
4767
4768 /* Apparently some font names include a comma, need to escape
4769 * that, because in 'guifont' it separates names. */
4770 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004772 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004773 {
4774 fontname = string_convert(&input_conv, p, NULL);
4775 vim_free(p);
4776 }
4777 else
4778 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 }
4780 }
4781
4782 if (response != GTK_RESPONSE_NONE)
4783 gtk_widget_destroy(dialog);
4784
4785 return fontname;
4786}
4787
4788/*
4789 * Some monospace fonts don't support a bold weight, and fall back
4790 * silently to the regular weight. But this is no good since our text
4791 * drawing function can emulate bold by overstriking. So let's try
4792 * to detect whether bold weight is actually available and emulate it
4793 * otherwise.
4794 *
4795 * Note that we don't need to check for italic style since Xft can
4796 * emulate italic on its own, provided you have a proper fontconfig
4797 * setup. We wouldn't be able to emulate it in Vim anyway.
4798 */
4799 static void
4800get_styled_font_variants(void)
4801{
4802 PangoFontDescription *bold_font_desc;
4803 PangoFont *plain_font;
4804 PangoFont *bold_font;
4805
4806 gui.font_can_bold = FALSE;
4807
4808 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4809
4810 if (plain_font == NULL)
4811 return;
4812
4813 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4814 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4815
4816 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4817 /*
4818 * The comparison relies on the unique handle nature of a PangoFont*,
4819 * i.e. it's assumed that a different PangoFont* won't refer to the
4820 * same font. Seems to work, and failing here isn't critical anyway.
4821 */
4822 if (bold_font != NULL)
4823 {
4824 gui.font_can_bold = (bold_font != plain_font);
4825 g_object_unref(bold_font);
4826 }
4827
4828 pango_font_description_free(bold_font_desc);
4829 g_object_unref(plain_font);
4830}
4831
4832#else /* !HAVE_GTK2 */
4833
4834/*
4835 * There is only one excuse I can give for the following attempt to manage font
4836 * styles:
4837 *
4838 * I HATE THE BRAIN DEAD WAY X11 IS HANDLING FONTS (--mdcki)
4839 * (Me too. --danielk)
4840 */
4841 static void
4842get_styled_font_variants(char_u * font_name)
4843{
4844 char *chunk[32];
4845 char *sdup;
4846 char *tmp;
4847 int len, i;
4848 GuiFont *styled_font[3];
4849
4850 styled_font[0] = &gui.bold_font;
4851 styled_font[1] = &gui.ital_font;
4852 styled_font[2] = &gui.boldital_font;
4853
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004854 /* First free whatever was previously there. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 for (i = 0; i < 3; ++i)
4856 if (*styled_font[i])
4857 {
4858 gdk_font_unref(*styled_font[i]);
4859 *styled_font[i] = NULL;
4860 }
4861
4862 if ((sdup = g_strdup((const char *)font_name)) == NULL)
4863 return;
4864
4865 /* split up the whole */
4866 i = 0;
4867 for (tmp = sdup; *tmp != '\0'; ++tmp)
4868 {
4869 if (*tmp == '-')
4870 {
4871 *tmp = '\0';
4872
4873 if (i == 32)
4874 break;
4875
4876 chunk[i] = tmp + 1;
4877 ++i;
4878 }
4879 }
4880
4881 if (i == 14)
4882 {
4883 GdkFont *font = NULL;
4884 const char *bold_chunk[3] = { "bold", NULL, "bold" };
4885 const char *italic_chunk[3] = { NULL, "o", "o" };
4886
4887 /* font name was complete */
4888 len = strlen((const char *)font_name) + 32;
4889
4890 for (i = 0; i < 3; ++i)
4891 {
4892 char *styled_name;
4893 int j;
4894
4895 styled_name = (char *)alloc(len);
4896 if (styled_name == NULL)
4897 {
4898 g_free(sdup);
4899 return;
4900 }
4901
4902 *styled_name = '\0';
4903
4904 for (j = 0; j < 14; ++j)
4905 {
4906 strcat(styled_name, "-");
4907 if (j == 2 && bold_chunk[i] != NULL)
4908 strcat(styled_name, bold_chunk[i]);
4909 else if (j == 3 && italic_chunk[i] != NULL)
4910 strcat(styled_name, italic_chunk[i]);
4911 else
4912 strcat(styled_name, chunk[j]);
4913 }
4914
4915 font = gui_mch_get_font((char_u *)styled_name, FALSE);
4916 if (font != NULL)
4917 *styled_font[i] = font;
4918
4919 vim_free(styled_name);
4920 }
4921 }
4922
4923 g_free(sdup);
4924}
4925#endif /* !HAVE_GTK2 */
4926
4927#ifdef HAVE_GTK2
4928static PangoEngineShape *default_shape_engine = NULL;
4929
4930/*
4931 * Create a map from ASCII characters in the range [32,126] to glyphs
4932 * of the current font. This is used by gui_gtk2_draw_string() to skip
4933 * the itemize and shaping process for the most common case.
4934 */
4935 static void
4936ascii_glyph_table_init(void)
4937{
4938 char_u ascii_chars[128];
4939 PangoAttrList *attr_list;
4940 GList *item_list;
4941 int i;
4942
4943 if (gui.ascii_glyphs != NULL)
4944 pango_glyph_string_free(gui.ascii_glyphs);
4945 if (gui.ascii_font != NULL)
4946 g_object_unref(gui.ascii_font);
4947
4948 gui.ascii_glyphs = NULL;
4949 gui.ascii_font = NULL;
4950
4951 /* For safety, fill in question marks for the control characters. */
4952 for (i = 0; i < 32; ++i)
4953 ascii_chars[i] = '?';
4954 for (; i < 127; ++i)
4955 ascii_chars[i] = i;
4956 ascii_chars[i] = '?';
4957
4958 attr_list = pango_attr_list_new();
4959 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4960 0, sizeof(ascii_chars), attr_list, NULL);
4961
4962 if (item_list != NULL && item_list->next == NULL) /* play safe */
4963 {
4964 PangoItem *item;
4965 int width;
4966
4967 item = (PangoItem *)item_list->data;
4968 width = gui.char_width * PANGO_SCALE;
4969
4970 /* Remember the shape engine used for ASCII. */
4971 default_shape_engine = item->analysis.shape_engine;
4972
4973 gui.ascii_font = item->analysis.font;
4974 g_object_ref(gui.ascii_font);
4975
4976 gui.ascii_glyphs = pango_glyph_string_new();
4977
4978 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4979 &item->analysis, gui.ascii_glyphs);
4980
4981 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4982
4983 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4984 {
4985 PangoGlyphGeometry *geom;
4986
4987 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4988 geom->x_offset += MAX(0, width - geom->width) / 2;
4989 geom->width = width;
4990 }
4991 }
4992
4993 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4994 g_list_free(item_list);
4995 pango_attr_list_unref(attr_list);
4996}
4997#endif /* HAVE_GTK2 */
4998
4999/*
5000 * Initialize Vim to use the font or fontset with the given name.
5001 * Return FAIL if the font could not be loaded, OK otherwise.
5002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005004gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
5006#ifdef HAVE_GTK2
5007 PangoFontDescription *font_desc;
5008 PangoLayout *layout;
5009 int width;
5010
5011 /* If font_name is NULL, this means to use the default, which should
5012 * be present on all proper Pango/fontconfig installations. */
5013 if (font_name == NULL)
5014 font_name = (char_u *)DEFAULT_FONT;
5015
5016 font_desc = gui_mch_get_font(font_name, FALSE);
5017
5018 if (font_desc == NULL)
5019 return FAIL;
5020
5021 gui_mch_free_font(gui.norm_font);
5022 gui.norm_font = font_desc;
5023
5024 pango_context_set_font_description(gui.text_context, font_desc);
5025
5026 layout = pango_layout_new(gui.text_context);
5027 pango_layout_set_text(layout, "MW", 2);
5028 pango_layout_get_size(layout, &width, NULL);
5029 /*
5030 * Set char_width to half the width obtained from pango_layout_get_size()
5031 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5032 * Pango to use the same width for both non-CJK characters (e.g. Latin
5033 * letters and numbers) and CJK characters. This results in 's p a c e d
5034 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5035 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5036 * width for CJK fonts.
5037 *
5038 * For related bugs, see:
5039 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5040 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5041 *
5042 * With this, for all four of the following cases, Vim works fine:
5043 * guifont=CJK_fixed_width_font
5044 * guifont=Non_CJK_fixed_font
5045 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5046 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5047 */
5048 if (is_cjk_font(gui.norm_font))
5049 {
5050 int cjk_width;
5051
5052 /* Measure the text extent of U+4E00 and U+4E8C */
5053 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
5054 pango_layout_get_size(layout, &cjk_width, NULL);
5055
5056 if (width == cjk_width) /* Xft not patched */
5057 width /= 2;
5058 }
5059 g_object_unref(layout);
5060
5061 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
5062
5063 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5064 if (gui.char_width <= 0)
5065 gui.char_width = 8;
5066
Bram Moolenaar231334e2005-07-25 20:46:57 +00005067 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068
5069 /* Set the fontname, which will be used for information purposes */
5070 hl_set_font_name(font_name);
5071
5072 get_styled_font_variants();
5073 ascii_glyph_table_init();
5074
5075 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5076 if (gui.wide_font != NULL
5077 && pango_font_description_equal(gui.norm_font, gui.wide_font))
5078 {
5079 pango_font_description_free(gui.wide_font);
5080 gui.wide_font = NULL;
5081 }
5082
5083#else /* !HAVE_GTK2 */
5084
5085 GdkFont *font = NULL;
5086
5087# ifdef FEAT_XFONTSET
5088 /* Try loading a fontset. If this fails we try loading a normal font. */
5089 if (fontset && font_name != NULL)
5090 font = gui_mch_get_fontset(font_name, TRUE, TRUE);
5091
5092 if (font == NULL)
5093# endif
5094 {
5095 /* If font_name is NULL, this means to use the default, which should
5096 * be present on all X11 servers. */
5097 if (font_name == NULL)
5098 font_name = (char_u *)DEFAULT_FONT;
5099 font = gui_mch_get_font(font_name, FALSE);
5100 }
5101
5102 if (font == NULL)
5103 return FAIL;
5104
5105 gui_mch_free_font(gui.norm_font);
5106# ifdef FEAT_XFONTSET
5107 gui_mch_free_fontset(gui.fontset);
5108 if (font->type == GDK_FONT_FONTSET)
5109 {
5110 gui.norm_font = NOFONT;
5111 gui.fontset = (GuiFontset)font;
5112 /* Use two bytes, this works around the problem that the result would
5113 * be zero if no 8-bit font was found. */
5114 gui.char_width = gdk_string_width(font, "xW") / 2;
5115 }
5116 else
5117# endif
5118 {
5119 gui.norm_font = font;
5120# ifdef FEAT_XFONTSET
5121 gui.fontset = NOFONTSET;
5122# endif
5123 gui.char_width = ((XFontStruct *)
5124 GDK_FONT_XFONT(font))->max_bounds.width;
5125 }
5126
5127 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5128 if (gui.char_width <= 0)
5129 gui.char_width = 8;
5130
5131 gui.char_height = font->ascent + font->descent + p_linespace;
5132 gui.char_ascent = font->ascent + p_linespace / 2;
5133
5134 /* A not-positive value of char_height may crash Vim. Only happens
5135 * if 'linespace' is negative (which does make sense sometimes). */
5136 gui.char_ascent = MAX(gui.char_ascent, 0);
5137 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
5138
5139 /* Set the fontname, which will be used for information purposes */
5140 hl_set_font_name(font_name);
5141
5142 if (font->type != GDK_FONT_FONTSET)
5143 get_styled_font_variants(font_name);
5144
5145 /* Synchronize the fonts used in user input dialogs, since otherwise
5146 * search/replace will be esp. annoying in case of international font
5147 * usage.
5148 */
5149 gui_gtk_synch_fonts();
5150
5151# ifdef FEAT_XIM
5152 /* Adjust input management behaviour to the capabilities of the new
5153 * fontset */
5154 xim_decide_input_style();
5155 if (xim_get_status_area_height())
5156 {
5157 /* Status area is required. Just create the empty container so that
5158 * mainwin will allocate the extra space for status area. */
5159 GtkWidget *alignment = gtk_alignment_new((gfloat)0.5, (gfloat)0.5,
5160 (gfloat)1.0, (gfloat)1.0);
5161
5162 gtk_widget_set_usize(alignment, 20, gui.char_height + 2);
5163 gtk_box_pack_end(GTK_BOX(GTK_BIN(gui.mainwin)->child),
5164 alignment, FALSE, FALSE, 0);
5165 gtk_widget_show(alignment);
5166 }
5167# endif
5168#endif /* !HAVE_GTK2 */
5169
5170 /* Preserve the logical dimensions of the screen. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005171 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 return OK;
5174}
5175
5176/*
5177 * Get a reference to the font "name".
5178 * Return zero for failure.
5179 */
5180 GuiFont
5181gui_mch_get_font(char_u *name, int report_error)
5182{
5183#ifdef HAVE_GTK2
5184 PangoFontDescription *font;
5185#else
5186 GdkFont *font;
5187#endif
5188
5189 /* can't do this when GUI is not running */
5190 if (!gui.in_use || name == NULL)
5191 return NULL;
5192
5193#ifdef HAVE_GTK2
5194 if (output_conv.vc_type != CONV_NONE)
5195 {
5196 char_u *buf;
5197
5198 buf = string_convert(&output_conv, name, NULL);
5199 if (buf != NULL)
5200 {
5201 font = pango_font_description_from_string((const char *)buf);
5202 vim_free(buf);
5203 }
5204 else
5205 font = NULL;
5206 }
5207 else
5208 font = pango_font_description_from_string((const char *)name);
5209
5210 if (font != NULL)
5211 {
5212 PangoFont *real_font;
5213
5214 /* pango_context_load_font() bails out if no font size is set */
5215 if (pango_font_description_get_size(font) <= 0)
5216 pango_font_description_set_size(font, 10 * PANGO_SCALE);
5217
5218 real_font = pango_context_load_font(gui.text_context, font);
5219
5220 if (real_font == NULL)
5221 {
5222 pango_font_description_free(font);
5223 font = NULL;
5224 }
5225 else
5226 g_object_unref(real_font);
5227 }
5228#else
5229 font = gdk_font_load((const gchar *)name);
5230#endif
5231
5232 if (font == NULL)
5233 {
5234 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00005235 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 return NULL;
5237 }
5238
5239#ifdef HAVE_GTK2
5240 /*
5241 * The fixed-width check has been disabled for GTK+ 2. Rationale:
5242 *
5243 * - The check tends to report false positives, particularly
5244 * in non-Latin locales or with old X fonts.
5245 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
5246 * GTK+ 2 Vim is actually capable of displaying variable width
5247 * fonts. Those will just be spaced out like in AA xterm.
5248 * - Failing here for the default font causes GUI startup to fail
5249 * even with wiped out configuration files.
5250 * - The font dialog displays all fonts unfiltered, and it's rather
5251 * annoying if 95% of the listed fonts produce an error message.
5252 */
5253# if 0
5254 {
5255 /* Check that this is a mono-spaced font. Naturally, this is a bit
5256 * hackish -- fixed-width isn't really suitable for i18n text :/ */
5257 PangoLayout *layout;
5258 unsigned int i;
5259 int last_width = -1;
5260 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
5261
5262 layout = pango_layout_new(gui.text_context);
5263 pango_layout_set_font_description(layout, font);
5264
5265 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
5266 {
5267 int width;
5268
5269 pango_layout_set_text(layout, &test_chars[i], 1);
5270 pango_layout_get_size(layout, &width, NULL);
5271
5272 if (last_width >= 0 && width != last_width)
5273 {
5274 pango_font_description_free(font);
5275 font = NULL;
5276 break;
5277 }
5278
5279 last_width = width;
5280 }
5281
5282 g_object_unref(layout);
5283 }
5284# endif
5285#else /* !HAVE_GTK2 */
5286 {
5287 XFontStruct *xfont;
5288
5289 /* reference this font as being in use */
5290 gdk_font_ref(font);
5291
5292 /* Check that this is a mono-spaced font.
5293 */
5294 xfont = (XFontStruct *) GDK_FONT_XFONT(font);
5295
5296 if (xfont->max_bounds.width != xfont->min_bounds.width)
5297 {
5298 gdk_font_unref(font);
5299 font = NULL;
5300 }
5301 }
5302#endif /* !HAVE_GTK2 */
5303
5304#if !defined(HAVE_GTK2) || 0 /* disabled for GTK+ 2, see above */
5305 if (font == NULL && report_error)
5306 EMSG2(_(e_fontwidth), name);
5307#endif
5308
5309 return font;
5310}
5311
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005312#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005313/*
5314 * Return the name of font "font" in allocated memory.
5315 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005316 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005317gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005318{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005319# ifdef HAVE_GTK2
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005320 if (font != NOFONT)
5321 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005322 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005323
Bram Moolenaar89d40322006-08-29 15:30:07 +00005324 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005325 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00005326 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005327
Bram Moolenaar89d40322006-08-29 15:30:07 +00005328 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005329 return s;
5330 }
5331 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005332# else
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005333 /* Don't know how to get the name, return what we got. */
5334 if (name != NULL)
5335 return vim_strsave(name);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005336# endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005337 return NULL;
5338}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005339#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341#if !defined(HAVE_GTK2) || defined(PROTO)
5342/*
5343 * Set the current text font.
5344 * Since we create all GC on demand, we use just gui.current_font to
5345 * indicate the desired current font.
5346 */
5347 void
5348gui_mch_set_font(GuiFont font)
5349{
5350 gui.current_font = font;
5351}
5352#endif
5353
5354#if defined(FEAT_XFONTSET) || defined(PROTO)
5355/*
5356 * Set the current text fontset.
5357 */
5358 void
5359gui_mch_set_fontset(GuiFontset fontset)
5360{
5361 gui.current_font = fontset;
5362}
5363#endif
5364
5365/*
5366 * If a font is not going to be used, free its structure.
5367 */
5368 void
5369gui_mch_free_font(GuiFont font)
5370{
5371 if (font != NOFONT)
5372#ifdef HAVE_GTK2
5373 pango_font_description_free(font);
5374#else
5375 gdk_font_unref(font);
5376#endif
5377}
5378
5379#if defined(FEAT_XFONTSET) || defined(PROTO)
5380/*
5381 * If a fontset is not going to be used, free its structure.
5382 */
5383 void
5384gui_mch_free_fontset(GuiFontset fontset)
5385{
5386 if (fontset != NOFONTSET)
5387 gdk_font_unref(fontset);
5388}
5389#endif
5390
5391
5392/*
5393 * Return the Pixel value (color) for the given color name. This routine was
5394 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5395 * Programmer's Guide.
5396 * Return INVALCOLOR for error.
5397 */
5398 guicolor_T
5399gui_mch_get_color(char_u *name)
5400{
5401 /* A number of colors that some X11 systems don't have */
5402 static const char *const vimnames[][2] =
5403 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00005404 {"LightRed", "#FFBBBB"},
5405 {"LightGreen", "#88FF88"},
5406 {"LightMagenta","#FFBBFF"},
5407 {"DarkCyan", "#008888"},
5408 {"DarkBlue", "#0000BB"},
5409 {"DarkRed", "#BB0000"},
5410 {"DarkMagenta", "#BB00BB"},
5411 {"DarkGrey", "#BBBBBB"},
5412 {"DarkYellow", "#BBBB00"},
5413 {"Gray10", "#1A1A1A"},
5414 {"Grey10", "#1A1A1A"},
5415 {"Gray20", "#333333"},
5416 {"Grey20", "#333333"},
5417 {"Gray30", "#4D4D4D"},
5418 {"Grey30", "#4D4D4D"},
5419 {"Gray40", "#666666"},
5420 {"Grey40", "#666666"},
5421 {"Gray50", "#7F7F7F"},
5422 {"Grey50", "#7F7F7F"},
5423 {"Gray60", "#999999"},
5424 {"Grey60", "#999999"},
5425 {"Gray70", "#B3B3B3"},
5426 {"Grey70", "#B3B3B3"},
5427 {"Gray80", "#CCCCCC"},
5428 {"Grey80", "#CCCCCC"},
5429 {"Gray90", "#E5E5E5"},
5430 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {NULL, NULL}
5432 };
5433
5434 if (!gui.in_use) /* can't do this when GUI not running */
5435 return INVALCOLOR;
5436
5437 while (name != NULL)
5438 {
5439 GdkColor color;
5440 int parsed;
5441 int i;
5442
5443 parsed = gdk_color_parse((const char *)name, &color);
5444
5445#ifndef HAVE_GTK2 /* ohh, lovely GTK+ 2, eases our pain :) */
5446 /*
5447 * Since we have already called gtk_set_locale here the bugger
5448 * XParseColor will accept only explicit color names in the language
Bram Moolenaar49325942007-05-10 19:19:59 +00005449 * of the current locale. However this will interfere with:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 * 1. Vim's global startup files
5451 * 2. Explicit color names in .vimrc
5452 *
5453 * Therefore we first try to parse the color in the current locale and
5454 * if it fails, we fall back to the portable "C" one.
5455 */
5456 if (!parsed)
5457 {
5458 char *current;
5459
5460 current = setlocale(LC_ALL, NULL);
5461 if (current != NULL)
5462 {
5463 char *saved;
5464
5465 saved = g_strdup(current);
5466 setlocale(LC_ALL, "C");
5467
5468 parsed = gdk_color_parse((const gchar *)name, &color);
5469
5470 setlocale(LC_ALL, saved);
5471 gtk_set_locale();
5472
5473 g_free(saved);
5474 }
5475 }
5476#endif /* !HAVE_GTK2 */
5477
5478 if (parsed)
5479 {
5480#ifdef HAVE_GTK2
5481 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
5482 &color, FALSE, TRUE);
5483#else
5484 gdk_color_alloc(gtk_widget_get_colormap(gui.drawarea), &color);
5485#endif
5486 return (guicolor_T)color.pixel;
5487 }
5488 /* add a few builtin names and try again */
5489 for (i = 0; ; ++i)
5490 {
5491 if (vimnames[i][0] == NULL)
5492 {
5493 name = NULL;
5494 break;
5495 }
5496 if (STRICMP(name, vimnames[i][0]) == 0)
5497 {
5498 name = (char_u *)vimnames[i][1];
5499 break;
5500 }
5501 }
5502 }
5503
5504 return INVALCOLOR;
5505}
5506
5507/*
5508 * Set the current text foreground color.
5509 */
5510 void
5511gui_mch_set_fg_color(guicolor_T color)
5512{
5513 gui.fgcolor->pixel = (unsigned long)color;
5514}
5515
5516/*
5517 * Set the current text background color.
5518 */
5519 void
5520gui_mch_set_bg_color(guicolor_T color)
5521{
5522 gui.bgcolor->pixel = (unsigned long)color;
5523}
5524
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005525/*
5526 * Set the current text special color.
5527 */
5528 void
5529gui_mch_set_sp_color(guicolor_T color)
5530{
5531 gui.spcolor->pixel = (unsigned long)color;
5532}
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534#ifdef HAVE_GTK2
5535/*
5536 * Function-like convenience macro for the sake of efficiency.
5537 */
5538#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5539 G_STMT_START{ \
5540 PangoAttribute *tmp_attr_; \
5541 tmp_attr_ = (Attribute); \
5542 tmp_attr_->start_index = (Start); \
5543 tmp_attr_->end_index = (End); \
5544 pango_attr_list_insert((AttrList), tmp_attr_); \
5545 }G_STMT_END
5546
5547 static void
5548apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
5549{
5550 char_u *start = NULL;
5551 char_u *p;
5552 int uc;
5553
5554 for (p = s; p < s + len; p += utf_byte2len(*p))
5555 {
5556 uc = utf_ptr2char(p);
5557
5558 if (start == NULL)
5559 {
5560 if (uc >= 0x80 && utf_char2cells(uc) == 2)
5561 start = p;
5562 }
5563 else if (uc < 0x80 /* optimization shortcut */
5564 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
5565 {
5566 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5567 attr_list, start - s, p - s);
5568 start = NULL;
5569 }
5570 }
5571
5572 if (start != NULL)
5573 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
5574 attr_list, start - s, len);
5575}
5576
5577 static int
5578count_cluster_cells(char_u *s, PangoItem *item,
5579 PangoGlyphString* glyphs, int i,
5580 int *cluster_width,
5581 int *last_glyph_rbearing)
5582{
5583 char_u *p;
5584 int next; /* glyph start index of next cluster */
5585 int start, end; /* string segment of current cluster */
5586 int width; /* real cluster width in Pango units */
5587 int uc;
5588 int cellcount = 0;
5589
5590 width = glyphs->glyphs[i].geometry.width;
5591
5592 for (next = i + 1; next < glyphs->num_glyphs; ++next)
5593 {
5594 if (glyphs->glyphs[next].attr.is_cluster_start)
5595 break;
5596 else if (glyphs->glyphs[next].geometry.width > width)
5597 width = glyphs->glyphs[next].geometry.width;
5598 }
5599
5600 start = item->offset + glyphs->log_clusters[i];
5601 end = item->offset + ((next < glyphs->num_glyphs) ?
5602 glyphs->log_clusters[next] : item->length);
5603
5604 for (p = s + start; p < s + end; p += utf_byte2len(*p))
5605 {
5606 uc = utf_ptr2char(p);
5607 if (uc < 0x80)
5608 ++cellcount;
5609 else if (!utf_iscomposing(uc))
5610 cellcount += utf_char2cells(uc);
5611 }
5612
5613 if (last_glyph_rbearing != NULL
5614 && cellcount > 0 && next == glyphs->num_glyphs)
5615 {
5616 PangoRectangle ink_rect;
5617 /*
5618 * If a certain combining mark had to be taken from a non-monospace
5619 * font, we have to compensate manually by adapting x_offset according
5620 * to the ink extents of the previous glyph.
5621 */
5622 pango_font_get_glyph_extents(item->analysis.font,
5623 glyphs->glyphs[i].glyph,
5624 &ink_rect, NULL);
5625
5626 if (PANGO_RBEARING(ink_rect) > 0)
5627 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
5628 }
5629
5630 if (cellcount > 0)
5631 *cluster_width = width;
5632
5633 return cellcount;
5634}
5635
5636/*
5637 * If there are only combining characters in the cluster, we cannot just
5638 * change the width of the previous glyph since there is none. Therefore
5639 * some guesswork is needed.
5640 *
5641 * If ink_rect.x is negative Pango apparently has taken care of the composing
5642 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5643 * to problems with composing from different fonts we still need to fine-tune
5644 * x_offset to avoid uglyness.
5645 *
5646 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5647 * the position of the previous glyph. Apparently this happens only with old
5648 * X fonts which don't provide the special combining information needed by
5649 * Pango.
5650 */
5651 static void
5652setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
5653 int last_cellcount, int last_cluster_width,
5654 int last_glyph_rbearing)
5655{
5656 PangoRectangle ink_rect;
5657 PangoRectangle logical_rect;
5658 int width;
5659
5660 width = last_cellcount * gui.char_width * PANGO_SCALE;
5661 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
5662 glyph->geometry.width = 0;
5663
5664 pango_font_get_glyph_extents(item->analysis.font,
5665 glyph->glyph,
5666 &ink_rect, &logical_rect);
5667 if (ink_rect.x < 0)
5668 {
5669 glyph->geometry.x_offset += last_glyph_rbearing;
5670 glyph->geometry.y_offset = logical_rect.height
5671 - (gui.char_height - p_linespace) * PANGO_SCALE;
5672 }
5673}
5674
5675 static void
5676draw_glyph_string(int row, int col, int num_cells, int flags,
5677 PangoFont *font, PangoGlyphString *glyphs)
5678{
5679 if (!(flags & DRAW_TRANSP))
5680 {
5681 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5682
5683 gdk_draw_rectangle(gui.drawarea->window,
5684 gui.text_gc,
5685 TRUE,
5686 FILL_X(col),
5687 FILL_Y(row),
5688 num_cells * gui.char_width,
5689 gui.char_height);
5690 }
5691
5692 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5693
5694 gdk_draw_glyphs(gui.drawarea->window,
5695 gui.text_gc,
5696 font,
5697 TEXT_X(col),
5698 TEXT_Y(row),
5699 glyphs);
5700
5701 /* redraw the contents with an offset of 1 to emulate bold */
5702 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
5703 gdk_draw_glyphs(gui.drawarea->window,
5704 gui.text_gc,
5705 font,
5706 TEXT_X(col) + 1,
5707 TEXT_Y(row),
5708 glyphs);
5709}
5710
5711#endif /* HAVE_GTK2 */
5712
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005713/*
5714 * Draw underline and undercurl at the bottom of the character cell.
5715 */
5716 static void
5717draw_under(int flags, int row, int col, int cells)
5718{
5719 int i;
5720 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005721 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005722 int y = FILL_Y(row + 1) - 1;
5723
5724 /* Undercurl: draw curl at the bottom of the character cell. */
5725 if (flags & DRAW_UNDERC)
5726 {
5727 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
5728 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
5729 {
5730 offset = val[i % 8];
5731 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
5732 }
5733 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5734 }
5735
5736 /* Underline: draw a line at the bottom of the character cell. */
5737 if (flags & DRAW_UNDERL)
5738 {
5739 /* When p_linespace is 0, overwrite the bottom row of pixels.
5740 * Otherwise put the line just below the character. */
5741 if (p_linespace > 1)
5742 y -= p_linespace - 1;
5743 gdk_draw_line(gui.drawarea->window, gui.text_gc,
5744 FILL_X(col), y,
5745 FILL_X(col + cells) - 1, y);
5746 }
5747}
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#if defined(HAVE_GTK2) || defined(PROTO)
5750 int
5751gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
5752{
5753 GdkRectangle area; /* area for clip mask */
5754 PangoGlyphString *glyphs; /* glyphs of current item */
5755 int column_offset = 0; /* column offset in cells */
5756 int i;
5757 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
5758 char_u *new_conv_buf;
5759 int convlen;
5760 char_u *sp, *bp;
5761 int plen;
5762
5763 if (gui.text_context == NULL || gui.drawarea->window == NULL)
5764 return len;
5765
5766 if (output_conv.vc_type != CONV_NONE)
5767 {
5768 /*
5769 * Convert characters from 'encoding' to 'termencoding', which is set
5770 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5771 * prohibits changing this to something else than UTF-8 if the GUI is
5772 * in use.
5773 */
5774 convlen = len;
5775 conv_buf = string_convert(&output_conv, s, &convlen);
5776 g_return_val_if_fail(conv_buf != NULL, len);
5777
5778 /* Correct for differences in char width: some chars are
5779 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5780 * compensate for that. */
5781 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
5782 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005783 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
5785 {
5786 new_conv_buf = alloc(convlen + 2);
5787 if (new_conv_buf == NULL)
5788 return len;
5789 plen += bp - conv_buf;
5790 mch_memmove(new_conv_buf, conv_buf, plen);
5791 new_conv_buf[plen] = ' ';
5792 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
5793 convlen - plen + 1);
5794 vim_free(conv_buf);
5795 conv_buf = new_conv_buf;
5796 ++convlen;
5797 bp = conv_buf + plen;
5798 plen = 1;
5799 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005800 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 bp += plen;
5802 }
5803 s = conv_buf;
5804 len = convlen;
5805 }
5806
5807 /*
5808 * Restrict all drawing to the current screen line in order to prevent
5809 * fuzzy font lookups from messing up the screen.
5810 */
5811 area.x = gui.border_offset;
5812 area.y = FILL_Y(row);
5813 area.width = gui.num_cols * gui.char_width;
5814 area.height = gui.char_height;
5815
5816 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
5817 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
5818
5819 glyphs = pango_glyph_string_new();
5820
5821 /*
5822 * Optimization hack: If possible, skip the itemize and shaping process
5823 * for pure ASCII strings. This optimization is particularly effective
5824 * because Vim draws space characters to clear parts of the screen.
5825 */
5826 if (!(flags & DRAW_ITALIC)
5827 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5828 && gui.ascii_glyphs != NULL)
5829 {
5830 char_u *p;
5831
5832 for (p = s; p < s + len; ++p)
5833 if (*p & 0x80)
5834 goto not_ascii;
5835
5836 pango_glyph_string_set_size(glyphs, len);
5837
5838 for (i = 0; i < len; ++i)
5839 {
5840 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5841 glyphs->log_clusters[i] = i;
5842 }
5843
5844 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5845
5846 column_offset = len;
5847 }
5848 else
5849not_ascii:
5850 {
5851 PangoAttrList *attr_list;
5852 GList *item_list;
5853 int cluster_width;
5854 int last_glyph_rbearing;
5855 int cells = 0; /* cells occupied by current cluster */
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005856#if 0
5857 int monospace13 = STRICMP(p_guifont, "monospace 13") == 0;
5858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005860 /* Safety check: pango crashes when invoked with invalid utf-8
5861 * characters. */
5862 if (!utf_valid_string(s, s + len))
5863 {
5864 column_offset = len;
5865 goto skipitall;
5866 }
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* original width of the current cluster */
5869 cluster_width = PANGO_SCALE * gui.char_width;
5870
5871 /* right bearing of the last non-composing glyph */
5872 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5873
5874 attr_list = pango_attr_list_new();
5875
5876 /* If 'guifontwide' is set then use that for double-width characters.
5877 * Otherwise just go with 'guifont' and let Pango do its thing. */
5878 if (gui.wide_font != NULL)
5879 apply_wide_font_attr(s, len, attr_list);
5880
5881 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5882 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5883 attr_list, 0, len);
5884 if (flags & DRAW_ITALIC)
5885 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5886 attr_list, 0, len);
5887 /*
5888 * Break the text into segments with consistent directional level
5889 * and shaping engine. Pure Latin text needs only a single segment,
5890 * so there's no need to worry about the loop's efficiency. Better
5891 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5892 */
5893 item_list = pango_itemize(gui.text_context,
5894 (const char *)s, 0, len, attr_list, NULL);
5895
5896 while (item_list != NULL)
5897 {
5898 PangoItem *item;
5899 int item_cells = 0; /* item length in cells */
5900
5901 item = (PangoItem *)item_list->data;
5902 item_list = g_list_delete_link(item_list, item_list);
5903 /*
5904 * Increment the bidirectional embedding level by 1 if it is not
5905 * even. An odd number means the output will be RTL, but we don't
5906 * want that since Vim handles right-to-left text on its own. It
5907 * would probably be sufficient to just set level = 0, but you can
5908 * never know :)
5909 *
5910 * Unfortunately we can't take advantage of Pango's ability to
5911 * render both LTR and RTL at the same time. In order to support
5912 * that, Vim's main screen engine would have to make use of Pango
5913 * functionality.
5914 */
5915 item->analysis.level = (item->analysis.level + 1) & (~1U);
5916
5917 /* HACK: Overrule the shape engine, we don't want shaping to be
5918 * done, because drawing the cursor would change the display. */
5919 item->analysis.shape_engine = default_shape_engine;
5920
5921 pango_shape((const char *)s + item->offset, item->length,
5922 &item->analysis, glyphs);
5923 /*
5924 * Fixed-width hack: iterate over the array and assign a fixed
5925 * width to each glyph, thus overriding the choice made by the
5926 * shaping engine. We use utf_char2cells() to determine the
5927 * number of cells needed.
5928 *
5929 * Also perform all kind of dark magic to get composing
5930 * characters right (and pretty too of course).
5931 */
5932 for (i = 0; i < glyphs->num_glyphs; ++i)
5933 {
5934 PangoGlyphInfo *glyph;
5935
5936 glyph = &glyphs->glyphs[i];
5937
5938 if (glyph->attr.is_cluster_start)
5939 {
5940 int cellcount;
5941
5942 cellcount = count_cluster_cells(
5943 s, item, glyphs, i, &cluster_width,
5944 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5945
5946 if (cellcount > 0)
5947 {
5948 int width;
5949
5950 width = cellcount * gui.char_width * PANGO_SCALE;
5951 glyph->geometry.x_offset +=
5952 MAX(0, width - cluster_width) / 2;
5953 glyph->geometry.width = width;
5954 }
5955 else
5956 {
5957 /* If there are only combining characters in the
5958 * cluster, we cannot just change the width of the
5959 * previous glyph since there is none. Therefore
5960 * some guesswork is needed. */
5961 setup_zero_width_cluster(item, glyph, cells,
5962 cluster_width,
5963 last_glyph_rbearing);
5964 }
5965
5966 item_cells += cellcount;
5967 cells = cellcount;
5968 }
5969 else if (i > 0)
5970 {
5971 int width;
5972
5973 /* There is a previous glyph, so we deal with combining
5974 * characters the canonical way. That is, setting the
5975 * width of the previous glyph to 0. */
5976 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 width = cells * gui.char_width * PANGO_SCALE;
5978 glyph->geometry.x_offset +=
5979 MAX(0, width - cluster_width) / 2;
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005980#if 0
5981 /* Dirty hack: for "monospace 13" font there is a bug that
5982 * draws composing chars in the wrong position. Add
5983 * "width" to the offset to work around that. */
5984 if (monospace13)
5985 glyph->geometry.x_offset = width;
5986#endif
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 glyph->geometry.width = width;
5989 }
5990 else /* i == 0 "cannot happen" */
5991 {
5992 glyph->geometry.width = 0;
5993 }
5994 }
5995
5996 /*** Aaaaand action! ***/
5997 draw_glyph_string(row, col + column_offset, item_cells,
5998 flags, item->analysis.font, glyphs);
5999
6000 pango_item_free(item);
6001
6002 column_offset += item_cells;
6003 }
6004
6005 pango_attr_list_unref(attr_list);
6006 }
6007
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006008skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006009 /* Draw underline and undercurl. */
6010 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
6012 pango_glyph_string_free(glyphs);
6013 vim_free(conv_buf);
6014
6015 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
6016
6017 return column_offset;
6018}
6019#endif /* HAVE_GTK2 */
6020
6021#if !defined(HAVE_GTK2) || defined(PROTO)
6022 void
6023gui_mch_draw_string(int row, int col, char_u *s, int len, int flags)
6024{
6025 static XChar2b *buf = NULL;
6026 static int buflen = 0;
6027 int is_wide;
6028 XChar2b *text;
6029 int textlen;
6030 XFontStruct *xfont;
6031 char_u *p;
6032# ifdef FEAT_MBYTE
6033 unsigned c;
6034# endif
6035 int width;
6036
6037 if (gui.current_font == NULL || gui.drawarea->window == NULL)
6038 return;
6039
6040 /*
6041 * Yeah yeah apparently the font support in GTK+ 1.2 only cares for either:
6042 * asians or 8-bit fonts. It is broken there, but no wonder the whole font
6043 * stuff is broken in X11 in first place. And the internationalization API
6044 * isn't something you would really like to use.
6045 */
6046
6047 xfont = (XFontStruct *)((GdkFontPrivate*)gui.current_font)->xfont;
6048 is_wide = ((xfont->min_byte1 != 0 || xfont->max_byte1 != 0)
6049# ifdef FEAT_XFONTSET
6050 && gui.fontset == NOFONTSET
6051# endif
6052 );
6053
6054 if (is_wide)
6055 {
6056 /* Convert a byte sequence to 16 bit characters for the Gdk functions.
6057 * Need a buffer for the 16 bit characters. Keep it between calls,
6058 * because allocating it each time is slow. */
6059 if (buflen < len)
6060 {
6061 XtFree((char *)buf);
6062 buf = (XChar2b *)XtMalloc(len * sizeof(XChar2b));
6063 buflen = len;
6064 }
6065
6066 p = s;
6067 textlen = 0;
6068 width = 0;
6069 while (p < s + len)
6070 {
6071# ifdef FEAT_MBYTE
6072 if (enc_utf8)
6073 {
6074 c = utf_ptr2char(p);
6075 if (c >= 0x10000) /* show chars > 0xffff as ? */
6076 c = 0xbf;
6077 buf[textlen].byte1 = c >> 8;
6078 buf[textlen].byte2 = c;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006079 p += utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 width += utf_char2cells(c);
6081 }
6082 else
6083# endif
6084 {
6085 buf[textlen].byte1 = '\0'; /* high eight bits */
6086 buf[textlen].byte2 = *p; /* low eight bits */
6087 ++p;
6088 ++width;
6089 }
6090 ++textlen;
6091 }
6092 text = buf;
6093 textlen = textlen * 2;
6094 }
6095 else
6096 {
6097 text = (XChar2b *)s;
6098 textlen = len;
6099# ifdef FEAT_MBYTE
6100 if (has_mbyte)
6101 {
6102 width = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006103 for (p = s; p < s + len; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 width += (*mb_ptr2cells)(p);
6105 }
6106 else
6107# endif
6108 width = len;
6109 }
6110
6111 if (!(flags & DRAW_TRANSP))
6112 {
6113 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6114 gdk_draw_rectangle(gui.drawarea->window,
6115 gui.text_gc,
6116 TRUE,
6117 FILL_X(col), FILL_Y(row),
6118 width * gui.char_width, gui.char_height);
6119 }
6120 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6121 gdk_draw_text(gui.drawarea->window,
6122 gui.current_font,
6123 gui.text_gc,
6124 TEXT_X(col), TEXT_Y(row),
6125 (const gchar *)text, textlen);
6126
6127 /* redraw the contents with an offset of 1 to emulate bold */
6128 if (flags & DRAW_BOLD)
6129 gdk_draw_text(gui.drawarea->window,
6130 gui.current_font,
6131 gui.text_gc,
6132 TEXT_X(col) + 1, TEXT_Y(row),
6133 (const gchar *)text, textlen);
6134
Bram Moolenaarf36d3692005-03-15 22:48:14 +00006135 /* Draw underline and undercurl. */
6136 draw_under(flags, row, col, width);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137}
6138#endif /* !HAVE_GTK2 */
6139
6140/*
6141 * Return OK if the key with the termcap name "name" is supported.
6142 */
6143 int
6144gui_mch_haskey(char_u *name)
6145{
6146 int i;
6147
6148 for (i = 0; special_keys[i].key_sym != 0; i++)
6149 if (name[0] == special_keys[i].code0
6150 && name[1] == special_keys[i].code1)
6151 return OK;
6152 return FAIL;
6153}
6154
6155#if defined(FEAT_TITLE) \
6156 || (defined(FEAT_XIM) && !defined(HAVE_GTK2)) \
6157 || defined(PROTO)
6158/*
6159 * Return the text window-id and display. Only required for X-based GUI's
6160 */
6161 int
6162gui_get_x11_windis(Window *win, Display **dis)
6163{
6164 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6165 {
6166 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6167 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
6168 return OK;
6169 }
6170
6171 *dis = NULL;
6172 *win = 0;
6173 return FAIL;
6174}
6175#endif
6176
6177#if defined(FEAT_CLIENTSERVER) \
6178 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6179
6180 Display *
6181gui_mch_get_display(void)
6182{
6183 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
6184 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
6185 else
6186 return NULL;
6187}
6188#endif
6189
6190 void
6191gui_mch_beep(void)
6192{
6193#ifdef HAVE_GTK_MULTIHEAD
6194 GdkDisplay *display;
6195
6196 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6197 display = gtk_widget_get_display(gui.mainwin);
6198 else
6199 display = gdk_display_get_default();
6200
6201 if (display != NULL)
6202 gdk_display_beep(display);
6203#else
6204 gdk_beep();
6205#endif
6206}
6207
6208 void
6209gui_mch_flash(int msec)
6210{
6211 GdkGCValues values;
6212 GdkGC *invert_gc;
6213
6214 if (gui.drawarea->window == NULL)
6215 return;
6216
6217 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6218 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
6219 values.function = GDK_XOR;
6220 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6221 &values,
6222 GDK_GC_FOREGROUND |
6223 GDK_GC_BACKGROUND |
6224 GDK_GC_FUNCTION);
6225 gdk_gc_set_exposures(invert_gc,
6226 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
6227 /*
6228 * Do a visual beep by changing back and forth in some undetermined way,
6229 * the foreground and background colors. This is due to the fact that
6230 * there can't be really any prediction about the effects of XOR on
6231 * arbitrary X11 servers. However this seems to be enough for what we
6232 * intend it to do.
6233 */
6234 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6235 TRUE,
6236 0, 0,
6237 FILL_X((int)Columns) + gui.border_offset,
6238 FILL_Y((int)Rows) + gui.border_offset);
6239
6240 gui_mch_flush();
6241 ui_delay((long)msec, TRUE); /* wait so many msec */
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 gdk_gc_destroy(invert_gc);
6250}
6251
6252/*
6253 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6254 */
6255 void
6256gui_mch_invert_rectangle(int r, int c, int nr, int nc)
6257{
6258 GdkGCValues values;
6259 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260
6261 if (gui.drawarea->window == NULL)
6262 return;
6263
Bram Moolenaar89d40322006-08-29 15:30:07 +00006264 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
6265 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 values.function = GDK_XOR;
6267 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
6268 &values,
6269 GDK_GC_FOREGROUND |
6270 GDK_GC_BACKGROUND |
6271 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00006272 gdk_gc_set_exposures(invert_gc, gui.visibility !=
6273 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
6275 TRUE,
6276 FILL_X(c), FILL_Y(r),
6277 (nc) * gui.char_width, (nr) * gui.char_height);
6278 gdk_gc_destroy(invert_gc);
6279}
6280
6281/*
6282 * Iconify the GUI window.
6283 */
6284 void
6285gui_mch_iconify(void)
6286{
6287#ifdef HAVE_GTK2
6288 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
6289#else
6290 XIconifyWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
6291 GDK_WINDOW_XWINDOW(gui.mainwin->window),
6292 DefaultScreen(GDK_WINDOW_XDISPLAY(gui.mainwin->window)));
6293#endif
6294}
6295
6296#if defined(FEAT_EVAL) || defined(PROTO)
6297/*
6298 * Bring the Vim window to the foreground.
6299 */
6300 void
6301gui_mch_set_foreground(void)
6302{
6303# ifdef HAVE_GTK2
6304 gtk_window_present(GTK_WINDOW(gui.mainwin));
6305# else
6306 gdk_window_raise(gui.mainwin->window);
6307# endif
6308}
6309#endif
6310
6311/*
6312 * Draw a cursor without focus.
6313 */
6314 void
6315gui_mch_draw_hollow_cursor(guicolor_T color)
6316{
6317 int i = 1;
6318
6319 if (gui.drawarea->window == NULL)
6320 return;
6321
6322 gui_mch_set_fg_color(color);
6323
6324 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6325#ifdef FEAT_MBYTE
6326 if (mb_lefthalve(gui.row, gui.col))
6327 i = 2;
6328#endif
6329 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6330 FALSE,
6331 FILL_X(gui.col), FILL_Y(gui.row),
6332 i * gui.char_width - 1, gui.char_height - 1);
6333}
6334
6335/*
6336 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6337 * color "color".
6338 */
6339 void
6340gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
6341{
6342 if (gui.drawarea->window == NULL)
6343 return;
6344
6345 gui_mch_set_fg_color(color);
6346
6347 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6348 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
6349 TRUE,
6350#ifdef FEAT_RIGHTLEFT
6351 /* vertical line should be on the right of current point */
6352 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
6353#endif
6354 FILL_X(gui.col),
6355 FILL_Y(gui.row) + gui.char_height - h,
6356 w, h);
6357}
6358
6359
6360/*
6361 * Catch up with any queued X11 events. This may put keyboard input into the
6362 * input buffer, call resize call-backs, trigger timers etc. If there is
6363 * nothing in the X11 event queue (& no timers pending), then we return
6364 * immediately.
6365 */
6366 void
6367gui_mch_update(void)
6368{
6369 while (gtk_events_pending() && !vim_is_input_buf_full())
6370 gtk_main_iteration_do(FALSE);
6371}
6372
6373 static gint
6374input_timer_cb(gpointer data)
6375{
6376 int *timed_out = (int *) data;
6377
Bram Moolenaar49325942007-05-10 19:19:59 +00006378 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 *timed_out = TRUE;
6380
6381 if (gtk_main_level() > 0)
6382 gtk_main_quit();
6383
6384 return FALSE; /* don't happen again */
6385}
6386
6387#ifdef FEAT_SNIFF
6388/*
6389 * Callback function, used when data is available on the SNiFF connection.
6390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 static void
6392sniff_request_cb(
6393 gpointer data,
6394 gint source_fd,
6395 GdkInputCondition condition)
6396{
6397 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
6398
6399 add_to_input_buf(bytes, 3);
6400
6401 if (gtk_main_level() > 0)
6402 gtk_main_quit();
6403}
6404#endif
6405
6406/*
6407 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6408 * from the keyboard.
6409 * wtime == -1 Wait forever.
6410 * wtime == 0 This should never happen.
6411 * wtime > 0 Wait wtime milliseconds for a character.
6412 * Returns OK if a character was found to be available within the given time,
6413 * or FAIL otherwise.
6414 */
6415 int
6416gui_mch_wait_for_chars(long wtime)
6417{
6418 int focus;
6419 guint timer;
6420 static int timed_out;
6421#ifdef FEAT_SNIFF
6422 static int sniff_on = 0;
6423 static gint sniff_input_id = 0;
6424#endif
6425
6426#ifdef FEAT_SNIFF
6427 if (sniff_on && !want_sniff_request)
6428 {
6429 if (sniff_input_id)
6430 gdk_input_remove(sniff_input_id);
6431 sniff_on = 0;
6432 }
6433 else if (!sniff_on && want_sniff_request)
6434 {
6435 /* Add fd_from_sniff to watch for available data in main loop. */
6436 sniff_input_id = gdk_input_add(fd_from_sniff,
6437 GDK_INPUT_READ, sniff_request_cb, NULL);
6438 sniff_on = 1;
6439 }
6440#endif
6441
6442 timed_out = FALSE;
6443
6444 /* this timeout makes sure that we will return if no characters arrived in
6445 * time */
6446
6447 if (wtime > 0)
6448 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
6449 else
6450 timer = 0;
6451
6452 focus = gui.in_focus;
6453
6454 do
6455 {
6456 /* Stop or start blinking when focus changes */
6457 if (gui.in_focus != focus)
6458 {
6459 if (gui.in_focus)
6460 gui_mch_start_blink();
6461 else
6462 gui_mch_stop_blink();
6463 focus = gui.in_focus;
6464 }
6465
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006466#if defined(FEAT_NETBEANS_INTG)
6467 /* Process the queued netbeans messages. */
6468 netbeans_parse_messages();
6469#endif
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 /*
6472 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006473 * Skip this if input is available anyway (can happen in rare
6474 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006476 if (!input_available())
6477 gtk_main();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
6479 /* Got char, return immediately */
6480 if (input_available())
6481 {
6482 if (timer != 0 && !timed_out)
6483 gtk_timeout_remove(timer);
6484 return OK;
6485 }
6486 } while (wtime < 0 || !timed_out);
6487
6488 /*
6489 * Flush all eventually pending (drawing) events.
6490 */
6491 gui_mch_update();
6492
6493 return FAIL;
6494}
6495
6496
6497/****************************************************************************
6498 * Output drawing routines.
6499 ****************************************************************************/
6500
6501
6502/* Flush any output to the screen */
6503 void
6504gui_mch_flush(void)
6505{
6506#ifdef HAVE_GTK_MULTIHEAD
6507 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
6508 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
6509#else
6510 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6511#endif
6512#ifdef HAVE_GTK2
6513 /* This happens to actually do what gui_mch_flush() is supposed to do,
6514 * according to the comment above. */
6515 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
6516 gdk_window_process_updates(gui.drawarea->window, FALSE);
6517#endif
6518}
6519
6520/*
6521 * Clear a rectangular region of the screen from text pos (row1, col1) to
6522 * (row2, col2) inclusive.
6523 */
6524 void
6525gui_mch_clear_block(int row1, int col1, int row2, int col2)
6526{
6527 GdkColor color;
6528
6529 if (gui.drawarea->window == NULL)
6530 return;
6531
6532 color.pixel = gui.back_pixel;
6533
6534 gdk_gc_set_foreground(gui.text_gc, &color);
6535
6536 /* Clear one extra pixel at the far right, for when bold characters have
6537 * spilled over to the window border. */
6538 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
6539 FILL_X(col1), FILL_Y(row1),
6540 (col2 - col1 + 1) * gui.char_width
6541 + (col2 == Columns - 1),
6542 (row2 - row1 + 1) * gui.char_height);
6543}
6544
6545 void
6546gui_mch_clear_all(void)
6547{
6548 if (gui.drawarea->window != NULL)
6549 gdk_window_clear(gui.drawarea->window);
6550}
6551
6552/*
6553 * Redraw any text revealed by scrolling up/down.
6554 */
6555 static void
6556check_copy_area(void)
6557{
6558 GdkEvent *event;
6559 int expose_count;
6560
6561 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
6562 return;
6563
6564 /* Avoid redrawing the cursor while scrolling or it'll end up where
6565 * we don't want it to be. I'm not sure if it's correct to call
6566 * gui_dont_update_cursor() at this point but it works as a quick
6567 * fix for now. */
6568 gui_dont_update_cursor();
6569
6570 do
6571 {
6572 /* Wait to check whether the scroll worked or not. */
6573 event = gdk_event_get_graphics_expose(gui.drawarea->window);
6574
6575 if (event == NULL)
6576 break; /* received NoExpose event */
6577
6578 gui_redraw(event->expose.area.x, event->expose.area.y,
6579 event->expose.area.width, event->expose.area.height);
6580
6581 expose_count = event->expose.count;
6582 gdk_event_free(event);
6583 }
6584 while (expose_count > 0); /* more events follow */
6585
6586 gui_can_update_cursor();
6587}
6588
6589/*
6590 * Delete the given number of lines from the given row, scrolling up any
6591 * text further down within the scroll region.
6592 */
6593 void
6594gui_mch_delete_lines(int row, int num_lines)
6595{
6596 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6597 return; /* Can't see the window */
6598
6599 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6600 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6601
6602 /* copy one extra pixel, for when bold has spilled over */
6603 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6604 FILL_X(gui.scroll_region_left), FILL_Y(row),
6605 gui.drawarea->window,
6606 FILL_X(gui.scroll_region_left),
6607 FILL_Y(row + num_lines),
6608 gui.char_width * (gui.scroll_region_right
6609 - gui.scroll_region_left + 1) + 1,
6610 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6611
6612 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
6613 gui.scroll_region_left,
6614 gui.scroll_region_bot, gui.scroll_region_right);
6615 check_copy_area();
6616}
6617
6618/*
6619 * Insert the given number of lines before the given row, scrolling down any
6620 * following text within the scroll region.
6621 */
6622 void
6623gui_mch_insert_lines(int row, int num_lines)
6624{
6625 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
6626 return; /* Can't see the window */
6627
6628 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
6629 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
6630
6631 /* copy one extra pixel, for when bold has spilled over */
6632 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
6633 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
6634 gui.drawarea->window,
6635 FILL_X(gui.scroll_region_left), FILL_Y(row),
6636 gui.char_width * (gui.scroll_region_right
6637 - gui.scroll_region_left + 1) + 1,
6638 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
6639
6640 gui_clear_block(row, gui.scroll_region_left,
6641 row + num_lines - 1, gui.scroll_region_right);
6642 check_copy_area();
6643}
6644
6645/*
6646 * X Selection stuff, for cutting and pasting text to other windows.
6647 */
6648 void
6649clip_mch_request_selection(VimClipboard *cbd)
6650{
6651 GdkAtom target;
6652 unsigned i;
6653 int nbytes;
6654 char_u *buffer;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006655 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656
6657 for (i = 0; i < N_SELECTION_TARGETS; ++i)
6658 {
6659 received_selection = RS_NONE;
6660 target = gdk_atom_intern(selection_targets[i].target, FALSE);
6661
6662 gtk_selection_convert(gui.drawarea,
6663 cbd->gtk_sel_atom, target,
6664 (guint32)GDK_CURRENT_TIME);
6665
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00006666 /* Hack: Wait up to three seconds for the selection. A hang was
6667 * noticed here when using the netrw plugin combined with ":gui"
6668 * during the FocusGained event. */
6669 start = time(NULL);
6670 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 gtk_main(); /* wait for selection_received_cb */
6672
6673 if (received_selection != RS_FAIL)
6674 return;
6675 }
6676
6677 /* Final fallback position - use the X CUT_BUFFER0 store */
6678 nbytes = 0;
6679 buffer = (char_u *)XFetchBuffer(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
6680 &nbytes, 0);
6681 if (nbytes > 0)
6682 {
6683 /* Got something */
6684 clip_yank_selection(MCHAR, buffer, (long)nbytes, cbd);
6685 if (p_verbose > 0)
Bram Moolenaar54ee7752005-05-31 22:22:17 +00006686 {
6687 verbose_enter();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 smsg((char_u *)_("Used CUT_BUFFER0 instead of empty selection"));
Bram Moolenaar54ee7752005-05-31 22:22:17 +00006689 verbose_leave();
6690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 }
6692 if (buffer != NULL)
6693 XFree(buffer);
6694}
6695
6696/*
6697 * Disown the selection.
6698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006700clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 /* WEIRD: when using NULL to actually disown the selection, we lose the
6703 * selection the first time we own it. */
6704 /*
6705 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
6706 gui_mch_update();
6707 */
6708}
6709
6710/*
6711 * Own the selection and return OK if it worked.
6712 */
6713 int
6714clip_mch_own_selection(VimClipboard *cbd)
6715{
6716 int success;
6717
6718 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
6719 (guint32)GDK_CURRENT_TIME);
6720 gui_mch_update();
6721 return (success) ? OK : FAIL;
6722}
6723
6724/*
6725 * Send the current selection to the clipboard. Do nothing for X because we
6726 * will fill in the selection only when requested by another app.
6727 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006729clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
6731}
6732
6733
6734#if defined(FEAT_MENU) || defined(PROTO)
6735/*
6736 * Make a menu item appear either active or not active (grey or not grey).
6737 */
6738 void
6739gui_mch_menu_grey(vimmenu_T *menu, int grey)
6740{
6741 if (menu->id == NULL)
6742 return;
6743
6744 if (menu_is_separator(menu->name))
6745 grey = TRUE;
6746
6747 gui_mch_menu_hidden(menu, FALSE);
6748 /* Be clever about bitfields versus true booleans here! */
6749 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
6750 {
6751 gtk_widget_set_sensitive(menu->id, !grey);
6752 gui_mch_update();
6753 }
6754}
6755
6756/*
6757 * Make menu item hidden or not hidden.
6758 */
6759 void
6760gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
6761{
6762 if (menu->id == 0)
6763 return;
6764
6765 if (hidden)
6766 {
6767 if (GTK_WIDGET_VISIBLE(menu->id))
6768 {
6769 gtk_widget_hide(menu->id);
6770 gui_mch_update();
6771 }
6772 }
6773 else
6774 {
6775 if (!GTK_WIDGET_VISIBLE(menu->id))
6776 {
6777 gtk_widget_show(menu->id);
6778 gui_mch_update();
6779 }
6780 }
6781}
6782
6783/*
6784 * This is called after setting all the menus to grey/hidden or not.
6785 */
6786 void
6787gui_mch_draw_menubar(void)
6788{
6789 /* just make sure that the visual changes get effect immediately */
6790 gui_mch_update();
6791}
6792#endif /* FEAT_MENU */
6793
6794/*
6795 * Scrollbar stuff.
6796 */
6797 void
6798gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
6799{
6800 if (sb->id == NULL)
6801 return;
6802
6803 if (flag)
6804 gtk_widget_show(sb->id);
6805 else
6806 gtk_widget_hide(sb->id);
6807
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00006808 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809}
6810
6811
6812/*
6813 * Return the RGB value of a pixel as long.
6814 */
6815 long_u
6816gui_mch_get_rgb(guicolor_T pixel)
6817{
6818 GdkColor color;
6819#ifndef HAVE_GTK2
6820 GdkColorContext *cc;
6821
6822 cc = gdk_color_context_new(gtk_widget_get_visual(gui.drawarea),
6823 gtk_widget_get_colormap(gui.drawarea));
6824 color.pixel = pixel;
6825 gdk_color_context_query_color(cc, &color);
6826
6827 gdk_color_context_free(cc);
6828#else
6829 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
6830 (unsigned long)pixel, &color);
6831#endif
6832
6833 return (((unsigned)color.red & 0xff00) << 8)
6834 | ((unsigned)color.green & 0xff00)
6835 | (((unsigned)color.blue & 0xff00) >> 8);
6836}
6837
6838/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006839 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006841 void
6842gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006844 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845}
6846
6847 void
6848gui_mch_setmouse(int x, int y)
6849{
6850 /* Sorry for the Xlib call, but we can't avoid it, since there is no
6851 * internal GDK mechanism present to accomplish this. (and for good
6852 * reason...) */
6853 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
6854 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
6855 0, 0, 0U, 0U, x, y);
6856}
6857
6858
6859#ifdef FEAT_MOUSESHAPE
6860/* The last set mouse pointer shape is remembered, to be used when it goes
6861 * from hidden to not hidden. */
6862static int last_shape = 0;
6863#endif
6864
6865/*
6866 * Use the blank mouse pointer or not.
6867 *
6868 * hide: TRUE = use blank ptr, FALSE = use parent ptr
6869 */
6870 void
6871gui_mch_mousehide(int hide)
6872{
6873 if (gui.pointer_hidden != hide)
6874 {
6875 gui.pointer_hidden = hide;
6876 if (gui.drawarea->window && gui.blank_pointer != NULL)
6877 {
6878 if (hide)
6879 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
6880 else
6881#ifdef FEAT_MOUSESHAPE
6882 mch_set_mouse_shape(last_shape);
6883#else
6884 gdk_window_set_cursor(gui.drawarea->window, NULL);
6885#endif
6886 }
6887 }
6888}
6889
6890#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
6891
6892/* Table for shape IDs. Keep in sync with the mshape_names[] table in
6893 * misc2.c! */
6894static const int mshape_ids[] =
6895{
6896 GDK_LEFT_PTR, /* arrow */
6897 GDK_CURSOR_IS_PIXMAP, /* blank */
6898 GDK_XTERM, /* beam */
6899 GDK_SB_V_DOUBLE_ARROW, /* updown */
6900 GDK_SIZING, /* udsizing */
6901 GDK_SB_H_DOUBLE_ARROW, /* leftright */
6902 GDK_SIZING, /* lrsizing */
6903 GDK_WATCH, /* busy */
6904 GDK_X_CURSOR, /* no */
6905 GDK_CROSSHAIR, /* crosshair */
6906 GDK_HAND1, /* hand1 */
6907 GDK_HAND2, /* hand2 */
6908 GDK_PENCIL, /* pencil */
6909 GDK_QUESTION_ARROW, /* question */
6910 GDK_RIGHT_PTR, /* right-arrow */
6911 GDK_CENTER_PTR, /* up-arrow */
6912 GDK_LEFT_PTR /* last one */
6913};
6914
6915 void
6916mch_set_mouse_shape(int shape)
6917{
6918 int id;
6919 GdkCursor *c;
6920
6921 if (gui.drawarea->window == NULL)
6922 return;
6923
6924 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
6925 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
6926 else
6927 {
6928 if (shape >= MSHAPE_NUMBERED)
6929 {
6930 id = shape - MSHAPE_NUMBERED;
6931 if (id >= GDK_LAST_CURSOR)
6932 id = GDK_LEFT_PTR;
6933 else
6934 id &= ~1; /* they are always even (why?) */
6935 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00006936 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00006938 else
6939 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940# ifdef HAVE_GTK_MULTIHEAD
6941 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00006942 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00006944 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945# endif
6946 gdk_window_set_cursor(gui.drawarea->window, c);
6947 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
6948 }
6949 if (shape != MSHAPE_HIDE)
6950 last_shape = shape;
6951}
6952#endif /* FEAT_MOUSESHAPE */
6953
6954
6955#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6956/*
6957 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
6958 * scaled down if the current font is not big enough, or scaled up if the image
6959 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
6960 * will be cut off if the current font is not big enough, or centered if it's
6961 * too small.
6962 */
6963# define SIGN_WIDTH (2 * gui.char_width)
6964# define SIGN_HEIGHT (gui.char_height)
6965# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
6966
6967# ifdef HAVE_GTK2
6968
6969 void
6970gui_mch_drawsign(int row, int col, int typenr)
6971{
6972 GdkPixbuf *sign;
6973
6974 sign = (GdkPixbuf *)sign_get_image(typenr);
6975
6976 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
6977 {
6978 int width;
6979 int height;
6980 int xoffset;
6981 int yoffset;
6982 int need_scale;
6983
6984 width = gdk_pixbuf_get_width(sign);
6985 height = gdk_pixbuf_get_height(sign);
6986 /*
6987 * Decide whether we need to scale. Allow one pixel of border
6988 * width to be cut off, in order to avoid excessive scaling for
6989 * tiny differences in font size.
6990 */
6991 need_scale = (width > SIGN_WIDTH + 2
6992 || height > SIGN_HEIGHT + 2
6993 || (width < 3 * SIGN_WIDTH / 4
6994 && height < 3 * SIGN_HEIGHT / 4));
6995 if (need_scale)
6996 {
6997 double aspect;
6998
6999 /* Keep the original aspect ratio */
7000 aspect = (double)height / (double)width;
7001 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
7002 width = MIN(width, SIGN_WIDTH);
7003 height = (double)width * aspect;
7004
7005 /* This doesn't seem to be worth caching, and doing so
7006 * would complicate the code quite a bit. */
7007 sign = gdk_pixbuf_scale_simple(sign, width, height,
7008 GDK_INTERP_BILINEAR);
7009 if (sign == NULL)
7010 return; /* out of memory */
7011 }
7012
7013 /* The origin is the upper-left corner of the pixmap. Therefore
7014 * these offset may become negative if the pixmap is smaller than
7015 * the 2x1 cells reserved for the sign icon. */
7016 xoffset = (width - SIGN_WIDTH) / 2;
7017 yoffset = (height - SIGN_HEIGHT) / 2;
7018
7019 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7020
7021 gdk_draw_rectangle(gui.drawarea->window,
7022 gui.text_gc,
7023 TRUE,
7024 FILL_X(col),
7025 FILL_Y(row),
7026 SIGN_WIDTH,
7027 SIGN_HEIGHT);
7028
7029# if GTK_CHECK_VERSION(2,1,1)
7030 gdk_draw_pixbuf(gui.drawarea->window,
7031 NULL,
7032 sign,
7033 MAX(0, xoffset),
7034 MAX(0, yoffset),
7035 FILL_X(col) - MIN(0, xoffset),
7036 FILL_Y(row) - MIN(0, yoffset),
7037 MIN(width, SIGN_WIDTH),
7038 MIN(height, SIGN_HEIGHT),
7039 GDK_RGB_DITHER_NORMAL,
7040 0, 0);
7041# else
7042 gdk_pixbuf_render_to_drawable_alpha(sign,
7043 gui.drawarea->window,
7044 MAX(0, xoffset),
7045 MAX(0, yoffset),
7046 FILL_X(col) - MIN(0, xoffset),
7047 FILL_Y(row) - MIN(0, yoffset),
7048 MIN(width, SIGN_WIDTH),
7049 MIN(height, SIGN_HEIGHT),
7050 GDK_PIXBUF_ALPHA_BILEVEL,
7051 127,
7052 GDK_RGB_DITHER_NORMAL,
7053 0, 0);
7054# endif
7055 if (need_scale)
7056 g_object_unref(sign);
7057 }
7058}
7059
7060 void *
7061gui_mch_register_sign(char_u *signfile)
7062{
7063 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
7064 {
7065 GdkPixbuf *sign;
7066 GError *error = NULL;
7067 char_u *message;
7068
7069 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
7070
7071 if (error == NULL)
7072 return sign;
7073
7074 message = (char_u *)error->message;
7075
7076 if (message != NULL && input_conv.vc_type != CONV_NONE)
7077 message = string_convert(&input_conv, message, NULL);
7078
7079 if (message != NULL)
7080 {
7081 /* The error message is already translated and will be more
7082 * descriptive than anything we could possibly do ourselves. */
7083 EMSG2("E255: %s", message);
7084
7085 if (input_conv.vc_type != CONV_NONE)
7086 vim_free(message);
7087 }
7088 g_error_free(error);
7089 }
7090
7091 return NULL;
7092}
7093
7094 void
7095gui_mch_destroy_sign(void *sign)
7096{
7097 if (sign != NULL)
7098 g_object_unref(sign);
7099}
7100
7101# else /* !HAVE_GTK2 */
7102
7103typedef struct
7104{
7105 GdkPixmap *pixmap;
7106 GdkBitmap *mask;
7107}
7108signicon_T;
7109
7110 void
7111gui_mch_drawsign(int row, int col, int typenr)
7112{
7113 signicon_T *sign;
7114
7115 sign = (signicon_T *)sign_get_image(typenr);
7116
7117 if (sign != NULL && sign->pixmap != NULL
7118 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7119 {
7120 int width;
7121 int height;
7122 int xoffset;
7123 int yoffset;
7124
7125 gdk_window_get_size(sign->pixmap, &width, &height);
7126
7127 /* The origin is the upper-left corner of the pixmap. Therefore
7128 * these offset may become negative if the pixmap is smaller than
7129 * the 2x1 cells reserved for the sign icon. */
7130 xoffset = (width - SIGN_WIDTH) / 2;
7131 yoffset = (height - SIGN_HEIGHT) / 2;
7132
7133 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
7134
7135 gdk_draw_rectangle(gui.drawarea->window,
7136 gui.text_gc,
7137 TRUE,
7138 FILL_X(col),
7139 FILL_Y(row),
7140 SIGN_WIDTH,
7141 SIGN_HEIGHT);
7142
7143 /* Set the clip mask for bilevel transparency */
7144 if (sign->mask != NULL)
7145 {
7146 gdk_gc_set_clip_origin(gui.text_gc,
7147 FILL_X(col) - xoffset,
7148 FILL_Y(row) - yoffset);
7149 gdk_gc_set_clip_mask(gui.text_gc, sign->mask);
7150 }
7151
7152 gdk_draw_pixmap(gui.drawarea->window,
7153 gui.text_gc,
7154 sign->pixmap,
7155 MAX(0, xoffset),
7156 MAX(0, yoffset),
7157 FILL_X(col) - MIN(0, xoffset),
7158 FILL_Y(row) - MIN(0, yoffset),
7159 MIN(width, SIGN_WIDTH),
7160 MIN(height, SIGN_HEIGHT));
7161
7162 gdk_gc_set_clip_mask(gui.text_gc, NULL);
7163 }
7164}
7165
7166 void *
7167gui_mch_register_sign(char_u *signfile)
7168{
7169 signicon_T *sign = NULL;
7170
7171 if (signfile[0] != NUL && signfile[0] != '-'
7172 && gui.drawarea != NULL && gui.drawarea->window != NULL)
7173 {
7174 sign = (signicon_T *)alloc(sizeof(signicon_T));
7175
7176 if (sign != NULL) /* NULL == OOM == "cannot really happen" */
7177 {
7178 sign->mask = NULL;
7179 sign->pixmap = gdk_pixmap_colormap_create_from_xpm(
7180 gui.drawarea->window, NULL,
7181 &sign->mask, NULL,
7182 (const char *)signfile);
7183
7184 if (sign->pixmap == NULL)
7185 {
7186 vim_free(sign);
7187 sign = NULL;
7188 EMSG(_(e_signdata));
7189 }
7190 }
7191 }
7192 return sign;
7193}
7194
7195 void
7196gui_mch_destroy_sign(void *sign)
7197{
7198 if (sign != NULL)
7199 {
7200 signicon_T *signicon = (signicon_T *)sign;
7201
7202 if (signicon->pixmap != NULL)
7203 gdk_pixmap_unref(signicon->pixmap);
7204 if (signicon->mask != NULL)
7205 gdk_bitmap_unref(signicon->mask);
7206
7207 vim_free(signicon);
7208 }
7209}
7210# endif /* !HAVE_GTK2 */
7211
7212#endif /* FEAT_SIGN_ICONS */