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