[gedit/wip/merge-encoding-settings: 26/33] EncodingsDialog: add Up and Down buttons to order chosen encodings



commit f2f2f545f3752947fa8649533a481db841b0b4f1
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Mar 22 16:03:19 2015 +0100

    EncodingsDialog: add Up and Down buttons to order chosen encodings
    
    I used glade to edit the .ui file. Keep the changes so that editing
    again the file with glade in the future will produce a smaller diff.
    
    The Up and Down buttons work only when one encoding is selected. The
    buttons sensitivity is correctly updated, even for the first/last
    encodings (the Up button is insensitive for the first item, and the Down
    button is insensitive for the last item).

 gedit/gedit-encodings-dialog.c               |  128 ++++++++++++++++++++++++-
 gedit/resources/ui/gedit-encodings-dialog.ui |  134 +++++++++++++-------------
 2 files changed, 192 insertions(+), 70 deletions(-)
---
diff --git a/gedit/gedit-encodings-dialog.c b/gedit/gedit-encodings-dialog.c
index a80eef1..2ff7fc1 100644
--- a/gedit/gedit-encodings-dialog.c
+++ b/gedit/gedit-encodings-dialog.c
@@ -46,6 +46,8 @@ struct _GeditEncodingsDialogPrivate
        GtkListStore *liststore_chosen;
        GtkTreeView *treeview_chosen;
        GtkWidget *remove_button;
+       GtkWidget *up_button;
+       GtkWidget *down_button;
 };
 
 enum
@@ -149,6 +151,8 @@ gedit_encodings_dialog_class_init (GeditEncodingsDialogClass *klass)
        gtk_widget_class_bind_template_child_private (widget_class, GeditEncodingsDialog, treeview_chosen);
        gtk_widget_class_bind_template_child_private (widget_class, GeditEncodingsDialog, add_button);
        gtk_widget_class_bind_template_child_private (widget_class, GeditEncodingsDialog, remove_button);
+       gtk_widget_class_bind_template_child_private (widget_class, GeditEncodingsDialog, up_button);
+       gtk_widget_class_bind_template_child_private (widget_class, GeditEncodingsDialog, down_button);
 }
 
 static void
@@ -163,14 +167,46 @@ update_add_button_sensitivity (GeditEncodingsDialog *dialog)
 }
 
 static void
-update_remove_button_sensitivity (GeditEncodingsDialog *dialog)
+update_chosen_buttons_sensitivity (GeditEncodingsDialog *dialog)
 {
        GtkTreeSelection *selection;
        gint count;
+       GList *selected_rows;
+       GtkTreeModel *model;
+       GtkTreePath *path;
+       gint *indices;
+       gint depth;
+       gint items_count;
+       gboolean first_item_selected;
+       gboolean last_item_selected;
 
        selection = gtk_tree_view_get_selection (dialog->priv->treeview_chosen);
        count = gtk_tree_selection_count_selected_rows (selection);
        gtk_widget_set_sensitive (dialog->priv->remove_button, count > 0);
+
+       if (count != 1)
+       {
+               gtk_widget_set_sensitive (dialog->priv->up_button, FALSE);
+               gtk_widget_set_sensitive (dialog->priv->down_button, FALSE);
+               return;
+       }
+
+       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
+       g_assert (g_list_length (selected_rows) == 1);
+
+       path = selected_rows->data;
+       indices = gtk_tree_path_get_indices_with_depth (path, &depth);
+       g_assert (depth == 1);
+
+       items_count = gtk_tree_model_iter_n_children (model, NULL);
+
+       first_item_selected = indices[0] == 0;
+       last_item_selected = indices[0] == (items_count - 1);
+
+       gtk_widget_set_sensitive (dialog->priv->up_button, !first_item_selected);
+       gtk_widget_set_sensitive (dialog->priv->down_button, !last_item_selected);
+
+       g_list_free_full (selected_rows, (GDestroyNotify) gtk_tree_path_free);
 }
 
 static void
@@ -304,6 +340,82 @@ remove_button_clicked_cb (GtkWidget            *button,
 }
 
 static void
+up_button_clicked_cb (GtkWidget            *button,
+                     GeditEncodingsDialog *dialog)
+{
+       GtkTreeSelection *selection;
+       GtkTreeModel *model;
+       GList *selected_rows;
+       GtkTreePath *path;
+       GtkTreeIter iter;
+       GtkTreeIter prev_iter;
+
+       selection = gtk_tree_view_get_selection (dialog->priv->treeview_chosen);
+       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
+
+       g_return_if_fail (model == GTK_TREE_MODEL (dialog->priv->liststore_chosen));
+       g_return_if_fail (g_list_length (selected_rows) == 1);
+
+       path = selected_rows->data;
+       if (!gtk_tree_model_get_iter (model, &iter, path))
+       {
+               g_return_if_reached ();
+       }
+
+       prev_iter = iter;
+       if (!gtk_tree_model_iter_previous (model, &prev_iter))
+       {
+               g_return_if_reached ();
+       }
+
+       gtk_list_store_move_before (dialog->priv->liststore_chosen,
+                                   &iter,
+                                   &prev_iter);
+
+       update_chosen_buttons_sensitivity (dialog);
+
+       g_list_free_full (selected_rows, (GDestroyNotify) gtk_tree_path_free);
+}
+
+static void
+down_button_clicked_cb (GtkWidget            *button,
+                       GeditEncodingsDialog *dialog)
+{
+       GtkTreeSelection *selection;
+       GtkTreeModel *model;
+       GList *selected_rows;
+       GtkTreePath *path;
+       GtkTreeIter iter;
+       GtkTreeIter next_iter;
+
+       selection = gtk_tree_view_get_selection (dialog->priv->treeview_chosen);
+       selected_rows = gtk_tree_selection_get_selected_rows (selection, &model);
+
+       g_return_if_fail (model == GTK_TREE_MODEL (dialog->priv->liststore_chosen));
+       g_return_if_fail (g_list_length (selected_rows) == 1);
+
+       path = selected_rows->data;
+       if (!gtk_tree_model_get_iter (model, &iter, path))
+       {
+               g_return_if_reached ();
+       }
+
+       next_iter = iter;
+       if (!gtk_tree_model_iter_next (model, &next_iter))
+       {
+               g_return_if_reached ();
+       }
+
+       gtk_list_store_move_after (dialog->priv->liststore_chosen,
+                                  &iter,
+                                  &next_iter);
+
+       update_chosen_buttons_sensitivity (dialog);
+
+       g_list_free_full (selected_rows, (GDestroyNotify) gtk_tree_path_free);
+}
+
+static void
 init_liststores (GeditEncodingsDialog *dialog)
 {
        gchar **enc_strv;
@@ -386,15 +498,25 @@ gedit_encodings_dialog_init (GeditEncodingsDialog *dialog)
 
        g_signal_connect_swapped (selection,
                                  "changed",
-                                 G_CALLBACK (update_remove_button_sensitivity),
+                                 G_CALLBACK (update_chosen_buttons_sensitivity),
                                  dialog);
 
-       update_remove_button_sensitivity (dialog);
+       update_chosen_buttons_sensitivity (dialog);
 
        g_signal_connect (dialog->priv->remove_button,
                          "clicked",
                          G_CALLBACK (remove_button_clicked_cb),
                          dialog);
+
+       g_signal_connect (dialog->priv->up_button,
+                         "clicked",
+                         G_CALLBACK (up_button_clicked_cb),
+                         dialog);
+
+       g_signal_connect (dialog->priv->down_button,
+                         "clicked",
+                         G_CALLBACK (down_button_clicked_cb),
+                         dialog);
 }
 
 GtkWidget *
diff --git a/gedit/resources/ui/gedit-encodings-dialog.ui b/gedit/resources/ui/gedit-encodings-dialog.ui
index 2fa3f46..6abdeec 100644
--- a/gedit/resources/ui/gedit-encodings-dialog.ui
+++ b/gedit/resources/ui/gedit-encodings-dialog.ui
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.18.1 -->
 <interface>
-  <!-- interface-requires gtk+ 3.8 -->
+  <requires lib="gtk+" version="3.10"/>
   <object class="GtkListStore" id="liststore_available">
     <columns>
       <!-- column-name name -->
@@ -11,6 +12,9 @@
       <column type="GtkSourceEncoding"/>
     </columns>
   </object>
+  <object class="GtkTreeModelSort" id="sort_available">
+    <property name="model">liststore_available</property>
+  </object>
   <object class="GtkListStore" id="liststore_chosen">
     <columns>
       <!-- column-name name -->
@@ -21,15 +25,10 @@
       <column type="GtkSourceEncoding"/>
     </columns>
   </object>
-  <object class="GtkTreeModelSort" id="sort_available">
-    <property name="model">liststore_available</property>
-  </object>
   <template class="GeditEncodingsDialog" parent="GtkDialog">
     <property name="width_request">650</property>
     <property name="height_request">400</property>
     <property name="can_focus">False</property>
-    <property name="has_focus">False</property>
-    <property name="is_focus">False</property>
     <property name="border_width">5</property>
     <property name="title" translatable="yes">Character encodings</property>
     <property name="modal">True</property>
@@ -39,27 +38,19 @@
       <object class="GtkBox" id="dialog-vbox3">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
-        <property name="has_focus">False</property>
-        <property name="is_focus">False</property>
         <property name="orientation">vertical</property>
         <property name="spacing">2</property>
         <child internal-child="action_area">
           <object class="GtkButtonBox" id="dialog-action_area3">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
-            <property name="has_focus">False</property>
-            <property name="is_focus">False</property>
             <property name="layout_style">end</property>
-            <property name="border_width">5</property>
-            <property name="spacing">6</property>
             <child>
               <object class="GtkButton" id="help_button">
                 <property name="label" translatable="yes">_Help</property>
                 <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="can_default">True</property>
                 <property name="receives_default">False</property>
                 <property name="use_underline">True</property>
@@ -76,8 +67,6 @@
                 <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="can_default">True</property>
                 <property name="receives_default">False</property>
                 <property name="use_underline">True</property>
@@ -94,8 +83,6 @@
                 <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="can_default">True</property>
                 <property name="receives_default">False</property>
                 <property name="use_underline">True</property>
@@ -118,37 +105,29 @@
           <object class="GtkGrid" id="encodings_dialog_contents">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
-            <property name="has_focus">False</property>
-            <property name="is_focus">False</property>
+            <property name="border_width">6</property>
             <property name="row_spacing">6</property>
             <property name="column_spacing">6</property>
             <property name="column_homogeneous">True</property>
-            <property name="border_width">6</property>
             <child>
               <object class="GtkLabel" id="available_label">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="valign">start</property>
-                <property name="xalign">0</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>
               </object>
               <packing>
                 <property name="left_attach">0</property>
                 <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
               </packing>
             </child>
             <child>
               <object class="GtkScrolledWindow" id="scrolledwindow2">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="hexpand">True</property>
                 <property name="vexpand">True</property>
                 <property name="shadow_type">etched-in</property>
@@ -156,8 +135,6 @@
                   <object class="GtkTreeView" id="treeview_available">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <property name="has_focus">False</property>
-                    <property name="is_focus">False</property>
                     <property name="model">sort_available</property>
                     <child internal-child="selection">
                       <object class="GtkTreeSelection" id="treeview_selection">
@@ -194,8 +171,6 @@
               <packing>
                 <property name="left_attach">0</property>
                 <property name="top_attach">1</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
               </packing>
             </child>
             <child>
@@ -204,8 +179,6 @@
                 <property name="use_action_appearance">False</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="receives_default">True</property>
                 <property name="halign">start</property>
                 <property name="valign">start</property>
@@ -214,43 +187,17 @@
               <packing>
                 <property name="left_attach">0</property>
                 <property name="top_attach">2</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
-              </packing>
-            </child>
-            <child>
-              <object class="GtkButton" id="remove_button">
-                <property name="label" translatable="yes">_Remove</property>
-                <property name="use_action_appearance">False</property>
-                <property name="visible">True</property>
-                <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
-                <property name="receives_default">True</property>
-                <property name="halign">start</property>
-                <property name="valign">start</property>
-                <property name="use_underline">True</property>
-              </object>
-              <packing>
-                <property name="left_attach">1</property>
-                <property name="top_attach">2</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
               </packing>
             </child>
             <child>
               <object class="GtkScrolledWindow" id="scrolledwindow3">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="shadow_type">etched-in</property>
                 <child>
                   <object class="GtkTreeView" id="treeview_chosen">
                     <property name="visible">True</property>
                     <property name="can_focus">True</property>
-                    <property name="has_focus">False</property>
-                    <property name="is_focus">False</property>
                     <property name="hexpand">True</property>
                     <property name="vexpand">True</property>
                     <property name="model">liststore_chosen</property>
@@ -287,27 +234,80 @@
               <packing>
                 <property name="left_attach">1</property>
                 <property name="top_attach">1</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
               </packing>
             </child>
             <child>
               <object class="GtkLabel" id="chosen_label">
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
-                <property name="has_focus">False</property>
-                <property name="is_focus">False</property>
                 <property name="valign">start</property>
-                <property name="xalign">0</property>
                 <property name="label" translatable="yes">Cho_sen encodings:</property>
                 <property name="use_underline">True</property>
                 <property name="mnemonic_widget">treeview_chosen</property>
+                <property name="xalign">0</property>
               </object>
               <packing>
                 <property name="left_attach">1</property>
                 <property name="top_attach">0</property>
-                <property name="width">1</property>
-                <property name="height">1</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkGrid" id="grid1">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="column_spacing">6</property>
+                <child>
+                  <object class="GtkButton" id="remove_button">
+                    <property name="label" translatable="yes">_Remove</property>
+                    <property name="use_action_appearance">False</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <property name="halign">start</property>
+                    <property name="valign">start</property>
+                    <property name="use_underline">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">0</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkButton" id="up_button">
+                    <property name="label" translatable="yes">_Up</property>
+                    <property name="use_action_appearance">False</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <property name="halign">start</property>
+                    <property name="valign">start</property>
+                    <property name="use_underline">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">1</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkButton" id="down_button">
+                    <property name="label" translatable="yes">_Down</property>
+                    <property name="use_action_appearance">False</property>
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="receives_default">True</property>
+                    <property name="halign">start</property>
+                    <property name="valign">start</property>
+                    <property name="use_underline">True</property>
+                  </object>
+                  <packing>
+                    <property name="left_attach">2</property>
+                    <property name="top_attach">0</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="top_attach">2</property>
               </packing>
             </child>
           </object>


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