xref: /vim-8.2.3635/src/gui_gtk.c (revision 84e9ade8)
1 /* vi:set ts=8 sts=4 sw=4 noet:
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 <[email protected]>
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  <[email protected]>
21  *		  Daniel Elstner  <[email protected]>
22  *
23  * Best supporting actor (He helped somewhat, aesthetically speaking):
24  * Maxime Romano <[email protected]>
25  *
26  * Support for GTK+ 3 was added by:
27  *
28  * 2016  Kazunobu Kuriyama  <[email protected]>
29  *
30  * With the help of Marius Gedminas and the word of Bram Moolenaar,
31  * "Let's give this some time to mature."
32  */
33 
34 #include "vim.h"
35 
36 #ifdef FEAT_GUI_GTK
37 # include "gui_gtk_f.h"
38 #endif
39 
40 // GTK defines MAX and MIN, but some system header files as well.  Undefine
41 // them and don't use them.
42 #ifdef MIN
43 # undef MIN
44 #endif
45 #ifdef MAX
46 # undef MAX
47 #endif
48 
49 #ifdef FEAT_GUI_GNOME
50 // Gnome redefines _() and N_().  Grrr...
51 # ifdef _
52 #  undef _
53 # endif
54 # ifdef N_
55 #  undef N_
56 # endif
57 # ifdef textdomain
58 #  undef textdomain
59 # endif
60 # ifdef bindtextdomain
61 #  undef bindtextdomain
62 # endif
63 # ifdef bind_textdomain_codeset
64 #  undef bind_textdomain_codeset
65 # endif
66 # if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
67 #  define ENABLE_NLS	// so the texts in the dialog boxes are translated
68 # endif
69 # include <gnome.h>
70 #endif
71 
72 #ifdef FEAT_GUI_GTK
73 # if GTK_CHECK_VERSION(3,0,0)
74 #  include <gdk/gdkkeysyms-compat.h>
75 # else
76 #  include <gdk/gdkkeysyms.h>
77 # endif
78 # include <gdk/gdk.h>
79 # ifdef MSWIN
80 #  include <gdk/gdkwin32.h>
81 # else
82 #  include <gdk/gdkx.h>
83 # endif
84 
85 # include <gtk/gtk.h>
86 #else
87 // define these items to be able to generate prototypes without GTK
88 typedef int GtkWidget;
89 # define gpointer int
90 # define guint8 int
91 # define GdkPixmap int
92 # define GdkBitmap int
93 # define GtkIconFactory int
94 # define GtkToolbar int
95 # define GtkAdjustment int
96 # define gboolean int
97 # define GdkEventKey int
98 # define CancelData int
99 #endif
100 
101 static void entry_activate_cb(GtkWidget *widget, gpointer data);
102 static void entry_changed_cb(GtkWidget *entry, GtkWidget *dialog);
103 static void find_replace_cb(GtkWidget *widget, gpointer data);
104 #if defined(FEAT_BROWSE) || defined(PROTO)
105 static void recent_func_log_func(
106 	const gchar *log_domain,
107 	GLogLevelFlags log_level,
108 	const gchar *message,
109 	gpointer user_data);
110 #endif
111 
112 #if defined(FEAT_TOOLBAR)
113 /*
114  * Table from BuiltIn## icon indices to GTK+ stock IDs.  Order must exactly
115  * match toolbar_names[] in menu.c!  All stock icons including the "vim-*"
116  * ones can be overridden in your gtkrc file.
117  */
118 # if GTK_CHECK_VERSION(3,10,0)
119 static const char * const menu_themed_names[] =
120 {
121     /* 00 */ "document-new",		// sub. GTK_STOCK_NEW
122     /* 01 */ "document-open",		// sub. GTK_STOCK_OPEN
123     /* 02 */ "document-save",		// sub. GTK_STOCK_SAVE
124     /* 03 */ "edit-undo",		// sub. GTK_STOCK_UNDO
125     /* 04 */ "edit-redo",		// sub. GTK_STOCK_REDO
126     /* 05 */ "edit-cut",		// sub. GTK_STOCK_CUT
127     /* 06 */ "edit-copy",		// sub. GTK_STOCK_COPY
128     /* 07 */ "edit-paste",		// sub. GTK_STOCK_PASTE
129     /* 08 */ "document-print",		// sub. GTK_STOCK_PRINT
130     /* 09 */ "help-browser",		// sub. GTK_STOCK_HELP
131     /* 10 */ "edit-find",		// sub. GTK_STOCK_FIND
132 #  if GTK_CHECK_VERSION(3,14,0)
133     // Use the file names in gui_gtk_res.xml, cutting off the extension.
134     // Similar changes follow.
135     /* 11 */ "stock_vim_save_all",
136     /* 12 */ "stock_vim_session_save",
137     /* 13 */ "stock_vim_session_new",
138     /* 14 */ "stock_vim_session_load",
139 #  else
140     /* 11 */ "vim-save-all",
141     /* 12 */ "vim-session-save",
142     /* 13 */ "vim-session-new",
143     /* 14 */ "vim-session-load",
144 #  endif
145     /* 15 */ "system-run",		// sub. GTK_STOCK_EXECUTE
146     /* 16 */ "edit-find-replace",	// sub. GTK_STOCK_FIND_AND_REPLACE
147     /* 17 */ "window-close",		// sub. GTK_STOCK_CLOSE, FIXME: fuzzy
148 #  if GTK_CHECK_VERSION(3,14,0)
149     /* 18 */ "stock_vim_window_maximize",
150     /* 19 */ "stock_vim_window_minimize",
151     /* 20 */ "stock_vim_window_split",
152     /* 21 */ "stock_vim_shell",
153 #  else
154     /* 18 */ "vim-window-maximize",
155     /* 19 */ "vim-window-minimize",
156     /* 20 */ "vim-window-split",
157     /* 21 */ "vim-shell",
158 #  endif
159     /* 22 */ "go-previous",		// sub. GTK_STOCK_GO_BACK
160     /* 23 */ "go-next",			// sub. GTK_STOCK_GO_FORWARD
161 #  if GTK_CHECK_VERSION(3,14,0)
162     /* 24 */ "stock_vim_find_help",
163 #  else
164     /* 24 */ "vim-find-help",
165 #  endif
166     /* 25 */ "gtk-convert",		// sub. GTK_STOCK_CONVERT
167     /* 26 */ "go-jump",			// sub. GTK_STOCK_JUMP_TO
168 #  if GTK_CHECK_VERSION(3,14,0)
169     /* 27 */ "stock_vim_build_tags",
170     /* 28 */ "stock_vim_window_split_vertical",
171     /* 29 */ "stock_vim_window_maximize_width",
172     /* 30 */ "stock_vim_window_minimize_width",
173 #  else
174     /* 27 */ "vim-build-tags",
175     /* 28 */ "vim-window-split-vertical",
176     /* 29 */ "vim-window-maximize-width",
177     /* 30 */ "vim-window-minimize-width",
178 #  endif
179     /* 31 */ "application-exit",	// GTK_STOCK_QUIT
180 };
181 # else // !GTK_CHECK_VERSION(3,10,0)
182 static const char * const menu_stock_ids[] =
183 {
184     /* 00 */ GTK_STOCK_NEW,
185     /* 01 */ GTK_STOCK_OPEN,
186     /* 02 */ GTK_STOCK_SAVE,
187     /* 03 */ GTK_STOCK_UNDO,
188     /* 04 */ GTK_STOCK_REDO,
189     /* 05 */ GTK_STOCK_CUT,
190     /* 06 */ GTK_STOCK_COPY,
191     /* 07 */ GTK_STOCK_PASTE,
192     /* 08 */ GTK_STOCK_PRINT,
193     /* 09 */ GTK_STOCK_HELP,
194     /* 10 */ GTK_STOCK_FIND,
195     /* 11 */ "vim-save-all",
196     /* 12 */ "vim-session-save",
197     /* 13 */ "vim-session-new",
198     /* 14 */ "vim-session-load",
199     /* 15 */ GTK_STOCK_EXECUTE,
200     /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
201     /* 17 */ GTK_STOCK_CLOSE,		// FIXME: fuzzy
202     /* 18 */ "vim-window-maximize",
203     /* 19 */ "vim-window-minimize",
204     /* 20 */ "vim-window-split",
205     /* 21 */ "vim-shell",
206     /* 22 */ GTK_STOCK_GO_BACK,
207     /* 23 */ GTK_STOCK_GO_FORWARD,
208     /* 24 */ "vim-find-help",
209     /* 25 */ GTK_STOCK_CONVERT,
210     /* 26 */ GTK_STOCK_JUMP_TO,
211     /* 27 */ "vim-build-tags",
212     /* 28 */ "vim-window-split-vertical",
213     /* 29 */ "vim-window-maximize-width",
214     /* 30 */ "vim-window-minimize-width",
215     /* 31 */ GTK_STOCK_QUIT
216 };
217 # endif // !GTK_CHECK_VERSION(3,10,0)
218 
219 # ifdef USE_GRESOURCE
220 #  if !GTK_CHECK_VERSION(3,10,0)
221 typedef struct IconNames {
222     const char *icon_name;
223     const char *file_name;
224 } IconNames;
225 
226 static IconNames stock_vim_icons[] = {
227     { "vim-build-tags", "stock_vim_build_tags.png" },
228     { "vim-find-help", "stock_vim_find_help.png" },
229     { "vim-save-all", "stock_vim_save_all.png" },
230     { "vim-session-load", "stock_vim_session_load.png" },
231     { "vim-session-new", "stock_vim_session_new.png" },
232     { "vim-session-save", "stock_vim_session_save.png" },
233     { "vim-shell", "stock_vim_shell.png" },
234     { "vim-window-maximize", "stock_vim_window_maximize.png" },
235     { "vim-window-maximize-width", "stock_vim_window_maximize_width.png" },
236     { "vim-window-minimize", "stock_vim_window_minimize.png" },
237     { "vim-window-minimize-width", "stock_vim_window_minimize_width.png" },
238     { "vim-window-split", "stock_vim_window_split.png" },
239     { "vim-window-split-vertical", "stock_vim_window_split_vertical.png" },
240     { NULL, NULL }
241 };
242 #  endif
243 # endif // USE_G_RESOURCE
244 
245 # ifndef USE_GRESOURCE
246     static void
247 add_stock_icon(GtkIconFactory	*factory,
248 	       const char	*stock_id,
249 	       const guint8	*inline_data,
250 	       int		data_length)
251 {
252     GdkPixbuf	*pixbuf;
253     GtkIconSet	*icon_set;
254 
255     pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
256     icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
257 
258     gtk_icon_factory_add(factory, stock_id, icon_set);
259 
260     gtk_icon_set_unref(icon_set);
261     g_object_unref(pixbuf);
262 }
263 # endif
264 
265     static int
266 lookup_menu_iconfile(char_u *iconfile, char_u *dest)
267 {
268     expand_env(iconfile, dest, MAXPATHL);
269 
270     if (mch_isFullName(dest))
271     {
272 	return vim_fexists(dest);
273     }
274     else
275     {
276 	static const char   suffixes[][4] = {"png", "xpm", "bmp"};
277 	char_u		    buf[MAXPATHL];
278 	unsigned int	    i;
279 
280 	for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
281 	    if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
282 	    {
283 		STRCPY(dest, buf);
284 		return TRUE;
285 	    }
286 
287 	return FALSE;
288     }
289 }
290 
291     static GtkWidget *
292 load_menu_iconfile(char_u *name, GtkIconSize icon_size)
293 {
294     GtkWidget	    *image = NULL;
295 # if GTK_CHECK_VERSION(3,10,0)
296     int		     pixel_size = -1;
297 
298     switch (icon_size)
299     {
300 	case GTK_ICON_SIZE_MENU:
301 	    pixel_size = 16;
302 	    break;
303 	case GTK_ICON_SIZE_SMALL_TOOLBAR:
304 	    pixel_size = 16;
305 	    break;
306 	case GTK_ICON_SIZE_LARGE_TOOLBAR:
307 	    pixel_size = 24;
308 	    break;
309 	case GTK_ICON_SIZE_BUTTON:
310 	    pixel_size = 16;
311 	    break;
312 	case GTK_ICON_SIZE_DND:
313 	    pixel_size = 32;
314 	    break;
315 	case GTK_ICON_SIZE_DIALOG:
316 	    pixel_size = 48;
317 	    break;
318 	case GTK_ICON_SIZE_INVALID:
319 	    // FALLTHROUGH
320 	default:
321 	    pixel_size = 0;
322 	    break;
323     }
324 
325     if (pixel_size > 0 || pixel_size == -1)
326     {
327 	GdkPixbuf * const pixbuf
328 	    = gdk_pixbuf_new_from_file_at_scale((const char *)name,
329 		pixel_size, pixel_size, TRUE, NULL);
330 	if (pixbuf != NULL)
331 	{
332 	    image = gtk_image_new_from_pixbuf(pixbuf);
333 	    g_object_unref(pixbuf);
334 	}
335     }
336     if (image == NULL)
337 	image = gtk_image_new_from_icon_name("image-missing", icon_size);
338 
339     return image;
340 # else // !GTK_CHECK_VERSION(3,10,0)
341     GtkIconSet	    *icon_set;
342     GtkIconSource   *icon_source;
343 
344     /*
345      * Rather than loading the icon directly into a GtkImage, create
346      * a new GtkIconSet and put it in there.  This way we can easily
347      * scale the toolbar icons on the fly when needed.
348      */
349     icon_set = gtk_icon_set_new();
350     icon_source = gtk_icon_source_new();
351 
352     gtk_icon_source_set_filename(icon_source, (const char *)name);
353     gtk_icon_set_add_source(icon_set, icon_source);
354 
355     image = gtk_image_new_from_icon_set(icon_set, icon_size);
356 
357     gtk_icon_source_free(icon_source);
358     gtk_icon_set_unref(icon_set);
359 
360     return image;
361 # endif // !GTK_CHECK_VERSION(3,10,0)
362 }
363 
364     static GtkWidget *
365 create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
366 {
367     GtkWidget	*image = NULL;
368     char_u	buf[MAXPATHL];
369 
370     // First use a specified "icon=" argument.
371     if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
372 	image = load_menu_iconfile(buf, icon_size);
373 
374     // If not found and not builtin specified try using the menu name.
375     if (image == NULL && !menu->icon_builtin
376 				     && lookup_menu_iconfile(menu->name, buf))
377 	image = load_menu_iconfile(buf, icon_size);
378 
379     // Still not found?  Then use a builtin icon, a blank one as fallback.
380     if (image == NULL)
381     {
382 # if GTK_CHECK_VERSION(3,10,0)
383 	const char *icon_name = NULL;
384 	const int   n_names = G_N_ELEMENTS(menu_themed_names);
385 
386 	if (menu->iconidx >= 0 && menu->iconidx < n_names)
387 	    icon_name = menu_themed_names[menu->iconidx];
388 	if (icon_name == NULL)
389 	    icon_name = "image-missing";
390 
391 	image = gtk_image_new_from_icon_name(icon_name, icon_size);
392 # else
393 	const char  *stock_id;
394 	const int   n_ids = G_N_ELEMENTS(menu_stock_ids);
395 
396 	if (menu->iconidx >= 0 && menu->iconidx < n_ids)
397 	    stock_id = menu_stock_ids[menu->iconidx];
398 	else
399 	    stock_id = GTK_STOCK_MISSING_IMAGE;
400 
401 	image = gtk_image_new_from_stock(stock_id, icon_size);
402 # endif
403     }
404 
405     return image;
406 }
407 
408     static gint
409 toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
410 			      GdkEventFocus *event UNUSED,
411 			      gpointer data UNUSED)
412 {
413     // When we're in a GtkPlug, we don't have window focus events, only widget
414     // focus.  To emulate stand-alone gvim, if a button gets focus (e.g.,
415     // <Tab> into GtkPlug) immediately pass it to mainwin.
416     if (gtk_socket_id != 0)
417 	gtk_widget_grab_focus(gui.drawarea);
418 
419     return TRUE;
420 }
421 #endif // FEAT_TOOLBAR
422 
423 #if defined(FEAT_TOOLBAR) || defined(PROTO)
424 
425     void
426 gui_gtk_register_stock_icons(void)
427 {
428 # ifndef USE_GRESOURCE
429 #  include "../pixmaps/stock_icons.h"
430     GtkIconFactory *factory;
431 
432     factory = gtk_icon_factory_new();
433 #  define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
434 
435     ADD_ICON("vim-build-tags",		  stock_vim_build_tags);
436     ADD_ICON("vim-find-help",		  stock_vim_find_help);
437     ADD_ICON("vim-save-all",		  stock_vim_save_all);
438     ADD_ICON("vim-session-load",	  stock_vim_session_load);
439     ADD_ICON("vim-session-new",		  stock_vim_session_new);
440     ADD_ICON("vim-session-save",	  stock_vim_session_save);
441     ADD_ICON("vim-shell",		  stock_vim_shell);
442     ADD_ICON("vim-window-maximize",	  stock_vim_window_maximize);
443     ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
444     ADD_ICON("vim-window-minimize",	  stock_vim_window_minimize);
445     ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
446     ADD_ICON("vim-window-split",	  stock_vim_window_split);
447     ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
448 
449 #  undef ADD_ICON
450 
451     gtk_icon_factory_add_default(factory);
452     g_object_unref(factory);
453 # else // defined(USE_GRESOURCE)
454     const char * const path_prefix = "/org/vim/gui/icon";
455 #  if GTK_CHECK_VERSION(3,14,0)
456     GdkScreen    *screen = NULL;
457     GtkIconTheme *icon_theme = NULL;
458 
459     if (GTK_IS_WIDGET(gui.mainwin))
460 	screen = gtk_widget_get_screen(gui.mainwin);
461     else
462 	screen = gdk_screen_get_default();
463     icon_theme = gtk_icon_theme_get_for_screen(screen);
464     gtk_icon_theme_add_resource_path(icon_theme, path_prefix);
465 #  elif GTK_CHECK_VERSION(3,0,0)
466     IconNames *names;
467 
468     for (names = stock_vim_icons; names->icon_name != NULL; names++)
469     {
470 	char path[MAXPATHL];
471 
472 	vim_snprintf(path, MAXPATHL, "%s/%s", path_prefix, names->file_name);
473 	GdkPixbuf *pixbuf = NULL;
474 	pixbuf = gdk_pixbuf_new_from_resource(path, NULL);
475 	if (pixbuf != NULL)
476 	{
477 	    const gint size = MAX(gdk_pixbuf_get_width(pixbuf),
478 				  gdk_pixbuf_get_height(pixbuf));
479 	    if (size > 16)
480 	    {
481 		// An icon theme is supposed to provide fixed-size
482 		// image files for each size, e.g., 16, 22, 24, ...
483 		// Naturally, in contrast to GtkIconSet, GtkIconTheme
484 		// won't prepare size variants for us out of a single
485 		// fixed-size image.
486 		//
487 		// Currently, Vim provides 24x24 images only while the
488 		// icon size on the menu and the toolbar is set to 16x16
489 		// by default.
490 		//
491 		// Resize them by ourselves until we have our own fully
492 		// fledged icon theme.
493 		GdkPixbuf *src = pixbuf;
494 		pixbuf = gdk_pixbuf_scale_simple(src,
495 						 16, 16,
496 						 GDK_INTERP_BILINEAR);
497 		if (pixbuf == NULL)
498 		    pixbuf = src;
499 		else
500 		    g_object_unref(src);
501 	    }
502 	    gtk_icon_theme_add_builtin_icon(names->icon_name, size, pixbuf);
503 	    g_object_unref(pixbuf);
504 	}
505     }
506 #  else // !GTK_CHECK_VERSION(3,0.0)
507     GtkIconFactory * const factory = gtk_icon_factory_new();
508     IconNames *names;
509 
510     for (names = stock_vim_icons; names->icon_name != NULL; names++)
511     {
512 	char path[MAXPATHL];
513 	GdkPixbuf *pixbuf;
514 
515 	vim_snprintf(path, MAXPATHL, "%s/%s", path_prefix, names->file_name);
516 	pixbuf = gdk_pixbuf_new_from_resource(path, NULL);
517 	if (pixbuf != NULL)
518 	{
519 	    GtkIconSet *icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
520 	    gtk_icon_factory_add(factory, names->icon_name, icon_set);
521 	    gtk_icon_set_unref(icon_set);
522 	    g_object_unref(pixbuf);
523 	}
524     }
525 
526     gtk_icon_factory_add_default(factory);
527     g_object_unref(factory);
528 #  endif // !GTK_CHECK_VERSION(3,0,0)
529 # endif // defined(USE_GRESOURCE)
530 }
531 
532 #endif // FEAT_TOOLBAR
533 
534 #if defined(FEAT_MENU) || defined(PROTO)
535 
536 /*
537  * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
538  * if necessary.  The caller must vim_free() the returned string.
539  *
540  *	Input	Output
541  *	_	__
542  *	&&	&
543  *	&	_	stripped if use_mnemonic == FALSE
544  *	<Tab>		end of menu label text
545  */
546     static char_u *
547 translate_mnemonic_tag(char_u *name, int use_mnemonic)
548 {
549     char_u  *buf;
550     char_u  *psrc;
551     char_u  *pdest;
552     int	    n_underscores = 0;
553 
554     name = CONVERT_TO_UTF8(name);
555     if (name == NULL)
556 	return NULL;
557 
558     for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
559 	if (*psrc == '_')
560 	    ++n_underscores;
561 
562     buf = alloc(psrc - name + n_underscores + 1);
563     if (buf != NULL)
564     {
565 	pdest = buf;
566 	for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
567 	{
568 	    if (*psrc == '_')
569 	    {
570 		*pdest++ = '_';
571 		*pdest++ = '_';
572 	    }
573 	    else if (*psrc != '&')
574 	    {
575 		*pdest++ = *psrc;
576 	    }
577 	    else if (*(psrc + 1) == '&')
578 	    {
579 		*pdest++ = *psrc++;
580 	    }
581 	    else if (use_mnemonic)
582 	    {
583 		*pdest++ = '_';
584 	    }
585 	}
586 	*pdest = NUL;
587     }
588 
589     CONVERT_TO_UTF8_FREE(name);
590     return buf;
591 }
592 
593     static void
594 menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
595 {
596     GtkWidget	*box;
597     char_u	*text;
598     int		use_mnemonic;
599 
600     // It would be neat to have image menu items, but that would require major
601     // changes to Vim's menu system.  Not to mention that all the translations
602     // had to be updated.
603     menu->id = gtk_menu_item_new();
604 # if GTK_CHECK_VERSION(3,2,0)
605     box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 20);
606     gtk_box_set_homogeneous(GTK_BOX(box), FALSE);
607 # else
608     box = gtk_hbox_new(FALSE, 20);
609 # endif
610 
611     use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
612     text = translate_mnemonic_tag(menu->name, use_mnemonic);
613 
614     menu->label = gtk_label_new_with_mnemonic((const char *)text);
615     vim_free(text);
616 
617     gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
618 
619     if (menu->actext != NULL && menu->actext[0] != NUL)
620     {
621 	text = CONVERT_TO_UTF8(menu->actext);
622 
623 	gtk_box_pack_end(GTK_BOX(box),
624 			 gtk_label_new((const char *)text),
625 			 FALSE, FALSE, 0);
626 
627 	CONVERT_TO_UTF8_FREE(text);
628     }
629 
630     gtk_container_add(GTK_CONTAINER(menu->id), box);
631     gtk_widget_show_all(menu->id);
632 }
633 
634     void
635 gui_mch_add_menu(vimmenu_T *menu, int idx)
636 {
637     vimmenu_T	*parent;
638     GtkWidget	*parent_widget;
639 
640     if (menu->name[0] == ']' || menu_is_popup(menu->name))
641     {
642 	menu->submenu_id = gtk_menu_new();
643 	return;
644     }
645 
646     parent = menu->parent;
647 
648     if ((parent != NULL && parent->submenu_id == NULL)
649 	    || !menu_is_menubar(menu->name))
650 	return;
651 
652     parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
653     menu_item_new(menu, parent_widget);
654 
655 # if !GTK_CHECK_VERSION(3,4,0)
656     // since the tearoff should always appear first, increment idx
657     if (parent != NULL && !menu_is_popup(parent->name))
658 	++idx;
659 # endif
660 
661     gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
662 
663     menu->submenu_id = gtk_menu_new();
664 
665     gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
666     gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
667 
668 # if !GTK_CHECK_VERSION(3,4,0)
669     menu->tearoff_handle = gtk_tearoff_menu_item_new();
670     if (vim_strchr(p_go, GO_TEAROFF) != NULL)
671 	gtk_widget_show(menu->tearoff_handle);
672 #  if GTK_CHECK_VERSION(3,0,0)
673     gtk_menu_shell_prepend(GTK_MENU_SHELL(menu->submenu_id),
674 	    menu->tearoff_handle);
675 #  else
676     gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
677 #  endif
678 # endif
679 }
680 
681     static void
682 menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
683 {
684     gui_menu_cb((vimmenu_T *)data);
685 }
686 
687     static void
688 menu_item_select(GtkWidget *widget UNUSED, gpointer data)
689 {
690     vimmenu_T	*menu;
691     char_u	*tooltip;
692     static int	did_msg = FALSE;
693 
694     if (State & CMDLINE)
695 	return;
696     menu = (vimmenu_T *)data;
697     tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
698     if (tooltip != NULL && utf_valid_string(tooltip, NULL))
699     {
700 	msg((char *)tooltip);
701 	did_msg = TRUE;
702 	setcursor();
703 	out_flush_cursor(TRUE, FALSE);
704     }
705     else if (did_msg)
706     {
707 	msg("");
708 	did_msg = FALSE;
709 	setcursor();
710 	out_flush_cursor(TRUE, FALSE);
711     }
712     CONVERT_TO_UTF8_FREE(tooltip);
713 }
714 
715     void
716 gui_mch_add_menu_item(vimmenu_T *menu, int idx)
717 {
718     vimmenu_T *parent;
719 
720     parent = menu->parent;
721 
722 # ifdef FEAT_TOOLBAR
723     if (menu_is_toolbar(parent->name))
724     {
725 	GtkToolbar *toolbar;
726 
727 	toolbar = GTK_TOOLBAR(gui.toolbar);
728 	menu->submenu_id = NULL;
729 
730 	if (menu_is_separator(menu->name))
731 	{
732 #  if GTK_CHECK_VERSION(3,0,0)
733 	    GtkToolItem *item = gtk_separator_tool_item_new();
734 	    gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),
735 		    TRUE);
736 	    gtk_tool_item_set_expand(GTK_TOOL_ITEM(item), FALSE);
737 	    gtk_widget_show(GTK_WIDGET(item));
738 
739 	    gtk_toolbar_insert(toolbar, item, idx);
740 #  else
741 	    gtk_toolbar_insert_space(toolbar, idx);
742 #  endif
743 	    menu->id = NULL;
744 	}
745 	else
746 	{
747 	    char_u *text;
748 	    char_u *tooltip;
749 
750 	    text    = CONVERT_TO_UTF8(menu->dname);
751 	    tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
752 	    if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
753 		// Invalid text, can happen when 'encoding' is changed.  Avoid
754 		// a nasty GTK error message, skip the tooltip.
755 		CONVERT_TO_UTF8_FREE(tooltip);
756 
757 #  if GTK_CHECK_VERSION(3,0,0)
758 	    {
759 		GtkWidget *icon;
760 		GtkToolItem *item;
761 
762 		icon = create_menu_icon(menu,
763 			gtk_toolbar_get_icon_size(toolbar));
764 		item = gtk_tool_button_new(icon, (const gchar *)text);
765 		gtk_tool_item_set_tooltip_text(item, (const gchar *)tooltip);
766 		g_signal_connect(G_OBJECT(item), "clicked",
767 			G_CALLBACK(&menu_item_activate), menu);
768 		gtk_widget_show_all(GTK_WIDGET(item));
769 
770 		gtk_toolbar_insert(toolbar, item, idx);
771 
772 		menu->id = GTK_WIDGET(item);
773 	    }
774 #  else
775 	    menu->id = gtk_toolbar_insert_item(
776 		    toolbar,
777 		    (const char *)text,
778 		    (const char *)tooltip,
779 		    NULL,
780 		    create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
781 		    G_CALLBACK(&menu_item_activate),
782 		    menu,
783 		    idx);
784 #  endif
785 
786 	    if (gtk_socket_id != 0)
787 		g_signal_connect(G_OBJECT(menu->id), "focus-in-event",
788 			G_CALLBACK(toolbar_button_focus_in_event), NULL);
789 
790 	    CONVERT_TO_UTF8_FREE(text);
791 	    CONVERT_TO_UTF8_FREE(tooltip);
792 	}
793     }
794     else
795 # endif // FEAT_TOOLBAR
796     {
797 	// No parent, must be a non-menubar menu
798 	if (parent == NULL || parent->submenu_id == NULL)
799 	    return;
800 
801 # if !GTK_CHECK_VERSION(3,4,0)
802 	// Make place for the possible tearoff handle item.  Not in the popup
803 	// menu, it doesn't have a tearoff item.
804 	if (!menu_is_popup(parent->name))
805 	    ++idx;
806 # endif
807 
808 	if (menu_is_separator(menu->name))
809 	{
810 	    // Separator: Just add it
811 # if GTK_CHECK_VERSION(3,0,0)
812 	    menu->id = gtk_separator_menu_item_new();
813 # else
814 	    menu->id = gtk_menu_item_new();
815 	    gtk_widget_set_sensitive(menu->id, FALSE);
816 # endif
817 	    gtk_widget_show(menu->id);
818 	    gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
819 		    menu->id, idx);
820 
821 	    return;
822 	}
823 
824 	// Add textual menu item.
825 	menu_item_new(menu, parent->submenu_id);
826 	gtk_widget_show(menu->id);
827 	gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
828 		menu->id, idx);
829 
830 	if (menu->id != NULL)
831 	{
832 	    g_signal_connect(G_OBJECT(menu->id), "activate",
833 			     G_CALLBACK(menu_item_activate), menu);
834 	    g_signal_connect(G_OBJECT(menu->id), "select",
835 			     G_CALLBACK(menu_item_select), menu);
836 	}
837     }
838 }
839 #endif // FEAT_MENU
840 
841 
842     void
843 gui_mch_set_text_area_pos(int x, int y, int w, int h)
844 {
845     gui_gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
846 }
847 
848 
849 #if defined(FEAT_MENU) || defined(PROTO)
850 /*
851  * Enable or disable accelerators for the toplevel menus.
852  */
853     void
854 gui_gtk_set_mnemonics(int enable)
855 {
856     vimmenu_T	*menu;
857     char_u	*name;
858 
859     FOR_ALL_MENUS(menu)
860     {
861 	if (menu->id == NULL)
862 	    continue;
863 
864 	name = translate_mnemonic_tag(menu->name, enable);
865 	gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
866 					 (const char *)name);
867 	vim_free(name);
868     }
869 }
870 
871 # if !GTK_CHECK_VERSION(3,4,0)
872     static void
873 recurse_tearoffs(vimmenu_T *menu, int val)
874 {
875     for (; menu != NULL; menu = menu->next)
876     {
877 	if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
878 		&& menu->name[0] != ']' && !menu_is_popup(menu->name))
879 	{
880 	    if (val)
881 		gtk_widget_show(menu->tearoff_handle);
882 	    else
883 		gtk_widget_hide(menu->tearoff_handle);
884 	}
885 	recurse_tearoffs(menu->children, val);
886     }
887 }
888 # endif
889 
890 # if GTK_CHECK_VERSION(3,4,0)
891     void
892 gui_mch_toggle_tearoffs(int enable UNUSED)
893 {
894     // Do nothing
895 }
896 # else
897     void
898 gui_mch_toggle_tearoffs(int enable)
899 {
900     recurse_tearoffs(root_menu, enable);
901 }
902 # endif
903 #endif // FEAT_MENU
904 
905 #if defined(FEAT_TOOLBAR)
906     static int
907 get_menu_position(vimmenu_T *menu)
908 {
909     vimmenu_T	*node;
910     int		idx = 0;
911 
912     for (node = menu->parent->children; node != menu; node = node->next)
913     {
914 	g_return_val_if_fail(node != NULL, -1);
915 	++idx;
916     }
917 
918     return idx;
919 }
920 #endif // FEAT_TOOLBAR
921 
922 
923 #if defined(FEAT_TOOLBAR) || defined(PROTO)
924     void
925 gui_mch_menu_set_tip(vimmenu_T *menu)
926 {
927     if (menu->id != NULL && menu->parent != NULL && gui.toolbar != NULL)
928     {
929 	char_u *tooltip;
930 
931 	tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
932 	if (tooltip != NULL && utf_valid_string(tooltip, NULL))
933 # if GTK_CHECK_VERSION(3,0,0)
934 	    // Only set the tooltip when it's valid utf-8.
935 	    gtk_widget_set_tooltip_text(menu->id, (const gchar *)tooltip);
936 # else
937 	    // Only set the tooltip when it's valid utf-8.
938 	    gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
939 				 menu->id, (const char *)tooltip, NULL);
940 # endif
941 	CONVERT_TO_UTF8_FREE(tooltip);
942     }
943 }
944 #endif // FEAT_TOOLBAR
945 
946 
947 #if defined(FEAT_MENU) || defined(PROTO)
948 /*
949  * Destroy the machine specific menu widget.
950  */
951     void
952 gui_mch_destroy_menu(vimmenu_T *menu)
953 {
954     // Don't let gtk_container_remove automatically destroy menu->id.
955     if (menu->id != NULL)
956 	g_object_ref(menu->id);
957 
958     // Workaround for a spurious gtk warning in Ubuntu: "Trying to remove
959     // a child that doesn't believe we're its parent."
960     // Remove widget from gui.menubar before destroying it.
961     if (menu->id != NULL && gui.menubar != NULL
962 			    && gtk_widget_get_parent(menu->id) == gui.menubar)
963 	gtk_container_remove(GTK_CONTAINER(gui.menubar), menu->id);
964 
965 # ifdef FEAT_TOOLBAR
966     if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
967     {
968 	if (menu_is_separator(menu->name))
969 #  if GTK_CHECK_VERSION(3,0,0)
970 	{
971 	    GtkToolItem *item = NULL;
972 
973 	    item = gtk_toolbar_get_nth_item(GTK_TOOLBAR(gui.toolbar),
974 					    get_menu_position(menu));
975 	    if (item != NULL)
976 		gtk_container_remove(GTK_CONTAINER(gui.toolbar),
977 				     GTK_WIDGET(item));
978 	}
979 #  else
980 	    gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
981 				     get_menu_position(menu));
982 #  endif
983 	else if (menu->id != NULL)
984 	    gtk_widget_destroy(menu->id);
985     }
986     else
987 # endif // FEAT_TOOLBAR
988     {
989 	if (menu->submenu_id != NULL)
990 	    gtk_widget_destroy(menu->submenu_id);
991 
992 	if (menu->id != NULL)
993 	    gtk_widget_destroy(menu->id);
994     }
995 
996     if (menu->id != NULL)
997 	g_object_unref(menu->id);
998     menu->submenu_id = NULL;
999     menu->id = NULL;
1000 }
1001 #endif // FEAT_MENU
1002 
1003 
1004 /*
1005  * Scrollbar stuff.
1006  */
1007     void
1008 gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
1009 {
1010     if (sb->id != NULL)
1011     {
1012 	GtkAdjustment *adjustment;
1013 
1014 	adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1015 
1016 	gtk_adjustment_set_lower(adjustment, 0.0);
1017 	gtk_adjustment_set_value(adjustment, val);
1018 	gtk_adjustment_set_upper(adjustment, max + 1);
1019 	gtk_adjustment_set_page_size(adjustment, size);
1020 	gtk_adjustment_set_page_increment(adjustment,
1021 					  size < 3L ? 1L : size - 2L);
1022 	gtk_adjustment_set_step_increment(adjustment, 1.0);
1023 
1024 	g_signal_handler_block(G_OBJECT(adjustment), (gulong)sb->handler_id);
1025 
1026 #if !GTK_CHECK_VERSION(3,18,0)
1027 	gtk_adjustment_changed(adjustment);
1028 #endif
1029 
1030 	g_signal_handler_unblock(G_OBJECT(adjustment),
1031 						      (gulong)sb->handler_id);
1032     }
1033 }
1034 
1035     void
1036 gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1037 {
1038     if (sb->id != NULL)
1039 	gui_gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
1040 }
1041 
1042     int
1043 gui_mch_get_scrollbar_xpadding(void)
1044 {
1045     int xpad;
1046 #if GTK_CHECK_VERSION(3,0,0)
1047     xpad = gtk_widget_get_allocated_width(gui.formwin)
1048 	  - gtk_widget_get_allocated_width(gui.drawarea) - gui.scrollbar_width;
1049 #else
1050     xpad = gui.formwin->allocation.width - gui.drawarea->allocation.width
1051 							 - gui.scrollbar_width;
1052 #endif
1053     if (gui.which_scrollbars[SBAR_LEFT] && gui.which_scrollbars[SBAR_RIGHT])
1054 	xpad -= gui.scrollbar_width;
1055 
1056     return (xpad < 0) ? 0 : xpad;
1057 }
1058 
1059     int
1060 gui_mch_get_scrollbar_ypadding(void)
1061 {
1062     int ypad;
1063 #if GTK_CHECK_VERSION(3,0,0)
1064     ypad = gtk_widget_get_allocated_height(gui.formwin)
1065 	- gtk_widget_get_allocated_height(gui.drawarea) - gui.scrollbar_height;
1066 #else
1067     ypad = gui.formwin->allocation.height - gui.drawarea->allocation.height
1068 							- gui.scrollbar_height;
1069 #endif
1070     return (ypad < 0) ? 0 : ypad;
1071 }
1072 
1073 /*
1074  * Take action upon scrollbar dragging.
1075  */
1076     static void
1077 adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
1078 {
1079     scrollbar_T	*sb;
1080     long	value;
1081     int		dragging = FALSE;
1082 
1083 #ifdef FEAT_XIM
1084     // cancel any preediting
1085     if (im_is_preediting())
1086 	xim_reset();
1087 #endif
1088 
1089     sb = gui_find_scrollbar((long)data);
1090     value = gtk_adjustment_get_value(adjustment);
1091 #if !GTK_CHECK_VERSION(3,0,0)
1092     /*
1093      * The dragging argument must be right for the scrollbar to work with
1094      * closed folds.  This isn't documented, hopefully this will keep on
1095      * working in later GTK versions.
1096      *
1097      * FIXME: Well, it doesn't work in GTK2. :)
1098      * HACK: Get the mouse pointer position, if it appears to be on an arrow
1099      * button set "dragging" to FALSE.  This assumes square buttons!
1100      */
1101     if (sb != NULL)
1102     {
1103 	dragging = TRUE;
1104 
1105 	if (sb->wp != NULL)
1106 	{
1107 	    int			x;
1108 	    int			y;
1109 	    GdkModifierType	state;
1110 	    int			width;
1111 	    int			height;
1112 
1113 	    // vertical scrollbar: need to set "dragging" properly in case
1114 	    // there are closed folds.
1115 	    gdk_window_get_pointer(sb->id->window, &x, &y, &state);
1116 	    gdk_window_get_size(sb->id->window, &width, &height);
1117 	    if (x >= 0 && x < width && y >= 0 && y < height)
1118 	    {
1119 		if (y < width)
1120 		{
1121 		    // up arrow: move one (closed fold) line up
1122 		    dragging = FALSE;
1123 		    value = sb->wp->w_topline - 2;
1124 		}
1125 		else if (y > height - width)
1126 		{
1127 		    // down arrow: move one (closed fold) line down
1128 		    dragging = FALSE;
1129 		    value = sb->wp->w_topline;
1130 		}
1131 	    }
1132 	}
1133     }
1134 #endif // !GTK_CHECK_VERSION(3,0,0)
1135     gui_drag_scrollbar(sb, value, dragging);
1136 }
1137 
1138 // SBAR_VERT or SBAR_HORIZ
1139     void
1140 gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1141 {
1142     if (orient == SBAR_HORIZ)
1143 #if GTK_CHECK_VERSION(3,2,0)
1144 	sb->id = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, NULL);
1145 #else
1146 	sb->id = gtk_hscrollbar_new(NULL);
1147 #endif
1148     else if (orient == SBAR_VERT)
1149 #if GTK_CHECK_VERSION(3,2,0)
1150 	sb->id = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, NULL);
1151 #else
1152 	sb->id = gtk_vscrollbar_new(NULL);
1153 #endif
1154 
1155     if (sb->id != NULL)
1156     {
1157 	GtkAdjustment *adjustment;
1158 
1159 	gtk_widget_set_can_focus(sb->id, FALSE);
1160 	gui_gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
1161 
1162 	adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1163 
1164 	sb->handler_id = g_signal_connect(
1165 			     G_OBJECT(adjustment), "value-changed",
1166 			     G_CALLBACK(adjustment_value_changed),
1167 			     GINT_TO_POINTER(sb->ident));
1168 	gui_mch_update();
1169     }
1170 }
1171 
1172     void
1173 gui_mch_destroy_scrollbar(scrollbar_T *sb)
1174 {
1175     if (sb->id != NULL)
1176     {
1177 	gtk_widget_destroy(sb->id);
1178 	sb->id = NULL;
1179     }
1180     gui_mch_update();
1181 }
1182 
1183 #if defined(FEAT_BROWSE) || defined(PROTO)
1184 /*
1185  * Implementation of the file selector related stuff
1186  */
1187 
1188 #ifndef USE_FILE_CHOOSER
1189     static void
1190 browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
1191 {
1192     gui_T *vw = (gui_T *)cbdata;
1193 
1194     if (vw->browse_fname != NULL)
1195 	g_free(vw->browse_fname);
1196 
1197     vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
1198 					GTK_FILE_SELECTION(vw->filedlg)));
1199     gtk_widget_hide(vw->filedlg);
1200 }
1201 
1202     static void
1203 browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
1204 {
1205     gui_T *vw = (gui_T *)cbdata;
1206 
1207     if (vw->browse_fname != NULL)
1208     {
1209 	g_free(vw->browse_fname);
1210 	vw->browse_fname = NULL;
1211     }
1212     gtk_widget_hide(vw->filedlg);
1213 }
1214 
1215     static gboolean
1216 browse_destroy_cb(GtkWidget *widget UNUSED)
1217 {
1218     if (gui.browse_fname != NULL)
1219     {
1220 	g_free(gui.browse_fname);
1221 	gui.browse_fname = NULL;
1222     }
1223     gui.filedlg = NULL;
1224     gtk_main_quit();
1225     return FALSE;
1226 }
1227 #endif
1228 
1229 /*
1230  * Put up a file requester.
1231  * Returns the selected name in allocated memory, or NULL for Cancel.
1232  * saving,			select file to write
1233  * title			title for the window
1234  * dflt				default name
1235  * ext				not used (extension added)
1236  * initdir			initial directory, NULL for current dir
1237  * filter			not used (file name filter)
1238  */
1239     char_u *
1240 gui_mch_browse(int saving UNUSED,
1241 	       char_u *title,
1242 	       char_u *dflt,
1243 	       char_u *ext UNUSED,
1244 	       char_u *initdir,
1245 	       char_u *filter)
1246 {
1247 #ifdef USE_FILE_CHOOSER
1248 # if GTK_CHECK_VERSION(3,20,0)
1249     GtkFileChooserNative	*fc;
1250 # else
1251     GtkWidget			*fc;
1252 # endif
1253 #endif
1254     char_u		dirbuf[MAXPATHL];
1255     guint		log_handler;
1256     const gchar		*domain = "Gtk";
1257 
1258     title = CONVERT_TO_UTF8(title);
1259 
1260     // GTK has a bug, it only works with an absolute path.
1261     if (initdir == NULL || *initdir == NUL)
1262 	mch_dirname(dirbuf, MAXPATHL);
1263     else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
1264 	dirbuf[0] = NUL;
1265     // Always need a trailing slash for a directory.
1266     add_pathsep(dirbuf);
1267 
1268     // If our pointer is currently hidden, then we should show it.
1269     gui_mch_mousehide(FALSE);
1270 
1271     // Hack: The GTK file dialog warns when it can't access a new file, this
1272     // makes it shut up. http://bugzilla.gnome.org/show_bug.cgi?id=664587
1273     log_handler = g_log_set_handler(domain, G_LOG_LEVEL_WARNING,
1274 						  recent_func_log_func, NULL);
1275 
1276 #ifdef USE_FILE_CHOOSER
1277     // We create the dialog each time, so that the button text can be "Open"
1278     // or "Save" according to the action.
1279 # if GTK_CHECK_VERSION(3,20,0)
1280     fc = gtk_file_chooser_native_new(
1281 # else
1282     fc = gtk_file_chooser_dialog_new(
1283 # endif
1284 	    (const gchar *)title,
1285 	    GTK_WINDOW(gui.mainwin),
1286 	    saving ? GTK_FILE_CHOOSER_ACTION_SAVE
1287 					   : GTK_FILE_CHOOSER_ACTION_OPEN,
1288 # if GTK_CHECK_VERSION(3,20,0)
1289 	    saving ? _("_Save") : _("_Open"), _("_Cancel"));
1290 # else
1291 #  if GTK_CHECK_VERSION(3,10,0)
1292 	    _("_Cancel"), GTK_RESPONSE_CANCEL,
1293 	    saving ? _("_Save") : _("_Open"), GTK_RESPONSE_ACCEPT,
1294 #  else
1295 	    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1296 	    saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
1297 #  endif
1298 	    NULL);
1299 # endif
1300     gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
1301 						       (const gchar *)dirbuf);
1302 
1303     if (filter != NULL && *filter != NUL)
1304     {
1305 	int     i = 0;
1306 	char_u  *patt;
1307 	char_u  *p = filter;
1308 	GtkFileFilter	*gfilter;
1309 
1310 	gfilter = gtk_file_filter_new();
1311 	patt = alloc(STRLEN(filter));
1312 	while (p != NULL && *p != NUL)
1313 	{
1314 	    if (*p == '\n' || *p == ';' || *p == '\t')
1315 	    {
1316 		STRNCPY(patt, filter, i);
1317 		patt[i] = '\0';
1318 		if (*p == '\t')
1319 		    gtk_file_filter_set_name(gfilter, (gchar *)patt);
1320 		else
1321 		{
1322 		    gtk_file_filter_add_pattern(gfilter, (gchar *)patt);
1323 		    if (*p == '\n')
1324 		    {
1325 			gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(fc),
1326 								     gfilter);
1327 			if (*(p + 1) != NUL)
1328 			    gfilter = gtk_file_filter_new();
1329 		    }
1330 		}
1331 		filter = ++p;
1332 		i = 0;
1333 	    }
1334 	    else
1335 	    {
1336 		p++;
1337 		i++;
1338 	    }
1339 	}
1340 	vim_free(patt);
1341     }
1342     if (saving && dflt != NULL && *dflt != NUL)
1343 	gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
1344 
1345     gui.browse_fname = NULL;
1346 # if GTK_CHECK_VERSION(3,20,0)
1347     if (gtk_native_dialog_run(GTK_NATIVE_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
1348 # else
1349     if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
1350 #endif
1351     {
1352 	char *filename;
1353 
1354 	filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
1355 	gui.browse_fname = (char_u *)g_strdup(filename);
1356 	g_free(filename);
1357     }
1358 # if GTK_CHECK_VERSION(3,20,0)
1359     g_object_unref(fc);
1360 # else
1361     gtk_widget_destroy(GTK_WIDGET(fc));
1362 # endif
1363 
1364 #else // !USE_FILE_CHOOSER
1365 
1366     if (gui.filedlg == NULL)
1367     {
1368 	GtkFileSelection	*fs;	// shortcut
1369 
1370 	gui.filedlg = gtk_file_selection_new((const gchar *)title);
1371 	gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
1372 	gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
1373 						     GTK_WINDOW(gui.mainwin));
1374 	fs = GTK_FILE_SELECTION(gui.filedlg);
1375 
1376 	gtk_container_border_width(GTK_CONTAINER(fs), 4);
1377 
1378 	gtk_signal_connect(GTK_OBJECT(fs->ok_button),
1379 		"clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
1380 	gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
1381 		"clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
1382 	// gtk_signal_connect() doesn't work for destroy, it causes a hang
1383 	gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
1384 		"destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
1385 		GTK_OBJECT(gui.filedlg));
1386     }
1387     else
1388 	gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
1389 
1390     // Concatenate "initdir" and "dflt".
1391     if (dflt != NULL && *dflt != NUL
1392 			      && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
1393 	STRCAT(dirbuf, dflt);
1394 
1395     gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
1396 						      (const gchar *)dirbuf);
1397 
1398     gtk_widget_show(gui.filedlg);
1399     gtk_main();
1400 #endif // !USE_FILE_CHOOSER
1401     g_log_remove_handler(domain, log_handler);
1402 
1403     CONVERT_TO_UTF8_FREE(title);
1404     if (gui.browse_fname == NULL)
1405 	return NULL;
1406 
1407     // shorten the file name if possible
1408     return vim_strsave(shorten_fname1(gui.browse_fname));
1409 }
1410 
1411 /*
1412  * Put up a directory selector
1413  * Returns the selected name in allocated memory, or NULL for Cancel.
1414  * title			title for the window
1415  * dflt				default name
1416  * initdir			initial directory, NULL for current dir
1417  */
1418     char_u *
1419 gui_mch_browsedir(
1420 	       char_u *title,
1421 	       char_u *initdir)
1422 {
1423 # if defined(GTK_FILE_CHOOSER)	    // Only in GTK 2.4 and later.
1424     char_u		dirbuf[MAXPATHL];
1425     char_u		*p;
1426     GtkWidget		*dirdlg;	    // file selection dialog
1427     char_u		*dirname = NULL;
1428 
1429     title = CONVERT_TO_UTF8(title);
1430 
1431     dirdlg = gtk_file_chooser_dialog_new(
1432 	    (const gchar *)title,
1433 	    GTK_WINDOW(gui.mainwin),
1434 	    GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
1435 #  if GTK_CHECK_VERSION(3,10,0)
1436 	    _("_Cancel"), GTK_RESPONSE_CANCEL,
1437 	    _("_OK"), GTK_RESPONSE_ACCEPT,
1438 #  else
1439 	    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1440 	    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1441 #  endif
1442 	    NULL);
1443 
1444     CONVERT_TO_UTF8_FREE(title);
1445 
1446     // if our pointer is currently hidden, then we should show it.
1447     gui_mch_mousehide(FALSE);
1448 
1449     // GTK appears to insist on an absolute path.
1450     if (initdir == NULL || *initdir == NUL
1451 	       || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
1452 	mch_dirname(dirbuf, MAXPATHL - 10);
1453 
1454     // Always need a trailing slash for a directory.
1455     // Also add a dummy file name, so that we get to the directory.
1456     add_pathsep(dirbuf);
1457     STRCAT(dirbuf, "@zd(*&1|");
1458     gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
1459 						      (const gchar *)dirbuf);
1460 
1461     // Run the dialog.
1462     if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
1463 	dirname = (char_u *)gtk_file_chooser_get_filename(
1464 						    GTK_FILE_CHOOSER(dirdlg));
1465     gtk_widget_destroy(dirdlg);
1466     if (dirname == NULL)
1467 	return NULL;
1468 
1469     // shorten the file name if possible
1470     p = vim_strsave(shorten_fname1(dirname));
1471     g_free(dirname);
1472     return p;
1473 
1474 # else // !defined(GTK_FILE_CHOOSER)
1475     // For GTK 2.2 and earlier: fall back to ordinary file selector.
1476     return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
1477 # endif // !defined(GTK_FILE_CHOOSER)
1478 }
1479 
1480 
1481 #endif	// FEAT_BROWSE
1482 
1483 #if defined(FEAT_GUI_DIALOG) || defined(PROTO)
1484 
1485     static GtkWidget *
1486 create_message_dialog(int type, char_u *title, char_u *message)
1487 {
1488     GtkWidget	    *dialog;
1489     GtkMessageType  message_type;
1490 
1491     switch (type)
1492     {
1493 	case VIM_ERROR:	    message_type = GTK_MESSAGE_ERROR;	 break;
1494 	case VIM_WARNING:   message_type = GTK_MESSAGE_WARNING;	 break;
1495 	case VIM_QUESTION:  message_type = GTK_MESSAGE_QUESTION; break;
1496 	default:	    message_type = GTK_MESSAGE_INFO;	 break;
1497     }
1498 
1499     message = CONVERT_TO_UTF8(message);
1500     dialog  = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1501 				     GTK_DIALOG_DESTROY_WITH_PARENT,
1502 				     message_type,
1503 				     GTK_BUTTONS_NONE,
1504 				     "%s", (const char *)message);
1505     CONVERT_TO_UTF8_FREE(message);
1506 
1507     if (title != NULL)
1508     {
1509 	title = CONVERT_TO_UTF8(title);
1510 	gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1511 	CONVERT_TO_UTF8_FREE(title);
1512     }
1513     else if (type == VIM_GENERIC)
1514     {
1515 	gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1516     }
1517 
1518     return dialog;
1519 }
1520 
1521 /*
1522  * Split up button_string into individual button labels by inserting
1523  * NUL bytes.  Also replace the Vim-style mnemonic accelerator prefix
1524  * '&' with '_'.  button_string must point to allocated memory!
1525  * Return an allocated array of pointers into button_string.
1526  */
1527     static char **
1528 split_button_string(char_u *button_string, int *n_buttons)
1529 {
1530     char	    **array;
1531     char_u	    *p;
1532     unsigned int    count = 1;
1533 
1534     for (p = button_string; *p != NUL; ++p)
1535 	if (*p == DLG_BUTTON_SEP)
1536 	    ++count;
1537 
1538     array = ALLOC_MULT(char *, count + 1);
1539     count = 0;
1540 
1541     if (array != NULL)
1542     {
1543 	array[count++] = (char *)button_string;
1544 	for (p = button_string; *p != NUL; )
1545 	{
1546 	    if (*p == DLG_BUTTON_SEP)
1547 	    {
1548 		*p++ = NUL;
1549 		array[count++] = (char *)p;
1550 	    }
1551 	    else if (*p == DLG_HOTKEY_CHAR)
1552 		*p++ = '_';
1553 	    else
1554 		MB_PTR_ADV(p);
1555 	}
1556 	array[count] = NULL; // currently not relied upon, but doesn't hurt
1557     }
1558 
1559     *n_buttons = count;
1560     return array;
1561 }
1562 
1563     static char **
1564 split_button_translation(const char *message)
1565 {
1566     char    **buttons = NULL;
1567     char_u  *str;
1568     int	    n_buttons = 0;
1569     int	    n_expected = 1;
1570 
1571     for (str = (char_u *)message; *str != NUL; ++str)
1572 	if (*str == DLG_BUTTON_SEP)
1573 	    ++n_expected;
1574 
1575     str = (char_u *)_(message);
1576     if (str != NULL)
1577     {
1578 	if (output_conv.vc_type != CONV_NONE)
1579 	    str = string_convert(&output_conv, str, NULL);
1580 	else
1581 	    str = vim_strsave(str);
1582 
1583 	if (str != NULL)
1584 	    buttons = split_button_string(str, &n_buttons);
1585     }
1586     /*
1587      * Uh-oh... this should never ever happen.	But we don't wanna crash
1588      * if the translation is broken, thus fall back to the untranslated
1589      * buttons string in case of emergency.
1590      */
1591     if (buttons == NULL || n_buttons != n_expected)
1592     {
1593 	vim_free(buttons);
1594 	vim_free(str);
1595 	buttons = NULL;
1596 	str = vim_strsave((char_u *)message);
1597 
1598 	if (str != NULL)
1599 	    buttons = split_button_string(str, &n_buttons);
1600 	if (buttons == NULL)
1601 	    vim_free(str);
1602     }
1603 
1604     return buttons;
1605 }
1606 
1607     static int
1608 button_equal(const char *a, const char *b)
1609 {
1610     while (*a != '\0' && *b != '\0')
1611     {
1612 	if (*a == '_' && *++a == '\0')
1613 	    break;
1614 	if (*b == '_' && *++b == '\0')
1615 	    break;
1616 
1617 	if (g_unichar_tolower(g_utf8_get_char(a))
1618 		!= g_unichar_tolower(g_utf8_get_char(b)))
1619 	    return FALSE;
1620 
1621 	a = g_utf8_next_char(a);
1622 	b = g_utf8_next_char(b);
1623     }
1624 
1625     return (*a == '\0' && *b == '\0');
1626 }
1627 
1628     static void
1629 dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1630 {
1631     char    **ok;
1632     char    **ync;  // "yes no cancel"
1633     char    **buttons;
1634     int	    n_buttons = 0;
1635     int	    idx;
1636 
1637     button_string = vim_strsave(button_string); // must be writable
1638     if (button_string == NULL)
1639 	return;
1640 
1641     // Check 'v' flag in 'guioptions': vertical button placement.
1642     if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1643     {
1644 # if GTK_CHECK_VERSION(3,0,0)
1645 	// Add GTK+ 3 code if necessary.
1646 	// N.B. GTK+ 3 doesn't allow you to access vbox and action_area via
1647 	// the C API.
1648 # else
1649 	GtkWidget	*vbutton_box;
1650 
1651 	vbutton_box = gtk_vbutton_box_new();
1652 	gtk_widget_show(vbutton_box);
1653 	gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1654 						 vbutton_box, TRUE, FALSE, 0);
1655 	// Overrule the "action_area" value, hopefully this works...
1656 	GTK_DIALOG(dialog)->action_area = vbutton_box;
1657 # endif
1658     }
1659 
1660     /*
1661      * Yes this is ugly, I don't particularly like it either.  But doing it
1662      * this way has the compelling advantage that translations need not to
1663      * be touched at all.  See below what 'ok' and 'ync' are used for.
1664      */
1665     ok	    = split_button_translation(N_("&Ok"));
1666     ync     = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1667     buttons = split_button_string(button_string, &n_buttons);
1668 
1669     /*
1670      * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1671      * environment.  Don't hit me -- it's all about consistency.
1672      * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1673      * other way around...
1674      */
1675     for (idx = 1; idx <= n_buttons; ++idx)
1676     {
1677 	char	*label;
1678 	char_u	*label8;
1679 
1680 	label = buttons[idx - 1];
1681 	/*
1682 	 * Perform some guesswork to find appropriate stock items for the
1683 	 * buttons.  We have to compare with a sample of the translated
1684 	 * button string to get things right.  Yes, this is hackish :/
1685 	 *
1686 	 * But even the common button labels aren't necessarily translated,
1687 	 * since anyone can create their own dialogs using Vim functions.
1688 	 * Thus we have to check for those too.
1689 	 */
1690 	if (ok != NULL && ync != NULL) // almost impossible to fail
1691 	{
1692 # if GTK_CHECK_VERSION(3,10,0)
1693 	    if	    (button_equal(label, ok[0]))    label = _("OK");
1694 	    else if (button_equal(label, ync[0]))   label = _("Yes");
1695 	    else if (button_equal(label, ync[1]))   label = _("No");
1696 	    else if (button_equal(label, ync[2]))   label = _("Cancel");
1697 	    else if (button_equal(label, "Ok"))     label = _("OK");
1698 	    else if (button_equal(label, "Yes"))    label = _("Yes");
1699 	    else if (button_equal(label, "No"))     label = _("No");
1700 	    else if (button_equal(label, "Cancel")) label = _("Cancel");
1701 # else
1702 	    if	    (button_equal(label, ok[0]))    label = GTK_STOCK_OK;
1703 	    else if (button_equal(label, ync[0]))   label = GTK_STOCK_YES;
1704 	    else if (button_equal(label, ync[1]))   label = GTK_STOCK_NO;
1705 	    else if (button_equal(label, ync[2]))   label = GTK_STOCK_CANCEL;
1706 	    else if (button_equal(label, "Ok"))     label = GTK_STOCK_OK;
1707 	    else if (button_equal(label, "Yes"))    label = GTK_STOCK_YES;
1708 	    else if (button_equal(label, "No"))     label = GTK_STOCK_NO;
1709 	    else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
1710 # endif
1711 	}
1712 	label8 = CONVERT_TO_UTF8((char_u *)label);
1713 	gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
1714 	CONVERT_TO_UTF8_FREE(label8);
1715     }
1716 
1717     if (ok != NULL)
1718 	vim_free(*ok);
1719     if (ync != NULL)
1720 	vim_free(*ync);
1721     vim_free(ok);
1722     vim_free(ync);
1723     vim_free(buttons);
1724     vim_free(button_string);
1725 }
1726 
1727 /*
1728  * Allow mnemonic accelerators to be activated without pressing <Alt>.
1729  * I'm not sure if it's a wise idea to do this.  However, the old GTK+ 1.2
1730  * GUI used to work this way, and I consider the impact on UI consistency
1731  * low enough to justify implementing this as a special Vim feature.
1732  */
1733 typedef struct _DialogInfo
1734 {
1735     int		ignore_enter;	    // no default button, ignore "Enter"
1736     int		noalt;		    // accept accelerators without Alt
1737     GtkDialog	*dialog;	    // Widget of the dialog
1738 } DialogInfo;
1739 
1740     static gboolean
1741 dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1742 {
1743     DialogInfo *di = (DialogInfo *)data;
1744 
1745     // Ignore hitting Enter (or Space) when there is no default button.
1746     if (di->ignore_enter && (event->keyval == GDK_Return
1747 						     || event->keyval == ' '))
1748 	return TRUE;
1749     else    // A different key was pressed, return to normal behavior
1750 	di->ignore_enter = FALSE;
1751 
1752     // Close the dialog when hitting "Esc".
1753     if (event->keyval == GDK_Escape)
1754     {
1755 	gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1756 	return TRUE;
1757     }
1758 
1759     if (di->noalt
1760 	      && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1761     {
1762 	return gtk_window_mnemonic_activate(
1763 		   GTK_WINDOW(widget), event->keyval,
1764 		   gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1765     }
1766 
1767     return FALSE; // continue emission
1768 }
1769 
1770     int
1771 gui_mch_dialog(int	type,	    // type of dialog
1772 	       char_u	*title,	    // title of dialog
1773 	       char_u	*message,   // message text
1774 	       char_u	*buttons,   // names of buttons
1775 	       int	def_but,    // default button
1776 	       char_u	*textfield, // text for textfield or NULL
1777 	       int	ex_cmd UNUSED)
1778 {
1779     GtkWidget	*dialog;
1780     GtkWidget	*entry = NULL;
1781     char_u	*text;
1782     int		response;
1783     DialogInfo  dialoginfo;
1784 
1785     dialog = create_message_dialog(type, title, message);
1786     dialoginfo.dialog = GTK_DIALOG(dialog);
1787     dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1788 
1789     if (textfield != NULL)
1790     {
1791 	GtkWidget *alignment;
1792 
1793 	entry = gtk_entry_new();
1794 	gtk_widget_show(entry);
1795 
1796 	// Make Enter work like pressing OK.
1797 	gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
1798 
1799 	text = CONVERT_TO_UTF8(textfield);
1800 	gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1801 	CONVERT_TO_UTF8_FREE(text);
1802 
1803 # if GTK_CHECK_VERSION(3,14,0)
1804 	gtk_widget_set_halign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1805 	gtk_widget_set_valign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1806 	gtk_widget_set_hexpand(GTK_WIDGET(entry), TRUE);
1807 	gtk_widget_set_vexpand(GTK_WIDGET(entry), TRUE);
1808 
1809 	alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1810 # else
1811 	alignment = gtk_alignment_new((float)0.5, (float)0.5,
1812 						      (float)1.0, (float)1.0);
1813 # endif
1814 	gtk_container_add(GTK_CONTAINER(alignment), entry);
1815 	gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1816 	gtk_widget_show(alignment);
1817 
1818 # if GTK_CHECK_VERSION(3,0,0)
1819 	{
1820 	    GtkWidget * const vbox
1821 		= gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1822 	    gtk_box_pack_start(GTK_BOX(vbox),
1823 		    alignment, TRUE, FALSE, 0);
1824 	}
1825 # else
1826 	gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1827 			   alignment, TRUE, FALSE, 0);
1828 # endif
1829 	dialoginfo.noalt = FALSE;
1830     }
1831     else
1832 	dialoginfo.noalt = TRUE;
1833 
1834     // Allow activation of mnemonic accelerators without pressing <Alt> when
1835     // there is no textfield.  Handle pressing Esc.
1836     g_signal_connect(G_OBJECT(dialog), "key-press-event",
1837 			 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1838 
1839     if (def_but > 0)
1840     {
1841 	gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1842 	dialoginfo.ignore_enter = FALSE;
1843     }
1844     else
1845 	// No default button, ignore pressing Enter.
1846 	dialoginfo.ignore_enter = TRUE;
1847 
1848     // Show the mouse pointer if it's currently hidden.
1849     gui_mch_mousehide(FALSE);
1850 
1851     response = gtk_dialog_run(GTK_DIALOG(dialog));
1852 
1853     // GTK_RESPONSE_NONE means the dialog was programmatically destroyed.
1854     if (response != GTK_RESPONSE_NONE)
1855     {
1856 	if (response == GTK_RESPONSE_ACCEPT)	    // Enter pressed
1857 	    response = def_but;
1858 	if (textfield != NULL)
1859 	{
1860 	    text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1861 	    text = CONVERT_FROM_UTF8(text);
1862 
1863 	    vim_strncpy(textfield, text, IOSIZE - 1);
1864 
1865 	    CONVERT_FROM_UTF8_FREE(text);
1866 	}
1867 	gtk_widget_destroy(dialog);
1868     }
1869 
1870     return response > 0 ? response : 0;
1871 }
1872 
1873 #endif // FEAT_GUI_DIALOG
1874 
1875 
1876 #if defined(FEAT_MENU) || defined(PROTO)
1877 
1878     void
1879 gui_mch_show_popupmenu(vimmenu_T *menu)
1880 {
1881 # if defined(FEAT_XIM)
1882     /*
1883      * Append a submenu for selecting an input method.	This is
1884      * currently the only way to switch input methods at runtime.
1885      */
1886 #  if !GTK_CHECK_VERSION(3,10,0)
1887     if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1888 					 "vim-has-im-menu") == NULL)
1889     {
1890 	GtkWidget   *menuitem;
1891 	GtkWidget   *submenu;
1892 	char_u	    *name;
1893 
1894 	menuitem = gtk_separator_menu_item_new();
1895 	gtk_widget_show(menuitem);
1896 	gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1897 
1898 	name = (char_u *)_("Input _Methods");
1899 	name = CONVERT_TO_UTF8(name);
1900 	menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1901 	CONVERT_TO_UTF8_FREE(name);
1902 	gtk_widget_show(menuitem);
1903 
1904 	submenu = gtk_menu_new();
1905 	gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1906 	gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1907 
1908 	gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1909 					     GTK_MENU_SHELL(submenu));
1910 	g_object_set_data(G_OBJECT(menu->submenu_id),
1911 			  "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1912     }
1913 #  endif
1914 # endif // FEAT_XIM
1915 
1916 # if GTK_CHECK_VERSION(3,22,2)
1917     {
1918 	GdkEventButton trigger;
1919 
1920 	// A pseudo event to have gtk_menu_popup_at_pointer() work. Since the
1921 	// function calculates the popup menu position on the basis of the
1922 	// actual pointer position when it is invoked, the fields x, y, x_root
1923 	// and y_root are set to zero for convenience.
1924 	trigger.type       = GDK_BUTTON_PRESS;
1925 	trigger.window     = gtk_widget_get_window(gui.drawarea);
1926 	trigger.send_event = FALSE;
1927 	trigger.time       = gui.event_time;
1928 	trigger.x	   = 0.0;
1929 	trigger.y	   = 0.0;
1930 	trigger.axes       = NULL;
1931 	trigger.state      = 0;
1932 	trigger.button     = 3;
1933 	trigger.device     = NULL;
1934 	trigger.x_root     = 0.0;
1935 	trigger.y_root     = 0.0;
1936 
1937 	gtk_menu_popup_at_pointer(GTK_MENU(menu->submenu_id),
1938 				  (GdkEvent *)&trigger);
1939     }
1940 #else
1941     gtk_menu_popup(GTK_MENU(menu->submenu_id),
1942 		   NULL, NULL,
1943 		   (GtkMenuPositionFunc)NULL, NULL,
1944 		   3U, gui.event_time);
1945 #endif
1946 }
1947 
1948 // Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1949 // popup_menu_position_func().
1950 static int popup_mouse_pos;
1951 
1952 /*
1953  * Menu position callback; used by gui_make_popup() to place the menu
1954  * at the current text cursor position.
1955  *
1956  * Note: The push_in output argument seems to affect scrolling of huge
1957  * menus that don't fit on the screen.	Leave it at the default for now.
1958  */
1959     static void
1960 popup_menu_position_func(GtkMenu *menu UNUSED,
1961 			 gint *x, gint *y,
1962 			 gboolean *push_in UNUSED,
1963 			 gpointer user_data UNUSED)
1964 {
1965     gdk_window_get_origin(gtk_widget_get_window(gui.drawarea), x, y);
1966 
1967     if (popup_mouse_pos)
1968     {
1969 	int	mx, my;
1970 
1971 	gui_mch_getmouse(&mx, &my);
1972 	*x += mx;
1973 	*y += my;
1974     }
1975     else if (curwin != NULL && gui.drawarea != NULL &&
1976 	     gtk_widget_get_window(gui.drawarea) != NULL)
1977     {
1978 	// Find the cursor position in the current window
1979 	*x += FILL_X(curwin->w_wincol + curwin->w_wcol + 1) + 1;
1980 	*y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1981     }
1982 }
1983 
1984     void
1985 gui_make_popup(char_u *path_name, int mouse_pos)
1986 {
1987     vimmenu_T *menu;
1988 
1989     popup_mouse_pos = mouse_pos;
1990 
1991     menu = gui_find_menu(path_name);
1992 
1993     if (menu != NULL && menu->submenu_id != NULL)
1994     {
1995 # if GTK_CHECK_VERSION(3,22,2)
1996 	GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
1997 	GdkEventButton trigger;
1998 
1999 	// A pseudo event to have gtk_menu_popup_at_*() functions work. Since
2000 	// the position where the menu pops up is automatically adjusted by
2001 	// the functions, none of the fields x, y, x_root and y_root has to be
2002 	// set to a specific value here; therefore, they are set to zero for
2003 	// convenience.
2004 	trigger.type       = GDK_BUTTON_PRESS;
2005 	trigger.window     = win;
2006 	trigger.send_event = FALSE;
2007 	trigger.time       = GDK_CURRENT_TIME;
2008 	trigger.x	   = 0.0;
2009 	trigger.y	   = 0.0;
2010 	trigger.axes       = NULL;
2011 	trigger.state      = 0;
2012 	trigger.button     = 0;
2013 	trigger.device     = NULL;
2014 	trigger.x_root     = 0.0;
2015 	trigger.y_root     = 0.0;
2016 
2017 	if (mouse_pos)
2018 	    gtk_menu_popup_at_pointer(GTK_MENU(menu->submenu_id),
2019 				      (GdkEvent *)&trigger);
2020 	else
2021 	{
2022 	    gint origin_x, origin_y;
2023 	    GdkRectangle rect = { 0, 0, 0, 0 };
2024 
2025 	    gdk_window_get_origin(win, &origin_x, &origin_y);
2026 	    popup_menu_position_func(NULL, &rect.x, &rect.y, NULL, NULL);
2027 
2028 	    rect.x -= origin_x;
2029 	    rect.y -= origin_y;
2030 
2031 	    gtk_menu_popup_at_rect(GTK_MENU(menu->submenu_id),
2032 				   win,
2033 				   &rect,
2034 				   GDK_GRAVITY_SOUTH_EAST,
2035 				   GDK_GRAVITY_NORTH_WEST,
2036 				   (GdkEvent *)&trigger);
2037 	}
2038 # else
2039 	gtk_menu_popup(GTK_MENU(menu->submenu_id),
2040 		       NULL, NULL,
2041 		       &popup_menu_position_func, NULL,
2042 		       0U, (guint32)GDK_CURRENT_TIME);
2043 # endif
2044     }
2045 }
2046 
2047 #endif // FEAT_MENU
2048 
2049 
2050 /*
2051  * We don't create it twice.
2052  */
2053 
2054 typedef struct _SharedFindReplace
2055 {
2056     GtkWidget *dialog;	// the main dialog widget
2057     GtkWidget *wword;	// 'Whole word only' check button
2058     GtkWidget *mcase;	// 'Match case' check button
2059     GtkWidget *up;	// search direction 'Up' radio button
2060     GtkWidget *down;	// search direction 'Down' radio button
2061     GtkWidget *what;	// 'Find what' entry text widget
2062     GtkWidget *with;	// 'Replace with' entry text widget
2063     GtkWidget *find;	// 'Find Next' action button
2064     GtkWidget *replace;	// 'Replace With' action button
2065     GtkWidget *all;	// 'Replace All' action button
2066 } SharedFindReplace;
2067 
2068 static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2069 static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2070 
2071     static int
2072 find_key_press_event(
2073 		GtkWidget	*widget UNUSED,
2074 		GdkEventKey	*event,
2075 		SharedFindReplace *frdp)
2076 {
2077     // If the user is holding one of the key modifiers we will just bail out,
2078     // thus preserving the possibility of normal focus traversal.
2079     if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
2080 	return FALSE;
2081 
2082     // the Escape key synthesizes a cancellation action
2083     if (event->keyval == GDK_Escape)
2084     {
2085 	gtk_widget_hide(frdp->dialog);
2086 
2087 	return TRUE;
2088     }
2089 
2090     // It would be delightful if it where possible to do search history
2091     // operations on the K_UP and K_DOWN keys here.
2092 
2093     return FALSE;
2094 }
2095 
2096     static GtkWidget *
2097 #if GTK_CHECK_VERSION(3,10,0)
2098 create_image_button(const char *stock_id UNUSED,
2099 		    const char *label)
2100 #else
2101 create_image_button(const char *stock_id,
2102 		    const char *label)
2103 #endif
2104 {
2105     char_u	*text;
2106     GtkWidget	*box;
2107     GtkWidget	*alignment;
2108     GtkWidget	*button;
2109 
2110     text = CONVERT_TO_UTF8((char_u *)label);
2111 
2112 #if GTK_CHECK_VERSION(3,2,0)
2113     box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
2114     gtk_box_set_homogeneous(GTK_BOX(box), FALSE);
2115 #else
2116     box = gtk_hbox_new(FALSE, 3);
2117 #endif
2118 #if !GTK_CHECK_VERSION(3,10,0)
2119     if (stock_id != NULL)
2120 	gtk_box_pack_start(GTK_BOX(box),
2121 			   gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
2122 			   FALSE, FALSE, 0);
2123 #endif
2124     gtk_box_pack_start(GTK_BOX(box),
2125 		       gtk_label_new((const char *)text),
2126 		       FALSE, FALSE, 0);
2127 
2128     CONVERT_TO_UTF8_FREE(text);
2129 
2130 #if GTK_CHECK_VERSION(3,14,0)
2131     gtk_widget_set_halign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2132     gtk_widget_set_valign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2133     gtk_widget_set_hexpand(GTK_WIDGET(box), TRUE);
2134     gtk_widget_set_vexpand(GTK_WIDGET(box), TRUE);
2135 
2136     alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2137 #else
2138     alignment = gtk_alignment_new((float)0.5, (float)0.5,
2139 						      (float)0.0, (float)0.0);
2140 #endif
2141     gtk_container_add(GTK_CONTAINER(alignment), box);
2142     gtk_widget_show_all(alignment);
2143 
2144     button = gtk_button_new();
2145     gtk_container_add(GTK_CONTAINER(button), alignment);
2146 
2147     return button;
2148 }
2149 
2150 /*
2151  * This is currently only used by find_replace_dialog_create(), and
2152  * I'd really like to keep it at that.	In other words: don't spread
2153  * this nasty hack all over the code.  Think twice.
2154  */
2155     static const char *
2156 convert_localized_message(char_u **buffer, const char *message)
2157 {
2158     if (output_conv.vc_type == CONV_NONE)
2159 	return message;
2160 
2161     vim_free(*buffer);
2162     *buffer = string_convert(&output_conv, (char_u *)message, NULL);
2163 
2164     return (const char *)*buffer;
2165 }
2166 
2167 /*
2168  * Returns the number of characters in GtkEntry.
2169  */
2170     static unsigned long
2171 entry_get_text_length(GtkEntry *entry)
2172 {
2173     g_return_val_if_fail(entry != NULL, 0);
2174     g_return_val_if_fail(GTK_IS_ENTRY(entry) == TRUE, 0);
2175 
2176 #if GTK_CHECK_VERSION(2,18,0)
2177     // 2.18 introduced a new object GtkEntryBuffer to handle text data for
2178     // GtkEntry instead of letting each instance of the latter have its own
2179     // storage for that.  The code below is almost identical to the
2180     // implementation of gtk_entry_get_text_length() for the versions >= 2.18.
2181     return gtk_entry_buffer_get_length(gtk_entry_get_buffer(entry));
2182 #elif GTK_CHECK_VERSION(2,14,0)
2183     // 2.14 introduced a new function to avoid memory management bugs which can
2184     // happen when gtk_entry_get_text() is used without due care and attention.
2185     return gtk_entry_get_text_length(entry);
2186 #else
2187     // gtk_entry_get_text() returns the pointer to the storage allocated
2188     // internally by the widget.  Accordingly, use the one with great care:
2189     // Don't free it nor modify the contents it points to; call the function
2190     // every time you need the pointer since its value may have been changed
2191     // by the widget.
2192     return g_utf8_strlen(gtk_entry_get_text(entry), -1);
2193 #endif
2194 }
2195 
2196     static void
2197 find_replace_dialog_create(char_u *arg, int do_replace)
2198 {
2199     GtkWidget	*hbox;		// main top down box
2200     GtkWidget	*actionarea;
2201     GtkWidget	*table;
2202     GtkWidget	*tmp;
2203     GtkWidget	*vbox;
2204     gboolean	sensitive;
2205     SharedFindReplace *frdp;
2206     char_u	*entry_text;
2207     int		wword = FALSE;
2208     int		mcase = !p_ic;
2209     char_u	*conv_buffer = NULL;
2210 #   define CONV(message) convert_localized_message(&conv_buffer, (message))
2211 
2212     frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2213 
2214     // Get the search string to use.
2215     entry_text = get_find_dialog_text(arg, &wword, &mcase);
2216 
2217     if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
2218     {
2219 	char_u *old_text = entry_text;
2220 	entry_text = string_convert(&output_conv, entry_text, NULL);
2221 	vim_free(old_text);
2222     }
2223 
2224     /*
2225      * If the dialog already exists, just raise it.
2226      */
2227     if (frdp->dialog)
2228     {
2229 	if (entry_text != NULL)
2230 	{
2231 	    gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2232 	    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2233 							     (gboolean)wword);
2234 	    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2235 							     (gboolean)mcase);
2236 	}
2237 	gtk_window_present(GTK_WINDOW(frdp->dialog));
2238 
2239 	// For :promptfind dialog, always give keyboard focus to 'what' entry.
2240 	// For :promptrepl dialog, give it to 'with' entry if 'what' has an
2241 	// non-empty entry; otherwise, to 'what' entry.
2242 	gtk_widget_grab_focus(frdp->what);
2243 	if (do_replace && entry_get_text_length(GTK_ENTRY(frdp->what)) > 0)
2244 	    gtk_widget_grab_focus(frdp->with);
2245 
2246 	vim_free(entry_text);
2247 	return;
2248     }
2249 
2250     frdp->dialog = gtk_dialog_new();
2251 #if GTK_CHECK_VERSION(3,0,0)
2252     // Nothing equivalent to gtk_dialog_set_has_separator() in GTK+ 3.
2253 #else
2254     gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
2255 #endif
2256     gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
2257     gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
2258 
2259     if (do_replace)
2260     {
2261 	gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2262 			     CONV(_("VIM - Search and Replace...")));
2263     }
2264     else
2265     {
2266 	gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2267 			     CONV(_("VIM - Search...")));
2268     }
2269 
2270 #if GTK_CHECK_VERSION(3,2,0)
2271     hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2272     gtk_box_set_homogeneous(GTK_BOX(hbox), FALSE);
2273 #else
2274     hbox = gtk_hbox_new(FALSE, 0);
2275 #endif
2276     gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2277 #if GTK_CHECK_VERSION(3,0,0)
2278     {
2279 	GtkWidget * const dialog_vbox
2280 	    = gtk_dialog_get_content_area(GTK_DIALOG(frdp->dialog));
2281 	gtk_container_add(GTK_CONTAINER(dialog_vbox), hbox);
2282     }
2283 #else
2284     gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
2285 #endif
2286 
2287     if (do_replace)
2288 #if GTK_CHECK_VERSION(3,4,0)
2289 	table = gtk_grid_new();
2290 #else
2291 	table = gtk_table_new(1024, 4, FALSE);
2292 #endif
2293     else
2294 #if GTK_CHECK_VERSION(3,4,0)
2295 	table = gtk_grid_new();
2296 #else
2297 	table = gtk_table_new(1024, 3, FALSE);
2298 #endif
2299     gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
2300     gtk_container_set_border_width(GTK_CONTAINER(table), 4);
2301 
2302     tmp = gtk_label_new(CONV(_("Find what:")));
2303 #if GTK_CHECK_VERSION(3,16,0)
2304     gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2305     gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2306 #elif GTK_CHECK_VERSION(3,14,0)
2307     {
2308 	GValue align_val = G_VALUE_INIT;
2309 
2310 	g_value_init(&align_val, G_TYPE_FLOAT);
2311 
2312 	g_value_set_float(&align_val, 0.0);
2313 	g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2314 
2315 	g_value_set_float(&align_val, 0.5);
2316 	g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2317 
2318 	g_value_unset(&align_val);
2319     }
2320 #else
2321     gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2322 #endif
2323 #if GTK_CHECK_VERSION(3,4,0)
2324     gtk_grid_attach(GTK_GRID(table), tmp, 0, 0, 2, 1);
2325 #else
2326     gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
2327 		     GTK_FILL, GTK_EXPAND, 2, 2);
2328 #endif
2329     frdp->what = gtk_entry_new();
2330     sensitive = (entry_text != NULL && entry_text[0] != NUL);
2331     if (entry_text != NULL)
2332 	gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2333     g_signal_connect(G_OBJECT(frdp->what), "changed",
2334 		     G_CALLBACK(entry_changed_cb), frdp->dialog);
2335     g_signal_connect_after(G_OBJECT(frdp->what), "key-press-event",
2336 			   G_CALLBACK(find_key_press_event),
2337 			   (gpointer) frdp);
2338 #if GTK_CHECK_VERSION(3,4,0)
2339     gtk_grid_attach(GTK_GRID(table), frdp->what, 2, 0, 5, 1);
2340 #else
2341     gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
2342 		     GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2343 #endif
2344 
2345     if (do_replace)
2346     {
2347 	tmp = gtk_label_new(CONV(_("Replace with:")));
2348 #if GTK_CHECK_VERSION(3,16,0)
2349 	gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2350 	gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2351 #elif GTK_CHECK_VERSION(3,14,0)
2352 	{
2353 	    GValue align_val = G_VALUE_INIT;
2354 
2355 	    g_value_init(&align_val, G_TYPE_FLOAT);
2356 
2357 	    g_value_set_float(&align_val, 0.0);
2358 	    g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2359 
2360 	    g_value_set_float(&align_val, 0.5);
2361 	    g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2362 
2363 	    g_value_unset(&align_val);
2364 	}
2365 #else
2366 	gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2367 #endif
2368 #if GTK_CHECK_VERSION(3,4,0)
2369 	gtk_grid_attach(GTK_GRID(table), tmp, 0, 1, 2, 1);
2370 #else
2371 	gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
2372 			 GTK_FILL, GTK_EXPAND, 2, 2);
2373 #endif
2374 	frdp->with = gtk_entry_new();
2375 	g_signal_connect(G_OBJECT(frdp->with), "activate",
2376 			 G_CALLBACK(find_replace_cb),
2377 			 GINT_TO_POINTER(FRD_R_FINDNEXT));
2378 	g_signal_connect_after(G_OBJECT(frdp->with), "key-press-event",
2379 			       G_CALLBACK(find_key_press_event),
2380 			       (gpointer) frdp);
2381 #if GTK_CHECK_VERSION(3,4,0)
2382 	gtk_grid_attach(GTK_GRID(table), frdp->with, 2, 1, 5, 1);
2383 #else
2384 	gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
2385 			 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2386 #endif
2387 
2388 	/*
2389 	 * Make the entry activation only change the input focus onto the
2390 	 * with item.
2391 	 */
2392 	g_signal_connect(G_OBJECT(frdp->what), "activate",
2393 			 G_CALLBACK(entry_activate_cb), frdp->with);
2394     }
2395     else
2396     {
2397 	/*
2398 	 * Make the entry activation do the search.
2399 	 */
2400 	g_signal_connect(G_OBJECT(frdp->what), "activate",
2401 			 G_CALLBACK(find_replace_cb),
2402 			 GINT_TO_POINTER(FRD_FINDNEXT));
2403     }
2404 
2405     // whole word only button
2406     frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
2407     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2408 							(gboolean)wword);
2409     if (do_replace)
2410 #if GTK_CHECK_VERSION(3,4,0)
2411 	gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 2, 5, 1);
2412 #else
2413 	gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
2414 			 GTK_FILL, GTK_EXPAND, 2, 2);
2415 #endif
2416     else
2417 #if GTK_CHECK_VERSION(3,4,0)
2418 	gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 3, 5, 1);
2419 #else
2420 	gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
2421 			 GTK_FILL, GTK_EXPAND, 2, 2);
2422 #endif
2423 
2424     // match case button
2425     frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
2426     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2427 							     (gboolean)mcase);
2428     if (do_replace)
2429 #if GTK_CHECK_VERSION(3,4,0)
2430 	gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 3, 5, 1);
2431 #else
2432 	gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
2433 			 GTK_FILL, GTK_EXPAND, 2, 2);
2434 #endif
2435     else
2436 #if GTK_CHECK_VERSION(3,4,0)
2437 	gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 4, 5, 1);
2438 #else
2439 	gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
2440 			 GTK_FILL, GTK_EXPAND, 2, 2);
2441 #endif
2442 
2443     tmp = gtk_frame_new(CONV(_("Direction")));
2444     if (do_replace)
2445 #if GTK_CHECK_VERSION(3,4,0)
2446 	gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 2, 4);
2447 #else
2448 	gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
2449 			 GTK_FILL, GTK_FILL, 2, 2);
2450 #endif
2451     else
2452 #if GTK_CHECK_VERSION(3,4,0)
2453 	gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 1, 3);
2454 #else
2455 	gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
2456 			 GTK_FILL, GTK_FILL, 2, 2);
2457 #endif
2458 #if GTK_CHECK_VERSION(3,2,0)
2459     vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2460     gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
2461 #else
2462     vbox = gtk_vbox_new(FALSE, 0);
2463 #endif
2464     gtk_container_set_border_width(GTK_CONTAINER(vbox), 0);
2465     gtk_container_add(GTK_CONTAINER(tmp), vbox);
2466 
2467     // 'Up' and 'Down' buttons
2468     frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
2469     gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
2470     frdp->down = gtk_radio_button_new_with_label(
2471 			gtk_radio_button_get_group(GTK_RADIO_BUTTON(frdp->up)),
2472 			CONV(_("Down")));
2473     gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
2474     gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
2475     gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
2476 
2477     // vbox to hold the action buttons
2478 #if GTK_CHECK_VERSION(3,2,0)
2479     actionarea = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
2480 #else
2481     actionarea = gtk_vbutton_box_new();
2482 #endif
2483     gtk_container_set_border_width(GTK_CONTAINER(actionarea), 2);
2484     gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
2485 
2486     // 'Find Next' button
2487 #if GTK_CHECK_VERSION(3,10,0)
2488     frdp->find = create_image_button(NULL, _("Find Next"));
2489 #else
2490     frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
2491 #endif
2492     gtk_widget_set_sensitive(frdp->find, sensitive);
2493 
2494     g_signal_connect(G_OBJECT(frdp->find), "clicked",
2495 		     G_CALLBACK(find_replace_cb),
2496 		     (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2497 				  : GINT_TO_POINTER(FRD_FINDNEXT));
2498 
2499     gtk_widget_set_can_default(frdp->find, TRUE);
2500     gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
2501     gtk_widget_grab_default(frdp->find);
2502 
2503     if (do_replace)
2504     {
2505 	// 'Replace' button
2506 #if GTK_CHECK_VERSION(3,10,0)
2507 	frdp->replace = create_image_button(NULL, _("Replace"));
2508 #else
2509 	frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
2510 #endif
2511 	gtk_widget_set_sensitive(frdp->replace, sensitive);
2512 	gtk_widget_set_can_default(frdp->find, TRUE);
2513 	gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
2514 	g_signal_connect(G_OBJECT(frdp->replace), "clicked",
2515 			 G_CALLBACK(find_replace_cb),
2516 			 GINT_TO_POINTER(FRD_REPLACE));
2517 
2518 	// 'Replace All' button
2519 #if GTK_CHECK_VERSION(3,10,0)
2520 	frdp->all = create_image_button(NULL, _("Replace All"));
2521 #else
2522 	frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
2523 #endif
2524 	gtk_widget_set_sensitive(frdp->all, sensitive);
2525 	gtk_widget_set_can_default(frdp->all, TRUE);
2526 	gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
2527 	g_signal_connect(G_OBJECT(frdp->all), "clicked",
2528 			 G_CALLBACK(find_replace_cb),
2529 			 GINT_TO_POINTER(FRD_REPLACEALL));
2530     }
2531 
2532     // 'Cancel' button
2533 #if GTK_CHECK_VERSION(3,10,0)
2534     tmp = gtk_button_new_with_mnemonic(_("_Close"));
2535 #else
2536     tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
2537 #endif
2538     gtk_widget_set_can_default(tmp, TRUE);
2539     gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
2540     g_signal_connect_swapped(G_OBJECT(tmp),
2541 			     "clicked", G_CALLBACK(gtk_widget_hide),
2542 			     G_OBJECT(frdp->dialog));
2543     g_signal_connect_swapped(G_OBJECT(frdp->dialog),
2544 			     "delete-event", G_CALLBACK(gtk_widget_hide_on_delete),
2545 			     G_OBJECT(frdp->dialog));
2546 
2547 #if GTK_CHECK_VERSION(3,2,0)
2548     tmp = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
2549 #else
2550     tmp = gtk_vseparator_new();
2551 #endif
2552     gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
2553 
2554     // Suppress automatic show of the unused action area
2555 #if GTK_CHECK_VERSION(3,0,0)
2556 # if !GTK_CHECK_VERSION(3,12,0)
2557     gtk_widget_hide(gtk_dialog_get_action_area(GTK_DIALOG(frdp->dialog)));
2558 # endif
2559 #else
2560     gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
2561 #endif
2562     gtk_widget_show_all(hbox);
2563     gtk_widget_show(frdp->dialog);
2564 
2565     vim_free(entry_text);
2566     vim_free(conv_buffer);
2567 #undef CONV
2568 }
2569 
2570     void
2571 gui_mch_find_dialog(exarg_T *eap)
2572 {
2573     if (gui.in_use)
2574 	find_replace_dialog_create(eap->arg, FALSE);
2575 }
2576 
2577     void
2578 gui_mch_replace_dialog(exarg_T *eap)
2579 {
2580     if (gui.in_use)
2581 	find_replace_dialog_create(eap->arg, TRUE);
2582 }
2583 
2584 /*
2585  * Callback for actions of the find and replace dialogs
2586  */
2587     static void
2588 find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
2589 {
2590     int			flags;
2591     char_u		*find_text;
2592     char_u		*repl_text;
2593     gboolean		direction_down;
2594     SharedFindReplace	*sfr;
2595 
2596     flags = (int)(long)data;	    // avoid a lint warning here
2597 
2598     // Get the search/replace strings from the dialog
2599     if (flags == FRD_FINDNEXT)
2600     {
2601 	repl_text = NULL;
2602 	sfr = &find_widgets;
2603     }
2604     else
2605     {
2606 	repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
2607 	sfr = &repl_widgets;
2608     }
2609 
2610     find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
2611     direction_down = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->down));
2612 
2613     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->wword)))
2614 	flags |= FRD_WHOLE_WORD;
2615     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->mcase)))
2616 	flags |= FRD_MATCH_CASE;
2617 
2618     repl_text = CONVERT_FROM_UTF8(repl_text);
2619     find_text = CONVERT_FROM_UTF8(find_text);
2620     gui_do_findrepl(flags, find_text, repl_text, direction_down);
2621     CONVERT_FROM_UTF8_FREE(repl_text);
2622     CONVERT_FROM_UTF8_FREE(find_text);
2623 }
2624 
2625 /*
2626  * our usual callback function
2627  */
2628     static void
2629 entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
2630 {
2631     gtk_widget_grab_focus(GTK_WIDGET(data));
2632 }
2633 
2634 /*
2635  * Syncing the find/replace dialogs on the fly is utterly useless crack,
2636  * and causes nothing but problems.  Please tell me a use case for which
2637  * you'd need both a find dialog and a find/replace one at the same time,
2638  * without being able to actually use them separately since they're syncing
2639  * all the time.  I don't think it's worthwhile to fix this nonsense,
2640  * particularly evil incarnation of braindeadness, whatever; I'd much rather
2641  * see it extinguished from this planet.  Thanks for listening.  Sorry.
2642  */
2643     static void
2644 entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
2645 {
2646     const gchar	*entry_text;
2647     gboolean	nonempty;
2648 
2649     entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
2650 
2651     if (!entry_text)
2652 	return;			// shouldn't happen
2653 
2654     nonempty = (entry_text[0] != '\0');
2655 
2656     if (dialog == find_widgets.dialog)
2657 	gtk_widget_set_sensitive(find_widgets.find, nonempty);
2658 
2659     if (dialog == repl_widgets.dialog)
2660     {
2661 	gtk_widget_set_sensitive(repl_widgets.find, nonempty);
2662 	gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
2663 	gtk_widget_set_sensitive(repl_widgets.all, nonempty);
2664     }
2665 }
2666 
2667 /*
2668  * ":helpfind"
2669  */
2670     void
2671 ex_helpfind(exarg_T *eap UNUSED)
2672 {
2673     // This will fail when menus are not loaded.  Well, it's only for
2674     // backwards compatibility anyway.
2675     do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
2676 }
2677 
2678 #if defined(FEAT_BROWSE) || defined(PROTO)
2679     static void
2680 recent_func_log_func(const gchar *log_domain UNUSED,
2681 		     GLogLevelFlags log_level UNUSED,
2682 		     const gchar *message UNUSED,
2683 		     gpointer user_data UNUSED)
2684 {
2685     // We just want to suppress the warnings.
2686     // http://bugzilla.gnome.org/show_bug.cgi?id=664587
2687 }
2688 #endif
2689