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