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