blob: 52d75e4a225b0e06e67496ba86f7cb15ecc1cb33 [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);
93
Bram Moolenaar182c5be2010-06-25 05:37:59 +020094#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000095/*
96 * Table from BuiltIn## icon indices to GTK+ stock IDs. Order must exactly
97 * match toolbar_names[] in menu.c! All stock icons including the "vim-*"
98 * ones can be overridden in your gtkrc file.
99 */
100static const char * const menu_stock_ids[] =
101{
102 /* 00 */ GTK_STOCK_NEW,
103 /* 01 */ GTK_STOCK_OPEN,
104 /* 02 */ GTK_STOCK_SAVE,
105 /* 03 */ GTK_STOCK_UNDO,
106 /* 04 */ GTK_STOCK_REDO,
107 /* 05 */ GTK_STOCK_CUT,
108 /* 06 */ GTK_STOCK_COPY,
109 /* 07 */ GTK_STOCK_PASTE,
110 /* 08 */ GTK_STOCK_PRINT,
111 /* 09 */ GTK_STOCK_HELP,
112 /* 10 */ GTK_STOCK_FIND,
113 /* 11 */ "vim-save-all",
114 /* 12 */ "vim-session-save",
115 /* 13 */ "vim-session-new",
116 /* 14 */ "vim-session-load",
117 /* 15 */ GTK_STOCK_EXECUTE,
118 /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
119 /* 17 */ GTK_STOCK_CLOSE, /* FIXME: fuzzy */
120 /* 18 */ "vim-window-maximize",
121 /* 19 */ "vim-window-minimize",
122 /* 20 */ "vim-window-split",
123 /* 21 */ "vim-shell",
124 /* 22 */ GTK_STOCK_GO_BACK,
125 /* 23 */ GTK_STOCK_GO_FORWARD,
126 /* 24 */ "vim-find-help",
127 /* 25 */ GTK_STOCK_CONVERT,
128 /* 26 */ GTK_STOCK_JUMP_TO,
129 /* 27 */ "vim-build-tags",
130 /* 28 */ "vim-window-split-vertical",
131 /* 29 */ "vim-window-maximize-width",
132 /* 30 */ "vim-window-minimize-width",
133 /* 31 */ GTK_STOCK_QUIT
134};
135
136 static void
137add_stock_icon(GtkIconFactory *factory,
138 const char *stock_id,
139 const guint8 *inline_data,
140 int data_length)
141{
142 GdkPixbuf *pixbuf;
143 GtkIconSet *icon_set;
144
145 pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
146 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
147
148 gtk_icon_factory_add(factory, stock_id, icon_set);
149
150 gtk_icon_set_unref(icon_set);
151 g_object_unref(pixbuf);
152}
153
154 static int
155lookup_menu_iconfile(char_u *iconfile, char_u *dest)
156{
157 expand_env(iconfile, dest, MAXPATHL);
158
159 if (mch_isFullName(dest))
160 {
161 return vim_fexists(dest);
162 }
163 else
164 {
165 static const char suffixes[][4] = {"png", "xpm", "bmp"};
166 char_u buf[MAXPATHL];
167 unsigned int i;
168
169 for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
170 if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
171 {
172 STRCPY(dest, buf);
173 return TRUE;
174 }
175
176 return FALSE;
177 }
178}
179
180 static GtkWidget *
181load_menu_iconfile(char_u *name, GtkIconSize icon_size)
182{
183 GtkWidget *image = NULL;
184 GtkIconSet *icon_set;
185 GtkIconSource *icon_source;
186
187 /*
188 * Rather than loading the icon directly into a GtkImage, create
189 * a new GtkIconSet and put it in there. This way we can easily
190 * scale the toolbar icons on the fly when needed.
191 */
192 icon_set = gtk_icon_set_new();
193 icon_source = gtk_icon_source_new();
194
195 gtk_icon_source_set_filename(icon_source, (const char *)name);
196 gtk_icon_set_add_source(icon_set, icon_source);
197
198 image = gtk_image_new_from_icon_set(icon_set, icon_size);
199
200 gtk_icon_source_free(icon_source);
201 gtk_icon_set_unref(icon_set);
202
203 return image;
204}
205
206 static GtkWidget *
207create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
208{
209 GtkWidget *image = NULL;
210 char_u buf[MAXPATHL];
211
212 /* First use a specified "icon=" argument. */
213 if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
214 image = load_menu_iconfile(buf, icon_size);
215
216 /* If not found and not builtin specified try using the menu name. */
217 if (image == NULL && !menu->icon_builtin
218 && lookup_menu_iconfile(menu->name, buf))
219 image = load_menu_iconfile(buf, icon_size);
220
221 /* Still not found? Then use a builtin icon, a blank one as fallback. */
222 if (image == NULL)
223 {
224 const char *stock_id;
225 const int n_ids = G_N_ELEMENTS(menu_stock_ids);
226
227 if (menu->iconidx >= 0 && menu->iconidx < n_ids)
228 stock_id = menu_stock_ids[menu->iconidx];
229 else
230 stock_id = GTK_STOCK_MISSING_IMAGE;
231
232 image = gtk_image_new_from_stock(stock_id, icon_size);
233 }
234
235 return image;
236}
237
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000238 static gint
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000239toolbar_button_focus_in_event(GtkWidget *widget UNUSED,
240 GdkEventFocus *event UNUSED,
241 gpointer data UNUSED)
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000242{
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000243 /* When we're in a GtkPlug, we don't have window focus events, only widget
244 * focus. To emulate stand-alone gvim, if a button gets focus (e.g.,
245 * <Tab> into GtkPlug) immediately pass it to mainwin. */
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000246 if (gtk_socket_id != 0)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000247 gtk_widget_grab_focus(gui.drawarea);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000248
249 return TRUE;
250}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200251#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200253#if defined(FEAT_TOOLBAR) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254
255 void
256gui_gtk_register_stock_icons(void)
257{
258# include "../pixmaps/stock_icons.h"
259 GtkIconFactory *factory;
260
261 factory = gtk_icon_factory_new();
262# define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
263
264 ADD_ICON("vim-build-tags", stock_vim_build_tags);
265 ADD_ICON("vim-find-help", stock_vim_find_help);
266 ADD_ICON("vim-save-all", stock_vim_save_all);
267 ADD_ICON("vim-session-load", stock_vim_session_load);
268 ADD_ICON("vim-session-new", stock_vim_session_new);
269 ADD_ICON("vim-session-save", stock_vim_session_save);
270 ADD_ICON("vim-shell", stock_vim_shell);
271 ADD_ICON("vim-window-maximize", stock_vim_window_maximize);
272 ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
273 ADD_ICON("vim-window-minimize", stock_vim_window_minimize);
274 ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
275 ADD_ICON("vim-window-split", stock_vim_window_split);
276 ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
277
278# undef ADD_ICON
279 gtk_icon_factory_add_default(factory);
280 g_object_unref(factory);
281}
282
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200283#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286#if defined(FEAT_MENU) || defined(PROTO)
287
288/*
289 * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
290 * if necessary. The caller must vim_free() the returned string.
291 *
292 * Input Output
293 * _ __
294 * && &
295 * & _ stripped if use_mnemonic == FALSE
296 * <Tab> end of menu label text
297 */
298 static char_u *
299translate_mnemonic_tag(char_u *name, int use_mnemonic)
300{
301 char_u *buf;
302 char_u *psrc;
303 char_u *pdest;
304 int n_underscores = 0;
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 name = CONVERT_TO_UTF8(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307 if (name == NULL)
308 return NULL;
309
310 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
311 if (*psrc == '_')
312 ++n_underscores;
313
314 buf = alloc((unsigned)(psrc - name + n_underscores + 1));
315 if (buf != NULL)
316 {
317 pdest = buf;
318 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
319 {
320 if (*psrc == '_')
321 {
322 *pdest++ = '_';
323 *pdest++ = '_';
324 }
325 else if (*psrc != '&')
326 {
327 *pdest++ = *psrc;
328 }
329 else if (*(psrc + 1) == '&')
330 {
331 *pdest++ = *psrc++;
332 }
333 else if (use_mnemonic)
334 {
335 *pdest++ = '_';
336 }
337 }
338 *pdest = NUL;
339 }
340
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 CONVERT_TO_UTF8_FREE(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 return buf;
343}
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 static void
346menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
347{
348 GtkWidget *box;
349 char_u *text;
350 int use_mnemonic;
351
352 /* It would be neat to have image menu items, but that would require major
353 * changes to Vim's menu system. Not to mention that all the translations
354 * had to be updated. */
355 menu->id = gtk_menu_item_new();
356 box = gtk_hbox_new(FALSE, 20);
357
358 use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
359 text = translate_mnemonic_tag(menu->name, use_mnemonic);
360
361 menu->label = gtk_label_new_with_mnemonic((const char *)text);
362 vim_free(text);
363
364 gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
365
366 if (menu->actext != NULL && menu->actext[0] != NUL)
367 {
368 text = CONVERT_TO_UTF8(menu->actext);
369
370 gtk_box_pack_end(GTK_BOX(box),
371 gtk_label_new((const char *)text),
372 FALSE, FALSE, 0);
373
374 CONVERT_TO_UTF8_FREE(text);
375 }
376
377 gtk_container_add(GTK_CONTAINER(menu->id), box);
378 gtk_widget_show_all(menu->id);
379}
380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 void
382gui_mch_add_menu(vimmenu_T *menu, int idx)
383{
384 vimmenu_T *parent;
385 GtkWidget *parent_widget;
386
387 if (menu->name[0] == ']' || menu_is_popup(menu->name))
388 {
389 menu->submenu_id = gtk_menu_new();
390 return;
391 }
392
393 parent = menu->parent;
394
395 if ((parent != NULL && parent->submenu_id == NULL)
396 || !menu_is_menubar(menu->name))
397 return;
398
399 parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
400 menu_item_new(menu, parent_widget);
401
402 /* since the tearoff should always appear first, increment idx */
403 if (parent != NULL && !menu_is_popup(parent->name))
404 ++idx;
405
406 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 menu->submenu_id = gtk_menu_new();
409
410 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
411 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
412
413 menu->tearoff_handle = gtk_tearoff_menu_item_new();
414 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
415 gtk_widget_show(menu->tearoff_handle);
416 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
417}
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000420menu_item_activate(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 gui_menu_cb((vimmenu_T *)data);
423
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 /* make sure the menu action is taken immediately */
425 if (gtk_main_level() > 0)
426 gtk_main_quit();
427}
428
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 void
430gui_mch_add_menu_item(vimmenu_T *menu, int idx)
431{
432 vimmenu_T *parent;
433
434 parent = menu->parent;
435
436# ifdef FEAT_TOOLBAR
437 if (menu_is_toolbar(parent->name))
438 {
439 GtkToolbar *toolbar;
440
441 toolbar = GTK_TOOLBAR(gui.toolbar);
442 menu->submenu_id = NULL;
443
444 if (menu_is_separator(menu->name))
445 {
446 gtk_toolbar_insert_space(toolbar, idx);
447 menu->id = NULL;
448 }
449 else
450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 char_u *text;
452 char_u *tooltip;
453
454 text = CONVERT_TO_UTF8(menu->dname);
455 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000456 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
457 /* Invalid text, can happen when 'encoding' is changed. Avoid
458 * a nasty GTK error message, skip the tooltip. */
459 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
461 menu->id = gtk_toolbar_insert_item(
462 toolbar,
463 (const char *)text,
464 (const char *)tooltip,
465 NULL,
466 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
467 G_CALLBACK(&menu_item_activate),
468 menu,
469 idx);
470
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000471 if (gtk_socket_id != 0)
472 gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
473 GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000474
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 CONVERT_TO_UTF8_FREE(text);
476 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 }
478 }
479 else
480# endif /* FEAT_TOOLBAR */
481 {
482 /* No parent, must be a non-menubar menu */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000483 if (parent == NULL || parent->submenu_id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 return;
485
486 /* Make place for the possible tearoff handle item. Not in the popup
487 * menu, it doesn't have a tearoff item. */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000488 if (!menu_is_popup(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 ++idx;
490
491 if (menu_is_separator(menu->name))
492 {
493 /* Separator: Just add it */
494 menu->id = gtk_menu_item_new();
495 gtk_widget_set_sensitive(menu->id, FALSE);
496 gtk_widget_show(menu->id);
497 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
498
499 return;
500 }
501
502 /* Add textual menu item. */
503 menu_item_new(menu, parent->submenu_id);
504 gtk_widget_show(menu->id);
505 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
506
507 if (menu->id != NULL)
508 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
509 GTK_SIGNAL_FUNC(menu_item_activate), menu);
510 }
511}
512#endif /* FEAT_MENU */
513
514
515 void
516gui_mch_set_text_area_pos(int x, int y, int w, int h)
517{
518 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
519}
520
521
522#if defined(FEAT_MENU) || defined(PROTO)
523/*
Bram Moolenaar79166c42007-05-10 18:29:51 +0000524 * Enable or disable accelerators for the toplevel menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 */
526 void
527gui_gtk_set_mnemonics(int enable)
528{
529 vimmenu_T *menu;
530 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 for (menu = root_menu; menu != NULL; menu = menu->next)
533 {
534 if (menu->id == NULL)
535 continue;
536
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 name = translate_mnemonic_tag(menu->name, enable);
538 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
539 (const char *)name);
540 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
542}
543
544 static void
545recurse_tearoffs(vimmenu_T *menu, int val)
546{
547 for (; menu != NULL; menu = menu->next)
548 {
549 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
550 && menu->name[0] != ']' && !menu_is_popup(menu->name))
551 {
552 if (val)
553 gtk_widget_show(menu->tearoff_handle);
554 else
555 gtk_widget_hide(menu->tearoff_handle);
556 }
557 recurse_tearoffs(menu->children, val);
558 }
559}
560
561 void
562gui_mch_toggle_tearoffs(int enable)
563{
564 recurse_tearoffs(root_menu, enable);
565}
566#endif /* FEAT_MENU */
567
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200568#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 static int
570get_menu_position(vimmenu_T *menu)
571{
572 vimmenu_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000573 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575 for (node = menu->parent->children; node != menu; node = node->next)
576 {
577 g_return_val_if_fail(node != NULL, -1);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000578 ++idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580
Bram Moolenaar89d40322006-08-29 15:30:07 +0000581 return idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200583#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585
586#if defined(FEAT_TOOLBAR) || defined(PROTO)
587 void
588gui_mch_menu_set_tip(vimmenu_T *menu)
589{
590 if (menu->id != NULL && menu->parent != NULL
591 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
592 {
593 char_u *tooltip;
594
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000596 if (tooltip == NULL || utf_valid_string(tooltip, NULL))
597 /* Only set the tooltip when it's valid utf-8. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
599 menu->id, (const char *)tooltip, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602}
603#endif /* FEAT_TOOLBAR */
604
605
606#if defined(FEAT_MENU) || defined(PROTO)
607/*
608 * Destroy the machine specific menu widget.
609 */
610 void
611gui_mch_destroy_menu(vimmenu_T *menu)
612{
613# ifdef FEAT_TOOLBAR
614 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 if (menu_is_separator(menu->name))
617 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
618 get_menu_position(menu));
619 else if (menu->id != NULL)
620 gtk_widget_destroy(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 }
622 else
623# endif /* FEAT_TOOLBAR */
624 {
625 if (menu->submenu_id != NULL)
626 gtk_widget_destroy(menu->submenu_id);
627
628 if (menu->id != NULL)
629 gtk_widget_destroy(menu->id);
630 }
631
632 menu->submenu_id = NULL;
633 menu->id = NULL;
634}
635#endif /* FEAT_MENU */
636
637
638/*
639 * Scrollbar stuff.
640 */
641 void
642gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
643{
644 if (sb->id != NULL)
645 {
646 GtkAdjustment *adjustment;
647
648 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
649
650 adjustment->lower = 0.0;
651 adjustment->value = val;
652 adjustment->upper = max + 1;
653 adjustment->page_size = size;
654 adjustment->page_increment = size < 3L ? 1L : size - 2L;
655 adjustment->step_increment = 1.0;
656
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 g_signal_handler_block(GTK_OBJECT(adjustment),
658 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 gtk_adjustment_changed(adjustment);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 g_signal_handler_unblock(GTK_OBJECT(adjustment),
661 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663}
664
665 void
666gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
667{
668 if (sb->id != NULL)
669 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
670}
671
672/*
673 * Take action upon scrollbar dragging.
674 */
675 static void
676adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
677{
678 scrollbar_T *sb;
679 long value;
680 int dragging = FALSE;
681
682#ifdef FEAT_XIM
683 /* cancel any preediting */
684 if (im_is_preediting())
685 xim_reset();
686#endif
687
688 sb = gui_find_scrollbar((long)data);
689 value = (long)adjustment->value;
690 /*
691 * The dragging argument must be right for the scrollbar to work with
692 * closed folds. This isn't documented, hopefully this will keep on
693 * working in later GTK versions.
694 *
695 * FIXME: Well, it doesn't work in GTK2. :)
696 * HACK: Get the mouse pointer position, if it appears to be on an arrow
697 * button set "dragging" to FALSE. This assumes square buttons!
698 */
699 if (sb != NULL)
700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 dragging = TRUE;
702
703 if (sb->wp != NULL)
704 {
705 int x;
706 int y;
707 GdkModifierType state;
708 int width;
709 int height;
710
711 /* vertical scrollbar: need to set "dragging" properly in case
712 * there are closed folds. */
713 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
714 gdk_window_get_size(sb->id->window, &width, &height);
715 if (x >= 0 && x < width && y >= 0 && y < height)
716 {
717 if (y < width)
718 {
719 /* up arrow: move one (closed fold) line up */
720 dragging = FALSE;
721 value = sb->wp->w_topline - 2;
722 }
723 else if (y > height - width)
724 {
725 /* down arrow: move one (closed fold) line down */
726 dragging = FALSE;
727 value = sb->wp->w_topline;
728 }
729 }
730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 }
732
733 gui_drag_scrollbar(sb, value, dragging);
734
735 if (gtk_main_level() > 0)
736 gtk_main_quit();
737}
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 Moolenaar149e8072008-07-31 20:29:28 +0000783/* This has been disabled, because the GTK library rewrites
784 * ~/.recently-used.xbel every time the main loop is quit. For Vim that means
785 * on just about any event. */
786/* # define USE_FILE_CHOOSER */
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000789#ifndef USE_FILE_CHOOSER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000791browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792{
793 gui_T *vw = (gui_T *)cbdata;
794
795 if (vw->browse_fname != NULL)
796 g_free(vw->browse_fname);
797
798 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
799 GTK_FILE_SELECTION(vw->filedlg)));
800 gtk_widget_hide(vw->filedlg);
801 if (gtk_main_level() > 0)
802 gtk_main_quit();
803}
804
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000806browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807{
808 gui_T *vw = (gui_T *)cbdata;
809
810 if (vw->browse_fname != NULL)
811 {
812 g_free(vw->browse_fname);
813 vw->browse_fname = NULL;
814 }
815 gtk_widget_hide(vw->filedlg);
816 if (gtk_main_level() > 0)
817 gtk_main_quit();
818}
819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000821browse_destroy_cb(GtkWidget *widget UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
823 if (gui.browse_fname != NULL)
824 {
825 g_free(gui.browse_fname);
826 gui.browse_fname = NULL;
827 }
828 gui.filedlg = NULL;
829
830 if (gtk_main_level() > 0)
831 gtk_main_quit();
832
833 return FALSE;
834}
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836
837/*
838 * Put up a file requester.
839 * Returns the selected name in allocated memory, or NULL for Cancel.
840 * saving, select file to write
841 * title title for the window
842 * dflt default name
843 * ext not used (extension added)
844 * initdir initial directory, NULL for current dir
845 * filter not used (file name filter)
846 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000848gui_mch_browse(int saving UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 char_u *title,
850 char_u *dflt,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000851 char_u *ext UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 char_u *initdir,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000853 char_u *filter UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000855#ifdef USE_FILE_CHOOSER
856 GtkWidget *fc;
857#endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000858 char_u dirbuf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 title = CONVERT_TO_UTF8(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000862 /* GTK has a bug, it only works with an absolute path. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000863 if (initdir == NULL || *initdir == NUL)
864 mch_dirname(dirbuf, MAXPATHL);
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000865 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000866 dirbuf[0] = NUL;
867 /* Always need a trailing slash for a directory. */
868 add_pathsep(dirbuf);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000869
870 /* If our pointer is currently hidden, then we should show it. */
871 gui_mch_mousehide(FALSE);
872
873#ifdef USE_FILE_CHOOSER
874 /* We create the dialog each time, so that the button text can be "Open"
875 * or "Save" according to the action. */
876 fc = gtk_file_chooser_dialog_new((const gchar *)title,
877 GTK_WINDOW(gui.mainwin),
878 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
879 : GTK_FILE_CHOOSER_ACTION_OPEN,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000880 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
Bram Moolenaard1350622006-10-24 20:01:06 +0000881 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000882 NULL);
883 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
884 (const gchar *)dirbuf);
885
886 gui.browse_fname = NULL;
887 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000889 char *filename;
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000890
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000891 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
892 gui.browse_fname = (char_u *)g_strdup(filename);
893 g_free(filename);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000894 }
895 gtk_widget_destroy(GTK_WIDGET(fc));
896
897#else
898
899 if (gui.filedlg == NULL)
900 {
901 GtkFileSelection *fs; /* shortcut */
902
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 gui.filedlg = gtk_file_selection_new((const gchar *)title);
904 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
905 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000906 GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 fs = GTK_FILE_SELECTION(gui.filedlg);
908
909 gtk_container_border_width(GTK_CONTAINER(fs), 4);
910
911 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
912 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
913 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
914 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
915 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
916 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
917 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
918 GTK_OBJECT(gui.filedlg));
919 }
920 else
921 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
922
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000923 /* Concatenate "initdir" and "dflt". */
924 if (dflt != NULL && *dflt != NUL
925 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
926 STRCAT(dirbuf, dflt);
927
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
929 (const gchar *)dirbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
931 gtk_widget_show(gui.filedlg);
932 while (gui.filedlg && GTK_WIDGET_DRAWABLE(gui.filedlg))
933 gtk_main_iteration_do(TRUE);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000936 CONVERT_TO_UTF8_FREE(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 if (gui.browse_fname == NULL)
938 return NULL;
939
940 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000941 return vim_strsave(shorten_fname1(gui.browse_fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942}
943
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000944/*
945 * Put up a directory selector
946 * Returns the selected name in allocated memory, or NULL for Cancel.
947 * title title for the window
948 * dflt default name
949 * initdir initial directory, NULL for current dir
950 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000951 char_u *
952gui_mch_browsedir(
953 char_u *title,
954 char_u *initdir)
955{
956# if defined(GTK_FILE_CHOOSER) /* Only in GTK 2.4 and later. */
957 char_u dirbuf[MAXPATHL];
958 char_u *p;
959 GtkWidget *dirdlg; /* file selection dialog */
960 char_u *dirname = NULL;
961
962 title = CONVERT_TO_UTF8(title);
963
964 dirdlg = gtk_file_chooser_dialog_new(
965 (const gchar *)title,
966 GTK_WINDOW(gui.mainwin),
967 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
968 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
969 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
970 NULL);
971
972 CONVERT_TO_UTF8_FREE(title);
973
974 /* if our pointer is currently hidden, then we should show it. */
975 gui_mch_mousehide(FALSE);
976
977 /* GTK appears to insist on an absolute path. */
978 if (initdir == NULL || *initdir == NUL
979 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
980 mch_dirname(dirbuf, MAXPATHL - 10);
981
982 /* Always need a trailing slash for a directory.
983 * Also add a dummy file name, so that we get to the directory. */
984 add_pathsep(dirbuf);
985 STRCAT(dirbuf, "@zd(*&1|");
986 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
987 (const gchar *)dirbuf);
988
989 /* Run the dialog. */
990 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
991 dirname = (char_u *)gtk_file_chooser_get_filename(
992 GTK_FILE_CHOOSER(dirdlg));
993 gtk_widget_destroy(dirdlg);
994 if (dirname == NULL)
995 return NULL;
996
997 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000998 p = vim_strsave(shorten_fname1(dirname));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000999 g_free(dirname);
1000 return p;
1001
1002# else
1003 /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
1004 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
1005# endif
1006}
1007
Bram Moolenaara7fc0102005-05-18 22:17:12 +00001008
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009#endif /* FEAT_BROWSE */
1010
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001011#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
1013 static GtkWidget *
1014create_message_dialog(int type, char_u *title, char_u *message)
1015{
1016 GtkWidget *dialog;
1017 GtkMessageType message_type;
1018
1019 switch (type)
1020 {
1021 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1022 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1023 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1024 default: message_type = GTK_MESSAGE_INFO; break;
1025 }
1026
1027 message = CONVERT_TO_UTF8(message);
1028 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1029 GTK_DIALOG_DESTROY_WITH_PARENT,
1030 message_type,
1031 GTK_BUTTONS_NONE,
1032 "%s", (const char *)message);
1033 CONVERT_TO_UTF8_FREE(message);
1034
1035 if (title != NULL)
1036 {
1037 title = CONVERT_TO_UTF8(title);
1038 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1039 CONVERT_TO_UTF8_FREE(title);
1040 }
1041 else if (type == VIM_GENERIC)
1042 {
1043 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1044 }
1045
1046 return dialog;
1047}
1048
1049/*
1050 * Split up button_string into individual button labels by inserting
1051 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1052 * '&' with '_'. button_string must point to allocated memory!
1053 * Return an allocated array of pointers into button_string.
1054 */
1055 static char **
1056split_button_string(char_u *button_string, int *n_buttons)
1057{
1058 char **array;
1059 char_u *p;
1060 unsigned int count = 1;
1061
1062 for (p = button_string; *p != NUL; ++p)
1063 if (*p == DLG_BUTTON_SEP)
1064 ++count;
1065
1066 array = (char **)alloc((count + 1) * sizeof(char *));
1067 count = 0;
1068
1069 if (array != NULL)
1070 {
1071 array[count++] = (char *)button_string;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001072 for (p = button_string; *p != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 {
1074 if (*p == DLG_BUTTON_SEP)
1075 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001076 *p++ = NUL;
1077 array[count++] = (char *)p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 }
1079 else if (*p == DLG_HOTKEY_CHAR)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001080 *p++ = '_';
1081 else
1082 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
1084 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1085 }
1086
1087 *n_buttons = count;
1088 return array;
1089}
1090
1091 static char **
1092split_button_translation(const char *message)
1093{
1094 char **buttons = NULL;
1095 char_u *str;
1096 int n_buttons = 0;
1097 int n_expected = 1;
1098
1099 for (str = (char_u *)message; *str != NUL; ++str)
1100 if (*str == DLG_BUTTON_SEP)
1101 ++n_expected;
1102
1103 str = (char_u *)_(message);
1104 if (str != NULL)
1105 {
1106 if (output_conv.vc_type != CONV_NONE)
1107 str = string_convert(&output_conv, str, NULL);
1108 else
1109 str = vim_strsave(str);
1110
1111 if (str != NULL)
1112 buttons = split_button_string(str, &n_buttons);
1113 }
1114 /*
1115 * Uh-oh... this should never ever happen. But we don't wanna crash
1116 * if the translation is broken, thus fall back to the untranslated
1117 * buttons string in case of emergency.
1118 */
1119 if (buttons == NULL || n_buttons != n_expected)
1120 {
1121 vim_free(buttons);
1122 vim_free(str);
1123 buttons = NULL;
1124 str = vim_strsave((char_u *)message);
1125
1126 if (str != NULL)
1127 buttons = split_button_string(str, &n_buttons);
1128 if (buttons == NULL)
1129 vim_free(str);
1130 }
1131
1132 return buttons;
1133}
1134
1135 static int
1136button_equal(const char *a, const char *b)
1137{
1138 while (*a != '\0' && *b != '\0')
1139 {
1140 if (*a == '_' && *++a == '\0')
1141 break;
1142 if (*b == '_' && *++b == '\0')
1143 break;
1144
1145 if (g_unichar_tolower(g_utf8_get_char(a))
1146 != g_unichar_tolower(g_utf8_get_char(b)))
1147 return FALSE;
1148
1149 a = g_utf8_next_char(a);
1150 b = g_utf8_next_char(b);
1151 }
1152
1153 return (*a == '\0' && *b == '\0');
1154}
1155
1156 static void
1157dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1158{
1159 char **ok;
1160 char **ync; /* "yes no cancel" */
1161 char **buttons;
1162 int n_buttons = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001163 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164
1165 button_string = vim_strsave(button_string); /* must be writable */
1166 if (button_string == NULL)
1167 return;
1168
1169 /* Check 'v' flag in 'guioptions': vertical button placement. */
1170 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1171 {
1172 GtkWidget *vbutton_box;
1173
1174 vbutton_box = gtk_vbutton_box_new();
1175 gtk_widget_show(vbutton_box);
1176 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1177 vbutton_box, TRUE, FALSE, 0);
1178 /* Overrule the "action_area" value, hopefully this works... */
1179 GTK_DIALOG(dialog)->action_area = vbutton_box;
1180 }
1181
1182 /*
1183 * Yes this is ugly, I don't particularly like it either. But doing it
1184 * this way has the compelling advantage that translations need not to
1185 * be touched at all. See below what 'ok' and 'ync' are used for.
1186 */
1187 ok = split_button_translation(N_("&Ok"));
1188 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1189 buttons = split_button_string(button_string, &n_buttons);
1190
1191 /*
1192 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1193 * environment. Don't hit me -- it's all about consistency.
1194 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1195 * other way around...
1196 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001197 for (idx = 1; idx <= n_buttons; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 {
1199 char *label;
1200 char_u *label8;
1201
Bram Moolenaar89d40322006-08-29 15:30:07 +00001202 label = buttons[idx - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 /*
1204 * Perform some guesswork to find appropriate stock items for the
1205 * buttons. We have to compare with a sample of the translated
1206 * button string to get things right. Yes, this is hackish :/
1207 *
1208 * But even the common button labels aren't necessarily translated,
1209 * since anyone can create their own dialogs using Vim functions.
1210 * Thus we have to check for those too.
1211 */
1212 if (ok != NULL && ync != NULL) /* almost impossible to fail */
1213 {
1214 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
1215 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
1216 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
1217 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
1218 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
1219 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
1220 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
1221 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
1222 }
1223 label8 = CONVERT_TO_UTF8((char_u *)label);
Bram Moolenaar89d40322006-08-29 15:30:07 +00001224 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 CONVERT_TO_UTF8_FREE(label8);
1226 }
1227
1228 if (ok != NULL)
1229 vim_free(*ok);
1230 if (ync != NULL)
1231 vim_free(*ync);
1232 vim_free(ok);
1233 vim_free(ync);
1234 vim_free(buttons);
1235 vim_free(button_string);
1236}
1237
1238/*
1239 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1240 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
1241 * GUI used to work this way, and I consider the impact on UI consistency
1242 * low enough to justify implementing this as a special Vim feature.
1243 */
1244typedef struct _DialogInfo
1245{
1246 int ignore_enter; /* no default button, ignore "Enter" */
1247 int noalt; /* accept accelerators without Alt */
1248 GtkDialog *dialog; /* Widget of the dialog */
1249} DialogInfo;
1250
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 static gboolean
1252dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1253{
1254 DialogInfo *di = (DialogInfo *)data;
1255
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001256 /* Ignore hitting Enter (or Space) when there is no default button. */
1257 if (di->ignore_enter && (event->keyval == GDK_Return
1258 || event->keyval == ' '))
1259 return TRUE;
1260 else /* A different key was pressed, return to normal behavior */
1261 di->ignore_enter = FALSE;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /* Close the dialog when hitting "Esc". */
1264 if (event->keyval == GDK_Escape)
1265 {
1266 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1267 return TRUE;
1268 }
1269
1270 if (di->noalt
1271 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1272 {
1273 return gtk_window_mnemonic_activate(
1274 GTK_WINDOW(widget), event->keyval,
1275 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1276 }
1277
1278 return FALSE; /* continue emission */
1279}
1280
1281 int
1282gui_mch_dialog(int type, /* type of dialog */
1283 char_u *title, /* title of dialog */
1284 char_u *message, /* message text */
1285 char_u *buttons, /* names of buttons */
1286 int def_but, /* default button */
1287 char_u *textfield) /* text for textfield or NULL */
1288{
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
1306 text = CONVERT_TO_UTF8(textfield);
1307 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1308 CONVERT_TO_UTF8_FREE(text);
1309
1310 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1311 (float)1.0, (float)1.0);
1312 gtk_container_add(GTK_CONTAINER(alignment), entry);
1313 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1314 gtk_widget_show(alignment);
1315
1316 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1317 alignment, TRUE, FALSE, 0);
1318 dialoginfo.noalt = FALSE;
1319 }
1320 else
1321 dialoginfo.noalt = TRUE;
1322
1323 /* Allow activation of mnemonic accelerators without pressing <Alt> when
Bram Moolenaar14716812006-05-04 21:54:08 +00001324 * there is no textfield. Handle pressing Esc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 g_signal_connect(G_OBJECT(dialog), "key_press_event",
1326 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1327
1328 if (def_but > 0)
1329 {
1330 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1331 dialoginfo.ignore_enter = FALSE;
1332 }
1333 else
1334 /* No default button, ignore pressing Enter. */
1335 dialoginfo.ignore_enter = TRUE;
1336
1337 /* Show the mouse pointer if it's currently hidden. */
1338 gui_mch_mousehide(FALSE);
1339
1340 response = gtk_dialog_run(GTK_DIALOG(dialog));
1341
1342 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
1343 if (response != GTK_RESPONSE_NONE)
1344 {
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001345 if (response == GTK_RESPONSE_ACCEPT) /* Enter pressed */
1346 response = def_but;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 if (textfield != NULL)
1348 {
1349 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1350 text = CONVERT_FROM_UTF8(text);
1351
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001352 vim_strncpy(textfield, text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 CONVERT_FROM_UTF8_FREE(text);
1355 }
1356 gtk_widget_destroy(dialog);
1357 }
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 return response > 0 ? response : 0;
1360}
1361
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001362#endif /* FEAT_GUI_DIALOG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364
1365#if defined(FEAT_MENU) || defined(PROTO)
1366
1367 void
1368gui_mch_show_popupmenu(vimmenu_T *menu)
1369{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001370# if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 /*
1372 * Append a submenu for selecting an input method. This is
1373 * currently the only way to switch input methods at runtime.
1374 */
1375 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1376 "vim-has-im-menu") == NULL)
1377 {
1378 GtkWidget *menuitem;
1379 GtkWidget *submenu;
1380 char_u *name;
1381
1382 menuitem = gtk_separator_menu_item_new();
1383 gtk_widget_show(menuitem);
1384 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1385
1386 name = (char_u *)_("Input _Methods");
1387 name = CONVERT_TO_UTF8(name);
1388 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1389 CONVERT_TO_UTF8_FREE(name);
1390 gtk_widget_show(menuitem);
1391
1392 submenu = gtk_menu_new();
1393 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1394 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1395
1396 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1397 GTK_MENU_SHELL(submenu));
1398 g_object_set_data(G_OBJECT(menu->submenu_id),
1399 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1400 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001401# endif /* FEAT_XIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402
1403 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1404 NULL, NULL,
1405 (GtkMenuPositionFunc)NULL, NULL,
1406 3U, (guint32)GDK_CURRENT_TIME);
1407}
1408
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001409/* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1410 * popup_menu_position_func(). */
1411static int popup_mouse_pos;
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413/*
1414 * Menu position callback; used by gui_make_popup() to place the menu
1415 * at the current text cursor position.
1416 *
1417 * Note: The push_in output argument seems to affect scrolling of huge
1418 * menus that don't fit on the screen. Leave it at the default for now.
1419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001421popup_menu_position_func(GtkMenu *menu UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 gint *x, gint *y,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001423 gboolean *push_in UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001424 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001426 gdk_window_get_origin(gui.drawarea->window, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001428 if (popup_mouse_pos)
1429 {
1430 int mx, my;
1431
1432 gui_mch_getmouse(&mx, &my);
1433 *x += mx;
1434 *y += my;
1435 }
1436 else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
1437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 /* Find the cursor position in the current window */
1439 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
1440 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1441 }
1442}
1443
1444 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001445gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 vimmenu_T *menu;
1448
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001449 popup_mouse_pos = mouse_pos;
1450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 menu = gui_find_menu(path_name);
1452
1453 if (menu != NULL && menu->submenu_id != NULL)
1454 {
1455 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1456 NULL, NULL,
1457 &popup_menu_position_func, NULL,
1458 0U, (guint32)GDK_CURRENT_TIME);
1459 }
1460}
1461
1462#endif /* FEAT_MENU */
1463
1464
1465/*
1466 * We don't create it twice.
1467 */
1468
1469typedef struct _SharedFindReplace
1470{
1471 GtkWidget *dialog; /* the main dialog widget */
1472 GtkWidget *wword; /* 'Whole word only' check button */
1473 GtkWidget *mcase; /* 'Match case' check button */
1474 GtkWidget *up; /* search direction 'Up' radio button */
1475 GtkWidget *down; /* search direction 'Down' radio button */
1476 GtkWidget *what; /* 'Find what' entry text widget */
1477 GtkWidget *with; /* 'Replace with' entry text widget */
1478 GtkWidget *find; /* 'Find Next' action button */
1479 GtkWidget *replace; /* 'Replace With' action button */
1480 GtkWidget *all; /* 'Replace All' action button */
1481} SharedFindReplace;
1482
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001483static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1484static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 static int
1487find_key_press_event(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001488 GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 GdkEventKey *event,
1490 SharedFindReplace *frdp)
1491{
1492 /* If the user is holding one of the key modifiers we will just bail out,
1493 * thus preserving the possibility of normal focus traversal.
1494 */
1495 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
1496 return FALSE;
1497
1498 /* the Escape key synthesizes a cancellation action */
1499 if (event->keyval == GDK_Escape)
1500 {
1501 gtk_widget_hide(frdp->dialog);
1502
1503 return TRUE;
1504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar79166c42007-05-10 18:29:51 +00001506 /* It would be delightful if it where possible to do search history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 * operations on the K_UP and K_DOWN keys here.
1508 */
1509
1510 return FALSE;
1511}
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 static GtkWidget *
1514create_image_button(const char *stock_id, const char *label)
1515{
1516 char_u *text;
1517 GtkWidget *box;
1518 GtkWidget *alignment;
1519 GtkWidget *button;
1520
1521 text = CONVERT_TO_UTF8((char_u *)label);
1522
1523 box = gtk_hbox_new(FALSE, 3);
1524 gtk_box_pack_start(GTK_BOX(box),
1525 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
1526 FALSE, FALSE, 0);
1527 gtk_box_pack_start(GTK_BOX(box),
1528 gtk_label_new((const char *)text),
1529 FALSE, FALSE, 0);
1530
1531 CONVERT_TO_UTF8_FREE(text);
1532
1533 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1534 (float)0.0, (float)0.0);
1535 gtk_container_add(GTK_CONTAINER(alignment), box);
1536 gtk_widget_show_all(alignment);
1537
1538 button = gtk_button_new();
1539 gtk_container_add(GTK_CONTAINER(button), alignment);
1540
1541 return button;
1542}
1543
1544/*
1545 * This is currently only used by find_replace_dialog_create(), and
1546 * I'd really like to keep it at that. In other words: don't spread
1547 * this nasty hack all over the code. Think twice.
1548 */
1549 static const char *
1550convert_localized_message(char_u **buffer, const char *message)
1551{
1552 if (output_conv.vc_type == CONV_NONE)
1553 return message;
1554
1555 vim_free(*buffer);
1556 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
1557
1558 return (const char *)*buffer;
1559}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560
1561 static void
1562find_replace_dialog_create(char_u *arg, int do_replace)
1563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 GtkWidget *hbox; /* main top down box */
1565 GtkWidget *actionarea;
1566 GtkWidget *table;
1567 GtkWidget *tmp;
1568 GtkWidget *vbox;
1569 gboolean sensitive;
1570 SharedFindReplace *frdp;
1571 char_u *entry_text;
1572 int wword = FALSE;
1573 int mcase = !p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 char_u *conv_buffer = NULL;
1575# define CONV(message) convert_localized_message(&conv_buffer, (message))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
1578
1579 /* Get the search string to use. */
1580 entry_text = get_find_dialog_text(arg, &wword, &mcase);
1581
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
1583 {
1584 char_u *old_text = entry_text;
1585 entry_text = string_convert(&output_conv, entry_text, NULL);
1586 vim_free(old_text);
1587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
1589 /*
1590 * If the dialog already exists, just raise it.
1591 */
1592 if (frdp->dialog)
1593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (entry_text != NULL)
1595 {
1596 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1597 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1598 (gboolean)wword);
1599 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1600 (gboolean)mcase);
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 gtk_window_present(GTK_WINDOW(frdp->dialog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 vim_free(entry_text);
1604 return;
1605 }
1606
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 frdp->dialog = gtk_dialog_new();
1608 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
1609 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
1610 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 if (do_replace)
1613 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1615 CONV(_("VIM - Search and Replace...")));
1616 }
1617 else
1618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1620 CONV(_("VIM - Search...")));
1621 }
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 hbox = gtk_hbox_new(FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
1625 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626
1627 if (do_replace)
1628 table = gtk_table_new(1024, 4, FALSE);
1629 else
1630 table = gtk_table_new(1024, 3, FALSE);
1631 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
1632 gtk_container_border_width(GTK_CONTAINER(table), 4);
1633
1634 tmp = gtk_label_new(CONV(_("Find what:")));
1635 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1636 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
1637 GTK_FILL, GTK_EXPAND, 2, 2);
1638 frdp->what = gtk_entry_new();
1639 sensitive = (entry_text != NULL && entry_text[0] != NUL);
1640 if (entry_text != NULL)
1641 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1642 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
1643 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
1644 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
1645 GTK_SIGNAL_FUNC(find_key_press_event),
1646 (gpointer) frdp);
1647 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
1648 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1649
1650 if (do_replace)
1651 {
1652 tmp = gtk_label_new(CONV(_("Replace with:")));
1653 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1654 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
1655 GTK_FILL, GTK_EXPAND, 2, 2);
1656 frdp->with = gtk_entry_new();
1657 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
1658 GTK_SIGNAL_FUNC(find_replace_cb),
1659 GINT_TO_POINTER(FRD_R_FINDNEXT));
1660 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
1661 GTK_SIGNAL_FUNC(find_key_press_event),
1662 (gpointer) frdp);
1663 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
1664 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1665
1666 /*
1667 * Make the entry activation only change the input focus onto the
1668 * with item.
1669 */
1670 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1671 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
1672 }
1673 else
1674 {
1675 /*
1676 * Make the entry activation do the search.
1677 */
1678 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1679 GTK_SIGNAL_FUNC(find_replace_cb),
1680 GINT_TO_POINTER(FRD_FINDNEXT));
1681 }
1682
1683 /* whole word only button */
1684 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
1685 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1686 (gboolean)wword);
1687 if (do_replace)
1688 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
1689 GTK_FILL, GTK_EXPAND, 2, 2);
1690 else
1691 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
1692 GTK_FILL, GTK_EXPAND, 2, 2);
1693
1694 /* match case button */
1695 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
1696 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1697 (gboolean)mcase);
1698 if (do_replace)
1699 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
1700 GTK_FILL, GTK_EXPAND, 2, 2);
1701 else
1702 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
1703 GTK_FILL, GTK_EXPAND, 2, 2);
1704
1705 tmp = gtk_frame_new(CONV(_("Direction")));
1706 if (do_replace)
1707 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
1708 GTK_FILL, GTK_FILL, 2, 2);
1709 else
1710 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
1711 GTK_FILL, GTK_FILL, 2, 2);
1712 vbox = gtk_vbox_new(FALSE, 0);
1713 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
1714 gtk_container_add(GTK_CONTAINER(tmp), vbox);
1715
1716 /* 'Up' and 'Down' buttons */
1717 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
1718 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
1719 frdp->down = gtk_radio_button_new_with_label(
1720 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
1721 CONV(_("Down")));
1722 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
1725
1726 /* vbox to hold the action buttons */
1727 actionarea = gtk_vbutton_box_new();
1728 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
1730
1731 /* 'Find Next' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 gtk_widget_set_sensitive(frdp->find, sensitive);
1734
1735 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
1736 GTK_SIGNAL_FUNC(find_replace_cb),
1737 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
1738 : GINT_TO_POINTER(FRD_FINDNEXT));
1739
1740 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
1741 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
1742 gtk_widget_grab_default(frdp->find);
1743
1744 if (do_replace)
1745 {
1746 /* 'Replace' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 gtk_widget_set_sensitive(frdp->replace, sensitive);
1749 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
1750 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
1751 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
1752 GTK_SIGNAL_FUNC(find_replace_cb),
1753 GINT_TO_POINTER(FRD_REPLACE));
1754
1755 /* 'Replace All' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 gtk_widget_set_sensitive(frdp->all, sensitive);
1758 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
1759 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
1760 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
1761 GTK_SIGNAL_FUNC(find_replace_cb),
1762 GINT_TO_POINTER(FRD_REPLACEALL));
1763 }
1764
1765 /* 'Cancel' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
1768 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
1769 gtk_signal_connect_object(GTK_OBJECT(tmp),
1770 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
1771 GTK_OBJECT(frdp->dialog));
1772 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
1773 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1774 GTK_OBJECT(frdp->dialog));
1775
1776 tmp = gtk_vseparator_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 /* Suppress automatic show of the unused action area */
1780 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
1781 gtk_widget_show_all(hbox);
1782 gtk_widget_show(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
1784 vim_free(entry_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 vim_free(conv_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#undef CONV
1787}
1788
1789 void
1790gui_mch_find_dialog(exarg_T *eap)
1791{
1792 if (gui.in_use)
1793 find_replace_dialog_create(eap->arg, FALSE);
1794}
1795
1796 void
1797gui_mch_replace_dialog(exarg_T *eap)
1798{
1799 if (gui.in_use)
1800 find_replace_dialog_create(eap->arg, TRUE);
1801}
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803/*
1804 * Callback for actions of the find and replace dialogs
1805 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001807find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808{
1809 int flags;
1810 char_u *find_text;
1811 char_u *repl_text;
1812 gboolean direction_down;
1813 SharedFindReplace *sfr;
1814 int rc;
1815
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 Moolenaar071d4272004-06-13 20:20:40 +00001840 rc = 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 if (rc && gtk_main_level() > 0)
1845 gtk_main_quit(); /* make sure cmd will be handled immediately */
1846}
1847
1848/* our usual callback function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001850entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
1852 gtk_widget_grab_focus(GTK_WIDGET(data));
1853}
1854
1855/*
1856 * Syncing the find/replace dialogs on the fly is utterly useless crack,
1857 * and causes nothing but problems. Please tell me a use case for which
1858 * you'd need both a find dialog and a find/replace one at the same time,
1859 * without being able to actually use them separately since they're syncing
1860 * all the time. I don't think it's worthwhile to fix this nonsense,
1861 * particularly evil incarnation of braindeadness, whatever; I'd much rather
1862 * see it extinguished from this planet. Thanks for listening. Sorry.
1863 */
1864 static void
1865entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
1866{
1867 const gchar *entry_text;
1868 gboolean nonempty;
1869
1870 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
1871
1872 if (!entry_text)
1873 return; /* shouldn't happen */
1874
1875 nonempty = (entry_text[0] != '\0');
1876
1877 if (dialog == find_widgets.dialog)
1878 {
1879 gtk_widget_set_sensitive(find_widgets.find, nonempty);
1880 }
1881
1882 if (dialog == repl_widgets.dialog)
1883 {
1884 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
1885 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
1886 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
1887 }
1888}
1889
1890/*
1891 * ":helpfind"
1892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 void
1894ex_helpfind(eap)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001895 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 /* This will fail when menus are not loaded. Well, it's only for
1898 * backwards compatibility anyway. */
1899 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
1900}