[gedit] EncodingsDialog: add Cancel and Apply buttons



commit b01ccc5cc08f48a72b9448406278f55b3ff2d229
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Apr 5 14:19:33 2015 +0200

    EncodingsDialog: add Cancel and Apply buttons
    
    Having only the close button (with an X, not as a text button) was a bit
    strange. Now with the Apply button it's clearer. The Cancel button
    cancels also the reset, if the Reset button was clicked.

 gedit/gedit-encodings-combo-box.c            |    7 +-
 gedit/gedit-encodings-dialog.c               |  329 ++++++++++++++-----------
 gedit/gedit-encodings-dialog.h               |    2 +
 gedit/resources/ui/gedit-encodings-dialog.ui |   30 +++-
 4 files changed, 219 insertions(+), 149 deletions(-)
---
diff --git a/gedit/gedit-encodings-combo-box.c b/gedit/gedit-encodings-combo-box.c
index bd3b173..b9b5797 100644
--- a/gedit/gedit-encodings-combo-box.c
+++ b/gedit/gedit-encodings-combo-box.c
@@ -169,8 +169,11 @@ dialog_response_cb (GtkDialog              *dialog,
                     gint                    response_id,
                     GeditEncodingsComboBox *menu)
 {
-       update_menu (menu);
-       gtk_widget_destroy (GTK_WIDGET (dialog));
+       if (response_id != GEDIT_ENCODINGS_DIALOG_RESPONSE_RESET)
+       {
+               update_menu (menu);
+               gtk_widget_destroy (GTK_WIDGET (dialog));
+       }
 }
 
 static void
diff --git a/gedit/gedit-encodings-dialog.c b/gedit/gedit-encodings-dialog.c
index dbbcdb7..8f3039c 100644
--- a/gedit/gedit-encodings-dialog.c
+++ b/gedit/gedit-encodings-dialog.c
@@ -31,6 +31,13 @@
 
 #include "gedit-settings.h"
 
+typedef enum _State
+{
+       STATE_UNMODIFIED,
+       STATE_MODIFIED,
+       STATE_RESET
+} State;
+
 struct _GeditEncodingsDialogPrivate
 {
        GSettings *enc_settings;
@@ -50,7 +57,7 @@ struct _GeditEncodingsDialogPrivate
        GtkWidget *up_button;
        GtkWidget *down_button;
 
-       guint modified : 1;
+       State state;
 };
 
 enum
@@ -64,29 +71,124 @@ enum
 G_DEFINE_TYPE_WITH_PRIVATE (GeditEncodingsDialog, gedit_encodings_dialog, GTK_TYPE_DIALOG)
 
 static void
-set_modified (GeditEncodingsDialog *dialog,
-             gboolean              modified)
+set_modified (GeditEncodingsDialog *dialog)
+{
+       dialog->priv->state = STATE_MODIFIED;
+       gtk_widget_set_sensitive (dialog->priv->reset_button, TRUE);
+}
+
+static void
+append_encoding (GtkListStore            *liststore,
+                const GtkSourceEncoding *encoding)
 {
-       dialog->priv->modified = modified;
+       GtkTreeIter iter;
+
+       gtk_list_store_append (liststore, &iter);
+       gtk_list_store_set (liststore, &iter,
+                           COLUMN_NAME, gtk_source_encoding_get_name (encoding),
+                           COLUMN_ENCODING, encoding,
+                           -1);
 
-       if (modified)
+       if (encoding == gtk_source_encoding_get_current ())
        {
-               gtk_widget_set_sensitive (dialog->priv->reset_button, TRUE);
+               gchar *charset = g_strdup_printf (_("%s (Current Locale)"),
+                                                 gtk_source_encoding_get_charset (encoding));
+
+               gtk_list_store_set (liststore, &iter,
+                                   COLUMN_CHARSET, charset,
+                                   -1);
+
+               g_free (charset);
+       }
+       else
+       {
+               gtk_list_store_set (liststore, &iter,
+                                   COLUMN_CHARSET, gtk_source_encoding_get_charset (encoding),
+                                   -1);
        }
 }
 
 static void
-gedit_encodings_dialog_dispose (GObject *object)
+init_liststores (GeditEncodingsDialog *dialog,
+                gboolean              reset)
 {
-       GeditEncodingsDialogPrivate *priv = GEDIT_ENCODINGS_DIALOG (object)->priv;
+       gboolean default_candidates;
+       GSList *chosen_encodings;
+       GSList *all_encodings;
+       GSList *l;
 
-       g_clear_object (&priv->enc_settings);
-       g_clear_object (&priv->add_button);
-       g_clear_object (&priv->remove_button);
-       g_clear_object (&priv->up_button);
-       g_clear_object (&priv->down_button);
+       /* Chosen encodings */
 
-       G_OBJECT_CLASS (gedit_encodings_dialog_parent_class)->dispose (object);
+       if (reset)
+       {
+               chosen_encodings = gtk_source_encoding_get_default_candidates ();
+               default_candidates = TRUE;
+       }
+       else
+       {
+               chosen_encodings = gedit_settings_get_candidate_encodings (&default_candidates);
+       }
+
+       gtk_widget_set_sensitive (dialog->priv->reset_button, !default_candidates);
+
+       for (l = chosen_encodings; l != NULL; l = l->next)
+       {
+               const GtkSourceEncoding *cur_encoding = l->data;
+               append_encoding (dialog->priv->liststore_chosen, cur_encoding);
+       }
+
+       /* Available encodings */
+
+       all_encodings = gtk_source_encoding_get_all ();
+
+       for (l = chosen_encodings; l != NULL; l = l->next)
+       {
+               const GtkSourceEncoding *chosen_encoding = l->data;
+               all_encodings = g_slist_remove (all_encodings, chosen_encoding);
+       }
+
+       for (l = all_encodings; l != NULL; l = l->next)
+       {
+               const GtkSourceEncoding *cur_encoding = l->data;
+               append_encoding (dialog->priv->liststore_available, cur_encoding);
+       }
+
+       g_slist_free (chosen_encodings);
+       g_slist_free (all_encodings);
+}
+
+static void
+reset_encodings (GeditEncodingsDialog *dialog)
+{
+       GtkDialog *msg_dialog;
+       gint response;
+
+       msg_dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (dialog),
+                                                        GTK_DIALOG_DESTROY_WITH_PARENT |
+                                                        GTK_DIALOG_MODAL,
+                                                        GTK_MESSAGE_QUESTION,
+                                                        GTK_BUTTONS_NONE,
+                                                        "%s",
+                                                        _("Do you really want to reset the "
+                                                          "character encodings' preferences?")));
+
+       gtk_dialog_add_buttons (msg_dialog,
+                               _("_Cancel"), GTK_RESPONSE_CANCEL,
+                               _("_Reset"), GTK_RESPONSE_ACCEPT,
+                               NULL);
+
+       response = gtk_dialog_run (msg_dialog);
+
+       if (response == GTK_RESPONSE_ACCEPT)
+       {
+               gtk_list_store_clear (dialog->priv->liststore_available);
+               gtk_list_store_clear (dialog->priv->liststore_chosen);
+
+               init_liststores (dialog, TRUE);
+               dialog->priv->state = STATE_RESET;
+       }
+
+       gtk_widget_destroy (GTK_WIDGET (msg_dialog));
 }
 
 static GSList *
@@ -139,29 +241,80 @@ encoding_list_to_strv (const GSList *enc_list)
 }
 
 static void
+apply_settings (GeditEncodingsDialog *dialog)
+{
+       switch (dialog->priv->state)
+       {
+               case STATE_MODIFIED:
+               {
+                       GSList *enc_list;
+                       gchar **enc_strv;
+
+                       enc_list = get_chosen_encodings_list (dialog);
+                       enc_strv = encoding_list_to_strv (enc_list);
+
+                       g_settings_set_strv (dialog->priv->enc_settings,
+                                            GEDIT_SETTINGS_CANDIDATE_ENCODINGS,
+                                            (const gchar * const *)enc_strv);
+
+                       g_slist_free (enc_list);
+                       g_strfreev (enc_strv);
+                       break;
+               }
+
+               case STATE_RESET:
+                       g_settings_reset (dialog->priv->enc_settings,
+                                         GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
+                       break;
+
+               case STATE_UNMODIFIED:
+                       /* Do nothing. */
+                       break;
+
+               default:
+                       g_assert_not_reached ();
+
+       }
+}
+
+static void
 gedit_encodings_dialog_response (GtkDialog *gtk_dialog,
                                  gint       response_id)
 {
        GeditEncodingsDialog *dialog = GEDIT_ENCODINGS_DIALOG (gtk_dialog);
 
-       if (dialog->priv->modified)
+       switch (response_id)
        {
-               GSList *enc_list;
-               gchar **enc_strv;
-
-               enc_list = get_chosen_encodings_list (dialog);
-               enc_strv = encoding_list_to_strv (enc_list);
+               case GEDIT_ENCODINGS_DIALOG_RESPONSE_RESET:
+                       reset_encodings (dialog);
+                       break;
 
-               g_settings_set_strv (dialog->priv->enc_settings,
-                                    GEDIT_SETTINGS_CANDIDATE_ENCODINGS,
-                                    (const gchar * const *)enc_strv);
+               case GTK_RESPONSE_APPLY:
+                       apply_settings (dialog);
+                       break;
 
-               g_slist_free (enc_list);
-               g_strfreev (enc_strv);
+               case GTK_RESPONSE_CANCEL:
+               default:
+                       /* Do nothing */
+                       break;
        }
 }
 
 static void
+gedit_encodings_dialog_dispose (GObject *object)
+{
+       GeditEncodingsDialogPrivate *priv = GEDIT_ENCODINGS_DIALOG (object)->priv;
+
+       g_clear_object (&priv->enc_settings);
+       g_clear_object (&priv->add_button);
+       g_clear_object (&priv->remove_button);
+       g_clear_object (&priv->up_button);
+       g_clear_object (&priv->down_button);
+
+       G_OBJECT_CLASS (gedit_encodings_dialog_parent_class)->dispose (object);
+}
+
+static void
 gedit_encodings_dialog_class_init (GeditEncodingsDialogClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
@@ -301,37 +454,6 @@ update_chosen_buttons_sensitivity (GeditEncodingsDialog *dialog)
        update_up_down_buttons_sensitivity (dialog);
 }
 
-static void
-append_encoding (GtkListStore            *liststore,
-                const GtkSourceEncoding *encoding)
-{
-       GtkTreeIter iter;
-
-       gtk_list_store_append (liststore, &iter);
-       gtk_list_store_set (liststore, &iter,
-                           COLUMN_NAME, gtk_source_encoding_get_name (encoding),
-                           COLUMN_ENCODING, encoding,
-                           -1);
-
-       if (encoding == gtk_source_encoding_get_current ())
-       {
-               gchar *charset = g_strdup_printf (_("%s (Current Locale)"),
-                                                 gtk_source_encoding_get_charset (encoding));
-
-               gtk_list_store_set (liststore, &iter,
-                                   COLUMN_CHARSET, charset,
-                                   -1);
-
-               g_free (charset);
-       }
-       else
-       {
-               gtk_list_store_set (liststore, &iter,
-                                   COLUMN_CHARSET, gtk_source_encoding_get_charset (encoding),
-                                   -1);
-       }
-}
-
 /* Removes all @paths from @orig, and append them at the end of @dest in the
  * same order.
  */
@@ -415,7 +537,7 @@ add_button_clicked_cb (GtkWidget            *button,
                            dialog->priv->liststore_available,
                            dialog->priv->liststore_chosen);
 
-       set_modified (dialog, TRUE);
+       set_modified (dialog);
 
        /* For the treeview_available, it's more natural to unselect the added
         * encodings.
@@ -485,7 +607,7 @@ remove_button_clicked_cb (GtkWidget            *button,
                            dialog->priv->liststore_chosen,
                            dialog->priv->liststore_available);
 
-       set_modified (dialog, TRUE);
+       set_modified (dialog);
 
        g_list_free (selected_rows);
        g_list_free_full (to_remove, (GDestroyNotify) gtk_tree_path_free);
@@ -524,7 +646,7 @@ up_button_clicked_cb (GtkWidget            *button,
                                    &iter,
                                    &prev_iter);
 
-       set_modified (dialog, TRUE);
+       set_modified (dialog);
 
        update_chosen_buttons_sensitivity (dialog);
 
@@ -564,7 +686,7 @@ down_button_clicked_cb (GtkWidget            *button,
                                   &iter,
                                   &next_iter);
 
-       set_modified (dialog, TRUE);
+       set_modified (dialog);
 
        update_chosen_buttons_sensitivity (dialog);
 
@@ -572,84 +694,6 @@ down_button_clicked_cb (GtkWidget            *button,
 }
 
 static void
-init_liststores (GeditEncodingsDialog *dialog)
-{
-       gboolean default_candidates;
-       GSList *chosen_encodings;
-       GSList *all_encodings;
-       GSList *l;
-
-       /* Chosen encodings */
-
-       chosen_encodings = gedit_settings_get_candidate_encodings (&default_candidates);
-
-       gtk_widget_set_sensitive (dialog->priv->reset_button, !default_candidates);
-
-       for (l = chosen_encodings; l != NULL; l = l->next)
-       {
-               const GtkSourceEncoding *cur_encoding = l->data;
-               append_encoding (dialog->priv->liststore_chosen, cur_encoding);
-       }
-
-       /* Available encodings */
-
-       all_encodings = gtk_source_encoding_get_all ();
-
-       for (l = chosen_encodings; l != NULL; l = l->next)
-       {
-               const GtkSourceEncoding *chosen_encoding = l->data;
-               all_encodings = g_slist_remove (all_encodings, chosen_encoding);
-       }
-
-       for (l = all_encodings; l != NULL; l = l->next)
-       {
-               const GtkSourceEncoding *cur_encoding = l->data;
-               append_encoding (dialog->priv->liststore_available, cur_encoding);
-       }
-
-       set_modified (dialog, FALSE);
-
-       g_slist_free (chosen_encodings);
-       g_slist_free (all_encodings);
-}
-
-static void
-reset_button_clicked_cb (GtkWidget            *button,
-                        GeditEncodingsDialog *dialog)
-{
-       GtkDialog *msg_dialog;
-       gint response;
-
-       msg_dialog = GTK_DIALOG (gtk_message_dialog_new (GTK_WINDOW (dialog),
-                                                        GTK_DIALOG_DESTROY_WITH_PARENT |
-                                                        GTK_DIALOG_MODAL,
-                                                        GTK_MESSAGE_QUESTION,
-                                                        GTK_BUTTONS_NONE,
-                                                        "%s",
-                                                        _("Do you really want to reset the "
-                                                          "character encodings' preferences?")));
-
-       gtk_dialog_add_buttons (msg_dialog,
-                               _("_Cancel"), GTK_RESPONSE_CANCEL,
-                               _("_Reset"), GTK_RESPONSE_ACCEPT,
-                               NULL);
-
-       response = gtk_dialog_run (msg_dialog);
-
-       if (response == GTK_RESPONSE_ACCEPT)
-       {
-               g_settings_reset (dialog->priv->enc_settings, GEDIT_SETTINGS_CANDIDATE_ENCODINGS);
-
-               gtk_list_store_clear (dialog->priv->liststore_available);
-               gtk_list_store_clear (dialog->priv->liststore_chosen);
-
-               init_liststores (dialog);
-       }
-
-       gtk_widget_destroy (GTK_WIDGET (msg_dialog));
-}
-
-static void
 init_toolbar_available (GeditEncodingsDialog *dialog)
 {
        GtkWidget *scrolled_window;
@@ -793,17 +837,12 @@ gedit_encodings_dialog_init (GeditEncodingsDialog *dialog)
 
        init_toolbar_available (dialog);
        init_toolbar_chosen (dialog);
-       init_liststores (dialog);
+       init_liststores (dialog, FALSE);
+       dialog->priv->state = STATE_UNMODIFIED;
 
-       /* Reset button */
        context = gtk_widget_get_style_context (dialog->priv->reset_button);
        gtk_style_context_add_class (context, GTK_STYLE_CLASS_DESTRUCTIVE_ACTION);
 
-       g_signal_connect (dialog->priv->reset_button,
-                         "clicked",
-                         G_CALLBACK (reset_button_clicked_cb),
-                         dialog);
-
        /* Available encodings */
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (dialog->priv->sort_available),
                                              COLUMN_NAME,
diff --git a/gedit/gedit-encodings-dialog.h b/gedit/gedit-encodings-dialog.h
index 59b2211..c99a940 100644
--- a/gedit/gedit-encodings-dialog.h
+++ b/gedit/gedit-encodings-dialog.h
@@ -36,6 +36,8 @@ typedef struct _GeditEncodingsDialog          GeditEncodingsDialog;
 typedef struct _GeditEncodingsDialogClass      GeditEncodingsDialogClass;
 typedef struct _GeditEncodingsDialogPrivate    GeditEncodingsDialogPrivate;
 
+#define GEDIT_ENCODINGS_DIALOG_RESPONSE_RESET  1
+
 struct _GeditEncodingsDialog
 {
        GtkDialog dialog;
diff --git a/gedit/resources/ui/gedit-encodings-dialog.ui b/gedit/resources/ui/gedit-encodings-dialog.ui
index 1d8ea2a..9b7b915 100644
--- a/gedit/resources/ui/gedit-encodings-dialog.ui
+++ b/gedit/resources/ui/gedit-encodings-dialog.ui
@@ -38,7 +38,6 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="has_subtitle">False</property>
-        <property name="show_close_button">True</property>
         <property name="title" translatable="yes">Character Encodings</property>
         <child>
           <object class="GtkButton" id="reset_button">
@@ -47,6 +46,28 @@
             <property name="use_underline">True</property>
           </object>
         </child>
+        <child>
+          <object class="GtkButton" id="cancel_button">
+            <property name="label" translatable="yes">_Cancel</property>
+            <property name="visible">True</property>
+            <property name="use_underline">True</property>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkButton" id="apply_button">
+            <property name="label" translatable="yes">_Apply</property>
+            <property name="visible">True</property>
+            <property name="use_underline">True</property>
+            <property name="can_default">True</property>
+          </object>
+          <packing>
+            <property name="pack_type">end</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
       </object>
     </child>
     <child internal-child="vbox">
@@ -68,7 +89,7 @@
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="valign">start</property>
-                <property name="label" translatable="yes">_Available Encodings</property>
+                <property name="label" translatable="yes">A_vailable Encodings</property>
                 <property name="use_underline">True</property>
                 <property name="mnemonic_widget">treeview_available</property>
                 <property name="xalign">0</property>
@@ -260,5 +281,10 @@
         </child>
       </object>
     </child>
+    <action-widgets>
+      <action-widget response="1">reset_button</action-widget>
+      <action-widget response="cancel">cancel_button</action-widget>
+      <action-widget response="apply" default="true">apply_button</action-widget>
+    </action-widgets>
   </template>
 </interface>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]