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