blob: aaebf305617544db75762349dd88ccd9a93cc6fd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaar30613902019-12-01 22:11:18 +010040// GTK defines MAX and MIN, but some system header files as well. Undefine
41// them and don't use them.
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#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
Bram Moolenaar30613902019-12-01 22:11:18 +010050// Gnome redefines _() and N_(). Grrr...
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# 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)
Bram Moolenaar30613902019-12-01 22:11:18 +010067# define ENABLE_NLS // so the texts in the dialog boxes are translated
Bram Moolenaar071d4272004-06-13 20:20:40 +000068# 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>
Bram Moolenaar4f974752019-02-17 17:44:42 +010079# ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000080# include <gdk/gdkwin32.h>
81# else
82# include <gdk/gdkx.h>
83# endif
84
85# include <gtk/gtk.h>
86#else
Bram Moolenaar30613902019-12-01 22:11:18 +010087// define these items to be able to generate prototypes without GTK
Bram Moolenaar071d4272004-06-13 20:20:40 +000088typedef 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{
Bram Moolenaar30613902019-12-01 22:11:18 +0100121 /* 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
Bram Moolenaar98921892016-02-23 17:14:37 +0100132# if GTK_CHECK_VERSION(3,14,0)
Bram Moolenaar30613902019-12-01 22:11:18 +0100133 // Use the file names in gui_gtk_res.xml, cutting off the extension.
134 // Similar changes follow.
Bram Moolenaar98921892016-02-23 17:14:37 +0100135 /* 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
Bram Moolenaar30613902019-12-01 22:11:18 +0100145 /* 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
Bram Moolenaar98921892016-02-23 17:14:37 +0100148# 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
Bram Moolenaar30613902019-12-01 22:11:18 +0100159 /* 22 */ "go-previous", // sub. GTK_STOCK_GO_BACK
160 /* 23 */ "go-next", // sub. GTK_STOCK_GO_FORWARD
Bram Moolenaar98921892016-02-23 17:14:37 +0100161# if GTK_CHECK_VERSION(3,14,0)
162 /* 24 */ "stock_vim_find_help",
163# else
164 /* 24 */ "vim-find-help",
165# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100166 /* 25 */ "gtk-convert", // sub. GTK_STOCK_CONVERT
167 /* 26 */ "go-jump", // sub. GTK_STOCK_JUMP_TO
Bram Moolenaar98921892016-02-23 17:14:37 +0100168# 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
Bram Moolenaar30613902019-12-01 22:11:18 +0100179 /* 31 */ "application-exit", // GTK_STOCK_QUIT
Bram Moolenaar98921892016-02-23 17:14:37 +0100180};
Bram Moolenaar30613902019-12-01 22:11:18 +0100181# 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,
Bram Moolenaar30613902019-12-01 22:11:18 +0100201 /* 17 */ GTK_STOCK_CLOSE, // FIXME: fuzzy
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 /* 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 Moolenaar30613902019-12-01 22:11:18 +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
Bram Moolenaar30613902019-12-01 22:11:18 +0100243# 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:
Bram Moolenaar30613902019-12-01 22:11:18 +0100319 // FALLTHROUGH
Bram Moolenaar98921892016-02-23 17:14:37 +0100320 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;
Bram Moolenaar30613902019-12-01 22:11:18 +0100340# 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 Moolenaar30613902019-12-01 22:11:18 +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
Bram Moolenaar30613902019-12-01 22:11:18 +0100370 // First use a specified "icon=" argument.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371 if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
372 image = load_menu_iconfile(buf, icon_size);
373
Bram Moolenaar30613902019-12-01 22:11:18 +0100374 // If not found and not builtin specified try using the menu name.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375 if (image == NULL && !menu->icon_builtin
376 && lookup_menu_iconfile(menu->name, buf))
377 image = load_menu_iconfile(buf, icon_size);
378
Bram Moolenaar30613902019-12-01 22:11:18 +0100379 // Still not found? Then use a builtin icon, a blank one as fallback.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 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 Moolenaar30613902019-12-01 22:11:18 +0100413 // 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 Moolenaar30613902019-12-01 22:11:18 +0100421#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);
Bram Moolenaar30613902019-12-01 22:11:18 +0100453# 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 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100481 // 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.
Bram Moolenaar98921892016-02-23 17:14:37 +0100493 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 Moolenaar30613902019-12-01 22:11:18 +0100506# else // !GTK_CHECK_VERSION(3,0.0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100507 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 Moolenaar30613902019-12-01 22:11:18 +0100528# endif // !GTK_CHECK_VERSION(3,0,0)
529# endif // defined(USE_GRESOURCE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
Bram Moolenaar30613902019-12-01 22:11:18 +0100532#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
Bram Moolenaar964b3742019-05-24 18:54:09 +0200562 buf = alloc(psrc - name + n_underscores + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 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
Bram Moolenaar30613902019-12-01 22:11:18 +0100600 // 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 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
Bram Moolenaar21b34b62017-06-13 14:34:01 +0200655# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar30613902019-12-01 22:11:18 +0100656 // since the tearoff should always appear first, increment idx
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (parent != NULL && !menu_is_popup(parent->name))
658 ++idx;
Bram Moolenaar21b34b62017-06-13 14:34:01 +0200659# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
662
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 menu->submenu_id = gtk_menu_new();
664
665 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
666 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
667
Bram Moolenaar98921892016-02-23 17:14:37 +0100668# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 menu->tearoff_handle = gtk_tearoff_menu_item_new();
670 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
671 gtk_widget_show(menu->tearoff_handle);
Bram Moolenaar98921892016-02-23 17:14:37 +0100672# if GTK_CHECK_VERSION(3,0,0)
673 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu->submenu_id),
674 menu->tearoff_handle);
675# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
Bram Moolenaar98921892016-02-23 17:14:37 +0100677# endif
678# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679}
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000682menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 gui_menu_cb((vimmenu_T *)data);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685}
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 void
688gui_mch_add_menu_item(vimmenu_T *menu, int idx)
689{
690 vimmenu_T *parent;
691
692 parent = menu->parent;
693
694# ifdef FEAT_TOOLBAR
695 if (menu_is_toolbar(parent->name))
696 {
697 GtkToolbar *toolbar;
698
699 toolbar = GTK_TOOLBAR(gui.toolbar);
700 menu->submenu_id = NULL;
701
702 if (menu_is_separator(menu->name))
703 {
Bram Moolenaar98921892016-02-23 17:14:37 +0100704# if GTK_CHECK_VERSION(3,0,0)
705 GtkToolItem *item = gtk_separator_tool_item_new();
706 gtk_separator_tool_item_set_draw(GTK_SEPARATOR_TOOL_ITEM(item),
707 TRUE);
708 gtk_tool_item_set_expand(GTK_TOOL_ITEM(item), FALSE);
709 gtk_widget_show(GTK_WIDGET(item));
710
711 gtk_toolbar_insert(toolbar, item, idx);
712# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 gtk_toolbar_insert_space(toolbar, idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 menu->id = NULL;
716 }
717 else
718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 char_u *text;
720 char_u *tooltip;
721
722 text = CONVERT_TO_UTF8(menu->dname);
723 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000724 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
Bram Moolenaar30613902019-12-01 22:11:18 +0100725 // Invalid text, can happen when 'encoding' is changed. Avoid
726 // a nasty GTK error message, skip the tooltip.
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000727 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaar98921892016-02-23 17:14:37 +0100729# if GTK_CHECK_VERSION(3,0,0)
730 {
731 GtkWidget *icon;
732 GtkToolItem *item;
733
734 icon = create_menu_icon(menu,
735 gtk_toolbar_get_icon_size(toolbar));
736 item = gtk_tool_button_new(icon, (const gchar *)text);
737 gtk_tool_item_set_tooltip_text(item, (const gchar *)tooltip);
738 g_signal_connect(G_OBJECT(item), "clicked",
739 G_CALLBACK(&menu_item_activate), menu);
740 gtk_widget_show_all(GTK_WIDGET(item));
741
742 gtk_toolbar_insert(toolbar, item, idx);
743
744 menu->id = GTK_WIDGET(item);
745 }
746# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 menu->id = gtk_toolbar_insert_item(
748 toolbar,
749 (const char *)text,
750 (const char *)tooltip,
751 NULL,
752 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
753 G_CALLBACK(&menu_item_activate),
754 menu,
755 idx);
Bram Moolenaar98921892016-02-23 17:14:37 +0100756# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000758 if (gtk_socket_id != 0)
Bram Moolenaar98921892016-02-23 17:14:37 +0100759 g_signal_connect(G_OBJECT(menu->id), "focus-in-event",
760 G_CALLBACK(toolbar_button_focus_in_event), NULL);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 CONVERT_TO_UTF8_FREE(text);
763 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765 }
766 else
Bram Moolenaar30613902019-12-01 22:11:18 +0100767# endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100769 // No parent, must be a non-menubar menu
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000770 if (parent == NULL || parent->submenu_id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 return;
772
Bram Moolenaar21b34b62017-06-13 14:34:01 +0200773# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar30613902019-12-01 22:11:18 +0100774 // Make place for the possible tearoff handle item. Not in the popup
775 // menu, it doesn't have a tearoff item.
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000776 if (!menu_is_popup(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 ++idx;
Bram Moolenaar21b34b62017-06-13 14:34:01 +0200778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 if (menu_is_separator(menu->name))
781 {
Bram Moolenaar30613902019-12-01 22:11:18 +0100782 // Separator: Just add it
Bram Moolenaar0b6cf692016-04-30 13:26:14 +0200783# if GTK_CHECK_VERSION(3,0,0)
784 menu->id = gtk_separator_menu_item_new();
785# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 menu->id = gtk_menu_item_new();
787 gtk_widget_set_sensitive(menu->id, FALSE);
Bram Moolenaar0b6cf692016-04-30 13:26:14 +0200788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 gtk_widget_show(menu->id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100790 gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
791 menu->id, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
793 return;
794 }
795
Bram Moolenaar30613902019-12-01 22:11:18 +0100796 // Add textual menu item.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 menu_item_new(menu, parent->submenu_id);
798 gtk_widget_show(menu->id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100799 gtk_menu_shell_insert(GTK_MENU_SHELL(parent->submenu_id),
800 menu->id, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 if (menu->id != NULL)
Bram Moolenaar98921892016-02-23 17:14:37 +0100803 g_signal_connect(G_OBJECT(menu->id), "activate",
804 G_CALLBACK(menu_item_activate), menu);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
806}
Bram Moolenaar30613902019-12-01 22:11:18 +0100807#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809
810 void
811gui_mch_set_text_area_pos(int x, int y, int w, int h)
812{
813 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
814}
815
816
817#if defined(FEAT_MENU) || defined(PROTO)
818/*
Bram Moolenaar79166c42007-05-10 18:29:51 +0000819 * Enable or disable accelerators for the toplevel menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 */
821 void
822gui_gtk_set_mnemonics(int enable)
823{
824 vimmenu_T *menu;
825 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
Bram Moolenaar00d253e2020-04-06 22:13:01 +0200827 FOR_ALL_MENUS(menu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 {
829 if (menu->id == NULL)
830 continue;
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 name = translate_mnemonic_tag(menu->name, enable);
833 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
834 (const char *)name);
835 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837}
838
Bram Moolenaar98921892016-02-23 17:14:37 +0100839# if !GTK_CHECK_VERSION(3,4,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 static void
841recurse_tearoffs(vimmenu_T *menu, int val)
842{
843 for (; menu != NULL; menu = menu->next)
844 {
845 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
846 && menu->name[0] != ']' && !menu_is_popup(menu->name))
847 {
848 if (val)
849 gtk_widget_show(menu->tearoff_handle);
850 else
851 gtk_widget_hide(menu->tearoff_handle);
852 }
853 recurse_tearoffs(menu->children, val);
854 }
855}
Bram Moolenaar98921892016-02-23 17:14:37 +0100856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaar98921892016-02-23 17:14:37 +0100858# if GTK_CHECK_VERSION(3,4,0)
859 void
860gui_mch_toggle_tearoffs(int enable UNUSED)
861{
Bram Moolenaar30613902019-12-01 22:11:18 +0100862 // Do nothing
Bram Moolenaar98921892016-02-23 17:14:37 +0100863}
864# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 void
866gui_mch_toggle_tearoffs(int enable)
867{
868 recurse_tearoffs(root_menu, enable);
869}
Bram Moolenaar98921892016-02-23 17:14:37 +0100870# endif
Bram Moolenaar30613902019-12-01 22:11:18 +0100871#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200873#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 static int
875get_menu_position(vimmenu_T *menu)
876{
877 vimmenu_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000878 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879
880 for (node = menu->parent->children; node != menu; node = node->next)
881 {
882 g_return_val_if_fail(node != NULL, -1);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000883 ++idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 }
885
Bram Moolenaar89d40322006-08-29 15:30:07 +0000886 return idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887}
Bram Moolenaar30613902019-12-01 22:11:18 +0100888#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
890
891#if defined(FEAT_TOOLBAR) || defined(PROTO)
892 void
893gui_mch_menu_set_tip(vimmenu_T *menu)
894{
895 if (menu->id != NULL && menu->parent != NULL
896 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
897 {
898 char_u *tooltip;
899
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar98921892016-02-23 17:14:37 +0100901 if (tooltip != NULL && utf_valid_string(tooltip, NULL))
902# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +0100903 // Only set the tooltip when it's valid utf-8.
Bram Moolenaar98921892016-02-23 17:14:37 +0100904 gtk_widget_set_tooltip_text(menu->id, (const gchar *)tooltip);
905# else
Bram Moolenaar30613902019-12-01 22:11:18 +0100906 // Only set the tooltip when it's valid utf-8.
Bram Moolenaar98921892016-02-23 17:14:37 +0100907 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
908 menu->id, (const char *)tooltip, NULL);
909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 }
912}
Bram Moolenaar30613902019-12-01 22:11:18 +0100913#endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
915
916#if defined(FEAT_MENU) || defined(PROTO)
917/*
918 * Destroy the machine specific menu widget.
919 */
920 void
921gui_mch_destroy_menu(vimmenu_T *menu)
922{
Bram Moolenaar30613902019-12-01 22:11:18 +0100923 // Don't let gtk_container_remove automatically destroy menu->id.
Bram Moolenaarf9da6802013-07-03 13:04:27 +0200924 if (menu->id != NULL)
925 g_object_ref(menu->id);
926
Bram Moolenaar30613902019-12-01 22:11:18 +0100927 // Workaround for a spurious gtk warning in Ubuntu: "Trying to remove
928 // a child that doesn't believe we're its parent."
929 // Remove widget from gui.menubar before destroying it.
Bram Moolenaarf9da6802013-07-03 13:04:27 +0200930 if (menu->id != NULL && gui.menubar != NULL
931 && gtk_widget_get_parent(menu->id) == gui.menubar)
932 gtk_container_remove(GTK_CONTAINER(gui.menubar), menu->id);
933
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934# ifdef FEAT_TOOLBAR
935 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
936 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (menu_is_separator(menu->name))
Bram Moolenaar98921892016-02-23 17:14:37 +0100938# if GTK_CHECK_VERSION(3,0,0)
939 {
940 GtkToolItem *item = NULL;
941
942 item = gtk_toolbar_get_nth_item(GTK_TOOLBAR(gui.toolbar),
943 get_menu_position(menu));
944 if (item != NULL)
945 gtk_container_remove(GTK_CONTAINER(gui.toolbar),
946 GTK_WIDGET(item));
947 }
948# else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
950 get_menu_position(menu));
Bram Moolenaar98921892016-02-23 17:14:37 +0100951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 else if (menu->id != NULL)
953 gtk_widget_destroy(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 }
955 else
Bram Moolenaar30613902019-12-01 22:11:18 +0100956# endif // FEAT_TOOLBAR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 {
958 if (menu->submenu_id != NULL)
959 gtk_widget_destroy(menu->submenu_id);
960
961 if (menu->id != NULL)
962 gtk_widget_destroy(menu->id);
963 }
964
Bram Moolenaarf9da6802013-07-03 13:04:27 +0200965 if (menu->id != NULL)
966 g_object_unref(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 menu->submenu_id = NULL;
968 menu->id = NULL;
969}
Bram Moolenaar30613902019-12-01 22:11:18 +0100970#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972
973/*
974 * Scrollbar stuff.
975 */
976 void
977gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
978{
979 if (sb->id != NULL)
980 {
981 GtkAdjustment *adjustment;
982
983 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
984
Bram Moolenaar98921892016-02-23 17:14:37 +0100985 gtk_adjustment_set_lower(adjustment, 0.0);
986 gtk_adjustment_set_value(adjustment, val);
987 gtk_adjustment_set_upper(adjustment, max + 1);
988 gtk_adjustment_set_page_size(adjustment, size);
989 gtk_adjustment_set_page_increment(adjustment,
990 size < 3L ? 1L : size - 2L);
991 gtk_adjustment_set_step_increment(adjustment, 1.0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992
Bram Moolenaar664323e2018-09-18 22:30:07 +0200993 g_signal_handler_block(G_OBJECT(adjustment), (gulong)sb->handler_id);
Bram Moolenaar98921892016-02-23 17:14:37 +0100994
995#if !GTK_CHECK_VERSION(3,18,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 gtk_adjustment_changed(adjustment);
Bram Moolenaar98921892016-02-23 17:14:37 +0100997#endif
998
Bram Moolenaar98921892016-02-23 17:14:37 +0100999 g_signal_handler_unblock(G_OBJECT(adjustment),
1000 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 }
1002}
1003
1004 void
1005gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1006{
1007 if (sb->id != NULL)
1008 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
1009}
1010
Bram Moolenaar203ec772020-07-17 20:43:43 +02001011 int
1012gui_mch_get_scrollbar_xpadding(void)
1013{
1014 // TODO: Calculate the padding for adjust scrollbar position when the
1015 // Window is maximized.
1016 return 0;
1017}
1018
1019 int
1020gui_mch_get_scrollbar_ypadding(void)
1021{
1022 // TODO: Calculate the padding for adjust scrollbar position when the
1023 // Window is maximized.
1024 return 0;
1025}
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027/*
1028 * Take action upon scrollbar dragging.
1029 */
1030 static void
1031adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
1032{
1033 scrollbar_T *sb;
1034 long value;
1035 int dragging = FALSE;
1036
1037#ifdef FEAT_XIM
Bram Moolenaar30613902019-12-01 22:11:18 +01001038 // cancel any preediting
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (im_is_preediting())
1040 xim_reset();
1041#endif
1042
1043 sb = gui_find_scrollbar((long)data);
Bram Moolenaar98921892016-02-23 17:14:37 +01001044 value = gtk_adjustment_get_value(adjustment);
Bram Moolenaar98921892016-02-23 17:14:37 +01001045#if !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 /*
1047 * The dragging argument must be right for the scrollbar to work with
1048 * closed folds. This isn't documented, hopefully this will keep on
1049 * working in later GTK versions.
1050 *
1051 * FIXME: Well, it doesn't work in GTK2. :)
1052 * HACK: Get the mouse pointer position, if it appears to be on an arrow
1053 * button set "dragging" to FALSE. This assumes square buttons!
1054 */
1055 if (sb != NULL)
1056 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 dragging = TRUE;
1058
1059 if (sb->wp != NULL)
1060 {
1061 int x;
1062 int y;
1063 GdkModifierType state;
1064 int width;
1065 int height;
1066
Bram Moolenaar30613902019-12-01 22:11:18 +01001067 // vertical scrollbar: need to set "dragging" properly in case
1068 // there are closed folds.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
1070 gdk_window_get_size(sb->id->window, &width, &height);
1071 if (x >= 0 && x < width && y >= 0 && y < height)
1072 {
1073 if (y < width)
1074 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001075 // up arrow: move one (closed fold) line up
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 dragging = FALSE;
1077 value = sb->wp->w_topline - 2;
1078 }
1079 else if (y > height - width)
1080 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001081 // down arrow: move one (closed fold) line down
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 dragging = FALSE;
1083 value = sb->wp->w_topline;
1084 }
1085 }
1086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001088#endif // !GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
Bram Moolenaar30613902019-12-01 22:11:18 +01001092// SBAR_VERT or SBAR_HORIZ
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 void
1094gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1095{
1096 if (orient == SBAR_HORIZ)
Bram Moolenaar98921892016-02-23 17:14:37 +01001097#if GTK_CHECK_VERSION(3,2,0)
1098 sb->id = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, NULL);
1099#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 sb->id = gtk_hscrollbar_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 else if (orient == SBAR_VERT)
Bram Moolenaar98921892016-02-23 17:14:37 +01001103#if GTK_CHECK_VERSION(3,2,0)
1104 sb->id = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, NULL);
1105#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 sb->id = gtk_vscrollbar_new(NULL);
Bram Moolenaar98921892016-02-23 17:14:37 +01001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 if (sb->id != NULL)
1110 {
1111 GtkAdjustment *adjustment;
1112
Bram Moolenaar98921892016-02-23 17:14:37 +01001113 gtk_widget_set_can_focus(sb->id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
1115
1116 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1117
Bram Moolenaar98921892016-02-23 17:14:37 +01001118 sb->handler_id = g_signal_connect(
1119 G_OBJECT(adjustment), "value-changed",
1120 G_CALLBACK(adjustment_value_changed),
1121 GINT_TO_POINTER(sb->ident));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 gui_mch_update();
1123 }
1124}
1125
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 void
1127gui_mch_destroy_scrollbar(scrollbar_T *sb)
1128{
1129 if (sb->id != NULL)
1130 {
1131 gtk_widget_destroy(sb->id);
1132 sb->id = NULL;
1133 }
1134 gui_mch_update();
1135}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137#if defined(FEAT_BROWSE) || defined(PROTO)
1138/*
1139 * Implementation of the file selector related stuff
1140 */
1141
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001142#ifndef USE_FILE_CHOOSER
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001144browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145{
1146 gui_T *vw = (gui_T *)cbdata;
1147
1148 if (vw->browse_fname != NULL)
1149 g_free(vw->browse_fname);
1150
1151 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
1152 GTK_FILE_SELECTION(vw->filedlg)));
1153 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154}
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001157browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158{
1159 gui_T *vw = (gui_T *)cbdata;
1160
1161 if (vw->browse_fname != NULL)
1162 {
1163 g_free(vw->browse_fname);
1164 vw->browse_fname = NULL;
1165 }
1166 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167}
1168
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001170browse_destroy_cb(GtkWidget *widget UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171{
1172 if (gui.browse_fname != NULL)
1173 {
1174 g_free(gui.browse_fname);
1175 gui.browse_fname = NULL;
1176 }
1177 gui.filedlg = NULL;
Bram Moolenaara3f41662010-07-11 19:01:06 +02001178 gtk_main_quit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return FALSE;
1180}
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182
1183/*
1184 * Put up a file requester.
1185 * Returns the selected name in allocated memory, or NULL for Cancel.
1186 * saving, select file to write
1187 * title title for the window
1188 * dflt default name
1189 * ext not used (extension added)
1190 * initdir initial directory, NULL for current dir
1191 * filter not used (file name filter)
1192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001194gui_mch_browse(int saving UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 char_u *title,
1196 char_u *dflt,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001197 char_u *ext UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 char_u *initdir,
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001199 char_u *filter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200{
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001201#ifdef USE_FILE_CHOOSER
1202 GtkWidget *fc;
1203#endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001204 char_u dirbuf[MAXPATHL];
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001205 guint log_handler;
1206 const gchar *domain = "Gtk";
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 title = CONVERT_TO_UTF8(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209
Bram Moolenaar30613902019-12-01 22:11:18 +01001210 // GTK has a bug, it only works with an absolute path.
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001211 if (initdir == NULL || *initdir == NUL)
1212 mch_dirname(dirbuf, MAXPATHL);
Bram Moolenaar57ac3a22006-10-10 16:28:30 +00001213 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001214 dirbuf[0] = NUL;
Bram Moolenaar30613902019-12-01 22:11:18 +01001215 // Always need a trailing slash for a directory.
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001216 add_pathsep(dirbuf);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001217
Bram Moolenaar30613902019-12-01 22:11:18 +01001218 // If our pointer is currently hidden, then we should show it.
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001219 gui_mch_mousehide(FALSE);
1220
Bram Moolenaar30613902019-12-01 22:11:18 +01001221 // Hack: The GTK file dialog warns when it can't access a new file, this
1222 // makes it shut up. http://bugzilla.gnome.org/show_bug.cgi?id=664587
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001223 log_handler = g_log_set_handler(domain, G_LOG_LEVEL_WARNING,
1224 recent_func_log_func, NULL);
1225
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001226#ifdef USE_FILE_CHOOSER
Bram Moolenaar30613902019-12-01 22:11:18 +01001227 // We create the dialog each time, so that the button text can be "Open"
1228 // or "Save" according to the action.
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001229 fc = gtk_file_chooser_dialog_new((const gchar *)title,
1230 GTK_WINDOW(gui.mainwin),
1231 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
1232 : GTK_FILE_CHOOSER_ACTION_OPEN,
Bram Moolenaar98921892016-02-23 17:14:37 +01001233# if GTK_CHECK_VERSION(3,10,0)
1234 _("_Cancel"), GTK_RESPONSE_CANCEL,
1235 saving ? _("_Save") : _("_Open"), GTK_RESPONSE_ACCEPT,
1236# else
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001237 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
Bram Moolenaard1350622006-10-24 20:01:06 +00001238 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
Bram Moolenaar98921892016-02-23 17:14:37 +01001239# endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001240 NULL);
1241 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
1242 (const gchar *)dirbuf);
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001243
1244 if (filter != NULL && *filter != NUL)
1245 {
1246 int i = 0;
1247 char_u *patt;
1248 char_u *p = filter;
Bram Moolenaar205f9f52012-10-18 05:18:32 +02001249 GtkFileFilter *gfilter;
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001250
1251 gfilter = gtk_file_filter_new();
1252 patt = alloc(STRLEN(filter));
1253 while (p != NULL && *p != NUL)
1254 {
1255 if (*p == '\n' || *p == ';' || *p == '\t')
1256 {
1257 STRNCPY(patt, filter, i);
1258 patt[i] = '\0';
1259 if (*p == '\t')
1260 gtk_file_filter_set_name(gfilter, (gchar *)patt);
1261 else
1262 {
1263 gtk_file_filter_add_pattern(gfilter, (gchar *)patt);
1264 if (*p == '\n')
1265 {
1266 gtk_file_chooser_add_filter((GtkFileChooser *)fc,
1267 gfilter);
1268 if (*(p + 1) != NUL)
1269 gfilter = gtk_file_filter_new();
1270 }
1271 }
1272 filter = ++p;
1273 i = 0;
1274 }
1275 else
1276 {
1277 p++;
1278 i++;
1279 }
1280 }
1281 vim_free(patt);
1282 }
Bram Moolenaara3f41662010-07-11 19:01:06 +02001283 if (saving && dflt != NULL && *dflt != NUL)
1284 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001285
1286 gui.browse_fname = NULL;
1287 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001289 char *filename;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001290
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00001291 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
1292 gui.browse_fname = (char_u *)g_strdup(filename);
1293 g_free(filename);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001294 }
1295 gtk_widget_destroy(GTK_WIDGET(fc));
1296
Bram Moolenaar30613902019-12-01 22:11:18 +01001297#else // !USE_FILE_CHOOSER
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001298
1299 if (gui.filedlg == NULL)
1300 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001301 GtkFileSelection *fs; // shortcut
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001302
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 gui.filedlg = gtk_file_selection_new((const gchar *)title);
1304 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
1305 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001306 GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 fs = GTK_FILE_SELECTION(gui.filedlg);
1308
1309 gtk_container_border_width(GTK_CONTAINER(fs), 4);
1310
1311 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
1312 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
1313 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
1314 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
Bram Moolenaar30613902019-12-01 22:11:18 +01001315 // gtk_signal_connect() doesn't work for destroy, it causes a hang
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
1317 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
1318 GTK_OBJECT(gui.filedlg));
1319 }
1320 else
1321 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
1322
Bram Moolenaar30613902019-12-01 22:11:18 +01001323 // Concatenate "initdir" and "dflt".
Bram Moolenaar57ac3a22006-10-10 16:28:30 +00001324 if (dflt != NULL && *dflt != NUL
1325 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
1326 STRCAT(dirbuf, dflt);
1327
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
1329 (const gchar *)dirbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 gtk_widget_show(gui.filedlg);
Bram Moolenaara3f41662010-07-11 19:01:06 +02001332 gtk_main();
Bram Moolenaar30613902019-12-01 22:11:18 +01001333#endif // !USE_FILE_CHOOSER
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001334 g_log_remove_handler(domain, log_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001336 CONVERT_TO_UTF8_FREE(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 if (gui.browse_fname == NULL)
1338 return NULL;
1339
Bram Moolenaar30613902019-12-01 22:11:18 +01001340 // shorten the file name if possible
Bram Moolenaard089d9b2007-09-30 12:02:55 +00001341 return vim_strsave(shorten_fname1(gui.browse_fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342}
1343
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001344/*
1345 * Put up a directory selector
1346 * Returns the selected name in allocated memory, or NULL for Cancel.
1347 * title title for the window
1348 * dflt default name
1349 * initdir initial directory, NULL for current dir
1350 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001351 char_u *
1352gui_mch_browsedir(
1353 char_u *title,
1354 char_u *initdir)
1355{
Bram Moolenaar30613902019-12-01 22:11:18 +01001356# if defined(GTK_FILE_CHOOSER) // Only in GTK 2.4 and later.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001357 char_u dirbuf[MAXPATHL];
1358 char_u *p;
Bram Moolenaar30613902019-12-01 22:11:18 +01001359 GtkWidget *dirdlg; // file selection dialog
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001360 char_u *dirname = NULL;
1361
1362 title = CONVERT_TO_UTF8(title);
1363
1364 dirdlg = gtk_file_chooser_dialog_new(
1365 (const gchar *)title,
1366 GTK_WINDOW(gui.mainwin),
1367 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
Bram Moolenaar98921892016-02-23 17:14:37 +01001368# if GTK_CHECK_VERSION(3,10,0)
1369 _("_Cancel"), GTK_RESPONSE_CANCEL,
1370 _("_OK"), GTK_RESPONSE_ACCEPT,
1371# else
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001372 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1373 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
Bram Moolenaar98921892016-02-23 17:14:37 +01001374# endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001375 NULL);
1376
1377 CONVERT_TO_UTF8_FREE(title);
1378
Bram Moolenaar30613902019-12-01 22:11:18 +01001379 // if our pointer is currently hidden, then we should show it.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001380 gui_mch_mousehide(FALSE);
1381
Bram Moolenaar30613902019-12-01 22:11:18 +01001382 // GTK appears to insist on an absolute path.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001383 if (initdir == NULL || *initdir == NUL
1384 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
1385 mch_dirname(dirbuf, MAXPATHL - 10);
1386
Bram Moolenaar30613902019-12-01 22:11:18 +01001387 // Always need a trailing slash for a directory.
1388 // Also add a dummy file name, so that we get to the directory.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001389 add_pathsep(dirbuf);
1390 STRCAT(dirbuf, "@zd(*&1|");
1391 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
1392 (const gchar *)dirbuf);
1393
Bram Moolenaar30613902019-12-01 22:11:18 +01001394 // Run the dialog.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001395 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
1396 dirname = (char_u *)gtk_file_chooser_get_filename(
1397 GTK_FILE_CHOOSER(dirdlg));
1398 gtk_widget_destroy(dirdlg);
1399 if (dirname == NULL)
1400 return NULL;
1401
Bram Moolenaar30613902019-12-01 22:11:18 +01001402 // shorten the file name if possible
Bram Moolenaard089d9b2007-09-30 12:02:55 +00001403 p = vim_strsave(shorten_fname1(dirname));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001404 g_free(dirname);
1405 return p;
1406
Bram Moolenaar30613902019-12-01 22:11:18 +01001407# else // !defined(GTK_FILE_CHOOSER)
1408 // For GTK 2.2 and earlier: fall back to ordinary file selector.
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001409 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
Bram Moolenaar30613902019-12-01 22:11:18 +01001410# endif // !defined(GTK_FILE_CHOOSER)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00001411}
1412
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001413
Bram Moolenaar30613902019-12-01 22:11:18 +01001414#endif // FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001416#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
1418 static GtkWidget *
1419create_message_dialog(int type, char_u *title, char_u *message)
1420{
1421 GtkWidget *dialog;
1422 GtkMessageType message_type;
1423
1424 switch (type)
1425 {
1426 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1427 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1428 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1429 default: message_type = GTK_MESSAGE_INFO; break;
1430 }
1431
1432 message = CONVERT_TO_UTF8(message);
1433 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1434 GTK_DIALOG_DESTROY_WITH_PARENT,
1435 message_type,
1436 GTK_BUTTONS_NONE,
1437 "%s", (const char *)message);
1438 CONVERT_TO_UTF8_FREE(message);
1439
1440 if (title != NULL)
1441 {
1442 title = CONVERT_TO_UTF8(title);
1443 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1444 CONVERT_TO_UTF8_FREE(title);
1445 }
1446 else if (type == VIM_GENERIC)
1447 {
1448 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1449 }
1450
1451 return dialog;
1452}
1453
1454/*
1455 * Split up button_string into individual button labels by inserting
1456 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1457 * '&' with '_'. button_string must point to allocated memory!
1458 * Return an allocated array of pointers into button_string.
1459 */
1460 static char **
1461split_button_string(char_u *button_string, int *n_buttons)
1462{
1463 char **array;
1464 char_u *p;
1465 unsigned int count = 1;
1466
1467 for (p = button_string; *p != NUL; ++p)
1468 if (*p == DLG_BUTTON_SEP)
1469 ++count;
1470
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001471 array = ALLOC_MULT(char *, count + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 count = 0;
1473
1474 if (array != NULL)
1475 {
1476 array[count++] = (char *)button_string;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001477 for (p = button_string; *p != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 if (*p == DLG_BUTTON_SEP)
1480 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001481 *p++ = NUL;
1482 array[count++] = (char *)p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484 else if (*p == DLG_HOTKEY_CHAR)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001485 *p++ = '_';
1486 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001487 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 }
Bram Moolenaar30613902019-12-01 22:11:18 +01001489 array[count] = NULL; // currently not relied upon, but doesn't hurt
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491
1492 *n_buttons = count;
1493 return array;
1494}
1495
1496 static char **
1497split_button_translation(const char *message)
1498{
1499 char **buttons = NULL;
1500 char_u *str;
1501 int n_buttons = 0;
1502 int n_expected = 1;
1503
1504 for (str = (char_u *)message; *str != NUL; ++str)
1505 if (*str == DLG_BUTTON_SEP)
1506 ++n_expected;
1507
1508 str = (char_u *)_(message);
1509 if (str != NULL)
1510 {
1511 if (output_conv.vc_type != CONV_NONE)
1512 str = string_convert(&output_conv, str, NULL);
1513 else
1514 str = vim_strsave(str);
1515
1516 if (str != NULL)
1517 buttons = split_button_string(str, &n_buttons);
1518 }
1519 /*
1520 * Uh-oh... this should never ever happen. But we don't wanna crash
1521 * if the translation is broken, thus fall back to the untranslated
1522 * buttons string in case of emergency.
1523 */
1524 if (buttons == NULL || n_buttons != n_expected)
1525 {
1526 vim_free(buttons);
1527 vim_free(str);
1528 buttons = NULL;
1529 str = vim_strsave((char_u *)message);
1530
1531 if (str != NULL)
1532 buttons = split_button_string(str, &n_buttons);
1533 if (buttons == NULL)
1534 vim_free(str);
1535 }
1536
1537 return buttons;
1538}
1539
1540 static int
1541button_equal(const char *a, const char *b)
1542{
1543 while (*a != '\0' && *b != '\0')
1544 {
1545 if (*a == '_' && *++a == '\0')
1546 break;
1547 if (*b == '_' && *++b == '\0')
1548 break;
1549
1550 if (g_unichar_tolower(g_utf8_get_char(a))
1551 != g_unichar_tolower(g_utf8_get_char(b)))
1552 return FALSE;
1553
1554 a = g_utf8_next_char(a);
1555 b = g_utf8_next_char(b);
1556 }
1557
1558 return (*a == '\0' && *b == '\0');
1559}
1560
1561 static void
1562dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1563{
1564 char **ok;
Bram Moolenaar30613902019-12-01 22:11:18 +01001565 char **ync; // "yes no cancel"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 char **buttons;
1567 int n_buttons = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001568 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569
Bram Moolenaar30613902019-12-01 22:11:18 +01001570 button_string = vim_strsave(button_string); // must be writable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (button_string == NULL)
1572 return;
1573
Bram Moolenaar30613902019-12-01 22:11:18 +01001574 // Check 'v' flag in 'guioptions': vertical button placement.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1576 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001577# if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01001578 // Add GTK+ 3 code if necessary.
1579 // N.B. GTK+ 3 doesn't allow you to access vbox and action_area via
1580 // the C API.
Bram Moolenaar98921892016-02-23 17:14:37 +01001581# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 GtkWidget *vbutton_box;
1583
1584 vbutton_box = gtk_vbutton_box_new();
1585 gtk_widget_show(vbutton_box);
1586 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1587 vbutton_box, TRUE, FALSE, 0);
Bram Moolenaar30613902019-12-01 22:11:18 +01001588 // Overrule the "action_area" value, hopefully this works...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 GTK_DIALOG(dialog)->action_area = vbutton_box;
Bram Moolenaar98921892016-02-23 17:14:37 +01001590# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 }
1592
1593 /*
1594 * Yes this is ugly, I don't particularly like it either. But doing it
1595 * this way has the compelling advantage that translations need not to
1596 * be touched at all. See below what 'ok' and 'ync' are used for.
1597 */
1598 ok = split_button_translation(N_("&Ok"));
1599 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1600 buttons = split_button_string(button_string, &n_buttons);
1601
1602 /*
1603 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1604 * environment. Don't hit me -- it's all about consistency.
1605 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1606 * other way around...
1607 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001608 for (idx = 1; idx <= n_buttons; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 {
1610 char *label;
1611 char_u *label8;
1612
Bram Moolenaar89d40322006-08-29 15:30:07 +00001613 label = buttons[idx - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 /*
1615 * Perform some guesswork to find appropriate stock items for the
1616 * buttons. We have to compare with a sample of the translated
1617 * button string to get things right. Yes, this is hackish :/
1618 *
1619 * But even the common button labels aren't necessarily translated,
1620 * since anyone can create their own dialogs using Vim functions.
1621 * Thus we have to check for those too.
1622 */
Bram Moolenaar30613902019-12-01 22:11:18 +01001623 if (ok != NULL && ync != NULL) // almost impossible to fail
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 {
Bram Moolenaar98921892016-02-23 17:14:37 +01001625# if GTK_CHECK_VERSION(3,10,0)
1626 if (button_equal(label, ok[0])) label = _("OK");
1627 else if (button_equal(label, ync[0])) label = _("Yes");
1628 else if (button_equal(label, ync[1])) label = _("No");
1629 else if (button_equal(label, ync[2])) label = _("Cancel");
1630 else if (button_equal(label, "Ok")) label = _("OK");
1631 else if (button_equal(label, "Yes")) label = _("Yes");
1632 else if (button_equal(label, "No")) label = _("No");
Bram Moolenaar4d196172016-02-27 18:07:44 +01001633 else if (button_equal(label, "Cancel")) label = _("Cancel");
Bram Moolenaar98921892016-02-23 17:14:37 +01001634# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
1636 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
1637 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
1638 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
1639 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
1640 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
1641 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
1642 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
Bram Moolenaar98921892016-02-23 17:14:37 +01001643# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 }
1645 label8 = CONVERT_TO_UTF8((char_u *)label);
Bram Moolenaar89d40322006-08-29 15:30:07 +00001646 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 CONVERT_TO_UTF8_FREE(label8);
1648 }
1649
1650 if (ok != NULL)
1651 vim_free(*ok);
1652 if (ync != NULL)
1653 vim_free(*ync);
1654 vim_free(ok);
1655 vim_free(ync);
1656 vim_free(buttons);
1657 vim_free(button_string);
1658}
1659
1660/*
1661 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1662 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
1663 * GUI used to work this way, and I consider the impact on UI consistency
1664 * low enough to justify implementing this as a special Vim feature.
1665 */
1666typedef struct _DialogInfo
1667{
Bram Moolenaar30613902019-12-01 22:11:18 +01001668 int ignore_enter; // no default button, ignore "Enter"
1669 int noalt; // accept accelerators without Alt
1670 GtkDialog *dialog; // Widget of the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671} DialogInfo;
1672
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 static gboolean
1674dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1675{
1676 DialogInfo *di = (DialogInfo *)data;
1677
Bram Moolenaar30613902019-12-01 22:11:18 +01001678 // Ignore hitting Enter (or Space) when there is no default button.
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001679 if (di->ignore_enter && (event->keyval == GDK_Return
1680 || event->keyval == ' '))
1681 return TRUE;
Bram Moolenaar30613902019-12-01 22:11:18 +01001682 else // A different key was pressed, return to normal behavior
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001683 di->ignore_enter = FALSE;
1684
Bram Moolenaar30613902019-12-01 22:11:18 +01001685 // Close the dialog when hitting "Esc".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (event->keyval == GDK_Escape)
1687 {
1688 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1689 return TRUE;
1690 }
1691
1692 if (di->noalt
1693 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1694 {
1695 return gtk_window_mnemonic_activate(
1696 GTK_WINDOW(widget), event->keyval,
1697 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1698 }
1699
Bram Moolenaar30613902019-12-01 22:11:18 +01001700 return FALSE; // continue emission
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701}
1702
1703 int
Bram Moolenaar30613902019-12-01 22:11:18 +01001704gui_mch_dialog(int type, // type of dialog
1705 char_u *title, // title of dialog
1706 char_u *message, // message text
1707 char_u *buttons, // names of buttons
1708 int def_but, // default button
1709 char_u *textfield, // text for textfield or NULL
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001710 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711{
1712 GtkWidget *dialog;
1713 GtkWidget *entry = NULL;
1714 char_u *text;
1715 int response;
1716 DialogInfo dialoginfo;
1717
1718 dialog = create_message_dialog(type, title, message);
1719 dialoginfo.dialog = GTK_DIALOG(dialog);
1720 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1721
1722 if (textfield != NULL)
1723 {
1724 GtkWidget *alignment;
1725
1726 entry = gtk_entry_new();
1727 gtk_widget_show(entry);
1728
Bram Moolenaar30613902019-12-01 22:11:18 +01001729 // Make Enter work like pressing OK.
Bram Moolenaar6c4b6462012-07-10 13:12:51 +02001730 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
Bram Moolenaardf6b11e2010-11-24 18:48:12 +01001731
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 text = CONVERT_TO_UTF8(textfield);
1733 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1734 CONVERT_TO_UTF8_FREE(text);
1735
Bram Moolenaar98921892016-02-23 17:14:37 +01001736# if GTK_CHECK_VERSION(3,14,0)
1737 gtk_widget_set_halign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1738 gtk_widget_set_valign(GTK_WIDGET(entry), GTK_ALIGN_CENTER);
1739 gtk_widget_set_hexpand(GTK_WIDGET(entry), TRUE);
1740 gtk_widget_set_vexpand(GTK_WIDGET(entry), TRUE);
1741
1742 alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
1743# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1745 (float)1.0, (float)1.0);
Bram Moolenaar98921892016-02-23 17:14:37 +01001746# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 gtk_container_add(GTK_CONTAINER(alignment), entry);
1748 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1749 gtk_widget_show(alignment);
1750
Bram Moolenaar98921892016-02-23 17:14:37 +01001751# if GTK_CHECK_VERSION(3,0,0)
1752 {
1753 GtkWidget * const vbox
1754 = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1755 gtk_box_pack_start(GTK_BOX(vbox),
1756 alignment, TRUE, FALSE, 0);
1757 }
1758# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1760 alignment, TRUE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01001761# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 dialoginfo.noalt = FALSE;
1763 }
1764 else
1765 dialoginfo.noalt = TRUE;
1766
Bram Moolenaar30613902019-12-01 22:11:18 +01001767 // Allow activation of mnemonic accelerators without pressing <Alt> when
1768 // there is no textfield. Handle pressing Esc.
Bram Moolenaar664323e2018-09-18 22:30:07 +02001769 g_signal_connect(G_OBJECT(dialog), "key-press-event",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1771
1772 if (def_but > 0)
1773 {
1774 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1775 dialoginfo.ignore_enter = FALSE;
1776 }
1777 else
Bram Moolenaar30613902019-12-01 22:11:18 +01001778 // No default button, ignore pressing Enter.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 dialoginfo.ignore_enter = TRUE;
1780
Bram Moolenaar30613902019-12-01 22:11:18 +01001781 // Show the mouse pointer if it's currently hidden.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 gui_mch_mousehide(FALSE);
1783
1784 response = gtk_dialog_run(GTK_DIALOG(dialog));
1785
Bram Moolenaar30613902019-12-01 22:11:18 +01001786 // GTK_RESPONSE_NONE means the dialog was programmatically destroyed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (response != GTK_RESPONSE_NONE)
1788 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001789 if (response == GTK_RESPONSE_ACCEPT) // Enter pressed
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001790 response = def_but;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (textfield != NULL)
1792 {
1793 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1794 text = CONVERT_FROM_UTF8(text);
1795
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001796 vim_strncpy(textfield, text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
1798 CONVERT_FROM_UTF8_FREE(text);
1799 }
1800 gtk_widget_destroy(dialog);
1801 }
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 return response > 0 ? response : 0;
1804}
1805
Bram Moolenaar30613902019-12-01 22:11:18 +01001806#endif // FEAT_GUI_DIALOG
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
1808
1809#if defined(FEAT_MENU) || defined(PROTO)
1810
1811 void
1812gui_mch_show_popupmenu(vimmenu_T *menu)
1813{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001814# if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 /*
1816 * Append a submenu for selecting an input method. This is
1817 * currently the only way to switch input methods at runtime.
1818 */
Bram Moolenaar98921892016-02-23 17:14:37 +01001819# if !GTK_CHECK_VERSION(3,10,0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1821 "vim-has-im-menu") == NULL)
1822 {
1823 GtkWidget *menuitem;
1824 GtkWidget *submenu;
1825 char_u *name;
1826
1827 menuitem = gtk_separator_menu_item_new();
1828 gtk_widget_show(menuitem);
1829 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1830
1831 name = (char_u *)_("Input _Methods");
1832 name = CONVERT_TO_UTF8(name);
1833 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1834 CONVERT_TO_UTF8_FREE(name);
1835 gtk_widget_show(menuitem);
1836
1837 submenu = gtk_menu_new();
1838 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1839 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1840
1841 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1842 GTK_MENU_SHELL(submenu));
1843 g_object_set_data(G_OBJECT(menu->submenu_id),
1844 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1845 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001846# endif
Bram Moolenaar30613902019-12-01 22:11:18 +01001847# endif // FEAT_XIM
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaara859f042016-11-17 19:11:55 +01001849# if GTK_CHECK_VERSION(3,22,2)
1850 {
1851 GdkEventButton trigger;
1852
Bram Moolenaar30613902019-12-01 22:11:18 +01001853 // A pseudo event to have gtk_menu_popup_at_pointer() work. Since the
1854 // function calculates the popup menu position on the basis of the
1855 // actual pointer position when it is invoked, the fields x, y, x_root
1856 // and y_root are set to zero for convenience.
Bram Moolenaara859f042016-11-17 19:11:55 +01001857 trigger.type = GDK_BUTTON_PRESS;
1858 trigger.window = gtk_widget_get_window(gui.drawarea);
1859 trigger.send_event = FALSE;
1860 trigger.time = gui.event_time;
Bram Moolenaar792f0e32018-02-27 17:27:13 +01001861 trigger.x = 0.0;
1862 trigger.y = 0.0;
Bram Moolenaara859f042016-11-17 19:11:55 +01001863 trigger.axes = NULL;
1864 trigger.state = 0;
1865 trigger.button = 3;
1866 trigger.device = NULL;
1867 trigger.x_root = 0.0;
1868 trigger.y_root = 0.0;
1869
1870 gtk_menu_popup_at_pointer(GTK_MENU(menu->submenu_id),
1871 (GdkEvent *)&trigger);
1872 }
1873#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1875 NULL, NULL,
1876 (GtkMenuPositionFunc)NULL, NULL,
Bram Moolenaar20892c12011-06-26 04:49:00 +02001877 3U, gui.event_time);
Bram Moolenaara859f042016-11-17 19:11:55 +01001878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879}
1880
Bram Moolenaar30613902019-12-01 22:11:18 +01001881// Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1882// popup_menu_position_func().
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001883static int popup_mouse_pos;
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885/*
1886 * Menu position callback; used by gui_make_popup() to place the menu
1887 * at the current text cursor position.
1888 *
1889 * Note: The push_in output argument seems to affect scrolling of huge
1890 * menus that don't fit on the screen. Leave it at the default for now.
1891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001893popup_menu_position_func(GtkMenu *menu UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 gint *x, gint *y,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001895 gboolean *push_in UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001896 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897{
Bram Moolenaar98921892016-02-23 17:14:37 +01001898 gdk_window_get_origin(gtk_widget_get_window(gui.drawarea), x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001900 if (popup_mouse_pos)
1901 {
1902 int mx, my;
1903
1904 gui_mch_getmouse(&mx, &my);
1905 *x += mx;
1906 *y += my;
1907 }
Bram Moolenaar98921892016-02-23 17:14:37 +01001908 else if (curwin != NULL && gui.drawarea != NULL &&
1909 gtk_widget_get_window(gui.drawarea) != NULL)
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001910 {
Bram Moolenaar30613902019-12-01 22:11:18 +01001911 // Find the cursor position in the current window
Bram Moolenaar53f81742017-09-22 14:35:51 +02001912 *x += FILL_X(curwin->w_wincol + curwin->w_wcol + 1) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1914 }
1915}
1916
1917 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001918gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 vimmenu_T *menu;
1921
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001922 popup_mouse_pos = mouse_pos;
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 menu = gui_find_menu(path_name);
1925
1926 if (menu != NULL && menu->submenu_id != NULL)
1927 {
Bram Moolenaara859f042016-11-17 19:11:55 +01001928# if GTK_CHECK_VERSION(3,22,2)
1929 GdkWindow * const win = gtk_widget_get_window(gui.drawarea);
1930 GdkEventButton trigger;
1931
Bram Moolenaar30613902019-12-01 22:11:18 +01001932 // A pseudo event to have gtk_menu_popup_at_*() functions work. Since
1933 // the position where the menu pops up is automatically adjusted by
1934 // the functions, none of the fields x, y, x_root and y_root has to be
1935 // set to a specific value here; therefore, they are set to zero for
1936 // convenience.
Bram Moolenaara859f042016-11-17 19:11:55 +01001937 trigger.type = GDK_BUTTON_PRESS;
1938 trigger.window = win;
1939 trigger.send_event = FALSE;
1940 trigger.time = GDK_CURRENT_TIME;
Bram Moolenaar792f0e32018-02-27 17:27:13 +01001941 trigger.x = 0.0;
1942 trigger.y = 0.0;
Bram Moolenaara859f042016-11-17 19:11:55 +01001943 trigger.axes = NULL;
1944 trigger.state = 0;
1945 trigger.button = 0;
1946 trigger.device = NULL;
1947 trigger.x_root = 0.0;
1948 trigger.y_root = 0.0;
1949
1950 if (mouse_pos)
1951 gtk_menu_popup_at_pointer(GTK_MENU(menu->submenu_id),
1952 (GdkEvent *)&trigger);
1953 else
1954 {
1955 gint origin_x, origin_y;
1956 GdkRectangle rect = { 0, 0, 0, 0 };
1957
1958 gdk_window_get_origin(win, &origin_x, &origin_y);
1959 popup_menu_position_func(NULL, &rect.x, &rect.y, NULL, NULL);
1960
1961 rect.x -= origin_x;
1962 rect.y -= origin_y;
1963
1964 gtk_menu_popup_at_rect(GTK_MENU(menu->submenu_id),
1965 win,
1966 &rect,
1967 GDK_GRAVITY_SOUTH_EAST,
1968 GDK_GRAVITY_NORTH_WEST,
1969 (GdkEvent *)&trigger);
1970 }
1971# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1973 NULL, NULL,
1974 &popup_menu_position_func, NULL,
1975 0U, (guint32)GDK_CURRENT_TIME);
Bram Moolenaara859f042016-11-17 19:11:55 +01001976# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978}
1979
Bram Moolenaar30613902019-12-01 22:11:18 +01001980#endif // FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982
1983/*
1984 * We don't create it twice.
1985 */
1986
1987typedef struct _SharedFindReplace
1988{
Bram Moolenaar30613902019-12-01 22:11:18 +01001989 GtkWidget *dialog; // the main dialog widget
1990 GtkWidget *wword; // 'Whole word only' check button
1991 GtkWidget *mcase; // 'Match case' check button
1992 GtkWidget *up; // search direction 'Up' radio button
1993 GtkWidget *down; // search direction 'Down' radio button
1994 GtkWidget *what; // 'Find what' entry text widget
1995 GtkWidget *with; // 'Replace with' entry text widget
1996 GtkWidget *find; // 'Find Next' action button
1997 GtkWidget *replace; // 'Replace With' action button
1998 GtkWidget *all; // 'Replace All' action button
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999} SharedFindReplace;
2000
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002001static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
2002static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 static int
2005find_key_press_event(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002006 GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 GdkEventKey *event,
2008 SharedFindReplace *frdp)
2009{
Bram Moolenaar30613902019-12-01 22:11:18 +01002010 // If the user is holding one of the key modifiers we will just bail out,
2011 // thus preserving the possibility of normal focus traversal.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
2013 return FALSE;
2014
Bram Moolenaar30613902019-12-01 22:11:18 +01002015 // the Escape key synthesizes a cancellation action
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (event->keyval == GDK_Escape)
2017 {
2018 gtk_widget_hide(frdp->dialog);
2019
2020 return TRUE;
2021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022
Bram Moolenaar30613902019-12-01 22:11:18 +01002023 // It would be delightful if it where possible to do search history
2024 // operations on the K_UP and K_DOWN keys here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 return FALSE;
2027}
2028
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 static GtkWidget *
Bram Moolenaar98921892016-02-23 17:14:37 +01002030#if GTK_CHECK_VERSION(3,10,0)
2031create_image_button(const char *stock_id UNUSED,
2032 const char *label)
2033#else
2034create_image_button(const char *stock_id,
2035 const char *label)
2036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
2038 char_u *text;
2039 GtkWidget *box;
2040 GtkWidget *alignment;
2041 GtkWidget *button;
2042
2043 text = CONVERT_TO_UTF8((char_u *)label);
2044
Bram Moolenaar98921892016-02-23 17:14:37 +01002045#if GTK_CHECK_VERSION(3,2,0)
2046 box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
2047 gtk_box_set_homogeneous(GTK_BOX(box), FALSE);
2048#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 box = gtk_hbox_new(FALSE, 3);
Bram Moolenaar98921892016-02-23 17:14:37 +01002050#endif
2051#if !GTK_CHECK_VERSION(3,10,0)
2052 if (stock_id != NULL)
2053 gtk_box_pack_start(GTK_BOX(box),
2054 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
2055 FALSE, FALSE, 0);
2056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 gtk_box_pack_start(GTK_BOX(box),
2058 gtk_label_new((const char *)text),
2059 FALSE, FALSE, 0);
2060
2061 CONVERT_TO_UTF8_FREE(text);
2062
Bram Moolenaar98921892016-02-23 17:14:37 +01002063#if GTK_CHECK_VERSION(3,14,0)
2064 gtk_widget_set_halign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2065 gtk_widget_set_valign(GTK_WIDGET(box), GTK_ALIGN_CENTER);
2066 gtk_widget_set_hexpand(GTK_WIDGET(box), TRUE);
2067 gtk_widget_set_vexpand(GTK_WIDGET(box), TRUE);
2068
2069 alignment = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2070#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2072 (float)0.0, (float)0.0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002073#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 gtk_container_add(GTK_CONTAINER(alignment), box);
2075 gtk_widget_show_all(alignment);
2076
2077 button = gtk_button_new();
2078 gtk_container_add(GTK_CONTAINER(button), alignment);
2079
2080 return button;
2081}
2082
2083/*
2084 * This is currently only used by find_replace_dialog_create(), and
2085 * I'd really like to keep it at that. In other words: don't spread
2086 * this nasty hack all over the code. Think twice.
2087 */
2088 static const char *
2089convert_localized_message(char_u **buffer, const char *message)
2090{
2091 if (output_conv.vc_type == CONV_NONE)
2092 return message;
2093
2094 vim_free(*buffer);
2095 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
2096
2097 return (const char *)*buffer;
2098}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
Bram Moolenaar06b77ef2018-02-04 14:32:57 +01002100/*
2101 * Returns the number of characters in GtkEntry.
2102 */
2103 static unsigned long
2104entry_get_text_length(GtkEntry *entry)
2105{
2106 g_return_val_if_fail(entry != NULL, 0);
2107 g_return_val_if_fail(GTK_IS_ENTRY(entry) == TRUE, 0);
2108
2109#if GTK_CHECK_VERSION(2,18,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002110 // 2.18 introduced a new object GtkEntryBuffer to handle text data for
2111 // GtkEntry instead of letting each instance of the latter have its own
2112 // storage for that. The code below is almost identical to the
2113 // implementation of gtk_entry_get_text_length() for the versions >= 2.18.
Bram Moolenaar06b77ef2018-02-04 14:32:57 +01002114 return gtk_entry_buffer_get_length(gtk_entry_get_buffer(entry));
2115#elif GTK_CHECK_VERSION(2,14,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002116 // 2.14 introduced a new function to avoid memory management bugs which can
2117 // happen when gtk_entry_get_text() is used without due care and attention.
Bram Moolenaar06b77ef2018-02-04 14:32:57 +01002118 return gtk_entry_get_text_length(entry);
2119#else
Bram Moolenaar30613902019-12-01 22:11:18 +01002120 // gtk_entry_get_text() returns the pointer to the storage allocated
2121 // internally by the widget. Accordingly, use the one with great care:
2122 // Don't free it nor modify the contents it points to; call the function
2123 // every time you need the pointer since its value may have been changed
2124 // by the widget.
Bram Moolenaar06b77ef2018-02-04 14:32:57 +01002125 return g_utf8_strlen(gtk_entry_get_text(entry), -1);
2126#endif
2127}
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 static void
2130find_replace_dialog_create(char_u *arg, int do_replace)
2131{
Bram Moolenaar30613902019-12-01 22:11:18 +01002132 GtkWidget *hbox; // main top down box
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 GtkWidget *actionarea;
2134 GtkWidget *table;
2135 GtkWidget *tmp;
2136 GtkWidget *vbox;
2137 gboolean sensitive;
2138 SharedFindReplace *frdp;
2139 char_u *entry_text;
2140 int wword = FALSE;
2141 int mcase = !p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 char_u *conv_buffer = NULL;
2143# define CONV(message) convert_localized_message(&conv_buffer, (message))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144
2145 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2146
Bram Moolenaar30613902019-12-01 22:11:18 +01002147 // Get the search string to use.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 entry_text = get_find_dialog_text(arg, &wword, &mcase);
2149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
2151 {
2152 char_u *old_text = entry_text;
2153 entry_text = string_convert(&output_conv, entry_text, NULL);
2154 vim_free(old_text);
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156
2157 /*
2158 * If the dialog already exists, just raise it.
2159 */
2160 if (frdp->dialog)
2161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 if (entry_text != NULL)
2163 {
2164 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
Bram Moolenaar98921892016-02-23 17:14:37 +01002165 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2166 (gboolean)wword);
2167 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2168 (gboolean)mcase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 gtk_window_present(GTK_WINDOW(frdp->dialog));
Bram Moolenaard7823d52018-01-28 15:36:42 +01002171
Bram Moolenaar30613902019-12-01 22:11:18 +01002172 // For :promptfind dialog, always give keyboard focus to 'what' entry.
2173 // For :promptrepl dialog, give it to 'with' entry if 'what' has an
2174 // non-empty entry; otherwise, to 'what' entry.
Bram Moolenaard7823d52018-01-28 15:36:42 +01002175 gtk_widget_grab_focus(frdp->what);
Bram Moolenaar06b77ef2018-02-04 14:32:57 +01002176 if (do_replace && entry_get_text_length(GTK_ENTRY(frdp->what)) > 0)
Bram Moolenaard7823d52018-01-28 15:36:42 +01002177 gtk_widget_grab_focus(frdp->with);
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 vim_free(entry_text);
2180 return;
2181 }
2182
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 frdp->dialog = gtk_dialog_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002184#if GTK_CHECK_VERSION(3,0,0)
Bram Moolenaar30613902019-12-01 22:11:18 +01002185 // Nothing equivalent to gtk_dialog_set_has_separator() in GTK+ 3.
Bram Moolenaar98921892016-02-23 17:14:37 +01002186#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
2190 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 if (do_replace)
2193 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2195 CONV(_("VIM - Search and Replace...")));
2196 }
2197 else
2198 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2200 CONV(_("VIM - Search...")));
2201 }
2202
Bram Moolenaar98921892016-02-23 17:14:37 +01002203#if GTK_CHECK_VERSION(3,2,0)
2204 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
2205 gtk_box_set_homogeneous(GTK_BOX(hbox), FALSE);
2206#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 hbox = gtk_hbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
Bram Moolenaar98921892016-02-23 17:14:37 +01002210#if GTK_CHECK_VERSION(3,0,0)
2211 {
2212 GtkWidget * const dialog_vbox
2213 = gtk_dialog_get_content_area(GTK_DIALOG(frdp->dialog));
2214 gtk_container_add(GTK_CONTAINER(dialog_vbox), hbox);
2215 }
2216#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
Bram Moolenaar98921892016-02-23 17:14:37 +01002218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002221#if GTK_CHECK_VERSION(3,4,0)
2222 table = gtk_grid_new();
2223#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 table = gtk_table_new(1024, 4, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002227#if GTK_CHECK_VERSION(3,4,0)
2228 table = gtk_grid_new();
2229#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 table = gtk_table_new(1024, 3, FALSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002233 gtk_container_set_border_width(GTK_CONTAINER(table), 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234
2235 tmp = gtk_label_new(CONV(_("Find what:")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002236#if GTK_CHECK_VERSION(3,16,0)
2237 gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2238 gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2239#elif GTK_CHECK_VERSION(3,14,0)
2240 {
2241 GValue align_val = G_VALUE_INIT;
2242
2243 g_value_init(&align_val, G_TYPE_FLOAT);
2244
2245 g_value_set_float(&align_val, 0.0);
2246 g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2247
2248 g_value_set_float(&align_val, 0.5);
2249 g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2250
2251 g_value_unset(&align_val);
2252 }
2253#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
Bram Moolenaar98921892016-02-23 17:14:37 +01002255#endif
2256#if GTK_CHECK_VERSION(3,4,0)
2257 gtk_grid_attach(GTK_GRID(table), tmp, 0, 0, 2, 1);
2258#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
2260 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 frdp->what = gtk_entry_new();
2263 sensitive = (entry_text != NULL && entry_text[0] != NUL);
2264 if (entry_text != NULL)
2265 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
Bram Moolenaar98921892016-02-23 17:14:37 +01002266 g_signal_connect(G_OBJECT(frdp->what), "changed",
2267 G_CALLBACK(entry_changed_cb), frdp->dialog);
2268 g_signal_connect_after(G_OBJECT(frdp->what), "key-press-event",
2269 G_CALLBACK(find_key_press_event),
2270 (gpointer) frdp);
Bram Moolenaar98921892016-02-23 17:14:37 +01002271#if GTK_CHECK_VERSION(3,4,0)
2272 gtk_grid_attach(GTK_GRID(table), frdp->what, 2, 0, 5, 1);
2273#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
2275 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 if (do_replace)
2279 {
2280 tmp = gtk_label_new(CONV(_("Replace with:")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002281#if GTK_CHECK_VERSION(3,16,0)
2282 gtk_label_set_xalign(GTK_LABEL(tmp), 0.0);
2283 gtk_label_set_yalign(GTK_LABEL(tmp), 0.5);
2284#elif GTK_CHECK_VERSION(3,14,0)
2285 {
2286 GValue align_val = G_VALUE_INIT;
2287
2288 g_value_init(&align_val, G_TYPE_FLOAT);
2289
2290 g_value_set_float(&align_val, 0.0);
2291 g_object_set_property(G_OBJECT(tmp), "xalign", &align_val);
2292
2293 g_value_set_float(&align_val, 0.5);
2294 g_object_set_property(G_OBJECT(tmp), "yalign", &align_val);
2295
2296 g_value_unset(&align_val);
2297 }
2298#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
Bram Moolenaar98921892016-02-23 17:14:37 +01002300#endif
2301#if GTK_CHECK_VERSION(3,4,0)
2302 gtk_grid_attach(GTK_GRID(table), tmp, 0, 1, 2, 1);
2303#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
2305 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 frdp->with = gtk_entry_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002308 g_signal_connect(G_OBJECT(frdp->with), "activate",
2309 G_CALLBACK(find_replace_cb),
2310 GINT_TO_POINTER(FRD_R_FINDNEXT));
2311 g_signal_connect_after(G_OBJECT(frdp->with), "key-press-event",
2312 G_CALLBACK(find_key_press_event),
2313 (gpointer) frdp);
Bram Moolenaar98921892016-02-23 17:14:37 +01002314#if GTK_CHECK_VERSION(3,4,0)
2315 gtk_grid_attach(GTK_GRID(table), frdp->with, 2, 1, 5, 1);
2316#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
2318 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321 /*
2322 * Make the entry activation only change the input focus onto the
2323 * with item.
2324 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002325 g_signal_connect(G_OBJECT(frdp->what), "activate",
2326 G_CALLBACK(entry_activate_cb), frdp->with);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 }
2328 else
2329 {
2330 /*
2331 * Make the entry activation do the search.
2332 */
Bram Moolenaar98921892016-02-23 17:14:37 +01002333 g_signal_connect(G_OBJECT(frdp->what), "activate",
2334 G_CALLBACK(find_replace_cb),
2335 GINT_TO_POINTER(FRD_FINDNEXT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337
Bram Moolenaar30613902019-12-01 22:11:18 +01002338 // whole word only button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002340 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->wword),
2341 (gboolean)wword);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002343#if GTK_CHECK_VERSION(3,4,0)
2344 gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 2, 5, 1);
2345#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
2347 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002350#if GTK_CHECK_VERSION(3,4,0)
2351 gtk_grid_attach(GTK_GRID(table), frdp->wword, 0, 3, 5, 1);
2352#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
2354 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaar30613902019-12-01 22:11:18 +01002357 // match case button
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002359 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->mcase),
2360 (gboolean)mcase);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002362#if GTK_CHECK_VERSION(3,4,0)
2363 gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 3, 5, 1);
2364#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
2366 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002369#if GTK_CHECK_VERSION(3,4,0)
2370 gtk_grid_attach(GTK_GRID(table), frdp->mcase, 0, 4, 5, 1);
2371#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
2373 GTK_FILL, GTK_EXPAND, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375
2376 tmp = gtk_frame_new(CONV(_("Direction")));
2377 if (do_replace)
Bram Moolenaar98921892016-02-23 17:14:37 +01002378#if GTK_CHECK_VERSION(3,4,0)
2379 gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 2, 4);
2380#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
2382 GTK_FILL, GTK_FILL, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 else
Bram Moolenaar98921892016-02-23 17:14:37 +01002385#if GTK_CHECK_VERSION(3,4,0)
2386 gtk_grid_attach(GTK_GRID(table), tmp, 5, 2, 1, 3);
2387#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
2389 GTK_FILL, GTK_FILL, 2, 2);
Bram Moolenaar98921892016-02-23 17:14:37 +01002390#endif
2391#if GTK_CHECK_VERSION(3,2,0)
2392 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
2393 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
2394#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 vbox = gtk_vbox_new(FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002396#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01002397 gtk_container_set_border_width(GTK_CONTAINER(vbox), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 gtk_container_add(GTK_CONTAINER(tmp), vbox);
2399
Bram Moolenaar30613902019-12-01 22:11:18 +01002400 // 'Up' and 'Down' buttons
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
2402 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002403 frdp->down = gtk_radio_button_new_with_label(
2404 gtk_radio_button_get_group(GTK_RADIO_BUTTON(frdp->up)),
2405 CONV(_("Down")));
Bram Moolenaar98921892016-02-23 17:14:37 +01002406 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
2409
Bram Moolenaar30613902019-12-01 22:11:18 +01002410 // vbox to hold the action buttons
Bram Moolenaar98921892016-02-23 17:14:37 +01002411#if GTK_CHECK_VERSION(3,2,0)
2412 actionarea = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
2413#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 actionarea = gtk_vbutton_box_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002415#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01002416 gtk_container_set_border_width(GTK_CONTAINER(actionarea), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
2418
Bram Moolenaar30613902019-12-01 22:11:18 +01002419 // '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 g_signal_connect(G_OBJECT(frdp->find), "clicked",
2428 G_CALLBACK(find_replace_cb),
2429 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2430 : GINT_TO_POINTER(FRD_FINDNEXT));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431
Bram Moolenaar98921892016-02-23 17:14:37 +01002432 gtk_widget_set_can_default(frdp->find, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
2434 gtk_widget_grab_default(frdp->find);
2435
2436 if (do_replace)
2437 {
Bram Moolenaar30613902019-12-01 22:11:18 +01002438 // 'Replace' button
Bram Moolenaar98921892016-02-23 17:14:37 +01002439#if GTK_CHECK_VERSION(3,10,0)
2440 frdp->replace = create_image_button(NULL, _("Replace"));
2441#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
Bram Moolenaar98921892016-02-23 17:14:37 +01002443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 gtk_widget_set_sensitive(frdp->replace, sensitive);
Bram Moolenaar98921892016-02-23 17:14:37 +01002445 gtk_widget_set_can_default(frdp->find, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002447 g_signal_connect(G_OBJECT(frdp->replace), "clicked",
2448 G_CALLBACK(find_replace_cb),
2449 GINT_TO_POINTER(FRD_REPLACE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar30613902019-12-01 22:11:18 +01002451 // 'Replace All' button
Bram Moolenaar98921892016-02-23 17:14:37 +01002452#if GTK_CHECK_VERSION(3,10,0)
2453 frdp->all = create_image_button(NULL, _("Replace All"));
2454#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
Bram Moolenaar98921892016-02-23 17:14:37 +01002456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 gtk_widget_set_sensitive(frdp->all, sensitive);
Bram Moolenaar98921892016-02-23 17:14:37 +01002458 gtk_widget_set_can_default(frdp->all, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002460 g_signal_connect(G_OBJECT(frdp->all), "clicked",
2461 G_CALLBACK(find_replace_cb),
2462 GINT_TO_POINTER(FRD_REPLACEALL));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 }
2464
Bram Moolenaar30613902019-12-01 22:11:18 +01002465 // 'Cancel' button
Bram Moolenaar98921892016-02-23 17:14:37 +01002466#if GTK_CHECK_VERSION(3,10,0)
2467 tmp = gtk_button_new_with_mnemonic(_("_Close"));
2468#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
Bram Moolenaar98921892016-02-23 17:14:37 +01002470#endif
Bram Moolenaar98921892016-02-23 17:14:37 +01002471 gtk_widget_set_can_default(tmp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
Bram Moolenaar98921892016-02-23 17:14:37 +01002473 g_signal_connect_swapped(G_OBJECT(tmp),
2474 "clicked", G_CALLBACK(gtk_widget_hide),
2475 G_OBJECT(frdp->dialog));
2476 g_signal_connect_swapped(G_OBJECT(frdp->dialog),
2477 "delete-event", G_CALLBACK(gtk_widget_hide_on_delete),
2478 G_OBJECT(frdp->dialog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaar98921892016-02-23 17:14:37 +01002480#if GTK_CHECK_VERSION(3,2,0)
2481 tmp = gtk_separator_new(GTK_ORIENTATION_VERTICAL);
2482#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 tmp = gtk_vseparator_new();
Bram Moolenaar98921892016-02-23 17:14:37 +01002484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar30613902019-12-01 22:11:18 +01002487 // Suppress automatic show of the unused action area
Bram Moolenaar98921892016-02-23 17:14:37 +01002488#if GTK_CHECK_VERSION(3,0,0)
2489# if !GTK_CHECK_VERSION(3,12,0)
2490 gtk_widget_hide(gtk_dialog_get_action_area(GTK_DIALOG(frdp->dialog)));
2491# endif
2492#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
Bram Moolenaar98921892016-02-23 17:14:37 +01002494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 gtk_widget_show_all(hbox);
2496 gtk_widget_show(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 vim_free(entry_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 vim_free(conv_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500#undef CONV
2501}
2502
2503 void
2504gui_mch_find_dialog(exarg_T *eap)
2505{
2506 if (gui.in_use)
2507 find_replace_dialog_create(eap->arg, FALSE);
2508}
2509
2510 void
2511gui_mch_replace_dialog(exarg_T *eap)
2512{
2513 if (gui.in_use)
2514 find_replace_dialog_create(eap->arg, TRUE);
2515}
2516
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517/*
2518 * Callback for actions of the find and replace dialogs
2519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002521find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522{
2523 int flags;
2524 char_u *find_text;
2525 char_u *repl_text;
2526 gboolean direction_down;
2527 SharedFindReplace *sfr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar30613902019-12-01 22:11:18 +01002529 flags = (int)(long)data; // avoid a lint warning here
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530
Bram Moolenaar30613902019-12-01 22:11:18 +01002531 // Get the search/replace strings from the dialog
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (flags == FRD_FINDNEXT)
2533 {
2534 repl_text = NULL;
2535 sfr = &find_widgets;
2536 }
2537 else
2538 {
2539 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
2540 sfr = &repl_widgets;
2541 }
2542
2543 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
Bram Moolenaar98921892016-02-23 17:14:37 +01002544 direction_down = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->down));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
Bram Moolenaar98921892016-02-23 17:14:37 +01002546 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->wword)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 flags |= FRD_WHOLE_WORD;
Bram Moolenaar98921892016-02-23 17:14:37 +01002548 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(sfr->mcase)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 flags |= FRD_MATCH_CASE;
2550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 repl_text = CONVERT_FROM_UTF8(repl_text);
2552 find_text = CONVERT_FROM_UTF8(find_text);
Bram Moolenaare980d8a2010-12-08 13:11:21 +01002553 gui_do_findrepl(flags, find_text, repl_text, direction_down);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 CONVERT_FROM_UTF8_FREE(repl_text);
2555 CONVERT_FROM_UTF8_FREE(find_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556}
2557
Bram Moolenaar30613902019-12-01 22:11:18 +01002558/*
2559 * our usual callback function
2560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00002562entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563{
2564 gtk_widget_grab_focus(GTK_WIDGET(data));
2565}
2566
2567/*
2568 * Syncing the find/replace dialogs on the fly is utterly useless crack,
2569 * and causes nothing but problems. Please tell me a use case for which
2570 * you'd need both a find dialog and a find/replace one at the same time,
2571 * without being able to actually use them separately since they're syncing
2572 * all the time. I don't think it's worthwhile to fix this nonsense,
2573 * particularly evil incarnation of braindeadness, whatever; I'd much rather
2574 * see it extinguished from this planet. Thanks for listening. Sorry.
2575 */
2576 static void
2577entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
2578{
2579 const gchar *entry_text;
2580 gboolean nonempty;
2581
2582 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
2583
2584 if (!entry_text)
Bram Moolenaar30613902019-12-01 22:11:18 +01002585 return; // shouldn't happen
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 nonempty = (entry_text[0] != '\0');
2588
2589 if (dialog == find_widgets.dialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 gtk_widget_set_sensitive(find_widgets.find, nonempty);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 if (dialog == repl_widgets.dialog)
2593 {
2594 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
2595 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
2596 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
2597 }
2598}
2599
2600/*
2601 * ":helpfind"
2602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 void
Bram Moolenaar66f948e2016-01-30 16:39:25 +01002604ex_helpfind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605{
Bram Moolenaar30613902019-12-01 22:11:18 +01002606 // This will fail when menus are not loaded. Well, it's only for
2607 // backwards compatibility anyway.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
2609}
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02002610
Bram Moolenaar08bc2742012-06-06 16:14:40 +02002611#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02002612 static void
2613recent_func_log_func(const gchar *log_domain UNUSED,
2614 GLogLevelFlags log_level UNUSED,
2615 const gchar *message UNUSED,
2616 gpointer user_data UNUSED)
2617{
Bram Moolenaar30613902019-12-01 22:11:18 +01002618 // We just want to suppress the warnings.
2619 // http://bugzilla.gnome.org/show_bug.cgi?id=664587
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02002620}
Bram Moolenaar08bc2742012-06-06 16:14:40 +02002621#endif