[libdazzle] box: rely on GtkBox ordering



commit 366eaa32fade272dbfc8bb4b5419d0112d5d0636
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jul 10 17:30:36 2017 -0700

    box: rely on GtkBox ordering
    
    The positioning in box is updated with each change, so we can
    just rely on that rather than our crude hack which wasn't really
    working correctly anyway.

 src/menus/dzl-menu-button-section.c |    4 +-
 src/menus/dzl-menu-button.c         |    4 +-
 src/widgets/dzl-box.c               |   86 -----------------------------------
 src/widgets/dzl-box.h               |    3 -
 tests/meson.build                   |    6 +++
 tests/test-box.c                    |   53 +++++++++++++++++++++
 6 files changed, 65 insertions(+), 91 deletions(-)
---
diff --git a/src/menus/dzl-menu-button-section.c b/src/menus/dzl-menu-button-section.c
index 9dec193..6d625b2 100644
--- a/src/menus/dzl-menu-button-section.c
+++ b/src/menus/dzl-menu-button-section.c
@@ -91,7 +91,9 @@ dzl_menu_button_section_items_changed (DzlMenuButtonSection *self,
                            "accel", accel,
                            "visible", TRUE,
                            NULL);
-      dzl_box_insert (self->items_box, GTK_WIDGET (item), i);
+      gtk_container_add_with_properties (GTK_CONTAINER (self->items_box), GTK_WIDGET (item),
+                                         "position", i,
+                                         NULL);
     }
 }
 
diff --git a/src/menus/dzl-menu-button.c b/src/menus/dzl-menu-button.c
index ec00eac..f17e514 100644
--- a/src/menus/dzl-menu-button.c
+++ b/src/menus/dzl-menu-button.c
@@ -113,7 +113,9 @@ dzl_menu_button_add_linked_model (DzlMenuButton *self,
                           "text-size-group", priv->text_size_group,
                           "visible", TRUE,
                           NULL);
-  dzl_box_insert (priv->popover_box, GTK_WIDGET (section), position);
+  gtk_container_add_with_properties (GTK_CONTAINER (priv->popover_box), GTK_WIDGET (section),
+                                     "position", position,
+                                     NULL);
 }
 
 static void
diff --git a/src/widgets/dzl-box.c b/src/widgets/dzl-box.c
index a5a9c43..283fccc 100644
--- a/src/widgets/dzl-box.c
+++ b/src/widgets/dzl-box.c
@@ -23,7 +23,6 @@
 typedef struct
 {
   gint  max_width_request;
-  guint has_used_insert : 1;
 } DzlBoxPrivate;
 
 enum {
@@ -36,87 +35,6 @@ G_DEFINE_TYPE_WITH_PRIVATE (DzlBox, dzl_box, GTK_TYPE_BOX)
 
 static GParamSpec *properties [LAST_PROP];
 
-static gint
-sort_by_position (GtkWidget    *child_a,
-                  GtkWidget    *child_b,
-                  GtkContainer *container)
-{
-  gint pos_a = 0;
-  gint pos_b = 0;
-
-  gtk_container_child_get (container, child_a, "position", &pos_a, NULL);
-  gtk_container_child_get (container, child_b, "position", &pos_b, NULL);
-
-  return pos_a - pos_b;
-}
-
-static void
-dzl_box_resort (DzlBox *self)
-{
-  GList *children;
-  gint i;
-
-  g_assert (DZL_IS_BOX (self));
-
-  if (gtk_widget_in_destruction (GTK_WIDGET (self)))
-    return;
-
-  children = gtk_container_get_children (GTK_CONTAINER (self));
-  children = g_list_sort_with_data (children, (GCompareDataFunc)sort_by_position, self);
-
-  i = 0;
-
-  for (const GList *iter = children; iter; iter = iter->next)
-    {
-      GtkWidget *child = iter->data;
-      guint position = ++i * 2 + 1;
-
-      gtk_container_child_set (GTK_CONTAINER (self), child,
-                               "position", position,
-                               NULL);
-    }
-
-  g_list_free (children);
-}
-
-static void
-dzl_box_remove (GtkContainer *container,
-                GtkWidget    *widget)
-{
-  DzlBox *self = (DzlBox *)container;
-  DzlBoxPrivate *priv = dzl_box_get_instance_private (self);
-
-  g_return_if_fail (DZL_IS_BOX (self));
-
-  GTK_CONTAINER_CLASS (dzl_box_parent_class)->remove (container, widget);
-
-  /* Only reshuffle if we've used insert */
-  if (priv->has_used_insert)
-    dzl_box_resort (self);
-}
-
-void
-dzl_box_insert (DzlBox    *self,
-                GtkWidget *widget,
-                guint      position)
-{
-  DzlBoxPrivate *priv = dzl_box_get_instance_private (self);
-
-  g_return_if_fail (DZL_IS_BOX (self));
-  g_return_if_fail (GTK_IS_WIDGET (widget));
-
-  priv->has_used_insert = TRUE;
-
-  /* We have position*2 so we have enough space to insert things
-   * before their target position. After adding or removing widgets,
-   * we update positions to match.
-   */
-  gtk_container_add_with_properties (GTK_CONTAINER (self), widget,
-                                     "position", position * 2,
-                                     NULL);
-  dzl_box_resort (self);
-}
-
 /**
  * dzl_box_get_nth_child:
  * @self: a #DzlBox
@@ -136,7 +54,6 @@ dzl_box_get_nth_child (DzlBox *self,
   g_return_val_if_fail (GTK_IS_BOX (self), NULL);
 
   list = gtk_container_get_children (GTK_CONTAINER (self));
-  list = g_list_sort_with_data (list, (GCompareDataFunc)sort_by_position, self);
   ret = g_list_nth_data (list, nth);
   g_list_free (list);
 
@@ -211,15 +128,12 @@ dzl_box_class_init (DzlBoxClass *klass)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
-  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
 
   object_class->get_property = dzl_box_get_property;
   object_class->set_property = dzl_box_set_property;
 
   widget_class->get_preferred_width = dzl_box_get_preferred_width;
 
-  container_class->remove = dzl_box_remove;
-
   properties [PROP_MAX_WIDTH_REQUEST] =
     g_param_spec_int ("max-width-request",
                       "Max Width Request",
diff --git a/src/widgets/dzl-box.h b/src/widgets/dzl-box.h
index 33d8462..1abc2f2 100644
--- a/src/widgets/dzl-box.h
+++ b/src/widgets/dzl-box.h
@@ -33,9 +33,6 @@ struct _DzlBoxClass
 };
 
 GtkWidget *dzl_box_new                   (void);
-void       dzl_box_insert                (DzlBox    *self,
-                                          GtkWidget *widget,
-                                          guint      position);
 GtkWidget *dzl_box_get_nth_child         (DzlBox    *self,
                                           guint      nth);
 gint       dzl_box_get_max_width_request (DzlBox    *self);
diff --git a/tests/meson.build b/tests/meson.build
index 18a65d7..aadb66a 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -306,4 +306,10 @@ test_joined_menu = executable('test-joined-menu', 'test-joined-menu.c',
   dependencies: libdazzle_deps + [libdazzle_dep],
 )
 
+test_box = executable('test-box', 'test-box.c',
+        c_args: test_cflags,
+     link_args: test_link_args,
+  dependencies: libdazzle_deps + [libdazzle_dep],
+)
+
 endif
diff --git a/tests/test-box.c b/tests/test-box.c
new file mode 100644
index 0000000..93a9163
--- /dev/null
+++ b/tests/test-box.c
@@ -0,0 +1,53 @@
+#include <dazzle.h>
+
+gint
+main (gint argc,
+      gchar *argv[])
+{
+  GtkWidget *window;
+  GtkWidget *box;
+  GtkWidget *label;
+
+  gtk_init (&argc, &argv);
+
+  window = g_object_new (GTK_TYPE_WINDOW,
+                         "title", "Box Test",
+                         NULL);
+  box = g_object_new (DZL_TYPE_BOX,
+                      "spacing", 12,
+                      "visible", TRUE,
+                      NULL);
+  gtk_container_add (GTK_CONTAINER (window), box);
+
+  for (guint i = 0; i < 10; i += 2)
+    {
+      g_autofree gchar *str = g_strdup_printf ("%u", i);
+
+      label = g_object_new (GTK_TYPE_LABEL,
+                            "label", str,
+                            "visible", TRUE,
+                            NULL);
+      gtk_container_add_with_properties (GTK_CONTAINER (box), label,
+                                         "position", i,
+                                         NULL);
+    }
+
+  for (guint i = 1; i < 10; i += 2)
+    {
+      g_autofree gchar *str = g_strdup_printf ("%u", i);
+
+      label = g_object_new (GTK_TYPE_LABEL,
+                            "label", str,
+                            "visible", TRUE,
+                            NULL);
+      gtk_container_add_with_properties (GTK_CONTAINER (box), label,
+                                         "position", i,
+                                         NULL);
+    }
+
+  g_signal_connect (window, "delete-event", gtk_main_quit, NULL);
+  gtk_window_present (GTK_WINDOW (window));
+  gtk_main ();
+
+  return 0;
+}


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