[nautilus/wip/antoniof/column-chooser-dialog-rework: 1/3] column-chooser: Build interface from template XML




commit 4e3111104ee9e6c441131654e707c3e5f79e9f85
Author: António Fernandes <antoniof gnome org>
Date:   Sat Jul 24 22:50:35 2021 +0100

    column-chooser: Build interface from template XML
    
    Keeping with the direction of preferring declarative UI definitions.
    
    Also, it will help with porting to GTK4.

 po/POTFILES.in                              |   1 +
 src/nautilus-column-chooser.c               | 185 ++++++----------------------
 src/nautilus-list-view.c                    |   1 -
 src/resources/nautilus.gresource.xml        |   1 +
 src/resources/ui/nautilus-column-chooser.ui | 172 ++++++++++++++++++++++++++
 5 files changed, 209 insertions(+), 151 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 429597558..a3ac09cd5 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -78,6 +78,7 @@ src/nautilus-x-content-bar.c
 src/resources/gtk/help-overlay.ui
 src/resources/ui/nautilus-batch-rename-dialog-menu.ui
 src/resources/ui/nautilus-batch-rename-dialog.ui
+src/resources/ui/nautilus-column-chooser.ui
 src/resources/ui/nautilus-compress-dialog.ui
 src/resources/ui/nautilus-create-folder-dialog.ui
 src/resources/ui/nautilus-file-properties-change-permissions.ui
diff --git a/src/nautilus-column-chooser.c b/src/nautilus-column-chooser.c
index 43d95c6bc..e2342835f 100644
--- a/src/nautilus-column-chooser.c
+++ b/src/nautilus-column-chooser.c
@@ -34,10 +34,9 @@ struct _NautilusColumnChooser
 {
     GtkBox parent;
 
-    GtkTreeView *view;
+    GtkWidget *view;
     GtkListStore *store;
 
-    GtkWidget *main_box;
     GtkWidget *move_up_button;
     GtkWidget *move_down_button;
     GtkWidget *use_default_button;
@@ -71,6 +70,21 @@ static guint signals[LAST_SIGNAL];
 G_DEFINE_TYPE (NautilusColumnChooser, nautilus_column_chooser, GTK_TYPE_BOX);
 
 static void nautilus_column_chooser_constructed (GObject *object);
+static void view_row_activated_callback (GtkTreeView       *tree_view,
+                                         GtkTreePath       *path,
+                                         GtkTreeViewColumn *column,
+                                         gpointer           user_data);
+static void selection_changed_callback (GtkTreeSelection *selection,
+                                        gpointer          user_data);
+static void visible_toggled_callback (GtkCellRendererToggle *cell,
+                                      char                  *path_string,
+                                      gpointer               user_data);
+static void move_up_clicked_callback (GtkWidget *button,
+                                      gpointer   user_data);
+static void move_down_clicked_callback (GtkWidget *button,
+                                        gpointer   user_data);
+static void use_default_clicked_callback (GtkWidget *button,
+                                          gpointer   user_data);
 
 static void
 nautilus_column_chooser_set_property (GObject      *object,
@@ -101,13 +115,28 @@ nautilus_column_chooser_set_property (GObject      *object,
 static void
 nautilus_column_chooser_class_init (NautilusColumnChooserClass *chooser_class)
 {
+    GtkWidgetClass *widget_class;
     GObjectClass *oclass;
 
+    widget_class = GTK_WIDGET_CLASS (chooser_class);
     oclass = G_OBJECT_CLASS (chooser_class);
 
     oclass->set_property = nautilus_column_chooser_set_property;
     oclass->constructed = nautilus_column_chooser_constructed;
 
+    gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/nautilus/ui/nautilus-column-chooser.ui");
+    gtk_widget_class_bind_template_child (widget_class, NautilusColumnChooser, view);
+    gtk_widget_class_bind_template_child (widget_class, NautilusColumnChooser, store);
+    gtk_widget_class_bind_template_child (widget_class, NautilusColumnChooser, move_up_button);
+    gtk_widget_class_bind_template_child (widget_class, NautilusColumnChooser, move_down_button);
+    gtk_widget_class_bind_template_child (widget_class, NautilusColumnChooser, use_default_button);
+    gtk_widget_class_bind_template_callback (widget_class, view_row_activated_callback);
+    gtk_widget_class_bind_template_callback (widget_class, selection_changed_callback);
+    gtk_widget_class_bind_template_callback (widget_class, visible_toggled_callback);
+    gtk_widget_class_bind_template_callback (widget_class, move_up_clicked_callback);
+    gtk_widget_class_bind_template_callback (widget_class, move_down_clicked_callback);
+    gtk_widget_class_bind_template_callback (widget_class, use_default_clicked_callback);
+
     signals[CHANGED] = g_signal_new
                            ("changed",
                            G_TYPE_FROM_CLASS (chooser_class),
@@ -140,7 +169,7 @@ update_buttons (NautilusColumnChooser *chooser)
     GtkTreeSelection *selection;
     GtkTreeIter iter;
 
-    selection = gtk_tree_view_get_selection (chooser->view);
+    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (chooser->view));
 
     if (gtk_tree_selection_get_selected (selection, NULL, &iter))
     {
@@ -241,80 +270,6 @@ row_deleted_callback (GtkTreeModel *model,
     list_changed (NAUTILUS_COLUMN_CHOOSER (user_data));
 }
 
-static void move_up_clicked_callback (GtkWidget *button,
-                                      gpointer   user_data);
-static void move_down_clicked_callback (GtkWidget *button,
-                                        gpointer   user_data);
-
-static void
-add_tree_view (NautilusColumnChooser *chooser)
-{
-    GtkWidget *scrolled;
-    GtkWidget *view;
-    GtkListStore *store;
-    GtkCellRenderer *cell;
-    GtkTreeSelection *selection;
-
-    view = gtk_tree_view_new ();
-    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (view), FALSE);
-
-    store = gtk_list_store_new (NUM_COLUMNS,
-                                G_TYPE_BOOLEAN,
-                                G_TYPE_STRING,
-                                G_TYPE_STRING,
-                                G_TYPE_BOOLEAN);
-
-    gtk_tree_view_set_model (GTK_TREE_VIEW (view),
-                             GTK_TREE_MODEL (store));
-    g_object_unref (store);
-
-    gtk_tree_view_set_reorderable (GTK_TREE_VIEW (view), TRUE);
-
-    g_signal_connect (view, "row-activated",
-                      G_CALLBACK (view_row_activated_callback), chooser);
-
-    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
-    g_signal_connect (selection, "changed",
-                      G_CALLBACK (selection_changed_callback), chooser);
-
-    cell = gtk_cell_renderer_toggle_new ();
-
-    g_signal_connect (G_OBJECT (cell), "toggled",
-                      G_CALLBACK (visible_toggled_callback), chooser);
-
-    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-                                                 -1, NULL,
-                                                 cell,
-                                                 "active", COLUMN_VISIBLE,
-                                                 "sensitive", COLUMN_SENSITIVE,
-                                                 NULL);
-
-    cell = gtk_cell_renderer_text_new ();
-
-    gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (view),
-                                                 -1, NULL,
-                                                 cell,
-                                                 "text", COLUMN_LABEL,
-                                                 "sensitive", COLUMN_SENSITIVE,
-                                                 NULL);
-
-    chooser->view = GTK_TREE_VIEW (view);
-    chooser->store = store;
-
-    gtk_widget_show (view);
-
-    scrolled = gtk_scrolled_window_new (NULL, NULL);
-    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
-                                         GTK_SHADOW_IN);
-    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
-                                    GTK_POLICY_AUTOMATIC,
-                                    GTK_POLICY_AUTOMATIC);
-    gtk_widget_show (GTK_WIDGET (scrolled));
-
-    gtk_container_add (GTK_CONTAINER (scrolled), view);
-    gtk_box_pack_start (GTK_BOX (chooser->main_box), scrolled, TRUE, TRUE, 0);
-}
-
 static void
 move_up_clicked_callback (GtkWidget *button,
                           gpointer   user_data)
@@ -325,7 +280,7 @@ move_up_clicked_callback (GtkWidget *button,
 
     chooser = NAUTILUS_COLUMN_CHOOSER (user_data);
 
-    selection = gtk_tree_view_get_selection (chooser->view);
+    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (chooser->view));
 
     if (gtk_tree_selection_get_selected (selection, NULL, &iter))
     {
@@ -356,7 +311,7 @@ move_down_clicked_callback (GtkWidget *button,
 
     chooser = NAUTILUS_COLUMN_CHOOSER (user_data);
 
-    selection = gtk_tree_view_get_selection (chooser->view);
+    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (chooser->view));
 
     if (gtk_tree_selection_get_selected (selection, NULL, &iter))
     {
@@ -383,64 +338,6 @@ use_default_clicked_callback (GtkWidget *button,
                    signals[USE_DEFAULT], 0);
 }
 
-static void
-add_buttons (NautilusColumnChooser *chooser)
-{
-    GtkWidget *inline_toolbar;
-    GtkStyleContext *style_context;
-    GtkToolItem *tool_item;
-    GtkWidget *box;
-
-    inline_toolbar = gtk_toolbar_new ();
-    gtk_widget_show (GTK_WIDGET (inline_toolbar));
-
-    style_context = gtk_widget_get_style_context (GTK_WIDGET (inline_toolbar));
-    gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_INLINE_TOOLBAR);
-    gtk_box_pack_start (GTK_BOX (chooser->main_box), inline_toolbar,
-                        FALSE, FALSE, 0);
-
-    box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
-    tool_item = gtk_tool_item_new ();
-    gtk_container_add (GTK_CONTAINER (tool_item), box);
-    gtk_container_add (GTK_CONTAINER (inline_toolbar), GTK_WIDGET (tool_item));
-
-    chooser->move_up_button = gtk_button_new_from_icon_name ("go-up-symbolic",
-                                                             GTK_ICON_SIZE_SMALL_TOOLBAR);
-    g_signal_connect (chooser->move_up_button,
-                      "clicked", G_CALLBACK (move_up_clicked_callback),
-                      chooser);
-    gtk_widget_set_sensitive (chooser->move_up_button, FALSE);
-    gtk_container_add (GTK_CONTAINER (box), chooser->move_up_button);
-
-    chooser->move_down_button = gtk_button_new_from_icon_name ("go-down-symbolic",
-                                                               GTK_ICON_SIZE_SMALL_TOOLBAR);
-    g_signal_connect (chooser->move_down_button,
-                      "clicked", G_CALLBACK (move_down_clicked_callback),
-                      chooser);
-    gtk_widget_set_sensitive (chooser->move_down_button, FALSE);
-    gtk_container_add (GTK_CONTAINER (box), chooser->move_down_button);
-
-    tool_item = gtk_separator_tool_item_new ();
-    gtk_separator_tool_item_set_draw (GTK_SEPARATOR_TOOL_ITEM (tool_item), FALSE);
-    gtk_tool_item_set_expand (tool_item, TRUE);
-    gtk_container_add (GTK_CONTAINER (inline_toolbar), GTK_WIDGET (tool_item));
-
-    box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
-    tool_item = gtk_tool_item_new ();
-    gtk_container_add (GTK_CONTAINER (tool_item), box);
-    gtk_container_add (GTK_CONTAINER (inline_toolbar), GTK_WIDGET (tool_item));
-
-    chooser->use_default_button = gtk_button_new_with_mnemonic (_("Reset to De_fault"));
-    gtk_widget_set_tooltip_text (chooser->use_default_button,
-                                 _("Replace the current List Columns settings with the default settings"));
-    g_signal_connect (chooser->use_default_button,
-                      "clicked", G_CALLBACK (use_default_clicked_callback),
-                      chooser);
-    gtk_container_add (GTK_CONTAINER (box), chooser->use_default_button);
-
-    gtk_widget_show_all (inline_toolbar);
-}
-
 static void
 populate_tree (NautilusColumnChooser *chooser)
 {
@@ -501,19 +398,7 @@ nautilus_column_chooser_constructed (GObject *object)
 static void
 nautilus_column_chooser_init (NautilusColumnChooser *chooser)
 {
-    g_object_set (G_OBJECT (chooser),
-                  "homogeneous", FALSE,
-                  "spacing", 8,
-                  "orientation", GTK_ORIENTATION_HORIZONTAL,
-                  NULL);
-
-    chooser->main_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-    gtk_widget_set_hexpand (chooser->main_box, TRUE);
-    gtk_widget_show (chooser->main_box);
-    gtk_container_add (GTK_CONTAINER (chooser), chooser->main_box);
-
-    add_tree_view (chooser);
-    add_buttons (chooser);
+    gtk_widget_init_template (GTK_WIDGET (chooser));
 }
 
 static void
diff --git a/src/nautilus-list-view.c b/src/nautilus-list-view.c
index 0ee93f640..79160e643 100644
--- a/src/nautilus-list-view.c
+++ b/src/nautilus-list-view.c
@@ -3442,7 +3442,6 @@ create_column_editor (NautilusListView *view)
     g_free (str);
 
     column_chooser = nautilus_column_chooser_new (file);
-    gtk_widget_show (column_chooser);
     gtk_box_pack_start (GTK_BOX (box), column_chooser, TRUE, TRUE, 0);
 
     g_signal_connect (column_chooser, "changed",
diff --git a/src/resources/nautilus.gresource.xml b/src/resources/nautilus.gresource.xml
index 158594e8d..adfa04861 100644
--- a/src/resources/nautilus.gresource.xml
+++ b/src/resources/nautilus.gresource.xml
@@ -7,6 +7,7 @@
     <file>ui/nautilus-toolbar.ui</file>
     <file>ui/nautilus-toolbar-switcher.ui</file>
     <file>ui/nautilus-toolbar-view-menu.ui</file>
+    <file>ui/nautilus-column-chooser.ui</file>
     <file>ui/nautilus-create-folder-dialog.ui</file>
     <file>ui/nautilus-compress-dialog.ui</file>
     <file>ui/nautilus-rename-file-popover.ui</file>
diff --git a/src/resources/ui/nautilus-column-chooser.ui b/src/resources/ui/nautilus-column-chooser.ui
new file mode 100644
index 000000000..79ead25a5
--- /dev/null
+++ b/src/resources/ui/nautilus-column-chooser.ui
@@ -0,0 +1,172 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Generated with glade 3.38.2 -->
+<interface>
+  <requires lib="gtk+" version="3.20"/>
+  <object class="GtkListStore" id="store">
+    <columns>
+      <!-- column-name COLUMN_VISIBLE -->
+      <column type="gboolean"/>
+      <!-- column-name COLUMN_LABEL -->
+      <column type="gchararray"/>
+      <!-- column-name COLUMN_NAME -->
+      <column type="gchararray"/>
+      <!-- column-name COLUMN_SENSITIVE -->
+      <column type="gboolean"/>
+    </columns>
+  </object>
+  <template class="NautilusColumnChooser" parent="GtkBox">
+    <property name="visible">True</property>
+    <property name="can-focus">False</property>
+    <property name="orientation">vertical</property>
+    <child>
+      <object class="GtkScrolledWindow">
+        <property name="visible">True</property>
+        <property name="can-focus">False</property>
+        <property name="shadow-type">in</property>
+        <child>
+          <object class="GtkTreeView" id="view">
+            <property name="visible">True</property>
+            <property name="can-focus">True</property>
+            <property name="vexpand">True</property>
+            <property name="model">store</property>
+            <property name="headers-visible">False</property>
+            <property name="reorderable">True</property>
+            <signal name="row-activated" handler="view_row_activated_callback" swapped="no"/>
+            <child internal-child="selection">
+              <object class="GtkTreeSelection">
+                <signal name="changed" handler="selection_changed_callback" swapped="no"/>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="visible-column">
+                <child>
+                  <object class="GtkCellRendererToggle">
+                    <signal name="toggled" handler="visible_toggled_callback" swapped="no"/>
+                  </object>
+                  <attributes>
+                    <attribute name="sensitive">3</attribute>
+                    <attribute name="active">0</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkTreeViewColumn" id="label-column">
+                <child>
+                  <object class="GtkCellRendererText"/>
+                  <attributes>
+                    <attribute name="sensitive">3</attribute>
+                    <attribute name="text">1</attribute>
+                  </attributes>
+                </child>
+              </object>
+            </child>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="fill">True</property>
+        <property name="position">0</property>
+      </packing>
+    </child>
+    <child>
+      <object class="GtkBox">
+        <property name="visible">True</property>
+        <property name="can-focus">False</property>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <child>
+              <object class="GtkButton" id="move_up_button">
+                <property name="visible">True</property>
+                <property name="sensitive">False</property>
+                <property name="can-focus">True</property>
+                <property name="receives-default">False</property>
+                <signal name="clicked" handler="move_up_clicked_callback" swapped="no"/>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="can-focus">False</property>
+                    <property name="icon-name">go-up-symbolic</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkButton" id="move_down_button">
+                <property name="visible">True</property>
+                <property name="sensitive">False</property>
+                <property name="can-focus">True</property>
+                <property name="receives-default">False</property>
+                <signal name="clicked" handler="move_down_clicked_callback" swapped="no"/>
+                <child>
+                  <object class="GtkImage">
+                    <property name="visible">True</property>
+                    <property name="can-focus">False</property>
+                    <property name="icon-name">go-down-symbolic</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+            <style>
+              <class name="linked"/>
+            </style>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="visible">True</property>
+            <property name="can-focus">False</property>
+            <child>
+              <object class="GtkButton" id="use_default_button">
+                <property name="label" translatable="yes">Reset do De_fault</property>
+                <property name="visible">True</property>
+                <property name="can-focus">True</property>
+                <property name="receives-default">False</property>
+                <property name="tooltip-text" translatable="yes">Replace the current List Columns settings 
with the default settings</property>
+                <property name="use-underline">True</property>
+                <signal name="clicked" handler="use_default_clicked_callback" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">True</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="pack-type">end</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <style>
+          <class name="inline-toolbar"/>
+        </style>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="fill">True</property>
+        <property name="position">1</property>
+      </packing>
+    </child>
+  </template>
+</interface>


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