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