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