blob: 5266dd7cef428a9e528d50715458d135c8cad367 [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 Moolenaar08bc2742012-06-06 16:14:40 +020093#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +020094static void recent_func_log_func(
95 const gchar *log_domain,
96 GLogLevelFlags log_level,
97 const gchar *message,
98 gpointer user_data);
Bram Moolenaar08bc2742012-06-06 16:14:40 +020099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200101#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102/*
103 * Table from BuiltIn## icon indices to GTK+ stock IDs. Order must exactly
104 * match toolbar_names[] in menu.c! All stock icons including the "vim-*"
105 * ones can be overridden in your gtkrc file.
106 */
107static const char * const menu_stock_ids[] =
108{
109 /* 00 */ GTK_STOCK_NEW,
110 /* 01 */ GTK_STOCK_OPEN,
111 /* 02 */ GTK_STOCK_SAVE,
112 /* 03 */ GTK_STOCK_UNDO,
113 /* 04 */ GTK_STOCK_REDO,
114 /* 05 */ GTK_STOCK_CUT,
115 /* 06 */ GTK_STOCK_COPY,
116 /* 07 */ GTK_STOCK_PASTE,
117 /* 08 */ GTK_STOCK_PRINT,
118 /* 09 */ GTK_STOCK_HELP,
119 /* 10 */ GTK_STOCK_FIND,
120 /* 11 */ "vim-save-all",
121 /* 12 */ "vim-session-save",
122 /* 13 */ "vim-session-new",
123 /* 14 */ "vim-session-load",
124 /* 15 */ GTK_STOCK_EXECUTE,
125 /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
126 /* 17 */ GTK_STOCK_CLOSE, /* FIXME: fuzzy */
127 /* 18 */ "vim-window-maximize",
128 /* 19 */ "vim-window-minimize",
129 /* 20 */ "vim-window-split",
130 /* 21 */ "vim-shell",
131 /* 22 */ GTK_STOCK_GO_BACK,
132 /* 23 */ GTK_STOCK_GO_FORWARD,
133 /* 24 */ "vim-find-help",
134 /* 25 */ GTK_STOCK_CONVERT,
135 /* 26 */ GTK_STOCK_JUMP_TO,
136 /* 27 */ "vim-build-tags",
137 /* 28 */ "vim-window-split-vertical",
138 /* 29 */ "vim-window-maximize-width",
139 /* 30 */ "vim-window-minimize-width",
140 /* 31 */ GTK_STOCK_QUIT
141};
142
143 static void
144add_stock_icon(GtkIconFactory *factory,
145 const char *stock_id,
146 const guint8 *inline_data,
147 int data_length)
148{
149 GdkPixbuf *pixbuf;
150 GtkIconSet *icon_set;
151
152 pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
153 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
154
155 gtk_icon_factory_add(factory, stock_id, icon_set);
156
157 gtk_icon_set_unref(icon_set);
158 g_object_unref(pixbuf);
159}
160
161 static int
162lookup_menu_iconfile(char_u *iconfile, char_u *dest)
163{
164 expand_env(iconfile, dest, MAXPATHL);
165
166 if (mch_isFullName(dest))
167 {
168 return vim_fexists(dest);
169 }
170 else
171 {
172 static const char suffixes[][4] = {"png", "xpm", "bmp"};
173 char_u buf[MAXPATHL];
174 unsigned int i;
175
176 for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
177 if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
178 {
179 STRCPY(dest, buf);
180 return TRUE;
181 }
182
183 return FALSE;
184 }
185}
186
187 static GtkWidget *
188load_menu_iconfile(char_u *name, GtkIconSize icon_size)
189{
190 GtkWidget *image = NULL;
191 GtkIconSet *icon_set;
192 GtkIconSource *icon_source;
193
194 /*
195 * Rather than loading the icon directly into a GtkImage, create
196 * a new GtkIconSet and put it in there. This way we can easily
197 * scale the toolbar icons on the fly when needed.
198 */
199 icon_set = gtk_icon_set_new();
200 icon_source = gtk_icon_source_new();
201
202 gtk_icon_source_set_filename(icon_source, (const char *)name);
203 gtk_icon_set_add_source(icon_set, icon_source);
204
205 image = gtk_image_new_from_icon_set(icon_set, icon_size);
206
207 gtk_icon_source_free(icon_source);
208 gtk_icon_set_unref(icon_set);
209
210 return image;
211}
212
213 static GtkWidget *
214create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
215{
216 GtkWidget *image = NULL;
217 char_u buf[MAXPATHL];
218
219 /* First use a specified "icon=" argument. */
220 if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
221 image = load_menu_iconfile(buf, icon_size);
222
223 /* If not found and not builtin specified try using the menu name. */
224 if (image == NULL && !menu->icon_builtin
225 && lookup_menu_iconfile(menu->name, buf))
226 image = load_menu_iconfile(buf, icon_size);
227
228 /* Still not found? Then use a builtin icon, a blank one as fallback. */
229 if (image == NULL)
230 {
231 const char *stock_id;
232 const int n_ids = G_N_ELEMENTS(menu_stock_ids);
233
234 if (menu->iconidx >= 0 && menu->iconidx < n_ids)
235 stock_id = menu_stock_ids[menu->iconidx];
236 else
237 stock_id = GTK_STOCK_MISSING_IMAGE;
238
239 image = gtk_image_new_from_stock(stock_id, icon_size);
240 }
241
242 return image;
243}
244
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000245 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000246toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
247 GdkEventFocus *event UNUSED,
248 gpointer data UNUSED)
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000249{
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000250 /* When we're in a GtkPlug, we don't have window focus events, only widget
251 * focus. To emulate stand-alone gvim, if a button gets focus (e.g.,
252 * <Tab> into GtkPlug) immediately pass it to mainwin. */
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000253 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000254 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000255
256 return TRUE;
257}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200258#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200260#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262 void
263gui_gtk_register_stock_icons(void)
264{
265# include "../pixmaps/stock_icons.h"
266 GtkIconFactory *factory;
267
268 factory = gtk_icon_factory_new();
269# define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
270
271 ADD_ICON("vim-build-tags", stock_vim_build_tags);
272 ADD_ICON("vim-find-help", stock_vim_find_help);
273 ADD_ICON("vim-save-all", stock_vim_save_all);
274 ADD_ICON("vim-session-load", stock_vim_session_load);
275 ADD_ICON("vim-session-new", stock_vim_session_new);
276 ADD_ICON("vim-session-save", stock_vim_session_save);
277 ADD_ICON("vim-shell", stock_vim_shell);
278 ADD_ICON("vim-window-maximize", stock_vim_window_maximize);
279 ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
280 ADD_ICON("vim-window-minimize", stock_vim_window_minimize);
281 ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
282 ADD_ICON("vim-window-split", stock_vim_window_split);
283 ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
284
285# undef ADD_ICON
286 gtk_icon_factory_add_default(factory);
287 g_object_unref(factory);
288}
289
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200290#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291
292
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#if defined(FEAT_MENU) || defined(PROTO)
294
295/*
296 * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
297 * if necessary. The caller must vim_free() the returned string.
298 *
299 * Input Output
300 * _ __
301 * && &
302 * & _ stripped if use_mnemonic == FALSE
303 * <Tab> end of menu label text
304 */
305 static char_u *
306translate_mnemonic_tag(char_u *name, int use_mnemonic)
307{
308 char_u *buf;
309 char_u *psrc;
310 char_u *pdest;
311 int n_underscores = 0;
312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 name = CONVERT_TO_UTF8(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 if (name == NULL)
315 return NULL;
316
317 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
318 if (*psrc == '_')
319 ++n_underscores;
320
321 buf = alloc((unsigned)(psrc - name + n_underscores + 1));
322 if (buf != NULL)
323 {
324 pdest = buf;
325 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
326 {
327 if (*psrc == '_')
328 {
329 *pdest++ = '_';
330 *pdest++ = '_';
331 }
332 else if (*psrc != '&')
333 {
334 *pdest++ = *psrc;
335 }
336 else if (*(psrc + 1) == '&')
337 {
338 *pdest++ = *psrc++;
339 }
340 else if (use_mnemonic)
341 {
342 *pdest++ = '_';
343 }
344 }
345 *pdest = NUL;
346 }
347
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 CONVERT_TO_UTF8_FREE(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 return buf;
350}
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 static void
353menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
354{
355 GtkWidget *box;
356 char_u *text;
357 int use_mnemonic;
358
359 /* It would be neat to have image menu items, but that would require major
360 * changes to Vim's menu system. Not to mention that all the translations
361 * had to be updated. */
362 menu->id = gtk_menu_item_new();
363 box = gtk_hbox_new(FALSE, 20);
364
365 use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
366 text = translate_mnemonic_tag(menu->name, use_mnemonic);
367
368 menu->label = gtk_label_new_with_mnemonic((const char *)text);
369 vim_free(text);
370
371 gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
372
373 if (menu->actext != NULL && menu->actext[0] != NUL)
374 {
375 text = CONVERT_TO_UTF8(menu->actext);
376
377 gtk_box_pack_end(GTK_BOX(box),
378 gtk_label_new((const char *)text),
379 FALSE, FALSE, 0);
380
381 CONVERT_TO_UTF8_FREE(text);
382 }
383
384 gtk_container_add(GTK_CONTAINER(menu->id), box);
385 gtk_widget_show_all(menu->id);
386}
387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 void
389gui_mch_add_menu(vimmenu_T *menu, int idx)
390{
391 vimmenu_T *parent;
392 GtkWidget *parent_widget;
393
394 if (menu->name[0] == ']' || menu_is_popup(menu->name))
395 {
396 menu->submenu_id = gtk_menu_new();
397 return;
398 }
399
400 parent = menu->parent;
401
402 if ((parent != NULL && parent->submenu_id == NULL)
403 || !menu_is_menubar(menu->name))
404 return;
405
406 parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
407 menu_item_new(menu, parent_widget);
408
409 /* since the tearoff should always appear first, increment idx */
410 if (parent != NULL && !menu_is_popup(parent->name))
411 ++idx;
412
413 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
414
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 menu->submenu_id = gtk_menu_new();
416
417 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
418 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
419
420 menu->tearoff_handle = gtk_tearoff_menu_item_new();
421 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
422 gtk_widget_show(menu->tearoff_handle);
423 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
424}
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000427menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 gui_menu_cb((vimmenu_T *)data);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430}
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 void
433gui_mch_add_menu_item(vimmenu_T *menu, int idx)
434{
435 vimmenu_T *parent;
436
437 parent = menu->parent;
438
439# ifdef FEAT_TOOLBAR
440 if (menu_is_toolbar(parent->name))
441 {
442 GtkToolbar *toolbar;
443
444 toolbar = GTK_TOOLBAR(gui.toolbar);
445 menu->submenu_id = NULL;
446
447 if (menu_is_separator(menu->name))
448 {
449 gtk_toolbar_insert_space(toolbar, idx);
450 menu->id = NULL;
451 }
452 else
453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 char_u *text;
455 char_u *tooltip;
456
457 text = CONVERT_TO_UTF8(menu->dname);
458 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000459 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
460 /* Invalid text, can happen when 'encoding' is changed. Avoid
461 * a nasty GTK error message, skip the tooltip. */
462 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463
464 menu->id = gtk_toolbar_insert_item(
465 toolbar,
466 (const char *)text,
467 (const char *)tooltip,
468 NULL,
469 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
470 G_CALLBACK(&menu_item_activate),
471 menu,
472 idx);
473
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000474 if (gtk_socket_id != 0)
475 gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
476 GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000477
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 CONVERT_TO_UTF8_FREE(text);
479 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 }
481 }
482 else
483# endif /* FEAT_TOOLBAR */
484 {
485 /* No parent, must be a non-menubar menu */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000486 if (parent == NULL || parent->submenu_id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487 return;
488
489 /* Make place for the possible tearoff handle item. Not in the popup
490 * menu, it doesn't have a tearoff item. */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000491 if (!menu_is_popup(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ++idx;
493
494 if (menu_is_separator(menu->name))
495 {
496 /* Separator: Just add it */
497 menu->id = gtk_menu_item_new();
498 gtk_widget_set_sensitive(menu->id, FALSE);
499 gtk_widget_show(menu->id);
500 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
501
502 return;
503 }
504
505 /* Add textual menu item. */
506 menu_item_new(menu, parent->submenu_id);
507 gtk_widget_show(menu->id);
508 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
509
510 if (menu->id != NULL)
511 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
512 GTK_SIGNAL_FUNC(menu_item_activate), menu);
513 }
514}
515#endif /* FEAT_MENU */
516
517
518 void
519gui_mch_set_text_area_pos(int x, int y, int w, int h)
520{
521 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
522}
523
524
525#if defined(FEAT_MENU) || defined(PROTO)
526/*
Bram Moolenaar79166c42007-05-10 18:29:51 +0000527 * Enable or disable accelerators for the toplevel menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 */
529 void
530gui_gtk_set_mnemonics(int enable)
531{
532 vimmenu_T *menu;
533 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534
535 for (menu = root_menu; menu != NULL; menu = menu->next)
536 {
537 if (menu->id == NULL)
538 continue;
539
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 name = translate_mnemonic_tag(menu->name, enable);
541 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
542 (const char *)name);
543 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
545}
546
547 static void
548recurse_tearoffs(vimmenu_T *menu, int val)
549{
550 for (; menu != NULL; menu = menu->next)
551 {
552 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
553 && menu->name[0] != ']' && !menu_is_popup(menu->name))
554 {
555 if (val)
556 gtk_widget_show(menu->tearoff_handle);
557 else
558 gtk_widget_hide(menu->tearoff_handle);
559 }
560 recurse_tearoffs(menu->children, val);
561 }
562}
563
564 void
565gui_mch_toggle_tearoffs(int enable)
566{
567 recurse_tearoffs(root_menu, enable);
568}
569#endif /* FEAT_MENU */
570
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200571#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 static int
573get_menu_position(vimmenu_T *menu)
574{
575 vimmenu_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000576 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577
578 for (node = menu->parent->children; node != menu; node = node->next)
579 {
580 g_return_val_if_fail(node != NULL, -1);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000581 ++idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583
Bram Moolenaar89d40322006-08-29 15:30:07 +0000584 return idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200586#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587
588
589#if defined(FEAT_TOOLBAR) || defined(PROTO)
590 void
591gui_mch_menu_set_tip(vimmenu_T *menu)
592{
593 if (menu->id != NULL && menu->parent != NULL
594 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
595 {
596 char_u *tooltip;
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000599 if (tooltip == NULL || utf_valid_string(tooltip, NULL))
600 /* Only set the tooltip when it's valid utf-8. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
602 menu->id, (const char *)tooltip, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 }
605}
606#endif /* FEAT_TOOLBAR */
607
608
609#if defined(FEAT_MENU) || defined(PROTO)
610/*
611 * Destroy the machine specific menu widget.
612 */
613 void
614gui_mch_destroy_menu(vimmenu_T *menu)
615{
616# ifdef FEAT_TOOLBAR
617 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 if (menu_is_separator(menu->name))
620 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
621 get_menu_position(menu));
622 else if (menu->id != NULL)
623 gtk_widget_destroy(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 }
625 else
626# endif /* FEAT_TOOLBAR */
627 {
628 if (menu->submenu_id != NULL)
629 gtk_widget_destroy(menu->submenu_id);
630
631 if (menu->id != NULL)
632 gtk_widget_destroy(menu->id);
633 }
634
635 menu->submenu_id = NULL;
636 menu->id = NULL;
637}
638#endif /* FEAT_MENU */
639
640
641/*
642 * Scrollbar stuff.
643 */
644 void
645gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
646{
647 if (sb->id != NULL)
648 {
649 GtkAdjustment *adjustment;
650
651 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
652
653 adjustment->lower = 0.0;
654 adjustment->value = val;
655 adjustment->upper = max + 1;
656 adjustment->page_size = size;
657 adjustment->page_increment = size < 3L ? 1L : size - 2L;
658 adjustment->step_increment = 1.0;
659
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 g_signal_handler_block(GTK_OBJECT(adjustment),
661 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 gtk_adjustment_changed(adjustment);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 g_signal_handler_unblock(GTK_OBJECT(adjustment),
664 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666}
667
668 void
669gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
670{
671 if (sb->id != NULL)
672 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
673}
674
675/*
676 * Take action upon scrollbar dragging.
677 */
678 static void
679adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
680{
681 scrollbar_T *sb;
682 long value;
683 int dragging = FALSE;
684
685#ifdef FEAT_XIM
686 /* cancel any preediting */
687 if (im_is_preediting())
688 xim_reset();
689#endif
690
691 sb = gui_find_scrollbar((long)data);
692 value = (long)adjustment->value;
693 /*
694 * The dragging argument must be right for the scrollbar to work with
695 * closed folds. This isn't documented, hopefully this will keep on
696 * working in later GTK versions.
697 *
698 * FIXME: Well, it doesn't work in GTK2. :)
699 * HACK: Get the mouse pointer position, if it appears to be on an arrow
700 * button set "dragging" to FALSE. This assumes square buttons!
701 */
702 if (sb != NULL)
703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 dragging = TRUE;
705
706 if (sb->wp != NULL)
707 {
708 int x;
709 int y;
710 GdkModifierType state;
711 int width;
712 int height;
713
714 /* vertical scrollbar: need to set "dragging" properly in case
715 * there are closed folds. */
716 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
717 gdk_window_get_size(sb->id->window, &width, &height);
718 if (x >= 0 && x < width && y >= 0 && y < height)
719 {
720 if (y < width)
721 {
722 /* up arrow: move one (closed fold) line up */
723 dragging = FALSE;
724 value = sb->wp->w_topline - 2;
725 }
726 else if (y > height - width)
727 {
728 /* down arrow: move one (closed fold) line down */
729 dragging = FALSE;
730 value = sb->wp->w_topline;
731 }
732 }
733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735
736 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737}
738
739/* SBAR_VERT or SBAR_HORIZ */
740 void
741gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
742{
743 if (orient == SBAR_HORIZ)
744 sb->id = gtk_hscrollbar_new(NULL);
745 else if (orient == SBAR_VERT)
746 sb->id = gtk_vscrollbar_new(NULL);
747
748 if (sb->id != NULL)
749 {
750 GtkAdjustment *adjustment;
751
752 GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
753 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
754
755 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
756
757 sb->handler_id = gtk_signal_connect(
758 GTK_OBJECT(adjustment), "value_changed",
759 GTK_SIGNAL_FUNC(adjustment_value_changed),
760 GINT_TO_POINTER(sb->ident));
761 gui_mch_update();
762 }
763}
764
765#if defined(FEAT_WINDOWS) || defined(PROTO)
766 void
767gui_mch_destroy_scrollbar(scrollbar_T *sb)
768{
769 if (sb->id != NULL)
770 {
771 gtk_widget_destroy(sb->id);
772 sb->id = NULL;
773 }
774 gui_mch_update();
775}
776#endif
777
778#if defined(FEAT_BROWSE) || defined(PROTO)
779/*
780 * Implementation of the file selector related stuff
781 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200782#if GTK_CHECK_VERSION(2,4,0)
Bram Moolenaara3f41662010-07-11 19:01:06 +0200783# define USE_FILE_CHOOSER
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000786#ifndef USE_FILE_CHOOSER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000788browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789{
790 gui_T *vw = (gui_T *)cbdata;
791
792 if (vw->browse_fname != NULL)
793 g_free(vw->browse_fname);
794
795 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
796 GTK_FILE_SELECTION(vw->filedlg)));
797 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000801browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
803 gui_T *vw = (gui_T *)cbdata;
804
805 if (vw->browse_fname != NULL)
806 {
807 g_free(vw->browse_fname);
808 vw->browse_fname = NULL;
809 }
810 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811}
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000814browse_destroy_cb(GtkWidget *widget UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815{
816 if (gui.browse_fname != NULL)
817 {
818 g_free(gui.browse_fname);
819 gui.browse_fname = NULL;
820 }
821 gui.filedlg = NULL;
Bram Moolenaara3f41662010-07-11 19:01:06 +0200822 gtk_main_quit();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 return FALSE;
824}
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827/*
828 * Put up a file requester.
829 * Returns the selected name in allocated memory, or NULL for Cancel.
830 * saving, select file to write
831 * title title for the window
832 * dflt default name
833 * ext not used (extension added)
834 * initdir initial directory, NULL for current dir
835 * filter not used (file name filter)
836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000838gui_mch_browse(int saving UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 char_u *title,
840 char_u *dflt,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000841 char_u *ext UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 char_u *initdir,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000843 char_u *filter UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844{
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000845#ifdef USE_FILE_CHOOSER
846 GtkWidget *fc;
847#endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000848 char_u dirbuf[MAXPATHL];
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +0200849 guint log_handler;
850 const gchar *domain = "Gtk";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 title = CONVERT_TO_UTF8(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000854 /* GTK has a bug, it only works with an absolute path. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000855 if (initdir == NULL || *initdir == NUL)
856 mch_dirname(dirbuf, MAXPATHL);
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000857 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000858 dirbuf[0] = NUL;
859 /* Always need a trailing slash for a directory. */
860 add_pathsep(dirbuf);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000861
862 /* If our pointer is currently hidden, then we should show it. */
863 gui_mch_mousehide(FALSE);
864
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +0200865 /* Hack: The GTK file dialog warns when it can't access a new file, this
866 * makes it shut up. http://bugzilla.gnome.org/show_bug.cgi?id=664587 */
867 log_handler = g_log_set_handler(domain, G_LOG_LEVEL_WARNING,
868 recent_func_log_func, NULL);
869
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000870#ifdef USE_FILE_CHOOSER
871 /* We create the dialog each time, so that the button text can be "Open"
872 * or "Save" according to the action. */
873 fc = gtk_file_chooser_dialog_new((const gchar *)title,
874 GTK_WINDOW(gui.mainwin),
875 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
876 : GTK_FILE_CHOOSER_ACTION_OPEN,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000877 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
Bram Moolenaard1350622006-10-24 20:01:06 +0000878 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000879 NULL);
880 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
881 (const gchar *)dirbuf);
Bram Moolenaara3f41662010-07-11 19:01:06 +0200882 if (saving && dflt != NULL && *dflt != NUL)
883 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000884
885 gui.browse_fname = NULL;
886 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000888 char *filename;
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000889
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000890 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
891 gui.browse_fname = (char_u *)g_strdup(filename);
892 g_free(filename);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000893 }
894 gtk_widget_destroy(GTK_WIDGET(fc));
895
896#else
897
898 if (gui.filedlg == NULL)
899 {
900 GtkFileSelection *fs; /* shortcut */
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 gui.filedlg = gtk_file_selection_new((const gchar *)title);
903 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
904 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000905 GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 fs = GTK_FILE_SELECTION(gui.filedlg);
907
908 gtk_container_border_width(GTK_CONTAINER(fs), 4);
909
910 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
911 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
912 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
913 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
914 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
915 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
916 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
917 GTK_OBJECT(gui.filedlg));
918 }
919 else
920 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
921
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000922 /* Concatenate "initdir" and "dflt". */
923 if (dflt != NULL && *dflt != NUL
924 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
925 STRCAT(dirbuf, dflt);
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
928 (const gchar *)dirbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
930 gtk_widget_show(gui.filedlg);
Bram Moolenaara3f41662010-07-11 19:01:06 +0200931 gtk_main();
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000932#endif
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +0200933 g_log_remove_handler(domain, log_handler);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000935 CONVERT_TO_UTF8_FREE(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (gui.browse_fname == NULL)
937 return NULL;
938
939 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000940 return vim_strsave(shorten_fname1(gui.browse_fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941}
942
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000943/*
944 * Put up a directory selector
945 * Returns the selected name in allocated memory, or NULL for Cancel.
946 * title title for the window
947 * dflt default name
948 * initdir initial directory, NULL for current dir
949 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000950 char_u *
951gui_mch_browsedir(
952 char_u *title,
953 char_u *initdir)
954{
955# if defined(GTK_FILE_CHOOSER) /* Only in GTK 2.4 and later. */
956 char_u dirbuf[MAXPATHL];
957 char_u *p;
958 GtkWidget *dirdlg; /* file selection dialog */
959 char_u *dirname = NULL;
960
961 title = CONVERT_TO_UTF8(title);
962
963 dirdlg = gtk_file_chooser_dialog_new(
964 (const gchar *)title,
965 GTK_WINDOW(gui.mainwin),
966 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
967 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
968 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
969 NULL);
970
971 CONVERT_TO_UTF8_FREE(title);
972
973 /* if our pointer is currently hidden, then we should show it. */
974 gui_mch_mousehide(FALSE);
975
976 /* GTK appears to insist on an absolute path. */
977 if (initdir == NULL || *initdir == NUL
978 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
979 mch_dirname(dirbuf, MAXPATHL - 10);
980
981 /* Always need a trailing slash for a directory.
982 * Also add a dummy file name, so that we get to the directory. */
983 add_pathsep(dirbuf);
984 STRCAT(dirbuf, "@zd(*&1|");
985 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
986 (const gchar *)dirbuf);
987
988 /* Run the dialog. */
989 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
990 dirname = (char_u *)gtk_file_chooser_get_filename(
991 GTK_FILE_CHOOSER(dirdlg));
992 gtk_widget_destroy(dirdlg);
993 if (dirname == NULL)
994 return NULL;
995
996 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000997 p = vim_strsave(shorten_fname1(dirname));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000998 g_free(dirname);
999 return p;
1000
1001# else
1002 /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
1003 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
1004# endif
1005}
1006
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008#endif /* FEAT_BROWSE */
1009
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001010#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011
1012 static GtkWidget *
1013create_message_dialog(int type, char_u *title, char_u *message)
1014{
1015 GtkWidget *dialog;
1016 GtkMessageType message_type;
1017
1018 switch (type)
1019 {
1020 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1021 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1022 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1023 default: message_type = GTK_MESSAGE_INFO; break;
1024 }
1025
1026 message = CONVERT_TO_UTF8(message);
1027 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1028 GTK_DIALOG_DESTROY_WITH_PARENT,
1029 message_type,
1030 GTK_BUTTONS_NONE,
1031 "%s", (const char *)message);
1032 CONVERT_TO_UTF8_FREE(message);
1033
1034 if (title != NULL)
1035 {
1036 title = CONVERT_TO_UTF8(title);
1037 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1038 CONVERT_TO_UTF8_FREE(title);
1039 }
1040 else if (type == VIM_GENERIC)
1041 {
1042 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1043 }
1044
1045 return dialog;
1046}
1047
1048/*
1049 * Split up button_string into individual button labels by inserting
1050 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1051 * '&' with '_'. button_string must point to allocated memory!
1052 * Return an allocated array of pointers into button_string.
1053 */
1054 static char **
1055split_button_string(char_u *button_string, int *n_buttons)
1056{
1057 char **array;
1058 char_u *p;
1059 unsigned int count = 1;
1060
1061 for (p = button_string; *p != NUL; ++p)
1062 if (*p == DLG_BUTTON_SEP)
1063 ++count;
1064
1065 array = (char **)alloc((count + 1) * sizeof(char *));
1066 count = 0;
1067
1068 if (array != NULL)
1069 {
1070 array[count++] = (char *)button_string;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001071 for (p = button_string; *p != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
1073 if (*p == DLG_BUTTON_SEP)
1074 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001075 *p++ = NUL;
1076 array[count++] = (char *)p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 else if (*p == DLG_HOTKEY_CHAR)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001079 *p++ = '_';
1080 else
1081 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1084 }
1085
1086 *n_buttons = count;
1087 return array;
1088}
1089
1090 static char **
1091split_button_translation(const char *message)
1092{
1093 char **buttons = NULL;
1094 char_u *str;
1095 int n_buttons = 0;
1096 int n_expected = 1;
1097
1098 for (str = (char_u *)message; *str != NUL; ++str)
1099 if (*str == DLG_BUTTON_SEP)
1100 ++n_expected;
1101
1102 str = (char_u *)_(message);
1103 if (str != NULL)
1104 {
1105 if (output_conv.vc_type != CONV_NONE)
1106 str = string_convert(&output_conv, str, NULL);
1107 else
1108 str = vim_strsave(str);
1109
1110 if (str != NULL)
1111 buttons = split_button_string(str, &n_buttons);
1112 }
1113 /*
1114 * Uh-oh... this should never ever happen. But we don't wanna crash
1115 * if the translation is broken, thus fall back to the untranslated
1116 * buttons string in case of emergency.
1117 */
1118 if (buttons == NULL || n_buttons != n_expected)
1119 {
1120 vim_free(buttons);
1121 vim_free(str);
1122 buttons = NULL;
1123 str = vim_strsave((char_u *)message);
1124
1125 if (str != NULL)
1126 buttons = split_button_string(str, &n_buttons);
1127 if (buttons == NULL)
1128 vim_free(str);
1129 }
1130
1131 return buttons;
1132}
1133
1134 static int
1135button_equal(const char *a, const char *b)
1136{
1137 while (*a != '\0' && *b != '\0')
1138 {
1139 if (*a == '_' && *++a == '\0')
1140 break;
1141 if (*b == '_' && *++b == '\0')
1142 break;
1143
1144 if (g_unichar_tolower(g_utf8_get_char(a))
1145 != g_unichar_tolower(g_utf8_get_char(b)))
1146 return FALSE;
1147
1148 a = g_utf8_next_char(a);
1149 b = g_utf8_next_char(b);
1150 }
1151
1152 return (*a == '\0' && *b == '\0');
1153}
1154
1155 static void
1156dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1157{
1158 char **ok;
1159 char **ync; /* "yes no cancel" */
1160 char **buttons;
1161 int n_buttons = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001162 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164 button_string = vim_strsave(button_string); /* must be writable */
1165 if (button_string == NULL)
1166 return;
1167
1168 /* Check 'v' flag in 'guioptions': vertical button placement. */
1169 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1170 {
1171 GtkWidget *vbutton_box;
1172
1173 vbutton_box = gtk_vbutton_box_new();
1174 gtk_widget_show(vbutton_box);
1175 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1176 vbutton_box, TRUE, FALSE, 0);
1177 /* Overrule the "action_area" value, hopefully this works... */
1178 GTK_DIALOG(dialog)->action_area = vbutton_box;
1179 }
1180
1181 /*
1182 * Yes this is ugly, I don't particularly like it either. But doing it
1183 * this way has the compelling advantage that translations need not to
1184 * be touched at all. See below what 'ok' and 'ync' are used for.
1185 */
1186 ok = split_button_translation(N_("&Ok"));
1187 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1188 buttons = split_button_string(button_string, &n_buttons);
1189
1190 /*
1191 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1192 * environment. Don't hit me -- it's all about consistency.
1193 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1194 * other way around...
1195 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001196 for (idx = 1; idx <= n_buttons; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 {
1198 char *label;
1199 char_u *label8;
1200
Bram Moolenaar89d40322006-08-29 15:30:07 +00001201 label = buttons[idx - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 /*
1203 * Perform some guesswork to find appropriate stock items for the
1204 * buttons. We have to compare with a sample of the translated
1205 * button string to get things right. Yes, this is hackish :/
1206 *
1207 * But even the common button labels aren't necessarily translated,
1208 * since anyone can create their own dialogs using Vim functions.
1209 * Thus we have to check for those too.
1210 */
1211 if (ok != NULL && ync != NULL) /* almost impossible to fail */
1212 {
1213 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
1214 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
1215 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
1216 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
1217 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
1218 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
1219 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
1220 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
1221 }
1222 label8 = CONVERT_TO_UTF8((char_u *)label);
Bram Moolenaar89d40322006-08-29 15:30:07 +00001223 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 CONVERT_TO_UTF8_FREE(label8);
1225 }
1226
1227 if (ok != NULL)
1228 vim_free(*ok);
1229 if (ync != NULL)
1230 vim_free(*ync);
1231 vim_free(ok);
1232 vim_free(ync);
1233 vim_free(buttons);
1234 vim_free(button_string);
1235}
1236
1237/*
1238 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1239 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
1240 * GUI used to work this way, and I consider the impact on UI consistency
1241 * low enough to justify implementing this as a special Vim feature.
1242 */
1243typedef struct _DialogInfo
1244{
1245 int ignore_enter; /* no default button, ignore "Enter" */
1246 int noalt; /* accept accelerators without Alt */
1247 GtkDialog *dialog; /* Widget of the dialog */
1248} DialogInfo;
1249
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 static gboolean
1251dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1252{
1253 DialogInfo *di = (DialogInfo *)data;
1254
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001255 /* Ignore hitting Enter (or Space) when there is no default button. */
1256 if (di->ignore_enter && (event->keyval == GDK_Return
1257 || event->keyval == ' '))
1258 return TRUE;
1259 else /* A different key was pressed, return to normal behavior */
1260 di->ignore_enter = FALSE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /* Close the dialog when hitting "Esc". */
1263 if (event->keyval == GDK_Escape)
1264 {
1265 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1266 return TRUE;
1267 }
1268
1269 if (di->noalt
1270 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1271 {
1272 return gtk_window_mnemonic_activate(
1273 GTK_WINDOW(widget), event->keyval,
1274 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1275 }
1276
1277 return FALSE; /* continue emission */
1278}
1279
1280 int
1281gui_mch_dialog(int type, /* type of dialog */
1282 char_u *title, /* title of dialog */
1283 char_u *message, /* message text */
1284 char_u *buttons, /* names of buttons */
1285 int def_but, /* default button */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001286 char_u *textfield, /* text for textfield or NULL */
1287 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288{
1289 GtkWidget *dialog;
1290 GtkWidget *entry = NULL;
1291 char_u *text;
1292 int response;
1293 DialogInfo dialoginfo;
1294
1295 dialog = create_message_dialog(type, title, message);
1296 dialoginfo.dialog = GTK_DIALOG(dialog);
1297 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1298
1299 if (textfield != NULL)
1300 {
1301 GtkWidget *alignment;
1302
1303 entry = gtk_entry_new();
1304 gtk_widget_show(entry);
1305
Bram Moolenaardf6b11e2010-11-24 18:48:12 +01001306 /* Make Enter work like pressing OK. */
1307 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 text = CONVERT_TO_UTF8(textfield);
1310 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1311 CONVERT_TO_UTF8_FREE(text);
1312
1313 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1314 (float)1.0, (float)1.0);
1315 gtk_container_add(GTK_CONTAINER(alignment), entry);
1316 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1317 gtk_widget_show(alignment);
1318
1319 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1320 alignment, TRUE, FALSE, 0);
1321 dialoginfo.noalt = FALSE;
1322 }
1323 else
1324 dialoginfo.noalt = TRUE;
1325
1326 /* Allow activation of mnemonic accelerators without pressing <Alt> when
Bram Moolenaar14716812006-05-04 21:54:08 +00001327 * there is no textfield. Handle pressing Esc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 g_signal_connect(G_OBJECT(dialog), "key_press_event",
1329 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1330
1331 if (def_but > 0)
1332 {
1333 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1334 dialoginfo.ignore_enter = FALSE;
1335 }
1336 else
1337 /* No default button, ignore pressing Enter. */
1338 dialoginfo.ignore_enter = TRUE;
1339
1340 /* Show the mouse pointer if it's currently hidden. */
1341 gui_mch_mousehide(FALSE);
1342
1343 response = gtk_dialog_run(GTK_DIALOG(dialog));
1344
1345 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
1346 if (response != GTK_RESPONSE_NONE)
1347 {
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001348 if (response == GTK_RESPONSE_ACCEPT) /* Enter pressed */
1349 response = def_but;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (textfield != NULL)
1351 {
1352 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1353 text = CONVERT_FROM_UTF8(text);
1354
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001355 vim_strncpy(textfield, text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356
1357 CONVERT_FROM_UTF8_FREE(text);
1358 }
1359 gtk_widget_destroy(dialog);
1360 }
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 return response > 0 ? response : 0;
1363}
1364
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001365#endif /* FEAT_GUI_DIALOG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367
1368#if defined(FEAT_MENU) || defined(PROTO)
1369
1370 void
1371gui_mch_show_popupmenu(vimmenu_T *menu)
1372{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001373# if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 /*
1375 * Append a submenu for selecting an input method. This is
1376 * currently the only way to switch input methods at runtime.
1377 */
1378 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1379 "vim-has-im-menu") == NULL)
1380 {
1381 GtkWidget *menuitem;
1382 GtkWidget *submenu;
1383 char_u *name;
1384
1385 menuitem = gtk_separator_menu_item_new();
1386 gtk_widget_show(menuitem);
1387 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1388
1389 name = (char_u *)_("Input _Methods");
1390 name = CONVERT_TO_UTF8(name);
1391 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1392 CONVERT_TO_UTF8_FREE(name);
1393 gtk_widget_show(menuitem);
1394
1395 submenu = gtk_menu_new();
1396 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1397 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1398
1399 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1400 GTK_MENU_SHELL(submenu));
1401 g_object_set_data(G_OBJECT(menu->submenu_id),
1402 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1403 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001404# endif /* FEAT_XIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
1406 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1407 NULL, NULL,
1408 (GtkMenuPositionFunc)NULL, NULL,
Bram Moolenaar20892c12011-06-26 04:49:00 +02001409 3U, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410}
1411
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001412/* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1413 * popup_menu_position_func(). */
1414static int popup_mouse_pos;
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416/*
1417 * Menu position callback; used by gui_make_popup() to place the menu
1418 * at the current text cursor position.
1419 *
1420 * Note: The push_in output argument seems to affect scrolling of huge
1421 * menus that don't fit on the screen. Leave it at the default for now.
1422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001424popup_menu_position_func(GtkMenu *menu UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 gint *x, gint *y,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001426 gboolean *push_in UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001427 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428{
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001429 gdk_window_get_origin(gui.drawarea->window, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001431 if (popup_mouse_pos)
1432 {
1433 int mx, my;
1434
1435 gui_mch_getmouse(&mx, &my);
1436 *x += mx;
1437 *y += my;
1438 }
1439 else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
1440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 /* Find the cursor position in the current window */
1442 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
1443 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1444 }
1445}
1446
1447 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001448gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 vimmenu_T *menu;
1451
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001452 popup_mouse_pos = mouse_pos;
1453
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 menu = gui_find_menu(path_name);
1455
1456 if (menu != NULL && menu->submenu_id != NULL)
1457 {
1458 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1459 NULL, NULL,
1460 &popup_menu_position_func, NULL,
1461 0U, (guint32)GDK_CURRENT_TIME);
1462 }
1463}
1464
1465#endif /* FEAT_MENU */
1466
1467
1468/*
1469 * We don't create it twice.
1470 */
1471
1472typedef struct _SharedFindReplace
1473{
1474 GtkWidget *dialog; /* the main dialog widget */
1475 GtkWidget *wword; /* 'Whole word only' check button */
1476 GtkWidget *mcase; /* 'Match case' check button */
1477 GtkWidget *up; /* search direction 'Up' radio button */
1478 GtkWidget *down; /* search direction 'Down' radio button */
1479 GtkWidget *what; /* 'Find what' entry text widget */
1480 GtkWidget *with; /* 'Replace with' entry text widget */
1481 GtkWidget *find; /* 'Find Next' action button */
1482 GtkWidget *replace; /* 'Replace With' action button */
1483 GtkWidget *all; /* 'Replace All' action button */
1484} SharedFindReplace;
1485
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001486static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1487static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 static int
1490find_key_press_event(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001491 GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 GdkEventKey *event,
1493 SharedFindReplace *frdp)
1494{
1495 /* If the user is holding one of the key modifiers we will just bail out,
1496 * thus preserving the possibility of normal focus traversal.
1497 */
1498 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
1499 return FALSE;
1500
1501 /* the Escape key synthesizes a cancellation action */
1502 if (event->keyval == GDK_Escape)
1503 {
1504 gtk_widget_hide(frdp->dialog);
1505
1506 return TRUE;
1507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508
Bram Moolenaar79166c42007-05-10 18:29:51 +00001509 /* It would be delightful if it where possible to do search history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 * operations on the K_UP and K_DOWN keys here.
1511 */
1512
1513 return FALSE;
1514}
1515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 static GtkWidget *
1517create_image_button(const char *stock_id, const char *label)
1518{
1519 char_u *text;
1520 GtkWidget *box;
1521 GtkWidget *alignment;
1522 GtkWidget *button;
1523
1524 text = CONVERT_TO_UTF8((char_u *)label);
1525
1526 box = gtk_hbox_new(FALSE, 3);
1527 gtk_box_pack_start(GTK_BOX(box),
1528 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
1529 FALSE, FALSE, 0);
1530 gtk_box_pack_start(GTK_BOX(box),
1531 gtk_label_new((const char *)text),
1532 FALSE, FALSE, 0);
1533
1534 CONVERT_TO_UTF8_FREE(text);
1535
1536 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1537 (float)0.0, (float)0.0);
1538 gtk_container_add(GTK_CONTAINER(alignment), box);
1539 gtk_widget_show_all(alignment);
1540
1541 button = gtk_button_new();
1542 gtk_container_add(GTK_CONTAINER(button), alignment);
1543
1544 return button;
1545}
1546
1547/*
1548 * This is currently only used by find_replace_dialog_create(), and
1549 * I'd really like to keep it at that. In other words: don't spread
1550 * this nasty hack all over the code. Think twice.
1551 */
1552 static const char *
1553convert_localized_message(char_u **buffer, const char *message)
1554{
1555 if (output_conv.vc_type == CONV_NONE)
1556 return message;
1557
1558 vim_free(*buffer);
1559 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
1560
1561 return (const char *)*buffer;
1562}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563
1564 static void
1565find_replace_dialog_create(char_u *arg, int do_replace)
1566{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 GtkWidget *hbox; /* main top down box */
1568 GtkWidget *actionarea;
1569 GtkWidget *table;
1570 GtkWidget *tmp;
1571 GtkWidget *vbox;
1572 gboolean sensitive;
1573 SharedFindReplace *frdp;
1574 char_u *entry_text;
1575 int wword = FALSE;
1576 int mcase = !p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 char_u *conv_buffer = NULL;
1578# define CONV(message) convert_localized_message(&conv_buffer, (message))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
1580 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
1581
1582 /* Get the search string to use. */
1583 entry_text = get_find_dialog_text(arg, &wword, &mcase);
1584
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
1586 {
1587 char_u *old_text = entry_text;
1588 entry_text = string_convert(&output_conv, entry_text, NULL);
1589 vim_free(old_text);
1590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591
1592 /*
1593 * If the dialog already exists, just raise it.
1594 */
1595 if (frdp->dialog)
1596 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (entry_text != NULL)
1598 {
1599 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1600 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1601 (gboolean)wword);
1602 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1603 (gboolean)mcase);
1604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 gtk_window_present(GTK_WINDOW(frdp->dialog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 vim_free(entry_text);
1607 return;
1608 }
1609
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 frdp->dialog = gtk_dialog_new();
1611 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
1612 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
1613 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 if (do_replace)
1616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1618 CONV(_("VIM - Search and Replace...")));
1619 }
1620 else
1621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1623 CONV(_("VIM - Search...")));
1624 }
1625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 hbox = gtk_hbox_new(FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
1628 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629
1630 if (do_replace)
1631 table = gtk_table_new(1024, 4, FALSE);
1632 else
1633 table = gtk_table_new(1024, 3, FALSE);
1634 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
1635 gtk_container_border_width(GTK_CONTAINER(table), 4);
1636
1637 tmp = gtk_label_new(CONV(_("Find what:")));
1638 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1639 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
1640 GTK_FILL, GTK_EXPAND, 2, 2);
1641 frdp->what = gtk_entry_new();
1642 sensitive = (entry_text != NULL && entry_text[0] != NUL);
1643 if (entry_text != NULL)
1644 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1645 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
1646 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
1647 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
1648 GTK_SIGNAL_FUNC(find_key_press_event),
1649 (gpointer) frdp);
1650 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
1651 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1652
1653 if (do_replace)
1654 {
1655 tmp = gtk_label_new(CONV(_("Replace with:")));
1656 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1657 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
1658 GTK_FILL, GTK_EXPAND, 2, 2);
1659 frdp->with = gtk_entry_new();
1660 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
1661 GTK_SIGNAL_FUNC(find_replace_cb),
1662 GINT_TO_POINTER(FRD_R_FINDNEXT));
1663 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
1664 GTK_SIGNAL_FUNC(find_key_press_event),
1665 (gpointer) frdp);
1666 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
1667 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1668
1669 /*
1670 * Make the entry activation only change the input focus onto the
1671 * with item.
1672 */
1673 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1674 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
1675 }
1676 else
1677 {
1678 /*
1679 * Make the entry activation do the search.
1680 */
1681 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1682 GTK_SIGNAL_FUNC(find_replace_cb),
1683 GINT_TO_POINTER(FRD_FINDNEXT));
1684 }
1685
1686 /* whole word only button */
1687 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
1688 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1689 (gboolean)wword);
1690 if (do_replace)
1691 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
1692 GTK_FILL, GTK_EXPAND, 2, 2);
1693 else
1694 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
1695 GTK_FILL, GTK_EXPAND, 2, 2);
1696
1697 /* match case button */
1698 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
1699 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1700 (gboolean)mcase);
1701 if (do_replace)
1702 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
1703 GTK_FILL, GTK_EXPAND, 2, 2);
1704 else
1705 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
1706 GTK_FILL, GTK_EXPAND, 2, 2);
1707
1708 tmp = gtk_frame_new(CONV(_("Direction")));
1709 if (do_replace)
1710 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
1711 GTK_FILL, GTK_FILL, 2, 2);
1712 else
1713 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
1714 GTK_FILL, GTK_FILL, 2, 2);
1715 vbox = gtk_vbox_new(FALSE, 0);
1716 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
1717 gtk_container_add(GTK_CONTAINER(tmp), vbox);
1718
1719 /* 'Up' and 'Down' buttons */
1720 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
1721 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
1722 frdp->down = gtk_radio_button_new_with_label(
1723 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
1724 CONV(_("Down")));
1725 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
1728
1729 /* vbox to hold the action buttons */
1730 actionarea = gtk_vbutton_box_new();
1731 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
1733
1734 /* 'Find Next' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 gtk_widget_set_sensitive(frdp->find, sensitive);
1737
1738 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
1739 GTK_SIGNAL_FUNC(find_replace_cb),
1740 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
1741 : GINT_TO_POINTER(FRD_FINDNEXT));
1742
1743 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
1744 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
1745 gtk_widget_grab_default(frdp->find);
1746
1747 if (do_replace)
1748 {
1749 /* 'Replace' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 gtk_widget_set_sensitive(frdp->replace, sensitive);
1752 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
1753 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
1754 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
1755 GTK_SIGNAL_FUNC(find_replace_cb),
1756 GINT_TO_POINTER(FRD_REPLACE));
1757
1758 /* 'Replace All' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 gtk_widget_set_sensitive(frdp->all, sensitive);
1761 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
1762 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
1763 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
1764 GTK_SIGNAL_FUNC(find_replace_cb),
1765 GINT_TO_POINTER(FRD_REPLACEALL));
1766 }
1767
1768 /* 'Cancel' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
1771 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
1772 gtk_signal_connect_object(GTK_OBJECT(tmp),
1773 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
1774 GTK_OBJECT(frdp->dialog));
1775 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
1776 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1777 GTK_OBJECT(frdp->dialog));
1778
1779 tmp = gtk_vseparator_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 /* Suppress automatic show of the unused action area */
1783 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
1784 gtk_widget_show_all(hbox);
1785 gtk_widget_show(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 vim_free(entry_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 vim_free(conv_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#undef CONV
1790}
1791
1792 void
1793gui_mch_find_dialog(exarg_T *eap)
1794{
1795 if (gui.in_use)
1796 find_replace_dialog_create(eap->arg, FALSE);
1797}
1798
1799 void
1800gui_mch_replace_dialog(exarg_T *eap)
1801{
1802 if (gui.in_use)
1803 find_replace_dialog_create(eap->arg, TRUE);
1804}
1805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
1807 * Callback for actions of the find and replace dialogs
1808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001810find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 int flags;
1813 char_u *find_text;
1814 char_u *repl_text;
1815 gboolean direction_down;
1816 SharedFindReplace *sfr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 flags = (int)(long)data; /* avoid a lint warning here */
1819
1820 /* Get the search/replace strings from the dialog */
1821 if (flags == FRD_FINDNEXT)
1822 {
1823 repl_text = NULL;
1824 sfr = &find_widgets;
1825 }
1826 else
1827 {
1828 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
1829 sfr = &repl_widgets;
1830 }
1831
1832 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
1833 direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
1834
1835 if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
1836 flags |= FRD_WHOLE_WORD;
1837 if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
1838 flags |= FRD_MATCH_CASE;
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 repl_text = CONVERT_FROM_UTF8(repl_text);
1841 find_text = CONVERT_FROM_UTF8(find_text);
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001842 gui_do_findrepl(flags, find_text, repl_text, direction_down);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 CONVERT_FROM_UTF8_FREE(repl_text);
1844 CONVERT_FROM_UTF8_FREE(find_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845}
1846
1847/* our usual callback function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001849entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850{
1851 gtk_widget_grab_focus(GTK_WIDGET(data));
1852}
1853
1854/*
1855 * Syncing the find/replace dialogs on the fly is utterly useless crack,
1856 * and causes nothing but problems. Please tell me a use case for which
1857 * you'd need both a find dialog and a find/replace one at the same time,
1858 * without being able to actually use them separately since they're syncing
1859 * all the time. I don't think it's worthwhile to fix this nonsense,
1860 * particularly evil incarnation of braindeadness, whatever; I'd much rather
1861 * see it extinguished from this planet. Thanks for listening. Sorry.
1862 */
1863 static void
1864entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
1865{
1866 const gchar *entry_text;
1867 gboolean nonempty;
1868
1869 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
1870
1871 if (!entry_text)
1872 return; /* shouldn't happen */
1873
1874 nonempty = (entry_text[0] != '\0');
1875
1876 if (dialog == find_widgets.dialog)
1877 {
1878 gtk_widget_set_sensitive(find_widgets.find, nonempty);
1879 }
1880
1881 if (dialog == repl_widgets.dialog)
1882 {
1883 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
1884 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
1885 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
1886 }
1887}
1888
1889/*
1890 * ":helpfind"
1891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 void
1893ex_helpfind(eap)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001894 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895{
1896 /* This will fail when menus are not loaded. Well, it's only for
1897 * backwards compatibility anyway. */
1898 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
1899}
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001900
Bram Moolenaar08bc2742012-06-06 16:14:40 +02001901#if defined(FEAT_BROWSE) || defined(PROTO)
Bram Moolenaar68fb5dc2012-04-25 17:10:16 +02001902 static void
1903recent_func_log_func(const gchar *log_domain UNUSED,
1904 GLogLevelFlags log_level UNUSED,
1905 const gchar *message UNUSED,
1906 gpointer user_data UNUSED)
1907{
1908 /* We just want to suppress the warnings. */
1909 /* http://bugzilla.gnome.org/show_bug.cgi?id=664587 */
1910}
Bram Moolenaar08bc2742012-06-06 16:14:40 +02001911#endif