Merge remote branch 'svn-vim' into vim
[MacVim.git] / src / gui_gtk.c
blob0535e204f7ad9c90745448d078c1fc75542f3f9f
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.
8 */
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>
23 * Best supporting actor (He helped somewhat, aesthetically speaking):
24 * Maxime Romano <verbophobe@hotmail.com>
27 #ifdef FEAT_GUI_GTK
28 # include "gui_gtk_f.h"
29 #endif
31 /* GTK defines MAX and MIN, but some system header files as well. Undefine
32 * them and don't use them. */
33 #ifdef MIN
34 # undef MIN
35 #endif
36 #ifdef MAX
37 # undef MAX
38 #endif
40 #include "vim.h"
42 #ifdef FEAT_GUI_GNOME
43 /* Gnome redefines _() and N_(). Grrr... */
44 # ifdef _
45 # undef _
46 # endif
47 # ifdef N_
48 # undef N_
49 # endif
50 # ifdef textdomain
51 # undef textdomain
52 # endif
53 # ifdef bindtextdomain
54 # undef bindtextdomain
55 # endif
56 # ifdef bind_textdomain_codeset
57 # undef bind_textdomain_codeset
58 # endif
59 # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
60 # define ENABLE_NLS /* so the texts in the dialog boxes are translated */
61 # endif
62 # include <gnome.h>
63 #endif
65 #if defined(FEAT_GUI_DIALOG) && !defined(HAVE_GTK2)
66 # include "../pixmaps/alert.xpm"
67 # include "../pixmaps/error.xpm"
68 # include "../pixmaps/generic.xpm"
69 # include "../pixmaps/info.xpm"
70 # include "../pixmaps/quest.xpm"
71 #endif
73 #if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
75 * Icons used by the toolbar code.
77 #include "../pixmaps/tb_new.xpm"
78 #include "../pixmaps/tb_open.xpm"
79 #include "../pixmaps/tb_close.xpm"
80 #include "../pixmaps/tb_save.xpm"
81 #include "../pixmaps/tb_print.xpm"
82 #include "../pixmaps/tb_cut.xpm"
83 #include "../pixmaps/tb_copy.xpm"
84 #include "../pixmaps/tb_paste.xpm"
85 #include "../pixmaps/tb_find.xpm"
86 #include "../pixmaps/tb_find_next.xpm"
87 #include "../pixmaps/tb_find_prev.xpm"
88 #include "../pixmaps/tb_find_help.xpm"
89 #include "../pixmaps/tb_exit.xpm"
90 #include "../pixmaps/tb_undo.xpm"
91 #include "../pixmaps/tb_redo.xpm"
92 #include "../pixmaps/tb_help.xpm"
93 #include "../pixmaps/tb_macro.xpm"
94 #include "../pixmaps/tb_make.xpm"
95 #include "../pixmaps/tb_save_all.xpm"
96 #include "../pixmaps/tb_jump.xpm"
97 #include "../pixmaps/tb_ctags.xpm"
98 #include "../pixmaps/tb_load_session.xpm"
99 #include "../pixmaps/tb_save_session.xpm"
100 #include "../pixmaps/tb_new_session.xpm"
101 #include "../pixmaps/tb_blank.xpm"
102 #include "../pixmaps/tb_maximize.xpm"
103 #include "../pixmaps/tb_split.xpm"
104 #include "../pixmaps/tb_minimize.xpm"
105 #include "../pixmaps/tb_shell.xpm"
106 #include "../pixmaps/tb_replace.xpm"
107 #include "../pixmaps/tb_vsplit.xpm"
108 #include "../pixmaps/tb_maxwidth.xpm"
109 #include "../pixmaps/tb_minwidth.xpm"
110 #endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
112 #ifdef FEAT_GUI_GTK
113 # include <gdk/gdkkeysyms.h>
114 # include <gdk/gdk.h>
115 # ifdef WIN3264
116 # include <gdk/gdkwin32.h>
117 # else
118 # include <gdk/gdkx.h>
119 # endif
121 # include <gtk/gtk.h>
122 #else
123 /* define these items to be able to generate prototypes without GTK */
124 typedef int GtkWidget;
125 # define gpointer int
126 # define guint8 int
127 # define GdkPixmap int
128 # define GdkBitmap int
129 # define GtkIconFactory int
130 # define GtkToolbar int
131 # define GtkAdjustment int
132 # define gboolean int
133 # define GdkEventKey int
134 # define CancelData int
135 #endif
137 static void entry_activate_cb(GtkWidget *widget, gpointer data);
138 static void entry_changed_cb(GtkWidget *entry, GtkWidget *dialog);
139 static void find_replace_cb(GtkWidget *widget, gpointer data);
140 #ifndef HAVE_GTK2
141 static void gui_gtk_position_in_parent(GtkWidget *parent, GtkWidget *child, gui_win_pos_T where);
142 #endif
144 #if defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)
146 * Table from BuiltIn## icon indices to GTK+ stock IDs. Order must exactly
147 * match toolbar_names[] in menu.c! All stock icons including the "vim-*"
148 * ones can be overridden in your gtkrc file.
150 static const char * const menu_stock_ids[] =
152 /* 00 */ GTK_STOCK_NEW,
153 /* 01 */ GTK_STOCK_OPEN,
154 /* 02 */ GTK_STOCK_SAVE,
155 /* 03 */ GTK_STOCK_UNDO,
156 /* 04 */ GTK_STOCK_REDO,
157 /* 05 */ GTK_STOCK_CUT,
158 /* 06 */ GTK_STOCK_COPY,
159 /* 07 */ GTK_STOCK_PASTE,
160 /* 08 */ GTK_STOCK_PRINT,
161 /* 09 */ GTK_STOCK_HELP,
162 /* 10 */ GTK_STOCK_FIND,
163 /* 11 */ "vim-save-all",
164 /* 12 */ "vim-session-save",
165 /* 13 */ "vim-session-new",
166 /* 14 */ "vim-session-load",
167 /* 15 */ GTK_STOCK_EXECUTE,
168 /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
169 /* 17 */ GTK_STOCK_CLOSE, /* FIXME: fuzzy */
170 /* 18 */ "vim-window-maximize",
171 /* 19 */ "vim-window-minimize",
172 /* 20 */ "vim-window-split",
173 /* 21 */ "vim-shell",
174 /* 22 */ GTK_STOCK_GO_BACK,
175 /* 23 */ GTK_STOCK_GO_FORWARD,
176 /* 24 */ "vim-find-help",
177 /* 25 */ GTK_STOCK_CONVERT,
178 /* 26 */ GTK_STOCK_JUMP_TO,
179 /* 27 */ "vim-build-tags",
180 /* 28 */ "vim-window-split-vertical",
181 /* 29 */ "vim-window-maximize-width",
182 /* 30 */ "vim-window-minimize-width",
183 /* 31 */ GTK_STOCK_QUIT
186 static void
187 add_stock_icon(GtkIconFactory *factory,
188 const char *stock_id,
189 const guint8 *inline_data,
190 int data_length)
192 GdkPixbuf *pixbuf;
193 GtkIconSet *icon_set;
195 pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
196 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
198 gtk_icon_factory_add(factory, stock_id, icon_set);
200 gtk_icon_set_unref(icon_set);
201 g_object_unref(pixbuf);
204 static int
205 lookup_menu_iconfile(char_u *iconfile, char_u *dest)
207 expand_env(iconfile, dest, MAXPATHL);
209 if (mch_isFullName(dest))
211 return vim_fexists(dest);
213 else
215 static const char suffixes[][4] = {"png", "xpm", "bmp"};
216 char_u buf[MAXPATHL];
217 unsigned int i;
219 for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
220 if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
222 STRCPY(dest, buf);
223 return TRUE;
226 return FALSE;
230 static GtkWidget *
231 load_menu_iconfile(char_u *name, GtkIconSize icon_size)
233 GtkWidget *image = NULL;
234 GtkIconSet *icon_set;
235 GtkIconSource *icon_source;
238 * Rather than loading the icon directly into a GtkImage, create
239 * a new GtkIconSet and put it in there. This way we can easily
240 * scale the toolbar icons on the fly when needed.
242 icon_set = gtk_icon_set_new();
243 icon_source = gtk_icon_source_new();
245 gtk_icon_source_set_filename(icon_source, (const char *)name);
246 gtk_icon_set_add_source(icon_set, icon_source);
248 image = gtk_image_new_from_icon_set(icon_set, icon_size);
250 gtk_icon_source_free(icon_source);
251 gtk_icon_set_unref(icon_set);
253 return image;
256 static GtkWidget *
257 create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
259 GtkWidget *image = NULL;
260 char_u buf[MAXPATHL];
262 /* First use a specified "icon=" argument. */
263 if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
264 image = load_menu_iconfile(buf, icon_size);
266 /* If not found and not builtin specified try using the menu name. */
267 if (image == NULL && !menu->icon_builtin
268 && lookup_menu_iconfile(menu->name, buf))
269 image = load_menu_iconfile(buf, icon_size);
271 /* Still not found? Then use a builtin icon, a blank one as fallback. */
272 if (image == NULL)
274 const char *stock_id;
275 const int n_ids = G_N_ELEMENTS(menu_stock_ids);
277 if (menu->iconidx >= 0 && menu->iconidx < n_ids)
278 stock_id = menu_stock_ids[menu->iconidx];
279 else
280 stock_id = GTK_STOCK_MISSING_IMAGE;
282 image = gtk_image_new_from_stock(stock_id, icon_size);
285 return image;
288 static gint
289 toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
290 GdkEventFocus *event UNUSED,
291 gpointer data UNUSED)
293 /* When we're in a GtkPlug, we don't have window focus events, only widget
294 * focus. To emulate stand-alone gvim, if a button gets focus (e.g.,
295 * <Tab> into GtkPlug) immediately pass it to mainwin. */
296 if (gtk_socket_id != 0)
297 gtk_widget_grab_focus(gui.drawarea);
299 return TRUE;
301 #endif /* FEAT_TOOLBAR && HAVE_GTK2 */
303 #if (defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)) || defined(PROTO)
305 void
306 gui_gtk_register_stock_icons(void)
308 # include "../pixmaps/stock_icons.h"
309 GtkIconFactory *factory;
311 factory = gtk_icon_factory_new();
312 # define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
314 ADD_ICON("vim-build-tags", stock_vim_build_tags);
315 ADD_ICON("vim-find-help", stock_vim_find_help);
316 ADD_ICON("vim-save-all", stock_vim_save_all);
317 ADD_ICON("vim-session-load", stock_vim_session_load);
318 ADD_ICON("vim-session-new", stock_vim_session_new);
319 ADD_ICON("vim-session-save", stock_vim_session_save);
320 ADD_ICON("vim-shell", stock_vim_shell);
321 ADD_ICON("vim-window-maximize", stock_vim_window_maximize);
322 ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
323 ADD_ICON("vim-window-minimize", stock_vim_window_minimize);
324 ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
325 ADD_ICON("vim-window-split", stock_vim_window_split);
326 ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
328 # undef ADD_ICON
329 gtk_icon_factory_add_default(factory);
330 g_object_unref(factory);
333 #endif /* FEAT_TOOLBAR && HAVE_GTK2 */
337 * Only use accelerators when gtk_menu_ensure_uline_accel_group() is
338 * available, which is in version 1.2.1. That was the first version where
339 * accelerators properly worked (according to the change log).
341 #ifdef GTK_CHECK_VERSION
342 # if GTK_CHECK_VERSION(1, 2, 1)
343 # define GTK_USE_ACCEL
344 # endif
345 #endif
347 #if defined(FEAT_MENU) || defined(PROTO)
350 * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
351 * if necessary. The caller must vim_free() the returned string.
353 * Input Output
354 * _ __
355 * && &
356 * & _ stripped if use_mnemonic == FALSE
357 * <Tab> end of menu label text
359 static char_u *
360 translate_mnemonic_tag(char_u *name, int use_mnemonic)
362 char_u *buf;
363 char_u *psrc;
364 char_u *pdest;
365 int n_underscores = 0;
367 # ifdef HAVE_GTK2
368 name = CONVERT_TO_UTF8(name);
369 # endif
370 if (name == NULL)
371 return NULL;
373 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
374 if (*psrc == '_')
375 ++n_underscores;
377 buf = alloc((unsigned)(psrc - name + n_underscores + 1));
378 if (buf != NULL)
380 pdest = buf;
381 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
383 if (*psrc == '_')
385 *pdest++ = '_';
386 *pdest++ = '_';
388 else if (*psrc != '&')
390 *pdest++ = *psrc;
392 else if (*(psrc + 1) == '&')
394 *pdest++ = *psrc++;
396 else if (use_mnemonic)
398 *pdest++ = '_';
401 *pdest = NUL;
404 # ifdef HAVE_GTK2
405 CONVERT_TO_UTF8_FREE(name);
406 # endif
407 return buf;
410 # ifdef HAVE_GTK2
412 static void
413 menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
415 GtkWidget *box;
416 char_u *text;
417 int use_mnemonic;
419 /* It would be neat to have image menu items, but that would require major
420 * changes to Vim's menu system. Not to mention that all the translations
421 * had to be updated. */
422 menu->id = gtk_menu_item_new();
423 box = gtk_hbox_new(FALSE, 20);
425 use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
426 text = translate_mnemonic_tag(menu->name, use_mnemonic);
428 menu->label = gtk_label_new_with_mnemonic((const char *)text);
429 vim_free(text);
431 gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
433 if (menu->actext != NULL && menu->actext[0] != NUL)
435 text = CONVERT_TO_UTF8(menu->actext);
437 gtk_box_pack_end(GTK_BOX(box),
438 gtk_label_new((const char *)text),
439 FALSE, FALSE, 0);
441 CONVERT_TO_UTF8_FREE(text);
444 gtk_container_add(GTK_CONTAINER(menu->id), box);
445 gtk_widget_show_all(menu->id);
448 # else /* !HAVE_GTK2 */
451 * Create a highly customized menu item by hand instead of by using:
453 * gtk_menu_item_new_with_label(menu->dname);
455 * This is necessary, since there is no other way in GTK+ 1 to get the
456 * not automatically parsed accelerator stuff right.
458 static void
459 menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
461 GtkWidget *widget;
462 GtkWidget *bin;
463 GtkWidget *label;
464 char_u *name;
465 guint accel_key;
467 widget = gtk_widget_new(GTK_TYPE_MENU_ITEM,
468 "GtkWidget::visible", TRUE,
469 "GtkWidget::sensitive", TRUE,
470 /* "GtkWidget::parent", parent->submenu_id, */
471 NULL);
472 bin = gtk_widget_new(GTK_TYPE_HBOX,
473 "GtkWidget::visible", TRUE,
474 "GtkWidget::parent", widget,
475 "GtkBox::spacing", 16,
476 NULL);
477 label = gtk_widget_new(GTK_TYPE_ACCEL_LABEL,
478 "GtkWidget::visible", TRUE,
479 "GtkWidget::parent", bin,
480 "GtkAccelLabel::accel_widget", widget,
481 "GtkMisc::xalign", 0.0,
482 NULL);
483 menu->label = label;
485 if (menu->actext)
486 gtk_widget_new(GTK_TYPE_LABEL,
487 "GtkWidget::visible", TRUE,
488 "GtkWidget::parent", bin,
489 "GtkLabel::label", menu->actext,
490 "GtkMisc::xalign", 1.0,
491 NULL);
494 * Translate VIM accelerator tagging into GTK+'s. Note that since GTK uses
495 * underscores as the accelerator key, we need to add an additional under-
496 * score for each understore that appears in the menu name.
498 # ifdef GTK_USE_ACCEL
499 name = translate_mnemonic_tag(menu->name,
500 (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget)));
501 # else
502 name = translate_mnemonic_tag(menu->name, FALSE);
503 # endif
505 /* let GTK do its thing */
506 accel_key = gtk_label_parse_uline(GTK_LABEL(label), (const char *)name);
507 vim_free(name);
509 # ifdef GTK_USE_ACCEL
510 /* Don't add accelator if 'winaltkeys' is "no". */
511 if (accel_key != GDK_VoidSymbol)
513 if (GTK_IS_MENU_BAR(parent_widget))
515 if (*p_wak != 'n')
516 gtk_widget_add_accelerator(widget,
517 "activate_item",
518 gui.accel_group,
519 accel_key, GDK_MOD1_MASK,
520 (GtkAccelFlags)0);
522 else
524 gtk_widget_add_accelerator(widget,
525 "activate_item",
526 gtk_menu_ensure_uline_accel_group(GTK_MENU(parent_widget)),
527 accel_key, 0,
528 (GtkAccelFlags)0);
531 # endif /* GTK_USE_ACCEL */
533 menu->id = widget;
536 # endif /* !HAVE_GTK2 */
538 void
539 gui_mch_add_menu(vimmenu_T *menu, int idx)
541 vimmenu_T *parent;
542 GtkWidget *parent_widget;
544 if (menu->name[0] == ']' || menu_is_popup(menu->name))
546 menu->submenu_id = gtk_menu_new();
547 return;
550 parent = menu->parent;
552 if ((parent != NULL && parent->submenu_id == NULL)
553 || !menu_is_menubar(menu->name))
554 return;
556 parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
557 menu_item_new(menu, parent_widget);
559 /* since the tearoff should always appear first, increment idx */
560 if (parent != NULL && !menu_is_popup(parent->name))
561 ++idx;
563 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
565 #ifndef HAVE_GTK2
567 * The "Help" menu is a special case, and should be placed at the far
568 * right hand side of the menu-bar. It's detected by its high priority.
570 * Right-aligning "Help" is considered bad UI design nowadays.
571 * Thus lets disable this for GTK+ 2 to match the environment.
573 if (parent == NULL && menu->priority >= 9999)
574 gtk_menu_item_right_justify(GTK_MENU_ITEM(menu->id));
575 #endif
577 menu->submenu_id = gtk_menu_new();
579 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
580 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
582 menu->tearoff_handle = gtk_tearoff_menu_item_new();
583 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
584 gtk_widget_show(menu->tearoff_handle);
585 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
588 static void
589 menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
591 gui_menu_cb((vimmenu_T *)data);
593 # ifndef HAVE_GTK2
594 /* Work around a bug in GTK+ 1: we don't seem to get a focus-in
595 * event after clicking a menu item shown via :popup. */
596 if (!gui.in_focus)
597 gui_focus_change(TRUE);
598 # endif
600 /* make sure the menu action is taken immediately */
601 if (gtk_main_level() > 0)
602 gtk_main_quit();
605 # if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
607 * These are the pixmaps used for the default buttons.
608 * Order must exactly match toolbar_names[] in menu.c!
610 static char **(built_in_pixmaps[]) =
612 tb_new_xpm,
613 tb_open_xpm,
614 tb_save_xpm,
615 tb_undo_xpm,
616 tb_redo_xpm,
617 tb_cut_xpm,
618 tb_copy_xpm,
619 tb_paste_xpm,
620 tb_print_xpm,
621 tb_help_xpm,
622 tb_find_xpm,
623 tb_save_all_xpm,
624 tb_save_session_xpm,
625 tb_new_session_xpm,
626 tb_load_session_xpm,
627 tb_macro_xpm,
628 tb_replace_xpm,
629 tb_close_xpm,
630 tb_maximize_xpm,
631 tb_minimize_xpm,
632 tb_split_xpm,
633 tb_shell_xpm,
634 tb_find_prev_xpm,
635 tb_find_next_xpm,
636 tb_find_help_xpm,
637 tb_make_xpm,
638 tb_jump_xpm,
639 tb_ctags_xpm,
640 tb_vsplit_xpm,
641 tb_maxwidth_xpm,
642 tb_minwidth_xpm,
643 tb_exit_xpm
647 * creates a blank pixmap using tb_blank
649 static void
650 pixmap_create_from_xpm(char **xpm, GdkPixmap **pixmap, GdkBitmap **mask)
652 *pixmap = gdk_pixmap_colormap_create_from_xpm_d(
653 NULL,
654 gtk_widget_get_colormap(gui.mainwin),
655 mask,
656 NULL,
657 xpm);
661 * creates a pixmap by using a built-in number
663 static void
664 pixmap_create_by_num(int pixmap_num, GdkPixmap **pixmap, GdkBitmap **mask)
666 if (pixmap_num >= 0 && pixmap_num < (sizeof(built_in_pixmaps)
667 / sizeof(built_in_pixmaps[0])))
668 pixmap_create_from_xpm(built_in_pixmaps[pixmap_num], pixmap, mask);
672 * Creates a pixmap by using the pixmap "name" found in 'runtimepath'/bitmaps/
674 static void
675 pixmap_create_by_dir(char_u *name, GdkPixmap **pixmap, GdkBitmap **mask)
677 char_u full_pathname[MAXPATHL + 1];
679 if (gui_find_bitmap(name, full_pathname, "xpm") == OK)
680 *pixmap = gdk_pixmap_colormap_create_from_xpm(
681 NULL,
682 gtk_widget_get_colormap(gui.mainwin),
683 mask,
684 &gui.mainwin->style->bg[GTK_STATE_NORMAL],
685 (const char *)full_pathname);
689 * Creates a pixmap by using the pixmap "fname".
691 static void
692 pixmap_create_from_file(char_u *fname, GdkPixmap **pixmap, GdkBitmap **mask)
694 *pixmap = gdk_pixmap_colormap_create_from_xpm(
695 NULL,
696 gtk_widget_get_colormap(gui.mainwin),
697 mask,
698 &gui.mainwin->style->bg[GTK_STATE_NORMAL],
699 (const char *)fname);
702 # endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
704 void
705 gui_mch_add_menu_item(vimmenu_T *menu, int idx)
707 vimmenu_T *parent;
709 parent = menu->parent;
711 # ifdef FEAT_TOOLBAR
712 if (menu_is_toolbar(parent->name))
714 GtkToolbar *toolbar;
716 toolbar = GTK_TOOLBAR(gui.toolbar);
717 menu->submenu_id = NULL;
719 if (menu_is_separator(menu->name))
721 gtk_toolbar_insert_space(toolbar, idx);
722 menu->id = NULL;
724 else
726 # ifdef HAVE_GTK2
727 char_u *text;
728 char_u *tooltip;
730 text = CONVERT_TO_UTF8(menu->dname);
731 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
732 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
733 /* Invalid text, can happen when 'encoding' is changed. Avoid
734 * a nasty GTK error message, skip the tooltip. */
735 CONVERT_TO_UTF8_FREE(tooltip);
737 menu->id = gtk_toolbar_insert_item(
738 toolbar,
739 (const char *)text,
740 (const char *)tooltip,
741 NULL,
742 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
743 G_CALLBACK(&menu_item_activate),
744 menu,
745 idx);
747 if (gtk_socket_id != 0)
748 gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
749 GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
751 CONVERT_TO_UTF8_FREE(text);
752 CONVERT_TO_UTF8_FREE(tooltip);
754 # else /* !HAVE_GTK2 */
756 GdkPixmap *pixmap = NULL;
757 GdkBitmap *mask = NULL;
759 /* First try user specified bitmap, then builtin, the a blank. */
760 if (menu->iconfile != NULL)
762 char_u buf[MAXPATHL + 1];
764 gui_find_iconfile(menu->iconfile, buf, "xpm");
765 pixmap_create_from_file(buf, &pixmap, &mask);
767 if (pixmap == NULL && !menu->icon_builtin)
768 pixmap_create_by_dir(menu->name, &pixmap, &mask);
769 if (pixmap == NULL && menu->iconidx >= 0)
770 pixmap_create_by_num(menu->iconidx, &pixmap, &mask);
771 if (pixmap == NULL)
772 pixmap_create_from_xpm(tb_blank_xpm, &pixmap, &mask);
773 if (pixmap == NULL)
774 return; /* should at least have blank pixmap, but if not... */
776 menu->id = gtk_toolbar_insert_item(
777 toolbar,
778 (char *)(menu->dname),
779 (char *)(menu->strings[MENU_INDEX_TIP]),
780 (char *)(menu->dname),
781 gtk_pixmap_new(pixmap, mask),
782 GTK_SIGNAL_FUNC(menu_item_activate),
783 (gpointer)menu,
784 idx);
785 # endif /* !HAVE_GTK2 */
788 else
789 # endif /* FEAT_TOOLBAR */
791 /* No parent, must be a non-menubar menu */
792 if (parent == NULL || parent->submenu_id == NULL)
793 return;
795 /* Make place for the possible tearoff handle item. Not in the popup
796 * menu, it doesn't have a tearoff item. */
797 if (!menu_is_popup(parent->name))
798 ++idx;
800 if (menu_is_separator(menu->name))
802 /* Separator: Just add it */
803 menu->id = gtk_menu_item_new();
804 gtk_widget_set_sensitive(menu->id, FALSE);
805 gtk_widget_show(menu->id);
806 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
808 return;
811 /* Add textual menu item. */
812 menu_item_new(menu, parent->submenu_id);
813 gtk_widget_show(menu->id);
814 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
816 if (menu->id != NULL)
817 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
818 GTK_SIGNAL_FUNC(menu_item_activate), menu);
821 #endif /* FEAT_MENU */
824 void
825 gui_mch_set_text_area_pos(int x, int y, int w, int h)
827 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
831 #if defined(FEAT_MENU) || defined(PROTO)
833 * Enable or disable accelerators for the toplevel menus.
835 void
836 gui_gtk_set_mnemonics(int enable)
838 vimmenu_T *menu;
839 char_u *name;
840 # if !defined(HAVE_GTK2) && defined(GTK_USE_ACCEL)
841 guint accel_key;
842 # endif
844 for (menu = root_menu; menu != NULL; menu = menu->next)
846 if (menu->id == NULL)
847 continue;
849 # if defined(HAVE_GTK2)
850 name = translate_mnemonic_tag(menu->name, enable);
851 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
852 (const char *)name);
853 vim_free(name);
854 # else
855 # if defined(GTK_USE_ACCEL)
856 name = translate_mnemonic_tag(menu->name, TRUE);
857 if (name != NULL)
859 accel_key = gtk_label_parse_uline(GTK_LABEL(menu->label),
860 (const char *)name);
861 if (accel_key != GDK_VoidSymbol)
862 gtk_widget_remove_accelerator(menu->id, gui.accel_group,
863 accel_key, GDK_MOD1_MASK);
864 if (enable && accel_key != GDK_VoidSymbol)
865 gtk_widget_add_accelerator(menu->id, "activate_item",
866 gui.accel_group,
867 accel_key, GDK_MOD1_MASK,
868 (GtkAccelFlags)0);
869 vim_free(name);
871 if (!enable)
873 name = translate_mnemonic_tag(menu->name, FALSE);
874 gtk_label_parse_uline(GTK_LABEL(menu->label), (const char *)name);
875 vim_free(name);
877 # endif
878 # endif
882 static void
883 recurse_tearoffs(vimmenu_T *menu, int val)
885 for (; menu != NULL; menu = menu->next)
887 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
888 && menu->name[0] != ']' && !menu_is_popup(menu->name))
890 if (val)
891 gtk_widget_show(menu->tearoff_handle);
892 else
893 gtk_widget_hide(menu->tearoff_handle);
895 recurse_tearoffs(menu->children, val);
899 void
900 gui_mch_toggle_tearoffs(int enable)
902 recurse_tearoffs(root_menu, enable);
904 #endif /* FEAT_MENU */
907 #if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
909 * Seems like there's a hole in the GTK Toolbar API: there's no provision for
910 * removing an item from the toolbar. Therefore I need to resort to going
911 * really deeply into the internal widget structures.
913 * <danielk> I'm not sure the statement above is true -- at least with
914 * GTK+ 2 one can just call gtk_widget_destroy() and be done with it.
915 * It is true though that you couldn't remove space items before GTK+ 2
916 * (without digging into the internals that is). But the code below
917 * doesn't seem to handle those either. Well, it's obsolete anyway.
919 static void
920 toolbar_remove_item_by_text(GtkToolbar *tb, const char *text)
922 GtkContainer *container;
923 GList *childl;
924 GtkToolbarChild *gtbc;
926 g_return_if_fail(tb != NULL);
927 g_return_if_fail(GTK_IS_TOOLBAR(tb));
928 container = GTK_CONTAINER(&tb->container);
930 for (childl = tb->children; childl; childl = childl->next)
932 gtbc = (GtkToolbarChild *)childl->data;
934 if (gtbc->type != GTK_TOOLBAR_CHILD_SPACE
935 && strcmp(GTK_LABEL(gtbc->label)->label, text) == 0)
937 gboolean was_visible;
939 was_visible = GTK_WIDGET_VISIBLE(gtbc->widget);
940 gtk_widget_unparent(gtbc->widget);
942 tb->children = g_list_remove_link(tb->children, childl);
943 g_free(gtbc);
944 g_list_free(childl);
945 tb->num_children--;
947 if (was_visible && GTK_WIDGET_VISIBLE(container))
948 gtk_widget_queue_resize(GTK_WIDGET(container));
950 break;
954 #endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
957 #if defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)
958 static int
959 get_menu_position(vimmenu_T *menu)
961 vimmenu_T *node;
962 int idx = 0;
964 for (node = menu->parent->children; node != menu; node = node->next)
966 g_return_val_if_fail(node != NULL, -1);
967 ++idx;
970 return idx;
972 #endif /* FEAT_TOOLBAR && HAVE_GTK2 */
975 #if defined(FEAT_TOOLBAR) || defined(PROTO)
976 void
977 gui_mch_menu_set_tip(vimmenu_T *menu)
979 if (menu->id != NULL && menu->parent != NULL
980 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
982 char_u *tooltip;
984 # ifdef HAVE_GTK2
985 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
986 if (tooltip == NULL || utf_valid_string(tooltip, NULL))
987 /* Only set the tooltip when it's valid utf-8. */
988 # else
989 tooltip = menu->strings[MENU_INDEX_TIP];
990 # endif
991 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
992 menu->id, (const char *)tooltip, NULL);
993 # ifdef HAVE_GTK2
994 CONVERT_TO_UTF8_FREE(tooltip);
995 # endif
998 #endif /* FEAT_TOOLBAR */
1001 #if defined(FEAT_MENU) || defined(PROTO)
1003 * Destroy the machine specific menu widget.
1005 void
1006 gui_mch_destroy_menu(vimmenu_T *menu)
1008 # ifdef FEAT_TOOLBAR
1009 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
1011 # ifdef HAVE_GTK2
1012 if (menu_is_separator(menu->name))
1013 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
1014 get_menu_position(menu));
1015 else if (menu->id != NULL)
1016 gtk_widget_destroy(menu->id);
1017 # else
1018 toolbar_remove_item_by_text(GTK_TOOLBAR(gui.toolbar),
1019 (const char *)menu->dname);
1020 # endif
1022 else
1023 # endif /* FEAT_TOOLBAR */
1025 if (menu->submenu_id != NULL)
1026 gtk_widget_destroy(menu->submenu_id);
1028 if (menu->id != NULL)
1029 gtk_widget_destroy(menu->id);
1032 menu->submenu_id = NULL;
1033 menu->id = NULL;
1035 #endif /* FEAT_MENU */
1039 * Scrollbar stuff.
1041 void
1042 gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
1044 if (sb->id != NULL)
1046 GtkAdjustment *adjustment;
1048 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1050 adjustment->lower = 0.0;
1051 adjustment->value = val;
1052 adjustment->upper = max + 1;
1053 adjustment->page_size = size;
1054 adjustment->page_increment = size < 3L ? 1L : size - 2L;
1055 adjustment->step_increment = 1.0;
1057 #ifdef HAVE_GTK2
1058 g_signal_handler_block(GTK_OBJECT(adjustment),
1059 (gulong)sb->handler_id);
1060 #else
1061 gtk_signal_handler_block(GTK_OBJECT(adjustment),
1062 (guint)sb->handler_id);
1063 #endif
1064 gtk_adjustment_changed(adjustment);
1065 #ifdef HAVE_GTK2
1066 g_signal_handler_unblock(GTK_OBJECT(adjustment),
1067 (gulong)sb->handler_id);
1068 #else
1069 gtk_signal_handler_unblock(GTK_OBJECT(adjustment),
1070 (guint)sb->handler_id);
1071 #endif
1075 void
1076 gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1078 if (sb->id != NULL)
1079 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
1083 * Take action upon scrollbar dragging.
1085 static void
1086 adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
1088 scrollbar_T *sb;
1089 long value;
1090 int dragging = FALSE;
1092 #ifdef FEAT_XIM
1093 /* cancel any preediting */
1094 if (im_is_preediting())
1095 xim_reset();
1096 #endif
1098 sb = gui_find_scrollbar((long)data);
1099 value = (long)adjustment->value;
1101 * The dragging argument must be right for the scrollbar to work with
1102 * closed folds. This isn't documented, hopefully this will keep on
1103 * working in later GTK versions.
1105 * FIXME: Well, it doesn't work in GTK2. :)
1106 * HACK: Get the mouse pointer position, if it appears to be on an arrow
1107 * button set "dragging" to FALSE. This assumes square buttons!
1109 if (sb != NULL)
1111 #ifdef HAVE_GTK2
1112 dragging = TRUE;
1114 if (sb->wp != NULL)
1116 int x;
1117 int y;
1118 GdkModifierType state;
1119 int width;
1120 int height;
1122 /* vertical scrollbar: need to set "dragging" properly in case
1123 * there are closed folds. */
1124 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
1125 gdk_window_get_size(sb->id->window, &width, &height);
1126 if (x >= 0 && x < width && y >= 0 && y < height)
1128 if (y < width)
1130 /* up arrow: move one (closed fold) line up */
1131 dragging = FALSE;
1132 value = sb->wp->w_topline - 2;
1134 else if (y > height - width)
1136 /* down arrow: move one (closed fold) line down */
1137 dragging = FALSE;
1138 value = sb->wp->w_topline;
1142 #else
1143 dragging = (GTK_RANGE(sb->id)->scroll_type == GTK_SCROLL_NONE);
1144 #endif
1147 gui_drag_scrollbar(sb, value, dragging);
1149 if (gtk_main_level() > 0)
1150 gtk_main_quit();
1153 /* SBAR_VERT or SBAR_HORIZ */
1154 void
1155 gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1157 if (orient == SBAR_HORIZ)
1158 sb->id = gtk_hscrollbar_new(NULL);
1159 else if (orient == SBAR_VERT)
1160 sb->id = gtk_vscrollbar_new(NULL);
1162 if (sb->id != NULL)
1164 GtkAdjustment *adjustment;
1166 GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
1167 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
1169 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1171 sb->handler_id = gtk_signal_connect(
1172 GTK_OBJECT(adjustment), "value_changed",
1173 GTK_SIGNAL_FUNC(adjustment_value_changed),
1174 GINT_TO_POINTER(sb->ident));
1175 gui_mch_update();
1179 #if defined(FEAT_WINDOWS) || defined(PROTO)
1180 void
1181 gui_mch_destroy_scrollbar(scrollbar_T *sb)
1183 if (sb->id != NULL)
1185 gtk_widget_destroy(sb->id);
1186 sb->id = NULL;
1188 gui_mch_update();
1190 #endif
1192 #if defined(FEAT_BROWSE) || defined(PROTO)
1194 * Implementation of the file selector related stuff
1196 #if defined(HAVE_GTK2) && GTK_CHECK_VERSION(2,4,0)
1197 /* This has been disabled, because the GTK library rewrites
1198 * ~/.recently-used.xbel every time the main loop is quit. For Vim that means
1199 * on just about any event. */
1200 /* # define USE_FILE_CHOOSER */
1201 #endif
1203 #ifndef USE_FILE_CHOOSER
1204 static void
1205 browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
1207 gui_T *vw = (gui_T *)cbdata;
1209 if (vw->browse_fname != NULL)
1210 g_free(vw->browse_fname);
1212 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
1213 GTK_FILE_SELECTION(vw->filedlg)));
1214 gtk_widget_hide(vw->filedlg);
1215 if (gtk_main_level() > 0)
1216 gtk_main_quit();
1219 static void
1220 browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
1222 gui_T *vw = (gui_T *)cbdata;
1224 if (vw->browse_fname != NULL)
1226 g_free(vw->browse_fname);
1227 vw->browse_fname = NULL;
1229 gtk_widget_hide(vw->filedlg);
1230 if (gtk_main_level() > 0)
1231 gtk_main_quit();
1234 static gboolean
1235 browse_destroy_cb(GtkWidget *widget UNUSED)
1237 if (gui.browse_fname != NULL)
1239 g_free(gui.browse_fname);
1240 gui.browse_fname = NULL;
1242 gui.filedlg = NULL;
1244 if (gtk_main_level() > 0)
1245 gtk_main_quit();
1247 return FALSE;
1249 #endif
1252 * Put up a file requester.
1253 * Returns the selected name in allocated memory, or NULL for Cancel.
1254 * saving, select file to write
1255 * title title for the window
1256 * dflt default name
1257 * ext not used (extension added)
1258 * initdir initial directory, NULL for current dir
1259 * filter not used (file name filter)
1261 char_u *
1262 gui_mch_browse(int saving UNUSED,
1263 char_u *title,
1264 char_u *dflt,
1265 char_u *ext UNUSED,
1266 char_u *initdir,
1267 char_u *filter UNUSED)
1269 #ifdef USE_FILE_CHOOSER
1270 GtkWidget *fc;
1271 #endif
1272 char_u dirbuf[MAXPATHL];
1274 # ifdef HAVE_GTK2
1275 title = CONVERT_TO_UTF8(title);
1276 # endif
1278 /* GTK has a bug, it only works with an absolute path. */
1279 if (initdir == NULL || *initdir == NUL)
1280 mch_dirname(dirbuf, MAXPATHL);
1281 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
1282 dirbuf[0] = NUL;
1283 /* Always need a trailing slash for a directory. */
1284 add_pathsep(dirbuf);
1286 /* If our pointer is currently hidden, then we should show it. */
1287 gui_mch_mousehide(FALSE);
1289 #ifdef USE_FILE_CHOOSER
1290 /* We create the dialog each time, so that the button text can be "Open"
1291 * or "Save" according to the action. */
1292 fc = gtk_file_chooser_dialog_new((const gchar *)title,
1293 GTK_WINDOW(gui.mainwin),
1294 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
1295 : GTK_FILE_CHOOSER_ACTION_OPEN,
1296 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1297 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1298 NULL);
1299 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
1300 (const gchar *)dirbuf);
1302 gui.browse_fname = NULL;
1303 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
1305 char *filename;
1307 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
1308 gui.browse_fname = (char_u *)g_strdup(filename);
1309 g_free(filename);
1311 gtk_widget_destroy(GTK_WIDGET(fc));
1313 #else
1315 if (gui.filedlg == NULL)
1317 GtkFileSelection *fs; /* shortcut */
1319 gui.filedlg = gtk_file_selection_new((const gchar *)title);
1320 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
1321 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
1322 GTK_WINDOW(gui.mainwin));
1323 fs = GTK_FILE_SELECTION(gui.filedlg);
1325 gtk_container_border_width(GTK_CONTAINER(fs), 4);
1327 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
1328 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
1329 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
1330 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
1331 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
1332 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
1333 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
1334 GTK_OBJECT(gui.filedlg));
1336 else
1337 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
1339 /* Concatenate "initdir" and "dflt". */
1340 if (dflt != NULL && *dflt != NUL
1341 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
1342 STRCAT(dirbuf, dflt);
1344 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
1345 (const gchar *)dirbuf);
1346 # ifndef HAVE_GTK2
1347 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1348 GTK_WIDGET(gui.filedlg), VW_POS_MOUSE);
1349 # endif
1351 gtk_widget_show(gui.filedlg);
1352 while (gui.filedlg && GTK_WIDGET_DRAWABLE(gui.filedlg))
1353 gtk_main_iteration_do(TRUE);
1354 #endif
1356 # ifdef HAVE_GTK2
1357 CONVERT_TO_UTF8_FREE(title);
1358 # endif
1359 if (gui.browse_fname == NULL)
1360 return NULL;
1362 /* shorten the file name if possible */
1363 return vim_strsave(shorten_fname1(gui.browse_fname));
1366 #if defined(HAVE_GTK2) || defined(PROTO)
1369 * Put up a directory selector
1370 * Returns the selected name in allocated memory, or NULL for Cancel.
1371 * title title for the window
1372 * dflt default name
1373 * initdir initial directory, NULL for current dir
1375 char_u *
1376 gui_mch_browsedir(
1377 char_u *title,
1378 char_u *initdir)
1380 # if defined(GTK_FILE_CHOOSER) /* Only in GTK 2.4 and later. */
1381 char_u dirbuf[MAXPATHL];
1382 char_u *p;
1383 GtkWidget *dirdlg; /* file selection dialog */
1384 char_u *dirname = NULL;
1386 title = CONVERT_TO_UTF8(title);
1388 dirdlg = gtk_file_chooser_dialog_new(
1389 (const gchar *)title,
1390 GTK_WINDOW(gui.mainwin),
1391 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1392 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1393 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1394 NULL);
1396 CONVERT_TO_UTF8_FREE(title);
1398 /* if our pointer is currently hidden, then we should show it. */
1399 gui_mch_mousehide(FALSE);
1401 /* GTK appears to insist on an absolute path. */
1402 if (initdir == NULL || *initdir == NUL
1403 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
1404 mch_dirname(dirbuf, MAXPATHL - 10);
1406 /* Always need a trailing slash for a directory.
1407 * Also add a dummy file name, so that we get to the directory. */
1408 add_pathsep(dirbuf);
1409 STRCAT(dirbuf, "@zd(*&1|");
1410 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
1411 (const gchar *)dirbuf);
1413 /* Run the dialog. */
1414 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
1415 dirname = (char_u *)gtk_file_chooser_get_filename(
1416 GTK_FILE_CHOOSER(dirdlg));
1417 gtk_widget_destroy(dirdlg);
1418 if (dirname == NULL)
1419 return NULL;
1421 /* shorten the file name if possible */
1422 p = vim_strsave(shorten_fname1(dirname));
1423 g_free(dirname);
1424 return p;
1426 # else
1427 /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
1428 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
1429 # endif
1431 #endif
1434 #endif /* FEAT_BROWSE */
1436 #if defined(FEAT_GUI_DIALOG) && !defined(HAVE_GTK2)
1438 static char_u *dialog_textfield = NULL;
1439 static GtkWidget *dialog_textentry;
1441 static void
1442 dlg_destroy(GtkWidget *dlg)
1444 if (dialog_textfield != NULL)
1446 const char *text;
1448 text = gtk_entry_get_text(GTK_ENTRY(dialog_textentry));
1449 vim_strncpy(dialog_textfield, (char_u *)text, IOSIZE - 1);
1452 /* Destroy the dialog, will break the waiting loop. */
1453 gtk_widget_destroy(dlg);
1456 # ifdef FEAT_GUI_GNOME
1457 static int
1458 gui_gnome_dialog( int type,
1459 char_u *title,
1460 char_u *message,
1461 char_u *buttons,
1462 int dfltbutton,
1463 char_u *textfield)
1465 GtkWidget *dlg;
1466 char *gdtype;
1467 char_u *buttons_copy, *p, *next;
1468 char **buttons_list;
1469 int butcount, cur;
1471 /* make a copy, so that we can insert NULs */
1472 if ((buttons_copy = vim_strsave(buttons)) == NULL)
1473 return -1;
1475 /* determine exact number of buttons and allocate array to hold them */
1476 for (butcount = 0, p = buttons; *p; p++)
1478 if (*p == '\n')
1479 butcount++;
1481 butcount++;
1482 buttons_list = g_new0(char *, butcount + 1);
1484 /* Add pixmap */
1485 switch (type)
1487 case VIM_ERROR:
1488 gdtype = GNOME_MESSAGE_BOX_ERROR;
1489 break;
1490 case VIM_WARNING:
1491 gdtype = GNOME_MESSAGE_BOX_WARNING;
1492 break;
1493 case VIM_INFO:
1494 gdtype = GNOME_MESSAGE_BOX_INFO;
1495 break;
1496 case VIM_QUESTION:
1497 gdtype = GNOME_MESSAGE_BOX_QUESTION;
1498 break;
1499 default:
1500 gdtype = GNOME_MESSAGE_BOX_GENERIC;
1503 p = buttons_copy;
1504 for (cur = 0; cur < butcount; ++cur)
1506 for (next = p; *next; ++next)
1508 if (*next == DLG_HOTKEY_CHAR)
1509 STRMOVE(next, next + 1);
1510 if (*next == DLG_BUTTON_SEP)
1512 *next++ = NUL;
1513 break;
1517 /* this should probably go into a table, but oh well */
1518 if (g_strcasecmp((char *)p, "Ok") == 0)
1519 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_OK);
1520 else if (g_strcasecmp((char *)p, "Cancel") == 0)
1521 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_CANCEL);
1522 else if (g_strcasecmp((char *)p, "Yes") == 0)
1523 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_YES);
1524 else if (g_strcasecmp((char *)p, "No") == 0)
1525 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_NO);
1526 else if (g_strcasecmp((char *)p, "Close") == 0)
1527 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_CLOSE);
1528 else if (g_strcasecmp((char *)p, "Help") == 0)
1529 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_HELP);
1530 else if (g_strcasecmp((char *)p, "Apply") == 0)
1531 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_APPLY);
1532 #if 0
1534 * these aren't really used that often anyway, but are listed here as
1535 * placeholders in case we need them.
1537 else if (g_strcasecmp((char *)p, "Next") == 0)
1538 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_NEXT);
1539 else if (g_strcasecmp((char *)p, "Prev") == 0)
1540 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_PREV);
1541 else if (g_strcasecmp((char *)p, "Up") == 0)
1542 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_UP);
1543 else if (g_strcasecmp((char *)p, "Down") == 0)
1544 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_DOWN);
1545 else if (g_strcasecmp((char *)p, "Font") == 0)
1546 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_FONT);
1547 #endif
1548 else
1549 buttons_list[cur] = g_strdup((char *)p);
1551 if (*next == NUL)
1552 break;
1554 p = next;
1556 vim_free(buttons_copy);
1558 dlg = gnome_message_box_newv((const char *)message,
1559 (const char *)gdtype,
1560 (const char **)buttons_list);
1561 for (cur = 0; cur < butcount; ++cur)
1562 g_free(buttons_list[cur]);
1563 g_free(buttons_list);
1565 dialog_textfield = textfield;
1566 if (textfield != NULL)
1568 /* Add text entry field */
1569 dialog_textentry = gtk_entry_new();
1570 gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dlg)->vbox), dialog_textentry,
1571 TRUE, TRUE, 0);
1572 gtk_entry_set_text(GTK_ENTRY(dialog_textentry),
1573 (const gchar *)textfield);
1574 gtk_entry_select_region(GTK_ENTRY(dialog_textentry), 0,
1575 STRLEN(textfield));
1576 gtk_entry_set_max_length(GTK_ENTRY(dialog_textentry), IOSIZE - 1);
1577 gtk_entry_set_position(GTK_ENTRY(dialog_textentry), STRLEN(textfield));
1578 gtk_widget_show(dialog_textentry);
1579 gtk_window_set_focus(GTK_WINDOW(dlg), dialog_textentry);
1582 gtk_signal_connect_object(GTK_OBJECT(dlg), "destroy",
1583 GTK_SIGNAL_FUNC(dlg_destroy), GTK_OBJECT(dlg));
1584 gnome_dialog_set_default(GNOME_DIALOG(dlg), dfltbutton + 1);
1585 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1586 GTK_WIDGET(dlg), VW_POS_MOUSE);
1588 return (1 + gnome_dialog_run_and_close(GNOME_DIALOG(dlg)));
1591 # endif /* FEAT_GUI_GNOME */
1593 typedef struct _ButtonData
1595 int *status;
1596 int index;
1597 GtkWidget *dialog;
1598 } ButtonData;
1600 typedef struct _CancelData
1602 int *status;
1603 int ignore_enter;
1604 GtkWidget *dialog;
1605 } CancelData;
1607 static void
1608 dlg_button_clicked(GtkWidget * widget, ButtonData *data)
1610 *(data->status) = data->index + 1;
1611 dlg_destroy(data->dialog);
1615 * This makes the Escape key equivalent to the cancel button.
1617 static int
1618 dlg_key_press_event(GtkWidget *widget, GdkEventKey *event, CancelData *data)
1620 /* Ignore hitting Enter (or Space) when there is no default button. */
1621 if (data->ignore_enter && (event->keyval == GDK_Return
1622 || event->keyval == ' '))
1623 return TRUE;
1624 else /* A different key was pressed, return to normal behavior */
1625 data->ignore_enter = FALSE;
1627 if (event->keyval != GDK_Escape && event->keyval != GDK_Return)
1628 return FALSE;
1630 /* The result value of 0 from a dialog is signaling cancelation.
1631 * 1 means OK. */
1632 *(data->status) = (event->keyval == GDK_Return);
1633 dlg_destroy(data->dialog);
1635 return TRUE;
1639 * Callback function for when the dialog was destroyed by a window manager.
1641 static void
1642 dlg_destroy_cb(int *p)
1644 *p = TRUE; /* set dialog_destroyed to break out of the loop */
1645 if (gtk_main_level() > 0)
1646 gtk_main_quit();
1650 gui_mch_dialog( int type, /* type of dialog */
1651 char_u *title, /* title of dialog */
1652 char_u *message, /* message text */
1653 char_u *buttons, /* names of buttons */
1654 int def_but, /* default button */
1655 char_u *textfield) /* text for textfield or NULL */
1657 char_u *names;
1658 char_u *p;
1659 int i;
1660 int butcount;
1661 int dialog_status = -1;
1662 int dialog_destroyed = FALSE;
1663 int vertical;
1665 GtkWidget *dialog;
1666 GtkWidget *frame;
1667 GtkWidget *vbox;
1668 GtkWidget *table;
1669 GtkWidget *dialogmessage;
1670 GtkWidget *action_area;
1671 GtkWidget *sub_area;
1672 GtkWidget *separator;
1673 GtkAccelGroup *accel_group;
1674 GtkWidget *pixmap;
1675 GdkPixmap *icon = NULL;
1676 GdkBitmap *mask = NULL;
1677 char **icon_data = NULL;
1679 GtkWidget **button;
1680 ButtonData *data;
1681 CancelData cancel_data;
1683 /* if our pointer is currently hidden, then we should show it. */
1684 gui_mch_mousehide(FALSE);
1686 # ifdef FEAT_GUI_GNOME
1687 /* If Gnome is available, use it for the dialog. */
1688 if (gtk_socket_id == 0)
1689 return gui_gnome_dialog(type, title, message, buttons, def_but,
1690 textfield);
1691 # endif
1693 if (title == NULL)
1694 title = (char_u *)_("Vim dialog...");
1696 if ((type < 0) || (type > VIM_LAST_TYPE))
1697 type = VIM_GENERIC;
1699 /* Check 'v' flag in 'guioptions': vertical button placement. */
1700 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
1702 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1703 gtk_window_set_title(GTK_WINDOW(dialog), (const gchar *)title);
1704 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
1705 gtk_widget_realize(dialog);
1706 gdk_window_set_decorations(dialog->window, GDK_DECOR_BORDER);
1707 gdk_window_set_functions(dialog->window, GDK_FUNC_MOVE);
1709 cancel_data.status = &dialog_status;
1710 cancel_data.dialog = dialog;
1711 gtk_signal_connect_after(GTK_OBJECT(dialog), "key_press_event",
1712 GTK_SIGNAL_FUNC(dlg_key_press_event),
1713 (gpointer) &cancel_data);
1714 /* Catch the destroy signal, otherwise we don't notice a window manager
1715 * destroying the dialog window. */
1716 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
1717 GTK_SIGNAL_FUNC(dlg_destroy_cb),
1718 (gpointer)&dialog_destroyed);
1720 gtk_grab_add(dialog);
1722 /* this makes it look beter on Motif style window managers */
1723 frame = gtk_frame_new(NULL);
1724 gtk_container_add(GTK_CONTAINER(dialog), frame);
1725 gtk_widget_show(frame);
1727 vbox = gtk_vbox_new(FALSE, 0);
1728 gtk_container_add(GTK_CONTAINER(frame), vbox);
1729 gtk_widget_show(vbox);
1731 table = gtk_table_new(1, 3, FALSE);
1732 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1733 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1734 gtk_container_border_width(GTK_CONTAINER(table), 4);
1735 gtk_box_pack_start(GTK_BOX(vbox), table, 4, 4, 0);
1736 gtk_widget_show(table);
1738 /* Add pixmap */
1739 switch (type)
1741 case VIM_GENERIC:
1742 icon_data = generic_xpm;
1743 break;
1744 case VIM_ERROR:
1745 icon_data = error_xpm;
1746 break;
1747 case VIM_WARNING:
1748 icon_data = alert_xpm;
1749 break;
1750 case VIM_INFO:
1751 icon_data = info_xpm;
1752 break;
1753 case VIM_QUESTION:
1754 icon_data = quest_xpm;
1755 break;
1756 default:
1757 icon_data = generic_xpm;
1759 icon = gdk_pixmap_colormap_create_from_xpm_d(NULL,
1760 gtk_widget_get_colormap(dialog),
1761 &mask, NULL, icon_data);
1762 if (icon)
1764 pixmap = gtk_pixmap_new(icon, mask);
1765 /* gtk_misc_set_alignment(GTK_MISC(pixmap), 0.5, 0.5); */
1766 gtk_table_attach_defaults(GTK_TABLE(table), pixmap, 0, 1, 0, 1);
1767 gtk_widget_show(pixmap);
1770 /* Add label */
1771 dialogmessage = gtk_label_new((const gchar *)message);
1772 gtk_table_attach_defaults(GTK_TABLE(table), dialogmessage, 1, 2, 0, 1);
1773 gtk_widget_show(dialogmessage);
1775 dialog_textfield = textfield;
1776 if (textfield != NULL)
1778 /* Add text entry field */
1779 dialog_textentry = gtk_entry_new();
1780 gtk_widget_set_usize(dialog_textentry, 400, -2);
1781 gtk_box_pack_start(GTK_BOX(vbox), dialog_textentry, TRUE, TRUE, 0);
1782 gtk_entry_set_text(GTK_ENTRY(dialog_textentry),
1783 (const gchar *)textfield);
1784 gtk_entry_select_region(GTK_ENTRY(dialog_textentry), 0,
1785 STRLEN(textfield));
1786 gtk_entry_set_max_length(GTK_ENTRY(dialog_textentry), IOSIZE - 1);
1787 gtk_entry_set_position(GTK_ENTRY(dialog_textentry), STRLEN(textfield));
1788 gtk_widget_show(dialog_textentry);
1791 /* Add box for buttons */
1792 action_area = gtk_hbox_new(FALSE, 0);
1793 gtk_container_border_width(GTK_CONTAINER(action_area), 4);
1794 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
1795 gtk_widget_show(action_area);
1797 /* Add a [vh]box in the hbox to center the buttons in the dialog. */
1798 if (vertical)
1799 sub_area = gtk_vbox_new(FALSE, 0);
1800 else
1801 sub_area = gtk_hbox_new(FALSE, 0);
1802 gtk_container_set_border_width(GTK_CONTAINER(sub_area), 0);
1803 gtk_box_pack_start(GTK_BOX(action_area), sub_area, TRUE, FALSE, 0);
1804 gtk_widget_show(sub_area);
1807 * Create the buttons.
1811 * Translate the Vim accelerator character into an underscore for GTK+.
1812 * Double underscores to keep them in the label.
1814 /* count the number of underscores */
1815 i = 1;
1816 for (p = buttons; *p; ++p)
1817 if (*p == '_')
1818 ++i;
1820 /* make a copy of "buttons" with the translated characters */
1821 names = alloc(STRLEN(buttons) + i);
1822 if (names == NULL)
1823 return -1;
1825 p = names;
1826 for (i = 0; buttons[i]; ++i)
1828 if (buttons[i] == DLG_HOTKEY_CHAR)
1829 *p++ = '_';
1830 else
1832 if (buttons[i] == '_')
1833 *p++ = '_';
1834 *p++ = buttons[i];
1837 *p = NUL;
1839 /* Count the number of buttons and allocate button[] and data[]. */
1840 butcount = 1;
1841 for (p = names; *p; ++p)
1842 if (*p == DLG_BUTTON_SEP)
1843 ++butcount;
1844 button = (GtkWidget **)alloc((unsigned)(butcount * sizeof(GtkWidget *)));
1845 data = (ButtonData *)alloc((unsigned)(butcount * sizeof(ButtonData)));
1846 if (button == NULL || data == NULL)
1848 vim_free(names);
1849 vim_free(button);
1850 vim_free(data);
1851 return -1;
1854 /* Attach the new accelerator group to the window. */
1855 accel_group = gtk_accel_group_new();
1856 gtk_accel_group_attach(accel_group, GTK_OBJECT(dialog));
1858 p = names;
1859 for (butcount = 0; *p; ++butcount)
1861 char_u *next;
1862 GtkWidget *label;
1863 # ifdef GTK_USE_ACCEL
1864 guint accel_key;
1865 # endif
1867 /* Chunk out this single button. */
1868 for (next = p; *next; ++next)
1870 if (*next == DLG_BUTTON_SEP)
1872 *next++ = NUL;
1873 break;
1877 button[butcount] = gtk_button_new();
1878 GTK_WIDGET_SET_FLAGS(button[butcount], GTK_CAN_DEFAULT);
1880 label = gtk_accel_label_new("");
1881 gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(label), dialog);
1883 # ifdef GTK_USE_ACCEL
1884 accel_key = gtk_label_parse_uline(GTK_LABEL(label), (const gchar *)p);
1885 /* Don't add accelator if 'winaltkeys' is "no". */
1886 if (accel_key != GDK_VoidSymbol)
1888 gtk_widget_add_accelerator(button[butcount],
1889 "clicked",
1890 accel_group,
1891 accel_key, 0,
1892 (GtkAccelFlags)0);
1894 # else
1895 (void)gtk_label_parse_uline(GTK_LABEL(label), (const gchar *)p);
1896 # endif
1898 gtk_container_add(GTK_CONTAINER(button[butcount]), label);
1899 gtk_widget_show_all(button[butcount]);
1901 data[butcount].status = &dialog_status;
1902 data[butcount].index = butcount;
1903 data[butcount].dialog = dialog;
1904 gtk_signal_connect(GTK_OBJECT(button[butcount]),
1905 (const char *)"clicked",
1906 GTK_SIGNAL_FUNC(dlg_button_clicked),
1907 (gpointer) &data[butcount]);
1909 gtk_box_pack_start(GTK_BOX(sub_area), button[butcount],
1910 TRUE, FALSE, 0);
1911 p = next;
1914 vim_free(names);
1916 cancel_data.ignore_enter = FALSE;
1917 if (butcount > 0)
1919 --def_but; /* 1 is first button */
1920 if (def_but >= butcount)
1921 def_but = -1;
1922 if (def_but >= 0)
1924 gtk_widget_grab_focus(button[def_but]);
1925 gtk_widget_grab_default(button[def_but]);
1927 else
1928 /* No default, ignore hitting Enter. */
1929 cancel_data.ignore_enter = TRUE;
1932 if (textfield != NULL)
1933 gtk_window_set_focus(GTK_WINDOW(dialog), dialog_textentry);
1935 separator = gtk_hseparator_new();
1936 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
1937 gtk_widget_show(separator);
1939 dialog_status = -1;
1941 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1942 GTK_WIDGET(dialog), VW_POS_MOUSE);
1944 gtk_widget_show(dialog);
1946 /* loop here until the dialog goes away */
1947 while (dialog_status == -1 && !dialog_destroyed
1948 && GTK_WIDGET_DRAWABLE(dialog))
1949 gtk_main_iteration_do(TRUE);
1951 if (dialog_status < 0)
1952 dialog_status = 0;
1953 if (dialog_status != 1 && textfield != NULL)
1954 *textfield = NUL; /* dialog was cancelled */
1956 /* let the garbage collector know that we don't need it any longer */
1957 gtk_accel_group_unref(accel_group);
1959 vim_free(button);
1960 vim_free(data);
1962 return dialog_status;
1965 #endif /* FEAT_GUI_DIALOG && !HAVE_GTK2 */
1968 #if (defined(FEAT_GUI_DIALOG) && defined(HAVE_GTK2)) || defined(PROTO)
1970 static GtkWidget *
1971 create_message_dialog(int type, char_u *title, char_u *message)
1973 GtkWidget *dialog;
1974 GtkMessageType message_type;
1976 switch (type)
1978 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1979 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1980 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1981 default: message_type = GTK_MESSAGE_INFO; break;
1984 message = CONVERT_TO_UTF8(message);
1985 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1986 GTK_DIALOG_DESTROY_WITH_PARENT,
1987 message_type,
1988 GTK_BUTTONS_NONE,
1989 "%s", (const char *)message);
1990 CONVERT_TO_UTF8_FREE(message);
1992 if (title != NULL)
1994 title = CONVERT_TO_UTF8(title);
1995 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1996 CONVERT_TO_UTF8_FREE(title);
1998 else if (type == VIM_GENERIC)
2000 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
2003 return dialog;
2007 * Split up button_string into individual button labels by inserting
2008 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
2009 * '&' with '_'. button_string must point to allocated memory!
2010 * Return an allocated array of pointers into button_string.
2012 static char **
2013 split_button_string(char_u *button_string, int *n_buttons)
2015 char **array;
2016 char_u *p;
2017 unsigned int count = 1;
2019 for (p = button_string; *p != NUL; ++p)
2020 if (*p == DLG_BUTTON_SEP)
2021 ++count;
2023 array = (char **)alloc((count + 1) * sizeof(char *));
2024 count = 0;
2026 if (array != NULL)
2028 array[count++] = (char *)button_string;
2029 for (p = button_string; *p != NUL; )
2031 if (*p == DLG_BUTTON_SEP)
2033 *p++ = NUL;
2034 array[count++] = (char *)p;
2036 else if (*p == DLG_HOTKEY_CHAR)
2037 *p++ = '_';
2038 else
2039 mb_ptr_adv(p);
2041 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
2044 *n_buttons = count;
2045 return array;
2048 static char **
2049 split_button_translation(const char *message)
2051 char **buttons = NULL;
2052 char_u *str;
2053 int n_buttons = 0;
2054 int n_expected = 1;
2056 for (str = (char_u *)message; *str != NUL; ++str)
2057 if (*str == DLG_BUTTON_SEP)
2058 ++n_expected;
2060 str = (char_u *)_(message);
2061 if (str != NULL)
2063 if (output_conv.vc_type != CONV_NONE)
2064 str = string_convert(&output_conv, str, NULL);
2065 else
2066 str = vim_strsave(str);
2068 if (str != NULL)
2069 buttons = split_button_string(str, &n_buttons);
2072 * Uh-oh... this should never ever happen. But we don't wanna crash
2073 * if the translation is broken, thus fall back to the untranslated
2074 * buttons string in case of emergency.
2076 if (buttons == NULL || n_buttons != n_expected)
2078 vim_free(buttons);
2079 vim_free(str);
2080 buttons = NULL;
2081 str = vim_strsave((char_u *)message);
2083 if (str != NULL)
2084 buttons = split_button_string(str, &n_buttons);
2085 if (buttons == NULL)
2086 vim_free(str);
2089 return buttons;
2092 static int
2093 button_equal(const char *a, const char *b)
2095 while (*a != '\0' && *b != '\0')
2097 if (*a == '_' && *++a == '\0')
2098 break;
2099 if (*b == '_' && *++b == '\0')
2100 break;
2102 if (g_unichar_tolower(g_utf8_get_char(a))
2103 != g_unichar_tolower(g_utf8_get_char(b)))
2104 return FALSE;
2106 a = g_utf8_next_char(a);
2107 b = g_utf8_next_char(b);
2110 return (*a == '\0' && *b == '\0');
2113 static void
2114 dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
2116 char **ok;
2117 char **ync; /* "yes no cancel" */
2118 char **buttons;
2119 int n_buttons = 0;
2120 int idx;
2122 button_string = vim_strsave(button_string); /* must be writable */
2123 if (button_string == NULL)
2124 return;
2126 /* Check 'v' flag in 'guioptions': vertical button placement. */
2127 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
2129 GtkWidget *vbutton_box;
2131 vbutton_box = gtk_vbutton_box_new();
2132 gtk_widget_show(vbutton_box);
2133 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2134 vbutton_box, TRUE, FALSE, 0);
2135 /* Overrule the "action_area" value, hopefully this works... */
2136 GTK_DIALOG(dialog)->action_area = vbutton_box;
2140 * Yes this is ugly, I don't particularly like it either. But doing it
2141 * this way has the compelling advantage that translations need not to
2142 * be touched at all. See below what 'ok' and 'ync' are used for.
2144 ok = split_button_translation(N_("&Ok"));
2145 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
2146 buttons = split_button_string(button_string, &n_buttons);
2149 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
2150 * environment. Don't hit me -- it's all about consistency.
2151 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
2152 * other way around...
2154 for (idx = 1; idx <= n_buttons; ++idx)
2156 char *label;
2157 char_u *label8;
2159 label = buttons[idx - 1];
2161 * Perform some guesswork to find appropriate stock items for the
2162 * buttons. We have to compare with a sample of the translated
2163 * button string to get things right. Yes, this is hackish :/
2165 * But even the common button labels aren't necessarily translated,
2166 * since anyone can create their own dialogs using Vim functions.
2167 * Thus we have to check for those too.
2169 if (ok != NULL && ync != NULL) /* almost impossible to fail */
2171 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
2172 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
2173 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
2174 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
2175 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
2176 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
2177 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
2178 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
2180 label8 = CONVERT_TO_UTF8((char_u *)label);
2181 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
2182 CONVERT_TO_UTF8_FREE(label8);
2185 if (ok != NULL)
2186 vim_free(*ok);
2187 if (ync != NULL)
2188 vim_free(*ync);
2189 vim_free(ok);
2190 vim_free(ync);
2191 vim_free(buttons);
2192 vim_free(button_string);
2196 * Allow mnemonic accelerators to be activated without pressing <Alt>.
2197 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
2198 * GUI used to work this way, and I consider the impact on UI consistency
2199 * low enough to justify implementing this as a special Vim feature.
2201 typedef struct _DialogInfo
2203 int ignore_enter; /* no default button, ignore "Enter" */
2204 int noalt; /* accept accelerators without Alt */
2205 GtkDialog *dialog; /* Widget of the dialog */
2206 } DialogInfo;
2208 static gboolean
2209 dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
2211 DialogInfo *di = (DialogInfo *)data;
2213 /* Ignore hitting Enter (or Space) when there is no default button. */
2214 if (di->ignore_enter && (event->keyval == GDK_Return
2215 || event->keyval == ' '))
2216 return TRUE;
2217 else /* A different key was pressed, return to normal behavior */
2218 di->ignore_enter = FALSE;
2220 /* Close the dialog when hitting "Esc". */
2221 if (event->keyval == GDK_Escape)
2223 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
2224 return TRUE;
2227 if (di->noalt
2228 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
2230 return gtk_window_mnemonic_activate(
2231 GTK_WINDOW(widget), event->keyval,
2232 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
2235 return FALSE; /* continue emission */
2239 gui_mch_dialog(int type, /* type of dialog */
2240 char_u *title, /* title of dialog */
2241 char_u *message, /* message text */
2242 char_u *buttons, /* names of buttons */
2243 int def_but, /* default button */
2244 char_u *textfield) /* text for textfield or NULL */
2246 GtkWidget *dialog;
2247 GtkWidget *entry = NULL;
2248 char_u *text;
2249 int response;
2250 DialogInfo dialoginfo;
2252 dialog = create_message_dialog(type, title, message);
2253 dialoginfo.dialog = GTK_DIALOG(dialog);
2254 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
2256 if (textfield != NULL)
2258 GtkWidget *alignment;
2260 entry = gtk_entry_new();
2261 gtk_widget_show(entry);
2263 text = CONVERT_TO_UTF8(textfield);
2264 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
2265 CONVERT_TO_UTF8_FREE(text);
2267 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2268 (float)1.0, (float)1.0);
2269 gtk_container_add(GTK_CONTAINER(alignment), entry);
2270 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
2271 gtk_widget_show(alignment);
2273 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2274 alignment, TRUE, FALSE, 0);
2275 dialoginfo.noalt = FALSE;
2277 else
2278 dialoginfo.noalt = TRUE;
2280 /* Allow activation of mnemonic accelerators without pressing <Alt> when
2281 * there is no textfield. Handle pressing Esc. */
2282 g_signal_connect(G_OBJECT(dialog), "key_press_event",
2283 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
2285 if (def_but > 0)
2287 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
2288 dialoginfo.ignore_enter = FALSE;
2290 else
2291 /* No default button, ignore pressing Enter. */
2292 dialoginfo.ignore_enter = TRUE;
2294 /* Show the mouse pointer if it's currently hidden. */
2295 gui_mch_mousehide(FALSE);
2297 response = gtk_dialog_run(GTK_DIALOG(dialog));
2299 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
2300 if (response != GTK_RESPONSE_NONE)
2302 if (response == GTK_RESPONSE_ACCEPT) /* Enter pressed */
2303 response = def_but;
2304 if (textfield != NULL)
2306 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
2307 text = CONVERT_FROM_UTF8(text);
2309 vim_strncpy(textfield, text, IOSIZE - 1);
2311 CONVERT_FROM_UTF8_FREE(text);
2313 gtk_widget_destroy(dialog);
2316 /* Terrible hack: When the text area still has focus when we remove the
2317 * dialog, somehow gvim loses window focus. This is with "point to type"
2318 * in the KDE 3.1 window manager. Warp the mouse pointer to outside the
2319 * window and back to avoid that. */
2320 if (!gui.in_focus)
2322 int x, y;
2324 gdk_window_get_pointer(gui.drawarea->window, &x, &y, NULL);
2325 gui_mch_setmouse(-100, -100);
2326 gui_mch_setmouse(x, y);
2329 return response > 0 ? response : 0;
2332 #endif /* FEAT_GUI_DIALOG && HAVE_GTK2 */
2335 #if defined(FEAT_MENU) || defined(PROTO)
2337 void
2338 gui_mch_show_popupmenu(vimmenu_T *menu)
2340 # if defined(FEAT_XIM) && defined(HAVE_GTK2)
2342 * Append a submenu for selecting an input method. This is
2343 * currently the only way to switch input methods at runtime.
2345 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
2346 "vim-has-im-menu") == NULL)
2348 GtkWidget *menuitem;
2349 GtkWidget *submenu;
2350 char_u *name;
2352 menuitem = gtk_separator_menu_item_new();
2353 gtk_widget_show(menuitem);
2354 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
2356 name = (char_u *)_("Input _Methods");
2357 name = CONVERT_TO_UTF8(name);
2358 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
2359 CONVERT_TO_UTF8_FREE(name);
2360 gtk_widget_show(menuitem);
2362 submenu = gtk_menu_new();
2363 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
2364 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
2366 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
2367 GTK_MENU_SHELL(submenu));
2368 g_object_set_data(G_OBJECT(menu->submenu_id),
2369 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
2371 # endif /* FEAT_XIM && HAVE_GTK2 */
2373 gtk_menu_popup(GTK_MENU(menu->submenu_id),
2374 NULL, NULL,
2375 (GtkMenuPositionFunc)NULL, NULL,
2376 3U, (guint32)GDK_CURRENT_TIME);
2379 /* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
2380 * popup_menu_position_func(). */
2381 static int popup_mouse_pos;
2384 * Menu position callback; used by gui_make_popup() to place the menu
2385 * at the current text cursor position.
2387 * Note: The push_in output argument seems to affect scrolling of huge
2388 * menus that don't fit on the screen. Leave it at the default for now.
2390 static void
2391 popup_menu_position_func(GtkMenu *menu UNUSED,
2392 gint *x, gint *y,
2393 # ifdef HAVE_GTK2
2394 gboolean *push_in UNUSED,
2395 # endif
2396 gpointer user_data UNUSED)
2398 gdk_window_get_origin(gui.drawarea->window, x, y);
2400 if (popup_mouse_pos)
2402 int mx, my;
2404 gui_mch_getmouse(&mx, &my);
2405 *x += mx;
2406 *y += my;
2408 else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
2410 /* Find the cursor position in the current window */
2411 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
2412 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
2416 void
2417 gui_make_popup(char_u *path_name, int mouse_pos)
2419 vimmenu_T *menu;
2421 popup_mouse_pos = mouse_pos;
2423 menu = gui_find_menu(path_name);
2425 if (menu != NULL && menu->submenu_id != NULL)
2427 gtk_menu_popup(GTK_MENU(menu->submenu_id),
2428 NULL, NULL,
2429 &popup_menu_position_func, NULL,
2430 0U, (guint32)GDK_CURRENT_TIME);
2434 #endif /* FEAT_MENU */
2438 * We don't create it twice.
2441 typedef struct _SharedFindReplace
2443 GtkWidget *dialog; /* the main dialog widget */
2444 GtkWidget *wword; /* 'Whole word only' check button */
2445 GtkWidget *mcase; /* 'Match case' check button */
2446 GtkWidget *up; /* search direction 'Up' radio button */
2447 GtkWidget *down; /* search direction 'Down' radio button */
2448 GtkWidget *what; /* 'Find what' entry text widget */
2449 GtkWidget *with; /* 'Replace with' entry text widget */
2450 GtkWidget *find; /* 'Find Next' action button */
2451 GtkWidget *replace; /* 'Replace With' action button */
2452 GtkWidget *all; /* 'Replace All' action button */
2453 } SharedFindReplace;
2455 static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2456 static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2458 static int
2459 find_key_press_event(
2460 GtkWidget *widget UNUSED,
2461 GdkEventKey *event,
2462 SharedFindReplace *frdp)
2464 /* If the user is holding one of the key modifiers we will just bail out,
2465 * thus preserving the possibility of normal focus traversal.
2467 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
2468 return FALSE;
2470 /* the Escape key synthesizes a cancellation action */
2471 if (event->keyval == GDK_Escape)
2473 gtk_widget_hide(frdp->dialog);
2475 return TRUE;
2478 * What the **** is this for? Disabled for GTK+ 2 because due to
2479 * gtk_signal_connect_after() it doesn't have any effect anyway.
2480 * (Fortunately.)
2482 #ifndef HAVE_GTK2
2483 /* block traversal resulting from those keys */
2484 if (event->keyval == GDK_Left
2485 || event->keyval == GDK_Right
2486 || event->keyval == GDK_space)
2487 return TRUE;
2488 #endif
2490 /* It would be delightful if it where possible to do search history
2491 * operations on the K_UP and K_DOWN keys here.
2494 return FALSE;
2497 #ifdef HAVE_GTK2
2498 static GtkWidget *
2499 create_image_button(const char *stock_id, const char *label)
2501 char_u *text;
2502 GtkWidget *box;
2503 GtkWidget *alignment;
2504 GtkWidget *button;
2506 text = CONVERT_TO_UTF8((char_u *)label);
2508 box = gtk_hbox_new(FALSE, 3);
2509 gtk_box_pack_start(GTK_BOX(box),
2510 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
2511 FALSE, FALSE, 0);
2512 gtk_box_pack_start(GTK_BOX(box),
2513 gtk_label_new((const char *)text),
2514 FALSE, FALSE, 0);
2516 CONVERT_TO_UTF8_FREE(text);
2518 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2519 (float)0.0, (float)0.0);
2520 gtk_container_add(GTK_CONTAINER(alignment), box);
2521 gtk_widget_show_all(alignment);
2523 button = gtk_button_new();
2524 gtk_container_add(GTK_CONTAINER(button), alignment);
2526 return button;
2530 * This is currently only used by find_replace_dialog_create(), and
2531 * I'd really like to keep it at that. In other words: don't spread
2532 * this nasty hack all over the code. Think twice.
2534 static const char *
2535 convert_localized_message(char_u **buffer, const char *message)
2537 if (output_conv.vc_type == CONV_NONE)
2538 return message;
2540 vim_free(*buffer);
2541 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
2543 return (const char *)*buffer;
2545 #endif /* HAVE_GTK2 */
2547 static void
2548 find_replace_dialog_create(char_u *arg, int do_replace)
2550 #ifndef HAVE_GTK2
2551 GtkWidget *frame;
2552 #endif
2553 GtkWidget *hbox; /* main top down box */
2554 GtkWidget *actionarea;
2555 GtkWidget *table;
2556 GtkWidget *tmp;
2557 GtkWidget *vbox;
2558 gboolean sensitive;
2559 SharedFindReplace *frdp;
2560 char_u *entry_text;
2561 int wword = FALSE;
2562 int mcase = !p_ic;
2563 #ifdef HAVE_GTK2
2564 char_u *conv_buffer = NULL;
2565 # define CONV(message) convert_localized_message(&conv_buffer, (message))
2566 #else
2567 # define CONV(message) (message)
2568 #endif
2570 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2572 /* Get the search string to use. */
2573 entry_text = get_find_dialog_text(arg, &wword, &mcase);
2575 #ifdef HAVE_GTK2
2576 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
2578 char_u *old_text = entry_text;
2579 entry_text = string_convert(&output_conv, entry_text, NULL);
2580 vim_free(old_text);
2582 #endif
2585 * If the dialog already exists, just raise it.
2587 if (frdp->dialog)
2589 #ifndef HAVE_GTK2
2590 /* always make the dialog appear where you want it even if the mainwin
2591 * has moved -- dbv */
2592 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
2593 GTK_WIDGET(frdp->dialog), VW_POS_MOUSE);
2594 gui_gtk_synch_fonts();
2596 if (!GTK_WIDGET_VISIBLE(frdp->dialog))
2598 gtk_widget_grab_focus(frdp->what);
2599 gtk_widget_show(frdp->dialog);
2601 #endif
2602 if (entry_text != NULL)
2604 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2605 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2606 (gboolean)wword);
2607 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2608 (gboolean)mcase);
2610 #ifdef HAVE_GTK2
2611 gtk_window_present(GTK_WINDOW(frdp->dialog));
2612 #else
2613 gdk_window_raise(frdp->dialog->window);
2614 #endif
2615 vim_free(entry_text);
2616 return;
2619 #ifdef HAVE_GTK2
2620 frdp->dialog = gtk_dialog_new();
2621 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
2622 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
2623 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
2624 #else
2625 frdp->dialog = gtk_window_new(GTK_WINDOW_DIALOG);
2626 #endif
2628 if (do_replace)
2630 #ifndef HAVE_GTK2
2631 gtk_window_set_wmclass(GTK_WINDOW(frdp->dialog), "searchrepl", "gvim");
2632 #endif
2633 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2634 CONV(_("VIM - Search and Replace...")));
2636 else
2638 #ifndef HAVE_GTK2
2639 gtk_window_set_wmclass(GTK_WINDOW(frdp->dialog), "search", "gvim");
2640 #endif
2641 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2642 CONV(_("VIM - Search...")));
2645 #ifndef HAVE_GTK2 /* Utter crack. Shudder. */
2646 gtk_widget_realize(frdp->dialog);
2647 gdk_window_set_decorations(frdp->dialog->window,
2648 GDK_DECOR_TITLE | GDK_DECOR_BORDER | GDK_DECOR_RESIZEH);
2649 gdk_window_set_functions(frdp->dialog->window,
2650 GDK_FUNC_RESIZE | GDK_FUNC_MOVE);
2651 #endif
2653 #ifndef HAVE_GTK2
2654 /* this makes it look better on Motif style window managers */
2655 frame = gtk_frame_new(NULL);
2656 gtk_container_add(GTK_CONTAINER(frdp->dialog), frame);
2657 #endif
2659 hbox = gtk_hbox_new(FALSE, 0);
2660 #ifdef HAVE_GTK2
2661 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2662 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
2663 #else
2664 gtk_container_add(GTK_CONTAINER(frame), hbox);
2665 #endif
2667 if (do_replace)
2668 table = gtk_table_new(1024, 4, FALSE);
2669 else
2670 table = gtk_table_new(1024, 3, FALSE);
2671 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
2672 gtk_container_border_width(GTK_CONTAINER(table), 4);
2674 tmp = gtk_label_new(CONV(_("Find what:")));
2675 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2676 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
2677 GTK_FILL, GTK_EXPAND, 2, 2);
2678 frdp->what = gtk_entry_new();
2679 sensitive = (entry_text != NULL && entry_text[0] != NUL);
2680 if (entry_text != NULL)
2681 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2682 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
2683 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
2684 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
2685 GTK_SIGNAL_FUNC(find_key_press_event),
2686 (gpointer) frdp);
2687 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
2688 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2690 if (do_replace)
2692 tmp = gtk_label_new(CONV(_("Replace with:")));
2693 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2694 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
2695 GTK_FILL, GTK_EXPAND, 2, 2);
2696 frdp->with = gtk_entry_new();
2697 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
2698 GTK_SIGNAL_FUNC(find_replace_cb),
2699 GINT_TO_POINTER(FRD_R_FINDNEXT));
2700 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
2701 GTK_SIGNAL_FUNC(find_key_press_event),
2702 (gpointer) frdp);
2703 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
2704 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2707 * Make the entry activation only change the input focus onto the
2708 * with item.
2710 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2711 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
2713 else
2716 * Make the entry activation do the search.
2718 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2719 GTK_SIGNAL_FUNC(find_replace_cb),
2720 GINT_TO_POINTER(FRD_FINDNEXT));
2723 /* whole word only button */
2724 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
2725 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2726 (gboolean)wword);
2727 if (do_replace)
2728 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
2729 GTK_FILL, GTK_EXPAND, 2, 2);
2730 else
2731 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
2732 GTK_FILL, GTK_EXPAND, 2, 2);
2734 /* match case button */
2735 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
2736 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2737 (gboolean)mcase);
2738 if (do_replace)
2739 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
2740 GTK_FILL, GTK_EXPAND, 2, 2);
2741 else
2742 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
2743 GTK_FILL, GTK_EXPAND, 2, 2);
2745 tmp = gtk_frame_new(CONV(_("Direction")));
2746 if (do_replace)
2747 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
2748 GTK_FILL, GTK_FILL, 2, 2);
2749 else
2750 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
2751 GTK_FILL, GTK_FILL, 2, 2);
2752 vbox = gtk_vbox_new(FALSE, 0);
2753 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
2754 gtk_container_add(GTK_CONTAINER(tmp), vbox);
2756 /* 'Up' and 'Down' buttons */
2757 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
2758 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
2759 frdp->down = gtk_radio_button_new_with_label(
2760 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
2761 CONV(_("Down")));
2762 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
2763 #ifdef HAVE_GTK2
2764 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
2765 #endif
2766 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
2768 /* vbox to hold the action buttons */
2769 actionarea = gtk_vbutton_box_new();
2770 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
2771 #ifndef HAVE_GTK2
2772 if (do_replace)
2774 gtk_button_box_set_layout(GTK_BUTTON_BOX(actionarea),
2775 GTK_BUTTONBOX_END);
2776 gtk_button_box_set_spacing(GTK_BUTTON_BOX(actionarea), 0);
2778 #endif
2779 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
2781 /* 'Find Next' button */
2782 #ifdef HAVE_GTK2
2783 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
2784 #else
2785 frdp->find = gtk_button_new_with_label(_("Find Next"));
2786 #endif
2787 gtk_widget_set_sensitive(frdp->find, sensitive);
2789 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
2790 GTK_SIGNAL_FUNC(find_replace_cb),
2791 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2792 : GINT_TO_POINTER(FRD_FINDNEXT));
2794 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
2795 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
2796 gtk_widget_grab_default(frdp->find);
2798 if (do_replace)
2800 /* 'Replace' button */
2801 #ifdef HAVE_GTK2
2802 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
2803 #else
2804 frdp->replace = gtk_button_new_with_label(_("Replace"));
2805 #endif
2806 gtk_widget_set_sensitive(frdp->replace, sensitive);
2807 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
2808 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
2809 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
2810 GTK_SIGNAL_FUNC(find_replace_cb),
2811 GINT_TO_POINTER(FRD_REPLACE));
2813 /* 'Replace All' button */
2814 #ifdef HAVE_GTK2
2815 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
2816 #else
2817 frdp->all = gtk_button_new_with_label(_("Replace All"));
2818 #endif
2819 gtk_widget_set_sensitive(frdp->all, sensitive);
2820 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
2821 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
2822 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
2823 GTK_SIGNAL_FUNC(find_replace_cb),
2824 GINT_TO_POINTER(FRD_REPLACEALL));
2827 /* 'Cancel' button */
2828 #ifdef HAVE_GTK2
2829 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
2830 #else
2831 tmp = gtk_button_new_with_label(_("Cancel"));
2832 #endif
2833 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
2834 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
2835 gtk_signal_connect_object(GTK_OBJECT(tmp),
2836 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
2837 GTK_OBJECT(frdp->dialog));
2838 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
2839 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
2840 GTK_OBJECT(frdp->dialog));
2842 tmp = gtk_vseparator_new();
2843 #ifdef HAVE_GTK2
2844 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
2845 #else
2846 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, TRUE, 0);
2847 #endif
2849 #ifndef HAVE_GTK2
2850 gtk_widget_grab_focus(frdp->what);
2852 /* show the frame and realize the frdp->dialog this gives us a window size
2853 * request that we'll use to position the window within the boundary of
2854 * the mainwin --dbv */
2855 gtk_widget_show_all(frame);
2856 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
2857 GTK_WIDGET(frdp->dialog), VW_POS_MOUSE);
2858 gui_gtk_synch_fonts();
2859 gtk_widget_show_all(frdp->dialog);
2860 #endif
2862 #ifdef HAVE_GTK2
2863 /* Suppress automatic show of the unused action area */
2864 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
2865 gtk_widget_show_all(hbox);
2866 gtk_widget_show(frdp->dialog);
2867 #endif
2869 vim_free(entry_text);
2870 #ifdef HAVE_GTK2
2871 vim_free(conv_buffer);
2872 #endif
2873 #undef CONV
2876 void
2877 gui_mch_find_dialog(exarg_T *eap)
2879 if (gui.in_use)
2880 find_replace_dialog_create(eap->arg, FALSE);
2883 void
2884 gui_mch_replace_dialog(exarg_T *eap)
2886 if (gui.in_use)
2887 find_replace_dialog_create(eap->arg, TRUE);
2891 #if !defined(HAVE_GTK2) || defined(PROTO)
2893 * Synchronize all gui elements, which are dependant upon the
2894 * main text font used. Those are in esp. the find/replace dialogs.
2895 * If you don't understand why this should be needed, please try to
2896 * search for "pięść" in iso8859-2.
2898 * (<danielk> I converted the comment above to UTF-8 to put
2899 * a stopper to the encoding mess. Forgive me :)
2901 * Obsolete with GTK2.
2903 void
2904 gui_gtk_synch_fonts(void)
2906 SharedFindReplace *frdp;
2907 int do_replace;
2909 /* OK this loop is a bit tricky... */
2910 for (do_replace = 0; do_replace <= 1; ++do_replace)
2912 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2913 if (frdp->dialog)
2915 GtkStyle *style;
2917 /* synch the font with whats used by the text itself */
2918 style = gtk_style_copy(gtk_widget_get_style(frdp->what));
2919 gdk_font_unref(style->font);
2920 # ifdef FEAT_XFONTSET
2921 if (gui.fontset != NOFONTSET)
2922 style->font = gui.fontset;
2923 else
2924 # endif
2925 style->font = gui.norm_font;
2926 gdk_font_ref(style->font);
2927 gtk_widget_set_style(frdp->what, style);
2928 gtk_style_unref(style);
2929 if (do_replace)
2931 style = gtk_style_copy(gtk_widget_get_style(frdp->with));
2932 gdk_font_unref(style->font);
2933 # ifdef FEAT_XFONTSET
2934 if (gui.fontset != NOFONTSET)
2935 style->font = gui.fontset;
2936 else
2937 # endif
2938 style->font = gui.norm_font;
2939 gdk_font_ref(style->font);
2940 gtk_widget_set_style(frdp->with, style);
2941 gtk_style_unref(style);
2946 #endif /* !HAVE_GTK2 */
2950 * Callback for actions of the find and replace dialogs
2952 static void
2953 find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
2955 int flags;
2956 char_u *find_text;
2957 char_u *repl_text;
2958 gboolean direction_down;
2959 SharedFindReplace *sfr;
2960 int rc;
2962 flags = (int)(long)data; /* avoid a lint warning here */
2964 /* Get the search/replace strings from the dialog */
2965 if (flags == FRD_FINDNEXT)
2967 repl_text = NULL;
2968 sfr = &find_widgets;
2970 else
2972 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
2973 sfr = &repl_widgets;
2976 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
2977 direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
2979 if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
2980 flags |= FRD_WHOLE_WORD;
2981 if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
2982 flags |= FRD_MATCH_CASE;
2984 #ifdef HAVE_GTK2
2985 repl_text = CONVERT_FROM_UTF8(repl_text);
2986 find_text = CONVERT_FROM_UTF8(find_text);
2987 #endif
2988 rc = gui_do_findrepl(flags, find_text, repl_text, direction_down);
2989 #ifdef HAVE_GTK2
2990 CONVERT_FROM_UTF8_FREE(repl_text);
2991 CONVERT_FROM_UTF8_FREE(find_text);
2992 #endif
2994 if (rc && gtk_main_level() > 0)
2995 gtk_main_quit(); /* make sure cmd will be handled immediately */
2998 /* our usual callback function */
2999 static void
3000 entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
3002 gtk_widget_grab_focus(GTK_WIDGET(data));
3006 * Syncing the find/replace dialogs on the fly is utterly useless crack,
3007 * and causes nothing but problems. Please tell me a use case for which
3008 * you'd need both a find dialog and a find/replace one at the same time,
3009 * without being able to actually use them separately since they're syncing
3010 * all the time. I don't think it's worthwhile to fix this nonsense,
3011 * particularly evil incarnation of braindeadness, whatever; I'd much rather
3012 * see it extinguished from this planet. Thanks for listening. Sorry.
3014 static void
3015 entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
3017 const gchar *entry_text;
3018 gboolean nonempty;
3020 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
3022 if (!entry_text)
3023 return; /* shouldn't happen */
3025 nonempty = (entry_text[0] != '\0');
3027 if (dialog == find_widgets.dialog)
3029 gtk_widget_set_sensitive(find_widgets.find, nonempty);
3032 if (dialog == repl_widgets.dialog)
3034 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
3035 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
3036 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
3041 * ":helpfind"
3043 void
3044 ex_helpfind(eap)
3045 exarg_T *eap UNUSED;
3047 /* This will fail when menus are not loaded. Well, it's only for
3048 * backwards compatibility anyway. */
3049 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
3052 #if !defined(HAVE_GTK2) || defined(PROTO) /* Crack crack crack. Brrrr. */
3054 /* gui_gtk_position_in_parent
3056 * this function causes a child window to be placed within the boundary of
3057 * the parent (mainwin) window.
3059 * you can specify where the window will be positioned by the third argument
3060 * (defined in gui.h):
3061 * VW_POS_CENTER at center of parent window
3062 * VW_POS_MOUSE center of child at mouse position
3063 * VW_POS_TOP_CENTER top of child at top of parent centered
3064 * horizontally about the mouse.
3066 * NOTE: for this function to act as desired the child window must have a
3067 * window size requested. this can be accomplished by packing/placing
3068 * child widgets onto a gtk_frame widget rather than the gtk_window
3069 * widget...
3071 * brent -- dbv
3073 static void
3074 gui_gtk_position_in_parent(
3075 GtkWidget *parent,
3076 GtkWidget *child,
3077 gui_win_pos_T where)
3079 GtkRequisition c_size;
3080 gint xPm, yPm;
3081 gint xP, yP, wP, hP, pos_x, pos_y;
3083 /* make sure the child widget is set up then get its size. */
3084 gtk_widget_size_request(child, &c_size);
3086 /* get origin and size of parent window */
3087 gdk_window_get_origin((GdkWindow *)(parent->window), &xP, &yP);
3088 gdk_window_get_size((GdkWindow *)(parent->window), &wP, &hP);
3090 if (c_size.width > wP || c_size.height > hP)
3092 /* doh! maybe the user should consider giving gVim a little more
3093 * screen real estate */
3094 gtk_widget_set_uposition(child , xP + 2 , yP + 2);
3095 return;
3098 if (where == VW_POS_MOUSE)
3100 /* position window at mouse pointer */
3101 gtk_widget_get_pointer(parent, &xPm, &yPm);
3102 pos_x = xP + xPm - (c_size.width) / 2;
3103 pos_y = yP + yPm - (c_size.height) / 2;
3105 else
3107 /* set child x origin so it is in center of Vim window */
3108 pos_x = xP + (wP - c_size.width) / 2;
3110 if (where == VW_POS_TOP_CENTER)
3111 pos_y = yP + 2;
3112 else
3113 /* where == VW_POS_CENTER */
3114 pos_y = yP + (hP - c_size.height) / 2;
3117 /* now, make sure the window will be inside the Vim window... */
3118 if (pos_x < xP)
3119 pos_x = xP + 2;
3120 if (pos_y < yP)
3121 pos_y = yP + 2;
3122 if ((pos_x + c_size.width) > (wP + xP))
3123 pos_x = xP + wP - c_size.width - 2;
3124 /* Assume 'guiheadroom' indicates the title bar height... */
3125 if ((pos_y + c_size.height + p_ghr / 2) > (hP + yP))
3127 gtk_widget_set_uposition(child, pos_x, pos_y);
3130 #endif /* !HAVE_GTK2 */