blob: 3e1616c853b9437e287f042247c362a79bb72f13 [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>
89#endif
90
91/*
92 * Easy-to-use macro for multihead support.
93 */
94#ifdef HAVE_GTK_MULTIHEAD
95# define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
96 gtk_widget_get_display(gui.mainwin), atom)
97#else
98# define GET_X_ATOM(atom) ((Atom)(atom))
99#endif
100
101/* Selection type distinguishers */
102enum
103{
104 TARGET_TYPE_NONE,
105 TARGET_UTF8_STRING,
106 TARGET_STRING,
107 TARGET_COMPOUND_TEXT,
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000108 TARGET_HTML,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 TARGET_TEXT,
110 TARGET_TEXT_URI_LIST,
111 TARGET_TEXT_PLAIN,
112 TARGET_VIM,
113 TARGET_VIMENC
114};
115
116/*
117 * Table of selection targets supported by Vim.
118 * Note: Order matters, preferred types should come first.
119 */
120static const GtkTargetEntry selection_targets[] =
121{
122 {VIMENC_ATOM_NAME, 0, TARGET_VIMENC},
123 {VIM_ATOM_NAME, 0, TARGET_VIM},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000124 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT},
127 {"TEXT", 0, TARGET_TEXT},
128 {"STRING", 0, TARGET_STRING}
129};
130#define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
131
132#ifdef FEAT_DND
133/*
134 * Table of DnD targets supported by Vim.
135 * Note: Order matters, preferred types should come first.
136 */
137static const GtkTargetEntry dnd_targets[] =
138{
139 {"text/uri-list", 0, TARGET_TEXT_URI_LIST},
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000140 {"text/html", 0, TARGET_HTML},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141 {"UTF8_STRING", 0, TARGET_UTF8_STRING},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 {"STRING", 0, TARGET_STRING},
143 {"text/plain", 0, TARGET_TEXT_PLAIN}
144};
145# define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
146#endif
147
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149/*
150 * "Monospace" is a standard font alias that should be present
151 * on all proper Pango/fontconfig installations.
152 */
153# define DEFAULT_FONT "Monospace 10"
154
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
156/*
157 * Atoms used to communicate save-yourself from the X11 session manager. There
158 * is no need to move them into the GUI struct, since they should be constant.
159 */
160static GdkAtom wm_protocols_atom = GDK_NONE;
161static GdkAtom save_yourself_atom = GDK_NONE;
162#endif
163
164/*
165 * Atoms used to control/reference X11 selections.
166 */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +0000167static GdkAtom html_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static GdkAtom utf8_string_atom = GDK_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169static GdkAtom vim_atom = GDK_NONE; /* Vim's own special selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170static GdkAtom vimenc_atom = GDK_NONE; /* Vim's extended selection format */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172/*
173 * Keycodes recognized by vim.
174 * NOTE: when changing this, the table in gui_x11.c probably needs the same
175 * change!
176 */
177static struct special_key
178{
179 guint key_sym;
180 char_u code0;
181 char_u code1;
182}
183const special_keys[] =
184{
185 {GDK_Up, 'k', 'u'},
186 {GDK_Down, 'k', 'd'},
187 {GDK_Left, 'k', 'l'},
188 {GDK_Right, 'k', 'r'},
189 {GDK_F1, 'k', '1'},
190 {GDK_F2, 'k', '2'},
191 {GDK_F3, 'k', '3'},
192 {GDK_F4, 'k', '4'},
193 {GDK_F5, 'k', '5'},
194 {GDK_F6, 'k', '6'},
195 {GDK_F7, 'k', '7'},
196 {GDK_F8, 'k', '8'},
197 {GDK_F9, 'k', '9'},
198 {GDK_F10, 'k', ';'},
199 {GDK_F11, 'F', '1'},
200 {GDK_F12, 'F', '2'},
201 {GDK_F13, 'F', '3'},
202 {GDK_F14, 'F', '4'},
203 {GDK_F15, 'F', '5'},
204 {GDK_F16, 'F', '6'},
205 {GDK_F17, 'F', '7'},
206 {GDK_F18, 'F', '8'},
207 {GDK_F19, 'F', '9'},
208 {GDK_F20, 'F', 'A'},
209 {GDK_F21, 'F', 'B'},
210 {GDK_Pause, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
211 {GDK_F22, 'F', 'C'},
212 {GDK_F23, 'F', 'D'},
213 {GDK_F24, 'F', 'E'},
214 {GDK_F25, 'F', 'F'},
215 {GDK_F26, 'F', 'G'},
216 {GDK_F27, 'F', 'H'},
217 {GDK_F28, 'F', 'I'},
218 {GDK_F29, 'F', 'J'},
219 {GDK_F30, 'F', 'K'},
220 {GDK_F31, 'F', 'L'},
221 {GDK_F32, 'F', 'M'},
222 {GDK_F33, 'F', 'N'},
223 {GDK_F34, 'F', 'O'},
224 {GDK_F35, 'F', 'P'},
225#ifdef SunXK_F36
226 {SunXK_F36, 'F', 'Q'},
227 {SunXK_F37, 'F', 'R'},
228#endif
229 {GDK_Help, '%', '1'},
230 {GDK_Undo, '&', '8'},
231 {GDK_BackSpace, 'k', 'b'},
232 {GDK_Insert, 'k', 'I'},
233 {GDK_Delete, 'k', 'D'},
234 {GDK_3270_BackTab, 'k', 'B'},
235 {GDK_Clear, 'k', 'C'},
236 {GDK_Home, 'k', 'h'},
237 {GDK_End, '@', '7'},
238 {GDK_Prior, 'k', 'P'},
239 {GDK_Next, 'k', 'N'},
240 {GDK_Print, '%', '9'},
241 /* Keypad keys: */
242 {GDK_KP_Left, 'k', 'l'},
243 {GDK_KP_Right, 'k', 'r'},
244 {GDK_KP_Up, 'k', 'u'},
245 {GDK_KP_Down, 'k', 'd'},
246 {GDK_KP_Insert, KS_EXTRA, (char_u)KE_KINS},
247 {GDK_KP_Delete, KS_EXTRA, (char_u)KE_KDEL},
248 {GDK_KP_Home, 'K', '1'},
249 {GDK_KP_End, 'K', '4'},
250 {GDK_KP_Prior, 'K', '3'}, /* page up */
251 {GDK_KP_Next, 'K', '5'}, /* page down */
252
253 {GDK_KP_Add, 'K', '6'},
254 {GDK_KP_Subtract, 'K', '7'},
255 {GDK_KP_Divide, 'K', '8'},
256 {GDK_KP_Multiply, 'K', '9'},
257 {GDK_KP_Enter, 'K', 'A'},
258 {GDK_KP_Decimal, 'K', 'B'},
259
260 {GDK_KP_0, 'K', 'C'},
261 {GDK_KP_1, 'K', 'D'},
262 {GDK_KP_2, 'K', 'E'},
263 {GDK_KP_3, 'K', 'F'},
264 {GDK_KP_4, 'K', 'G'},
265 {GDK_KP_5, 'K', 'H'},
266 {GDK_KP_6, 'K', 'I'},
267 {GDK_KP_7, 'K', 'J'},
268 {GDK_KP_8, 'K', 'K'},
269 {GDK_KP_9, 'K', 'L'},
270
271 /* End of list marker: */
272 {0, 0, 0}
273};
274
275/*
276 * Flags for command line options table below.
277 */
278#define ARG_FONT 1
279#define ARG_GEOMETRY 2
280#define ARG_REVERSE 3
281#define ARG_NOREVERSE 4
282#define ARG_BACKGROUND 5
283#define ARG_FOREGROUND 6
284#define ARG_ICONIC 7
285#define ARG_ROLE 8
286#define ARG_NETBEANS 9
287#define ARG_XRM 10 /* ignored */
288#define ARG_MENUFONT 11 /* ignored */
289#define ARG_INDEX_MASK 0x00ff
290#define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
291#define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
292#define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
293#define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
294#define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
295
296/*
297 * This table holds all the X GUI command line options allowed. This includes
298 * the standard ones so that we can skip them when Vim is started without the
299 * GUI (but the GUI might start up later).
300 *
301 * When changing this, also update doc/gui_x11.txt and the usage message!!!
302 */
303typedef struct
304{
305 const char *name;
306 unsigned int flags;
307}
308cmdline_option_T;
309
310static const cmdline_option_T cmdline_options[] =
311{
312 /* We handle these options ourselves */
313 {"-fn", ARG_FONT|ARG_HAS_VALUE},
314 {"-font", ARG_FONT|ARG_HAS_VALUE},
315 {"-geom", ARG_GEOMETRY|ARG_HAS_VALUE},
316 {"-geometry", ARG_GEOMETRY|ARG_HAS_VALUE},
317 {"-rv", ARG_REVERSE},
318 {"-reverse", ARG_REVERSE},
319 {"+rv", ARG_NOREVERSE},
320 {"+reverse", ARG_NOREVERSE},
321 {"-bg", ARG_BACKGROUND|ARG_HAS_VALUE},
322 {"-background", ARG_BACKGROUND|ARG_HAS_VALUE},
323 {"-fg", ARG_FOREGROUND|ARG_HAS_VALUE},
324 {"-foreground", ARG_FOREGROUND|ARG_HAS_VALUE},
325 {"-iconic", ARG_ICONIC},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {"--role", ARG_ROLE|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_NETBEANS_INTG
328 {"-nb", ARG_NETBEANS}, /* non-standard value format */
329 {"-xrm", ARG_XRM|ARG_HAS_VALUE}, /* not implemented */
330 {"-mf", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
331 {"-menufont", ARG_MENUFONT|ARG_HAS_VALUE}, /* not implemented */
332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 /* Arguments handled by GTK (and GNOME) internally. */
334 {"--g-fatal-warnings", ARG_FOR_GTK},
335 {"--gdk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
336 {"--gdk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
337 {"--gtk-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
338 {"--gtk-no-debug", ARG_FOR_GTK|ARG_HAS_VALUE},
339 {"--gtk-module", ARG_FOR_GTK|ARG_HAS_VALUE},
340 {"--sync", ARG_FOR_GTK},
341 {"--display", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
342 {"--name", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
343 {"--class", ARG_FOR_GTK|ARG_HAS_VALUE|ARG_COMPAT_LONG},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 {"--screen", ARG_FOR_GTK|ARG_HAS_VALUE},
345 {"--gxid-host", ARG_FOR_GTK|ARG_HAS_VALUE},
346 {"--gxid-port", ARG_FOR_GTK|ARG_HAS_VALUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef FEAT_GUI_GNOME
348 {"--load-modules", ARG_FOR_GTK|ARG_HAS_VALUE},
349 {"--sm-client-id", ARG_FOR_GTK|ARG_HAS_VALUE},
350 {"--sm-config-prefix", ARG_FOR_GTK|ARG_HAS_VALUE},
351 {"--sm-disable", ARG_FOR_GTK},
352 {"--oaf-ior-fd", ARG_FOR_GTK|ARG_HAS_VALUE},
353 {"--oaf-activate-iid", ARG_FOR_GTK|ARG_HAS_VALUE},
354 {"--oaf-private", ARG_FOR_GTK},
355 {"--enable-sound", ARG_FOR_GTK},
356 {"--disable-sound", ARG_FOR_GTK},
357 {"--espeaker", ARG_FOR_GTK|ARG_HAS_VALUE},
358 {"-?", ARG_FOR_GTK|ARG_NEEDS_GUI},
359 {"--help", ARG_FOR_GTK|ARG_NEEDS_GUI|ARG_KEEP},
360 {"--usage", ARG_FOR_GTK|ARG_NEEDS_GUI},
361# if 0 /* conflicts with Vim's own --version argument */
362 {"--version", ARG_FOR_GTK|ARG_NEEDS_GUI},
363# endif
364 {"--disable-crash-dialog", ARG_FOR_GTK},
365#endif
366 {NULL, 0}
367};
368
369static int gui_argc = 0;
370static char **gui_argv = NULL;
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372static const char *role_argument = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
374static const char *restart_command = NULL;
Bram Moolenaar9085f802009-06-03 14:20:21 +0000375static char *abs_restart_command = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#endif
377static int found_iconic_arg = FALSE;
378
379#ifdef FEAT_GUI_GNOME
380/*
381 * Can't use Gnome if --socketid given
382 */
383static int using_gnome = 0;
384#else
385# define using_gnome 0
386#endif
387
388/*
389 * Parse the GUI related command-line arguments. Any arguments used are
390 * deleted from argv, and *argc is decremented accordingly. This is called
391 * when vim is started, whether or not the GUI has been started.
392 */
393 void
394gui_mch_prepare(int *argc, char **argv)
395{
396 const cmdline_option_T *option;
397 int i = 0;
398 int len = 0;
399
400#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
401 /*
402 * Determine the command used to invoke Vim, to be passed as restart
403 * command to the session manager. If argv[0] contains any directory
404 * components try building an absolute path, otherwise leave it as is.
405 */
406 restart_command = argv[0];
407
408 if (strchr(argv[0], G_DIR_SEPARATOR) != NULL)
409 {
410 char_u buf[MAXPATHL];
411
412 if (mch_FullName((char_u *)argv[0], buf, (int)sizeof(buf), TRUE) == OK)
Bram Moolenaar9085f802009-06-03 14:20:21 +0000413 {
414 abs_restart_command = (char *)vim_strsave(buf);
415 restart_command = abs_restart_command;
416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 }
418#endif
419
420 /*
421 * Move all the entries in argv which are relevant to GTK+ and GNOME
422 * into gui_argv. Freed later in gui_mch_init().
423 */
424 gui_argc = 0;
425 gui_argv = (char **)alloc((unsigned)((*argc + 1) * sizeof(char *)));
426
427 g_return_if_fail(gui_argv != NULL);
428
429 gui_argv[gui_argc++] = argv[i++];
430
431 while (i < *argc)
432 {
433 /* Don't waste CPU cycles on non-option arguments. */
434 if (argv[i][0] != '-' && argv[i][0] != '+')
435 {
436 ++i;
437 continue;
438 }
439
440 /* Look for argv[i] in cmdline_options[] table. */
441 for (option = &cmdline_options[0]; option->name != NULL; ++option)
442 {
443 len = strlen(option->name);
444
445 if (strncmp(argv[i], option->name, len) == 0)
446 {
447 if (argv[i][len] == '\0')
448 break;
449 /* allow --foo=bar style */
450 if (argv[i][len] == '=' && (option->flags & ARG_HAS_VALUE))
451 break;
452#ifdef FEAT_NETBEANS_INTG
453 /* darn, -nb has non-standard syntax */
454 if (vim_strchr((char_u *)":=", argv[i][len]) != NULL
455 && (option->flags & ARG_INDEX_MASK) == ARG_NETBEANS)
456 break;
457#endif
458 }
459 else if ((option->flags & ARG_COMPAT_LONG)
460 && strcmp(argv[i], option->name + 1) == 0)
461 {
462 /* Replace the standard X arguments "-name" and "-display"
463 * with their GNU-style long option counterparts. */
464 argv[i] = (char *)option->name;
465 break;
466 }
467 }
468 if (option->name == NULL) /* no match */
469 {
470 ++i;
471 continue;
472 }
473
474 if (option->flags & ARG_FOR_GTK)
475 {
476 /* Move the argument into gui_argv, which
477 * will later be passed to gtk_init_check() */
478 gui_argv[gui_argc++] = argv[i];
479 }
480 else
481 {
482 char *value = NULL;
483
484 /* Extract the option's value if there is one.
485 * Accept both "--foo bar" and "--foo=bar" style. */
486 if (option->flags & ARG_HAS_VALUE)
487 {
488 if (argv[i][len] == '=')
489 value = &argv[i][len + 1];
490 else if (i + 1 < *argc && strcmp(argv[i + 1], "--") != 0)
491 value = argv[i + 1];
492 }
493
494 /* Check for options handled by Vim itself */
495 switch (option->flags & ARG_INDEX_MASK)
496 {
497 case ARG_REVERSE:
498 found_reverse_arg = TRUE;
499 break;
500 case ARG_NOREVERSE:
501 found_reverse_arg = FALSE;
502 break;
503 case ARG_FONT:
504 font_argument = value;
505 break;
506 case ARG_GEOMETRY:
507 if (value != NULL)
508 gui.geom = vim_strsave((char_u *)value);
509 break;
510 case ARG_BACKGROUND:
511 background_argument = value;
512 break;
513 case ARG_FOREGROUND:
514 foreground_argument = value;
515 break;
516 case ARG_ICONIC:
517 found_iconic_arg = TRUE;
518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 case ARG_ROLE:
520 role_argument = value; /* used later in gui_mch_open() */
521 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_NETBEANS_INTG
523 case ARG_NETBEANS:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 gui.dofork = FALSE; /* don't fork() when starting GUI */
525 netbeansArg = argv[i];
526 break;
527#endif
528 default:
529 break;
530 }
531 }
532
533 /* These arguments make gnome_program_init() print a message and exit.
534 * Must start the GUI for this, otherwise ":gui" will exit later! */
535 if (option->flags & ARG_NEEDS_GUI)
536 gui.starting = TRUE;
537
538 if (option->flags & ARG_KEEP)
539 ++i;
540 else
541 {
542 /* Remove the flag from the argument vector. */
543 if (--*argc > i)
544 {
545 int n_strip = 1;
546
547 /* Move the argument's value as well, if there is one. */
548 if ((option->flags & ARG_HAS_VALUE)
549 && argv[i][len] != '='
550 && strcmp(argv[i + 1], "--") != 0)
551 {
552 ++n_strip;
553 --*argc;
554 if (option->flags & ARG_FOR_GTK)
555 gui_argv[gui_argc++] = argv[i + 1];
556 }
557
558 if (*argc > i)
559 mch_memmove(&argv[i], &argv[i + n_strip],
560 (*argc - i) * sizeof(char *));
561 }
562 argv[*argc] = NULL;
563 }
564 }
565
566 gui_argv[gui_argc] = NULL;
567}
568
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000569#if defined(EXITFREE) || defined(PROTO)
570 void
571gui_mch_free_all()
572{
573 vim_free(gui_argv);
Bram Moolenaar9085f802009-06-03 14:20:21 +0000574#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
575 vim_free(abs_restart_command);
576#endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000577}
578#endif
579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580/*
581 * This should be maybe completely removed.
582 * Doesn't seem possible, since check_copy_area() relies on
583 * this information. --danielk
584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000586visibility_event(GtkWidget *widget UNUSED,
587 GdkEventVisibility *event,
588 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 gui.visibility = event->state;
591 /*
592 * When we do an gdk_window_copy_area(), and the window is partially
593 * obscured, we want to receive an event to tell us whether it worked
594 * or not.
595 */
596 if (gui.text_gc != NULL)
597 gdk_gc_set_exposures(gui.text_gc,
598 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
599 return FALSE;
600}
601
602/*
603 * Redraw the corresponding portions of the screen.
604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000606expose_event(GtkWidget *widget UNUSED,
607 GdkEventExpose *event,
608 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 /* Skip this when the GUI isn't set up yet, will redraw later. */
611 if (gui.starting)
612 return FALSE;
613
614 out_flush(); /* make sure all output has been processed */
615 gui_redraw(event->area.x, event->area.y,
616 event->area.width, event->area.height);
617
618 /* Clear the border areas if needed */
619 if (event->area.x < FILL_X(0))
620 gdk_window_clear_area(gui.drawarea->window, 0, 0, FILL_X(0), 0);
621 if (event->area.y < FILL_Y(0))
622 gdk_window_clear_area(gui.drawarea->window, 0, 0, 0, FILL_Y(0));
623 if (event->area.x > FILL_X(Columns))
624 gdk_window_clear_area(gui.drawarea->window,
625 FILL_X((int)Columns), 0, 0, 0);
626 if (event->area.y > FILL_Y(Rows))
627 gdk_window_clear_area(gui.drawarea->window, 0, FILL_Y((int)Rows), 0, 0);
628
629 return FALSE;
630}
631
632#ifdef FEAT_CLIENTSERVER
633/*
634 * Handle changes to the "Comm" property
635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000637property_event(GtkWidget *widget,
638 GdkEventProperty *event,
639 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 if (event->type == GDK_PROPERTY_NOTIFY
642 && event->state == (int)GDK_PROPERTY_NEW_VALUE
643 && GDK_WINDOW_XWINDOW(event->window) == commWindow
644 && GET_X_ATOM(event->atom) == commProperty)
645 {
646 XEvent xev;
647
648 /* Translate to XLib */
649 xev.xproperty.type = PropertyNotify;
650 xev.xproperty.atom = commProperty;
651 xev.xproperty.window = commWindow;
652 xev.xproperty.state = PropertyNewValue;
653 serverEventProc(GDK_WINDOW_XDISPLAY(widget->window), &xev);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 }
655 return FALSE;
656}
657#endif
658
659
660/****************************************************************************
661 * Focus handlers:
662 */
663
664
665/*
666 * This is a simple state machine:
667 * BLINK_NONE not blinking at all
668 * BLINK_OFF blinking, cursor is not shown
669 * BLINK_ON blinking, cursor is shown
670 */
671
672#define BLINK_NONE 0
673#define BLINK_OFF 1
674#define BLINK_ON 2
675
676static int blink_state = BLINK_NONE;
677static long_u blink_waittime = 700;
678static long_u blink_ontime = 400;
679static long_u blink_offtime = 250;
680static guint blink_timer = 0;
681
682 void
683gui_mch_set_blinking(long waittime, long on, long off)
684{
685 blink_waittime = waittime;
686 blink_ontime = on;
687 blink_offtime = off;
688}
689
690/*
691 * Stop the cursor blinking. Show the cursor if it wasn't shown.
692 */
693 void
694gui_mch_stop_blink(void)
695{
696 if (blink_timer)
697 {
698 gtk_timeout_remove(blink_timer);
699 blink_timer = 0;
700 }
701 if (blink_state == BLINK_OFF)
702 gui_update_cursor(TRUE, FALSE);
703 blink_state = BLINK_NONE;
704}
705
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000707blink_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
709 if (blink_state == BLINK_ON)
710 {
711 gui_undraw_cursor();
712 blink_state = BLINK_OFF;
713 blink_timer = gtk_timeout_add((guint32)blink_offtime,
714 (GtkFunction) blink_cb, NULL);
715 }
716 else
717 {
718 gui_update_cursor(TRUE, FALSE);
719 blink_state = BLINK_ON;
720 blink_timer = gtk_timeout_add((guint32)blink_ontime,
721 (GtkFunction) blink_cb, NULL);
722 }
723
724 return FALSE; /* don't happen again */
725}
726
727/*
728 * Start the cursor blinking. If it was already blinking, this restarts the
729 * waiting time and shows the cursor.
730 */
731 void
732gui_mch_start_blink(void)
733{
734 if (blink_timer)
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 gtk_timeout_remove(blink_timer);
Bram Moolenaar7bcdb7d2014-04-06 21:08:45 +0200737 blink_timer = 0;
738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 /* Only switch blinking on if none of the times is zero */
740 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
741 {
742 blink_timer = gtk_timeout_add((guint32)blink_waittime,
743 (GtkFunction) blink_cb, NULL);
744 blink_state = BLINK_ON;
745 gui_update_cursor(TRUE, FALSE);
746 }
747}
748
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000750enter_notify_event(GtkWidget *widget UNUSED,
751 GdkEventCrossing *event UNUSED,
752 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753{
754 if (blink_state == BLINK_NONE)
755 gui_mch_start_blink();
756
757 /* make sure keyboard input goes there */
758 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
759 gtk_widget_grab_focus(gui.drawarea);
760
761 return FALSE;
762}
763
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000765leave_notify_event(GtkWidget *widget UNUSED,
766 GdkEventCrossing *event UNUSED,
767 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768{
769 if (blink_state != BLINK_NONE)
770 gui_mch_stop_blink();
771
772 return FALSE;
773}
774
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000776focus_in_event(GtkWidget *widget,
777 GdkEventFocus *event UNUSED,
778 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779{
780 gui_focus_change(TRUE);
781
782 if (blink_state == BLINK_NONE)
783 gui_mch_start_blink();
784
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000785 /* make sure keyboard input goes to the draw area (if this is focus for a
786 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000787 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000788 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 return TRUE;
791}
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000794focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200795 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000796 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797{
798 gui_focus_change(FALSE);
799
800 if (blink_state != BLINK_NONE)
801 gui_mch_stop_blink();
802
803 return TRUE;
804}
805
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807/*
808 * Translate a GDK key value to UTF-8 independently of the current locale.
809 * The output is written to string, which must have room for at least 6 bytes
810 * plus the NUL terminator. Returns the length in bytes.
811 *
812 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
813 * of GdkEventKey::string instead. But event->string is evil; see here why:
814 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
815 */
816 static int
817keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
818{
819 int len;
820 guint32 uc;
821
822 uc = gdk_keyval_to_unicode(keyval);
823 if (uc != 0)
824 {
825 /* Check for CTRL-foo */
826 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
827 {
828 /* These mappings look arbitrary at the first glance, but in fact
829 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
830 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
831 * more sense and is consistent with usual terminal behaviour). */
832 if (uc >= '@')
833 string[0] = uc & 0x1F;
834 else if (uc == '2')
835 string[0] = NUL;
836 else if (uc >= '3' && uc <= '7')
837 string[0] = uc ^ 0x28;
838 else if (uc == '8')
839 string[0] = BS;
840 else if (uc == '?')
841 string[0] = DEL;
842 else
843 string[0] = uc;
844 len = 1;
845 }
846 else
847 {
848 /* Translate a normal key to UTF-8. This doesn't work for dead
849 * keys of course, you _have_ to use an input method for that. */
850 len = utf_char2bytes((int)uc, string);
851 }
852 }
853 else
854 {
855 /* Translate keys which are represented by ASCII control codes in Vim.
856 * There are only a few of those; most control keys are translated to
857 * special terminal-like control sequences. */
858 len = 1;
859 switch (keyval)
860 {
861 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
862 string[0] = TAB;
863 break;
864 case GDK_Linefeed:
865 string[0] = NL;
866 break;
867 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
868 string[0] = CAR;
869 break;
870 case GDK_Escape:
871 string[0] = ESC;
872 break;
873 default:
874 len = 0;
875 break;
876 }
877 }
878 string[len] = NUL;
879
880 return len;
881}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000883 static int
884modifiers_gdk2vim(guint state)
885{
886 int modifiers = 0;
887
888 if (state & GDK_SHIFT_MASK)
889 modifiers |= MOD_MASK_SHIFT;
890 if (state & GDK_CONTROL_MASK)
891 modifiers |= MOD_MASK_CTRL;
892 if (state & GDK_MOD1_MASK)
893 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +0200894#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200895 if (state & GDK_SUPER_MASK)
896 modifiers |= MOD_MASK_META;
897#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000898 if (state & GDK_MOD4_MASK)
899 modifiers |= MOD_MASK_META;
900
901 return modifiers;
902}
903
904 static int
905modifiers_gdk2mouse(guint state)
906{
907 int modifiers = 0;
908
909 if (state & GDK_SHIFT_MASK)
910 modifiers |= MOUSE_SHIFT;
911 if (state & GDK_CONTROL_MASK)
912 modifiers |= MOUSE_CTRL;
913 if (state & GDK_MOD1_MASK)
914 modifiers |= MOUSE_ALT;
915
916 return modifiers;
917}
918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919/*
920 * Main keyboard handler:
921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000923key_press_event(GtkWidget *widget UNUSED,
924 GdkEventKey *event,
925 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200927 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
929 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 guint key_sym;
931 int len;
932 int i;
933 int modifiers;
934 int key;
935 guint state;
936 char_u *s, *d;
937
Bram Moolenaar20892c12011-06-26 04:49:00 +0200938 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 key_sym = event->keyval;
940 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941
942#ifdef FEAT_XIM
943 if (xim_queue_key_press_event(event, TRUE))
944 return TRUE;
945#endif
946
947#ifdef FEAT_HANGULIN
948 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
949 {
950 hangul_input_state_toggle();
951 return TRUE;
952 }
953#endif
954
955#ifdef SunXK_F36
956 /*
957 * These keys have bogus lookup strings, and trapping them here is
958 * easier than trying to XRebindKeysym() on them with every possible
959 * combination of modifiers.
960 */
961 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
962 len = 0;
963 else
964#endif
965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 len = keyval_to_string(key_sym, state, string2);
967
968 /* Careful: convert_input() doesn't handle the NUL character.
969 * No need to convert pure ASCII anyway, thus the len > 1 check. */
970 if (len > 1 && input_conv.vc_type != CONV_NONE)
971 len = convert_input(string2, len, sizeof(string2));
972
973 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 d = string;
975 for (i = 0; i < len; ++i)
976 {
977 *d++ = s[i];
978 if (d[-1] == CSI && d + 2 < string + sizeof(string))
979 {
980 /* Turn CSI into K_CSI. */
981 *d++ = KS_EXTRA;
982 *d++ = (int)KE_CSI;
983 }
984 }
985 len = d - string;
986 }
987
988 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
989 if (key_sym == GDK_ISO_Left_Tab)
990 {
991 key_sym = GDK_Tab;
992 state |= GDK_SHIFT_MASK;
993 }
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#ifdef FEAT_MENU
996 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
997 * is a menu shortcut, we ignore everything with the ALT modifier. */
998 if ((state & GDK_MOD1_MASK)
999 && gui.menu_is_active
1000 && (*p_wak == 'y'
1001 || (*p_wak == 'm'
1002 && len == 1
1003 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /* For GTK2 we return false to signify that we haven't handled the
1005 * keypress, so that gtk will handle the mnemonic or accelerator. */
1006 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
1008
1009 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1010 * that already has the 8th bit set.
1011 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1012 * Don't do this for double-byte encodings, it turns the char into a lead
1013 * byte. */
1014 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001015 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001016#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001017 || (state & GDK_SUPER_MASK)
1018#endif
1019 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1021 && (string[0] & 0x80) == 0
1022 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 )
1025 {
1026 string[0] |= 0x80;
1027 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (enc_utf8) /* convert to utf-8 */
1029 {
1030 string[1] = string[0] & 0xbf;
1031 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1032 if (string[1] == CSI)
1033 {
1034 string[2] = KS_EXTRA;
1035 string[3] = (int)KE_CSI;
1036 len = 4;
1037 }
1038 else
1039 len = 2;
1040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 }
1042
1043 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1044 * value) to detect backspace, delete and keypad keys. */
1045 if (len == 0 || len == 1)
1046 {
1047 for (i = 0; special_keys[i].key_sym != 0; i++)
1048 {
1049 if (special_keys[i].key_sym == key_sym)
1050 {
1051 string[0] = CSI;
1052 string[1] = special_keys[i].code0;
1053 string[2] = special_keys[i].code1;
1054 len = -3;
1055 break;
1056 }
1057 }
1058 }
1059
1060 if (len == 0) /* Unrecognized key */
1061 return TRUE;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 /* Special keys (and a few others) may have modifiers. Also when using a
1064 * double-byte encoding (can't set the 8th bit). */
1065 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1066 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1067 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1068 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001069 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001070#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001071 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001073 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001075 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
1077 /*
1078 * For some keys a shift modifier is translated into another key
1079 * code.
1080 */
1081 if (len == -3)
1082 key = TO_SPECIAL(string[1], string[2]);
1083 else
1084 key = string[0];
1085
1086 key = simplify_key(key, &modifiers);
1087 if (key == CSI)
1088 key = K_CSI;
1089 if (IS_SPECIAL(key))
1090 {
1091 string[0] = CSI;
1092 string[1] = K_SECOND(key);
1093 string[2] = K_THIRD(key);
1094 len = 3;
1095 }
1096 else
1097 {
1098 string[0] = key;
1099 len = 1;
1100 }
1101
1102 if (modifiers != 0)
1103 {
1104 string2[0] = CSI;
1105 string2[1] = KS_MODIFIER;
1106 string2[2] = modifiers;
1107 add_to_input_buf(string2, 3);
1108 }
1109 }
1110
1111 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1112 || (string[0] == intr_char && intr_char != Ctrl_C)))
1113 {
1114 trash_input_buf();
1115 got_int = TRUE;
1116 }
1117
1118 add_to_input_buf(string, len);
1119
1120 /* blank out the pointer if necessary */
1121 if (p_mh)
1122 gui_mch_mousehide(TRUE);
1123
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 return TRUE;
1125}
1126
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001127#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001129key_release_event(GtkWidget *widget UNUSED,
1130 GdkEventKey *event,
1131 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132{
Bram Moolenaar20892c12011-06-26 04:49:00 +02001133 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 /*
1135 * GTK+ 2 input methods may do fancy stuff on key release events too.
1136 * With the default IM for instance, you can enter any UCS code point
1137 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1138 */
1139 return xim_queue_key_press_event(event, FALSE);
1140}
1141#endif
1142
1143
1144/****************************************************************************
1145 * Selection handlers:
1146 */
1147
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001149selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001151 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152{
1153 if (event->selection == clip_plus.gtk_sel_atom)
1154 clip_lose_selection(&clip_plus);
1155 else
1156 clip_lose_selection(&clip_star);
1157
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 return TRUE;
1159}
1160
1161#define RS_NONE 0 /* selection_received_cb() not called yet */
1162#define RS_OK 1 /* selection_received_cb() called and OK */
1163#define RS_FAIL 2 /* selection_received_cb() called and failed */
1164static int received_selection = RS_NONE;
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001167selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001169 guint time_ UNUSED,
1170 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 VimClipboard *cbd;
1173 char_u *text;
1174 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001177 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
1179 if (data->selection == clip_plus.gtk_sel_atom)
1180 cbd = &clip_plus;
1181 else
1182 cbd = &clip_star;
1183
1184 text = (char_u *)data->data;
1185 len = data->length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186
1187 if (text == NULL || len <= 0)
1188 {
1189 received_selection = RS_FAIL;
1190 /* clip_free_selection(cbd); ??? */
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 return;
1193 }
1194
1195 if (data->type == vim_atom)
1196 {
1197 motion_type = *text++;
1198 --len;
1199 }
1200
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (data->type == vimenc_atom)
1202 {
1203 char_u *enc;
1204 vimconv_T conv;
1205
1206 motion_type = *text++;
1207 --len;
1208
1209 enc = text;
1210 text += STRLEN(text) + 1;
1211 len -= text - enc;
1212
1213 /* If the encoding of the text is different from 'encoding', attempt
1214 * converting it. */
1215 conv.vc_type = CONV_NONE;
1216 convert_setup(&conv, enc, p_enc);
1217 if (conv.vc_type != CONV_NONE)
1218 {
1219 tmpbuf = string_convert(&conv, text, &len);
1220 if (tmpbuf != NULL)
1221 text = tmpbuf;
1222 convert_setup(&conv, NULL, NULL);
1223 }
1224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 /* gtk_selection_data_get_text() handles all the nasty details
1227 * and targets and encodings etc. This rocks so hard. */
1228 else
1229 {
1230 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1231 if (tmpbuf_utf8 != NULL)
1232 {
1233 len = STRLEN(tmpbuf_utf8);
1234 if (input_conv.vc_type != CONV_NONE)
1235 {
1236 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1237 if (tmpbuf != NULL)
1238 text = tmpbuf;
1239 }
1240 else
1241 text = tmpbuf_utf8;
1242 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001243 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1244 {
1245 vimconv_T conv;
1246
1247 /* UTF-16, we get this for HTML */
1248 conv.vc_type = CONV_NONE;
1249 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1250
1251 if (conv.vc_type != CONV_NONE)
1252 {
1253 text += 2;
1254 len -= 2;
1255 tmpbuf = string_convert(&conv, text, &len);
1256 convert_setup(&conv, NULL, NULL);
1257 }
1258 if (tmpbuf != NULL)
1259 text = tmpbuf;
1260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001263 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001264 while (len > 0 && text[len - 1] == NUL)
1265 --len;
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 clip_yank_selection(motion_type, text, (long)len, cbd);
1268 received_selection = RS_OK;
1269 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271}
1272
1273/*
1274 * Prepare our selection data for passing it to the external selection
1275 * client.
1276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001278selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 GtkSelectionData *selection_data,
1280 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001281 guint time_ UNUSED,
1282 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 char_u *string;
1285 char_u *tmpbuf;
1286 long_u tmplen;
1287 int length;
1288 int motion_type;
1289 GdkAtom type;
1290 VimClipboard *cbd;
1291
1292 if (selection_data->selection == clip_plus.gtk_sel_atom)
1293 cbd = &clip_plus;
1294 else
1295 cbd = &clip_star;
1296
1297 if (!cbd->owned)
1298 return; /* Shouldn't ever happen */
1299
1300 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001301 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 && info != (guint)TARGET_UTF8_STRING
1303 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 && info != (guint)TARGET_VIM
1305 && info != (guint)TARGET_COMPOUND_TEXT
1306 && info != (guint)TARGET_TEXT)
1307 return;
1308
1309 /* get the selection from the '*'/'+' register */
1310 clip_get_selection(cbd);
1311
1312 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1313 if (motion_type < 0 || string == NULL)
1314 return;
1315 /* Due to int arguments we can't handle more than G_MAXINT. Also
1316 * reserve one extra byte for NUL or the motion type; just in case.
1317 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1318 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1319
1320 if (info == (guint)TARGET_VIM)
1321 {
1322 tmpbuf = alloc((unsigned)length + 1);
1323 if (tmpbuf != NULL)
1324 {
1325 tmpbuf[0] = motion_type;
1326 mch_memmove(tmpbuf + 1, string, (size_t)length);
1327 }
1328 /* For our own format, the first byte contains the motion type */
1329 ++length;
1330 vim_free(string);
1331 string = tmpbuf;
1332 type = vim_atom;
1333 }
1334
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001335 else if (info == (guint)TARGET_HTML)
1336 {
1337 vimconv_T conv;
1338
1339 /* Since we get utf-16, we probably should set it as well. */
1340 conv.vc_type = CONV_NONE;
1341 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1342 if (conv.vc_type != CONV_NONE)
1343 {
1344 tmpbuf = string_convert(&conv, string, &length);
1345 convert_setup(&conv, NULL, NULL);
1346 vim_free(string);
1347 string = tmpbuf;
1348 }
1349
1350 /* Prepend the BOM: "fffe" */
1351 if (string != NULL)
1352 {
1353 tmpbuf = alloc(length + 2);
1354 tmpbuf[0] = 0xff;
1355 tmpbuf[1] = 0xfe;
1356 mch_memmove(tmpbuf + 2, string, (size_t)length);
1357 vim_free(string);
1358 string = tmpbuf;
1359 length += 2;
1360
1361 selection_data->type = selection_data->target;
1362 selection_data->format = 16; /* 16 bits per char */
1363 gtk_selection_data_set(selection_data, html_atom, 16,
1364 string, length);
1365 vim_free(string);
1366 }
1367 return;
1368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 else if (info == (guint)TARGET_VIMENC)
1370 {
1371 int l = STRLEN(p_enc);
1372
1373 /* contents: motion_type 'encoding' NUL text */
1374 tmpbuf = alloc((unsigned)length + l + 2);
1375 if (tmpbuf != NULL)
1376 {
1377 tmpbuf[0] = motion_type;
1378 STRCPY(tmpbuf + 1, p_enc);
1379 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1380 }
1381 length += l + 2;
1382 vim_free(string);
1383 string = tmpbuf;
1384 type = vimenc_atom;
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /* gtk_selection_data_set_text() handles everything for us. This is
1388 * so easy and simple and cool, it'd be insane not to use it. */
1389 else
1390 {
1391 if (output_conv.vc_type != CONV_NONE)
1392 {
1393 tmpbuf = string_convert(&output_conv, string, &length);
1394 vim_free(string);
1395 if (tmpbuf == NULL)
1396 return;
1397 string = tmpbuf;
1398 }
1399 /* Validate the string to avoid runtime warnings */
1400 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1401 {
1402 gtk_selection_data_set_text(selection_data,
1403 (const char *)string, length);
1404 }
1405 vim_free(string);
1406 return;
1407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
1409 if (string != NULL)
1410 {
1411 selection_data->type = selection_data->target;
1412 selection_data->format = 8; /* 8 bits per char */
1413
1414 gtk_selection_data_set(selection_data, type, 8, string, length);
1415 vim_free(string);
1416 }
1417}
1418
1419/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001420 * Check if the GUI can be started. Called before gvimrc is sourced and
1421 * before fork().
1422 * Return OK or FAIL.
1423 */
1424 int
1425gui_mch_early_init_check(void)
1426{
1427 char_u *p;
1428
1429 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1430 p = mch_getenv((char_u *)"DISPLAY");
1431 if (p == NULL || *p == NUL)
1432 {
1433 gui.dying = TRUE;
1434 EMSG(_((char *)e_opendisp));
1435 return FAIL;
1436 }
1437 return OK;
1438}
1439
1440/*
1441 * Check if the GUI can be started. Called before gvimrc is sourced but after
1442 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 * 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
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001453 /* This defaults to argv[0], but we want it to match the name of the
1454 * shipped gvim.desktop so that Vim's windows can be associated with this
1455 * file. */
1456 g_set_prgname("gvim");
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1459 if (!gtk_init_check(&gui_argc, &gui_argv))
1460 {
1461 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001462 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return FAIL;
1464 }
1465
1466 return OK;
1467}
1468
1469
1470/****************************************************************************
1471 * Mouse handling callbacks
1472 */
1473
1474
1475static guint mouse_click_timer = 0;
1476static int mouse_timed_out = TRUE;
1477
1478/*
1479 * Timer used to recognize multiple clicks of the mouse button
1480 */
1481 static gint
1482mouse_click_timer_cb(gpointer data)
1483{
1484 /* we don't use this information currently */
1485 int *timed_out = (int *) data;
1486
1487 *timed_out = TRUE;
1488 return FALSE; /* don't happen again */
1489}
1490
1491static guint motion_repeat_timer = 0;
1492static int motion_repeat_offset = FALSE;
1493static gint motion_repeat_timer_cb(gpointer);
1494
1495 static void
1496process_motion_notify(int x, int y, GdkModifierType state)
1497{
1498 int button;
1499 int_u vim_modifiers;
1500
1501 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1502 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1503 GDK_BUTTON5_MASK))
1504 ? MOUSE_DRAG : ' ';
1505
1506 /* If our pointer is currently hidden, then we should show it. */
1507 gui_mch_mousehide(FALSE);
1508
1509 /* Just moving the rodent above the drawing area without any button
1510 * being pressed. */
1511 if (button != MOUSE_DRAG)
1512 {
1513 gui_mouse_moved(x, y);
1514 return;
1515 }
1516
1517 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001518 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar49325942007-05-10 19:19:59 +00001520 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1522
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 /*
1524 * Auto repeat timer handling.
1525 */
1526 if (x < 0 || y < 0
1527 || x >= gui.drawarea->allocation.width
1528 || y >= gui.drawarea->allocation.height)
1529 {
1530
1531 int dx;
1532 int dy;
1533 int offshoot;
1534 int delay = 10;
1535
1536 /* Calculate the maximal distance of the cursor from the drawing area.
1537 * (offshoot can't become negative here!).
1538 */
1539 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1540 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1541
1542 offshoot = dx > dy ? dx : dy;
1543
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001544 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 * distance of 127 pixels from the main window.
1546 *
1547 * One could think endlessly about the most ergonomic variant here.
1548 * For example it could make sense to calculate the distance from the
1549 * drags start instead...
1550 *
1551 * Maybe a parabolic interpolation would suite us better here too...
1552 */
1553 if (offshoot > 127)
1554 {
1555 /* 5 appears to be somehow near to my perceptual limits :-). */
1556 delay = 5;
1557 }
1558 else
1559 {
1560 delay = (130 * (127 - offshoot)) / 127 + 5;
1561 }
1562
1563 /* shoot again */
1564 if (!motion_repeat_timer)
1565 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1566 motion_repeat_timer_cb, NULL);
1567 }
1568}
1569
1570/*
1571 * Timer used to recognize multiple clicks of the mouse button.
1572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001574motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575{
1576 int x;
1577 int y;
1578 GdkModifierType state;
1579
1580 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1581
1582 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1583 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1584 GDK_BUTTON5_MASK)))
1585 {
1586 motion_repeat_timer = 0;
1587 return FALSE;
1588 }
1589
1590 /* If there already is a mouse click in the input buffer, wait another
1591 * time (otherwise we would create a backlog of clicks) */
1592 if (vim_used_in_input_buf() > 10)
1593 return TRUE;
1594
1595 motion_repeat_timer = 0;
1596
1597 /*
1598 * Fake a motion event.
1599 * Trick: Pretend the mouse moved to the next character on every other
1600 * event, otherwise drag events will be discarded, because they are still
1601 * in the same character.
1602 */
1603 if (motion_repeat_offset)
1604 x += gui.char_width;
1605
1606 motion_repeat_offset = !motion_repeat_offset;
1607 process_motion_notify(x, y, state);
1608
1609 /* Don't happen again. We will get reinstalled in the synthetic event
1610 * if needed -- thus repeating should still work. */
1611 return FALSE;
1612}
1613
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001615motion_notify_event(GtkWidget *widget,
1616 GdkEventMotion *event,
1617 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 if (event->is_hint)
1620 {
1621 int x;
1622 int y;
1623 GdkModifierType state;
1624
1625 gdk_window_get_pointer(widget->window, &x, &y, &state);
1626 process_motion_notify(x, y, state);
1627 }
1628 else
1629 {
1630 process_motion_notify((int)event->x, (int)event->y,
1631 (GdkModifierType)event->state);
1632 }
1633
1634 return TRUE; /* handled */
1635}
1636
1637
1638/*
1639 * Mouse button handling. Note please that we are capturing multiple click's
1640 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1641 * This is due to the way the generic VIM code is recognizing multiple clicks.
1642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001644button_press_event(GtkWidget *widget,
1645 GdkEventButton *event,
1646 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 int button;
1649 int repeated_click = FALSE;
1650 int x, y;
1651 int_u vim_modifiers;
1652
Bram Moolenaar20892c12011-06-26 04:49:00 +02001653 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001654
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 /* Make sure we have focus now we've been selected */
1656 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1657 gtk_widget_grab_focus(widget);
1658
1659 /*
1660 * Don't let additional events about multiple clicks send by GTK to us
1661 * after the initial button press event confuse us.
1662 */
1663 if (event->type != GDK_BUTTON_PRESS)
1664 return FALSE;
1665
1666 x = event->x;
1667 y = event->y;
1668
1669 /* Handle multiple clicks */
1670 if (!mouse_timed_out && mouse_click_timer)
1671 {
1672 gtk_timeout_remove(mouse_click_timer);
1673 mouse_click_timer = 0;
1674 repeated_click = TRUE;
1675 }
1676
1677 mouse_timed_out = FALSE;
1678 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1679 mouse_click_timer_cb, &mouse_timed_out);
1680
1681 switch (event->button)
1682 {
1683 case 1:
1684 button = MOUSE_LEFT;
1685 break;
1686 case 2:
1687 button = MOUSE_MIDDLE;
1688 break;
1689 case 3:
1690 button = MOUSE_RIGHT;
1691 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 default:
1693 return FALSE; /* Unknown button */
1694 }
1695
1696#ifdef FEAT_XIM
1697 /* cancel any preediting */
1698 if (im_is_preediting())
1699 xim_reset();
1700#endif
1701
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001702 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
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;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001731 case GDK_SCROLL_LEFT:
1732 button = MOUSE_7;
1733 break;
1734 case GDK_SCROLL_RIGHT:
1735 button = MOUSE_6;
1736 break;
1737 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 return FALSE;
1739 }
1740
1741# ifdef FEAT_XIM
1742 /* cancel any preediting */
1743 if (im_is_preediting())
1744 xim_reset();
1745# endif
1746
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001747 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
1749 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1750 FALSE, vim_modifiers);
1751
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 return TRUE;
1753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754
1755
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001757button_release_event(GtkWidget *widget UNUSED,
1758 GdkEventButton *event,
1759 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760{
1761 int x, y;
1762 int_u vim_modifiers;
1763
Bram Moolenaar20892c12011-06-26 04:49:00 +02001764 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 /* Remove any motion "machine gun" timers used for automatic further
1767 extension of allocation areas if outside of the applications window
1768 area .*/
1769 if (motion_repeat_timer)
1770 {
1771 gtk_timeout_remove(motion_repeat_timer);
1772 motion_repeat_timer = 0;
1773 }
1774
1775 x = event->x;
1776 y = event->y;
1777
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001778 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779
1780 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 return TRUE;
1783}
1784
1785
1786#ifdef FEAT_DND
1787/****************************************************************************
1788 * Drag aNd Drop support handlers.
1789 */
1790
1791/*
1792 * Count how many items there may be and separate them with a NUL.
1793 * Apparently the items are separated with \r\n. This is not documented,
1794 * thus be careful not to go past the end. Also allow separation with
1795 * NUL characters.
1796 */
1797 static int
1798count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1799{
1800 int i;
1801 char_u *p = out;
1802 int count = 0;
1803
1804 for (i = 0; i < len; ++i)
1805 {
1806 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1807 {
1808 if (p > out && p[-1] != NUL)
1809 {
1810 ++count;
1811 *p++ = NUL;
1812 }
1813 }
1814 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1815 {
1816 *p++ = hexhex2nr(raw + i + 1);
1817 i += 2;
1818 }
1819 else
1820 *p++ = raw[i];
1821 }
1822 if (p > out && p[-1] != NUL)
1823 {
1824 *p = NUL; /* last item didn't have \r or \n */
1825 ++count;
1826 }
1827 return count;
1828}
1829
1830/*
1831 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1832 * this process, URI which protocol is not "file:" are removed. Return
1833 * length of array (less than "max").
1834 */
1835 static int
1836filter_uri_list(char_u **outlist, int max, char_u *src)
1837{
1838 int i, j;
1839
1840 for (i = j = 0; i < max; ++i)
1841 {
1842 outlist[i] = NULL;
1843 if (STRNCMP(src, "file:", 5) == 0)
1844 {
1845 src += 5;
1846 if (STRNCMP(src, "//localhost", 11) == 0)
1847 src += 11;
1848 while (src[0] == '/' && src[1] == '/')
1849 ++src;
1850 outlist[j++] = vim_strsave(src);
1851 }
1852 src += STRLEN(src) + 1;
1853 }
1854 return j;
1855}
1856
1857 static char_u **
1858parse_uri_list(int *count, char_u *data, int len)
1859{
1860 int n = 0;
1861 char_u *tmp = NULL;
1862 char_u **array = NULL;;
1863
1864 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1865 {
1866 n = count_and_decode_uri_list(tmp, data, len);
1867 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1868 n = filter_uri_list(array, n, tmp);
1869 }
1870 vim_free(tmp);
1871 *count = n;
1872 return array;
1873}
1874
1875 static void
1876drag_handle_uri_list(GdkDragContext *context,
1877 GtkSelectionData *data,
1878 guint time_,
1879 GdkModifierType state,
1880 gint x,
1881 gint y)
1882{
1883 char_u **fnames;
1884 int nfiles = 0;
1885
1886 fnames = parse_uri_list(&nfiles, data->data, data->length);
1887
1888 if (fnames != NULL && nfiles > 0)
1889 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001890 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
1892 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1893
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001894 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1897 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001898 else
1899 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900}
1901
1902 static void
1903drag_handle_text(GdkDragContext *context,
1904 GtkSelectionData *data,
1905 guint time_,
1906 GdkModifierType state)
1907{
1908 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1909 char_u *text;
1910 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 text = data->data;
1914 len = data->length;
1915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (data->type == utf8_string_atom)
1917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (input_conv.vc_type != CONV_NONE)
1919 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (tmpbuf != NULL)
1921 text = tmpbuf;
1922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 dnd_yank_drag_data(text, (long)len);
1925 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001928 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
1930 if (dropkey[2] != 0)
1931 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1932 else
1933 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934}
1935
1936/*
1937 * DND receiver.
1938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 static void
1940drag_data_received_cb(GtkWidget *widget,
1941 GdkDragContext *context,
1942 gint x,
1943 gint y,
1944 GtkSelectionData *data,
1945 guint info,
1946 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001947 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948{
1949 GdkModifierType state;
1950
1951 /* Guard against trash */
1952 if (data->data == NULL
1953 || data->length <= 0
1954 || data->format != 8
1955 || data->data[data->length] != '\0')
1956 {
1957 gtk_drag_finish(context, FALSE, FALSE, time_);
1958 return;
1959 }
1960
1961 /* Get the current modifier state for proper distinguishment between
1962 * different operations later. */
1963 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1964
1965 /* Not sure about the role of "text/plain" here... */
1966 if (info == (guint)TARGET_TEXT_URI_LIST)
1967 drag_handle_uri_list(context, data, time_, state, x, y);
1968 else
1969 drag_handle_text(context, data, time_, state);
1970
1971}
1972#endif /* FEAT_DND */
1973
1974
1975#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1976/*
1977 * GnomeClient interact callback. Check for unsaved buffers that cannot
1978 * be abandoned and pop up a dialog asking the user for confirmation if
1979 * necessary.
1980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001982sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001984 GnomeDialogType type UNUSED,
1985 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986{
1987 cmdmod_T save_cmdmod;
1988 gboolean shutdown_cancelled;
1989
1990 save_cmdmod = cmdmod;
1991
1992# ifdef FEAT_BROWSE
1993 cmdmod.browse = TRUE;
1994# endif
1995# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1996 cmdmod.confirm = TRUE;
1997# endif
1998 /*
1999 * If there are changed buffers, present the user with
2000 * a dialog if possible, otherwise give an error message.
2001 */
2002 shutdown_cancelled = check_changed_any(FALSE);
2003
2004 exiting = FALSE;
2005 cmdmod = save_cmdmod;
2006 setcursor(); /* position the cursor */
2007 out_flush();
2008 /*
2009 * If the user hit the [Cancel] button the whole shutdown
2010 * will be cancelled. Wow, quite powerful feature (:
2011 */
2012 gnome_interaction_key_return(key, shutdown_cancelled);
2013}
2014
2015/*
2016 * Generate a script that can be used to restore the current editing session.
2017 * Save the value of v:this_session before running :mksession in order to make
2018 * automagic session save fully transparent. Return TRUE on success.
2019 */
2020 static int
2021write_session_file(char_u *filename)
2022{
2023 char_u *escaped_filename;
2024 char *mksession_cmdline;
2025 unsigned int save_ssop_flags;
2026 int failed;
2027
2028 /*
2029 * Build an ex command line to create a script that restores the current
2030 * session if executed. Escape the filename to avoid nasty surprises.
2031 */
2032 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2033 if (escaped_filename == NULL)
2034 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002035 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2036 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 /*
2040 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2041 * unpredictable effects when the session is saved automatically. Also,
2042 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2043 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2044 * enormously large if the GNOME session feature is used regularly.
2045 */
2046 save_ssop_flags = ssop_flags;
2047 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002048 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2051 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2052 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002053 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 ssop_flags = save_ssop_flags;
2056 g_free(mksession_cmdline);
2057 /*
2058 * Reopen the file and append a command to restore v:this_session,
2059 * as if this save never happened. This is to avoid conflicts with
2060 * the user's own sessions. FIXME: It's probably less hackish to add
2061 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2062 */
2063 if (!failed)
2064 {
2065 FILE *fd;
2066
2067 fd = open_exfile(filename, TRUE, APPENDBIN);
2068
2069 failed = (fd == NULL
2070 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2071 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2072
2073 if (fd != NULL && fclose(fd) != 0)
2074 failed = TRUE;
2075
2076 if (failed)
2077 mch_remove(filename);
2078 }
2079
2080 return !failed;
2081}
2082
2083/*
2084 * "save_yourself" signal handler. Initiate an interaction to ask the user
2085 * for confirmation if necessary. Save the current editing session and tell
2086 * the session manager how to restart Vim.
2087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 static gboolean
2089sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002090 gint phase UNUSED,
2091 GnomeSaveStyle save_style UNUSED,
2092 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002094 gboolean fast UNUSED,
2095 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
2097 static const char suffix[] = "-session.vim";
2098 char *session_file;
2099 unsigned int len;
2100 gboolean success;
2101
2102 /* Always request an interaction if possible. check_changed_any()
2103 * won't actually show a dialog unless any buffers have been modified.
2104 * There doesn't seem to be an obvious way to check that without
2105 * automatically firing the dialog. Anyway, it works just fine. */
2106 if (interact_style == GNOME_INTERACT_ANY)
2107 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2108 &sm_client_check_changed_any,
2109 NULL);
2110 out_flush();
2111 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2112
2113 /* The path is unique for each session save. We do neither know nor care
2114 * which session script will actually be used later. This decision is in
2115 * the domain of the session manager. */
2116 session_file = gnome_config_get_real_path(
2117 gnome_client_get_config_prefix(client));
2118 len = strlen(session_file);
2119
2120 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2121 --len; /* get rid of the superfluous trailing '/' */
2122
2123 session_file = g_renew(char, session_file, len + sizeof(suffix));
2124 memcpy(session_file + len, suffix, sizeof(suffix));
2125
2126 success = write_session_file((char_u *)session_file);
2127
2128 if (success)
2129 {
2130 const char *argv[8];
2131 int i;
2132
2133 /* Tell the session manager how to wipe out the stored session data.
2134 * This isn't as dangerous as it looks, don't worry :) session_file
2135 * is a unique absolute filename. Usually it'll be something like
2136 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2137 i = 0;
2138 argv[i++] = "rm";
2139 argv[i++] = session_file;
2140 argv[i] = NULL;
2141
2142 gnome_client_set_discard_command(client, i, (char **)argv);
2143
2144 /* Tell the session manager how to restore the just saved session.
2145 * This is easily done thanks to Vim's -S option. Pass the -f flag
2146 * since there's no need to fork -- it might even cause confusion.
2147 * Also pass the window role to give the WM something to match on.
2148 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2149 i = 0;
2150 argv[i++] = restart_command;
2151 argv[i++] = "-f";
2152 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 argv[i++] = "--role";
2154 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 argv[i++] = "-S";
2156 argv[i++] = session_file;
2157 argv[i] = NULL;
2158
2159 gnome_client_set_restart_command(client, i, (char **)argv);
2160 gnome_client_set_clone_command(client, 0, NULL);
2161 }
2162
2163 g_free(session_file);
2164
2165 return success;
2166}
2167
2168/*
2169 * Called when the session manager wants us to die. There isn't much to save
2170 * here since "save_yourself" has been emitted before (unless serious trouble
2171 * is happening).
2172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002174sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175{
2176 /* Don't write messages to the GUI anymore */
2177 full_screen = FALSE;
2178
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002179 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002180 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002181 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 preserve_exit();
2183}
2184
2185/*
2186 * Connect our signal handlers to be notified on session save and shutdown.
2187 */
2188 static void
2189setup_save_yourself(void)
2190{
2191 GnomeClient *client;
2192
2193 client = gnome_master_client();
2194
2195 if (client != NULL)
2196 {
2197 /* Must use the deprecated gtk_signal_connect() for compatibility
2198 * with GNOME 1. Arrgh, zombies! */
2199 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2200 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2201 gtk_signal_connect(GTK_OBJECT(client), "die",
2202 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2203 }
2204}
2205
2206#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2207
2208# ifdef USE_XSMP
2209/*
2210 * GTK tells us that XSMP needs attention
2211 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 static gboolean
2213local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002214 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 GIOCondition condition;
2216 gpointer data;
2217{
2218 if (condition == G_IO_IN)
2219 {
2220 /* Do stuff; maybe close connection */
2221 if (xsmp_handle_requests() == FAIL)
2222 g_io_channel_unref((GIOChannel *)data);
2223 return TRUE;
2224 }
2225 /* Error */
2226 g_io_channel_unref((GIOChannel *)data);
2227 xsmp_close();
2228 return TRUE;
2229}
2230# endif /* USE_XSMP */
2231
2232/*
2233 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2234 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2235 */
2236 static void
2237setup_save_yourself(void)
2238{
2239 Atom *existing_atoms = NULL;
2240 int count = 0;
2241
2242#ifdef USE_XSMP
2243 if (xsmp_icefd != -1)
2244 {
2245 /*
2246 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2247 * set up GTK IO monitor
2248 */
2249 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2250
2251 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2252 local_xsmp_handle_requests, (gpointer)g_io);
2253 }
2254 else
2255#endif
2256 {
2257 /* Fall back to old method */
2258
2259 /* first get the existing value */
2260 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2261 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2262 &existing_atoms, &count))
2263 {
2264 Atom *new_atoms;
2265 Atom save_yourself_xatom;
2266 int i;
2267
2268 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2269
2270 /* check if WM_SAVE_YOURSELF isn't there yet */
2271 for (i = 0; i < count; ++i)
2272 if (existing_atoms[i] == save_yourself_xatom)
2273 break;
2274
2275 if (i == count)
2276 {
2277 /* allocate an Atoms array which is one item longer */
2278 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2279 * sizeof(Atom)));
2280 if (new_atoms != NULL)
2281 {
2282 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2283 new_atoms[count] = save_yourself_xatom;
2284 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2285 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2286 new_atoms, count + 1);
2287 vim_free(new_atoms);
2288 }
2289 }
2290 XFree(existing_atoms);
2291 }
2292 }
2293}
2294
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295/*
2296 * Installing a global event filter seems to be the only way to catch
2297 * client messages of type WM_PROTOCOLS without overriding GDK's own
2298 * client message event filter. Well, that's still better than trying
2299 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 *
2301 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2302 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2303 * I have the nasty feeling modern session managers just don't send this
2304 * deprecated message anymore. Addition: confirmed by several people.
2305 *
2306 * The GNOME session support is much cooler anyway. Unlike this ugly
2307 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2308 * it should work with KDE as well.
2309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002311global_event_filter(GdkXEvent *xev,
2312 GdkEvent *event UNUSED,
2313 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314{
2315 XEvent *xevent = (XEvent *)xev;
2316
2317 if (xevent != NULL
2318 && xevent->type == ClientMessage
2319 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002320 && (long_u)xevent->xclient.data.l[0]
2321 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
2323 out_flush();
2324 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2325 /*
2326 * Set the window's WM_COMMAND property, to let the window manager
2327 * know we are done saving ourselves. We don't want to be
2328 * restarted, thus set argv to NULL.
2329 */
2330 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2331 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2332 NULL, 0);
2333 return GDK_FILTER_REMOVE;
2334 }
2335
2336 return GDK_FILTER_CONTINUE;
2337}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2339
2340
2341/*
2342 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2343 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002345mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346{
2347/* If you get an error message here, you still need to unpack the runtime
2348 * archive! */
2349#ifdef magick
2350# undef magick
2351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* A bit hackish, but avoids casting later and allows optimization */
2353# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354#define magick vim32x32
2355#include "../runtime/vim32x32.xpm"
2356#undef magick
2357#define magick vim16x16
2358#include "../runtime/vim16x16.xpm"
2359#undef magick
2360#define magick vim48x48
2361#include "../runtime/vim48x48.xpm"
2362#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 /* When started with "--echo-wid" argument, write window ID on stdout. */
2366 if (echo_wid_arg)
2367 {
2368 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2369 fflush(stdout);
2370 }
2371
2372 if (vim_strchr(p_go, GO_ICON) != NULL)
2373 {
2374 /*
2375 * Add an icon to the main window. For fun and convenience of the user.
2376 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 GList *icons = NULL;
2378
2379 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2380 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2381 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2382
2383 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2384
2385 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2386 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 }
2388
2389#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2390 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#endif
2393 /* Setup to indicate to the window manager that we want to catch the
2394 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2395 * manager instead. */
2396#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2397 if (using_gnome)
2398#endif
2399 setup_save_yourself();
2400
2401#ifdef FEAT_CLIENTSERVER
2402 if (serverName == NULL && serverDelayedStartName != NULL)
2403 {
2404 /* This is a :gui command in a plain vim with no previous server */
2405 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2406
2407 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2408 serverDelayedStartName);
2409 }
2410 else
2411 {
2412 /*
2413 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2414 * have to change the "server" registration to that of the main window
2415 * If we have not registered a name yet, remember the window
2416 */
2417 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2418 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2419 }
2420 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2421 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2422 GTK_SIGNAL_FUNC(property_event), NULL);
2423#endif
2424}
2425
2426 static GdkCursor *
2427create_blank_pointer(void)
2428{
2429 GdkWindow *root_window = NULL;
2430 GdkPixmap *blank_mask;
2431 GdkCursor *cursor;
2432 GdkColor color = { 0, 0, 0, 0 };
2433 char blank_data[] = { 0x0 };
2434
2435#ifdef HAVE_GTK_MULTIHEAD
2436 root_window = gtk_widget_get_root_window(gui.mainwin);
2437#endif
2438
2439 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2440 * in size. */
2441 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2442 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2443 &color, &color, 0, 0);
2444 gdk_bitmap_unref(blank_mask);
2445
2446 return cursor;
2447}
2448
2449#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 static void
2451mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002452 GdkScreen *previous_screen UNUSED,
2453 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
2455 if (!gtk_widget_has_screen(widget))
2456 return;
2457
2458 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002459 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 */
2461 if (gui.blank_pointer != NULL)
2462 gdk_cursor_unref(gui.blank_pointer);
2463
2464 gui.blank_pointer = create_blank_pointer();
2465
2466 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2467 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2468
2469 /*
2470 * Create a new PangoContext for this screen, and initialize it
2471 * with the current font if necessary.
2472 */
2473 if (gui.text_context != NULL)
2474 g_object_unref(gui.text_context);
2475
2476 gui.text_context = gtk_widget_create_pango_context(widget);
2477 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2478
2479 if (gui.norm_font != NULL)
2480 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002481 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002482 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484}
2485#endif /* HAVE_GTK_MULTIHEAD */
2486
2487/*
2488 * After the drawing area comes up, we calculate all colors and create the
2489 * dummy blank cursor.
2490 *
2491 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2492 * fact that the main VIM engine doesn't take them into account anywhere.
2493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002495drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 GtkWidget *sbar;
2498
2499#ifdef FEAT_XIM
2500 xim_init();
2501#endif
2502 gui_mch_new_colors();
2503 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2504
2505 gui.blank_pointer = create_blank_pointer();
2506 if (gui.pointer_hidden)
2507 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2508
2509 /* get the actual size of the scrollbars, if they are realized */
2510 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2511 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2512 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2513 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2514 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2515 gui.scrollbar_width = sbar->allocation.width;
2516
2517 sbar = gui.bottom_sbar.id;
2518 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2519 gui.scrollbar_height = sbar->allocation.height;
2520}
2521
2522/*
2523 * Properly clean up on shutdown.
2524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002526drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 /* Don't write messages to the GUI anymore */
2529 full_screen = FALSE;
2530
2531#ifdef FEAT_XIM
2532 im_shutdown();
2533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 if (gui.ascii_glyphs != NULL)
2535 {
2536 pango_glyph_string_free(gui.ascii_glyphs);
2537 gui.ascii_glyphs = NULL;
2538 }
2539 if (gui.ascii_font != NULL)
2540 {
2541 g_object_unref(gui.ascii_font);
2542 gui.ascii_font = NULL;
2543 }
2544 g_object_unref(gui.text_context);
2545 gui.text_context = NULL;
2546
2547 g_object_unref(gui.text_gc);
2548 gui.text_gc = NULL;
2549
2550 gdk_cursor_unref(gui.blank_pointer);
2551 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552}
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002555drawarea_style_set_cb(GtkWidget *widget UNUSED,
2556 GtkStyle *previous_style UNUSED,
2557 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558{
2559 gui_mch_new_colors();
2560}
2561
2562/*
2563 * Callback routine for the "delete_event" signal on the toplevel window.
2564 * Tries to vim gracefully, or refuses to exit with changed buffers.
2565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002567delete_event_cb(GtkWidget *widget UNUSED,
2568 GdkEventAny *event UNUSED,
2569 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 gui_shell_closed();
2572 return TRUE;
2573}
2574
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002575#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2576 static int
2577get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2578{
2579 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2580
2581#ifdef FEAT_GUI_GNOME
2582 if (using_gnome && widget != NULL)
2583 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002584 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002585 BonoboDockItem *dockitem;
2586
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002587 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002588 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2589 {
2590 /* Only menu & toolbar are dock items. Could tabline be?
2591 * Seem to be only the 2 defined in GNOME */
2592 widget = parent;
2593 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002594
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002595 if (dockitem == NULL || dockitem->is_floating)
2596 return 0;
2597 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2598 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002599 }
2600#endif
2601 if (widget != NULL
2602 && item_orientation == orientation
2603 && GTK_WIDGET_REALIZED(widget)
2604 && GTK_WIDGET_VISIBLE(widget))
2605 {
2606 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2607 return widget->allocation.height;
2608 else
2609 return widget->allocation.width;
2610 }
2611 return 0;
2612}
2613#endif
2614
2615 static int
2616get_menu_tool_width(void)
2617{
2618 int width = 0;
2619
2620#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2621# ifdef FEAT_MENU
2622 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2623# endif
2624# ifdef FEAT_TOOLBAR
2625 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2626# endif
2627# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002628 if (gui.tabline != NULL)
2629 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002630# endif
2631#endif
2632
2633 return width;
2634}
2635
2636 static int
2637get_menu_tool_height(void)
2638{
2639 int height = 0;
2640
2641#ifdef FEAT_MENU
2642 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2643#endif
2644#ifdef FEAT_TOOLBAR
2645 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2646#endif
2647#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002648 if (gui.tabline != NULL)
2649 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002650#endif
2651
2652 return height;
2653}
2654
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002655/* This controls whether we can set the real window hints at
2656 * start-up when in a GtkPlug.
2657 * 0 = normal processing (default)
2658 * 1 = init. hints set, no-one's tried to reset since last check
2659 * 2 = init. hints set, attempt made to change hints
2660 */
2661static int init_window_hints_state = 0;
2662
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002663 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002664update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002665{
2666 static int old_width = 0;
2667 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002668 static int old_min_width = 0;
2669 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002670 static int old_char_width = 0;
2671 static int old_char_height = 0;
2672
2673 int width;
2674 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002675 int min_width;
2676 int min_height;
2677
2678 /* At start-up, don't try to set the hints until the initial
2679 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002680 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002681 */
2682 if (!(force_width && force_height) && init_window_hints_state > 0)
2683 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002684 /* Don't do it! */
2685 init_window_hints_state = 2;
2686 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002687 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002688
2689 /* This also needs to be done when the main window isn't there yet,
2690 * otherwise the hints don't work. */
2691 width = gui_get_base_width();
2692 height = gui_get_base_height();
2693# ifdef FEAT_MENU
2694 height += tabline_height() * gui.char_height;
2695# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002696 width += get_menu_tool_width();
2697 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002698
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002699 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002700 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002701 * 'set columns=' or init. -geom) we briefly set the min. to the size
2702 * we wish to be instead of the legitimate minimum so that we actually
2703 * resize correctly.
2704 */
2705 if (force_width && force_height)
2706 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002707 min_width = force_width;
2708 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002709 }
2710 else
2711 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002712 min_width = width + MIN_COLUMNS * gui.char_width;
2713 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002714 }
2715
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002716 /* Avoid an expose event when the size didn't change. */
2717 if (width != old_width
2718 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002719 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002720 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002721 || gui.char_width != old_char_width
2722 || gui.char_height != old_char_height)
2723 {
2724 GdkGeometry geometry;
2725 GdkWindowHints geometry_mask;
2726
2727 geometry.width_inc = gui.char_width;
2728 geometry.height_inc = gui.char_height;
2729 geometry.base_width = width;
2730 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002731 geometry.min_width = min_width;
2732 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002733 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2734 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002735 /* Using gui.formwin as geometry widget doesn't work as expected
2736 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2737 * in Vim confuse GTK+. */
2738 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2739 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002740 old_width = width;
2741 old_height = height;
2742 old_min_width = min_width;
2743 old_min_height = min_height;
2744 old_char_width = gui.char_width;
2745 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002746 }
2747}
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#ifdef FEAT_TOOLBAR
2750
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751/*
2752 * This extra effort wouldn't be necessary if we only used stock icons in the
2753 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2754 * shouldn't be treated differently, thus we do need this.
2755 */
2756 static void
2757icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2758{
2759 if (GTK_IS_IMAGE(widget))
2760 {
2761 GtkImage *image = (GtkImage *)widget;
2762
2763 /* User-defined icons are stored in a GtkIconSet */
2764 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2765 {
2766 GtkIconSet *icon_set;
2767 GtkIconSize icon_size;
2768
2769 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2770 icon_size = (GtkIconSize)(long)user_data;
2771
2772 gtk_icon_set_ref(icon_set);
2773 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2774 gtk_icon_set_unref(icon_set);
2775 }
2776 }
2777 else if (GTK_IS_CONTAINER(widget))
2778 {
2779 gtk_container_foreach((GtkContainer *)widget,
2780 &icon_size_changed_foreach,
2781 user_data);
2782 }
2783}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784
2785 static void
2786set_toolbar_style(GtkToolbar *toolbar)
2787{
2788 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 GtkIconSize size;
2790 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2793 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2794 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002795 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2797 style = GTK_TOOLBAR_BOTH;
2798 else if (toolbar_flags & TOOLBAR_TEXT)
2799 style = GTK_TOOLBAR_TEXT;
2800 else
2801 style = GTK_TOOLBAR_ICONS;
2802
2803 gtk_toolbar_set_style(toolbar, style);
2804 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2805
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 switch (tbis_flags)
2807 {
2808 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2809 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2810 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2811 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2812 default: size = GTK_ICON_SIZE_INVALID; break;
2813 }
2814 oldsize = gtk_toolbar_get_icon_size(toolbar);
2815
2816 if (size == GTK_ICON_SIZE_INVALID)
2817 {
2818 /* Let global user preferences decide the icon size. */
2819 gtk_toolbar_unset_icon_size(toolbar);
2820 size = gtk_toolbar_get_icon_size(toolbar);
2821 }
2822 if (size != oldsize)
2823 {
2824 gtk_container_foreach(GTK_CONTAINER(toolbar),
2825 &icon_size_changed_foreach,
2826 GINT_TO_POINTER((int)size));
2827 }
2828 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829}
2830
2831#endif /* FEAT_TOOLBAR */
2832
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002833#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2834static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002835static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002836static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002837static int clicked_page; /* page clicked in tab line */
2838
2839/*
2840 * Handle selecting an item in the tab line popup menu.
2841 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002842 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002843tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002844{
Bram Moolenaara5621492006-02-25 21:55:24 +00002845 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002846 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002847}
2848
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002849 static void
2850add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2851{
2852 GtkWidget *item;
2853 char_u *utf_text;
2854
2855 utf_text = CONVERT_TO_UTF8(text);
2856 item = gtk_menu_item_new_with_label((const char *)utf_text);
2857 gtk_widget_show(item);
2858 CONVERT_TO_UTF8_FREE(utf_text);
2859
2860 gtk_container_add(GTK_CONTAINER(menu), item);
2861 gtk_signal_connect(GTK_OBJECT(item), "activate",
2862 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002863 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002864}
2865
Bram Moolenaara5621492006-02-25 21:55:24 +00002866/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002867 * Create a menu for the tab line.
2868 */
2869 static GtkWidget *
2870create_tabline_menu(void)
2871{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002872 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002873
2874 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002875 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2876 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2877 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002878
2879 return menu;
2880}
2881
2882 static gboolean
2883on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2884{
2885 /* Was this button press event ? */
2886 if (event->type == GDK_BUTTON_PRESS)
2887 {
2888 GdkEventButton *bevent = (GdkEventButton *)event;
2889 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002890 int y = bevent->y;
2891 GtkWidget *tabwidget;
2892 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002893
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002894 /* When ignoring events return TRUE so that the selected page doesn't
2895 * change. */
2896 if (hold_gui_events
2897# ifdef FEAT_CMDWIN
2898 || cmdwin_type != 0
2899# endif
2900 )
2901 return TRUE;
2902
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002903 tabwin = gdk_window_at_pointer(&x, &y);
2904 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2905 clicked_page = (int)(long)gtk_object_get_user_data(
2906 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002907
2908 /* If the event was generated for 3rd button popup the menu. */
2909 if (bevent->button == 3)
2910 {
2911 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2912 bevent->button, bevent->time);
2913 /* We handled the event. */
2914 return TRUE;
2915 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002916 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002917 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002918 if (clicked_page == 0)
2919 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002920 /* Click after all tabs moves to next tab page. When "x" is
2921 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002922 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002923 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002924 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002925 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002926
Bram Moolenaara5621492006-02-25 21:55:24 +00002927 /* We didn't handle the event. */
2928 return FALSE;
2929}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002930
2931/*
2932 * Handle selecting one of the tabs.
2933 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002934 static void
2935on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002936 GtkNotebook *notebook UNUSED,
2937 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002938 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002939 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002940{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002941 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002942 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002943 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002944 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002945}
2946
2947/*
2948 * Show or hide the tabline.
2949 */
2950 void
2951gui_mch_show_tabline(int showit)
2952{
2953 if (gui.tabline == NULL)
2954 return;
2955
2956 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2957 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002958 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002959 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002960 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002961 if (showit)
2962 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002963 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002964
2965 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002966}
2967
2968/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002969 * Return TRUE when tabline is displayed.
2970 */
2971 int
2972gui_mch_showing_tabline(void)
2973{
2974 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002975 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002976}
2977
2978/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002979 * Update the labels of the tabline.
2980 */
2981 void
2982gui_mch_update_tabline(void)
2983{
2984 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002985 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002986 GtkWidget *label;
2987 tabpage_T *tp;
2988 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002989 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002990 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002991 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002992
2993 if (gui.tabline == NULL)
2994 return;
2995
2996 ignore_tabline_evt = TRUE;
2997
2998 /* Add a label for each tab page. They all contain the same text area. */
2999 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
3000 {
3001 if (tp == curtab)
3002 curtabidx = nr;
3003
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003004 tab_num = nr + 1;
3005
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003006 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3007 if (page == NULL)
3008 {
3009 /* Add notebook page */
3010 page = gtk_vbox_new(FALSE, 0);
3011 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003012 event_box = gtk_event_box_new();
3013 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003014 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003015 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003016 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003017 gtk_widget_show(label);
3018 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3019 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003020 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003021 nr++);
3022 }
3023
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003024 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003025 gtk_object_set_user_data(GTK_OBJECT(event_box),
3026 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003027 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003028 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003029 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003030 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003031 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003032
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003033 get_tabline_label(tp, TRUE);
3034 labeltext = CONVERT_TO_UTF8(NameBuff);
3035 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3036 (const char *)labeltext, NULL);
3037 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003038 }
3039
3040 /* Remove any old labels. */
3041 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3042 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3043
3044 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003045 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003046
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003047 /* Make sure everything is in place before drawing text. */
3048 gui_mch_update();
3049
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003050 ignore_tabline_evt = FALSE;
3051}
3052
3053/*
3054 * Set the current tab to "nr". First tab is 1.
3055 */
3056 void
3057gui_mch_set_curtab(nr)
3058 int nr;
3059{
3060 if (gui.tabline == NULL)
3061 return;
3062
3063 ignore_tabline_evt = TRUE;
3064 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003065 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003066 ignore_tabline_evt = FALSE;
3067}
3068
3069#endif /* FEAT_GUI_TABLINE */
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003072 * Add selection targets for PRIMARY and CLIPBOARD selections.
3073 */
3074 void
3075gui_gtk_set_selection_targets(void)
3076{
3077 int i, j = 0;
3078 int n_targets = N_SELECTION_TARGETS;
3079 GtkTargetEntry targets[N_SELECTION_TARGETS];
3080
3081 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3082 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003083 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003084 * return something, instead of trying another target. Therefore only
3085 * offer TARGET_HTML when it works. */
3086 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3087 n_targets--;
3088 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003089 targets[j++] = selection_targets[i];
3090 }
3091
3092 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3093 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3094 gtk_selection_add_targets(gui.drawarea,
3095 (GdkAtom)GDK_SELECTION_PRIMARY,
3096 targets, n_targets);
3097 gtk_selection_add_targets(gui.drawarea,
3098 (GdkAtom)clip_plus.gtk_sel_atom,
3099 targets, n_targets);
3100}
3101
3102/*
3103 * Set up for receiving DND items.
3104 */
3105 void
3106gui_gtk_set_dnd_targets(void)
3107{
3108 int i, j = 0;
3109 int n_targets = N_DND_TARGETS;
3110 GtkTargetEntry targets[N_DND_TARGETS];
3111
3112 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3113 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003114 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003115 n_targets--;
3116 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003117 targets[j++] = dnd_targets[i];
3118 }
3119
3120 gtk_drag_dest_unset(gui.drawarea);
3121 gtk_drag_dest_set(gui.drawarea,
3122 GTK_DEST_DEFAULT_ALL,
3123 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003124 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003125}
3126
3127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3129 * Returns OK for success, FAIL when the GUI can't be started.
3130 */
3131 int
3132gui_mch_init(void)
3133{
3134 GtkWidget *vbox;
3135
3136#ifdef FEAT_GUI_GNOME
3137 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3138 * exits on failure, but that's a non-issue because we already called
3139 * gtk_init_check() in gui_mch_init_check(). */
3140 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3143 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003144# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar1ff32c52014-04-29 15:11:43 +02003145 {
3146 char *p = setlocale(LC_NUMERIC, NULL);
3147
3148 /* Make sure strtod() uses a decimal point, not a comma. Gnome
3149 * init may change it. */
3150 if (p == NULL || strcmp(p, "C") != 0)
3151 setlocale(LC_NUMERIC, "C");
3152 }
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003153# endif
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#endif
3156 vim_free(gui_argv);
3157 gui_argv = NULL;
3158
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003159#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 /* Set the human-readable application name */
3161 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 /*
3164 * Force UTF-8 output no matter what the value of 'encoding' is.
3165 * did_set_string_option() in option.c prohibits changing 'termencoding'
3166 * to something else than UTF-8 if the GUI is in use.
3167 */
3168 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3169
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003170#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003172#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3174 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003175#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#endif
3178
3179 /* Initialize values */
3180 gui.border_width = 2;
3181 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3182 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003183 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003185 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003187 /* LINTED: avoid warning: conversion to 'unsigned long' */
3188 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003191 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 /* Set default foreground and background colors. */
3195 gui.norm_pixel = gui.def_norm_pixel;
3196 gui.back_pixel = gui.def_back_pixel;
3197
3198 if (gtk_socket_id != 0)
3199 {
3200 GtkWidget *plug;
3201
3202 /* Use GtkSocket from another app. */
3203#ifdef HAVE_GTK_MULTIHEAD
3204 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3205 gtk_socket_id);
3206#else
3207 plug = gtk_plug_new(gtk_socket_id);
3208#endif
3209 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3210 {
3211 gui.mainwin = plug;
3212 }
3213 else
3214 {
3215 g_warning("Connection to GTK+ socket (ID %u) failed",
3216 (unsigned int)gtk_socket_id);
3217 /* Pretend we never wanted it if it failed (get own window) */
3218 gtk_socket_id = 0;
3219 }
3220 }
3221
3222 if (gtk_socket_id == 0)
3223 {
3224#ifdef FEAT_GUI_GNOME
3225 if (using_gnome)
3226 {
3227 gui.mainwin = gnome_app_new("Vim", NULL);
3228# ifdef USE_XSMP
3229 /* Use the GNOME save-yourself functionality now. */
3230 xsmp_close();
3231# endif
3232 }
3233 else
3234#endif
3235 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3236 }
3237
3238 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3239
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 /* Create the PangoContext used for drawing all text. */
3241 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3242 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3245 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3246
3247 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3248 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3249
3250 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3251 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3252#ifdef HAVE_GTK_MULTIHEAD
3253 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3254 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 gui.accel_group = gtk_accel_group_new();
3257 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003259 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 vbox = gtk_vbox_new(FALSE, 0);
3261
3262#ifdef FEAT_GUI_GNOME
3263 if (using_gnome)
3264 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003265# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 /* automagically restore menubar/toolbar placement */
3267 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3268# endif
3269 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3270 }
3271 else
3272#endif
3273 {
3274 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3275 gtk_widget_show(vbox);
3276 }
3277
3278#ifdef FEAT_MENU
3279 /*
3280 * Create the menubar and handle
3281 */
3282 gui.menubar = gtk_menu_bar_new();
3283 gtk_widget_set_name(gui.menubar, "vim-menubar");
3284
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003285 /* Avoid that GTK takes <F10> away from us. */
3286 {
3287 GtkSettings *gtk_settings;
3288
3289 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3290 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3291 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003292
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294# ifdef FEAT_GUI_GNOME
3295 if (using_gnome)
3296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 BonoboDockItem *dockitem;
3298
3299 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3300 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3301 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003302 /* We don't want the menu to float. */
3303 bonobo_dock_item_set_behavior(dockitem,
3304 bonobo_dock_item_get_behavior(dockitem)
3305 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 }
3308 else
3309# endif /* FEAT_GUI_GNOME */
3310 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003311 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3312 * disabled in gui_init() later. */
3313 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3315 }
3316#endif /* FEAT_MENU */
3317
3318#ifdef FEAT_TOOLBAR
3319 /*
3320 * Create the toolbar and handle
3321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 /* some aesthetics on the toolbar */
3323 gtk_rc_parse_string(
3324 "style \"vim-toolbar-style\" {\n"
3325 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3326 "}\n"
3327 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3328 gui.toolbar = gtk_toolbar_new();
3329 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3331
3332# ifdef FEAT_GUI_GNOME
3333 if (using_gnome)
3334 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 BonoboDockItem *dockitem;
3336
3337 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3338 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3339 GNOME_APP_TOOLBAR_NAME);
3340 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003341 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003342 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003343 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003344 bonobo_dock_item_get_behavior(dockitem)
3345 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348 else
3349# endif /* FEAT_GUI_GNOME */
3350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3352 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3353 gtk_widget_show(gui.toolbar);
3354 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3355 }
3356#endif /* FEAT_TOOLBAR */
3357
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003358#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003359 /*
3360 * Use a Notebook for the tab pages labels. The labels are hidden by
3361 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003362 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003363 gui.tabline = gtk_notebook_new();
3364 gtk_widget_show(gui.tabline);
3365 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3366 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3367 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003368 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003369 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003370
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003371 tabline_tooltip = gtk_tooltips_new();
3372 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3373
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003374 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003375 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003376
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003377 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003378 page = gtk_vbox_new(FALSE, 0);
3379 gtk_widget_show(page);
3380 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3381 label = gtk_label_new("-Empty-");
3382 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003383 event_box = gtk_event_box_new();
3384 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003385 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003386 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003387 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003388 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003389 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003390
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003391 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003392 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003393
3394 /* Create a popup menu for the tab line and connect it. */
3395 tabline_menu = create_tabline_menu();
3396 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003397 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003398#endif
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 gui.formwin = gtk_form_new();
3401 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3402 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3403
3404 gui.drawarea = gtk_drawing_area_new();
3405
3406 /* Determine which events we will filter. */
3407 gtk_widget_set_events(gui.drawarea,
3408 GDK_EXPOSURE_MASK |
3409 GDK_ENTER_NOTIFY_MASK |
3410 GDK_LEAVE_NOTIFY_MASK |
3411 GDK_BUTTON_PRESS_MASK |
3412 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 GDK_KEY_PRESS_MASK |
3415 GDK_KEY_RELEASE_MASK |
3416 GDK_POINTER_MOTION_MASK |
3417 GDK_POINTER_MOTION_HINT_MASK);
3418
3419 gtk_widget_show(gui.drawarea);
3420 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3421 gtk_widget_show(gui.formwin);
3422 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3423
3424 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3425 * and not the window. */
3426 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3427 : GTK_OBJECT(gui.drawarea),
3428 "key_press_event",
3429 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003430#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 /* Also forward key release events for the benefit of GTK+ 2 input
3432 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3433 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3434 : G_OBJECT(gui.drawarea),
3435 "key_release_event",
3436 G_CALLBACK(&key_release_event), NULL);
3437#endif
3438 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3439 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3440 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3441 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3442
3443 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3444 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3445
3446 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3447
3448#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3449 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3450 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3451#endif
3452
3453 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003454 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3456
3457 /*
3458 * Set clipboard specific atoms
3459 */
3460 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3463 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3464
3465 /*
3466 * Start out by adding the configured border width into the border offset.
3467 */
3468 gui.border_offset = gui.border_width;
3469
3470 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3471 GTK_SIGNAL_FUNC(visibility_event), NULL);
3472 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3473 GTK_SIGNAL_FUNC(expose_event), NULL);
3474
3475 /*
3476 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3477 * Only needed for some window managers.
3478 */
3479 if (vim_strchr(p_go, GO_POINTER) != NULL)
3480 {
3481 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3482 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3483 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3484 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3485 }
3486
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003487 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3488 * only its widgets. Arguably, this could be common code and we not use
3489 * the window focus at all, but let's be safe.
3490 */
3491 if (gtk_socket_id == 0)
3492 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003493 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3494 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3495 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3496 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003497 }
3498 else
3499 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003500 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3501 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3502 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3503 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003504#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003505 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3506 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3507 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3508 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003509#endif /* FEAT_GUI_TABLINE */
3510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
3512 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3513 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3514 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3515 GTK_SIGNAL_FUNC(button_press_event), NULL);
3516 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3517 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3519 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 /*
3522 * Add selection handler functions.
3523 */
3524 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3525 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3526 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3527 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3528
Bram Moolenaara76638f2010-06-05 12:49:46 +02003529 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
3531 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3532 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3533
3534 /* Pretend we don't have input focus, we will get an event if we do. */
3535 gui.in_focus = FALSE;
3536
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 return OK;
3538}
3539
3540#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3541/*
3542 * This is called from gui_start() after a fork() has been done.
3543 * We have to tell the session manager our new PID.
3544 */
3545 void
3546gui_mch_forked(void)
3547{
3548 if (using_gnome)
3549 {
3550 GnomeClient *client;
3551
3552 client = gnome_master_client();
3553
3554 if (client != NULL)
3555 gnome_client_set_process_id(client, getpid());
3556 }
3557}
3558#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3559
3560/*
3561 * Called when the foreground or background color has been changed.
3562 * This used to change the graphics contexts directly but we are
3563 * currently manipulating them where desired.
3564 */
3565 void
3566gui_mch_new_colors(void)
3567{
3568 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3569 {
3570 GdkColor color = { 0, 0, 0, 0 };
3571
3572 color.pixel = gui.back_pixel;
3573 gdk_window_set_background(gui.drawarea->window, &color);
3574 }
3575}
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577/*
3578 * This signal informs us about the need to rearrange our sub-widgets.
3579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003581form_configure_event(GtkWidget *widget UNUSED,
3582 GdkEventConfigure *event,
3583 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003585 int usable_height = event->height;
3586
3587 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3588 * no. of char-heights), so we have to manually sanitise them.
3589 * Widths seem to sort themselves out, don't ask me why.
3590 */
3591 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003592 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003593
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003595 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 gtk_form_thaw(GTK_FORM(gui.formwin));
3597
3598 return TRUE;
3599}
3600
3601/*
3602 * Function called when window already closed.
3603 * We can't do much more here than to trying to preserve what had been done,
3604 * since the window is already inevitably going away.
3605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003607mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 /* Don't write messages to the GUI anymore */
3610 full_screen = FALSE;
3611
3612 gui.mainwin = NULL;
3613 gui.drawarea = NULL;
3614
3615 if (!exiting) /* only do anything if the destroy was unexpected */
3616 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003617 vim_strncpy(IObuff,
3618 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3619 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 preserve_exit();
3621 }
3622}
3623
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003624
3625/*
3626 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3627 * hints (and thus the required size from -geom), but that after that we
3628 * put the hints back to normal (the actual minimum size) so we may
3629 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003630 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003631 * only way we have of making ourselves bigger (by set lines/columns).
3632 * Thus set hints at start-up to ensure correct init. size, then a
3633 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003634 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003635 * We'll not let the default hints be set while this timer's active.
3636 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003637 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003638check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003639{
3640 if (init_window_hints_state == 1)
3641 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003642 /* Safe to use normal hints now */
3643 init_window_hints_state = 0;
3644 update_window_manager_hints(0, 0);
3645 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003646 }
3647
3648 /* Keep on trying */
3649 init_window_hints_state = 1;
3650 return TRUE;
3651}
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653/*
3654 * Open the GUI window which was created by a call to gui_mch_init().
3655 */
3656 int
3657gui_mch_open(void)
3658{
3659 guicolor_T fg_pixel = INVALCOLOR;
3660 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003661 guint pixel_width;
3662 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 /*
3665 * Allow setting a window role on the command line, or invent one
3666 * if none was specified. This is mainly useful for GNOME session
3667 * support; allowing the WM to restore window placement.
3668 */
3669 if (role_argument != NULL)
3670 {
3671 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3672 }
3673 else
3674 {
3675 char *role;
3676
3677 /* Invent a unique-enough ID string for the role */
3678 role = g_strdup_printf("vim-%u-%u-%u",
3679 (unsigned)mch_get_pid(),
3680 (unsigned)g_random_int(),
3681 (unsigned)time(NULL));
3682
3683 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3684 g_free(role);
3685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
3687 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 /* Determine user specified geometry, if present. */
3691 if (gui.geom != NULL)
3692 {
3693 int mask;
3694 unsigned int w, h;
3695 int x = 0;
3696 int y = 0;
3697
3698 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3699
3700 if (mask & WidthValue)
3701 Columns = w;
3702 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003703 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003704 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003705 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003707 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003708 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003709
3710 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3711 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3712
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003713 pixel_width += get_menu_tool_width();
3714 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003715
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003717 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003718 int ww, hh;
3719 gui_mch_get_screen_dimensions(&ww, &hh);
3720 hh += p_ghr + get_menu_tool_height();
3721 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003722 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003723 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003724 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003725 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 vim_free(gui.geom);
3729 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003730
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003731 /* From now until everyone's stopped trying to set the window hints
3732 * to their correct minimum values, stop them being set as we need
3733 * them to remain at our required size for the parent GtkSocket to
3734 * give us the right initial size.
3735 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003736 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003737 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003738 update_window_manager_hints(pixel_width, pixel_height);
3739 init_window_hints_state = 1;
3740 g_timeout_add(1000, check_startup_plug_hints, NULL);
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003744 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3745 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003746 /* For GTK2 changing the size of the form widget doesn't cause window
3747 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003748 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003749 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003750 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 if (foreground_argument != NULL)
3753 fg_pixel = gui_get_color((char_u *)foreground_argument);
3754 if (fg_pixel == INVALCOLOR)
3755 fg_pixel = gui_get_color((char_u *)"Black");
3756
3757 if (background_argument != NULL)
3758 bg_pixel = gui_get_color((char_u *)background_argument);
3759 if (bg_pixel == INVALCOLOR)
3760 bg_pixel = gui_get_color((char_u *)"White");
3761
3762 if (found_reverse_arg)
3763 {
3764 gui.def_norm_pixel = bg_pixel;
3765 gui.def_back_pixel = fg_pixel;
3766 }
3767 else
3768 {
3769 gui.def_norm_pixel = fg_pixel;
3770 gui.def_back_pixel = bg_pixel;
3771 }
3772
3773 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3774 * in a vimrc file) */
3775 set_normal_colors();
3776
3777 /* Check that none of the colors are the same as the background color */
3778 gui_check_colors();
3779
3780 /* Get the colors for the highlight groups (gui_check_colors() might have
3781 * changed them). */
3782 highlight_gui_started(); /* re-init colors and fonts */
3783
3784 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3785 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3786
3787#ifdef FEAT_HANGULIN
3788 hangul_keyboard_set();
3789#endif
3790
3791 /*
3792 * Notify the fixed area about the need to resize the contents of the
3793 * gui.formwin, which we use for random positioning of the included
3794 * components.
3795 *
3796 * We connect this signal deferred finally after anything is in place,
3797 * since this is intended to handle resizements coming from the window
3798 * manager upon us and should not interfere with what VIM is requesting
3799 * upon startup.
3800 */
3801 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3802 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3803
3804#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003805 /* Set up for receiving DND items. */
3806 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
3808 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3809 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3810#endif
3811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003813 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 if (found_iconic_arg && gtk_socket_id == 0)
3815 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816
3817 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003818#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 unsigned long menu_handler = 0;
3820# ifdef FEAT_TOOLBAR
3821 unsigned long tool_handler = 0;
3822# endif
3823 /*
3824 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3825 * show when restoring the saved layout configuration. We can't just
3826 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3827 * a toplevel window and thus will be realized immediately. Instead,
3828 * connect signal handlers to hide the widgets just after they've been
3829 * marked visible, but before the main window is realized.
3830 */
3831 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3832 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3833 G_CALLBACK(&gtk_widget_hide),
3834 NULL);
3835# ifdef FEAT_TOOLBAR
3836 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3837 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3838 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3839 G_CALLBACK(&gtk_widget_hide),
3840 NULL);
3841# endif
3842#endif
3843 gtk_widget_show(gui.mainwin);
3844
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003845#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 if (menu_handler != 0)
3847 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3848# ifdef FEAT_TOOLBAR
3849 if (tool_handler != 0)
3850 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3851# endif
3852#endif
3853 }
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 return OK;
3856}
3857
3858
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003860gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861{
3862 if (gui.mainwin != NULL)
3863 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864}
3865
3866/*
3867 * Get the position of the top left corner of the window.
3868 */
3869 int
3870gui_mch_get_winpos(int *x, int *y)
3871{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 return OK;
3874}
3875
3876/*
3877 * Set the position of the top left corner of the window to the given
3878 * coordinates.
3879 */
3880 void
3881gui_mch_set_winpos(int x, int y)
3882{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884}
3885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886#if 0
3887static int resize_idle_installed = FALSE;
3888/*
3889 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3890 * the shell size doesn't exceed the window size, i.e. if the window manager
3891 * ignored our size request. Usually this happens if the window is maximized.
3892 *
3893 * FIXME: It'd be nice if we could find a little more orthodox solution.
3894 * See also the remark below in gui_mch_set_shellsize().
3895 *
3896 * DISABLED: When doing ":set lines+=1" this function would first invoke
3897 * gui_resize_shell() with the old size, then the normal callback would
3898 * report the new size through form_configure_event(). That caused the window
3899 * layout to be messed up.
3900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 static gboolean
3902force_shell_resize_idle(gpointer data)
3903{
3904 if (gui.mainwin != NULL
3905 && GTK_WIDGET_REALIZED(gui.mainwin)
3906 && GTK_WIDGET_VISIBLE(gui.mainwin))
3907 {
3908 int width;
3909 int height;
3910
3911 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3912
3913 width -= get_menu_tool_width();
3914 height -= get_menu_tool_height();
3915
3916 gui_resize_shell(width, height);
3917 }
3918
3919 resize_idle_installed = FALSE;
3920 return FALSE; /* don't call me again */
3921}
3922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923
Bram Moolenaar09736232009-09-23 16:14:49 +00003924/*
3925 * Return TRUE if the main window is maximized.
3926 */
3927 int
3928gui_mch_maximized()
3929{
3930 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3931 && (gdk_window_get_state(gui.mainwin->window)
3932 & GDK_WINDOW_STATE_MAXIMIZED));
3933}
3934
3935/*
3936 * Unmaximize the main window
3937 */
3938 void
3939gui_mch_unmaximize()
3940{
3941 if (gui.mainwin != NULL)
3942 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3943}
Bram Moolenaar09736232009-09-23 16:14:49 +00003944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02003946 * Called when the font changed while the window is maximized. Compute the
3947 * new Rows and Columns. This is like resizing the window.
3948 */
3949 void
3950gui_mch_newfont()
3951{
3952 int w, h;
3953
3954 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
3955 w -= get_menu_tool_width();
3956 h -= get_menu_tool_height();
3957 gui_resize_shell(w, h);
3958}
3959
3960/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 * Set the windows size.
3962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 void
3964gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003965 int min_width UNUSED, int min_height UNUSED,
3966 int base_width UNUSED, int base_height UNUSED,
3967 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 /* give GTK+ a chance to put all widget's into place */
3970 gui_mch_update();
3971
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003972 /* this will cause the proper resizement to happen too */
3973 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003974 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003977 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 * main window instead. */
3979 width += get_menu_tool_width();
3980 height += get_menu_tool_height();
3981
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003982 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003983 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003984 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003985 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003987# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (!resize_idle_installed)
3989 {
3990 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3991 &force_shell_resize_idle, NULL, NULL);
3992 resize_idle_installed = TRUE;
3993 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 /*
3996 * Wait until all events are processed to prevent a crash because the
3997 * real size of the drawing area doesn't reflect Vim's internal ideas.
3998 *
3999 * This is a bit of a hack, since Vim is a terminal application with a GUI
4000 * on top, while the GUI expects to be the boss.
4001 */
4002 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003}
4004
4005
4006/*
4007 * The screen size is used to make sure the initial window doesn't get bigger
4008 * than the screen. This subtracts some room for menubar, toolbar and window
4009 * decorations.
4010 */
4011 void
4012gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4013{
4014#ifdef HAVE_GTK_MULTIHEAD
4015 GdkScreen* screen;
4016
4017 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4018 screen = gtk_widget_get_screen(gui.mainwin);
4019 else
4020 screen = gdk_screen_get_default();
4021
4022 *screen_w = gdk_screen_get_width(screen);
4023 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4024#else
4025 *screen_w = gdk_screen_width();
4026 /* Subtract 'guiheadroom' from the height to allow some room for the
4027 * window manager (task list and window title bar). */
4028 *screen_h = gdk_screen_height() - p_ghr;
4029#endif
4030
4031 /*
4032 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4033 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004034 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 * This should be completely changed later.
4036 */
4037 *screen_w -= get_menu_tool_width();
4038 *screen_h -= get_menu_tool_height();
4039}
4040
4041#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004043gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 if (title != NULL && output_conv.vc_type != CONV_NONE)
4046 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047
4048 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4049
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 if (output_conv.vc_type != CONV_NONE)
4051 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052}
4053#endif /* FEAT_TITLE */
4054
4055#if defined(FEAT_MENU) || defined(PROTO)
4056 void
4057gui_mch_enable_menu(int showit)
4058{
4059 GtkWidget *widget;
4060
4061# ifdef FEAT_GUI_GNOME
4062 if (using_gnome)
4063 widget = gui.menubar_h;
4064 else
4065# endif
4066 widget = gui.menubar;
4067
Bram Moolenaar18144c82006-04-12 21:52:12 +00004068 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4069 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
4071 if (showit)
4072 gtk_widget_show(widget);
4073 else
4074 gtk_widget_hide(widget);
4075
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004076 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078}
4079#endif /* FEAT_MENU */
4080
4081#if defined(FEAT_TOOLBAR) || defined(PROTO)
4082 void
4083gui_mch_show_toolbar(int showit)
4084{
4085 GtkWidget *widget;
4086
4087 if (gui.toolbar == NULL)
4088 return;
4089
4090# ifdef FEAT_GUI_GNOME
4091 if (using_gnome)
4092 widget = gui.toolbar_h;
4093 else
4094# endif
4095 widget = gui.toolbar;
4096
4097 if (showit)
4098 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4099
4100 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4101 {
4102 if (showit)
4103 gtk_widget_show(widget);
4104 else
4105 gtk_widget_hide(widget);
4106
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004107 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
4109}
4110#endif /* FEAT_TOOLBAR */
4111
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112/*
4113 * Check if a given font is a CJK font. This is done in a very crude manner. It
4114 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4115 * font. Consequently, this function cannot be used as a general purpose check
4116 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4117 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4118 */
4119 static int
4120is_cjk_font(PangoFontDescription *font_desc)
4121{
4122 static const char * const cjk_langs[] =
4123 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4124
4125 PangoFont *font;
4126 unsigned i;
4127 int is_cjk = FALSE;
4128
4129 font = pango_context_load_font(gui.text_context, font_desc);
4130
4131 if (font == NULL)
4132 return FALSE;
4133
4134 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4135 {
4136 PangoCoverage *coverage;
4137 gunichar uc;
4138
4139 coverage = pango_font_get_coverage(
4140 font, pango_language_from_string(cjk_langs[i]));
4141
4142 if (coverage != NULL)
4143 {
4144 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4145 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4146 pango_coverage_unref(coverage);
4147 }
4148 }
4149
4150 g_object_unref(font);
4151
4152 return is_cjk;
4153}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaar231334e2005-07-25 20:46:57 +00004155/*
4156 * Adjust gui.char_height (after 'linespace' was changed).
4157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004159gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 PangoFontMetrics *metrics;
4162 int ascent;
4163 int descent;
4164
4165 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4166 pango_context_get_language(gui.text_context));
4167 ascent = pango_font_metrics_get_ascent(metrics);
4168 descent = pango_font_metrics_get_descent(metrics);
4169
4170 pango_font_metrics_unref(metrics);
4171
4172 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004173 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004174 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4176
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 /* A not-positive value of char_height may crash Vim. Only happens
4178 * if 'linespace' is negative (which does make sense sometimes). */
4179 gui.char_ascent = MAX(gui.char_ascent, 0);
4180 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4181
4182 return OK;
4183}
4184
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185/*
4186 * Put up a font dialog and return the selected font name in allocated memory.
4187 * "oldval" is the previous value. Return NULL when cancelled.
4188 * This should probably go into gui_gtk.c. Hmm.
4189 * FIXME:
4190 * The GTK2 font selection dialog has no filtering API. So we could either
4191 * a) implement our own (possibly copying the code from somewhere else) or
4192 * b) just live with it.
4193 */
4194 char_u *
4195gui_mch_font_dialog(char_u *oldval)
4196{
4197 GtkWidget *dialog;
4198 int response;
4199 char_u *fontname = NULL;
4200 char_u *oldname;
4201
4202 dialog = gtk_font_selection_dialog_new(NULL);
4203
4204 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4205 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4206
4207 if (oldval != NULL && oldval[0] != NUL)
4208 {
4209 if (output_conv.vc_type != CONV_NONE)
4210 oldname = string_convert(&output_conv, oldval, NULL);
4211 else
4212 oldname = oldval;
4213
4214 /* Annoying bug in GTK (or Pango): if the font name does not include a
4215 * size, zero is used. Use default point size ten. */
4216 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4217 {
4218 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4219
4220 if (p != NULL)
4221 {
4222 STRCPY(p + STRLEN(p), " 10");
4223 if (oldname != oldval)
4224 vim_free(oldname);
4225 oldname = p;
4226 }
4227 }
4228
4229 gtk_font_selection_dialog_set_font_name(
4230 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4231
4232 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004233 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004235 else
4236 gtk_font_selection_dialog_set_font_name(
4237 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 response = gtk_dialog_run(GTK_DIALOG(dialog));
4240
4241 if (response == GTK_RESPONSE_OK)
4242 {
4243 char *name;
4244
4245 name = gtk_font_selection_dialog_get_font_name(
4246 GTK_FONT_SELECTION_DIALOG(dialog));
4247 if (name != NULL)
4248 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 char_u *p;
4250
4251 /* Apparently some font names include a comma, need to escape
4252 * that, because in 'guifont' it separates names. */
4253 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004255 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004256 {
4257 fontname = string_convert(&input_conv, p, NULL);
4258 vim_free(p);
4259 }
4260 else
4261 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 }
4264
4265 if (response != GTK_RESPONSE_NONE)
4266 gtk_widget_destroy(dialog);
4267
4268 return fontname;
4269}
4270
4271/*
4272 * Some monospace fonts don't support a bold weight, and fall back
4273 * silently to the regular weight. But this is no good since our text
4274 * drawing function can emulate bold by overstriking. So let's try
4275 * to detect whether bold weight is actually available and emulate it
4276 * otherwise.
4277 *
4278 * Note that we don't need to check for italic style since Xft can
4279 * emulate italic on its own, provided you have a proper fontconfig
4280 * setup. We wouldn't be able to emulate it in Vim anyway.
4281 */
4282 static void
4283get_styled_font_variants(void)
4284{
4285 PangoFontDescription *bold_font_desc;
4286 PangoFont *plain_font;
4287 PangoFont *bold_font;
4288
4289 gui.font_can_bold = FALSE;
4290
4291 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4292
4293 if (plain_font == NULL)
4294 return;
4295
4296 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4297 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4298
4299 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4300 /*
4301 * The comparison relies on the unique handle nature of a PangoFont*,
4302 * i.e. it's assumed that a different PangoFont* won't refer to the
4303 * same font. Seems to work, and failing here isn't critical anyway.
4304 */
4305 if (bold_font != NULL)
4306 {
4307 gui.font_can_bold = (bold_font != plain_font);
4308 g_object_unref(bold_font);
4309 }
4310
4311 pango_font_description_free(bold_font_desc);
4312 g_object_unref(plain_font);
4313}
4314
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315static PangoEngineShape *default_shape_engine = NULL;
4316
4317/*
4318 * Create a map from ASCII characters in the range [32,126] to glyphs
4319 * of the current font. This is used by gui_gtk2_draw_string() to skip
4320 * the itemize and shaping process for the most common case.
4321 */
4322 static void
4323ascii_glyph_table_init(void)
4324{
4325 char_u ascii_chars[128];
4326 PangoAttrList *attr_list;
4327 GList *item_list;
4328 int i;
4329
4330 if (gui.ascii_glyphs != NULL)
4331 pango_glyph_string_free(gui.ascii_glyphs);
4332 if (gui.ascii_font != NULL)
4333 g_object_unref(gui.ascii_font);
4334
4335 gui.ascii_glyphs = NULL;
4336 gui.ascii_font = NULL;
4337
4338 /* For safety, fill in question marks for the control characters. */
4339 for (i = 0; i < 32; ++i)
4340 ascii_chars[i] = '?';
4341 for (; i < 127; ++i)
4342 ascii_chars[i] = i;
4343 ascii_chars[i] = '?';
4344
4345 attr_list = pango_attr_list_new();
4346 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4347 0, sizeof(ascii_chars), attr_list, NULL);
4348
4349 if (item_list != NULL && item_list->next == NULL) /* play safe */
4350 {
4351 PangoItem *item;
4352 int width;
4353
4354 item = (PangoItem *)item_list->data;
4355 width = gui.char_width * PANGO_SCALE;
4356
4357 /* Remember the shape engine used for ASCII. */
4358 default_shape_engine = item->analysis.shape_engine;
4359
4360 gui.ascii_font = item->analysis.font;
4361 g_object_ref(gui.ascii_font);
4362
4363 gui.ascii_glyphs = pango_glyph_string_new();
4364
4365 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4366 &item->analysis, gui.ascii_glyphs);
4367
4368 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4369
4370 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4371 {
4372 PangoGlyphGeometry *geom;
4373
4374 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4375 geom->x_offset += MAX(0, width - geom->width) / 2;
4376 geom->width = width;
4377 }
4378 }
4379
4380 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4381 g_list_free(item_list);
4382 pango_attr_list_unref(attr_list);
4383}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385/*
4386 * Initialize Vim to use the font or fontset with the given name.
4387 * Return FAIL if the font could not be loaded, OK otherwise.
4388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004390gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 PangoFontDescription *font_desc;
4393 PangoLayout *layout;
4394 int width;
4395
4396 /* If font_name is NULL, this means to use the default, which should
4397 * be present on all proper Pango/fontconfig installations. */
4398 if (font_name == NULL)
4399 font_name = (char_u *)DEFAULT_FONT;
4400
4401 font_desc = gui_mch_get_font(font_name, FALSE);
4402
4403 if (font_desc == NULL)
4404 return FAIL;
4405
4406 gui_mch_free_font(gui.norm_font);
4407 gui.norm_font = font_desc;
4408
4409 pango_context_set_font_description(gui.text_context, font_desc);
4410
4411 layout = pango_layout_new(gui.text_context);
4412 pango_layout_set_text(layout, "MW", 2);
4413 pango_layout_get_size(layout, &width, NULL);
4414 /*
4415 * Set char_width to half the width obtained from pango_layout_get_size()
4416 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4417 * Pango to use the same width for both non-CJK characters (e.g. Latin
4418 * letters and numbers) and CJK characters. This results in 's p a c e d
4419 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4420 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4421 * width for CJK fonts.
4422 *
4423 * For related bugs, see:
4424 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4425 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4426 *
4427 * With this, for all four of the following cases, Vim works fine:
4428 * guifont=CJK_fixed_width_font
4429 * guifont=Non_CJK_fixed_font
4430 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4431 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4432 */
4433 if (is_cjk_font(gui.norm_font))
4434 {
4435 int cjk_width;
4436
4437 /* Measure the text extent of U+4E00 and U+4E8C */
4438 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4439 pango_layout_get_size(layout, &cjk_width, NULL);
4440
4441 if (width == cjk_width) /* Xft not patched */
4442 width /= 2;
4443 }
4444 g_object_unref(layout);
4445
4446 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4447
4448 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4449 if (gui.char_width <= 0)
4450 gui.char_width = 8;
4451
Bram Moolenaar231334e2005-07-25 20:46:57 +00004452 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 /* Set the fontname, which will be used for information purposes */
4455 hl_set_font_name(font_name);
4456
4457 get_styled_font_variants();
4458 ascii_glyph_table_init();
4459
4460 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4461 if (gui.wide_font != NULL
4462 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4463 {
4464 pango_font_description_free(gui.wide_font);
4465 gui.wide_font = NULL;
4466 }
4467
Bram Moolenaare161c792009-11-03 17:13:59 +00004468 if (gui_mch_maximized())
4469 {
Bram Moolenaare161c792009-11-03 17:13:59 +00004470 /* Update lines and columns in accordance with the new font, keep the
4471 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004472 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00004473 }
4474 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004475 {
4476 /* Preserve the logical dimensions of the screen. */
4477 update_window_manager_hints(0, 0);
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
4480 return OK;
4481}
4482
4483/*
4484 * Get a reference to the font "name".
4485 * Return zero for failure.
4486 */
4487 GuiFont
4488gui_mch_get_font(char_u *name, int report_error)
4489{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 /* can't do this when GUI is not running */
4493 if (!gui.in_use || name == NULL)
4494 return NULL;
4495
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 if (output_conv.vc_type != CONV_NONE)
4497 {
4498 char_u *buf;
4499
4500 buf = string_convert(&output_conv, name, NULL);
4501 if (buf != NULL)
4502 {
4503 font = pango_font_description_from_string((const char *)buf);
4504 vim_free(buf);
4505 }
4506 else
4507 font = NULL;
4508 }
4509 else
4510 font = pango_font_description_from_string((const char *)name);
4511
4512 if (font != NULL)
4513 {
4514 PangoFont *real_font;
4515
4516 /* pango_context_load_font() bails out if no font size is set */
4517 if (pango_font_description_get_size(font) <= 0)
4518 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4519
4520 real_font = pango_context_load_font(gui.text_context, font);
4521
4522 if (real_font == NULL)
4523 {
4524 pango_font_description_free(font);
4525 font = NULL;
4526 }
4527 else
4528 g_object_unref(real_font);
4529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 if (font == NULL)
4532 {
4533 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004534 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 return NULL;
4536 }
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 return font;
4539}
4540
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004541#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004542/*
4543 * Return the name of font "font" in allocated memory.
4544 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004545 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004546gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004547{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004548 if (font != NOFONT)
4549 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004550 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004551
Bram Moolenaar89d40322006-08-29 15:30:07 +00004552 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004553 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004554 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004555
Bram Moolenaar89d40322006-08-29 15:30:07 +00004556 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004557 return s;
4558 }
4559 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004560 return NULL;
4561}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004562#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004563
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564/*
4565 * If a font is not going to be used, free its structure.
4566 */
4567 void
4568gui_mch_free_font(GuiFont font)
4569{
4570 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572}
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574/*
4575 * Return the Pixel value (color) for the given color name. This routine was
4576 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4577 * Programmer's Guide.
4578 * Return INVALCOLOR for error.
4579 */
4580 guicolor_T
4581gui_mch_get_color(char_u *name)
4582{
4583 /* A number of colors that some X11 systems don't have */
4584 static const char *const vimnames[][2] =
4585 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004586 {"LightRed", "#FFBBBB"},
4587 {"LightGreen", "#88FF88"},
4588 {"LightMagenta","#FFBBFF"},
4589 {"DarkCyan", "#008888"},
4590 {"DarkBlue", "#0000BB"},
4591 {"DarkRed", "#BB0000"},
4592 {"DarkMagenta", "#BB00BB"},
4593 {"DarkGrey", "#BBBBBB"},
4594 {"DarkYellow", "#BBBB00"},
4595 {"Gray10", "#1A1A1A"},
4596 {"Grey10", "#1A1A1A"},
4597 {"Gray20", "#333333"},
4598 {"Grey20", "#333333"},
4599 {"Gray30", "#4D4D4D"},
4600 {"Grey30", "#4D4D4D"},
4601 {"Gray40", "#666666"},
4602 {"Grey40", "#666666"},
4603 {"Gray50", "#7F7F7F"},
4604 {"Grey50", "#7F7F7F"},
4605 {"Gray60", "#999999"},
4606 {"Grey60", "#999999"},
4607 {"Gray70", "#B3B3B3"},
4608 {"Grey70", "#B3B3B3"},
4609 {"Gray80", "#CCCCCC"},
4610 {"Grey80", "#CCCCCC"},
4611 {"Gray90", "#E5E5E5"},
4612 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {NULL, NULL}
4614 };
4615
4616 if (!gui.in_use) /* can't do this when GUI not running */
4617 return INVALCOLOR;
4618
4619 while (name != NULL)
4620 {
4621 GdkColor color;
4622 int parsed;
4623 int i;
4624
4625 parsed = gdk_color_parse((const char *)name, &color);
4626
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (parsed)
4628 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4630 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 return (guicolor_T)color.pixel;
4632 }
4633 /* add a few builtin names and try again */
4634 for (i = 0; ; ++i)
4635 {
4636 if (vimnames[i][0] == NULL)
4637 {
4638 name = NULL;
4639 break;
4640 }
4641 if (STRICMP(name, vimnames[i][0]) == 0)
4642 {
4643 name = (char_u *)vimnames[i][1];
4644 break;
4645 }
4646 }
4647 }
4648
4649 return INVALCOLOR;
4650}
4651
4652/*
4653 * Set the current text foreground color.
4654 */
4655 void
4656gui_mch_set_fg_color(guicolor_T color)
4657{
4658 gui.fgcolor->pixel = (unsigned long)color;
4659}
4660
4661/*
4662 * Set the current text background color.
4663 */
4664 void
4665gui_mch_set_bg_color(guicolor_T color)
4666{
4667 gui.bgcolor->pixel = (unsigned long)color;
4668}
4669
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004670/*
4671 * Set the current text special color.
4672 */
4673 void
4674gui_mch_set_sp_color(guicolor_T color)
4675{
4676 gui.spcolor->pixel = (unsigned long)color;
4677}
4678
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679/*
4680 * Function-like convenience macro for the sake of efficiency.
4681 */
4682#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4683 G_STMT_START{ \
4684 PangoAttribute *tmp_attr_; \
4685 tmp_attr_ = (Attribute); \
4686 tmp_attr_->start_index = (Start); \
4687 tmp_attr_->end_index = (End); \
4688 pango_attr_list_insert((AttrList), tmp_attr_); \
4689 }G_STMT_END
4690
4691 static void
4692apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4693{
4694 char_u *start = NULL;
4695 char_u *p;
4696 int uc;
4697
4698 for (p = s; p < s + len; p += utf_byte2len(*p))
4699 {
4700 uc = utf_ptr2char(p);
4701
4702 if (start == NULL)
4703 {
4704 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4705 start = p;
4706 }
4707 else if (uc < 0x80 /* optimization shortcut */
4708 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4709 {
4710 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4711 attr_list, start - s, p - s);
4712 start = NULL;
4713 }
4714 }
4715
4716 if (start != NULL)
4717 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4718 attr_list, start - s, len);
4719}
4720
4721 static int
4722count_cluster_cells(char_u *s, PangoItem *item,
4723 PangoGlyphString* glyphs, int i,
4724 int *cluster_width,
4725 int *last_glyph_rbearing)
4726{
4727 char_u *p;
4728 int next; /* glyph start index of next cluster */
4729 int start, end; /* string segment of current cluster */
4730 int width; /* real cluster width in Pango units */
4731 int uc;
4732 int cellcount = 0;
4733
4734 width = glyphs->glyphs[i].geometry.width;
4735
4736 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4737 {
4738 if (glyphs->glyphs[next].attr.is_cluster_start)
4739 break;
4740 else if (glyphs->glyphs[next].geometry.width > width)
4741 width = glyphs->glyphs[next].geometry.width;
4742 }
4743
4744 start = item->offset + glyphs->log_clusters[i];
4745 end = item->offset + ((next < glyphs->num_glyphs) ?
4746 glyphs->log_clusters[next] : item->length);
4747
4748 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4749 {
4750 uc = utf_ptr2char(p);
4751 if (uc < 0x80)
4752 ++cellcount;
4753 else if (!utf_iscomposing(uc))
4754 cellcount += utf_char2cells(uc);
4755 }
4756
4757 if (last_glyph_rbearing != NULL
4758 && cellcount > 0 && next == glyphs->num_glyphs)
4759 {
4760 PangoRectangle ink_rect;
4761 /*
4762 * If a certain combining mark had to be taken from a non-monospace
4763 * font, we have to compensate manually by adapting x_offset according
4764 * to the ink extents of the previous glyph.
4765 */
4766 pango_font_get_glyph_extents(item->analysis.font,
4767 glyphs->glyphs[i].glyph,
4768 &ink_rect, NULL);
4769
4770 if (PANGO_RBEARING(ink_rect) > 0)
4771 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4772 }
4773
4774 if (cellcount > 0)
4775 *cluster_width = width;
4776
4777 return cellcount;
4778}
4779
4780/*
4781 * If there are only combining characters in the cluster, we cannot just
4782 * change the width of the previous glyph since there is none. Therefore
4783 * some guesswork is needed.
4784 *
4785 * If ink_rect.x is negative Pango apparently has taken care of the composing
4786 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4787 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004788 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 *
4790 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4791 * the position of the previous glyph. Apparently this happens only with old
4792 * X fonts which don't provide the special combining information needed by
4793 * Pango.
4794 */
4795 static void
4796setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4797 int last_cellcount, int last_cluster_width,
4798 int last_glyph_rbearing)
4799{
4800 PangoRectangle ink_rect;
4801 PangoRectangle logical_rect;
4802 int width;
4803
4804 width = last_cellcount * gui.char_width * PANGO_SCALE;
4805 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4806 glyph->geometry.width = 0;
4807
4808 pango_font_get_glyph_extents(item->analysis.font,
4809 glyph->glyph,
4810 &ink_rect, &logical_rect);
4811 if (ink_rect.x < 0)
4812 {
4813 glyph->geometry.x_offset += last_glyph_rbearing;
4814 glyph->geometry.y_offset = logical_rect.height
4815 - (gui.char_height - p_linespace) * PANGO_SCALE;
4816 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004817 else
4818 /* If the accent width is smaller than the cluster width, position it
4819 * in the middle. */
4820 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821}
4822
4823 static void
4824draw_glyph_string(int row, int col, int num_cells, int flags,
4825 PangoFont *font, PangoGlyphString *glyphs)
4826{
4827 if (!(flags & DRAW_TRANSP))
4828 {
4829 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4830
4831 gdk_draw_rectangle(gui.drawarea->window,
4832 gui.text_gc,
4833 TRUE,
4834 FILL_X(col),
4835 FILL_Y(row),
4836 num_cells * gui.char_width,
4837 gui.char_height);
4838 }
4839
4840 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4841
4842 gdk_draw_glyphs(gui.drawarea->window,
4843 gui.text_gc,
4844 font,
4845 TEXT_X(col),
4846 TEXT_Y(row),
4847 glyphs);
4848
4849 /* redraw the contents with an offset of 1 to emulate bold */
4850 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4851 gdk_draw_glyphs(gui.drawarea->window,
4852 gui.text_gc,
4853 font,
4854 TEXT_X(col) + 1,
4855 TEXT_Y(row),
4856 glyphs);
4857}
4858
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004859/*
4860 * Draw underline and undercurl at the bottom of the character cell.
4861 */
4862 static void
4863draw_under(int flags, int row, int col, int cells)
4864{
4865 int i;
4866 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004867 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004868 int y = FILL_Y(row + 1) - 1;
4869
4870 /* Undercurl: draw curl at the bottom of the character cell. */
4871 if (flags & DRAW_UNDERC)
4872 {
4873 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4874 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4875 {
4876 offset = val[i % 8];
4877 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4878 }
4879 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4880 }
4881
4882 /* Underline: draw a line at the bottom of the character cell. */
4883 if (flags & DRAW_UNDERL)
4884 {
4885 /* When p_linespace is 0, overwrite the bottom row of pixels.
4886 * Otherwise put the line just below the character. */
4887 if (p_linespace > 1)
4888 y -= p_linespace - 1;
4889 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4890 FILL_X(col), y,
4891 FILL_X(col + cells) - 1, y);
4892 }
4893}
4894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 int
4896gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4897{
4898 GdkRectangle area; /* area for clip mask */
4899 PangoGlyphString *glyphs; /* glyphs of current item */
4900 int column_offset = 0; /* column offset in cells */
4901 int i;
4902 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4903 char_u *new_conv_buf;
4904 int convlen;
4905 char_u *sp, *bp;
4906 int plen;
4907
4908 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4909 return len;
4910
4911 if (output_conv.vc_type != CONV_NONE)
4912 {
4913 /*
4914 * Convert characters from 'encoding' to 'termencoding', which is set
4915 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4916 * prohibits changing this to something else than UTF-8 if the GUI is
4917 * in use.
4918 */
4919 convlen = len;
4920 conv_buf = string_convert(&output_conv, s, &convlen);
4921 g_return_val_if_fail(conv_buf != NULL, len);
4922
4923 /* Correct for differences in char width: some chars are
4924 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4925 * compensate for that. */
4926 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4927 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004928 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4930 {
4931 new_conv_buf = alloc(convlen + 2);
4932 if (new_conv_buf == NULL)
4933 return len;
4934 plen += bp - conv_buf;
4935 mch_memmove(new_conv_buf, conv_buf, plen);
4936 new_conv_buf[plen] = ' ';
4937 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4938 convlen - plen + 1);
4939 vim_free(conv_buf);
4940 conv_buf = new_conv_buf;
4941 ++convlen;
4942 bp = conv_buf + plen;
4943 plen = 1;
4944 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004945 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 bp += plen;
4947 }
4948 s = conv_buf;
4949 len = convlen;
4950 }
4951
4952 /*
4953 * Restrict all drawing to the current screen line in order to prevent
4954 * fuzzy font lookups from messing up the screen.
4955 */
4956 area.x = gui.border_offset;
4957 area.y = FILL_Y(row);
4958 area.width = gui.num_cols * gui.char_width;
4959 area.height = gui.char_height;
4960
4961 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4962 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4963
4964 glyphs = pango_glyph_string_new();
4965
4966 /*
4967 * Optimization hack: If possible, skip the itemize and shaping process
4968 * for pure ASCII strings. This optimization is particularly effective
4969 * because Vim draws space characters to clear parts of the screen.
4970 */
4971 if (!(flags & DRAW_ITALIC)
4972 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4973 && gui.ascii_glyphs != NULL)
4974 {
4975 char_u *p;
4976
4977 for (p = s; p < s + len; ++p)
4978 if (*p & 0x80)
4979 goto not_ascii;
4980
4981 pango_glyph_string_set_size(glyphs, len);
4982
4983 for (i = 0; i < len; ++i)
4984 {
4985 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4986 glyphs->log_clusters[i] = i;
4987 }
4988
4989 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
4990
4991 column_offset = len;
4992 }
4993 else
4994not_ascii:
4995 {
4996 PangoAttrList *attr_list;
4997 GList *item_list;
4998 int cluster_width;
4999 int last_glyph_rbearing;
5000 int cells = 0; /* cells occupied by current cluster */
5001
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005002 /* Safety check: pango crashes when invoked with invalid utf-8
5003 * characters. */
5004 if (!utf_valid_string(s, s + len))
5005 {
5006 column_offset = len;
5007 goto skipitall;
5008 }
5009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 /* original width of the current cluster */
5011 cluster_width = PANGO_SCALE * gui.char_width;
5012
5013 /* right bearing of the last non-composing glyph */
5014 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5015
5016 attr_list = pango_attr_list_new();
5017
5018 /* If 'guifontwide' is set then use that for double-width characters.
5019 * Otherwise just go with 'guifont' and let Pango do its thing. */
5020 if (gui.wide_font != NULL)
5021 apply_wide_font_attr(s, len, attr_list);
5022
5023 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5024 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5025 attr_list, 0, len);
5026 if (flags & DRAW_ITALIC)
5027 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5028 attr_list, 0, len);
5029 /*
5030 * Break the text into segments with consistent directional level
5031 * and shaping engine. Pure Latin text needs only a single segment,
5032 * so there's no need to worry about the loop's efficiency. Better
5033 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5034 */
5035 item_list = pango_itemize(gui.text_context,
5036 (const char *)s, 0, len, attr_list, NULL);
5037
5038 while (item_list != NULL)
5039 {
5040 PangoItem *item;
5041 int item_cells = 0; /* item length in cells */
5042
5043 item = (PangoItem *)item_list->data;
5044 item_list = g_list_delete_link(item_list, item_list);
5045 /*
5046 * Increment the bidirectional embedding level by 1 if it is not
5047 * even. An odd number means the output will be RTL, but we don't
5048 * want that since Vim handles right-to-left text on its own. It
5049 * would probably be sufficient to just set level = 0, but you can
5050 * never know :)
5051 *
5052 * Unfortunately we can't take advantage of Pango's ability to
5053 * render both LTR and RTL at the same time. In order to support
5054 * that, Vim's main screen engine would have to make use of Pango
5055 * functionality.
5056 */
5057 item->analysis.level = (item->analysis.level + 1) & (~1U);
5058
5059 /* HACK: Overrule the shape engine, we don't want shaping to be
5060 * done, because drawing the cursor would change the display. */
5061 item->analysis.shape_engine = default_shape_engine;
5062
5063 pango_shape((const char *)s + item->offset, item->length,
5064 &item->analysis, glyphs);
5065 /*
5066 * Fixed-width hack: iterate over the array and assign a fixed
5067 * width to each glyph, thus overriding the choice made by the
5068 * shaping engine. We use utf_char2cells() to determine the
5069 * number of cells needed.
5070 *
5071 * Also perform all kind of dark magic to get composing
5072 * characters right (and pretty too of course).
5073 */
5074 for (i = 0; i < glyphs->num_glyphs; ++i)
5075 {
5076 PangoGlyphInfo *glyph;
5077
5078 glyph = &glyphs->glyphs[i];
5079
5080 if (glyph->attr.is_cluster_start)
5081 {
5082 int cellcount;
5083
5084 cellcount = count_cluster_cells(
5085 s, item, glyphs, i, &cluster_width,
5086 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5087
5088 if (cellcount > 0)
5089 {
5090 int width;
5091
5092 width = cellcount * gui.char_width * PANGO_SCALE;
5093 glyph->geometry.x_offset +=
5094 MAX(0, width - cluster_width) / 2;
5095 glyph->geometry.width = width;
5096 }
5097 else
5098 {
5099 /* If there are only combining characters in the
5100 * cluster, we cannot just change the width of the
5101 * previous glyph since there is none. Therefore
5102 * some guesswork is needed. */
5103 setup_zero_width_cluster(item, glyph, cells,
5104 cluster_width,
5105 last_glyph_rbearing);
5106 }
5107
5108 item_cells += cellcount;
5109 cells = cellcount;
5110 }
5111 else if (i > 0)
5112 {
5113 int width;
5114
5115 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005116 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02005117 * In some circumstances Pango uses a positive x_offset,
5118 * then use the width of the previous glyph for this one
5119 * and set the previous width to zero.
5120 * Otherwise we get a negative x_offset, Pango has already
5121 * positioned the combining char, keep the widths as they
5122 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005123 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02005124 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005125 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02005126 {
5127 glyphs->glyphs[i].geometry.width =
5128 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005129 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02005130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 width = cells * gui.char_width * PANGO_SCALE;
5132 glyph->geometry.x_offset +=
5133 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 else /* i == 0 "cannot happen" */
5136 {
5137 glyph->geometry.width = 0;
5138 }
5139 }
5140
5141 /*** Aaaaand action! ***/
5142 draw_glyph_string(row, col + column_offset, item_cells,
5143 flags, item->analysis.font, glyphs);
5144
5145 pango_item_free(item);
5146
5147 column_offset += item_cells;
5148 }
5149
5150 pango_attr_list_unref(attr_list);
5151 }
5152
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005153skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005154 /* Draw underline and undercurl. */
5155 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156
5157 pango_glyph_string_free(glyphs);
5158 vim_free(conv_buf);
5159
5160 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5161
5162 return column_offset;
5163}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164
5165/*
5166 * Return OK if the key with the termcap name "name" is supported.
5167 */
5168 int
5169gui_mch_haskey(char_u *name)
5170{
5171 int i;
5172
5173 for (i = 0; special_keys[i].key_sym != 0; i++)
5174 if (name[0] == special_keys[i].code0
5175 && name[1] == special_keys[i].code1)
5176 return OK;
5177 return FAIL;
5178}
5179
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005180#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
5182 * Return the text window-id and display. Only required for X-based GUI's
5183 */
5184 int
5185gui_get_x11_windis(Window *win, Display **dis)
5186{
5187 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5188 {
5189 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5190 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5191 return OK;
5192 }
5193
5194 *dis = NULL;
5195 *win = 0;
5196 return FAIL;
5197}
5198#endif
5199
5200#if defined(FEAT_CLIENTSERVER) \
5201 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5202
5203 Display *
5204gui_mch_get_display(void)
5205{
5206 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5207 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5208 else
5209 return NULL;
5210}
5211#endif
5212
5213 void
5214gui_mch_beep(void)
5215{
5216#ifdef HAVE_GTK_MULTIHEAD
5217 GdkDisplay *display;
5218
5219 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5220 display = gtk_widget_get_display(gui.mainwin);
5221 else
5222 display = gdk_display_get_default();
5223
5224 if (display != NULL)
5225 gdk_display_beep(display);
5226#else
5227 gdk_beep();
5228#endif
5229}
5230
5231 void
5232gui_mch_flash(int msec)
5233{
5234 GdkGCValues values;
5235 GdkGC *invert_gc;
5236
5237 if (gui.drawarea->window == NULL)
5238 return;
5239
5240 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5241 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5242 values.function = GDK_XOR;
5243 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5244 &values,
5245 GDK_GC_FOREGROUND |
5246 GDK_GC_BACKGROUND |
5247 GDK_GC_FUNCTION);
5248 gdk_gc_set_exposures(invert_gc,
5249 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5250 /*
5251 * Do a visual beep by changing back and forth in some undetermined way,
5252 * the foreground and background colors. This is due to the fact that
5253 * there can't be really any prediction about the effects of XOR on
5254 * arbitrary X11 servers. However this seems to be enough for what we
5255 * intend it to do.
5256 */
5257 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5258 TRUE,
5259 0, 0,
5260 FILL_X((int)Columns) + gui.border_offset,
5261 FILL_Y((int)Rows) + gui.border_offset);
5262
5263 gui_mch_flush();
5264 ui_delay((long)msec, TRUE); /* wait so many msec */
5265
5266 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5267 TRUE,
5268 0, 0,
5269 FILL_X((int)Columns) + gui.border_offset,
5270 FILL_Y((int)Rows) + gui.border_offset);
5271
5272 gdk_gc_destroy(invert_gc);
5273}
5274
5275/*
5276 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5277 */
5278 void
5279gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5280{
5281 GdkGCValues values;
5282 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283
5284 if (gui.drawarea->window == NULL)
5285 return;
5286
Bram Moolenaar89d40322006-08-29 15:30:07 +00005287 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5288 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 values.function = GDK_XOR;
5290 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5291 &values,
5292 GDK_GC_FOREGROUND |
5293 GDK_GC_BACKGROUND |
5294 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005295 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5296 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5298 TRUE,
5299 FILL_X(c), FILL_Y(r),
5300 (nc) * gui.char_width, (nr) * gui.char_height);
5301 gdk_gc_destroy(invert_gc);
5302}
5303
5304/*
5305 * Iconify the GUI window.
5306 */
5307 void
5308gui_mch_iconify(void)
5309{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311}
5312
5313#if defined(FEAT_EVAL) || defined(PROTO)
5314/*
5315 * Bring the Vim window to the foreground.
5316 */
5317 void
5318gui_mch_set_foreground(void)
5319{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321}
5322#endif
5323
5324/*
5325 * Draw a cursor without focus.
5326 */
5327 void
5328gui_mch_draw_hollow_cursor(guicolor_T color)
5329{
5330 int i = 1;
5331
5332 if (gui.drawarea->window == NULL)
5333 return;
5334
5335 gui_mch_set_fg_color(color);
5336
5337 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (mb_lefthalve(gui.row, gui.col))
5339 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5341 FALSE,
5342 FILL_X(gui.col), FILL_Y(gui.row),
5343 i * gui.char_width - 1, gui.char_height - 1);
5344}
5345
5346/*
5347 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5348 * color "color".
5349 */
5350 void
5351gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5352{
5353 if (gui.drawarea->window == NULL)
5354 return;
5355
5356 gui_mch_set_fg_color(color);
5357
5358 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5359 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5360 TRUE,
5361#ifdef FEAT_RIGHTLEFT
5362 /* vertical line should be on the right of current point */
5363 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5364#endif
5365 FILL_X(gui.col),
5366 FILL_Y(gui.row) + gui.char_height - h,
5367 w, h);
5368}
5369
5370
5371/*
5372 * Catch up with any queued X11 events. This may put keyboard input into the
5373 * input buffer, call resize call-backs, trigger timers etc. If there is
5374 * nothing in the X11 event queue (& no timers pending), then we return
5375 * immediately.
5376 */
5377 void
5378gui_mch_update(void)
5379{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005380 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5381 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382}
5383
5384 static gint
5385input_timer_cb(gpointer data)
5386{
5387 int *timed_out = (int *) data;
5388
Bram Moolenaar49325942007-05-10 19:19:59 +00005389 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 *timed_out = TRUE;
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 return FALSE; /* don't happen again */
5393}
5394
5395#ifdef FEAT_SNIFF
5396/*
5397 * Callback function, used when data is available on the SNiFF connection.
5398 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 static void
5400sniff_request_cb(
5401 gpointer data,
5402 gint source_fd,
5403 GdkInputCondition condition)
5404{
5405 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5406
5407 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408}
5409#endif
5410
5411/*
5412 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5413 * from the keyboard.
5414 * wtime == -1 Wait forever.
5415 * wtime == 0 This should never happen.
5416 * wtime > 0 Wait wtime milliseconds for a character.
5417 * Returns OK if a character was found to be available within the given time,
5418 * or FAIL otherwise.
5419 */
5420 int
5421gui_mch_wait_for_chars(long wtime)
5422{
5423 int focus;
5424 guint timer;
5425 static int timed_out;
5426#ifdef FEAT_SNIFF
5427 static int sniff_on = 0;
5428 static gint sniff_input_id = 0;
5429#endif
5430
5431#ifdef FEAT_SNIFF
5432 if (sniff_on && !want_sniff_request)
5433 {
5434 if (sniff_input_id)
5435 gdk_input_remove(sniff_input_id);
5436 sniff_on = 0;
5437 }
5438 else if (!sniff_on && want_sniff_request)
5439 {
5440 /* Add fd_from_sniff to watch for available data in main loop. */
5441 sniff_input_id = gdk_input_add(fd_from_sniff,
5442 GDK_INPUT_READ, sniff_request_cb, NULL);
5443 sniff_on = 1;
5444 }
5445#endif
5446
5447 timed_out = FALSE;
5448
5449 /* this timeout makes sure that we will return if no characters arrived in
5450 * time */
5451
5452 if (wtime > 0)
5453 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5454 else
5455 timer = 0;
5456
5457 focus = gui.in_focus;
5458
5459 do
5460 {
5461 /* Stop or start blinking when focus changes */
5462 if (gui.in_focus != focus)
5463 {
5464 if (gui.in_focus)
5465 gui_mch_start_blink();
5466 else
5467 gui_mch_stop_blink();
5468 focus = gui.in_focus;
5469 }
5470
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005471#if defined(FEAT_NETBEANS_INTG)
Bram Moolenaarede547a2011-02-25 15:15:50 +01005472 /* Process any queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005473 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005474#endif
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476 /*
5477 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005478 * Skip this if input is available anyway (can happen in rare
5479 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005481 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005482 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483
5484 /* Got char, return immediately */
5485 if (input_available())
5486 {
5487 if (timer != 0 && !timed_out)
5488 gtk_timeout_remove(timer);
5489 return OK;
5490 }
5491 } while (wtime < 0 || !timed_out);
5492
5493 /*
5494 * Flush all eventually pending (drawing) events.
5495 */
5496 gui_mch_update();
5497
5498 return FAIL;
5499}
5500
5501
5502/****************************************************************************
5503 * Output drawing routines.
5504 ****************************************************************************/
5505
5506
5507/* Flush any output to the screen */
5508 void
5509gui_mch_flush(void)
5510{
5511#ifdef HAVE_GTK_MULTIHEAD
5512 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5513 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5514#else
5515 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 /* This happens to actually do what gui_mch_flush() is supposed to do,
5518 * according to the comment above. */
5519 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5520 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521}
5522
5523/*
5524 * Clear a rectangular region of the screen from text pos (row1, col1) to
5525 * (row2, col2) inclusive.
5526 */
5527 void
5528gui_mch_clear_block(int row1, int col1, int row2, int col2)
5529{
5530 GdkColor color;
5531
5532 if (gui.drawarea->window == NULL)
5533 return;
5534
5535 color.pixel = gui.back_pixel;
5536
5537 gdk_gc_set_foreground(gui.text_gc, &color);
5538
5539 /* Clear one extra pixel at the far right, for when bold characters have
5540 * spilled over to the window border. */
5541 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5542 FILL_X(col1), FILL_Y(row1),
5543 (col2 - col1 + 1) * gui.char_width
5544 + (col2 == Columns - 1),
5545 (row2 - row1 + 1) * gui.char_height);
5546}
5547
5548 void
5549gui_mch_clear_all(void)
5550{
5551 if (gui.drawarea->window != NULL)
5552 gdk_window_clear(gui.drawarea->window);
5553}
5554
5555/*
5556 * Redraw any text revealed by scrolling up/down.
5557 */
5558 static void
5559check_copy_area(void)
5560{
5561 GdkEvent *event;
5562 int expose_count;
5563
5564 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5565 return;
5566
5567 /* Avoid redrawing the cursor while scrolling or it'll end up where
5568 * we don't want it to be. I'm not sure if it's correct to call
5569 * gui_dont_update_cursor() at this point but it works as a quick
5570 * fix for now. */
5571 gui_dont_update_cursor();
5572
5573 do
5574 {
5575 /* Wait to check whether the scroll worked or not. */
5576 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5577
5578 if (event == NULL)
5579 break; /* received NoExpose event */
5580
5581 gui_redraw(event->expose.area.x, event->expose.area.y,
5582 event->expose.area.width, event->expose.area.height);
5583
5584 expose_count = event->expose.count;
5585 gdk_event_free(event);
5586 }
5587 while (expose_count > 0); /* more events follow */
5588
5589 gui_can_update_cursor();
5590}
5591
5592/*
5593 * Delete the given number of lines from the given row, scrolling up any
5594 * text further down within the scroll region.
5595 */
5596 void
5597gui_mch_delete_lines(int row, int num_lines)
5598{
5599 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5600 return; /* Can't see the window */
5601
5602 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5603 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5604
5605 /* copy one extra pixel, for when bold has spilled over */
5606 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5607 FILL_X(gui.scroll_region_left), FILL_Y(row),
5608 gui.drawarea->window,
5609 FILL_X(gui.scroll_region_left),
5610 FILL_Y(row + num_lines),
5611 gui.char_width * (gui.scroll_region_right
5612 - gui.scroll_region_left + 1) + 1,
5613 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5614
5615 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5616 gui.scroll_region_left,
5617 gui.scroll_region_bot, gui.scroll_region_right);
5618 check_copy_area();
5619}
5620
5621/*
5622 * Insert the given number of lines before the given row, scrolling down any
5623 * following text within the scroll region.
5624 */
5625 void
5626gui_mch_insert_lines(int row, int num_lines)
5627{
5628 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5629 return; /* Can't see the window */
5630
5631 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5632 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5633
5634 /* copy one extra pixel, for when bold has spilled over */
5635 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5636 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5637 gui.drawarea->window,
5638 FILL_X(gui.scroll_region_left), FILL_Y(row),
5639 gui.char_width * (gui.scroll_region_right
5640 - gui.scroll_region_left + 1) + 1,
5641 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5642
5643 gui_clear_block(row, gui.scroll_region_left,
5644 row + num_lines - 1, gui.scroll_region_right);
5645 check_copy_area();
5646}
5647
5648/*
5649 * X Selection stuff, for cutting and pasting text to other windows.
5650 */
5651 void
5652clip_mch_request_selection(VimClipboard *cbd)
5653{
5654 GdkAtom target;
5655 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005656 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
5658 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5659 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005660 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5661 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 received_selection = RS_NONE;
5663 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5664
5665 gtk_selection_convert(gui.drawarea,
5666 cbd->gtk_sel_atom, target,
5667 (guint32)GDK_CURRENT_TIME);
5668
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005669 /* Hack: Wait up to three seconds for the selection. A hang was
5670 * noticed here when using the netrw plugin combined with ":gui"
5671 * during the FocusGained event. */
5672 start = time(NULL);
5673 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005674 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 if (received_selection != RS_FAIL)
5677 return;
5678 }
5679
5680 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005681 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682}
5683
5684/*
5685 * Disown the selection.
5686 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005688clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005690 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692}
5693
5694/*
5695 * Own the selection and return OK if it worked.
5696 */
5697 int
5698clip_mch_own_selection(VimClipboard *cbd)
5699{
5700 int success;
5701
5702 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02005703 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 gui_mch_update();
5705 return (success) ? OK : FAIL;
5706}
5707
5708/*
5709 * Send the current selection to the clipboard. Do nothing for X because we
5710 * will fill in the selection only when requested by another app.
5711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005713clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
5715}
5716
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005717 int
5718clip_gtk_owner_exists(VimClipboard *cbd)
5719{
5720 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
5721}
5722
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724#if defined(FEAT_MENU) || defined(PROTO)
5725/*
5726 * Make a menu item appear either active or not active (grey or not grey).
5727 */
5728 void
5729gui_mch_menu_grey(vimmenu_T *menu, int grey)
5730{
5731 if (menu->id == NULL)
5732 return;
5733
5734 if (menu_is_separator(menu->name))
5735 grey = TRUE;
5736
5737 gui_mch_menu_hidden(menu, FALSE);
5738 /* Be clever about bitfields versus true booleans here! */
5739 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5740 {
5741 gtk_widget_set_sensitive(menu->id, !grey);
5742 gui_mch_update();
5743 }
5744}
5745
5746/*
5747 * Make menu item hidden or not hidden.
5748 */
5749 void
5750gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5751{
5752 if (menu->id == 0)
5753 return;
5754
5755 if (hidden)
5756 {
5757 if (GTK_WIDGET_VISIBLE(menu->id))
5758 {
5759 gtk_widget_hide(menu->id);
5760 gui_mch_update();
5761 }
5762 }
5763 else
5764 {
5765 if (!GTK_WIDGET_VISIBLE(menu->id))
5766 {
5767 gtk_widget_show(menu->id);
5768 gui_mch_update();
5769 }
5770 }
5771}
5772
5773/*
5774 * This is called after setting all the menus to grey/hidden or not.
5775 */
5776 void
5777gui_mch_draw_menubar(void)
5778{
5779 /* just make sure that the visual changes get effect immediately */
5780 gui_mch_update();
5781}
5782#endif /* FEAT_MENU */
5783
5784/*
5785 * Scrollbar stuff.
5786 */
5787 void
5788gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5789{
5790 if (sb->id == NULL)
5791 return;
5792
5793 if (flag)
5794 gtk_widget_show(sb->id);
5795 else
5796 gtk_widget_hide(sb->id);
5797
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005798 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799}
5800
5801
5802/*
5803 * Return the RGB value of a pixel as long.
5804 */
5805 long_u
5806gui_mch_get_rgb(guicolor_T pixel)
5807{
5808 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5810 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
5812 return (((unsigned)color.red & 0xff00) << 8)
5813 | ((unsigned)color.green & 0xff00)
5814 | (((unsigned)color.blue & 0xff00) >> 8);
5815}
5816
5817/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005818 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005820 void
5821gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005823 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824}
5825
5826 void
5827gui_mch_setmouse(int x, int y)
5828{
5829 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5830 * internal GDK mechanism present to accomplish this. (and for good
5831 * reason...) */
5832 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5833 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5834 0, 0, 0U, 0U, x, y);
5835}
5836
5837
5838#ifdef FEAT_MOUSESHAPE
5839/* The last set mouse pointer shape is remembered, to be used when it goes
5840 * from hidden to not hidden. */
5841static int last_shape = 0;
5842#endif
5843
5844/*
5845 * Use the blank mouse pointer or not.
5846 *
5847 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5848 */
5849 void
5850gui_mch_mousehide(int hide)
5851{
5852 if (gui.pointer_hidden != hide)
5853 {
5854 gui.pointer_hidden = hide;
5855 if (gui.drawarea->window && gui.blank_pointer != NULL)
5856 {
5857 if (hide)
5858 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5859 else
5860#ifdef FEAT_MOUSESHAPE
5861 mch_set_mouse_shape(last_shape);
5862#else
5863 gdk_window_set_cursor(gui.drawarea->window, NULL);
5864#endif
5865 }
5866 }
5867}
5868
5869#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5870
5871/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5872 * misc2.c! */
5873static const int mshape_ids[] =
5874{
5875 GDK_LEFT_PTR, /* arrow */
5876 GDK_CURSOR_IS_PIXMAP, /* blank */
5877 GDK_XTERM, /* beam */
5878 GDK_SB_V_DOUBLE_ARROW, /* updown */
5879 GDK_SIZING, /* udsizing */
5880 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5881 GDK_SIZING, /* lrsizing */
5882 GDK_WATCH, /* busy */
5883 GDK_X_CURSOR, /* no */
5884 GDK_CROSSHAIR, /* crosshair */
5885 GDK_HAND1, /* hand1 */
5886 GDK_HAND2, /* hand2 */
5887 GDK_PENCIL, /* pencil */
5888 GDK_QUESTION_ARROW, /* question */
5889 GDK_RIGHT_PTR, /* right-arrow */
5890 GDK_CENTER_PTR, /* up-arrow */
5891 GDK_LEFT_PTR /* last one */
5892};
5893
5894 void
5895mch_set_mouse_shape(int shape)
5896{
5897 int id;
5898 GdkCursor *c;
5899
5900 if (gui.drawarea->window == NULL)
5901 return;
5902
5903 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5904 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5905 else
5906 {
5907 if (shape >= MSHAPE_NUMBERED)
5908 {
5909 id = shape - MSHAPE_NUMBERED;
5910 if (id >= GDK_LAST_CURSOR)
5911 id = GDK_LEFT_PTR;
5912 else
5913 id &= ~1; /* they are always even (why?) */
5914 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005915 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005917 else
5918 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919# ifdef HAVE_GTK_MULTIHEAD
5920 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005921 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005923 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924# endif
5925 gdk_window_set_cursor(gui.drawarea->window, c);
5926 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5927 }
5928 if (shape != MSHAPE_HIDE)
5929 last_shape = shape;
5930}
5931#endif /* FEAT_MOUSESHAPE */
5932
5933
5934#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5935/*
5936 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5937 * scaled down if the current font is not big enough, or scaled up if the image
5938 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5939 * will be cut off if the current font is not big enough, or centered if it's
5940 * too small.
5941 */
5942# define SIGN_WIDTH (2 * gui.char_width)
5943# define SIGN_HEIGHT (gui.char_height)
5944# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5945
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 void
5947gui_mch_drawsign(int row, int col, int typenr)
5948{
5949 GdkPixbuf *sign;
5950
5951 sign = (GdkPixbuf *)sign_get_image(typenr);
5952
5953 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5954 {
5955 int width;
5956 int height;
5957 int xoffset;
5958 int yoffset;
5959 int need_scale;
5960
5961 width = gdk_pixbuf_get_width(sign);
5962 height = gdk_pixbuf_get_height(sign);
5963 /*
5964 * Decide whether we need to scale. Allow one pixel of border
5965 * width to be cut off, in order to avoid excessive scaling for
5966 * tiny differences in font size.
5967 */
5968 need_scale = (width > SIGN_WIDTH + 2
5969 || height > SIGN_HEIGHT + 2
5970 || (width < 3 * SIGN_WIDTH / 4
5971 && height < 3 * SIGN_HEIGHT / 4));
5972 if (need_scale)
5973 {
5974 double aspect;
5975
5976 /* Keep the original aspect ratio */
5977 aspect = (double)height / (double)width;
5978 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5979 width = MIN(width, SIGN_WIDTH);
5980 height = (double)width * aspect;
5981
5982 /* This doesn't seem to be worth caching, and doing so
5983 * would complicate the code quite a bit. */
5984 sign = gdk_pixbuf_scale_simple(sign, width, height,
5985 GDK_INTERP_BILINEAR);
5986 if (sign == NULL)
5987 return; /* out of memory */
5988 }
5989
5990 /* The origin is the upper-left corner of the pixmap. Therefore
5991 * these offset may become negative if the pixmap is smaller than
5992 * the 2x1 cells reserved for the sign icon. */
5993 xoffset = (width - SIGN_WIDTH) / 2;
5994 yoffset = (height - SIGN_HEIGHT) / 2;
5995
5996 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5997
5998 gdk_draw_rectangle(gui.drawarea->window,
5999 gui.text_gc,
6000 TRUE,
6001 FILL_X(col),
6002 FILL_Y(row),
6003 SIGN_WIDTH,
6004 SIGN_HEIGHT);
6005
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 gdk_pixbuf_render_to_drawable_alpha(sign,
6007 gui.drawarea->window,
6008 MAX(0, xoffset),
6009 MAX(0, yoffset),
6010 FILL_X(col) - MIN(0, xoffset),
6011 FILL_Y(row) - MIN(0, yoffset),
6012 MIN(width, SIGN_WIDTH),
6013 MIN(height, SIGN_HEIGHT),
6014 GDK_PIXBUF_ALPHA_BILEVEL,
6015 127,
6016 GDK_RGB_DITHER_NORMAL,
6017 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 if (need_scale)
6019 g_object_unref(sign);
6020 }
6021}
6022
6023 void *
6024gui_mch_register_sign(char_u *signfile)
6025{
6026 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6027 {
6028 GdkPixbuf *sign;
6029 GError *error = NULL;
6030 char_u *message;
6031
6032 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6033
6034 if (error == NULL)
6035 return sign;
6036
6037 message = (char_u *)error->message;
6038
6039 if (message != NULL && input_conv.vc_type != CONV_NONE)
6040 message = string_convert(&input_conv, message, NULL);
6041
6042 if (message != NULL)
6043 {
6044 /* The error message is already translated and will be more
6045 * descriptive than anything we could possibly do ourselves. */
6046 EMSG2("E255: %s", message);
6047
6048 if (input_conv.vc_type != CONV_NONE)
6049 vim_free(message);
6050 }
6051 g_error_free(error);
6052 }
6053
6054 return NULL;
6055}
6056
6057 void
6058gui_mch_destroy_sign(void *sign)
6059{
6060 if (sign != NULL)
6061 g_object_unref(sign);
6062}
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064#endif /* FEAT_SIGN_ICONS */