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