blob: 50079b406aa8dff735fae1c34b34f34498fcd072 [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);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423}
424
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 void
426gui_mch_add_menu_item(vimmenu_T *menu, int idx)
427{
428 vimmenu_T *parent;
429
430 parent = menu->parent;
431
432# ifdef FEAT_TOOLBAR
433 if (menu_is_toolbar(parent->name))
434 {
435 GtkToolbar *toolbar;
436
437 toolbar = GTK_TOOLBAR(gui.toolbar);
438 menu->submenu_id = NULL;
439
440 if (menu_is_separator(menu->name))
441 {
442 gtk_toolbar_insert_space(toolbar, idx);
443 menu->id = NULL;
444 }
445 else
446 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 char_u *text;
448 char_u *tooltip;
449
450 text = CONVERT_TO_UTF8(menu->dname);
451 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000452 if (tooltip != NULL && !utf_valid_string(tooltip, NULL))
453 /* Invalid text, can happen when 'encoding' is changed. Avoid
454 * a nasty GTK error message, skip the tooltip. */
455 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456
457 menu->id = gtk_toolbar_insert_item(
458 toolbar,
459 (const char *)text,
460 (const char *)tooltip,
461 NULL,
462 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
463 G_CALLBACK(&menu_item_activate),
464 menu,
465 idx);
466
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000467 if (gtk_socket_id != 0)
468 gtk_signal_connect(GTK_OBJECT(menu->id), "focus_in_event",
469 GTK_SIGNAL_FUNC(toolbar_button_focus_in_event), NULL);
Bram Moolenaar9b2200a2006-03-20 21:55:45 +0000470
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471 CONVERT_TO_UTF8_FREE(text);
472 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 }
474 }
475 else
476# endif /* FEAT_TOOLBAR */
477 {
478 /* No parent, must be a non-menubar menu */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000479 if (parent == NULL || parent->submenu_id == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 return;
481
482 /* Make place for the possible tearoff handle item. Not in the popup
483 * menu, it doesn't have a tearoff item. */
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000484 if (!menu_is_popup(parent->name))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 ++idx;
486
487 if (menu_is_separator(menu->name))
488 {
489 /* Separator: Just add it */
490 menu->id = gtk_menu_item_new();
491 gtk_widget_set_sensitive(menu->id, FALSE);
492 gtk_widget_show(menu->id);
493 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
494
495 return;
496 }
497
498 /* Add textual menu item. */
499 menu_item_new(menu, parent->submenu_id);
500 gtk_widget_show(menu->id);
501 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
502
503 if (menu->id != NULL)
504 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
505 GTK_SIGNAL_FUNC(menu_item_activate), menu);
506 }
507}
508#endif /* FEAT_MENU */
509
510
511 void
512gui_mch_set_text_area_pos(int x, int y, int w, int h)
513{
514 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
515}
516
517
518#if defined(FEAT_MENU) || defined(PROTO)
519/*
Bram Moolenaar79166c42007-05-10 18:29:51 +0000520 * Enable or disable accelerators for the toplevel menus.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
522 void
523gui_gtk_set_mnemonics(int enable)
524{
525 vimmenu_T *menu;
526 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527
528 for (menu = root_menu; menu != NULL; menu = menu->next)
529 {
530 if (menu->id == NULL)
531 continue;
532
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 name = translate_mnemonic_tag(menu->name, enable);
534 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
535 (const char *)name);
536 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 }
538}
539
540 static void
541recurse_tearoffs(vimmenu_T *menu, int val)
542{
543 for (; menu != NULL; menu = menu->next)
544 {
545 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
546 && menu->name[0] != ']' && !menu_is_popup(menu->name))
547 {
548 if (val)
549 gtk_widget_show(menu->tearoff_handle);
550 else
551 gtk_widget_hide(menu->tearoff_handle);
552 }
553 recurse_tearoffs(menu->children, val);
554 }
555}
556
557 void
558gui_mch_toggle_tearoffs(int enable)
559{
560 recurse_tearoffs(root_menu, enable);
561}
562#endif /* FEAT_MENU */
563
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200564#if defined(FEAT_TOOLBAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 static int
566get_menu_position(vimmenu_T *menu)
567{
568 vimmenu_T *node;
Bram Moolenaar89d40322006-08-29 15:30:07 +0000569 int idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570
571 for (node = menu->parent->children; node != menu; node = node->next)
572 {
573 g_return_val_if_fail(node != NULL, -1);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000574 ++idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 }
576
Bram Moolenaar89d40322006-08-29 15:30:07 +0000577 return idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578}
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200579#endif /* FEAT_TOOLBAR */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580
581
582#if defined(FEAT_TOOLBAR) || defined(PROTO)
583 void
584gui_mch_menu_set_tip(vimmenu_T *menu)
585{
586 if (menu->id != NULL && menu->parent != NULL
587 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
588 {
589 char_u *tooltip;
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +0000592 if (tooltip == NULL || utf_valid_string(tooltip, NULL))
593 /* Only set the tooltip when it's valid utf-8. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
595 menu->id, (const char *)tooltip, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 CONVERT_TO_UTF8_FREE(tooltip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598}
599#endif /* FEAT_TOOLBAR */
600
601
602#if defined(FEAT_MENU) || defined(PROTO)
603/*
604 * Destroy the machine specific menu widget.
605 */
606 void
607gui_mch_destroy_menu(vimmenu_T *menu)
608{
609# ifdef FEAT_TOOLBAR
610 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 if (menu_is_separator(menu->name))
613 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
614 get_menu_position(menu));
615 else if (menu->id != NULL)
616 gtk_widget_destroy(menu->id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
618 else
619# endif /* FEAT_TOOLBAR */
620 {
621 if (menu->submenu_id != NULL)
622 gtk_widget_destroy(menu->submenu_id);
623
624 if (menu->id != NULL)
625 gtk_widget_destroy(menu->id);
626 }
627
628 menu->submenu_id = NULL;
629 menu->id = NULL;
630}
631#endif /* FEAT_MENU */
632
633
634/*
635 * Scrollbar stuff.
636 */
637 void
638gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
639{
640 if (sb->id != NULL)
641 {
642 GtkAdjustment *adjustment;
643
644 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
645
646 adjustment->lower = 0.0;
647 adjustment->value = val;
648 adjustment->upper = max + 1;
649 adjustment->page_size = size;
650 adjustment->page_increment = size < 3L ? 1L : size - 2L;
651 adjustment->step_increment = 1.0;
652
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 g_signal_handler_block(GTK_OBJECT(adjustment),
654 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 gtk_adjustment_changed(adjustment);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 g_signal_handler_unblock(GTK_OBJECT(adjustment),
657 (gulong)sb->handler_id);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659}
660
661 void
662gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
663{
664 if (sb->id != NULL)
665 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
666}
667
668/*
669 * Take action upon scrollbar dragging.
670 */
671 static void
672adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
673{
674 scrollbar_T *sb;
675 long value;
676 int dragging = FALSE;
677
678#ifdef FEAT_XIM
679 /* cancel any preediting */
680 if (im_is_preediting())
681 xim_reset();
682#endif
683
684 sb = gui_find_scrollbar((long)data);
685 value = (long)adjustment->value;
686 /*
687 * The dragging argument must be right for the scrollbar to work with
688 * closed folds. This isn't documented, hopefully this will keep on
689 * working in later GTK versions.
690 *
691 * FIXME: Well, it doesn't work in GTK2. :)
692 * HACK: Get the mouse pointer position, if it appears to be on an arrow
693 * button set "dragging" to FALSE. This assumes square buttons!
694 */
695 if (sb != NULL)
696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 dragging = TRUE;
698
699 if (sb->wp != NULL)
700 {
701 int x;
702 int y;
703 GdkModifierType state;
704 int width;
705 int height;
706
707 /* vertical scrollbar: need to set "dragging" properly in case
708 * there are closed folds. */
709 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
710 gdk_window_get_size(sb->id->window, &width, &height);
711 if (x >= 0 && x < width && y >= 0 && y < height)
712 {
713 if (y < width)
714 {
715 /* up arrow: move one (closed fold) line up */
716 dragging = FALSE;
717 value = sb->wp->w_topline - 2;
718 }
719 else if (y > height - width)
720 {
721 /* down arrow: move one (closed fold) line down */
722 dragging = FALSE;
723 value = sb->wp->w_topline;
724 }
725 }
726 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 }
728
729 gui_drag_scrollbar(sb, value, dragging);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730}
731
732/* SBAR_VERT or SBAR_HORIZ */
733 void
734gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
735{
736 if (orient == SBAR_HORIZ)
737 sb->id = gtk_hscrollbar_new(NULL);
738 else if (orient == SBAR_VERT)
739 sb->id = gtk_vscrollbar_new(NULL);
740
741 if (sb->id != NULL)
742 {
743 GtkAdjustment *adjustment;
744
745 GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
746 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
747
748 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
749
750 sb->handler_id = gtk_signal_connect(
751 GTK_OBJECT(adjustment), "value_changed",
752 GTK_SIGNAL_FUNC(adjustment_value_changed),
753 GINT_TO_POINTER(sb->ident));
754 gui_mch_update();
755 }
756}
757
758#if defined(FEAT_WINDOWS) || defined(PROTO)
759 void
760gui_mch_destroy_scrollbar(scrollbar_T *sb)
761{
762 if (sb->id != NULL)
763 {
764 gtk_widget_destroy(sb->id);
765 sb->id = NULL;
766 }
767 gui_mch_update();
768}
769#endif
770
771#if defined(FEAT_BROWSE) || defined(PROTO)
772/*
773 * Implementation of the file selector related stuff
774 */
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200775#if GTK_CHECK_VERSION(2,4,0)
Bram Moolenaara3f41662010-07-11 19:01:06 +0200776# define USE_FILE_CHOOSER
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000779#ifndef USE_FILE_CHOOSER
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000781browse_ok_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 gui_T *vw = (gui_T *)cbdata;
784
785 if (vw->browse_fname != NULL)
786 g_free(vw->browse_fname);
787
788 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
789 GTK_FILE_SELECTION(vw->filedlg)));
790 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791}
792
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000794browse_cancel_cb(GtkWidget *widget UNUSED, gpointer cbdata)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795{
796 gui_T *vw = (gui_T *)cbdata;
797
798 if (vw->browse_fname != NULL)
799 {
800 g_free(vw->browse_fname);
801 vw->browse_fname = NULL;
802 }
803 gtk_widget_hide(vw->filedlg);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804}
805
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 static gboolean
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000807browse_destroy_cb(GtkWidget *widget UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808{
809 if (gui.browse_fname != NULL)
810 {
811 g_free(gui.browse_fname);
812 gui.browse_fname = NULL;
813 }
814 gui.filedlg = NULL;
Bram Moolenaara3f41662010-07-11 19:01:06 +0200815 gtk_main_quit();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 return FALSE;
817}
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
820/*
821 * Put up a file requester.
822 * Returns the selected name in allocated memory, or NULL for Cancel.
823 * saving, select file to write
824 * title title for the window
825 * dflt default name
826 * ext not used (extension added)
827 * initdir initial directory, NULL for current dir
828 * filter not used (file name filter)
829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 char_u *
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000831gui_mch_browse(int saving UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 char_u *title,
833 char_u *dflt,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000834 char_u *ext UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 char_u *initdir,
Bram Moolenaarb85cb212009-05-17 14:24:23 +0000836 char_u *filter UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837{
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000838#ifdef USE_FILE_CHOOSER
839 GtkWidget *fc;
840#endif
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000841 char_u dirbuf[MAXPATHL];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 title = CONVERT_TO_UTF8(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000845 /* GTK has a bug, it only works with an absolute path. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000846 if (initdir == NULL || *initdir == NUL)
847 mch_dirname(dirbuf, MAXPATHL);
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000848 else if (vim_FullName(initdir, dirbuf, MAXPATHL - 2, FALSE) == FAIL)
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000849 dirbuf[0] = NUL;
850 /* Always need a trailing slash for a directory. */
851 add_pathsep(dirbuf);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000852
853 /* If our pointer is currently hidden, then we should show it. */
854 gui_mch_mousehide(FALSE);
855
856#ifdef USE_FILE_CHOOSER
857 /* We create the dialog each time, so that the button text can be "Open"
858 * or "Save" according to the action. */
859 fc = gtk_file_chooser_dialog_new((const gchar *)title,
860 GTK_WINDOW(gui.mainwin),
861 saving ? GTK_FILE_CHOOSER_ACTION_SAVE
862 : GTK_FILE_CHOOSER_ACTION_OPEN,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000863 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
Bram Moolenaard1350622006-10-24 20:01:06 +0000864 saving ? GTK_STOCK_SAVE : GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000865 NULL);
866 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(fc),
867 (const gchar *)dirbuf);
Bram Moolenaara3f41662010-07-11 19:01:06 +0200868 if (saving && dflt != NULL && *dflt != NUL)
869 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(fc), (char *)dflt);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000870
871 gui.browse_fname = NULL;
872 if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000874 char *filename;
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000875
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000876 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc));
877 gui.browse_fname = (char_u *)g_strdup(filename);
878 g_free(filename);
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000879 }
880 gtk_widget_destroy(GTK_WIDGET(fc));
881
882#else
883
884 if (gui.filedlg == NULL)
885 {
886 GtkFileSelection *fs; /* shortcut */
887
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 gui.filedlg = gtk_file_selection_new((const gchar *)title);
889 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
890 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000891 GTK_WINDOW(gui.mainwin));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 fs = GTK_FILE_SELECTION(gui.filedlg);
893
894 gtk_container_border_width(GTK_CONTAINER(fs), 4);
895
896 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
897 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
898 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
899 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
900 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
901 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
902 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
903 GTK_OBJECT(gui.filedlg));
904 }
905 else
906 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
907
Bram Moolenaar57ac3a22006-10-10 16:28:30 +0000908 /* Concatenate "initdir" and "dflt". */
909 if (dflt != NULL && *dflt != NUL
910 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
911 STRCAT(dirbuf, dflt);
912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
914 (const gchar *)dirbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
916 gtk_widget_show(gui.filedlg);
Bram Moolenaara3f41662010-07-11 19:01:06 +0200917 gtk_main();
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000920 CONVERT_TO_UTF8_FREE(title);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (gui.browse_fname == NULL)
922 return NULL;
923
924 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000925 return vim_strsave(shorten_fname1(gui.browse_fname));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926}
927
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000928/*
929 * Put up a directory selector
930 * Returns the selected name in allocated memory, or NULL for Cancel.
931 * title title for the window
932 * dflt default name
933 * initdir initial directory, NULL for current dir
934 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000935 char_u *
936gui_mch_browsedir(
937 char_u *title,
938 char_u *initdir)
939{
940# if defined(GTK_FILE_CHOOSER) /* Only in GTK 2.4 and later. */
941 char_u dirbuf[MAXPATHL];
942 char_u *p;
943 GtkWidget *dirdlg; /* file selection dialog */
944 char_u *dirname = NULL;
945
946 title = CONVERT_TO_UTF8(title);
947
948 dirdlg = gtk_file_chooser_dialog_new(
949 (const gchar *)title,
950 GTK_WINDOW(gui.mainwin),
951 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,
952 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
953 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
954 NULL);
955
956 CONVERT_TO_UTF8_FREE(title);
957
958 /* if our pointer is currently hidden, then we should show it. */
959 gui_mch_mousehide(FALSE);
960
961 /* GTK appears to insist on an absolute path. */
962 if (initdir == NULL || *initdir == NUL
963 || vim_FullName(initdir, dirbuf, MAXPATHL - 10, FALSE) == FAIL)
964 mch_dirname(dirbuf, MAXPATHL - 10);
965
966 /* Always need a trailing slash for a directory.
967 * Also add a dummy file name, so that we get to the directory. */
968 add_pathsep(dirbuf);
969 STRCAT(dirbuf, "@zd(*&1|");
970 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dirdlg),
971 (const gchar *)dirbuf);
972
973 /* Run the dialog. */
974 if (gtk_dialog_run(GTK_DIALOG(dirdlg)) == GTK_RESPONSE_ACCEPT)
975 dirname = (char_u *)gtk_file_chooser_get_filename(
976 GTK_FILE_CHOOSER(dirdlg));
977 gtk_widget_destroy(dirdlg);
978 if (dirname == NULL)
979 return NULL;
980
981 /* shorten the file name if possible */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000982 p = vim_strsave(shorten_fname1(dirname));
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000983 g_free(dirname);
984 return p;
985
986# else
987 /* For GTK 2.2 and earlier: fall back to ordinary file selector. */
988 return gui_mch_browse(0, title, NULL, NULL, initdir, NULL);
989# endif
990}
991
Bram Moolenaara7fc0102005-05-18 22:17:12 +0000992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif /* FEAT_BROWSE */
994
Bram Moolenaar182c5be2010-06-25 05:37:59 +0200995#if defined(FEAT_GUI_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 static GtkWidget *
998create_message_dialog(int type, char_u *title, char_u *message)
999{
1000 GtkWidget *dialog;
1001 GtkMessageType message_type;
1002
1003 switch (type)
1004 {
1005 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1006 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1007 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1008 default: message_type = GTK_MESSAGE_INFO; break;
1009 }
1010
1011 message = CONVERT_TO_UTF8(message);
1012 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1013 GTK_DIALOG_DESTROY_WITH_PARENT,
1014 message_type,
1015 GTK_BUTTONS_NONE,
1016 "%s", (const char *)message);
1017 CONVERT_TO_UTF8_FREE(message);
1018
1019 if (title != NULL)
1020 {
1021 title = CONVERT_TO_UTF8(title);
1022 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1023 CONVERT_TO_UTF8_FREE(title);
1024 }
1025 else if (type == VIM_GENERIC)
1026 {
1027 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1028 }
1029
1030 return dialog;
1031}
1032
1033/*
1034 * Split up button_string into individual button labels by inserting
1035 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1036 * '&' with '_'. button_string must point to allocated memory!
1037 * Return an allocated array of pointers into button_string.
1038 */
1039 static char **
1040split_button_string(char_u *button_string, int *n_buttons)
1041{
1042 char **array;
1043 char_u *p;
1044 unsigned int count = 1;
1045
1046 for (p = button_string; *p != NUL; ++p)
1047 if (*p == DLG_BUTTON_SEP)
1048 ++count;
1049
1050 array = (char **)alloc((count + 1) * sizeof(char *));
1051 count = 0;
1052
1053 if (array != NULL)
1054 {
1055 array[count++] = (char *)button_string;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001056 for (p = button_string; *p != NUL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 {
1058 if (*p == DLG_BUTTON_SEP)
1059 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001060 *p++ = NUL;
1061 array[count++] = (char *)p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 }
1063 else if (*p == DLG_HOTKEY_CHAR)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00001064 *p++ = '_';
1065 else
1066 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 }
1068 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1069 }
1070
1071 *n_buttons = count;
1072 return array;
1073}
1074
1075 static char **
1076split_button_translation(const char *message)
1077{
1078 char **buttons = NULL;
1079 char_u *str;
1080 int n_buttons = 0;
1081 int n_expected = 1;
1082
1083 for (str = (char_u *)message; *str != NUL; ++str)
1084 if (*str == DLG_BUTTON_SEP)
1085 ++n_expected;
1086
1087 str = (char_u *)_(message);
1088 if (str != NULL)
1089 {
1090 if (output_conv.vc_type != CONV_NONE)
1091 str = string_convert(&output_conv, str, NULL);
1092 else
1093 str = vim_strsave(str);
1094
1095 if (str != NULL)
1096 buttons = split_button_string(str, &n_buttons);
1097 }
1098 /*
1099 * Uh-oh... this should never ever happen. But we don't wanna crash
1100 * if the translation is broken, thus fall back to the untranslated
1101 * buttons string in case of emergency.
1102 */
1103 if (buttons == NULL || n_buttons != n_expected)
1104 {
1105 vim_free(buttons);
1106 vim_free(str);
1107 buttons = NULL;
1108 str = vim_strsave((char_u *)message);
1109
1110 if (str != NULL)
1111 buttons = split_button_string(str, &n_buttons);
1112 if (buttons == NULL)
1113 vim_free(str);
1114 }
1115
1116 return buttons;
1117}
1118
1119 static int
1120button_equal(const char *a, const char *b)
1121{
1122 while (*a != '\0' && *b != '\0')
1123 {
1124 if (*a == '_' && *++a == '\0')
1125 break;
1126 if (*b == '_' && *++b == '\0')
1127 break;
1128
1129 if (g_unichar_tolower(g_utf8_get_char(a))
1130 != g_unichar_tolower(g_utf8_get_char(b)))
1131 return FALSE;
1132
1133 a = g_utf8_next_char(a);
1134 b = g_utf8_next_char(b);
1135 }
1136
1137 return (*a == '\0' && *b == '\0');
1138}
1139
1140 static void
1141dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
1142{
1143 char **ok;
1144 char **ync; /* "yes no cancel" */
1145 char **buttons;
1146 int n_buttons = 0;
Bram Moolenaar89d40322006-08-29 15:30:07 +00001147 int idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148
1149 button_string = vim_strsave(button_string); /* must be writable */
1150 if (button_string == NULL)
1151 return;
1152
1153 /* Check 'v' flag in 'guioptions': vertical button placement. */
1154 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
1155 {
1156 GtkWidget *vbutton_box;
1157
1158 vbutton_box = gtk_vbutton_box_new();
1159 gtk_widget_show(vbutton_box);
1160 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1161 vbutton_box, TRUE, FALSE, 0);
1162 /* Overrule the "action_area" value, hopefully this works... */
1163 GTK_DIALOG(dialog)->action_area = vbutton_box;
1164 }
1165
1166 /*
1167 * Yes this is ugly, I don't particularly like it either. But doing it
1168 * this way has the compelling advantage that translations need not to
1169 * be touched at all. See below what 'ok' and 'ync' are used for.
1170 */
1171 ok = split_button_translation(N_("&Ok"));
1172 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
1173 buttons = split_button_string(button_string, &n_buttons);
1174
1175 /*
1176 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
1177 * environment. Don't hit me -- it's all about consistency.
1178 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
1179 * other way around...
1180 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001181 for (idx = 1; idx <= n_buttons; ++idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 {
1183 char *label;
1184 char_u *label8;
1185
Bram Moolenaar89d40322006-08-29 15:30:07 +00001186 label = buttons[idx - 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 /*
1188 * Perform some guesswork to find appropriate stock items for the
1189 * buttons. We have to compare with a sample of the translated
1190 * button string to get things right. Yes, this is hackish :/
1191 *
1192 * But even the common button labels aren't necessarily translated,
1193 * since anyone can create their own dialogs using Vim functions.
1194 * Thus we have to check for those too.
1195 */
1196 if (ok != NULL && ync != NULL) /* almost impossible to fail */
1197 {
1198 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
1199 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
1200 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
1201 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
1202 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
1203 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
1204 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
1205 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
1206 }
1207 label8 = CONVERT_TO_UTF8((char_u *)label);
Bram Moolenaar89d40322006-08-29 15:30:07 +00001208 gtk_dialog_add_button(dialog, (const gchar *)label8, idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 CONVERT_TO_UTF8_FREE(label8);
1210 }
1211
1212 if (ok != NULL)
1213 vim_free(*ok);
1214 if (ync != NULL)
1215 vim_free(*ync);
1216 vim_free(ok);
1217 vim_free(ync);
1218 vim_free(buttons);
1219 vim_free(button_string);
1220}
1221
1222/*
1223 * Allow mnemonic accelerators to be activated without pressing <Alt>.
1224 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
1225 * GUI used to work this way, and I consider the impact on UI consistency
1226 * low enough to justify implementing this as a special Vim feature.
1227 */
1228typedef struct _DialogInfo
1229{
1230 int ignore_enter; /* no default button, ignore "Enter" */
1231 int noalt; /* accept accelerators without Alt */
1232 GtkDialog *dialog; /* Widget of the dialog */
1233} DialogInfo;
1234
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 static gboolean
1236dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
1237{
1238 DialogInfo *di = (DialogInfo *)data;
1239
Bram Moolenaard2c765e2007-08-14 13:00:40 +00001240 /* Ignore hitting Enter (or Space) when there is no default button. */
1241 if (di->ignore_enter && (event->keyval == GDK_Return
1242 || event->keyval == ' '))
1243 return TRUE;
1244 else /* A different key was pressed, return to normal behavior */
1245 di->ignore_enter = FALSE;
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 /* Close the dialog when hitting "Esc". */
1248 if (event->keyval == GDK_Escape)
1249 {
1250 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
1251 return TRUE;
1252 }
1253
1254 if (di->noalt
1255 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
1256 {
1257 return gtk_window_mnemonic_activate(
1258 GTK_WINDOW(widget), event->keyval,
1259 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
1260 }
1261
1262 return FALSE; /* continue emission */
1263}
1264
1265 int
1266gui_mch_dialog(int type, /* type of dialog */
1267 char_u *title, /* title of dialog */
1268 char_u *message, /* message text */
1269 char_u *buttons, /* names of buttons */
1270 int def_but, /* default button */
Bram Moolenaard2c340a2011-01-17 20:08:11 +01001271 char_u *textfield, /* text for textfield or NULL */
1272 int ex_cmd UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273{
1274 GtkWidget *dialog;
1275 GtkWidget *entry = NULL;
1276 char_u *text;
1277 int response;
1278 DialogInfo dialoginfo;
1279
1280 dialog = create_message_dialog(type, title, message);
1281 dialoginfo.dialog = GTK_DIALOG(dialog);
1282 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
1283
1284 if (textfield != NULL)
1285 {
1286 GtkWidget *alignment;
1287
1288 entry = gtk_entry_new();
1289 gtk_widget_show(entry);
1290
Bram Moolenaardf6b11e2010-11-24 18:48:12 +01001291 /* Make Enter work like pressing OK. */
1292 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
1293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 text = CONVERT_TO_UTF8(textfield);
1295 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
1296 CONVERT_TO_UTF8_FREE(text);
1297
1298 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1299 (float)1.0, (float)1.0);
1300 gtk_container_add(GTK_CONTAINER(alignment), entry);
1301 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
1302 gtk_widget_show(alignment);
1303
1304 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
1305 alignment, TRUE, FALSE, 0);
1306 dialoginfo.noalt = FALSE;
1307 }
1308 else
1309 dialoginfo.noalt = TRUE;
1310
1311 /* Allow activation of mnemonic accelerators without pressing <Alt> when
Bram Moolenaar14716812006-05-04 21:54:08 +00001312 * there is no textfield. Handle pressing Esc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 g_signal_connect(G_OBJECT(dialog), "key_press_event",
1314 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
1315
1316 if (def_but > 0)
1317 {
1318 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
1319 dialoginfo.ignore_enter = FALSE;
1320 }
1321 else
1322 /* No default button, ignore pressing Enter. */
1323 dialoginfo.ignore_enter = TRUE;
1324
1325 /* Show the mouse pointer if it's currently hidden. */
1326 gui_mch_mousehide(FALSE);
1327
1328 response = gtk_dialog_run(GTK_DIALOG(dialog));
1329
1330 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
1331 if (response != GTK_RESPONSE_NONE)
1332 {
Bram Moolenaar82cf9b62005-06-07 21:09:25 +00001333 if (response == GTK_RESPONSE_ACCEPT) /* Enter pressed */
1334 response = def_but;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (textfield != NULL)
1336 {
1337 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
1338 text = CONVERT_FROM_UTF8(text);
1339
Bram Moolenaarce0842a2005-07-18 21:58:11 +00001340 vim_strncpy(textfield, text, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 CONVERT_FROM_UTF8_FREE(text);
1343 }
1344 gtk_widget_destroy(dialog);
1345 }
1346
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 return response > 0 ? response : 0;
1348}
1349
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001350#endif /* FEAT_GUI_DIALOG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352
1353#if defined(FEAT_MENU) || defined(PROTO)
1354
1355 void
1356gui_mch_show_popupmenu(vimmenu_T *menu)
1357{
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001358# if defined(FEAT_XIM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 /*
1360 * Append a submenu for selecting an input method. This is
1361 * currently the only way to switch input methods at runtime.
1362 */
1363 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
1364 "vim-has-im-menu") == NULL)
1365 {
1366 GtkWidget *menuitem;
1367 GtkWidget *submenu;
1368 char_u *name;
1369
1370 menuitem = gtk_separator_menu_item_new();
1371 gtk_widget_show(menuitem);
1372 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1373
1374 name = (char_u *)_("Input _Methods");
1375 name = CONVERT_TO_UTF8(name);
1376 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
1377 CONVERT_TO_UTF8_FREE(name);
1378 gtk_widget_show(menuitem);
1379
1380 submenu = gtk_menu_new();
1381 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
1382 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
1383
1384 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
1385 GTK_MENU_SHELL(submenu));
1386 g_object_set_data(G_OBJECT(menu->submenu_id),
1387 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
1388 }
Bram Moolenaar182c5be2010-06-25 05:37:59 +02001389# endif /* FEAT_XIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390
1391 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1392 NULL, NULL,
1393 (GtkMenuPositionFunc)NULL, NULL,
Bram Moolenaar20892c12011-06-26 04:49:00 +02001394 3U, gui.event_time);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395}
1396
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001397/* Ugly global variable to pass "mouse_pos" flag from gui_make_popup() to
1398 * popup_menu_position_func(). */
1399static int popup_mouse_pos;
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401/*
1402 * Menu position callback; used by gui_make_popup() to place the menu
1403 * at the current text cursor position.
1404 *
1405 * Note: The push_in output argument seems to affect scrolling of huge
1406 * menus that don't fit on the screen. Leave it at the default for now.
1407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001409popup_menu_position_func(GtkMenu *menu UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 gint *x, gint *y,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001411 gboolean *push_in UNUSED,
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001412 gpointer user_data UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001414 gdk_window_get_origin(gui.drawarea->window, x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001416 if (popup_mouse_pos)
1417 {
1418 int mx, my;
1419
1420 gui_mch_getmouse(&mx, &my);
1421 *x += mx;
1422 *y += my;
1423 }
1424 else if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
1425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* Find the cursor position in the current window */
1427 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
1428 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
1429 }
1430}
1431
1432 void
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001433gui_make_popup(char_u *path_name, int mouse_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434{
1435 vimmenu_T *menu;
1436
Bram Moolenaar045e82d2005-07-08 22:25:33 +00001437 popup_mouse_pos = mouse_pos;
1438
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 menu = gui_find_menu(path_name);
1440
1441 if (menu != NULL && menu->submenu_id != NULL)
1442 {
1443 gtk_menu_popup(GTK_MENU(menu->submenu_id),
1444 NULL, NULL,
1445 &popup_menu_position_func, NULL,
1446 0U, (guint32)GDK_CURRENT_TIME);
1447 }
1448}
1449
1450#endif /* FEAT_MENU */
1451
1452
1453/*
1454 * We don't create it twice.
1455 */
1456
1457typedef struct _SharedFindReplace
1458{
1459 GtkWidget *dialog; /* the main dialog widget */
1460 GtkWidget *wword; /* 'Whole word only' check button */
1461 GtkWidget *mcase; /* 'Match case' check button */
1462 GtkWidget *up; /* search direction 'Up' radio button */
1463 GtkWidget *down; /* search direction 'Down' radio button */
1464 GtkWidget *what; /* 'Find what' entry text widget */
1465 GtkWidget *with; /* 'Replace with' entry text widget */
1466 GtkWidget *find; /* 'Find Next' action button */
1467 GtkWidget *replace; /* 'Replace With' action button */
1468 GtkWidget *all; /* 'Replace All' action button */
1469} SharedFindReplace;
1470
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001471static SharedFindReplace find_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
1472static SharedFindReplace repl_widgets = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 static int
1475find_key_press_event(
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001476 GtkWidget *widget UNUSED,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 GdkEventKey *event,
1478 SharedFindReplace *frdp)
1479{
1480 /* If the user is holding one of the key modifiers we will just bail out,
1481 * thus preserving the possibility of normal focus traversal.
1482 */
1483 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
1484 return FALSE;
1485
1486 /* the Escape key synthesizes a cancellation action */
1487 if (event->keyval == GDK_Escape)
1488 {
1489 gtk_widget_hide(frdp->dialog);
1490
1491 return TRUE;
1492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar79166c42007-05-10 18:29:51 +00001494 /* It would be delightful if it where possible to do search history
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * operations on the K_UP and K_DOWN keys here.
1496 */
1497
1498 return FALSE;
1499}
1500
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 static GtkWidget *
1502create_image_button(const char *stock_id, const char *label)
1503{
1504 char_u *text;
1505 GtkWidget *box;
1506 GtkWidget *alignment;
1507 GtkWidget *button;
1508
1509 text = CONVERT_TO_UTF8((char_u *)label);
1510
1511 box = gtk_hbox_new(FALSE, 3);
1512 gtk_box_pack_start(GTK_BOX(box),
1513 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
1514 FALSE, FALSE, 0);
1515 gtk_box_pack_start(GTK_BOX(box),
1516 gtk_label_new((const char *)text),
1517 FALSE, FALSE, 0);
1518
1519 CONVERT_TO_UTF8_FREE(text);
1520
1521 alignment = gtk_alignment_new((float)0.5, (float)0.5,
1522 (float)0.0, (float)0.0);
1523 gtk_container_add(GTK_CONTAINER(alignment), box);
1524 gtk_widget_show_all(alignment);
1525
1526 button = gtk_button_new();
1527 gtk_container_add(GTK_CONTAINER(button), alignment);
1528
1529 return button;
1530}
1531
1532/*
1533 * This is currently only used by find_replace_dialog_create(), and
1534 * I'd really like to keep it at that. In other words: don't spread
1535 * this nasty hack all over the code. Think twice.
1536 */
1537 static const char *
1538convert_localized_message(char_u **buffer, const char *message)
1539{
1540 if (output_conv.vc_type == CONV_NONE)
1541 return message;
1542
1543 vim_free(*buffer);
1544 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
1545
1546 return (const char *)*buffer;
1547}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548
1549 static void
1550find_replace_dialog_create(char_u *arg, int do_replace)
1551{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 GtkWidget *hbox; /* main top down box */
1553 GtkWidget *actionarea;
1554 GtkWidget *table;
1555 GtkWidget *tmp;
1556 GtkWidget *vbox;
1557 gboolean sensitive;
1558 SharedFindReplace *frdp;
1559 char_u *entry_text;
1560 int wword = FALSE;
1561 int mcase = !p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *conv_buffer = NULL;
1563# define CONV(message) convert_localized_message(&conv_buffer, (message))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564
1565 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
1566
1567 /* Get the search string to use. */
1568 entry_text = get_find_dialog_text(arg, &wword, &mcase);
1569
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
1571 {
1572 char_u *old_text = entry_text;
1573 entry_text = string_convert(&output_conv, entry_text, NULL);
1574 vim_free(old_text);
1575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
1577 /*
1578 * If the dialog already exists, just raise it.
1579 */
1580 if (frdp->dialog)
1581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (entry_text != NULL)
1583 {
1584 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1585 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1586 (gboolean)wword);
1587 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1588 (gboolean)mcase);
1589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 gtk_window_present(GTK_WINDOW(frdp->dialog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 vim_free(entry_text);
1592 return;
1593 }
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 frdp->dialog = gtk_dialog_new();
1596 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
1597 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
1598 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599
1600 if (do_replace)
1601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1603 CONV(_("VIM - Search and Replace...")));
1604 }
1605 else
1606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
1608 CONV(_("VIM - Search...")));
1609 }
1610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 hbox = gtk_hbox_new(FALSE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
1613 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614
1615 if (do_replace)
1616 table = gtk_table_new(1024, 4, FALSE);
1617 else
1618 table = gtk_table_new(1024, 3, FALSE);
1619 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
1620 gtk_container_border_width(GTK_CONTAINER(table), 4);
1621
1622 tmp = gtk_label_new(CONV(_("Find what:")));
1623 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1624 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
1625 GTK_FILL, GTK_EXPAND, 2, 2);
1626 frdp->what = gtk_entry_new();
1627 sensitive = (entry_text != NULL && entry_text[0] != NUL);
1628 if (entry_text != NULL)
1629 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
1630 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
1631 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
1632 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
1633 GTK_SIGNAL_FUNC(find_key_press_event),
1634 (gpointer) frdp);
1635 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
1636 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1637
1638 if (do_replace)
1639 {
1640 tmp = gtk_label_new(CONV(_("Replace with:")));
1641 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
1642 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
1643 GTK_FILL, GTK_EXPAND, 2, 2);
1644 frdp->with = gtk_entry_new();
1645 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
1646 GTK_SIGNAL_FUNC(find_replace_cb),
1647 GINT_TO_POINTER(FRD_R_FINDNEXT));
1648 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
1649 GTK_SIGNAL_FUNC(find_key_press_event),
1650 (gpointer) frdp);
1651 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
1652 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
1653
1654 /*
1655 * Make the entry activation only change the input focus onto the
1656 * with item.
1657 */
1658 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1659 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
1660 }
1661 else
1662 {
1663 /*
1664 * Make the entry activation do the search.
1665 */
1666 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
1667 GTK_SIGNAL_FUNC(find_replace_cb),
1668 GINT_TO_POINTER(FRD_FINDNEXT));
1669 }
1670
1671 /* whole word only button */
1672 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
1673 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
1674 (gboolean)wword);
1675 if (do_replace)
1676 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
1677 GTK_FILL, GTK_EXPAND, 2, 2);
1678 else
1679 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
1680 GTK_FILL, GTK_EXPAND, 2, 2);
1681
1682 /* match case button */
1683 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
1684 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
1685 (gboolean)mcase);
1686 if (do_replace)
1687 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
1688 GTK_FILL, GTK_EXPAND, 2, 2);
1689 else
1690 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
1691 GTK_FILL, GTK_EXPAND, 2, 2);
1692
1693 tmp = gtk_frame_new(CONV(_("Direction")));
1694 if (do_replace)
1695 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
1696 GTK_FILL, GTK_FILL, 2, 2);
1697 else
1698 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
1699 GTK_FILL, GTK_FILL, 2, 2);
1700 vbox = gtk_vbox_new(FALSE, 0);
1701 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
1702 gtk_container_add(GTK_CONTAINER(tmp), vbox);
1703
1704 /* 'Up' and 'Down' buttons */
1705 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
1706 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
1707 frdp->down = gtk_radio_button_new_with_label(
1708 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
1709 CONV(_("Down")));
1710 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
1713
1714 /* vbox to hold the action buttons */
1715 actionarea = gtk_vbutton_box_new();
1716 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
1718
1719 /* 'Find Next' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 gtk_widget_set_sensitive(frdp->find, sensitive);
1722
1723 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
1724 GTK_SIGNAL_FUNC(find_replace_cb),
1725 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
1726 : GINT_TO_POINTER(FRD_FINDNEXT));
1727
1728 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
1729 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
1730 gtk_widget_grab_default(frdp->find);
1731
1732 if (do_replace)
1733 {
1734 /* 'Replace' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 gtk_widget_set_sensitive(frdp->replace, sensitive);
1737 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
1738 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
1739 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
1740 GTK_SIGNAL_FUNC(find_replace_cb),
1741 GINT_TO_POINTER(FRD_REPLACE));
1742
1743 /* 'Replace All' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 gtk_widget_set_sensitive(frdp->all, sensitive);
1746 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
1747 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
1748 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
1749 GTK_SIGNAL_FUNC(find_replace_cb),
1750 GINT_TO_POINTER(FRD_REPLACEALL));
1751 }
1752
1753 /* 'Cancel' button */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
1756 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
1757 gtk_signal_connect_object(GTK_OBJECT(tmp),
1758 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
1759 GTK_OBJECT(frdp->dialog));
1760 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
1761 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
1762 GTK_OBJECT(frdp->dialog));
1763
1764 tmp = gtk_vseparator_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 /* Suppress automatic show of the unused action area */
1768 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
1769 gtk_widget_show_all(hbox);
1770 gtk_widget_show(frdp->dialog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771
1772 vim_free(entry_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 vim_free(conv_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#undef CONV
1775}
1776
1777 void
1778gui_mch_find_dialog(exarg_T *eap)
1779{
1780 if (gui.in_use)
1781 find_replace_dialog_create(eap->arg, FALSE);
1782}
1783
1784 void
1785gui_mch_replace_dialog(exarg_T *eap)
1786{
1787 if (gui.in_use)
1788 find_replace_dialog_create(eap->arg, TRUE);
1789}
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791/*
1792 * Callback for actions of the find and replace dialogs
1793 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001795find_replace_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
1797 int flags;
1798 char_u *find_text;
1799 char_u *repl_text;
1800 gboolean direction_down;
1801 SharedFindReplace *sfr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802
1803 flags = (int)(long)data; /* avoid a lint warning here */
1804
1805 /* Get the search/replace strings from the dialog */
1806 if (flags == FRD_FINDNEXT)
1807 {
1808 repl_text = NULL;
1809 sfr = &find_widgets;
1810 }
1811 else
1812 {
1813 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
1814 sfr = &repl_widgets;
1815 }
1816
1817 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
1818 direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
1819
1820 if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
1821 flags |= FRD_WHOLE_WORD;
1822 if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
1823 flags |= FRD_MATCH_CASE;
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 repl_text = CONVERT_FROM_UTF8(repl_text);
1826 find_text = CONVERT_FROM_UTF8(find_text);
Bram Moolenaare980d8a2010-12-08 13:11:21 +01001827 gui_do_findrepl(flags, find_text, repl_text, direction_down);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 CONVERT_FROM_UTF8_FREE(repl_text);
1829 CONVERT_FROM_UTF8_FREE(find_text);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830}
1831
1832/* our usual callback function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 static void
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001834entry_activate_cb(GtkWidget *widget UNUSED, gpointer data)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835{
1836 gtk_widget_grab_focus(GTK_WIDGET(data));
1837}
1838
1839/*
1840 * Syncing the find/replace dialogs on the fly is utterly useless crack,
1841 * and causes nothing but problems. Please tell me a use case for which
1842 * you'd need both a find dialog and a find/replace one at the same time,
1843 * without being able to actually use them separately since they're syncing
1844 * all the time. I don't think it's worthwhile to fix this nonsense,
1845 * particularly evil incarnation of braindeadness, whatever; I'd much rather
1846 * see it extinguished from this planet. Thanks for listening. Sorry.
1847 */
1848 static void
1849entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
1850{
1851 const gchar *entry_text;
1852 gboolean nonempty;
1853
1854 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
1855
1856 if (!entry_text)
1857 return; /* shouldn't happen */
1858
1859 nonempty = (entry_text[0] != '\0');
1860
1861 if (dialog == find_widgets.dialog)
1862 {
1863 gtk_widget_set_sensitive(find_widgets.find, nonempty);
1864 }
1865
1866 if (dialog == repl_widgets.dialog)
1867 {
1868 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
1869 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
1870 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
1871 }
1872}
1873
1874/*
1875 * ":helpfind"
1876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 void
1878ex_helpfind(eap)
Bram Moolenaarb85cb212009-05-17 14:24:23 +00001879 exarg_T *eap UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 /* This will fail when menus are not loaded. Well, it's only for
1882 * backwards compatibility anyway. */
1883 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
1884}