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