blob: 23651bdb6f0c6436ef0da1526cf454a2b802b0af [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/* missing prototype in bonobo-dock-item.h */
49extern void bonobo_dock_item_set_behavior(BonoboDockItem *dock_item, BonoboDockItemBehavior beh);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
51
52#if !defined(FEAT_GUI_GTK) && defined(PROTO)
53/* When generating prototypes we don't want syntax errors. */
54# define GdkAtom int
55# define GdkEventExpose int
56# define GdkEventFocus int
57# define GdkEventVisibility int
58# define GdkEventProperty int
59# define GtkContainer int
60# define GtkTargetEntry int
61# define GtkType int
62# define GtkWidget int
63# define gint int
64# define gpointer int
65# define guint int
66# define GdkEventKey int
67# define GdkEventSelection int
68# define GtkSelectionData int
69# define GdkEventMotion int
70# define GdkEventButton int
71# define GdkDragContext int
72# define GdkEventConfigure int
73# define GdkEventClient int
74#else
75# include <gdk/gdkkeysyms.h>
76# include <gdk/gdk.h>
77# ifdef WIN3264
78# include <gdk/gdkwin32.h>
79# else
80# include <gdk/gdkx.h>
81# endif
82
83# include <gtk/gtk.h>
84# include "gui_gtk_f.h"
85#endif
86
87#ifdef HAVE_X11_SUNKEYSYM_H
88# include <X11/Sunkeysym.h>
Bram Moolenaar7cfea752010-06-22 06:07:12 +020089static guint32 clipboard_event_time = CurrentTime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
91
92/*
93 * Easy-to-use macro for multihead support.
94 */
95#ifdef HAVE_GTK_MULTIHEAD
96# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
97 gtk_widget_get_display(gui.mainwin), atom)
98#else
99# define GET_X_ATOM(atom) ((Atom)(atom))
100#endif
101
102/* Selection type distinguishers */
103enum
104{
105 TARGET_TYPE_NONE,
106 TARGET_UTF8_STRING,
107 TARGET_STRING,
108 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000109 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110 TARGET_TEXT,
111 TARGET_TEXT_URI_LIST,
112 TARGET_TEXT_PLAIN,
113 TARGET_VIM,
114 TARGET_VIMENC
115};
116
117/*
118 * Table of selection targets supported by Vim.
119 * Note: Order matters, preferred types should come first.
120 */
121static const GtkTargetEntry selection_targets[] =
122{
123 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
124 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000125 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
128 {"TEXT", 0, TARGET_TEXT},
129 {"STRING", 0, TARGET_STRING}
130};
131#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
132
133#ifdef FEAT_DND
134/*
135 * Table of DnD targets supported by Vim.
136 * Note: Order matters, preferred types should come first.
137 */
138static const GtkTargetEntry dnd_targets[] =
139{
140 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000141 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 {"STRING", 0, TARGET_STRING},
144 {"text/plain", 0, TARGET_TEXT_PLAIN}
145};
146# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
147#endif
148
149
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150/*
151 * "Monospace" is a standard font alias that should be present
152 * on all proper Pango/fontconfig installations.
153 */
154# define DEFAULT_FONT "Monospace 10"
155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
157/*
158 * Atoms used to communicate save-yourself from the X11 session manager. There
159 * is no need to move them into the GUI struct, since they should be constant.
160 */
161static GdkAtom wm_protocols_atom = GDK_NONE;
162static GdkAtom save_yourself_atom = GDK_NONE;
163#endif
164
165/*
166 * Atoms used to control/reference X11 selections.
167 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000168static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172
173/*
174 * Keycodes recognized by vim.
175 * NOTE: when changing this, the table in gui_x11.c probably needs the same
176 * change!
177 */
178static struct special_key
179{
180 guint key_sym;
181 char_u code0;
182 char_u code1;
183}
184const special_keys[] =
185{
186 {GDK_Up, 'k', 'u'},
187 {GDK_Down, 'k', 'd'},
188 {GDK_Left, 'k', 'l'},
189 {GDK_Right, 'k', 'r'},
190 {GDK_F1, 'k', '1'},
191 {GDK_F2, 'k', '2'},
192 {GDK_F3, 'k', '3'},
193 {GDK_F4, 'k', '4'},
194 {GDK_F5, 'k', '5'},
195 {GDK_F6, 'k', '6'},
196 {GDK_F7, 'k', '7'},
197 {GDK_F8, 'k', '8'},
198 {GDK_F9, 'k', '9'},
199 {GDK_F10, 'k', ';'},
200 {GDK_F11, 'F', '1'},
201 {GDK_F12, 'F', '2'},
202 {GDK_F13, 'F', '3'},
203 {GDK_F14, 'F', '4'},
204 {GDK_F15, 'F', '5'},
205 {GDK_F16, 'F', '6'},
206 {GDK_F17, 'F', '7'},
207 {GDK_F18, 'F', '8'},
208 {GDK_F19, 'F', '9'},
209 {GDK_F20, 'F', 'A'},
210 {GDK_F21, 'F', 'B'},
211 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
212 {GDK_F22, 'F', 'C'},
213 {GDK_F23, 'F', 'D'},
214 {GDK_F24, 'F', 'E'},
215 {GDK_F25, 'F', 'F'},
216 {GDK_F26, 'F', 'G'},
217 {GDK_F27, 'F', 'H'},
218 {GDK_F28, 'F', 'I'},
219 {GDK_F29, 'F', 'J'},
220 {GDK_F30, 'F', 'K'},
221 {GDK_F31, 'F', 'L'},
222 {GDK_F32, 'F', 'M'},
223 {GDK_F33, 'F', 'N'},
224 {GDK_F34, 'F', 'O'},
225 {GDK_F35, 'F', 'P'},
226#ifdef SunXK_F36
227 {SunXK_F36, 'F', 'Q'},
228 {SunXK_F37, 'F', 'R'},
229#endif
230 {GDK_Help, '%', '1'},
231 {GDK_Undo, '&', '8'},
232 {GDK_BackSpace, 'k', 'b'},
233 {GDK_Insert, 'k', 'I'},
234 {GDK_Delete, 'k', 'D'},
235 {GDK_3270_BackTab, 'k', 'B'},
236 {GDK_Clear, 'k', 'C'},
237 {GDK_Home, 'k', 'h'},
238 {GDK_End, '@', '7'},
239 {GDK_Prior, 'k', 'P'},
240 {GDK_Next, 'k', 'N'},
241 {GDK_Print, '%', '9'},
242 /* Keypad keys: */
243 {GDK_KP_Left, 'k', 'l'},
244 {GDK_KP_Right, 'k', 'r'},
245 {GDK_KP_Up, 'k', 'u'},
246 {GDK_KP_Down, 'k', 'd'},
247 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
248 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
249 {GDK_KP_Home, 'K', '1'},
250 {GDK_KP_End, 'K', '4'},
251 {GDK_KP_Prior, 'K', '3'}, /* page up */
252 {GDK_KP_Next, 'K', '5'}, /* page down */
253
254 {GDK_KP_Add, 'K', '6'},
255 {GDK_KP_Subtract, 'K', '7'},
256 {GDK_KP_Divide, 'K', '8'},
257 {GDK_KP_Multiply, 'K', '9'},
258 {GDK_KP_Enter, 'K', 'A'},
259 {GDK_KP_Decimal, 'K', 'B'},
260
261 {GDK_KP_0, 'K', 'C'},
262 {GDK_KP_1, 'K', 'D'},
263 {GDK_KP_2, 'K', 'E'},
264 {GDK_KP_3, 'K', 'F'},
265 {GDK_KP_4, 'K', 'G'},
266 {GDK_KP_5, 'K', 'H'},
267 {GDK_KP_6, 'K', 'I'},
268 {GDK_KP_7, 'K', 'J'},
269 {GDK_KP_8, 'K', 'K'},
270 {GDK_KP_9, 'K', 'L'},
271
272 /* End of list marker: */
273 {0, 0, 0}
274};
275
276/*
277 * Flags for command line options table below.
278 */
279#define ARG_FONT 1
280#define ARG_GEOMETRY 2
281#define ARG_REVERSE 3
282#define ARG_NOREVERSE 4
283#define ARG_BACKGROUND 5
284#define ARG_FOREGROUND 6
285#define ARG_ICONIC 7
286#define ARG_ROLE 8
287#define ARG_NETBEANS 9
288#define ARG_XRM 10 /* ignored */
289#define ARG_MENUFONT 11 /* ignored */
290#define ARG_INDEX_MASK 0x00ff
291#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
292#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
293#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
294#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
295#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
296
297/*
298 * This table holds all the X GUI command line options allowed. This includes
299 * the standard ones so that we can skip them when Vim is started without the
300 * GUI (but the GUI might start up later).
301 *
302 * When changing this, also update doc/gui_x11.txt and the usage message!!!
303 */
304typedef struct
305{
306 const char *name;
307 unsigned int flags;
308}
309cmdline_option_T;
310
311static const cmdline_option_T cmdline_options[] =
312{
313 /* We handle these options ourselves */
314 {"-fn", ARG_FONT|ARG_HAS_VALUE},
315 {"-font", ARG_FONT|ARG_HAS_VALUE},
316 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
317 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
318 {"-rv", ARG_REVERSE},
319 {"-reverse", ARG_REVERSE},
320 {"+rv", ARG_NOREVERSE},
321 {"+reverse", ARG_NOREVERSE},
322 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
323 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
324 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
325 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
326 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_NETBEANS_INTG
329 {"-nb", ARG_NETBEANS}, /* non-standard value format */
330 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
331 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
332 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 /* Arguments handled by GTK (and GNOME) internally. */
335 {"--g-fatal-warnings", ARG_FOR_GTK},
336 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
337 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
338 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
339 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
340 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
341 {"--sync", ARG_FOR_GTK},
342 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
343 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
344 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
346 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
347 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#ifdef FEAT_GUI_GNOME
349 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
352 {"--sm-disable", ARG_FOR_GTK},
353 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
355 {"--oaf-private", ARG_FOR_GTK},
356 {"--enable-sound", ARG_FOR_GTK},
357 {"--disable-sound", ARG_FOR_GTK},
358 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
359 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
360 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
361 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
362# if 0 /* conflicts with Vim's own --version argument */
363 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
364# endif
365 {"--disable-crash-dialog", ARG_FOR_GTK},
366#endif
367 {NULL, 0}
368};
369
370static int gui_argc = 0;
371static char **gui_argv = NULL;
372
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
375static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000376static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377#endif
378static int found_iconic_arg = FALSE;
379
380#ifdef FEAT_GUI_GNOME
381/*
382 * Can't use Gnome if --socketid given
383 */
384static int using_gnome = 0;
385#else
386# define using_gnome 0
387#endif
388
389/*
390 * Parse the GUI related command-line arguments. Any arguments used are
391 * deleted from argv, and *argc is decremented accordingly. This is called
392 * when vim is started, whether or not the GUI has been started.
393 */
394 void
395gui_mch_prepare(int *argc, char **argv)
396{
397 const cmdline_option_T *option;
398 int i = 0;
399 int len = 0;
400
401#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
402 /*
403 * Determine the command used to invoke Vim, to be passed as restart
404 * command to the session manager. If argv[0] contains any directory
405 * components try building an absolute path, otherwise leave it as is.
406 */
407 restart_command = argv[0];
408
409 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
410 {
411 char_u buf[MAXPATHL];
412
413 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000414 {
415 abs_restart_command = (char *)vim_strsave(buf);
416 restart_command = abs_restart_command;
417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 }
419#endif
420
421 /*
422 * Move all the entries in argv which are relevant to GTK+ and GNOME
423 * into gui_argv. Freed later in gui_mch_init().
424 */
425 gui_argc = 0;
426 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
427
428 g_return_if_fail(gui_argv != NULL);
429
430 gui_argv[gui_argc++] = argv[i++];
431
432 while (i < *argc)
433 {
434 /* Don't waste CPU cycles on non-option arguments. */
435 if (argv[i][0] != '-' && argv[i][0] != '+')
436 {
437 ++i;
438 continue;
439 }
440
441 /* Look for argv[i] in cmdline_options[] table. */
442 for (option = &cmdline_options[0]; option->name != NULL; ++option)
443 {
444 len = strlen(option->name);
445
446 if (strncmp(argv[i], option->name, len) == 0)
447 {
448 if (argv[i][len] == '\0')
449 break;
450 /* allow --foo=bar style */
451 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
452 break;
453#ifdef FEAT_NETBEANS_INTG
454 /* darn, -nb has non-standard syntax */
455 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
456 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
457 break;
458#endif
459 }
460 else if ((option->flags & ARG_COMPAT_LONG)
461 && strcmp(argv[i], option->name + 1) == 0)
462 {
463 /* Replace the standard X arguments "-name" and "-display"
464 * with their GNU-style long option counterparts. */
465 argv[i] = (char *)option->name;
466 break;
467 }
468 }
469 if (option->name == NULL) /* no match */
470 {
471 ++i;
472 continue;
473 }
474
475 if (option->flags & ARG_FOR_GTK)
476 {
477 /* Move the argument into gui_argv, which
478 * will later be passed to gtk_init_check() */
479 gui_argv[gui_argc++] = argv[i];
480 }
481 else
482 {
483 char *value = NULL;
484
485 /* Extract the option's value if there is one.
486 * Accept both "--foo bar" and "--foo=bar" style. */
487 if (option->flags & ARG_HAS_VALUE)
488 {
489 if (argv[i][len] == '=')
490 value = &argv[i][len + 1];
491 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
492 value = argv[i + 1];
493 }
494
495 /* Check for options handled by Vim itself */
496 switch (option->flags & ARG_INDEX_MASK)
497 {
498 case ARG_REVERSE:
499 found_reverse_arg = TRUE;
500 break;
501 case ARG_NOREVERSE:
502 found_reverse_arg = FALSE;
503 break;
504 case ARG_FONT:
505 font_argument = value;
506 break;
507 case ARG_GEOMETRY:
508 if (value != NULL)
509 gui.geom = vim_strsave((char_u *)value);
510 break;
511 case ARG_BACKGROUND:
512 background_argument = value;
513 break;
514 case ARG_FOREGROUND:
515 foreground_argument = value;
516 break;
517 case ARG_ICONIC:
518 found_iconic_arg = TRUE;
519 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 case ARG_ROLE:
521 role_argument = value; /* used later in gui_mch_open() */
522 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523#ifdef FEAT_NETBEANS_INTG
524 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 gui.dofork = FALSE; /* don't fork() when starting GUI */
526 netbeansArg = argv[i];
527 break;
528#endif
529 default:
530 break;
531 }
532 }
533
534 /* These arguments make gnome_program_init() print a message and exit.
535 * Must start the GUI for this, otherwise ":gui" will exit later! */
536 if (option->flags & ARG_NEEDS_GUI)
537 gui.starting = TRUE;
538
539 if (option->flags & ARG_KEEP)
540 ++i;
541 else
542 {
543 /* Remove the flag from the argument vector. */
544 if (--*argc > i)
545 {
546 int n_strip = 1;
547
548 /* Move the argument's value as well, if there is one. */
549 if ((option->flags & ARG_HAS_VALUE)
550 && argv[i][len] != '='
551 && strcmp(argv[i + 1], "--") != 0)
552 {
553 ++n_strip;
554 --*argc;
555 if (option->flags & ARG_FOR_GTK)
556 gui_argv[gui_argc++] = argv[i + 1];
557 }
558
559 if (*argc > i)
560 mch_memmove(&argv[i], &argv[i + n_strip],
561 (*argc - i) * sizeof(char *));
562 }
563 argv[*argc] = NULL;
564 }
565 }
566
567 gui_argv[gui_argc] = NULL;
568}
569
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000570#if defined(EXITFREE) || defined(PROTO)
571 void
572gui_mch_free_all()
573{
574 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000575#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
576 vim_free(abs_restart_command);
577#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000578}
579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581/*
582 * This should be maybe completely removed.
583 * Doesn't seem possible, since check_copy_area() relies on
584 * this information. --danielk
585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000587visibility_event(GtkWidget *widget UNUSED,
588 GdkEventVisibility *event,
589 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 gui.visibility = event->state;
592 /*
593 * When we do an gdk_window_copy_area(), and the window is partially
594 * obscured, we want to receive an event to tell us whether it worked
595 * or not.
596 */
597 if (gui.text_gc != NULL)
598 gdk_gc_set_exposures(gui.text_gc,
599 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
600 return FALSE;
601}
602
603/*
604 * Redraw the corresponding portions of the screen.
605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000607expose_event(GtkWidget *widget UNUSED,
608 GdkEventExpose *event,
609 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610{
611 /* Skip this when the GUI isn't set up yet, will redraw later. */
612 if (gui.starting)
613 return FALSE;
614
615 out_flush(); /* make sure all output has been processed */
616 gui_redraw(event->area.x, event->area.y,
617 event->area.width, event->area.height);
618
619 /* Clear the border areas if needed */
620 if (event->area.x < FILL_X(0))
621 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
622 if (event->area.y < FILL_Y(0))
623 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
624 if (event->area.x > FILL_X(Columns))
625 gdk_window_clear_area(gui.drawarea->window,
626 FILL_X((int)Columns), 0, 0, 0);
627 if (event->area.y > FILL_Y(Rows))
628 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
629
630 return FALSE;
631}
632
633#ifdef FEAT_CLIENTSERVER
634/*
635 * Handle changes to the "Comm" property
636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000638property_event(GtkWidget *widget,
639 GdkEventProperty *event,
640 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 if (event->type == GDK_PROPERTY_NOTIFY
643 && event->state == (int)GDK_PROPERTY_NEW_VALUE
644 && GDK_WINDOW_XWINDOW(event->window) == commWindow
645 && GET_X_ATOM(event->atom) == commProperty)
646 {
647 XEvent xev;
648
649 /* Translate to XLib */
650 xev.xproperty.type = PropertyNotify;
651 xev.xproperty.atom = commProperty;
652 xev.xproperty.window = commWindow;
653 xev.xproperty.state = PropertyNewValue;
654 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
655
656 if (gtk_main_level() > 0)
657 gtk_main_quit();
658 }
659 return FALSE;
660}
661#endif
662
663
664/****************************************************************************
665 * Focus handlers:
666 */
667
668
669/*
670 * This is a simple state machine:
671 * BLINK_NONE not blinking at all
672 * BLINK_OFF blinking, cursor is not shown
673 * BLINK_ON blinking, cursor is shown
674 */
675
676#define BLINK_NONE 0
677#define BLINK_OFF 1
678#define BLINK_ON 2
679
680static int blink_state = BLINK_NONE;
681static long_u blink_waittime = 700;
682static long_u blink_ontime = 400;
683static long_u blink_offtime = 250;
684static guint blink_timer = 0;
685
686 void
687gui_mch_set_blinking(long waittime, long on, long off)
688{
689 blink_waittime = waittime;
690 blink_ontime = on;
691 blink_offtime = off;
692}
693
694/*
695 * Stop the cursor blinking. Show the cursor if it wasn't shown.
696 */
697 void
698gui_mch_stop_blink(void)
699{
700 if (blink_timer)
701 {
702 gtk_timeout_remove(blink_timer);
703 blink_timer = 0;
704 }
705 if (blink_state == BLINK_OFF)
706 gui_update_cursor(TRUE, FALSE);
707 blink_state = BLINK_NONE;
708}
709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000711blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712{
713 if (blink_state == BLINK_ON)
714 {
715 gui_undraw_cursor();
716 blink_state = BLINK_OFF;
717 blink_timer = gtk_timeout_add((guint32)blink_offtime,
718 (GtkFunction) blink_cb, NULL);
719 }
720 else
721 {
722 gui_update_cursor(TRUE, FALSE);
723 blink_state = BLINK_ON;
724 blink_timer = gtk_timeout_add((guint32)blink_ontime,
725 (GtkFunction) blink_cb, NULL);
726 }
727
728 return FALSE; /* don't happen again */
729}
730
731/*
732 * Start the cursor blinking. If it was already blinking, this restarts the
733 * waiting time and shows the cursor.
734 */
735 void
736gui_mch_start_blink(void)
737{
738 if (blink_timer)
739 gtk_timeout_remove(blink_timer);
740 /* Only switch blinking on if none of the times is zero */
741 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
742 {
743 blink_timer = gtk_timeout_add((guint32)blink_waittime,
744 (GtkFunction) blink_cb, NULL);
745 blink_state = BLINK_ON;
746 gui_update_cursor(TRUE, FALSE);
747 }
748}
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000751enter_notify_event(GtkWidget *widget UNUSED,
752 GdkEventCrossing *event UNUSED,
753 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754{
755 if (blink_state == BLINK_NONE)
756 gui_mch_start_blink();
757
758 /* make sure keyboard input goes there */
759 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
760 gtk_widget_grab_focus(gui.drawarea);
761
762 return FALSE;
763}
764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000766leave_notify_event(GtkWidget *widget UNUSED,
767 GdkEventCrossing *event UNUSED,
768 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769{
770 if (blink_state != BLINK_NONE)
771 gui_mch_stop_blink();
772
773 return FALSE;
774}
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000777focus_in_event(GtkWidget *widget,
778 GdkEventFocus *event UNUSED,
779 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 gui_focus_change(TRUE);
782
783 if (blink_state == BLINK_NONE)
784 gui_mch_start_blink();
785
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000786 /* make sure keyboard input goes to the draw area (if this is focus for a
787 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000788 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000789 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000791 /* make sure the input buffer is read */
792 if (gtk_main_level() > 0)
793 gtk_main_quit();
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 return TRUE;
796}
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000799focus_out_event(GtkWidget *widget UNUSED,
800 GdkEventFocus *event UNUSED,
801 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
803 gui_focus_change(FALSE);
804
805 if (blink_state != BLINK_NONE)
806 gui_mch_stop_blink();
807
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000808 /* make sure the input buffer is read */
809 if (gtk_main_level() > 0)
810 gtk_main_quit();
811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 return TRUE;
813}
814
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816/*
817 * Translate a GDK key value to UTF-8 independently of the current locale.
818 * The output is written to string, which must have room for at least 6 bytes
819 * plus the NUL terminator. Returns the length in bytes.
820 *
821 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
822 * of GdkEventKey::string instead. But event->string is evil; see here why:
823 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
824 */
825 static int
826keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
827{
828 int len;
829 guint32 uc;
830
831 uc = gdk_keyval_to_unicode(keyval);
832 if (uc != 0)
833 {
834 /* Check for CTRL-foo */
835 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
836 {
837 /* These mappings look arbitrary at the first glance, but in fact
838 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
839 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
840 * more sense and is consistent with usual terminal behaviour). */
841 if (uc >= '@')
842 string[0] = uc & 0x1F;
843 else if (uc == '2')
844 string[0] = NUL;
845 else if (uc >= '3' && uc <= '7')
846 string[0] = uc ^ 0x28;
847 else if (uc == '8')
848 string[0] = BS;
849 else if (uc == '?')
850 string[0] = DEL;
851 else
852 string[0] = uc;
853 len = 1;
854 }
855 else
856 {
857 /* Translate a normal key to UTF-8. This doesn't work for dead
858 * keys of course, you _have_ to use an input method for that. */
859 len = utf_char2bytes((int)uc, string);
860 }
861 }
862 else
863 {
864 /* Translate keys which are represented by ASCII control codes in Vim.
865 * There are only a few of those; most control keys are translated to
866 * special terminal-like control sequences. */
867 len = 1;
868 switch (keyval)
869 {
870 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
871 string[0] = TAB;
872 break;
873 case GDK_Linefeed:
874 string[0] = NL;
875 break;
876 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
877 string[0] = CAR;
878 break;
879 case GDK_Escape:
880 string[0] = ESC;
881 break;
882 default:
883 len = 0;
884 break;
885 }
886 }
887 string[len] = NUL;
888
889 return len;
890}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000892 static int
893modifiers_gdk2vim(guint state)
894{
895 int modifiers = 0;
896
897 if (state & GDK_SHIFT_MASK)
898 modifiers |= MOD_MASK_SHIFT;
899 if (state & GDK_CONTROL_MASK)
900 modifiers |= MOD_MASK_CTRL;
901 if (state & GDK_MOD1_MASK)
902 modifiers |= MOD_MASK_ALT;
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200903#ifdef GDK_SUPER_MASK
904 if (state & GDK_SUPER_MASK)
905 modifiers |= MOD_MASK_META;
906#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000907 if (state & GDK_MOD4_MASK)
908 modifiers |= MOD_MASK_META;
909
910 return modifiers;
911}
912
913 static int
914modifiers_gdk2mouse(guint state)
915{
916 int modifiers = 0;
917
918 if (state & GDK_SHIFT_MASK)
919 modifiers |= MOUSE_SHIFT;
920 if (state & GDK_CONTROL_MASK)
921 modifiers |= MOUSE_CTRL;
922 if (state & GDK_MOD1_MASK)
923 modifiers |= MOUSE_ALT;
924
925 return modifiers;
926}
927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928/*
929 * Main keyboard handler:
930 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000932key_press_event(GtkWidget *widget UNUSED,
933 GdkEventKey *event,
934 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200936 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
938 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 guint key_sym;
940 int len;
941 int i;
942 int modifiers;
943 int key;
944 guint state;
945 char_u *s, *d;
946
Bram Moolenaar7cfea752010-06-22 06:07:12 +0200947 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 key_sym = event->keyval;
949 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950
951#ifdef FEAT_XIM
952 if (xim_queue_key_press_event(event, TRUE))
953 return TRUE;
954#endif
955
956#ifdef FEAT_HANGULIN
957 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
958 {
959 hangul_input_state_toggle();
960 return TRUE;
961 }
962#endif
963
964#ifdef SunXK_F36
965 /*
966 * These keys have bogus lookup strings, and trapping them here is
967 * easier than trying to XRebindKeysym() on them with every possible
968 * combination of modifiers.
969 */
970 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
971 len = 0;
972 else
973#endif
974 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 len = keyval_to_string(key_sym, state, string2);
976
977 /* Careful: convert_input() doesn't handle the NUL character.
978 * No need to convert pure ASCII anyway, thus the len > 1 check. */
979 if (len > 1 && input_conv.vc_type != CONV_NONE)
980 len = convert_input(string2, len, sizeof(string2));
981
982 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 d = string;
984 for (i = 0; i < len; ++i)
985 {
986 *d++ = s[i];
987 if (d[-1] == CSI && d + 2 < string + sizeof(string))
988 {
989 /* Turn CSI into K_CSI. */
990 *d++ = KS_EXTRA;
991 *d++ = (int)KE_CSI;
992 }
993 }
994 len = d - string;
995 }
996
997 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
998 if (key_sym == GDK_ISO_Left_Tab)
999 {
1000 key_sym = GDK_Tab;
1001 state |= GDK_SHIFT_MASK;
1002 }
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_MENU
1005 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1006 * is a menu shortcut, we ignore everything with the ALT modifier. */
1007 if ((state & GDK_MOD1_MASK)
1008 && gui.menu_is_active
1009 && (*p_wak == 'y'
1010 || (*p_wak == 'm'
1011 && len == 1
1012 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /* For GTK2 we return false to signify that we haven't handled the
1014 * keypress, so that gtk will handle the mnemonic or accelerator. */
1015 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016#endif
1017
1018 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1019 * that already has the 8th bit set.
1020 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1021 * Don't do this for double-byte encodings, it turns the char into a lead
1022 * byte. */
1023 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001024 && ((state & GDK_MOD1_MASK)
1025#ifdef GDK_SUPER_MASK
1026 || (state & GDK_SUPER_MASK)
1027#endif
1028 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1030 && (string[0] & 0x80) == 0
1031 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 )
1034 {
1035 string[0] |= 0x80;
1036 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (enc_utf8) /* convert to utf-8 */
1038 {
1039 string[1] = string[0] & 0xbf;
1040 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1041 if (string[1] == CSI)
1042 {
1043 string[2] = KS_EXTRA;
1044 string[3] = (int)KE_CSI;
1045 len = 4;
1046 }
1047 else
1048 len = 2;
1049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 }
1051
1052 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1053 * value) to detect backspace, delete and keypad keys. */
1054 if (len == 0 || len == 1)
1055 {
1056 for (i = 0; special_keys[i].key_sym != 0; i++)
1057 {
1058 if (special_keys[i].key_sym == key_sym)
1059 {
1060 string[0] = CSI;
1061 string[1] = special_keys[i].code0;
1062 string[2] = special_keys[i].code1;
1063 len = -3;
1064 break;
1065 }
1066 }
1067 }
1068
1069 if (len == 0) /* Unrecognized key */
1070 return TRUE;
1071
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 /* Special keys (and a few others) may have modifiers. Also when using a
1073 * double-byte encoding (can't set the 8th bit). */
1074 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1075 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1076 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1077 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001078 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001079#ifdef GDK_SUPER_MASK
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001080 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001082 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001084 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 /*
1087 * For some keys a shift modifier is translated into another key
1088 * code.
1089 */
1090 if (len == -3)
1091 key = TO_SPECIAL(string[1], string[2]);
1092 else
1093 key = string[0];
1094
1095 key = simplify_key(key, &modifiers);
1096 if (key == CSI)
1097 key = K_CSI;
1098 if (IS_SPECIAL(key))
1099 {
1100 string[0] = CSI;
1101 string[1] = K_SECOND(key);
1102 string[2] = K_THIRD(key);
1103 len = 3;
1104 }
1105 else
1106 {
1107 string[0] = key;
1108 len = 1;
1109 }
1110
1111 if (modifiers != 0)
1112 {
1113 string2[0] = CSI;
1114 string2[1] = KS_MODIFIER;
1115 string2[2] = modifiers;
1116 add_to_input_buf(string2, 3);
1117 }
1118 }
1119
1120 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1121 || (string[0] == intr_char && intr_char != Ctrl_C)))
1122 {
1123 trash_input_buf();
1124 got_int = TRUE;
1125 }
1126
1127 add_to_input_buf(string, len);
1128
1129 /* blank out the pointer if necessary */
1130 if (p_mh)
1131 gui_mch_mousehide(TRUE);
1132
1133 if (gtk_main_level() > 0)
1134 gtk_main_quit();
1135
1136 return TRUE;
1137}
1138
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001139#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001141key_release_event(GtkWidget *widget UNUSED,
1142 GdkEventKey *event,
1143 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144{
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001145 clipboard_event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 /*
1147 * GTK+ 2 input methods may do fancy stuff on key release events too.
1148 * With the default IM for instance, you can enter any UCS code point
1149 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1150 */
1151 return xim_queue_key_press_event(event, FALSE);
1152}
1153#endif
1154
1155
1156/****************************************************************************
1157 * Selection handlers:
1158 */
1159
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001161selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001163 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164{
1165 if (event->selection == clip_plus.gtk_sel_atom)
1166 clip_lose_selection(&clip_plus);
1167 else
1168 clip_lose_selection(&clip_star);
1169
1170 if (gtk_main_level() > 0)
1171 gtk_main_quit();
1172
1173 return TRUE;
1174}
1175
1176#define RS_NONE 0 /* selection_received_cb() not called yet */
1177#define RS_OK 1 /* selection_received_cb() called and OK */
1178#define RS_FAIL 2 /* selection_received_cb() called and failed */
1179static int received_selection = RS_NONE;
1180
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001182selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001184 guint time_ UNUSED,
1185 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
1187 VimClipboard *cbd;
1188 char_u *text;
1189 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 int len;
1192 int motion_type;
1193
1194 if (data->selection == clip_plus.gtk_sel_atom)
1195 cbd = &clip_plus;
1196 else
1197 cbd = &clip_star;
1198
1199 text = (char_u *)data->data;
1200 len = data->length;
1201 motion_type = MCHAR;
1202
1203 if (text == NULL || len <= 0)
1204 {
1205 received_selection = RS_FAIL;
1206 /* clip_free_selection(cbd); ??? */
1207
1208 if (gtk_main_level() > 0)
1209 gtk_main_quit();
1210
1211 return;
1212 }
1213
1214 if (data->type == vim_atom)
1215 {
1216 motion_type = *text++;
1217 --len;
1218 }
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 else if (data->type == vimenc_atom)
1221 {
1222 char_u *enc;
1223 vimconv_T conv;
1224
1225 motion_type = *text++;
1226 --len;
1227
1228 enc = text;
1229 text += STRLEN(text) + 1;
1230 len -= text - enc;
1231
1232 /* If the encoding of the text is different from 'encoding', attempt
1233 * converting it. */
1234 conv.vc_type = CONV_NONE;
1235 convert_setup(&conv, enc, p_enc);
1236 if (conv.vc_type != CONV_NONE)
1237 {
1238 tmpbuf = string_convert(&conv, text, &len);
1239 if (tmpbuf != NULL)
1240 text = tmpbuf;
1241 convert_setup(&conv, NULL, NULL);
1242 }
1243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 /* gtk_selection_data_get_text() handles all the nasty details
1246 * and targets and encodings etc. This rocks so hard. */
1247 else
1248 {
1249 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1250 if (tmpbuf_utf8 != NULL)
1251 {
1252 len = STRLEN(tmpbuf_utf8);
1253 if (input_conv.vc_type != CONV_NONE)
1254 {
1255 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1256 if (tmpbuf != NULL)
1257 text = tmpbuf;
1258 }
1259 else
1260 text = tmpbuf_utf8;
1261 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001262 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1263 {
1264 vimconv_T conv;
1265
1266 /* UTF-16, we get this for HTML */
1267 conv.vc_type = CONV_NONE;
1268 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1269
1270 if (conv.vc_type != CONV_NONE)
1271 {
1272 text += 2;
1273 len -= 2;
1274 tmpbuf = string_convert(&conv, text, &len);
1275 convert_setup(&conv, NULL, NULL);
1276 }
1277 if (tmpbuf != NULL)
1278 text = tmpbuf;
1279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281
Bram Moolenaara76638f2010-06-05 12:49:46 +02001282 /* Chop off any traiing NUL bytes. OpenOffice sends these. */
1283 while (len > 0 && text[len - 1] == NUL)
1284 --len;
1285
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 clip_yank_selection(motion_type, text, (long)len, cbd);
1287 received_selection = RS_OK;
1288 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290
1291 if (gtk_main_level() > 0)
1292 gtk_main_quit();
1293}
1294
1295/*
1296 * Prepare our selection data for passing it to the external selection
1297 * client.
1298 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001300selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 GtkSelectionData *selection_data,
1302 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001303 guint time_ UNUSED,
1304 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305{
1306 char_u *string;
1307 char_u *tmpbuf;
1308 long_u tmplen;
1309 int length;
1310 int motion_type;
1311 GdkAtom type;
1312 VimClipboard *cbd;
1313
1314 if (selection_data->selection == clip_plus.gtk_sel_atom)
1315 cbd = &clip_plus;
1316 else
1317 cbd = &clip_star;
1318
1319 if (!cbd->owned)
1320 return; /* Shouldn't ever happen */
1321
1322 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001323 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && info != (guint)TARGET_UTF8_STRING
1325 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 && info != (guint)TARGET_VIM
1327 && info != (guint)TARGET_COMPOUND_TEXT
1328 && info != (guint)TARGET_TEXT)
1329 return;
1330
1331 /* get the selection from the '*'/'+' register */
1332 clip_get_selection(cbd);
1333
1334 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1335 if (motion_type < 0 || string == NULL)
1336 return;
1337 /* Due to int arguments we can't handle more than G_MAXINT. Also
1338 * reserve one extra byte for NUL or the motion type; just in case.
1339 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1340 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1341
1342 if (info == (guint)TARGET_VIM)
1343 {
1344 tmpbuf = alloc((unsigned)length + 1);
1345 if (tmpbuf != NULL)
1346 {
1347 tmpbuf[0] = motion_type;
1348 mch_memmove(tmpbuf + 1, string, (size_t)length);
1349 }
1350 /* For our own format, the first byte contains the motion type */
1351 ++length;
1352 vim_free(string);
1353 string = tmpbuf;
1354 type = vim_atom;
1355 }
1356
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001357 else if (info == (guint)TARGET_HTML)
1358 {
1359 vimconv_T conv;
1360
1361 /* Since we get utf-16, we probably should set it as well. */
1362 conv.vc_type = CONV_NONE;
1363 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1364 if (conv.vc_type != CONV_NONE)
1365 {
1366 tmpbuf = string_convert(&conv, string, &length);
1367 convert_setup(&conv, NULL, NULL);
1368 vim_free(string);
1369 string = tmpbuf;
1370 }
1371
1372 /* Prepend the BOM: "fffe" */
1373 if (string != NULL)
1374 {
1375 tmpbuf = alloc(length + 2);
1376 tmpbuf[0] = 0xff;
1377 tmpbuf[1] = 0xfe;
1378 mch_memmove(tmpbuf + 2, string, (size_t)length);
1379 vim_free(string);
1380 string = tmpbuf;
1381 length += 2;
1382
1383 selection_data->type = selection_data->target;
1384 selection_data->format = 16; /* 16 bits per char */
1385 gtk_selection_data_set(selection_data, html_atom, 16,
1386 string, length);
1387 vim_free(string);
1388 }
1389 return;
1390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 else if (info == (guint)TARGET_VIMENC)
1392 {
1393 int l = STRLEN(p_enc);
1394
1395 /* contents: motion_type 'encoding' NUL text */
1396 tmpbuf = alloc((unsigned)length + l + 2);
1397 if (tmpbuf != NULL)
1398 {
1399 tmpbuf[0] = motion_type;
1400 STRCPY(tmpbuf + 1, p_enc);
1401 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1402 }
1403 length += l + 2;
1404 vim_free(string);
1405 string = tmpbuf;
1406 type = vimenc_atom;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 /* gtk_selection_data_set_text() handles everything for us. This is
1410 * so easy and simple and cool, it'd be insane not to use it. */
1411 else
1412 {
1413 if (output_conv.vc_type != CONV_NONE)
1414 {
1415 tmpbuf = string_convert(&output_conv, string, &length);
1416 vim_free(string);
1417 if (tmpbuf == NULL)
1418 return;
1419 string = tmpbuf;
1420 }
1421 /* Validate the string to avoid runtime warnings */
1422 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1423 {
1424 gtk_selection_data_set_text(selection_data,
1425 (const char *)string, length);
1426 }
1427 vim_free(string);
1428 return;
1429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 if (string != NULL)
1432 {
1433 selection_data->type = selection_data->target;
1434 selection_data->format = 8; /* 8 bits per char */
1435
1436 gtk_selection_data_set(selection_data, type, 8, string, length);
1437 vim_free(string);
1438 }
1439}
1440
1441/*
1442 * Check if the GUI can be started. Called before gvimrc is sourced.
1443 * Return OK or FAIL.
1444 */
1445 int
1446gui_mch_init_check(void)
1447{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448#ifdef FEAT_GUI_GNOME
1449 if (gtk_socket_id == 0)
1450 using_gnome = 1;
1451#endif
1452
1453 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1454 if (!gtk_init_check(&gui_argc, &gui_argv))
1455 {
1456 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001457 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 return FAIL;
1459 }
1460
1461 return OK;
1462}
1463
1464
1465/****************************************************************************
1466 * Mouse handling callbacks
1467 */
1468
1469
1470static guint mouse_click_timer = 0;
1471static int mouse_timed_out = TRUE;
1472
1473/*
1474 * Timer used to recognize multiple clicks of the mouse button
1475 */
1476 static gint
1477mouse_click_timer_cb(gpointer data)
1478{
1479 /* we don't use this information currently */
1480 int *timed_out = (int *) data;
1481
1482 *timed_out = TRUE;
1483 return FALSE; /* don't happen again */
1484}
1485
1486static guint motion_repeat_timer = 0;
1487static int motion_repeat_offset = FALSE;
1488static gint motion_repeat_timer_cb(gpointer);
1489
1490 static void
1491process_motion_notify(int x, int y, GdkModifierType state)
1492{
1493 int button;
1494 int_u vim_modifiers;
1495
1496 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1497 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1498 GDK_BUTTON5_MASK))
1499 ? MOUSE_DRAG : ' ';
1500
1501 /* If our pointer is currently hidden, then we should show it. */
1502 gui_mch_mousehide(FALSE);
1503
1504 /* Just moving the rodent above the drawing area without any button
1505 * being pressed. */
1506 if (button != MOUSE_DRAG)
1507 {
1508 gui_mouse_moved(x, y);
1509 return;
1510 }
1511
1512 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001513 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
Bram Moolenaar49325942007-05-10 19:19:59 +00001515 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1517
1518 if (gtk_main_level() > 0)
1519 gtk_main_quit();
1520
1521 /*
1522 * Auto repeat timer handling.
1523 */
1524 if (x < 0 || y < 0
1525 || x >= gui.drawarea->allocation.width
1526 || y >= gui.drawarea->allocation.height)
1527 {
1528
1529 int dx;
1530 int dy;
1531 int offshoot;
1532 int delay = 10;
1533
1534 /* Calculate the maximal distance of the cursor from the drawing area.
1535 * (offshoot can't become negative here!).
1536 */
1537 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1538 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1539
1540 offshoot = dx > dy ? dx : dy;
1541
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001542 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 * distance of 127 pixels from the main window.
1544 *
1545 * One could think endlessly about the most ergonomic variant here.
1546 * For example it could make sense to calculate the distance from the
1547 * drags start instead...
1548 *
1549 * Maybe a parabolic interpolation would suite us better here too...
1550 */
1551 if (offshoot > 127)
1552 {
1553 /* 5 appears to be somehow near to my perceptual limits :-). */
1554 delay = 5;
1555 }
1556 else
1557 {
1558 delay = (130 * (127 - offshoot)) / 127 + 5;
1559 }
1560
1561 /* shoot again */
1562 if (!motion_repeat_timer)
1563 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1564 motion_repeat_timer_cb, NULL);
1565 }
1566}
1567
1568/*
1569 * Timer used to recognize multiple clicks of the mouse button.
1570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001572motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 int x;
1575 int y;
1576 GdkModifierType state;
1577
1578 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1579
1580 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1581 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1582 GDK_BUTTON5_MASK)))
1583 {
1584 motion_repeat_timer = 0;
1585 return FALSE;
1586 }
1587
1588 /* If there already is a mouse click in the input buffer, wait another
1589 * time (otherwise we would create a backlog of clicks) */
1590 if (vim_used_in_input_buf() > 10)
1591 return TRUE;
1592
1593 motion_repeat_timer = 0;
1594
1595 /*
1596 * Fake a motion event.
1597 * Trick: Pretend the mouse moved to the next character on every other
1598 * event, otherwise drag events will be discarded, because they are still
1599 * in the same character.
1600 */
1601 if (motion_repeat_offset)
1602 x += gui.char_width;
1603
1604 motion_repeat_offset = !motion_repeat_offset;
1605 process_motion_notify(x, y, state);
1606
1607 /* Don't happen again. We will get reinstalled in the synthetic event
1608 * if needed -- thus repeating should still work. */
1609 return FALSE;
1610}
1611
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001613motion_notify_event(GtkWidget *widget,
1614 GdkEventMotion *event,
1615 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616{
1617 if (event->is_hint)
1618 {
1619 int x;
1620 int y;
1621 GdkModifierType state;
1622
1623 gdk_window_get_pointer(widget->window, &x, &y, &state);
1624 process_motion_notify(x, y, state);
1625 }
1626 else
1627 {
1628 process_motion_notify((int)event->x, (int)event->y,
1629 (GdkModifierType)event->state);
1630 }
1631
1632 return TRUE; /* handled */
1633}
1634
1635
1636/*
1637 * Mouse button handling. Note please that we are capturing multiple click's
1638 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1639 * This is due to the way the generic VIM code is recognizing multiple clicks.
1640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001642button_press_event(GtkWidget *widget,
1643 GdkEventButton *event,
1644 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645{
1646 int button;
1647 int repeated_click = FALSE;
1648 int x, y;
1649 int_u vim_modifiers;
1650
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001651 clipboard_event_time = event->time;
1652
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 /* Make sure we have focus now we've been selected */
1654 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1655 gtk_widget_grab_focus(widget);
1656
1657 /*
1658 * Don't let additional events about multiple clicks send by GTK to us
1659 * after the initial button press event confuse us.
1660 */
1661 if (event->type != GDK_BUTTON_PRESS)
1662 return FALSE;
1663
1664 x = event->x;
1665 y = event->y;
1666
1667 /* Handle multiple clicks */
1668 if (!mouse_timed_out && mouse_click_timer)
1669 {
1670 gtk_timeout_remove(mouse_click_timer);
1671 mouse_click_timer = 0;
1672 repeated_click = TRUE;
1673 }
1674
1675 mouse_timed_out = FALSE;
1676 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1677 mouse_click_timer_cb, &mouse_timed_out);
1678
1679 switch (event->button)
1680 {
1681 case 1:
1682 button = MOUSE_LEFT;
1683 break;
1684 case 2:
1685 button = MOUSE_MIDDLE;
1686 break;
1687 case 3:
1688 button = MOUSE_RIGHT;
1689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 default:
1691 return FALSE; /* Unknown button */
1692 }
1693
1694#ifdef FEAT_XIM
1695 /* cancel any preediting */
1696 if (im_is_preediting())
1697 xim_reset();
1698#endif
1699
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001700 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
1702 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
1703 if (gtk_main_level() > 0)
1704 gtk_main_quit(); /* make sure the above will be handled immediately */
1705
1706 return TRUE;
1707}
1708
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001710 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001713scroll_event(GtkWidget *widget,
1714 GdkEventScroll *event,
1715 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716{
1717 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001718 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719
1720 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1721 gtk_widget_grab_focus(widget);
1722
1723 switch (event->direction)
1724 {
1725 case GDK_SCROLL_UP:
1726 button = MOUSE_4;
1727 break;
1728 case GDK_SCROLL_DOWN:
1729 button = MOUSE_5;
1730 break;
1731 default: /* We don't care about left and right... Yet. */
1732 return FALSE;
1733 }
1734
1735# ifdef FEAT_XIM
1736 /* cancel any preediting */
1737 if (im_is_preediting())
1738 xim_reset();
1739# endif
1740
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001741 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742
1743 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1744 FALSE, vim_modifiers);
1745
1746 if (gtk_main_level() > 0)
1747 gtk_main_quit(); /* make sure the above will be handled immediately */
1748
1749 return TRUE;
1750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001754button_release_event(GtkWidget *widget UNUSED,
1755 GdkEventButton *event,
1756 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int x, y;
1759 int_u vim_modifiers;
1760
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001761 clipboard_event_time = event->time;
1762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* Remove any motion "machine gun" timers used for automatic further
1764 extension of allocation areas if outside of the applications window
1765 area .*/
1766 if (motion_repeat_timer)
1767 {
1768 gtk_timeout_remove(motion_repeat_timer);
1769 motion_repeat_timer = 0;
1770 }
1771
1772 x = event->x;
1773 y = event->y;
1774
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001775 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
1778 if (gtk_main_level() > 0)
1779 gtk_main_quit(); /* make sure it will be handled immediately */
1780
1781 return TRUE;
1782}
1783
1784
1785#ifdef FEAT_DND
1786/****************************************************************************
1787 * Drag aNd Drop support handlers.
1788 */
1789
1790/*
1791 * Count how many items there may be and separate them with a NUL.
1792 * Apparently the items are separated with \r\n. This is not documented,
1793 * thus be careful not to go past the end. Also allow separation with
1794 * NUL characters.
1795 */
1796 static int
1797count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1798{
1799 int i;
1800 char_u *p = out;
1801 int count = 0;
1802
1803 for (i = 0; i < len; ++i)
1804 {
1805 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1806 {
1807 if (p > out && p[-1] != NUL)
1808 {
1809 ++count;
1810 *p++ = NUL;
1811 }
1812 }
1813 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1814 {
1815 *p++ = hexhex2nr(raw + i + 1);
1816 i += 2;
1817 }
1818 else
1819 *p++ = raw[i];
1820 }
1821 if (p > out && p[-1] != NUL)
1822 {
1823 *p = NUL; /* last item didn't have \r or \n */
1824 ++count;
1825 }
1826 return count;
1827}
1828
1829/*
1830 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1831 * this process, URI which protocol is not "file:" are removed. Return
1832 * length of array (less than "max").
1833 */
1834 static int
1835filter_uri_list(char_u **outlist, int max, char_u *src)
1836{
1837 int i, j;
1838
1839 for (i = j = 0; i < max; ++i)
1840 {
1841 outlist[i] = NULL;
1842 if (STRNCMP(src, "file:", 5) == 0)
1843 {
1844 src += 5;
1845 if (STRNCMP(src, "//localhost", 11) == 0)
1846 src += 11;
1847 while (src[0] == '/' && src[1] == '/')
1848 ++src;
1849 outlist[j++] = vim_strsave(src);
1850 }
1851 src += STRLEN(src) + 1;
1852 }
1853 return j;
1854}
1855
1856 static char_u **
1857parse_uri_list(int *count, char_u *data, int len)
1858{
1859 int n = 0;
1860 char_u *tmp = NULL;
1861 char_u **array = NULL;;
1862
1863 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1864 {
1865 n = count_and_decode_uri_list(tmp, data, len);
1866 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1867 n = filter_uri_list(array, n, tmp);
1868 }
1869 vim_free(tmp);
1870 *count = n;
1871 return array;
1872}
1873
1874 static void
1875drag_handle_uri_list(GdkDragContext *context,
1876 GtkSelectionData *data,
1877 guint time_,
1878 GdkModifierType state,
1879 gint x,
1880 gint y)
1881{
1882 char_u **fnames;
1883 int nfiles = 0;
1884
1885 fnames = parse_uri_list(&nfiles, data->data, data->length);
1886
1887 if (fnames != NULL && nfiles > 0)
1888 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001889 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1892
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001893 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1896 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001897 else
1898 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899}
1900
1901 static void
1902drag_handle_text(GdkDragContext *context,
1903 GtkSelectionData *data,
1904 guint time_,
1905 GdkModifierType state)
1906{
1907 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1908 char_u *text;
1909 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912 text = data->data;
1913 len = data->length;
1914
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (data->type == utf8_string_atom)
1916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (input_conv.vc_type != CONV_NONE)
1918 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 if (tmpbuf != NULL)
1920 text = tmpbuf;
1921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923 dnd_yank_drag_data(text, (long)len);
1924 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001927 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928
1929 if (dropkey[2] != 0)
1930 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1931 else
1932 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
1933
1934 if (gtk_main_level() > 0)
1935 gtk_main_quit();
1936}
1937
1938/*
1939 * DND receiver.
1940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 static void
1942drag_data_received_cb(GtkWidget *widget,
1943 GdkDragContext *context,
1944 gint x,
1945 gint y,
1946 GtkSelectionData *data,
1947 guint info,
1948 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001949 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950{
1951 GdkModifierType state;
1952
1953 /* Guard against trash */
1954 if (data->data == NULL
1955 || data->length <= 0
1956 || data->format != 8
1957 || data->data[data->length] != '\0')
1958 {
1959 gtk_drag_finish(context, FALSE, FALSE, time_);
1960 return;
1961 }
1962
1963 /* Get the current modifier state for proper distinguishment between
1964 * different operations later. */
1965 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1966
1967 /* Not sure about the role of "text/plain" here... */
1968 if (info == (guint)TARGET_TEXT_URI_LIST)
1969 drag_handle_uri_list(context, data, time_, state, x, y);
1970 else
1971 drag_handle_text(context, data, time_, state);
1972
1973}
1974#endif /* FEAT_DND */
1975
1976
1977#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1978/*
1979 * GnomeClient interact callback. Check for unsaved buffers that cannot
1980 * be abandoned and pop up a dialog asking the user for confirmation if
1981 * necessary.
1982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001984sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001986 GnomeDialogType type UNUSED,
1987 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 cmdmod_T save_cmdmod;
1990 gboolean shutdown_cancelled;
1991
1992 save_cmdmod = cmdmod;
1993
1994# ifdef FEAT_BROWSE
1995 cmdmod.browse = TRUE;
1996# endif
1997# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1998 cmdmod.confirm = TRUE;
1999# endif
2000 /*
2001 * If there are changed buffers, present the user with
2002 * a dialog if possible, otherwise give an error message.
2003 */
2004 shutdown_cancelled = check_changed_any(FALSE);
2005
2006 exiting = FALSE;
2007 cmdmod = save_cmdmod;
2008 setcursor(); /* position the cursor */
2009 out_flush();
2010 /*
2011 * If the user hit the [Cancel] button the whole shutdown
2012 * will be cancelled. Wow, quite powerful feature (:
2013 */
2014 gnome_interaction_key_return(key, shutdown_cancelled);
2015}
2016
2017/*
2018 * Generate a script that can be used to restore the current editing session.
2019 * Save the value of v:this_session before running :mksession in order to make
2020 * automagic session save fully transparent. Return TRUE on success.
2021 */
2022 static int
2023write_session_file(char_u *filename)
2024{
2025 char_u *escaped_filename;
2026 char *mksession_cmdline;
2027 unsigned int save_ssop_flags;
2028 int failed;
2029
2030 /*
2031 * Build an ex command line to create a script that restores the current
2032 * session if executed. Escape the filename to avoid nasty surprises.
2033 */
2034 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2035 if (escaped_filename == NULL)
2036 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002037 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2038 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002040
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 /*
2042 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2043 * unpredictable effects when the session is saved automatically. Also,
2044 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2045 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2046 * enormously large if the GNOME session feature is used regularly.
2047 */
2048 save_ssop_flags = ssop_flags;
2049 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002050 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2053 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2054 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002055 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 ssop_flags = save_ssop_flags;
2058 g_free(mksession_cmdline);
2059 /*
2060 * Reopen the file and append a command to restore v:this_session,
2061 * as if this save never happened. This is to avoid conflicts with
2062 * the user's own sessions. FIXME: It's probably less hackish to add
2063 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2064 */
2065 if (!failed)
2066 {
2067 FILE *fd;
2068
2069 fd = open_exfile(filename, TRUE, APPENDBIN);
2070
2071 failed = (fd == NULL
2072 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2073 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2074
2075 if (fd != NULL && fclose(fd) != 0)
2076 failed = TRUE;
2077
2078 if (failed)
2079 mch_remove(filename);
2080 }
2081
2082 return !failed;
2083}
2084
2085/*
2086 * "save_yourself" signal handler. Initiate an interaction to ask the user
2087 * for confirmation if necessary. Save the current editing session and tell
2088 * the session manager how to restart Vim.
2089 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 static gboolean
2091sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002092 gint phase UNUSED,
2093 GnomeSaveStyle save_style UNUSED,
2094 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002096 gboolean fast UNUSED,
2097 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098{
2099 static const char suffix[] = "-session.vim";
2100 char *session_file;
2101 unsigned int len;
2102 gboolean success;
2103
2104 /* Always request an interaction if possible. check_changed_any()
2105 * won't actually show a dialog unless any buffers have been modified.
2106 * There doesn't seem to be an obvious way to check that without
2107 * automatically firing the dialog. Anyway, it works just fine. */
2108 if (interact_style == GNOME_INTERACT_ANY)
2109 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2110 &sm_client_check_changed_any,
2111 NULL);
2112 out_flush();
2113 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2114
2115 /* The path is unique for each session save. We do neither know nor care
2116 * which session script will actually be used later. This decision is in
2117 * the domain of the session manager. */
2118 session_file = gnome_config_get_real_path(
2119 gnome_client_get_config_prefix(client));
2120 len = strlen(session_file);
2121
2122 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2123 --len; /* get rid of the superfluous trailing '/' */
2124
2125 session_file = g_renew(char, session_file, len + sizeof(suffix));
2126 memcpy(session_file + len, suffix, sizeof(suffix));
2127
2128 success = write_session_file((char_u *)session_file);
2129
2130 if (success)
2131 {
2132 const char *argv[8];
2133 int i;
2134
2135 /* Tell the session manager how to wipe out the stored session data.
2136 * This isn't as dangerous as it looks, don't worry :) session_file
2137 * is a unique absolute filename. Usually it'll be something like
2138 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2139 i = 0;
2140 argv[i++] = "rm";
2141 argv[i++] = session_file;
2142 argv[i] = NULL;
2143
2144 gnome_client_set_discard_command(client, i, (char **)argv);
2145
2146 /* Tell the session manager how to restore the just saved session.
2147 * This is easily done thanks to Vim's -S option. Pass the -f flag
2148 * since there's no need to fork -- it might even cause confusion.
2149 * Also pass the window role to give the WM something to match on.
2150 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2151 i = 0;
2152 argv[i++] = restart_command;
2153 argv[i++] = "-f";
2154 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 argv[i++] = "--role";
2156 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 argv[i++] = "-S";
2158 argv[i++] = session_file;
2159 argv[i] = NULL;
2160
2161 gnome_client_set_restart_command(client, i, (char **)argv);
2162 gnome_client_set_clone_command(client, 0, NULL);
2163 }
2164
2165 g_free(session_file);
2166
2167 return success;
2168}
2169
2170/*
2171 * Called when the session manager wants us to die. There isn't much to save
2172 * here since "save_yourself" has been emitted before (unless serious trouble
2173 * is happening).
2174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002176sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177{
2178 /* Don't write messages to the GUI anymore */
2179 full_screen = FALSE;
2180
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002181 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002182 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002183 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 preserve_exit();
2185}
2186
2187/*
2188 * Connect our signal handlers to be notified on session save and shutdown.
2189 */
2190 static void
2191setup_save_yourself(void)
2192{
2193 GnomeClient *client;
2194
2195 client = gnome_master_client();
2196
2197 if (client != NULL)
2198 {
2199 /* Must use the deprecated gtk_signal_connect() for compatibility
2200 * with GNOME 1. Arrgh, zombies! */
2201 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2202 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2203 gtk_signal_connect(GTK_OBJECT(client), "die",
2204 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2205 }
2206}
2207
2208#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2209
2210# ifdef USE_XSMP
2211/*
2212 * GTK tells us that XSMP needs attention
2213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 static gboolean
2215local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002216 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 GIOCondition condition;
2218 gpointer data;
2219{
2220 if (condition == G_IO_IN)
2221 {
2222 /* Do stuff; maybe close connection */
2223 if (xsmp_handle_requests() == FAIL)
2224 g_io_channel_unref((GIOChannel *)data);
2225 return TRUE;
2226 }
2227 /* Error */
2228 g_io_channel_unref((GIOChannel *)data);
2229 xsmp_close();
2230 return TRUE;
2231}
2232# endif /* USE_XSMP */
2233
2234/*
2235 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2236 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2237 */
2238 static void
2239setup_save_yourself(void)
2240{
2241 Atom *existing_atoms = NULL;
2242 int count = 0;
2243
2244#ifdef USE_XSMP
2245 if (xsmp_icefd != -1)
2246 {
2247 /*
2248 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2249 * set up GTK IO monitor
2250 */
2251 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2252
2253 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2254 local_xsmp_handle_requests, (gpointer)g_io);
2255 }
2256 else
2257#endif
2258 {
2259 /* Fall back to old method */
2260
2261 /* first get the existing value */
2262 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2263 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2264 &existing_atoms, &count))
2265 {
2266 Atom *new_atoms;
2267 Atom save_yourself_xatom;
2268 int i;
2269
2270 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2271
2272 /* check if WM_SAVE_YOURSELF isn't there yet */
2273 for (i = 0; i < count; ++i)
2274 if (existing_atoms[i] == save_yourself_xatom)
2275 break;
2276
2277 if (i == count)
2278 {
2279 /* allocate an Atoms array which is one item longer */
2280 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2281 * sizeof(Atom)));
2282 if (new_atoms != NULL)
2283 {
2284 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2285 new_atoms[count] = save_yourself_xatom;
2286 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2287 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2288 new_atoms, count + 1);
2289 vim_free(new_atoms);
2290 }
2291 }
2292 XFree(existing_atoms);
2293 }
2294 }
2295}
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297/*
2298 * Installing a global event filter seems to be the only way to catch
2299 * client messages of type WM_PROTOCOLS without overriding GDK's own
2300 * client message event filter. Well, that's still better than trying
2301 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 *
2303 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2304 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2305 * I have the nasty feeling modern session managers just don't send this
2306 * deprecated message anymore. Addition: confirmed by several people.
2307 *
2308 * The GNOME session support is much cooler anyway. Unlike this ugly
2309 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2310 * it should work with KDE as well.
2311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002313global_event_filter(GdkXEvent *xev,
2314 GdkEvent *event UNUSED,
2315 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316{
2317 XEvent *xevent = (XEvent *)xev;
2318
2319 if (xevent != NULL
2320 && xevent->type == ClientMessage
2321 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002322 && (long_u)xevent->xclient.data.l[0]
2323 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
2325 out_flush();
2326 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2327 /*
2328 * Set the window's WM_COMMAND property, to let the window manager
2329 * know we are done saving ourselves. We don't want to be
2330 * restarted, thus set argv to NULL.
2331 */
2332 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2333 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2334 NULL, 0);
2335 return GDK_FILTER_REMOVE;
2336 }
2337
2338 return GDK_FILTER_CONTINUE;
2339}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2341
2342
2343/*
2344 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002347mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
2349/* If you get an error message here, you still need to unpack the runtime
2350 * archive! */
2351#ifdef magick
2352# undef magick
2353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 /* A bit hackish, but avoids casting later and allows optimization */
2355# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#define magick vim32x32
2357#include "../runtime/vim32x32.xpm"
2358#undef magick
2359#define magick vim16x16
2360#include "../runtime/vim16x16.xpm"
2361#undef magick
2362#define magick vim48x48
2363#include "../runtime/vim48x48.xpm"
2364#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366
2367 /* When started with "--echo-wid" argument, write window ID on stdout. */
2368 if (echo_wid_arg)
2369 {
2370 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2371 fflush(stdout);
2372 }
2373
2374 if (vim_strchr(p_go, GO_ICON) != NULL)
2375 {
2376 /*
2377 * Add an icon to the main window. For fun and convenience of the user.
2378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 GList *icons = NULL;
2380
2381 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2382 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2383 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2384
2385 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2386
2387 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2388 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 }
2390
2391#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2392 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394#endif
2395 /* Setup to indicate to the window manager that we want to catch the
2396 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2397 * manager instead. */
2398#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2399 if (using_gnome)
2400#endif
2401 setup_save_yourself();
2402
2403#ifdef FEAT_CLIENTSERVER
2404 if (serverName == NULL && serverDelayedStartName != NULL)
2405 {
2406 /* This is a :gui command in a plain vim with no previous server */
2407 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2408
2409 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2410 serverDelayedStartName);
2411 }
2412 else
2413 {
2414 /*
2415 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2416 * have to change the "server" registration to that of the main window
2417 * If we have not registered a name yet, remember the window
2418 */
2419 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2420 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2421 }
2422 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2423 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2424 GTK_SIGNAL_FUNC(property_event), NULL);
2425#endif
2426}
2427
2428 static GdkCursor *
2429create_blank_pointer(void)
2430{
2431 GdkWindow *root_window = NULL;
2432 GdkPixmap *blank_mask;
2433 GdkCursor *cursor;
2434 GdkColor color = { 0, 0, 0, 0 };
2435 char blank_data[] = { 0x0 };
2436
2437#ifdef HAVE_GTK_MULTIHEAD
2438 root_window = gtk_widget_get_root_window(gui.mainwin);
2439#endif
2440
2441 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2442 * in size. */
2443 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2444 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2445 &color, &color, 0, 0);
2446 gdk_bitmap_unref(blank_mask);
2447
2448 return cursor;
2449}
2450
2451#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 static void
2453mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002454 GdkScreen *previous_screen UNUSED,
2455 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 if (!gtk_widget_has_screen(widget))
2458 return;
2459
2460 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002461 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
2463 if (gui.blank_pointer != NULL)
2464 gdk_cursor_unref(gui.blank_pointer);
2465
2466 gui.blank_pointer = create_blank_pointer();
2467
2468 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2469 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2470
2471 /*
2472 * Create a new PangoContext for this screen, and initialize it
2473 * with the current font if necessary.
2474 */
2475 if (gui.text_context != NULL)
2476 g_object_unref(gui.text_context);
2477
2478 gui.text_context = gtk_widget_create_pango_context(widget);
2479 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2480
2481 if (gui.norm_font != NULL)
2482 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002483 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002484 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486}
2487#endif /* HAVE_GTK_MULTIHEAD */
2488
2489/*
2490 * After the drawing area comes up, we calculate all colors and create the
2491 * dummy blank cursor.
2492 *
2493 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2494 * fact that the main VIM engine doesn't take them into account anywhere.
2495 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002497drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
2499 GtkWidget *sbar;
2500
2501#ifdef FEAT_XIM
2502 xim_init();
2503#endif
2504 gui_mch_new_colors();
2505 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2506
2507 gui.blank_pointer = create_blank_pointer();
2508 if (gui.pointer_hidden)
2509 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2510
2511 /* get the actual size of the scrollbars, if they are realized */
2512 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2513 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2514 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2515 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2516 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2517 gui.scrollbar_width = sbar->allocation.width;
2518
2519 sbar = gui.bottom_sbar.id;
2520 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2521 gui.scrollbar_height = sbar->allocation.height;
2522}
2523
2524/*
2525 * Properly clean up on shutdown.
2526 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002528drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529{
2530 /* Don't write messages to the GUI anymore */
2531 full_screen = FALSE;
2532
2533#ifdef FEAT_XIM
2534 im_shutdown();
2535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (gui.ascii_glyphs != NULL)
2537 {
2538 pango_glyph_string_free(gui.ascii_glyphs);
2539 gui.ascii_glyphs = NULL;
2540 }
2541 if (gui.ascii_font != NULL)
2542 {
2543 g_object_unref(gui.ascii_font);
2544 gui.ascii_font = NULL;
2545 }
2546 g_object_unref(gui.text_context);
2547 gui.text_context = NULL;
2548
2549 g_object_unref(gui.text_gc);
2550 gui.text_gc = NULL;
2551
2552 gdk_cursor_unref(gui.blank_pointer);
2553 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554}
2555
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002557drawarea_style_set_cb(GtkWidget *widget UNUSED,
2558 GtkStyle *previous_style UNUSED,
2559 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 gui_mch_new_colors();
2562}
2563
2564/*
2565 * Callback routine for the "delete_event" signal on the toplevel window.
2566 * Tries to vim gracefully, or refuses to exit with changed buffers.
2567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002569delete_event_cb(GtkWidget *widget UNUSED,
2570 GdkEventAny *event UNUSED,
2571 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
2573 gui_shell_closed();
2574 return TRUE;
2575}
2576
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002577#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2578 static int
2579get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2580{
2581 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2582
2583#ifdef FEAT_GUI_GNOME
2584 if (using_gnome && widget != NULL)
2585 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002586 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002587 BonoboDockItem *dockitem;
2588
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002589 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002590 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2591 {
2592 /* Only menu & toolbar are dock items. Could tabline be?
2593 * Seem to be only the 2 defined in GNOME */
2594 widget = parent;
2595 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002596
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002597 if (dockitem == NULL || dockitem->is_floating)
2598 return 0;
2599 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2600 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002601 }
2602#endif
2603 if (widget != NULL
2604 && item_orientation == orientation
2605 && GTK_WIDGET_REALIZED(widget)
2606 && GTK_WIDGET_VISIBLE(widget))
2607 {
2608 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2609 return widget->allocation.height;
2610 else
2611 return widget->allocation.width;
2612 }
2613 return 0;
2614}
2615#endif
2616
2617 static int
2618get_menu_tool_width(void)
2619{
2620 int width = 0;
2621
2622#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2623# ifdef FEAT_MENU
2624 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2625# endif
2626# ifdef FEAT_TOOLBAR
2627 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2628# endif
2629# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002630 if (gui.tabline != NULL)
2631 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002632# endif
2633#endif
2634
2635 return width;
2636}
2637
2638 static int
2639get_menu_tool_height(void)
2640{
2641 int height = 0;
2642
2643#ifdef FEAT_MENU
2644 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2645#endif
2646#ifdef FEAT_TOOLBAR
2647 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2648#endif
2649#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002650 if (gui.tabline != NULL)
2651 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002652#endif
2653
2654 return height;
2655}
2656
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002657/* This controls whether we can set the real window hints at
2658 * start-up when in a GtkPlug.
2659 * 0 = normal processing (default)
2660 * 1 = init. hints set, no-one's tried to reset since last check
2661 * 2 = init. hints set, attempt made to change hints
2662 */
2663static int init_window_hints_state = 0;
2664
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002665 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002666update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002667{
2668 static int old_width = 0;
2669 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002670 static int old_min_width = 0;
2671 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002672 static int old_char_width = 0;
2673 static int old_char_height = 0;
2674
2675 int width;
2676 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002677 int min_width;
2678 int min_height;
2679
2680 /* At start-up, don't try to set the hints until the initial
2681 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002682 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002683 */
2684 if (!(force_width && force_height) && init_window_hints_state > 0)
2685 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002686 /* Don't do it! */
2687 init_window_hints_state = 2;
2688 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002689 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002690
2691 /* This also needs to be done when the main window isn't there yet,
2692 * otherwise the hints don't work. */
2693 width = gui_get_base_width();
2694 height = gui_get_base_height();
2695# ifdef FEAT_MENU
2696 height += tabline_height() * gui.char_height;
2697# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002698 width += get_menu_tool_width();
2699 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002700
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002701 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002702 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002703 * 'set columns=' or init. -geom) we briefly set the min. to the size
2704 * we wish to be instead of the legitimate minimum so that we actually
2705 * resize correctly.
2706 */
2707 if (force_width && force_height)
2708 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002709 min_width = force_width;
2710 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002711 }
2712 else
2713 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002714 min_width = width + MIN_COLUMNS * gui.char_width;
2715 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002716 }
2717
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002718 /* Avoid an expose event when the size didn't change. */
2719 if (width != old_width
2720 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002721 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002722 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002723 || gui.char_width != old_char_width
2724 || gui.char_height != old_char_height)
2725 {
2726 GdkGeometry geometry;
2727 GdkWindowHints geometry_mask;
2728
2729 geometry.width_inc = gui.char_width;
2730 geometry.height_inc = gui.char_height;
2731 geometry.base_width = width;
2732 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002733 geometry.min_width = min_width;
2734 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002735 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2736 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002737 /* Using gui.formwin as geometry widget doesn't work as expected
2738 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2739 * in Vim confuse GTK+. */
2740 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2741 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002742 old_width = width;
2743 old_height = height;
2744 old_min_width = min_width;
2745 old_min_height = min_height;
2746 old_char_width = gui.char_width;
2747 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002748 }
2749}
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#ifdef FEAT_TOOLBAR
2752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753/*
2754 * This extra effort wouldn't be necessary if we only used stock icons in the
2755 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2756 * shouldn't be treated differently, thus we do need this.
2757 */
2758 static void
2759icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2760{
2761 if (GTK_IS_IMAGE(widget))
2762 {
2763 GtkImage *image = (GtkImage *)widget;
2764
2765 /* User-defined icons are stored in a GtkIconSet */
2766 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2767 {
2768 GtkIconSet *icon_set;
2769 GtkIconSize icon_size;
2770
2771 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2772 icon_size = (GtkIconSize)(long)user_data;
2773
2774 gtk_icon_set_ref(icon_set);
2775 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2776 gtk_icon_set_unref(icon_set);
2777 }
2778 }
2779 else if (GTK_IS_CONTAINER(widget))
2780 {
2781 gtk_container_foreach((GtkContainer *)widget,
2782 &icon_size_changed_foreach,
2783 user_data);
2784 }
2785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 static void
2788set_toolbar_style(GtkToolbar *toolbar)
2789{
2790 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 GtkIconSize size;
2792 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2795 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2796 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002797 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2799 style = GTK_TOOLBAR_BOTH;
2800 else if (toolbar_flags & TOOLBAR_TEXT)
2801 style = GTK_TOOLBAR_TEXT;
2802 else
2803 style = GTK_TOOLBAR_ICONS;
2804
2805 gtk_toolbar_set_style(toolbar, style);
2806 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2807
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 switch (tbis_flags)
2809 {
2810 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2811 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2812 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2813 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2814 default: size = GTK_ICON_SIZE_INVALID; break;
2815 }
2816 oldsize = gtk_toolbar_get_icon_size(toolbar);
2817
2818 if (size == GTK_ICON_SIZE_INVALID)
2819 {
2820 /* Let global user preferences decide the icon size. */
2821 gtk_toolbar_unset_icon_size(toolbar);
2822 size = gtk_toolbar_get_icon_size(toolbar);
2823 }
2824 if (size != oldsize)
2825 {
2826 gtk_container_foreach(GTK_CONTAINER(toolbar),
2827 &icon_size_changed_foreach,
2828 GINT_TO_POINTER((int)size));
2829 }
2830 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831}
2832
2833#endif /* FEAT_TOOLBAR */
2834
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002835#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2836static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002837static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002838static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002839static int clicked_page; /* page clicked in tab line */
2840
2841/*
2842 * Handle selecting an item in the tab line popup menu.
2843 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002844 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002845tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002846{
Bram Moolenaara5621492006-02-25 21:55:24 +00002847 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002848 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002849
2850 if (gtk_main_level() > 0)
2851 gtk_main_quit();
2852}
2853
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002854 static void
2855add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2856{
2857 GtkWidget *item;
2858 char_u *utf_text;
2859
2860 utf_text = CONVERT_TO_UTF8(text);
2861 item = gtk_menu_item_new_with_label((const char *)utf_text);
2862 gtk_widget_show(item);
2863 CONVERT_TO_UTF8_FREE(utf_text);
2864
2865 gtk_container_add(GTK_CONTAINER(menu), item);
2866 gtk_signal_connect(GTK_OBJECT(item), "activate",
2867 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002868 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002869}
2870
Bram Moolenaara5621492006-02-25 21:55:24 +00002871/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002872 * Create a menu for the tab line.
2873 */
2874 static GtkWidget *
2875create_tabline_menu(void)
2876{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002877 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002878
2879 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002880 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2881 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2882 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002883
2884 return menu;
2885}
2886
2887 static gboolean
2888on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2889{
2890 /* Was this button press event ? */
2891 if (event->type == GDK_BUTTON_PRESS)
2892 {
2893 GdkEventButton *bevent = (GdkEventButton *)event;
2894 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002895 int y = bevent->y;
2896 GtkWidget *tabwidget;
2897 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002898
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002899 /* When ignoring events return TRUE so that the selected page doesn't
2900 * change. */
2901 if (hold_gui_events
2902# ifdef FEAT_CMDWIN
2903 || cmdwin_type != 0
2904# endif
2905 )
2906 return TRUE;
2907
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002908 tabwin = gdk_window_at_pointer(&x, &y);
2909 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2910 clicked_page = (int)(long)gtk_object_get_user_data(
2911 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002912
2913 /* If the event was generated for 3rd button popup the menu. */
2914 if (bevent->button == 3)
2915 {
2916 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2917 bevent->button, bevent->time);
2918 /* We handled the event. */
2919 return TRUE;
2920 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002921 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002922 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002923 if (clicked_page == 0)
2924 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002925 /* Click after all tabs moves to next tab page. When "x" is
2926 * small guess it's the left button. */
2927 if (send_tabline_event(x < 50 ? -1 : 0) && gtk_main_level() > 0)
Bram Moolenaar96351572006-05-05 21:16:59 +00002928 gtk_main_quit();
2929 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002930 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002931 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002932
Bram Moolenaara5621492006-02-25 21:55:24 +00002933 /* We didn't handle the event. */
2934 return FALSE;
2935}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002936
2937/*
2938 * Handle selecting one of the tabs.
2939 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002940 static void
2941on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002942 GtkNotebook *notebook UNUSED,
2943 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002944 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002945 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002946{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002947 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002948 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002949 if (send_tabline_event(idx + 1) && gtk_main_level() > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002950 gtk_main_quit();
2951 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002952}
2953
2954/*
2955 * Show or hide the tabline.
2956 */
2957 void
2958gui_mch_show_tabline(int showit)
2959{
2960 if (gui.tabline == NULL)
2961 return;
2962
2963 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2964 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002965 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002966 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002967 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002968 if (showit)
2969 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002970 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002971
2972 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002973}
2974
2975/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002976 * Return TRUE when tabline is displayed.
2977 */
2978 int
2979gui_mch_showing_tabline(void)
2980{
2981 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002982 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002983}
2984
2985/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002986 * Update the labels of the tabline.
2987 */
2988 void
2989gui_mch_update_tabline(void)
2990{
2991 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002992 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002993 GtkWidget *label;
2994 tabpage_T *tp;
2995 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002996 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002997 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002998 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002999
3000 if (gui.tabline == NULL)
3001 return;
3002
3003 ignore_tabline_evt = TRUE;
3004
3005 /* Add a label for each tab page. They all contain the same text area. */
3006 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3007 {
3008 if (tp == curtab)
3009 curtabidx = nr;
3010
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003011 tab_num = nr + 1;
3012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003013 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3014 if (page == NULL)
3015 {
3016 /* Add notebook page */
3017 page = gtk_vbox_new(FALSE, 0);
3018 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003019 event_box = gtk_event_box_new();
3020 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003021 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003022 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003023 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003024 gtk_widget_show(label);
3025 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3026 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003027 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003028 nr++);
3029 }
3030
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003031 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003032 gtk_object_set_user_data(GTK_OBJECT(event_box),
3033 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003034 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003035 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003036 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003037 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003038 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003039
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003040 get_tabline_label(tp, TRUE);
3041 labeltext = CONVERT_TO_UTF8(NameBuff);
3042 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3043 (const char *)labeltext, NULL);
3044 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003045 }
3046
3047 /* Remove any old labels. */
3048 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3049 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3050
3051 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003052 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003053
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003054 /* Make sure everything is in place before drawing text. */
3055 gui_mch_update();
3056
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003057 ignore_tabline_evt = FALSE;
3058}
3059
3060/*
3061 * Set the current tab to "nr". First tab is 1.
3062 */
3063 void
3064gui_mch_set_curtab(nr)
3065 int nr;
3066{
3067 if (gui.tabline == NULL)
3068 return;
3069
3070 ignore_tabline_evt = TRUE;
3071 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003072 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003073 ignore_tabline_evt = FALSE;
3074}
3075
3076#endif /* FEAT_GUI_TABLINE */
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003079 * Add selection targets for PRIMARY and CLIPBOARD selections.
3080 */
3081 void
3082gui_gtk_set_selection_targets(void)
3083{
3084 int i, j = 0;
3085 int n_targets = N_SELECTION_TARGETS;
3086 GtkTargetEntry targets[N_SELECTION_TARGETS];
3087
3088 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3089 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003090 /* OpenOffice tries to use TARGET_HTML and fails when it doesn't
3091 * return something, instead of trying another target. Therefore only
3092 * offer TARGET_HTML when it works. */
3093 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3094 n_targets--;
3095 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003096 targets[j++] = selection_targets[i];
3097 }
3098
3099 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3100 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3101 gtk_selection_add_targets(gui.drawarea,
3102 (GdkAtom)GDK_SELECTION_PRIMARY,
3103 targets, n_targets);
3104 gtk_selection_add_targets(gui.drawarea,
3105 (GdkAtom)clip_plus.gtk_sel_atom,
3106 targets, n_targets);
3107}
3108
3109/*
3110 * Set up for receiving DND items.
3111 */
3112 void
3113gui_gtk_set_dnd_targets(void)
3114{
3115 int i, j = 0;
3116 int n_targets = N_DND_TARGETS;
3117 GtkTargetEntry targets[N_DND_TARGETS];
3118
3119 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3120 {
Bram Moolenaara76638f2010-06-05 12:49:46 +02003121 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3122 n_targets--;
3123 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003124 targets[j++] = dnd_targets[i];
3125 }
3126
3127 gtk_drag_dest_unset(gui.drawarea);
3128 gtk_drag_dest_set(gui.drawarea,
3129 GTK_DEST_DEFAULT_ALL,
3130 targets, n_targets,
3131 GDK_ACTION_COPY);
3132}
3133
3134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3136 * Returns OK for success, FAIL when the GUI can't be started.
3137 */
3138 int
3139gui_mch_init(void)
3140{
3141 GtkWidget *vbox;
3142
3143#ifdef FEAT_GUI_GNOME
3144 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3145 * exits on failure, but that's a non-issue because we already called
3146 * gtk_init_check() in gui_mch_init_check(). */
3147 if (using_gnome)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3149 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
3151 vim_free(gui_argv);
3152 gui_argv = NULL;
3153
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003154#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 /* Set the human-readable application name */
3156 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 /*
3159 * Force UTF-8 output no matter what the value of 'encoding' is.
3160 * did_set_string_option() in option.c prohibits changing 'termencoding'
3161 * to something else than UTF-8 if the GUI is in use.
3162 */
3163 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3164
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003165#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3169 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003170#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172#endif
3173
3174 /* Initialize values */
3175 gui.border_width = 2;
3176 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3177 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003178 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003180 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003182 /* LINTED: avoid warning: conversion to 'unsigned long' */
3183 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003186 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /* Set default foreground and background colors. */
3190 gui.norm_pixel = gui.def_norm_pixel;
3191 gui.back_pixel = gui.def_back_pixel;
3192
3193 if (gtk_socket_id != 0)
3194 {
3195 GtkWidget *plug;
3196
3197 /* Use GtkSocket from another app. */
3198#ifdef HAVE_GTK_MULTIHEAD
3199 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3200 gtk_socket_id);
3201#else
3202 plug = gtk_plug_new(gtk_socket_id);
3203#endif
3204 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3205 {
3206 gui.mainwin = plug;
3207 }
3208 else
3209 {
3210 g_warning("Connection to GTK+ socket (ID %u) failed",
3211 (unsigned int)gtk_socket_id);
3212 /* Pretend we never wanted it if it failed (get own window) */
3213 gtk_socket_id = 0;
3214 }
3215 }
3216
3217 if (gtk_socket_id == 0)
3218 {
3219#ifdef FEAT_GUI_GNOME
3220 if (using_gnome)
3221 {
3222 gui.mainwin = gnome_app_new("Vim", NULL);
3223# ifdef USE_XSMP
3224 /* Use the GNOME save-yourself functionality now. */
3225 xsmp_close();
3226# endif
3227 }
3228 else
3229#endif
3230 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3231 }
3232
3233 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3234
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 /* Create the PangoContext used for drawing all text. */
3236 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3237 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3240 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3241
3242 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3243 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3244
3245 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3246 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3247#ifdef HAVE_GTK_MULTIHEAD
3248 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3249 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 gui.accel_group = gtk_accel_group_new();
3252 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003254 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 vbox = gtk_vbox_new(FALSE, 0);
3256
3257#ifdef FEAT_GUI_GNOME
3258 if (using_gnome)
3259 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003260# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 /* automagically restore menubar/toolbar placement */
3262 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3263# endif
3264 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3265 }
3266 else
3267#endif
3268 {
3269 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3270 gtk_widget_show(vbox);
3271 }
3272
3273#ifdef FEAT_MENU
3274 /*
3275 * Create the menubar and handle
3276 */
3277 gui.menubar = gtk_menu_bar_new();
3278 gtk_widget_set_name(gui.menubar, "vim-menubar");
3279
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003280 /* Avoid that GTK takes <F10> away from us. */
3281 {
3282 GtkSettings *gtk_settings;
3283
3284 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3285 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3286 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003287
3288
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289# ifdef FEAT_GUI_GNOME
3290 if (using_gnome)
3291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 BonoboDockItem *dockitem;
3293
3294 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3295 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3296 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003297 /* We don't want the menu to float. */
3298 bonobo_dock_item_set_behavior(dockitem,
3299 bonobo_dock_item_get_behavior(dockitem)
3300 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 else
3304# endif /* FEAT_GUI_GNOME */
3305 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003306 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3307 * disabled in gui_init() later. */
3308 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3310 }
3311#endif /* FEAT_MENU */
3312
3313#ifdef FEAT_TOOLBAR
3314 /*
3315 * Create the toolbar and handle
3316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 /* some aesthetics on the toolbar */
3318 gtk_rc_parse_string(
3319 "style \"vim-toolbar-style\" {\n"
3320 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3321 "}\n"
3322 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3323 gui.toolbar = gtk_toolbar_new();
3324 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3326
3327# ifdef FEAT_GUI_GNOME
3328 if (using_gnome)
3329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 BonoboDockItem *dockitem;
3331
3332 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3333 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3334 GNOME_APP_TOOLBAR_NAME);
3335 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003336 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003337 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003338 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003339 bonobo_dock_item_get_behavior(dockitem)
3340 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 }
3343 else
3344# endif /* FEAT_GUI_GNOME */
3345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3347 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3348 gtk_widget_show(gui.toolbar);
3349 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3350 }
3351#endif /* FEAT_TOOLBAR */
3352
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003353#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003354 /*
3355 * Use a Notebook for the tab pages labels. The labels are hidden by
3356 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003357 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003358 gui.tabline = gtk_notebook_new();
3359 gtk_widget_show(gui.tabline);
3360 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3361 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3362 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003363 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003364 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003365
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003366 tabline_tooltip = gtk_tooltips_new();
3367 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3368
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003369 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003370 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003371
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003372 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003373 page = gtk_vbox_new(FALSE, 0);
3374 gtk_widget_show(page);
3375 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3376 label = gtk_label_new("-Empty-");
3377 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003378 event_box = gtk_event_box_new();
3379 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003380 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003381 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003382 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003383 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003384 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003385
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003386 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003387 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003388
3389 /* Create a popup menu for the tab line and connect it. */
3390 tabline_menu = create_tabline_menu();
3391 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003392 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003393#endif
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 gui.formwin = gtk_form_new();
3396 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3397 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3398
3399 gui.drawarea = gtk_drawing_area_new();
3400
3401 /* Determine which events we will filter. */
3402 gtk_widget_set_events(gui.drawarea,
3403 GDK_EXPOSURE_MASK |
3404 GDK_ENTER_NOTIFY_MASK |
3405 GDK_LEAVE_NOTIFY_MASK |
3406 GDK_BUTTON_PRESS_MASK |
3407 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 GDK_KEY_PRESS_MASK |
3410 GDK_KEY_RELEASE_MASK |
3411 GDK_POINTER_MOTION_MASK |
3412 GDK_POINTER_MOTION_HINT_MASK);
3413
3414 gtk_widget_show(gui.drawarea);
3415 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3416 gtk_widget_show(gui.formwin);
3417 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3418
3419 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3420 * and not the window. */
3421 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3422 : GTK_OBJECT(gui.drawarea),
3423 "key_press_event",
3424 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003425#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 /* Also forward key release events for the benefit of GTK+ 2 input
3427 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3428 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3429 : G_OBJECT(gui.drawarea),
3430 "key_release_event",
3431 G_CALLBACK(&key_release_event), NULL);
3432#endif
3433 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3434 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3435 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3436 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3437
3438 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3439 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3440
3441 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3442
3443#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3444 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3445 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3446#endif
3447
3448 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003449 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3451
3452 /*
3453 * Set clipboard specific atoms
3454 */
3455 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3458 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3459
3460 /*
3461 * Start out by adding the configured border width into the border offset.
3462 */
3463 gui.border_offset = gui.border_width;
3464
3465 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3466 GTK_SIGNAL_FUNC(visibility_event), NULL);
3467 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3468 GTK_SIGNAL_FUNC(expose_event), NULL);
3469
3470 /*
3471 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3472 * Only needed for some window managers.
3473 */
3474 if (vim_strchr(p_go, GO_POINTER) != NULL)
3475 {
3476 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3477 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3478 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3479 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3480 }
3481
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003482 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3483 * only its widgets. Arguably, this could be common code and we not use
3484 * the window focus at all, but let's be safe.
3485 */
3486 if (gtk_socket_id == 0)
3487 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003488 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3489 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3490 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3491 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003492 }
3493 else
3494 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003495 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3496 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3497 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3498 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003499#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003500 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3501 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3502 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3503 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003504#endif /* FEAT_GUI_TABLINE */
3505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3508 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3509 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3510 GTK_SIGNAL_FUNC(button_press_event), NULL);
3511 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3512 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3514 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515
3516 /*
3517 * Add selection handler functions.
3518 */
3519 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3520 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3521 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3522 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3523
Bram Moolenaara76638f2010-06-05 12:49:46 +02003524 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
3526 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3527 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3528
3529 /* Pretend we don't have input focus, we will get an event if we do. */
3530 gui.in_focus = FALSE;
3531
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 return OK;
3533}
3534
3535#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3536/*
3537 * This is called from gui_start() after a fork() has been done.
3538 * We have to tell the session manager our new PID.
3539 */
3540 void
3541gui_mch_forked(void)
3542{
3543 if (using_gnome)
3544 {
3545 GnomeClient *client;
3546
3547 client = gnome_master_client();
3548
3549 if (client != NULL)
3550 gnome_client_set_process_id(client, getpid());
3551 }
3552}
3553#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3554
3555/*
3556 * Called when the foreground or background color has been changed.
3557 * This used to change the graphics contexts directly but we are
3558 * currently manipulating them where desired.
3559 */
3560 void
3561gui_mch_new_colors(void)
3562{
3563 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3564 {
3565 GdkColor color = { 0, 0, 0, 0 };
3566
3567 color.pixel = gui.back_pixel;
3568 gdk_window_set_background(gui.drawarea->window, &color);
3569 }
3570}
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572/*
3573 * This signal informs us about the need to rearrange our sub-widgets.
3574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003576form_configure_event(GtkWidget *widget UNUSED,
3577 GdkEventConfigure *event,
3578 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003580 int usable_height = event->height;
3581
3582 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3583 * no. of char-heights), so we have to manually sanitise them.
3584 * Widths seem to sort themselves out, don't ask me why.
3585 */
3586 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003587 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003590 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 gtk_form_thaw(GTK_FORM(gui.formwin));
3592
3593 return TRUE;
3594}
3595
3596/*
3597 * Function called when window already closed.
3598 * We can't do much more here than to trying to preserve what had been done,
3599 * since the window is already inevitably going away.
3600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003602mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 /* Don't write messages to the GUI anymore */
3605 full_screen = FALSE;
3606
3607 gui.mainwin = NULL;
3608 gui.drawarea = NULL;
3609
3610 if (!exiting) /* only do anything if the destroy was unexpected */
3611 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003612 vim_strncpy(IObuff,
3613 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3614 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 preserve_exit();
3616 }
3617}
3618
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003619
3620/*
3621 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3622 * hints (and thus the required size from -geom), but that after that we
3623 * put the hints back to normal (the actual minimum size) so we may
3624 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003625 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003626 * only way we have of making ourselves bigger (by set lines/columns).
3627 * Thus set hints at start-up to ensure correct init. size, then a
3628 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003629 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003630 * We'll not let the default hints be set while this timer's active.
3631 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003632 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003633check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003634{
3635 if (init_window_hints_state == 1)
3636 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003637 /* Safe to use normal hints now */
3638 init_window_hints_state = 0;
3639 update_window_manager_hints(0, 0);
3640 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003641 }
3642
3643 /* Keep on trying */
3644 init_window_hints_state = 1;
3645 return TRUE;
3646}
3647
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648/*
3649 * Open the GUI window which was created by a call to gui_mch_init().
3650 */
3651 int
3652gui_mch_open(void)
3653{
3654 guicolor_T fg_pixel = INVALCOLOR;
3655 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003656 guint pixel_width;
3657 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 /*
3660 * Allow setting a window role on the command line, or invent one
3661 * if none was specified. This is mainly useful for GNOME session
3662 * support; allowing the WM to restore window placement.
3663 */
3664 if (role_argument != NULL)
3665 {
3666 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3667 }
3668 else
3669 {
3670 char *role;
3671
3672 /* Invent a unique-enough ID string for the role */
3673 role = g_strdup_printf("vim-%u-%u-%u",
3674 (unsigned)mch_get_pid(),
3675 (unsigned)g_random_int(),
3676 (unsigned)time(NULL));
3677
3678 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3679 g_free(role);
3680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684
3685 /* Determine user specified geometry, if present. */
3686 if (gui.geom != NULL)
3687 {
3688 int mask;
3689 unsigned int w, h;
3690 int x = 0;
3691 int y = 0;
3692
3693 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3694
3695 if (mask & WidthValue)
3696 Columns = w;
3697 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003698 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003699 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003700 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003702 }
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003703
3704 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3705 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3706
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003707 pixel_width += get_menu_tool_width();
3708 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003711 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003712 int ww, hh;
3713 gui_mch_get_screen_dimensions(&ww, &hh);
3714 hh += p_ghr + get_menu_tool_height();
3715 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003716 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003717 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003718 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003719 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 vim_free(gui.geom);
3723 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003724
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003725 /* From now until everyone's stopped trying to set the window hints
3726 * to their correct minimum values, stop them being set as we need
3727 * them to remain at our required size for the parent GtkSocket to
3728 * give us the right initial size.
3729 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003730 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003731 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003732 update_window_manager_hints(pixel_width, pixel_height);
3733 init_window_hints_state = 1;
3734 g_timeout_add(1000, check_startup_plug_hints, NULL);
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003738 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3739 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003740 /* For GTK2 changing the size of the form widget doesn't cause window
3741 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003742 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003743 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003744 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
3746 if (foreground_argument != NULL)
3747 fg_pixel = gui_get_color((char_u *)foreground_argument);
3748 if (fg_pixel == INVALCOLOR)
3749 fg_pixel = gui_get_color((char_u *)"Black");
3750
3751 if (background_argument != NULL)
3752 bg_pixel = gui_get_color((char_u *)background_argument);
3753 if (bg_pixel == INVALCOLOR)
3754 bg_pixel = gui_get_color((char_u *)"White");
3755
3756 if (found_reverse_arg)
3757 {
3758 gui.def_norm_pixel = bg_pixel;
3759 gui.def_back_pixel = fg_pixel;
3760 }
3761 else
3762 {
3763 gui.def_norm_pixel = fg_pixel;
3764 gui.def_back_pixel = bg_pixel;
3765 }
3766
3767 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3768 * in a vimrc file) */
3769 set_normal_colors();
3770
3771 /* Check that none of the colors are the same as the background color */
3772 gui_check_colors();
3773
3774 /* Get the colors for the highlight groups (gui_check_colors() might have
3775 * changed them). */
3776 highlight_gui_started(); /* re-init colors and fonts */
3777
3778 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3779 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3780
3781#ifdef FEAT_HANGULIN
3782 hangul_keyboard_set();
3783#endif
3784
3785 /*
3786 * Notify the fixed area about the need to resize the contents of the
3787 * gui.formwin, which we use for random positioning of the included
3788 * components.
3789 *
3790 * We connect this signal deferred finally after anything is in place,
3791 * since this is intended to handle resizements coming from the window
3792 * manager upon us and should not interfere with what VIM is requesting
3793 * upon startup.
3794 */
3795 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3796 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3797
3798#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003799 /* Set up for receiving DND items. */
3800 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3803 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3804#endif
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003807 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (found_iconic_arg && gtk_socket_id == 0)
3809 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810
3811 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003812#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 unsigned long menu_handler = 0;
3814# ifdef FEAT_TOOLBAR
3815 unsigned long tool_handler = 0;
3816# endif
3817 /*
3818 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3819 * show when restoring the saved layout configuration. We can't just
3820 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3821 * a toplevel window and thus will be realized immediately. Instead,
3822 * connect signal handlers to hide the widgets just after they've been
3823 * marked visible, but before the main window is realized.
3824 */
3825 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3826 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3827 G_CALLBACK(&gtk_widget_hide),
3828 NULL);
3829# ifdef FEAT_TOOLBAR
3830 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3831 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3832 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3833 G_CALLBACK(&gtk_widget_hide),
3834 NULL);
3835# endif
3836#endif
3837 gtk_widget_show(gui.mainwin);
3838
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003839#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 if (menu_handler != 0)
3841 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3842# ifdef FEAT_TOOLBAR
3843 if (tool_handler != 0)
3844 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3845# endif
3846#endif
3847 }
3848
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 return OK;
3850}
3851
3852
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003854gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
3856 if (gui.mainwin != NULL)
3857 gtk_widget_destroy(gui.mainwin);
3858
3859 if (gtk_main_level() > 0)
3860 gtk_main_quit();
3861}
3862
3863/*
3864 * Get the position of the top left corner of the window.
3865 */
3866 int
3867gui_mch_get_winpos(int *x, int *y)
3868{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 return OK;
3871}
3872
3873/*
3874 * Set the position of the top left corner of the window to the given
3875 * coordinates.
3876 */
3877 void
3878gui_mch_set_winpos(int x, int y)
3879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881}
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883#if 0
3884static int resize_idle_installed = FALSE;
3885/*
3886 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3887 * the shell size doesn't exceed the window size, i.e. if the window manager
3888 * ignored our size request. Usually this happens if the window is maximized.
3889 *
3890 * FIXME: It'd be nice if we could find a little more orthodox solution.
3891 * See also the remark below in gui_mch_set_shellsize().
3892 *
3893 * DISABLED: When doing ":set lines+=1" this function would first invoke
3894 * gui_resize_shell() with the old size, then the normal callback would
3895 * report the new size through form_configure_event(). That caused the window
3896 * layout to be messed up.
3897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 static gboolean
3899force_shell_resize_idle(gpointer data)
3900{
3901 if (gui.mainwin != NULL
3902 && GTK_WIDGET_REALIZED(gui.mainwin)
3903 && GTK_WIDGET_VISIBLE(gui.mainwin))
3904 {
3905 int width;
3906 int height;
3907
3908 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3909
3910 width -= get_menu_tool_width();
3911 height -= get_menu_tool_height();
3912
3913 gui_resize_shell(width, height);
3914 }
3915
3916 resize_idle_installed = FALSE;
3917 return FALSE; /* don't call me again */
3918}
3919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
Bram Moolenaar09736232009-09-23 16:14:49 +00003921/*
3922 * Return TRUE if the main window is maximized.
3923 */
3924 int
3925gui_mch_maximized()
3926{
3927 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3928 && (gdk_window_get_state(gui.mainwin->window)
3929 & GDK_WINDOW_STATE_MAXIMIZED));
3930}
3931
3932/*
3933 * Unmaximize the main window
3934 */
3935 void
3936gui_mch_unmaximize()
3937{
3938 if (gui.mainwin != NULL)
3939 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3940}
Bram Moolenaar09736232009-09-23 16:14:49 +00003941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942/*
3943 * Set the windows size.
3944 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 void
3946gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003947 int min_width UNUSED, int min_height UNUSED,
3948 int base_width UNUSED, int base_height UNUSED,
3949 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 /* give GTK+ a chance to put all widget's into place */
3952 gui_mch_update();
3953
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003954 /* this will cause the proper resizement to happen too */
3955 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003956 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003959 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 * main window instead. */
3961 width += get_menu_tool_width();
3962 height += get_menu_tool_height();
3963
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003964 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003965 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003966 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003967 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003969# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (!resize_idle_installed)
3971 {
3972 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3973 &force_shell_resize_idle, NULL, NULL);
3974 resize_idle_installed = TRUE;
3975 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 /*
3978 * Wait until all events are processed to prevent a crash because the
3979 * real size of the drawing area doesn't reflect Vim's internal ideas.
3980 *
3981 * This is a bit of a hack, since Vim is a terminal application with a GUI
3982 * on top, while the GUI expects to be the boss.
3983 */
3984 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985}
3986
3987
3988/*
3989 * The screen size is used to make sure the initial window doesn't get bigger
3990 * than the screen. This subtracts some room for menubar, toolbar and window
3991 * decorations.
3992 */
3993 void
3994gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
3995{
3996#ifdef HAVE_GTK_MULTIHEAD
3997 GdkScreen* screen;
3998
3999 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4000 screen = gtk_widget_get_screen(gui.mainwin);
4001 else
4002 screen = gdk_screen_get_default();
4003
4004 *screen_w = gdk_screen_get_width(screen);
4005 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4006#else
4007 *screen_w = gdk_screen_width();
4008 /* Subtract 'guiheadroom' from the height to allow some room for the
4009 * window manager (task list and window title bar). */
4010 *screen_h = gdk_screen_height() - p_ghr;
4011#endif
4012
4013 /*
4014 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4015 * the toolbar and menubar for GTK, we subtract them from the screen
4016 * hight, so that the window size can be made to fit on the screen.
4017 * This should be completely changed later.
4018 */
4019 *screen_w -= get_menu_tool_width();
4020 *screen_h -= get_menu_tool_height();
4021}
4022
4023#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004025gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 if (title != NULL && output_conv.vc_type != CONV_NONE)
4028 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 if (output_conv.vc_type != CONV_NONE)
4033 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034}
4035#endif /* FEAT_TITLE */
4036
4037#if defined(FEAT_MENU) || defined(PROTO)
4038 void
4039gui_mch_enable_menu(int showit)
4040{
4041 GtkWidget *widget;
4042
4043# ifdef FEAT_GUI_GNOME
4044 if (using_gnome)
4045 widget = gui.menubar_h;
4046 else
4047# endif
4048 widget = gui.menubar;
4049
Bram Moolenaar18144c82006-04-12 21:52:12 +00004050 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4051 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 {
4053 if (showit)
4054 gtk_widget_show(widget);
4055 else
4056 gtk_widget_hide(widget);
4057
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004058 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 }
4060}
4061#endif /* FEAT_MENU */
4062
4063#if defined(FEAT_TOOLBAR) || defined(PROTO)
4064 void
4065gui_mch_show_toolbar(int showit)
4066{
4067 GtkWidget *widget;
4068
4069 if (gui.toolbar == NULL)
4070 return;
4071
4072# ifdef FEAT_GUI_GNOME
4073 if (using_gnome)
4074 widget = gui.toolbar_h;
4075 else
4076# endif
4077 widget = gui.toolbar;
4078
4079 if (showit)
4080 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4081
4082 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4083 {
4084 if (showit)
4085 gtk_widget_show(widget);
4086 else
4087 gtk_widget_hide(widget);
4088
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004089 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
4091}
4092#endif /* FEAT_TOOLBAR */
4093
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094/*
4095 * Check if a given font is a CJK font. This is done in a very crude manner. It
4096 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4097 * font. Consequently, this function cannot be used as a general purpose check
4098 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4099 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4100 */
4101 static int
4102is_cjk_font(PangoFontDescription *font_desc)
4103{
4104 static const char * const cjk_langs[] =
4105 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4106
4107 PangoFont *font;
4108 unsigned i;
4109 int is_cjk = FALSE;
4110
4111 font = pango_context_load_font(gui.text_context, font_desc);
4112
4113 if (font == NULL)
4114 return FALSE;
4115
4116 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4117 {
4118 PangoCoverage *coverage;
4119 gunichar uc;
4120
4121 coverage = pango_font_get_coverage(
4122 font, pango_language_from_string(cjk_langs[i]));
4123
4124 if (coverage != NULL)
4125 {
4126 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4127 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4128 pango_coverage_unref(coverage);
4129 }
4130 }
4131
4132 g_object_unref(font);
4133
4134 return is_cjk;
4135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaar231334e2005-07-25 20:46:57 +00004137/*
4138 * Adjust gui.char_height (after 'linespace' was changed).
4139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004141gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 PangoFontMetrics *metrics;
4144 int ascent;
4145 int descent;
4146
4147 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4148 pango_context_get_language(gui.text_context));
4149 ascent = pango_font_metrics_get_ascent(metrics);
4150 descent = pango_font_metrics_get_descent(metrics);
4151
4152 pango_font_metrics_unref(metrics);
4153
4154 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004155 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004156 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /* A not-positive value of char_height may crash Vim. Only happens
4160 * if 'linespace' is negative (which does make sense sometimes). */
4161 gui.char_ascent = MAX(gui.char_ascent, 0);
4162 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4163
4164 return OK;
4165}
4166
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167/*
4168 * Put up a font dialog and return the selected font name in allocated memory.
4169 * "oldval" is the previous value. Return NULL when cancelled.
4170 * This should probably go into gui_gtk.c. Hmm.
4171 * FIXME:
4172 * The GTK2 font selection dialog has no filtering API. So we could either
4173 * a) implement our own (possibly copying the code from somewhere else) or
4174 * b) just live with it.
4175 */
4176 char_u *
4177gui_mch_font_dialog(char_u *oldval)
4178{
4179 GtkWidget *dialog;
4180 int response;
4181 char_u *fontname = NULL;
4182 char_u *oldname;
4183
4184 dialog = gtk_font_selection_dialog_new(NULL);
4185
4186 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4187 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4188
4189 if (oldval != NULL && oldval[0] != NUL)
4190 {
4191 if (output_conv.vc_type != CONV_NONE)
4192 oldname = string_convert(&output_conv, oldval, NULL);
4193 else
4194 oldname = oldval;
4195
4196 /* Annoying bug in GTK (or Pango): if the font name does not include a
4197 * size, zero is used. Use default point size ten. */
4198 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4199 {
4200 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4201
4202 if (p != NULL)
4203 {
4204 STRCPY(p + STRLEN(p), " 10");
4205 if (oldname != oldval)
4206 vim_free(oldname);
4207 oldname = p;
4208 }
4209 }
4210
4211 gtk_font_selection_dialog_set_font_name(
4212 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4213
4214 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004215 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004217 else
4218 gtk_font_selection_dialog_set_font_name(
4219 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
4221 response = gtk_dialog_run(GTK_DIALOG(dialog));
4222
4223 if (response == GTK_RESPONSE_OK)
4224 {
4225 char *name;
4226
4227 name = gtk_font_selection_dialog_get_font_name(
4228 GTK_FONT_SELECTION_DIALOG(dialog));
4229 if (name != NULL)
4230 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004231 char_u *p;
4232
4233 /* Apparently some font names include a comma, need to escape
4234 * that, because in 'guifont' it separates names. */
4235 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004237 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004238 {
4239 fontname = string_convert(&input_conv, p, NULL);
4240 vim_free(p);
4241 }
4242 else
4243 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 }
4245 }
4246
4247 if (response != GTK_RESPONSE_NONE)
4248 gtk_widget_destroy(dialog);
4249
4250 return fontname;
4251}
4252
4253/*
4254 * Some monospace fonts don't support a bold weight, and fall back
4255 * silently to the regular weight. But this is no good since our text
4256 * drawing function can emulate bold by overstriking. So let's try
4257 * to detect whether bold weight is actually available and emulate it
4258 * otherwise.
4259 *
4260 * Note that we don't need to check for italic style since Xft can
4261 * emulate italic on its own, provided you have a proper fontconfig
4262 * setup. We wouldn't be able to emulate it in Vim anyway.
4263 */
4264 static void
4265get_styled_font_variants(void)
4266{
4267 PangoFontDescription *bold_font_desc;
4268 PangoFont *plain_font;
4269 PangoFont *bold_font;
4270
4271 gui.font_can_bold = FALSE;
4272
4273 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4274
4275 if (plain_font == NULL)
4276 return;
4277
4278 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4279 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4280
4281 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4282 /*
4283 * The comparison relies on the unique handle nature of a PangoFont*,
4284 * i.e. it's assumed that a different PangoFont* won't refer to the
4285 * same font. Seems to work, and failing here isn't critical anyway.
4286 */
4287 if (bold_font != NULL)
4288 {
4289 gui.font_can_bold = (bold_font != plain_font);
4290 g_object_unref(bold_font);
4291 }
4292
4293 pango_font_description_free(bold_font_desc);
4294 g_object_unref(plain_font);
4295}
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297static PangoEngineShape *default_shape_engine = NULL;
4298
4299/*
4300 * Create a map from ASCII characters in the range [32,126] to glyphs
4301 * of the current font. This is used by gui_gtk2_draw_string() to skip
4302 * the itemize and shaping process for the most common case.
4303 */
4304 static void
4305ascii_glyph_table_init(void)
4306{
4307 char_u ascii_chars[128];
4308 PangoAttrList *attr_list;
4309 GList *item_list;
4310 int i;
4311
4312 if (gui.ascii_glyphs != NULL)
4313 pango_glyph_string_free(gui.ascii_glyphs);
4314 if (gui.ascii_font != NULL)
4315 g_object_unref(gui.ascii_font);
4316
4317 gui.ascii_glyphs = NULL;
4318 gui.ascii_font = NULL;
4319
4320 /* For safety, fill in question marks for the control characters. */
4321 for (i = 0; i < 32; ++i)
4322 ascii_chars[i] = '?';
4323 for (; i < 127; ++i)
4324 ascii_chars[i] = i;
4325 ascii_chars[i] = '?';
4326
4327 attr_list = pango_attr_list_new();
4328 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4329 0, sizeof(ascii_chars), attr_list, NULL);
4330
4331 if (item_list != NULL && item_list->next == NULL) /* play safe */
4332 {
4333 PangoItem *item;
4334 int width;
4335
4336 item = (PangoItem *)item_list->data;
4337 width = gui.char_width * PANGO_SCALE;
4338
4339 /* Remember the shape engine used for ASCII. */
4340 default_shape_engine = item->analysis.shape_engine;
4341
4342 gui.ascii_font = item->analysis.font;
4343 g_object_ref(gui.ascii_font);
4344
4345 gui.ascii_glyphs = pango_glyph_string_new();
4346
4347 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4348 &item->analysis, gui.ascii_glyphs);
4349
4350 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4351
4352 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4353 {
4354 PangoGlyphGeometry *geom;
4355
4356 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4357 geom->x_offset += MAX(0, width - geom->width) / 2;
4358 geom->width = width;
4359 }
4360 }
4361
4362 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4363 g_list_free(item_list);
4364 pango_attr_list_unref(attr_list);
4365}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366
4367/*
4368 * Initialize Vim to use the font or fontset with the given name.
4369 * Return FAIL if the font could not be loaded, OK otherwise.
4370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004372gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 PangoFontDescription *font_desc;
4375 PangoLayout *layout;
4376 int width;
4377
4378 /* If font_name is NULL, this means to use the default, which should
4379 * be present on all proper Pango/fontconfig installations. */
4380 if (font_name == NULL)
4381 font_name = (char_u *)DEFAULT_FONT;
4382
4383 font_desc = gui_mch_get_font(font_name, FALSE);
4384
4385 if (font_desc == NULL)
4386 return FAIL;
4387
4388 gui_mch_free_font(gui.norm_font);
4389 gui.norm_font = font_desc;
4390
4391 pango_context_set_font_description(gui.text_context, font_desc);
4392
4393 layout = pango_layout_new(gui.text_context);
4394 pango_layout_set_text(layout, "MW", 2);
4395 pango_layout_get_size(layout, &width, NULL);
4396 /*
4397 * Set char_width to half the width obtained from pango_layout_get_size()
4398 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4399 * Pango to use the same width for both non-CJK characters (e.g. Latin
4400 * letters and numbers) and CJK characters. This results in 's p a c e d
4401 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4402 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4403 * width for CJK fonts.
4404 *
4405 * For related bugs, see:
4406 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4407 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4408 *
4409 * With this, for all four of the following cases, Vim works fine:
4410 * guifont=CJK_fixed_width_font
4411 * guifont=Non_CJK_fixed_font
4412 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4413 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4414 */
4415 if (is_cjk_font(gui.norm_font))
4416 {
4417 int cjk_width;
4418
4419 /* Measure the text extent of U+4E00 and U+4E8C */
4420 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4421 pango_layout_get_size(layout, &cjk_width, NULL);
4422
4423 if (width == cjk_width) /* Xft not patched */
4424 width /= 2;
4425 }
4426 g_object_unref(layout);
4427
4428 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4429
4430 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4431 if (gui.char_width <= 0)
4432 gui.char_width = 8;
4433
Bram Moolenaar231334e2005-07-25 20:46:57 +00004434 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 /* Set the fontname, which will be used for information purposes */
4437 hl_set_font_name(font_name);
4438
4439 get_styled_font_variants();
4440 ascii_glyph_table_init();
4441
4442 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4443 if (gui.wide_font != NULL
4444 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4445 {
4446 pango_font_description_free(gui.wide_font);
4447 gui.wide_font = NULL;
4448 }
4449
Bram Moolenaare161c792009-11-03 17:13:59 +00004450 if (gui_mch_maximized())
4451 {
4452 int w, h;
4453
4454 /* Update lines and columns in accordance with the new font, keep the
4455 * window maximized. */
4456 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
4457 w -= get_menu_tool_width();
4458 h -= get_menu_tool_height();
4459 gui_resize_shell(w, h);
4460 }
4461 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004462 {
4463 /* Preserve the logical dimensions of the screen. */
4464 update_window_manager_hints(0, 0);
4465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 return OK;
4468}
4469
4470/*
4471 * Get a reference to the font "name".
4472 * Return zero for failure.
4473 */
4474 GuiFont
4475gui_mch_get_font(char_u *name, int report_error)
4476{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 /* can't do this when GUI is not running */
4480 if (!gui.in_use || name == NULL)
4481 return NULL;
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (output_conv.vc_type != CONV_NONE)
4484 {
4485 char_u *buf;
4486
4487 buf = string_convert(&output_conv, name, NULL);
4488 if (buf != NULL)
4489 {
4490 font = pango_font_description_from_string((const char *)buf);
4491 vim_free(buf);
4492 }
4493 else
4494 font = NULL;
4495 }
4496 else
4497 font = pango_font_description_from_string((const char *)name);
4498
4499 if (font != NULL)
4500 {
4501 PangoFont *real_font;
4502
4503 /* pango_context_load_font() bails out if no font size is set */
4504 if (pango_font_description_get_size(font) <= 0)
4505 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4506
4507 real_font = pango_context_load_font(gui.text_context, font);
4508
4509 if (real_font == NULL)
4510 {
4511 pango_font_description_free(font);
4512 font = NULL;
4513 }
4514 else
4515 g_object_unref(real_font);
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 if (font == NULL)
4519 {
4520 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004521 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 return NULL;
4523 }
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 /*
4526 * The fixed-width check has been disabled for GTK+ 2. Rationale:
4527 *
4528 * - The check tends to report false positives, particularly
4529 * in non-Latin locales or with old X fonts.
4530 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
4531 * GTK+ 2 Vim is actually capable of displaying variable width
4532 * fonts. Those will just be spaced out like in AA xterm.
4533 * - Failing here for the default font causes GUI startup to fail
4534 * even with wiped out configuration files.
4535 * - The font dialog displays all fonts unfiltered, and it's rather
4536 * annoying if 95% of the listed fonts produce an error message.
4537 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02004538#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
4540 /* Check that this is a mono-spaced font. Naturally, this is a bit
4541 * hackish -- fixed-width isn't really suitable for i18n text :/ */
4542 PangoLayout *layout;
4543 unsigned int i;
4544 int last_width = -1;
4545 const char test_chars[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
4546
4547 layout = pango_layout_new(gui.text_context);
4548 pango_layout_set_font_description(layout, font);
4549
4550 for (i = 0; i < G_N_ELEMENTS(test_chars); ++i)
4551 {
4552 int width;
4553
4554 pango_layout_set_text(layout, &test_chars[i], 1);
4555 pango_layout_get_size(layout, &width, NULL);
4556
4557 if (last_width >= 0 && width != last_width)
4558 {
4559 pango_font_description_free(font);
4560 font = NULL;
4561 break;
4562 }
4563
4564 last_width = width;
4565 }
4566
4567 g_object_unref(layout);
4568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#endif
4570
4571 return font;
4572}
4573
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004574#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004575/*
4576 * Return the name of font "font" in allocated memory.
4577 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004578 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004579gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004580{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004581 if (font != NOFONT)
4582 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004583 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004584
Bram Moolenaar89d40322006-08-29 15:30:07 +00004585 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004586 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004587 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004588
Bram Moolenaar89d40322006-08-29 15:30:07 +00004589 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004590 return s;
4591 }
4592 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004593 return NULL;
4594}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004595#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597/*
4598 * If a font is not going to be used, free its structure.
4599 */
4600 void
4601gui_mch_free_font(GuiFont font)
4602{
4603 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605}
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607/*
4608 * Return the Pixel value (color) for the given color name. This routine was
4609 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4610 * Programmer's Guide.
4611 * Return INVALCOLOR for error.
4612 */
4613 guicolor_T
4614gui_mch_get_color(char_u *name)
4615{
4616 /* A number of colors that some X11 systems don't have */
4617 static const char *const vimnames[][2] =
4618 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004619 {"LightRed", "#FFBBBB"},
4620 {"LightGreen", "#88FF88"},
4621 {"LightMagenta","#FFBBFF"},
4622 {"DarkCyan", "#008888"},
4623 {"DarkBlue", "#0000BB"},
4624 {"DarkRed", "#BB0000"},
4625 {"DarkMagenta", "#BB00BB"},
4626 {"DarkGrey", "#BBBBBB"},
4627 {"DarkYellow", "#BBBB00"},
4628 {"Gray10", "#1A1A1A"},
4629 {"Grey10", "#1A1A1A"},
4630 {"Gray20", "#333333"},
4631 {"Grey20", "#333333"},
4632 {"Gray30", "#4D4D4D"},
4633 {"Grey30", "#4D4D4D"},
4634 {"Gray40", "#666666"},
4635 {"Grey40", "#666666"},
4636 {"Gray50", "#7F7F7F"},
4637 {"Grey50", "#7F7F7F"},
4638 {"Gray60", "#999999"},
4639 {"Grey60", "#999999"},
4640 {"Gray70", "#B3B3B3"},
4641 {"Grey70", "#B3B3B3"},
4642 {"Gray80", "#CCCCCC"},
4643 {"Grey80", "#CCCCCC"},
4644 {"Gray90", "#E5E5E5"},
4645 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {NULL, NULL}
4647 };
4648
4649 if (!gui.in_use) /* can't do this when GUI not running */
4650 return INVALCOLOR;
4651
4652 while (name != NULL)
4653 {
4654 GdkColor color;
4655 int parsed;
4656 int i;
4657
4658 parsed = gdk_color_parse((const char *)name, &color);
4659
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 if (parsed)
4661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4663 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 return (guicolor_T)color.pixel;
4665 }
4666 /* add a few builtin names and try again */
4667 for (i = 0; ; ++i)
4668 {
4669 if (vimnames[i][0] == NULL)
4670 {
4671 name = NULL;
4672 break;
4673 }
4674 if (STRICMP(name, vimnames[i][0]) == 0)
4675 {
4676 name = (char_u *)vimnames[i][1];
4677 break;
4678 }
4679 }
4680 }
4681
4682 return INVALCOLOR;
4683}
4684
4685/*
4686 * Set the current text foreground color.
4687 */
4688 void
4689gui_mch_set_fg_color(guicolor_T color)
4690{
4691 gui.fgcolor->pixel = (unsigned long)color;
4692}
4693
4694/*
4695 * Set the current text background color.
4696 */
4697 void
4698gui_mch_set_bg_color(guicolor_T color)
4699{
4700 gui.bgcolor->pixel = (unsigned long)color;
4701}
4702
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004703/*
4704 * Set the current text special color.
4705 */
4706 void
4707gui_mch_set_sp_color(guicolor_T color)
4708{
4709 gui.spcolor->pixel = (unsigned long)color;
4710}
4711
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712/*
4713 * Function-like convenience macro for the sake of efficiency.
4714 */
4715#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4716 G_STMT_START{ \
4717 PangoAttribute *tmp_attr_; \
4718 tmp_attr_ = (Attribute); \
4719 tmp_attr_->start_index = (Start); \
4720 tmp_attr_->end_index = (End); \
4721 pango_attr_list_insert((AttrList), tmp_attr_); \
4722 }G_STMT_END
4723
4724 static void
4725apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4726{
4727 char_u *start = NULL;
4728 char_u *p;
4729 int uc;
4730
4731 for (p = s; p < s + len; p += utf_byte2len(*p))
4732 {
4733 uc = utf_ptr2char(p);
4734
4735 if (start == NULL)
4736 {
4737 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4738 start = p;
4739 }
4740 else if (uc < 0x80 /* optimization shortcut */
4741 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4742 {
4743 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4744 attr_list, start - s, p - s);
4745 start = NULL;
4746 }
4747 }
4748
4749 if (start != NULL)
4750 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4751 attr_list, start - s, len);
4752}
4753
4754 static int
4755count_cluster_cells(char_u *s, PangoItem *item,
4756 PangoGlyphString* glyphs, int i,
4757 int *cluster_width,
4758 int *last_glyph_rbearing)
4759{
4760 char_u *p;
4761 int next; /* glyph start index of next cluster */
4762 int start, end; /* string segment of current cluster */
4763 int width; /* real cluster width in Pango units */
4764 int uc;
4765 int cellcount = 0;
4766
4767 width = glyphs->glyphs[i].geometry.width;
4768
4769 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4770 {
4771 if (glyphs->glyphs[next].attr.is_cluster_start)
4772 break;
4773 else if (glyphs->glyphs[next].geometry.width > width)
4774 width = glyphs->glyphs[next].geometry.width;
4775 }
4776
4777 start = item->offset + glyphs->log_clusters[i];
4778 end = item->offset + ((next < glyphs->num_glyphs) ?
4779 glyphs->log_clusters[next] : item->length);
4780
4781 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4782 {
4783 uc = utf_ptr2char(p);
4784 if (uc < 0x80)
4785 ++cellcount;
4786 else if (!utf_iscomposing(uc))
4787 cellcount += utf_char2cells(uc);
4788 }
4789
4790 if (last_glyph_rbearing != NULL
4791 && cellcount > 0 && next == glyphs->num_glyphs)
4792 {
4793 PangoRectangle ink_rect;
4794 /*
4795 * If a certain combining mark had to be taken from a non-monospace
4796 * font, we have to compensate manually by adapting x_offset according
4797 * to the ink extents of the previous glyph.
4798 */
4799 pango_font_get_glyph_extents(item->analysis.font,
4800 glyphs->glyphs[i].glyph,
4801 &ink_rect, NULL);
4802
4803 if (PANGO_RBEARING(ink_rect) > 0)
4804 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4805 }
4806
4807 if (cellcount > 0)
4808 *cluster_width = width;
4809
4810 return cellcount;
4811}
4812
4813/*
4814 * If there are only combining characters in the cluster, we cannot just
4815 * change the width of the previous glyph since there is none. Therefore
4816 * some guesswork is needed.
4817 *
4818 * If ink_rect.x is negative Pango apparently has taken care of the composing
4819 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4820 * to problems with composing from different fonts we still need to fine-tune
4821 * x_offset to avoid uglyness.
4822 *
4823 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4824 * the position of the previous glyph. Apparently this happens only with old
4825 * X fonts which don't provide the special combining information needed by
4826 * Pango.
4827 */
4828 static void
4829setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4830 int last_cellcount, int last_cluster_width,
4831 int last_glyph_rbearing)
4832{
4833 PangoRectangle ink_rect;
4834 PangoRectangle logical_rect;
4835 int width;
4836
4837 width = last_cellcount * gui.char_width * PANGO_SCALE;
4838 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4839 glyph->geometry.width = 0;
4840
4841 pango_font_get_glyph_extents(item->analysis.font,
4842 glyph->glyph,
4843 &ink_rect, &logical_rect);
4844 if (ink_rect.x < 0)
4845 {
4846 glyph->geometry.x_offset += last_glyph_rbearing;
4847 glyph->geometry.y_offset = logical_rect.height
4848 - (gui.char_height - p_linespace) * PANGO_SCALE;
4849 }
4850}
4851
4852 static void
4853draw_glyph_string(int row, int col, int num_cells, int flags,
4854 PangoFont *font, PangoGlyphString *glyphs)
4855{
4856 if (!(flags & DRAW_TRANSP))
4857 {
4858 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4859
4860 gdk_draw_rectangle(gui.drawarea->window,
4861 gui.text_gc,
4862 TRUE,
4863 FILL_X(col),
4864 FILL_Y(row),
4865 num_cells * gui.char_width,
4866 gui.char_height);
4867 }
4868
4869 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4870
4871 gdk_draw_glyphs(gui.drawarea->window,
4872 gui.text_gc,
4873 font,
4874 TEXT_X(col),
4875 TEXT_Y(row),
4876 glyphs);
4877
4878 /* redraw the contents with an offset of 1 to emulate bold */
4879 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4880 gdk_draw_glyphs(gui.drawarea->window,
4881 gui.text_gc,
4882 font,
4883 TEXT_X(col) + 1,
4884 TEXT_Y(row),
4885 glyphs);
4886}
4887
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004888/*
4889 * Draw underline and undercurl at the bottom of the character cell.
4890 */
4891 static void
4892draw_under(int flags, int row, int col, int cells)
4893{
4894 int i;
4895 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004896 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004897 int y = FILL_Y(row + 1) - 1;
4898
4899 /* Undercurl: draw curl at the bottom of the character cell. */
4900 if (flags & DRAW_UNDERC)
4901 {
4902 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4903 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4904 {
4905 offset = val[i % 8];
4906 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4907 }
4908 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4909 }
4910
4911 /* Underline: draw a line at the bottom of the character cell. */
4912 if (flags & DRAW_UNDERL)
4913 {
4914 /* When p_linespace is 0, overwrite the bottom row of pixels.
4915 * Otherwise put the line just below the character. */
4916 if (p_linespace > 1)
4917 y -= p_linespace - 1;
4918 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4919 FILL_X(col), y,
4920 FILL_X(col + cells) - 1, y);
4921 }
4922}
4923
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 int
4925gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4926{
4927 GdkRectangle area; /* area for clip mask */
4928 PangoGlyphString *glyphs; /* glyphs of current item */
4929 int column_offset = 0; /* column offset in cells */
4930 int i;
4931 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4932 char_u *new_conv_buf;
4933 int convlen;
4934 char_u *sp, *bp;
4935 int plen;
4936
4937 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4938 return len;
4939
4940 if (output_conv.vc_type != CONV_NONE)
4941 {
4942 /*
4943 * Convert characters from 'encoding' to 'termencoding', which is set
4944 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4945 * prohibits changing this to something else than UTF-8 if the GUI is
4946 * in use.
4947 */
4948 convlen = len;
4949 conv_buf = string_convert(&output_conv, s, &convlen);
4950 g_return_val_if_fail(conv_buf != NULL, len);
4951
4952 /* Correct for differences in char width: some chars are
4953 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4954 * compensate for that. */
4955 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4956 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004957 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4959 {
4960 new_conv_buf = alloc(convlen + 2);
4961 if (new_conv_buf == NULL)
4962 return len;
4963 plen += bp - conv_buf;
4964 mch_memmove(new_conv_buf, conv_buf, plen);
4965 new_conv_buf[plen] = ' ';
4966 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4967 convlen - plen + 1);
4968 vim_free(conv_buf);
4969 conv_buf = new_conv_buf;
4970 ++convlen;
4971 bp = conv_buf + plen;
4972 plen = 1;
4973 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004974 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 bp += plen;
4976 }
4977 s = conv_buf;
4978 len = convlen;
4979 }
4980
4981 /*
4982 * Restrict all drawing to the current screen line in order to prevent
4983 * fuzzy font lookups from messing up the screen.
4984 */
4985 area.x = gui.border_offset;
4986 area.y = FILL_Y(row);
4987 area.width = gui.num_cols * gui.char_width;
4988 area.height = gui.char_height;
4989
4990 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4991 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4992
4993 glyphs = pango_glyph_string_new();
4994
4995 /*
4996 * Optimization hack: If possible, skip the itemize and shaping process
4997 * for pure ASCII strings. This optimization is particularly effective
4998 * because Vim draws space characters to clear parts of the screen.
4999 */
5000 if (!(flags & DRAW_ITALIC)
5001 && !((flags & DRAW_BOLD) && gui.font_can_bold)
5002 && gui.ascii_glyphs != NULL)
5003 {
5004 char_u *p;
5005
5006 for (p = s; p < s + len; ++p)
5007 if (*p & 0x80)
5008 goto not_ascii;
5009
5010 pango_glyph_string_set_size(glyphs, len);
5011
5012 for (i = 0; i < len; ++i)
5013 {
5014 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
5015 glyphs->log_clusters[i] = i;
5016 }
5017
5018 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
5019
5020 column_offset = len;
5021 }
5022 else
5023not_ascii:
5024 {
5025 PangoAttrList *attr_list;
5026 GList *item_list;
5027 int cluster_width;
5028 int last_glyph_rbearing;
5029 int cells = 0; /* cells occupied by current cluster */
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005030#if 0
5031 int monospace13 = STRICMP(p_guifont, "monospace 13") == 0;
5032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005034 /* Safety check: pango crashes when invoked with invalid utf-8
5035 * characters. */
5036 if (!utf_valid_string(s, s + len))
5037 {
5038 column_offset = len;
5039 goto skipitall;
5040 }
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* original width of the current cluster */
5043 cluster_width = PANGO_SCALE * gui.char_width;
5044
5045 /* right bearing of the last non-composing glyph */
5046 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5047
5048 attr_list = pango_attr_list_new();
5049
5050 /* If 'guifontwide' is set then use that for double-width characters.
5051 * Otherwise just go with 'guifont' and let Pango do its thing. */
5052 if (gui.wide_font != NULL)
5053 apply_wide_font_attr(s, len, attr_list);
5054
5055 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5056 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5057 attr_list, 0, len);
5058 if (flags & DRAW_ITALIC)
5059 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5060 attr_list, 0, len);
5061 /*
5062 * Break the text into segments with consistent directional level
5063 * and shaping engine. Pure Latin text needs only a single segment,
5064 * so there's no need to worry about the loop's efficiency. Better
5065 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5066 */
5067 item_list = pango_itemize(gui.text_context,
5068 (const char *)s, 0, len, attr_list, NULL);
5069
5070 while (item_list != NULL)
5071 {
5072 PangoItem *item;
5073 int item_cells = 0; /* item length in cells */
5074
5075 item = (PangoItem *)item_list->data;
5076 item_list = g_list_delete_link(item_list, item_list);
5077 /*
5078 * Increment the bidirectional embedding level by 1 if it is not
5079 * even. An odd number means the output will be RTL, but we don't
5080 * want that since Vim handles right-to-left text on its own. It
5081 * would probably be sufficient to just set level = 0, but you can
5082 * never know :)
5083 *
5084 * Unfortunately we can't take advantage of Pango's ability to
5085 * render both LTR and RTL at the same time. In order to support
5086 * that, Vim's main screen engine would have to make use of Pango
5087 * functionality.
5088 */
5089 item->analysis.level = (item->analysis.level + 1) & (~1U);
5090
5091 /* HACK: Overrule the shape engine, we don't want shaping to be
5092 * done, because drawing the cursor would change the display. */
5093 item->analysis.shape_engine = default_shape_engine;
5094
5095 pango_shape((const char *)s + item->offset, item->length,
5096 &item->analysis, glyphs);
5097 /*
5098 * Fixed-width hack: iterate over the array and assign a fixed
5099 * width to each glyph, thus overriding the choice made by the
5100 * shaping engine. We use utf_char2cells() to determine the
5101 * number of cells needed.
5102 *
5103 * Also perform all kind of dark magic to get composing
5104 * characters right (and pretty too of course).
5105 */
5106 for (i = 0; i < glyphs->num_glyphs; ++i)
5107 {
5108 PangoGlyphInfo *glyph;
5109
5110 glyph = &glyphs->glyphs[i];
5111
5112 if (glyph->attr.is_cluster_start)
5113 {
5114 int cellcount;
5115
5116 cellcount = count_cluster_cells(
5117 s, item, glyphs, i, &cluster_width,
5118 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5119
5120 if (cellcount > 0)
5121 {
5122 int width;
5123
5124 width = cellcount * gui.char_width * PANGO_SCALE;
5125 glyph->geometry.x_offset +=
5126 MAX(0, width - cluster_width) / 2;
5127 glyph->geometry.width = width;
5128 }
5129 else
5130 {
5131 /* If there are only combining characters in the
5132 * cluster, we cannot just change the width of the
5133 * previous glyph since there is none. Therefore
5134 * some guesswork is needed. */
5135 setup_zero_width_cluster(item, glyph, cells,
5136 cluster_width,
5137 last_glyph_rbearing);
5138 }
5139
5140 item_cells += cellcount;
5141 cells = cellcount;
5142 }
5143 else if (i > 0)
5144 {
5145 int width;
5146
5147 /* There is a previous glyph, so we deal with combining
5148 * characters the canonical way. That is, setting the
5149 * width of the previous glyph to 0. */
5150 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 width = cells * gui.char_width * PANGO_SCALE;
5152 glyph->geometry.x_offset +=
5153 MAX(0, width - cluster_width) / 2;
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005154#if 0
5155 /* Dirty hack: for "monospace 13" font there is a bug that
5156 * draws composing chars in the wrong position. Add
5157 * "width" to the offset to work around that. */
5158 if (monospace13)
5159 glyph->geometry.x_offset = width;
5160#endif
5161
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 glyph->geometry.width = width;
5163 }
5164 else /* i == 0 "cannot happen" */
5165 {
5166 glyph->geometry.width = 0;
5167 }
5168 }
5169
5170 /*** Aaaaand action! ***/
5171 draw_glyph_string(row, col + column_offset, item_cells,
5172 flags, item->analysis.font, glyphs);
5173
5174 pango_item_free(item);
5175
5176 column_offset += item_cells;
5177 }
5178
5179 pango_attr_list_unref(attr_list);
5180 }
5181
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005182skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005183 /* Draw underline and undercurl. */
5184 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185
5186 pango_glyph_string_free(glyphs);
5187 vim_free(conv_buf);
5188
5189 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5190
5191 return column_offset;
5192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194/*
5195 * Return OK if the key with the termcap name "name" is supported.
5196 */
5197 int
5198gui_mch_haskey(char_u *name)
5199{
5200 int i;
5201
5202 for (i = 0; special_keys[i].key_sym != 0; i++)
5203 if (name[0] == special_keys[i].code0
5204 && name[1] == special_keys[i].code1)
5205 return OK;
5206 return FAIL;
5207}
5208
5209#if defined(FEAT_TITLE) \
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 || defined(PROTO)
5211/*
5212 * Return the text window-id and display. Only required for X-based GUI's
5213 */
5214 int
5215gui_get_x11_windis(Window *win, Display **dis)
5216{
5217 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5218 {
5219 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5220 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5221 return OK;
5222 }
5223
5224 *dis = NULL;
5225 *win = 0;
5226 return FAIL;
5227}
5228#endif
5229
5230#if defined(FEAT_CLIENTSERVER) \
5231 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5232
5233 Display *
5234gui_mch_get_display(void)
5235{
5236 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5237 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5238 else
5239 return NULL;
5240}
5241#endif
5242
5243 void
5244gui_mch_beep(void)
5245{
5246#ifdef HAVE_GTK_MULTIHEAD
5247 GdkDisplay *display;
5248
5249 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5250 display = gtk_widget_get_display(gui.mainwin);
5251 else
5252 display = gdk_display_get_default();
5253
5254 if (display != NULL)
5255 gdk_display_beep(display);
5256#else
5257 gdk_beep();
5258#endif
5259}
5260
5261 void
5262gui_mch_flash(int msec)
5263{
5264 GdkGCValues values;
5265 GdkGC *invert_gc;
5266
5267 if (gui.drawarea->window == NULL)
5268 return;
5269
5270 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5271 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5272 values.function = GDK_XOR;
5273 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5274 &values,
5275 GDK_GC_FOREGROUND |
5276 GDK_GC_BACKGROUND |
5277 GDK_GC_FUNCTION);
5278 gdk_gc_set_exposures(invert_gc,
5279 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5280 /*
5281 * Do a visual beep by changing back and forth in some undetermined way,
5282 * the foreground and background colors. This is due to the fact that
5283 * there can't be really any prediction about the effects of XOR on
5284 * arbitrary X11 servers. However this seems to be enough for what we
5285 * intend it to do.
5286 */
5287 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5288 TRUE,
5289 0, 0,
5290 FILL_X((int)Columns) + gui.border_offset,
5291 FILL_Y((int)Rows) + gui.border_offset);
5292
5293 gui_mch_flush();
5294 ui_delay((long)msec, TRUE); /* wait so many msec */
5295
5296 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5297 TRUE,
5298 0, 0,
5299 FILL_X((int)Columns) + gui.border_offset,
5300 FILL_Y((int)Rows) + gui.border_offset);
5301
5302 gdk_gc_destroy(invert_gc);
5303}
5304
5305/*
5306 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5307 */
5308 void
5309gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5310{
5311 GdkGCValues values;
5312 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313
5314 if (gui.drawarea->window == NULL)
5315 return;
5316
Bram Moolenaar89d40322006-08-29 15:30:07 +00005317 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5318 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 values.function = GDK_XOR;
5320 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5321 &values,
5322 GDK_GC_FOREGROUND |
5323 GDK_GC_BACKGROUND |
5324 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005325 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5326 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5328 TRUE,
5329 FILL_X(c), FILL_Y(r),
5330 (nc) * gui.char_width, (nr) * gui.char_height);
5331 gdk_gc_destroy(invert_gc);
5332}
5333
5334/*
5335 * Iconify the GUI window.
5336 */
5337 void
5338gui_mch_iconify(void)
5339{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341}
5342
5343#if defined(FEAT_EVAL) || defined(PROTO)
5344/*
5345 * Bring the Vim window to the foreground.
5346 */
5347 void
5348gui_mch_set_foreground(void)
5349{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351}
5352#endif
5353
5354/*
5355 * Draw a cursor without focus.
5356 */
5357 void
5358gui_mch_draw_hollow_cursor(guicolor_T color)
5359{
5360 int i = 1;
5361
5362 if (gui.drawarea->window == NULL)
5363 return;
5364
5365 gui_mch_set_fg_color(color);
5366
5367 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (mb_lefthalve(gui.row, gui.col))
5369 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5371 FALSE,
5372 FILL_X(gui.col), FILL_Y(gui.row),
5373 i * gui.char_width - 1, gui.char_height - 1);
5374}
5375
5376/*
5377 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5378 * color "color".
5379 */
5380 void
5381gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5382{
5383 if (gui.drawarea->window == NULL)
5384 return;
5385
5386 gui_mch_set_fg_color(color);
5387
5388 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5389 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5390 TRUE,
5391#ifdef FEAT_RIGHTLEFT
5392 /* vertical line should be on the right of current point */
5393 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5394#endif
5395 FILL_X(gui.col),
5396 FILL_Y(gui.row) + gui.char_height - h,
5397 w, h);
5398}
5399
5400
5401/*
5402 * Catch up with any queued X11 events. This may put keyboard input into the
5403 * input buffer, call resize call-backs, trigger timers etc. If there is
5404 * nothing in the X11 event queue (& no timers pending), then we return
5405 * immediately.
5406 */
5407 void
5408gui_mch_update(void)
5409{
5410 while (gtk_events_pending() && !vim_is_input_buf_full())
5411 gtk_main_iteration_do(FALSE);
5412}
5413
5414 static gint
5415input_timer_cb(gpointer data)
5416{
5417 int *timed_out = (int *) data;
5418
Bram Moolenaar49325942007-05-10 19:19:59 +00005419 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 *timed_out = TRUE;
5421
5422 if (gtk_main_level() > 0)
5423 gtk_main_quit();
5424
5425 return FALSE; /* don't happen again */
5426}
5427
5428#ifdef FEAT_SNIFF
5429/*
5430 * Callback function, used when data is available on the SNiFF connection.
5431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 static void
5433sniff_request_cb(
5434 gpointer data,
5435 gint source_fd,
5436 GdkInputCondition condition)
5437{
5438 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5439
5440 add_to_input_buf(bytes, 3);
5441
5442 if (gtk_main_level() > 0)
5443 gtk_main_quit();
5444}
5445#endif
5446
5447/*
5448 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5449 * from the keyboard.
5450 * wtime == -1 Wait forever.
5451 * wtime == 0 This should never happen.
5452 * wtime > 0 Wait wtime milliseconds for a character.
5453 * Returns OK if a character was found to be available within the given time,
5454 * or FAIL otherwise.
5455 */
5456 int
5457gui_mch_wait_for_chars(long wtime)
5458{
5459 int focus;
5460 guint timer;
5461 static int timed_out;
5462#ifdef FEAT_SNIFF
5463 static int sniff_on = 0;
5464 static gint sniff_input_id = 0;
5465#endif
5466
5467#ifdef FEAT_SNIFF
5468 if (sniff_on && !want_sniff_request)
5469 {
5470 if (sniff_input_id)
5471 gdk_input_remove(sniff_input_id);
5472 sniff_on = 0;
5473 }
5474 else if (!sniff_on && want_sniff_request)
5475 {
5476 /* Add fd_from_sniff to watch for available data in main loop. */
5477 sniff_input_id = gdk_input_add(fd_from_sniff,
5478 GDK_INPUT_READ, sniff_request_cb, NULL);
5479 sniff_on = 1;
5480 }
5481#endif
5482
5483 timed_out = FALSE;
5484
5485 /* this timeout makes sure that we will return if no characters arrived in
5486 * time */
5487
5488 if (wtime > 0)
5489 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5490 else
5491 timer = 0;
5492
5493 focus = gui.in_focus;
5494
5495 do
5496 {
5497 /* Stop or start blinking when focus changes */
5498 if (gui.in_focus != focus)
5499 {
5500 if (gui.in_focus)
5501 gui_mch_start_blink();
5502 else
5503 gui_mch_stop_blink();
5504 focus = gui.in_focus;
5505 }
5506
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005507#if defined(FEAT_NETBEANS_INTG)
5508 /* Process the queued netbeans messages. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02005509 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005510#endif
5511
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 /*
5513 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005514 * Skip this if input is available anyway (can happen in rare
5515 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005517 if (!input_available())
5518 gtk_main();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519
5520 /* Got char, return immediately */
5521 if (input_available())
5522 {
5523 if (timer != 0 && !timed_out)
5524 gtk_timeout_remove(timer);
5525 return OK;
5526 }
5527 } while (wtime < 0 || !timed_out);
5528
5529 /*
5530 * Flush all eventually pending (drawing) events.
5531 */
5532 gui_mch_update();
5533
5534 return FAIL;
5535}
5536
5537
5538/****************************************************************************
5539 * Output drawing routines.
5540 ****************************************************************************/
5541
5542
5543/* Flush any output to the screen */
5544 void
5545gui_mch_flush(void)
5546{
5547#ifdef HAVE_GTK_MULTIHEAD
5548 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5549 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5550#else
5551 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 /* This happens to actually do what gui_mch_flush() is supposed to do,
5554 * according to the comment above. */
5555 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5556 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557}
5558
5559/*
5560 * Clear a rectangular region of the screen from text pos (row1, col1) to
5561 * (row2, col2) inclusive.
5562 */
5563 void
5564gui_mch_clear_block(int row1, int col1, int row2, int col2)
5565{
5566 GdkColor color;
5567
5568 if (gui.drawarea->window == NULL)
5569 return;
5570
5571 color.pixel = gui.back_pixel;
5572
5573 gdk_gc_set_foreground(gui.text_gc, &color);
5574
5575 /* Clear one extra pixel at the far right, for when bold characters have
5576 * spilled over to the window border. */
5577 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5578 FILL_X(col1), FILL_Y(row1),
5579 (col2 - col1 + 1) * gui.char_width
5580 + (col2 == Columns - 1),
5581 (row2 - row1 + 1) * gui.char_height);
5582}
5583
5584 void
5585gui_mch_clear_all(void)
5586{
5587 if (gui.drawarea->window != NULL)
5588 gdk_window_clear(gui.drawarea->window);
5589}
5590
5591/*
5592 * Redraw any text revealed by scrolling up/down.
5593 */
5594 static void
5595check_copy_area(void)
5596{
5597 GdkEvent *event;
5598 int expose_count;
5599
5600 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5601 return;
5602
5603 /* Avoid redrawing the cursor while scrolling or it'll end up where
5604 * we don't want it to be. I'm not sure if it's correct to call
5605 * gui_dont_update_cursor() at this point but it works as a quick
5606 * fix for now. */
5607 gui_dont_update_cursor();
5608
5609 do
5610 {
5611 /* Wait to check whether the scroll worked or not. */
5612 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5613
5614 if (event == NULL)
5615 break; /* received NoExpose event */
5616
5617 gui_redraw(event->expose.area.x, event->expose.area.y,
5618 event->expose.area.width, event->expose.area.height);
5619
5620 expose_count = event->expose.count;
5621 gdk_event_free(event);
5622 }
5623 while (expose_count > 0); /* more events follow */
5624
5625 gui_can_update_cursor();
5626}
5627
5628/*
5629 * Delete the given number of lines from the given row, scrolling up any
5630 * text further down within the scroll region.
5631 */
5632 void
5633gui_mch_delete_lines(int row, int num_lines)
5634{
5635 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5636 return; /* Can't see the window */
5637
5638 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5639 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5640
5641 /* copy one extra pixel, for when bold has spilled over */
5642 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5643 FILL_X(gui.scroll_region_left), FILL_Y(row),
5644 gui.drawarea->window,
5645 FILL_X(gui.scroll_region_left),
5646 FILL_Y(row + num_lines),
5647 gui.char_width * (gui.scroll_region_right
5648 - gui.scroll_region_left + 1) + 1,
5649 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5650
5651 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5652 gui.scroll_region_left,
5653 gui.scroll_region_bot, gui.scroll_region_right);
5654 check_copy_area();
5655}
5656
5657/*
5658 * Insert the given number of lines before the given row, scrolling down any
5659 * following text within the scroll region.
5660 */
5661 void
5662gui_mch_insert_lines(int row, int num_lines)
5663{
5664 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5665 return; /* Can't see the window */
5666
5667 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5668 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5669
5670 /* copy one extra pixel, for when bold has spilled over */
5671 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5672 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5673 gui.drawarea->window,
5674 FILL_X(gui.scroll_region_left), FILL_Y(row),
5675 gui.char_width * (gui.scroll_region_right
5676 - gui.scroll_region_left + 1) + 1,
5677 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5678
5679 gui_clear_block(row, gui.scroll_region_left,
5680 row + num_lines - 1, gui.scroll_region_right);
5681 check_copy_area();
5682}
5683
5684/*
5685 * X Selection stuff, for cutting and pasting text to other windows.
5686 */
5687 void
5688clip_mch_request_selection(VimClipboard *cbd)
5689{
5690 GdkAtom target;
5691 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005692 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5695 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005696 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5697 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 received_selection = RS_NONE;
5699 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5700
5701 gtk_selection_convert(gui.drawarea,
5702 cbd->gtk_sel_atom, target,
5703 (guint32)GDK_CURRENT_TIME);
5704
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005705 /* Hack: Wait up to three seconds for the selection. A hang was
5706 * noticed here when using the netrw plugin combined with ":gui"
5707 * during the FocusGained event. */
5708 start = time(NULL);
5709 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 gtk_main(); /* wait for selection_received_cb */
5711
5712 if (received_selection != RS_FAIL)
5713 return;
5714 }
5715
5716 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005717 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718}
5719
5720/*
5721 * Disown the selection.
5722 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005724clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725{
5726 /* WEIRD: when using NULL to actually disown the selection, we lose the
5727 * selection the first time we own it. */
5728 /*
5729 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
5730 gui_mch_update();
5731 */
5732}
5733
5734/*
5735 * Own the selection and return OK if it worked.
5736 */
5737 int
5738clip_mch_own_selection(VimClipboard *cbd)
5739{
5740 int success;
5741
5742 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar7cfea752010-06-22 06:07:12 +02005743 clipboard_event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 gui_mch_update();
5745 return (success) ? OK : FAIL;
5746}
5747
5748/*
5749 * Send the current selection to the clipboard. Do nothing for X because we
5750 * will fill in the selection only when requested by another app.
5751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005753clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754{
5755}
5756
5757
5758#if defined(FEAT_MENU) || defined(PROTO)
5759/*
5760 * Make a menu item appear either active or not active (grey or not grey).
5761 */
5762 void
5763gui_mch_menu_grey(vimmenu_T *menu, int grey)
5764{
5765 if (menu->id == NULL)
5766 return;
5767
5768 if (menu_is_separator(menu->name))
5769 grey = TRUE;
5770
5771 gui_mch_menu_hidden(menu, FALSE);
5772 /* Be clever about bitfields versus true booleans here! */
5773 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5774 {
5775 gtk_widget_set_sensitive(menu->id, !grey);
5776 gui_mch_update();
5777 }
5778}
5779
5780/*
5781 * Make menu item hidden or not hidden.
5782 */
5783 void
5784gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5785{
5786 if (menu->id == 0)
5787 return;
5788
5789 if (hidden)
5790 {
5791 if (GTK_WIDGET_VISIBLE(menu->id))
5792 {
5793 gtk_widget_hide(menu->id);
5794 gui_mch_update();
5795 }
5796 }
5797 else
5798 {
5799 if (!GTK_WIDGET_VISIBLE(menu->id))
5800 {
5801 gtk_widget_show(menu->id);
5802 gui_mch_update();
5803 }
5804 }
5805}
5806
5807/*
5808 * This is called after setting all the menus to grey/hidden or not.
5809 */
5810 void
5811gui_mch_draw_menubar(void)
5812{
5813 /* just make sure that the visual changes get effect immediately */
5814 gui_mch_update();
5815}
5816#endif /* FEAT_MENU */
5817
5818/*
5819 * Scrollbar stuff.
5820 */
5821 void
5822gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5823{
5824 if (sb->id == NULL)
5825 return;
5826
5827 if (flag)
5828 gtk_widget_show(sb->id);
5829 else
5830 gtk_widget_hide(sb->id);
5831
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005832 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833}
5834
5835
5836/*
5837 * Return the RGB value of a pixel as long.
5838 */
5839 long_u
5840gui_mch_get_rgb(guicolor_T pixel)
5841{
5842 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5844 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
5846 return (((unsigned)color.red & 0xff00) << 8)
5847 | ((unsigned)color.green & 0xff00)
5848 | (((unsigned)color.blue & 0xff00) >> 8);
5849}
5850
5851/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005852 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005854 void
5855gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005857 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858}
5859
5860 void
5861gui_mch_setmouse(int x, int y)
5862{
5863 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5864 * internal GDK mechanism present to accomplish this. (and for good
5865 * reason...) */
5866 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5867 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5868 0, 0, 0U, 0U, x, y);
5869}
5870
5871
5872#ifdef FEAT_MOUSESHAPE
5873/* The last set mouse pointer shape is remembered, to be used when it goes
5874 * from hidden to not hidden. */
5875static int last_shape = 0;
5876#endif
5877
5878/*
5879 * Use the blank mouse pointer or not.
5880 *
5881 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5882 */
5883 void
5884gui_mch_mousehide(int hide)
5885{
5886 if (gui.pointer_hidden != hide)
5887 {
5888 gui.pointer_hidden = hide;
5889 if (gui.drawarea->window && gui.blank_pointer != NULL)
5890 {
5891 if (hide)
5892 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5893 else
5894#ifdef FEAT_MOUSESHAPE
5895 mch_set_mouse_shape(last_shape);
5896#else
5897 gdk_window_set_cursor(gui.drawarea->window, NULL);
5898#endif
5899 }
5900 }
5901}
5902
5903#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5904
5905/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5906 * misc2.c! */
5907static const int mshape_ids[] =
5908{
5909 GDK_LEFT_PTR, /* arrow */
5910 GDK_CURSOR_IS_PIXMAP, /* blank */
5911 GDK_XTERM, /* beam */
5912 GDK_SB_V_DOUBLE_ARROW, /* updown */
5913 GDK_SIZING, /* udsizing */
5914 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5915 GDK_SIZING, /* lrsizing */
5916 GDK_WATCH, /* busy */
5917 GDK_X_CURSOR, /* no */
5918 GDK_CROSSHAIR, /* crosshair */
5919 GDK_HAND1, /* hand1 */
5920 GDK_HAND2, /* hand2 */
5921 GDK_PENCIL, /* pencil */
5922 GDK_QUESTION_ARROW, /* question */
5923 GDK_RIGHT_PTR, /* right-arrow */
5924 GDK_CENTER_PTR, /* up-arrow */
5925 GDK_LEFT_PTR /* last one */
5926};
5927
5928 void
5929mch_set_mouse_shape(int shape)
5930{
5931 int id;
5932 GdkCursor *c;
5933
5934 if (gui.drawarea->window == NULL)
5935 return;
5936
5937 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5938 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5939 else
5940 {
5941 if (shape >= MSHAPE_NUMBERED)
5942 {
5943 id = shape - MSHAPE_NUMBERED;
5944 if (id >= GDK_LAST_CURSOR)
5945 id = GDK_LEFT_PTR;
5946 else
5947 id &= ~1; /* they are always even (why?) */
5948 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005949 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005951 else
5952 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953# ifdef HAVE_GTK_MULTIHEAD
5954 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005955 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005957 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958# endif
5959 gdk_window_set_cursor(gui.drawarea->window, c);
5960 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5961 }
5962 if (shape != MSHAPE_HIDE)
5963 last_shape = shape;
5964}
5965#endif /* FEAT_MOUSESHAPE */
5966
5967
5968#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5969/*
5970 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5971 * scaled down if the current font is not big enough, or scaled up if the image
5972 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5973 * will be cut off if the current font is not big enough, or centered if it's
5974 * too small.
5975 */
5976# define SIGN_WIDTH (2 * gui.char_width)
5977# define SIGN_HEIGHT (gui.char_height)
5978# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 void
5981gui_mch_drawsign(int row, int col, int typenr)
5982{
5983 GdkPixbuf *sign;
5984
5985 sign = (GdkPixbuf *)sign_get_image(typenr);
5986
5987 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5988 {
5989 int width;
5990 int height;
5991 int xoffset;
5992 int yoffset;
5993 int need_scale;
5994
5995 width = gdk_pixbuf_get_width(sign);
5996 height = gdk_pixbuf_get_height(sign);
5997 /*
5998 * Decide whether we need to scale. Allow one pixel of border
5999 * width to be cut off, in order to avoid excessive scaling for
6000 * tiny differences in font size.
6001 */
6002 need_scale = (width > SIGN_WIDTH + 2
6003 || height > SIGN_HEIGHT + 2
6004 || (width < 3 * SIGN_WIDTH / 4
6005 && height < 3 * SIGN_HEIGHT / 4));
6006 if (need_scale)
6007 {
6008 double aspect;
6009
6010 /* Keep the original aspect ratio */
6011 aspect = (double)height / (double)width;
6012 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
6013 width = MIN(width, SIGN_WIDTH);
6014 height = (double)width * aspect;
6015
6016 /* This doesn't seem to be worth caching, and doing so
6017 * would complicate the code quite a bit. */
6018 sign = gdk_pixbuf_scale_simple(sign, width, height,
6019 GDK_INTERP_BILINEAR);
6020 if (sign == NULL)
6021 return; /* out of memory */
6022 }
6023
6024 /* The origin is the upper-left corner of the pixmap. Therefore
6025 * these offset may become negative if the pixmap is smaller than
6026 * the 2x1 cells reserved for the sign icon. */
6027 xoffset = (width - SIGN_WIDTH) / 2;
6028 yoffset = (height - SIGN_HEIGHT) / 2;
6029
6030 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
6031
6032 gdk_draw_rectangle(gui.drawarea->window,
6033 gui.text_gc,
6034 TRUE,
6035 FILL_X(col),
6036 FILL_Y(row),
6037 SIGN_WIDTH,
6038 SIGN_HEIGHT);
6039
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 gdk_pixbuf_render_to_drawable_alpha(sign,
6041 gui.drawarea->window,
6042 MAX(0, xoffset),
6043 MAX(0, yoffset),
6044 FILL_X(col) - MIN(0, xoffset),
6045 FILL_Y(row) - MIN(0, yoffset),
6046 MIN(width, SIGN_WIDTH),
6047 MIN(height, SIGN_HEIGHT),
6048 GDK_PIXBUF_ALPHA_BILEVEL,
6049 127,
6050 GDK_RGB_DITHER_NORMAL,
6051 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 if (need_scale)
6053 g_object_unref(sign);
6054 }
6055}
6056
6057 void *
6058gui_mch_register_sign(char_u *signfile)
6059{
6060 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6061 {
6062 GdkPixbuf *sign;
6063 GError *error = NULL;
6064 char_u *message;
6065
6066 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6067
6068 if (error == NULL)
6069 return sign;
6070
6071 message = (char_u *)error->message;
6072
6073 if (message != NULL && input_conv.vc_type != CONV_NONE)
6074 message = string_convert(&input_conv, message, NULL);
6075
6076 if (message != NULL)
6077 {
6078 /* The error message is already translated and will be more
6079 * descriptive than anything we could possibly do ourselves. */
6080 EMSG2("E255: %s", message);
6081
6082 if (input_conv.vc_type != CONV_NONE)
6083 vim_free(message);
6084 }
6085 g_error_free(error);
6086 }
6087
6088 return NULL;
6089}
6090
6091 void
6092gui_mch_destroy_sign(void *sign)
6093{
6094 if (sign != NULL)
6095 g_object_unref(sign);
6096}
6097
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098#endif /* FEAT_SIGN_ICONS */