blob: 6decec0df3c8ad7692be1f1dc6b8db39bbcab735 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Porting to GTK+ was done by:
12 *
Bram Moolenaarfca34d62005-01-04 21:38:36 +000013 * (C) 1998,1999,2000 by Marcin Dalecki <martin@dalecki.de>
Bram Moolenaar071d4272004-06-13 20:20:40 +000014 *
15 * With GREAT support and continuous encouragements by Andy Kahn and of
16 * course Bram Moolenaar!
17 *
18 * Support for GTK+ 2 was added by:
19 *
20 * (C) 2002,2003 Jason Hildebrand <jason@peaceworks.ca>
21 * Daniel Elstner <daniel.elstner@gmx.net>
22 *
23 * Best supporting actor (He helped somewhat, aesthetically speaking):
24 * Maxime Romano <verbophobe@hotmail.com>
Bram Moolenaar98921892016-02-23 17:14:37 +010025 *
26 * Support for GTK+ 3 was added by:
27 *
28 * 2016 Kazunobu Kuriyama <kazunobu.kuriyama@gmail.com>
29 *
30 * With the help of Marius Gedminas and the word of Bram Moolenaar,
31 * "Let's give this some time to mature."
Bram Moolenaar071d4272004-06-13 20:20:40 +000032 */
33
Bram Moolenaar98921892016-02-23 17:14:37 +010034#include "vim.h"
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036#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
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#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
Bram Moolenaara2dd9002007-05-14 17:38:30 +000063# ifdef bind_textdomain_codeset
64# undef bind_textdomain_codeset
Bram Moolenaar79166c42007-05-10 18:29:51 +000065# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000066# 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
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#ifdef FEAT_GUI_GTK
Bram Moolenaar98921892016-02-23 17:14:37 +010073# if GTK_CHECK_VERSION(3,0,0)
74# include <gdk/gdkkeysyms-compat.h>
75# else
76# include <gdk/gdkkeysyms.h>
77# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000078# include <gdk/gdk.h>
79# ifdef WIN3264
80# include <gdk/gdkwin32.h>
81# else
82# include <gdk/gdkx.h>
83# endif
84
85# include <gtk/gtk.h>
86#else
87/* define these items to be able to generate prototypes without GTK */
88typedef 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101static void entry_activate_cb(GtkWidget *widget, gpointer data);
102static void entry_changed_cb(GtkWidget *entry, GtkWidget *dialog);
103static void find_replace_cb(GtkWidget *widget, gpointer data);
Bram Moolenaar08bc2742012-06-06 16:14:40 +0200104#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +0200105static void recent_func_log_func(
106 const gchar *log_domain,
107 GLogLevelFlags log_level,
108 const gchar *message,
109 gpointer user_data);
Bram Moolenaar08bc2742012-06-06 16:14:40 +0200110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200112#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113/*
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 */
Bram Moolenaar98921892016-02-23 17:14:37 +0100118# if GTK_CHECK_VERSION(3,10,0)
119static 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) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182static 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};
Bram Moolenaar98921892016-02-23 17:14:37 +0100217# endif /* !GTK_CHECK_VERSION(3,10,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218
Bram Moolenaar98921892016-02-23 17:14:37 +0100219# ifdef USE_GRESOURCE
220# if !GTK_CHECK_VERSION(3,10,0)
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100221typedef struct IconNames {
222 const char *icon_name;
223 const char *file_name;
224} IconNames;
225
226static 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};
Bram Moolenaar98921892016-02-23 17:14:37 +0100242# endif
243# endif /* USE_G_RESOURCE */
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100244
Bram Moolenaar98921892016-02-23 17:14:37 +0100245# ifndef USE_GRESOURCE
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 static void
247add_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}
Bram Moolenaar98921892016-02-23 17:14:37 +0100263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265 static int
266lookup_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 *
292load_menu_iconfile(char_u *name, GtkIconSize icon_size)
293{
294 GtkWidget *image = NULL;
Bram Moolenaar98921892016-02-23 17:14:37 +0100295# 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) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 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;
Bram Moolenaar98921892016-02-23 17:14:37 +0100361# endif /* !GTK_CHECK_VERSION(3,10,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362}
363
364 static GtkWidget *
365create_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 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100382# 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 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);
Bram Moolenaar98921892016-02-23 17:14:37 +0100402# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 }
404
405 return image;
406}
407
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000408 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000409toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
410 GdkEventFocus *event UNUSED,
411 gpointer data UNUSED)
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000412{
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000413 /* 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. */
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000416 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000417 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000418
419 return TRUE;
420}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200421#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200423#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424
425 void
426gui_gtk_register_stock_icons(void)
427{
Bram Moolenaar98921892016-02-23 17:14:37 +0100428# ifndef USE_GRESOURCE
429# include "../pixmaps/stock_icons.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 GtkIconFactory *factory;
431
432 factory = gtk_icon_factory_new();
Bram Moolenaar98921892016-02-23 17:14:37 +0100433# define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434
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
Bram Moolenaar98921892016-02-23 17:14:37 +0100449# undef ADD_ICON
450
451 gtk_icon_factory_add_default(factory);
452 g_object_unref(factory);
453# else /* defined(USE_GRESOURCE) */
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100454 const char * const path_prefix = "/org/vim/gui/icon";
Bram Moolenaar98921892016-02-23 17:14:37 +0100455# 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)
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100466 IconNames *names;
467
468 for (names = stock_vim_icons; names->icon_name != NULL; names++)
469 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100470 char path[MAXPATHL];
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100471
Bram Moolenaar98921892016-02-23 17:14:37 +0100472 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 }
Bram Moolenaar36e294c2015-12-29 18:55:46 +0100505 }
Bram Moolenaar98921892016-02-23 17:14:37 +0100506# 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 gtk_icon_factory_add_default(factory);
527 g_object_unref(factory);
Bram Moolenaar98921892016-02-23 17:14:37 +0100528# endif /* !GTK_CHECK_VERSION(3,0,0) */
529# endif /* defined(USE_GRESOURCE) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200532#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534#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 *
547translate_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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 name = CONVERT_TO_UTF8(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 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((unsigned)(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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 CONVERT_TO_UTF8_FREE(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 return buf;
591}
592
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 static void
594menu_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();
Bram Moolenaar98921892016-02-23 17:14:37 +0100604# 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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 box = gtk_hbox_new(FALSE, 20);
Bram Moolenaar98921892016-02-23 17:14:37 +0100609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
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
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 void
635gui_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 /* since the tearoff should always appear first, increment idx */
656 if (parent != NULL && !menu_is_popup(parent->name))
657 ++idx;
658
659 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
660
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 menu->submenu_id = gtk_menu_new();
662
663 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
664 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
665
Bram Moolenaar98921892016-02-23 17:14:37 +0100666# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 menu->tearoff_handle = gtk_tearoff_menu_item_new();
668 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
669 gtk_widget_show(menu->tearoff_handle);
Bram Moolenaar98921892016-02-23 17:14:37 +0100670# if GTK_CHECK_VERSION(3,0,0)
671 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu->submenu_id),
672 menu->tearoff_handle);
673# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
Bram Moolenaar98921892016-02-23 17:14:37 +0100675# endif
676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677}
678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000680menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 gui_menu_cb((vimmenu_T *)data);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683}
684
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 void
686gui_mch_add_menu_item(vimmenu_T *menu, int idx)
687{
688 vimmenu_T *parent;
689
690 parent = menu->parent;
691
692# ifdef FEAT_TOOLBAR
693 if (menu_is_toolbar(parent->name))
694 {
695 GtkToolbar *toolbar;
696
697 toolbar = GTK_TOOLBAR(gui.toolbar);
698 menu->submenu_id = NULL;
699
700 if (menu_is_separator(menu->name))
701 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100702# if GTK_CHECK_VERSION(3,0,0)
703 GtkToolItem *item = gtk_separator_tool_item_new();
704 gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),
705 TRUE);
706 gtk_tool_item_set_expand(GTK_TOOL_ITEM(item), FALSE);
707 gtk_widget_show(GTK_WIDGET(item));
708
709 gtk_toolbar_insert(toolbar, item, idx);
710# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 gtk_toolbar_insert_space(toolbar, idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100712# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 menu->id = NULL;
714 }
715 else
716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 char_u *text;
718 char_u *tooltip;
719
720 text = CONVERT_TO_UTF8(menu->dname);
721 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000722 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
723 /* Invalid text, can happen when 'encoding' is changed. Avoid
724 * a nasty GTK error message, skip the tooltip. */
725 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaar98921892016-02-23 17:14:37 +0100727# if GTK_CHECK_VERSION(3,0,0)
728 {
729 GtkWidget *icon;
730 GtkToolItem *item;
731
732 icon = create_menu_icon(menu,
733 gtk_toolbar_get_icon_size(toolbar));
734 item = gtk_tool_button_new(icon, (const gchar *)text);
735 gtk_tool_item_set_tooltip_text(item, (const gchar *)tooltip);
736 g_signal_connect(G_OBJECT(item), "clicked",
737 G_CALLBACK(&menu_item_activate), menu);
738 gtk_widget_show_all(GTK_WIDGET(item));
739
740 gtk_toolbar_insert(toolbar, item, idx);
741
742 menu->id = GTK_WIDGET(item);
743 }
744# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 menu->id = gtk_toolbar_insert_item(
746 toolbar,
747 (const char *)text,
748 (const char *)tooltip,
749 NULL,
750 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
751 G_CALLBACK(&menu_item_activate),
752 menu,
753 idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100754# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000756 if (gtk_socket_id != 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100757# if GTK_CHECK_VERSION(3,0,0)
758 g_signal_connect(G_OBJECT(menu->id), "focus-in-event",
759 G_CALLBACK(toolbar_button_focus_in_event), NULL);
760# else
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000761 gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
762 GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +0100763# endif
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 CONVERT_TO_UTF8_FREE(text);
766 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 }
768 }
769 else
770# endif /* FEAT_TOOLBAR */
771 {
772 /* No parent, must be a non-menubar menu */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000773 if (parent == NULL || parent->submenu_id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 return;
775
776 /* Make place for the possible tearoff handle item. Not in the popup
777 * menu, it doesn't have a tearoff item. */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000778 if (!menu_is_popup(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ++idx;
780
781 if (menu_is_separator(menu->name))
782 {
783 /* Separator: Just add it */
784 menu->id = gtk_menu_item_new();
785 gtk_widget_set_sensitive(menu->id, FALSE);
786 gtk_widget_show(menu->id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100787# if GTK_CHECK_VERSION(3,0,0)
788 gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
789 menu->id, idx);
790# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
794 return;
795 }
796
797 /* Add textual menu item. */
798 menu_item_new(menu, parent->submenu_id);
799 gtk_widget_show(menu->id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100800# if GTK_CHECK_VERSION(3,0,0)
801 gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
802 menu->id, idx);
803# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
807 if (menu->id != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +0100808# if GTK_CHECK_VERSION(3,0,0)
809 g_signal_connect(G_OBJECT(menu->id), "activate",
810 G_CALLBACK(menu_item_activate), menu);
811# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
813 GTK_SIGNAL_FUNC(menu_item_activate), menu);
Bram Moolenaar98921892016-02-23 17:14:37 +0100814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816}
817#endif /* FEAT_MENU */
818
819
820 void
821gui_mch_set_text_area_pos(int x, int y, int w, int h)
822{
823 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
824}
825
826
827#if defined(FEAT_MENU) || defined(PROTO)
828/*
Bram Moolenaar79166c42007-05-10 18:29:51 +0000829 * Enable or disable accelerators for the toplevel menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 */
831 void
832gui_gtk_set_mnemonics(int enable)
833{
834 vimmenu_T *menu;
835 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837 for (menu = root_menu; menu != NULL; menu = menu->next)
838 {
839 if (menu->id == NULL)
840 continue;
841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 name = translate_mnemonic_tag(menu->name, enable);
843 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
844 (const char *)name);
845 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847}
848
Bram Moolenaar98921892016-02-23 17:14:37 +0100849# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 static void
851recurse_tearoffs(vimmenu_T *menu, int val)
852{
853 for (; menu != NULL; menu = menu->next)
854 {
855 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
856 && menu->name[0] != ']' && !menu_is_popup(menu->name))
857 {
858 if (val)
859 gtk_widget_show(menu->tearoff_handle);
860 else
861 gtk_widget_hide(menu->tearoff_handle);
862 }
863 recurse_tearoffs(menu->children, val);
864 }
865}
Bram Moolenaar98921892016-02-23 17:14:37 +0100866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867
Bram Moolenaar98921892016-02-23 17:14:37 +0100868# if GTK_CHECK_VERSION(3,4,0)
869 void
870gui_mch_toggle_tearoffs(int enable UNUSED)
871{
872 /* Do nothing */
873}
874# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 void
876gui_mch_toggle_tearoffs(int enable)
877{
878 recurse_tearoffs(root_menu, enable);
879}
Bram Moolenaar98921892016-02-23 17:14:37 +0100880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif /* FEAT_MENU */
882
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200883#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 static int
885get_menu_position(vimmenu_T *menu)
886{
887 vimmenu_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000888 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890 for (node = menu->parent->children; node != menu; node = node->next)
891 {
892 g_return_val_if_fail(node != NULL, -1);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000893 ++idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895
Bram Moolenaar89d40322006-08-29 15:30:07 +0000896 return idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200898#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
900
901#if defined(FEAT_TOOLBAR) || defined(PROTO)
902 void
903gui_mch_menu_set_tip(vimmenu_T *menu)
904{
905 if (menu->id != NULL && menu->parent != NULL
906 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
907 {
908 char_u *tooltip;
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar98921892016-02-23 17:14:37 +0100911 if (tooltip != NULL && utf_valid_string(tooltip, NULL))
912# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000913 /* Only set the tooltip when it's valid utf-8. */
Bram Moolenaar98921892016-02-23 17:14:37 +0100914 gtk_widget_set_tooltip_text(menu->id, (const gchar *)tooltip);
915# else
916 /* Only set the tooltip when it's valid utf-8. */
917 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
918 menu->id, (const char *)tooltip, NULL);
919# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 }
922}
923#endif /* FEAT_TOOLBAR */
924
925
926#if defined(FEAT_MENU) || defined(PROTO)
927/*
928 * Destroy the machine specific menu widget.
929 */
930 void
931gui_mch_destroy_menu(vimmenu_T *menu)
932{
Bram Moolenaarf9da6802013-07-03 13:04:27 +0200933 /* Don't let gtk_container_remove automatically destroy menu->id. */
934 if (menu->id != NULL)
935 g_object_ref(menu->id);
936
937 /* Workaround for a spurious gtk warning in Ubuntu: "Trying to remove
938 * a child that doesn't believe we're it's parent."
939 * Remove widget from gui.menubar before destroying it. */
940 if (menu->id != NULL && gui.menubar != NULL
941 && gtk_widget_get_parent(menu->id) == gui.menubar)
942 gtk_container_remove(GTK_CONTAINER(gui.menubar), menu->id);
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944# ifdef FEAT_TOOLBAR
945 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (menu_is_separator(menu->name))
Bram Moolenaar98921892016-02-23 17:14:37 +0100948# if GTK_CHECK_VERSION(3,0,0)
949 {
950 GtkToolItem *item = NULL;
951
952 item = gtk_toolbar_get_nth_item(GTK_TOOLBAR(gui.toolbar),
953 get_menu_position(menu));
954 if (item != NULL)
955 gtk_container_remove(GTK_CONTAINER(gui.toolbar),
956 GTK_WIDGET(item));
957 }
958# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
960 get_menu_position(menu));
Bram Moolenaar98921892016-02-23 17:14:37 +0100961# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 else if (menu->id != NULL)
963 gtk_widget_destroy(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 else
966# endif /* FEAT_TOOLBAR */
967 {
968 if (menu->submenu_id != NULL)
969 gtk_widget_destroy(menu->submenu_id);
970
971 if (menu->id != NULL)
972 gtk_widget_destroy(menu->id);
973 }
974
Bram Moolenaarf9da6802013-07-03 13:04:27 +0200975 if (menu->id != NULL)
976 g_object_unref(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 menu->submenu_id = NULL;
978 menu->id = NULL;
979}
980#endif /* FEAT_MENU */
981
982
983/*
984 * Scrollbar stuff.
985 */
986 void
987gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
988{
989 if (sb->id != NULL)
990 {
991 GtkAdjustment *adjustment;
992
993 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
994
Bram Moolenaar98921892016-02-23 17:14:37 +0100995#if GTK_CHECK_VERSION(3,0,0)
996 gtk_adjustment_set_lower(adjustment, 0.0);
997 gtk_adjustment_set_value(adjustment, val);
998 gtk_adjustment_set_upper(adjustment, max + 1);
999 gtk_adjustment_set_page_size(adjustment, size);
1000 gtk_adjustment_set_page_increment(adjustment,
1001 size < 3L ? 1L : size - 2L);
1002 gtk_adjustment_set_step_increment(adjustment, 1.0);
1003#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 adjustment->lower = 0.0;
1005 adjustment->value = val;
1006 adjustment->upper = max + 1;
1007 adjustment->page_size = size;
1008 adjustment->page_increment = size < 3L ? 1L : size - 2L;
1009 adjustment->step_increment = 1.0;
Bram Moolenaar98921892016-02-23 17:14:37 +01001010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
Bram Moolenaar98921892016-02-23 17:14:37 +01001012#if GTK_CHECK_VERSION(3,0,0)
1013 g_signal_handler_block(G_OBJECT(adjustment),
1014 (gulong)sb->handler_id);
1015#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 g_signal_handler_block(GTK_OBJECT(adjustment),
1017 (gulong)sb->handler_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01001018#endif
1019
1020#if !GTK_CHECK_VERSION(3,18,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 gtk_adjustment_changed(adjustment);
Bram Moolenaar98921892016-02-23 17:14:37 +01001022#endif
1023
1024#if GTK_CHECK_VERSION(3,0,0)
1025 g_signal_handler_unblock(G_OBJECT(adjustment),
1026 (gulong)sb->handler_id);
1027#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 g_signal_handler_unblock(GTK_OBJECT(adjustment),
1029 (gulong)sb->handler_id);
Bram Moolenaar98921892016-02-23 17:14:37 +01001030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032}
1033
1034 void
1035gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1036{
1037 if (sb->id != NULL)
1038 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
1039}
1040
1041/*
1042 * Take action upon scrollbar dragging.
1043 */
1044 static void
1045adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
1046{
1047 scrollbar_T *sb;
1048 long value;
1049 int dragging = FALSE;
1050
1051#ifdef FEAT_XIM
1052 /* cancel any preediting */
1053 if (im_is_preediting())
1054 xim_reset();
1055#endif
1056
1057 sb = gui_find_scrollbar((long)data);
Bram Moolenaar98921892016-02-23 17:14:37 +01001058#if GTK_CHECK_VERSION(3,0,0)
1059 value = gtk_adjustment_get_value(adjustment);
1060#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 value = (long)adjustment->value;
Bram Moolenaar98921892016-02-23 17:14:37 +01001062#endif
1063#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 /*
1065 * The dragging argument must be right for the scrollbar to work with
1066 * closed folds. This isn't documented, hopefully this will keep on
1067 * working in later GTK versions.
1068 *
1069 * FIXME: Well, it doesn't work in GTK2. :)
1070 * HACK: Get the mouse pointer position, if it appears to be on an arrow
1071 * button set "dragging" to FALSE. This assumes square buttons!
1072 */
1073 if (sb != NULL)
1074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 dragging = TRUE;
1076
1077 if (sb->wp != NULL)
1078 {
1079 int x;
1080 int y;
1081 GdkModifierType state;
1082 int width;
1083 int height;
1084
1085 /* vertical scrollbar: need to set "dragging" properly in case
1086 * there are closed folds. */
1087 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
1088 gdk_window_get_size(sb->id->window, &width, &height);
1089 if (x >= 0 && x < width && y >= 0 && y < height)
1090 {
1091 if (y < width)
1092 {
1093 /* up arrow: move one (closed fold) line up */
1094 dragging = FALSE;
1095 value = sb->wp->w_topline - 2;
1096 }
1097 else if (y > height - width)
1098 {
1099 /* down arrow: move one (closed fold) line down */
1100 dragging = FALSE;
1101 value = sb->wp->w_topline;
1102 }
1103 }
1104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001106#endif /* !GTK_CHECK_VERSION(3,0,0) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108}
1109
1110/* SBAR_VERT or SBAR_HORIZ */
1111 void
1112gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1113{
1114 if (orient == SBAR_HORIZ)
Bram Moolenaar98921892016-02-23 17:14:37 +01001115#if GTK_CHECK_VERSION(3,2,0)
1116 sb->id = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, NULL);
1117#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 sb->id = gtk_hscrollbar_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 else if (orient == SBAR_VERT)
Bram Moolenaar98921892016-02-23 17:14:37 +01001121#if GTK_CHECK_VERSION(3,2,0)
1122 sb->id = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, NULL);
1123#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 sb->id = gtk_vscrollbar_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
1127 if (sb->id != NULL)
1128 {
1129 GtkAdjustment *adjustment;
1130
Bram Moolenaar98921892016-02-23 17:14:37 +01001131#if GTK_CHECK_VERSION(3,0,0)
1132 gtk_widget_set_can_focus(sb->id, FALSE);
1133#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
Bram Moolenaar98921892016-02-23 17:14:37 +01001135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
1137
1138 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1139
Bram Moolenaar98921892016-02-23 17:14:37 +01001140#if GTK_CHECK_VERSION(3,0,0)
1141 sb->handler_id = g_signal_connect(
1142 G_OBJECT(adjustment), "value-changed",
1143 G_CALLBACK(adjustment_value_changed),
1144 GINT_TO_POINTER(sb->ident));
1145#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 sb->handler_id = gtk_signal_connect(
1147 GTK_OBJECT(adjustment), "value_changed",
1148 GTK_SIGNAL_FUNC(adjustment_value_changed),
1149 GINT_TO_POINTER(sb->ident));
Bram Moolenaar98921892016-02-23 17:14:37 +01001150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 gui_mch_update();
1152 }
1153}
1154
1155#if defined(FEAT_WINDOWS) || defined(PROTO)
1156 void
1157gui_mch_destroy_scrollbar(scrollbar_T *sb)
1158{
1159 if (sb->id != NULL)
1160 {
1161 gtk_widget_destroy(sb->id);
1162 sb->id = NULL;
1163 }
1164 gui_mch_update();
1165}
1166#endif
1167
1168#if defined(FEAT_BROWSE) || defined(PROTO)
1169/*
1170 * Implementation of the file selector related stuff
1171 */
1172
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001173#ifndef USE_FILE_CHOOSER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001175browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176{
1177 gui_T *vw = (gui_T *)cbdata;
1178
1179 if (vw->browse_fname != NULL)
1180 g_free(vw->browse_fname);
1181
1182 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
1183 GTK_FILE_SELECTION(vw->filedlg)));
1184 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185}
1186
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001188browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189{
1190 gui_T *vw = (gui_T *)cbdata;
1191
1192 if (vw->browse_fname != NULL)
1193 {
1194 g_free(vw->browse_fname);
1195 vw->browse_fname = NULL;
1196 }
1197 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198}
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001201browse_destroy_cb(GtkWidget *widget UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202{
1203 if (gui.browse_fname != NULL)
1204 {
1205 g_free(gui.browse_fname);
1206 gui.browse_fname = NULL;
1207 }
1208 gui.filedlg = NULL;
Bram Moolenaara3f41662010-07-11 19:01:06 +02001209 gtk_main_quit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return FALSE;
1211}
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213
1214/*
1215 * Put up a file requester.
1216 * Returns the selected name in allocated memory, or NULL for Cancel.
1217 * saving, select file to write
1218 * title title for the window
1219 * dflt default name
1220 * ext not used (extension added)
1221 * initdir initial directory, NULL for current dir
1222 * filter not used (file name filter)
1223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001225gui_mch_browse(int saving UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 char_u *title,
1227 char_u *dflt,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001228 char_u *ext UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 char_u *initdir,
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001230 char_u *filter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231{
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001232#ifdef USE_FILE_CHOOSER
1233 GtkWidget *fc;
1234#endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001235 char_u dirbuf[MAXPATHL];
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001236 guint log_handler;
1237 const gchar *domain = "Gtk";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 title = CONVERT_TO_UTF8(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240
Bram Moolenaar57ac3a22006-10-10 16:28:30 +00001241 /* GTK has a bug, it only works with an absolute path. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001242 if (initdir == NULL || *initdir == NUL)
1243 mch_dirname(dirbuf, MAXPATHL);
Bram Moolenaar57ac3a22006-10-10 16:28:30 +00001244 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001245 dirbuf[0] = NUL;
1246 /* Always need a trailing slash for a directory. */
1247 add_pathsep(dirbuf);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001248
1249 /* If our pointer is currently hidden, then we should show it. */
1250 gui_mch_mousehide(FALSE);
1251
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001252 /* Hack: The GTK file dialog warns when it can't access a new file, this
1253 * makes it shut up. http://bugzilla.gnome.org/show_bug.cgi?id=664587 */
1254 log_handler = g_log_set_handler(domain, G_LOG_LEVEL_WARNING,
1255 recent_func_log_func, NULL);
1256
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001257#ifdef USE_FILE_CHOOSER
1258 /* We create the dialog each time, so that the button text can be "Open"
1259 * or "Save" according to the action. */
1260 fc = gtk_file_chooser_dialog_new((const gchar *)title,
1261 GTK_WINDOW(gui.mainwin),
1262 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
1263 : GTK_FILE_CHOOSER_ACTION_OPEN,
Bram Moolenaar98921892016-02-23 17:14:37 +01001264# if GTK_CHECK_VERSION(3,10,0)
1265 _("_Cancel"), GTK_RESPONSE_CANCEL,
1266 saving ? _("_Save") : _("_Open"), GTK_RESPONSE_ACCEPT,
1267# else
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001268 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
Bram Moolenaard1350622006-10-24 20:01:06 +00001269 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
Bram Moolenaar98921892016-02-23 17:14:37 +01001270# endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001271 NULL);
1272 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
1273 (const gchar *)dirbuf);
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001274
1275 if (filter != NULL && *filter != NUL)
1276 {
1277 int i = 0;
1278 char_u *patt;
1279 char_u *p = filter;
Bram Moolenaar205f9f52012-10-18 05:18:32 +02001280 GtkFileFilter *gfilter;
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001281
1282 gfilter = gtk_file_filter_new();
1283 patt = alloc(STRLEN(filter));
1284 while (p != NULL && *p != NUL)
1285 {
1286 if (*p == '\n' || *p == ';' || *p == '\t')
1287 {
1288 STRNCPY(patt, filter, i);
1289 patt[i] = '\0';
1290 if (*p == '\t')
1291 gtk_file_filter_set_name(gfilter, (gchar *)patt);
1292 else
1293 {
1294 gtk_file_filter_add_pattern(gfilter, (gchar *)patt);
1295 if (*p == '\n')
1296 {
1297 gtk_file_chooser_add_filter((GtkFileChooser *)fc,
1298 gfilter);
1299 if (*(p + 1) != NUL)
1300 gfilter = gtk_file_filter_new();
1301 }
1302 }
1303 filter = ++p;
1304 i = 0;
1305 }
1306 else
1307 {
1308 p++;
1309 i++;
1310 }
1311 }
1312 vim_free(patt);
1313 }
Bram Moolenaara3f41662010-07-11 19:01:06 +02001314 if (saving && dflt != NULL && *dflt != NUL)
1315 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001316
1317 gui.browse_fname = NULL;
1318 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001320 char *filename;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001321
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001322 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
1323 gui.browse_fname = (char_u *)g_strdup(filename);
1324 g_free(filename);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001325 }
1326 gtk_widget_destroy(GTK_WIDGET(fc));
1327
Bram Moolenaar98921892016-02-23 17:14:37 +01001328#else /* !USE_FILE_CHOOSER */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001329
1330 if (gui.filedlg == NULL)
1331 {
1332 GtkFileSelection *fs; /* shortcut */
1333
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 gui.filedlg = gtk_file_selection_new((const gchar *)title);
1335 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
1336 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001337 GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 fs = GTK_FILE_SELECTION(gui.filedlg);
1339
1340 gtk_container_border_width(GTK_CONTAINER(fs), 4);
1341
1342 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
1343 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
1344 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
1345 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
1346 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
1347 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
1348 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
1349 GTK_OBJECT(gui.filedlg));
1350 }
1351 else
1352 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
1353
Bram Moolenaar57ac3a22006-10-10 16:28:30 +00001354 /* Concatenate "initdir" and "dflt". */
1355 if (dflt != NULL && *dflt != NUL
1356 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
1357 STRCAT(dirbuf, dflt);
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
1360 (const gchar *)dirbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
1362 gtk_widget_show(gui.filedlg);
Bram Moolenaara3f41662010-07-11 19:01:06 +02001363 gtk_main();
Bram Moolenaar98921892016-02-23 17:14:37 +01001364#endif /* !USE_FILE_CHOOSER */
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001365 g_log_remove_handler(domain, log_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001367 CONVERT_TO_UTF8_FREE(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 if (gui.browse_fname == NULL)
1369 return NULL;
1370
1371 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00001372 return vim_strsave(shorten_fname1(gui.browse_fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373}
1374
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001375/*
1376 * Put up a directory selector
1377 * Returns the selected name in allocated memory, or NULL for Cancel.
1378 * title title for the window
1379 * dflt default name
1380 * initdir initial directory, NULL for current dir
1381 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001382 char_u *
1383gui_mch_browsedir(
1384 char_u *title,
1385 char_u *initdir)
1386{
1387# if defined(GTK_FILE_CHOOSER) /* Only in GTK 2.4 and later. */
1388 char_u dirbuf[MAXPATHL];
1389 char_u *p;
1390 GtkWidget *dirdlg; /* file selection dialog */
1391 char_u *dirname = NULL;
1392
1393 title = CONVERT_TO_UTF8(title);
1394
1395 dirdlg = gtk_file_chooser_dialog_new(
1396 (const gchar *)title,
1397 GTK_WINDOW(gui.mainwin),
1398 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
Bram Moolenaar98921892016-02-23 17:14:37 +01001399# if GTK_CHECK_VERSION(3,10,0)
1400 _("_Cancel"), GTK_RESPONSE_CANCEL,
1401 _("_OK"), GTK_RESPONSE_ACCEPT,
1402# else
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001403 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1404 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
Bram Moolenaar98921892016-02-23 17:14:37 +01001405# endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001406 NULL);
1407
1408 CONVERT_TO_UTF8_FREE(title);
1409
1410 /* if our pointer is currently hidden, then we should show it. */
1411 gui_mch_mousehide(FALSE);
1412
1413 /* GTK appears to insist on an absolute path. */
1414 if (initdir == NULL || *initdir == NUL
1415 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
1416 mch_dirname(dirbuf, MAXPATHL - 10);
1417
1418 /* Always need a trailing slash for a directory.
1419 * Also add a dummy file name, so that we get to the directory. */
1420 add_pathsep(dirbuf);
1421 STRCAT(dirbuf, "@zd(*&1|");
1422 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
1423 (const gchar *)dirbuf);
1424
1425 /* Run the dialog. */
1426 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
1427 dirname = (char_u *)gtk_file_chooser_get_filename(
1428 GTK_FILE_CHOOSER(dirdlg));
1429 gtk_widget_destroy(dirdlg);
1430 if (dirname == NULL)
1431 return NULL;
1432
1433 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00001434 p = vim_strsave(shorten_fname1(dirname));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001435 g_free(dirname);
1436 return p;
1437
Bram Moolenaar98921892016-02-23 17:14:37 +01001438# else /* !defined(GTK_FILE_CHOOSER) */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001439 /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
1440 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001441# endif /* !defined(GTK_FILE_CHOOSER) */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001442}
1443
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001444
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445#endif /* FEAT_BROWSE */
1446
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001447#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
1449 static GtkWidget *
1450create_message_dialog(int type, char_u *title, char_u *message)
1451{
1452 GtkWidget *dialog;
1453 GtkMessageType message_type;
1454
1455 switch (type)
1456 {
1457 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1458 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1459 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1460 default: message_type = GTK_MESSAGE_INFO; break;
1461 }
1462
1463 message = CONVERT_TO_UTF8(message);
1464 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1465 GTK_DIALOG_DESTROY_WITH_PARENT,
1466 message_type,
1467 GTK_BUTTONS_NONE,
1468 "%s", (const char *)message);
1469 CONVERT_TO_UTF8_FREE(message);
1470
1471 if (title != NULL)
1472 {
1473 title = CONVERT_TO_UTF8(title);
1474 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1475 CONVERT_TO_UTF8_FREE(title);
1476 }
1477 else if (type == VIM_GENERIC)
1478 {
1479 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1480 }
1481
1482 return dialog;
1483}
1484
1485/*
1486 * Split up button_string into individual button labels by inserting
1487 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1488 * '&' with '_'. button_string must point to allocated memory!
1489 * Return an allocated array of pointers into button_string.
1490 */
1491 static char **
1492split_button_string(char_u *button_string, int *n_buttons)
1493{
1494 char **array;
1495 char_u *p;
1496 unsigned int count = 1;
1497
1498 for (p = button_string; *p != NUL; ++p)
1499 if (*p == DLG_BUTTON_SEP)
1500 ++count;
1501
1502 array = (char **)alloc((count + 1) * sizeof(char *));
1503 count = 0;
1504
1505 if (array != NULL)
1506 {
1507 array[count++] = (char *)button_string;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001508 for (p = button_string; *p != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
1510 if (*p == DLG_BUTTON_SEP)
1511 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001512 *p++ = NUL;
1513 array[count++] = (char *)p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 }
1515 else if (*p == DLG_HOTKEY_CHAR)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001516 *p++ = '_';
1517 else
1518 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 }
1520 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1521 }
1522
1523 *n_buttons = count;
1524 return array;
1525}
1526
1527 static char **
1528split_button_translation(const char *message)
1529{
1530 char **buttons = NULL;
1531 char_u *str;
1532 int n_buttons = 0;
1533 int n_expected = 1;
1534
1535 for (str = (char_u *)message; *str != NUL; ++str)
1536 if (*str == DLG_BUTTON_SEP)
1537 ++n_expected;
1538
1539 str = (char_u *)_(message);
1540 if (str != NULL)
1541 {
1542 if (output_conv.vc_type != CONV_NONE)
1543 str = string_convert(&output_conv, str, NULL);
1544 else
1545 str = vim_strsave(str);
1546
1547 if (str != NULL)
1548 buttons = split_button_string(str, &n_buttons);
1549 }
1550 /*
1551 * Uh-oh... this should never ever happen. But we don't wanna crash
1552 * if the translation is broken, thus fall back to the untranslated
1553 * buttons string in case of emergency.
1554 */
1555 if (buttons == NULL || n_buttons != n_expected)
1556 {
1557 vim_free(buttons);
1558 vim_free(str);
1559 buttons = NULL;
1560 str = vim_strsave((char_u *)message);
1561
1562 if (str != NULL)
1563 buttons = split_button_string(str, &n_buttons);
1564 if (buttons == NULL)
1565 vim_free(str);
1566 }
1567
1568 return buttons;
1569}
1570
1571 static int
1572button_equal(const char *a, const char *b)
1573{
1574 while (*a != '\0' && *b != '\0')
1575 {
1576 if (*a == '_' && *++a == '\0')
1577 break;
1578 if (*b == '_' && *++b == '\0')
1579 break;
1580
1581 if (g_unichar_tolower(g_utf8_get_char(a))
1582 != g_unichar_tolower(g_utf8_get_char(b)))
1583 return FALSE;
1584
1585 a = g_utf8_next_char(a);
1586 b = g_utf8_next_char(b);
1587 }
1588
1589 return (*a == '\0' && *b == '\0');
1590}
1591
1592 static void
1593dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1594{
1595 char **ok;
1596 char **ync; /* "yes no cancel" */
1597 char **buttons;
1598 int n_buttons = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001599 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
1601 button_string = vim_strsave(button_string); /* must be writable */
1602 if (button_string == NULL)
1603 return;
1604
1605 /* Check 'v' flag in 'guioptions': vertical button placement. */
1606 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1607 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001608# if GTK_CHECK_VERSION(3,0,0)
1609 /* Add GTK+ 3 code if necessary. */
1610 /* N.B. GTK+ 3 doesn't allow you to access vbox and action_area via
1611 * the C API. */
1612# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 GtkWidget *vbutton_box;
1614
1615 vbutton_box = gtk_vbutton_box_new();
1616 gtk_widget_show(vbutton_box);
1617 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1618 vbutton_box, TRUE, FALSE, 0);
1619 /* Overrule the "action_area" value, hopefully this works... */
1620 GTK_DIALOG(dialog)->action_area = vbutton_box;
Bram Moolenaar98921892016-02-23 17:14:37 +01001621# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
1623
1624 /*
1625 * Yes this is ugly, I don't particularly like it either. But doing it
1626 * this way has the compelling advantage that translations need not to
1627 * be touched at all. See below what 'ok' and 'ync' are used for.
1628 */
1629 ok = split_button_translation(N_("&Ok"));
1630 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1631 buttons = split_button_string(button_string, &n_buttons);
1632
1633 /*
1634 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1635 * environment. Don't hit me -- it's all about consistency.
1636 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1637 * other way around...
1638 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 for (idx = 1; idx <= n_buttons; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 {
1641 char *label;
1642 char_u *label8;
1643
Bram Moolenaar89d40322006-08-29 15:30:07 +00001644 label = buttons[idx - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 /*
1646 * Perform some guesswork to find appropriate stock items for the
1647 * buttons. We have to compare with a sample of the translated
1648 * button string to get things right. Yes, this is hackish :/
1649 *
1650 * But even the common button labels aren't necessarily translated,
1651 * since anyone can create their own dialogs using Vim functions.
1652 * Thus we have to check for those too.
1653 */
1654 if (ok != NULL && ync != NULL) /* almost impossible to fail */
1655 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001656# if GTK_CHECK_VERSION(3,10,0)
1657 if (button_equal(label, ok[0])) label = _("OK");
1658 else if (button_equal(label, ync[0])) label = _("Yes");
1659 else if (button_equal(label, ync[1])) label = _("No");
1660 else if (button_equal(label, ync[2])) label = _("Cancel");
1661 else if (button_equal(label, "Ok")) label = _("OK");
1662 else if (button_equal(label, "Yes")) label = _("Yes");
1663 else if (button_equal(label, "No")) label = _("No");
1664 else if (button_equal(label, "Cancel")) label = _("Canccl");
1665# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
1667 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
1668 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
1669 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
1670 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
1671 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
1672 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
1673 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
Bram Moolenaar98921892016-02-23 17:14:37 +01001674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 label8 = CONVERT_TO_UTF8((char_u *)label);
Bram Moolenaar89d40322006-08-29 15:30:07 +00001677 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 CONVERT_TO_UTF8_FREE(label8);
1679 }
1680
1681 if (ok != NULL)
1682 vim_free(*ok);
1683 if (ync != NULL)
1684 vim_free(*ync);
1685 vim_free(ok);
1686 vim_free(ync);
1687 vim_free(buttons);
1688 vim_free(button_string);
1689}
1690
1691/*
1692 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1693 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
1694 * GUI used to work this way, and I consider the impact on UI consistency
1695 * low enough to justify implementing this as a special Vim feature.
1696 */
1697typedef struct _DialogInfo
1698{
1699 int ignore_enter; /* no default button, ignore "Enter" */
1700 int noalt; /* accept accelerators without Alt */
1701 GtkDialog *dialog; /* Widget of the dialog */
1702} DialogInfo;
1703
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 static gboolean
1705dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1706{
1707 DialogInfo *di = (DialogInfo *)data;
1708
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001709 /* Ignore hitting Enter (or Space) when there is no default button. */
1710 if (di->ignore_enter && (event->keyval == GDK_Return
1711 || event->keyval == ' '))
1712 return TRUE;
1713 else /* A different key was pressed, return to normal behavior */
1714 di->ignore_enter = FALSE;
1715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 /* Close the dialog when hitting "Esc". */
1717 if (event->keyval == GDK_Escape)
1718 {
1719 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1720 return TRUE;
1721 }
1722
1723 if (di->noalt
1724 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1725 {
1726 return gtk_window_mnemonic_activate(
1727 GTK_WINDOW(widget), event->keyval,
1728 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1729 }
1730
1731 return FALSE; /* continue emission */
1732}
1733
1734 int
1735gui_mch_dialog(int type, /* type of dialog */
1736 char_u *title, /* title of dialog */
1737 char_u *message, /* message text */
1738 char_u *buttons, /* names of buttons */
1739 int def_but, /* default button */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001740 char_u *textfield, /* text for textfield or NULL */
1741 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 GtkWidget *dialog;
1744 GtkWidget *entry = NULL;
1745 char_u *text;
1746 int response;
1747 DialogInfo dialoginfo;
1748
1749 dialog = create_message_dialog(type, title, message);
1750 dialoginfo.dialog = GTK_DIALOG(dialog);
1751 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1752
1753 if (textfield != NULL)
1754 {
1755 GtkWidget *alignment;
1756
1757 entry = gtk_entry_new();
1758 gtk_widget_show(entry);
1759
Bram Moolenaardf6b11e2010-11-24 18:48:12 +01001760 /* Make Enter work like pressing OK. */
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001761 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
Bram Moolenaardf6b11e2010-11-24 18:48:12 +01001762
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 text = CONVERT_TO_UTF8(textfield);
1764 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1765 CONVERT_TO_UTF8_FREE(text);
1766
Bram Moolenaar98921892016-02-23 17:14:37 +01001767# if GTK_CHECK_VERSION(3,14,0)
1768 gtk_widget_set_halign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1769 gtk_widget_set_valign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1770 gtk_widget_set_hexpand(GTK_WIDGET(entry), TRUE);
1771 gtk_widget_set_vexpand(GTK_WIDGET(entry), TRUE);
1772
1773 alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1774# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1776 (float)1.0, (float)1.0);
Bram Moolenaar98921892016-02-23 17:14:37 +01001777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 gtk_container_add(GTK_CONTAINER(alignment), entry);
1779 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1780 gtk_widget_show(alignment);
1781
Bram Moolenaar98921892016-02-23 17:14:37 +01001782# if GTK_CHECK_VERSION(3,0,0)
1783 {
1784 GtkWidget * const vbox
1785 = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1786 gtk_box_pack_start(GTK_BOX(vbox),
1787 alignment, TRUE, FALSE, 0);
1788 }
1789# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1791 alignment, TRUE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01001792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 dialoginfo.noalt = FALSE;
1794 }
1795 else
1796 dialoginfo.noalt = TRUE;
1797
1798 /* Allow activation of mnemonic accelerators without pressing <Alt> when
Bram Moolenaar14716812006-05-04 21:54:08 +00001799 * there is no textfield. Handle pressing Esc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 g_signal_connect(G_OBJECT(dialog), "key_press_event",
1801 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1802
1803 if (def_but > 0)
1804 {
1805 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1806 dialoginfo.ignore_enter = FALSE;
1807 }
1808 else
1809 /* No default button, ignore pressing Enter. */
1810 dialoginfo.ignore_enter = TRUE;
1811
1812 /* Show the mouse pointer if it's currently hidden. */
1813 gui_mch_mousehide(FALSE);
1814
1815 response = gtk_dialog_run(GTK_DIALOG(dialog));
1816
1817 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
1818 if (response != GTK_RESPONSE_NONE)
1819 {
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001820 if (response == GTK_RESPONSE_ACCEPT) /* Enter pressed */
1821 response = def_but;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 if (textfield != NULL)
1823 {
1824 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1825 text = CONVERT_FROM_UTF8(text);
1826
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001827 vim_strncpy(textfield, text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828
1829 CONVERT_FROM_UTF8_FREE(text);
1830 }
1831 gtk_widget_destroy(dialog);
1832 }
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 return response > 0 ? response : 0;
1835}
1836
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001837#endif /* FEAT_GUI_DIALOG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838
1839
1840#if defined(FEAT_MENU) || defined(PROTO)
1841
1842 void
1843gui_mch_show_popupmenu(vimmenu_T *menu)
1844{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001845# if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 /*
1847 * Append a submenu for selecting an input method. This is
1848 * currently the only way to switch input methods at runtime.
1849 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001850# if !GTK_CHECK_VERSION(3,10,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1852 "vim-has-im-menu") == NULL)
1853 {
1854 GtkWidget *menuitem;
1855 GtkWidget *submenu;
1856 char_u *name;
1857
1858 menuitem = gtk_separator_menu_item_new();
1859 gtk_widget_show(menuitem);
1860 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1861
1862 name = (char_u *)_("Input _Methods");
1863 name = CONVERT_TO_UTF8(name);
1864 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1865 CONVERT_TO_UTF8_FREE(name);
1866 gtk_widget_show(menuitem);
1867
1868 submenu = gtk_menu_new();
1869 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1870 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1871
1872 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1873 GTK_MENU_SHELL(submenu));
1874 g_object_set_data(G_OBJECT(menu->submenu_id),
1875 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1876 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001877# endif
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001878# endif /* FEAT_XIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
1880 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1881 NULL, NULL,
1882 (GtkMenuPositionFunc)NULL, NULL,
Bram Moolenaar20892c12011-06-26 04:49:00 +02001883 3U, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884}
1885
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001886/* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1887 * popup_menu_position_func(). */
1888static int popup_mouse_pos;
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
1891 * Menu position callback; used by gui_make_popup() to place the menu
1892 * at the current text cursor position.
1893 *
1894 * Note: The push_in output argument seems to affect scrolling of huge
1895 * menus that don't fit on the screen. Leave it at the default for now.
1896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001898popup_menu_position_func(GtkMenu *menu UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 gint *x, gint *y,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001900 gboolean *push_in UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001901 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902{
Bram Moolenaar98921892016-02-23 17:14:37 +01001903# if GTK_CHECK_VERSION(3,0,0)
1904 gdk_window_get_origin(gtk_widget_get_window(gui.drawarea), x, y);
1905# else
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001906 gdk_window_get_origin(gui.drawarea->window, x, y);
Bram Moolenaar98921892016-02-23 17:14:37 +01001907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001909 if (popup_mouse_pos)
1910 {
1911 int mx, my;
1912
1913 gui_mch_getmouse(&mx, &my);
1914 *x += mx;
1915 *y += my;
1916 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001917# if GTK_CHECK_VERSION(3,0,0)
1918 else if (curwin != NULL && gui.drawarea != NULL &&
1919 gtk_widget_get_window(gui.drawarea) != NULL)
1920# else
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001921 else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +01001922# endif
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001923 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 /* Find the cursor position in the current window */
1925 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
1926 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1927 }
1928}
1929
1930 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001931gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932{
1933 vimmenu_T *menu;
1934
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001935 popup_mouse_pos = mouse_pos;
1936
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 menu = gui_find_menu(path_name);
1938
1939 if (menu != NULL && menu->submenu_id != NULL)
1940 {
1941 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1942 NULL, NULL,
1943 &popup_menu_position_func, NULL,
1944 0U, (guint32)GDK_CURRENT_TIME);
1945 }
1946}
1947
1948#endif /* FEAT_MENU */
1949
1950
1951/*
1952 * We don't create it twice.
1953 */
1954
1955typedef struct _SharedFindReplace
1956{
1957 GtkWidget *dialog; /* the main dialog widget */
1958 GtkWidget *wword; /* 'Whole word only' check button */
1959 GtkWidget *mcase; /* 'Match case' check button */
1960 GtkWidget *up; /* search direction 'Up' radio button */
1961 GtkWidget *down; /* search direction 'Down' radio button */
1962 GtkWidget *what; /* 'Find what' entry text widget */
1963 GtkWidget *with; /* 'Replace with' entry text widget */
1964 GtkWidget *find; /* 'Find Next' action button */
1965 GtkWidget *replace; /* 'Replace With' action button */
1966 GtkWidget *all; /* 'Replace All' action button */
1967} SharedFindReplace;
1968
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001969static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1970static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 static int
1973find_key_press_event(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001974 GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 GdkEventKey *event,
1976 SharedFindReplace *frdp)
1977{
1978 /* If the user is holding one of the key modifiers we will just bail out,
1979 * thus preserving the possibility of normal focus traversal.
1980 */
1981 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
1982 return FALSE;
1983
1984 /* the Escape key synthesizes a cancellation action */
1985 if (event->keyval == GDK_Escape)
1986 {
1987 gtk_widget_hide(frdp->dialog);
1988
1989 return TRUE;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991
Bram Moolenaar79166c42007-05-10 18:29:51 +00001992 /* It would be delightful if it where possible to do search history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 * operations on the K_UP and K_DOWN keys here.
1994 */
1995
1996 return FALSE;
1997}
1998
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 static GtkWidget *
Bram Moolenaar98921892016-02-23 17:14:37 +01002000#if GTK_CHECK_VERSION(3,10,0)
2001create_image_button(const char *stock_id UNUSED,
2002 const char *label)
2003#else
2004create_image_button(const char *stock_id,
2005 const char *label)
2006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007{
2008 char_u *text;
2009 GtkWidget *box;
2010 GtkWidget *alignment;
2011 GtkWidget *button;
2012
2013 text = CONVERT_TO_UTF8((char_u *)label);
2014
Bram Moolenaar98921892016-02-23 17:14:37 +01002015#if GTK_CHECK_VERSION(3,2,0)
2016 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
2017 gtk_box_set_homogeneous(GTK_BOX(box), FALSE);
2018#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 box = gtk_hbox_new(FALSE, 3);
Bram Moolenaar98921892016-02-23 17:14:37 +01002020#endif
2021#if !GTK_CHECK_VERSION(3,10,0)
2022 if (stock_id != NULL)
2023 gtk_box_pack_start(GTK_BOX(box),
2024 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
2025 FALSE, FALSE, 0);
2026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 gtk_box_pack_start(GTK_BOX(box),
2028 gtk_label_new((const char *)text),
2029 FALSE, FALSE, 0);
2030
2031 CONVERT_TO_UTF8_FREE(text);
2032
Bram Moolenaar98921892016-02-23 17:14:37 +01002033#if GTK_CHECK_VERSION(3,14,0)
2034 gtk_widget_set_halign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2035 gtk_widget_set_valign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2036 gtk_widget_set_hexpand(GTK_WIDGET(box), TRUE);
2037 gtk_widget_set_vexpand(GTK_WIDGET(box), TRUE);
2038
2039 alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2040#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2042 (float)0.0, (float)0.0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 gtk_container_add(GTK_CONTAINER(alignment), box);
2045 gtk_widget_show_all(alignment);
2046
2047 button = gtk_button_new();
2048 gtk_container_add(GTK_CONTAINER(button), alignment);
2049
2050 return button;
2051}
2052
2053/*
2054 * This is currently only used by find_replace_dialog_create(), and
2055 * I'd really like to keep it at that. In other words: don't spread
2056 * this nasty hack all over the code. Think twice.
2057 */
2058 static const char *
2059convert_localized_message(char_u **buffer, const char *message)
2060{
2061 if (output_conv.vc_type == CONV_NONE)
2062 return message;
2063
2064 vim_free(*buffer);
2065 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
2066
2067 return (const char *)*buffer;
2068}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
2070 static void
2071find_replace_dialog_create(char_u *arg, int do_replace)
2072{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 GtkWidget *hbox; /* main top down box */
2074 GtkWidget *actionarea;
2075 GtkWidget *table;
2076 GtkWidget *tmp;
2077 GtkWidget *vbox;
2078 gboolean sensitive;
2079 SharedFindReplace *frdp;
2080 char_u *entry_text;
2081 int wword = FALSE;
2082 int mcase = !p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 char_u *conv_buffer = NULL;
2084# define CONV(message) convert_localized_message(&conv_buffer, (message))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085
2086 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2087
2088 /* Get the search string to use. */
2089 entry_text = get_find_dialog_text(arg, &wword, &mcase);
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
2092 {
2093 char_u *old_text = entry_text;
2094 entry_text = string_convert(&output_conv, entry_text, NULL);
2095 vim_free(old_text);
2096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098 /*
2099 * If the dialog already exists, just raise it.
2100 */
2101 if (frdp->dialog)
2102 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 if (entry_text != NULL)
2104 {
2105 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
Bram Moolenaar98921892016-02-23 17:14:37 +01002106#if GTK_CHECK_VERSION(3,0,0)
2107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2108 (gboolean)wword);
2109 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2110 (gboolean)mcase);
2111#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2113 (gboolean)wword);
2114 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2115 (gboolean)mcase);
Bram Moolenaar98921892016-02-23 17:14:37 +01002116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 gtk_window_present(GTK_WINDOW(frdp->dialog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 vim_free(entry_text);
2120 return;
2121 }
2122
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 frdp->dialog = gtk_dialog_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002124#if GTK_CHECK_VERSION(3,0,0)
2125 /* Nothing equivalent to gtk_dialog_set_has_separator() in GTK+ 3. */
2126#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
2130 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131
2132 if (do_replace)
2133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2135 CONV(_("VIM - Search and Replace...")));
2136 }
2137 else
2138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2140 CONV(_("VIM - Search...")));
2141 }
2142
Bram Moolenaar98921892016-02-23 17:14:37 +01002143#if GTK_CHECK_VERSION(3,2,0)
2144 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2145 gtk_box_set_homogeneous(GTK_BOX(hbox), FALSE);
2146#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 hbox = gtk_hbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
Bram Moolenaar98921892016-02-23 17:14:37 +01002150#if GTK_CHECK_VERSION(3,0,0)
2151 {
2152 GtkWidget * const dialog_vbox
2153 = gtk_dialog_get_content_area(GTK_DIALOG(frdp->dialog));
2154 gtk_container_add(GTK_CONTAINER(dialog_vbox), hbox);
2155 }
2156#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
Bram Moolenaar98921892016-02-23 17:14:37 +01002158#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159
2160 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002161#if GTK_CHECK_VERSION(3,4,0)
2162 table = gtk_grid_new();
2163#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 table = gtk_table_new(1024, 4, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002167#if GTK_CHECK_VERSION(3,4,0)
2168 table = gtk_grid_new();
2169#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 table = gtk_table_new(1024, 3, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002173#if GTK_CHECK_VERSION(3,0,0)
2174 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
2175#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 gtk_container_border_width(GTK_CONTAINER(table), 4);
Bram Moolenaar98921892016-02-23 17:14:37 +01002177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 tmp = gtk_label_new(CONV(_("Find what:")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002180#if GTK_CHECK_VERSION(3,16,0)
2181 gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2182 gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2183#elif GTK_CHECK_VERSION(3,14,0)
2184 {
2185 GValue align_val = G_VALUE_INIT;
2186
2187 g_value_init(&align_val, G_TYPE_FLOAT);
2188
2189 g_value_set_float(&align_val, 0.0);
2190 g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2191
2192 g_value_set_float(&align_val, 0.5);
2193 g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2194
2195 g_value_unset(&align_val);
2196 }
2197#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
Bram Moolenaar98921892016-02-23 17:14:37 +01002199#endif
2200#if GTK_CHECK_VERSION(3,4,0)
2201 gtk_grid_attach(GTK_GRID(table), tmp, 0, 0, 2, 1);
2202#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
2204 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 frdp->what = gtk_entry_new();
2207 sensitive = (entry_text != NULL && entry_text[0] != NUL);
2208 if (entry_text != NULL)
2209 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
Bram Moolenaar98921892016-02-23 17:14:37 +01002210#if GTK_CHECK_VERSION(3,0,0)
2211 g_signal_connect(G_OBJECT(frdp->what), "changed",
2212 G_CALLBACK(entry_changed_cb), frdp->dialog);
2213 g_signal_connect_after(G_OBJECT(frdp->what), "key-press-event",
2214 G_CALLBACK(find_key_press_event),
2215 (gpointer) frdp);
2216#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
2218 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
2219 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
2220 GTK_SIGNAL_FUNC(find_key_press_event),
2221 (gpointer) frdp);
Bram Moolenaar98921892016-02-23 17:14:37 +01002222#endif
2223#if GTK_CHECK_VERSION(3,4,0)
2224 gtk_grid_attach(GTK_GRID(table), frdp->what, 2, 0, 5, 1);
2225#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
2227 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 if (do_replace)
2231 {
2232 tmp = gtk_label_new(CONV(_("Replace with:")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002233#if GTK_CHECK_VERSION(3,16,0)
2234 gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2235 gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2236#elif GTK_CHECK_VERSION(3,14,0)
2237 {
2238 GValue align_val = G_VALUE_INIT;
2239
2240 g_value_init(&align_val, G_TYPE_FLOAT);
2241
2242 g_value_set_float(&align_val, 0.0);
2243 g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2244
2245 g_value_set_float(&align_val, 0.5);
2246 g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2247
2248 g_value_unset(&align_val);
2249 }
2250#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
Bram Moolenaar98921892016-02-23 17:14:37 +01002252#endif
2253#if GTK_CHECK_VERSION(3,4,0)
2254 gtk_grid_attach(GTK_GRID(table), tmp, 0, 1, 2, 1);
2255#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
2257 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 frdp->with = gtk_entry_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002260#if GTK_CHECK_VERSION(3,0,0)
2261 g_signal_connect(G_OBJECT(frdp->with), "activate",
2262 G_CALLBACK(find_replace_cb),
2263 GINT_TO_POINTER(FRD_R_FINDNEXT));
2264 g_signal_connect_after(G_OBJECT(frdp->with), "key-press-event",
2265 G_CALLBACK(find_key_press_event),
2266 (gpointer) frdp);
2267#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
2269 GTK_SIGNAL_FUNC(find_replace_cb),
2270 GINT_TO_POINTER(FRD_R_FINDNEXT));
2271 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
2272 GTK_SIGNAL_FUNC(find_key_press_event),
2273 (gpointer) frdp);
Bram Moolenaar98921892016-02-23 17:14:37 +01002274#endif
2275#if GTK_CHECK_VERSION(3,4,0)
2276 gtk_grid_attach(GTK_GRID(table), frdp->with, 2, 1, 5, 1);
2277#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
2279 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 /*
2283 * Make the entry activation only change the input focus onto the
2284 * with item.
2285 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002286#if GTK_CHECK_VERSION(3,0,0)
2287 g_signal_connect(G_OBJECT(frdp->what), "activate",
2288 G_CALLBACK(entry_activate_cb), frdp->with);
2289#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2291 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
Bram Moolenaar98921892016-02-23 17:14:37 +01002292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 else
2295 {
2296 /*
2297 * Make the entry activation do the search.
2298 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002299#if GTK_CHECK_VERSION(3,0,0)
2300 g_signal_connect(G_OBJECT(frdp->what), "activate",
2301 G_CALLBACK(find_replace_cb),
2302 GINT_TO_POINTER(FRD_FINDNEXT));
2303#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2305 GTK_SIGNAL_FUNC(find_replace_cb),
2306 GINT_TO_POINTER(FRD_FINDNEXT));
Bram Moolenaar98921892016-02-23 17:14:37 +01002307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309
2310 /* whole word only button */
2311 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002312#if GTK_CHECK_VERSION(3,0,0)
2313 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2314 (gboolean)wword);
2315#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2317 (gboolean)wword);
Bram Moolenaar98921892016-02-23 17:14:37 +01002318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002320#if GTK_CHECK_VERSION(3,4,0)
2321 gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 2, 5, 1);
2322#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
2324 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002327#if GTK_CHECK_VERSION(3,4,0)
2328 gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 3, 5, 1);
2329#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
2331 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
2334 /* match case button */
2335 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002336#if GTK_CHECK_VERSION(3,0,0)
2337 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2338 (gboolean)mcase);
2339#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2341 (gboolean)mcase);
Bram Moolenaar98921892016-02-23 17:14:37 +01002342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002344#if GTK_CHECK_VERSION(3,4,0)
2345 gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 3, 5, 1);
2346#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
2348 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002351#if GTK_CHECK_VERSION(3,4,0)
2352 gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 4, 5, 1);
2353#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
2355 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 tmp = gtk_frame_new(CONV(_("Direction")));
2359 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002360#if GTK_CHECK_VERSION(3,4,0)
2361 gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 2, 4);
2362#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
2364 GTK_FILL, GTK_FILL, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002367#if GTK_CHECK_VERSION(3,4,0)
2368 gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 1, 3);
2369#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
2371 GTK_FILL, GTK_FILL, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002372#endif
2373#if GTK_CHECK_VERSION(3,2,0)
2374 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2375 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
2376#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002378#endif
2379#if GTK_CHECK_VERSION(3,0,0)
2380 gtk_container_set_border_width(GTK_CONTAINER(vbox), 0);
2381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 gtk_container_add(GTK_CONTAINER(tmp), vbox);
2385
2386 /* 'Up' and 'Down' buttons */
2387 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
2388 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002389#if GTK_CHECK_VERSION(3,0,0)
2390 frdp->down = gtk_radio_button_new_with_label(
2391 gtk_radio_button_get_group(GTK_RADIO_BUTTON(frdp->up)),
2392 CONV(_("Down")));
2393#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 frdp->down = gtk_radio_button_new_with_label(
2395 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
2396 CONV(_("Down")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002397#endif
2398#if GTK_CHECK_VERSION(3,0,0)
2399 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
2400#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
2405
2406 /* vbox to hold the action buttons */
Bram Moolenaar98921892016-02-23 17:14:37 +01002407#if GTK_CHECK_VERSION(3,2,0)
2408 actionarea = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
2409#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 actionarea = gtk_vbutton_box_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002411#endif
2412#if GTK_CHECK_VERSION(3,0,0)
2413 gtk_container_set_border_width(GTK_CONTAINER(actionarea), 2);
2414#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
2418
2419 /* 'Find Next' button */
Bram Moolenaar98921892016-02-23 17:14:37 +01002420#if GTK_CHECK_VERSION(3,10,0)
2421 frdp->find = create_image_button(NULL, _("Find Next"));
2422#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
Bram Moolenaar98921892016-02-23 17:14:37 +01002424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 gtk_widget_set_sensitive(frdp->find, sensitive);
2426
Bram Moolenaar98921892016-02-23 17:14:37 +01002427#if GTK_CHECK_VERSION(3,0,0)
2428 g_signal_connect(G_OBJECT(frdp->find), "clicked",
2429 G_CALLBACK(find_replace_cb),
2430 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2431 : GINT_TO_POINTER(FRD_FINDNEXT));
2432#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
2434 GTK_SIGNAL_FUNC(find_replace_cb),
2435 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2436 : GINT_TO_POINTER(FRD_FINDNEXT));
Bram Moolenaar98921892016-02-23 17:14:37 +01002437#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438
Bram Moolenaar98921892016-02-23 17:14:37 +01002439#if GTK_CHECK_VERSION(3,0,0)
2440 gtk_widget_set_can_default(frdp->find, TRUE);
2441#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
Bram Moolenaar98921892016-02-23 17:14:37 +01002443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
2445 gtk_widget_grab_default(frdp->find);
2446
2447 if (do_replace)
2448 {
2449 /* 'Replace' button */
Bram Moolenaar98921892016-02-23 17:14:37 +01002450#if GTK_CHECK_VERSION(3,10,0)
2451 frdp->replace = create_image_button(NULL, _("Replace"));
2452#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
Bram Moolenaar98921892016-02-23 17:14:37 +01002454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 gtk_widget_set_sensitive(frdp->replace, sensitive);
Bram Moolenaar98921892016-02-23 17:14:37 +01002456#if GTK_CHECK_VERSION(3,0,0)
2457 gtk_widget_set_can_default(frdp->find, TRUE);
2458#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
Bram Moolenaar98921892016-02-23 17:14:37 +01002460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002462#if GTK_CHECK_VERSION(3,0,0)
2463 g_signal_connect(G_OBJECT(frdp->replace), "clicked",
2464 G_CALLBACK(find_replace_cb),
2465 GINT_TO_POINTER(FRD_REPLACE));
2466#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
2468 GTK_SIGNAL_FUNC(find_replace_cb),
2469 GINT_TO_POINTER(FRD_REPLACE));
Bram Moolenaar98921892016-02-23 17:14:37 +01002470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
2472 /* 'Replace All' button */
Bram Moolenaar98921892016-02-23 17:14:37 +01002473#if GTK_CHECK_VERSION(3,10,0)
2474 frdp->all = create_image_button(NULL, _("Replace All"));
2475#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
Bram Moolenaar98921892016-02-23 17:14:37 +01002477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 gtk_widget_set_sensitive(frdp->all, sensitive);
Bram Moolenaar98921892016-02-23 17:14:37 +01002479#if GTK_CHECK_VERSION(3,0,0)
2480 gtk_widget_set_can_default(frdp->all, TRUE);
2481#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
Bram Moolenaar98921892016-02-23 17:14:37 +01002483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002485#if GTK_CHECK_VERSION(3,0,0)
2486 g_signal_connect(G_OBJECT(frdp->all), "clicked",
2487 G_CALLBACK(find_replace_cb),
2488 GINT_TO_POINTER(FRD_REPLACEALL));
2489#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
2491 GTK_SIGNAL_FUNC(find_replace_cb),
2492 GINT_TO_POINTER(FRD_REPLACEALL));
Bram Moolenaar98921892016-02-23 17:14:37 +01002493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
2495
2496 /* 'Cancel' button */
Bram Moolenaar98921892016-02-23 17:14:37 +01002497#if GTK_CHECK_VERSION(3,10,0)
2498 tmp = gtk_button_new_with_mnemonic(_("_Close"));
2499#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002501#endif
2502#if GTK_CHECK_VERSION(3,0,0)
2503 gtk_widget_set_can_default(tmp, TRUE);
2504#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
Bram Moolenaar98921892016-02-23 17:14:37 +01002506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002508#if GTK_CHECK_VERSION(3,0,0)
2509 g_signal_connect_swapped(G_OBJECT(tmp),
2510 "clicked", G_CALLBACK(gtk_widget_hide),
2511 G_OBJECT(frdp->dialog));
2512 g_signal_connect_swapped(G_OBJECT(frdp->dialog),
2513 "delete-event", G_CALLBACK(gtk_widget_hide_on_delete),
2514 G_OBJECT(frdp->dialog));
2515#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 gtk_signal_connect_object(GTK_OBJECT(tmp),
2517 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
2518 GTK_OBJECT(frdp->dialog));
2519 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
2520 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
2521 GTK_OBJECT(frdp->dialog));
Bram Moolenaar98921892016-02-23 17:14:37 +01002522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar98921892016-02-23 17:14:37 +01002524#if GTK_CHECK_VERSION(3,2,0)
2525 tmp = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
2526#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 tmp = gtk_vseparator_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002528#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 /* Suppress automatic show of the unused action area */
Bram Moolenaar98921892016-02-23 17:14:37 +01002532#if GTK_CHECK_VERSION(3,0,0)
2533# if !GTK_CHECK_VERSION(3,12,0)
2534 gtk_widget_hide(gtk_dialog_get_action_area(GTK_DIALOG(frdp->dialog)));
2535# endif
2536#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
Bram Moolenaar98921892016-02-23 17:14:37 +01002538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 gtk_widget_show_all(hbox);
2540 gtk_widget_show(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 vim_free(entry_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 vim_free(conv_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544#undef CONV
2545}
2546
2547 void
2548gui_mch_find_dialog(exarg_T *eap)
2549{
2550 if (gui.in_use)
2551 find_replace_dialog_create(eap->arg, FALSE);
2552}
2553
2554 void
2555gui_mch_replace_dialog(exarg_T *eap)
2556{
2557 if (gui.in_use)
2558 find_replace_dialog_create(eap->arg, TRUE);
2559}
2560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561/*
2562 * Callback for actions of the find and replace dialogs
2563 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002565find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566{
2567 int flags;
2568 char_u *find_text;
2569 char_u *repl_text;
2570 gboolean direction_down;
2571 SharedFindReplace *sfr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 flags = (int)(long)data; /* avoid a lint warning here */
2574
2575 /* Get the search/replace strings from the dialog */
2576 if (flags == FRD_FINDNEXT)
2577 {
2578 repl_text = NULL;
2579 sfr = &find_widgets;
2580 }
2581 else
2582 {
2583 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
2584 sfr = &repl_widgets;
2585 }
2586
2587 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
Bram Moolenaar98921892016-02-23 17:14:37 +01002588#if GTK_CHECK_VERSION(3,0,0)
2589 direction_down = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->down));
2590#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
Bram Moolenaar98921892016-02-23 17:14:37 +01002592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaar98921892016-02-23 17:14:37 +01002594#if GTK_CHECK_VERSION(3,0,0)
2595 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->wword)))
2596#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
Bram Moolenaar98921892016-02-23 17:14:37 +01002598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 flags |= FRD_WHOLE_WORD;
Bram Moolenaar98921892016-02-23 17:14:37 +01002600#if GTK_CHECK_VERSION(3,0,0)
2601 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->mcase)))
2602#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
Bram Moolenaar98921892016-02-23 17:14:37 +01002604#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 flags |= FRD_MATCH_CASE;
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 repl_text = CONVERT_FROM_UTF8(repl_text);
2608 find_text = CONVERT_FROM_UTF8(find_text);
Bram Moolenaare980d8a2010-12-08 13:11:21 +01002609 gui_do_findrepl(flags, find_text, repl_text, direction_down);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 CONVERT_FROM_UTF8_FREE(repl_text);
2611 CONVERT_FROM_UTF8_FREE(find_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612}
2613
2614/* our usual callback function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002616entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
2618 gtk_widget_grab_focus(GTK_WIDGET(data));
2619}
2620
2621/*
2622 * Syncing the find/replace dialogs on the fly is utterly useless crack,
2623 * and causes nothing but problems. Please tell me a use case for which
2624 * you'd need both a find dialog and a find/replace one at the same time,
2625 * without being able to actually use them separately since they're syncing
2626 * all the time. I don't think it's worthwhile to fix this nonsense,
2627 * particularly evil incarnation of braindeadness, whatever; I'd much rather
2628 * see it extinguished from this planet. Thanks for listening. Sorry.
2629 */
2630 static void
2631entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
2632{
2633 const gchar *entry_text;
2634 gboolean nonempty;
2635
2636 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
2637
2638 if (!entry_text)
2639 return; /* shouldn't happen */
2640
2641 nonempty = (entry_text[0] != '\0');
2642
2643 if (dialog == find_widgets.dialog)
2644 {
2645 gtk_widget_set_sensitive(find_widgets.find, nonempty);
2646 }
2647
2648 if (dialog == repl_widgets.dialog)
2649 {
2650 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
2651 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
2652 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
2653 }
2654}
2655
2656/*
2657 * ":helpfind"
2658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002660ex_helpfind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 /* This will fail when menus are not loaded. Well, it's only for
2663 * backwards compatibility anyway. */
2664 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
2665}
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02002666
Bram Moolenaar08bc2742012-06-06 16:14:40 +02002667#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02002668 static void
2669recent_func_log_func(const gchar *log_domain UNUSED,
2670 GLogLevelFlags log_level UNUSED,
2671 const gchar *message UNUSED,
2672 gpointer user_data UNUSED)
2673{
2674 /* We just want to suppress the warnings. */
2675 /* http://bugzilla.gnome.org/show_bug.cgi?id=664587 */
2676}
Bram Moolenaar08bc2742012-06-06 16:14:40 +02002677#endif