blob: 0d552c05f5717df6f926747e83839fed1fa0281d [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 *
13 * (C) 1998,1999,2000 by Marcin Dalecki <dalecki@evision.ag>
14 *
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
56# if defined(FEAT_GETTEXT) && !defined(ENABLE_NLS)
57# define ENABLE_NLS /* so the texts in the dialog boxes are translated */
58# endif
59# include <gnome.h>
60#endif
61
62#if defined(FEAT_GUI_DIALOG) && !defined(HAVE_GTK2)
63# include "../pixmaps/alert.xpm"
64# include "../pixmaps/error.xpm"
65# include "../pixmaps/generic.xpm"
66# include "../pixmaps/info.xpm"
67# include "../pixmaps/quest.xpm"
68#endif
69
70#if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
71/*
72 * Icons used by the toolbar code.
73 */
74#include "../pixmaps/tb_new.xpm"
75#include "../pixmaps/tb_open.xpm"
76#include "../pixmaps/tb_close.xpm"
77#include "../pixmaps/tb_save.xpm"
78#include "../pixmaps/tb_print.xpm"
79#include "../pixmaps/tb_cut.xpm"
80#include "../pixmaps/tb_copy.xpm"
81#include "../pixmaps/tb_paste.xpm"
82#include "../pixmaps/tb_find.xpm"
83#include "../pixmaps/tb_find_next.xpm"
84#include "../pixmaps/tb_find_prev.xpm"
85#include "../pixmaps/tb_find_help.xpm"
86#include "../pixmaps/tb_exit.xpm"
87#include "../pixmaps/tb_undo.xpm"
88#include "../pixmaps/tb_redo.xpm"
89#include "../pixmaps/tb_help.xpm"
90#include "../pixmaps/tb_macro.xpm"
91#include "../pixmaps/tb_make.xpm"
92#include "../pixmaps/tb_save_all.xpm"
93#include "../pixmaps/tb_jump.xpm"
94#include "../pixmaps/tb_ctags.xpm"
95#include "../pixmaps/tb_load_session.xpm"
96#include "../pixmaps/tb_save_session.xpm"
97#include "../pixmaps/tb_new_session.xpm"
98#include "../pixmaps/tb_blank.xpm"
99#include "../pixmaps/tb_maximize.xpm"
100#include "../pixmaps/tb_split.xpm"
101#include "../pixmaps/tb_minimize.xpm"
102#include "../pixmaps/tb_shell.xpm"
103#include "../pixmaps/tb_replace.xpm"
104#include "../pixmaps/tb_vsplit.xpm"
105#include "../pixmaps/tb_maxwidth.xpm"
106#include "../pixmaps/tb_minwidth.xpm"
107#endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
108
109#ifdef FEAT_GUI_GTK
110# include <gdk/gdkkeysyms.h>
111# include <gdk/gdk.h>
112# ifdef WIN3264
113# include <gdk/gdkwin32.h>
114# else
115# include <gdk/gdkx.h>
116# endif
117
118# include <gtk/gtk.h>
119#else
120/* define these items to be able to generate prototypes without GTK */
121typedef int GtkWidget;
122# define gpointer int
123# define guint8 int
124# define GdkPixmap int
125# define GdkBitmap int
126# define GtkIconFactory int
127# define GtkToolbar int
128# define GtkAdjustment int
129# define gboolean int
130# define GdkEventKey int
131# define CancelData int
132#endif
133
134#ifdef HAVE_GTK2
135/*
136 * Convenience macros to convert from 'encoding' to 'termencoding' and
137 * vice versa. If no conversion is necessary the passed-in pointer is
138 * returned as is, without allocating any memory. Thus additional _FREE()
139 * macros are provided. The _FREE() macros also set the pointer to NULL,
140 * in order to avoid bugs due to illegal memory access only happening if
141 * 'encoding' != utf-8...
142 *
143 * Defining these macros as pure expressions looks a bit tricky but
144 * avoids depending on the context of the macro expansion. One of the
145 * rare occasions where the comma operator comes in handy :)
146 *
147 * Note: Do NOT keep the result around when handling control back to
148 * the main Vim! The user could change 'encoding' at any time.
149 */
150# define CONVERT_TO_UTF8(String) \
151 ((output_conv.vc_type == CONV_NONE || (String) == NULL) \
152 ? (String) \
153 : string_convert(&output_conv, (String), NULL))
154
155# define CONVERT_TO_UTF8_FREE(String) \
156 ((String) = ((output_conv.vc_type == CONV_NONE) \
157 ? (char_u *)NULL \
158 : (vim_free(String), (char_u *)NULL)))
159
160# define CONVERT_FROM_UTF8(String) \
161 ((input_conv.vc_type == CONV_NONE || (String) == NULL) \
162 ? (String) \
163 : string_convert(&input_conv, (String), NULL))
164
165# define CONVERT_FROM_UTF8_FREE(String) \
166 ((String) = ((input_conv.vc_type == CONV_NONE) \
167 ? (char_u *)NULL \
168 : (vim_free(String), (char_u *)NULL)))
169
170#endif /* HAVE_GTK2 */
171
172static void entry_activate_cb(GtkWidget *widget, gpointer data);
173static void entry_changed_cb(GtkWidget *entry, GtkWidget *dialog);
174static void find_replace_cb(GtkWidget *widget, gpointer data);
175
176#if defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)
177/*
178 * Table from BuiltIn## icon indices to GTK+ stock IDs. Order must exactly
179 * match toolbar_names[] in menu.c! All stock icons including the "vim-*"
180 * ones can be overridden in your gtkrc file.
181 */
182static const char * const menu_stock_ids[] =
183{
184 /* 00 */ GTK_STOCK_NEW,
185 /* 01 */ GTK_STOCK_OPEN,
186 /* 02 */ GTK_STOCK_SAVE,
187 /* 03 */ GTK_STOCK_UNDO,
188 /* 04 */ GTK_STOCK_REDO,
189 /* 05 */ GTK_STOCK_CUT,
190 /* 06 */ GTK_STOCK_COPY,
191 /* 07 */ GTK_STOCK_PASTE,
192 /* 08 */ GTK_STOCK_PRINT,
193 /* 09 */ GTK_STOCK_HELP,
194 /* 10 */ GTK_STOCK_FIND,
195 /* 11 */ "vim-save-all",
196 /* 12 */ "vim-session-save",
197 /* 13 */ "vim-session-new",
198 /* 14 */ "vim-session-load",
199 /* 15 */ GTK_STOCK_EXECUTE,
200 /* 16 */ GTK_STOCK_FIND_AND_REPLACE,
201 /* 17 */ GTK_STOCK_CLOSE, /* FIXME: fuzzy */
202 /* 18 */ "vim-window-maximize",
203 /* 19 */ "vim-window-minimize",
204 /* 20 */ "vim-window-split",
205 /* 21 */ "vim-shell",
206 /* 22 */ GTK_STOCK_GO_BACK,
207 /* 23 */ GTK_STOCK_GO_FORWARD,
208 /* 24 */ "vim-find-help",
209 /* 25 */ GTK_STOCK_CONVERT,
210 /* 26 */ GTK_STOCK_JUMP_TO,
211 /* 27 */ "vim-build-tags",
212 /* 28 */ "vim-window-split-vertical",
213 /* 29 */ "vim-window-maximize-width",
214 /* 30 */ "vim-window-minimize-width",
215 /* 31 */ GTK_STOCK_QUIT
216};
217
218 static void
219add_stock_icon(GtkIconFactory *factory,
220 const char *stock_id,
221 const guint8 *inline_data,
222 int data_length)
223{
224 GdkPixbuf *pixbuf;
225 GtkIconSet *icon_set;
226
227 pixbuf = gdk_pixbuf_new_from_inline(data_length, inline_data, FALSE, NULL);
228 icon_set = gtk_icon_set_new_from_pixbuf(pixbuf);
229
230 gtk_icon_factory_add(factory, stock_id, icon_set);
231
232 gtk_icon_set_unref(icon_set);
233 g_object_unref(pixbuf);
234}
235
236 static int
237lookup_menu_iconfile(char_u *iconfile, char_u *dest)
238{
239 expand_env(iconfile, dest, MAXPATHL);
240
241 if (mch_isFullName(dest))
242 {
243 return vim_fexists(dest);
244 }
245 else
246 {
247 static const char suffixes[][4] = {"png", "xpm", "bmp"};
248 char_u buf[MAXPATHL];
249 unsigned int i;
250
251 for (i = 0; i < G_N_ELEMENTS(suffixes); ++i)
252 if (gui_find_bitmap(dest, buf, (char *)suffixes[i]) == OK)
253 {
254 STRCPY(dest, buf);
255 return TRUE;
256 }
257
258 return FALSE;
259 }
260}
261
262 static GtkWidget *
263load_menu_iconfile(char_u *name, GtkIconSize icon_size)
264{
265 GtkWidget *image = NULL;
266 GtkIconSet *icon_set;
267 GtkIconSource *icon_source;
268
269 /*
270 * Rather than loading the icon directly into a GtkImage, create
271 * a new GtkIconSet and put it in there. This way we can easily
272 * scale the toolbar icons on the fly when needed.
273 */
274 icon_set = gtk_icon_set_new();
275 icon_source = gtk_icon_source_new();
276
277 gtk_icon_source_set_filename(icon_source, (const char *)name);
278 gtk_icon_set_add_source(icon_set, icon_source);
279
280 image = gtk_image_new_from_icon_set(icon_set, icon_size);
281
282 gtk_icon_source_free(icon_source);
283 gtk_icon_set_unref(icon_set);
284
285 return image;
286}
287
288 static GtkWidget *
289create_menu_icon(vimmenu_T *menu, GtkIconSize icon_size)
290{
291 GtkWidget *image = NULL;
292 char_u buf[MAXPATHL];
293
294 /* First use a specified "icon=" argument. */
295 if (menu->iconfile != NULL && lookup_menu_iconfile(menu->iconfile, buf))
296 image = load_menu_iconfile(buf, icon_size);
297
298 /* If not found and not builtin specified try using the menu name. */
299 if (image == NULL && !menu->icon_builtin
300 && lookup_menu_iconfile(menu->name, buf))
301 image = load_menu_iconfile(buf, icon_size);
302
303 /* Still not found? Then use a builtin icon, a blank one as fallback. */
304 if (image == NULL)
305 {
306 const char *stock_id;
307 const int n_ids = G_N_ELEMENTS(menu_stock_ids);
308
309 if (menu->iconidx >= 0 && menu->iconidx < n_ids)
310 stock_id = menu_stock_ids[menu->iconidx];
311 else
312 stock_id = GTK_STOCK_MISSING_IMAGE;
313
314 image = gtk_image_new_from_stock(stock_id, icon_size);
315 }
316
317 return image;
318}
319
320#endif /* FEAT_TOOLBAR && HAVE_GTK2 */
321
322#if (defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)) || defined(PROTO)
323
324 void
325gui_gtk_register_stock_icons(void)
326{
327# include "../pixmaps/stock_icons.h"
328 GtkIconFactory *factory;
329
330 factory = gtk_icon_factory_new();
331# define ADD_ICON(Name, Data) add_stock_icon(factory, Name, Data, (int)sizeof(Data))
332
333 ADD_ICON("vim-build-tags", stock_vim_build_tags);
334 ADD_ICON("vim-find-help", stock_vim_find_help);
335 ADD_ICON("vim-save-all", stock_vim_save_all);
336 ADD_ICON("vim-session-load", stock_vim_session_load);
337 ADD_ICON("vim-session-new", stock_vim_session_new);
338 ADD_ICON("vim-session-save", stock_vim_session_save);
339 ADD_ICON("vim-shell", stock_vim_shell);
340 ADD_ICON("vim-window-maximize", stock_vim_window_maximize);
341 ADD_ICON("vim-window-maximize-width", stock_vim_window_maximize_width);
342 ADD_ICON("vim-window-minimize", stock_vim_window_minimize);
343 ADD_ICON("vim-window-minimize-width", stock_vim_window_minimize_width);
344 ADD_ICON("vim-window-split", stock_vim_window_split);
345 ADD_ICON("vim-window-split-vertical", stock_vim_window_split_vertical);
346
347# undef ADD_ICON
348 gtk_icon_factory_add_default(factory);
349 g_object_unref(factory);
350}
351
352#endif /* FEAT_TOOLBAR && HAVE_GTK2 */
353
354
355/*
356 * Only use accelerators when gtk_menu_ensure_uline_accel_group() is
357 * available, which is in version 1.2.1. That was the first version where
358 * accelerators properly worked (according to the change log).
359 */
360#ifdef GTK_CHECK_VERSION
361# if GTK_CHECK_VERSION(1, 2, 1)
362# define GTK_USE_ACCEL
363# endif
364#endif
365
366#if defined(FEAT_MENU) || defined(PROTO)
367
368/*
369 * Translate Vim's mnemonic tagging to GTK+ style and convert to UTF-8
370 * if necessary. The caller must vim_free() the returned string.
371 *
372 * Input Output
373 * _ __
374 * && &
375 * & _ stripped if use_mnemonic == FALSE
376 * <Tab> end of menu label text
377 */
378 static char_u *
379translate_mnemonic_tag(char_u *name, int use_mnemonic)
380{
381 char_u *buf;
382 char_u *psrc;
383 char_u *pdest;
384 int n_underscores = 0;
385
386# ifdef HAVE_GTK2
387 name = CONVERT_TO_UTF8(name);
388# endif
389 if (name == NULL)
390 return NULL;
391
392 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
393 if (*psrc == '_')
394 ++n_underscores;
395
396 buf = alloc((unsigned)(psrc - name + n_underscores + 1));
397 if (buf != NULL)
398 {
399 pdest = buf;
400 for (psrc = name; *psrc != NUL && *psrc != TAB; ++psrc)
401 {
402 if (*psrc == '_')
403 {
404 *pdest++ = '_';
405 *pdest++ = '_';
406 }
407 else if (*psrc != '&')
408 {
409 *pdest++ = *psrc;
410 }
411 else if (*(psrc + 1) == '&')
412 {
413 *pdest++ = *psrc++;
414 }
415 else if (use_mnemonic)
416 {
417 *pdest++ = '_';
418 }
419 }
420 *pdest = NUL;
421 }
422
423# ifdef HAVE_GTK2
424 CONVERT_TO_UTF8_FREE(name);
425# endif
426 return buf;
427}
428
429# ifdef HAVE_GTK2
430
431 static void
432menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
433{
434 GtkWidget *box;
435 char_u *text;
436 int use_mnemonic;
437
438 /* It would be neat to have image menu items, but that would require major
439 * changes to Vim's menu system. Not to mention that all the translations
440 * had to be updated. */
441 menu->id = gtk_menu_item_new();
442 box = gtk_hbox_new(FALSE, 20);
443
444 use_mnemonic = (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget));
445 text = translate_mnemonic_tag(menu->name, use_mnemonic);
446
447 menu->label = gtk_label_new_with_mnemonic((const char *)text);
448 vim_free(text);
449
450 gtk_box_pack_start(GTK_BOX(box), menu->label, FALSE, FALSE, 0);
451
452 if (menu->actext != NULL && menu->actext[0] != NUL)
453 {
454 text = CONVERT_TO_UTF8(menu->actext);
455
456 gtk_box_pack_end(GTK_BOX(box),
457 gtk_label_new((const char *)text),
458 FALSE, FALSE, 0);
459
460 CONVERT_TO_UTF8_FREE(text);
461 }
462
463 gtk_container_add(GTK_CONTAINER(menu->id), box);
464 gtk_widget_show_all(menu->id);
465}
466
467# else /* !HAVE_GTK2 */
468
469/*
470 * Create a highly customized menu item by hand instead of by using:
471 *
472 * gtk_menu_item_new_with_label(menu->dname);
473 *
474 * This is neccessary, since there is no other way in GTK+ 1 to get the
475 * not automatically parsed accellerator stuff right.
476 */
477 static void
478menu_item_new(vimmenu_T *menu, GtkWidget *parent_widget)
479{
480 GtkWidget *widget;
481 GtkWidget *bin;
482 GtkWidget *label;
483 char_u *name;
484 guint accel_key;
485
486 widget = gtk_widget_new(GTK_TYPE_MENU_ITEM,
487 "GtkWidget::visible", TRUE,
488 "GtkWidget::sensitive", TRUE,
489 /* "GtkWidget::parent", parent->submenu_id, */
490 NULL);
491 bin = gtk_widget_new(GTK_TYPE_HBOX,
492 "GtkWidget::visible", TRUE,
493 "GtkWidget::parent", widget,
494 "GtkBox::spacing", 16,
495 NULL);
496 label = gtk_widget_new(GTK_TYPE_ACCEL_LABEL,
497 "GtkWidget::visible", TRUE,
498 "GtkWidget::parent", bin,
499 "GtkAccelLabel::accel_widget", widget,
500 "GtkMisc::xalign", 0.0,
501 NULL);
502 menu->label = label;
503
504 if (menu->actext)
505 gtk_widget_new(GTK_TYPE_LABEL,
506 "GtkWidget::visible", TRUE,
507 "GtkWidget::parent", bin,
508 "GtkLabel::label", menu->actext,
509 "GtkMisc::xalign", 1.0,
510 NULL);
511
512 /*
513 * Translate VIM accelerator tagging into GTK+'s. Note that since GTK uses
514 * underscores as the accelerator key, we need to add an additional under-
515 * score for each understore that appears in the menu name.
516 */
517# ifdef GTK_USE_ACCEL
518 name = translate_mnemonic_tag(menu->name,
519 (p_wak[0] != 'n' || !GTK_IS_MENU_BAR(parent_widget)));
520# else
521 name = translate_mnemonic_tag(menu->name, FALSE);
522# endif
523
524 /* let GTK do its thing */
525 accel_key = gtk_label_parse_uline(GTK_LABEL(label), (const char *)name);
526 vim_free(name);
527
528# ifdef GTK_USE_ACCEL
529 /* Don't add accelator if 'winaltkeys' is "no". */
530 if (accel_key != GDK_VoidSymbol)
531 {
532 if (GTK_IS_MENU_BAR(parent_widget))
533 {
534 if (*p_wak != 'n')
535 gtk_widget_add_accelerator(widget,
536 "activate_item",
537 gui.accel_group,
538 accel_key, GDK_MOD1_MASK,
539 (GtkAccelFlags)0);
540 }
541 else
542 {
543 gtk_widget_add_accelerator(widget,
544 "activate_item",
545 gtk_menu_ensure_uline_accel_group(GTK_MENU(parent_widget)),
546 accel_key, 0,
547 (GtkAccelFlags)0);
548 }
549 }
550# endif /* GTK_USE_ACCEL */
551
552 menu->id = widget;
553}
554
555# endif /* !HAVE_GTK2 */
556
557 void
558gui_mch_add_menu(vimmenu_T *menu, int idx)
559{
560 vimmenu_T *parent;
561 GtkWidget *parent_widget;
562
563 if (menu->name[0] == ']' || menu_is_popup(menu->name))
564 {
565 menu->submenu_id = gtk_menu_new();
566 return;
567 }
568
569 parent = menu->parent;
570
571 if ((parent != NULL && parent->submenu_id == NULL)
572 || !menu_is_menubar(menu->name))
573 return;
574
575 parent_widget = (parent != NULL) ? parent->submenu_id : gui.menubar;
576 menu_item_new(menu, parent_widget);
577
578 /* since the tearoff should always appear first, increment idx */
579 if (parent != NULL && !menu_is_popup(parent->name))
580 ++idx;
581
582 gtk_menu_shell_insert(GTK_MENU_SHELL(parent_widget), menu->id, idx);
583
584#ifndef HAVE_GTK2
585 /*
586 * The "Help" menu is a special case, and should be placed at the far
587 * right hand side of the menu-bar. It's detected by its high priority.
588 *
589 * Right-aligning "Help" is considered bad UI design nowadays.
590 * Thus lets disable this for GTK+ 2 to match the environment.
591 */
592 if (parent == NULL && menu->priority >= 9999)
593 gtk_menu_item_right_justify(GTK_MENU_ITEM(menu->id));
594#endif
595
596 menu->submenu_id = gtk_menu_new();
597
598 gtk_menu_set_accel_group(GTK_MENU(menu->submenu_id), gui.accel_group);
599 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu->id), menu->submenu_id);
600
601 menu->tearoff_handle = gtk_tearoff_menu_item_new();
602 if (vim_strchr(p_go, GO_TEAROFF) != NULL)
603 gtk_widget_show(menu->tearoff_handle);
604 gtk_menu_prepend(GTK_MENU(menu->submenu_id), menu->tearoff_handle);
605}
606
607/*ARGSUSED*/
608 static void
609menu_item_activate(GtkWidget *widget, gpointer data)
610{
611 gui_menu_cb((vimmenu_T *)data);
612
613# ifndef HAVE_GTK2
614 /* Work around a bug in GTK+ 1: we don't seem to get a focus-in
615 * event after clicking a menu item shown via :popup. */
616 if (!gui.in_focus)
617 gui_focus_change(TRUE);
618# endif
619
620 /* make sure the menu action is taken immediately */
621 if (gtk_main_level() > 0)
622 gtk_main_quit();
623}
624
625# if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
626/*
627 * These are the pixmaps used for the default buttons.
628 * Order must exactly match toolbar_names[] in menu.c!
629 */
630static char **(built_in_pixmaps[]) =
631{
632 tb_new_xpm,
633 tb_open_xpm,
634 tb_save_xpm,
635 tb_undo_xpm,
636 tb_redo_xpm,
637 tb_cut_xpm,
638 tb_copy_xpm,
639 tb_paste_xpm,
640 tb_print_xpm,
641 tb_help_xpm,
642 tb_find_xpm,
643 tb_save_all_xpm,
644 tb_save_session_xpm,
645 tb_new_session_xpm,
646 tb_load_session_xpm,
647 tb_macro_xpm,
648 tb_replace_xpm,
649 tb_close_xpm,
650 tb_maximize_xpm,
651 tb_minimize_xpm,
652 tb_split_xpm,
653 tb_shell_xpm,
654 tb_find_prev_xpm,
655 tb_find_next_xpm,
656 tb_find_help_xpm,
657 tb_make_xpm,
658 tb_jump_xpm,
659 tb_ctags_xpm,
660 tb_vsplit_xpm,
661 tb_maxwidth_xpm,
662 tb_minwidth_xpm,
663 tb_exit_xpm
664};
665
666/*
667 * creates a blank pixmap using tb_blank
668 */
669 static void
670pixmap_create_from_xpm(char **xpm, GdkPixmap **pixmap, GdkBitmap **mask)
671{
672 *pixmap = gdk_pixmap_colormap_create_from_xpm_d(
673 NULL,
674 gtk_widget_get_colormap(gui.mainwin),
675 mask,
676 NULL,
677 xpm);
678}
679
680/*
681 * creates a pixmap by using a built-in number
682 */
683 static void
684pixmap_create_by_num(int pixmap_num, GdkPixmap **pixmap, GdkBitmap **mask)
685{
686 if (pixmap_num >= 0 && pixmap_num < (sizeof(built_in_pixmaps)
687 / sizeof(built_in_pixmaps[0])))
688 pixmap_create_from_xpm(built_in_pixmaps[pixmap_num], pixmap, mask);
689}
690
691/*
692 * Creates a pixmap by using the pixmap "name" found in 'runtimepath'/bitmaps/
693 */
694 static void
695pixmap_create_by_dir(char_u *name, GdkPixmap **pixmap, GdkBitmap **mask)
696{
697 char_u full_pathname[MAXPATHL + 1];
698
699 if (gui_find_bitmap(name, full_pathname, "xpm") == OK)
700 *pixmap = gdk_pixmap_colormap_create_from_xpm(
701 NULL,
702 gtk_widget_get_colormap(gui.mainwin),
703 mask,
704 &gui.mainwin->style->bg[GTK_STATE_NORMAL],
705 (const char *)full_pathname);
706}
707
708/*
709 * Creates a pixmap by using the pixmap "fname".
710 */
711 static void
712pixmap_create_from_file(char_u *fname, GdkPixmap **pixmap, GdkBitmap **mask)
713{
714 *pixmap = gdk_pixmap_colormap_create_from_xpm(
715 NULL,
716 gtk_widget_get_colormap(gui.mainwin),
717 mask,
718 &gui.mainwin->style->bg[GTK_STATE_NORMAL],
719 (const char *)fname);
720}
721
722# endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
723
724 void
725gui_mch_add_menu_item(vimmenu_T *menu, int idx)
726{
727 vimmenu_T *parent;
728
729 parent = menu->parent;
730
731# ifdef FEAT_TOOLBAR
732 if (menu_is_toolbar(parent->name))
733 {
734 GtkToolbar *toolbar;
735
736 toolbar = GTK_TOOLBAR(gui.toolbar);
737 menu->submenu_id = NULL;
738
739 if (menu_is_separator(menu->name))
740 {
741 gtk_toolbar_insert_space(toolbar, idx);
742 menu->id = NULL;
743 }
744 else
745 {
746# ifdef HAVE_GTK2
747 char_u *text;
748 char_u *tooltip;
749
750 text = CONVERT_TO_UTF8(menu->dname);
751 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
752
753 menu->id = gtk_toolbar_insert_item(
754 toolbar,
755 (const char *)text,
756 (const char *)tooltip,
757 NULL,
758 create_menu_icon(menu, gtk_toolbar_get_icon_size(toolbar)),
759 G_CALLBACK(&menu_item_activate),
760 menu,
761 idx);
762
763 CONVERT_TO_UTF8_FREE(text);
764 CONVERT_TO_UTF8_FREE(tooltip);
765
766# else /* !HAVE_GTK2 */
767
768 GdkPixmap *pixmap = NULL;
769 GdkBitmap *mask = NULL;
770
771 /* First try user specified bitmap, then builtin, the a blank. */
772 if (menu->iconfile != NULL)
773 {
774 char_u buf[MAXPATHL + 1];
775
776 gui_find_iconfile(menu->iconfile, buf, "xpm");
777 pixmap_create_from_file(buf, &pixmap, &mask);
778 }
779 if (pixmap == NULL && !menu->icon_builtin)
780 pixmap_create_by_dir(menu->name, &pixmap, &mask);
781 if (pixmap == NULL && menu->iconidx >= 0)
782 pixmap_create_by_num(menu->iconidx, &pixmap, &mask);
783 if (pixmap == NULL)
784 pixmap_create_from_xpm(tb_blank_xpm, &pixmap, &mask);
785 if (pixmap == NULL)
786 return; /* should at least have blank pixmap, but if not... */
787
788 menu->id = gtk_toolbar_insert_item(
789 toolbar,
790 (char *)(menu->dname),
791 (char *)(menu->strings[MENU_INDEX_TIP]),
792 (char *)(menu->dname),
793 gtk_pixmap_new(pixmap, mask),
794 GTK_SIGNAL_FUNC(menu_item_activate),
795 (gpointer)menu,
796 idx);
797# endif /* !HAVE_GTK2 */
798 }
799 }
800 else
801# endif /* FEAT_TOOLBAR */
802 {
803 /* No parent, must be a non-menubar menu */
804 if (parent->submenu_id == NULL)
805 return;
806
807 /* Make place for the possible tearoff handle item. Not in the popup
808 * menu, it doesn't have a tearoff item. */
809 if (parent != NULL && !menu_is_popup(parent->name))
810 ++idx;
811
812 if (menu_is_separator(menu->name))
813 {
814 /* Separator: Just add it */
815 menu->id = gtk_menu_item_new();
816 gtk_widget_set_sensitive(menu->id, FALSE);
817 gtk_widget_show(menu->id);
818 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
819
820 return;
821 }
822
823 /* Add textual menu item. */
824 menu_item_new(menu, parent->submenu_id);
825 gtk_widget_show(menu->id);
826 gtk_menu_insert(GTK_MENU(parent->submenu_id), menu->id, idx);
827
828 if (menu->id != NULL)
829 gtk_signal_connect(GTK_OBJECT(menu->id), "activate",
830 GTK_SIGNAL_FUNC(menu_item_activate), menu);
831 }
832}
833#endif /* FEAT_MENU */
834
835
836 void
837gui_mch_set_text_area_pos(int x, int y, int w, int h)
838{
839 gtk_form_move_resize(GTK_FORM(gui.formwin), gui.drawarea, x, y, w, h);
840}
841
842
843#if defined(FEAT_MENU) || defined(PROTO)
844/*
845 * Enable or disable accelators for the toplevel menus.
846 */
847 void
848gui_gtk_set_mnemonics(int enable)
849{
850 vimmenu_T *menu;
851 char_u *name;
852# if !defined(HAVE_GTK2) && defined(GTK_USE_ACCEL)
853 guint accel_key;
854# endif
855
856 for (menu = root_menu; menu != NULL; menu = menu->next)
857 {
858 if (menu->id == NULL)
859 continue;
860
861# if defined(HAVE_GTK2)
862 name = translate_mnemonic_tag(menu->name, enable);
863 gtk_label_set_text_with_mnemonic(GTK_LABEL(menu->label),
864 (const char *)name);
865 vim_free(name);
866# elif defined(GTK_USE_ACCEL)
867 name = translate_mnemonic_tag(menu->name, TRUE);
868 if (name != NULL)
869 {
870 accel_key = gtk_label_parse_uline(GTK_LABEL(menu->label),
871 (const char *)name);
872 if (accel_key != GDK_VoidSymbol)
873 gtk_widget_remove_accelerator(menu->id, gui.accel_group,
874 accel_key, GDK_MOD1_MASK);
875 if (enable && accel_key != GDK_VoidSymbol)
876 gtk_widget_add_accelerator(menu->id, "activate_item",
877 gui.accel_group,
878 accel_key, GDK_MOD1_MASK,
879 (GtkAccelFlags)0);
880 vim_free(name);
881 }
882 if (!enable)
883 {
884 name = translate_mnemonic_tag(menu->name, FALSE);
885 gtk_label_parse_uline(GTK_LABEL(menu->label), (const char *)name);
886 vim_free(name);
887 }
888# endif
889 }
890}
891
892 static void
893recurse_tearoffs(vimmenu_T *menu, int val)
894{
895 for (; menu != NULL; menu = menu->next)
896 {
897 if (menu->submenu_id != NULL && menu->tearoff_handle != NULL
898 && menu->name[0] != ']' && !menu_is_popup(menu->name))
899 {
900 if (val)
901 gtk_widget_show(menu->tearoff_handle);
902 else
903 gtk_widget_hide(menu->tearoff_handle);
904 }
905 recurse_tearoffs(menu->children, val);
906 }
907}
908
909 void
910gui_mch_toggle_tearoffs(int enable)
911{
912 recurse_tearoffs(root_menu, enable);
913}
914#endif /* FEAT_MENU */
915
916
917#if defined(FEAT_TOOLBAR) && !defined(HAVE_GTK2)
918/*
919 * Seems like there's a hole in the GTK Toolbar API: there's no provision for
920 * removing an item from the toolbar. Therefore I need to resort to going
921 * really deeply into the internal widget structures.
922 *
923 * <danielk> I'm not sure the statement above is true -- at least with
924 * GTK+ 2 one can just call gtk_widget_destroy() and be done with it.
925 * It is true though that you couldn't remove space items before GTK+ 2
926 * (without digging into the internals that is). But the code below
927 * doesn't seem to handle those either. Well, it's obsolete anyway.
928 */
929 static void
930toolbar_remove_item_by_text(GtkToolbar *tb, const char *text)
931{
932 GtkContainer *container;
933 GList *childl;
934 GtkToolbarChild *gtbc;
935
936 g_return_if_fail(tb != NULL);
937 g_return_if_fail(GTK_IS_TOOLBAR(tb));
938 container = GTK_CONTAINER(&tb->container);
939
940 for (childl = tb->children; childl; childl = childl->next)
941 {
942 gtbc = (GtkToolbarChild *)childl->data;
943
944 if (gtbc->type != GTK_TOOLBAR_CHILD_SPACE
945 && strcmp(GTK_LABEL(gtbc->label)->label, text) == 0)
946 {
947 gboolean was_visible;
948
949 was_visible = GTK_WIDGET_VISIBLE(gtbc->widget);
950 gtk_widget_unparent(gtbc->widget);
951
952 tb->children = g_list_remove_link(tb->children, childl);
953 g_free(gtbc);
954 g_list_free(childl);
955 tb->num_children--;
956
957 if (was_visible && GTK_WIDGET_VISIBLE(container))
958 gtk_widget_queue_resize(GTK_WIDGET(container));
959
960 break;
961 }
962 }
963}
964#endif /* FEAT_TOOLBAR && !HAVE_GTK2 */
965
966
967#if defined(FEAT_TOOLBAR) && defined(HAVE_GTK2)
968 static int
969get_menu_position(vimmenu_T *menu)
970{
971 vimmenu_T *node;
972 int index = 0;
973
974 for (node = menu->parent->children; node != menu; node = node->next)
975 {
976 g_return_val_if_fail(node != NULL, -1);
977 ++index;
978 }
979
980 return index;
981}
982#endif /* FEAT_TOOLBAR && HAVE_GTK2 */
983
984
985#if defined(FEAT_TOOLBAR) || defined(PROTO)
986 void
987gui_mch_menu_set_tip(vimmenu_T *menu)
988{
989 if (menu->id != NULL && menu->parent != NULL
990 && gui.toolbar != NULL && menu_is_toolbar(menu->parent->name))
991 {
992 char_u *tooltip;
993
994# ifdef HAVE_GTK2
995 tooltip = CONVERT_TO_UTF8(menu->strings[MENU_INDEX_TIP]);
996# else
997 tooltip = menu->strings[MENU_INDEX_TIP];
998# endif
999 gtk_tooltips_set_tip(GTK_TOOLBAR(gui.toolbar)->tooltips,
1000 menu->id, (const char *)tooltip, NULL);
1001# ifdef HAVE_GTK2
1002 CONVERT_TO_UTF8_FREE(tooltip);
1003# endif
1004 }
1005}
1006#endif /* FEAT_TOOLBAR */
1007
1008
1009#if defined(FEAT_MENU) || defined(PROTO)
1010/*
1011 * Destroy the machine specific menu widget.
1012 */
1013 void
1014gui_mch_destroy_menu(vimmenu_T *menu)
1015{
1016# ifdef FEAT_TOOLBAR
1017 if (menu->parent != NULL && menu_is_toolbar(menu->parent->name))
1018 {
1019# ifdef HAVE_GTK2
1020 if (menu_is_separator(menu->name))
1021 gtk_toolbar_remove_space(GTK_TOOLBAR(gui.toolbar),
1022 get_menu_position(menu));
1023 else if (menu->id != NULL)
1024 gtk_widget_destroy(menu->id);
1025# else
1026 toolbar_remove_item_by_text(GTK_TOOLBAR(gui.toolbar),
1027 (const char *)menu->dname);
1028# endif
1029 }
1030 else
1031# endif /* FEAT_TOOLBAR */
1032 {
1033 if (menu->submenu_id != NULL)
1034 gtk_widget_destroy(menu->submenu_id);
1035
1036 if (menu->id != NULL)
1037 gtk_widget_destroy(menu->id);
1038 }
1039
1040 menu->submenu_id = NULL;
1041 menu->id = NULL;
1042}
1043#endif /* FEAT_MENU */
1044
1045
1046/*
1047 * Scrollbar stuff.
1048 */
1049 void
1050gui_mch_set_scrollbar_thumb(scrollbar_T *sb, long val, long size, long max)
1051{
1052 if (sb->id != NULL)
1053 {
1054 GtkAdjustment *adjustment;
1055
1056 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1057
1058 adjustment->lower = 0.0;
1059 adjustment->value = val;
1060 adjustment->upper = max + 1;
1061 adjustment->page_size = size;
1062 adjustment->page_increment = size < 3L ? 1L : size - 2L;
1063 adjustment->step_increment = 1.0;
1064
1065#ifdef HAVE_GTK2
1066 g_signal_handler_block(GTK_OBJECT(adjustment),
1067 (gulong)sb->handler_id);
1068#else
1069 gtk_signal_handler_block(GTK_OBJECT(adjustment),
1070 (guint)sb->handler_id);
1071#endif
1072 gtk_adjustment_changed(adjustment);
1073#ifdef HAVE_GTK2
1074 g_signal_handler_unblock(GTK_OBJECT(adjustment),
1075 (gulong)sb->handler_id);
1076#else
1077 gtk_signal_handler_unblock(GTK_OBJECT(adjustment),
1078 (guint)sb->handler_id);
1079#endif
1080 }
1081}
1082
1083 void
1084gui_mch_set_scrollbar_pos(scrollbar_T *sb, int x, int y, int w, int h)
1085{
1086 if (sb->id != NULL)
1087 gtk_form_move_resize(GTK_FORM(gui.formwin), sb->id, x, y, w, h);
1088}
1089
1090/*
1091 * Take action upon scrollbar dragging.
1092 */
1093 static void
1094adjustment_value_changed(GtkAdjustment *adjustment, gpointer data)
1095{
1096 scrollbar_T *sb;
1097 long value;
1098 int dragging = FALSE;
1099
1100#ifdef FEAT_XIM
1101 /* cancel any preediting */
1102 if (im_is_preediting())
1103 xim_reset();
1104#endif
1105
1106 sb = gui_find_scrollbar((long)data);
1107 value = (long)adjustment->value;
1108 /*
1109 * The dragging argument must be right for the scrollbar to work with
1110 * closed folds. This isn't documented, hopefully this will keep on
1111 * working in later GTK versions.
1112 *
1113 * FIXME: Well, it doesn't work in GTK2. :)
1114 * HACK: Get the mouse pointer position, if it appears to be on an arrow
1115 * button set "dragging" to FALSE. This assumes square buttons!
1116 */
1117 if (sb != NULL)
1118 {
1119#ifdef HAVE_GTK2
1120 dragging = TRUE;
1121
1122 if (sb->wp != NULL)
1123 {
1124 int x;
1125 int y;
1126 GdkModifierType state;
1127 int width;
1128 int height;
1129
1130 /* vertical scrollbar: need to set "dragging" properly in case
1131 * there are closed folds. */
1132 gdk_window_get_pointer(sb->id->window, &x, &y, &state);
1133 gdk_window_get_size(sb->id->window, &width, &height);
1134 if (x >= 0 && x < width && y >= 0 && y < height)
1135 {
1136 if (y < width)
1137 {
1138 /* up arrow: move one (closed fold) line up */
1139 dragging = FALSE;
1140 value = sb->wp->w_topline - 2;
1141 }
1142 else if (y > height - width)
1143 {
1144 /* down arrow: move one (closed fold) line down */
1145 dragging = FALSE;
1146 value = sb->wp->w_topline;
1147 }
1148 }
1149 }
1150#else
1151 dragging = (GTK_RANGE(sb->id)->scroll_type == GTK_SCROLL_NONE);
1152#endif
1153 }
1154
1155 gui_drag_scrollbar(sb, value, dragging);
1156
1157 if (gtk_main_level() > 0)
1158 gtk_main_quit();
1159}
1160
1161/* SBAR_VERT or SBAR_HORIZ */
1162 void
1163gui_mch_create_scrollbar(scrollbar_T *sb, int orient)
1164{
1165 if (orient == SBAR_HORIZ)
1166 sb->id = gtk_hscrollbar_new(NULL);
1167 else if (orient == SBAR_VERT)
1168 sb->id = gtk_vscrollbar_new(NULL);
1169
1170 if (sb->id != NULL)
1171 {
1172 GtkAdjustment *adjustment;
1173
1174 GTK_WIDGET_UNSET_FLAGS(sb->id, GTK_CAN_FOCUS);
1175 gtk_form_put(GTK_FORM(gui.formwin), sb->id, 0, 0);
1176
1177 adjustment = gtk_range_get_adjustment(GTK_RANGE(sb->id));
1178
1179 sb->handler_id = gtk_signal_connect(
1180 GTK_OBJECT(adjustment), "value_changed",
1181 GTK_SIGNAL_FUNC(adjustment_value_changed),
1182 GINT_TO_POINTER(sb->ident));
1183 gui_mch_update();
1184 }
1185}
1186
1187#if defined(FEAT_WINDOWS) || defined(PROTO)
1188 void
1189gui_mch_destroy_scrollbar(scrollbar_T *sb)
1190{
1191 if (sb->id != NULL)
1192 {
1193 gtk_widget_destroy(sb->id);
1194 sb->id = NULL;
1195 }
1196 gui_mch_update();
1197}
1198#endif
1199
1200#if defined(FEAT_BROWSE) || defined(PROTO)
1201/*
1202 * Implementation of the file selector related stuff
1203 */
1204
1205/*ARGSUSED*/
1206 static void
1207browse_ok_cb(GtkWidget *widget, gpointer cbdata)
1208{
1209 gui_T *vw = (gui_T *)cbdata;
1210
1211 if (vw->browse_fname != NULL)
1212 g_free(vw->browse_fname);
1213
1214 vw->browse_fname = (char_u *)g_strdup(gtk_file_selection_get_filename(
1215 GTK_FILE_SELECTION(vw->filedlg)));
1216 gtk_widget_hide(vw->filedlg);
1217 if (gtk_main_level() > 0)
1218 gtk_main_quit();
1219}
1220
1221/*ARGSUSED*/
1222 static void
1223browse_cancel_cb(GtkWidget *widget, gpointer cbdata)
1224{
1225 gui_T *vw = (gui_T *)cbdata;
1226
1227 if (vw->browse_fname != NULL)
1228 {
1229 g_free(vw->browse_fname);
1230 vw->browse_fname = NULL;
1231 }
1232 gtk_widget_hide(vw->filedlg);
1233 if (gtk_main_level() > 0)
1234 gtk_main_quit();
1235}
1236
1237/*ARGSUSED*/
1238 static gboolean
1239browse_destroy_cb(GtkWidget * widget)
1240{
1241 if (gui.browse_fname != NULL)
1242 {
1243 g_free(gui.browse_fname);
1244 gui.browse_fname = NULL;
1245 }
1246 gui.filedlg = NULL;
1247
1248 if (gtk_main_level() > 0)
1249 gtk_main_quit();
1250
1251 return FALSE;
1252}
1253
1254/*
1255 * Put up a file requester.
1256 * Returns the selected name in allocated memory, or NULL for Cancel.
1257 * saving, select file to write
1258 * title title for the window
1259 * dflt default name
1260 * ext not used (extension added)
1261 * initdir initial directory, NULL for current dir
1262 * filter not used (file name filter)
1263 */
1264/*ARGSUSED*/
1265 char_u *
1266gui_mch_browse(int saving,
1267 char_u *title,
1268 char_u *dflt,
1269 char_u *ext,
1270 char_u *initdir,
1271 char_u *filter)
1272{
1273 GtkFileSelection *fs; /* shortcut */
1274 char_u dirbuf[MAXPATHL];
1275 char_u *p;
1276
1277# ifdef HAVE_GTK2
1278 title = CONVERT_TO_UTF8(title);
1279# endif
1280
1281 if (!gui.filedlg)
1282 {
1283 gui.filedlg = gtk_file_selection_new((const gchar *)title);
1284 gtk_window_set_modal(GTK_WINDOW(gui.filedlg), TRUE);
1285 gtk_window_set_transient_for(GTK_WINDOW(gui.filedlg),
1286 GTK_WINDOW(gui.mainwin));
1287 fs = GTK_FILE_SELECTION(gui.filedlg);
1288
1289 gtk_container_border_width(GTK_CONTAINER(fs), 4);
1290
1291 gtk_signal_connect(GTK_OBJECT(fs->ok_button),
1292 "clicked", GTK_SIGNAL_FUNC(browse_ok_cb), &gui);
1293 gtk_signal_connect(GTK_OBJECT(fs->cancel_button),
1294 "clicked", GTK_SIGNAL_FUNC(browse_cancel_cb), &gui);
1295 /* gtk_signal_connect() doesn't work for destroy, it causes a hang */
1296 gtk_signal_connect_object(GTK_OBJECT(gui.filedlg),
1297 "destroy", GTK_SIGNAL_FUNC(browse_destroy_cb),
1298 GTK_OBJECT(gui.filedlg));
1299 }
1300 else
1301 gtk_window_set_title(GTK_WINDOW(gui.filedlg), (const gchar *)title);
1302
1303# ifdef HAVE_GTK2
1304 CONVERT_TO_UTF8_FREE(title);
1305# endif
1306
1307 /* if our pointer is currently hidden, then we should show it. */
1308 gui_mch_mousehide(FALSE);
1309
1310 /* Concatenate "initdir" and "dflt". */
1311 if (initdir == NULL || *initdir == NUL)
1312 mch_dirname(dirbuf, MAXPATHL);
1313 else if (STRLEN(initdir) + 2 < MAXPATHL)
1314 STRCPY(dirbuf, initdir);
1315 else
1316 dirbuf[0] = NUL;
1317 /* Always need a trailing slash for a directory. */
1318 add_pathsep(dirbuf);
1319 if (dflt != NULL && *dflt != NUL
1320 && STRLEN(dirbuf) + 2 + STRLEN(dflt) < MAXPATHL)
1321 STRCAT(dirbuf, dflt);
1322
1323 gtk_file_selection_set_filename(GTK_FILE_SELECTION(gui.filedlg),
1324 (const gchar *)dirbuf);
1325# ifndef HAVE_GTK2
1326 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1327 GTK_WIDGET(gui.filedlg), VW_POS_MOUSE);
1328# endif
1329
1330 gtk_widget_show(gui.filedlg);
1331 while (gui.filedlg && GTK_WIDGET_DRAWABLE(gui.filedlg))
1332 gtk_main_iteration_do(TRUE);
1333
1334 if (gui.browse_fname == NULL)
1335 return NULL;
1336
1337 /* shorten the file name if possible */
1338 mch_dirname(dirbuf, MAXPATHL);
1339 p = shorten_fname(gui.browse_fname, dirbuf);
1340 if (p == NULL)
1341 p = gui.browse_fname;
1342 return vim_strsave(p);
1343}
1344
1345#endif /* FEAT_BROWSE */
1346
1347#if (defined(FEAT_GUI_DIALOG) && !defined(HAVE_GTK2)) || defined(PROTO)
1348
1349static char_u *dialog_textfield = NULL;
1350static GtkWidget *dialog_textentry;
1351
1352 static void
1353dlg_destroy(GtkWidget *dlg)
1354{
1355 if (dialog_textfield != NULL)
1356 {
1357 const char *text;
1358
1359 text = gtk_entry_get_text(GTK_ENTRY(dialog_textentry));
1360 STRNCPY(dialog_textfield, text, IOSIZE);
1361 dialog_textfield[IOSIZE - 1] = NUL;
1362 }
1363
1364 /* Destroy the dialog, will break the waiting loop. */
1365 gtk_widget_destroy(dlg);
1366}
1367
1368# ifdef FEAT_GUI_GNOME
1369/* ARGSUSED */
1370 static int
1371gui_gnome_dialog( int type,
1372 char_u *title,
1373 char_u *message,
1374 char_u *buttons,
1375 int dfltbutton,
1376 char_u *textfield)
1377{
1378 GtkWidget *dlg;
1379 char *gdtype;
1380 char_u *buttons_copy, *p, *next;
1381 char **buttons_list;
1382 int butcount, cur;
1383
1384 /* make a copy, so that we can insert NULs */
1385 if ((buttons_copy = vim_strsave(buttons)) == NULL)
1386 return -1;
1387
1388 /* determine exact number of buttons and allocate array to hold them */
1389 for (butcount = 0, p = buttons; *p; p++)
1390 {
1391 if (*p == '\n')
1392 butcount++;
1393 }
1394 butcount++;
1395 buttons_list = g_new0(char *, butcount + 1);
1396
1397 /* Add pixmap */
1398 switch (type)
1399 {
1400 case VIM_ERROR:
1401 gdtype = GNOME_MESSAGE_BOX_ERROR;
1402 break;
1403 case VIM_WARNING:
1404 gdtype = GNOME_MESSAGE_BOX_WARNING;
1405 break;
1406 case VIM_INFO:
1407 gdtype = GNOME_MESSAGE_BOX_INFO;
1408 break;
1409 case VIM_QUESTION:
1410 gdtype = GNOME_MESSAGE_BOX_QUESTION;
1411 break;
1412 default:
1413 gdtype = GNOME_MESSAGE_BOX_GENERIC;
1414 };
1415
1416 p = buttons_copy;
1417 for (cur = 0; cur < butcount; ++cur)
1418 {
1419 for (next = p; *next; ++next)
1420 {
1421 if (*next == DLG_HOTKEY_CHAR)
1422 mch_memmove(next, next + 1, STRLEN(next));
1423 if (*next == DLG_BUTTON_SEP)
1424 {
1425 *next++ = NUL;
1426 break;
1427 }
1428 }
1429
1430 /* this should probably go into a table, but oh well */
1431 if (g_strcasecmp((char *)p, "Ok") == 0)
1432 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_OK);
1433 else if (g_strcasecmp((char *)p, "Cancel") == 0)
1434 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_CANCEL);
1435 else if (g_strcasecmp((char *)p, "Yes") == 0)
1436 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_YES);
1437 else if (g_strcasecmp((char *)p, "No") == 0)
1438 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_NO);
1439 else if (g_strcasecmp((char *)p, "Close") == 0)
1440 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_CLOSE);
1441 else if (g_strcasecmp((char *)p, "Help") == 0)
1442 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_HELP);
1443 else if (g_strcasecmp((char *)p, "Apply") == 0)
1444 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_APPLY);
1445#if 0
1446 /*
1447 * these aren't really used that often anyway, but are listed here as
1448 * placeholders in case we need them.
1449 */
1450 else if (g_strcasecmp((char *)p, "Next") == 0)
1451 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_NEXT);
1452 else if (g_strcasecmp((char *)p, "Prev") == 0)
1453 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_PREV);
1454 else if (g_strcasecmp((char *)p, "Up") == 0)
1455 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_UP);
1456 else if (g_strcasecmp((char *)p, "Down") == 0)
1457 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_DOWN);
1458 else if (g_strcasecmp((char *)p, "Font") == 0)
1459 buttons_list[cur] = g_strdup(GNOME_STOCK_BUTTON_FONT);
1460#endif
1461 else
1462 buttons_list[cur] = g_strdup((char *)p);
1463
1464 if (*next == NUL)
1465 break;
1466
1467 p = next;
1468 }
1469 vim_free(buttons_copy);
1470
1471 dlg = gnome_message_box_newv((const char *)message,
1472 (const char *)gdtype,
1473 (const char **)buttons_list);
1474 for (cur = 0; cur < butcount; ++cur)
1475 g_free(buttons_list[cur]);
1476 g_free(buttons_list);
1477
1478 dialog_textfield = textfield;
1479 if (textfield != NULL)
1480 {
1481 /* Add text entry field */
1482 dialog_textentry = gtk_entry_new();
1483 gtk_box_pack_start(GTK_BOX(GNOME_DIALOG(dlg)->vbox), dialog_textentry,
1484 TRUE, TRUE, 0);
1485 gtk_entry_set_text(GTK_ENTRY(dialog_textentry),
1486 (const gchar *)textfield);
1487 gtk_entry_select_region(GTK_ENTRY(dialog_textentry), 0,
1488 STRLEN(textfield));
1489 gtk_entry_set_max_length(GTK_ENTRY(dialog_textentry), IOSIZE - 1);
1490 gtk_entry_set_position(GTK_ENTRY(dialog_textentry), STRLEN(textfield));
1491 gtk_widget_show(dialog_textentry);
1492 gtk_window_set_focus(GTK_WINDOW(dlg), dialog_textentry);
1493 }
1494
1495 gtk_signal_connect_object(GTK_OBJECT(dlg), "destroy",
1496 GTK_SIGNAL_FUNC(dlg_destroy), GTK_OBJECT(dlg));
1497 gnome_dialog_set_default(GNOME_DIALOG(dlg), dfltbutton + 1);
1498 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1499 GTK_WIDGET(dlg), VW_POS_MOUSE);
1500
1501 return (1 + gnome_dialog_run_and_close(GNOME_DIALOG(dlg)));
1502}
1503
1504# endif /* FEAT_GUI_GNOME */
1505
1506typedef struct _ButtonData
1507{
1508 int *status;
1509 int index;
1510 GtkWidget *dialog;
1511} ButtonData;
1512
1513typedef struct _CancelData
1514{
1515 int *status;
1516 int ignore_enter;
1517 GtkWidget *dialog;
1518} CancelData;
1519
1520/* ARGSUSED */
1521 static void
1522dlg_button_clicked(GtkWidget * widget, ButtonData *data)
1523{
1524 *(data->status) = data->index + 1;
1525 dlg_destroy(data->dialog);
1526}
1527
1528/*
1529 * This makes the Escape key equivalent to the cancel button.
1530 */
1531/*ARGSUSED*/
1532 static int
1533dlg_key_press_event(GtkWidget * widget, GdkEventKey * event, CancelData *data)
1534{
1535 /* Ignore hitting Enter when there is no default button. */
1536 if (data->ignore_enter && event->keyval == GDK_Return)
1537 return TRUE;
1538
1539 if (event->keyval != GDK_Escape && event->keyval != GDK_Return)
1540 return FALSE;
1541
1542 /* The result value of 0 from a dialog is signaling cancelation.
1543 * 1 means OK. */
1544 *(data->status) = (event->keyval == GDK_Return);
1545 dlg_destroy(data->dialog);
1546
1547 return TRUE;
1548}
1549
1550/*
1551 * Callback function for when the dialog was destroyed by a window manager.
1552 */
1553 static void
1554dlg_destroy_cb(int *p)
1555{
1556 *p = TRUE; /* set dialog_destroyed to break out of the loop */
1557 if (gtk_main_level() > 0)
1558 gtk_main_quit();
1559}
1560
1561/* ARGSUSED */
1562 int
1563gui_mch_dialog( int type, /* type of dialog */
1564 char_u *title, /* title of dialog */
1565 char_u *message, /* message text */
1566 char_u *buttons, /* names of buttons */
1567 int def_but, /* default button */
1568 char_u *textfield) /* text for textfield or NULL */
1569{
1570 char_u *names;
1571 char_u *p;
1572 int i;
1573 int butcount;
1574 int dialog_status = -1;
1575 int dialog_destroyed = FALSE;
1576 int vertical;
1577
1578 GtkWidget *dialog;
1579 GtkWidget *frame;
1580 GtkWidget *vbox;
1581 GtkWidget *table;
1582 GtkWidget *dialogmessage;
1583 GtkWidget *action_area;
1584 GtkWidget *sub_area;
1585 GtkWidget *separator;
1586 GtkAccelGroup *accel_group;
1587 GtkWidget *pixmap;
1588 GdkPixmap *icon = NULL;
1589 GdkBitmap *mask = NULL;
1590 char **icon_data = NULL;
1591
1592 GtkWidget **button;
1593 ButtonData *data;
1594 CancelData cancel_data;
1595
1596 /* if our pointer is currently hidden, then we should show it. */
1597 gui_mch_mousehide(FALSE);
1598
1599# ifdef FEAT_GUI_GNOME
1600 /* If Gnome is available, use it for the dialog. */
1601 if (gtk_socket_id == 0)
1602 return gui_gnome_dialog(type, title, message, buttons, def_but,
1603 textfield);
1604# endif
1605
1606 if (title == NULL)
1607 title = (char_u *)_("Vim dialog...");
1608
1609 if ((type < 0) || (type > VIM_LAST_TYPE))
1610 type = VIM_GENERIC;
1611
1612 /* Check 'v' flag in 'guioptions': vertical button placement. */
1613 vertical = (vim_strchr(p_go, GO_VERTICAL) != NULL);
1614
1615 dialog = gtk_window_new(GTK_WINDOW_DIALOG);
1616 gtk_window_set_title(GTK_WINDOW(dialog), (const gchar *)title);
1617 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(gui.mainwin));
1618 gtk_widget_realize(dialog);
1619 gdk_window_set_decorations(dialog->window, GDK_DECOR_BORDER);
1620 gdk_window_set_functions(dialog->window, GDK_FUNC_MOVE);
1621
1622 cancel_data.status = &dialog_status;
1623 cancel_data.dialog = dialog;
1624 gtk_signal_connect_after(GTK_OBJECT(dialog), "key_press_event",
1625 GTK_SIGNAL_FUNC(dlg_key_press_event),
1626 (gpointer) &cancel_data);
1627 /* Catch the destroy signal, otherwise we don't notice a window manager
1628 * destroying the dialog window. */
1629 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
1630 GTK_SIGNAL_FUNC(dlg_destroy_cb),
1631 (gpointer)&dialog_destroyed);
1632
1633 gtk_grab_add(dialog);
1634
1635 /* this makes it look beter on Motif style window managers */
1636 frame = gtk_frame_new(NULL);
1637 gtk_container_add(GTK_CONTAINER(dialog), frame);
1638 gtk_widget_show(frame);
1639
1640 vbox = gtk_vbox_new(FALSE, 0);
1641 gtk_container_add(GTK_CONTAINER(frame), vbox);
1642 gtk_widget_show(vbox);
1643
1644 table = gtk_table_new(1, 3, FALSE);
1645 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
1646 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
1647 gtk_container_border_width(GTK_CONTAINER(table), 4);
1648 gtk_box_pack_start(GTK_BOX(vbox), table, 4, 4, 0);
1649 gtk_widget_show(table);
1650
1651 /* Add pixmap */
1652 switch (type)
1653 {
1654 case VIM_GENERIC:
1655 icon_data = generic_xpm;
1656 break;
1657 case VIM_ERROR:
1658 icon_data = error_xpm;
1659 break;
1660 case VIM_WARNING:
1661 icon_data = alert_xpm;
1662 break;
1663 case VIM_INFO:
1664 icon_data = info_xpm;
1665 break;
1666 case VIM_QUESTION:
1667 icon_data = quest_xpm;
1668 break;
1669 default:
1670 icon_data = generic_xpm;
1671 };
1672 icon = gdk_pixmap_colormap_create_from_xpm_d(NULL,
1673 gtk_widget_get_colormap(dialog),
1674 &mask, NULL, icon_data);
1675 if (icon)
1676 {
1677 pixmap = gtk_pixmap_new(icon, mask);
1678 /* gtk_misc_set_alignment(GTK_MISC(pixmap), 0.5, 0.5); */
1679 gtk_table_attach_defaults(GTK_TABLE(table), pixmap, 0, 1, 0, 1);
1680 gtk_widget_show(pixmap);
1681 }
1682
1683 /* Add label */
1684 dialogmessage = gtk_label_new((const gchar *)message);
1685 gtk_table_attach_defaults(GTK_TABLE(table), dialogmessage, 1, 2, 0, 1);
1686 gtk_widget_show(dialogmessage);
1687
1688 dialog_textfield = textfield;
1689 if (textfield != NULL)
1690 {
1691 /* Add text entry field */
1692 dialog_textentry = gtk_entry_new();
1693 gtk_widget_set_usize(dialog_textentry, 400, -2);
1694 gtk_box_pack_start(GTK_BOX(vbox), dialog_textentry, TRUE, TRUE, 0);
1695 gtk_entry_set_text(GTK_ENTRY(dialog_textentry),
1696 (const gchar *)textfield);
1697 gtk_entry_select_region(GTK_ENTRY(dialog_textentry), 0,
1698 STRLEN(textfield));
1699 gtk_entry_set_max_length(GTK_ENTRY(dialog_textentry), IOSIZE - 1);
1700 gtk_entry_set_position(GTK_ENTRY(dialog_textentry), STRLEN(textfield));
1701 gtk_widget_show(dialog_textentry);
1702 }
1703
1704 /* Add box for buttons */
1705 action_area = gtk_hbox_new(FALSE, 0);
1706 gtk_container_border_width(GTK_CONTAINER(action_area), 4);
1707 gtk_box_pack_end(GTK_BOX(vbox), action_area, FALSE, TRUE, 0);
1708 gtk_widget_show(action_area);
1709
1710 /* Add a [vh]box in the hbox to center the buttons in the dialog. */
1711 if (vertical)
1712 sub_area = gtk_vbox_new(FALSE, 0);
1713 else
1714 sub_area = gtk_hbox_new(FALSE, 0);
1715 gtk_container_set_border_width(GTK_CONTAINER(sub_area), 0);
1716 gtk_box_pack_start(GTK_BOX(action_area), sub_area, TRUE, FALSE, 0);
1717 gtk_widget_show(sub_area);
1718
1719 /*
1720 * Create the buttons.
1721 */
1722
1723 /*
1724 * Translate the Vim accelerator character into an underscore for GTK+.
1725 * Double underscores to keep them in the label.
1726 */
1727 /* count the number of underscores */
1728 i = 1;
1729 for (p = buttons; *p; ++p)
1730 if (*p == '_')
1731 ++i;
1732
1733 /* make a copy of "buttons" with the translated characters */
1734 names = alloc(STRLEN(buttons) + i);
1735 if (names == NULL)
1736 return -1;
1737
1738 p = names;
1739 for (i = 0; buttons[i]; ++i)
1740 {
1741 if (buttons[i] == DLG_HOTKEY_CHAR)
1742 *p++ = '_';
1743 else
1744 {
1745 if (buttons[i] == '_')
1746 *p++ = '_';
1747 *p++ = buttons[i];
1748 }
1749 }
1750 *p = NUL;
1751
1752 /* Count the number of buttons and allocate button[] and data[]. */
1753 butcount = 1;
1754 for (p = names; *p; ++p)
1755 if (*p == DLG_BUTTON_SEP)
1756 ++butcount;
1757 button = (GtkWidget **)alloc((unsigned)(butcount * sizeof(GtkWidget *)));
1758 data = (ButtonData *)alloc((unsigned)(butcount * sizeof(ButtonData)));
1759 if (button == NULL || data == NULL)
1760 {
1761 vim_free(names);
1762 vim_free(button);
1763 vim_free(data);
1764 return -1;
1765 }
1766
1767 /* Attach the new accelerator group to the window. */
1768 accel_group = gtk_accel_group_new();
1769 gtk_accel_group_attach(accel_group, GTK_OBJECT(dialog));
1770
1771 p = names;
1772 for (butcount = 0; *p; ++butcount)
1773 {
1774 char_u *next;
1775 GtkWidget *label;
1776# ifdef GTK_USE_ACCEL
1777 guint accel_key;
1778# endif
1779
1780 /* Chunk out this single button. */
1781 for (next = p; *next; ++next)
1782 {
1783 if (*next == DLG_BUTTON_SEP)
1784 {
1785 *next++ = NUL;
1786 break;
1787 }
1788 }
1789
1790 button[butcount] = gtk_button_new();
1791 GTK_WIDGET_SET_FLAGS(button[butcount], GTK_CAN_DEFAULT);
1792
1793 label = gtk_accel_label_new("");
1794 gtk_accel_label_set_accel_widget(GTK_ACCEL_LABEL(label), dialog);
1795
1796# ifdef GTK_USE_ACCEL
1797 accel_key = gtk_label_parse_uline(GTK_LABEL(label), (const gchar *)p);
1798 /* Don't add accelator if 'winaltkeys' is "no". */
1799 if (accel_key != GDK_VoidSymbol)
1800 {
1801 gtk_widget_add_accelerator(button[butcount],
1802 "clicked",
1803 accel_group,
1804 accel_key, 0,
1805 0);
1806 }
1807# else
1808 (void)gtk_label_parse_uline(GTK_LABEL(label), (const gchar *)p);
1809# endif
1810
1811 gtk_container_add(GTK_CONTAINER(button[butcount]), label);
1812 gtk_widget_show_all(button[butcount]);
1813
1814 data[butcount].status = &dialog_status;
1815 data[butcount].index = butcount;
1816 data[butcount].dialog = dialog;
1817 gtk_signal_connect(GTK_OBJECT(button[butcount]),
1818 (const char *)"clicked",
1819 GTK_SIGNAL_FUNC(dlg_button_clicked),
1820 (gpointer) &data[butcount]);
1821
1822 gtk_box_pack_start(GTK_BOX(sub_area), button[butcount],
1823 TRUE, FALSE, 0);
1824 p = next;
1825 }
1826
1827 vim_free(names);
1828
1829 cancel_data.ignore_enter = FALSE;
1830 if (butcount > 0)
1831 {
1832 --def_but; /* 1 is first button */
1833 if (def_but >= butcount)
1834 def_but = -1;
1835 if (def_but >= 0)
1836 {
1837 gtk_widget_grab_focus(button[def_but]);
1838 gtk_widget_grab_default(button[def_but]);
1839 }
1840 else
1841 /* No default, ignore hitting Enter. */
1842 cancel_data.ignore_enter = TRUE;
1843 }
1844
1845 if (textfield != NULL)
1846 gtk_window_set_focus(GTK_WINDOW(dialog), dialog_textentry);
1847
1848 separator = gtk_hseparator_new();
1849 gtk_box_pack_end(GTK_BOX(vbox), separator, FALSE, TRUE, 0);
1850 gtk_widget_show(separator);
1851
1852 dialog_status = -1;
1853
1854 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
1855 GTK_WIDGET(dialog), VW_POS_MOUSE);
1856
1857 gtk_widget_show(dialog);
1858
1859 /* loop here until the dialog goes away */
1860 while (dialog_status == -1 && !dialog_destroyed
1861 && GTK_WIDGET_DRAWABLE(dialog))
1862 gtk_main_iteration_do(TRUE);
1863
1864 if (dialog_status < 0)
1865 dialog_status = 0;
1866 if (dialog_status != 1 && textfield != NULL)
1867 *textfield = NUL; /* dialog was cancelled */
1868
1869 /* let the garbage collector know that we don't need it any longer */
1870 gtk_accel_group_unref(accel_group);
1871
1872 vim_free(button);
1873 vim_free(data);
1874
1875 return dialog_status;
1876}
1877
1878#endif /* FEAT_GUI_DIALOG && !HAVE_GTK2 */
1879
1880
1881#if defined(FEAT_GUI_DIALOG) && defined(HAVE_GTK2)
1882
1883 static GtkWidget *
1884create_message_dialog(int type, char_u *title, char_u *message)
1885{
1886 GtkWidget *dialog;
1887 GtkMessageType message_type;
1888
1889 switch (type)
1890 {
1891 case VIM_ERROR: message_type = GTK_MESSAGE_ERROR; break;
1892 case VIM_WARNING: message_type = GTK_MESSAGE_WARNING; break;
1893 case VIM_QUESTION: message_type = GTK_MESSAGE_QUESTION; break;
1894 default: message_type = GTK_MESSAGE_INFO; break;
1895 }
1896
1897 message = CONVERT_TO_UTF8(message);
1898 dialog = gtk_message_dialog_new(GTK_WINDOW(gui.mainwin),
1899 GTK_DIALOG_DESTROY_WITH_PARENT,
1900 message_type,
1901 GTK_BUTTONS_NONE,
1902 "%s", (const char *)message);
1903 CONVERT_TO_UTF8_FREE(message);
1904
1905 if (title != NULL)
1906 {
1907 title = CONVERT_TO_UTF8(title);
1908 gtk_window_set_title(GTK_WINDOW(dialog), (const char *)title);
1909 CONVERT_TO_UTF8_FREE(title);
1910 }
1911 else if (type == VIM_GENERIC)
1912 {
1913 gtk_window_set_title(GTK_WINDOW(dialog), "VIM");
1914 }
1915
1916 return dialog;
1917}
1918
1919/*
1920 * Split up button_string into individual button labels by inserting
1921 * NUL bytes. Also replace the Vim-style mnemonic accelerator prefix
1922 * '&' with '_'. button_string must point to allocated memory!
1923 * Return an allocated array of pointers into button_string.
1924 */
1925 static char **
1926split_button_string(char_u *button_string, int *n_buttons)
1927{
1928 char **array;
1929 char_u *p;
1930 unsigned int count = 1;
1931
1932 for (p = button_string; *p != NUL; ++p)
1933 if (*p == DLG_BUTTON_SEP)
1934 ++count;
1935
1936 array = (char **)alloc((count + 1) * sizeof(char *));
1937 count = 0;
1938
1939 if (array != NULL)
1940 {
1941 array[count++] = (char *)button_string;
1942 for (p = button_string; *p != NUL; ++p)
1943 {
1944 if (*p == DLG_BUTTON_SEP)
1945 {
1946 *p = NUL;
1947 array[count++] = (char *)p + 1;
1948 }
1949 else if (*p == DLG_HOTKEY_CHAR)
1950 *p = '_';
1951#ifdef FEAT_MBYTE
1952 else if (has_mbyte)
1953 p += (*mb_ptr2len_check)(p) - 1;
1954#endif
1955 }
1956 array[count] = NULL; /* currently not relied upon, but doesn't hurt */
1957 }
1958
1959 *n_buttons = count;
1960 return array;
1961}
1962
1963 static char **
1964split_button_translation(const char *message)
1965{
1966 char **buttons = NULL;
1967 char_u *str;
1968 int n_buttons = 0;
1969 int n_expected = 1;
1970
1971 for (str = (char_u *)message; *str != NUL; ++str)
1972 if (*str == DLG_BUTTON_SEP)
1973 ++n_expected;
1974
1975 str = (char_u *)_(message);
1976 if (str != NULL)
1977 {
1978 if (output_conv.vc_type != CONV_NONE)
1979 str = string_convert(&output_conv, str, NULL);
1980 else
1981 str = vim_strsave(str);
1982
1983 if (str != NULL)
1984 buttons = split_button_string(str, &n_buttons);
1985 }
1986 /*
1987 * Uh-oh... this should never ever happen. But we don't wanna crash
1988 * if the translation is broken, thus fall back to the untranslated
1989 * buttons string in case of emergency.
1990 */
1991 if (buttons == NULL || n_buttons != n_expected)
1992 {
1993 vim_free(buttons);
1994 vim_free(str);
1995 buttons = NULL;
1996 str = vim_strsave((char_u *)message);
1997
1998 if (str != NULL)
1999 buttons = split_button_string(str, &n_buttons);
2000 if (buttons == NULL)
2001 vim_free(str);
2002 }
2003
2004 return buttons;
2005}
2006
2007 static int
2008button_equal(const char *a, const char *b)
2009{
2010 while (*a != '\0' && *b != '\0')
2011 {
2012 if (*a == '_' && *++a == '\0')
2013 break;
2014 if (*b == '_' && *++b == '\0')
2015 break;
2016
2017 if (g_unichar_tolower(g_utf8_get_char(a))
2018 != g_unichar_tolower(g_utf8_get_char(b)))
2019 return FALSE;
2020
2021 a = g_utf8_next_char(a);
2022 b = g_utf8_next_char(b);
2023 }
2024
2025 return (*a == '\0' && *b == '\0');
2026}
2027
2028 static void
2029dialog_add_buttons(GtkDialog *dialog, char_u *button_string)
2030{
2031 char **ok;
2032 char **ync; /* "yes no cancel" */
2033 char **buttons;
2034 int n_buttons = 0;
2035 int index;
2036
2037 button_string = vim_strsave(button_string); /* must be writable */
2038 if (button_string == NULL)
2039 return;
2040
2041 /* Check 'v' flag in 'guioptions': vertical button placement. */
2042 if (vim_strchr(p_go, GO_VERTICAL) != NULL)
2043 {
2044 GtkWidget *vbutton_box;
2045
2046 vbutton_box = gtk_vbutton_box_new();
2047 gtk_widget_show(vbutton_box);
2048 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2049 vbutton_box, TRUE, FALSE, 0);
2050 /* Overrule the "action_area" value, hopefully this works... */
2051 GTK_DIALOG(dialog)->action_area = vbutton_box;
2052 }
2053
2054 /*
2055 * Yes this is ugly, I don't particularly like it either. But doing it
2056 * this way has the compelling advantage that translations need not to
2057 * be touched at all. See below what 'ok' and 'ync' are used for.
2058 */
2059 ok = split_button_translation(N_("&Ok"));
2060 ync = split_button_translation(N_("&Yes\n&No\n&Cancel"));
2061 buttons = split_button_string(button_string, &n_buttons);
2062
2063 /*
2064 * Yes, the buttons are in reversed order to match the GNOME 2 desktop
2065 * environment. Don't hit me -- it's all about consistency.
2066 * Well, apparently somebody changed his mind: with GTK 2.2.4 it works the
2067 * other way around...
2068 */
2069 for (index = 1; index <= n_buttons; ++index)
2070 {
2071 char *label;
2072 char_u *label8;
2073
2074 label = buttons[index - 1];
2075 /*
2076 * Perform some guesswork to find appropriate stock items for the
2077 * buttons. We have to compare with a sample of the translated
2078 * button string to get things right. Yes, this is hackish :/
2079 *
2080 * But even the common button labels aren't necessarily translated,
2081 * since anyone can create their own dialogs using Vim functions.
2082 * Thus we have to check for those too.
2083 */
2084 if (ok != NULL && ync != NULL) /* almost impossible to fail */
2085 {
2086 if (button_equal(label, ok[0])) label = GTK_STOCK_OK;
2087 else if (button_equal(label, ync[0])) label = GTK_STOCK_YES;
2088 else if (button_equal(label, ync[1])) label = GTK_STOCK_NO;
2089 else if (button_equal(label, ync[2])) label = GTK_STOCK_CANCEL;
2090 else if (button_equal(label, "Ok")) label = GTK_STOCK_OK;
2091 else if (button_equal(label, "Yes")) label = GTK_STOCK_YES;
2092 else if (button_equal(label, "No")) label = GTK_STOCK_NO;
2093 else if (button_equal(label, "Cancel")) label = GTK_STOCK_CANCEL;
2094 }
2095 label8 = CONVERT_TO_UTF8((char_u *)label);
2096 gtk_dialog_add_button(dialog, (const gchar *)label8, index);
2097 CONVERT_TO_UTF8_FREE(label8);
2098 }
2099
2100 if (ok != NULL)
2101 vim_free(*ok);
2102 if (ync != NULL)
2103 vim_free(*ync);
2104 vim_free(ok);
2105 vim_free(ync);
2106 vim_free(buttons);
2107 vim_free(button_string);
2108}
2109
2110/*
2111 * Allow mnemonic accelerators to be activated without pressing <Alt>.
2112 * I'm not sure if it's a wise idea to do this. However, the old GTK+ 1.2
2113 * GUI used to work this way, and I consider the impact on UI consistency
2114 * low enough to justify implementing this as a special Vim feature.
2115 */
2116typedef struct _DialogInfo
2117{
2118 int ignore_enter; /* no default button, ignore "Enter" */
2119 int noalt; /* accept accelerators without Alt */
2120 GtkDialog *dialog; /* Widget of the dialog */
2121} DialogInfo;
2122
2123/*ARGSUSED2*/
2124 static gboolean
2125dialog_key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
2126{
2127 DialogInfo *di = (DialogInfo *)data;
2128
2129 /* Ignore hitting "Enter" if there is no default button. */
2130 if (di->ignore_enter && event->keyval == GDK_Return)
2131 return TRUE;
2132
2133 /* Close the dialog when hitting "Esc". */
2134 if (event->keyval == GDK_Escape)
2135 {
2136 gtk_dialog_response(di->dialog, GTK_RESPONSE_REJECT);
2137 return TRUE;
2138 }
2139
2140 if (di->noalt
2141 && (event->state & gtk_accelerator_get_default_mod_mask()) == 0)
2142 {
2143 return gtk_window_mnemonic_activate(
2144 GTK_WINDOW(widget), event->keyval,
2145 gtk_window_get_mnemonic_modifier(GTK_WINDOW(widget)));
2146 }
2147
2148 return FALSE; /* continue emission */
2149}
2150
2151 int
2152gui_mch_dialog(int type, /* type of dialog */
2153 char_u *title, /* title of dialog */
2154 char_u *message, /* message text */
2155 char_u *buttons, /* names of buttons */
2156 int def_but, /* default button */
2157 char_u *textfield) /* text for textfield or NULL */
2158{
2159 GtkWidget *dialog;
2160 GtkWidget *entry = NULL;
2161 char_u *text;
2162 int response;
2163 DialogInfo dialoginfo;
2164
2165 dialog = create_message_dialog(type, title, message);
2166 dialoginfo.dialog = GTK_DIALOG(dialog);
2167 dialog_add_buttons(GTK_DIALOG(dialog), buttons);
2168
2169 if (textfield != NULL)
2170 {
2171 GtkWidget *alignment;
2172
2173 entry = gtk_entry_new();
2174 gtk_widget_show(entry);
2175
2176 text = CONVERT_TO_UTF8(textfield);
2177 gtk_entry_set_text(GTK_ENTRY(entry), (const char *)text);
2178 CONVERT_TO_UTF8_FREE(text);
2179
2180 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2181 (float)1.0, (float)1.0);
2182 gtk_container_add(GTK_CONTAINER(alignment), entry);
2183 gtk_container_set_border_width(GTK_CONTAINER(alignment), 5);
2184 gtk_widget_show(alignment);
2185
2186 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox),
2187 alignment, TRUE, FALSE, 0);
2188 dialoginfo.noalt = FALSE;
2189 }
2190 else
2191 dialoginfo.noalt = TRUE;
2192
2193 /* Allow activation of mnemonic accelerators without pressing <Alt> when
2194 * there is no textfield. Handle pressing Enter and Esc. */
2195 g_signal_connect(G_OBJECT(dialog), "key_press_event",
2196 G_CALLBACK(&dialog_key_press_event_cb), &dialoginfo);
2197
2198 if (def_but > 0)
2199 {
2200 gtk_dialog_set_default_response(GTK_DIALOG(dialog), def_but);
2201 dialoginfo.ignore_enter = FALSE;
2202 }
2203 else
2204 /* No default button, ignore pressing Enter. */
2205 dialoginfo.ignore_enter = TRUE;
2206
2207 /* Show the mouse pointer if it's currently hidden. */
2208 gui_mch_mousehide(FALSE);
2209
2210 response = gtk_dialog_run(GTK_DIALOG(dialog));
2211
2212 /* GTK_RESPONSE_NONE means the dialog was programmatically destroyed. */
2213 if (response != GTK_RESPONSE_NONE)
2214 {
2215 if (textfield != NULL)
2216 {
2217 text = (char_u *)gtk_entry_get_text(GTK_ENTRY(entry));
2218 text = CONVERT_FROM_UTF8(text);
2219
2220 STRNCPY(textfield, text, IOSIZE);
2221 textfield[IOSIZE - 1] = NUL;
2222
2223 CONVERT_FROM_UTF8_FREE(text);
2224 }
2225 gtk_widget_destroy(dialog);
2226 }
2227
2228 /* Terrible hack: When the text area still has focus when we remove the
2229 * dialog, somehow gvim loses window focus. This is with "point to type"
2230 * in the KDE 3.1 window manager. Warp the mouse pointer to outside the
2231 * window and back to avoid that. */
2232 if (!gui.in_focus)
2233 {
2234 int x, y;
2235
2236 gdk_window_get_pointer(gui.drawarea->window, &x, &y, NULL);
2237 gui_mch_setmouse(-100, -100);
2238 gui_mch_setmouse(x, y);
2239 }
2240
2241 return response > 0 ? response : 0;
2242}
2243
2244#endif /* FEAT_GUI_DIALOG && HAVE_GTK2 */
2245
2246
2247#if defined(FEAT_MENU) || defined(PROTO)
2248
2249 void
2250gui_mch_show_popupmenu(vimmenu_T *menu)
2251{
2252# if defined(FEAT_XIM) && defined(HAVE_GTK2)
2253 /*
2254 * Append a submenu for selecting an input method. This is
2255 * currently the only way to switch input methods at runtime.
2256 */
2257 if (xic != NULL && g_object_get_data(G_OBJECT(menu->submenu_id),
2258 "vim-has-im-menu") == NULL)
2259 {
2260 GtkWidget *menuitem;
2261 GtkWidget *submenu;
2262 char_u *name;
2263
2264 menuitem = gtk_separator_menu_item_new();
2265 gtk_widget_show(menuitem);
2266 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
2267
2268 name = (char_u *)_("Input _Methods");
2269 name = CONVERT_TO_UTF8(name);
2270 menuitem = gtk_menu_item_new_with_mnemonic((const char *)name);
2271 CONVERT_TO_UTF8_FREE(name);
2272 gtk_widget_show(menuitem);
2273
2274 submenu = gtk_menu_new();
2275 gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
2276 gtk_menu_shell_append(GTK_MENU_SHELL(menu->submenu_id), menuitem);
2277
2278 gtk_im_multicontext_append_menuitems(GTK_IM_MULTICONTEXT(xic),
2279 GTK_MENU_SHELL(submenu));
2280 g_object_set_data(G_OBJECT(menu->submenu_id),
2281 "vim-has-im-menu", GINT_TO_POINTER(TRUE));
2282 }
2283# endif /* FEAT_XIM && HAVE_GTK2 */
2284
2285 gtk_menu_popup(GTK_MENU(menu->submenu_id),
2286 NULL, NULL,
2287 (GtkMenuPositionFunc)NULL, NULL,
2288 3U, (guint32)GDK_CURRENT_TIME);
2289}
2290
2291/*
2292 * Menu position callback; used by gui_make_popup() to place the menu
2293 * at the current text cursor position.
2294 *
2295 * Note: The push_in output argument seems to affect scrolling of huge
2296 * menus that don't fit on the screen. Leave it at the default for now.
2297 */
2298/*ARGSUSED0*/
2299 static void
2300popup_menu_position_func(GtkMenu *menu,
2301 gint *x, gint *y,
2302# ifdef HAVE_GTK2
2303 gboolean *push_in,
2304# endif
2305 gpointer user_data)
2306{
2307 if (curwin != NULL && gui.drawarea != NULL && gui.drawarea->window != NULL)
2308 {
2309 gdk_window_get_origin(gui.drawarea->window, x, y);
2310
2311 /* Find the cursor position in the current window */
2312 *x += FILL_X(W_WINCOL(curwin) + curwin->w_wcol + 1) + 1;
2313 *y += FILL_Y(W_WINROW(curwin) + curwin->w_wrow + 1) + 1;
2314 }
2315}
2316
2317 void
2318gui_make_popup(char_u *path_name)
2319{
2320 vimmenu_T *menu;
2321
2322 menu = gui_find_menu(path_name);
2323
2324 if (menu != NULL && menu->submenu_id != NULL)
2325 {
2326 gtk_menu_popup(GTK_MENU(menu->submenu_id),
2327 NULL, NULL,
2328 &popup_menu_position_func, NULL,
2329 0U, (guint32)GDK_CURRENT_TIME);
2330 }
2331}
2332
2333#endif /* FEAT_MENU */
2334
2335
2336/*
2337 * We don't create it twice.
2338 */
2339
2340typedef struct _SharedFindReplace
2341{
2342 GtkWidget *dialog; /* the main dialog widget */
2343 GtkWidget *wword; /* 'Whole word only' check button */
2344 GtkWidget *mcase; /* 'Match case' check button */
2345 GtkWidget *up; /* search direction 'Up' radio button */
2346 GtkWidget *down; /* search direction 'Down' radio button */
2347 GtkWidget *what; /* 'Find what' entry text widget */
2348 GtkWidget *with; /* 'Replace with' entry text widget */
2349 GtkWidget *find; /* 'Find Next' action button */
2350 GtkWidget *replace; /* 'Replace With' action button */
2351 GtkWidget *all; /* 'Replace All' action button */
2352} SharedFindReplace;
2353
2354static SharedFindReplace find_widgets = { NULL, };
2355static SharedFindReplace repl_widgets = { NULL, };
2356
2357/* ARGSUSED */
2358 static int
2359find_key_press_event(
2360 GtkWidget *widget,
2361 GdkEventKey *event,
2362 SharedFindReplace *frdp)
2363{
2364 /* If the user is holding one of the key modifiers we will just bail out,
2365 * thus preserving the possibility of normal focus traversal.
2366 */
2367 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
2368 return FALSE;
2369
2370 /* the Escape key synthesizes a cancellation action */
2371 if (event->keyval == GDK_Escape)
2372 {
2373 gtk_widget_hide(frdp->dialog);
2374
2375 return TRUE;
2376 }
2377 /*
2378 * What the **** is this for? Disabled for GTK+ 2 because due to
2379 * gtk_signal_connect_after() it doesn't have any effect anyway.
2380 * (Fortunately.)
2381 */
2382#ifndef HAVE_GTK2
2383 /* block traversal resulting from those keys */
2384 if (event->keyval == GDK_Left
2385 || event->keyval == GDK_Right
2386 || event->keyval == GDK_space)
2387 return TRUE;
2388#endif
2389
2390 /* It would be delightfull if it where possible to do search history
2391 * operations on the K_UP and K_DOWN keys here.
2392 */
2393
2394 return FALSE;
2395}
2396
2397#ifdef HAVE_GTK2
2398 static GtkWidget *
2399create_image_button(const char *stock_id, const char *label)
2400{
2401 char_u *text;
2402 GtkWidget *box;
2403 GtkWidget *alignment;
2404 GtkWidget *button;
2405
2406 text = CONVERT_TO_UTF8((char_u *)label);
2407
2408 box = gtk_hbox_new(FALSE, 3);
2409 gtk_box_pack_start(GTK_BOX(box),
2410 gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_BUTTON),
2411 FALSE, FALSE, 0);
2412 gtk_box_pack_start(GTK_BOX(box),
2413 gtk_label_new((const char *)text),
2414 FALSE, FALSE, 0);
2415
2416 CONVERT_TO_UTF8_FREE(text);
2417
2418 alignment = gtk_alignment_new((float)0.5, (float)0.5,
2419 (float)0.0, (float)0.0);
2420 gtk_container_add(GTK_CONTAINER(alignment), box);
2421 gtk_widget_show_all(alignment);
2422
2423 button = gtk_button_new();
2424 gtk_container_add(GTK_CONTAINER(button), alignment);
2425
2426 return button;
2427}
2428
2429/*
2430 * This is currently only used by find_replace_dialog_create(), and
2431 * I'd really like to keep it at that. In other words: don't spread
2432 * this nasty hack all over the code. Think twice.
2433 */
2434 static const char *
2435convert_localized_message(char_u **buffer, const char *message)
2436{
2437 if (output_conv.vc_type == CONV_NONE)
2438 return message;
2439
2440 vim_free(*buffer);
2441 *buffer = string_convert(&output_conv, (char_u *)message, NULL);
2442
2443 return (const char *)*buffer;
2444}
2445#endif /* HAVE_GTK2 */
2446
2447 static void
2448find_replace_dialog_create(char_u *arg, int do_replace)
2449{
2450#ifndef HAVE_GTK2
2451 GtkWidget *frame;
2452#endif
2453 GtkWidget *hbox; /* main top down box */
2454 GtkWidget *actionarea;
2455 GtkWidget *table;
2456 GtkWidget *tmp;
2457 GtkWidget *vbox;
2458 gboolean sensitive;
2459 SharedFindReplace *frdp;
2460 char_u *entry_text;
2461 int wword = FALSE;
2462 int mcase = !p_ic;
2463#ifdef HAVE_GTK2
2464 char_u *conv_buffer = NULL;
2465# define CONV(message) convert_localized_message(&conv_buffer, (message))
2466#else
2467# define CONV(message) (message)
2468#endif
2469
2470 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2471
2472 /* Get the search string to use. */
2473 entry_text = get_find_dialog_text(arg, &wword, &mcase);
2474
2475#ifdef HAVE_GTK2
2476 if (entry_text != NULL && output_conv.vc_type != CONV_NONE)
2477 {
2478 char_u *old_text = entry_text;
2479 entry_text = string_convert(&output_conv, entry_text, NULL);
2480 vim_free(old_text);
2481 }
2482#endif
2483
2484 /*
2485 * If the dialog already exists, just raise it.
2486 */
2487 if (frdp->dialog)
2488 {
2489#ifndef HAVE_GTK2
2490 /* always make the dialog appear where you want it even if the mainwin
2491 * has moved -- dbv */
2492 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
2493 GTK_WIDGET(frdp->dialog), VW_POS_MOUSE);
2494 gui_gtk_synch_fonts();
2495
2496 if (!GTK_WIDGET_VISIBLE(frdp->dialog))
2497 {
2498 gtk_widget_grab_focus(frdp->what);
2499 gtk_widget_show(frdp->dialog);
2500 }
2501#endif
2502 if (entry_text != NULL)
2503 {
2504 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2505 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2506 (gboolean)wword);
2507 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2508 (gboolean)mcase);
2509 }
2510#ifdef HAVE_GTK2
2511 gtk_window_present(GTK_WINDOW(frdp->dialog));
2512#else
2513 gdk_window_raise(frdp->dialog->window);
2514#endif
2515 vim_free(entry_text);
2516 return;
2517 }
2518
2519#ifdef HAVE_GTK2
2520 frdp->dialog = gtk_dialog_new();
2521 gtk_dialog_set_has_separator(GTK_DIALOG(frdp->dialog), FALSE);
2522 gtk_window_set_transient_for(GTK_WINDOW(frdp->dialog), GTK_WINDOW(gui.mainwin));
2523 gtk_window_set_destroy_with_parent(GTK_WINDOW(frdp->dialog), TRUE);
2524#else
2525 frdp->dialog = gtk_window_new(GTK_WINDOW_DIALOG);
2526#endif
2527
2528 if (do_replace)
2529 {
2530#ifndef HAVE_GTK2
2531 gtk_window_set_wmclass(GTK_WINDOW(frdp->dialog), "searchrepl", "gvim");
2532#endif
2533 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2534 CONV(_("VIM - Search and Replace...")));
2535 }
2536 else
2537 {
2538#ifndef HAVE_GTK2
2539 gtk_window_set_wmclass(GTK_WINDOW(frdp->dialog), "search", "gvim");
2540#endif
2541 gtk_window_set_title(GTK_WINDOW(frdp->dialog),
2542 CONV(_("VIM - Search...")));
2543 }
2544
2545#ifndef HAVE_GTK2 /* Utter crack. Shudder. */
2546 gtk_widget_realize(frdp->dialog);
2547 gdk_window_set_decorations(frdp->dialog->window,
2548 GDK_DECOR_TITLE | GDK_DECOR_BORDER | GDK_DECOR_RESIZEH);
2549 gdk_window_set_functions(frdp->dialog->window,
2550 GDK_FUNC_RESIZE | GDK_FUNC_MOVE);
2551#endif
2552
2553#ifndef HAVE_GTK2
2554 /* this makes it look better on Motif style window managers */
2555 frame = gtk_frame_new(NULL);
2556 gtk_container_add(GTK_CONTAINER(frdp->dialog), frame);
2557#endif
2558
2559 hbox = gtk_hbox_new(FALSE, 0);
2560#ifdef HAVE_GTK2
2561 gtk_container_set_border_width(GTK_CONTAINER(hbox), 10);
2562 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(frdp->dialog)->vbox), hbox);
2563#else
2564 gtk_container_add(GTK_CONTAINER(frame), hbox);
2565#endif
2566
2567 if (do_replace)
2568 table = gtk_table_new(1024, 4, FALSE);
2569 else
2570 table = gtk_table_new(1024, 3, FALSE);
2571 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
2572 gtk_container_border_width(GTK_CONTAINER(table), 4);
2573
2574 tmp = gtk_label_new(CONV(_("Find what:")));
2575 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2576 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 0, 1,
2577 GTK_FILL, GTK_EXPAND, 2, 2);
2578 frdp->what = gtk_entry_new();
2579 sensitive = (entry_text != NULL && entry_text[0] != NUL);
2580 if (entry_text != NULL)
2581 gtk_entry_set_text(GTK_ENTRY(frdp->what), (char *)entry_text);
2582 gtk_signal_connect(GTK_OBJECT(frdp->what), "changed",
2583 GTK_SIGNAL_FUNC(entry_changed_cb), frdp->dialog);
2584 gtk_signal_connect_after(GTK_OBJECT(frdp->what), "key_press_event",
2585 GTK_SIGNAL_FUNC(find_key_press_event),
2586 (gpointer) frdp);
2587 gtk_table_attach(GTK_TABLE(table), frdp->what, 1, 1024, 0, 1,
2588 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2589
2590 if (do_replace)
2591 {
2592 tmp = gtk_label_new(CONV(_("Replace with:")));
2593 gtk_misc_set_alignment(GTK_MISC(tmp), (gfloat)0.0, (gfloat)0.5);
2594 gtk_table_attach(GTK_TABLE(table), tmp, 0, 1, 1, 2,
2595 GTK_FILL, GTK_EXPAND, 2, 2);
2596 frdp->with = gtk_entry_new();
2597 gtk_signal_connect(GTK_OBJECT(frdp->with), "activate",
2598 GTK_SIGNAL_FUNC(find_replace_cb),
2599 GINT_TO_POINTER(FRD_R_FINDNEXT));
2600 gtk_signal_connect_after(GTK_OBJECT(frdp->with), "key_press_event",
2601 GTK_SIGNAL_FUNC(find_key_press_event),
2602 (gpointer) frdp);
2603 gtk_table_attach(GTK_TABLE(table), frdp->with, 1, 1024, 1, 2,
2604 GTK_EXPAND | GTK_FILL, GTK_EXPAND, 2, 2);
2605
2606 /*
2607 * Make the entry activation only change the input focus onto the
2608 * with item.
2609 */
2610 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2611 GTK_SIGNAL_FUNC(entry_activate_cb), frdp->with);
2612 }
2613 else
2614 {
2615 /*
2616 * Make the entry activation do the search.
2617 */
2618 gtk_signal_connect(GTK_OBJECT(frdp->what), "activate",
2619 GTK_SIGNAL_FUNC(find_replace_cb),
2620 GINT_TO_POINTER(FRD_FINDNEXT));
2621 }
2622
2623 /* whole word only button */
2624 frdp->wword = gtk_check_button_new_with_label(CONV(_("Match whole word only")));
2625 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->wword),
2626 (gboolean)wword);
2627 if (do_replace)
2628 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 2, 3,
2629 GTK_FILL, GTK_EXPAND, 2, 2);
2630 else
2631 gtk_table_attach(GTK_TABLE(table), frdp->wword, 0, 1023, 1, 2,
2632 GTK_FILL, GTK_EXPAND, 2, 2);
2633
2634 /* match case button */
2635 frdp->mcase = gtk_check_button_new_with_label(CONV(_("Match case")));
2636 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->mcase),
2637 (gboolean)mcase);
2638 if (do_replace)
2639 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 3, 4,
2640 GTK_FILL, GTK_EXPAND, 2, 2);
2641 else
2642 gtk_table_attach(GTK_TABLE(table), frdp->mcase, 0, 1023, 2, 3,
2643 GTK_FILL, GTK_EXPAND, 2, 2);
2644
2645 tmp = gtk_frame_new(CONV(_("Direction")));
2646 if (do_replace)
2647 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 2, 4,
2648 GTK_FILL, GTK_FILL, 2, 2);
2649 else
2650 gtk_table_attach(GTK_TABLE(table), tmp, 1023, 1024, 1, 3,
2651 GTK_FILL, GTK_FILL, 2, 2);
2652 vbox = gtk_vbox_new(FALSE, 0);
2653 gtk_container_border_width(GTK_CONTAINER(vbox), 0);
2654 gtk_container_add(GTK_CONTAINER(tmp), vbox);
2655
2656 /* 'Up' and 'Down' buttons */
2657 frdp->up = gtk_radio_button_new_with_label(NULL, CONV(_("Up")));
2658 gtk_box_pack_start(GTK_BOX(vbox), frdp->up, TRUE, TRUE, 0);
2659 frdp->down = gtk_radio_button_new_with_label(
2660 gtk_radio_button_group(GTK_RADIO_BUTTON(frdp->up)),
2661 CONV(_("Down")));
2662 gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(frdp->down), TRUE);
2663#ifdef HAVE_GTK2
2664 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
2665#endif
2666 gtk_box_pack_start(GTK_BOX(vbox), frdp->down, TRUE, TRUE, 0);
2667
2668 /* vbox to hold the action buttons */
2669 actionarea = gtk_vbutton_box_new();
2670 gtk_container_border_width(GTK_CONTAINER(actionarea), 2);
2671#ifndef HAVE_GTK2
2672 if (do_replace)
2673 {
2674 gtk_button_box_set_layout(GTK_BUTTON_BOX(actionarea),
2675 GTK_BUTTONBOX_END);
2676 gtk_button_box_set_spacing(GTK_BUTTON_BOX(actionarea), 0);
2677 }
2678#endif
2679 gtk_box_pack_end(GTK_BOX(hbox), actionarea, FALSE, FALSE, 0);
2680
2681 /* 'Find Next' button */
2682#ifdef HAVE_GTK2
2683 frdp->find = create_image_button(GTK_STOCK_FIND, _("Find Next"));
2684#else
2685 frdp->find = gtk_button_new_with_label(_("Find Next"));
2686#endif
2687 gtk_widget_set_sensitive(frdp->find, sensitive);
2688
2689 gtk_signal_connect(GTK_OBJECT(frdp->find), "clicked",
2690 GTK_SIGNAL_FUNC(find_replace_cb),
2691 (do_replace) ? GINT_TO_POINTER(FRD_R_FINDNEXT)
2692 : GINT_TO_POINTER(FRD_FINDNEXT));
2693
2694 GTK_WIDGET_SET_FLAGS(frdp->find, GTK_CAN_DEFAULT);
2695 gtk_box_pack_start(GTK_BOX(actionarea), frdp->find, FALSE, FALSE, 0);
2696 gtk_widget_grab_default(frdp->find);
2697
2698 if (do_replace)
2699 {
2700 /* 'Replace' button */
2701#ifdef HAVE_GTK2
2702 frdp->replace = create_image_button(GTK_STOCK_CONVERT, _("Replace"));
2703#else
2704 frdp->replace = gtk_button_new_with_label(_("Replace"));
2705#endif
2706 gtk_widget_set_sensitive(frdp->replace, sensitive);
2707 GTK_WIDGET_SET_FLAGS(frdp->replace, GTK_CAN_DEFAULT);
2708 gtk_box_pack_start(GTK_BOX(actionarea), frdp->replace, FALSE, FALSE, 0);
2709 gtk_signal_connect(GTK_OBJECT(frdp->replace), "clicked",
2710 GTK_SIGNAL_FUNC(find_replace_cb),
2711 GINT_TO_POINTER(FRD_REPLACE));
2712
2713 /* 'Replace All' button */
2714#ifdef HAVE_GTK2
2715 frdp->all = create_image_button(GTK_STOCK_CONVERT, _("Replace All"));
2716#else
2717 frdp->all = gtk_button_new_with_label(_("Replace All"));
2718#endif
2719 gtk_widget_set_sensitive(frdp->all, sensitive);
2720 GTK_WIDGET_SET_FLAGS(frdp->all, GTK_CAN_DEFAULT);
2721 gtk_box_pack_start(GTK_BOX(actionarea), frdp->all, FALSE, FALSE, 0);
2722 gtk_signal_connect(GTK_OBJECT(frdp->all), "clicked",
2723 GTK_SIGNAL_FUNC(find_replace_cb),
2724 GINT_TO_POINTER(FRD_REPLACEALL));
2725 }
2726
2727 /* 'Cancel' button */
2728#ifdef HAVE_GTK2
2729 tmp = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
2730#else
2731 tmp = gtk_button_new_with_label(_("Cancel"));
2732#endif
2733 GTK_WIDGET_SET_FLAGS(tmp, GTK_CAN_DEFAULT);
2734 gtk_box_pack_end(GTK_BOX(actionarea), tmp, FALSE, FALSE, 0);
2735 gtk_signal_connect_object(GTK_OBJECT(tmp),
2736 "clicked", GTK_SIGNAL_FUNC(gtk_widget_hide),
2737 GTK_OBJECT(frdp->dialog));
2738 gtk_signal_connect_object(GTK_OBJECT(frdp->dialog),
2739 "delete_event", GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete),
2740 GTK_OBJECT(frdp->dialog));
2741
2742 tmp = gtk_vseparator_new();
2743#ifdef HAVE_GTK2
2744 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, FALSE, 10);
2745#else
2746 gtk_box_pack_end(GTK_BOX(hbox), tmp, FALSE, TRUE, 0);
2747#endif
2748
2749#ifndef HAVE_GTK2
2750 gtk_widget_grab_focus(frdp->what);
2751
2752 /* show the frame and realize the frdp->dialog this gives us a window size
2753 * request that we'll use to position the window within the boundary of
2754 * the mainwin --dbv */
2755 gtk_widget_show_all(frame);
2756 gui_gtk_position_in_parent(GTK_WIDGET(gui.mainwin),
2757 GTK_WIDGET(frdp->dialog), VW_POS_MOUSE);
2758 gui_gtk_synch_fonts();
2759 gtk_widget_show_all(frdp->dialog);
2760#endif
2761
2762#ifdef HAVE_GTK2
2763 /* Suppress automatic show of the unused action area */
2764 gtk_widget_hide(GTK_DIALOG(frdp->dialog)->action_area);
2765 gtk_widget_show_all(hbox);
2766 gtk_widget_show(frdp->dialog);
2767#endif
2768
2769 vim_free(entry_text);
2770#ifdef HAVE_GTK2
2771 vim_free(conv_buffer);
2772#endif
2773#undef CONV
2774}
2775
2776 void
2777gui_mch_find_dialog(exarg_T *eap)
2778{
2779 if (gui.in_use)
2780 find_replace_dialog_create(eap->arg, FALSE);
2781}
2782
2783 void
2784gui_mch_replace_dialog(exarg_T *eap)
2785{
2786 if (gui.in_use)
2787 find_replace_dialog_create(eap->arg, TRUE);
2788}
2789
2790
2791#if !defined(HAVE_GTK2) || defined(PROTO)
2792/*
2793 * Synchronize all gui elements, which are dependant upon the
2794 * main text font used. Those are in esp. the find/replace dialogs.
2795 * If you don't understand why this should be needed, please try to
2796 * search for "pięść" in iso8859-2.
2797 *
2798 * (<danielk> I converted the comment above to UTF-8 to put
2799 * a stopper to the encoding mess. Forgive me :)
2800 *
2801 * Obsolete with GTK2.
2802 */
2803 void
2804gui_gtk_synch_fonts(void)
2805{
2806 SharedFindReplace *frdp;
2807 int do_replace;
2808
2809 /* OK this loop is a bit tricky... */
2810 for (do_replace = 0; do_replace <= 1; ++do_replace)
2811 {
2812 frdp = (do_replace) ? (&repl_widgets) : (&find_widgets);
2813 if (frdp->dialog)
2814 {
2815 GtkStyle *style;
2816
2817 /* synch the font with whats used by the text itself */
2818 style = gtk_style_copy(gtk_widget_get_style(frdp->what));
2819 gdk_font_unref(style->font);
2820# ifdef FEAT_XFONTSET
2821 if (gui.fontset != NOFONTSET)
2822 style->font = gui.fontset;
2823 else
2824# endif
2825 style->font = gui.norm_font;
2826 gdk_font_ref(style->font);
2827 gtk_widget_set_style(frdp->what, style);
2828 gtk_style_unref(style);
2829 if (do_replace)
2830 {
2831 style = gtk_style_copy(gtk_widget_get_style(frdp->with));
2832 gdk_font_unref(style->font);
2833# ifdef FEAT_XFONTSET
2834 if (gui.fontset != NOFONTSET)
2835 style->font = gui.fontset;
2836 else
2837# endif
2838 style->font = gui.norm_font;
2839 gdk_font_ref(style->font);
2840 gtk_widget_set_style(frdp->with, style);
2841 gtk_style_unref(style);
2842 }
2843 }
2844 }
2845}
2846#endif /* !HAVE_GTK2 */
2847
2848
2849/*
2850 * Callback for actions of the find and replace dialogs
2851 */
2852/*ARGSUSED*/
2853 static void
2854find_replace_cb(GtkWidget *widget, gpointer data)
2855{
2856 int flags;
2857 char_u *find_text;
2858 char_u *repl_text;
2859 gboolean direction_down;
2860 SharedFindReplace *sfr;
2861 int rc;
2862
2863 flags = (int)(long)data; /* avoid a lint warning here */
2864
2865 /* Get the search/replace strings from the dialog */
2866 if (flags == FRD_FINDNEXT)
2867 {
2868 repl_text = NULL;
2869 sfr = &find_widgets;
2870 }
2871 else
2872 {
2873 repl_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(repl_widgets.with));
2874 sfr = &repl_widgets;
2875 }
2876
2877 find_text = (char_u *)gtk_entry_get_text(GTK_ENTRY(sfr->what));
2878 direction_down = GTK_TOGGLE_BUTTON(sfr->down)->active;
2879
2880 if (GTK_TOGGLE_BUTTON(sfr->wword)->active)
2881 flags |= FRD_WHOLE_WORD;
2882 if (GTK_TOGGLE_BUTTON(sfr->mcase)->active)
2883 flags |= FRD_MATCH_CASE;
2884
2885#ifdef HAVE_GTK2
2886 repl_text = CONVERT_FROM_UTF8(repl_text);
2887 find_text = CONVERT_FROM_UTF8(find_text);
2888#endif
2889 rc = gui_do_findrepl(flags, find_text, repl_text, direction_down);
2890#ifdef HAVE_GTK2
2891 CONVERT_FROM_UTF8_FREE(repl_text);
2892 CONVERT_FROM_UTF8_FREE(find_text);
2893#endif
2894
2895 if (rc && gtk_main_level() > 0)
2896 gtk_main_quit(); /* make sure cmd will be handled immediately */
2897}
2898
2899/* our usual callback function */
2900/*ARGSUSED*/
2901 static void
2902entry_activate_cb(GtkWidget *widget, gpointer data)
2903{
2904 gtk_widget_grab_focus(GTK_WIDGET(data));
2905}
2906
2907/*
2908 * Syncing the find/replace dialogs on the fly is utterly useless crack,
2909 * and causes nothing but problems. Please tell me a use case for which
2910 * you'd need both a find dialog and a find/replace one at the same time,
2911 * without being able to actually use them separately since they're syncing
2912 * all the time. I don't think it's worthwhile to fix this nonsense,
2913 * particularly evil incarnation of braindeadness, whatever; I'd much rather
2914 * see it extinguished from this planet. Thanks for listening. Sorry.
2915 */
2916 static void
2917entry_changed_cb(GtkWidget * entry, GtkWidget * dialog)
2918{
2919 const gchar *entry_text;
2920 gboolean nonempty;
2921
2922 entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
2923
2924 if (!entry_text)
2925 return; /* shouldn't happen */
2926
2927 nonempty = (entry_text[0] != '\0');
2928
2929 if (dialog == find_widgets.dialog)
2930 {
2931 gtk_widget_set_sensitive(find_widgets.find, nonempty);
2932 }
2933
2934 if (dialog == repl_widgets.dialog)
2935 {
2936 gtk_widget_set_sensitive(repl_widgets.find, nonempty);
2937 gtk_widget_set_sensitive(repl_widgets.replace, nonempty);
2938 gtk_widget_set_sensitive(repl_widgets.all, nonempty);
2939 }
2940}
2941
2942/*
2943 * ":helpfind"
2944 */
2945/*ARGSUSED*/
2946 void
2947ex_helpfind(eap)
2948 exarg_T *eap;
2949{
2950 /* This will fail when menus are not loaded. Well, it's only for
2951 * backwards compatibility anyway. */
2952 do_cmdline_cmd((char_u *)"emenu ToolBar.FindHelp");
2953}
2954
2955#if !defined(HAVE_GTK2) || defined(PROTO) /* Crack crack crack. Brrrr. */
2956
2957/* gui_gtk_position_in_parent
2958 *
2959 * this function causes a child window to be placed within the boundary of
2960 * the parent (mainwin) window.
2961 *
2962 * you can specify where the window will be positioned by the third argument
2963 * (defined in gui.h):
2964 * VW_POS_CENTER at center of parent window
2965 * VW_POS_MOUSE center of child at mouse position
2966 * VW_POS_TOP_CENTER top of child at top of parent centered
2967 * horizontally about the mouse.
2968 *
2969 * NOTE: for this function to act as desired the child window must have a
2970 * window size requested. this can be accomplished by packing/placing
2971 * child widgets onto a gtk_frame widget rather than the gtk_window
2972 * widget...
2973 *
2974 * brent -- dbv
2975 */
2976 void
2977gui_gtk_position_in_parent(
2978 GtkWidget *parent,
2979 GtkWidget *child,
2980 gui_win_pos_T where)
2981{
2982 GtkRequisition c_size;
2983 gint xPm, yPm;
2984 gint xP, yP, wP, hP, pos_x, pos_y;
2985
2986 /* make sure the child widget is set up then get its size. */
2987 gtk_widget_size_request(child, &c_size);
2988
2989 /* get origin and size of parent window */
2990 gdk_window_get_origin((GdkWindow *)(parent->window), &xP, &yP);
2991 gdk_window_get_size((GdkWindow *)(parent->window), &wP, &hP);
2992
2993 if (c_size.width > wP || c_size.height > hP)
2994 {
2995 /* doh! maybe the user should consider giving gVim a little more
2996 * screen real estate */
2997 gtk_widget_set_uposition(child , xP + 2 , yP + 2);
2998 return;
2999 }
3000
3001 if (where == VW_POS_MOUSE)
3002 {
3003 /* position window at mouse pointer */
3004 gtk_widget_get_pointer(parent, &xPm, &yPm);
3005 pos_x = xP + xPm - (c_size.width) / 2;
3006 pos_y = yP + yPm - (c_size.height) / 2;
3007 }
3008 else
3009 {
3010 /* set child x origin so it is in center of Vim window */
3011 pos_x = xP + (wP - c_size.width) / 2;
3012
3013 if (where == VW_POS_TOP_CENTER)
3014 pos_y = yP + 2;
3015 else
3016 /* where == VW_POS_CENTER */
3017 pos_y = yP + (hP - c_size.height) / 2;
3018 }
3019
3020 /* now, make sure the window will be inside the Vim window... */
3021 if (pos_x < xP)
3022 pos_x = xP + 2;
3023 if (pos_y < yP)
3024 pos_y = yP + 2;
3025 if ((pos_x + c_size.width) > (wP + xP))
3026 pos_x = xP + wP - c_size.width - 2;
3027 /* Assume 'guiheadroom' indicates the title bar height... */
3028 if ((pos_y + c_size.height + p_ghr / 2) > (hP + yP))
3029 pos_y = yP + hP - c_size.height - 2 - p_ghr / 2;
3030
3031 gtk_widget_set_uposition(child, pos_x, pos_y);
3032}
3033
3034#endif /* !HAVE_GTK2 */
3035