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