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