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