home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / gui_gtk.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-05-21  |  80.8 KB  |  3,019 lines

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