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