]> asedeno.scripts.mit.edu Git - PuTTY.git/blobdiff - unix/gtkdlg.c
Better not forget to make sure GTK1 doesn't break.
[PuTTY.git] / unix / gtkdlg.c
index ae8dad4805f2f1c5009e08464e83628eefe82b05..966164ac025e4ecf3a04bcc44802b5c907e70cfb 100644 (file)
@@ -2,15 +2,6 @@
  * gtkdlg.c - GTK implementation of the PuTTY configuration box.
  */
 
-/*
- * TODO when porting to GTK 2.0:
- * 
- *  - GtkList is deprecated and we should switch to GtkTreeView instead
- *    (done for GtkTree).
- *  - GtkLabel has a built-in mnemonic scheme, so we should at
- *    least consider switching to that from the current adhockery.
- */
-
 #include <assert.h>
 #include <stdarg.h>
 #include <ctype.h>
@@ -56,6 +47,7 @@ struct uctrl {
     GtkWidget *text;         /* for text */
     GtkWidget *label;         /* for dlg_label_change */
     GtkAdjustment *adj;       /* for the scrollbar in a list box */
+    guint entrysig;
     guint textsig;
 };
 
@@ -102,12 +94,12 @@ static gboolean widget_focus(GtkWidget *widget, GdkEventFocus *event,
 static void shortcut_add(struct Shortcuts *scs, GtkWidget *labelw,
                         int chr, int action, void *ptr);
 static void shortcut_highlight(GtkWidget *label, int chr);
-static int listitem_single_key(GtkWidget *item, GdkEventKey *event,
-                               gpointer data);
-static int listitem_multi_key(GtkWidget *item, GdkEventKey *event,
-                                 gpointer data);
-static int listitem_button(GtkWidget *item, GdkEventButton *event,
-                           gpointer data);
+static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
+                                   gpointer data);
+static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
+                                  gpointer data);
+static gboolean listitem_button(GtkWidget *item, GdkEventButton *event,
+                               gpointer data);
 static void menuitem_activate(GtkMenuItem *item, gpointer data);
 static void coloursel_ok(GtkButton *button, gpointer data);
 static void coloursel_cancel(GtkButton *button, gpointer data);
@@ -300,7 +292,27 @@ void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
     struct uctrl *uc = dlg_find_byctrl(dp, ctrl);
     assert(uc->ctrl->generic.type == CTRL_EDITBOX);
     assert(uc->entry != NULL);
+    /*
+     * GTK 2 implements gtk_entry_set_text by means of two separate
+     * operations: first delete the previous text leaving the empty
+     * string, then insert the new text. This causes two calls to
+     * the "changed" signal.
+     * 
+     * The first call to "changed", if allowed to proceed normally,
+     * will cause an EVENT_VALCHANGE event on the edit box, causing
+     * a call to dlg_editbox_get() which will read the empty string
+     * out of the GtkEntry - and promptly write it straight into
+     * the Config structure, which is precisely where our `text'
+     * pointer is probably pointing, so the second editing
+     * operation will insert that instead of the string we
+     * originally asked for.
+     * 
+     * Hence, we must block our "changed" signal handler for the
+     * duration of this call to gtk_entry_set_text.
+     */
+    g_signal_handler_block(uc->entry, uc->entrysig);
     gtk_entry_set_text(GTK_ENTRY(uc->entry), text);
+    g_signal_handler_unblock(uc->entry, uc->entrysig);
 }
 
 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
@@ -963,7 +975,8 @@ static void button_toggled(GtkToggleButton *tb, gpointer data)
     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_VALCHANGE);
 }
 
-static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
+static gboolean editbox_key(GtkWidget *widget, GdkEventKey *event,
+                           gpointer data)
 {
     /*
      * GtkEntry has a nasty habit of eating the Return key, which
@@ -975,7 +988,7 @@ static int editbox_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
      * in the dialog just like it will everywhere else.
      */
     if (event->keyval == GDK_Return && widget->parent != NULL) {
-       gint return_val;
+       gboolean return_val;
        gtk_signal_emit_stop_by_name(GTK_OBJECT(widget), "key_press_event");
        gtk_signal_emit_by_name(GTK_OBJECT(widget->parent), "key_press_event",
                                event, &return_val);
@@ -993,16 +1006,17 @@ static void editbox_changed(GtkEditable *ed, gpointer data)
     }
 }
 
-static void editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
-                             gpointer data)
+static gboolean editbox_lostfocus(GtkWidget *ed, GdkEventFocus *event,
+                                 gpointer data)
 {
     struct dlgparam *dp = (struct dlgparam *)data;
     struct uctrl *uc = dlg_find_bywidget(dp, GTK_WIDGET(ed));
     uc->ctrl->generic.handler(uc->ctrl, dp, dp->data, EVENT_REFRESH);
+    return FALSE;
 }
 
-static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data,
-                        int multiple)
+static gboolean listitem_key(GtkWidget *item, GdkEventKey *event,
+                            gpointer data, int multiple)
 {
     GtkAdjustment *adj = GTK_ADJUSTMENT(data);
 
@@ -1095,20 +1109,20 @@ static int listitem_key(GtkWidget *item, GdkEventKey *event, gpointer data,
     return FALSE;
 }
 
-static int listitem_single_key(GtkWidget *item, GdkEventKey *event,
-                               gpointer data)
+static gboolean listitem_single_key(GtkWidget *item, GdkEventKey *event,
+                                   gpointer data)
 {
     return listitem_key(item, event, data, FALSE);
 }
 
-static int listitem_multi_key(GtkWidget *item, GdkEventKey *event,
-                                 gpointer data)
+static gboolean listitem_multi_key(GtkWidget *item, GdkEventKey *event,
+                                  gpointer data)
 {
     return listitem_key(item, event, data, TRUE);
 }
 
-static int listitem_button(GtkWidget *item, GdkEventButton *event,
-                           gpointer data)
+static gboolean listitem_button(GtkWidget *item, GdkEventButton *event,
+                               gpointer data)
 {
     struct dlgparam *dp = (struct dlgparam *)data;
     if (event->type == GDK_2BUTTON_PRESS ||
@@ -1500,8 +1514,9 @@ GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
                        gtk_entry_set_visibility(GTK_ENTRY(w), FALSE);
                    uc->entry = w;
                }
-               gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
-                                  GTK_SIGNAL_FUNC(editbox_changed), dp);
+               uc->entrysig =
+                   gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
+                                      GTK_SIGNAL_FUNC(editbox_changed), dp);
                gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
                                   GTK_SIGNAL_FUNC(editbox_key), dp);
                gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
@@ -1592,8 +1607,9 @@ GtkWidget *layout_ctrls(struct dlgparam *dp, struct Shortcuts *scs,
 
                gtk_signal_connect(GTK_OBJECT(uc->entry), "key_press_event",
                                   GTK_SIGNAL_FUNC(editbox_key), dp);
-               gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
-                                  GTK_SIGNAL_FUNC(editbox_changed), dp);
+               uc->entrysig =
+                   gtk_signal_connect(GTK_OBJECT(uc->entry), "changed",
+                                      GTK_SIGNAL_FUNC(editbox_changed), dp);
                 gtk_signal_connect(GTK_OBJECT(uc->entry), "focus_in_event",
                                    GTK_SIGNAL_FUNC(widget_focus), dp);
                 gtk_signal_connect(GTK_OBJECT(uc->button), "focus_in_event",
@@ -2104,6 +2120,72 @@ int get_listitemheight(void)
     return req.height;
 }
 
+void set_dialog_action_area(GtkDialog *dlg, GtkWidget *w)
+{
+#if !GTK_CHECK_VERSION(2,0,0)
+
+    /*
+     * In GTK 1, laying out the buttons at the bottom of the
+     * configuration box is nice and easy, because a GtkDialog's
+     * action_area is a GtkHBox which stretches to cover the full
+     * width of the dialog. So we just put our Columns widget
+     * straight into that hbox, and it ends up just where we want
+     * it.
+     */
+    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
+                      w, TRUE, TRUE, 0);
+
+#else
+    /*
+     * In GTK 2, the action area is now a GtkHButtonBox and its
+     * layout behaviour seems to be different: it doesn't stretch
+     * to cover the full width of the window, but instead finds its
+     * own preferred width and right-aligns that within the window.
+     * This isn't what we want, because we have both left-aligned
+     * and right-aligned buttons coming out of the above call to
+     * layout_ctrls(), and right-aligning the whole thing will
+     * result in the former being centred and looking weird.
+     *
+     * So instead we abandon the dialog's action area completely:
+     * we gtk_widget_hide() it in the below code, and we also call
+     * gtk_dialog_set_has_separator() to remove the separator above
+     * it. We then insert our own action area into the end of the
+     * dialog's main vbox, and add our own separator above that.
+     *
+     * (Ideally, if we were a native GTK app, we would use the
+     * GtkHButtonBox's _own_ innate ability to support one set of
+     * buttons being right-aligned and one left-aligned. But to do
+     * that here, we would have to either (a) pick apart our cross-
+     * platform layout structures and treat them specially for this
+     * particular set of controls, which would be painful, or else
+     * (b) develop a special and simpler cross-platform
+     * representation for these particular controls, and introduce
+     * special-case code into all the _other_ platforms to handle
+     * it. Neither appeals. Therefore, I regretfully discard the
+     * GTKHButtonBox and go it alone.)
+     */
+
+    GtkWidget *align;
+    align = gtk_alignment_new(0, 0, 1, 1);
+    gtk_container_add(GTK_CONTAINER(align), w);
+    /*
+     * The purpose of this GtkAlignment is to provide padding
+     * around the buttons. The padding we use is twice the padding
+     * used in our GtkColumns, because we nest two GtkColumns most
+     * of the time (one separating the tree view from the main
+     * controls, and another for the main controls themselves).
+     */
+    gtk_alignment_set_padding(GTK_ALIGNMENT(align), 8, 8, 8, 8);
+    gtk_widget_show(align);
+    gtk_box_pack_end(GTK_BOX(dlg->vbox), align, FALSE, TRUE, 0);
+    w = gtk_hseparator_new();
+    gtk_box_pack_end(GTK_BOX(dlg->vbox), w, FALSE, TRUE, 0);
+    gtk_widget_show(w);
+    gtk_widget_hide(dlg->action_area);
+    gtk_dialog_set_has_separator(dlg, FALSE);
+#endif
+}
+
 int do_config_box(const char *title, Config *cfg, int midsession,
                  int protcfginfo)
 {
@@ -2198,8 +2280,8 @@ int do_config_box(const char *title, Config *cfg, int midsession,
 
        if (!*s->pathname) {
            w = layout_ctrls(&dp, &scs, s, listitemheight, GTK_WINDOW(window));
-           gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
-                              w, TRUE, TRUE, 0);
+
+           set_dialog_action_area(GTK_DIALOG(window), w);
        } else {
            int j = path ? ctrl_path_compare(s->pathname, path) : 0;
            if (j != INT_MAX) {        /* add to treeview, start new panel */
@@ -2271,6 +2353,19 @@ int do_config_box(const char *title, Config *cfg, int midsession,
                                   TREESTORE_PARAMS, nselparams,
                                   -1);
                treeiterlevels[j] = treeiter;
+
+               if (j > 0) {
+                   GtkTreePath *path;
+
+                   path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore),
+                                                  &treeiterlevels[j-1]);
+                   if (j < 2)
+                       gtk_tree_view_expand_row(GTK_TREE_VIEW(tree), path,
+                                                FALSE);
+                   else
+                       gtk_tree_view_collapse_row(GTK_TREE_VIEW(tree), path);
+                   gtk_tree_path_free(path);
+               }
 #else
                treeitem = gtk_tree_item_new_with_label(c);
                if (j > 0) {
@@ -2316,7 +2411,6 @@ int do_config_box(const char *title, Config *cfg, int midsession,
     }
 
 #if GTK_CHECK_VERSION(2,0,0)
-    gtk_tree_view_expand_all(GTK_TREE_VIEW(tree));
     g_signal_connect(G_OBJECT(treeselection), "changed",
                     G_CALLBACK(treeselection_changed), selparams);
 #else
@@ -2467,8 +2561,7 @@ int messagebox(GtkWidget *parentwin, char *title, char *msg, int minwid, ...)
     window = gtk_dialog_new();
     gtk_window_set_title(GTK_WINDOW(window), title);
     w0 = layout_ctrls(&dp, &scs, s0, 0, GTK_WINDOW(window));
-    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
-                      w0, TRUE, TRUE, 0);
+    set_dialog_action_area(GTK_DIALOG(window), w0);
     gtk_widget_show(w0);
     w1 = layout_ctrls(&dp, &scs, s1, 0, GTK_WINDOW(window));
     gtk_container_set_border_width(GTK_CONTAINER(w1), 10);
@@ -2661,7 +2754,7 @@ static void licence_clicked(GtkButton *button, gpointer data)
        "Portions copyright Robert de Bath, Joris van Rantwijk, Delian "
        "Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas "
        "Barry, Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, "
-       "Markus Kuhn, and CORE SDI S.A.\n\n"
+       "Markus Kuhn, Colin Watson, and CORE SDI S.A.\n\n"
 
        "Permission is hereby granted, free of charge, to any person "
        "obtaining a copy of this software and associated documentation "
@@ -2913,8 +3006,7 @@ void showeventlog(void *estuff, void *parentwin)
     sfree(title);
     w0 = layout_ctrls(&es->dp, &es->scs, s0,
                      listitemheight, GTK_WINDOW(window));
-    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->action_area),
-                      w0, TRUE, TRUE, 0);
+    set_dialog_action_area(GTK_DIALOG(window), w0);
     gtk_widget_show(w0);
     w1 = layout_ctrls(&es->dp, &es->scs, s1,
                      listitemheight, GTK_WINDOW(window));