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