[gtk/prop-list: 66/73] gtk-demo: Demo columnview sorting



commit fb0cec827a8ca71c683f9f9a84d065b442dc9b57
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Dec 14 11:53:24 2019 -0500

    gtk-demo: Demo columnview sorting
    
    Enhance the settings demo to have a sortable column.

 demos/gtk-demo/listview_settings.c  | 22 +++++++++++++++++-----
 demos/gtk-demo/listview_settings.ui |  7 ++++---
 2 files changed, 21 insertions(+), 8 deletions(-)
---
diff --git a/demos/gtk-demo/listview_settings.c b/demos/gtk-demo/listview_settings.c
index 75af45a0c0..6480d8afbc 100644
--- a/demos/gtk-demo/listview_settings.c
+++ b/demos/gtk-demo/listview_settings.c
@@ -2,7 +2,9 @@
  *
  * This demo shows a settings viewer for GSettings.
  *
- * It demonstrates how to implement support for trees with listview.
+ * It demonstrates how to implement support for trees with GtkListView.
+ *
+ * It also shows how to set up sorting for columns in a GtkColumnView.
  */
 
 #include <gtk/gtk.h>
@@ -119,12 +121,13 @@ static gboolean
 transform_settings_to_keys (GBinding     *binding,
                             const GValue *from_value,
                             GValue       *to_value,
-                            gpointer      unused)
+                            gpointer      data)
 {
   GtkTreeListRow *treelistrow;
   GSettings *settings;
   GSettingsSchema *schema;
   GListStore *store;
+  GtkSortListModel *sort_model;
   char **keys;
   guint i;
 
@@ -137,7 +140,6 @@ transform_settings_to_keys (GBinding     *binding,
   store = g_list_store_new (SETTINGS_TYPE_KEY);
 
   keys = g_settings_schema_list_keys (schema);
-  qsort (keys, g_strv_length (keys), sizeof (char *), strvcmp);
 
   for (i = 0; keys[i] != NULL; i++)
     {
@@ -152,7 +154,10 @@ transform_settings_to_keys (GBinding     *binding,
   g_settings_schema_unref (schema);
   g_object_unref (settings);
 
-  g_value_take_object (to_value, store);
+  sort_model = gtk_sort_list_model_new (G_LIST_MODEL (store),
+                                        gtk_column_view_get_sorter (GTK_COLUMN_VIEW (data)));
+
+  g_value_take_object (to_value, sort_model);
 
   return TRUE;
 }
@@ -215,6 +220,8 @@ do_listview_settings (GtkWidget *do_widget)
       GtkTreeListModel *treemodel;
       GtkSingleSelection *selection;
       GtkBuilder *builder;
+      GtkColumnViewColumn *name_column;
+      GtkSorter *sorter;
 
       g_type_ensure (SETTINGS_TYPE_KEY);
 
@@ -240,11 +247,16 @@ do_listview_settings (GtkWidget *do_widget)
                                    G_BINDING_SYNC_CREATE,
                                    transform_settings_to_keys,
                                    NULL,
-                                   NULL, NULL);
+                                   columnview, NULL);
       gtk_list_view_set_model (GTK_LIST_VIEW (listview), G_LIST_MODEL (selection));
       g_object_unref (selection);
       g_object_unref (treemodel);
       g_object_unref (model);
+
+      name_column = GTK_COLUMN_VIEW_COLUMN (gtk_builder_get_object (builder, "name_column"));
+      sorter = gtk_string_sorter_new (gtk_property_expression_new (SETTINGS_TYPE_KEY, NULL, "name"));
+      gtk_column_view_column_set_sorter (name_column, sorter);
+      g_object_unref (sorter);
     }
 
   if (!gtk_widget_get_visible (window))
diff --git a/demos/gtk-demo/listview_settings.ui b/demos/gtk-demo/listview_settings.ui
index 529adb4756..2bc197f68f 100644
--- a/demos/gtk-demo/listview_settings.ui
+++ b/demos/gtk-demo/listview_settings.ui
@@ -2,10 +2,11 @@
 <interface>
   <object class="GtkWindow" id="window">
     <property name="title" translatable="yes">Settings</property>
-    <property name="default-width">600</property>
-    <property name="default-height">400</property>
+    <property name="default-width">640</property>
+    <property name="default-height">480</property>
     <child>
       <object class="GtkPaned">
+        <property name="position">300</property>
         <child>
           <object class="GtkScrolledWindow">
             <child>
@@ -47,7 +48,7 @@
             <child>
               <object class="GtkColumnView" id="columnview">
                 <child>
-                  <object class="GtkColumnViewColumn">
+                  <object class="GtkColumnViewColumn" id="name_column">
                     <property name="title">Name</property>
                     <property name="factory">
                       <object class="GtkBuilderListItemFactory">


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