1/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved		by Bram Moolenaar
4 *
5 * Do ":help uganda"  in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
13 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
14 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003  Jason Hildebrand  <jason@peaceworks.ca>
21 *		  Daniel Elstner  <daniel.elstner@gmx.net>
22 *
23 * Best supporting actor (He helped somewhat, aesthetically speaking):
24 * Maxime Romano <verbophobe@hotmail.com>
25 */
26
27#ifdef FEAT_GUI_GTK
28# include "gui_gtk_f.h"
29#endif
30
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
39
40#include "vim.h"
41
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
64
65#ifdef FEAT_GUI_GTK
66# include <gdk/gdkkeysyms.h>
67# include <gdk/gdk.h>
68# ifdef WIN3264
69#  include <gdk/gdkwin32.h>
70# else
71#  include <gdk/gdkx.h>
72# endif
73
74# include <gtk/gtk.h>
75#else
76/* define these items to be able to generate prototypes without GTK */
77typedef int GtkWidget;
78# define gpointer int
79# define guint8 int
80# define GdkPixmap int
81# define GdkBitmap int
82# define GtkIconFactory int
83# define GtkToolbar int
84# define GtkAdjustment int
85# define gboolean int
86# define GdkEventKey int
87# define CancelData int
88#endif
89
90static void entry_activate_cb(GtkWidget *widget, gpointer data);
91static void entry_changed_cb(GtkWidget *entry, GtkWidget *dialog);
92static void find_replace_cb(GtkWidget *widget, gpointer data);
93
94#if defined(FEAT_TOOLBAR)
95/*
96 * Table from BuiltIn## icon indices to GTK+ stock IDs.  Order must exactly
97 * match toolbar_names[] in menu.c!  All stock icons including the "vim-*"
98 * ones can be overridden in your gtkrc file.
99 */
100static const char * const menu_stock_ids[] =
101{
102    /* 00 */ GTK_STOCK_NEW,
103    /* 01 */ GTK_STOCK_OPEN,
104    /* 02 */ GTK_STOCK_SAVE,
105    /* 03 */ GTK_STOCK_UNDO,
106    /* 04 */ GTK_STOCK_REDO,
107    /* 05 */ GTK_STOCK_CUT,
108    /* 06 */ GTK_STOCK_COPY,
109    /* 07 */ GTK_STOCK_PASTE,
110    /* 08 */ GTK_STOCK_PRINT,
111    /* 09 */ GTK_STOCK_HELP,
112    /* 10 */ GTK_STOCK_FIND,
113    /* 11 */ "vim-save-all",
114    /* 12 */ "vim-session-save",
115    /* 13 */ "vim-session-new",
116    /* 14 */ "vim-session-load",
117    /* 15 */ GTK_STOCK_EXECUTE,
118    /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
119    /* 17 */ GTK_STOCK_CLOSE,		/* FIXME: fuzzy */
120    /* 18 */ "vim-window-maximize",
121    /* 19 */ "vim-window-minimize",
122    /* 20 */ "vim-window-split",
123    /* 21 */ "vim-shell",
124    /* 22 */ GTK_STOCK_GO_BACK,
125    /* 23 */ GTK_STOCK_GO_FORWARD,
126    /* 24 */ "vim-find-help",
127    /* 25 */ GTK_STOCK_CONVERT,
128    /* 26 */ GTK_STOCK_JUMP_TO,
129    /* 27 */ "vim-build-tags",
130    /* 28 */ "vim-window-split-vertical",
131    /* 29 */ "vim-window-maximize-width",
132    /* 30 */ "vim-window-minimize-width",
133    /* 31 */ GTK_STOCK_QUIT
134};
135
136    static void
137add_stock_icon(GtkIconFactory	*factory,
138	       const char	*stock_id,
139	       const guint8	*inline_data,
140	       int		data_length)
141{
142    GdkPixbuf	*pixbuf;
143    GtkIconSet	*icon_set;
144
145    pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
146    icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
147
148    gtk_icon_factory_add(factory, stock_id, icon_set);
149
150    gtk_icon_set_unref(icon_set);
151    g_object_unref(pixbuf);
152}
153
154    static int
155lookup_menu_iconfile(char_u *iconfile, char_u *dest)
156{
157    expand_env(iconfile, dest, MAXPATHL);
158
159    if (mch_isFullName(dest))
160    {
161	return vim_fexists(dest);
162    }
163    else
164    {
165	static const char   suffixes[][4] = {"png", "xpm", "bmp"};
166	char_u		    buf[MAXPATHL];
167	unsigned int	    i;
168
169	for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
170	    if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
171	    {
172		STRCPY(dest, buf);
173		return TRUE;
174	    }
175
176	return FALSE;
177    }
178}
179
180    static GtkWidget *
181load_menu_iconfile(char_u *name, GtkIconSize icon_size)
182{
183    GtkWidget	    *image = NULL;
184    GtkIconSet	    *icon_set;
185    GtkIconSource   *icon_source;
186
187    /*
188     * Rather than loading the icon directly into a GtkImage, create
189     * a new GtkIconSet and put it in there.  This way we can easily
190     * scale the toolbar icons on the fly when needed.
191     */
192    icon_set = gtk_icon_set_new();
193    icon_source = gtk_icon_source_new();
194
195    gtk_icon_source_set_filename(icon_source, (const char *)name);
196    gtk_icon_set_add_source(icon_set, icon_source);
197
198    image = gtk_image_new_from_icon_set(icon_set, icon_size);
199
200    gtk_icon_source_free(icon_source);
201    gtk_icon_set_unref(icon_set);
202
203    return image;
204}
205
206    static GtkWidget *
207create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
208{
209    GtkWidget	*image = NULL;
210    char_u	buf[MAXPATHL];
211
212    /* First use a specified "icon=" argument. */
213    if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
214	image = load_menu_iconfile(buf, icon_size);
215
216    /* If not found and not builtin specified try using the menu name. */
217    if (image == NULL && !menu->icon_builtin
218				     && lookup_menu_iconfile(menu->name, buf))
219	image = load_menu_iconfile(buf, icon_size);
220
221    /* Still not found?  Then use a builtin icon, a blank one as fallback. */
222    if (image == NULL)
223    {
224	const char  *stock_id;
225	const int   n_ids = G_N_ELEMENTS(menu_stock_ids);
226
227	if (menu->iconidx >= 0 && menu->iconidx < n_ids)
228	    stock_id = menu_stock_ids[menu->iconidx];
229	else
230	    stock_id = GTK_STOCK_MISSING_IMAGE;
231
232	image = gtk_image_new_from_stock(stock_id, icon_size);
233    }
234
235    return image;
236}
237
238    static gint
239toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
240			      GdkEventFocus *event UNUSED,
241			      gpointer data UNUSED)
242{
243    /* When we're in a GtkPlug, we don't have window focus events, only widget
244     * focus.  To emulate stand-alone gvim, if a button gets focus (e.g.,
245     * <Tab> into GtkPlug) immediately pass it to mainwin. */
246    if (gtk_socket_id != 0)
247	gtk_widget_grab_focus(gui.drawarea);
248
249    return TRUE;
250}
251#endif /* FEAT_TOOLBAR */
252
253#if defined(FEAT_TOOLBAR) || defined(PROTO)
254
255    void
256gui_gtk_register_stock_icons(void)
257{
258#   include "../pixmaps/stock_icons.h"
259    GtkIconFactory *factory;
260
261    factory = gtk_icon_factory_new();
262#   define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
263
264    ADD_ICON("vim-build-tags",		  stock_vim_build_tags);
265    ADD_ICON("vim-find-help",		  stock_vim_find_help);
266    ADD_ICON("vim-save-all",		  stock_vim_save_all);
267    ADD_ICON("vim-session-load",	  stock_vim_session_load);
268    ADD_ICON("vim-session-new",		  stock_vim_session_new);
269    ADD_ICON("vim-session-save",	  stock_vim_session_save);
270    ADD_ICON("vim-shell",		  stock_vim_shell);
271    ADD_ICON("vim-window-maximize",	  stock_vim_window_maximize);
272    ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
273    ADD_ICON("vim-window-minimize",	  stock_vim_window_minimize);
274    ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
275    ADD_ICON("vim-window-split",	  stock_vim_window_split);
276    ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
277
278#   undef ADD_ICON
279    gtk_icon_factory_add_default(factory);
280    g_object_unref(factory);
281}
282
283#endif /* FEAT_TOOLBAR */
284
285
286#if defined(FEAT_MENU) || defined(PROTO)
287
288/*
289 * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
290 * if necessary.  The caller must vim_free() the returned string.
291 *
292 *	Input	Output
293 *	_	__
294 *	&&	&
295 *	&	_	stripped if use_mnemonic == FALSE
296 *	<Tab>		end of menu label text
297 */
298    static char_u *
299translate_mnemonic_tag(char_u *name, int use_mnemonic)
300{
301    char_u  *buf;
302    char_u  *psrc;
303    char_u  *pdest;
304    int	    n_underscores = 0;
305
306    name = CONVERT_TO_UTF8(name);
307    if (name == NULL)
308	return NULL;
309
310    for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
311	if (*psrc == '_')
312	    ++n_underscores;
313
314    buf = alloc((unsigned)(psrc - name + n_underscores + 1));
315    if (buf != NULL)
316    {
317	pdest = buf;
318	for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
319	{
320	    if (*psrc == '_')
321	    {
322		*pdest++ = '_';
323		*pdest++ = '_';
324	    }
325	    else if (*psrc != '&')
326	    {
327		*pdest++ = *psrc;
328	    }
329	    else if (*(psrc + 1) == '&')
330	    {
331		*pdest++ = *psrc++;
332	    }
333	    else if (use_mnemonic)
334	    {
335		*pdest++ = '_';
336	    }
337	}
338	*pdest = NUL;
339    }
340
341    CONVERT_TO_UTF8_FREE(name);
342    return buf;
343}
344
345    static void
346menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
347{
348    GtkWidget	*box;
349    char_u	*text;
350    int		use_mnemonic;
351
352    /* It would be neat to have image menu items, but that would require major
353     * changes to Vim's menu system.  Not to mention that all the translations
354     * had to be updated. */
355    menu->id = gtk_menu_item_new();
356    box = gtk_hbox_new(FALSE, 20);
357
358    use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
359    text = translate_mnemonic_tag(menu->name, use_mnemonic);
360
361    menu->label = gtk_label_new_with_mnemonic((const char *)text);
362    vim_free(text);
363
364    gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
365
366    if (menu->actext != NULL && menu->actext[0] != NUL)
367    {
368	text = CONVERT_TO_UTF8(menu->actext);
369
370	gtk_box_pack_end(GTK_BOX(box),
371			 gtk_label_new((const char *)text),
372			 FALSE, FALSE, 0);
373
374	CONVERT_TO_UTF8_FREE(text);
375    }
376
377    gtk_container_add(GTK_CONTAINER(menu->id), box);
378    gtk_widget_show_all(menu->id);
379}
380
381    void
382gui_mch_add_menu(vimmenu_T *menu, int idx)
383{
384    vimmenu_T	*parent;
385    GtkWidget	*parent_widget;
386
387    if (menu->name[0] == ']' || menu_is_popup(menu->name))
388    {
389	menu->submenu_id = gtk_menu_new();
390	return;
391    }
392
393    parent = menu->parent;
394
395    if ((parent != NULL && parent->submenu_id == NULL)
396	    || !menu_is_menubar(menu->name))
397	return;
398
399    parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
400    menu_item_new(menu, parent_widget);
401
402    /* since the tearoff should always appear first, increment idx */
403    if (parent != NULL && !menu_is_popup(parent->name))
404	++idx;
405
406    gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
407
408    menu->submenu_id = gtk_menu_new();
409
410    gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
411    gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
412
413    menu->tearoff_handle = gtk_tearoff_menu_item_new();
414    if (vim_strchr(p_go, GO_TEAROFF) != NULL)
415	gtk_widget_show(menu->tearoff_handle);
416    gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
417}
418
419    static void
420menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
421{
422    gui_menu_cb((vimmenu_T *)data);
423}
424
425    void
426gui_mch_add_menu_item(vimmenu_T *menu, int idx)
427{
428    vimmenu_T *parent;
429
430    parent = menu->parent;
431
432# ifdef FEAT_TOOLBAR
433    if (menu_is_toolbar(parent->name))
434    {
435	GtkToolbar *toolbar;
436
437	toolbar = GTK_TOOLBAR(gui.toolbar);
438	menu->submenu_id = NULL;
439
440	if (menu_is_separator(menu->name))
441	{
442	    gtk_toolbar_insert_space(toolbar, idx);
443	    menu->id = NULL;
444	}
445	else
446	{
447	    char_u *text;
448	    char_u *tooltip;
449
450	    text    = CONVERT_TO_UTF8(menu->dname);
451	    tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
452	    if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
453		/* Invalid text, can happen when 'encoding' is changed.  Avoid
454		 * a nasty GTK error message, skip the tooltip. */
455		CONVERT_TO_UTF8_FREE(tooltip);
456
457	    menu->id = gtk_toolbar_insert_item(
458		    toolbar,
459		    (const char *)text,
460		    (const char *)tooltip,
461		    NULL,
462		    create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
463		    G_CALLBACK(&menu_item_activate),
464		    menu,
465		    idx);
466
467	    if (gtk_socket_id != 0)
468		gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
469			GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
470
471	    CONVERT_TO_UTF8_FREE(text);
472	    CONVERT_TO_UTF8_FREE(tooltip);
473	}
474    }
475    else
476# endif /* FEAT_TOOLBAR */
477    {
478	/* No parent, must be a non-menubar menu */
479	if (parent == NULL || parent->submenu_id == NULL)
480	    return;
481
482	/* Make place for the possible tearoff handle item.  Not in the popup
483	 * menu, it doesn't have a tearoff item. */
484	if (!menu_is_popup(parent->name))
485	    ++idx;
486
487	if (menu_is_separator(menu->name))
488	{
489	    /* Separator: Just add it */
490	    menu->id = gtk_menu_item_new();
491	    gtk_widget_set_sensitive(menu->id, FALSE);
492	    gtk_widget_show(menu->id);
493	    gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
494
495	    return;
496	}
497
498	/* Add textual menu item. */
499	menu_item_new(menu, parent->submenu_id);
500	gtk_widget_show(menu->id);
501	gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
502
503	if (menu->id != NULL)
504	    gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
505			       GTK_SIGNAL_FUNC(menu_item_activate), menu);
506    }
507}
508#endif /* FEAT_MENU */
509
510
511    void
512gui_mch_set_text_area_pos(int x, int y, int w, int h)
513{
514    gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
515}
516
517
518#if defined(FEAT_MENU) || defined(PROTO)
519/*
520 * Enable or disable accelerators for the toplevel menus.
521 */
522    void
523gui_gtk_set_mnemonics(int enable)
524{
525    vimmenu_T	*menu;
526    char_u	*name;
527
528    for (menu = root_menu; menu != NULL; menu = menu->next)
529    {
530	if (menu->id == NULL)
531	    continue;
532
533	name = translate_mnemonic_tag(menu->name, enable);
534	gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
535					 (const char *)name);
536	vim_free(name);
537    }
538}
539
540    static void
541recurse_tearoffs(vimmenu_T *menu, int val)
542{
543    for (; menu != NULL; menu = menu->next)
544    {
545	if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
546		&& menu->name[0] != ']' && !menu_is_popup(menu->name))
547	{
548	    if (val)
549		gtk_widget_show(menu->tearoff_handle);
550	    else
551		gtk_widget_hide(menu->tearoff_handle);
552	}
553	recurse_tearoffs(menu->children, val);
554    }
555}
556
557    void
558gui_mch_toggle_tearoffs(int enable)
559{
560    recurse_tearoffs(root_menu, enable);
561}
562#endif /* FEAT_MENU */
563
564#if defined(FEAT_TOOLBAR)
565    static int
566get_menu_position(vimmenu_T *menu)
567{
568    vimmenu_T	*node;
569    int		idx = 0;
570
571    for (node = menu->parent->children; node != menu; node = node->next)
572    {
573	g_return_val_if_fail(node != NULL, -1);
574	++idx;
575    }
576
577    return idx;
578}
579#endif /* FEAT_TOOLBAR */
580
581
582#if defined(FEAT_TOOLBAR) || defined(PROTO)
583    void
584gui_mch_menu_set_tip(vimmenu_T *menu)
585{
586    if (menu->id != NULL && menu->parent != NULL
587	    && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
588    {
589	char_u *tooltip;
590
591	tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
592	if (tooltip == NULL || utf_valid_string(tooltip, NULL))
593	    /* Only set the tooltip when it's valid utf-8. */
594	gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
595			     menu->id, (const char *)tooltip, NULL);
596	CONVERT_TO_UTF8_FREE(tooltip);
597    }
598}
599#endif /* FEAT_TOOLBAR */
600
601
602#if defined(FEAT_MENU) || defined(PROTO)
603/*
604 * Destroy the machine specific menu widget.
605 */
606    void
607gui_mch_destroy_menu(vimmenu_T *menu)
608{
609# ifdef FEAT_TOOLBAR
610    if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
611    {
612	if (menu_is_separator(menu->name))
613	    gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
614				     get_menu_position(menu));
615	else if (menu->id != NULL)
616	    gtk_widget_destroy(menu->id);
617    }
618    else
619# endif /* FEAT_TOOLBAR */
620    {
621	if (menu->submenu_id != NULL)
622	    gtk_widget_destroy(menu->submenu_id);
623
624	if (menu->id != NULL)
625	    gtk_widget_destroy(menu->id);
626    }
627
628    menu->submenu_id = NULL;
629    menu->id = NULL;
630}
631#endif /* FEAT_MENU */
632
633
634/*
635 * Scrollbar stuff.
636 */
637    void
638gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
639{
640    if (sb->id != NULL)
641    {
642	GtkAdjustment *adjustment;
643
644	adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
645
646	adjustment->lower = 0.0;
647	adjustment->value = val;
648	adjustment->upper = max + 1;
649	adjustment->page_size = size;
650	adjustment->page_increment = size < 3L ? 1L : size - 2L;
651	adjustment->step_increment = 1.0;
652
653	g_signal_handler_block(GTK_OBJECT(adjustment),
654						      (gulong)sb->handler_id);
655	gtk_adjustment_changed(adjustment);
656	g_signal_handler_unblock(GTK_OBJECT(adjustment),
657						      (gulong)sb->handler_id);
658    }
659}
660
661    void
662gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
663{
664    if (sb->id != NULL)
665	gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
666}
667
668/*
669 * Take action upon scrollbar dragging.
670 */
671    static void
672adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
673{
674    scrollbar_T	*sb;
675    long	value;
676    int		dragging = FALSE;
677
678#ifdef FEAT_XIM
679    /* cancel any preediting */
680    if (im_is_preediting())
681	xim_reset();
682#endif
683
684    sb = gui_find_scrollbar((long)data);
685    value = (long)adjustment->value;
686    /*
687     * The dragging argument must be right for the scrollbar to work with
688     * closed folds.  This isn't documented, hopefully this will keep on
689     * working in later GTK versions.
690     *
691     * FIXME: Well, it doesn't work in GTK2. :)
692     * HACK: Get the mouse pointer position, if it appears to be on an arrow
693     * button set "dragging" to FALSE.  This assumes square buttons!
694     */
695    if (sb != NULL)
696    {
697	dragging = TRUE;
698
699	if (sb->wp != NULL)
700	{
701	    int			x;
702	    int			y;
703	    GdkModifierType	state;
704	    int			width;
705	    int			height;
706
707	    /* vertical scrollbar: need to set "dragging" properly in case
708	     * there are closed folds. */
709	    gdk_window_get_pointer(sb->id->window, &x, &y, &state);
710	    gdk_window_get_size(sb->id->window, &width, &height);
711	    if (x >= 0 && x < width && y >= 0 && y < height)
712	    {
713		if (y < width)
714		{
715		    /* up arrow: move one (closed fold) line up */
716		    dragging = FALSE;
717		    value = sb->wp->w_topline - 2;
718		}
719		else if (y > height - width)
720		{
721		    /* down arrow: move one (closed fold) line down */
722		    dragging = FALSE;
723		    value = sb->wp->w_topline;
724		}
725	    }
726	}
727    }
728
729    gui_drag_scrollbar(sb, value, dragging);
730}
731
732/* SBAR_VERT or SBAR_HORIZ */
733    void
734gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
735{
736    if (orient == SBAR_HORIZ)
737	sb->id = gtk_hscrollbar_new(NULL);
738    else if (orient == SBAR_VERT)
739	sb->id = gtk_vscrollbar_new(NULL);
740
741    if (sb->id != NULL)
742    {
743	GtkAdjustment *adjustment;
744
745	GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
746	gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
747
748	adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
749
750	sb->handler_id = gtk_signal_connect(
751			     GTK_OBJECT(adjustment), "value_changed",
752			     GTK_SIGNAL_FUNC(adjustment_value_changed),
753			     GINT_TO_POINTER(sb->ident));
754	gui_mch_update();
755    }
756}
757
758#if defined(FEAT_WINDOWS) || defined(PROTO)
759    void
760gui_mch_destroy_scrollbar(scrollbar_T *sb)
761{
762    if (sb->id != NULL)
763    {
764	gtk_widget_destroy(sb->id);
765	sb->id = NULL;
766    }
767    gui_mch_update();
768}
769#endif
770
771#if defined(FEAT_BROWSE) || defined(PROTO)
772/*
773 * Implementation of the file selector related stuff
774 */
775#if GTK_CHECK_VERSION(2,4,0)
776# define USE_FILE_CHOOSER
777#endif
778
779#ifndef USE_FILE_CHOOSER
780    static void
781browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
782{
783    gui_T *vw = (gui_T *)cbdata;
784
785    if (vw->browse_fname != NULL)
786	g_free(vw->browse_fname);
787
788    vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
789					GTK_FILE_SELECTION(vw->filedlg)));
790    gtk_widget_hide(vw->filedlg);
791}
792
793    static void
794browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
795{
796    gui_T *vw = (gui_T *)cbdata;
797
798    if (vw->browse_fname != NULL)
799    {
800	g_free(vw->browse_fname);
801	vw->browse_fname = NULL;
802    }
803    gtk_widget_hide(vw->filedlg);
804}
805
806    static gboolean
807browse_destroy_cb(GtkWidget *widget UNUSED)
808{
809    if (gui.browse_fname != NULL)
810    {
811	g_free(gui.browse_fname);
812	gui.browse_fname = NULL;
813    }
814    gui.filedlg = NULL;
815    gtk_main_quit();
816    return FALSE;
817}
818#endif
819
820/*
821 * Put up a file requester.
822 * Returns the selected name in allocated memory, or NULL for Cancel.
823 * saving,			select file to write
824 * title			title for the window
825 * dflt				default name
826 * ext				not used (extension added)
827 * initdir			initial directory, NULL for current dir
828 * filter			not used (file name filter)
829 */
830    char_u *
831gui_mch_browse(int saving UNUSED,
832	       char_u *title,
833	       char_u *dflt,
834	       char_u *ext UNUSED,
835	       char_u *initdir,
836	       char_u *filter UNUSED)
837{
838#ifdef USE_FILE_CHOOSER
839    GtkWidget		*fc;
840#endif
841    char_u		dirbuf[MAXPATHL];
842
843    title = CONVERT_TO_UTF8(title);
844
845    /* GTK has a bug, it only works with an absolute path. */
846    if (initdir == NULL || *initdir == NUL)
847	mch_dirname(dirbuf, MAXPATHL);
848    else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
849	dirbuf[0] = NUL;
850    /* Always need a trailing slash for a directory. */
851    add_pathsep(dirbuf);
852
853    /* If our pointer is currently hidden, then we should show it. */
854    gui_mch_mousehide(FALSE);
855
856#ifdef USE_FILE_CHOOSER
857    /* We create the dialog each time, so that the button text can be "Open"
858     * or "Save" according to the action. */
859    fc = gtk_file_chooser_dialog_new((const gchar *)title,
860	    GTK_WINDOW(gui.mainwin),
861	    saving ? GTK_FILE_CHOOSER_ACTION_SAVE
862					   : GTK_FILE_CHOOSER_ACTION_OPEN,
863	    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
864	    saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
865	    NULL);
866    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
867						       (const gchar *)dirbuf);
868    if (saving && dflt != NULL && *dflt != NUL)
869	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
870
871    gui.browse_fname = NULL;
872    if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
873    {
874	char *filename;
875
876	filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
877	gui.browse_fname = (char_u *)g_strdup(filename);
878	g_free(filename);
879    }
880    gtk_widget_destroy(GTK_WIDGET(fc));
881
882#else
883
884    if (gui.filedlg == NULL)
885    {
886	GtkFileSelection	*fs;	/* shortcut */
887
888	gui.filedlg = gtk_file_selection_new((const gchar *)title);
889	gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
890	gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
891						     GTK_WINDOW(gui.mainwin));
892	fs = GTK_FILE_SELECTION(gui.filedlg);
893
894	gtk_container_border_width(GTK_CONTAINER(fs), 4);
895
896	gtk_signal_connect(GTK_OBJECT(fs->ok_button),
897		"clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
898	gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
899		"clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
900	/* gtk_signal_connect() doesn't work for destroy, it causes a hang */
901	gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
902		"destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
903		GTK_OBJECT(gui.filedlg));
904    }
905    else
906	gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
907
908    /* Concatenate "initdir" and "dflt". */
909    if (dflt != NULL && *dflt != NUL
910			      && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
911	STRCAT(dirbuf, dflt);
912
913    gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
914						      (const gchar *)dirbuf);
915
916    gtk_widget_show(gui.filedlg);
917    gtk_main();
918#endif
919
920    CONVERT_TO_UTF8_FREE(title);
921    if (gui.browse_fname == NULL)
922	return NULL;
923
924    /* shorten the file name if possible */
925    return vim_strsave(shorten_fname1(gui.browse_fname));
926}
927
928/*
929 * Put up a directory selector
930 * Returns the selected name in allocated memory, or NULL for Cancel.
931 * title			title for the window
932 * dflt				default name
933 * initdir			initial directory, NULL for current dir
934 */
935    char_u *
936gui_mch_browsedir(
937	       char_u *title,
938	       char_u *initdir)
939{
940# if defined(GTK_FILE_CHOOSER)	    /* Only in GTK 2.4 and later. */
941    char_u		dirbuf[MAXPATHL];
942    char_u		*p;
943    GtkWidget		*dirdlg;	    /* file selection dialog */
944    char_u		*dirname = NULL;
945
946    title = CONVERT_TO_UTF8(title);
947
948    dirdlg = gtk_file_chooser_dialog_new(
949	    (const gchar *)title,
950	    GTK_WINDOW(gui.mainwin),
951	    GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
952	    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
953	    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
954	    NULL);
955
956    CONVERT_TO_UTF8_FREE(title);
957
958    /* if our pointer is currently hidden, then we should show it. */
959    gui_mch_mousehide(FALSE);
960
961    /* GTK appears to insist on an absolute path. */
962    if (initdir == NULL || *initdir == NUL
963	       || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
964	mch_dirname(dirbuf, MAXPATHL - 10);
965
966    /* Always need a trailing slash for a directory.
967     * Also add a dummy file name, so that we get to the directory. */
968    add_pathsep(dirbuf);
969    STRCAT(dirbuf, "@zd(*&1|");
970    gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
971						      (const gchar *)dirbuf);
972
973    /* Run the dialog. */
974    if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
975	dirname = (char_u *)gtk_file_chooser_get_filename(
976						    GTK_FILE_CHOOSER(dirdlg));
977    gtk_widget_destroy(dirdlg);
978    if (dirname == NULL)
979	return NULL;
980
981    /* shorten the file name if possible */
982    p = vim_strsave(shorten_fname1(dirname));
983    g_free(dirname);
984    return p;
985
986# else
987    /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
988    return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
989# endif
990}
991
992
993#endif	/* FEAT_BROWSE */
994
995#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
996
997    static GtkWidget *
998create_message_dialog(int type, char_u *title, char_u *message)
999{
1000    GtkWidget	    *dialog;
1001    GtkMessageType  message_type;
1002
1003    switch (type)
1004    {
1005	case VIM_ERROR:	    message_type = GTK_MESSAGE_ERROR;	 break;
1006	case VIM_WARNING:   message_type = GTK_MESSAGE_WARNING;	 break;
1007	case VIM_QUESTION:  message_type = GTK_MESSAGE_QUESTION; break;
1008	default:	    message_type = GTK_MESSAGE_INFO;	 break;
1009    }
1010
1011    message = CONVERT_TO_UTF8(message);
1012    dialog  = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1013				     GTK_DIALOG_DESTROY_WITH_PARENT,
1014				     message_type,
1015				     GTK_BUTTONS_NONE,
1016				     "%s", (const char *)message);
1017    CONVERT_TO_UTF8_FREE(message);
1018
1019    if (title != NULL)
1020    {
1021	title = CONVERT_TO_UTF8(title);
1022	gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1023	CONVERT_TO_UTF8_FREE(title);
1024    }
1025    else if (type == VIM_GENERIC)
1026    {
1027	gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1028    }
1029
1030    return dialog;
1031}
1032
1033/*
1034 * Split up button_string into individual button labels by inserting
1035 * NUL bytes.  Also replace the Vim-style mnemonic accelerator prefix
1036 * '&' with '_'.  button_string must point to allocated memory!
1037 * Return an allocated array of pointers into button_string.
1038 */
1039    static char **
1040split_button_string(char_u *button_string, int *n_buttons)
1041{
1042    char	    **array;
1043    char_u	    *p;
1044    unsigned int    count = 1;
1045
1046    for (p = button_string; *p != NUL; ++p)
1047	if (*p == DLG_BUTTON_SEP)
1048	    ++count;
1049
1050    array = (char **)alloc((count + 1) * sizeof(char *));
1051    count = 0;
1052
1053    if (array != NULL)
1054    {
1055	array[count++] = (char *)button_string;
1056	for (p = button_string; *p != NUL; )
1057	{
1058	    if (*p == DLG_BUTTON_SEP)
1059	    {
1060		*p++ = NUL;
1061		array[count++] = (char *)p;
1062	    }
1063	    else if (*p == DLG_HOTKEY_CHAR)
1064		*p++ = '_';
1065	    else
1066		mb_ptr_adv(p);
1067	}
1068	array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1069    }
1070
1071    *n_buttons = count;
1072    return array;
1073}
1074
1075    static char **
1076split_button_translation(const char *message)
1077{
1078    char    **buttons = NULL;
1079    char_u  *str;
1080    int	    n_buttons = 0;
1081    int	    n_expected = 1;
1082
1083    for (str = (char_u *)message; *str != NUL; ++str)
1084	if (*str == DLG_BUTTON_SEP)
1085	    ++n_expected;
1086
1087    str = (char_u *)_(message);
1088    if (str != NULL)
1089    {
1090	if (output_conv.vc_type != CONV_NONE)
1091	    str = string_convert(&output_conv, str, NULL);
1092	else
1093	    str = vim_strsave(str);
1094
1095	if (str != NULL)
1096	    buttons = split_button_string(str, &n_buttons);
1097    }
1098    /*
1099     * Uh-oh... this should never ever happen.	But we don't wanna crash
1100     * if the translation is broken, thus fall back to the untranslated
1101     * buttons string in case of emergency.
1102     */
1103    if (buttons == NULL || n_buttons != n_expected)
1104    {
1105	vim_free(buttons);
1106	vim_free(str);
1107	buttons = NULL;
1108	str = vim_strsave((char_u *)message);
1109
1110	if (str != NULL)
1111	    buttons = split_button_string(str, &n_buttons);
1112	if (buttons == NULL)
1113	    vim_free(str);
1114    }
1115
1116    return buttons;
1117}
1118
1119    static int
1120button_equal(const char *a, const char *b)
1121{
1122    while (*a != '\0' && *b != '\0')
1123    {
1124	if (*a == '_' && *++a == '\0')
1125	    break;
1126	if (*b == '_' && *++b == '\0')
1127	    break;
1128
1129	if (g_unichar_tolower(g_utf8_get_char(a))
1130		!= g_unichar_tolower(g_utf8_get_char(b)))
1131	    return FALSE;
1132
1133	a = g_utf8_next_char(a);
1134	b = g_utf8_next_char(b);
1135    }
1136
1137    return (*a == '\0' && *b == '\0');
1138}
1139
1140    static void
1141dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1142{
1143    char    **ok;
1144    char    **ync;  /* "yes no cancel" */
1145    char    **buttons;
1146    int	    n_buttons = 0;
1147    int	    idx;
1148
1149    button_string = vim_strsave(button_string); /* must be writable */
1150    if (button_string == NULL)
1151	return;
1152
1153    /* Check 'v' flag in 'guioptions': vertical button placement. */
1154    if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1155    {
1156	GtkWidget	*vbutton_box;
1157
1158	vbutton_box = gtk_vbutton_box_new();
1159	gtk_widget_show(vbutton_box);
1160	gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1161						 vbutton_box, TRUE, FALSE, 0);
1162	/* Overrule the "action_area" value, hopefully this works... */
1163	GTK_DIALOG(dialog)->action_area = vbutton_box;
1164    }
1165
1166    /*
1167     * Yes this is ugly, I don't particularly like it either.  But doing it
1168     * this way has the compelling advantage that translations need not to
1169     * be touched at all.  See below what 'ok' and 'ync' are used for.
1170     */
1171    ok	    = split_button_translation(N_("&Ok"));
1172    ync     = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1173    buttons = split_button_string(button_string, &n_buttons);
1174
1175    /*
1176     * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1177     * environment.  Don't hit me -- it's all about consistency.
1178     * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1179     * other way around...
1180     */
1181    for (idx = 1; idx <= n_buttons; ++idx)
1182    {
1183	char	*label;
1184	char_u	*label8;
1185
1186	label = buttons[idx - 1];
1187	/*
1188	 * Perform some guesswork to find appropriate stock items for the
1189	 * buttons.  We have to compare with a sample of the translated
1190	 * button string to get things right.  Yes, this is hackish :/
1191	 *
1192	 * But even the common button labels aren't necessarily translated,
1193	 * since anyone can create their own dialogs using Vim functions.
1194	 * Thus we have to check for those too.
1195	 */
1196	if (ok != NULL && ync != NULL) /* almost impossible to fail */
1197	{
1198	    if	    (button_equal(label, ok[0]))    label = GTK_STOCK_OK;
1199	    else if (button_equal(label, ync[0]))   label = GTK_STOCK_YES;
1200	    else if (button_equal(label, ync[1]))   label = GTK_STOCK_NO;
1201	    else if (button_equal(label, ync[2]))   label = GTK_STOCK_CANCEL;
1202	    else if (button_equal(label, "Ok"))     label = GTK_STOCK_OK;
1203	    else if (button_equal(label, "Yes"))    label = GTK_STOCK_YES;
1204	    else if (button_equal(label, "No"))     label = GTK_STOCK_NO;
1205	    else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
1206	}
1207	label8 = CONVERT_TO_UTF8((char_u *)label);
1208	gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
1209	CONVERT_TO_UTF8_FREE(label8);
1210    }
1211
1212    if (ok != NULL)
1213	vim_free(*ok);
1214    if (ync != NULL)
1215	vim_free(*ync);
1216    vim_free(ok);
1217    vim_free(ync);
1218    vim_free(buttons);
1219    vim_free(button_string);
1220}
1221
1222/*
1223 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1224 * I'm not sure if it's a wise idea to do this.  However, the old GTK+ 1.2
1225 * GUI used to work this way, and I consider the impact on UI consistency
1226 * low enough to justify implementing this as a special Vim feature.
1227 */
1228typedef struct _DialogInfo
1229{
1230    int		ignore_enter;	    /* no default button, ignore "Enter" */
1231    int		noalt;		    /* accept accelerators without Alt */
1232    GtkDialog	*dialog;	    /* Widget of the dialog */
1233} DialogInfo;
1234
1235    static gboolean
1236dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1237{
1238    DialogInfo *di = (DialogInfo *)data;
1239
1240    /* Ignore hitting Enter (or Space) when there is no default button. */
1241    if (di->ignore_enter && (event->keyval == GDK_Return
1242						     || event->keyval == ' '))
1243	return TRUE;
1244    else    /* A different key was pressed, return to normal behavior */
1245	di->ignore_enter = FALSE;
1246
1247    /* Close the dialog when hitting "Esc". */
1248    if (event->keyval == GDK_Escape)
1249    {
1250	gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1251	return TRUE;
1252    }
1253
1254    if (di->noalt
1255	      && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1256    {
1257	return gtk_window_mnemonic_activate(
1258		   GTK_WINDOW(widget), event->keyval,
1259		   gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1260    }
1261
1262    return FALSE; /* continue emission */
1263}
1264
1265    int
1266gui_mch_dialog(int	type,	    /* type of dialog */
1267	       char_u	*title,	    /* title of dialog */
1268	       char_u	*message,   /* message text */
1269	       char_u	*buttons,   /* names of buttons */
1270	       int	def_but,    /* default button */
1271	       char_u	*textfield) /* text for textfield or NULL */
1272{
1273    GtkWidget	*dialog;
1274    GtkWidget	*entry = NULL;
1275    char_u	*text;
1276    int		response;
1277    DialogInfo  dialoginfo;
1278
1279    dialog = create_message_dialog(type, title, message);
1280    dialoginfo.dialog = GTK_DIALOG(dialog);
1281    dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1282
1283    if (textfield != NULL)
1284    {
1285	GtkWidget *alignment;
1286
1287	entry = gtk_entry_new();
1288	gtk_widget_show(entry);
1289
1290	text = CONVERT_TO_UTF8(textfield);
1291	gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1292	CONVERT_TO_UTF8_FREE(text);
1293
1294	alignment = gtk_alignment_new((float)0.5, (float)0.5,
1295						      (float)1.0, (float)1.0);
1296	gtk_container_add(GTK_CONTAINER(alignment), entry);
1297	gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1298	gtk_widget_show(alignment);
1299
1300	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1301			   alignment, TRUE, FALSE, 0);
1302	dialoginfo.noalt = FALSE;
1303    }
1304    else
1305	dialoginfo.noalt = TRUE;
1306
1307    /* Allow activation of mnemonic accelerators without pressing <Alt> when
1308     * there is no textfield.  Handle pressing Esc. */
1309    g_signal_connect(G_OBJECT(dialog), "key_press_event",
1310			 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1311
1312    if (def_but > 0)
1313    {
1314	gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1315	dialoginfo.ignore_enter = FALSE;
1316    }
1317    else
1318	/* No default button, ignore pressing Enter. */
1319	dialoginfo.ignore_enter = TRUE;
1320
1321    /* Show the mouse pointer if it's currently hidden. */
1322    gui_mch_mousehide(FALSE);
1323
1324    response = gtk_dialog_run(GTK_DIALOG(dialog));
1325
1326    /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
1327    if (response != GTK_RESPONSE_NONE)
1328    {
1329	if (response == GTK_RESPONSE_ACCEPT)	    /* Enter pressed */
1330	    response = def_but;
1331	if (textfield != NULL)
1332	{
1333	    text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1334	    text = CONVERT_FROM_UTF8(text);
1335
1336	    vim_strncpy(textfield, text, IOSIZE - 1);
1337
1338	    CONVERT_FROM_UTF8_FREE(text);
1339	}
1340	gtk_widget_destroy(dialog);
1341    }
1342
1343    return response > 0 ? response : 0;
1344}
1345
1346#endif /* FEAT_GUI_DIALOG */
1347
1348
1349#if defined(FEAT_MENU) || defined(PROTO)
1350
1351    void
1352gui_mch_show_popupmenu(vimmenu_T *menu)
1353{
1354# if defined(FEAT_XIM)
1355    /*
1356     * Append a submenu for selecting an input method.	This is
1357     * currently the only way to switch input methods at runtime.
1358     */
1359    if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1360					 "vim-has-im-menu") == NULL)
1361    {
1362	GtkWidget   *menuitem;
1363	GtkWidget   *submenu;
1364	char_u	    *name;
1365
1366	menuitem = gtk_separator_menu_item_new();
1367	gtk_widget_show(menuitem);
1368	gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1369
1370	name = (char_u *)_("Input _Methods");
1371	name = CONVERT_TO_UTF8(name);
1372	menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1373	CONVERT_TO_UTF8_FREE(name);
1374	gtk_widget_show(menuitem);
1375
1376	submenu = gtk_menu_new();
1377	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1378	gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1379
1380	gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1381					     GTK_MENU_SHELL(submenu));
1382	g_object_set_data(G_OBJECT(menu->submenu_id),
1383			  "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1384    }
1385# endif /* FEAT_XIM */
1386
1387    gtk_menu_popup(GTK_MENU(menu->submenu_id),
1388		   NULL, NULL,
1389		   (GtkMenuPositionFunc)NULL, NULL,
1390		   3U, (guint32)GDK_CURRENT_TIME);
1391}
1392
1393/* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1394 * popup_menu_position_func(). */
1395static int popup_mouse_pos;
1396
1397/*
1398 * Menu position callback; used by gui_make_popup() to place the menu
1399 * at the current text cursor position.
1400 *
1401 * Note: The push_in output argument seems to affect scrolling of huge
1402 * menus that don't fit on the screen.	Leave it at the default for now.
1403 */
1404    static void
1405popup_menu_position_func(GtkMenu *menu UNUSED,
1406			 gint *x, gint *y,
1407			 gboolean *push_in UNUSED,
1408			 gpointer user_data UNUSED)
1409{
1410    gdk_window_get_origin(gui.drawarea->window, x, y);
1411
1412    if (popup_mouse_pos)
1413    {
1414	int	mx, my;
1415
1416	gui_mch_getmouse(&mx, &my);
1417	*x += mx;
1418	*y += my;
1419    }
1420    else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
1421    {
1422	/* Find the cursor position in the current window */
1423	*x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
1424	*y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1425    }
1426}
1427
1428    void
1429gui_make_popup(char_u *path_name, int mouse_pos)
1430{
1431    vimmenu_T *menu;
1432
1433    popup_mouse_pos = mouse_pos;
1434
1435    menu = gui_find_menu(path_name);
1436
1437    if (menu != NULL && menu->submenu_id != NULL)
1438    {
1439	gtk_menu_popup(GTK_MENU(menu->submenu_id),
1440		       NULL, NULL,
1441		       &popup_menu_position_func, NULL,
1442		       0U, (guint32)GDK_CURRENT_TIME);
1443    }
1444}
1445
1446#endif /* FEAT_MENU */
1447
1448
1449/*
1450 * We don't create it twice.
1451 */
1452
1453typedef struct _SharedFindReplace
1454{
1455    GtkWidget *dialog;	/* the main dialog widget */
1456    GtkWidget *wword;	/* 'Whole word only' check button */
1457    GtkWidget *mcase;	/* 'Match case' check button */
1458    GtkWidget *up;	/* search direction 'Up' radio button */
1459    GtkWidget *down;	/* search direction 'Down' radio button */
1460    GtkWidget *what;	/* 'Find what' entry text widget */
1461    GtkWidget *with;	/* 'Replace with' entry text widget */
1462    GtkWidget *find;	/* 'Find Next' action button */
1463    GtkWidget *replace;	/* 'Replace With' action button */
1464    GtkWidget *all;	/* 'Replace All' action button */
1465} SharedFindReplace;
1466
1467static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1468static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1469
1470    static int
1471find_key_press_event(
1472		GtkWidget	*widget UNUSED,
1473		GdkEventKey	*event,
1474		SharedFindReplace *frdp)
1475{
1476    /* If the user is holding one of the key modifiers we will just bail out,
1477     * thus preserving the possibility of normal focus traversal.
1478     */
1479    if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
1480	return FALSE;
1481
1482    /* the Escape key synthesizes a cancellation action */
1483    if (event->keyval == GDK_Escape)
1484    {
1485	gtk_widget_hide(frdp->dialog);
1486
1487	return TRUE;
1488    }
1489
1490    /* It would be delightful if it where possible to do search history
1491     * operations on the K_UP and K_DOWN keys here.
1492     */
1493
1494    return FALSE;
1495}
1496
1497    static GtkWidget *
1498create_image_button(const char *stock_id, const char *label)
1499{
1500    char_u	*text;
1501    GtkWidget	*box;
1502    GtkWidget	*alignment;
1503    GtkWidget	*button;
1504
1505    text = CONVERT_TO_UTF8((char_u *)label);
1506
1507    box = gtk_hbox_new(FALSE, 3);
1508    gtk_box_pack_start(GTK_BOX(box),
1509		       gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
1510		       FALSE, FALSE, 0);
1511    gtk_box_pack_start(GTK_BOX(box),
1512		       gtk_label_new((const char *)text),
1513		       FALSE, FALSE, 0);
1514
1515    CONVERT_TO_UTF8_FREE(text);
1516
1517    alignment = gtk_alignment_new((float)0.5, (float)0.5,
1518						      (float)0.0, (float)0.0);
1519    gtk_container_add(GTK_CONTAINER(alignment), box);
1520    gtk_widget_show_all(alignment);
1521
1522    button = gtk_button_new();
1523    gtk_container_add(GTK_CONTAINER(button), alignment);
1524
1525    return button;
1526}
1527
1528/*
1529 * This is currently only used by find_replace_dialog_create(), and
1530 * I'd really like to keep it at that.	In other words: don't spread
1531 * this nasty hack all over the code.  Think twice.
1532 */
1533    static const char *
1534convert_localized_message(char_u **buffer, const char *message)
1535{
1536    if (output_conv.vc_type == CONV_NONE)
1537	return message;
1538
1539    vim_free(*buffer);
1540    *buffer = string_convert(&output_conv, (char_u *)message, NULL);
1541
1542    return (const char *)*buffer;
1543}
1544
1545    static void
1546find_replace_dialog_create(char_u *arg, int do_replace)
1547{
1548    GtkWidget	*hbox;		/* main top down box */
1549    GtkWidget	*actionarea;
1550    GtkWidget	*table;
1551    GtkWidget	*tmp;
1552    GtkWidget	*vbox;
1553    gboolean	sensitive;
1554    SharedFindReplace *frdp;
1555    char_u	*entry_text;
1556    int		wword = FALSE;
1557    int		mcase = !p_ic;
1558    char_u	*conv_buffer = NULL;
1559#   define CONV(message) convert_localized_message(&conv_buffer, (message))
1560
1561    frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
1562
1563    /* Get the search string to use. */
1564    entry_text = get_find_dialog_text(arg, &wword, &mcase);
1565
1566    if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
1567    {
1568	char_u *old_text = entry_text;
1569	entry_text = string_convert(&output_conv, entry_text, NULL);
1570	vim_free(old_text);
1571    }
1572
1573    /*
1574     * If the dialog already exists, just raise it.
1575     */
1576    if (frdp->dialog)
1577    {
1578	if (entry_text != NULL)
1579	{
1580	    gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1581	    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1582							     (gboolean)wword);
1583	    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1584							     (gboolean)mcase);
1585	}
1586	gtk_window_present(GTK_WINDOW(frdp->dialog));
1587	vim_free(entry_text);
1588	return;
1589    }
1590
1591    frdp->dialog = gtk_dialog_new();
1592    gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
1593    gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
1594    gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
1595
1596    if (do_replace)
1597    {
1598	gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1599			     CONV(_("VIM - Search and Replace...")));
1600    }
1601    else
1602    {
1603	gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1604			     CONV(_("VIM - Search...")));
1605    }
1606
1607    hbox = gtk_hbox_new(FALSE, 0);
1608    gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
1609    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
1610
1611    if (do_replace)
1612	table = gtk_table_new(1024, 4, FALSE);
1613    else
1614	table = gtk_table_new(1024, 3, FALSE);
1615    gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
1616    gtk_container_border_width(GTK_CONTAINER(table), 4);
1617
1618    tmp = gtk_label_new(CONV(_("Find what:")));
1619    gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1620    gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
1621		     GTK_FILL, GTK_EXPAND, 2, 2);
1622    frdp->what = gtk_entry_new();
1623    sensitive = (entry_text != NULL && entry_text[0] != NUL);
1624    if (entry_text != NULL)
1625	gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1626    gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
1627		       GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
1628    gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
1629				 GTK_SIGNAL_FUNC(find_key_press_event),
1630				 (gpointer) frdp);
1631    gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
1632		     GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1633
1634    if (do_replace)
1635    {
1636	tmp = gtk_label_new(CONV(_("Replace with:")));
1637	gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1638	gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
1639			 GTK_FILL, GTK_EXPAND, 2, 2);
1640	frdp->with = gtk_entry_new();
1641	gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
1642			   GTK_SIGNAL_FUNC(find_replace_cb),
1643			   GINT_TO_POINTER(FRD_R_FINDNEXT));
1644	gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
1645				 GTK_SIGNAL_FUNC(find_key_press_event),
1646				 (gpointer) frdp);
1647	gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
1648			 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1649
1650	/*
1651	 * Make the entry activation only change the input focus onto the
1652	 * with item.
1653	 */
1654	gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1655			   GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
1656    }
1657    else
1658    {
1659	/*
1660	 * Make the entry activation do the search.
1661	 */
1662	gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1663			   GTK_SIGNAL_FUNC(find_replace_cb),
1664			   GINT_TO_POINTER(FRD_FINDNEXT));
1665    }
1666
1667    /* whole word only button */
1668    frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
1669    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1670							(gboolean)wword);
1671    if (do_replace)
1672	gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
1673			 GTK_FILL, GTK_EXPAND, 2, 2);
1674    else
1675	gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
1676			 GTK_FILL, GTK_EXPAND, 2, 2);
1677
1678    /* match case button */
1679    frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
1680    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1681							     (gboolean)mcase);
1682    if (do_replace)
1683	gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
1684			 GTK_FILL, GTK_EXPAND, 2, 2);
1685    else
1686	gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
1687			 GTK_FILL, GTK_EXPAND, 2, 2);
1688
1689    tmp = gtk_frame_new(CONV(_("Direction")));
1690    if (do_replace)
1691	gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
1692			 GTK_FILL, GTK_FILL, 2, 2);
1693    else
1694	gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
1695			 GTK_FILL, GTK_FILL, 2, 2);
1696    vbox = gtk_vbox_new(FALSE, 0);
1697    gtk_container_border_width(GTK_CONTAINER(vbox), 0);
1698    gtk_container_add(GTK_CONTAINER(tmp), vbox);
1699
1700    /* 'Up' and 'Down' buttons */
1701    frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
1702    gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
1703    frdp->down = gtk_radio_button_new_with_label(
1704			gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
1705			CONV(_("Down")));
1706    gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
1707    gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
1708    gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
1709
1710    /* vbox to hold the action buttons */
1711    actionarea = gtk_vbutton_box_new();
1712    gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
1713    gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
1714
1715    /* 'Find Next' button */
1716    frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
1717    gtk_widget_set_sensitive(frdp->find, sensitive);
1718
1719    gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
1720		       GTK_SIGNAL_FUNC(find_replace_cb),
1721		       (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
1722				    : GINT_TO_POINTER(FRD_FINDNEXT));
1723
1724    GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
1725    gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
1726    gtk_widget_grab_default(frdp->find);
1727
1728    if (do_replace)
1729    {
1730	/* 'Replace' button */
1731	frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
1732	gtk_widget_set_sensitive(frdp->replace, sensitive);
1733	GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
1734	gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
1735	gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
1736			   GTK_SIGNAL_FUNC(find_replace_cb),
1737			   GINT_TO_POINTER(FRD_REPLACE));
1738
1739	/* 'Replace All' button */
1740	frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
1741	gtk_widget_set_sensitive(frdp->all, sensitive);
1742	GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
1743	gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
1744	gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
1745			   GTK_SIGNAL_FUNC(find_replace_cb),
1746			   GINT_TO_POINTER(FRD_REPLACEALL));
1747    }
1748
1749    /* 'Cancel' button */
1750    tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
1751    GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
1752    gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
1753    gtk_signal_connect_object(GTK_OBJECT(tmp),
1754			      "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
1755			      GTK_OBJECT(frdp->dialog));
1756    gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
1757			      "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1758			      GTK_OBJECT(frdp->dialog));
1759
1760    tmp = gtk_vseparator_new();
1761    gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
1762
1763    /* Suppress automatic show of the unused action area */
1764    gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
1765    gtk_widget_show_all(hbox);
1766    gtk_widget_show(frdp->dialog);
1767
1768    vim_free(entry_text);
1769    vim_free(conv_buffer);
1770#undef CONV
1771}
1772
1773    void
1774gui_mch_find_dialog(exarg_T *eap)
1775{
1776    if (gui.in_use)
1777	find_replace_dialog_create(eap->arg, FALSE);
1778}
1779
1780    void
1781gui_mch_replace_dialog(exarg_T *eap)
1782{
1783    if (gui.in_use)
1784	find_replace_dialog_create(eap->arg, TRUE);
1785}
1786
1787/*
1788 * Callback for actions of the find and replace dialogs
1789 */
1790    static void
1791find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
1792{
1793    int			flags;
1794    char_u		*find_text;
1795    char_u		*repl_text;
1796    gboolean		direction_down;
1797    SharedFindReplace	*sfr;
1798    int			rc;
1799
1800    flags = (int)(long)data;	    /* avoid a lint warning here */
1801
1802    /* Get the search/replace strings from the dialog */
1803    if (flags == FRD_FINDNEXT)
1804    {
1805	repl_text = NULL;
1806	sfr = &find_widgets;
1807    }
1808    else
1809    {
1810	repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
1811	sfr = &repl_widgets;
1812    }
1813
1814    find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
1815    direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
1816
1817    if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
1818	flags |= FRD_WHOLE_WORD;
1819    if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
1820	flags |= FRD_MATCH_CASE;
1821
1822    repl_text = CONVERT_FROM_UTF8(repl_text);
1823    find_text = CONVERT_FROM_UTF8(find_text);
1824    rc = gui_do_findrepl(flags, find_text, repl_text, direction_down);
1825    CONVERT_FROM_UTF8_FREE(repl_text);
1826    CONVERT_FROM_UTF8_FREE(find_text);
1827}
1828
1829/* our usual callback function */
1830    static void
1831entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
1832{
1833    gtk_widget_grab_focus(GTK_WIDGET(data));
1834}
1835
1836/*
1837 * Syncing the find/replace dialogs on the fly is utterly useless crack,
1838 * and causes nothing but problems.  Please tell me a use case for which
1839 * you'd need both a find dialog and a find/replace one at the same time,
1840 * without being able to actually use them separately since they're syncing
1841 * all the time.  I don't think it's worthwhile to fix this nonsense,
1842 * particularly evil incarnation of braindeadness, whatever; I'd much rather
1843 * see it extinguished from this planet.  Thanks for listening.  Sorry.
1844 */
1845    static void
1846entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
1847{
1848    const gchar	*entry_text;
1849    gboolean	nonempty;
1850
1851    entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
1852
1853    if (!entry_text)
1854	return;			/* shouldn't happen */
1855
1856    nonempty = (entry_text[0] != '\0');
1857
1858    if (dialog == find_widgets.dialog)
1859    {
1860	gtk_widget_set_sensitive(find_widgets.find, nonempty);
1861    }
1862
1863    if (dialog == repl_widgets.dialog)
1864    {
1865	gtk_widget_set_sensitive(repl_widgets.find, nonempty);
1866	gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
1867	gtk_widget_set_sensitive(repl_widgets.all, nonempty);
1868    }
1869}
1870
1871/*
1872 * ":helpfind"
1873 */
1874    void
1875ex_helpfind(eap)
1876    exarg_T	*eap UNUSED;
1877{
1878    /* This will fail when menus are not loaded.  Well, it's only for
1879     * backwards compatibility anyway. */
1880    do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
1881}
1882