[libdazzle] prefs: add API for simplified table columns



commit d85eeff80ce76ea8a643a86ccd67821d80041404
Author: Christian Hergert <chergert redhat com>
Date:   Wed Sep 12 22:18:59 2018 -0700

    prefs: add API for simplified table columns

 src/prefs/dzl-preferences-group-private.h |  2 +
 src/prefs/dzl-preferences-group.c         | 33 ++++++++++++++
 src/prefs/dzl-preferences-group.h         | 19 ++++----
 src/prefs/dzl-preferences-view.c          | 73 +++++++++++++++++++++++++++++++
 src/prefs/dzl-preferences.c               | 22 ++++++++++
 src/prefs/dzl-preferences.h               | 11 +++++
 tests/test-preferences.c                  | 26 +++++++++++
 7 files changed, 178 insertions(+), 8 deletions(-)
---
diff --git a/src/prefs/dzl-preferences-group-private.h b/src/prefs/dzl-preferences-group-private.h
index 32d2137..261ff76 100644
--- a/src/prefs/dzl-preferences-group-private.h
+++ b/src/prefs/dzl-preferences-group-private.h
@@ -40,6 +40,8 @@ struct _DzlPreferencesGroup
 
   GtkListBoxRow *last_focused;
   guint          last_focused_tab_backward : 1;
+
+  GHashTable    *size_groups;
 };
 
 G_END_DECLS
diff --git a/src/prefs/dzl-preferences-group.c b/src/prefs/dzl-preferences-group.c
index 6142054..85df7fa 100644
--- a/src/prefs/dzl-preferences-group.c
+++ b/src/prefs/dzl-preferences-group.c
@@ -136,6 +136,7 @@ dzl_preferences_group_finalize (GObject *object)
   DzlPreferencesGroup *self = (DzlPreferencesGroup *)object;
 
   g_clear_pointer (&self->widgets, g_ptr_array_unref);
+  g_clear_pointer (&self->size_groups, g_hash_table_unref);
 
   G_OBJECT_CLASS (dzl_preferences_group_parent_class)->finalize (object);
 }
@@ -446,3 +447,35 @@ dzl_preferences_group_refilter (DzlPreferencesGroup *self,
 
   return lookup.matches;
 }
+
+/**
+ * dzl_preferences_group_get_size_group:
+ * @self: a #DzlPreferencesGroup
+ *
+ * Gets a size group that can be used to organize items in
+ * a group based on columns.
+ *
+ * Returns: (not nullable) (transfer none): a #GtkSizeGroup
+ */
+GtkSizeGroup *
+dzl_preferences_group_get_size_group (DzlPreferencesGroup *self,
+                                      guint                column)
+{
+  GtkSizeGroup *ret;
+
+  g_return_val_if_fail (DZL_IS_PREFERENCES_GROUP (self), NULL);
+
+  if (self->size_groups == NULL)
+    self->size_groups = g_hash_table_new_full (g_direct_hash,
+                                               g_direct_equal,
+                                               NULL,
+                                               g_object_unref);
+
+  if (!(ret = g_hash_table_lookup (self->size_groups, GUINT_TO_POINTER (column))))
+    {
+      ret = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
+      g_hash_table_insert (self->size_groups, GUINT_TO_POINTER (column), ret);
+    }
+
+  return ret;
+}
diff --git a/src/prefs/dzl-preferences-group.h b/src/prefs/dzl-preferences-group.h
index e8fe9ce..35d6241 100644
--- a/src/prefs/dzl-preferences-group.h
+++ b/src/prefs/dzl-preferences-group.h
@@ -33,18 +33,21 @@ DZL_AVAILABLE_IN_ALL
 G_DECLARE_FINAL_TYPE (DzlPreferencesGroup, dzl_preferences_group, DZL, PREFERENCES_GROUP, GtkBin)
 
 DZL_AVAILABLE_IN_ALL
-void         dzl_preferences_group_add          (DzlPreferencesGroup *self,
-                                                 GtkWidget           *widget);
+void         dzl_preferences_group_add            (DzlPreferencesGroup *self,
+                                                   GtkWidget           *widget);
 DZL_AVAILABLE_IN_ALL
-const gchar *dzl_preferences_group_get_title    (DzlPreferencesGroup *self);
+const gchar  *dzl_preferences_group_get_title      (DzlPreferencesGroup *self);
 DZL_AVAILABLE_IN_ALL
-gint         dzl_preferences_group_get_priority (DzlPreferencesGroup *self);
+gint          dzl_preferences_group_get_priority   (DzlPreferencesGroup *self);
 DZL_AVAILABLE_IN_ALL
-void         dzl_preferences_group_set_map      (DzlPreferencesGroup *self,
-                                                 GHashTable          *map);
+void          dzl_preferences_group_set_map        (DzlPreferencesGroup *self,
+                                                    GHashTable          *map);
 DZL_AVAILABLE_IN_ALL
-guint        dzl_preferences_group_refilter     (DzlPreferencesGroup *self,
-                                                 DzlPatternSpec      *spec);
+guint         dzl_preferences_group_refilter       (DzlPreferencesGroup *self,
+                                                    DzlPatternSpec      *spec);
+DZL_AVAILABLE_IN_3_32
+GtkSizeGroup *dzl_preferences_group_get_size_group (DzlPreferencesGroup *self,
+                                                    guint                column);
 
 
 G_END_DECLS
diff --git a/src/prefs/dzl-preferences-view.c b/src/prefs/dzl-preferences-view.c
index 19091bd..eb62e13 100644
--- a/src/prefs/dzl-preferences-view.c
+++ b/src/prefs/dzl-preferences-view.c
@@ -1002,6 +1002,78 @@ dzl_preferences_view_get_widget (DzlPreferences *preferences,
   return tracked ? tracked->widget : NULL;
 }
 
+static guint
+dzl_preferences_view_add_table_row_va (DzlPreferences *preferences,
+                                       const gchar    *page_name,
+                                       const gchar    *group_name,
+                                       GtkWidget      *first_widget,
+                                       va_list         args)
+{
+  DzlPreferencesView *self = (DzlPreferencesView *)preferences;
+  DzlPreferencesViewPrivate *priv = dzl_preferences_view_get_instance_private (self);
+  DzlPreferencesGroup *group;
+  GtkWidget *page;
+  GtkWidget *column = first_widget;
+  GtkListBoxRow *row;
+  GtkBox *box;
+  guint column_id = 0;
+  guint widget_id;
+
+  g_assert (DZL_IS_PREFERENCES_VIEW (self));
+  g_assert (page_name != NULL);
+  g_assert (group_name != NULL);
+  g_assert (GTK_IS_WIDGET (column));
+
+  page = dzl_preferences_view_get_page (self, page_name);
+
+  if (page == NULL)
+    {
+      g_warning ("No page named \"%s\" could be found.", page_name);
+      return 0;
+    }
+
+  group = dzl_preferences_page_get_group (DZL_PREFERENCES_PAGE (page), group_name);
+
+  if (group == NULL)
+    {
+      g_warning ("No such preferences group \"%s\" in page \"%s\"",
+                 group_name, page_name);
+      return 0;
+    }
+
+  row = g_object_new (DZL_TYPE_PREFERENCES_BIN,
+                      "visible", TRUE,
+                      NULL);
+  box = g_object_new (GTK_TYPE_BOX,
+                      "orientation", GTK_ORIENTATION_HORIZONTAL,
+                      "visible", TRUE,
+                      NULL);
+  gtk_container_add (GTK_CONTAINER (row), GTK_WIDGET (box));
+
+  do
+    {
+      GtkSizeGroup *size_group;
+
+      if ((size_group = dzl_preferences_group_get_size_group (group, column_id)))
+        gtk_size_group_add_widget (size_group, column);
+
+      gtk_container_add_with_properties (GTK_CONTAINER (box), column,
+                                         "expand", FALSE,
+                                         NULL);
+
+      column = va_arg (args, GtkWidget*);
+      column_id++;
+    }
+  while (column != NULL);
+
+  dzl_preferences_group_add (group, GTK_WIDGET (row));
+
+  widget_id = ++priv->last_widget_id;
+  dzl_preferences_view_track (self, widget_id, GTK_WIDGET (row));
+
+  return widget_id;
+}
+
 static void
 dzl_preferences_iface_init (DzlPreferencesInterface *iface)
 {
@@ -1017,6 +1089,7 @@ dzl_preferences_iface_init (DzlPreferencesInterface *iface)
   iface->set_page = dzl_preferences_view_set_page;
   iface->remove_id = dzl_preferences_view_remove_id;
   iface->get_widget = dzl_preferences_view_get_widget;
+  iface->add_table_row_va = dzl_preferences_view_add_table_row_va;
 }
 
 GtkWidget *
diff --git a/src/prefs/dzl-preferences.c b/src/prefs/dzl-preferences.c
index a783e67..98680fb 100644
--- a/src/prefs/dzl-preferences.c
+++ b/src/prefs/dzl-preferences.c
@@ -262,3 +262,25 @@ dzl_preferences_get_widget (DzlPreferences *self,
 
   return DZL_PREFERENCES_GET_IFACE (self)->get_widget (self, widget_id);
 }
+
+guint
+dzl_preferences_add_table_row (DzlPreferences *self,
+                               const gchar    *page_name,
+                               const gchar    *group_name,
+                               GtkWidget      *first_widget,
+                               ...)
+{
+  va_list args;
+  gint ret;
+
+  g_return_val_if_fail (DZL_IS_PREFERENCES (self), 0);
+  g_return_val_if_fail (page_name != NULL, 0);
+  g_return_val_if_fail (group_name != NULL, 0);
+  g_return_val_if_fail (GTK_IS_WIDGET (first_widget), 0);
+
+  va_start (args, first_widget);
+  ret = DZL_PREFERENCES_GET_IFACE (self)->add_table_row_va (self, page_name, group_name, first_widget, args);
+  va_end (args);
+
+  return ret;
+}
diff --git a/src/prefs/dzl-preferences.h b/src/prefs/dzl-preferences.h
index de08c8a..4be88c2 100644
--- a/src/prefs/dzl-preferences.h
+++ b/src/prefs/dzl-preferences.h
@@ -113,6 +113,11 @@ struct _DzlPreferencesInterface
                                              guint                 widget_id);
   GtkWidget *(*get_widget)                  (DzlPreferences       *self,
                                              guint                 widget_id);
+  guint      (*add_table_row_va)            (DzlPreferences       *self,
+                                             const gchar          *page_name,
+                                             const gchar          *group_name,
+                                             GtkWidget            *first_widget,
+                                             va_list               args);
 };
 
 DZL_AVAILABLE_IN_ALL
@@ -206,6 +211,12 @@ void       dzl_preferences_set_page         (DzlPreferences       *self,
 DZL_AVAILABLE_IN_ALL
 GtkWidget *dzl_preferences_get_widget       (DzlPreferences       *self,
                                              guint                 widget_id);
+DZL_AVAILABLE_IN_3_32
+guint      dzl_preferences_add_table_row    (DzlPreferences       *self,
+                                             const gchar          *page_name,
+                                             const gchar          *group_name,
+                                             GtkWidget            *first_widget,
+                                             ...) G_GNUC_NULL_TERMINATED;
 
 G_END_DECLS
 
diff --git a/tests/test-preferences.c b/tests/test-preferences.c
index 5ae50c7..4dab577 100644
--- a/tests/test-preferences.c
+++ b/tests/test-preferences.c
@@ -25,6 +25,32 @@ add_preferences (DzlPreferences *prefs)
   dzl_preferences_add_group (prefs, "appearance", "basic", NULL, 0);
   dzl_preferences_add_switch (prefs, "appearance", "basic", "com.example", "foo", NULL, NULL, "Global Dark 
Theme", "Applications need to be restarted for this change to take place", "dark theme", 0);
   dzl_preferences_add_switch (prefs, "appearance", "basic", "com.example", "foo", NULL, NULL, "Animations", 
NULL, "animations", 1);
+  dzl_preferences_add_table_row (prefs, "appearance", "basic",
+                                 g_object_new (GTK_TYPE_LABEL,
+                                               "label", "Key",
+                                               "visible", TRUE,
+                                               "width-chars", 15,
+                                               "xalign", 0.0f,
+                                               NULL),
+                                 g_object_new (GTK_TYPE_LABEL,
+                                               "hexpand", TRUE,
+                                               "xalign", 0.0f,
+                                               "label", "Value",
+                                               "visible", TRUE,
+                                               NULL),
+                                 NULL);
+  dzl_preferences_add_table_row (prefs, "appearance", "basic",
+                                 g_object_new (GTK_TYPE_LABEL,
+                                               "label", "Key 2",
+                                               "visible", TRUE,
+                                               "xalign", 0.0f,
+                                               NULL),
+                                 g_object_new (GTK_TYPE_LABEL,
+                                               "xalign", 0.0f,
+                                               "label", "Value 2",
+                                               "visible", TRUE,
+                                               NULL),
+                                 NULL);
 
   dzl_preferences_add_list_group (prefs, "appearance", "themes", "Themes", GTK_SELECTION_NONE, 10);
 


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