1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * Porting to GTK+ was done by:
13 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
18 * Support for GTK+ 2 was added by:
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
27 /* Gnome redefines _() and N_(). Grrr... */
37 # ifdef bindtextdomain
38 # undef bindtextdomain
40 # ifdef bind_textdomain_codeset
41 # undef bind_textdomain_codeset
43 # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
44 # define ENABLE_NLS /* so the texts in the dialog boxes are translated */
49 /* missing prototype in bonobo-dock-item.h */
50 extern void bonobo_dock_item_set_behavior(BonoboDockItem
*dock_item
, BonoboDockItemBehavior beh
);
54 #if !defined(FEAT_GUI_GTK) && defined(PROTO)
55 /* When generating prototypes we don't want syntax errors. */
57 # define GdkEventExpose int
58 # define GdkEventFocus int
59 # define GdkEventVisibility int
60 # define GdkEventProperty int
61 # define GtkContainer int
62 # define GtkTargetEntry int
64 # define GtkWidget 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
77 # include <gdk/gdkkeysyms.h>
80 # include <gdk/gdkwin32.h>
82 # include <gdk/gdkx.h>
86 # include "gui_gtk_f.h"
89 #ifdef HAVE_X11_SUNKEYSYM_H
90 # include <X11/Sunkeysym.h>
94 * Easy-to-use macro for multihead support.
96 #ifdef HAVE_GTK_MULTIHEAD
97 # define GET_X_ATOM(atom) gdk_x11_atom_to_xatom_for_display( \
98 gtk_widget_get_display(gui.mainwin), atom)
100 # define GET_X_ATOM(atom) ((Atom)(atom))
103 /* Selection type distinguishers */
109 TARGET_COMPOUND_TEXT
,
112 TARGET_TEXT_URI_LIST
,
119 * Table of selection targets supported by Vim.
120 * Note: Order matters, preferred types should come first.
122 static const GtkTargetEntry selection_targets
[] =
124 {VIMENC_ATOM_NAME
, 0, TARGET_VIMENC
},
125 {VIM_ATOM_NAME
, 0, TARGET_VIM
},
127 {"text/html", 0, TARGET_HTML
},
128 {"UTF8_STRING", 0, TARGET_UTF8_STRING
},
130 {"COMPOUND_TEXT", 0, TARGET_COMPOUND_TEXT
},
131 {"TEXT", 0, TARGET_TEXT
},
132 {"STRING", 0, TARGET_STRING
}
134 #define N_SELECTION_TARGETS (sizeof(selection_targets) / sizeof(selection_targets[0]))
138 * Table of DnD targets supported by Vim.
139 * Note: Order matters, preferred types should come first.
141 static const GtkTargetEntry dnd_targets
[] =
143 {"text/uri-list", 0, TARGET_TEXT_URI_LIST
},
145 {"text/html", 0, TARGET_HTML
},
146 {"UTF8_STRING", 0, TARGET_UTF8_STRING
},
148 {"STRING", 0, TARGET_STRING
},
149 {"text/plain", 0, TARGET_TEXT_PLAIN
}
151 # define N_DND_TARGETS (sizeof(dnd_targets) / sizeof(dnd_targets[0]))
157 * "Monospace" is a standard font alias that should be present
158 * on all proper Pango/fontconfig installations.
160 # define DEFAULT_FONT "Monospace 10"
162 #else /* !HAVE_GTK2 */
164 * This is the single only fixed width font in X11, which seems to be present
165 * on all servers and available in all the variants we need.
167 # define DEFAULT_FONT "-adobe-courier-medium-r-normal-*-14-*-*-*-m-*-*-*"
169 #endif /* !HAVE_GTK2 */
171 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
173 * Atoms used to communicate save-yourself from the X11 session manager. There
174 * is no need to move them into the GUI struct, since they should be constant.
176 static GdkAtom wm_protocols_atom
= GDK_NONE
;
177 static GdkAtom save_yourself_atom
= GDK_NONE
;
181 * Atoms used to control/reference X11 selections.
184 static GdkAtom html_atom
= GDK_NONE
;
185 static GdkAtom utf8_string_atom
= GDK_NONE
;
188 static GdkAtom compound_text_atom
= GDK_NONE
;
189 static GdkAtom text_atom
= GDK_NONE
;
191 static GdkAtom vim_atom
= GDK_NONE
; /* Vim's own special selection format */
193 static GdkAtom vimenc_atom
= GDK_NONE
; /* Vim's extended selection format */
197 * Keycodes recognized by vim.
198 * NOTE: when changing this, the table in gui_x11.c probably needs the same
201 static struct special_key
207 const special_keys
[] =
210 {GDK_Down
, 'k', 'd'},
211 {GDK_Left
, 'k', 'l'},
212 {GDK_Right
, 'k', 'r'},
234 {GDK_Pause
, 'F', 'B'}, /* Pause == F21 according to netbeans.txt */
250 {SunXK_F36
, 'F', 'Q'},
251 {SunXK_F37
, 'F', 'R'},
253 {GDK_Help
, '%', '1'},
254 {GDK_Undo
, '&', '8'},
255 {GDK_BackSpace
, 'k', 'b'},
256 {GDK_Insert
, 'k', 'I'},
257 {GDK_Delete
, 'k', 'D'},
258 {GDK_3270_BackTab
, 'k', 'B'},
259 {GDK_Clear
, 'k', 'C'},
260 {GDK_Home
, 'k', 'h'},
262 {GDK_Prior
, 'k', 'P'},
263 {GDK_Next
, 'k', 'N'},
264 {GDK_Print
, '%', '9'},
266 {GDK_KP_Left
, 'k', 'l'},
267 {GDK_KP_Right
, 'k', 'r'},
268 {GDK_KP_Up
, 'k', 'u'},
269 {GDK_KP_Down
, 'k', 'd'},
270 {GDK_KP_Insert
, KS_EXTRA
, (char_u
)KE_KINS
},
271 {GDK_KP_Delete
, KS_EXTRA
, (char_u
)KE_KDEL
},
272 {GDK_KP_Home
, 'K', '1'},
273 {GDK_KP_End
, 'K', '4'},
274 {GDK_KP_Prior
, 'K', '3'}, /* page up */
275 {GDK_KP_Next
, 'K', '5'}, /* page down */
277 {GDK_KP_Add
, 'K', '6'},
278 {GDK_KP_Subtract
, 'K', '7'},
279 {GDK_KP_Divide
, 'K', '8'},
280 {GDK_KP_Multiply
, 'K', '9'},
281 {GDK_KP_Enter
, 'K', 'A'},
282 {GDK_KP_Decimal
, 'K', 'B'},
284 {GDK_KP_0
, 'K', 'C'},
285 {GDK_KP_1
, 'K', 'D'},
286 {GDK_KP_2
, 'K', 'E'},
287 {GDK_KP_3
, 'K', 'F'},
288 {GDK_KP_4
, 'K', 'G'},
289 {GDK_KP_5
, 'K', 'H'},
290 {GDK_KP_6
, 'K', 'I'},
291 {GDK_KP_7
, 'K', 'J'},
292 {GDK_KP_8
, 'K', 'K'},
293 {GDK_KP_9
, 'K', 'L'},
295 /* End of list marker: */
300 * Flags for command line options table below.
303 #define ARG_GEOMETRY 2
304 #define ARG_REVERSE 3
305 #define ARG_NOREVERSE 4
306 #define ARG_BACKGROUND 5
307 #define ARG_FOREGROUND 6
310 #define ARG_NETBEANS 9
311 #define ARG_XRM 10 /* ignored */
312 #define ARG_MENUFONT 11 /* ignored */
313 #define ARG_INDEX_MASK 0x00ff
314 #define ARG_HAS_VALUE 0x0100 /* a value is expected after the argument */
315 #define ARG_NEEDS_GUI 0x0200 /* need to initialize the GUI for this */
316 #define ARG_FOR_GTK 0x0400 /* argument is handled by GTK+ or GNOME */
317 #define ARG_COMPAT_LONG 0x0800 /* accept -foo but substitute with --foo */
318 #define ARG_KEEP 0x1000 /* don't remove argument from argv[] */
321 * This table holds all the X GUI command line options allowed. This includes
322 * the standard ones so that we can skip them when Vim is started without the
323 * GUI (but the GUI might start up later).
325 * When changing this, also update doc/gui_x11.txt and the usage message!!!
334 static const cmdline_option_T cmdline_options
[] =
336 /* We handle these options ourselves */
337 {"-fn", ARG_FONT
|ARG_HAS_VALUE
},
338 {"-font", ARG_FONT
|ARG_HAS_VALUE
},
339 {"-geom", ARG_GEOMETRY
|ARG_HAS_VALUE
},
340 {"-geometry", ARG_GEOMETRY
|ARG_HAS_VALUE
},
341 {"-rv", ARG_REVERSE
},
342 {"-reverse", ARG_REVERSE
},
343 {"+rv", ARG_NOREVERSE
},
344 {"+reverse", ARG_NOREVERSE
},
345 {"-bg", ARG_BACKGROUND
|ARG_HAS_VALUE
},
346 {"-background", ARG_BACKGROUND
|ARG_HAS_VALUE
},
347 {"-fg", ARG_FOREGROUND
|ARG_HAS_VALUE
},
348 {"-foreground", ARG_FOREGROUND
|ARG_HAS_VALUE
},
349 {"-iconic", ARG_ICONIC
},
351 {"--role", ARG_ROLE
|ARG_HAS_VALUE
},
353 #ifdef FEAT_NETBEANS_INTG
354 {"-nb", ARG_NETBEANS
}, /* non-standard value format */
355 {"-xrm", ARG_XRM
|ARG_HAS_VALUE
}, /* not implemented */
356 {"-mf", ARG_MENUFONT
|ARG_HAS_VALUE
}, /* not implemented */
357 {"-menufont", ARG_MENUFONT
|ARG_HAS_VALUE
}, /* not implemented */
359 #if 0 /* not implemented; these arguments don't make sense for GTK+ */
360 {"-boldfont", ARG_HAS_VALUE
},
361 {"-italicfont", ARG_HAS_VALUE
},
362 {"-bw", ARG_HAS_VALUE
},
363 {"-borderwidth", ARG_HAS_VALUE
},
364 {"-sw", ARG_HAS_VALUE
},
365 {"-scrollbarwidth", ARG_HAS_VALUE
},
367 /* Arguments handled by GTK (and GNOME) internally. */
368 {"--g-fatal-warnings", ARG_FOR_GTK
},
369 {"--gdk-debug", ARG_FOR_GTK
|ARG_HAS_VALUE
},
370 {"--gdk-no-debug", ARG_FOR_GTK
|ARG_HAS_VALUE
},
371 {"--gtk-debug", ARG_FOR_GTK
|ARG_HAS_VALUE
},
372 {"--gtk-no-debug", ARG_FOR_GTK
|ARG_HAS_VALUE
},
373 {"--gtk-module", ARG_FOR_GTK
|ARG_HAS_VALUE
},
374 {"--sync", ARG_FOR_GTK
},
375 {"--display", ARG_FOR_GTK
|ARG_HAS_VALUE
|ARG_COMPAT_LONG
},
376 {"--name", ARG_FOR_GTK
|ARG_HAS_VALUE
|ARG_COMPAT_LONG
},
377 {"--class", ARG_FOR_GTK
|ARG_HAS_VALUE
|ARG_COMPAT_LONG
},
379 {"--screen", ARG_FOR_GTK
|ARG_HAS_VALUE
},
380 {"--gxid-host", ARG_FOR_GTK
|ARG_HAS_VALUE
},
381 {"--gxid-port", ARG_FOR_GTK
|ARG_HAS_VALUE
},
382 #else /* these don't seem to exist anymore */
383 {"--no-xshm", ARG_FOR_GTK
},
384 {"--xim-preedit", ARG_FOR_GTK
|ARG_HAS_VALUE
},
385 {"--xim-status", ARG_FOR_GTK
|ARG_HAS_VALUE
},
386 {"--gxid_host", ARG_FOR_GTK
|ARG_HAS_VALUE
},
387 {"--gxid_port", ARG_FOR_GTK
|ARG_HAS_VALUE
},
389 #ifdef FEAT_GUI_GNOME
390 {"--load-modules", ARG_FOR_GTK
|ARG_HAS_VALUE
},
391 {"--sm-client-id", ARG_FOR_GTK
|ARG_HAS_VALUE
},
392 {"--sm-config-prefix", ARG_FOR_GTK
|ARG_HAS_VALUE
},
393 {"--sm-disable", ARG_FOR_GTK
},
394 {"--oaf-ior-fd", ARG_FOR_GTK
|ARG_HAS_VALUE
},
395 {"--oaf-activate-iid", ARG_FOR_GTK
|ARG_HAS_VALUE
},
396 {"--oaf-private", ARG_FOR_GTK
},
397 {"--enable-sound", ARG_FOR_GTK
},
398 {"--disable-sound", ARG_FOR_GTK
},
399 {"--espeaker", ARG_FOR_GTK
|ARG_HAS_VALUE
},
400 {"-?", ARG_FOR_GTK
|ARG_NEEDS_GUI
},
401 {"--help", ARG_FOR_GTK
|ARG_NEEDS_GUI
|ARG_KEEP
},
402 {"--usage", ARG_FOR_GTK
|ARG_NEEDS_GUI
},
403 # if 0 /* conflicts with Vim's own --version argument */
404 {"--version", ARG_FOR_GTK
|ARG_NEEDS_GUI
},
406 {"--disable-crash-dialog", ARG_FOR_GTK
},
411 static int gui_argc
= 0;
412 static char **gui_argv
= NULL
;
415 static const char *role_argument
= NULL
;
417 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
418 static const char *restart_command
= NULL
;
419 static char *abs_restart_command
= NULL
;
421 static int found_iconic_arg
= FALSE
;
423 #ifdef FEAT_GUI_GNOME
425 * Can't use Gnome if --socketid given
427 static int using_gnome
= 0;
429 # define using_gnome 0
433 * Parse the GUI related command-line arguments. Any arguments used are
434 * deleted from argv, and *argc is decremented accordingly. This is called
435 * when vim is started, whether or not the GUI has been started.
438 gui_mch_prepare(int *argc
, char **argv
)
440 const cmdline_option_T
*option
;
444 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
446 * Determine the command used to invoke Vim, to be passed as restart
447 * command to the session manager. If argv[0] contains any directory
448 * components try building an absolute path, otherwise leave it as is.
450 restart_command
= argv
[0];
452 if (strchr(argv
[0], G_DIR_SEPARATOR
) != NULL
)
454 char_u buf
[MAXPATHL
];
456 if (mch_FullName((char_u
*)argv
[0], buf
, (int)sizeof(buf
), TRUE
) == OK
)
458 abs_restart_command
= (char *)vim_strsave(buf
);
459 restart_command
= abs_restart_command
;
465 * Move all the entries in argv which are relevant to GTK+ and GNOME
466 * into gui_argv. Freed later in gui_mch_init().
469 gui_argv
= (char **)alloc((unsigned)((*argc
+ 1) * sizeof(char *)));
471 g_return_if_fail(gui_argv
!= NULL
);
473 gui_argv
[gui_argc
++] = argv
[i
++];
477 /* Don't waste CPU cycles on non-option arguments. */
478 if (argv
[i
][0] != '-' && argv
[i
][0] != '+')
484 /* Look for argv[i] in cmdline_options[] table. */
485 for (option
= &cmdline_options
[0]; option
->name
!= NULL
; ++option
)
487 len
= strlen(option
->name
);
489 if (strncmp(argv
[i
], option
->name
, len
) == 0)
491 if (argv
[i
][len
] == '\0')
493 /* allow --foo=bar style */
494 if (argv
[i
][len
] == '=' && (option
->flags
& ARG_HAS_VALUE
))
496 #ifdef FEAT_NETBEANS_INTG
497 /* darn, -nb has non-standard syntax */
498 if (vim_strchr((char_u
*)":=", argv
[i
][len
]) != NULL
499 && (option
->flags
& ARG_INDEX_MASK
) == ARG_NETBEANS
)
503 else if ((option
->flags
& ARG_COMPAT_LONG
)
504 && strcmp(argv
[i
], option
->name
+ 1) == 0)
506 /* Replace the standard X arguments "-name" and "-display"
507 * with their GNU-style long option counterparts. */
508 argv
[i
] = (char *)option
->name
;
512 if (option
->name
== NULL
) /* no match */
518 if (option
->flags
& ARG_FOR_GTK
)
520 /* Move the argument into gui_argv, which
521 * will later be passed to gtk_init_check() */
522 gui_argv
[gui_argc
++] = argv
[i
];
528 /* Extract the option's value if there is one.
529 * Accept both "--foo bar" and "--foo=bar" style. */
530 if (option
->flags
& ARG_HAS_VALUE
)
532 if (argv
[i
][len
] == '=')
533 value
= &argv
[i
][len
+ 1];
534 else if (i
+ 1 < *argc
&& strcmp(argv
[i
+ 1], "--") != 0)
538 /* Check for options handled by Vim itself */
539 switch (option
->flags
& ARG_INDEX_MASK
)
542 found_reverse_arg
= TRUE
;
545 found_reverse_arg
= FALSE
;
548 font_argument
= value
;
552 gui
.geom
= vim_strsave((char_u
*)value
);
555 background_argument
= value
;
558 foreground_argument
= value
;
561 found_iconic_arg
= TRUE
;
565 role_argument
= value
; /* used later in gui_mch_open() */
568 #ifdef FEAT_NETBEANS_INTG
571 gui
.dofork
= FALSE
; /* don't fork() when starting GUI */
572 netbeansArg
= argv
[i
];
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
)
585 if (option
->flags
& ARG_KEEP
)
589 /* Remove the flag from the argument vector. */
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)
601 if (option
->flags
& ARG_FOR_GTK
)
602 gui_argv
[gui_argc
++] = argv
[i
+ 1];
606 mch_memmove(&argv
[i
], &argv
[i
+ n_strip
],
607 (*argc
- i
) * sizeof(char *));
613 gui_argv
[gui_argc
] = NULL
;
616 #if defined(EXITFREE) || defined(PROTO)
621 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
622 vim_free(abs_restart_command
);
628 * This should be maybe completely removed.
629 * Doesn't seem possible, since check_copy_area() relies on
630 * this information. --danielk
633 visibility_event(GtkWidget
*widget UNUSED
,
634 GdkEventVisibility
*event
,
635 gpointer data UNUSED
)
637 gui
.visibility
= event
->state
;
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
643 if (gui
.text_gc
!= NULL
)
644 gdk_gc_set_exposures(gui
.text_gc
,
645 gui
.visibility
!= GDK_VISIBILITY_UNOBSCURED
);
650 * Redraw the corresponding portions of the screen.
653 expose_event(GtkWidget
*widget UNUSED
,
654 GdkEventExpose
*event
,
655 gpointer data UNUSED
)
657 /* Skip this when the GUI isn't set up yet, will redraw later. */
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
);
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);
679 #ifdef FEAT_CLIENTSERVER
681 * Handle changes to the "Comm" property
684 property_event(GtkWidget
*widget
,
685 GdkEventProperty
*event
,
686 gpointer data UNUSED
)
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
)
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
);
702 if (gtk_main_level() > 0)
710 /****************************************************************************
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
726 static int blink_state
= BLINK_NONE
;
727 static long_u blink_waittime
= 700;
728 static long_u blink_ontime
= 400;
729 static long_u blink_offtime
= 250;
730 static guint blink_timer
= 0;
733 gui_mch_set_blinking(long waittime
, long on
, long off
)
735 blink_waittime
= waittime
;
741 * Stop the cursor blinking. Show the cursor if it wasn't shown.
744 gui_mch_stop_blink(void)
748 gtk_timeout_remove(blink_timer
);
751 if (blink_state
== BLINK_OFF
)
752 gui_update_cursor(TRUE
, FALSE
);
753 blink_state
= BLINK_NONE
;
757 blink_cb(gpointer data UNUSED
)
759 if (blink_state
== BLINK_ON
)
762 blink_state
= BLINK_OFF
;
763 blink_timer
= gtk_timeout_add((guint32
)blink_offtime
,
764 (GtkFunction
) blink_cb
, NULL
);
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
);
774 return FALSE
; /* don't happen again */
778 * Start the cursor blinking. If it was already blinking, this restarts the
779 * waiting time and shows the cursor.
782 gui_mch_start_blink(void)
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
)
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
);
797 enter_notify_event(GtkWidget
*widget UNUSED
,
798 GdkEventCrossing
*event UNUSED
,
799 gpointer data UNUSED
)
801 if (blink_state
== BLINK_NONE
)
802 gui_mch_start_blink();
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
);
812 leave_notify_event(GtkWidget
*widget UNUSED
,
813 GdkEventCrossing
*event UNUSED
,
814 gpointer data UNUSED
)
816 if (blink_state
!= BLINK_NONE
)
817 gui_mch_stop_blink();
823 focus_in_event(GtkWidget
*widget
,
824 GdkEventFocus
*event UNUSED
,
825 gpointer data UNUSED
)
827 gui_focus_change(TRUE
);
829 if (blink_state
== BLINK_NONE
)
830 gui_mch_start_blink();
832 /* make sure keyboard input goes to the draw area (if this is focus for a
834 if (widget
!= gui
.drawarea
)
835 gtk_widget_grab_focus(gui
.drawarea
);
837 /* make sure the input buffer is read */
838 if (gtk_main_level() > 0)
845 focus_out_event(GtkWidget
*widget UNUSED
,
846 GdkEventFocus
*event UNUSED
,
847 gpointer data UNUSED
)
849 gui_focus_change(FALSE
);
851 if (blink_state
!= BLINK_NONE
)
852 gui_mch_stop_blink();
854 /* make sure the input buffer is read */
855 if (gtk_main_level() > 0)
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.
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
873 keyval_to_string(unsigned int keyval
, unsigned int state
, char_u
*string
)
878 uc
= gdk_keyval_to_unicode(keyval
);
881 /* Check for CTRL-foo */
882 if ((state
& GDK_CONTROL_MASK
) && uc
>= 0x20 && uc
< 0x80)
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). */
889 string
[0] = uc
& 0x1F;
892 else if (uc
>= '3' && uc
<= '7')
893 string
[0] = uc
^ 0x28;
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
);
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. */
917 case GDK_Tab
: case GDK_KP_Tab
: case GDK_ISO_Left_Tab
:
923 case GDK_Return
: case GDK_ISO_Enter
: case GDK_3270_Enter
:
938 #endif /* HAVE_GTK2 */
941 modifiers_gdk2vim(guint state
)
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
;
951 if (state
& GDK_MOD4_MASK
)
952 modifiers
|= MOD_MASK_META
;
958 modifiers_gdk2mouse(guint state
)
962 if (state
& GDK_SHIFT_MASK
)
963 modifiers
|= MOUSE_SHIFT
;
964 if (state
& GDK_CONTROL_MASK
)
965 modifiers
|= MOUSE_CTRL
;
966 if (state
& GDK_MOD1_MASK
)
967 modifiers
|= MOUSE_ALT
;
973 * Main keyboard handler:
976 key_press_event(GtkWidget
*widget UNUSED
,
978 gpointer data UNUSED
)
981 /* 256 bytes is way over the top, but for safety let's reduce it only
982 * for GTK+ 2 where we know for sure how large the string might get.
983 * (That is, up to 6 bytes + NUL + CSI escapes + safety measure.) */
984 char_u string
[32], string2
[32];
986 char_u string
[256], string2
[256];
996 key_sym
= event
->keyval
;
997 state
= event
->state
;
998 #ifndef HAVE_GTK2 /* deprecated */
1000 g_assert(len
<= sizeof(string
));
1005 * It appears as if we always want to consume a key-press (there currently
1006 * aren't any 'return FALSE's), so we always do this: when running in a
1007 * GtkPlug and not a window, we must prevent emission of the key_press
1008 * EVENT from continuing (which is 'beyond' the level of stopping mere
1009 * signals by returning FALSE), otherwise things like tab/cursor-keys are
1010 * processed by the GtkPlug default handler, which moves input focus away
1012 * Note: This should no longer be necessary with GTK+ 2.
1014 if (gtk_socket_id
!= 0)
1015 gtk_signal_emit_stop_by_name(GTK_OBJECT(widget
), "key_press_event");
1019 if (xim_queue_key_press_event(event
, TRUE
))
1023 #ifdef FEAT_HANGULIN
1024 if (key_sym
== GDK_space
&& (state
& GDK_SHIFT_MASK
))
1026 hangul_input_state_toggle();
1033 * These keys have bogus lookup strings, and trapping them here is
1034 * easier than trying to XRebindKeysym() on them with every possible
1035 * combination of modifiers.
1037 if (key_sym
== SunXK_F36
|| key_sym
== SunXK_F37
)
1043 len
= keyval_to_string(key_sym
, state
, string2
);
1045 /* Careful: convert_input() doesn't handle the NUL character.
1046 * No need to convert pure ASCII anyway, thus the len > 1 check. */
1047 if (len
> 1 && input_conv
.vc_type
!= CONV_NONE
)
1048 len
= convert_input(string2
, len
, sizeof(string2
));
1053 if (input_conv
.vc_type
!= CONV_NONE
)
1055 mch_memmove(string2
, event
->string
, len
);
1056 len
= convert_input(string2
, len
, sizeof(string2
));
1061 s
= (char_u
*)event
->string
;
1065 for (i
= 0; i
< len
; ++i
)
1068 if (d
[-1] == CSI
&& d
+ 2 < string
+ sizeof(string
))
1070 /* Turn CSI into K_CSI. */
1078 /* Shift-Tab results in Left_Tab, but we want <S-Tab> */
1079 if (key_sym
== GDK_ISO_Left_Tab
)
1082 state
|= GDK_SHIFT_MASK
;
1085 #ifndef HAVE_GTK2 /* for GTK+ 2, we handle this in keyval_to_string() */
1086 if ((key_sym
== GDK_2
|| key_sym
== GDK_at
) && (state
& GDK_CONTROL_MASK
))
1088 string
[0] = NUL
; /* CTRL-2 and CTRL-@ is NUL */
1091 else if (len
== 0 && (key_sym
== GDK_space
|| key_sym
== GDK_Tab
))
1093 /* When there are modifiers, these keys get zero length; we need the
1094 * original key here to be able to add a modifier below. */
1095 string
[0] = (key_sym
& 0xff);
1101 /* If there is a menu and 'wak' is "yes", or 'wak' is "menu" and the key
1102 * is a menu shortcut, we ignore everything with the ALT modifier. */
1103 if ((state
& GDK_MOD1_MASK
)
1104 && gui
.menu_is_active
1108 && gui_is_menu_shortcut(string
[0]))))
1110 /* For GTK2 we return false to signify that we haven't handled the
1111 * keypress, so that gtk will handle the mnemonic or accelerator. */
1118 /* Check for Alt/Meta key (Mod1Mask), but not for a BS, DEL or character
1119 * that already has the 8th bit set.
1120 * Don't do this for <S-M-Tab>, that should become K_S_TAB with ALT.
1121 * Don't do this for double-byte encodings, it turns the char into a lead
1124 && (state
& GDK_MOD1_MASK
)
1125 && !(key_sym
== GDK_BackSpace
|| key_sym
== GDK_Delete
)
1126 && (string
[0] & 0x80) == 0
1127 && !(key_sym
== GDK_Tab
&& (state
& GDK_SHIFT_MASK
))
1134 state
&= ~GDK_MOD1_MASK
; /* don't use it again */
1136 if (enc_utf8
) /* convert to utf-8 */
1138 string
[1] = string
[0] & 0xbf;
1139 string
[0] = ((unsigned)string
[0] >> 6) + 0xc0;
1140 if (string
[1] == CSI
)
1142 string
[2] = KS_EXTRA
;
1143 string
[3] = (int)KE_CSI
;
1152 /* Check for special keys. Also do this when len == 1 (key has an ASCII
1153 * value) to detect backspace, delete and keypad keys. */
1154 if (len
== 0 || len
== 1)
1156 for (i
= 0; special_keys
[i
].key_sym
!= 0; i
++)
1158 if (special_keys
[i
].key_sym
== key_sym
)
1161 string
[1] = special_keys
[i
].code0
;
1162 string
[2] = special_keys
[i
].code1
;
1169 if (len
== 0) /* Unrecognized key */
1172 #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) && !defined(HAVE_GTK2)
1173 /* Cancel or type backspace. For GTK2, im_commit_cb() does the same. */
1174 preedit_start_col
= MAXCOL
;
1175 xim_changed_while_preediting
= TRUE
;
1178 /* Special keys (and a few others) may have modifiers. Also when using a
1179 * double-byte encoding (can't set the 8th bit). */
1180 if (len
== -3 || key_sym
== GDK_space
|| key_sym
== GDK_Tab
1181 || key_sym
== GDK_Return
|| key_sym
== GDK_Linefeed
1182 || key_sym
== GDK_Escape
|| key_sym
== GDK_KP_Tab
1183 || key_sym
== GDK_ISO_Enter
|| key_sym
== GDK_3270_Enter
1185 || (enc_dbcs
&& len
== 1 && (state
& GDK_MOD1_MASK
))
1189 modifiers
= modifiers_gdk2vim(state
);
1192 * For some keys a shift modifier is translated into another key
1196 key
= TO_SPECIAL(string
[1], string
[2]);
1200 key
= simplify_key(key
, &modifiers
);
1203 if (IS_SPECIAL(key
))
1206 string
[1] = K_SECOND(key
);
1207 string
[2] = K_THIRD(key
);
1219 string2
[1] = KS_MODIFIER
;
1220 string2
[2] = modifiers
;
1221 add_to_input_buf(string2
, 3);
1225 if (len
== 1 && ((string
[0] == Ctrl_C
&& ctrl_c_interrupts
)
1226 || (string
[0] == intr_char
&& intr_char
!= Ctrl_C
)))
1232 add_to_input_buf(string
, len
);
1234 /* blank out the pointer if necessary */
1236 gui_mch_mousehide(TRUE
);
1238 if (gtk_main_level() > 0)
1244 #if defined(FEAT_XIM) && defined(HAVE_GTK2)
1246 key_release_event(GtkWidget
*widget UNUSED
,
1248 gpointer data UNUSED
)
1251 * GTK+ 2 input methods may do fancy stuff on key release events too.
1252 * With the default IM for instance, you can enter any UCS code point
1253 * by holding down CTRL-SHIFT and typing hexadecimal digits.
1255 return xim_queue_key_press_event(event
, FALSE
);
1260 /****************************************************************************
1261 * Selection handlers:
1265 selection_clear_event(GtkWidget
*widget UNUSED
,
1266 GdkEventSelection
*event
,
1267 gpointer user_data UNUSED
)
1269 if (event
->selection
== clip_plus
.gtk_sel_atom
)
1270 clip_lose_selection(&clip_plus
);
1272 clip_lose_selection(&clip_star
);
1274 if (gtk_main_level() > 0)
1280 #define RS_NONE 0 /* selection_received_cb() not called yet */
1281 #define RS_OK 1 /* selection_received_cb() called and OK */
1282 #define RS_FAIL 2 /* selection_received_cb() called and failed */
1283 static int received_selection
= RS_NONE
;
1286 selection_received_cb(GtkWidget
*widget UNUSED
,
1287 GtkSelectionData
*data
,
1289 gpointer user_data UNUSED
)
1293 char_u
*tmpbuf
= NULL
;
1295 guchar
*tmpbuf_utf8
= NULL
;
1300 if (data
->selection
== clip_plus
.gtk_sel_atom
)
1305 text
= (char_u
*)data
->data
;
1307 motion_type
= MCHAR
;
1309 if (text
== NULL
|| len
<= 0)
1311 received_selection
= RS_FAIL
;
1312 /* clip_free_selection(cbd); ??? */
1314 if (gtk_main_level() > 0)
1320 if (data
->type
== vim_atom
)
1322 motion_type
= *text
++;
1327 else if (data
->type
== vimenc_atom
)
1332 motion_type
= *text
++;
1336 text
+= STRLEN(text
) + 1;
1339 /* If the encoding of the text is different from 'encoding', attempt
1341 conv
.vc_type
= CONV_NONE
;
1342 convert_setup(&conv
, enc
, p_enc
);
1343 if (conv
.vc_type
!= CONV_NONE
)
1345 tmpbuf
= string_convert(&conv
, text
, &len
);
1348 convert_setup(&conv
, NULL
, NULL
);
1354 /* gtk_selection_data_get_text() handles all the nasty details
1355 * and targets and encodings etc. This rocks so hard. */
1358 tmpbuf_utf8
= gtk_selection_data_get_text(data
);
1359 if (tmpbuf_utf8
!= NULL
)
1361 len
= STRLEN(tmpbuf_utf8
);
1362 if (input_conv
.vc_type
!= CONV_NONE
)
1364 tmpbuf
= string_convert(&input_conv
, tmpbuf_utf8
, &len
);
1371 else if (len
>= 2 && text
[0] == 0xff && text
[1] == 0xfe)
1375 /* UTF-16, we get this for HTML */
1376 conv
.vc_type
= CONV_NONE
;
1377 convert_setup_ext(&conv
, (char_u
*)"utf-16le", FALSE
, p_enc
, TRUE
);
1379 if (conv
.vc_type
!= CONV_NONE
)
1383 tmpbuf
= string_convert(&conv
, text
, &len
);
1384 convert_setup(&conv
, NULL
, NULL
);
1390 #else /* !HAVE_GTK2 */
1392 else if (data
->type
== utf8_string_atom
)
1396 conv
.vc_type
= CONV_NONE
;
1397 convert_setup(&conv
, (char_u
*)"utf-8", p_enc
);
1399 if (conv
.vc_type
!= CONV_NONE
)
1401 tmpbuf
= string_convert(&conv
, text
, &len
);
1402 convert_setup(&conv
, NULL
, NULL
);
1408 else if (data
->type
== compound_text_atom
|| data
->type
== text_atom
)
1413 unsigned tmplen
= 0;
1415 count
= gdk_text_property_to_text_list(data
->type
, data
->format
,
1416 data
->data
, data
->length
,
1418 for (i
= 0; i
< count
; ++i
)
1419 tmplen
+= strlen(list
[i
]);
1421 tmpbuf
= alloc(tmplen
+ 1);
1425 for (i
= 0; i
< count
; ++i
)
1426 STRCAT(tmpbuf
, list
[i
]);
1432 gdk_free_text_list(list
);
1434 #endif /* !HAVE_GTK2 */
1436 clip_yank_selection(motion_type
, text
, (long)len
, cbd
);
1437 received_selection
= RS_OK
;
1440 g_free(tmpbuf_utf8
);
1443 if (gtk_main_level() > 0)
1448 * Prepare our selection data for passing it to the external selection
1452 selection_get_cb(GtkWidget
*widget UNUSED
,
1453 GtkSelectionData
*selection_data
,
1456 gpointer user_data UNUSED
)
1466 if (selection_data
->selection
== clip_plus
.gtk_sel_atom
)
1472 return; /* Shouldn't ever happen */
1474 if (info
!= (guint
)TARGET_STRING
1476 && (!clip_html
|| info
!= (guint
)TARGET_HTML
)
1477 && info
!= (guint
)TARGET_UTF8_STRING
1478 && info
!= (guint
)TARGET_VIMENC
1480 && info
!= (guint
)TARGET_VIM
1481 && info
!= (guint
)TARGET_COMPOUND_TEXT
1482 && info
!= (guint
)TARGET_TEXT
)
1485 /* get the selection from the '*'/'+' register */
1486 clip_get_selection(cbd
);
1488 motion_type
= clip_convert_selection(&string
, &tmplen
, cbd
);
1489 if (motion_type
< 0 || string
== NULL
)
1491 /* Due to int arguments we can't handle more than G_MAXINT. Also
1492 * reserve one extra byte for NUL or the motion type; just in case.
1493 * (Not that pasting 2G of text is ever going to work, but... ;-) */
1494 length
= MIN(tmplen
, (long_u
)(G_MAXINT
- 1));
1496 if (info
== (guint
)TARGET_VIM
)
1498 tmpbuf
= alloc((unsigned)length
+ 1);
1501 tmpbuf
[0] = motion_type
;
1502 mch_memmove(tmpbuf
+ 1, string
, (size_t)length
);
1504 /* For our own format, the first byte contains the motion type */
1512 else if (info
== (guint
)TARGET_HTML
)
1516 /* Since we get utf-16, we probably should set it as well. */
1517 conv
.vc_type
= CONV_NONE
;
1518 convert_setup_ext(&conv
, p_enc
, TRUE
, (char_u
*)"utf-16le", FALSE
);
1519 if (conv
.vc_type
!= CONV_NONE
)
1521 tmpbuf
= string_convert(&conv
, string
, &length
);
1522 convert_setup(&conv
, NULL
, NULL
);
1527 /* Prepend the BOM: "fffe" */
1530 tmpbuf
= alloc(length
+ 2);
1533 mch_memmove(tmpbuf
+ 2, string
, (size_t)length
);
1538 selection_data
->type
= selection_data
->target
;
1539 selection_data
->format
= 16; /* 16 bits per char */
1540 gtk_selection_data_set(selection_data
, html_atom
, 16,
1546 else if (info
== (guint
)TARGET_VIMENC
)
1548 int l
= STRLEN(p_enc
);
1550 /* contents: motion_type 'encoding' NUL text */
1551 tmpbuf
= alloc((unsigned)length
+ l
+ 2);
1554 tmpbuf
[0] = motion_type
;
1555 STRCPY(tmpbuf
+ 1, p_enc
);
1556 mch_memmove(tmpbuf
+ l
+ 2, string
, (size_t)length
);
1566 /* gtk_selection_data_set_text() handles everything for us. This is
1567 * so easy and simple and cool, it'd be insane not to use it. */
1570 if (output_conv
.vc_type
!= CONV_NONE
)
1572 tmpbuf
= string_convert(&output_conv
, string
, &length
);
1578 /* Validate the string to avoid runtime warnings */
1579 if (g_utf8_validate((const char *)string
, (gssize
)length
, NULL
))
1581 gtk_selection_data_set_text(selection_data
,
1582 (const char *)string
, length
);
1587 #else /* !HAVE_GTK2 */
1589 else if (info
== (guint
)TARGET_UTF8_STRING
)
1593 conv
.vc_type
= CONV_NONE
;
1594 convert_setup(&conv
, p_enc
, (char_u
*)"utf-8");
1596 if (conv
.vc_type
!= CONV_NONE
)
1598 tmpbuf
= string_convert(&conv
, string
, &length
);
1599 convert_setup(&conv
, NULL
, NULL
);
1603 type
= utf8_string_atom
;
1606 else if (info
== (guint
)TARGET_COMPOUND_TEXT
1607 || info
== (guint
)TARGET_TEXT
)
1611 /* Copy the string to ensure NUL-termination */
1612 tmpbuf
= vim_strnsave(string
, length
);
1616 gdk_string_to_compound_text((const char *)tmpbuf
,
1617 &type
, &format
, &string
, &length
);
1619 selection_data
->type
= type
;
1620 selection_data
->format
= format
;
1621 gtk_selection_data_set(selection_data
, type
, format
, string
, length
);
1622 gdk_free_compound_text(string
);
1628 type
= GDK_TARGET_STRING
;
1630 #endif /* !HAVE_GTK2 */
1634 selection_data
->type
= selection_data
->target
;
1635 selection_data
->format
= 8; /* 8 bits per char */
1637 gtk_selection_data_set(selection_data
, type
, 8, string
, length
);
1643 * Check if the GUI can be started. Called before gvimrc is sourced.
1644 * Return OK or FAIL.
1647 gui_mch_init_check(void)
1650 /* This is needed to make the locale handling consistent between the GUI
1651 * and the rest of VIM. */
1655 #ifdef FEAT_GUI_GNOME
1656 if (gtk_socket_id
== 0)
1660 /* Don't use gtk_init() or gnome_init(), it exits on failure. */
1661 if (!gtk_init_check(&gui_argc
, &gui_argv
))
1664 EMSG(_((char *)e_opendisp
));
1672 /****************************************************************************
1673 * Mouse handling callbacks
1677 static guint mouse_click_timer
= 0;
1678 static int mouse_timed_out
= TRUE
;
1681 * Timer used to recognize multiple clicks of the mouse button
1684 mouse_click_timer_cb(gpointer data
)
1686 /* we don't use this information currently */
1687 int *timed_out
= (int *) data
;
1690 return FALSE
; /* don't happen again */
1693 static guint motion_repeat_timer
= 0;
1694 static int motion_repeat_offset
= FALSE
;
1695 static gint
motion_repeat_timer_cb(gpointer
);
1698 process_motion_notify(int x
, int y
, GdkModifierType state
)
1701 int_u vim_modifiers
;
1703 button
= (state
& (GDK_BUTTON1_MASK
| GDK_BUTTON2_MASK
|
1704 GDK_BUTTON3_MASK
| GDK_BUTTON4_MASK
|
1708 /* If our pointer is currently hidden, then we should show it. */
1709 gui_mch_mousehide(FALSE
);
1711 /* Just moving the rodent above the drawing area without any button
1713 if (button
!= MOUSE_DRAG
)
1715 gui_mouse_moved(x
, y
);
1719 /* translate modifier coding between the main engine and GTK */
1720 vim_modifiers
= modifiers_gdk2mouse(state
);
1722 /* inform the editor engine about the occurrence of this event */
1723 gui_send_mouse_event(button
, x
, y
, FALSE
, vim_modifiers
);
1725 if (gtk_main_level() > 0)
1729 * Auto repeat timer handling.
1732 || x
>= gui
.drawarea
->allocation
.width
1733 || y
>= gui
.drawarea
->allocation
.height
)
1741 /* Calculate the maximal distance of the cursor from the drawing area.
1742 * (offshoot can't become negative here!).
1744 dx
= x
< 0 ? -x
: x
- gui
.drawarea
->allocation
.width
;
1745 dy
= y
< 0 ? -y
: y
- gui
.drawarea
->allocation
.height
;
1747 offshoot
= dx
> dy
? dx
: dy
;
1749 /* Make a linearly decaying timer delay with a threshold of 5 at a
1750 * distance of 127 pixels from the main window.
1752 * One could think endlessly about the most ergonomic variant here.
1753 * For example it could make sense to calculate the distance from the
1754 * drags start instead...
1756 * Maybe a parabolic interpolation would suite us better here too...
1760 /* 5 appears to be somehow near to my perceptual limits :-). */
1765 delay
= (130 * (127 - offshoot
)) / 127 + 5;
1769 if (!motion_repeat_timer
)
1770 motion_repeat_timer
= gtk_timeout_add((guint32
)delay
,
1771 motion_repeat_timer_cb
, NULL
);
1776 * Timer used to recognize multiple clicks of the mouse button.
1779 motion_repeat_timer_cb(gpointer data UNUSED
)
1783 GdkModifierType state
;
1785 gdk_window_get_pointer(gui
.drawarea
->window
, &x
, &y
, &state
);
1787 if (!(state
& (GDK_BUTTON1_MASK
| GDK_BUTTON2_MASK
|
1788 GDK_BUTTON3_MASK
| GDK_BUTTON4_MASK
|
1791 motion_repeat_timer
= 0;
1795 /* If there already is a mouse click in the input buffer, wait another
1796 * time (otherwise we would create a backlog of clicks) */
1797 if (vim_used_in_input_buf() > 10)
1800 motion_repeat_timer
= 0;
1803 * Fake a motion event.
1804 * Trick: Pretend the mouse moved to the next character on every other
1805 * event, otherwise drag events will be discarded, because they are still
1806 * in the same character.
1808 if (motion_repeat_offset
)
1809 x
+= gui
.char_width
;
1811 motion_repeat_offset
= !motion_repeat_offset
;
1812 process_motion_notify(x
, y
, state
);
1814 /* Don't happen again. We will get reinstalled in the synthetic event
1815 * if needed -- thus repeating should still work. */
1820 motion_notify_event(GtkWidget
*widget
,
1821 GdkEventMotion
*event
,
1822 gpointer data UNUSED
)
1828 GdkModifierType state
;
1830 gdk_window_get_pointer(widget
->window
, &x
, &y
, &state
);
1831 process_motion_notify(x
, y
, state
);
1835 process_motion_notify((int)event
->x
, (int)event
->y
,
1836 (GdkModifierType
)event
->state
);
1839 return TRUE
; /* handled */
1844 * Mouse button handling. Note please that we are capturing multiple click's
1845 * by our own timeout mechanism instead of the one provided by GTK+ itself.
1846 * This is due to the way the generic VIM code is recognizing multiple clicks.
1849 button_press_event(GtkWidget
*widget
,
1850 GdkEventButton
*event
,
1851 gpointer data UNUSED
)
1854 int repeated_click
= FALSE
;
1856 int_u vim_modifiers
;
1858 /* Make sure we have focus now we've been selected */
1859 if (gtk_socket_id
!= 0 && !GTK_WIDGET_HAS_FOCUS(widget
))
1860 gtk_widget_grab_focus(widget
);
1863 * Don't let additional events about multiple clicks send by GTK to us
1864 * after the initial button press event confuse us.
1866 if (event
->type
!= GDK_BUTTON_PRESS
)
1872 /* Handle multiple clicks */
1873 if (!mouse_timed_out
&& mouse_click_timer
)
1875 gtk_timeout_remove(mouse_click_timer
);
1876 mouse_click_timer
= 0;
1877 repeated_click
= TRUE
;
1880 mouse_timed_out
= FALSE
;
1881 mouse_click_timer
= gtk_timeout_add((guint32
)p_mouset
,
1882 mouse_click_timer_cb
, &mouse_timed_out
);
1884 switch (event
->button
)
1887 button
= MOUSE_LEFT
;
1890 button
= MOUSE_MIDDLE
;
1893 button
= MOUSE_RIGHT
;
1904 return FALSE
; /* Unknown button */
1908 /* cancel any preediting */
1909 if (im_is_preediting())
1913 vim_modifiers
= modifiers_gdk2mouse(event
->state
);
1915 gui_send_mouse_event(button
, x
, y
, repeated_click
, vim_modifiers
);
1916 if (gtk_main_level() > 0)
1917 gtk_main_quit(); /* make sure the above will be handled immediately */
1924 * GTK+ 2 doesn't handle mouse buttons 4, 5, 6 and 7 the same way as GTK+ 1.
1925 * Instead, it abstracts scrolling via the new GdkEventScroll.
1928 scroll_event(GtkWidget
*widget
,
1929 GdkEventScroll
*event
,
1930 gpointer data UNUSED
)
1933 int_u vim_modifiers
;
1935 if (gtk_socket_id
!= 0 && !GTK_WIDGET_HAS_FOCUS(widget
))
1936 gtk_widget_grab_focus(widget
);
1938 switch (event
->direction
)
1943 case GDK_SCROLL_DOWN
:
1946 default: /* We don't care about left and right... Yet. */
1951 /* cancel any preediting */
1952 if (im_is_preediting())
1956 vim_modifiers
= modifiers_gdk2mouse(event
->state
);
1958 gui_send_mouse_event(button
, (int)event
->x
, (int)event
->y
,
1959 FALSE
, vim_modifiers
);
1961 if (gtk_main_level() > 0)
1962 gtk_main_quit(); /* make sure the above will be handled immediately */
1966 #endif /* HAVE_GTK2 */
1970 button_release_event(GtkWidget
*widget UNUSED
,
1971 GdkEventButton
*event
,
1972 gpointer data UNUSED
)
1975 int_u vim_modifiers
;
1977 /* Remove any motion "machine gun" timers used for automatic further
1978 extension of allocation areas if outside of the applications window
1980 if (motion_repeat_timer
)
1982 gtk_timeout_remove(motion_repeat_timer
);
1983 motion_repeat_timer
= 0;
1989 vim_modifiers
= modifiers_gdk2mouse(event
->state
);
1991 gui_send_mouse_event(MOUSE_RELEASE
, x
, y
, FALSE
, vim_modifiers
);
1992 if (gtk_main_level() > 0)
1993 gtk_main_quit(); /* make sure it will be handled immediately */
2000 /****************************************************************************
2001 * Drag aNd Drop support handlers.
2005 * Count how many items there may be and separate them with a NUL.
2006 * Apparently the items are separated with \r\n. This is not documented,
2007 * thus be careful not to go past the end. Also allow separation with
2011 count_and_decode_uri_list(char_u
*out
, char_u
*raw
, int len
)
2017 for (i
= 0; i
< len
; ++i
)
2019 if (raw
[i
] == NUL
|| raw
[i
] == '\n' || raw
[i
] == '\r')
2021 if (p
> out
&& p
[-1] != NUL
)
2027 else if (raw
[i
] == '%' && i
+ 2 < len
&& hexhex2nr(raw
+ i
+ 1) > 0)
2029 *p
++ = hexhex2nr(raw
+ i
+ 1);
2035 if (p
> out
&& p
[-1] != NUL
)
2037 *p
= NUL
; /* last item didn't have \r or \n */
2044 * Parse NUL separated "src" strings. Make it an array "outlist" form. On
2045 * this process, URI which protocol is not "file:" are removed. Return
2046 * length of array (less than "max").
2049 filter_uri_list(char_u
**outlist
, int max
, char_u
*src
)
2053 for (i
= j
= 0; i
< max
; ++i
)
2056 if (STRNCMP(src
, "file:", 5) == 0)
2059 if (STRNCMP(src
, "//localhost", 11) == 0)
2061 while (src
[0] == '/' && src
[1] == '/')
2063 outlist
[j
++] = vim_strsave(src
);
2065 src
+= STRLEN(src
) + 1;
2071 parse_uri_list(int *count
, char_u
*data
, int len
)
2075 char_u
**array
= NULL
;;
2077 if (data
!= NULL
&& len
> 0 && (tmp
= (char_u
*)alloc(len
+ 1)) != NULL
)
2079 n
= count_and_decode_uri_list(tmp
, data
, len
);
2080 if (n
> 0 && (array
= (char_u
**)alloc(n
* sizeof(char_u
*))) != NULL
)
2081 n
= filter_uri_list(array
, n
, tmp
);
2089 drag_handle_uri_list(GdkDragContext
*context
,
2090 GtkSelectionData
*data
,
2092 GdkModifierType state
,
2099 fnames
= parse_uri_list(&nfiles
, data
->data
, data
->length
);
2101 if (fnames
!= NULL
&& nfiles
> 0)
2105 gtk_drag_finish(context
, TRUE
, FALSE
, time_
); /* accept */
2107 modifiers
= modifiers_gdk2mouse(state
);
2109 gui_handle_drop(x
, y
, modifiers
, fnames
, nfiles
);
2116 drag_handle_text(GdkDragContext
*context
,
2117 GtkSelectionData
*data
,
2119 GdkModifierType state
)
2121 char_u dropkey
[6] = {CSI
, KS_MODIFIER
, 0, CSI
, KS_EXTRA
, (char_u
)KE_DROP
};
2125 char_u
*tmpbuf
= NULL
;
2132 if (data
->type
== utf8_string_atom
)
2135 if (input_conv
.vc_type
!= CONV_NONE
)
2136 tmpbuf
= string_convert(&input_conv
, text
, &len
);
2140 conv
.vc_type
= CONV_NONE
;
2141 convert_setup(&conv
, (char_u
*)"utf-8", p_enc
);
2143 if (conv
.vc_type
!= CONV_NONE
)
2145 tmpbuf
= string_convert(&conv
, text
, &len
);
2146 convert_setup(&conv
, NULL
, NULL
);
2152 # endif /* FEAT_MBYTE */
2154 dnd_yank_drag_data(text
, (long)len
);
2155 gtk_drag_finish(context
, TRUE
, FALSE
, time_
); /* accept */
2160 dropkey
[2] = modifiers_gdk2vim(state
);
2162 if (dropkey
[2] != 0)
2163 add_to_input_buf(dropkey
, (int)sizeof(dropkey
));
2165 add_to_input_buf(dropkey
+ 3, (int)(sizeof(dropkey
) - 3));
2167 if (gtk_main_level() > 0)
2175 drag_data_received_cb(GtkWidget
*widget
,
2176 GdkDragContext
*context
,
2179 GtkSelectionData
*data
,
2182 gpointer user_data UNUSED
)
2184 GdkModifierType state
;
2186 /* Guard against trash */
2187 if (data
->data
== NULL
2188 || data
->length
<= 0
2189 || data
->format
!= 8
2190 || data
->data
[data
->length
] != '\0')
2192 gtk_drag_finish(context
, FALSE
, FALSE
, time_
);
2196 /* Get the current modifier state for proper distinguishment between
2197 * different operations later. */
2198 gdk_window_get_pointer(widget
->window
, NULL
, NULL
, &state
);
2200 /* Not sure about the role of "text/plain" here... */
2201 if (info
== (guint
)TARGET_TEXT_URI_LIST
)
2202 drag_handle_uri_list(context
, data
, time_
, state
, x
, y
);
2204 drag_handle_text(context
, data
, time_
, state
);
2207 #endif /* FEAT_DND */
2210 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2212 * GnomeClient interact callback. Check for unsaved buffers that cannot
2213 * be abandoned and pop up a dialog asking the user for confirmation if
2217 sm_client_check_changed_any(GnomeClient
*client
,
2219 GnomeDialogType type
,
2222 cmdmod_T save_cmdmod
;
2223 gboolean shutdown_cancelled
;
2225 save_cmdmod
= cmdmod
;
2228 cmdmod
.browse
= TRUE
;
2230 # if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2231 cmdmod
.confirm
= TRUE
;
2234 * If there are changed buffers, present the user with
2235 * a dialog if possible, otherwise give an error message.
2237 shutdown_cancelled
= check_changed_any(FALSE
);
2240 cmdmod
= save_cmdmod
;
2241 setcursor(); /* position the cursor */
2244 * If the user hit the [Cancel] button the whole shutdown
2245 * will be cancelled. Wow, quite powerful feature (:
2247 gnome_interaction_key_return(key
, shutdown_cancelled
);
2251 * Generate a script that can be used to restore the current editing session.
2252 * Save the value of v:this_session before running :mksession in order to make
2253 * automagic session save fully transparent. Return TRUE on success.
2256 write_session_file(char_u
*filename
)
2258 char_u
*escaped_filename
;
2259 char *mksession_cmdline
;
2260 unsigned int save_ssop_flags
;
2264 * Build an ex command line to create a script that restores the current
2265 * session if executed. Escape the filename to avoid nasty surprises.
2267 escaped_filename
= vim_strsave_escaped(filename
, escape_chars
);
2268 if (escaped_filename
== NULL
)
2270 mksession_cmdline
= g_strconcat("mksession ", (char *)escaped_filename
,
2272 vim_free(escaped_filename
);
2275 * Use a reasonable hardcoded set of 'sessionoptions' flags to avoid
2276 * unpredictable effects when the session is saved automatically. Also,
2277 * we definitely need SSOP_GLOBALS to be able to restore v:this_session.
2278 * Don't use SSOP_BUFFERS to prevent the buffer list from becoming
2279 * enormously large if the GNOME session feature is used regularly.
2281 save_ssop_flags
= ssop_flags
;
2282 ssop_flags
= (SSOP_BLANK
|SSOP_CURDIR
|SSOP_FOLDS
|SSOP_GLOBALS
2283 |SSOP_HELP
|SSOP_OPTIONS
|SSOP_WINSIZE
|SSOP_TABPAGES
);
2285 do_cmdline_cmd((char_u
*)"let Save_VV_this_session = v:this_session");
2286 failed
= (do_cmdline_cmd((char_u
*)mksession_cmdline
) == FAIL
);
2287 do_cmdline_cmd((char_u
*)"let v:this_session = Save_VV_this_session");
2288 do_unlet((char_u
*)"Save_VV_this_session", TRUE
);
2290 ssop_flags
= save_ssop_flags
;
2291 g_free(mksession_cmdline
);
2293 * Reopen the file and append a command to restore v:this_session,
2294 * as if this save never happened. This is to avoid conflicts with
2295 * the user's own sessions. FIXME: It's probably less hackish to add
2296 * a "stealth" flag to 'sessionoptions' -- gotta ask Bram.
2302 fd
= open_exfile(filename
, TRUE
, APPENDBIN
);
2304 failed
= (fd
== NULL
2305 || put_line(fd
, "let v:this_session = Save_VV_this_session") == FAIL
2306 || put_line(fd
, "unlet Save_VV_this_session") == FAIL
);
2308 if (fd
!= NULL
&& fclose(fd
) != 0)
2312 mch_remove(filename
);
2319 * "save_yourself" signal handler. Initiate an interaction to ask the user
2320 * for confirmation if necessary. Save the current editing session and tell
2321 * the session manager how to restart Vim.
2324 sm_client_save_yourself(GnomeClient
*client
,
2326 GnomeSaveStyle save_style
,
2328 GnomeInteractStyle interact_style
,
2332 static const char suffix
[] = "-session.vim";
2337 /* Always request an interaction if possible. check_changed_any()
2338 * won't actually show a dialog unless any buffers have been modified.
2339 * There doesn't seem to be an obvious way to check that without
2340 * automatically firing the dialog. Anyway, it works just fine. */
2341 if (interact_style
== GNOME_INTERACT_ANY
)
2342 gnome_client_request_interaction(client
, GNOME_DIALOG_NORMAL
,
2343 &sm_client_check_changed_any
,
2346 ml_sync_all(FALSE
, FALSE
); /* preserve all swap files */
2348 /* The path is unique for each session save. We do neither know nor care
2349 * which session script will actually be used later. This decision is in
2350 * the domain of the session manager. */
2351 session_file
= gnome_config_get_real_path(
2352 gnome_client_get_config_prefix(client
));
2353 len
= strlen(session_file
);
2355 if (len
> 0 && session_file
[len
-1] == G_DIR_SEPARATOR
)
2356 --len
; /* get rid of the superfluous trailing '/' */
2358 session_file
= g_renew(char, session_file
, len
+ sizeof(suffix
));
2359 memcpy(session_file
+ len
, suffix
, sizeof(suffix
));
2361 success
= write_session_file((char_u
*)session_file
);
2365 const char *argv
[8];
2368 /* Tell the session manager how to wipe out the stored session data.
2369 * This isn't as dangerous as it looks, don't worry :) session_file
2370 * is a unique absolute filename. Usually it'll be something like
2371 * `/home/user/.gnome2/vim-XXXXXX-session.vim'. */
2374 argv
[i
++] = session_file
;
2377 gnome_client_set_discard_command(client
, i
, (char **)argv
);
2379 /* Tell the session manager how to restore the just saved session.
2380 * This is easily done thanks to Vim's -S option. Pass the -f flag
2381 * since there's no need to fork -- it might even cause confusion.
2382 * Also pass the window role to give the WM something to match on.
2383 * The role is set in gui_mch_open(), thus should _never_ be NULL. */
2385 argv
[i
++] = restart_command
;
2389 argv
[i
++] = "--role";
2390 argv
[i
++] = gtk_window_get_role(GTK_WINDOW(gui
.mainwin
));
2393 argv
[i
++] = session_file
;
2396 gnome_client_set_restart_command(client
, i
, (char **)argv
);
2397 gnome_client_set_clone_command(client
, 0, NULL
);
2400 g_free(session_file
);
2406 * Called when the session manager wants us to die. There isn't much to save
2407 * here since "save_yourself" has been emitted before (unless serious trouble
2411 sm_client_die(GnomeClient
*client
, gpointer data
)
2413 /* Don't write messages to the GUI anymore */
2414 full_screen
= FALSE
;
2416 vim_strncpy(IObuff
, (char_u
*)
2417 _("Vim: Received \"die\" request from session manager\n"),
2423 * Connect our signal handlers to be notified on session save and shutdown.
2426 setup_save_yourself(void)
2428 GnomeClient
*client
;
2430 client
= gnome_master_client();
2434 /* Must use the deprecated gtk_signal_connect() for compatibility
2435 * with GNOME 1. Arrgh, zombies! */
2436 gtk_signal_connect(GTK_OBJECT(client
), "save_yourself",
2437 GTK_SIGNAL_FUNC(&sm_client_save_yourself
), NULL
);
2438 gtk_signal_connect(GTK_OBJECT(client
), "die",
2439 GTK_SIGNAL_FUNC(&sm_client_die
), NULL
);
2443 #else /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2447 * GTK tells us that XSMP needs attention
2450 local_xsmp_handle_requests(source
, condition
, data
)
2451 GIOChannel
*source UNUSED
;
2452 GIOCondition condition
;
2455 if (condition
== G_IO_IN
)
2457 /* Do stuff; maybe close connection */
2458 if (xsmp_handle_requests() == FAIL
)
2459 g_io_channel_unref((GIOChannel
*)data
);
2463 g_io_channel_unref((GIOChannel
*)data
);
2467 # endif /* USE_XSMP */
2470 * Setup the WM_PROTOCOLS to indicate we want the WM_SAVE_YOURSELF event.
2471 * This is an ugly use of X functions. GTK doesn't offer an alternative.
2474 setup_save_yourself(void)
2476 Atom
*existing_atoms
= NULL
;
2480 if (xsmp_icefd
!= -1)
2483 * Use XSMP is preference to legacy WM_SAVE_YOURSELF;
2484 * set up GTK IO monitor
2486 GIOChannel
*g_io
= g_io_channel_unix_new(xsmp_icefd
);
2488 g_io_add_watch(g_io
, G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
2489 local_xsmp_handle_requests
, (gpointer
)g_io
);
2494 /* Fall back to old method */
2496 /* first get the existing value */
2497 if (XGetWMProtocols(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2498 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
),
2499 &existing_atoms
, &count
))
2502 Atom save_yourself_xatom
;
2505 save_yourself_xatom
= GET_X_ATOM(save_yourself_atom
);
2507 /* check if WM_SAVE_YOURSELF isn't there yet */
2508 for (i
= 0; i
< count
; ++i
)
2509 if (existing_atoms
[i
] == save_yourself_xatom
)
2514 /* allocate an Atoms array which is one item longer */
2515 new_atoms
= (Atom
*)alloc((unsigned)((count
+ 1)
2517 if (new_atoms
!= NULL
)
2519 memcpy(new_atoms
, existing_atoms
, count
* sizeof(Atom
));
2520 new_atoms
[count
] = save_yourself_xatom
;
2521 XSetWMProtocols(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2522 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
),
2523 new_atoms
, count
+ 1);
2524 vim_free(new_atoms
);
2527 XFree(existing_atoms
);
2534 * Installing a global event filter seems to be the only way to catch
2535 * client messages of type WM_PROTOCOLS without overriding GDK's own
2536 * client message event filter. Well, that's still better than trying
2537 * to guess what the GDK filter had done if it had been invoked instead
2538 * (This is what we did for GTK+ 1.2, see below).
2540 * GTK2_FIXME: This doesn't seem to work. For some reason we never
2541 * receive WM_SAVE_YOURSELF even though everything is set up correctly.
2542 * I have the nasty feeling modern session managers just don't send this
2543 * deprecated message anymore. Addition: confirmed by several people.
2545 * The GNOME session support is much cooler anyway. Unlike this ugly
2546 * WM_SAVE_YOURSELF hack it actually stores the session... And yes,
2547 * it should work with KDE as well.
2549 static GdkFilterReturn
2550 global_event_filter(GdkXEvent
*xev
,
2551 GdkEvent
*event UNUSED
,
2552 gpointer data UNUSED
)
2554 XEvent
*xevent
= (XEvent
*)xev
;
2557 && xevent
->type
== ClientMessage
2558 && xevent
->xclient
.message_type
== GET_X_ATOM(wm_protocols_atom
)
2559 && (long_u
)xevent
->xclient
.data
.l
[0]
2560 == GET_X_ATOM(save_yourself_atom
))
2563 ml_sync_all(FALSE
, FALSE
); /* preserve all swap files */
2565 * Set the window's WM_COMMAND property, to let the window manager
2566 * know we are done saving ourselves. We don't want to be
2567 * restarted, thus set argv to NULL.
2569 XSetCommand(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2570 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
),
2572 return GDK_FILTER_REMOVE
;
2575 return GDK_FILTER_CONTINUE
;
2578 # else /* !HAVE_GTK2 */
2581 * GDK handler for X ClientMessage events.
2583 static GdkFilterReturn
2584 gdk_wm_protocols_filter(GdkXEvent
*xev
, GdkEvent
*event
, gpointer data
)
2586 /* From example in gdkevents.c/gdk_wm_protocols_filter */
2587 XEvent
*xevent
= (XEvent
*)xev
;
2591 if (xevent
->xclient
.data
.l
[0] == GET_X_ATOM(save_yourself_atom
))
2594 ml_sync_all(FALSE
, FALSE
); /* preserve all swap files */
2596 /* Set the window's WM_COMMAND property, to let the window manager
2597 * know we are done saving ourselves. We don't want to be
2598 * restarted, thus set argv to NULL. */
2599 XSetCommand(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2600 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
),
2604 * Functionality from gdkevents.c/gdk_wm_protocols_filter;
2605 * Registering this filter apparently overrides the default GDK one,
2606 * so we need to perform its functionality. There seems no way to
2607 * register for WM_PROTOCOLS, and only process the WM_SAVE_YOURSELF
2608 * bit; it's all or nothing. Update: No, there is a way -- but it
2609 * only works with GTK+ 2 apparently. See above.
2611 else if (xevent
->xclient
.data
.l
[0] == GET_X_ATOM(gdk_wm_delete_window
))
2613 event
->any
.type
= GDK_DELETE
;
2614 return GDK_FILTER_TRANSLATE
;
2618 return GDK_FILTER_REMOVE
;
2620 # endif /* !HAVE_GTK2 */
2622 #endif /* !(FEAT_GUI_GNOME && FEAT_SESSION) */
2626 * Setup the window icon & xcmdsrv comm after the main window has been realized.
2629 mainwin_realize(GtkWidget
*widget UNUSED
, gpointer data UNUSED
)
2631 /* If you get an error message here, you still need to unpack the runtime
2637 /* A bit hackish, but avoids casting later and allows optimization */
2638 # define static static const
2640 #define magick vim32x32
2641 #include "../runtime/vim32x32.xpm"
2643 #define magick vim16x16
2644 #include "../runtime/vim16x16.xpm"
2646 #define magick vim48x48
2647 #include "../runtime/vim48x48.xpm"
2653 /* When started with "--echo-wid" argument, write window ID on stdout. */
2656 printf("WID: %ld\n", (long)GDK_WINDOW_XWINDOW(gui
.mainwin
->window
));
2660 if (vim_strchr(p_go
, GO_ICON
) != NULL
)
2663 * Add an icon to the main window. For fun and convenience of the user.
2666 GList
*icons
= NULL
;
2668 icons
= g_list_prepend(icons
, gdk_pixbuf_new_from_xpm_data(vim16x16
));
2669 icons
= g_list_prepend(icons
, gdk_pixbuf_new_from_xpm_data(vim32x32
));
2670 icons
= g_list_prepend(icons
, gdk_pixbuf_new_from_xpm_data(vim48x48
));
2672 gtk_window_set_icon_list(GTK_WINDOW(gui
.mainwin
), icons
);
2674 g_list_foreach(icons
, (GFunc
)&g_object_unref
, NULL
);
2677 #else /* !HAVE_GTK2 */
2680 GdkBitmap
*icon_mask
= NULL
;
2681 char **magick
= vim32x32
;
2687 * Adjust the icon to the preferences of the actual window manager.
2688 * This is once again a workaround for a deficiency in GTK+ 1.2.
2690 xdisplay
= GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
);
2691 root_window
= XRootWindow(xdisplay
, DefaultScreen(xdisplay
));
2692 if (XGetIconSizes(xdisplay
, root_window
, &size
, &number_sizes
))
2694 if (number_sizes
> 0)
2696 if (size
->max_height
>= 48 && size
->max_height
>= 48)
2698 else if (size
->max_height
>= 32 && size
->max_height
>= 32)
2700 else if (size
->max_height
>= 16 && size
->max_height
>= 16)
2705 icon
= gdk_pixmap_create_from_xpm_d(gui
.mainwin
->window
,
2706 &icon_mask
, NULL
, magick
);
2708 /* Note: for some reason gdk_window_set_icon() doesn't acquire
2709 * a reference on the pixmap, thus we _have_ to leak it. */
2710 gdk_window_set_icon(gui
.mainwin
->window
, NULL
, icon
, icon_mask
);
2712 #endif /* !HAVE_GTK2 */
2715 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
2716 /* Register a handler for WM_SAVE_YOURSELF with GDK's low-level X I/F */
2718 gdk_window_add_filter(NULL
, &global_event_filter
, NULL
);
2720 gdk_add_client_message_filter(wm_protocols_atom
,
2721 &gdk_wm_protocols_filter
, NULL
);
2724 /* Setup to indicate to the window manager that we want to catch the
2725 * WM_SAVE_YOURSELF event. For GNOME, this connects to the session
2726 * manager instead. */
2727 #if defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)
2730 setup_save_yourself();
2732 #ifdef FEAT_CLIENTSERVER
2733 if (serverName
== NULL
&& serverDelayedStartName
!= NULL
)
2735 /* This is a :gui command in a plain vim with no previous server */
2736 commWindow
= GDK_WINDOW_XWINDOW(gui
.mainwin
->window
);
2738 (void)serverRegisterName(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2739 serverDelayedStartName
);
2744 * Cannot handle "XLib-only" windows with gtk event routines, we'll
2745 * have to change the "server" registration to that of the main window
2746 * If we have not registered a name yet, remember the window
2748 serverChangeRegisteredWindow(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
2749 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
));
2751 gtk_widget_add_events(gui
.mainwin
, GDK_PROPERTY_CHANGE_MASK
);
2752 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "property_notify_event",
2753 GTK_SIGNAL_FUNC(property_event
), NULL
);
2758 create_blank_pointer(void)
2760 GdkWindow
*root_window
= NULL
;
2761 GdkPixmap
*blank_mask
;
2763 GdkColor color
= { 0, 0, 0, 0 };
2764 char blank_data
[] = { 0x0 };
2766 #ifdef HAVE_GTK_MULTIHEAD
2767 root_window
= gtk_widget_get_root_window(gui
.mainwin
);
2770 /* Create a pseudo blank pointer, which is in fact one pixel by one pixel
2772 blank_mask
= gdk_bitmap_create_from_data(root_window
, blank_data
, 1, 1);
2773 cursor
= gdk_cursor_new_from_pixmap(blank_mask
, blank_mask
,
2774 &color
, &color
, 0, 0);
2775 gdk_bitmap_unref(blank_mask
);
2780 #ifdef HAVE_GTK_MULTIHEAD
2782 mainwin_screen_changed_cb(GtkWidget
*widget
,
2783 GdkScreen
*previous_screen UNUSED
,
2784 gpointer data UNUSED
)
2786 if (!gtk_widget_has_screen(widget
))
2790 * Recreate the invisible mouse cursor.
2792 if (gui
.blank_pointer
!= NULL
)
2793 gdk_cursor_unref(gui
.blank_pointer
);
2795 gui
.blank_pointer
= create_blank_pointer();
2797 if (gui
.pointer_hidden
&& gui
.drawarea
->window
!= NULL
)
2798 gdk_window_set_cursor(gui
.drawarea
->window
, gui
.blank_pointer
);
2801 * Create a new PangoContext for this screen, and initialize it
2802 * with the current font if necessary.
2804 if (gui
.text_context
!= NULL
)
2805 g_object_unref(gui
.text_context
);
2807 gui
.text_context
= gtk_widget_create_pango_context(widget
);
2808 pango_context_set_base_dir(gui
.text_context
, PANGO_DIRECTION_LTR
);
2810 if (gui
.norm_font
!= NULL
)
2812 gui_mch_init_font(p_guifont
, FALSE
);
2813 gui_set_shellsize(FALSE
, FALSE
, RESIZE_BOTH
);
2816 #endif /* HAVE_GTK_MULTIHEAD */
2819 * After the drawing area comes up, we calculate all colors and create the
2820 * dummy blank cursor.
2822 * Don't try to set any VIM scrollbar sizes anywhere here. I'm relying on the
2823 * fact that the main VIM engine doesn't take them into account anywhere.
2826 drawarea_realize_cb(GtkWidget
*widget
, gpointer data UNUSED
)
2833 gui_mch_new_colors();
2834 gui
.text_gc
= gdk_gc_new(gui
.drawarea
->window
);
2836 gui
.blank_pointer
= create_blank_pointer();
2837 if (gui
.pointer_hidden
)
2838 gdk_window_set_cursor(widget
->window
, gui
.blank_pointer
);
2840 /* get the actual size of the scrollbars, if they are realized */
2841 sbar
= firstwin
->w_scrollbars
[SBAR_LEFT
].id
;
2842 if (!sbar
|| (!gui
.which_scrollbars
[SBAR_LEFT
]
2843 && firstwin
->w_scrollbars
[SBAR_RIGHT
].id
))
2844 sbar
= firstwin
->w_scrollbars
[SBAR_RIGHT
].id
;
2845 if (sbar
&& GTK_WIDGET_REALIZED(sbar
) && sbar
->allocation
.width
)
2846 gui
.scrollbar_width
= sbar
->allocation
.width
;
2848 sbar
= gui
.bottom_sbar
.id
;
2849 if (sbar
&& GTK_WIDGET_REALIZED(sbar
) && sbar
->allocation
.height
)
2850 gui
.scrollbar_height
= sbar
->allocation
.height
;
2854 * Properly clean up on shutdown.
2857 drawarea_unrealize_cb(GtkWidget
*widget UNUSED
, gpointer data UNUSED
)
2859 /* Don't write messages to the GUI anymore */
2860 full_screen
= FALSE
;
2866 if (gui
.ascii_glyphs
!= NULL
)
2868 pango_glyph_string_free(gui
.ascii_glyphs
);
2869 gui
.ascii_glyphs
= NULL
;
2871 if (gui
.ascii_font
!= NULL
)
2873 g_object_unref(gui
.ascii_font
);
2874 gui
.ascii_font
= NULL
;
2876 g_object_unref(gui
.text_context
);
2877 gui
.text_context
= NULL
;
2879 g_object_unref(gui
.text_gc
);
2882 gdk_cursor_unref(gui
.blank_pointer
);
2883 gui
.blank_pointer
= NULL
;
2885 gdk_gc_unref(gui
.text_gc
);
2888 gdk_cursor_destroy(gui
.blank_pointer
);
2889 gui
.blank_pointer
= NULL
;
2894 drawarea_style_set_cb(GtkWidget
*widget UNUSED
,
2895 GtkStyle
*previous_style UNUSED
,
2896 gpointer data UNUSED
)
2898 gui_mch_new_colors();
2902 * Callback routine for the "delete_event" signal on the toplevel window.
2903 * Tries to vim gracefully, or refuses to exit with changed buffers.
2906 delete_event_cb(GtkWidget
*widget UNUSED
,
2907 GdkEventAny
*event UNUSED
,
2908 gpointer data UNUSED
)
2914 #if defined(FEAT_MENU) || defined(FEAT_TOOLBAR) || defined(FEAT_GUI_TABLINE)
2916 get_item_dimensions(GtkWidget
*widget
, GtkOrientation orientation
)
2918 GtkOrientation item_orientation
= GTK_ORIENTATION_HORIZONTAL
;
2920 #ifdef FEAT_GUI_GNOME
2921 if (using_gnome
&& widget
!= NULL
)
2925 BonoboDockItem
*dockitem
;
2927 parent
= gtk_widget_get_parent(widget
);
2928 if (G_TYPE_FROM_INSTANCE(parent
) == BONOBO_TYPE_DOCK_ITEM
)
2930 /* Only menu & toolbar are dock items. Could tabline be?
2931 * Seem to be only the 2 defined in GNOME */
2933 dockitem
= BONOBO_DOCK_ITEM(widget
);
2935 if (dockitem
== NULL
|| dockitem
->is_floating
)
2937 item_orientation
= bonobo_dock_item_get_orientation(dockitem
);
2940 GnomeDockItem
*dockitem
;
2942 widget
= widget
->parent
;
2943 dockitem
= GNOME_DOCK_ITEM(widget
);
2945 if (dockitem
== NULL
|| dockitem
->is_floating
)
2947 item_orientation
= gnome_dock_item_get_orientation(dockitem
);
2952 && item_orientation
== orientation
2953 && GTK_WIDGET_REALIZED(widget
)
2954 && GTK_WIDGET_VISIBLE(widget
))
2956 if (orientation
== GTK_ORIENTATION_HORIZONTAL
)
2957 return widget
->allocation
.height
;
2959 return widget
->allocation
.width
;
2966 get_menu_tool_width(void)
2970 #ifdef FEAT_GUI_GNOME /* these are never vertical without GNOME */
2972 width
+= get_item_dimensions(gui
.menubar
, GTK_ORIENTATION_VERTICAL
);
2974 # ifdef FEAT_TOOLBAR
2975 width
+= get_item_dimensions(gui
.toolbar
, GTK_ORIENTATION_VERTICAL
);
2977 # ifdef FEAT_GUI_TABLINE
2978 if (gui
.tabline
!= NULL
)
2979 width
+= get_item_dimensions(gui
.tabline
, GTK_ORIENTATION_VERTICAL
);
2987 get_menu_tool_height(void)
2992 height
+= get_item_dimensions(gui
.menubar
, GTK_ORIENTATION_HORIZONTAL
);
2995 height
+= get_item_dimensions(gui
.toolbar
, GTK_ORIENTATION_HORIZONTAL
);
2997 #ifdef FEAT_GUI_TABLINE
2998 if (gui
.tabline
!= NULL
)
2999 height
+= get_item_dimensions(gui
.tabline
, GTK_ORIENTATION_HORIZONTAL
);
3005 /* This controls whether we can set the real window hints at
3006 * start-up when in a GtkPlug.
3007 * 0 = normal processing (default)
3008 * 1 = init. hints set, no-one's tried to reset since last check
3009 * 2 = init. hints set, attempt made to change hints
3011 static int init_window_hints_state
= 0;
3014 update_window_manager_hints(int force_width
, int force_height
)
3016 static int old_width
= 0;
3017 static int old_height
= 0;
3018 static int old_min_width
= 0;
3019 static int old_min_height
= 0;
3020 static int old_char_width
= 0;
3021 static int old_char_height
= 0;
3028 /* At start-up, don't try to set the hints until the initial
3029 * values have been used (those that dictate our initial size)
3030 * Let forced (i.e., correct) values through always.
3032 if (!(force_width
&& force_height
) && init_window_hints_state
> 0)
3035 init_window_hints_state
= 2;
3039 /* This also needs to be done when the main window isn't there yet,
3040 * otherwise the hints don't work. */
3041 width
= gui_get_base_width();
3042 height
= gui_get_base_height();
3044 height
+= tabline_height() * gui
.char_height
;
3047 width
+= get_menu_tool_width();
3048 height
+= get_menu_tool_height();
3051 /* GtkSockets use GtkPlug's [gui,mainwin] min-size hints to determine
3052 * their actual widget size. When we set our size ourselves (e.g.,
3053 * 'set columns=' or init. -geom) we briefly set the min. to the size
3054 * we wish to be instead of the legitimate minimum so that we actually
3057 if (force_width
&& force_height
)
3059 min_width
= force_width
;
3060 min_height
= force_height
;
3064 min_width
= width
+ MIN_COLUMNS
* gui
.char_width
;
3065 min_height
= height
+ MIN_LINES
* gui
.char_height
;
3068 /* Avoid an expose event when the size didn't change. */
3069 if (width
!= old_width
3070 || height
!= old_height
3071 || min_width
!= old_min_width
3072 || min_height
!= old_min_height
3073 || gui
.char_width
!= old_char_width
3074 || gui
.char_height
!= old_char_height
)
3076 GdkGeometry geometry
;
3077 GdkWindowHints geometry_mask
;
3079 geometry
.width_inc
= gui
.char_width
;
3080 geometry
.height_inc
= gui
.char_height
;
3081 geometry
.base_width
= width
;
3082 geometry
.base_height
= height
;
3083 geometry
.min_width
= min_width
;
3084 geometry
.min_height
= min_height
;
3085 geometry_mask
= GDK_HINT_BASE_SIZE
|GDK_HINT_RESIZE_INC
3088 /* Using gui.formwin as geometry widget doesn't work as expected
3089 * with GTK+ 2 -- dunno why. Presumably all the resizing hacks
3090 * in Vim confuse GTK+. */
3091 gtk_window_set_geometry_hints(GTK_WINDOW(gui
.mainwin
), gui
.mainwin
,
3092 &geometry
, geometry_mask
);
3094 gtk_window_set_geometry_hints(GTK_WINDOW(gui
.mainwin
), gui
.formwin
,
3095 &geometry
, geometry_mask
);
3098 old_height
= height
;
3099 old_min_width
= min_width
;
3100 old_min_height
= min_height
;
3101 old_char_width
= gui
.char_width
;
3102 old_char_height
= gui
.char_height
;
3110 * This extra effort wouldn't be necessary if we only used stock icons in the
3111 * toolbar, as we do for all builtin icons. But user-defined toolbar icons
3112 * shouldn't be treated differently, thus we do need this.
3115 icon_size_changed_foreach(GtkWidget
*widget
, gpointer user_data
)
3117 if (GTK_IS_IMAGE(widget
))
3119 GtkImage
*image
= (GtkImage
*)widget
;
3121 /* User-defined icons are stored in a GtkIconSet */
3122 if (gtk_image_get_storage_type(image
) == GTK_IMAGE_ICON_SET
)
3124 GtkIconSet
*icon_set
;
3125 GtkIconSize icon_size
;
3127 gtk_image_get_icon_set(image
, &icon_set
, &icon_size
);
3128 icon_size
= (GtkIconSize
)(long)user_data
;
3130 gtk_icon_set_ref(icon_set
);
3131 gtk_image_set_from_icon_set(image
, icon_set
, icon_size
);
3132 gtk_icon_set_unref(icon_set
);
3135 else if (GTK_IS_CONTAINER(widget
))
3137 gtk_container_foreach((GtkContainer
*)widget
,
3138 &icon_size_changed_foreach
,
3142 # endif /* HAVE_GTK2 */
3145 set_toolbar_style(GtkToolbar
*toolbar
)
3147 GtkToolbarStyle style
;
3150 GtkIconSize oldsize
;
3154 if ((toolbar_flags
& (TOOLBAR_TEXT
| TOOLBAR_ICONS
| TOOLBAR_HORIZ
))
3155 == (TOOLBAR_TEXT
| TOOLBAR_ICONS
| TOOLBAR_HORIZ
))
3156 style
= GTK_TOOLBAR_BOTH_HORIZ
;
3159 if ((toolbar_flags
& (TOOLBAR_TEXT
| TOOLBAR_ICONS
))
3160 == (TOOLBAR_TEXT
| TOOLBAR_ICONS
))
3161 style
= GTK_TOOLBAR_BOTH
;
3162 else if (toolbar_flags
& TOOLBAR_TEXT
)
3163 style
= GTK_TOOLBAR_TEXT
;
3165 style
= GTK_TOOLBAR_ICONS
;
3167 gtk_toolbar_set_style(toolbar
, style
);
3168 gtk_toolbar_set_tooltips(toolbar
, (toolbar_flags
& TOOLBAR_TOOLTIPS
) != 0);
3173 case TBIS_TINY
: size
= GTK_ICON_SIZE_MENU
; break;
3174 case TBIS_SMALL
: size
= GTK_ICON_SIZE_SMALL_TOOLBAR
; break;
3175 case TBIS_MEDIUM
: size
= GTK_ICON_SIZE_BUTTON
; break;
3176 case TBIS_LARGE
: size
= GTK_ICON_SIZE_LARGE_TOOLBAR
; break;
3177 default: size
= GTK_ICON_SIZE_INVALID
; break;
3179 oldsize
= gtk_toolbar_get_icon_size(toolbar
);
3181 if (size
== GTK_ICON_SIZE_INVALID
)
3183 /* Let global user preferences decide the icon size. */
3184 gtk_toolbar_unset_icon_size(toolbar
);
3185 size
= gtk_toolbar_get_icon_size(toolbar
);
3187 if (size
!= oldsize
)
3189 gtk_container_foreach(GTK_CONTAINER(toolbar
),
3190 &icon_size_changed_foreach
,
3191 GINT_TO_POINTER((int)size
));
3193 gtk_toolbar_set_icon_size(toolbar
, size
);
3197 #endif /* FEAT_TOOLBAR */
3199 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
3200 static int ignore_tabline_evt
= FALSE
;
3201 static GtkWidget
*tabline_menu
;
3202 static GtkTooltips
*tabline_tooltip
;
3203 static int clicked_page
; /* page clicked in tab line */
3206 * Handle selecting an item in the tab line popup menu.
3209 tabline_menu_handler(GtkMenuItem
*item UNUSED
, gpointer user_data
)
3211 /* Add the string cmd into input buffer */
3212 send_tabline_menu_event(clicked_page
, (int)(long)user_data
);
3214 if (gtk_main_level() > 0)
3219 add_tabline_menu_item(GtkWidget
*menu
, char_u
*text
, int resp
)
3224 utf_text
= CONVERT_TO_UTF8(text
);
3225 item
= gtk_menu_item_new_with_label((const char *)utf_text
);
3226 gtk_widget_show(item
);
3227 CONVERT_TO_UTF8_FREE(utf_text
);
3229 gtk_container_add(GTK_CONTAINER(menu
), item
);
3230 gtk_signal_connect(GTK_OBJECT(item
), "activate",
3231 GTK_SIGNAL_FUNC(tabline_menu_handler
),
3232 (gpointer
)(long)resp
);
3236 * Create a menu for the tab line.
3239 create_tabline_menu(void)
3243 menu
= gtk_menu_new();
3244 add_tabline_menu_item(menu
, (char_u
*)_("Close"), TABLINE_MENU_CLOSE
);
3245 add_tabline_menu_item(menu
, (char_u
*)_("New tab"), TABLINE_MENU_NEW
);
3246 add_tabline_menu_item(menu
, (char_u
*)_("Open Tab..."), TABLINE_MENU_OPEN
);
3252 on_tabline_menu(GtkWidget
*widget
, GdkEvent
*event
)
3254 /* Was this button press event ? */
3255 if (event
->type
== GDK_BUTTON_PRESS
)
3257 GdkEventButton
*bevent
= (GdkEventButton
*)event
;
3260 GtkWidget
*tabwidget
;
3263 /* When ignoring events return TRUE so that the selected page doesn't
3272 tabwin
= gdk_window_at_pointer(&x
, &y
);
3273 gdk_window_get_user_data(tabwin
, (gpointer
)&tabwidget
);
3274 clicked_page
= (int)(long)gtk_object_get_user_data(
3275 GTK_OBJECT(tabwidget
));
3277 /* If the event was generated for 3rd button popup the menu. */
3278 if (bevent
->button
== 3)
3280 gtk_menu_popup(GTK_MENU(widget
), NULL
, NULL
, NULL
, NULL
,
3281 bevent
->button
, bevent
->time
);
3282 /* We handled the event. */
3285 else if (bevent
->button
== 1)
3287 if (clicked_page
== 0)
3289 /* Click after all tabs moves to next tab page. When "x" is
3290 * small guess it's the left button. */
3291 if (send_tabline_event(x
< 50 ? -1 : 0) && gtk_main_level() > 0)
3296 gtk_notebook_set_page(GTK_NOTEBOOK(gui
.tabline
),
3302 /* We didn't handle the event. */
3307 * Handle selecting one of the tabs.
3311 GtkNotebook
*notebook UNUSED
,
3312 GtkNotebookPage
*page UNUSED
,
3314 gpointer data UNUSED
)
3316 if (!ignore_tabline_evt
)
3318 if (send_tabline_event(idx
+ 1) && gtk_main_level() > 0)
3324 static int showing_tabline
= 0;
3328 * Show or hide the tabline.
3331 gui_mch_show_tabline(int showit
)
3333 if (gui
.tabline
== NULL
)
3337 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3338 if (!showit
!= !gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui
.tabline
)))
3340 if (!showit
!= !showing_tabline
)
3343 /* Note: this may cause a resize event */
3344 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui
.tabline
), showit
);
3345 update_window_manager_hints(0, 0);
3347 showing_tabline
= showit
;
3350 GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(gui
.tabline
), GTK_CAN_FOCUS
);
3357 * Return TRUE when tabline is displayed.
3360 gui_mch_showing_tabline(void)
3362 return gui
.tabline
!= NULL
3364 /* gtk_notebook_get_show_tabs does not exist in gtk+-1.2.10 */
3365 && gtk_notebook_get_show_tabs(GTK_NOTEBOOK(gui
.tabline
))
3373 * Update the labels of the tabline.
3376 gui_mch_update_tabline(void)
3379 GtkWidget
*event_box
;
3387 if (gui
.tabline
== NULL
)
3390 ignore_tabline_evt
= TRUE
;
3392 /* Add a label for each tab page. They all contain the same text area. */
3393 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
, ++nr
)
3400 page
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui
.tabline
), nr
);
3403 /* Add notebook page */
3404 page
= gtk_vbox_new(FALSE
, 0);
3405 gtk_widget_show(page
);
3406 event_box
= gtk_event_box_new();
3407 gtk_widget_show(event_box
);
3408 label
= gtk_label_new("-Empty-");
3409 gtk_misc_set_padding(GTK_MISC(label
), 2, 2);
3410 gtk_container_add(GTK_CONTAINER(event_box
), label
);
3411 gtk_widget_show(label
);
3412 gtk_notebook_insert_page(GTK_NOTEBOOK(gui
.tabline
),
3418 event_box
= gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui
.tabline
), page
);
3419 gtk_object_set_user_data(GTK_OBJECT(event_box
),
3420 (gpointer
)(long)tab_num
);
3421 label
= GTK_BIN(event_box
)->child
;
3422 get_tabline_label(tp
, FALSE
);
3423 labeltext
= CONVERT_TO_UTF8(NameBuff
);
3424 gtk_label_set_text(GTK_LABEL(label
), (const char *)labeltext
);
3425 CONVERT_TO_UTF8_FREE(labeltext
);
3427 get_tabline_label(tp
, TRUE
);
3428 labeltext
= CONVERT_TO_UTF8(NameBuff
);
3429 gtk_tooltips_set_tip(GTK_TOOLTIPS(tabline_tooltip
), event_box
,
3430 (const char *)labeltext
, NULL
);
3431 CONVERT_TO_UTF8_FREE(labeltext
);
3434 /* Remove any old labels. */
3435 while (gtk_notebook_get_nth_page(GTK_NOTEBOOK(gui
.tabline
), nr
) != NULL
)
3436 gtk_notebook_remove_page(GTK_NOTEBOOK(gui
.tabline
), nr
);
3438 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui
.tabline
)) != curtabidx
)
3439 gtk_notebook_set_page(GTK_NOTEBOOK(gui
.tabline
), curtabidx
);
3441 /* Make sure everything is in place before drawing text. */
3444 ignore_tabline_evt
= FALSE
;
3448 * Set the current tab to "nr". First tab is 1.
3451 gui_mch_set_curtab(nr
)
3454 if (gui
.tabline
== NULL
)
3457 ignore_tabline_evt
= TRUE
;
3458 if (gtk_notebook_current_page(GTK_NOTEBOOK(gui
.tabline
)) != nr
- 1)
3459 gtk_notebook_set_page(GTK_NOTEBOOK(gui
.tabline
), nr
- 1);
3460 ignore_tabline_evt
= FALSE
;
3463 #endif /* FEAT_GUI_TABLINE */
3466 * Initialize the GUI. Create all the windows, set up all the callbacks etc.
3467 * Returns OK for success, FAIL when the GUI can't be started.
3474 #ifdef FEAT_GUI_GNOME
3475 /* Initialize the GNOME libraries. gnome_program_init()/gnome_init()
3476 * exits on failure, but that's a non-issue because we already called
3477 * gtk_init_check() in gui_mch_init_check(). */
3480 gnome_program_init(VIMPACKAGE
, VIM_VERSION_SHORT
,
3481 LIBGNOMEUI_MODULE
, gui_argc
, gui_argv
, NULL
);
3483 gnome_init(VIMPACKAGE
, VIM_VERSION_SHORT
, gui_argc
, gui_argv
);
3490 # if GLIB_CHECK_VERSION(2,1,3)
3491 /* Set the human-readable application name */
3492 g_set_application_name("Vim");
3495 * Force UTF-8 output no matter what the value of 'encoding' is.
3496 * did_set_string_option() in option.c prohibits changing 'termencoding'
3497 * to something else than UTF-8 if the GUI is in use.
3499 set_option_value((char_u
*)"termencoding", 0L, (char_u
*)"utf-8", 0);
3501 # ifdef FEAT_TOOLBAR
3502 gui_gtk_register_stock_icons();
3504 /* FIXME: Need to install the classic icons and a gtkrc.classic file.
3505 * The hard part is deciding install locations and the Makefile magic. */
3507 gtk_rc_parse("gtkrc");
3511 /* Initialize values */
3512 gui
.border_width
= 2;
3513 gui
.scrollbar_width
= SB_DEFAULT_WIDTH
;
3514 gui
.scrollbar_height
= SB_DEFAULT_WIDTH
;
3515 /* LINTED: avoid warning: conversion to 'unsigned long' */
3516 gui
.fgcolor
= g_new0(GdkColor
, 1);
3517 /* LINTED: avoid warning: conversion to 'unsigned long' */
3518 gui
.bgcolor
= g_new0(GdkColor
, 1);
3519 /* LINTED: avoid warning: conversion to 'unsigned long' */
3520 gui
.spcolor
= g_new0(GdkColor
, 1);
3522 /* Initialise atoms */
3524 html_atom
= gdk_atom_intern("text/html", FALSE
);
3525 utf8_string_atom
= gdk_atom_intern("UTF8_STRING", FALSE
);
3528 compound_text_atom
= gdk_atom_intern("COMPOUND_TEXT", FALSE
);
3529 text_atom
= gdk_atom_intern("TEXT", FALSE
);
3532 /* Set default foreground and background colors. */
3533 gui
.norm_pixel
= gui
.def_norm_pixel
;
3534 gui
.back_pixel
= gui
.def_back_pixel
;
3536 if (gtk_socket_id
!= 0)
3540 /* Use GtkSocket from another app. */
3541 #ifdef HAVE_GTK_MULTIHEAD
3542 plug
= gtk_plug_new_for_display(gdk_display_get_default(),
3545 plug
= gtk_plug_new(gtk_socket_id
);
3547 if (plug
!= NULL
&& GTK_PLUG(plug
)->socket_window
!= NULL
)
3553 g_warning("Connection to GTK+ socket (ID %u) failed",
3554 (unsigned int)gtk_socket_id
);
3555 /* Pretend we never wanted it if it failed (get own window) */
3560 if (gtk_socket_id
== 0)
3562 #ifdef FEAT_GUI_GNOME
3565 gui
.mainwin
= gnome_app_new("Vim", NULL
);
3567 /* Use the GNOME save-yourself functionality now. */
3573 gui
.mainwin
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
3576 gtk_widget_set_name(gui
.mainwin
, "vim-main-window");
3579 /* Create the PangoContext used for drawing all text. */
3580 gui
.text_context
= gtk_widget_create_pango_context(gui
.mainwin
);
3581 pango_context_set_base_dir(gui
.text_context
, PANGO_DIRECTION_LTR
);
3585 gtk_window_set_policy(GTK_WINDOW(gui
.mainwin
), TRUE
, TRUE
, TRUE
);
3587 gtk_container_border_width(GTK_CONTAINER(gui
.mainwin
), 0);
3588 gtk_widget_add_events(gui
.mainwin
, GDK_VISIBILITY_NOTIFY_MASK
);
3590 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "delete_event",
3591 GTK_SIGNAL_FUNC(&delete_event_cb
), NULL
);
3593 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "realize",
3594 GTK_SIGNAL_FUNC(&mainwin_realize
), NULL
);
3595 #ifdef HAVE_GTK_MULTIHEAD
3596 g_signal_connect(G_OBJECT(gui
.mainwin
), "screen_changed",
3597 G_CALLBACK(&mainwin_screen_changed_cb
), NULL
);
3600 gui
.accel_group
= gtk_accel_group_new();
3601 gtk_window_add_accel_group(GTK_WINDOW(gui
.mainwin
), gui
.accel_group
);
3603 gui
.accel_group
= gtk_accel_group_get_default();
3606 /* A vertical box holds the menubar, toolbar and main text window. */
3607 vbox
= gtk_vbox_new(FALSE
, 0);
3609 #ifdef FEAT_GUI_GNOME
3612 # if defined(HAVE_GTK2) && defined(FEAT_MENU)
3613 /* automagically restore menubar/toolbar placement */
3614 gnome_app_enable_layout_config(GNOME_APP(gui
.mainwin
), TRUE
);
3616 gnome_app_set_contents(GNOME_APP(gui
.mainwin
), vbox
);
3621 gtk_container_add(GTK_CONTAINER(gui
.mainwin
), vbox
);
3622 gtk_widget_show(vbox
);
3627 * Create the menubar and handle
3629 gui
.menubar
= gtk_menu_bar_new();
3630 gtk_widget_set_name(gui
.menubar
, "vim-menubar");
3633 /* Avoid that GTK takes <F10> away from us. */
3635 GtkSettings
*gtk_settings
;
3637 gtk_settings
= gtk_settings_get_for_screen(gdk_screen_get_default());
3638 g_object_set(gtk_settings
, "gtk-menu-bar-accel", NULL
, NULL
);
3643 # ifdef FEAT_GUI_GNOME
3647 BonoboDockItem
*dockitem
;
3649 gnome_app_set_menus(GNOME_APP(gui
.mainwin
), GTK_MENU_BAR(gui
.menubar
));
3650 dockitem
= gnome_app_get_dock_item_by_name(GNOME_APP(gui
.mainwin
),
3651 GNOME_APP_MENUBAR_NAME
);
3652 /* We don't want the menu to float. */
3653 bonobo_dock_item_set_behavior(dockitem
,
3654 bonobo_dock_item_get_behavior(dockitem
)
3655 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING
);
3656 gui
.menubar_h
= GTK_WIDGET(dockitem
);
3658 gui
.menubar_h
= gnome_dock_item_new("VimMainMenu",
3659 GNOME_DOCK_ITEM_BEH_EXCLUSIVE
|
3660 GNOME_DOCK_ITEM_BEH_NEVER_VERTICAL
);
3661 gtk_container_add(GTK_CONTAINER(gui
.menubar_h
), gui
.menubar
);
3663 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui
.mainwin
)->dock
),
3664 GNOME_DOCK_ITEM(gui
.menubar_h
),
3665 GNOME_DOCK_TOP
, /* placement */
3667 0, /* band_position */
3670 gtk_widget_show(gui
.menubar
);
3674 # endif /* FEAT_GUI_GNOME */
3676 /* Always show the menubar, otherwise <F10> doesn't work. It may be
3677 * disabled in gui_init() later. */
3678 gtk_widget_show(gui
.menubar
);
3679 gtk_box_pack_start(GTK_BOX(vbox
), gui
.menubar
, FALSE
, FALSE
, 0);
3681 #endif /* FEAT_MENU */
3685 * Create the toolbar and handle
3688 /* some aesthetics on the toolbar */
3689 gtk_rc_parse_string(
3690 "style \"vim-toolbar-style\" {\n"
3691 " GtkToolbar::button_relief = GTK_RELIEF_NONE\n"
3693 "widget \"*.vim-toolbar\" style \"vim-toolbar-style\"\n");
3694 gui
.toolbar
= gtk_toolbar_new();
3695 gtk_widget_set_name(gui
.toolbar
, "vim-toolbar");
3697 gui
.toolbar
= gtk_toolbar_new(GTK_ORIENTATION_HORIZONTAL
,
3699 gtk_toolbar_set_button_relief(GTK_TOOLBAR(gui
.toolbar
), GTK_RELIEF_NONE
);
3701 set_toolbar_style(GTK_TOOLBAR(gui
.toolbar
));
3703 # ifdef FEAT_GUI_GNOME
3707 BonoboDockItem
*dockitem
;
3709 gnome_app_set_toolbar(GNOME_APP(gui
.mainwin
), GTK_TOOLBAR(gui
.toolbar
));
3710 dockitem
= gnome_app_get_dock_item_by_name(GNOME_APP(gui
.mainwin
),
3711 GNOME_APP_TOOLBAR_NAME
);
3712 gui
.toolbar_h
= GTK_WIDGET(dockitem
);
3713 /* When the toolbar is floating it gets stuck. So long as that isn't
3714 * fixed let's disallow floating. */
3715 bonobo_dock_item_set_behavior(dockitem
,
3716 bonobo_dock_item_get_behavior(dockitem
)
3717 | BONOBO_DOCK_ITEM_BEH_NEVER_FLOATING
);
3718 gtk_container_set_border_width(GTK_CONTAINER(gui
.toolbar
), 0);
3720 GtkWidget
*dockitem
;
3722 dockitem
= gnome_dock_item_new("VimToolBar",
3723 GNOME_DOCK_ITEM_BEH_EXCLUSIVE
);
3724 gtk_container_add(GTK_CONTAINER(dockitem
), GTK_WIDGET(gui
.toolbar
));
3725 gui
.toolbar_h
= dockitem
;
3727 gnome_dock_add_item(GNOME_DOCK(GNOME_APP(gui
.mainwin
)->dock
),
3728 GNOME_DOCK_ITEM(dockitem
),
3729 GNOME_DOCK_TOP
, /* placement */
3731 1, /* band_position */
3734 gtk_container_border_width(GTK_CONTAINER(gui
.toolbar
), 2);
3735 gtk_widget_show(gui
.toolbar
);
3739 # endif /* FEAT_GUI_GNOME */
3742 gtk_container_border_width(GTK_CONTAINER(gui
.toolbar
), 1);
3744 if (vim_strchr(p_go
, GO_TOOLBAR
) != NULL
3745 && (toolbar_flags
& (TOOLBAR_TEXT
| TOOLBAR_ICONS
)))
3746 gtk_widget_show(gui
.toolbar
);
3747 gtk_box_pack_start(GTK_BOX(vbox
), gui
.toolbar
, FALSE
, FALSE
, 0);
3749 #endif /* FEAT_TOOLBAR */
3751 #ifdef FEAT_GUI_TABLINE
3753 * Use a Notebook for the tab pages labels. The labels are hidden by
3756 gui
.tabline
= gtk_notebook_new();
3757 gtk_widget_show(gui
.tabline
);
3758 gtk_box_pack_start(GTK_BOX(vbox
), gui
.tabline
, FALSE
, FALSE
, 0);
3759 gtk_notebook_set_show_border(GTK_NOTEBOOK(gui
.tabline
), FALSE
);
3760 gtk_notebook_set_show_tabs(GTK_NOTEBOOK(gui
.tabline
), FALSE
);
3761 gtk_notebook_set_scrollable(GTK_NOTEBOOK(gui
.tabline
), TRUE
);
3762 gtk_notebook_set_tab_border(GTK_NOTEBOOK(gui
.tabline
), FALSE
);
3764 tabline_tooltip
= gtk_tooltips_new();
3765 gtk_tooltips_enable(GTK_TOOLTIPS(tabline_tooltip
));
3768 GtkWidget
*page
, *label
, *event_box
;
3770 /* Add the first tab. */
3771 page
= gtk_vbox_new(FALSE
, 0);
3772 gtk_widget_show(page
);
3773 gtk_container_add(GTK_CONTAINER(gui
.tabline
), page
);
3774 label
= gtk_label_new("-Empty-");
3775 gtk_widget_show(label
);
3776 event_box
= gtk_event_box_new();
3777 gtk_widget_show(event_box
);
3778 gtk_object_set_user_data(GTK_OBJECT(event_box
), (gpointer
)1L);
3779 gtk_misc_set_padding(GTK_MISC(label
), 2, 2);
3780 gtk_container_add(GTK_CONTAINER(event_box
), label
);
3781 gtk_notebook_set_tab_label(GTK_NOTEBOOK(gui
.tabline
), page
, event_box
);
3784 gtk_signal_connect(GTK_OBJECT(gui
.tabline
), "switch_page",
3785 GTK_SIGNAL_FUNC(on_select_tab
), NULL
);
3787 /* Create a popup menu for the tab line and connect it. */
3788 tabline_menu
= create_tabline_menu();
3789 gtk_signal_connect_object(GTK_OBJECT(gui
.tabline
), "button_press_event",
3790 GTK_SIGNAL_FUNC(on_tabline_menu
), GTK_OBJECT(tabline_menu
));
3793 gui
.formwin
= gtk_form_new();
3794 gtk_container_border_width(GTK_CONTAINER(gui
.formwin
), 0);
3795 gtk_widget_set_events(gui
.formwin
, GDK_EXPOSURE_MASK
);
3797 gui
.drawarea
= gtk_drawing_area_new();
3799 /* Determine which events we will filter. */
3800 gtk_widget_set_events(gui
.drawarea
,
3802 GDK_ENTER_NOTIFY_MASK
|
3803 GDK_LEAVE_NOTIFY_MASK
|
3804 GDK_BUTTON_PRESS_MASK
|
3805 GDK_BUTTON_RELEASE_MASK
|
3809 GDK_KEY_PRESS_MASK
|
3810 GDK_KEY_RELEASE_MASK
|
3811 GDK_POINTER_MOTION_MASK
|
3812 GDK_POINTER_MOTION_HINT_MASK
);
3814 gtk_widget_show(gui
.drawarea
);
3815 gtk_form_put(GTK_FORM(gui
.formwin
), gui
.drawarea
, 0, 0);
3816 gtk_widget_show(gui
.formwin
);
3817 gtk_box_pack_start(GTK_BOX(vbox
), gui
.formwin
, TRUE
, TRUE
, 0);
3819 /* For GtkSockets, key-presses must go to the focus widget (drawarea)
3820 * and not the window. */
3821 gtk_signal_connect((gtk_socket_id
== 0) ? GTK_OBJECT(gui
.mainwin
)
3822 : GTK_OBJECT(gui
.drawarea
),
3824 GTK_SIGNAL_FUNC(key_press_event
), NULL
);
3825 #if defined(FEAT_XIM) && defined(HAVE_GTK2)
3826 /* Also forward key release events for the benefit of GTK+ 2 input
3827 * modules. Try CTRL-SHIFT-xdigits to enter a Unicode code point. */
3828 g_signal_connect((gtk_socket_id
== 0) ? G_OBJECT(gui
.mainwin
)
3829 : G_OBJECT(gui
.drawarea
),
3830 "key_release_event",
3831 G_CALLBACK(&key_release_event
), NULL
);
3833 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "realize",
3834 GTK_SIGNAL_FUNC(drawarea_realize_cb
), NULL
);
3835 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "unrealize",
3836 GTK_SIGNAL_FUNC(drawarea_unrealize_cb
), NULL
);
3838 gtk_signal_connect_after(GTK_OBJECT(gui
.drawarea
), "style_set",
3839 GTK_SIGNAL_FUNC(&drawarea_style_set_cb
), NULL
);
3841 gui
.visibility
= GDK_VISIBILITY_UNOBSCURED
;
3843 #if !(defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION))
3844 wm_protocols_atom
= gdk_atom_intern("WM_PROTOCOLS", FALSE
);
3845 save_yourself_atom
= gdk_atom_intern("WM_SAVE_YOURSELF", FALSE
);
3848 if (gtk_socket_id
!= 0)
3849 /* make sure keyboard input can go to the drawarea */
3850 GTK_WIDGET_SET_FLAGS(gui
.drawarea
, GTK_CAN_FOCUS
);
3853 * Set clipboard specific atoms
3855 vim_atom
= gdk_atom_intern(VIM_ATOM_NAME
, FALSE
);
3857 vimenc_atom
= gdk_atom_intern(VIMENC_ATOM_NAME
, FALSE
);
3859 clip_star
.gtk_sel_atom
= GDK_SELECTION_PRIMARY
;
3860 clip_plus
.gtk_sel_atom
= gdk_atom_intern("CLIPBOARD", FALSE
);
3863 * Start out by adding the configured border width into the border offset.
3865 gui
.border_offset
= gui
.border_width
;
3867 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "visibility_notify_event",
3868 GTK_SIGNAL_FUNC(visibility_event
), NULL
);
3869 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "expose_event",
3870 GTK_SIGNAL_FUNC(expose_event
), NULL
);
3873 * Only install these enter/leave callbacks when 'p' in 'guioptions'.
3874 * Only needed for some window managers.
3876 if (vim_strchr(p_go
, GO_POINTER
) != NULL
)
3878 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "leave_notify_event",
3879 GTK_SIGNAL_FUNC(leave_notify_event
), NULL
);
3880 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "enter_notify_event",
3881 GTK_SIGNAL_FUNC(enter_notify_event
), NULL
);
3884 /* Real windows can get focus ... GtkPlug, being a mere container can't,
3885 * only its widgets. Arguably, this could be common code and we not use
3886 * the window focus at all, but let's be safe.
3888 if (gtk_socket_id
== 0)
3890 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "focus_out_event",
3891 GTK_SIGNAL_FUNC(focus_out_event
), NULL
);
3892 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "focus_in_event",
3893 GTK_SIGNAL_FUNC(focus_in_event
), NULL
);
3897 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "focus_out_event",
3898 GTK_SIGNAL_FUNC(focus_out_event
), NULL
);
3899 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "focus_in_event",
3900 GTK_SIGNAL_FUNC(focus_in_event
), NULL
);
3901 #ifdef FEAT_GUI_TABLINE
3902 gtk_signal_connect(GTK_OBJECT(gui
.tabline
), "focus_out_event",
3903 GTK_SIGNAL_FUNC(focus_out_event
), NULL
);
3904 gtk_signal_connect(GTK_OBJECT(gui
.tabline
), "focus_in_event",
3905 GTK_SIGNAL_FUNC(focus_in_event
), NULL
);
3906 #endif /* FEAT_GUI_TABLINE */
3909 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "motion_notify_event",
3910 GTK_SIGNAL_FUNC(motion_notify_event
), NULL
);
3911 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "button_press_event",
3912 GTK_SIGNAL_FUNC(button_press_event
), NULL
);
3913 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "button_release_event",
3914 GTK_SIGNAL_FUNC(button_release_event
), NULL
);
3916 g_signal_connect(G_OBJECT(gui
.drawarea
), "scroll_event",
3917 G_CALLBACK(&scroll_event
), NULL
);
3921 * Add selection handler functions.
3923 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "selection_clear_event",
3924 GTK_SIGNAL_FUNC(selection_clear_event
), NULL
);
3925 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "selection_received",
3926 GTK_SIGNAL_FUNC(selection_received_cb
), NULL
);
3929 * Add selection targets for PRIMARY and CLIPBOARD selections.
3931 gtk_selection_add_targets(gui
.drawarea
,
3932 (GdkAtom
)GDK_SELECTION_PRIMARY
,
3933 selection_targets
, N_SELECTION_TARGETS
);
3934 gtk_selection_add_targets(gui
.drawarea
,
3935 (GdkAtom
)clip_plus
.gtk_sel_atom
,
3936 selection_targets
, N_SELECTION_TARGETS
);
3938 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "selection_get",
3939 GTK_SIGNAL_FUNC(selection_get_cb
), NULL
);
3941 /* Pretend we don't have input focus, we will get an event if we do. */
3942 gui
.in_focus
= FALSE
;
3947 #if (defined(FEAT_GUI_GNOME) && defined(FEAT_SESSION)) || defined(PROTO)
3949 * This is called from gui_start() after a fork() has been done.
3950 * We have to tell the session manager our new PID.
3953 gui_mch_forked(void)
3957 GnomeClient
*client
;
3959 client
= gnome_master_client();
3962 gnome_client_set_process_id(client
, getpid());
3965 #endif /* FEAT_GUI_GNOME && FEAT_SESSION */
3968 * Called when the foreground or background color has been changed.
3969 * This used to change the graphics contexts directly but we are
3970 * currently manipulating them where desired.
3973 gui_mch_new_colors(void)
3975 if (gui
.drawarea
!= NULL
&& gui
.drawarea
->window
!= NULL
)
3977 GdkColor color
= { 0, 0, 0, 0 };
3979 color
.pixel
= gui
.back_pixel
;
3980 gdk_window_set_background(gui
.drawarea
->window
, &color
);
3985 * This signal informs us about the need to rearrange our sub-widgets.
3988 form_configure_event(GtkWidget
*widget UNUSED
,
3989 GdkEventConfigure
*event
,
3990 gpointer data UNUSED
)
3992 int usable_height
= event
->height
;
3994 /* When in a GtkPlug, we can't guarantee valid heights (as a round
3995 * no. of char-heights), so we have to manually sanitise them.
3996 * Widths seem to sort themselves out, don't ask me why.
3998 if (gtk_socket_id
!= 0)
3999 usable_height
-= (gui
.char_height
- (gui
.char_height
/2)); /* sic. */
4001 gtk_form_freeze(GTK_FORM(gui
.formwin
));
4002 gui_resize_shell(event
->width
, usable_height
);
4003 gtk_form_thaw(GTK_FORM(gui
.formwin
));
4009 * Function called when window already closed.
4010 * We can't do much more here than to trying to preserve what had been done,
4011 * since the window is already inevitably going away.
4014 mainwin_destroy_cb(GtkObject
*object UNUSED
, gpointer data UNUSED
)
4016 /* Don't write messages to the GUI anymore */
4017 full_screen
= FALSE
;
4020 gui
.drawarea
= NULL
;
4022 if (!exiting
) /* only do anything if the destroy was unexpected */
4025 (char_u
*)_("Vim: Main window unexpectedly destroyed\n"),
4033 * Bit of a hack to ensure we start GtkPlug windows with the correct window
4034 * hints (and thus the required size from -geom), but that after that we
4035 * put the hints back to normal (the actual minimum size) so we may
4036 * subsequently be resized smaller. GtkSocket (the parent end) uses the
4037 * plug's window 'min hints to set *it's* minimum size, but that's also the
4038 * only way we have of making ourselves bigger (by set lines/columns).
4039 * Thus set hints at start-up to ensure correct init. size, then a
4040 * second after the final attempt to reset the real minimum hinst (done by
4041 * scrollbar init.), actually do the standard hinst and stop the timer.
4042 * We'll not let the default hints be set while this timer's active.
4045 check_startup_plug_hints(gpointer data UNUSED
)
4047 if (init_window_hints_state
== 1)
4049 /* Safe to use normal hints now */
4050 init_window_hints_state
= 0;
4051 update_window_manager_hints(0, 0);
4052 return FALSE
; /* stop timer */
4055 /* Keep on trying */
4056 init_window_hints_state
= 1;
4062 * Open the GUI window which was created by a call to gui_mch_init().
4067 guicolor_T fg_pixel
= INVALCOLOR
;
4068 guicolor_T bg_pixel
= INVALCOLOR
;
4074 * Allow setting a window role on the command line, or invent one
4075 * if none was specified. This is mainly useful for GNOME session
4076 * support; allowing the WM to restore window placement.
4078 if (role_argument
!= NULL
)
4080 gtk_window_set_role(GTK_WINDOW(gui
.mainwin
), role_argument
);
4086 /* Invent a unique-enough ID string for the role */
4087 role
= g_strdup_printf("vim-%u-%u-%u",
4088 (unsigned)mch_get_pid(),
4089 (unsigned)g_random_int(),
4090 (unsigned)time(NULL
));
4092 gtk_window_set_role(GTK_WINDOW(gui
.mainwin
), role
);
4097 if (gui_win_x
!= -1 && gui_win_y
!= -1)
4099 gtk_window_move(GTK_WINDOW(gui
.mainwin
), gui_win_x
, gui_win_y
);
4101 gtk_widget_set_uposition(gui
.mainwin
, gui_win_x
, gui_win_y
);
4104 /* Determine user specified geometry, if present. */
4105 if (gui
.geom
!= NULL
)
4112 mask
= XParseGeometry((char *)gui
.geom
, &x
, &y
, &w
, &h
);
4114 if (mask
& WidthValue
)
4116 if (mask
& HeightValue
)
4118 if (p_window
> (long)h
- 1 || !option_was_set((char_u
*)"window"))
4123 pixel_width
= (guint
)(gui_get_base_width() + Columns
* gui
.char_width
);
4124 pixel_height
= (guint
)(gui_get_base_height() + Rows
* gui
.char_height
);
4127 pixel_width
+= get_menu_tool_width();
4128 pixel_height
+= get_menu_tool_height();
4131 if (mask
& (XValue
| YValue
))
4134 gui_mch_get_screen_dimensions(&ww
, &hh
);
4135 hh
+= p_ghr
+ get_menu_tool_height();
4136 ww
+= get_menu_tool_width();
4137 if (mask
& XNegative
)
4138 x
+= ww
- pixel_width
;
4139 if (mask
& YNegative
)
4140 y
+= hh
- pixel_height
;
4142 gtk_window_move(GTK_WINDOW(gui
.mainwin
), x
, y
);
4144 gtk_widget_set_uposition(gui
.mainwin
, x
, y
);
4150 /* From now until everyone's stopped trying to set the window hints
4151 * to their correct minimum values, stop them being set as we need
4152 * them to remain at our required size for the parent GtkSocket to
4153 * give us the right initial size.
4155 if (gtk_socket_id
!= 0 && (mask
& WidthValue
|| mask
& HeightValue
))
4157 update_window_manager_hints(pixel_width
, pixel_height
);
4158 init_window_hints_state
= 1;
4159 g_timeout_add(1000, check_startup_plug_hints
, NULL
);
4163 pixel_width
= (guint
)(gui_get_base_width() + Columns
* gui
.char_width
);
4164 pixel_height
= (guint
)(gui_get_base_height() + Rows
* gui
.char_height
);
4166 /* For GTK2 changing the size of the form widget doesn't cause window
4168 if (gtk_socket_id
== 0)
4169 gtk_window_resize(GTK_WINDOW(gui
.mainwin
), pixel_width
, pixel_height
);
4171 gtk_form_set_size(GTK_FORM(gui
.formwin
), pixel_width
, pixel_height
);
4173 update_window_manager_hints(0, 0);
4175 if (foreground_argument
!= NULL
)
4176 fg_pixel
= gui_get_color((char_u
*)foreground_argument
);
4177 if (fg_pixel
== INVALCOLOR
)
4178 fg_pixel
= gui_get_color((char_u
*)"Black");
4180 if (background_argument
!= NULL
)
4181 bg_pixel
= gui_get_color((char_u
*)background_argument
);
4182 if (bg_pixel
== INVALCOLOR
)
4183 bg_pixel
= gui_get_color((char_u
*)"White");
4185 if (found_reverse_arg
)
4187 gui
.def_norm_pixel
= bg_pixel
;
4188 gui
.def_back_pixel
= fg_pixel
;
4192 gui
.def_norm_pixel
= fg_pixel
;
4193 gui
.def_back_pixel
= bg_pixel
;
4196 /* Get the colors from the "Normal" and "Menu" group (set in syntax.c or
4197 * in a vimrc file) */
4198 set_normal_colors();
4200 /* Check that none of the colors are the same as the background color */
4203 /* Get the colors for the highlight groups (gui_check_colors() might have
4205 highlight_gui_started(); /* re-init colors and fonts */
4207 gtk_signal_connect(GTK_OBJECT(gui
.mainwin
), "destroy",
4208 GTK_SIGNAL_FUNC(mainwin_destroy_cb
), NULL
);
4210 #ifdef FEAT_HANGULIN
4211 hangul_keyboard_set();
4215 * Notify the fixed area about the need to resize the contents of the
4216 * gui.formwin, which we use for random positioning of the included
4219 * We connect this signal deferred finally after anything is in place,
4220 * since this is intended to handle resizements coming from the window
4221 * manager upon us and should not interfere with what VIM is requesting
4224 gtk_signal_connect(GTK_OBJECT(gui
.formwin
), "configure_event",
4225 GTK_SIGNAL_FUNC(form_configure_event
), NULL
);
4229 * Set up for receiving DND items.
4231 gtk_drag_dest_set(gui
.drawarea
,
4232 GTK_DEST_DEFAULT_ALL
,
4233 dnd_targets
, N_DND_TARGETS
,
4236 gtk_signal_connect(GTK_OBJECT(gui
.drawarea
), "drag_data_received",
4237 GTK_SIGNAL_FUNC(drag_data_received_cb
), NULL
);
4241 /* With GTK+ 2, we need to iconify the window before calling show()
4242 * to avoid mapping the window for a short time. This is just as one
4243 * would expect it to work, but it's different in GTK+ 1. The funny
4244 * thing is that iconifying after show() _does_ work with GTK+ 1.
4245 * (BTW doing this in the "realize" handler makes no difference.) */
4246 if (found_iconic_arg
&& gtk_socket_id
== 0)
4251 #if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4252 unsigned long menu_handler
= 0;
4253 # ifdef FEAT_TOOLBAR
4254 unsigned long tool_handler
= 0;
4257 * Urgh hackish :/ For some reason BonoboDockLayout always forces a
4258 * show when restoring the saved layout configuration. We can't just
4259 * hide the widgets again after gtk_widget_show(gui.mainwin) since it's
4260 * a toplevel window and thus will be realized immediately. Instead,
4261 * connect signal handlers to hide the widgets just after they've been
4262 * marked visible, but before the main window is realized.
4264 if (using_gnome
&& vim_strchr(p_go
, GO_MENUS
) == NULL
)
4265 menu_handler
= g_signal_connect_after(gui
.menubar_h
, "show",
4266 G_CALLBACK(>k_widget_hide
),
4268 # ifdef FEAT_TOOLBAR
4269 if (using_gnome
&& vim_strchr(p_go
, GO_TOOLBAR
) == NULL
4270 && (toolbar_flags
& (TOOLBAR_TEXT
| TOOLBAR_ICONS
)))
4271 tool_handler
= g_signal_connect_after(gui
.toolbar_h
, "show",
4272 G_CALLBACK(>k_widget_hide
),
4276 gtk_widget_show(gui
.mainwin
);
4278 #if defined(FEAT_GUI_GNOME) && defined(HAVE_GTK2) && defined(FEAT_MENU)
4279 if (menu_handler
!= 0)
4280 g_signal_handler_disconnect(gui
.menubar_h
, menu_handler
);
4281 # ifdef FEAT_TOOLBAR
4282 if (tool_handler
!= 0)
4283 g_signal_handler_disconnect(gui
.toolbar_h
, tool_handler
);
4289 /* With GTK+ 1, we need to iconify the window after calling show().
4290 * See the comment above for details. */
4291 if (found_iconic_arg
&& gtk_socket_id
== 0)
4300 gui_mch_exit(int rc UNUSED
)
4302 if (gui
.mainwin
!= NULL
)
4303 gtk_widget_destroy(gui
.mainwin
);
4305 if (gtk_main_level() > 0)
4310 * Get the position of the top left corner of the window.
4313 gui_mch_get_winpos(int *x
, int *y
)
4316 gtk_window_get_position(GTK_WINDOW(gui
.mainwin
), x
, y
);
4318 /* For some people this must be gdk_window_get_origin() for a correct
4319 * result. Where is the documentation! */
4320 gdk_window_get_root_origin(gui
.mainwin
->window
, x
, y
);
4326 * Set the position of the top left corner of the window to the given
4330 gui_mch_set_winpos(int x
, int y
)
4333 gtk_window_move(GTK_WINDOW(gui
.mainwin
), x
, y
);
4335 gdk_window_move(gui
.mainwin
->window
, x
, y
);
4341 static int resize_idle_installed
= FALSE
;
4343 * Idle handler to force resize. Used by gui_mch_set_shellsize() to ensure
4344 * the shell size doesn't exceed the window size, i.e. if the window manager
4345 * ignored our size request. Usually this happens if the window is maximized.
4347 * FIXME: It'd be nice if we could find a little more orthodox solution.
4348 * See also the remark below in gui_mch_set_shellsize().
4350 * DISABLED: When doing ":set lines+=1" this function would first invoke
4351 * gui_resize_shell() with the old size, then the normal callback would
4352 * report the new size through form_configure_event(). That caused the window
4353 * layout to be messed up.
4356 force_shell_resize_idle(gpointer data
)
4358 if (gui
.mainwin
!= NULL
4359 && GTK_WIDGET_REALIZED(gui
.mainwin
)
4360 && GTK_WIDGET_VISIBLE(gui
.mainwin
))
4365 gtk_window_get_size(GTK_WINDOW(gui
.mainwin
), &width
, &height
);
4367 width
-= get_menu_tool_width();
4368 height
-= get_menu_tool_height();
4370 gui_resize_shell(width
, height
);
4373 resize_idle_installed
= FALSE
;
4374 return FALSE
; /* don't call me again */
4377 #endif /* HAVE_GTK2 */
4379 #if defined(HAVE_GTK2) || defined(PROTO)
4381 * Return TRUE if the main window is maximized.
4386 return (gui
.mainwin
!= NULL
&& gui
.mainwin
->window
!= NULL
4387 && (gdk_window_get_state(gui
.mainwin
->window
)
4388 & GDK_WINDOW_STATE_MAXIMIZED
));
4392 * Unmaximize the main window
4395 gui_mch_unmaximize()
4397 if (gui
.mainwin
!= NULL
)
4398 gtk_window_unmaximize(GTK_WINDOW(gui
.mainwin
));
4403 * Set the windows size.
4406 gui_mch_set_shellsize(int width
, int height
,
4407 int min_width UNUSED
, int min_height UNUSED
,
4408 int base_width UNUSED
, int base_height UNUSED
,
4409 int direction UNUSED
)
4412 /* Hack: When the form already is at the desired size, the window might
4413 * have been resized with the mouse. Force a resize by setting a
4414 * different size first. */
4415 if (GTK_FORM(gui
.formwin
)->width
== width
4416 && GTK_FORM(gui
.formwin
)->height
== height
)
4418 gtk_form_set_size(GTK_FORM(gui
.formwin
), width
+ 1, height
+ 1);
4421 gtk_form_set_size(GTK_FORM(gui
.formwin
), width
, height
);
4424 /* give GTK+ a chance to put all widget's into place */
4428 /* this will cause the proper resizement to happen too */
4429 update_window_manager_hints(0, 0);
4431 #else /* HAVE_GTK2 */
4432 /* this will cause the proper resizement to happen too */
4433 if (gtk_socket_id
== 0)
4434 update_window_manager_hints(0, 0);
4436 /* With GTK+ 2, changing the size of the form widget doesn't resize
4437 * the window. So let's do it the other way around and resize the
4438 * main window instead. */
4439 width
+= get_menu_tool_width();
4440 height
+= get_menu_tool_height();
4442 if (gtk_socket_id
== 0)
4443 gtk_window_resize(GTK_WINDOW(gui
.mainwin
), width
, height
);
4445 update_window_manager_hints(width
, height
);
4448 if (!resize_idle_installed
)
4450 g_idle_add_full(GDK_PRIORITY_EVENTS
+ 10,
4451 &force_shell_resize_idle
, NULL
, NULL
);
4452 resize_idle_installed
= TRUE
;
4456 * Wait until all events are processed to prevent a crash because the
4457 * real size of the drawing area doesn't reflect Vim's internal ideas.
4459 * This is a bit of a hack, since Vim is a terminal application with a GUI
4460 * on top, while the GUI expects to be the boss.
4468 * The screen size is used to make sure the initial window doesn't get bigger
4469 * than the screen. This subtracts some room for menubar, toolbar and window
4473 gui_mch_get_screen_dimensions(int *screen_w
, int *screen_h
)
4475 #ifdef HAVE_GTK_MULTIHEAD
4478 if (gui
.mainwin
!= NULL
&& gtk_widget_has_screen(gui
.mainwin
))
4479 screen
= gtk_widget_get_screen(gui
.mainwin
);
4481 screen
= gdk_screen_get_default();
4483 *screen_w
= gdk_screen_get_width(screen
);
4484 *screen_h
= gdk_screen_get_height(screen
) - p_ghr
;
4486 *screen_w
= gdk_screen_width();
4487 /* Subtract 'guiheadroom' from the height to allow some room for the
4488 * window manager (task list and window title bar). */
4489 *screen_h
= gdk_screen_height() - p_ghr
;
4493 * FIXME: dirty trick: Because the gui_get_base_height() doesn't include
4494 * the toolbar and menubar for GTK, we subtract them from the screen
4495 * hight, so that the window size can be made to fit on the screen.
4496 * This should be completely changed later.
4498 *screen_w
-= get_menu_tool_width();
4499 *screen_h
-= get_menu_tool_height();
4502 #if defined(FEAT_TITLE) || defined(PROTO)
4504 gui_mch_settitle(char_u
*title
, char_u
*icon UNUSED
)
4507 if (title
!= NULL
&& output_conv
.vc_type
!= CONV_NONE
)
4508 title
= string_convert(&output_conv
, title
, NULL
);
4511 gtk_window_set_title(GTK_WINDOW(gui
.mainwin
), (const char *)title
);
4514 if (output_conv
.vc_type
!= CONV_NONE
)
4518 #endif /* FEAT_TITLE */
4520 #if defined(FEAT_MENU) || defined(PROTO)
4522 gui_mch_enable_menu(int showit
)
4526 # ifdef FEAT_GUI_GNOME
4528 widget
= gui
.menubar_h
;
4531 widget
= gui
.menubar
;
4533 /* Do not disable the menu while starting up, otherwise F10 doesn't work. */
4534 if (!showit
!= !GTK_WIDGET_VISIBLE(widget
) && !gui
.starting
)
4537 gtk_widget_show(widget
);
4539 gtk_widget_hide(widget
);
4541 update_window_manager_hints(0, 0);
4544 #endif /* FEAT_MENU */
4546 #if defined(FEAT_TOOLBAR) || defined(PROTO)
4548 gui_mch_show_toolbar(int showit
)
4552 if (gui
.toolbar
== NULL
)
4555 # ifdef FEAT_GUI_GNOME
4557 widget
= gui
.toolbar_h
;
4560 widget
= gui
.toolbar
;
4563 set_toolbar_style(GTK_TOOLBAR(gui
.toolbar
));
4565 if (!showit
!= !GTK_WIDGET_VISIBLE(widget
))
4568 gtk_widget_show(widget
);
4570 gtk_widget_hide(widget
);
4572 update_window_manager_hints(0, 0);
4575 #endif /* FEAT_TOOLBAR */
4579 * Get a font structure for highlighting.
4580 * "cbdata" is a pointer to the global gui structure.
4583 font_sel_ok(GtkWidget
*wgt
, gpointer cbdata
)
4585 gui_T
*vw
= (gui_T
*)cbdata
;
4586 GtkFontSelectionDialog
*fs
= (GtkFontSelectionDialog
*)vw
->fontdlg
;
4589 g_free(vw
->fontname
);
4591 vw
->fontname
= (char_u
*)gtk_font_selection_dialog_get_font_name(fs
);
4592 gtk_widget_hide(vw
->fontdlg
);
4593 if (gtk_main_level() > 0)
4598 font_sel_cancel(GtkWidget
*wgt
, gpointer cbdata
)
4600 gui_T
*vw
= (gui_T
*)cbdata
;
4602 gtk_widget_hide(vw
->fontdlg
);
4603 if (gtk_main_level() > 0)
4608 font_sel_destroy(GtkWidget
*wgt
, gpointer cbdata
)
4610 gui_T
*vw
= (gui_T
*)cbdata
;
4613 if (gtk_main_level() > 0)
4616 #endif /* !HAVE_GTK2 */
4620 * Check if a given font is a CJK font. This is done in a very crude manner. It
4621 * just see if U+04E00 for zh and ja and U+AC00 for ko are covered in a given
4622 * font. Consequently, this function cannot be used as a general purpose check
4623 * for CJK-ness for which fontconfig APIs should be used. This is only used by
4624 * gui_mch_init_font() to deal with 'CJK fixed width fonts'.
4627 is_cjk_font(PangoFontDescription
*font_desc
)
4629 static const char * const cjk_langs
[] =
4630 {"zh_CN", "zh_TW", "zh_HK", "ja", "ko"};
4636 font
= pango_context_load_font(gui
.text_context
, font_desc
);
4641 for (i
= 0; !is_cjk
&& i
< G_N_ELEMENTS(cjk_langs
); ++i
)
4643 PangoCoverage
*coverage
;
4646 coverage
= pango_font_get_coverage(
4647 font
, pango_language_from_string(cjk_langs
[i
]));
4649 if (coverage
!= NULL
)
4651 uc
= (cjk_langs
[i
][0] == 'k') ? 0xAC00 : 0x4E00;
4652 is_cjk
= (pango_coverage_get(coverage
, uc
) == PANGO_COVERAGE_EXACT
);
4653 pango_coverage_unref(coverage
);
4657 g_object_unref(font
);
4661 #endif /* HAVE_GTK2 */
4664 * Adjust gui.char_height (after 'linespace' was changed).
4667 gui_mch_adjust_charheight(void)
4670 PangoFontMetrics
*metrics
;
4674 metrics
= pango_context_get_metrics(gui
.text_context
, gui
.norm_font
,
4675 pango_context_get_language(gui
.text_context
));
4676 ascent
= pango_font_metrics_get_ascent(metrics
);
4677 descent
= pango_font_metrics_get_descent(metrics
);
4679 pango_font_metrics_unref(metrics
);
4681 gui
.char_height
= (ascent
+ descent
+ PANGO_SCALE
- 1) / PANGO_SCALE
4683 /* LINTED: avoid warning: bitwise operation on signed value */
4684 gui
.char_ascent
= PANGO_PIXELS(ascent
+ p_linespace
* PANGO_SCALE
/ 2);
4686 #else /* !HAVE_GTK2 */
4688 gui
.char_height
= gui
.current_font
->ascent
+ gui
.current_font
->descent
4690 gui
.char_ascent
= gui
.current_font
->ascent
+ p_linespace
/ 2;
4692 #endif /* !HAVE_GTK2 */
4694 /* A not-positive value of char_height may crash Vim. Only happens
4695 * if 'linespace' is negative (which does make sense sometimes). */
4696 gui
.char_ascent
= MAX(gui
.char_ascent
, 0);
4697 gui
.char_height
= MAX(gui
.char_height
, gui
.char_ascent
+ 1);
4702 #if defined(FEAT_XFONTSET) || defined(PROTO)
4704 * Try to load the requested fontset.
4707 gui_mch_get_fontset(char_u
*name
, int report_error
, int fixed_width
)
4711 if (!gui
.in_use
|| name
== NULL
)
4714 font
= gdk_fontset_load((gchar
*)name
);
4719 EMSG2(_(e_fontset
), name
);
4722 /* TODO: check if the font is fixed width. */
4724 /* reference this font as being in use */
4727 return (GuiFontset
)font
;
4729 #endif /* FEAT_XFONTSET */
4733 * Put up a font dialog and return the selected font name in allocated memory.
4734 * "oldval" is the previous value.
4735 * Return NULL when cancelled.
4738 gui_mch_font_dialog(char_u
*oldval
)
4740 char_u
*fontname
= NULL
;
4744 GtkFontSelectionDialog
*fsd
= NULL
;
4746 gui
.fontdlg
= gtk_font_selection_dialog_new(_("Font Selection"));
4747 fsd
= GTK_FONT_SELECTION_DIALOG(gui
.fontdlg
);
4748 gtk_window_set_modal(GTK_WINDOW(gui
.fontdlg
), TRUE
);
4749 gtk_window_set_transient_for(GTK_WINDOW(gui
.fontdlg
),
4750 GTK_WINDOW(gui
.mainwin
));
4751 gtk_signal_connect(GTK_OBJECT(gui
.fontdlg
), "destroy",
4752 GTK_SIGNAL_FUNC(font_sel_destroy
), &gui
);
4753 gtk_signal_connect(GTK_OBJECT(fsd
->ok_button
), "clicked",
4754 GTK_SIGNAL_FUNC(font_sel_ok
), &gui
);
4755 gtk_signal_connect(GTK_OBJECT(fsd
->cancel_button
), "clicked",
4756 GTK_SIGNAL_FUNC(font_sel_cancel
), &gui
);
4759 if (oldval
!= NULL
&& *oldval
!= NUL
)
4760 gtk_font_selection_dialog_set_font_name(
4761 GTK_FONT_SELECTION_DIALOG(gui
.fontdlg
), (char *)oldval
);
4763 gtk_font_selection_dialog_set_font_name(
4764 GTK_FONT_SELECTION_DIALOG(gui
.fontdlg
), DEFAULT_FONT
);
4768 g_free(gui
.fontname
);
4769 gui
.fontname
= NULL
;
4771 gtk_window_position(GTK_WINDOW(gui
.fontdlg
), GTK_WIN_POS_MOUSE
);
4772 gtk_widget_show(gui
.fontdlg
);
4774 static gchar
*spacings
[] = {"c", "m", NULL
};
4776 /* In GTK 1.2.3 this must be after the gtk_widget_show() call,
4777 * otherwise everything is blocked for ten seconds. */
4778 gtk_font_selection_dialog_set_filter(
4779 GTK_FONT_SELECTION_DIALOG(gui
.fontdlg
),
4780 GTK_FONT_FILTER_BASE
,
4781 GTK_FONT_ALL
, NULL
, NULL
,
4782 NULL
, NULL
, spacings
, NULL
);
4785 /* Wait for the font dialog to be closed. */
4786 while (gui
.fontdlg
&& GTK_WIDGET_DRAWABLE(gui
.fontdlg
))
4787 gtk_main_iteration_do(TRUE
);
4789 if (gui
.fontname
!= NULL
)
4791 /* Apparently some font names include a comma, need to escape that,
4792 * because in 'guifont' it separates names. */
4793 fontname
= vim_strsave_escaped(gui
.fontname
, (char_u
*)",");
4794 g_free(gui
.fontname
);
4795 gui
.fontname
= NULL
;
4799 #endif /* !HAVE_GTK2 */
4803 * Put up a font dialog and return the selected font name in allocated memory.
4804 * "oldval" is the previous value. Return NULL when cancelled.
4805 * This should probably go into gui_gtk.c. Hmm.
4807 * The GTK2 font selection dialog has no filtering API. So we could either
4808 * a) implement our own (possibly copying the code from somewhere else) or
4809 * b) just live with it.
4812 gui_mch_font_dialog(char_u
*oldval
)
4816 char_u
*fontname
= NULL
;
4819 dialog
= gtk_font_selection_dialog_new(NULL
);
4821 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(gui
.mainwin
));
4822 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
4824 if (oldval
!= NULL
&& oldval
[0] != NUL
)
4826 if (output_conv
.vc_type
!= CONV_NONE
)
4827 oldname
= string_convert(&output_conv
, oldval
, NULL
);
4831 /* Annoying bug in GTK (or Pango): if the font name does not include a
4832 * size, zero is used. Use default point size ten. */
4833 if (!vim_isdigit(oldname
[STRLEN(oldname
) - 1]))
4835 char_u
*p
= vim_strnsave(oldname
, STRLEN(oldname
) + 3);
4839 STRCPY(p
+ STRLEN(p
), " 10");
4840 if (oldname
!= oldval
)
4846 gtk_font_selection_dialog_set_font_name(
4847 GTK_FONT_SELECTION_DIALOG(dialog
), (const char *)oldname
);
4849 if (oldname
!= oldval
)
4853 gtk_font_selection_dialog_set_font_name(
4854 GTK_FONT_SELECTION_DIALOG(dialog
), DEFAULT_FONT
);
4856 response
= gtk_dialog_run(GTK_DIALOG(dialog
));
4858 if (response
== GTK_RESPONSE_OK
)
4862 name
= gtk_font_selection_dialog_get_font_name(
4863 GTK_FONT_SELECTION_DIALOG(dialog
));
4868 /* Apparently some font names include a comma, need to escape
4869 * that, because in 'guifont' it separates names. */
4870 p
= vim_strsave_escaped((char_u
*)name
, (char_u
*)",");
4872 if (p
!= NULL
&& input_conv
.vc_type
!= CONV_NONE
)
4874 fontname
= string_convert(&input_conv
, p
, NULL
);
4882 if (response
!= GTK_RESPONSE_NONE
)
4883 gtk_widget_destroy(dialog
);
4889 * Some monospace fonts don't support a bold weight, and fall back
4890 * silently to the regular weight. But this is no good since our text
4891 * drawing function can emulate bold by overstriking. So let's try
4892 * to detect whether bold weight is actually available and emulate it
4895 * Note that we don't need to check for italic style since Xft can
4896 * emulate italic on its own, provided you have a proper fontconfig
4897 * setup. We wouldn't be able to emulate it in Vim anyway.
4900 get_styled_font_variants(void)
4902 PangoFontDescription
*bold_font_desc
;
4903 PangoFont
*plain_font
;
4904 PangoFont
*bold_font
;
4906 gui
.font_can_bold
= FALSE
;
4908 plain_font
= pango_context_load_font(gui
.text_context
, gui
.norm_font
);
4910 if (plain_font
== NULL
)
4913 bold_font_desc
= pango_font_description_copy_static(gui
.norm_font
);
4914 pango_font_description_set_weight(bold_font_desc
, PANGO_WEIGHT_BOLD
);
4916 bold_font
= pango_context_load_font(gui
.text_context
, bold_font_desc
);
4918 * The comparison relies on the unique handle nature of a PangoFont*,
4919 * i.e. it's assumed that a different PangoFont* won't refer to the
4920 * same font. Seems to work, and failing here isn't critical anyway.
4922 if (bold_font
!= NULL
)
4924 gui
.font_can_bold
= (bold_font
!= plain_font
);
4925 g_object_unref(bold_font
);
4928 pango_font_description_free(bold_font_desc
);
4929 g_object_unref(plain_font
);
4932 #else /* !HAVE_GTK2 */
4935 * There is only one excuse I can give for the following attempt to manage font
4938 * I HATE THE BRAIN DEAD WAY X11 IS HANDLING FONTS (--mdcki)
4939 * (Me too. --danielk)
4942 get_styled_font_variants(char_u
* font_name
)
4948 GuiFont
*styled_font
[3];
4950 styled_font
[0] = &gui
.bold_font
;
4951 styled_font
[1] = &gui
.ital_font
;
4952 styled_font
[2] = &gui
.boldital_font
;
4954 /* First free whatever was previously there. */
4955 for (i
= 0; i
< 3; ++i
)
4956 if (*styled_font
[i
])
4958 gdk_font_unref(*styled_font
[i
]);
4959 *styled_font
[i
] = NULL
;
4962 if ((sdup
= g_strdup((const char *)font_name
)) == NULL
)
4965 /* split up the whole */
4967 for (tmp
= sdup
; *tmp
!= '\0'; ++tmp
)
4983 GdkFont
*font
= NULL
;
4984 const char *bold_chunk
[3] = { "bold", NULL
, "bold" };
4985 const char *italic_chunk
[3] = { NULL
, "o", "o" };
4987 /* font name was complete */
4988 len
= strlen((const char *)font_name
) + 32;
4990 for (i
= 0; i
< 3; ++i
)
4995 styled_name
= (char *)alloc(len
);
4996 if (styled_name
== NULL
)
5002 *styled_name
= '\0';
5004 for (j
= 0; j
< 14; ++j
)
5006 strcat(styled_name
, "-");
5007 if (j
== 2 && bold_chunk
[i
] != NULL
)
5008 strcat(styled_name
, bold_chunk
[i
]);
5009 else if (j
== 3 && italic_chunk
[i
] != NULL
)
5010 strcat(styled_name
, italic_chunk
[i
]);
5012 strcat(styled_name
, chunk
[j
]);
5015 font
= gui_mch_get_font((char_u
*)styled_name
, FALSE
);
5017 *styled_font
[i
] = font
;
5019 vim_free(styled_name
);
5025 #endif /* !HAVE_GTK2 */
5028 static PangoEngineShape
*default_shape_engine
= NULL
;
5031 * Create a map from ASCII characters in the range [32,126] to glyphs
5032 * of the current font. This is used by gui_gtk2_draw_string() to skip
5033 * the itemize and shaping process for the most common case.
5036 ascii_glyph_table_init(void)
5038 char_u ascii_chars
[128];
5039 PangoAttrList
*attr_list
;
5043 if (gui
.ascii_glyphs
!= NULL
)
5044 pango_glyph_string_free(gui
.ascii_glyphs
);
5045 if (gui
.ascii_font
!= NULL
)
5046 g_object_unref(gui
.ascii_font
);
5048 gui
.ascii_glyphs
= NULL
;
5049 gui
.ascii_font
= NULL
;
5051 /* For safety, fill in question marks for the control characters. */
5052 for (i
= 0; i
< 32; ++i
)
5053 ascii_chars
[i
] = '?';
5054 for (; i
< 127; ++i
)
5056 ascii_chars
[i
] = '?';
5058 attr_list
= pango_attr_list_new();
5059 item_list
= pango_itemize(gui
.text_context
, (const char *)ascii_chars
,
5060 0, sizeof(ascii_chars
), attr_list
, NULL
);
5062 if (item_list
!= NULL
&& item_list
->next
== NULL
) /* play safe */
5067 item
= (PangoItem
*)item_list
->data
;
5068 width
= gui
.char_width
* PANGO_SCALE
;
5070 /* Remember the shape engine used for ASCII. */
5071 default_shape_engine
= item
->analysis
.shape_engine
;
5073 gui
.ascii_font
= item
->analysis
.font
;
5074 g_object_ref(gui
.ascii_font
);
5076 gui
.ascii_glyphs
= pango_glyph_string_new();
5078 pango_shape((const char *)ascii_chars
, sizeof(ascii_chars
),
5079 &item
->analysis
, gui
.ascii_glyphs
);
5081 g_return_if_fail(gui
.ascii_glyphs
->num_glyphs
== sizeof(ascii_chars
));
5083 for (i
= 0; i
< gui
.ascii_glyphs
->num_glyphs
; ++i
)
5085 PangoGlyphGeometry
*geom
;
5087 geom
= &gui
.ascii_glyphs
->glyphs
[i
].geometry
;
5088 geom
->x_offset
+= MAX(0, width
- geom
->width
) / 2;
5089 geom
->width
= width
;
5093 g_list_foreach(item_list
, (GFunc
)&pango_item_free
, NULL
);
5094 g_list_free(item_list
);
5095 pango_attr_list_unref(attr_list
);
5097 #endif /* HAVE_GTK2 */
5100 * Initialize Vim to use the font or fontset with the given name.
5101 * Return FAIL if the font could not be loaded, OK otherwise.
5104 gui_mch_init_font(char_u
*font_name
, int fontset UNUSED
)
5107 PangoFontDescription
*font_desc
;
5108 PangoLayout
*layout
;
5111 /* If font_name is NULL, this means to use the default, which should
5112 * be present on all proper Pango/fontconfig installations. */
5113 if (font_name
== NULL
)
5114 font_name
= (char_u
*)DEFAULT_FONT
;
5116 font_desc
= gui_mch_get_font(font_name
, FALSE
);
5118 if (font_desc
== NULL
)
5121 gui_mch_free_font(gui
.norm_font
);
5122 gui
.norm_font
= font_desc
;
5124 pango_context_set_font_description(gui
.text_context
, font_desc
);
5126 layout
= pango_layout_new(gui
.text_context
);
5127 pango_layout_set_text(layout
, "MW", 2);
5128 pango_layout_get_size(layout
, &width
, NULL
);
5130 * Set char_width to half the width obtained from pango_layout_get_size()
5131 * for CJK fixed_width/bi-width fonts. An unpatched version of Xft leads
5132 * Pango to use the same width for both non-CJK characters (e.g. Latin
5133 * letters and numbers) and CJK characters. This results in 's p a c e d
5134 * o u t' rendering when a CJK 'fixed width' font is used. To work around
5135 * that, divide the width returned by Pango by 2 if cjk_width is equal to
5136 * width for CJK fonts.
5138 * For related bugs, see:
5139 * http://bugzilla.gnome.org/show_bug.cgi?id=106618
5140 * http://bugzilla.gnome.org/show_bug.cgi?id=106624
5142 * With this, for all four of the following cases, Vim works fine:
5143 * guifont=CJK_fixed_width_font
5144 * guifont=Non_CJK_fixed_font
5145 * guifont=Non_CJK_fixed_font,CJK_Fixed_font
5146 * guifont=Non_CJK_fixed_font guifontwide=CJK_fixed_font
5148 if (is_cjk_font(gui
.norm_font
))
5152 /* Measure the text extent of U+4E00 and U+4E8C */
5153 pango_layout_set_text(layout
, "\344\270\200\344\272\214", -1);
5154 pango_layout_get_size(layout
, &cjk_width
, NULL
);
5156 if (width
== cjk_width
) /* Xft not patched */
5159 g_object_unref(layout
);
5161 gui
.char_width
= (width
/ 2 + PANGO_SCALE
- 1) / PANGO_SCALE
;
5163 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5164 if (gui
.char_width
<= 0)
5167 gui_mch_adjust_charheight();
5169 /* Set the fontname, which will be used for information purposes */
5170 hl_set_font_name(font_name
);
5172 get_styled_font_variants();
5173 ascii_glyph_table_init();
5175 /* Avoid unnecessary overhead if 'guifontwide' is equal to 'guifont'. */
5176 if (gui
.wide_font
!= NULL
5177 && pango_font_description_equal(gui
.norm_font
, gui
.wide_font
))
5179 pango_font_description_free(gui
.wide_font
);
5180 gui
.wide_font
= NULL
;
5183 #else /* !HAVE_GTK2 */
5185 GdkFont
*font
= NULL
;
5187 # ifdef FEAT_XFONTSET
5188 /* Try loading a fontset. If this fails we try loading a normal font. */
5189 if (fontset
&& font_name
!= NULL
)
5190 font
= gui_mch_get_fontset(font_name
, TRUE
, TRUE
);
5195 /* If font_name is NULL, this means to use the default, which should
5196 * be present on all X11 servers. */
5197 if (font_name
== NULL
)
5198 font_name
= (char_u
*)DEFAULT_FONT
;
5199 font
= gui_mch_get_font(font_name
, FALSE
);
5205 gui_mch_free_font(gui
.norm_font
);
5206 # ifdef FEAT_XFONTSET
5207 gui_mch_free_fontset(gui
.fontset
);
5208 if (font
->type
== GDK_FONT_FONTSET
)
5210 gui
.norm_font
= NOFONT
;
5211 gui
.fontset
= (GuiFontset
)font
;
5212 /* Use two bytes, this works around the problem that the result would
5213 * be zero if no 8-bit font was found. */
5214 gui
.char_width
= gdk_string_width(font
, "xW") / 2;
5219 gui
.norm_font
= font
;
5220 # ifdef FEAT_XFONTSET
5221 gui
.fontset
= NOFONTSET
;
5223 gui
.char_width
= ((XFontStruct
*)
5224 GDK_FONT_XFONT(font
))->max_bounds
.width
;
5227 /* A zero width may cause a crash. Happens for semi-invalid fontsets. */
5228 if (gui
.char_width
<= 0)
5231 gui
.char_height
= font
->ascent
+ font
->descent
+ p_linespace
;
5232 gui
.char_ascent
= font
->ascent
+ p_linespace
/ 2;
5234 /* A not-positive value of char_height may crash Vim. Only happens
5235 * if 'linespace' is negative (which does make sense sometimes). */
5236 gui
.char_ascent
= MAX(gui
.char_ascent
, 0);
5237 gui
.char_height
= MAX(gui
.char_height
, gui
.char_ascent
+ 1);
5239 /* Set the fontname, which will be used for information purposes */
5240 hl_set_font_name(font_name
);
5242 if (font
->type
!= GDK_FONT_FONTSET
)
5243 get_styled_font_variants(font_name
);
5245 /* Synchronize the fonts used in user input dialogs, since otherwise
5246 * search/replace will be esp. annoying in case of international font
5249 gui_gtk_synch_fonts();
5252 /* Adjust input management behaviour to the capabilities of the new
5254 xim_decide_input_style();
5255 if (xim_get_status_area_height())
5257 /* Status area is required. Just create the empty container so that
5258 * mainwin will allocate the extra space for status area. */
5259 GtkWidget
*alignment
= gtk_alignment_new((gfloat
)0.5, (gfloat
)0.5,
5260 (gfloat
)1.0, (gfloat
)1.0);
5262 gtk_widget_set_usize(alignment
, 20, gui
.char_height
+ 2);
5263 gtk_box_pack_end(GTK_BOX(GTK_BIN(gui
.mainwin
)->child
),
5264 alignment
, FALSE
, FALSE
, 0);
5265 gtk_widget_show(alignment
);
5268 #endif /* !HAVE_GTK2 */
5271 if (gui_mch_maximized())
5275 /* Update lines and columns in accordance with the new font, keep the
5276 * window maximized. */
5277 gtk_window_get_size(GTK_WINDOW(gui
.mainwin
), &w
, &h
);
5278 w
-= get_menu_tool_width();
5279 h
-= get_menu_tool_height();
5280 gui_resize_shell(w
, h
);
5285 /* Preserve the logical dimensions of the screen. */
5286 update_window_manager_hints(0, 0);
5293 * Get a reference to the font "name".
5294 * Return zero for failure.
5297 gui_mch_get_font(char_u
*name
, int report_error
)
5300 PangoFontDescription
*font
;
5305 /* can't do this when GUI is not running */
5306 if (!gui
.in_use
|| name
== NULL
)
5310 if (output_conv
.vc_type
!= CONV_NONE
)
5314 buf
= string_convert(&output_conv
, name
, NULL
);
5317 font
= pango_font_description_from_string((const char *)buf
);
5324 font
= pango_font_description_from_string((const char *)name
);
5328 PangoFont
*real_font
;
5330 /* pango_context_load_font() bails out if no font size is set */
5331 if (pango_font_description_get_size(font
) <= 0)
5332 pango_font_description_set_size(font
, 10 * PANGO_SCALE
);
5334 real_font
= pango_context_load_font(gui
.text_context
, font
);
5336 if (real_font
== NULL
)
5338 pango_font_description_free(font
);
5342 g_object_unref(real_font
);
5345 font
= gdk_font_load((const gchar
*)name
);
5351 EMSG2(_((char *)e_font
), name
);
5357 * The fixed-width check has been disabled for GTK+ 2. Rationale:
5359 * - The check tends to report false positives, particularly
5360 * in non-Latin locales or with old X fonts.
5361 * - Thanks to our fixed-width hack in gui_gtk2_draw_string(),
5362 * GTK+ 2 Vim is actually capable of displaying variable width
5363 * fonts. Those will just be spaced out like in AA xterm.
5364 * - Failing here for the default font causes GUI startup to fail
5365 * even with wiped out configuration files.
5366 * - The font dialog displays all fonts unfiltered, and it's rather
5367 * annoying if 95% of the listed fonts produce an error message.
5371 /* Check that this is a mono-spaced font. Naturally, this is a bit
5372 * hackish -- fixed-width isn't really suitable for i18n text :/ */
5373 PangoLayout
*layout
;
5375 int last_width
= -1;
5376 const char test_chars
[] = { 'W', 'i', ',', 'x' }; /* arbitrary */
5378 layout
= pango_layout_new(gui
.text_context
);
5379 pango_layout_set_font_description(layout
, font
);
5381 for (i
= 0; i
< G_N_ELEMENTS(test_chars
); ++i
)
5385 pango_layout_set_text(layout
, &test_chars
[i
], 1);
5386 pango_layout_get_size(layout
, &width
, NULL
);
5388 if (last_width
>= 0 && width
!= last_width
)
5390 pango_font_description_free(font
);
5398 g_object_unref(layout
);
5401 #else /* !HAVE_GTK2 */
5405 /* reference this font as being in use */
5408 /* Check that this is a mono-spaced font.
5410 xfont
= (XFontStruct
*) GDK_FONT_XFONT(font
);
5412 if (xfont
->max_bounds
.width
!= xfont
->min_bounds
.width
)
5414 gdk_font_unref(font
);
5418 #endif /* !HAVE_GTK2 */
5420 #if !defined(HAVE_GTK2) || 0 /* disabled for GTK+ 2, see above */
5421 if (font
== NULL
&& report_error
)
5422 EMSG2(_(e_fontwidth
), name
);
5428 #if defined(FEAT_EVAL) || defined(PROTO)
5430 * Return the name of font "font" in allocated memory.
5433 gui_mch_get_fontname(GuiFont font
, char_u
*name UNUSED
)
5438 char *pangoname
= pango_font_description_to_string(font
);
5440 if (pangoname
!= NULL
)
5442 char_u
*s
= vim_strsave((char_u
*)pangoname
);
5449 /* Don't know how to get the name, return what we got. */
5451 return vim_strsave(name
);
5457 #if !defined(HAVE_GTK2) || defined(PROTO)
5459 * Set the current text font.
5460 * Since we create all GC on demand, we use just gui.current_font to
5461 * indicate the desired current font.
5464 gui_mch_set_font(GuiFont font
)
5466 gui
.current_font
= font
;
5470 #if defined(FEAT_XFONTSET) || defined(PROTO)
5472 * Set the current text fontset.
5475 gui_mch_set_fontset(GuiFontset fontset
)
5477 gui
.current_font
= fontset
;
5482 * If a font is not going to be used, free its structure.
5485 gui_mch_free_font(GuiFont font
)
5489 pango_font_description_free(font
);
5491 gdk_font_unref(font
);
5495 #if defined(FEAT_XFONTSET) || defined(PROTO)
5497 * If a fontset is not going to be used, free its structure.
5500 gui_mch_free_fontset(GuiFontset fontset
)
5502 if (fontset
!= NOFONTSET
)
5503 gdk_font_unref(fontset
);
5509 * Return the Pixel value (color) for the given color name. This routine was
5510 * pretty much taken from example code in the Silicon Graphics OSF/Motif
5511 * Programmer's Guide.
5512 * Return INVALCOLOR for error.
5515 gui_mch_get_color(char_u
*name
)
5517 /* A number of colors that some X11 systems don't have */
5518 static const char *const vimnames
[][2] =
5520 {"LightRed", "#FFBBBB"},
5521 {"LightGreen", "#88FF88"},
5522 {"LightMagenta","#FFBBFF"},
5523 {"DarkCyan", "#008888"},
5524 {"DarkBlue", "#0000BB"},
5525 {"DarkRed", "#BB0000"},
5526 {"DarkMagenta", "#BB00BB"},
5527 {"DarkGrey", "#BBBBBB"},
5528 {"DarkYellow", "#BBBB00"},
5529 {"Gray10", "#1A1A1A"},
5530 {"Grey10", "#1A1A1A"},
5531 {"Gray20", "#333333"},
5532 {"Grey20", "#333333"},
5533 {"Gray30", "#4D4D4D"},
5534 {"Grey30", "#4D4D4D"},
5535 {"Gray40", "#666666"},
5536 {"Grey40", "#666666"},
5537 {"Gray50", "#7F7F7F"},
5538 {"Grey50", "#7F7F7F"},
5539 {"Gray60", "#999999"},
5540 {"Grey60", "#999999"},
5541 {"Gray70", "#B3B3B3"},
5542 {"Grey70", "#B3B3B3"},
5543 {"Gray80", "#CCCCCC"},
5544 {"Grey80", "#CCCCCC"},
5545 {"Gray90", "#E5E5E5"},
5546 {"Grey90", "#E5E5E5"},
5550 if (!gui
.in_use
) /* can't do this when GUI not running */
5553 while (name
!= NULL
)
5559 parsed
= gdk_color_parse((const char *)name
, &color
);
5561 #ifndef HAVE_GTK2 /* ohh, lovely GTK+ 2, eases our pain :) */
5563 * Since we have already called gtk_set_locale here the bugger
5564 * XParseColor will accept only explicit color names in the language
5565 * of the current locale. However this will interfere with:
5566 * 1. Vim's global startup files
5567 * 2. Explicit color names in .vimrc
5569 * Therefore we first try to parse the color in the current locale and
5570 * if it fails, we fall back to the portable "C" one.
5576 current
= setlocale(LC_ALL
, NULL
);
5577 if (current
!= NULL
)
5581 saved
= g_strdup(current
);
5582 setlocale(LC_ALL
, "C");
5584 parsed
= gdk_color_parse((const gchar
*)name
, &color
);
5586 setlocale(LC_ALL
, saved
);
5592 #endif /* !HAVE_GTK2 */
5597 gdk_colormap_alloc_color(gtk_widget_get_colormap(gui
.drawarea
),
5598 &color
, FALSE
, TRUE
);
5600 gdk_color_alloc(gtk_widget_get_colormap(gui
.drawarea
), &color
);
5602 return (guicolor_T
)color
.pixel
;
5604 /* add a few builtin names and try again */
5607 if (vimnames
[i
][0] == NULL
)
5612 if (STRICMP(name
, vimnames
[i
][0]) == 0)
5614 name
= (char_u
*)vimnames
[i
][1];
5624 * Set the current text foreground color.
5627 gui_mch_set_fg_color(guicolor_T color
)
5629 gui
.fgcolor
->pixel
= (unsigned long)color
;
5633 * Set the current text background color.
5636 gui_mch_set_bg_color(guicolor_T color
)
5638 gui
.bgcolor
->pixel
= (unsigned long)color
;
5642 * Set the current text special color.
5645 gui_mch_set_sp_color(guicolor_T color
)
5647 gui
.spcolor
->pixel
= (unsigned long)color
;
5652 * Function-like convenience macro for the sake of efficiency.
5654 #define INSERT_PANGO_ATTR(Attribute, AttrList, Start, End) \
5656 PangoAttribute *tmp_attr_; \
5657 tmp_attr_ = (Attribute); \
5658 tmp_attr_->start_index = (Start); \
5659 tmp_attr_->end_index = (End); \
5660 pango_attr_list_insert((AttrList), tmp_attr_); \
5664 apply_wide_font_attr(char_u
*s
, int len
, PangoAttrList
*attr_list
)
5666 char_u
*start
= NULL
;
5670 for (p
= s
; p
< s
+ len
; p
+= utf_byte2len(*p
))
5672 uc
= utf_ptr2char(p
);
5676 if (uc
>= 0x80 && utf_char2cells(uc
) == 2)
5679 else if (uc
< 0x80 /* optimization shortcut */
5680 || (utf_char2cells(uc
) != 2 && !utf_iscomposing(uc
)))
5682 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui
.wide_font
),
5683 attr_list
, start
- s
, p
- s
);
5689 INSERT_PANGO_ATTR(pango_attr_font_desc_new(gui
.wide_font
),
5690 attr_list
, start
- s
, len
);
5694 count_cluster_cells(char_u
*s
, PangoItem
*item
,
5695 PangoGlyphString
* glyphs
, int i
,
5697 int *last_glyph_rbearing
)
5700 int next
; /* glyph start index of next cluster */
5701 int start
, end
; /* string segment of current cluster */
5702 int width
; /* real cluster width in Pango units */
5706 width
= glyphs
->glyphs
[i
].geometry
.width
;
5708 for (next
= i
+ 1; next
< glyphs
->num_glyphs
; ++next
)
5710 if (glyphs
->glyphs
[next
].attr
.is_cluster_start
)
5712 else if (glyphs
->glyphs
[next
].geometry
.width
> width
)
5713 width
= glyphs
->glyphs
[next
].geometry
.width
;
5716 start
= item
->offset
+ glyphs
->log_clusters
[i
];
5717 end
= item
->offset
+ ((next
< glyphs
->num_glyphs
) ?
5718 glyphs
->log_clusters
[next
] : item
->length
);
5720 for (p
= s
+ start
; p
< s
+ end
; p
+= utf_byte2len(*p
))
5722 uc
= utf_ptr2char(p
);
5725 else if (!utf_iscomposing(uc
))
5726 cellcount
+= utf_char2cells(uc
);
5729 if (last_glyph_rbearing
!= NULL
5730 && cellcount
> 0 && next
== glyphs
->num_glyphs
)
5732 PangoRectangle ink_rect
;
5734 * If a certain combining mark had to be taken from a non-monospace
5735 * font, we have to compensate manually by adapting x_offset according
5736 * to the ink extents of the previous glyph.
5738 pango_font_get_glyph_extents(item
->analysis
.font
,
5739 glyphs
->glyphs
[i
].glyph
,
5742 if (PANGO_RBEARING(ink_rect
) > 0)
5743 *last_glyph_rbearing
= PANGO_RBEARING(ink_rect
);
5747 *cluster_width
= width
;
5753 * If there are only combining characters in the cluster, we cannot just
5754 * change the width of the previous glyph since there is none. Therefore
5755 * some guesswork is needed.
5757 * If ink_rect.x is negative Pango apparently has taken care of the composing
5758 * by itself. Actually setting x_offset = 0 should be sufficient then, but due
5759 * to problems with composing from different fonts we still need to fine-tune
5760 * x_offset to avoid uglyness.
5762 * If ink_rect.x is not negative, force overstriking by pointing x_offset to
5763 * the position of the previous glyph. Apparently this happens only with old
5764 * X fonts which don't provide the special combining information needed by
5768 setup_zero_width_cluster(PangoItem
*item
, PangoGlyphInfo
*glyph
,
5769 int last_cellcount
, int last_cluster_width
,
5770 int last_glyph_rbearing
)
5772 PangoRectangle ink_rect
;
5773 PangoRectangle logical_rect
;
5776 width
= last_cellcount
* gui
.char_width
* PANGO_SCALE
;
5777 glyph
->geometry
.x_offset
= -width
+ MAX(0, width
- last_cluster_width
) / 2;
5778 glyph
->geometry
.width
= 0;
5780 pango_font_get_glyph_extents(item
->analysis
.font
,
5782 &ink_rect
, &logical_rect
);
5785 glyph
->geometry
.x_offset
+= last_glyph_rbearing
;
5786 glyph
->geometry
.y_offset
= logical_rect
.height
5787 - (gui
.char_height
- p_linespace
) * PANGO_SCALE
;
5792 draw_glyph_string(int row
, int col
, int num_cells
, int flags
,
5793 PangoFont
*font
, PangoGlyphString
*glyphs
)
5795 if (!(flags
& DRAW_TRANSP
))
5797 gdk_gc_set_foreground(gui
.text_gc
, gui
.bgcolor
);
5799 gdk_draw_rectangle(gui
.drawarea
->window
,
5804 num_cells
* gui
.char_width
,
5808 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
5810 gdk_draw_glyphs(gui
.drawarea
->window
,
5817 /* redraw the contents with an offset of 1 to emulate bold */
5818 if ((flags
& DRAW_BOLD
) && !gui
.font_can_bold
)
5819 gdk_draw_glyphs(gui
.drawarea
->window
,
5827 #endif /* HAVE_GTK2 */
5830 * Draw underline and undercurl at the bottom of the character cell.
5833 draw_under(int flags
, int row
, int col
, int cells
)
5837 static const int val
[8] = {1, 0, 0, 0, 1, 2, 2, 2 };
5838 int y
= FILL_Y(row
+ 1) - 1;
5840 /* Undercurl: draw curl at the bottom of the character cell. */
5841 if (flags
& DRAW_UNDERC
)
5843 gdk_gc_set_foreground(gui
.text_gc
, gui
.spcolor
);
5844 for (i
= FILL_X(col
); i
< FILL_X(col
+ cells
); ++i
)
5846 offset
= val
[i
% 8];
5847 gdk_draw_point(gui
.drawarea
->window
, gui
.text_gc
, i
, y
- offset
);
5849 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
5852 /* Underline: draw a line at the bottom of the character cell. */
5853 if (flags
& DRAW_UNDERL
)
5855 /* When p_linespace is 0, overwrite the bottom row of pixels.
5856 * Otherwise put the line just below the character. */
5857 if (p_linespace
> 1)
5858 y
-= p_linespace
- 1;
5859 gdk_draw_line(gui
.drawarea
->window
, gui
.text_gc
,
5861 FILL_X(col
+ cells
) - 1, y
);
5865 #if defined(HAVE_GTK2) || defined(PROTO)
5867 gui_gtk2_draw_string(int row
, int col
, char_u
*s
, int len
, int flags
)
5869 GdkRectangle area
; /* area for clip mask */
5870 PangoGlyphString
*glyphs
; /* glyphs of current item */
5871 int column_offset
= 0; /* column offset in cells */
5873 char_u
*conv_buf
= NULL
; /* result of UTF-8 conversion */
5874 char_u
*new_conv_buf
;
5879 if (gui
.text_context
== NULL
|| gui
.drawarea
->window
== NULL
)
5882 if (output_conv
.vc_type
!= CONV_NONE
)
5885 * Convert characters from 'encoding' to 'termencoding', which is set
5886 * to UTF-8 by gui_mch_init(). did_set_string_option() in option.c
5887 * prohibits changing this to something else than UTF-8 if the GUI is
5891 conv_buf
= string_convert(&output_conv
, s
, &convlen
);
5892 g_return_val_if_fail(conv_buf
!= NULL
, len
);
5894 /* Correct for differences in char width: some chars are
5895 * double-wide in 'encoding' but single-wide in utf-8. Add a space to
5896 * compensate for that. */
5897 for (sp
= s
, bp
= conv_buf
; sp
< s
+ len
&& bp
< conv_buf
+ convlen
; )
5899 plen
= utf_ptr2len(bp
);
5900 if ((*mb_ptr2cells
)(sp
) == 2 && utf_ptr2cells(bp
) == 1)
5902 new_conv_buf
= alloc(convlen
+ 2);
5903 if (new_conv_buf
== NULL
)
5905 plen
+= bp
- conv_buf
;
5906 mch_memmove(new_conv_buf
, conv_buf
, plen
);
5907 new_conv_buf
[plen
] = ' ';
5908 mch_memmove(new_conv_buf
+ plen
+ 1, conv_buf
+ plen
,
5909 convlen
- plen
+ 1);
5911 conv_buf
= new_conv_buf
;
5913 bp
= conv_buf
+ plen
;
5916 sp
+= (*mb_ptr2len
)(sp
);
5924 * Restrict all drawing to the current screen line in order to prevent
5925 * fuzzy font lookups from messing up the screen.
5927 area
.x
= gui
.border_offset
;
5928 area
.y
= FILL_Y(row
);
5929 area
.width
= gui
.num_cols
* gui
.char_width
;
5930 area
.height
= gui
.char_height
;
5932 gdk_gc_set_clip_origin(gui
.text_gc
, 0, 0);
5933 gdk_gc_set_clip_rectangle(gui
.text_gc
, &area
);
5935 glyphs
= pango_glyph_string_new();
5938 * Optimization hack: If possible, skip the itemize and shaping process
5939 * for pure ASCII strings. This optimization is particularly effective
5940 * because Vim draws space characters to clear parts of the screen.
5942 if (!(flags
& DRAW_ITALIC
)
5943 && !((flags
& DRAW_BOLD
) && gui
.font_can_bold
)
5944 && gui
.ascii_glyphs
!= NULL
)
5948 for (p
= s
; p
< s
+ len
; ++p
)
5952 pango_glyph_string_set_size(glyphs
, len
);
5954 for (i
= 0; i
< len
; ++i
)
5956 glyphs
->glyphs
[i
] = gui
.ascii_glyphs
->glyphs
[s
[i
]];
5957 glyphs
->log_clusters
[i
] = i
;
5960 draw_glyph_string(row
, col
, len
, flags
, gui
.ascii_font
, glyphs
);
5962 column_offset
= len
;
5967 PangoAttrList
*attr_list
;
5970 int last_glyph_rbearing
;
5971 int cells
= 0; /* cells occupied by current cluster */
5973 int monospace13
= STRICMP(p_guifont
, "monospace 13") == 0;
5976 /* Safety check: pango crashes when invoked with invalid utf-8
5978 if (!utf_valid_string(s
, s
+ len
))
5980 column_offset
= len
;
5984 /* original width of the current cluster */
5985 cluster_width
= PANGO_SCALE
* gui
.char_width
;
5987 /* right bearing of the last non-composing glyph */
5988 last_glyph_rbearing
= PANGO_SCALE
* gui
.char_width
;
5990 attr_list
= pango_attr_list_new();
5992 /* If 'guifontwide' is set then use that for double-width characters.
5993 * Otherwise just go with 'guifont' and let Pango do its thing. */
5994 if (gui
.wide_font
!= NULL
)
5995 apply_wide_font_attr(s
, len
, attr_list
);
5997 if ((flags
& DRAW_BOLD
) && gui
.font_can_bold
)
5998 INSERT_PANGO_ATTR(pango_attr_weight_new(PANGO_WEIGHT_BOLD
),
6000 if (flags
& DRAW_ITALIC
)
6001 INSERT_PANGO_ATTR(pango_attr_style_new(PANGO_STYLE_ITALIC
),
6004 * Break the text into segments with consistent directional level
6005 * and shaping engine. Pure Latin text needs only a single segment,
6006 * so there's no need to worry about the loop's efficiency. Better
6007 * try to optimize elsewhere, e.g. reducing exposes and stuff :)
6009 item_list
= pango_itemize(gui
.text_context
,
6010 (const char *)s
, 0, len
, attr_list
, NULL
);
6012 while (item_list
!= NULL
)
6015 int item_cells
= 0; /* item length in cells */
6017 item
= (PangoItem
*)item_list
->data
;
6018 item_list
= g_list_delete_link(item_list
, item_list
);
6020 * Increment the bidirectional embedding level by 1 if it is not
6021 * even. An odd number means the output will be RTL, but we don't
6022 * want that since Vim handles right-to-left text on its own. It
6023 * would probably be sufficient to just set level = 0, but you can
6026 * Unfortunately we can't take advantage of Pango's ability to
6027 * render both LTR and RTL at the same time. In order to support
6028 * that, Vim's main screen engine would have to make use of Pango
6031 item
->analysis
.level
= (item
->analysis
.level
+ 1) & (~1U);
6033 /* HACK: Overrule the shape engine, we don't want shaping to be
6034 * done, because drawing the cursor would change the display. */
6035 item
->analysis
.shape_engine
= default_shape_engine
;
6037 pango_shape((const char *)s
+ item
->offset
, item
->length
,
6038 &item
->analysis
, glyphs
);
6040 * Fixed-width hack: iterate over the array and assign a fixed
6041 * width to each glyph, thus overriding the choice made by the
6042 * shaping engine. We use utf_char2cells() to determine the
6043 * number of cells needed.
6045 * Also perform all kind of dark magic to get composing
6046 * characters right (and pretty too of course).
6048 for (i
= 0; i
< glyphs
->num_glyphs
; ++i
)
6050 PangoGlyphInfo
*glyph
;
6052 glyph
= &glyphs
->glyphs
[i
];
6054 if (glyph
->attr
.is_cluster_start
)
6058 cellcount
= count_cluster_cells(
6059 s
, item
, glyphs
, i
, &cluster_width
,
6060 (item_list
!= NULL
) ? &last_glyph_rbearing
: NULL
);
6066 width
= cellcount
* gui
.char_width
* PANGO_SCALE
;
6067 glyph
->geometry
.x_offset
+=
6068 MAX(0, width
- cluster_width
) / 2;
6069 glyph
->geometry
.width
= width
;
6073 /* If there are only combining characters in the
6074 * cluster, we cannot just change the width of the
6075 * previous glyph since there is none. Therefore
6076 * some guesswork is needed. */
6077 setup_zero_width_cluster(item
, glyph
, cells
,
6079 last_glyph_rbearing
);
6082 item_cells
+= cellcount
;
6089 /* There is a previous glyph, so we deal with combining
6090 * characters the canonical way. That is, setting the
6091 * width of the previous glyph to 0. */
6092 glyphs
->glyphs
[i
- 1].geometry
.width
= 0;
6093 width
= cells
* gui
.char_width
* PANGO_SCALE
;
6094 glyph
->geometry
.x_offset
+=
6095 MAX(0, width
- cluster_width
) / 2;
6097 /* Dirty hack: for "monospace 13" font there is a bug that
6098 * draws composing chars in the wrong position. Add
6099 * "width" to the offset to work around that. */
6101 glyph
->geometry
.x_offset
= width
;
6104 glyph
->geometry
.width
= width
;
6106 else /* i == 0 "cannot happen" */
6108 glyph
->geometry
.width
= 0;
6112 /*** Aaaaand action! ***/
6113 draw_glyph_string(row
, col
+ column_offset
, item_cells
,
6114 flags
, item
->analysis
.font
, glyphs
);
6116 pango_item_free(item
);
6118 column_offset
+= item_cells
;
6121 pango_attr_list_unref(attr_list
);
6125 /* Draw underline and undercurl. */
6126 draw_under(flags
, row
, col
, column_offset
);
6128 pango_glyph_string_free(glyphs
);
6131 gdk_gc_set_clip_rectangle(gui
.text_gc
, NULL
);
6133 return column_offset
;
6135 #endif /* HAVE_GTK2 */
6137 #if !defined(HAVE_GTK2) || defined(PROTO)
6139 gui_mch_draw_string(int row
, int col
, char_u
*s
, int len
, int flags
)
6141 static XChar2b
*buf
= NULL
;
6142 static int buflen
= 0;
6153 if (gui
.current_font
== NULL
|| gui
.drawarea
->window
== NULL
)
6157 * Yeah yeah apparently the font support in GTK+ 1.2 only cares for either:
6158 * asians or 8-bit fonts. It is broken there, but no wonder the whole font
6159 * stuff is broken in X11 in first place. And the internationalization API
6160 * isn't something you would really like to use.
6163 xfont
= (XFontStruct
*)((GdkFontPrivate
*)gui
.current_font
)->xfont
;
6164 is_wide
= ((xfont
->min_byte1
!= 0 || xfont
->max_byte1
!= 0)
6165 # ifdef FEAT_XFONTSET
6166 && gui
.fontset
== NOFONTSET
6172 /* Convert a byte sequence to 16 bit characters for the Gdk functions.
6173 * Need a buffer for the 16 bit characters. Keep it between calls,
6174 * because allocating it each time is slow. */
6177 XtFree((char *)buf
);
6178 buf
= (XChar2b
*)XtMalloc(len
* sizeof(XChar2b
));
6192 /* TODO: use the composing characters */
6193 c
= utfc_ptr2char_len(p
, pcc
, len
- (p
- s
));
6194 if (c
>= 0x10000) /* show chars > 0xffff as ? */
6196 buf
[textlen
].byte1
= c
>> 8;
6197 buf
[textlen
].byte2
= c
;
6198 p
+= utfc_ptr2len_len(p
, len
- (p
- s
));
6199 width
+= utf_char2cells(c
);
6204 buf
[textlen
].byte1
= '\0'; /* high eight bits */
6205 buf
[textlen
].byte2
= *p
; /* low eight bits */
6212 textlen
= textlen
* 2;
6216 text
= (XChar2b
*)s
;
6222 for (p
= s
; p
< s
+ len
; p
+= (*mb_ptr2len_len
)(p
, len
- (p
- s
)))
6223 width
+= (*mb_ptr2cells_len
)(p
, len
- (p
- s
));
6230 if (!(flags
& DRAW_TRANSP
))
6232 gdk_gc_set_foreground(gui
.text_gc
, gui
.bgcolor
);
6233 gdk_draw_rectangle(gui
.drawarea
->window
,
6236 FILL_X(col
), FILL_Y(row
),
6237 width
* gui
.char_width
, gui
.char_height
);
6239 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
6240 gdk_draw_text(gui
.drawarea
->window
,
6243 TEXT_X(col
), TEXT_Y(row
),
6244 (const gchar
*)text
, textlen
);
6246 /* redraw the contents with an offset of 1 to emulate bold */
6247 if (flags
& DRAW_BOLD
)
6248 gdk_draw_text(gui
.drawarea
->window
,
6251 TEXT_X(col
) + 1, TEXT_Y(row
),
6252 (const gchar
*)text
, textlen
);
6254 /* Draw underline and undercurl. */
6255 draw_under(flags
, row
, col
, width
);
6257 #endif /* !HAVE_GTK2 */
6260 * Return OK if the key with the termcap name "name" is supported.
6263 gui_mch_haskey(char_u
*name
)
6267 for (i
= 0; special_keys
[i
].key_sym
!= 0; i
++)
6268 if (name
[0] == special_keys
[i
].code0
6269 && name
[1] == special_keys
[i
].code1
)
6274 #if defined(FEAT_TITLE) \
6275 || (defined(FEAT_XIM) && !defined(HAVE_GTK2)) \
6278 * Return the text window-id and display. Only required for X-based GUI's
6281 gui_get_x11_windis(Window
*win
, Display
**dis
)
6283 if (gui
.mainwin
!= NULL
&& gui
.mainwin
->window
!= NULL
)
6285 *dis
= GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
);
6286 *win
= GDK_WINDOW_XWINDOW(gui
.mainwin
->window
);
6296 #if defined(FEAT_CLIENTSERVER) \
6297 || (defined(FEAT_X11) && defined(FEAT_CLIPBOARD)) || defined(PROTO)
6300 gui_mch_get_display(void)
6302 if (gui
.mainwin
!= NULL
&& gui
.mainwin
->window
!= NULL
)
6303 return GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
);
6312 #ifdef HAVE_GTK_MULTIHEAD
6313 GdkDisplay
*display
;
6315 if (gui
.mainwin
!= NULL
&& GTK_WIDGET_REALIZED(gui
.mainwin
))
6316 display
= gtk_widget_get_display(gui
.mainwin
);
6318 display
= gdk_display_get_default();
6320 if (display
!= NULL
)
6321 gdk_display_beep(display
);
6328 gui_mch_flash(int msec
)
6333 if (gui
.drawarea
->window
== NULL
)
6336 values
.foreground
.pixel
= gui
.norm_pixel
^ gui
.back_pixel
;
6337 values
.background
.pixel
= gui
.norm_pixel
^ gui
.back_pixel
;
6338 values
.function
= GDK_XOR
;
6339 invert_gc
= gdk_gc_new_with_values(gui
.drawarea
->window
,
6344 gdk_gc_set_exposures(invert_gc
,
6345 gui
.visibility
!= GDK_VISIBILITY_UNOBSCURED
);
6347 * Do a visual beep by changing back and forth in some undetermined way,
6348 * the foreground and background colors. This is due to the fact that
6349 * there can't be really any prediction about the effects of XOR on
6350 * arbitrary X11 servers. However this seems to be enough for what we
6353 gdk_draw_rectangle(gui
.drawarea
->window
, invert_gc
,
6356 FILL_X((int)Columns
) + gui
.border_offset
,
6357 FILL_Y((int)Rows
) + gui
.border_offset
);
6360 ui_delay((long)msec
, TRUE
); /* wait so many msec */
6362 gdk_draw_rectangle(gui
.drawarea
->window
, invert_gc
,
6365 FILL_X((int)Columns
) + gui
.border_offset
,
6366 FILL_Y((int)Rows
) + gui
.border_offset
);
6368 gdk_gc_destroy(invert_gc
);
6372 * Invert a rectangle from row r, column c, for nr rows and nc columns.
6375 gui_mch_invert_rectangle(int r
, int c
, int nr
, int nc
)
6380 if (gui
.drawarea
->window
== NULL
)
6383 values
.foreground
.pixel
= gui
.norm_pixel
^ gui
.back_pixel
;
6384 values
.background
.pixel
= gui
.norm_pixel
^ gui
.back_pixel
;
6385 values
.function
= GDK_XOR
;
6386 invert_gc
= gdk_gc_new_with_values(gui
.drawarea
->window
,
6391 gdk_gc_set_exposures(invert_gc
, gui
.visibility
!=
6392 GDK_VISIBILITY_UNOBSCURED
);
6393 gdk_draw_rectangle(gui
.drawarea
->window
, invert_gc
,
6395 FILL_X(c
), FILL_Y(r
),
6396 (nc
) * gui
.char_width
, (nr
) * gui
.char_height
);
6397 gdk_gc_destroy(invert_gc
);
6401 * Iconify the GUI window.
6404 gui_mch_iconify(void)
6407 gtk_window_iconify(GTK_WINDOW(gui
.mainwin
));
6409 XIconifyWindow(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
),
6410 GDK_WINDOW_XWINDOW(gui
.mainwin
->window
),
6411 DefaultScreen(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
)));
6415 #if defined(FEAT_EVAL) || defined(PROTO)
6417 * Bring the Vim window to the foreground.
6420 gui_mch_set_foreground(void)
6423 gtk_window_present(GTK_WINDOW(gui
.mainwin
));
6425 gdk_window_raise(gui
.mainwin
->window
);
6431 * Draw a cursor without focus.
6434 gui_mch_draw_hollow_cursor(guicolor_T color
)
6438 if (gui
.drawarea
->window
== NULL
)
6441 gui_mch_set_fg_color(color
);
6443 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
6445 if (mb_lefthalve(gui
.row
, gui
.col
))
6448 gdk_draw_rectangle(gui
.drawarea
->window
, gui
.text_gc
,
6450 FILL_X(gui
.col
), FILL_Y(gui
.row
),
6451 i
* gui
.char_width
- 1, gui
.char_height
- 1);
6455 * Draw part of a cursor, "w" pixels wide, and "h" pixels high, using
6459 gui_mch_draw_part_cursor(int w
, int h
, guicolor_T color
)
6461 if (gui
.drawarea
->window
== NULL
)
6464 gui_mch_set_fg_color(color
);
6466 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
6467 gdk_draw_rectangle(gui
.drawarea
->window
, gui
.text_gc
,
6469 #ifdef FEAT_RIGHTLEFT
6470 /* vertical line should be on the right of current point */
6471 CURSOR_BAR_RIGHT
? FILL_X(gui
.col
+ 1) - w
:
6474 FILL_Y(gui
.row
) + gui
.char_height
- h
,
6480 * Catch up with any queued X11 events. This may put keyboard input into the
6481 * input buffer, call resize call-backs, trigger timers etc. If there is
6482 * nothing in the X11 event queue (& no timers pending), then we return
6486 gui_mch_update(void)
6488 while (gtk_events_pending() && !vim_is_input_buf_full())
6489 gtk_main_iteration_do(FALSE
);
6493 input_timer_cb(gpointer data
)
6495 int *timed_out
= (int *) data
;
6497 /* Just inform the caller about the occurrence of it */
6500 if (gtk_main_level() > 0)
6503 return FALSE
; /* don't happen again */
6508 * Callback function, used when data is available on the SNiFF connection.
6514 GdkInputCondition condition
)
6516 static char_u bytes
[3] = {CSI
, (int)KS_EXTRA
, (int)KE_SNIFF
};
6518 add_to_input_buf(bytes
, 3);
6520 if (gtk_main_level() > 0)
6526 * GUI input routine called by gui_wait_for_chars(). Waits for a character
6527 * from the keyboard.
6528 * wtime == -1 Wait forever.
6529 * wtime == 0 This should never happen.
6530 * wtime > 0 Wait wtime milliseconds for a character.
6531 * Returns OK if a character was found to be available within the given time,
6532 * or FAIL otherwise.
6535 gui_mch_wait_for_chars(long wtime
)
6539 static int timed_out
;
6541 static int sniff_on
= 0;
6542 static gint sniff_input_id
= 0;
6546 if (sniff_on
&& !want_sniff_request
)
6549 gdk_input_remove(sniff_input_id
);
6552 else if (!sniff_on
&& want_sniff_request
)
6554 /* Add fd_from_sniff to watch for available data in main loop. */
6555 sniff_input_id
= gdk_input_add(fd_from_sniff
,
6556 GDK_INPUT_READ
, sniff_request_cb
, NULL
);
6563 /* this timeout makes sure that we will return if no characters arrived in
6567 timer
= gtk_timeout_add((guint32
)wtime
, input_timer_cb
, &timed_out
);
6571 focus
= gui
.in_focus
;
6575 /* Stop or start blinking when focus changes */
6576 if (gui
.in_focus
!= focus
)
6579 gui_mch_start_blink();
6581 gui_mch_stop_blink();
6582 focus
= gui
.in_focus
;
6585 #if defined(FEAT_NETBEANS_INTG)
6586 /* Process the queued netbeans messages. */
6587 netbeans_parse_messages();
6591 * Loop in GTK+ processing until a timeout or input occurs.
6592 * Skip this if input is available anyway (can happen in rare
6593 * situations, sort of race condition).
6595 if (!input_available())
6598 /* Got char, return immediately */
6599 if (input_available())
6601 if (timer
!= 0 && !timed_out
)
6602 gtk_timeout_remove(timer
);
6605 } while (wtime
< 0 || !timed_out
);
6608 * Flush all eventually pending (drawing) events.
6616 /****************************************************************************
6617 * Output drawing routines.
6618 ****************************************************************************/
6621 /* Flush any output to the screen */
6625 #ifdef HAVE_GTK_MULTIHEAD
6626 if (gui
.mainwin
!= NULL
&& GTK_WIDGET_REALIZED(gui
.mainwin
))
6627 gdk_display_sync(gtk_widget_get_display(gui
.mainwin
));
6629 gdk_flush(); /* historical misnomer: calls XSync(), not XFlush() */
6632 /* This happens to actually do what gui_mch_flush() is supposed to do,
6633 * according to the comment above. */
6634 if (gui
.drawarea
!= NULL
&& gui
.drawarea
->window
!= NULL
)
6635 gdk_window_process_updates(gui
.drawarea
->window
, FALSE
);
6640 * Clear a rectangular region of the screen from text pos (row1, col1) to
6641 * (row2, col2) inclusive.
6644 gui_mch_clear_block(int row1
, int col1
, int row2
, int col2
)
6648 if (gui
.drawarea
->window
== NULL
)
6651 color
.pixel
= gui
.back_pixel
;
6653 gdk_gc_set_foreground(gui
.text_gc
, &color
);
6655 /* Clear one extra pixel at the far right, for when bold characters have
6656 * spilled over to the window border. */
6657 gdk_draw_rectangle(gui
.drawarea
->window
, gui
.text_gc
, TRUE
,
6658 FILL_X(col1
), FILL_Y(row1
),
6659 (col2
- col1
+ 1) * gui
.char_width
6660 + (col2
== Columns
- 1),
6661 (row2
- row1
+ 1) * gui
.char_height
);
6665 gui_mch_clear_all(void)
6667 if (gui
.drawarea
->window
!= NULL
)
6668 gdk_window_clear(gui
.drawarea
->window
);
6672 * Redraw any text revealed by scrolling up/down.
6675 check_copy_area(void)
6680 if (gui
.visibility
!= GDK_VISIBILITY_PARTIAL
)
6683 /* Avoid redrawing the cursor while scrolling or it'll end up where
6684 * we don't want it to be. I'm not sure if it's correct to call
6685 * gui_dont_update_cursor() at this point but it works as a quick
6687 gui_dont_update_cursor();
6691 /* Wait to check whether the scroll worked or not. */
6692 event
= gdk_event_get_graphics_expose(gui
.drawarea
->window
);
6695 break; /* received NoExpose event */
6697 gui_redraw(event
->expose
.area
.x
, event
->expose
.area
.y
,
6698 event
->expose
.area
.width
, event
->expose
.area
.height
);
6700 expose_count
= event
->expose
.count
;
6701 gdk_event_free(event
);
6703 while (expose_count
> 0); /* more events follow */
6705 gui_can_update_cursor();
6709 * Delete the given number of lines from the given row, scrolling up any
6710 * text further down within the scroll region.
6713 gui_mch_delete_lines(int row
, int num_lines
)
6715 if (gui
.visibility
== GDK_VISIBILITY_FULLY_OBSCURED
)
6716 return; /* Can't see the window */
6718 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
6719 gdk_gc_set_background(gui
.text_gc
, gui
.bgcolor
);
6721 /* copy one extra pixel, for when bold has spilled over */
6722 gdk_window_copy_area(gui
.drawarea
->window
, gui
.text_gc
,
6723 FILL_X(gui
.scroll_region_left
), FILL_Y(row
),
6724 gui
.drawarea
->window
,
6725 FILL_X(gui
.scroll_region_left
),
6726 FILL_Y(row
+ num_lines
),
6727 gui
.char_width
* (gui
.scroll_region_right
6728 - gui
.scroll_region_left
+ 1) + 1,
6729 gui
.char_height
* (gui
.scroll_region_bot
- row
- num_lines
+ 1));
6731 gui_clear_block(gui
.scroll_region_bot
- num_lines
+ 1,
6732 gui
.scroll_region_left
,
6733 gui
.scroll_region_bot
, gui
.scroll_region_right
);
6738 * Insert the given number of lines before the given row, scrolling down any
6739 * following text within the scroll region.
6742 gui_mch_insert_lines(int row
, int num_lines
)
6744 if (gui
.visibility
== GDK_VISIBILITY_FULLY_OBSCURED
)
6745 return; /* Can't see the window */
6747 gdk_gc_set_foreground(gui
.text_gc
, gui
.fgcolor
);
6748 gdk_gc_set_background(gui
.text_gc
, gui
.bgcolor
);
6750 /* copy one extra pixel, for when bold has spilled over */
6751 gdk_window_copy_area(gui
.drawarea
->window
, gui
.text_gc
,
6752 FILL_X(gui
.scroll_region_left
), FILL_Y(row
+ num_lines
),
6753 gui
.drawarea
->window
,
6754 FILL_X(gui
.scroll_region_left
), FILL_Y(row
),
6755 gui
.char_width
* (gui
.scroll_region_right
6756 - gui
.scroll_region_left
+ 1) + 1,
6757 gui
.char_height
* (gui
.scroll_region_bot
- row
- num_lines
+ 1));
6759 gui_clear_block(row
, gui
.scroll_region_left
,
6760 row
+ num_lines
- 1, gui
.scroll_region_right
);
6765 * X Selection stuff, for cutting and pasting text to other windows.
6768 clip_mch_request_selection(VimClipboard
*cbd
)
6774 for (i
= 0; i
< N_SELECTION_TARGETS
; ++i
)
6777 if (!clip_html
&& selection_targets
[i
].info
== TARGET_HTML
)
6780 received_selection
= RS_NONE
;
6781 target
= gdk_atom_intern(selection_targets
[i
].target
, FALSE
);
6783 gtk_selection_convert(gui
.drawarea
,
6784 cbd
->gtk_sel_atom
, target
,
6785 (guint32
)GDK_CURRENT_TIME
);
6787 /* Hack: Wait up to three seconds for the selection. A hang was
6788 * noticed here when using the netrw plugin combined with ":gui"
6789 * during the FocusGained event. */
6791 while (received_selection
== RS_NONE
&& time(NULL
) < start
+ 3)
6792 gtk_main(); /* wait for selection_received_cb */
6794 if (received_selection
!= RS_FAIL
)
6798 /* Final fallback position - use the X CUT_BUFFER0 store */
6799 yank_cut_buffer0(GDK_WINDOW_XDISPLAY(gui
.mainwin
->window
), cbd
);
6803 * Disown the selection.
6806 clip_mch_lose_selection(VimClipboard
*cbd UNUSED
)
6808 /* WEIRD: when using NULL to actually disown the selection, we lose the
6809 * selection the first time we own it. */
6811 gtk_selection_owner_set(NULL, cbd->gtk_sel_atom, (guint32)GDK_CURRENT_TIME);
6817 * Own the selection and return OK if it worked.
6820 clip_mch_own_selection(VimClipboard
*cbd
)
6824 success
= gtk_selection_owner_set(gui
.drawarea
, cbd
->gtk_sel_atom
,
6825 (guint32
)GDK_CURRENT_TIME
);
6827 return (success
) ? OK
: FAIL
;
6831 * Send the current selection to the clipboard. Do nothing for X because we
6832 * will fill in the selection only when requested by another app.
6835 clip_mch_set_selection(VimClipboard
*cbd UNUSED
)
6840 #if defined(FEAT_MENU) || defined(PROTO)
6842 * Make a menu item appear either active or not active (grey or not grey).
6845 gui_mch_menu_grey(vimmenu_T
*menu
, int grey
)
6847 if (menu
->id
== NULL
)
6850 if (menu_is_separator(menu
->name
))
6853 gui_mch_menu_hidden(menu
, FALSE
);
6854 /* Be clever about bitfields versus true booleans here! */
6855 if (!GTK_WIDGET_SENSITIVE(menu
->id
) == !grey
)
6857 gtk_widget_set_sensitive(menu
->id
, !grey
);
6863 * Make menu item hidden or not hidden.
6866 gui_mch_menu_hidden(vimmenu_T
*menu
, int hidden
)
6873 if (GTK_WIDGET_VISIBLE(menu
->id
))
6875 gtk_widget_hide(menu
->id
);
6881 if (!GTK_WIDGET_VISIBLE(menu
->id
))
6883 gtk_widget_show(menu
->id
);
6890 * This is called after setting all the menus to grey/hidden or not.
6893 gui_mch_draw_menubar(void)
6895 /* just make sure that the visual changes get effect immediately */
6898 #endif /* FEAT_MENU */
6904 gui_mch_enable_scrollbar(scrollbar_T
*sb
, int flag
)
6910 gtk_widget_show(sb
->id
);
6912 gtk_widget_hide(sb
->id
);
6914 update_window_manager_hints(0, 0);
6919 * Return the RGB value of a pixel as long.
6922 gui_mch_get_rgb(guicolor_T pixel
)
6926 GdkColorContext
*cc
;
6928 cc
= gdk_color_context_new(gtk_widget_get_visual(gui
.drawarea
),
6929 gtk_widget_get_colormap(gui
.drawarea
));
6930 color
.pixel
= pixel
;
6931 gdk_color_context_query_color(cc
, &color
);
6933 gdk_color_context_free(cc
);
6935 gdk_colormap_query_color(gtk_widget_get_colormap(gui
.drawarea
),
6936 (unsigned long)pixel
, &color
);
6939 return (((unsigned)color
.red
& 0xff00) << 8)
6940 | ((unsigned)color
.green
& 0xff00)
6941 | (((unsigned)color
.blue
& 0xff00) >> 8);
6945 * Get current mouse coordinates in text window.
6948 gui_mch_getmouse(int *x
, int *y
)
6950 gdk_window_get_pointer(gui
.drawarea
->window
, x
, y
, NULL
);
6954 gui_mch_setmouse(int x
, int y
)
6956 /* Sorry for the Xlib call, but we can't avoid it, since there is no
6957 * internal GDK mechanism present to accomplish this. (and for good
6959 XWarpPointer(GDK_WINDOW_XDISPLAY(gui
.drawarea
->window
),
6960 (Window
)0, GDK_WINDOW_XWINDOW(gui
.drawarea
->window
),
6961 0, 0, 0U, 0U, x
, y
);
6965 #ifdef FEAT_MOUSESHAPE
6966 /* The last set mouse pointer shape is remembered, to be used when it goes
6967 * from hidden to not hidden. */
6968 static int last_shape
= 0;
6972 * Use the blank mouse pointer or not.
6974 * hide: TRUE = use blank ptr, FALSE = use parent ptr
6977 gui_mch_mousehide(int hide
)
6979 if (gui
.pointer_hidden
!= hide
)
6981 gui
.pointer_hidden
= hide
;
6982 if (gui
.drawarea
->window
&& gui
.blank_pointer
!= NULL
)
6985 gdk_window_set_cursor(gui
.drawarea
->window
, gui
.blank_pointer
);
6987 #ifdef FEAT_MOUSESHAPE
6988 mch_set_mouse_shape(last_shape
);
6990 gdk_window_set_cursor(gui
.drawarea
->window
, NULL
);
6996 #if defined(FEAT_MOUSESHAPE) || defined(PROTO)
6998 /* Table for shape IDs. Keep in sync with the mshape_names[] table in
7000 static const int mshape_ids
[] =
7002 GDK_LEFT_PTR
, /* arrow */
7003 GDK_CURSOR_IS_PIXMAP
, /* blank */
7004 GDK_XTERM
, /* beam */
7005 GDK_SB_V_DOUBLE_ARROW
, /* updown */
7006 GDK_SIZING
, /* udsizing */
7007 GDK_SB_H_DOUBLE_ARROW
, /* leftright */
7008 GDK_SIZING
, /* lrsizing */
7009 GDK_WATCH
, /* busy */
7010 GDK_X_CURSOR
, /* no */
7011 GDK_CROSSHAIR
, /* crosshair */
7012 GDK_HAND1
, /* hand1 */
7013 GDK_HAND2
, /* hand2 */
7014 GDK_PENCIL
, /* pencil */
7015 GDK_QUESTION_ARROW
, /* question */
7016 GDK_RIGHT_PTR
, /* right-arrow */
7017 GDK_CENTER_PTR
, /* up-arrow */
7018 GDK_LEFT_PTR
/* last one */
7022 mch_set_mouse_shape(int shape
)
7027 if (gui
.drawarea
->window
== NULL
)
7030 if (shape
== MSHAPE_HIDE
|| gui
.pointer_hidden
)
7031 gdk_window_set_cursor(gui
.drawarea
->window
, gui
.blank_pointer
);
7034 if (shape
>= MSHAPE_NUMBERED
)
7036 id
= shape
- MSHAPE_NUMBERED
;
7037 if (id
>= GDK_LAST_CURSOR
)
7040 id
&= ~1; /* they are always even (why?) */
7042 else if (shape
< (int)(sizeof(mshape_ids
) / sizeof(int)))
7043 id
= mshape_ids
[shape
];
7046 # ifdef HAVE_GTK_MULTIHEAD
7047 c
= gdk_cursor_new_for_display(
7048 gtk_widget_get_display(gui
.drawarea
), (GdkCursorType
)id
);
7050 c
= gdk_cursor_new((GdkCursorType
)id
);
7052 gdk_window_set_cursor(gui
.drawarea
->window
, c
);
7053 gdk_cursor_destroy(c
); /* Unref, actually. Bloody GTK+ 1. */
7055 if (shape
!= MSHAPE_HIDE
)
7058 #endif /* FEAT_MOUSESHAPE */
7061 #if defined(FEAT_SIGN_ICONS) || defined(PROTO)
7063 * Signs are currently always 2 chars wide. With GTK+ 2, the image will be
7064 * scaled down if the current font is not big enough, or scaled up if the image
7065 * size is less than 3/4 of the maximum sign size. With GTK+ 1, the pixmap
7066 * will be cut off if the current font is not big enough, or centered if it's
7069 # define SIGN_WIDTH (2 * gui.char_width)
7070 # define SIGN_HEIGHT (gui.char_height)
7071 # define SIGN_ASPECT ((double)SIGN_HEIGHT / (double)SIGN_WIDTH)
7076 gui_mch_drawsign(int row
, int col
, int typenr
)
7080 sign
= (GdkPixbuf
*)sign_get_image(typenr
);
7082 if (sign
!= NULL
&& gui
.drawarea
!= NULL
&& gui
.drawarea
->window
!= NULL
)
7090 width
= gdk_pixbuf_get_width(sign
);
7091 height
= gdk_pixbuf_get_height(sign
);
7093 * Decide whether we need to scale. Allow one pixel of border
7094 * width to be cut off, in order to avoid excessive scaling for
7095 * tiny differences in font size.
7097 need_scale
= (width
> SIGN_WIDTH
+ 2
7098 || height
> SIGN_HEIGHT
+ 2
7099 || (width
< 3 * SIGN_WIDTH
/ 4
7100 && height
< 3 * SIGN_HEIGHT
/ 4));
7105 /* Keep the original aspect ratio */
7106 aspect
= (double)height
/ (double)width
;
7107 width
= (double)SIGN_WIDTH
* SIGN_ASPECT
/ aspect
;
7108 width
= MIN(width
, SIGN_WIDTH
);
7109 height
= (double)width
* aspect
;
7111 /* This doesn't seem to be worth caching, and doing so
7112 * would complicate the code quite a bit. */
7113 sign
= gdk_pixbuf_scale_simple(sign
, width
, height
,
7114 GDK_INTERP_BILINEAR
);
7116 return; /* out of memory */
7119 /* The origin is the upper-left corner of the pixmap. Therefore
7120 * these offset may become negative if the pixmap is smaller than
7121 * the 2x1 cells reserved for the sign icon. */
7122 xoffset
= (width
- SIGN_WIDTH
) / 2;
7123 yoffset
= (height
- SIGN_HEIGHT
) / 2;
7125 gdk_gc_set_foreground(gui
.text_gc
, gui
.bgcolor
);
7127 gdk_draw_rectangle(gui
.drawarea
->window
,
7135 # if GTK_CHECK_VERSION(2,1,1)
7136 gdk_draw_pixbuf(gui
.drawarea
->window
,
7141 FILL_X(col
) - MIN(0, xoffset
),
7142 FILL_Y(row
) - MIN(0, yoffset
),
7143 MIN(width
, SIGN_WIDTH
),
7144 MIN(height
, SIGN_HEIGHT
),
7145 GDK_RGB_DITHER_NORMAL
,
7148 gdk_pixbuf_render_to_drawable_alpha(sign
,
7149 gui
.drawarea
->window
,
7152 FILL_X(col
) - MIN(0, xoffset
),
7153 FILL_Y(row
) - MIN(0, yoffset
),
7154 MIN(width
, SIGN_WIDTH
),
7155 MIN(height
, SIGN_HEIGHT
),
7156 GDK_PIXBUF_ALPHA_BILEVEL
,
7158 GDK_RGB_DITHER_NORMAL
,
7162 g_object_unref(sign
);
7167 gui_mch_register_sign(char_u
*signfile
)
7169 if (signfile
[0] != NUL
&& signfile
[0] != '-' && gui
.in_use
)
7172 GError
*error
= NULL
;
7175 sign
= gdk_pixbuf_new_from_file((const char *)signfile
, &error
);
7180 message
= (char_u
*)error
->message
;
7182 if (message
!= NULL
&& input_conv
.vc_type
!= CONV_NONE
)
7183 message
= string_convert(&input_conv
, message
, NULL
);
7185 if (message
!= NULL
)
7187 /* The error message is already translated and will be more
7188 * descriptive than anything we could possibly do ourselves. */
7189 EMSG2("E255: %s", message
);
7191 if (input_conv
.vc_type
!= CONV_NONE
)
7194 g_error_free(error
);
7201 gui_mch_destroy_sign(void *sign
)
7204 g_object_unref(sign
);
7207 # else /* !HAVE_GTK2 */
7217 gui_mch_drawsign(int row
, int col
, int typenr
)
7221 sign
= (signicon_T
*)sign_get_image(typenr
);
7223 if (sign
!= NULL
&& sign
->pixmap
!= NULL
7224 && gui
.drawarea
!= NULL
&& gui
.drawarea
->window
!= NULL
)
7231 gdk_window_get_size(sign
->pixmap
, &width
, &height
);
7233 /* The origin is the upper-left corner of the pixmap. Therefore
7234 * these offset may become negative if the pixmap is smaller than
7235 * the 2x1 cells reserved for the sign icon. */
7236 xoffset
= (width
- SIGN_WIDTH
) / 2;
7237 yoffset
= (height
- SIGN_HEIGHT
) / 2;
7239 gdk_gc_set_foreground(gui
.text_gc
, gui
.bgcolor
);
7241 gdk_draw_rectangle(gui
.drawarea
->window
,
7249 /* Set the clip mask for bilevel transparency */
7250 if (sign
->mask
!= NULL
)
7252 gdk_gc_set_clip_origin(gui
.text_gc
,
7253 FILL_X(col
) - xoffset
,
7254 FILL_Y(row
) - yoffset
);
7255 gdk_gc_set_clip_mask(gui
.text_gc
, sign
->mask
);
7258 gdk_draw_pixmap(gui
.drawarea
->window
,
7263 FILL_X(col
) - MIN(0, xoffset
),
7264 FILL_Y(row
) - MIN(0, yoffset
),
7265 MIN(width
, SIGN_WIDTH
),
7266 MIN(height
, SIGN_HEIGHT
));
7268 gdk_gc_set_clip_mask(gui
.text_gc
, NULL
);
7273 gui_mch_register_sign(char_u
*signfile
)
7275 signicon_T
*sign
= NULL
;
7277 if (signfile
[0] != NUL
&& signfile
[0] != '-'
7278 && gui
.drawarea
!= NULL
&& gui
.drawarea
->window
!= NULL
)
7280 sign
= (signicon_T
*)alloc(sizeof(signicon_T
));
7282 if (sign
!= NULL
) /* NULL == OOM == "cannot really happen" */
7285 sign
->pixmap
= gdk_pixmap_colormap_create_from_xpm(
7286 gui
.drawarea
->window
, NULL
,
7288 (const char *)signfile
);
7290 if (sign
->pixmap
== NULL
)
7294 EMSG(_(e_signdata
));
7302 gui_mch_destroy_sign(void *sign
)
7306 signicon_T
*signicon
= (signicon_T
*)sign
;
7308 if (signicon
->pixmap
!= NULL
)
7309 gdk_pixmap_unref(signicon
->pixmap
);
7310 if (signicon
->mask
!= NULL
)
7311 gdk_bitmap_unref(signicon
->mask
);
7316 # endif /* !HAVE_GTK2 */
7318 #endif /* FEAT_SIGN_ICONS */