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