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