blob: 05295bf08486b1ccb38b168cf8dce36e717b23d3 [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)
735 gtk_timeout_remove(blink_timer);
736 /* Only switch blinking on if none of the times is zero */
737 if (blink_waittime && blink_ontime && blink_offtime && gui.in_focus)
738 {
739 blink_timer = gtk_timeout_add((guint32)blink_waittime,
740 (GtkFunction) blink_cb, NULL);
741 blink_state = BLINK_ON;
742 gui_update_cursor(TRUE, FALSE);
743 }
744}
745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000747enter_notify_event(GtkWidget *widget UNUSED,
748 GdkEventCrossing *event UNUSED,
749 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 if (blink_state == BLINK_NONE)
752 gui_mch_start_blink();
753
754 /* make sure keyboard input goes there */
755 if (gtk_socket_id == 0 || !GTK_WIDGET_HAS_FOCUS(gui.drawarea))
756 gtk_widget_grab_focus(gui.drawarea);
757
758 return FALSE;
759}
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000762leave_notify_event(GtkWidget *widget UNUSED,
763 GdkEventCrossing *event UNUSED,
764 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765{
766 if (blink_state != BLINK_NONE)
767 gui_mch_stop_blink();
768
769 return FALSE;
770}
771
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000773focus_in_event(GtkWidget *widget,
774 GdkEventFocus *event UNUSED,
775 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776{
777 gui_focus_change(TRUE);
778
779 if (blink_state == BLINK_NONE)
780 gui_mch_start_blink();
781
Bram Moolenaar9c8791f2007-09-05 19:47:23 +0000782 /* make sure keyboard input goes to the draw area (if this is focus for a
783 * window) */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +0000784 if (widget != gui.drawarea)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000785 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
787 return TRUE;
788}
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000791focus_out_event(GtkWidget *widget UNUSED,
Bram Moolenaarcc448b32010-07-14 16:52:17 +0200792 GdkEventFocus *event UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000793 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794{
795 gui_focus_change(FALSE);
796
797 if (blink_state != BLINK_NONE)
798 gui_mch_stop_blink();
799
800 return TRUE;
801}
802
803
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804/*
805 * Translate a GDK key value to UTF-8 independently of the current locale.
806 * The output is written to string, which must have room for at least 6 bytes
807 * plus the NUL terminator. Returns the length in bytes.
808 *
809 * This function is used in the GTK+ 2 GUI only. The GTK+ 1 code makes use
810 * of GdkEventKey::string instead. But event->string is evil; see here why:
811 * http://developer.gnome.org/doc/API/2.0/gdk/gdk-Event-Structures.html#GdkEventKey
812 */
813 static int
814keyval_to_string(unsigned int keyval, unsigned int state, char_u *string)
815{
816 int len;
817 guint32 uc;
818
819 uc = gdk_keyval_to_unicode(keyval);
820 if (uc != 0)
821 {
822 /* Check for CTRL-foo */
823 if ((state & GDK_CONTROL_MASK) && uc >= 0x20 && uc < 0x80)
824 {
825 /* These mappings look arbitrary at the first glance, but in fact
826 * resemble quite exactly the behaviour of the GTK+ 1.2 GUI on my
827 * machine. The only difference is BS vs. DEL for CTRL-8 (makes
828 * more sense and is consistent with usual terminal behaviour). */
829 if (uc >= '@')
830 string[0] = uc & 0x1F;
831 else if (uc == '2')
832 string[0] = NUL;
833 else if (uc >= '3' && uc <= '7')
834 string[0] = uc ^ 0x28;
835 else if (uc == '8')
836 string[0] = BS;
837 else if (uc == '?')
838 string[0] = DEL;
839 else
840 string[0] = uc;
841 len = 1;
842 }
843 else
844 {
845 /* Translate a normal key to UTF-8. This doesn't work for dead
846 * keys of course, you _have_ to use an input method for that. */
847 len = utf_char2bytes((int)uc, string);
848 }
849 }
850 else
851 {
852 /* Translate keys which are represented by ASCII control codes in Vim.
853 * There are only a few of those; most control keys are translated to
854 * special terminal-like control sequences. */
855 len = 1;
856 switch (keyval)
857 {
858 case GDK_Tab: case GDK_KP_Tab: case GDK_ISO_Left_Tab:
859 string[0] = TAB;
860 break;
861 case GDK_Linefeed:
862 string[0] = NL;
863 break;
864 case GDK_Return: case GDK_ISO_Enter: case GDK_3270_Enter:
865 string[0] = CAR;
866 break;
867 case GDK_Escape:
868 string[0] = ESC;
869 break;
870 default:
871 len = 0;
872 break;
873 }
874 }
875 string[len] = NUL;
876
877 return len;
878}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000880 static int
881modifiers_gdk2vim(guint state)
882{
883 int modifiers = 0;
884
885 if (state & GDK_SHIFT_MASK)
886 modifiers |= MOD_MASK_SHIFT;
887 if (state & GDK_CONTROL_MASK)
888 modifiers |= MOD_MASK_CTRL;
889 if (state & GDK_MOD1_MASK)
890 modifiers |= MOD_MASK_ALT;
Bram Moolenaar580061a2010-08-13 13:57:13 +0200891#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +0200892 if (state & GDK_SUPER_MASK)
893 modifiers |= MOD_MASK_META;
894#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000895 if (state & GDK_MOD4_MASK)
896 modifiers |= MOD_MASK_META;
897
898 return modifiers;
899}
900
901 static int
902modifiers_gdk2mouse(guint state)
903{
904 int modifiers = 0;
905
906 if (state & GDK_SHIFT_MASK)
907 modifiers |= MOUSE_SHIFT;
908 if (state & GDK_CONTROL_MASK)
909 modifiers |= MOUSE_CTRL;
910 if (state & GDK_MOD1_MASK)
911 modifiers |= MOUSE_ALT;
912
913 return modifiers;
914}
915
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916/*
917 * Main keyboard handler:
918 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000920key_press_event(GtkWidget *widget UNUSED,
921 GdkEventKey *event,
922 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923{
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200924 /* For GTK+ 2 we know for sure how large the string might get.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
926 char_u string[32], string2[32];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 guint key_sym;
928 int len;
929 int i;
930 int modifiers;
931 int key;
932 guint state;
933 char_u *s, *d;
934
Bram Moolenaar20892c12011-06-26 04:49:00 +0200935 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 key_sym = event->keyval;
937 state = event->state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938
939#ifdef FEAT_XIM
940 if (xim_queue_key_press_event(event, TRUE))
941 return TRUE;
942#endif
943
944#ifdef FEAT_HANGULIN
945 if (key_sym == GDK_space && (state & GDK_SHIFT_MASK))
946 {
947 hangul_input_state_toggle();
948 return TRUE;
949 }
950#endif
951
952#ifdef SunXK_F36
953 /*
954 * These keys have bogus lookup strings, and trapping them here is
955 * easier than trying to XRebindKeysym() on them with every possible
956 * combination of modifiers.
957 */
958 if (key_sym == SunXK_F36 || key_sym == SunXK_F37)
959 len = 0;
960 else
961#endif
962 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 len = keyval_to_string(key_sym, state, string2);
964
965 /* Careful: convert_input() doesn't handle the NUL character.
966 * No need to convert pure ASCII anyway, thus the len > 1 check. */
967 if (len > 1 && input_conv.vc_type != CONV_NONE)
968 len = convert_input(string2, len, sizeof(string2));
969
970 s = string2;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 d = string;
972 for (i = 0; i < len; ++i)
973 {
974 *d++ = s[i];
975 if (d[-1] == CSI && d + 2 < string + sizeof(string))
976 {
977 /* Turn CSI into K_CSI. */
978 *d++ = KS_EXTRA;
979 *d++ = (int)KE_CSI;
980 }
981 }
982 len = d - string;
983 }
984
985 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
986 if (key_sym == GDK_ISO_Left_Tab)
987 {
988 key_sym = GDK_Tab;
989 state |= GDK_SHIFT_MASK;
990 }
991
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992#ifdef FEAT_MENU
993 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
994 * is a menu shortcut, we ignore everything with the ALT modifier. */
995 if ((state & GDK_MOD1_MASK)
996 && gui.menu_is_active
997 && (*p_wak == 'y'
998 || (*p_wak == 'm'
999 && len == 1
1000 && gui_is_menu_shortcut(string[0]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /* For GTK2 we return false to signify that we haven't handled the
1002 * keypress, so that gtk will handle the mnemonic or accelerator. */
1003 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1007 * that already has the 8th bit set.
1008 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1009 * Don't do this for double-byte encodings, it turns the char into a lead
1010 * byte. */
1011 if (len == 1
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001012 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001013#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001014 || (state & GDK_SUPER_MASK)
1015#endif
1016 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 && !(key_sym == GDK_BackSpace || key_sym == GDK_Delete)
1018 && (string[0] & 0x80) == 0
1019 && !(key_sym == GDK_Tab && (state & GDK_SHIFT_MASK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 && !enc_dbcs
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 )
1022 {
1023 string[0] |= 0x80;
1024 state &= ~GDK_MOD1_MASK; /* don't use it again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (enc_utf8) /* convert to utf-8 */
1026 {
1027 string[1] = string[0] & 0xbf;
1028 string[0] = ((unsigned)string[0] >> 6) + 0xc0;
1029 if (string[1] == CSI)
1030 {
1031 string[2] = KS_EXTRA;
1032 string[3] = (int)KE_CSI;
1033 len = 4;
1034 }
1035 else
1036 len = 2;
1037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 }
1039
1040 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1041 * value) to detect backspace, delete and keypad keys. */
1042 if (len == 0 || len == 1)
1043 {
1044 for (i = 0; special_keys[i].key_sym != 0; i++)
1045 {
1046 if (special_keys[i].key_sym == key_sym)
1047 {
1048 string[0] = CSI;
1049 string[1] = special_keys[i].code0;
1050 string[2] = special_keys[i].code1;
1051 len = -3;
1052 break;
1053 }
1054 }
1055 }
1056
1057 if (len == 0) /* Unrecognized key */
1058 return TRUE;
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /* Special keys (and a few others) may have modifiers. Also when using a
1061 * double-byte encoding (can't set the 8th bit). */
1062 if (len == -3 || key_sym == GDK_space || key_sym == GDK_Tab
1063 || key_sym == GDK_Return || key_sym == GDK_Linefeed
1064 || key_sym == GDK_Escape || key_sym == GDK_KP_Tab
1065 || key_sym == GDK_ISO_Enter || key_sym == GDK_3270_Enter
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001066 || (enc_dbcs && len == 1 && ((state & GDK_MOD1_MASK)
Bram Moolenaar35f330c2010-08-15 13:53:58 +02001067#if GTK_CHECK_VERSION(2,10,0)
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001068 || (state & GDK_SUPER_MASK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069#endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001070 )))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001072 modifiers = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
1074 /*
1075 * For some keys a shift modifier is translated into another key
1076 * code.
1077 */
1078 if (len == -3)
1079 key = TO_SPECIAL(string[1], string[2]);
1080 else
1081 key = string[0];
1082
1083 key = simplify_key(key, &modifiers);
1084 if (key == CSI)
1085 key = K_CSI;
1086 if (IS_SPECIAL(key))
1087 {
1088 string[0] = CSI;
1089 string[1] = K_SECOND(key);
1090 string[2] = K_THIRD(key);
1091 len = 3;
1092 }
1093 else
1094 {
1095 string[0] = key;
1096 len = 1;
1097 }
1098
1099 if (modifiers != 0)
1100 {
1101 string2[0] = CSI;
1102 string2[1] = KS_MODIFIER;
1103 string2[2] = modifiers;
1104 add_to_input_buf(string2, 3);
1105 }
1106 }
1107
1108 if (len == 1 && ((string[0] == Ctrl_C && ctrl_c_interrupts)
1109 || (string[0] == intr_char && intr_char != Ctrl_C)))
1110 {
1111 trash_input_buf();
1112 got_int = TRUE;
1113 }
1114
1115 add_to_input_buf(string, len);
1116
1117 /* blank out the pointer if necessary */
1118 if (p_mh)
1119 gui_mch_mousehide(TRUE);
1120
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 return TRUE;
1122}
1123
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001124#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001126key_release_event(GtkWidget *widget UNUSED,
1127 GdkEventKey *event,
1128 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129{
Bram Moolenaar20892c12011-06-26 04:49:00 +02001130 gui.event_time = event->time;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 /*
1132 * GTK+ 2 input methods may do fancy stuff on key release events too.
1133 * With the default IM for instance, you can enter any UCS code point
1134 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1135 */
1136 return xim_queue_key_press_event(event, FALSE);
1137}
1138#endif
1139
1140
1141/****************************************************************************
1142 * Selection handlers:
1143 */
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001146selection_clear_event(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 GdkEventSelection *event,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001148 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149{
1150 if (event->selection == clip_plus.gtk_sel_atom)
1151 clip_lose_selection(&clip_plus);
1152 else
1153 clip_lose_selection(&clip_star);
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 return TRUE;
1156}
1157
1158#define RS_NONE 0 /* selection_received_cb() not called yet */
1159#define RS_OK 1 /* selection_received_cb() called and OK */
1160#define RS_FAIL 2 /* selection_received_cb() called and failed */
1161static int received_selection = RS_NONE;
1162
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001164selection_received_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 GtkSelectionData *data,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001166 guint time_ UNUSED,
1167 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168{
1169 VimClipboard *cbd;
1170 char_u *text;
1171 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 guchar *tmpbuf_utf8 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 int len;
Bram Moolenaard44347f2011-06-19 01:14:29 +02001174 int motion_type = MAUTO;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175
1176 if (data->selection == clip_plus.gtk_sel_atom)
1177 cbd = &clip_plus;
1178 else
1179 cbd = &clip_star;
1180
1181 text = (char_u *)data->data;
1182 len = data->length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
1184 if (text == NULL || len <= 0)
1185 {
1186 received_selection = RS_FAIL;
1187 /* clip_free_selection(cbd); ??? */
1188
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 return;
1190 }
1191
1192 if (data->type == vim_atom)
1193 {
1194 motion_type = *text++;
1195 --len;
1196 }
1197
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 else if (data->type == vimenc_atom)
1199 {
1200 char_u *enc;
1201 vimconv_T conv;
1202
1203 motion_type = *text++;
1204 --len;
1205
1206 enc = text;
1207 text += STRLEN(text) + 1;
1208 len -= text - enc;
1209
1210 /* If the encoding of the text is different from 'encoding', attempt
1211 * converting it. */
1212 conv.vc_type = CONV_NONE;
1213 convert_setup(&conv, enc, p_enc);
1214 if (conv.vc_type != CONV_NONE)
1215 {
1216 tmpbuf = string_convert(&conv, text, &len);
1217 if (tmpbuf != NULL)
1218 text = tmpbuf;
1219 convert_setup(&conv, NULL, NULL);
1220 }
1221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 /* gtk_selection_data_get_text() handles all the nasty details
1224 * and targets and encodings etc. This rocks so hard. */
1225 else
1226 {
1227 tmpbuf_utf8 = gtk_selection_data_get_text(data);
1228 if (tmpbuf_utf8 != NULL)
1229 {
1230 len = STRLEN(tmpbuf_utf8);
1231 if (input_conv.vc_type != CONV_NONE)
1232 {
1233 tmpbuf = string_convert(&input_conv, tmpbuf_utf8, &len);
1234 if (tmpbuf != NULL)
1235 text = tmpbuf;
1236 }
1237 else
1238 text = tmpbuf_utf8;
1239 }
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001240 else if (len >= 2 && text[0] == 0xff && text[1] == 0xfe)
1241 {
1242 vimconv_T conv;
1243
1244 /* UTF-16, we get this for HTML */
1245 conv.vc_type = CONV_NONE;
1246 convert_setup_ext(&conv, (char_u *)"utf-16le", FALSE, p_enc, TRUE);
1247
1248 if (conv.vc_type != CONV_NONE)
1249 {
1250 text += 2;
1251 len -= 2;
1252 tmpbuf = string_convert(&conv, text, &len);
1253 convert_setup(&conv, NULL, NULL);
1254 }
1255 if (tmpbuf != NULL)
1256 text = tmpbuf;
1257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001260 /* Chop off any trailing NUL bytes. OpenOffice sends these. */
Bram Moolenaara76638f2010-06-05 12:49:46 +02001261 while (len > 0 && text[len - 1] == NUL)
1262 --len;
1263
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 clip_yank_selection(motion_type, text, (long)len, cbd);
1265 received_selection = RS_OK;
1266 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 g_free(tmpbuf_utf8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268}
1269
1270/*
1271 * Prepare our selection data for passing it to the external selection
1272 * client.
1273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001275selection_get_cb(GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 GtkSelectionData *selection_data,
1277 guint info,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001278 guint time_ UNUSED,
1279 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280{
1281 char_u *string;
1282 char_u *tmpbuf;
1283 long_u tmplen;
1284 int length;
1285 int motion_type;
1286 GdkAtom type;
1287 VimClipboard *cbd;
1288
1289 if (selection_data->selection == clip_plus.gtk_sel_atom)
1290 cbd = &clip_plus;
1291 else
1292 cbd = &clip_star;
1293
1294 if (!cbd->owned)
1295 return; /* Shouldn't ever happen */
1296
1297 if (info != (guint)TARGET_STRING
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001298 && (!clip_html || info != (guint)TARGET_HTML)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 && info != (guint)TARGET_UTF8_STRING
1300 && info != (guint)TARGET_VIMENC
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 && info != (guint)TARGET_VIM
1302 && info != (guint)TARGET_COMPOUND_TEXT
1303 && info != (guint)TARGET_TEXT)
1304 return;
1305
1306 /* get the selection from the '*'/'+' register */
1307 clip_get_selection(cbd);
1308
1309 motion_type = clip_convert_selection(&string, &tmplen, cbd);
1310 if (motion_type < 0 || string == NULL)
1311 return;
1312 /* Due to int arguments we can't handle more than G_MAXINT. Also
1313 * reserve one extra byte for NUL or the motion type; just in case.
1314 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1315 length = MIN(tmplen, (long_u)(G_MAXINT - 1));
1316
1317 if (info == (guint)TARGET_VIM)
1318 {
1319 tmpbuf = alloc((unsigned)length + 1);
1320 if (tmpbuf != NULL)
1321 {
1322 tmpbuf[0] = motion_type;
1323 mch_memmove(tmpbuf + 1, string, (size_t)length);
1324 }
1325 /* For our own format, the first byte contains the motion type */
1326 ++length;
1327 vim_free(string);
1328 string = tmpbuf;
1329 type = vim_atom;
1330 }
1331
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00001332 else if (info == (guint)TARGET_HTML)
1333 {
1334 vimconv_T conv;
1335
1336 /* Since we get utf-16, we probably should set it as well. */
1337 conv.vc_type = CONV_NONE;
1338 convert_setup_ext(&conv, p_enc, TRUE, (char_u *)"utf-16le", FALSE);
1339 if (conv.vc_type != CONV_NONE)
1340 {
1341 tmpbuf = string_convert(&conv, string, &length);
1342 convert_setup(&conv, NULL, NULL);
1343 vim_free(string);
1344 string = tmpbuf;
1345 }
1346
1347 /* Prepend the BOM: "fffe" */
1348 if (string != NULL)
1349 {
1350 tmpbuf = alloc(length + 2);
1351 tmpbuf[0] = 0xff;
1352 tmpbuf[1] = 0xfe;
1353 mch_memmove(tmpbuf + 2, string, (size_t)length);
1354 vim_free(string);
1355 string = tmpbuf;
1356 length += 2;
1357
1358 selection_data->type = selection_data->target;
1359 selection_data->format = 16; /* 16 bits per char */
1360 gtk_selection_data_set(selection_data, html_atom, 16,
1361 string, length);
1362 vim_free(string);
1363 }
1364 return;
1365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 else if (info == (guint)TARGET_VIMENC)
1367 {
1368 int l = STRLEN(p_enc);
1369
1370 /* contents: motion_type 'encoding' NUL text */
1371 tmpbuf = alloc((unsigned)length + l + 2);
1372 if (tmpbuf != NULL)
1373 {
1374 tmpbuf[0] = motion_type;
1375 STRCPY(tmpbuf + 1, p_enc);
1376 mch_memmove(tmpbuf + l + 2, string, (size_t)length);
1377 }
1378 length += l + 2;
1379 vim_free(string);
1380 string = tmpbuf;
1381 type = vimenc_atom;
1382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 /* gtk_selection_data_set_text() handles everything for us. This is
1385 * so easy and simple and cool, it'd be insane not to use it. */
1386 else
1387 {
1388 if (output_conv.vc_type != CONV_NONE)
1389 {
1390 tmpbuf = string_convert(&output_conv, string, &length);
1391 vim_free(string);
1392 if (tmpbuf == NULL)
1393 return;
1394 string = tmpbuf;
1395 }
1396 /* Validate the string to avoid runtime warnings */
1397 if (g_utf8_validate((const char *)string, (gssize)length, NULL))
1398 {
1399 gtk_selection_data_set_text(selection_data,
1400 (const char *)string, length);
1401 }
1402 vim_free(string);
1403 return;
1404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 if (string != NULL)
1407 {
1408 selection_data->type = selection_data->target;
1409 selection_data->format = 8; /* 8 bits per char */
1410
1411 gtk_selection_data_set(selection_data, type, 8, string, length);
1412 vim_free(string);
1413 }
1414}
1415
1416/*
Bram Moolenaar29695702012-05-18 17:03:18 +02001417 * Check if the GUI can be started. Called before gvimrc is sourced and
1418 * before fork().
1419 * Return OK or FAIL.
1420 */
1421 int
1422gui_mch_early_init_check(void)
1423{
1424 char_u *p;
1425
1426 /* Guess that when $DISPLAY isn't set the GUI can't start. */
1427 p = mch_getenv((char_u *)"DISPLAY");
1428 if (p == NULL || *p == NUL)
1429 {
1430 gui.dying = TRUE;
1431 EMSG(_((char *)e_opendisp));
1432 return FAIL;
1433 }
1434 return OK;
1435}
1436
1437/*
1438 * Check if the GUI can be started. Called before gvimrc is sourced but after
1439 * fork().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 * Return OK or FAIL.
1441 */
1442 int
1443gui_mch_init_check(void)
1444{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#ifdef FEAT_GUI_GNOME
1446 if (gtk_socket_id == 0)
1447 using_gnome = 1;
1448#endif
1449
Bram Moolenaar8dd79012013-05-21 12:52:04 +02001450 /* This defaults to argv[0], but we want it to match the name of the
1451 * shipped gvim.desktop so that Vim's windows can be associated with this
1452 * file. */
1453 g_set_prgname("gvim");
1454
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1456 if (!gtk_init_check(&gui_argc, &gui_argv))
1457 {
1458 gui.dying = TRUE;
Bram Moolenaarc93e7912008-07-08 10:46:08 +00001459 EMSG(_((char *)e_opendisp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return FAIL;
1461 }
1462
1463 return OK;
1464}
1465
1466
1467/****************************************************************************
1468 * Mouse handling callbacks
1469 */
1470
1471
1472static guint mouse_click_timer = 0;
1473static int mouse_timed_out = TRUE;
1474
1475/*
1476 * Timer used to recognize multiple clicks of the mouse button
1477 */
1478 static gint
1479mouse_click_timer_cb(gpointer data)
1480{
1481 /* we don't use this information currently */
1482 int *timed_out = (int *) data;
1483
1484 *timed_out = TRUE;
1485 return FALSE; /* don't happen again */
1486}
1487
1488static guint motion_repeat_timer = 0;
1489static int motion_repeat_offset = FALSE;
1490static gint motion_repeat_timer_cb(gpointer);
1491
1492 static void
1493process_motion_notify(int x, int y, GdkModifierType state)
1494{
1495 int button;
1496 int_u vim_modifiers;
1497
1498 button = (state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1499 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1500 GDK_BUTTON5_MASK))
1501 ? MOUSE_DRAG : ' ';
1502
1503 /* If our pointer is currently hidden, then we should show it. */
1504 gui_mch_mousehide(FALSE);
1505
1506 /* Just moving the rodent above the drawing area without any button
1507 * being pressed. */
1508 if (button != MOUSE_DRAG)
1509 {
1510 gui_mouse_moved(x, y);
1511 return;
1512 }
1513
1514 /* translate modifier coding between the main engine and GTK */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001515 vim_modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaar49325942007-05-10 19:19:59 +00001517 /* inform the editor engine about the occurrence of this event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 gui_send_mouse_event(button, x, y, FALSE, vim_modifiers);
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 /*
1521 * Auto repeat timer handling.
1522 */
1523 if (x < 0 || y < 0
1524 || x >= gui.drawarea->allocation.width
1525 || y >= gui.drawarea->allocation.height)
1526 {
1527
1528 int dx;
1529 int dy;
1530 int offshoot;
1531 int delay = 10;
1532
1533 /* Calculate the maximal distance of the cursor from the drawing area.
1534 * (offshoot can't become negative here!).
1535 */
1536 dx = x < 0 ? -x : x - gui.drawarea->allocation.width;
1537 dy = y < 0 ? -y : y - gui.drawarea->allocation.height;
1538
1539 offshoot = dx > dy ? dx : dy;
1540
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001541 /* Make a linearly decaying timer delay with a threshold of 5 at a
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 * distance of 127 pixels from the main window.
1543 *
1544 * One could think endlessly about the most ergonomic variant here.
1545 * For example it could make sense to calculate the distance from the
1546 * drags start instead...
1547 *
1548 * Maybe a parabolic interpolation would suite us better here too...
1549 */
1550 if (offshoot > 127)
1551 {
1552 /* 5 appears to be somehow near to my perceptual limits :-). */
1553 delay = 5;
1554 }
1555 else
1556 {
1557 delay = (130 * (127 - offshoot)) / 127 + 5;
1558 }
1559
1560 /* shoot again */
1561 if (!motion_repeat_timer)
1562 motion_repeat_timer = gtk_timeout_add((guint32)delay,
1563 motion_repeat_timer_cb, NULL);
1564 }
1565}
1566
1567/*
1568 * Timer used to recognize multiple clicks of the mouse button.
1569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001571motion_repeat_timer_cb(gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572{
1573 int x;
1574 int y;
1575 GdkModifierType state;
1576
1577 gdk_window_get_pointer(gui.drawarea->window, &x, &y, &state);
1578
1579 if (!(state & (GDK_BUTTON1_MASK | GDK_BUTTON2_MASK |
1580 GDK_BUTTON3_MASK | GDK_BUTTON4_MASK |
1581 GDK_BUTTON5_MASK)))
1582 {
1583 motion_repeat_timer = 0;
1584 return FALSE;
1585 }
1586
1587 /* If there already is a mouse click in the input buffer, wait another
1588 * time (otherwise we would create a backlog of clicks) */
1589 if (vim_used_in_input_buf() > 10)
1590 return TRUE;
1591
1592 motion_repeat_timer = 0;
1593
1594 /*
1595 * Fake a motion event.
1596 * Trick: Pretend the mouse moved to the next character on every other
1597 * event, otherwise drag events will be discarded, because they are still
1598 * in the same character.
1599 */
1600 if (motion_repeat_offset)
1601 x += gui.char_width;
1602
1603 motion_repeat_offset = !motion_repeat_offset;
1604 process_motion_notify(x, y, state);
1605
1606 /* Don't happen again. We will get reinstalled in the synthetic event
1607 * if needed -- thus repeating should still work. */
1608 return FALSE;
1609}
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001612motion_notify_event(GtkWidget *widget,
1613 GdkEventMotion *event,
1614 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 if (event->is_hint)
1617 {
1618 int x;
1619 int y;
1620 GdkModifierType state;
1621
1622 gdk_window_get_pointer(widget->window, &x, &y, &state);
1623 process_motion_notify(x, y, state);
1624 }
1625 else
1626 {
1627 process_motion_notify((int)event->x, (int)event->y,
1628 (GdkModifierType)event->state);
1629 }
1630
1631 return TRUE; /* handled */
1632}
1633
1634
1635/*
1636 * Mouse button handling. Note please that we are capturing multiple click's
1637 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1638 * This is due to the way the generic VIM code is recognizing multiple clicks.
1639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001641button_press_event(GtkWidget *widget,
1642 GdkEventButton *event,
1643 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644{
1645 int button;
1646 int repeated_click = FALSE;
1647 int x, y;
1648 int_u vim_modifiers;
1649
Bram Moolenaar20892c12011-06-26 04:49:00 +02001650 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 /* Make sure we have focus now we've been selected */
1653 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1654 gtk_widget_grab_focus(widget);
1655
1656 /*
1657 * Don't let additional events about multiple clicks send by GTK to us
1658 * after the initial button press event confuse us.
1659 */
1660 if (event->type != GDK_BUTTON_PRESS)
1661 return FALSE;
1662
1663 x = event->x;
1664 y = event->y;
1665
1666 /* Handle multiple clicks */
1667 if (!mouse_timed_out && mouse_click_timer)
1668 {
1669 gtk_timeout_remove(mouse_click_timer);
1670 mouse_click_timer = 0;
1671 repeated_click = TRUE;
1672 }
1673
1674 mouse_timed_out = FALSE;
1675 mouse_click_timer = gtk_timeout_add((guint32)p_mouset,
1676 mouse_click_timer_cb, &mouse_timed_out);
1677
1678 switch (event->button)
1679 {
1680 case 1:
1681 button = MOUSE_LEFT;
1682 break;
1683 case 2:
1684 button = MOUSE_MIDDLE;
1685 break;
1686 case 3:
1687 button = MOUSE_RIGHT;
1688 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 default:
1690 return FALSE; /* Unknown button */
1691 }
1692
1693#ifdef FEAT_XIM
1694 /* cancel any preediting */
1695 if (im_is_preediting())
1696 xim_reset();
1697#endif
1698
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001699 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
1701 gui_send_mouse_event(button, x, y, repeated_click, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702
1703 return TRUE;
1704}
1705
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706/*
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001707 * GTK+ 2 abstracts scrolling via the GdkEventScroll.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001710scroll_event(GtkWidget *widget,
1711 GdkEventScroll *event,
1712 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 int button;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001715 int_u vim_modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716
1717 if (gtk_socket_id != 0 && !GTK_WIDGET_HAS_FOCUS(widget))
1718 gtk_widget_grab_focus(widget);
1719
1720 switch (event->direction)
1721 {
1722 case GDK_SCROLL_UP:
1723 button = MOUSE_4;
1724 break;
1725 case GDK_SCROLL_DOWN:
1726 button = MOUSE_5;
1727 break;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001728 case GDK_SCROLL_LEFT:
1729 button = MOUSE_7;
1730 break;
1731 case GDK_SCROLL_RIGHT:
1732 button = MOUSE_6;
1733 break;
1734 default: /* This shouldn't happen */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 return FALSE;
1736 }
1737
1738# ifdef FEAT_XIM
1739 /* cancel any preediting */
1740 if (im_is_preediting())
1741 xim_reset();
1742# endif
1743
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001744 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745
1746 gui_send_mouse_event(button, (int)event->x, (int)event->y,
1747 FALSE, vim_modifiers);
1748
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 return TRUE;
1750}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001754button_release_event(GtkWidget *widget UNUSED,
1755 GdkEventButton *event,
1756 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757{
1758 int x, y;
1759 int_u vim_modifiers;
1760
Bram Moolenaar20892c12011-06-26 04:49:00 +02001761 gui.event_time = event->time;
Bram Moolenaar7cfea752010-06-22 06:07:12 +02001762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 /* Remove any motion "machine gun" timers used for automatic further
1764 extension of allocation areas if outside of the applications window
1765 area .*/
1766 if (motion_repeat_timer)
1767 {
1768 gtk_timeout_remove(motion_repeat_timer);
1769 motion_repeat_timer = 0;
1770 }
1771
1772 x = event->x;
1773 y = event->y;
1774
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001775 vim_modifiers = modifiers_gdk2mouse(event->state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777 gui_send_mouse_event(MOUSE_RELEASE, x, y, FALSE, vim_modifiers);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
1779 return TRUE;
1780}
1781
1782
1783#ifdef FEAT_DND
1784/****************************************************************************
1785 * Drag aNd Drop support handlers.
1786 */
1787
1788/*
1789 * Count how many items there may be and separate them with a NUL.
1790 * Apparently the items are separated with \r\n. This is not documented,
1791 * thus be careful not to go past the end. Also allow separation with
1792 * NUL characters.
1793 */
1794 static int
1795count_and_decode_uri_list(char_u *out, char_u *raw, int len)
1796{
1797 int i;
1798 char_u *p = out;
1799 int count = 0;
1800
1801 for (i = 0; i < len; ++i)
1802 {
1803 if (raw[i] == NUL || raw[i] == '\n' || raw[i] == '\r')
1804 {
1805 if (p > out && p[-1] != NUL)
1806 {
1807 ++count;
1808 *p++ = NUL;
1809 }
1810 }
1811 else if (raw[i] == '%' && i + 2 < len && hexhex2nr(raw + i + 1) > 0)
1812 {
1813 *p++ = hexhex2nr(raw + i + 1);
1814 i += 2;
1815 }
1816 else
1817 *p++ = raw[i];
1818 }
1819 if (p > out && p[-1] != NUL)
1820 {
1821 *p = NUL; /* last item didn't have \r or \n */
1822 ++count;
1823 }
1824 return count;
1825}
1826
1827/*
1828 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
1829 * this process, URI which protocol is not "file:" are removed. Return
1830 * length of array (less than "max").
1831 */
1832 static int
1833filter_uri_list(char_u **outlist, int max, char_u *src)
1834{
1835 int i, j;
1836
1837 for (i = j = 0; i < max; ++i)
1838 {
1839 outlist[i] = NULL;
1840 if (STRNCMP(src, "file:", 5) == 0)
1841 {
1842 src += 5;
1843 if (STRNCMP(src, "//localhost", 11) == 0)
1844 src += 11;
1845 while (src[0] == '/' && src[1] == '/')
1846 ++src;
1847 outlist[j++] = vim_strsave(src);
1848 }
1849 src += STRLEN(src) + 1;
1850 }
1851 return j;
1852}
1853
1854 static char_u **
1855parse_uri_list(int *count, char_u *data, int len)
1856{
1857 int n = 0;
1858 char_u *tmp = NULL;
1859 char_u **array = NULL;;
1860
1861 if (data != NULL && len > 0 && (tmp = (char_u *)alloc(len + 1)) != NULL)
1862 {
1863 n = count_and_decode_uri_list(tmp, data, len);
1864 if (n > 0 && (array = (char_u **)alloc(n * sizeof(char_u *))) != NULL)
1865 n = filter_uri_list(array, n, tmp);
1866 }
1867 vim_free(tmp);
1868 *count = n;
1869 return array;
1870}
1871
1872 static void
1873drag_handle_uri_list(GdkDragContext *context,
1874 GtkSelectionData *data,
1875 guint time_,
1876 GdkModifierType state,
1877 gint x,
1878 gint y)
1879{
1880 char_u **fnames;
1881 int nfiles = 0;
1882
1883 fnames = parse_uri_list(&nfiles, data->data, data->length);
1884
1885 if (fnames != NULL && nfiles > 0)
1886 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001887 int_u modifiers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
1890
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001891 modifiers = modifiers_gdk2mouse(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 gui_handle_drop(x, y, modifiers, fnames, nfiles);
1894 }
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001895 else
1896 vim_free(fnames);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897}
1898
1899 static void
1900drag_handle_text(GdkDragContext *context,
1901 GtkSelectionData *data,
1902 guint time_,
1903 GdkModifierType state)
1904{
1905 char_u dropkey[6] = {CSI, KS_MODIFIER, 0, CSI, KS_EXTRA, (char_u)KE_DROP};
1906 char_u *text;
1907 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 char_u *tmpbuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
1910 text = data->data;
1911 len = data->length;
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 if (data->type == utf8_string_atom)
1914 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 if (input_conv.vc_type != CONV_NONE)
1916 tmpbuf = string_convert(&input_conv, text, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (tmpbuf != NULL)
1918 text = tmpbuf;
1919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921 dnd_yank_drag_data(text, (long)len);
1922 gtk_drag_finish(context, TRUE, FALSE, time_); /* accept */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 vim_free(tmpbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001925 dropkey[2] = modifiers_gdk2vim(state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927 if (dropkey[2] != 0)
1928 add_to_input_buf(dropkey, (int)sizeof(dropkey));
1929 else
1930 add_to_input_buf(dropkey + 3, (int)(sizeof(dropkey) - 3));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931}
1932
1933/*
1934 * DND receiver.
1935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 static void
1937drag_data_received_cb(GtkWidget *widget,
1938 GdkDragContext *context,
1939 gint x,
1940 gint y,
1941 GtkSelectionData *data,
1942 guint info,
1943 guint time_,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001944 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945{
1946 GdkModifierType state;
1947
1948 /* Guard against trash */
1949 if (data->data == NULL
1950 || data->length <= 0
1951 || data->format != 8
1952 || data->data[data->length] != '\0')
1953 {
1954 gtk_drag_finish(context, FALSE, FALSE, time_);
1955 return;
1956 }
1957
1958 /* Get the current modifier state for proper distinguishment between
1959 * different operations later. */
1960 gdk_window_get_pointer(widget->window, NULL, NULL, &state);
1961
1962 /* Not sure about the role of "text/plain" here... */
1963 if (info == (guint)TARGET_TEXT_URI_LIST)
1964 drag_handle_uri_list(context, data, time_, state, x, y);
1965 else
1966 drag_handle_text(context, data, time_, state);
1967
1968}
1969#endif /* FEAT_DND */
1970
1971
1972#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
1973/*
1974 * GnomeClient interact callback. Check for unsaved buffers that cannot
1975 * be abandoned and pop up a dialog asking the user for confirmation if
1976 * necessary.
1977 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001979sm_client_check_changed_any(GnomeClient *client UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 gint key,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02001981 GnomeDialogType type UNUSED,
1982 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983{
1984 cmdmod_T save_cmdmod;
1985 gboolean shutdown_cancelled;
1986
1987 save_cmdmod = cmdmod;
1988
1989# ifdef FEAT_BROWSE
1990 cmdmod.browse = TRUE;
1991# endif
1992# if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1993 cmdmod.confirm = TRUE;
1994# endif
1995 /*
1996 * If there are changed buffers, present the user with
1997 * a dialog if possible, otherwise give an error message.
1998 */
1999 shutdown_cancelled = check_changed_any(FALSE);
2000
2001 exiting = FALSE;
2002 cmdmod = save_cmdmod;
2003 setcursor(); /* position the cursor */
2004 out_flush();
2005 /*
2006 * If the user hit the [Cancel] button the whole shutdown
2007 * will be cancelled. Wow, quite powerful feature (:
2008 */
2009 gnome_interaction_key_return(key, shutdown_cancelled);
2010}
2011
2012/*
2013 * Generate a script that can be used to restore the current editing session.
2014 * Save the value of v:this_session before running :mksession in order to make
2015 * automagic session save fully transparent. Return TRUE on success.
2016 */
2017 static int
2018write_session_file(char_u *filename)
2019{
2020 char_u *escaped_filename;
2021 char *mksession_cmdline;
2022 unsigned int save_ssop_flags;
2023 int failed;
2024
2025 /*
2026 * Build an ex command line to create a script that restores the current
2027 * session if executed. Escape the filename to avoid nasty surprises.
2028 */
2029 escaped_filename = vim_strsave_escaped(filename, escape_chars);
2030 if (escaped_filename == NULL)
2031 return FALSE;
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002032 mksession_cmdline = g_strconcat("mksession ", (char *)escaped_filename,
2033 NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 vim_free(escaped_filename);
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002035
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /*
2037 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2038 * unpredictable effects when the session is saved automatically. Also,
2039 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2040 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2041 * enormously large if the GNOME session feature is used regularly.
2042 */
2043 save_ssop_flags = ssop_flags;
2044 ssop_flags = (SSOP_BLANK|SSOP_CURDIR|SSOP_FOLDS|SSOP_GLOBALS
Bram Moolenaardc3213d2007-06-19 16:03:50 +00002045 |SSOP_HELP|SSOP_OPTIONS|SSOP_WINSIZE|SSOP_TABPAGES);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046
2047 do_cmdline_cmd((char_u *)"let Save_VV_this_session = v:this_session");
2048 failed = (do_cmdline_cmd((char_u *)mksession_cmdline) == FAIL);
2049 do_cmdline_cmd((char_u *)"let v:this_session = Save_VV_this_session");
Bram Moolenaar485db9b2005-01-31 19:23:41 +00002050 do_unlet((char_u *)"Save_VV_this_session", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051
2052 ssop_flags = save_ssop_flags;
2053 g_free(mksession_cmdline);
2054 /*
2055 * Reopen the file and append a command to restore v:this_session,
2056 * as if this save never happened. This is to avoid conflicts with
2057 * the user's own sessions. FIXME: It's probably less hackish to add
2058 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2059 */
2060 if (!failed)
2061 {
2062 FILE *fd;
2063
2064 fd = open_exfile(filename, TRUE, APPENDBIN);
2065
2066 failed = (fd == NULL
2067 || put_line(fd, "let v:this_session = Save_VV_this_session") == FAIL
2068 || put_line(fd, "unlet Save_VV_this_session") == FAIL);
2069
2070 if (fd != NULL && fclose(fd) != 0)
2071 failed = TRUE;
2072
2073 if (failed)
2074 mch_remove(filename);
2075 }
2076
2077 return !failed;
2078}
2079
2080/*
2081 * "save_yourself" signal handler. Initiate an interaction to ask the user
2082 * for confirmation if necessary. Save the current editing session and tell
2083 * the session manager how to restart Vim.
2084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 static gboolean
2086sm_client_save_yourself(GnomeClient *client,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002087 gint phase UNUSED,
2088 GnomeSaveStyle save_style UNUSED,
2089 gboolean shutdown UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 GnomeInteractStyle interact_style,
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002091 gboolean fast UNUSED,
2092 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093{
2094 static const char suffix[] = "-session.vim";
2095 char *session_file;
2096 unsigned int len;
2097 gboolean success;
2098
2099 /* Always request an interaction if possible. check_changed_any()
2100 * won't actually show a dialog unless any buffers have been modified.
2101 * There doesn't seem to be an obvious way to check that without
2102 * automatically firing the dialog. Anyway, it works just fine. */
2103 if (interact_style == GNOME_INTERACT_ANY)
2104 gnome_client_request_interaction(client, GNOME_DIALOG_NORMAL,
2105 &sm_client_check_changed_any,
2106 NULL);
2107 out_flush();
2108 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2109
2110 /* The path is unique for each session save. We do neither know nor care
2111 * which session script will actually be used later. This decision is in
2112 * the domain of the session manager. */
2113 session_file = gnome_config_get_real_path(
2114 gnome_client_get_config_prefix(client));
2115 len = strlen(session_file);
2116
2117 if (len > 0 && session_file[len-1] == G_DIR_SEPARATOR)
2118 --len; /* get rid of the superfluous trailing '/' */
2119
2120 session_file = g_renew(char, session_file, len + sizeof(suffix));
2121 memcpy(session_file + len, suffix, sizeof(suffix));
2122
2123 success = write_session_file((char_u *)session_file);
2124
2125 if (success)
2126 {
2127 const char *argv[8];
2128 int i;
2129
2130 /* Tell the session manager how to wipe out the stored session data.
2131 * This isn't as dangerous as it looks, don't worry :) session_file
2132 * is a unique absolute filename. Usually it'll be something like
2133 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2134 i = 0;
2135 argv[i++] = "rm";
2136 argv[i++] = session_file;
2137 argv[i] = NULL;
2138
2139 gnome_client_set_discard_command(client, i, (char **)argv);
2140
2141 /* Tell the session manager how to restore the just saved session.
2142 * This is easily done thanks to Vim's -S option. Pass the -f flag
2143 * since there's no need to fork -- it might even cause confusion.
2144 * Also pass the window role to give the WM something to match on.
2145 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2146 i = 0;
2147 argv[i++] = restart_command;
2148 argv[i++] = "-f";
2149 argv[i++] = "-g";
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 argv[i++] = "--role";
2151 argv[i++] = gtk_window_get_role(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 argv[i++] = "-S";
2153 argv[i++] = session_file;
2154 argv[i] = NULL;
2155
2156 gnome_client_set_restart_command(client, i, (char **)argv);
2157 gnome_client_set_clone_command(client, 0, NULL);
2158 }
2159
2160 g_free(session_file);
2161
2162 return success;
2163}
2164
2165/*
2166 * Called when the session manager wants us to die. There isn't much to save
2167 * here since "save_yourself" has been emitted before (unless serious trouble
2168 * is happening).
2169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 static void
Bram Moolenaar30bb4142010-05-17 22:07:15 +02002171sm_client_die(GnomeClient *client UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
2173 /* Don't write messages to the GUI anymore */
2174 full_screen = FALSE;
2175
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002176 vim_strncpy(IObuff, (char_u *)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00002177 _("Vim: Received \"die\" request from session manager\n"),
Bram Moolenaarc93e7912008-07-08 10:46:08 +00002178 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 preserve_exit();
2180}
2181
2182/*
2183 * Connect our signal handlers to be notified on session save and shutdown.
2184 */
2185 static void
2186setup_save_yourself(void)
2187{
2188 GnomeClient *client;
2189
2190 client = gnome_master_client();
2191
2192 if (client != NULL)
2193 {
2194 /* Must use the deprecated gtk_signal_connect() for compatibility
2195 * with GNOME 1. Arrgh, zombies! */
2196 gtk_signal_connect(GTK_OBJECT(client), "save_yourself",
2197 GTK_SIGNAL_FUNC(&sm_client_save_yourself), NULL);
2198 gtk_signal_connect(GTK_OBJECT(client), "die",
2199 GTK_SIGNAL_FUNC(&sm_client_die), NULL);
2200 }
2201}
2202
2203#else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2204
2205# ifdef USE_XSMP
2206/*
2207 * GTK tells us that XSMP needs attention
2208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 static gboolean
2210local_xsmp_handle_requests(source, condition, data)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002211 GIOChannel *source UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 GIOCondition condition;
2213 gpointer data;
2214{
2215 if (condition == G_IO_IN)
2216 {
2217 /* Do stuff; maybe close connection */
2218 if (xsmp_handle_requests() == FAIL)
2219 g_io_channel_unref((GIOChannel *)data);
2220 return TRUE;
2221 }
2222 /* Error */
2223 g_io_channel_unref((GIOChannel *)data);
2224 xsmp_close();
2225 return TRUE;
2226}
2227# endif /* USE_XSMP */
2228
2229/*
2230 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2231 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2232 */
2233 static void
2234setup_save_yourself(void)
2235{
2236 Atom *existing_atoms = NULL;
2237 int count = 0;
2238
2239#ifdef USE_XSMP
2240 if (xsmp_icefd != -1)
2241 {
2242 /*
2243 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2244 * set up GTK IO monitor
2245 */
2246 GIOChannel *g_io = g_io_channel_unix_new(xsmp_icefd);
2247
2248 g_io_add_watch(g_io, G_IO_IN | G_IO_ERR | G_IO_HUP,
2249 local_xsmp_handle_requests, (gpointer)g_io);
2250 }
2251 else
2252#endif
2253 {
2254 /* Fall back to old method */
2255
2256 /* first get the existing value */
2257 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2258 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2259 &existing_atoms, &count))
2260 {
2261 Atom *new_atoms;
2262 Atom save_yourself_xatom;
2263 int i;
2264
2265 save_yourself_xatom = GET_X_ATOM(save_yourself_atom);
2266
2267 /* check if WM_SAVE_YOURSELF isn't there yet */
2268 for (i = 0; i < count; ++i)
2269 if (existing_atoms[i] == save_yourself_xatom)
2270 break;
2271
2272 if (i == count)
2273 {
2274 /* allocate an Atoms array which is one item longer */
2275 new_atoms = (Atom *)alloc((unsigned)((count + 1)
2276 * sizeof(Atom)));
2277 if (new_atoms != NULL)
2278 {
2279 memcpy(new_atoms, existing_atoms, count * sizeof(Atom));
2280 new_atoms[count] = save_yourself_xatom;
2281 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2282 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2283 new_atoms, count + 1);
2284 vim_free(new_atoms);
2285 }
2286 }
2287 XFree(existing_atoms);
2288 }
2289 }
2290}
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292/*
2293 * Installing a global event filter seems to be the only way to catch
2294 * client messages of type WM_PROTOCOLS without overriding GDK's own
2295 * client message event filter. Well, that's still better than trying
2296 * to guess what the GDK filter had done if it had been invoked instead
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 *
2298 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2299 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2300 * I have the nasty feeling modern session managers just don't send this
2301 * deprecated message anymore. Addition: confirmed by several people.
2302 *
2303 * The GNOME session support is much cooler anyway. Unlike this ugly
2304 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2305 * it should work with KDE as well.
2306 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 static GdkFilterReturn
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002308global_event_filter(GdkXEvent *xev,
2309 GdkEvent *event UNUSED,
2310 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 XEvent *xevent = (XEvent *)xev;
2313
2314 if (xevent != NULL
2315 && xevent->type == ClientMessage
2316 && xevent->xclient.message_type == GET_X_ATOM(wm_protocols_atom)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002317 && (long_u)xevent->xclient.data.l[0]
2318 == GET_X_ATOM(save_yourself_atom))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
2320 out_flush();
2321 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
2322 /*
2323 * Set the window's WM_COMMAND property, to let the window manager
2324 * know we are done saving ourselves. We don't want to be
2325 * restarted, thus set argv to NULL.
2326 */
2327 XSetCommand(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2328 GDK_WINDOW_XWINDOW(gui.mainwin->window),
2329 NULL, 0);
2330 return GDK_FILTER_REMOVE;
2331 }
2332
2333 return GDK_FILTER_CONTINUE;
2334}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335#endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2336
2337
2338/*
2339 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002342mainwin_realize(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
2344/* If you get an error message here, you still need to unpack the runtime
2345 * archive! */
2346#ifdef magick
2347# undef magick
2348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 /* A bit hackish, but avoids casting later and allows optimization */
2350# define static static const
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#define magick vim32x32
2352#include "../runtime/vim32x32.xpm"
2353#undef magick
2354#define magick vim16x16
2355#include "../runtime/vim16x16.xpm"
2356#undef magick
2357#define magick vim48x48
2358#include "../runtime/vim48x48.xpm"
2359#undef magick
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360# undef static
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 /* When started with "--echo-wid" argument, write window ID on stdout. */
2363 if (echo_wid_arg)
2364 {
2365 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui.mainwin->window));
2366 fflush(stdout);
2367 }
2368
2369 if (vim_strchr(p_go, GO_ICON) != NULL)
2370 {
2371 /*
2372 * Add an icon to the main window. For fun and convenience of the user.
2373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 GList *icons = NULL;
2375
2376 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim16x16));
2377 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim32x32));
2378 icons = g_list_prepend(icons, gdk_pixbuf_new_from_xpm_data(vim48x48));
2379
2380 gtk_window_set_icon_list(GTK_WINDOW(gui.mainwin), icons);
2381
2382 g_list_foreach(icons, (GFunc)&g_object_unref, NULL);
2383 g_list_free(icons);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385
2386#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2387 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 gdk_window_add_filter(NULL, &global_event_filter, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389#endif
2390 /* Setup to indicate to the window manager that we want to catch the
2391 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2392 * manager instead. */
2393#if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2394 if (using_gnome)
2395#endif
2396 setup_save_yourself();
2397
2398#ifdef FEAT_CLIENTSERVER
2399 if (serverName == NULL && serverDelayedStartName != NULL)
2400 {
2401 /* This is a :gui command in a plain vim with no previous server */
2402 commWindow = GDK_WINDOW_XWINDOW(gui.mainwin->window);
2403
2404 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2405 serverDelayedStartName);
2406 }
2407 else
2408 {
2409 /*
2410 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2411 * have to change the "server" registration to that of the main window
2412 * If we have not registered a name yet, remember the window
2413 */
2414 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui.mainwin->window),
2415 GDK_WINDOW_XWINDOW(gui.mainwin->window));
2416 }
2417 gtk_widget_add_events(gui.mainwin, GDK_PROPERTY_CHANGE_MASK);
2418 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "property_notify_event",
2419 GTK_SIGNAL_FUNC(property_event), NULL);
2420#endif
2421}
2422
2423 static GdkCursor *
2424create_blank_pointer(void)
2425{
2426 GdkWindow *root_window = NULL;
2427 GdkPixmap *blank_mask;
2428 GdkCursor *cursor;
2429 GdkColor color = { 0, 0, 0, 0 };
2430 char blank_data[] = { 0x0 };
2431
2432#ifdef HAVE_GTK_MULTIHEAD
2433 root_window = gtk_widget_get_root_window(gui.mainwin);
2434#endif
2435
2436 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2437 * in size. */
2438 blank_mask = gdk_bitmap_create_from_data(root_window, blank_data, 1, 1);
2439 cursor = gdk_cursor_new_from_pixmap(blank_mask, blank_mask,
2440 &color, &color, 0, 0);
2441 gdk_bitmap_unref(blank_mask);
2442
2443 return cursor;
2444}
2445
2446#ifdef HAVE_GTK_MULTIHEAD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 static void
2448mainwin_screen_changed_cb(GtkWidget *widget,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002449 GdkScreen *previous_screen UNUSED,
2450 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
2452 if (!gtk_widget_has_screen(widget))
2453 return;
2454
2455 /*
Bram Moolenaar49325942007-05-10 19:19:59 +00002456 * Recreate the invisible mouse cursor.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 */
2458 if (gui.blank_pointer != NULL)
2459 gdk_cursor_unref(gui.blank_pointer);
2460
2461 gui.blank_pointer = create_blank_pointer();
2462
2463 if (gui.pointer_hidden && gui.drawarea->window != NULL)
2464 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
2465
2466 /*
2467 * Create a new PangoContext for this screen, and initialize it
2468 * with the current font if necessary.
2469 */
2470 if (gui.text_context != NULL)
2471 g_object_unref(gui.text_context);
2472
2473 gui.text_context = gtk_widget_create_pango_context(widget);
2474 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
2475
2476 if (gui.norm_font != NULL)
2477 {
Bram Moolenaar46c9c732004-12-12 11:37:09 +00002478 gui_mch_init_font(p_guifont, FALSE);
Bram Moolenaar04a9d452006-03-27 21:03:26 +00002479 gui_set_shellsize(FALSE, FALSE, RESIZE_BOTH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481}
2482#endif /* HAVE_GTK_MULTIHEAD */
2483
2484/*
2485 * After the drawing area comes up, we calculate all colors and create the
2486 * dummy blank cursor.
2487 *
2488 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2489 * fact that the main VIM engine doesn't take them into account anywhere.
2490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002492drawarea_realize_cb(GtkWidget *widget, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
2494 GtkWidget *sbar;
2495
2496#ifdef FEAT_XIM
2497 xim_init();
2498#endif
2499 gui_mch_new_colors();
2500 gui.text_gc = gdk_gc_new(gui.drawarea->window);
2501
2502 gui.blank_pointer = create_blank_pointer();
2503 if (gui.pointer_hidden)
2504 gdk_window_set_cursor(widget->window, gui.blank_pointer);
2505
2506 /* get the actual size of the scrollbars, if they are realized */
2507 sbar = firstwin->w_scrollbars[SBAR_LEFT].id;
2508 if (!sbar || (!gui.which_scrollbars[SBAR_LEFT]
2509 && firstwin->w_scrollbars[SBAR_RIGHT].id))
2510 sbar = firstwin->w_scrollbars[SBAR_RIGHT].id;
2511 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.width)
2512 gui.scrollbar_width = sbar->allocation.width;
2513
2514 sbar = gui.bottom_sbar.id;
2515 if (sbar && GTK_WIDGET_REALIZED(sbar) && sbar->allocation.height)
2516 gui.scrollbar_height = sbar->allocation.height;
2517}
2518
2519/*
2520 * Properly clean up on shutdown.
2521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002523drawarea_unrealize_cb(GtkWidget *widget UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 /* Don't write messages to the GUI anymore */
2526 full_screen = FALSE;
2527
2528#ifdef FEAT_XIM
2529 im_shutdown();
2530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 if (gui.ascii_glyphs != NULL)
2532 {
2533 pango_glyph_string_free(gui.ascii_glyphs);
2534 gui.ascii_glyphs = NULL;
2535 }
2536 if (gui.ascii_font != NULL)
2537 {
2538 g_object_unref(gui.ascii_font);
2539 gui.ascii_font = NULL;
2540 }
2541 g_object_unref(gui.text_context);
2542 gui.text_context = NULL;
2543
2544 g_object_unref(gui.text_gc);
2545 gui.text_gc = NULL;
2546
2547 gdk_cursor_unref(gui.blank_pointer);
2548 gui.blank_pointer = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549}
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002552drawarea_style_set_cb(GtkWidget *widget UNUSED,
2553 GtkStyle *previous_style UNUSED,
2554 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555{
2556 gui_mch_new_colors();
2557}
2558
2559/*
2560 * Callback routine for the "delete_event" signal on the toplevel window.
2561 * Tries to vim gracefully, or refuses to exit with changed buffers.
2562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002564delete_event_cb(GtkWidget *widget UNUSED,
2565 GdkEventAny *event UNUSED,
2566 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567{
2568 gui_shell_closed();
2569 return TRUE;
2570}
2571
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002572#if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2573 static int
2574get_item_dimensions(GtkWidget *widget, GtkOrientation orientation)
2575{
2576 GtkOrientation item_orientation = GTK_ORIENTATION_HORIZONTAL;
2577
2578#ifdef FEAT_GUI_GNOME
2579 if (using_gnome && widget != NULL)
2580 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002581 GtkWidget *parent;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002582 BonoboDockItem *dockitem;
2583
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002584 parent = gtk_widget_get_parent(widget);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002585 if (G_TYPE_FROM_INSTANCE(parent) == BONOBO_TYPE_DOCK_ITEM)
2586 {
2587 /* Only menu & toolbar are dock items. Could tabline be?
2588 * Seem to be only the 2 defined in GNOME */
2589 widget = parent;
2590 dockitem = BONOBO_DOCK_ITEM(widget);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002591
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002592 if (dockitem == NULL || dockitem->is_floating)
2593 return 0;
2594 item_orientation = bonobo_dock_item_get_orientation(dockitem);
2595 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002596 }
2597#endif
2598 if (widget != NULL
2599 && item_orientation == orientation
2600 && GTK_WIDGET_REALIZED(widget)
2601 && GTK_WIDGET_VISIBLE(widget))
2602 {
2603 if (orientation == GTK_ORIENTATION_HORIZONTAL)
2604 return widget->allocation.height;
2605 else
2606 return widget->allocation.width;
2607 }
2608 return 0;
2609}
2610#endif
2611
2612 static int
2613get_menu_tool_width(void)
2614{
2615 int width = 0;
2616
2617#ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2618# ifdef FEAT_MENU
2619 width += get_item_dimensions(gui.menubar, GTK_ORIENTATION_VERTICAL);
2620# endif
2621# ifdef FEAT_TOOLBAR
2622 width += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_VERTICAL);
2623# endif
2624# ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002625 if (gui.tabline != NULL)
2626 width += get_item_dimensions(gui.tabline, GTK_ORIENTATION_VERTICAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002627# endif
2628#endif
2629
2630 return width;
2631}
2632
2633 static int
2634get_menu_tool_height(void)
2635{
2636 int height = 0;
2637
2638#ifdef FEAT_MENU
2639 height += get_item_dimensions(gui.menubar, GTK_ORIENTATION_HORIZONTAL);
2640#endif
2641#ifdef FEAT_TOOLBAR
2642 height += get_item_dimensions(gui.toolbar, GTK_ORIENTATION_HORIZONTAL);
2643#endif
2644#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00002645 if (gui.tabline != NULL)
2646 height += get_item_dimensions(gui.tabline, GTK_ORIENTATION_HORIZONTAL);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002647#endif
2648
2649 return height;
2650}
2651
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002652/* This controls whether we can set the real window hints at
2653 * start-up when in a GtkPlug.
2654 * 0 = normal processing (default)
2655 * 1 = init. hints set, no-one's tried to reset since last check
2656 * 2 = init. hints set, attempt made to change hints
2657 */
2658static int init_window_hints_state = 0;
2659
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002660 static void
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002661update_window_manager_hints(int force_width, int force_height)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002662{
2663 static int old_width = 0;
2664 static int old_height = 0;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002665 static int old_min_width = 0;
2666 static int old_min_height = 0;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002667 static int old_char_width = 0;
2668 static int old_char_height = 0;
2669
2670 int width;
2671 int height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002672 int min_width;
2673 int min_height;
2674
2675 /* At start-up, don't try to set the hints until the initial
2676 * values have been used (those that dictate our initial size)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002677 * Let forced (i.e., correct) values through always.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002678 */
2679 if (!(force_width && force_height) && init_window_hints_state > 0)
2680 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002681 /* Don't do it! */
2682 init_window_hints_state = 2;
2683 return;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002684 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002685
2686 /* This also needs to be done when the main window isn't there yet,
2687 * otherwise the hints don't work. */
2688 width = gui_get_base_width();
2689 height = gui_get_base_height();
2690# ifdef FEAT_MENU
2691 height += tabline_height() * gui.char_height;
2692# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002693 width += get_menu_tool_width();
2694 height += get_menu_tool_height();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002695
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002696 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
Bram Moolenaar49325942007-05-10 19:19:59 +00002697 * their actual widget size. When we set our size ourselves (e.g.,
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002698 * 'set columns=' or init. -geom) we briefly set the min. to the size
2699 * we wish to be instead of the legitimate minimum so that we actually
2700 * resize correctly.
2701 */
2702 if (force_width && force_height)
2703 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002704 min_width = force_width;
2705 min_height = force_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002706 }
2707 else
2708 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002709 min_width = width + MIN_COLUMNS * gui.char_width;
2710 min_height = height + MIN_LINES * gui.char_height;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002711 }
2712
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002713 /* Avoid an expose event when the size didn't change. */
2714 if (width != old_width
2715 || height != old_height
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002716 || min_width != old_min_width
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002717 || min_height != old_min_height
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002718 || gui.char_width != old_char_width
2719 || gui.char_height != old_char_height)
2720 {
2721 GdkGeometry geometry;
2722 GdkWindowHints geometry_mask;
2723
2724 geometry.width_inc = gui.char_width;
2725 geometry.height_inc = gui.char_height;
2726 geometry.base_width = width;
2727 geometry.base_height = height;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002728 geometry.min_width = min_width;
2729 geometry.min_height = min_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002730 geometry_mask = GDK_HINT_BASE_SIZE|GDK_HINT_RESIZE_INC
2731 |GDK_HINT_MIN_SIZE;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002732 /* Using gui.formwin as geometry widget doesn't work as expected
2733 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
2734 * in Vim confuse GTK+. */
2735 gtk_window_set_geometry_hints(GTK_WINDOW(gui.mainwin), gui.mainwin,
2736 &geometry, geometry_mask);
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00002737 old_width = width;
2738 old_height = height;
2739 old_min_width = min_width;
2740 old_min_height = min_height;
2741 old_char_width = gui.char_width;
2742 old_char_height = gui.char_height;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002743 }
2744}
2745
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746#ifdef FEAT_TOOLBAR
2747
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748/*
2749 * This extra effort wouldn't be necessary if we only used stock icons in the
2750 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
2751 * shouldn't be treated differently, thus we do need this.
2752 */
2753 static void
2754icon_size_changed_foreach(GtkWidget *widget, gpointer user_data)
2755{
2756 if (GTK_IS_IMAGE(widget))
2757 {
2758 GtkImage *image = (GtkImage *)widget;
2759
2760 /* User-defined icons are stored in a GtkIconSet */
2761 if (gtk_image_get_storage_type(image) == GTK_IMAGE_ICON_SET)
2762 {
2763 GtkIconSet *icon_set;
2764 GtkIconSize icon_size;
2765
2766 gtk_image_get_icon_set(image, &icon_set, &icon_size);
2767 icon_size = (GtkIconSize)(long)user_data;
2768
2769 gtk_icon_set_ref(icon_set);
2770 gtk_image_set_from_icon_set(image, icon_set, icon_size);
2771 gtk_icon_set_unref(icon_set);
2772 }
2773 }
2774 else if (GTK_IS_CONTAINER(widget))
2775 {
2776 gtk_container_foreach((GtkContainer *)widget,
2777 &icon_size_changed_foreach,
2778 user_data);
2779 }
2780}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781
2782 static void
2783set_toolbar_style(GtkToolbar *toolbar)
2784{
2785 GtkToolbarStyle style;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 GtkIconSize size;
2787 GtkIconSize oldsize;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2790 == (TOOLBAR_TEXT | TOOLBAR_ICONS | TOOLBAR_HORIZ))
2791 style = GTK_TOOLBAR_BOTH_HORIZ;
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002792 else if ((toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 == (TOOLBAR_TEXT | TOOLBAR_ICONS))
2794 style = GTK_TOOLBAR_BOTH;
2795 else if (toolbar_flags & TOOLBAR_TEXT)
2796 style = GTK_TOOLBAR_TEXT;
2797 else
2798 style = GTK_TOOLBAR_ICONS;
2799
2800 gtk_toolbar_set_style(toolbar, style);
2801 gtk_toolbar_set_tooltips(toolbar, (toolbar_flags & TOOLBAR_TOOLTIPS) != 0);
2802
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 switch (tbis_flags)
2804 {
2805 case TBIS_TINY: size = GTK_ICON_SIZE_MENU; break;
2806 case TBIS_SMALL: size = GTK_ICON_SIZE_SMALL_TOOLBAR; break;
2807 case TBIS_MEDIUM: size = GTK_ICON_SIZE_BUTTON; break;
2808 case TBIS_LARGE: size = GTK_ICON_SIZE_LARGE_TOOLBAR; break;
2809 default: size = GTK_ICON_SIZE_INVALID; break;
2810 }
2811 oldsize = gtk_toolbar_get_icon_size(toolbar);
2812
2813 if (size == GTK_ICON_SIZE_INVALID)
2814 {
2815 /* Let global user preferences decide the icon size. */
2816 gtk_toolbar_unset_icon_size(toolbar);
2817 size = gtk_toolbar_get_icon_size(toolbar);
2818 }
2819 if (size != oldsize)
2820 {
2821 gtk_container_foreach(GTK_CONTAINER(toolbar),
2822 &icon_size_changed_foreach,
2823 GINT_TO_POINTER((int)size));
2824 }
2825 gtk_toolbar_set_icon_size(toolbar, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826}
2827
2828#endif /* FEAT_TOOLBAR */
2829
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002830#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
2831static int ignore_tabline_evt = FALSE;
Bram Moolenaara5621492006-02-25 21:55:24 +00002832static GtkWidget *tabline_menu;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002833static GtkTooltips *tabline_tooltip;
Bram Moolenaara5621492006-02-25 21:55:24 +00002834static int clicked_page; /* page clicked in tab line */
2835
2836/*
2837 * Handle selecting an item in the tab line popup menu.
2838 */
Bram Moolenaara5621492006-02-25 21:55:24 +00002839 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002840tabline_menu_handler(GtkMenuItem *item UNUSED, gpointer user_data)
Bram Moolenaara5621492006-02-25 21:55:24 +00002841{
Bram Moolenaara5621492006-02-25 21:55:24 +00002842 /* Add the string cmd into input buffer */
Bram Moolenaarc6fe9192006-04-09 21:54:49 +00002843 send_tabline_menu_event(clicked_page, (int)(long)user_data);
Bram Moolenaara5621492006-02-25 21:55:24 +00002844}
2845
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002846 static void
2847add_tabline_menu_item(GtkWidget *menu, char_u *text, int resp)
2848{
2849 GtkWidget *item;
2850 char_u *utf_text;
2851
2852 utf_text = CONVERT_TO_UTF8(text);
2853 item = gtk_menu_item_new_with_label((const char *)utf_text);
2854 gtk_widget_show(item);
2855 CONVERT_TO_UTF8_FREE(utf_text);
2856
2857 gtk_container_add(GTK_CONTAINER(menu), item);
2858 gtk_signal_connect(GTK_OBJECT(item), "activate",
2859 GTK_SIGNAL_FUNC(tabline_menu_handler),
Bram Moolenaar47b46d72008-07-02 19:05:48 +00002860 (gpointer)(long)resp);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002861}
2862
Bram Moolenaara5621492006-02-25 21:55:24 +00002863/*
Bram Moolenaara5621492006-02-25 21:55:24 +00002864 * Create a menu for the tab line.
2865 */
2866 static GtkWidget *
2867create_tabline_menu(void)
2868{
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002869 GtkWidget *menu;
Bram Moolenaara5621492006-02-25 21:55:24 +00002870
2871 menu = gtk_menu_new();
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002872 add_tabline_menu_item(menu, (char_u *)_("Close"), TABLINE_MENU_CLOSE);
2873 add_tabline_menu_item(menu, (char_u *)_("New tab"), TABLINE_MENU_NEW);
2874 add_tabline_menu_item(menu, (char_u *)_("Open Tab..."), TABLINE_MENU_OPEN);
Bram Moolenaara5621492006-02-25 21:55:24 +00002875
2876 return menu;
2877}
2878
2879 static gboolean
2880on_tabline_menu(GtkWidget *widget, GdkEvent *event)
2881{
2882 /* Was this button press event ? */
2883 if (event->type == GDK_BUTTON_PRESS)
2884 {
2885 GdkEventButton *bevent = (GdkEventButton *)event;
2886 int x = bevent->x;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002887 int y = bevent->y;
2888 GtkWidget *tabwidget;
2889 GdkWindow *tabwin;
Bram Moolenaara5621492006-02-25 21:55:24 +00002890
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002891 /* When ignoring events return TRUE so that the selected page doesn't
2892 * change. */
2893 if (hold_gui_events
2894# ifdef FEAT_CMDWIN
2895 || cmdwin_type != 0
2896# endif
2897 )
2898 return TRUE;
2899
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002900 tabwin = gdk_window_at_pointer(&x, &y);
2901 gdk_window_get_user_data(tabwin, (gpointer)&tabwidget);
2902 clicked_page = (int)(long)gtk_object_get_user_data(
2903 GTK_OBJECT(tabwidget));
Bram Moolenaara5621492006-02-25 21:55:24 +00002904
2905 /* If the event was generated for 3rd button popup the menu. */
2906 if (bevent->button == 3)
2907 {
2908 gtk_menu_popup(GTK_MENU(widget), NULL, NULL, NULL, NULL,
2909 bevent->button, bevent->time);
2910 /* We handled the event. */
2911 return TRUE;
2912 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002913 else if (bevent->button == 1)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002914 {
Bram Moolenaar96351572006-05-05 21:16:59 +00002915 if (clicked_page == 0)
2916 {
Bram Moolenaar07354542007-09-15 12:07:46 +00002917 /* Click after all tabs moves to next tab page. When "x" is
2918 * small guess it's the left button. */
Bram Moolenaara3f41662010-07-11 19:01:06 +02002919 send_tabline_event(x < 50 ? -1 : 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002920 }
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002921 }
Bram Moolenaara5621492006-02-25 21:55:24 +00002922 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002923
Bram Moolenaara5621492006-02-25 21:55:24 +00002924 /* We didn't handle the event. */
2925 return FALSE;
2926}
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002927
2928/*
2929 * Handle selecting one of the tabs.
2930 */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002931 static void
2932on_select_tab(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002933 GtkNotebook *notebook UNUSED,
2934 GtkNotebookPage *page UNUSED,
Bram Moolenaar89d40322006-08-29 15:30:07 +00002935 gint idx,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002936 gpointer data UNUSED)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002937{
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002938 if (!ignore_tabline_evt)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002939 {
Bram Moolenaara3f41662010-07-11 19:01:06 +02002940 send_tabline_event(idx + 1);
Bram Moolenaareddf53b2006-02-27 00:11:10 +00002941 }
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002942}
2943
2944/*
2945 * Show or hide the tabline.
2946 */
2947 void
2948gui_mch_show_tabline(int showit)
2949{
2950 if (gui.tabline == NULL)
2951 return;
2952
2953 if (!showit != !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline)))
2954 {
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002955 /* Note: this may cause a resize event */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002956 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), showit);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00002957 update_window_manager_hints(0, 0);
Bram Moolenaar96351572006-05-05 21:16:59 +00002958 if (showit)
2959 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui.tabline), GTK_CAN_FOCUS);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002960 }
Bram Moolenaar96351572006-05-05 21:16:59 +00002961
2962 gui_mch_update();
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002963}
2964
2965/*
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002966 * Return TRUE when tabline is displayed.
2967 */
2968 int
2969gui_mch_showing_tabline(void)
2970{
2971 return gui.tabline != NULL
Bram Moolenaar182c5be2010-06-25 05:37:59 +02002972 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui.tabline));
Bram Moolenaar3517bb12006-03-03 22:58:45 +00002973}
2974
2975/*
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002976 * Update the labels of the tabline.
2977 */
2978 void
2979gui_mch_update_tabline(void)
2980{
2981 GtkWidget *page;
Bram Moolenaar437df8f2006-04-27 21:47:44 +00002982 GtkWidget *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002983 GtkWidget *label;
2984 tabpage_T *tp;
2985 int nr = 0;
Bram Moolenaar8ea91232006-04-28 22:41:43 +00002986 int tab_num;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002987 int curtabidx = 0;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00002988 char_u *labeltext;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00002989
2990 if (gui.tabline == NULL)
2991 return;
2992
2993 ignore_tabline_evt = TRUE;
2994
2995 /* Add a label for each tab page. They all contain the same text area. */
2996 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next, ++nr)
2997 {
2998 if (tp == curtab)
2999 curtabidx = nr;
3000
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003001 tab_num = nr + 1;
3002
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003003 page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr);
3004 if (page == NULL)
3005 {
3006 /* Add notebook page */
3007 page = gtk_vbox_new(FALSE, 0);
3008 gtk_widget_show(page);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003009 event_box = gtk_event_box_new();
3010 gtk_widget_show(event_box);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003011 label = gtk_label_new("-Empty-");
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003012 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003013 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003014 gtk_widget_show(label);
3015 gtk_notebook_insert_page(GTK_NOTEBOOK(gui.tabline),
3016 page,
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003017 event_box,
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003018 nr++);
3019 }
3020
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003021 event_box = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui.tabline), page);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003022 gtk_object_set_user_data(GTK_OBJECT(event_box),
3023 (gpointer)(long)tab_num);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003024 label = GTK_BIN(event_box)->child;
Bram Moolenaar57657d82006-04-21 22:12:41 +00003025 get_tabline_label(tp, FALSE);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003026 labeltext = CONVERT_TO_UTF8(NameBuff);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003027 gtk_label_set_text(GTK_LABEL(label), (const char *)labeltext);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003028 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003029
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003030 get_tabline_label(tp, TRUE);
3031 labeltext = CONVERT_TO_UTF8(NameBuff);
3032 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip), event_box,
3033 (const char *)labeltext, NULL);
3034 CONVERT_TO_UTF8_FREE(labeltext);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003035 }
3036
3037 /* Remove any old labels. */
3038 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui.tabline), nr) != NULL)
3039 gtk_notebook_remove_page(GTK_NOTEBOOK(gui.tabline), nr);
3040
3041 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != curtabidx)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003042 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), curtabidx);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003043
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003044 /* Make sure everything is in place before drawing text. */
3045 gui_mch_update();
3046
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003047 ignore_tabline_evt = FALSE;
3048}
3049
3050/*
3051 * Set the current tab to "nr". First tab is 1.
3052 */
3053 void
3054gui_mch_set_curtab(nr)
3055 int nr;
3056{
3057 if (gui.tabline == NULL)
3058 return;
3059
3060 ignore_tabline_evt = TRUE;
3061 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui.tabline)) != nr - 1)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003062 gtk_notebook_set_page(GTK_NOTEBOOK(gui.tabline), nr - 1);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003063 ignore_tabline_evt = FALSE;
3064}
3065
3066#endif /* FEAT_GUI_TABLINE */
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068/*
Bram Moolenaara76638f2010-06-05 12:49:46 +02003069 * Add selection targets for PRIMARY and CLIPBOARD selections.
3070 */
3071 void
3072gui_gtk_set_selection_targets(void)
3073{
3074 int i, j = 0;
3075 int n_targets = N_SELECTION_TARGETS;
3076 GtkTargetEntry targets[N_SELECTION_TARGETS];
3077
3078 for (i = 0; i < (int)N_SELECTION_TARGETS; ++i)
3079 {
Bram Moolenaar29695702012-05-18 17:03:18 +02003080 /* OpenOffice tries to use TARGET_HTML and fails when we don't
Bram Moolenaara76638f2010-06-05 12:49:46 +02003081 * return something, instead of trying another target. Therefore only
3082 * offer TARGET_HTML when it works. */
3083 if (!clip_html && selection_targets[i].info == TARGET_HTML)
3084 n_targets--;
3085 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003086 targets[j++] = selection_targets[i];
3087 }
3088
3089 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)GDK_SELECTION_PRIMARY);
3090 gtk_selection_clear_targets(gui.drawarea, (GdkAtom)clip_plus.gtk_sel_atom);
3091 gtk_selection_add_targets(gui.drawarea,
3092 (GdkAtom)GDK_SELECTION_PRIMARY,
3093 targets, n_targets);
3094 gtk_selection_add_targets(gui.drawarea,
3095 (GdkAtom)clip_plus.gtk_sel_atom,
3096 targets, n_targets);
3097}
3098
3099/*
3100 * Set up for receiving DND items.
3101 */
3102 void
3103gui_gtk_set_dnd_targets(void)
3104{
3105 int i, j = 0;
3106 int n_targets = N_DND_TARGETS;
3107 GtkTargetEntry targets[N_DND_TARGETS];
3108
3109 for (i = 0; i < (int)N_DND_TARGETS; ++i)
3110 {
Bram Moolenaarb931d742011-10-26 11:36:25 +02003111 if (!clip_html && dnd_targets[i].info == TARGET_HTML)
Bram Moolenaara76638f2010-06-05 12:49:46 +02003112 n_targets--;
3113 else
Bram Moolenaara76638f2010-06-05 12:49:46 +02003114 targets[j++] = dnd_targets[i];
3115 }
3116
3117 gtk_drag_dest_unset(gui.drawarea);
3118 gtk_drag_dest_set(gui.drawarea,
3119 GTK_DEST_DEFAULT_ALL,
3120 targets, n_targets,
Bram Moolenaarba7cc9f2011-02-25 17:10:27 +01003121 GDK_ACTION_COPY | GDK_ACTION_MOVE);
Bram Moolenaara76638f2010-06-05 12:49:46 +02003122}
3123
3124/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3126 * Returns OK for success, FAIL when the GUI can't be started.
3127 */
3128 int
3129gui_mch_init(void)
3130{
3131 GtkWidget *vbox;
3132
3133#ifdef FEAT_GUI_GNOME
3134 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3135 * exits on failure, but that's a non-issue because we already called
3136 * gtk_init_check() in gui_mch_init_check(). */
3137 if (using_gnome)
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 gnome_program_init(VIMPACKAGE, VIM_VERSION_SHORT,
3140 LIBGNOMEUI_MODULE, gui_argc, gui_argv, NULL);
Bram Moolenaar3be71ce2013-01-23 16:00:11 +01003141# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
3142 /* Make sure strtod() uses a decimal point, not a comma. Gnome init
3143 * may change it. */
3144 if (setlocale(LC_NUMERIC, NULL) != (char *) "C")
3145 setlocale(LC_NUMERIC, "C");
3146# endif
3147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#endif
3149 vim_free(gui_argv);
3150 gui_argv = NULL;
3151
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003152#if GLIB_CHECK_VERSION(2,1,3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 /* Set the human-readable application name */
3154 g_set_application_name("Vim");
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003155#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 /*
3157 * Force UTF-8 output no matter what the value of 'encoding' is.
3158 * did_set_string_option() in option.c prohibits changing 'termencoding'
3159 * to something else than UTF-8 if the GUI is in use.
3160 */
3161 set_option_value((char_u *)"termencoding", 0L, (char_u *)"utf-8", 0);
3162
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003163#ifdef FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 gui_gtk_register_stock_icons();
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3167 * The hard part is deciding install locations and the Makefile magic. */
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003168#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 gtk_rc_parse("gtkrc");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170#endif
3171
3172 /* Initialize values */
3173 gui.border_width = 2;
3174 gui.scrollbar_width = SB_DEFAULT_WIDTH;
3175 gui.scrollbar_height = SB_DEFAULT_WIDTH;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003176 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 gui.fgcolor = g_new0(GdkColor, 1);
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00003178 /* LINTED: avoid warning: conversion to 'unsigned long' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 gui.bgcolor = g_new0(GdkColor, 1);
Bram Moolenaarf36d3692005-03-15 22:48:14 +00003180 /* LINTED: avoid warning: conversion to 'unsigned long' */
3181 gui.spcolor = g_new0(GdkColor, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182
3183 /* Initialise atoms */
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00003184 html_atom = gdk_atom_intern("text/html", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 utf8_string_atom = gdk_atom_intern("UTF8_STRING", FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
3187 /* Set default foreground and background colors. */
3188 gui.norm_pixel = gui.def_norm_pixel;
3189 gui.back_pixel = gui.def_back_pixel;
3190
3191 if (gtk_socket_id != 0)
3192 {
3193 GtkWidget *plug;
3194
3195 /* Use GtkSocket from another app. */
3196#ifdef HAVE_GTK_MULTIHEAD
3197 plug = gtk_plug_new_for_display(gdk_display_get_default(),
3198 gtk_socket_id);
3199#else
3200 plug = gtk_plug_new(gtk_socket_id);
3201#endif
3202 if (plug != NULL && GTK_PLUG(plug)->socket_window != NULL)
3203 {
3204 gui.mainwin = plug;
3205 }
3206 else
3207 {
3208 g_warning("Connection to GTK+ socket (ID %u) failed",
3209 (unsigned int)gtk_socket_id);
3210 /* Pretend we never wanted it if it failed (get own window) */
3211 gtk_socket_id = 0;
3212 }
3213 }
3214
3215 if (gtk_socket_id == 0)
3216 {
3217#ifdef FEAT_GUI_GNOME
3218 if (using_gnome)
3219 {
3220 gui.mainwin = gnome_app_new("Vim", NULL);
3221# ifdef USE_XSMP
3222 /* Use the GNOME save-yourself functionality now. */
3223 xsmp_close();
3224# endif
3225 }
3226 else
3227#endif
3228 gui.mainwin = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3229 }
3230
3231 gtk_widget_set_name(gui.mainwin, "vim-main-window");
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* Create the PangoContext used for drawing all text. */
3234 gui.text_context = gtk_widget_create_pango_context(gui.mainwin);
3235 pango_context_set_base_dir(gui.text_context, PANGO_DIRECTION_LTR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 gtk_container_border_width(GTK_CONTAINER(gui.mainwin), 0);
3238 gtk_widget_add_events(gui.mainwin, GDK_VISIBILITY_NOTIFY_MASK);
3239
3240 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "delete_event",
3241 GTK_SIGNAL_FUNC(&delete_event_cb), NULL);
3242
3243 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "realize",
3244 GTK_SIGNAL_FUNC(&mainwin_realize), NULL);
3245#ifdef HAVE_GTK_MULTIHEAD
3246 g_signal_connect(G_OBJECT(gui.mainwin), "screen_changed",
3247 G_CALLBACK(&mainwin_screen_changed_cb), NULL);
3248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 gui.accel_group = gtk_accel_group_new();
3250 gtk_window_add_accel_group(GTK_WINDOW(gui.mainwin), gui.accel_group);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003252 /* A vertical box holds the menubar, toolbar and main text window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 vbox = gtk_vbox_new(FALSE, 0);
3254
3255#ifdef FEAT_GUI_GNOME
3256 if (using_gnome)
3257 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003258# if defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 /* automagically restore menubar/toolbar placement */
3260 gnome_app_enable_layout_config(GNOME_APP(gui.mainwin), TRUE);
3261# endif
3262 gnome_app_set_contents(GNOME_APP(gui.mainwin), vbox);
3263 }
3264 else
3265#endif
3266 {
3267 gtk_container_add(GTK_CONTAINER(gui.mainwin), vbox);
3268 gtk_widget_show(vbox);
3269 }
3270
3271#ifdef FEAT_MENU
3272 /*
3273 * Create the menubar and handle
3274 */
3275 gui.menubar = gtk_menu_bar_new();
3276 gtk_widget_set_name(gui.menubar, "vim-menubar");
3277
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003278 /* Avoid that GTK takes <F10> away from us. */
3279 {
3280 GtkSettings *gtk_settings;
3281
3282 gtk_settings = gtk_settings_get_for_screen(gdk_screen_get_default());
3283 g_object_set(gtk_settings, "gtk-menu-bar-accel", NULL, NULL);
3284 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00003285
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287# ifdef FEAT_GUI_GNOME
3288 if (using_gnome)
3289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 BonoboDockItem *dockitem;
3291
3292 gnome_app_set_menus(GNOME_APP(gui.mainwin), GTK_MENU_BAR(gui.menubar));
3293 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3294 GNOME_APP_MENUBAR_NAME);
Bram Moolenaardb552d602006-03-23 22:59:57 +00003295 /* We don't want the menu to float. */
3296 bonobo_dock_item_set_behavior(dockitem,
3297 bonobo_dock_item_get_behavior(dockitem)
3298 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 gui.menubar_h = GTK_WIDGET(dockitem);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301 else
3302# endif /* FEAT_GUI_GNOME */
3303 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00003304 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3305 * disabled in gui_init() later. */
3306 gtk_widget_show(gui.menubar);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 gtk_box_pack_start(GTK_BOX(vbox), gui.menubar, FALSE, FALSE, 0);
3308 }
3309#endif /* FEAT_MENU */
3310
3311#ifdef FEAT_TOOLBAR
3312 /*
3313 * Create the toolbar and handle
3314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 /* some aesthetics on the toolbar */
3316 gtk_rc_parse_string(
3317 "style \"vim-toolbar-style\" {\n"
3318 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3319 "}\n"
3320 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3321 gui.toolbar = gtk_toolbar_new();
3322 gtk_widget_set_name(gui.toolbar, "vim-toolbar");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
3324
3325# ifdef FEAT_GUI_GNOME
3326 if (using_gnome)
3327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 BonoboDockItem *dockitem;
3329
3330 gnome_app_set_toolbar(GNOME_APP(gui.mainwin), GTK_TOOLBAR(gui.toolbar));
3331 dockitem = gnome_app_get_dock_item_by_name(GNOME_APP(gui.mainwin),
3332 GNOME_APP_TOOLBAR_NAME);
3333 gui.toolbar_h = GTK_WIDGET(dockitem);
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003334 /* When the toolbar is floating it gets stuck. So long as that isn't
Bram Moolenaardb552d602006-03-23 22:59:57 +00003335 * fixed let's disallow floating. */
Bram Moolenaare580b0c2006-03-21 21:33:03 +00003336 bonobo_dock_item_set_behavior(dockitem,
Bram Moolenaardb552d602006-03-23 22:59:57 +00003337 bonobo_dock_item_get_behavior(dockitem)
3338 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 gtk_container_set_border_width(GTK_CONTAINER(gui.toolbar), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 }
3341 else
3342# endif /* FEAT_GUI_GNOME */
3343 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (vim_strchr(p_go, GO_TOOLBAR) != NULL
3345 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3346 gtk_widget_show(gui.toolbar);
3347 gtk_box_pack_start(GTK_BOX(vbox), gui.toolbar, FALSE, FALSE, 0);
3348 }
3349#endif /* FEAT_TOOLBAR */
3350
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003351#ifdef FEAT_GUI_TABLINE
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003352 /*
3353 * Use a Notebook for the tab pages labels. The labels are hidden by
3354 * default.
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003355 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003356 gui.tabline = gtk_notebook_new();
3357 gtk_widget_show(gui.tabline);
3358 gtk_box_pack_start(GTK_BOX(vbox), gui.tabline, FALSE, FALSE, 0);
3359 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui.tabline), FALSE);
3360 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003361 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui.tabline), TRUE);
Bram Moolenaar96351572006-05-05 21:16:59 +00003362 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui.tabline), FALSE);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003363
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003364 tabline_tooltip = gtk_tooltips_new();
3365 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip));
3366
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003367 {
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003368 GtkWidget *page, *label, *event_box;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003369
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003370 /* Add the first tab. */
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003371 page = gtk_vbox_new(FALSE, 0);
3372 gtk_widget_show(page);
3373 gtk_container_add(GTK_CONTAINER(gui.tabline), page);
3374 label = gtk_label_new("-Empty-");
3375 gtk_widget_show(label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003376 event_box = gtk_event_box_new();
3377 gtk_widget_show(event_box);
Bram Moolenaar47b46d72008-07-02 19:05:48 +00003378 gtk_object_set_user_data(GTK_OBJECT(event_box), (gpointer)1L);
Bram Moolenaar8ea91232006-04-28 22:41:43 +00003379 gtk_misc_set_padding(GTK_MISC(label), 2, 2);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003380 gtk_container_add(GTK_CONTAINER(event_box), label);
Bram Moolenaar437df8f2006-04-27 21:47:44 +00003381 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui.tabline), page, event_box);
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00003382 }
Bram Moolenaar54a709e2006-05-04 21:57:11 +00003383
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003384 gtk_signal_connect(GTK_OBJECT(gui.tabline), "switch_page",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003385 GTK_SIGNAL_FUNC(on_select_tab), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003386
3387 /* Create a popup menu for the tab line and connect it. */
3388 tabline_menu = create_tabline_menu();
3389 gtk_signal_connect_object(GTK_OBJECT(gui.tabline), "button_press_event",
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003390 GTK_SIGNAL_FUNC(on_tabline_menu), GTK_OBJECT(tabline_menu));
Bram Moolenaar32466aa2006-02-24 23:53:04 +00003391#endif
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 gui.formwin = gtk_form_new();
3394 gtk_container_border_width(GTK_CONTAINER(gui.formwin), 0);
3395 gtk_widget_set_events(gui.formwin, GDK_EXPOSURE_MASK);
3396
3397 gui.drawarea = gtk_drawing_area_new();
3398
3399 /* Determine which events we will filter. */
3400 gtk_widget_set_events(gui.drawarea,
3401 GDK_EXPOSURE_MASK |
3402 GDK_ENTER_NOTIFY_MASK |
3403 GDK_LEAVE_NOTIFY_MASK |
3404 GDK_BUTTON_PRESS_MASK |
3405 GDK_BUTTON_RELEASE_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 GDK_SCROLL_MASK |
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 GDK_KEY_PRESS_MASK |
3408 GDK_KEY_RELEASE_MASK |
3409 GDK_POINTER_MOTION_MASK |
3410 GDK_POINTER_MOTION_HINT_MASK);
3411
3412 gtk_widget_show(gui.drawarea);
3413 gtk_form_put(GTK_FORM(gui.formwin), gui.drawarea, 0, 0);
3414 gtk_widget_show(gui.formwin);
3415 gtk_box_pack_start(GTK_BOX(vbox), gui.formwin, TRUE, TRUE, 0);
3416
3417 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3418 * and not the window. */
3419 gtk_signal_connect((gtk_socket_id == 0) ? GTK_OBJECT(gui.mainwin)
3420 : GTK_OBJECT(gui.drawarea),
3421 "key_press_event",
3422 GTK_SIGNAL_FUNC(key_press_event), NULL);
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003423#if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 /* Also forward key release events for the benefit of GTK+ 2 input
3425 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3426 g_signal_connect((gtk_socket_id == 0) ? G_OBJECT(gui.mainwin)
3427 : G_OBJECT(gui.drawarea),
3428 "key_release_event",
3429 G_CALLBACK(&key_release_event), NULL);
3430#endif
3431 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "realize",
3432 GTK_SIGNAL_FUNC(drawarea_realize_cb), NULL);
3433 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "unrealize",
3434 GTK_SIGNAL_FUNC(drawarea_unrealize_cb), NULL);
3435
3436 gtk_signal_connect_after(GTK_OBJECT(gui.drawarea), "style_set",
3437 GTK_SIGNAL_FUNC(&drawarea_style_set_cb), NULL);
3438
3439 gui.visibility = GDK_VISIBILITY_UNOBSCURED;
3440
3441#if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3442 wm_protocols_atom = gdk_atom_intern("WM_PROTOCOLS", FALSE);
3443 save_yourself_atom = gdk_atom_intern("WM_SAVE_YOURSELF", FALSE);
3444#endif
3445
3446 if (gtk_socket_id != 0)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003447 /* make sure keyboard input can go to the drawarea */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 GTK_WIDGET_SET_FLAGS(gui.drawarea, GTK_CAN_FOCUS);
3449
3450 /*
3451 * Set clipboard specific atoms
3452 */
3453 vim_atom = gdk_atom_intern(VIM_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 vimenc_atom = gdk_atom_intern(VIMENC_ATOM_NAME, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 clip_star.gtk_sel_atom = GDK_SELECTION_PRIMARY;
3456 clip_plus.gtk_sel_atom = gdk_atom_intern("CLIPBOARD", FALSE);
3457
3458 /*
3459 * Start out by adding the configured border width into the border offset.
3460 */
3461 gui.border_offset = gui.border_width;
3462
3463 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "visibility_notify_event",
3464 GTK_SIGNAL_FUNC(visibility_event), NULL);
3465 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "expose_event",
3466 GTK_SIGNAL_FUNC(expose_event), NULL);
3467
3468 /*
3469 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3470 * Only needed for some window managers.
3471 */
3472 if (vim_strchr(p_go, GO_POINTER) != NULL)
3473 {
3474 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "leave_notify_event",
3475 GTK_SIGNAL_FUNC(leave_notify_event), NULL);
3476 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "enter_notify_event",
3477 GTK_SIGNAL_FUNC(enter_notify_event), NULL);
3478 }
3479
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003480 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3481 * only its widgets. Arguably, this could be common code and we not use
3482 * the window focus at all, but let's be safe.
3483 */
3484 if (gtk_socket_id == 0)
3485 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003486 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_out_event",
3487 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3488 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "focus_in_event",
3489 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003490 }
3491 else
3492 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003493 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_out_event",
3494 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3495 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "focus_in_event",
3496 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003497#ifdef FEAT_GUI_TABLINE
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003498 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_out_event",
3499 GTK_SIGNAL_FUNC(focus_out_event), NULL);
3500 gtk_signal_connect(GTK_OBJECT(gui.tabline), "focus_in_event",
3501 GTK_SIGNAL_FUNC(focus_in_event), NULL);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003502#endif /* FEAT_GUI_TABLINE */
3503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "motion_notify_event",
3506 GTK_SIGNAL_FUNC(motion_notify_event), NULL);
3507 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_press_event",
3508 GTK_SIGNAL_FUNC(button_press_event), NULL);
3509 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "button_release_event",
3510 GTK_SIGNAL_FUNC(button_release_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 g_signal_connect(G_OBJECT(gui.drawarea), "scroll_event",
3512 G_CALLBACK(&scroll_event), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 /*
3515 * Add selection handler functions.
3516 */
3517 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_clear_event",
3518 GTK_SIGNAL_FUNC(selection_clear_event), NULL);
3519 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_received",
3520 GTK_SIGNAL_FUNC(selection_received_cb), NULL);
3521
Bram Moolenaara76638f2010-06-05 12:49:46 +02003522 gui_gtk_set_selection_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523
3524 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "selection_get",
3525 GTK_SIGNAL_FUNC(selection_get_cb), NULL);
3526
3527 /* Pretend we don't have input focus, we will get an event if we do. */
3528 gui.in_focus = FALSE;
3529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 return OK;
3531}
3532
3533#if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3534/*
3535 * This is called from gui_start() after a fork() has been done.
3536 * We have to tell the session manager our new PID.
3537 */
3538 void
3539gui_mch_forked(void)
3540{
3541 if (using_gnome)
3542 {
3543 GnomeClient *client;
3544
3545 client = gnome_master_client();
3546
3547 if (client != NULL)
3548 gnome_client_set_process_id(client, getpid());
3549 }
3550}
3551#endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3552
3553/*
3554 * Called when the foreground or background color has been changed.
3555 * This used to change the graphics contexts directly but we are
3556 * currently manipulating them where desired.
3557 */
3558 void
3559gui_mch_new_colors(void)
3560{
3561 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
3562 {
3563 GdkColor color = { 0, 0, 0, 0 };
3564
3565 color.pixel = gui.back_pixel;
3566 gdk_window_set_background(gui.drawarea->window, &color);
3567 }
3568}
3569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570/*
3571 * This signal informs us about the need to rearrange our sub-widgets.
3572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003574form_configure_event(GtkWidget *widget UNUSED,
3575 GdkEventConfigure *event,
3576 gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577{
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003578 int usable_height = event->height;
3579
3580 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3581 * no. of char-heights), so we have to manually sanitise them.
3582 * Widths seem to sort themselves out, don't ask me why.
3583 */
3584 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003585 usable_height -= (gui.char_height - (gui.char_height/2)); /* sic. */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 gtk_form_freeze(GTK_FORM(gui.formwin));
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003588 gui_resize_shell(event->width, usable_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 gtk_form_thaw(GTK_FORM(gui.formwin));
3590
3591 return TRUE;
3592}
3593
3594/*
3595 * Function called when window already closed.
3596 * We can't do much more here than to trying to preserve what had been done,
3597 * since the window is already inevitably going away.
3598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003600mainwin_destroy_cb(GtkObject *object UNUSED, gpointer data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601{
3602 /* Don't write messages to the GUI anymore */
3603 full_screen = FALSE;
3604
3605 gui.mainwin = NULL;
3606 gui.drawarea = NULL;
3607
3608 if (!exiting) /* only do anything if the destroy was unexpected */
3609 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003610 vim_strncpy(IObuff,
3611 (char_u *)_("Vim: Main window unexpectedly destroyed\n"),
3612 IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 preserve_exit();
3614 }
3615}
3616
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003617
3618/*
3619 * Bit of a hack to ensure we start GtkPlug windows with the correct window
3620 * hints (and thus the required size from -geom), but that after that we
3621 * put the hints back to normal (the actual minimum size) so we may
3622 * subsequently be resized smaller. GtkSocket (the parent end) uses the
Bram Moolenaar49325942007-05-10 19:19:59 +00003623 * plug's window 'min hints to set *it's* minimum size, but that's also the
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003624 * only way we have of making ourselves bigger (by set lines/columns).
3625 * Thus set hints at start-up to ensure correct init. size, then a
3626 * second after the final attempt to reset the real minimum hinst (done by
Bram Moolenaar49325942007-05-10 19:19:59 +00003627 * scrollbar init.), actually do the standard hinst and stop the timer.
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003628 * We'll not let the default hints be set while this timer's active.
3629 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003630 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003631check_startup_plug_hints(gpointer data UNUSED)
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003632{
3633 if (init_window_hints_state == 1)
3634 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003635 /* Safe to use normal hints now */
3636 init_window_hints_state = 0;
3637 update_window_manager_hints(0, 0);
3638 return FALSE; /* stop timer */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003639 }
3640
3641 /* Keep on trying */
3642 init_window_hints_state = 1;
3643 return TRUE;
3644}
3645
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646/*
3647 * Open the GUI window which was created by a call to gui_mch_init().
3648 */
3649 int
3650gui_mch_open(void)
3651{
3652 guicolor_T fg_pixel = INVALCOLOR;
3653 guicolor_T bg_pixel = INVALCOLOR;
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003654 guint pixel_width;
3655 guint pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 /*
3658 * Allow setting a window role on the command line, or invent one
3659 * if none was specified. This is mainly useful for GNOME session
3660 * support; allowing the WM to restore window placement.
3661 */
3662 if (role_argument != NULL)
3663 {
3664 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role_argument);
3665 }
3666 else
3667 {
3668 char *role;
3669
3670 /* Invent a unique-enough ID string for the role */
3671 role = g_strdup_printf("vim-%u-%u-%u",
3672 (unsigned)mch_get_pid(),
3673 (unsigned)g_random_int(),
3674 (unsigned)time(NULL));
3675
3676 gtk_window_set_role(GTK_WINDOW(gui.mainwin), role);
3677 g_free(role);
3678 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 if (gui_win_x != -1 && gui_win_y != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 gtk_window_move(GTK_WINDOW(gui.mainwin), gui_win_x, gui_win_y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
3683 /* Determine user specified geometry, if present. */
3684 if (gui.geom != NULL)
3685 {
3686 int mask;
3687 unsigned int w, h;
3688 int x = 0;
3689 int y = 0;
3690
3691 mask = XParseGeometry((char *)gui.geom, &x, &y, &w, &h);
3692
3693 if (mask & WidthValue)
3694 Columns = w;
3695 if (mask & HeightValue)
Bram Moolenaard68071d2006-05-02 22:08:30 +00003696 {
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003697 if (p_window > (long)h - 1 || !option_was_set((char_u *)"window"))
Bram Moolenaard68071d2006-05-02 22:08:30 +00003698 p_window = h - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 Rows = h;
Bram Moolenaard68071d2006-05-02 22:08:30 +00003700 }
Bram Moolenaare057d402013-06-30 17:51:51 +02003701 limit_screen_size();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003702
3703 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3704 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
3705
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003706 pixel_width += get_menu_tool_width();
3707 pixel_height += get_menu_tool_height();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 if (mask & (XValue | YValue))
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003710 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003711 int ww, hh;
3712 gui_mch_get_screen_dimensions(&ww, &hh);
3713 hh += p_ghr + get_menu_tool_height();
3714 ww += get_menu_tool_width();
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003715 if (mask & XNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003716 x += ww - pixel_width;
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003717 if (mask & YNegative)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003718 y += hh - pixel_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar2dd8b522007-10-19 12:33:44 +00003720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 vim_free(gui.geom);
3722 gui.geom = NULL;
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003723
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003724 /* From now until everyone's stopped trying to set the window hints
3725 * to their correct minimum values, stop them being set as we need
3726 * them to remain at our required size for the parent GtkSocket to
3727 * give us the right initial size.
3728 */
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003729 if (gtk_socket_id != 0 && (mask & WidthValue || mask & HeightValue))
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003730 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003731 update_window_manager_hints(pixel_width, pixel_height);
3732 init_window_hints_state = 1;
3733 g_timeout_add(1000, check_startup_plug_hints, NULL);
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
3736
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003737 pixel_width = (guint)(gui_get_base_width() + Columns * gui.char_width);
3738 pixel_height = (guint)(gui_get_base_height() + Rows * gui.char_height);
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003739 /* For GTK2 changing the size of the form widget doesn't cause window
3740 * resizing. */
Bram Moolenaardebe25a2010-06-06 17:41:24 +02003741 if (gtk_socket_id == 0)
Bram Moolenaar79ef6d62009-09-23 15:35:48 +00003742 gtk_window_resize(GTK_WINDOW(gui.mainwin), pixel_width, pixel_height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003743 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744
3745 if (foreground_argument != NULL)
3746 fg_pixel = gui_get_color((char_u *)foreground_argument);
3747 if (fg_pixel == INVALCOLOR)
3748 fg_pixel = gui_get_color((char_u *)"Black");
3749
3750 if (background_argument != NULL)
3751 bg_pixel = gui_get_color((char_u *)background_argument);
3752 if (bg_pixel == INVALCOLOR)
3753 bg_pixel = gui_get_color((char_u *)"White");
3754
3755 if (found_reverse_arg)
3756 {
3757 gui.def_norm_pixel = bg_pixel;
3758 gui.def_back_pixel = fg_pixel;
3759 }
3760 else
3761 {
3762 gui.def_norm_pixel = fg_pixel;
3763 gui.def_back_pixel = bg_pixel;
3764 }
3765
3766 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
3767 * in a vimrc file) */
3768 set_normal_colors();
3769
3770 /* Check that none of the colors are the same as the background color */
3771 gui_check_colors();
3772
3773 /* Get the colors for the highlight groups (gui_check_colors() might have
3774 * changed them). */
3775 highlight_gui_started(); /* re-init colors and fonts */
3776
3777 gtk_signal_connect(GTK_OBJECT(gui.mainwin), "destroy",
3778 GTK_SIGNAL_FUNC(mainwin_destroy_cb), NULL);
3779
3780#ifdef FEAT_HANGULIN
3781 hangul_keyboard_set();
3782#endif
3783
3784 /*
3785 * Notify the fixed area about the need to resize the contents of the
3786 * gui.formwin, which we use for random positioning of the included
3787 * components.
3788 *
3789 * We connect this signal deferred finally after anything is in place,
3790 * since this is intended to handle resizements coming from the window
3791 * manager upon us and should not interfere with what VIM is requesting
3792 * upon startup.
3793 */
3794 gtk_signal_connect(GTK_OBJECT(gui.formwin), "configure_event",
3795 GTK_SIGNAL_FUNC(form_configure_event), NULL);
3796
3797#ifdef FEAT_DND
Bram Moolenaara76638f2010-06-05 12:49:46 +02003798 /* Set up for receiving DND items. */
3799 gui_gtk_set_dnd_targets();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800
3801 gtk_signal_connect(GTK_OBJECT(gui.drawarea), "drag_data_received",
3802 GTK_SIGNAL_FUNC(drag_data_received_cb), NULL);
3803#endif
3804
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 /* With GTK+ 2, we need to iconify the window before calling show()
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003806 * to avoid mapping the window for a short time. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 if (found_iconic_arg && gtk_socket_id == 0)
3808 gui_mch_iconify();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809
3810 {
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003811#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 unsigned long menu_handler = 0;
3813# ifdef FEAT_TOOLBAR
3814 unsigned long tool_handler = 0;
3815# endif
3816 /*
3817 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
3818 * show when restoring the saved layout configuration. We can't just
3819 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
3820 * a toplevel window and thus will be realized immediately. Instead,
3821 * connect signal handlers to hide the widgets just after they've been
3822 * marked visible, but before the main window is realized.
3823 */
3824 if (using_gnome && vim_strchr(p_go, GO_MENUS) == NULL)
3825 menu_handler = g_signal_connect_after(gui.menubar_h, "show",
3826 G_CALLBACK(&gtk_widget_hide),
3827 NULL);
3828# ifdef FEAT_TOOLBAR
3829 if (using_gnome && vim_strchr(p_go, GO_TOOLBAR) == NULL
3830 && (toolbar_flags & (TOOLBAR_TEXT | TOOLBAR_ICONS)))
3831 tool_handler = g_signal_connect_after(gui.toolbar_h, "show",
3832 G_CALLBACK(&gtk_widget_hide),
3833 NULL);
3834# endif
3835#endif
3836 gtk_widget_show(gui.mainwin);
3837
Bram Moolenaar182c5be2010-06-25 05:37:59 +02003838#if defined(FEAT_GUI_GNOME) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 if (menu_handler != 0)
3840 g_signal_handler_disconnect(gui.menubar_h, menu_handler);
3841# ifdef FEAT_TOOLBAR
3842 if (tool_handler != 0)
3843 g_signal_handler_disconnect(gui.toolbar_h, tool_handler);
3844# endif
3845#endif
3846 }
3847
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return OK;
3849}
3850
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003853gui_mch_exit(int rc UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 if (gui.mainwin != NULL)
3856 gtk_widget_destroy(gui.mainwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857}
3858
3859/*
3860 * Get the position of the top left corner of the window.
3861 */
3862 int
3863gui_mch_get_winpos(int *x, int *y)
3864{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 gtk_window_get_position(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 return OK;
3867}
3868
3869/*
3870 * Set the position of the top left corner of the window to the given
3871 * coordinates.
3872 */
3873 void
3874gui_mch_set_winpos(int x, int y)
3875{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 gtk_window_move(GTK_WINDOW(gui.mainwin), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877}
3878
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879#if 0
3880static int resize_idle_installed = FALSE;
3881/*
3882 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
3883 * the shell size doesn't exceed the window size, i.e. if the window manager
3884 * ignored our size request. Usually this happens if the window is maximized.
3885 *
3886 * FIXME: It'd be nice if we could find a little more orthodox solution.
3887 * See also the remark below in gui_mch_set_shellsize().
3888 *
3889 * DISABLED: When doing ":set lines+=1" this function would first invoke
3890 * gui_resize_shell() with the old size, then the normal callback would
3891 * report the new size through form_configure_event(). That caused the window
3892 * layout to be messed up.
3893 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 static gboolean
3895force_shell_resize_idle(gpointer data)
3896{
3897 if (gui.mainwin != NULL
3898 && GTK_WIDGET_REALIZED(gui.mainwin)
3899 && GTK_WIDGET_VISIBLE(gui.mainwin))
3900 {
3901 int width;
3902 int height;
3903
3904 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &width, &height);
3905
3906 width -= get_menu_tool_width();
3907 height -= get_menu_tool_height();
3908
3909 gui_resize_shell(width, height);
3910 }
3911
3912 resize_idle_installed = FALSE;
3913 return FALSE; /* don't call me again */
3914}
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
Bram Moolenaar09736232009-09-23 16:14:49 +00003917/*
3918 * Return TRUE if the main window is maximized.
3919 */
3920 int
3921gui_mch_maximized()
3922{
3923 return (gui.mainwin != NULL && gui.mainwin->window != NULL
3924 && (gdk_window_get_state(gui.mainwin->window)
3925 & GDK_WINDOW_STATE_MAXIMIZED));
3926}
3927
3928/*
3929 * Unmaximize the main window
3930 */
3931 void
3932gui_mch_unmaximize()
3933{
3934 if (gui.mainwin != NULL)
3935 gtk_window_unmaximize(GTK_WINDOW(gui.mainwin));
3936}
Bram Moolenaar09736232009-09-23 16:14:49 +00003937
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938/*
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02003939 * Called when the font changed while the window is maximized. Compute the
3940 * new Rows and Columns. This is like resizing the window.
3941 */
3942 void
3943gui_mch_newfont()
3944{
3945 int w, h;
3946
3947 gtk_window_get_size(GTK_WINDOW(gui.mainwin), &w, &h);
3948 w -= get_menu_tool_width();
3949 h -= get_menu_tool_height();
3950 gui_resize_shell(w, h);
3951}
3952
3953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 * Set the windows size.
3955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 void
3957gui_mch_set_shellsize(int width, int height,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00003958 int min_width UNUSED, int min_height UNUSED,
3959 int base_width UNUSED, int base_height UNUSED,
3960 int direction UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 /* give GTK+ a chance to put all widget's into place */
3963 gui_mch_update();
3964
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003965 /* this will cause the proper resizement to happen too */
3966 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003967 update_window_manager_hints(0, 0);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003968
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 /* With GTK+ 2, changing the size of the form widget doesn't resize
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003970 * the window. So let's do it the other way around and resize the
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 * main window instead. */
3972 width += get_menu_tool_width();
3973 height += get_menu_tool_height();
3974
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003975 if (gtk_socket_id == 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003976 gtk_window_resize(GTK_WINDOW(gui.mainwin), width, height);
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00003977 else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003978 update_window_manager_hints(width, height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003980# if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (!resize_idle_installed)
3982 {
3983 g_idle_add_full(GDK_PRIORITY_EVENTS + 10,
3984 &force_shell_resize_idle, NULL, NULL);
3985 resize_idle_installed = TRUE;
3986 }
Bram Moolenaarfff2bee2010-05-15 13:56:02 +02003987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 /*
3989 * Wait until all events are processed to prevent a crash because the
3990 * real size of the drawing area doesn't reflect Vim's internal ideas.
3991 *
3992 * This is a bit of a hack, since Vim is a terminal application with a GUI
3993 * on top, while the GUI expects to be the boss.
3994 */
3995 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998
3999/*
4000 * The screen size is used to make sure the initial window doesn't get bigger
4001 * than the screen. This subtracts some room for menubar, toolbar and window
4002 * decorations.
4003 */
4004 void
4005gui_mch_get_screen_dimensions(int *screen_w, int *screen_h)
4006{
4007#ifdef HAVE_GTK_MULTIHEAD
4008 GdkScreen* screen;
4009
4010 if (gui.mainwin != NULL && gtk_widget_has_screen(gui.mainwin))
4011 screen = gtk_widget_get_screen(gui.mainwin);
4012 else
4013 screen = gdk_screen_get_default();
4014
4015 *screen_w = gdk_screen_get_width(screen);
4016 *screen_h = gdk_screen_get_height(screen) - p_ghr;
4017#else
4018 *screen_w = gdk_screen_width();
4019 /* Subtract 'guiheadroom' from the height to allow some room for the
4020 * window manager (task list and window title bar). */
4021 *screen_h = gdk_screen_height() - p_ghr;
4022#endif
4023
4024 /*
4025 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4026 * the toolbar and menubar for GTK, we subtract them from the screen
Bram Moolenaar64404472010-06-26 06:24:45 +02004027 * height, so that the window size can be made to fit on the screen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 * This should be completely changed later.
4029 */
4030 *screen_w -= get_menu_tool_width();
4031 *screen_h -= get_menu_tool_height();
4032}
4033
4034#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004036gui_mch_settitle(char_u *title, char_u *icon UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (title != NULL && output_conv.vc_type != CONV_NONE)
4039 title = string_convert(&output_conv, title, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 gtk_window_set_title(GTK_WINDOW(gui.mainwin), (const char *)title);
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (output_conv.vc_type != CONV_NONE)
4044 vim_free(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045}
4046#endif /* FEAT_TITLE */
4047
4048#if defined(FEAT_MENU) || defined(PROTO)
4049 void
4050gui_mch_enable_menu(int showit)
4051{
4052 GtkWidget *widget;
4053
4054# ifdef FEAT_GUI_GNOME
4055 if (using_gnome)
4056 widget = gui.menubar_h;
4057 else
4058# endif
4059 widget = gui.menubar;
4060
Bram Moolenaar18144c82006-04-12 21:52:12 +00004061 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4062 if (!showit != !GTK_WIDGET_VISIBLE(widget) && !gui.starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
4064 if (showit)
4065 gtk_widget_show(widget);
4066 else
4067 gtk_widget_hide(widget);
4068
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004069 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071}
4072#endif /* FEAT_MENU */
4073
4074#if defined(FEAT_TOOLBAR) || defined(PROTO)
4075 void
4076gui_mch_show_toolbar(int showit)
4077{
4078 GtkWidget *widget;
4079
4080 if (gui.toolbar == NULL)
4081 return;
4082
4083# ifdef FEAT_GUI_GNOME
4084 if (using_gnome)
4085 widget = gui.toolbar_h;
4086 else
4087# endif
4088 widget = gui.toolbar;
4089
4090 if (showit)
4091 set_toolbar_style(GTK_TOOLBAR(gui.toolbar));
4092
4093 if (!showit != !GTK_WIDGET_VISIBLE(widget))
4094 {
4095 if (showit)
4096 gtk_widget_show(widget);
4097 else
4098 gtk_widget_hide(widget);
4099
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00004100 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 }
4102}
4103#endif /* FEAT_TOOLBAR */
4104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105/*
4106 * Check if a given font is a CJK font. This is done in a very crude manner. It
4107 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4108 * font. Consequently, this function cannot be used as a general purpose check
4109 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4110 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4111 */
4112 static int
4113is_cjk_font(PangoFontDescription *font_desc)
4114{
4115 static const char * const cjk_langs[] =
4116 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4117
4118 PangoFont *font;
4119 unsigned i;
4120 int is_cjk = FALSE;
4121
4122 font = pango_context_load_font(gui.text_context, font_desc);
4123
4124 if (font == NULL)
4125 return FALSE;
4126
4127 for (i = 0; !is_cjk && i < G_N_ELEMENTS(cjk_langs); ++i)
4128 {
4129 PangoCoverage *coverage;
4130 gunichar uc;
4131
4132 coverage = pango_font_get_coverage(
4133 font, pango_language_from_string(cjk_langs[i]));
4134
4135 if (coverage != NULL)
4136 {
4137 uc = (cjk_langs[i][0] == 'k') ? 0xAC00 : 0x4E00;
4138 is_cjk = (pango_coverage_get(coverage, uc) == PANGO_COVERAGE_EXACT);
4139 pango_coverage_unref(coverage);
4140 }
4141 }
4142
4143 g_object_unref(font);
4144
4145 return is_cjk;
4146}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147
Bram Moolenaar231334e2005-07-25 20:46:57 +00004148/*
4149 * Adjust gui.char_height (after 'linespace' was changed).
4150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 int
Bram Moolenaar231334e2005-07-25 20:46:57 +00004152gui_mch_adjust_charheight(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 PangoFontMetrics *metrics;
4155 int ascent;
4156 int descent;
4157
4158 metrics = pango_context_get_metrics(gui.text_context, gui.norm_font,
4159 pango_context_get_language(gui.text_context));
4160 ascent = pango_font_metrics_get_ascent(metrics);
4161 descent = pango_font_metrics_get_descent(metrics);
4162
4163 pango_font_metrics_unref(metrics);
4164
4165 gui.char_height = (ascent + descent + PANGO_SCALE - 1) / PANGO_SCALE
Bram Moolenaar231334e2005-07-25 20:46:57 +00004166 + p_linespace;
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00004167 /* LINTED: avoid warning: bitwise operation on signed value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 gui.char_ascent = PANGO_PIXELS(ascent + p_linespace * PANGO_SCALE / 2);
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 /* A not-positive value of char_height may crash Vim. Only happens
4171 * if 'linespace' is negative (which does make sense sometimes). */
4172 gui.char_ascent = MAX(gui.char_ascent, 0);
4173 gui.char_height = MAX(gui.char_height, gui.char_ascent + 1);
4174
4175 return OK;
4176}
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178/*
4179 * Put up a font dialog and return the selected font name in allocated memory.
4180 * "oldval" is the previous value. Return NULL when cancelled.
4181 * This should probably go into gui_gtk.c. Hmm.
4182 * FIXME:
4183 * The GTK2 font selection dialog has no filtering API. So we could either
4184 * a) implement our own (possibly copying the code from somewhere else) or
4185 * b) just live with it.
4186 */
4187 char_u *
4188gui_mch_font_dialog(char_u *oldval)
4189{
4190 GtkWidget *dialog;
4191 int response;
4192 char_u *fontname = NULL;
4193 char_u *oldname;
4194
4195 dialog = gtk_font_selection_dialog_new(NULL);
4196
4197 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
4198 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
4199
4200 if (oldval != NULL && oldval[0] != NUL)
4201 {
4202 if (output_conv.vc_type != CONV_NONE)
4203 oldname = string_convert(&output_conv, oldval, NULL);
4204 else
4205 oldname = oldval;
4206
4207 /* Annoying bug in GTK (or Pango): if the font name does not include a
4208 * size, zero is used. Use default point size ten. */
4209 if (!vim_isdigit(oldname[STRLEN(oldname) - 1]))
4210 {
4211 char_u *p = vim_strnsave(oldname, STRLEN(oldname) + 3);
4212
4213 if (p != NULL)
4214 {
4215 STRCPY(p + STRLEN(p), " 10");
4216 if (oldname != oldval)
4217 vim_free(oldname);
4218 oldname = p;
4219 }
4220 }
4221
4222 gtk_font_selection_dialog_set_font_name(
4223 GTK_FONT_SELECTION_DIALOG(dialog), (const char *)oldname);
4224
4225 if (oldname != oldval)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004226 vim_free(oldname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
Bram Moolenaarbef9d832009-09-11 13:46:41 +00004228 else
4229 gtk_font_selection_dialog_set_font_name(
4230 GTK_FONT_SELECTION_DIALOG(dialog), DEFAULT_FONT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231
4232 response = gtk_dialog_run(GTK_DIALOG(dialog));
4233
4234 if (response == GTK_RESPONSE_OK)
4235 {
4236 char *name;
4237
4238 name = gtk_font_selection_dialog_get_font_name(
4239 GTK_FONT_SELECTION_DIALOG(dialog));
4240 if (name != NULL)
4241 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004242 char_u *p;
4243
4244 /* Apparently some font names include a comma, need to escape
4245 * that, because in 'guifont' it separates names. */
4246 p = vim_strsave_escaped((char_u *)name, (char_u *)",");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 g_free(name);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00004248 if (p != NULL && input_conv.vc_type != CONV_NONE)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004249 {
4250 fontname = string_convert(&input_conv, p, NULL);
4251 vim_free(p);
4252 }
4253 else
4254 fontname = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257
4258 if (response != GTK_RESPONSE_NONE)
4259 gtk_widget_destroy(dialog);
4260
4261 return fontname;
4262}
4263
4264/*
4265 * Some monospace fonts don't support a bold weight, and fall back
4266 * silently to the regular weight. But this is no good since our text
4267 * drawing function can emulate bold by overstriking. So let's try
4268 * to detect whether bold weight is actually available and emulate it
4269 * otherwise.
4270 *
4271 * Note that we don't need to check for italic style since Xft can
4272 * emulate italic on its own, provided you have a proper fontconfig
4273 * setup. We wouldn't be able to emulate it in Vim anyway.
4274 */
4275 static void
4276get_styled_font_variants(void)
4277{
4278 PangoFontDescription *bold_font_desc;
4279 PangoFont *plain_font;
4280 PangoFont *bold_font;
4281
4282 gui.font_can_bold = FALSE;
4283
4284 plain_font = pango_context_load_font(gui.text_context, gui.norm_font);
4285
4286 if (plain_font == NULL)
4287 return;
4288
4289 bold_font_desc = pango_font_description_copy_static(gui.norm_font);
4290 pango_font_description_set_weight(bold_font_desc, PANGO_WEIGHT_BOLD);
4291
4292 bold_font = pango_context_load_font(gui.text_context, bold_font_desc);
4293 /*
4294 * The comparison relies on the unique handle nature of a PangoFont*,
4295 * i.e. it's assumed that a different PangoFont* won't refer to the
4296 * same font. Seems to work, and failing here isn't critical anyway.
4297 */
4298 if (bold_font != NULL)
4299 {
4300 gui.font_can_bold = (bold_font != plain_font);
4301 g_object_unref(bold_font);
4302 }
4303
4304 pango_font_description_free(bold_font_desc);
4305 g_object_unref(plain_font);
4306}
4307
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308static PangoEngineShape *default_shape_engine = NULL;
4309
4310/*
4311 * Create a map from ASCII characters in the range [32,126] to glyphs
4312 * of the current font. This is used by gui_gtk2_draw_string() to skip
4313 * the itemize and shaping process for the most common case.
4314 */
4315 static void
4316ascii_glyph_table_init(void)
4317{
4318 char_u ascii_chars[128];
4319 PangoAttrList *attr_list;
4320 GList *item_list;
4321 int i;
4322
4323 if (gui.ascii_glyphs != NULL)
4324 pango_glyph_string_free(gui.ascii_glyphs);
4325 if (gui.ascii_font != NULL)
4326 g_object_unref(gui.ascii_font);
4327
4328 gui.ascii_glyphs = NULL;
4329 gui.ascii_font = NULL;
4330
4331 /* For safety, fill in question marks for the control characters. */
4332 for (i = 0; i < 32; ++i)
4333 ascii_chars[i] = '?';
4334 for (; i < 127; ++i)
4335 ascii_chars[i] = i;
4336 ascii_chars[i] = '?';
4337
4338 attr_list = pango_attr_list_new();
4339 item_list = pango_itemize(gui.text_context, (const char *)ascii_chars,
4340 0, sizeof(ascii_chars), attr_list, NULL);
4341
4342 if (item_list != NULL && item_list->next == NULL) /* play safe */
4343 {
4344 PangoItem *item;
4345 int width;
4346
4347 item = (PangoItem *)item_list->data;
4348 width = gui.char_width * PANGO_SCALE;
4349
4350 /* Remember the shape engine used for ASCII. */
4351 default_shape_engine = item->analysis.shape_engine;
4352
4353 gui.ascii_font = item->analysis.font;
4354 g_object_ref(gui.ascii_font);
4355
4356 gui.ascii_glyphs = pango_glyph_string_new();
4357
4358 pango_shape((const char *)ascii_chars, sizeof(ascii_chars),
4359 &item->analysis, gui.ascii_glyphs);
4360
4361 g_return_if_fail(gui.ascii_glyphs->num_glyphs == sizeof(ascii_chars));
4362
4363 for (i = 0; i < gui.ascii_glyphs->num_glyphs; ++i)
4364 {
4365 PangoGlyphGeometry *geom;
4366
4367 geom = &gui.ascii_glyphs->glyphs[i].geometry;
4368 geom->x_offset += MAX(0, width - geom->width) / 2;
4369 geom->width = width;
4370 }
4371 }
4372
4373 g_list_foreach(item_list, (GFunc)&pango_item_free, NULL);
4374 g_list_free(item_list);
4375 pango_attr_list_unref(attr_list);
4376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378/*
4379 * Initialize Vim to use the font or fontset with the given name.
4380 * Return FAIL if the font could not be loaded, OK otherwise.
4381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 int
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004383gui_mch_init_font(char_u *font_name, int fontset UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 PangoFontDescription *font_desc;
4386 PangoLayout *layout;
4387 int width;
4388
4389 /* If font_name is NULL, this means to use the default, which should
4390 * be present on all proper Pango/fontconfig installations. */
4391 if (font_name == NULL)
4392 font_name = (char_u *)DEFAULT_FONT;
4393
4394 font_desc = gui_mch_get_font(font_name, FALSE);
4395
4396 if (font_desc == NULL)
4397 return FAIL;
4398
4399 gui_mch_free_font(gui.norm_font);
4400 gui.norm_font = font_desc;
4401
4402 pango_context_set_font_description(gui.text_context, font_desc);
4403
4404 layout = pango_layout_new(gui.text_context);
4405 pango_layout_set_text(layout, "MW", 2);
4406 pango_layout_get_size(layout, &width, NULL);
4407 /*
4408 * Set char_width to half the width obtained from pango_layout_get_size()
4409 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
4410 * Pango to use the same width for both non-CJK characters (e.g. Latin
4411 * letters and numbers) and CJK characters. This results in 's p a c e d
4412 * o u t' rendering when a CJK 'fixed width' font is used. To work around
4413 * that, divide the width returned by Pango by 2 if cjk_width is equal to
4414 * width for CJK fonts.
4415 *
4416 * For related bugs, see:
4417 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
4418 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
4419 *
4420 * With this, for all four of the following cases, Vim works fine:
4421 * guifont=CJK_fixed_width_font
4422 * guifont=Non_CJK_fixed_font
4423 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
4424 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
4425 */
4426 if (is_cjk_font(gui.norm_font))
4427 {
4428 int cjk_width;
4429
4430 /* Measure the text extent of U+4E00 and U+4E8C */
4431 pango_layout_set_text(layout, "\344\270\200\344\272\214", -1);
4432 pango_layout_get_size(layout, &cjk_width, NULL);
4433
4434 if (width == cjk_width) /* Xft not patched */
4435 width /= 2;
4436 }
4437 g_object_unref(layout);
4438
4439 gui.char_width = (width / 2 + PANGO_SCALE - 1) / PANGO_SCALE;
4440
4441 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
4442 if (gui.char_width <= 0)
4443 gui.char_width = 8;
4444
Bram Moolenaar231334e2005-07-25 20:46:57 +00004445 gui_mch_adjust_charheight();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 /* Set the fontname, which will be used for information purposes */
4448 hl_set_font_name(font_name);
4449
4450 get_styled_font_variants();
4451 ascii_glyph_table_init();
4452
4453 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
4454 if (gui.wide_font != NULL
4455 && pango_font_description_equal(gui.norm_font, gui.wide_font))
4456 {
4457 pango_font_description_free(gui.wide_font);
4458 gui.wide_font = NULL;
4459 }
4460
Bram Moolenaare161c792009-11-03 17:13:59 +00004461 if (gui_mch_maximized())
4462 {
Bram Moolenaare161c792009-11-03 17:13:59 +00004463 /* Update lines and columns in accordance with the new font, keep the
4464 * window maximized. */
Bram Moolenaar12bc1b52011-08-10 17:44:45 +02004465 gui_mch_newfont();
Bram Moolenaare161c792009-11-03 17:13:59 +00004466 }
4467 else
Bram Moolenaare161c792009-11-03 17:13:59 +00004468 {
4469 /* Preserve the logical dimensions of the screen. */
4470 update_window_manager_hints(0, 0);
4471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 return OK;
4474}
4475
4476/*
4477 * Get a reference to the font "name".
4478 * Return zero for failure.
4479 */
4480 GuiFont
4481gui_mch_get_font(char_u *name, int report_error)
4482{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 PangoFontDescription *font;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484
4485 /* can't do this when GUI is not running */
4486 if (!gui.in_use || name == NULL)
4487 return NULL;
4488
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 if (output_conv.vc_type != CONV_NONE)
4490 {
4491 char_u *buf;
4492
4493 buf = string_convert(&output_conv, name, NULL);
4494 if (buf != NULL)
4495 {
4496 font = pango_font_description_from_string((const char *)buf);
4497 vim_free(buf);
4498 }
4499 else
4500 font = NULL;
4501 }
4502 else
4503 font = pango_font_description_from_string((const char *)name);
4504
4505 if (font != NULL)
4506 {
4507 PangoFont *real_font;
4508
4509 /* pango_context_load_font() bails out if no font size is set */
4510 if (pango_font_description_get_size(font) <= 0)
4511 pango_font_description_set_size(font, 10 * PANGO_SCALE);
4512
4513 real_font = pango_context_load_font(gui.text_context, font);
4514
4515 if (real_font == NULL)
4516 {
4517 pango_font_description_free(font);
4518 font = NULL;
4519 }
4520 else
4521 g_object_unref(real_font);
4522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523
4524 if (font == NULL)
4525 {
4526 if (report_error)
Bram Moolenaarc93e7912008-07-08 10:46:08 +00004527 EMSG2(_((char *)e_font), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 return NULL;
4529 }
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return font;
4532}
4533
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004534#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004535/*
4536 * Return the name of font "font" in allocated memory.
4537 */
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004538 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004539gui_mch_get_fontname(GuiFont font, char_u *name UNUSED)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004540{
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004541 if (font != NOFONT)
4542 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004543 char *pangoname = pango_font_description_to_string(font);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004544
Bram Moolenaar89d40322006-08-29 15:30:07 +00004545 if (pangoname != NULL)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004546 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00004547 char_u *s = vim_strsave((char_u *)pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004548
Bram Moolenaar89d40322006-08-29 15:30:07 +00004549 g_free(pangoname);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004550 return s;
4551 }
4552 }
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004553 return NULL;
4554}
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004555#endif
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557/*
4558 * If a font is not going to be used, free its structure.
4559 */
4560 void
4561gui_mch_free_font(GuiFont font)
4562{
4563 if (font != NOFONT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 pango_font_description_free(font);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565}
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567/*
4568 * Return the Pixel value (color) for the given color name. This routine was
4569 * pretty much taken from example code in the Silicon Graphics OSF/Motif
4570 * Programmer's Guide.
4571 * Return INVALCOLOR for error.
4572 */
4573 guicolor_T
4574gui_mch_get_color(char_u *name)
4575{
4576 /* A number of colors that some X11 systems don't have */
4577 static const char *const vimnames[][2] =
4578 {
Bram Moolenaarb21e5842006-04-16 18:30:08 +00004579 {"LightRed", "#FFBBBB"},
4580 {"LightGreen", "#88FF88"},
4581 {"LightMagenta","#FFBBFF"},
4582 {"DarkCyan", "#008888"},
4583 {"DarkBlue", "#0000BB"},
4584 {"DarkRed", "#BB0000"},
4585 {"DarkMagenta", "#BB00BB"},
4586 {"DarkGrey", "#BBBBBB"},
4587 {"DarkYellow", "#BBBB00"},
4588 {"Gray10", "#1A1A1A"},
4589 {"Grey10", "#1A1A1A"},
4590 {"Gray20", "#333333"},
4591 {"Grey20", "#333333"},
4592 {"Gray30", "#4D4D4D"},
4593 {"Grey30", "#4D4D4D"},
4594 {"Gray40", "#666666"},
4595 {"Grey40", "#666666"},
4596 {"Gray50", "#7F7F7F"},
4597 {"Grey50", "#7F7F7F"},
4598 {"Gray60", "#999999"},
4599 {"Grey60", "#999999"},
4600 {"Gray70", "#B3B3B3"},
4601 {"Grey70", "#B3B3B3"},
4602 {"Gray80", "#CCCCCC"},
4603 {"Grey80", "#CCCCCC"},
4604 {"Gray90", "#E5E5E5"},
4605 {"Grey90", "#E5E5E5"},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {NULL, NULL}
4607 };
4608
4609 if (!gui.in_use) /* can't do this when GUI not running */
4610 return INVALCOLOR;
4611
4612 while (name != NULL)
4613 {
4614 GdkColor color;
4615 int parsed;
4616 int i;
4617
4618 parsed = gdk_color_parse((const char *)name, &color);
4619
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 if (parsed)
4621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui.drawarea),
4623 &color, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 return (guicolor_T)color.pixel;
4625 }
4626 /* add a few builtin names and try again */
4627 for (i = 0; ; ++i)
4628 {
4629 if (vimnames[i][0] == NULL)
4630 {
4631 name = NULL;
4632 break;
4633 }
4634 if (STRICMP(name, vimnames[i][0]) == 0)
4635 {
4636 name = (char_u *)vimnames[i][1];
4637 break;
4638 }
4639 }
4640 }
4641
4642 return INVALCOLOR;
4643}
4644
4645/*
4646 * Set the current text foreground color.
4647 */
4648 void
4649gui_mch_set_fg_color(guicolor_T color)
4650{
4651 gui.fgcolor->pixel = (unsigned long)color;
4652}
4653
4654/*
4655 * Set the current text background color.
4656 */
4657 void
4658gui_mch_set_bg_color(guicolor_T color)
4659{
4660 gui.bgcolor->pixel = (unsigned long)color;
4661}
4662
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004663/*
4664 * Set the current text special color.
4665 */
4666 void
4667gui_mch_set_sp_color(guicolor_T color)
4668{
4669 gui.spcolor->pixel = (unsigned long)color;
4670}
4671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672/*
4673 * Function-like convenience macro for the sake of efficiency.
4674 */
4675#define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
4676 G_STMT_START{ \
4677 PangoAttribute *tmp_attr_; \
4678 tmp_attr_ = (Attribute); \
4679 tmp_attr_->start_index = (Start); \
4680 tmp_attr_->end_index = (End); \
4681 pango_attr_list_insert((AttrList), tmp_attr_); \
4682 }G_STMT_END
4683
4684 static void
4685apply_wide_font_attr(char_u *s, int len, PangoAttrList *attr_list)
4686{
4687 char_u *start = NULL;
4688 char_u *p;
4689 int uc;
4690
4691 for (p = s; p < s + len; p += utf_byte2len(*p))
4692 {
4693 uc = utf_ptr2char(p);
4694
4695 if (start == NULL)
4696 {
4697 if (uc >= 0x80 && utf_char2cells(uc) == 2)
4698 start = p;
4699 }
4700 else if (uc < 0x80 /* optimization shortcut */
4701 || (utf_char2cells(uc) != 2 && !utf_iscomposing(uc)))
4702 {
4703 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4704 attr_list, start - s, p - s);
4705 start = NULL;
4706 }
4707 }
4708
4709 if (start != NULL)
4710 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui.wide_font),
4711 attr_list, start - s, len);
4712}
4713
4714 static int
4715count_cluster_cells(char_u *s, PangoItem *item,
4716 PangoGlyphString* glyphs, int i,
4717 int *cluster_width,
4718 int *last_glyph_rbearing)
4719{
4720 char_u *p;
4721 int next; /* glyph start index of next cluster */
4722 int start, end; /* string segment of current cluster */
4723 int width; /* real cluster width in Pango units */
4724 int uc;
4725 int cellcount = 0;
4726
4727 width = glyphs->glyphs[i].geometry.width;
4728
4729 for (next = i + 1; next < glyphs->num_glyphs; ++next)
4730 {
4731 if (glyphs->glyphs[next].attr.is_cluster_start)
4732 break;
4733 else if (glyphs->glyphs[next].geometry.width > width)
4734 width = glyphs->glyphs[next].geometry.width;
4735 }
4736
4737 start = item->offset + glyphs->log_clusters[i];
4738 end = item->offset + ((next < glyphs->num_glyphs) ?
4739 glyphs->log_clusters[next] : item->length);
4740
4741 for (p = s + start; p < s + end; p += utf_byte2len(*p))
4742 {
4743 uc = utf_ptr2char(p);
4744 if (uc < 0x80)
4745 ++cellcount;
4746 else if (!utf_iscomposing(uc))
4747 cellcount += utf_char2cells(uc);
4748 }
4749
4750 if (last_glyph_rbearing != NULL
4751 && cellcount > 0 && next == glyphs->num_glyphs)
4752 {
4753 PangoRectangle ink_rect;
4754 /*
4755 * If a certain combining mark had to be taken from a non-monospace
4756 * font, we have to compensate manually by adapting x_offset according
4757 * to the ink extents of the previous glyph.
4758 */
4759 pango_font_get_glyph_extents(item->analysis.font,
4760 glyphs->glyphs[i].glyph,
4761 &ink_rect, NULL);
4762
4763 if (PANGO_RBEARING(ink_rect) > 0)
4764 *last_glyph_rbearing = PANGO_RBEARING(ink_rect);
4765 }
4766
4767 if (cellcount > 0)
4768 *cluster_width = width;
4769
4770 return cellcount;
4771}
4772
4773/*
4774 * If there are only combining characters in the cluster, we cannot just
4775 * change the width of the previous glyph since there is none. Therefore
4776 * some guesswork is needed.
4777 *
4778 * If ink_rect.x is negative Pango apparently has taken care of the composing
4779 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
4780 * to problems with composing from different fonts we still need to fine-tune
Bram Moolenaar64404472010-06-26 06:24:45 +02004781 * x_offset to avoid ugliness.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 *
4783 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
4784 * the position of the previous glyph. Apparently this happens only with old
4785 * X fonts which don't provide the special combining information needed by
4786 * Pango.
4787 */
4788 static void
4789setup_zero_width_cluster(PangoItem *item, PangoGlyphInfo *glyph,
4790 int last_cellcount, int last_cluster_width,
4791 int last_glyph_rbearing)
4792{
4793 PangoRectangle ink_rect;
4794 PangoRectangle logical_rect;
4795 int width;
4796
4797 width = last_cellcount * gui.char_width * PANGO_SCALE;
4798 glyph->geometry.x_offset = -width + MAX(0, width - last_cluster_width) / 2;
4799 glyph->geometry.width = 0;
4800
4801 pango_font_get_glyph_extents(item->analysis.font,
4802 glyph->glyph,
4803 &ink_rect, &logical_rect);
4804 if (ink_rect.x < 0)
4805 {
4806 glyph->geometry.x_offset += last_glyph_rbearing;
4807 glyph->geometry.y_offset = logical_rect.height
4808 - (gui.char_height - p_linespace) * PANGO_SCALE;
4809 }
Bram Moolenaar706e84b2010-08-07 15:46:45 +02004810 else
4811 /* If the accent width is smaller than the cluster width, position it
4812 * in the middle. */
4813 glyph->geometry.x_offset = -width + MAX(0, width - ink_rect.width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814}
4815
4816 static void
4817draw_glyph_string(int row, int col, int num_cells, int flags,
4818 PangoFont *font, PangoGlyphString *glyphs)
4819{
4820 if (!(flags & DRAW_TRANSP))
4821 {
4822 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
4823
4824 gdk_draw_rectangle(gui.drawarea->window,
4825 gui.text_gc,
4826 TRUE,
4827 FILL_X(col),
4828 FILL_Y(row),
4829 num_cells * gui.char_width,
4830 gui.char_height);
4831 }
4832
4833 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4834
4835 gdk_draw_glyphs(gui.drawarea->window,
4836 gui.text_gc,
4837 font,
4838 TEXT_X(col),
4839 TEXT_Y(row),
4840 glyphs);
4841
4842 /* redraw the contents with an offset of 1 to emulate bold */
4843 if ((flags & DRAW_BOLD) && !gui.font_can_bold)
4844 gdk_draw_glyphs(gui.drawarea->window,
4845 gui.text_gc,
4846 font,
4847 TEXT_X(col) + 1,
4848 TEXT_Y(row),
4849 glyphs);
4850}
4851
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004852/*
4853 * Draw underline and undercurl at the bottom of the character cell.
4854 */
4855 static void
4856draw_under(int flags, int row, int col, int cells)
4857{
4858 int i;
4859 int offset;
Bram Moolenaarb85cb212009-05-17 14:24:23 +00004860 static const int val[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
Bram Moolenaarf36d3692005-03-15 22:48:14 +00004861 int y = FILL_Y(row + 1) - 1;
4862
4863 /* Undercurl: draw curl at the bottom of the character cell. */
4864 if (flags & DRAW_UNDERC)
4865 {
4866 gdk_gc_set_foreground(gui.text_gc, gui.spcolor);
4867 for (i = FILL_X(col); i < FILL_X(col + cells); ++i)
4868 {
4869 offset = val[i % 8];
4870 gdk_draw_point(gui.drawarea->window, gui.text_gc, i, y - offset);
4871 }
4872 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
4873 }
4874
4875 /* Underline: draw a line at the bottom of the character cell. */
4876 if (flags & DRAW_UNDERL)
4877 {
4878 /* When p_linespace is 0, overwrite the bottom row of pixels.
4879 * Otherwise put the line just below the character. */
4880 if (p_linespace > 1)
4881 y -= p_linespace - 1;
4882 gdk_draw_line(gui.drawarea->window, gui.text_gc,
4883 FILL_X(col), y,
4884 FILL_X(col + cells) - 1, y);
4885 }
4886}
4887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 int
4889gui_gtk2_draw_string(int row, int col, char_u *s, int len, int flags)
4890{
4891 GdkRectangle area; /* area for clip mask */
4892 PangoGlyphString *glyphs; /* glyphs of current item */
4893 int column_offset = 0; /* column offset in cells */
4894 int i;
4895 char_u *conv_buf = NULL; /* result of UTF-8 conversion */
4896 char_u *new_conv_buf;
4897 int convlen;
4898 char_u *sp, *bp;
4899 int plen;
4900
4901 if (gui.text_context == NULL || gui.drawarea->window == NULL)
4902 return len;
4903
4904 if (output_conv.vc_type != CONV_NONE)
4905 {
4906 /*
4907 * Convert characters from 'encoding' to 'termencoding', which is set
4908 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
4909 * prohibits changing this to something else than UTF-8 if the GUI is
4910 * in use.
4911 */
4912 convlen = len;
4913 conv_buf = string_convert(&output_conv, s, &convlen);
4914 g_return_val_if_fail(conv_buf != NULL, len);
4915
4916 /* Correct for differences in char width: some chars are
4917 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
4918 * compensate for that. */
4919 for (sp = s, bp = conv_buf; sp < s + len && bp < conv_buf + convlen; )
4920 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004921 plen = utf_ptr2len(bp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if ((*mb_ptr2cells)(sp) == 2 && utf_ptr2cells(bp) == 1)
4923 {
4924 new_conv_buf = alloc(convlen + 2);
4925 if (new_conv_buf == NULL)
4926 return len;
4927 plen += bp - conv_buf;
4928 mch_memmove(new_conv_buf, conv_buf, plen);
4929 new_conv_buf[plen] = ' ';
4930 mch_memmove(new_conv_buf + plen + 1, conv_buf + plen,
4931 convlen - plen + 1);
4932 vim_free(conv_buf);
4933 conv_buf = new_conv_buf;
4934 ++convlen;
4935 bp = conv_buf + plen;
4936 plen = 1;
4937 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004938 sp += (*mb_ptr2len)(sp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 bp += plen;
4940 }
4941 s = conv_buf;
4942 len = convlen;
4943 }
4944
4945 /*
4946 * Restrict all drawing to the current screen line in order to prevent
4947 * fuzzy font lookups from messing up the screen.
4948 */
4949 area.x = gui.border_offset;
4950 area.y = FILL_Y(row);
4951 area.width = gui.num_cols * gui.char_width;
4952 area.height = gui.char_height;
4953
4954 gdk_gc_set_clip_origin(gui.text_gc, 0, 0);
4955 gdk_gc_set_clip_rectangle(gui.text_gc, &area);
4956
4957 glyphs = pango_glyph_string_new();
4958
4959 /*
4960 * Optimization hack: If possible, skip the itemize and shaping process
4961 * for pure ASCII strings. This optimization is particularly effective
4962 * because Vim draws space characters to clear parts of the screen.
4963 */
4964 if (!(flags & DRAW_ITALIC)
4965 && !((flags & DRAW_BOLD) && gui.font_can_bold)
4966 && gui.ascii_glyphs != NULL)
4967 {
4968 char_u *p;
4969
4970 for (p = s; p < s + len; ++p)
4971 if (*p & 0x80)
4972 goto not_ascii;
4973
4974 pango_glyph_string_set_size(glyphs, len);
4975
4976 for (i = 0; i < len; ++i)
4977 {
4978 glyphs->glyphs[i] = gui.ascii_glyphs->glyphs[s[i]];
4979 glyphs->log_clusters[i] = i;
4980 }
4981
4982 draw_glyph_string(row, col, len, flags, gui.ascii_font, glyphs);
4983
4984 column_offset = len;
4985 }
4986 else
4987not_ascii:
4988 {
4989 PangoAttrList *attr_list;
4990 GList *item_list;
4991 int cluster_width;
4992 int last_glyph_rbearing;
4993 int cells = 0; /* cells occupied by current cluster */
4994
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00004995 /* Safety check: pango crashes when invoked with invalid utf-8
4996 * characters. */
4997 if (!utf_valid_string(s, s + len))
4998 {
4999 column_offset = len;
5000 goto skipitall;
5001 }
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 /* original width of the current cluster */
5004 cluster_width = PANGO_SCALE * gui.char_width;
5005
5006 /* right bearing of the last non-composing glyph */
5007 last_glyph_rbearing = PANGO_SCALE * gui.char_width;
5008
5009 attr_list = pango_attr_list_new();
5010
5011 /* If 'guifontwide' is set then use that for double-width characters.
5012 * Otherwise just go with 'guifont' and let Pango do its thing. */
5013 if (gui.wide_font != NULL)
5014 apply_wide_font_attr(s, len, attr_list);
5015
5016 if ((flags & DRAW_BOLD) && gui.font_can_bold)
5017 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD),
5018 attr_list, 0, len);
5019 if (flags & DRAW_ITALIC)
5020 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC),
5021 attr_list, 0, len);
5022 /*
5023 * Break the text into segments with consistent directional level
5024 * and shaping engine. Pure Latin text needs only a single segment,
5025 * so there's no need to worry about the loop's efficiency. Better
5026 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
5027 */
5028 item_list = pango_itemize(gui.text_context,
5029 (const char *)s, 0, len, attr_list, NULL);
5030
5031 while (item_list != NULL)
5032 {
5033 PangoItem *item;
5034 int item_cells = 0; /* item length in cells */
5035
5036 item = (PangoItem *)item_list->data;
5037 item_list = g_list_delete_link(item_list, item_list);
5038 /*
5039 * Increment the bidirectional embedding level by 1 if it is not
5040 * even. An odd number means the output will be RTL, but we don't
5041 * want that since Vim handles right-to-left text on its own. It
5042 * would probably be sufficient to just set level = 0, but you can
5043 * never know :)
5044 *
5045 * Unfortunately we can't take advantage of Pango's ability to
5046 * render both LTR and RTL at the same time. In order to support
5047 * that, Vim's main screen engine would have to make use of Pango
5048 * functionality.
5049 */
5050 item->analysis.level = (item->analysis.level + 1) & (~1U);
5051
5052 /* HACK: Overrule the shape engine, we don't want shaping to be
5053 * done, because drawing the cursor would change the display. */
5054 item->analysis.shape_engine = default_shape_engine;
5055
5056 pango_shape((const char *)s + item->offset, item->length,
5057 &item->analysis, glyphs);
5058 /*
5059 * Fixed-width hack: iterate over the array and assign a fixed
5060 * width to each glyph, thus overriding the choice made by the
5061 * shaping engine. We use utf_char2cells() to determine the
5062 * number of cells needed.
5063 *
5064 * Also perform all kind of dark magic to get composing
5065 * characters right (and pretty too of course).
5066 */
5067 for (i = 0; i < glyphs->num_glyphs; ++i)
5068 {
5069 PangoGlyphInfo *glyph;
5070
5071 glyph = &glyphs->glyphs[i];
5072
5073 if (glyph->attr.is_cluster_start)
5074 {
5075 int cellcount;
5076
5077 cellcount = count_cluster_cells(
5078 s, item, glyphs, i, &cluster_width,
5079 (item_list != NULL) ? &last_glyph_rbearing : NULL);
5080
5081 if (cellcount > 0)
5082 {
5083 int width;
5084
5085 width = cellcount * gui.char_width * PANGO_SCALE;
5086 glyph->geometry.x_offset +=
5087 MAX(0, width - cluster_width) / 2;
5088 glyph->geometry.width = width;
5089 }
5090 else
5091 {
5092 /* If there are only combining characters in the
5093 * cluster, we cannot just change the width of the
5094 * previous glyph since there is none. Therefore
5095 * some guesswork is needed. */
5096 setup_zero_width_cluster(item, glyph, cells,
5097 cluster_width,
5098 last_glyph_rbearing);
5099 }
5100
5101 item_cells += cellcount;
5102 cells = cellcount;
5103 }
5104 else if (i > 0)
5105 {
5106 int width;
5107
5108 /* There is a previous glyph, so we deal with combining
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005109 * characters the canonical way.
Bram Moolenaar96118f32010-08-08 14:40:37 +02005110 * In some circumstances Pango uses a positive x_offset,
5111 * then use the width of the previous glyph for this one
5112 * and set the previous width to zero.
5113 * Otherwise we get a negative x_offset, Pango has already
5114 * positioned the combining char, keep the widths as they
5115 * are.
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005116 * For both adjust the x_offset to position the glyph in
Bram Moolenaar96118f32010-08-08 14:40:37 +02005117 * the middle. */
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005118 if (glyph->geometry.x_offset >= 0)
Bram Moolenaar96118f32010-08-08 14:40:37 +02005119 {
5120 glyphs->glyphs[i].geometry.width =
5121 glyphs->glyphs[i - 1].geometry.width;
Bram Moolenaar706e84b2010-08-07 15:46:45 +02005122 glyphs->glyphs[i - 1].geometry.width = 0;
Bram Moolenaar96118f32010-08-08 14:40:37 +02005123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 width = cells * gui.char_width * PANGO_SCALE;
5125 glyph->geometry.x_offset +=
5126 MAX(0, width - cluster_width) / 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
5128 else /* i == 0 "cannot happen" */
5129 {
5130 glyph->geometry.width = 0;
5131 }
5132 }
5133
5134 /*** Aaaaand action! ***/
5135 draw_glyph_string(row, col + column_offset, item_cells,
5136 flags, item->analysis.font, glyphs);
5137
5138 pango_item_free(item);
5139
5140 column_offset += item_cells;
5141 }
5142
5143 pango_attr_list_unref(attr_list);
5144 }
5145
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00005146skipitall:
Bram Moolenaarf36d3692005-03-15 22:48:14 +00005147 /* Draw underline and undercurl. */
5148 draw_under(flags, row, col, column_offset);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149
5150 pango_glyph_string_free(glyphs);
5151 vim_free(conv_buf);
5152
5153 gdk_gc_set_clip_rectangle(gui.text_gc, NULL);
5154
5155 return column_offset;
5156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158/*
5159 * Return OK if the key with the termcap name "name" is supported.
5160 */
5161 int
5162gui_mch_haskey(char_u *name)
5163{
5164 int i;
5165
5166 for (i = 0; special_keys[i].key_sym != 0; i++)
5167 if (name[0] == special_keys[i].code0
5168 && name[1] == special_keys[i].code1)
5169 return OK;
5170 return FAIL;
5171}
5172
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01005173#if defined(FEAT_TITLE) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174/*
5175 * Return the text window-id and display. Only required for X-based GUI's
5176 */
5177 int
5178gui_get_x11_windis(Window *win, Display **dis)
5179{
5180 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5181 {
5182 *dis = GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5183 *win = GDK_WINDOW_XWINDOW(gui.mainwin->window);
5184 return OK;
5185 }
5186
5187 *dis = NULL;
5188 *win = 0;
5189 return FAIL;
5190}
5191#endif
5192
5193#if defined(FEAT_CLIENTSERVER) \
5194 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
5195
5196 Display *
5197gui_mch_get_display(void)
5198{
5199 if (gui.mainwin != NULL && gui.mainwin->window != NULL)
5200 return GDK_WINDOW_XDISPLAY(gui.mainwin->window);
5201 else
5202 return NULL;
5203}
5204#endif
5205
5206 void
5207gui_mch_beep(void)
5208{
5209#ifdef HAVE_GTK_MULTIHEAD
5210 GdkDisplay *display;
5211
5212 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5213 display = gtk_widget_get_display(gui.mainwin);
5214 else
5215 display = gdk_display_get_default();
5216
5217 if (display != NULL)
5218 gdk_display_beep(display);
5219#else
5220 gdk_beep();
5221#endif
5222}
5223
5224 void
5225gui_mch_flash(int msec)
5226{
5227 GdkGCValues values;
5228 GdkGC *invert_gc;
5229
5230 if (gui.drawarea->window == NULL)
5231 return;
5232
5233 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5234 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
5235 values.function = GDK_XOR;
5236 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5237 &values,
5238 GDK_GC_FOREGROUND |
5239 GDK_GC_BACKGROUND |
5240 GDK_GC_FUNCTION);
5241 gdk_gc_set_exposures(invert_gc,
5242 gui.visibility != GDK_VISIBILITY_UNOBSCURED);
5243 /*
5244 * Do a visual beep by changing back and forth in some undetermined way,
5245 * the foreground and background colors. This is due to the fact that
5246 * there can't be really any prediction about the effects of XOR on
5247 * arbitrary X11 servers. However this seems to be enough for what we
5248 * intend it to do.
5249 */
5250 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5251 TRUE,
5252 0, 0,
5253 FILL_X((int)Columns) + gui.border_offset,
5254 FILL_Y((int)Rows) + gui.border_offset);
5255
5256 gui_mch_flush();
5257 ui_delay((long)msec, TRUE); /* wait so many msec */
5258
5259 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5260 TRUE,
5261 0, 0,
5262 FILL_X((int)Columns) + gui.border_offset,
5263 FILL_Y((int)Rows) + gui.border_offset);
5264
5265 gdk_gc_destroy(invert_gc);
5266}
5267
5268/*
5269 * Invert a rectangle from row r, column c, for nr rows and nc columns.
5270 */
5271 void
5272gui_mch_invert_rectangle(int r, int c, int nr, int nc)
5273{
5274 GdkGCValues values;
5275 GdkGC *invert_gc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 if (gui.drawarea->window == NULL)
5278 return;
5279
Bram Moolenaar89d40322006-08-29 15:30:07 +00005280 values.foreground.pixel = gui.norm_pixel ^ gui.back_pixel;
5281 values.background.pixel = gui.norm_pixel ^ gui.back_pixel;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 values.function = GDK_XOR;
5283 invert_gc = gdk_gc_new_with_values(gui.drawarea->window,
5284 &values,
5285 GDK_GC_FOREGROUND |
5286 GDK_GC_BACKGROUND |
5287 GDK_GC_FUNCTION);
Bram Moolenaar89d40322006-08-29 15:30:07 +00005288 gdk_gc_set_exposures(invert_gc, gui.visibility !=
5289 GDK_VISIBILITY_UNOBSCURED);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 gdk_draw_rectangle(gui.drawarea->window, invert_gc,
5291 TRUE,
5292 FILL_X(c), FILL_Y(r),
5293 (nc) * gui.char_width, (nr) * gui.char_height);
5294 gdk_gc_destroy(invert_gc);
5295}
5296
5297/*
5298 * Iconify the GUI window.
5299 */
5300 void
5301gui_mch_iconify(void)
5302{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 gtk_window_iconify(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304}
5305
5306#if defined(FEAT_EVAL) || defined(PROTO)
5307/*
5308 * Bring the Vim window to the foreground.
5309 */
5310 void
5311gui_mch_set_foreground(void)
5312{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 gtk_window_present(GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314}
5315#endif
5316
5317/*
5318 * Draw a cursor without focus.
5319 */
5320 void
5321gui_mch_draw_hollow_cursor(guicolor_T color)
5322{
5323 int i = 1;
5324
5325 if (gui.drawarea->window == NULL)
5326 return;
5327
5328 gui_mch_set_fg_color(color);
5329
5330 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 if (mb_lefthalve(gui.row, gui.col))
5332 i = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5334 FALSE,
5335 FILL_X(gui.col), FILL_Y(gui.row),
5336 i * gui.char_width - 1, gui.char_height - 1);
5337}
5338
5339/*
5340 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
5341 * color "color".
5342 */
5343 void
5344gui_mch_draw_part_cursor(int w, int h, guicolor_T color)
5345{
5346 if (gui.drawarea->window == NULL)
5347 return;
5348
5349 gui_mch_set_fg_color(color);
5350
5351 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5352 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc,
5353 TRUE,
5354#ifdef FEAT_RIGHTLEFT
5355 /* vertical line should be on the right of current point */
5356 CURSOR_BAR_RIGHT ? FILL_X(gui.col + 1) - w :
5357#endif
5358 FILL_X(gui.col),
5359 FILL_Y(gui.row) + gui.char_height - h,
5360 w, h);
5361}
5362
5363
5364/*
5365 * Catch up with any queued X11 events. This may put keyboard input into the
5366 * input buffer, call resize call-backs, trigger timers etc. If there is
5367 * nothing in the X11 event queue (& no timers pending), then we return
5368 * immediately.
5369 */
5370 void
5371gui_mch_update(void)
5372{
Bram Moolenaara3f41662010-07-11 19:01:06 +02005373 while (g_main_context_pending(NULL) && !vim_is_input_buf_full())
5374 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375}
5376
5377 static gint
5378input_timer_cb(gpointer data)
5379{
5380 int *timed_out = (int *) data;
5381
Bram Moolenaar49325942007-05-10 19:19:59 +00005382 /* Just inform the caller about the occurrence of it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 *timed_out = TRUE;
5384
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 return FALSE; /* don't happen again */
5386}
5387
5388#ifdef FEAT_SNIFF
5389/*
5390 * Callback function, used when data is available on the SNiFF connection.
5391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 static void
5393sniff_request_cb(
5394 gpointer data,
5395 gint source_fd,
5396 GdkInputCondition condition)
5397{
5398 static char_u bytes[3] = {CSI, (int)KS_EXTRA, (int)KE_SNIFF};
5399
5400 add_to_input_buf(bytes, 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401}
5402#endif
5403
5404/*
5405 * GUI input routine called by gui_wait_for_chars(). Waits for a character
5406 * from the keyboard.
5407 * wtime == -1 Wait forever.
5408 * wtime == 0 This should never happen.
5409 * wtime > 0 Wait wtime milliseconds for a character.
5410 * Returns OK if a character was found to be available within the given time,
5411 * or FAIL otherwise.
5412 */
5413 int
5414gui_mch_wait_for_chars(long wtime)
5415{
5416 int focus;
5417 guint timer;
5418 static int timed_out;
5419#ifdef FEAT_SNIFF
5420 static int sniff_on = 0;
5421 static gint sniff_input_id = 0;
5422#endif
5423
5424#ifdef FEAT_SNIFF
5425 if (sniff_on && !want_sniff_request)
5426 {
5427 if (sniff_input_id)
5428 gdk_input_remove(sniff_input_id);
5429 sniff_on = 0;
5430 }
5431 else if (!sniff_on && want_sniff_request)
5432 {
5433 /* Add fd_from_sniff to watch for available data in main loop. */
5434 sniff_input_id = gdk_input_add(fd_from_sniff,
5435 GDK_INPUT_READ, sniff_request_cb, NULL);
5436 sniff_on = 1;
5437 }
5438#endif
5439
5440 timed_out = FALSE;
5441
5442 /* this timeout makes sure that we will return if no characters arrived in
5443 * time */
5444
5445 if (wtime > 0)
5446 timer = gtk_timeout_add((guint32)wtime, input_timer_cb, &timed_out);
5447 else
5448 timer = 0;
5449
5450 focus = gui.in_focus;
5451
5452 do
5453 {
5454 /* Stop or start blinking when focus changes */
5455 if (gui.in_focus != focus)
5456 {
5457 if (gui.in_focus)
5458 gui_mch_start_blink();
5459 else
5460 gui_mch_stop_blink();
5461 focus = gui.in_focus;
5462 }
5463
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005464#if defined(FEAT_NETBEANS_INTG)
Bram Moolenaarede547a2011-02-25 15:15:50 +01005465 /* Process any queued netbeans messages. */
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005466 netbeans_parse_messages();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005467#endif
5468
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 /*
5470 * Loop in GTK+ processing until a timeout or input occurs.
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005471 * Skip this if input is available anyway (can happen in rare
5472 * situations, sort of race condition).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00005474 if (!input_available())
Bram Moolenaara3f41662010-07-11 19:01:06 +02005475 g_main_context_iteration(NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 /* Got char, return immediately */
5478 if (input_available())
5479 {
5480 if (timer != 0 && !timed_out)
5481 gtk_timeout_remove(timer);
5482 return OK;
5483 }
5484 } while (wtime < 0 || !timed_out);
5485
5486 /*
5487 * Flush all eventually pending (drawing) events.
5488 */
5489 gui_mch_update();
5490
5491 return FAIL;
5492}
5493
5494
5495/****************************************************************************
5496 * Output drawing routines.
5497 ****************************************************************************/
5498
5499
5500/* Flush any output to the screen */
5501 void
5502gui_mch_flush(void)
5503{
5504#ifdef HAVE_GTK_MULTIHEAD
5505 if (gui.mainwin != NULL && GTK_WIDGET_REALIZED(gui.mainwin))
5506 gdk_display_sync(gtk_widget_get_display(gui.mainwin));
5507#else
5508 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
5509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 /* This happens to actually do what gui_mch_flush() is supposed to do,
5511 * according to the comment above. */
5512 if (gui.drawarea != NULL && gui.drawarea->window != NULL)
5513 gdk_window_process_updates(gui.drawarea->window, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514}
5515
5516/*
5517 * Clear a rectangular region of the screen from text pos (row1, col1) to
5518 * (row2, col2) inclusive.
5519 */
5520 void
5521gui_mch_clear_block(int row1, int col1, int row2, int col2)
5522{
5523 GdkColor color;
5524
5525 if (gui.drawarea->window == NULL)
5526 return;
5527
5528 color.pixel = gui.back_pixel;
5529
5530 gdk_gc_set_foreground(gui.text_gc, &color);
5531
5532 /* Clear one extra pixel at the far right, for when bold characters have
5533 * spilled over to the window border. */
5534 gdk_draw_rectangle(gui.drawarea->window, gui.text_gc, TRUE,
5535 FILL_X(col1), FILL_Y(row1),
5536 (col2 - col1 + 1) * gui.char_width
5537 + (col2 == Columns - 1),
5538 (row2 - row1 + 1) * gui.char_height);
5539}
5540
5541 void
5542gui_mch_clear_all(void)
5543{
5544 if (gui.drawarea->window != NULL)
5545 gdk_window_clear(gui.drawarea->window);
5546}
5547
5548/*
5549 * Redraw any text revealed by scrolling up/down.
5550 */
5551 static void
5552check_copy_area(void)
5553{
5554 GdkEvent *event;
5555 int expose_count;
5556
5557 if (gui.visibility != GDK_VISIBILITY_PARTIAL)
5558 return;
5559
5560 /* Avoid redrawing the cursor while scrolling or it'll end up where
5561 * we don't want it to be. I'm not sure if it's correct to call
5562 * gui_dont_update_cursor() at this point but it works as a quick
5563 * fix for now. */
5564 gui_dont_update_cursor();
5565
5566 do
5567 {
5568 /* Wait to check whether the scroll worked or not. */
5569 event = gdk_event_get_graphics_expose(gui.drawarea->window);
5570
5571 if (event == NULL)
5572 break; /* received NoExpose event */
5573
5574 gui_redraw(event->expose.area.x, event->expose.area.y,
5575 event->expose.area.width, event->expose.area.height);
5576
5577 expose_count = event->expose.count;
5578 gdk_event_free(event);
5579 }
5580 while (expose_count > 0); /* more events follow */
5581
5582 gui_can_update_cursor();
5583}
5584
5585/*
5586 * Delete the given number of lines from the given row, scrolling up any
5587 * text further down within the scroll region.
5588 */
5589 void
5590gui_mch_delete_lines(int row, int num_lines)
5591{
5592 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5593 return; /* Can't see the window */
5594
5595 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5596 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5597
5598 /* copy one extra pixel, for when bold has spilled over */
5599 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5600 FILL_X(gui.scroll_region_left), FILL_Y(row),
5601 gui.drawarea->window,
5602 FILL_X(gui.scroll_region_left),
5603 FILL_Y(row + num_lines),
5604 gui.char_width * (gui.scroll_region_right
5605 - gui.scroll_region_left + 1) + 1,
5606 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5607
5608 gui_clear_block(gui.scroll_region_bot - num_lines + 1,
5609 gui.scroll_region_left,
5610 gui.scroll_region_bot, gui.scroll_region_right);
5611 check_copy_area();
5612}
5613
5614/*
5615 * Insert the given number of lines before the given row, scrolling down any
5616 * following text within the scroll region.
5617 */
5618 void
5619gui_mch_insert_lines(int row, int num_lines)
5620{
5621 if (gui.visibility == GDK_VISIBILITY_FULLY_OBSCURED)
5622 return; /* Can't see the window */
5623
5624 gdk_gc_set_foreground(gui.text_gc, gui.fgcolor);
5625 gdk_gc_set_background(gui.text_gc, gui.bgcolor);
5626
5627 /* copy one extra pixel, for when bold has spilled over */
5628 gdk_window_copy_area(gui.drawarea->window, gui.text_gc,
5629 FILL_X(gui.scroll_region_left), FILL_Y(row + num_lines),
5630 gui.drawarea->window,
5631 FILL_X(gui.scroll_region_left), FILL_Y(row),
5632 gui.char_width * (gui.scroll_region_right
5633 - gui.scroll_region_left + 1) + 1,
5634 gui.char_height * (gui.scroll_region_bot - row - num_lines + 1));
5635
5636 gui_clear_block(row, gui.scroll_region_left,
5637 row + num_lines - 1, gui.scroll_region_right);
5638 check_copy_area();
5639}
5640
5641/*
5642 * X Selection stuff, for cutting and pasting text to other windows.
5643 */
5644 void
5645clip_mch_request_selection(VimClipboard *cbd)
5646{
5647 GdkAtom target;
5648 unsigned i;
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005649 time_t start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650
5651 for (i = 0; i < N_SELECTION_TARGETS; ++i)
5652 {
Bram Moolenaar3a6eaa52009-06-16 13:23:06 +00005653 if (!clip_html && selection_targets[i].info == TARGET_HTML)
5654 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 received_selection = RS_NONE;
5656 target = gdk_atom_intern(selection_targets[i].target, FALSE);
5657
5658 gtk_selection_convert(gui.drawarea,
5659 cbd->gtk_sel_atom, target,
5660 (guint32)GDK_CURRENT_TIME);
5661
Bram Moolenaar51b5ab92008-01-06 14:17:07 +00005662 /* Hack: Wait up to three seconds for the selection. A hang was
5663 * noticed here when using the netrw plugin combined with ":gui"
5664 * during the FocusGained event. */
5665 start = time(NULL);
5666 while (received_selection == RS_NONE && time(NULL) < start + 3)
Bram Moolenaara3f41662010-07-11 19:01:06 +02005667 g_main_context_iteration(NULL, TRUE); /* wait for selection_received_cb */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 if (received_selection != RS_FAIL)
5670 return;
5671 }
5672
5673 /* Final fallback position - use the X CUT_BUFFER0 store */
Bram Moolenaarbbc936b2009-07-01 16:04:58 +00005674 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui.mainwin->window), cbd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675}
5676
5677/*
5678 * Disown the selection.
5679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005681clip_mch_lose_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005683 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 gui_mch_update();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685}
5686
5687/*
5688 * Own the selection and return OK if it worked.
5689 */
5690 int
5691clip_mch_own_selection(VimClipboard *cbd)
5692{
5693 int success;
5694
5695 success = gtk_selection_owner_set(gui.drawarea, cbd->gtk_sel_atom,
Bram Moolenaar20892c12011-06-26 04:49:00 +02005696 gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 gui_mch_update();
5698 return (success) ? OK : FAIL;
5699}
5700
5701/*
5702 * Send the current selection to the clipboard. Do nothing for X because we
5703 * will fill in the selection only when requested by another app.
5704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005706clip_mch_set_selection(VimClipboard *cbd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
5708}
5709
Bram Moolenaar1a0316c2013-03-13 17:50:25 +01005710 int
5711clip_gtk_owner_exists(VimClipboard *cbd)
5712{
5713 return gdk_selection_owner_get(cbd->gtk_sel_atom) != NULL;
5714}
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717#if defined(FEAT_MENU) || defined(PROTO)
5718/*
5719 * Make a menu item appear either active or not active (grey or not grey).
5720 */
5721 void
5722gui_mch_menu_grey(vimmenu_T *menu, int grey)
5723{
5724 if (menu->id == NULL)
5725 return;
5726
5727 if (menu_is_separator(menu->name))
5728 grey = TRUE;
5729
5730 gui_mch_menu_hidden(menu, FALSE);
5731 /* Be clever about bitfields versus true booleans here! */
5732 if (!GTK_WIDGET_SENSITIVE(menu->id) == !grey)
5733 {
5734 gtk_widget_set_sensitive(menu->id, !grey);
5735 gui_mch_update();
5736 }
5737}
5738
5739/*
5740 * Make menu item hidden or not hidden.
5741 */
5742 void
5743gui_mch_menu_hidden(vimmenu_T *menu, int hidden)
5744{
5745 if (menu->id == 0)
5746 return;
5747
5748 if (hidden)
5749 {
5750 if (GTK_WIDGET_VISIBLE(menu->id))
5751 {
5752 gtk_widget_hide(menu->id);
5753 gui_mch_update();
5754 }
5755 }
5756 else
5757 {
5758 if (!GTK_WIDGET_VISIBLE(menu->id))
5759 {
5760 gtk_widget_show(menu->id);
5761 gui_mch_update();
5762 }
5763 }
5764}
5765
5766/*
5767 * This is called after setting all the menus to grey/hidden or not.
5768 */
5769 void
5770gui_mch_draw_menubar(void)
5771{
5772 /* just make sure that the visual changes get effect immediately */
5773 gui_mch_update();
5774}
5775#endif /* FEAT_MENU */
5776
5777/*
5778 * Scrollbar stuff.
5779 */
5780 void
5781gui_mch_enable_scrollbar(scrollbar_T *sb, int flag)
5782{
5783 if (sb->id == NULL)
5784 return;
5785
5786 if (flag)
5787 gtk_widget_show(sb->id);
5788 else
5789 gtk_widget_hide(sb->id);
5790
Bram Moolenaarb3656ed2006-03-20 21:59:49 +00005791 update_window_manager_hints(0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
5794
5795/*
5796 * Return the RGB value of a pixel as long.
5797 */
5798 long_u
5799gui_mch_get_rgb(guicolor_T pixel)
5800{
5801 GdkColor color;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 gdk_colormap_query_color(gtk_widget_get_colormap(gui.drawarea),
5803 (unsigned long)pixel, &color);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 return (((unsigned)color.red & 0xff00) << 8)
5806 | ((unsigned)color.green & 0xff00)
5807 | (((unsigned)color.blue & 0xff00) >> 8);
5808}
5809
5810/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005811 * Get current mouse coordinates in text window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005813 void
5814gui_mch_getmouse(int *x, int *y)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005816 gdk_window_get_pointer(gui.drawarea->window, x, y, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819 void
5820gui_mch_setmouse(int x, int y)
5821{
5822 /* Sorry for the Xlib call, but we can't avoid it, since there is no
5823 * internal GDK mechanism present to accomplish this. (and for good
5824 * reason...) */
5825 XWarpPointer(GDK_WINDOW_XDISPLAY(gui.drawarea->window),
5826 (Window)0, GDK_WINDOW_XWINDOW(gui.drawarea->window),
5827 0, 0, 0U, 0U, x, y);
5828}
5829
5830
5831#ifdef FEAT_MOUSESHAPE
5832/* The last set mouse pointer shape is remembered, to be used when it goes
5833 * from hidden to not hidden. */
5834static int last_shape = 0;
5835#endif
5836
5837/*
5838 * Use the blank mouse pointer or not.
5839 *
5840 * hide: TRUE = use blank ptr, FALSE = use parent ptr
5841 */
5842 void
5843gui_mch_mousehide(int hide)
5844{
5845 if (gui.pointer_hidden != hide)
5846 {
5847 gui.pointer_hidden = hide;
5848 if (gui.drawarea->window && gui.blank_pointer != NULL)
5849 {
5850 if (hide)
5851 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5852 else
5853#ifdef FEAT_MOUSESHAPE
5854 mch_set_mouse_shape(last_shape);
5855#else
5856 gdk_window_set_cursor(gui.drawarea->window, NULL);
5857#endif
5858 }
5859 }
5860}
5861
5862#if defined(FEAT_MOUSESHAPE) || defined(PROTO)
5863
5864/* Table for shape IDs. Keep in sync with the mshape_names[] table in
5865 * misc2.c! */
5866static const int mshape_ids[] =
5867{
5868 GDK_LEFT_PTR, /* arrow */
5869 GDK_CURSOR_IS_PIXMAP, /* blank */
5870 GDK_XTERM, /* beam */
5871 GDK_SB_V_DOUBLE_ARROW, /* updown */
5872 GDK_SIZING, /* udsizing */
5873 GDK_SB_H_DOUBLE_ARROW, /* leftright */
5874 GDK_SIZING, /* lrsizing */
5875 GDK_WATCH, /* busy */
5876 GDK_X_CURSOR, /* no */
5877 GDK_CROSSHAIR, /* crosshair */
5878 GDK_HAND1, /* hand1 */
5879 GDK_HAND2, /* hand2 */
5880 GDK_PENCIL, /* pencil */
5881 GDK_QUESTION_ARROW, /* question */
5882 GDK_RIGHT_PTR, /* right-arrow */
5883 GDK_CENTER_PTR, /* up-arrow */
5884 GDK_LEFT_PTR /* last one */
5885};
5886
5887 void
5888mch_set_mouse_shape(int shape)
5889{
5890 int id;
5891 GdkCursor *c;
5892
5893 if (gui.drawarea->window == NULL)
5894 return;
5895
5896 if (shape == MSHAPE_HIDE || gui.pointer_hidden)
5897 gdk_window_set_cursor(gui.drawarea->window, gui.blank_pointer);
5898 else
5899 {
5900 if (shape >= MSHAPE_NUMBERED)
5901 {
5902 id = shape - MSHAPE_NUMBERED;
5903 if (id >= GDK_LAST_CURSOR)
5904 id = GDK_LEFT_PTR;
5905 else
5906 id &= ~1; /* they are always even (why?) */
5907 }
Bram Moolenaarb85cb212009-05-17 14:24:23 +00005908 else if (shape < (int)(sizeof(mshape_ids) / sizeof(int)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 id = mshape_ids[shape];
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005910 else
5911 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912# ifdef HAVE_GTK_MULTIHEAD
5913 c = gdk_cursor_new_for_display(
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005914 gtk_widget_get_display(gui.drawarea), (GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915# else
Bram Moolenaarb71ec9f2005-01-25 22:22:02 +00005916 c = gdk_cursor_new((GdkCursorType)id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917# endif
5918 gdk_window_set_cursor(gui.drawarea->window, c);
5919 gdk_cursor_destroy(c); /* Unref, actually. Bloody GTK+ 1. */
5920 }
5921 if (shape != MSHAPE_HIDE)
5922 last_shape = shape;
5923}
5924#endif /* FEAT_MOUSESHAPE */
5925
5926
5927#if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5928/*
5929 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
5930 * scaled down if the current font is not big enough, or scaled up if the image
5931 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
5932 * will be cut off if the current font is not big enough, or centered if it's
5933 * too small.
5934 */
5935# define SIGN_WIDTH (2 * gui.char_width)
5936# define SIGN_HEIGHT (gui.char_height)
5937# define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
5938
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 void
5940gui_mch_drawsign(int row, int col, int typenr)
5941{
5942 GdkPixbuf *sign;
5943
5944 sign = (GdkPixbuf *)sign_get_image(typenr);
5945
5946 if (sign != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
5947 {
5948 int width;
5949 int height;
5950 int xoffset;
5951 int yoffset;
5952 int need_scale;
5953
5954 width = gdk_pixbuf_get_width(sign);
5955 height = gdk_pixbuf_get_height(sign);
5956 /*
5957 * Decide whether we need to scale. Allow one pixel of border
5958 * width to be cut off, in order to avoid excessive scaling for
5959 * tiny differences in font size.
5960 */
5961 need_scale = (width > SIGN_WIDTH + 2
5962 || height > SIGN_HEIGHT + 2
5963 || (width < 3 * SIGN_WIDTH / 4
5964 && height < 3 * SIGN_HEIGHT / 4));
5965 if (need_scale)
5966 {
5967 double aspect;
5968
5969 /* Keep the original aspect ratio */
5970 aspect = (double)height / (double)width;
5971 width = (double)SIGN_WIDTH * SIGN_ASPECT / aspect;
5972 width = MIN(width, SIGN_WIDTH);
5973 height = (double)width * aspect;
5974
5975 /* This doesn't seem to be worth caching, and doing so
5976 * would complicate the code quite a bit. */
5977 sign = gdk_pixbuf_scale_simple(sign, width, height,
5978 GDK_INTERP_BILINEAR);
5979 if (sign == NULL)
5980 return; /* out of memory */
5981 }
5982
5983 /* The origin is the upper-left corner of the pixmap. Therefore
5984 * these offset may become negative if the pixmap is smaller than
5985 * the 2x1 cells reserved for the sign icon. */
5986 xoffset = (width - SIGN_WIDTH) / 2;
5987 yoffset = (height - SIGN_HEIGHT) / 2;
5988
5989 gdk_gc_set_foreground(gui.text_gc, gui.bgcolor);
5990
5991 gdk_draw_rectangle(gui.drawarea->window,
5992 gui.text_gc,
5993 TRUE,
5994 FILL_X(col),
5995 FILL_Y(row),
5996 SIGN_WIDTH,
5997 SIGN_HEIGHT);
5998
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 gdk_pixbuf_render_to_drawable_alpha(sign,
6000 gui.drawarea->window,
6001 MAX(0, xoffset),
6002 MAX(0, yoffset),
6003 FILL_X(col) - MIN(0, xoffset),
6004 FILL_Y(row) - MIN(0, yoffset),
6005 MIN(width, SIGN_WIDTH),
6006 MIN(height, SIGN_HEIGHT),
6007 GDK_PIXBUF_ALPHA_BILEVEL,
6008 127,
6009 GDK_RGB_DITHER_NORMAL,
6010 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011 if (need_scale)
6012 g_object_unref(sign);
6013 }
6014}
6015
6016 void *
6017gui_mch_register_sign(char_u *signfile)
6018{
6019 if (signfile[0] != NUL && signfile[0] != '-' && gui.in_use)
6020 {
6021 GdkPixbuf *sign;
6022 GError *error = NULL;
6023 char_u *message;
6024
6025 sign = gdk_pixbuf_new_from_file((const char *)signfile, &error);
6026
6027 if (error == NULL)
6028 return sign;
6029
6030 message = (char_u *)error->message;
6031
6032 if (message != NULL && input_conv.vc_type != CONV_NONE)
6033 message = string_convert(&input_conv, message, NULL);
6034
6035 if (message != NULL)
6036 {
6037 /* The error message is already translated and will be more
6038 * descriptive than anything we could possibly do ourselves. */
6039 EMSG2("E255: %s", message);
6040
6041 if (input_conv.vc_type != CONV_NONE)
6042 vim_free(message);
6043 }
6044 g_error_free(error);
6045 }
6046
6047 return NULL;
6048}
6049
6050 void
6051gui_mch_destroy_sign(void *sign)
6052{
6053 if (sign != NULL)
6054 g_object_unref(sign);
6055}
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057#endif /* FEAT_SIGN_ICONS */