[gtk+/wip/csoriano/pathbar-bin-view-window: 31/31] Experiment with a view and bin window



commit af361132b9e7d15200e4b1516669dbed1fadf4af
Author: Carlos Soriano <csoriano gnome org>
Date:   Thu Jun 2 17:01:49 2016 +0200

    Experiment with a view and bin window

 gtk/gtkhidingbox.c    |  237 ++++++++++++++++++++++++++++++-------------------
 gtk/gtkhidingbox.h    |   11 ++-
 tests/testhidingbox.c |    9 ++-
 3 files changed, 164 insertions(+), 93 deletions(-)
---
diff --git a/gtk/gtkhidingbox.c b/gtk/gtkhidingbox.c
index 2417741..b6e13b4 100644
--- a/gtk/gtkhidingbox.c
+++ b/gtk/gtkhidingbox.c
@@ -56,7 +56,9 @@ struct _GtkHidingBoxPrivate
 
   gboolean invert_animation;
 
-  GtkWidget *scrolled_window;
+  GdkWindow *bin_window;
+  GdkWindow *view_window;
+
   GtkWidget *box;
   GtkAdjustment *hadjustment;
 
@@ -71,35 +73,7 @@ struct _GtkHidingBoxPrivate
 static void
 gtk_hiding_box_buildable_init (GtkBuildableIface *iface);
 
-G_DEFINE_TYPE_WITH_CODE (GtkHidingBox, gtk_hiding_box, GTK_TYPE_CONTAINER,
-                         G_ADD_PRIVATE (GtkHidingBox)
-                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, gtk_hiding_box_buildable_init))
-
-
-static void
-gtk_hiding_box_buildable_add_child (GtkBuildable *buildable,
-                                    GtkBuilder   *builder,
-                                    GObject      *child,
-                                    const gchar  *type)
-{
-  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (GTK_HIDING_BOX (buildable));
-
-  if (!type)
-    {
-      gtk_container_add (GTK_CONTAINER (buildable), GTK_WIDGET (child));
-      priv->needs_update = TRUE;
-    }
-  else
-    {
-      GTK_BUILDER_WARN_INVALID_CHILD_TYPE (GTK_HIDING_BOX (buildable), type);
-    }
-}
-
-static void
-gtk_hiding_box_buildable_init (GtkBuildableIface *iface)
-{
-  iface->add_child = gtk_hiding_box_buildable_add_child;
-}
+G_DEFINE_TYPE (GtkHidingBox, gtk_hiding_box, GTK_TYPE_WIDGET)
 
 enum {
   PROP_0,
@@ -155,11 +129,11 @@ gtk_hiding_box_get_property (GObject    *object,
     }
 }
 
-static void
-gtk_hiding_box_add (GtkContainer *container,
+void
+gtk_hiding_box_add (GtkHidingBox *self,
                     GtkWidget    *widget)
 {
-  GtkHidingBox *box = GTK_HIDING_BOX (container);
+  GtkHidingBox *box = GTK_HIDING_BOX (self);
   GtkWidget *revealer;
   GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
   GtkStyleContext *style_context;
@@ -173,7 +147,8 @@ gtk_hiding_box_add (GtkContainer *container,
   g_print ("box fine? %s \n", G_OBJECT_TYPE_NAME (priv->box));
   gtk_container_add (GTK_CONTAINER (priv->box), revealer);
   priv->children = g_list_append (priv->children, widget);
-  gtk_widget_show (revealer);
+  gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
+  gtk_widget_show_all (revealer);
 
   g_print ("add\n");
 }
@@ -220,12 +195,11 @@ unrevealed_really_remove_child (GObject    *widget,
   really_remove_child (self, gtk_bin_get_child (GTK_BIN (widget)));
 }
 
-static void
-gtk_hiding_box_remove (GtkContainer *container,
+void
+gtk_hiding_box_remove (GtkHidingBox *self,
                        GtkWidget    *widget)
 {
-  GtkHidingBox *box = GTK_HIDING_BOX (container);
-  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
+  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
   GtkWidget *to_remove;
 
   g_print ("remove %p %s\n", widget, G_OBJECT_TYPE_NAME (widget));
@@ -237,31 +211,58 @@ gtk_hiding_box_remove (GtkContainer *container,
   priv->widgets_to_remove = g_list_append (priv->widgets_to_remove, to_remove);
   priv->children = g_list_remove (priv->children, to_remove);
   priv->needs_update = TRUE;
-  gtk_widget_queue_resize (GTK_WIDGET (container));
+
+  gtk_widget_queue_resize (GTK_WIDGET (self));
 }
 
 static void
-gtk_hiding_box_forall (GtkContainer *container,
-                       gboolean      include_internals,
-                       GtkCallback   callback,
-                       gpointer      callback_data)
+get_children_allocation (GtkHidingBox  *self,
+                         GtkAllocation *allocation,
+                         GtkAllocation *child_allocation)
 {
-  GtkHidingBox *box = GTK_HIDING_BOX (container);
-  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
+  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
+  GtkWidget *child_widget;
   GList *child;
+  GtkRequestedSize *sizes_temp;
+  gint i;
+  GList *children;
+  gint current_children_min_width = 0;
+  gint current_children_nat_width = 0;
 
-  for (child = priv->children; child != NULL; child = child->next)
-    (* callback) (child->data, callback_data);
+  children = g_list_copy (priv->children);
+  sizes_temp = g_newa (GtkRequestedSize, g_list_length (priv->children));
+  child_allocation->x = 0;
+  child_allocation->y = 0;
+  child_allocation->height = allocation->height;
 
-  if (include_internals)
-  {
-    (* callback) (priv->scrolled_window, callback_data);
+  if (priv->inverted)
+    children = g_list_reverse (children);
+
+  /* Retrieve desired size for visible children. */
+  for (i = 0, child = children; child != NULL; i++, child = child->next)
+    {
+      child_widget = GTK_WIDGET (child->data);
 
-    for (child = priv->widgets_to_remove; child != NULL; child = child->next)
-      (* callback) (child->data, callback_data);
-  }
+      /* If we are in the middle of a revealer animation, get the revealer
+       * allocation */
+      gtk_widget_get_preferred_width_for_height (gtk_widget_get_parent (child_widget),
+                                                 allocation->height,
+                                                 &sizes_temp[i].minimum_size,
+                                                 &sizes_temp[i].natural_size);
+
+      current_children_min_width += sizes_temp[i].minimum_size;
+      current_children_nat_width += sizes_temp[i].natural_size;
+    }
+
+  if (current_children_nat_width > allocation->width)
+    child_allocation->width = current_children_min_width;
+  else
+    child_allocation->width = current_children_nat_width;
+
+  g_list_free (children);
 }
 
+#if 0
 static void
 update_children_visibility (GtkHidingBox  *box,
                             GtkAllocation *allocation)
@@ -391,13 +392,13 @@ idle_update_revealers (GtkHidingBox *box)
               gdouble max_real_adjustment;
 
               max_real_adjustment = gtk_adjustment_get_upper (priv->hadjustment) -
-                                    gtk_widget_get_allocated_width (priv->scrolled_window);
+                                    gtk_widget_get_allocated_width (priv->bin_window);
 
               g_print ("animation velocity %f %f\n", max_real_adjustment, velocity);
-              revealer_animation_time = (gtk_widget_get_allocated_width (priv->scrolled_window) -
+              revealer_animation_time = (gtk_widget_get_allocated_width (priv->bin_window) -
                                          priv->allocated_children_width) /
                                          velocity;
-              g_print ("animation time %d %d %d\n", priv->allocated_children_width, 
gtk_widget_get_allocated_width (priv->scrolled_window), revealer_animation_time);
+              g_print ("animation time %d %d %d\n", priv->allocated_children_width, 
gtk_widget_get_allocated_width (priv->bin_window), revealer_animation_time);
               add_opacity_class (revealer, "pathbar-invert-animation-opacity-off");
               g_signal_connect (revealer, "notify::child-revealed", (GCallback) opacity_off, box);
               gtk_revealer_set_transition_duration (GTK_REVEALER (revealer), revealer_animation_time);
@@ -468,31 +469,37 @@ idle_update_revealers (GtkHidingBox *box)
 
 }
 
+#endif
+
 static void
 gtk_hiding_box_size_allocate (GtkWidget     *widget,
                               GtkAllocation *allocation)
 {
-  GtkHidingBox *box = GTK_HIDING_BOX (widget);
-  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
+  GtkHidingBox *self = GTK_HIDING_BOX (widget);
+  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
   GtkAllocation child_allocation;
   GtkRequestedSize *sizes;
 
   gtk_widget_set_allocation (widget, allocation);
+  gdk_window_move_resize (priv->view_window,
+                          allocation->x, allocation->y,
+                          allocation->width, allocation->height);
+
 
   sizes = g_newa (GtkRequestedSize, g_list_length (priv->children));
-  update_children_visibility (box, allocation);
+  //update_children_visibility (self, allocation);
 
-  idle_update_revealers (box);
+  //idle_update_revealers (self);
 
-  child_allocation.x = allocation->x;
-  child_allocation.y = allocation->y;
-  child_allocation.width = allocation->width;
-  child_allocation.height = allocation->height;
-  gtk_widget_size_allocate (priv->scrolled_window, &child_allocation);
+  get_children_allocation (self, allocation, &child_allocation);
+  gtk_widget_size_allocate (priv->box, &child_allocation);
 
-  _gtk_widget_set_simple_clip (widget, NULL);
+  gdk_window_move_resize (priv->bin_window,
+                          20, 0,
+                          child_allocation.width, child_allocation.height);
 }
 
+#if 0
 static void
 update_hadjustment (GtkHidingBox  *self)
 {
@@ -501,9 +508,9 @@ update_hadjustment (GtkHidingBox  *self)
   gdouble max_real_adjustment;
 
   max_real_adjustment = gtk_adjustment_get_upper (priv->hadjustment) -
-                        gtk_widget_get_allocated_width (priv->scrolled_window);
+                        gtk_widget_get_allocated_width (priv->bin_window);
 
-  g_print ("aligment changed %f %f %d %f\n", max_real_adjustment, gtk_adjustment_get_upper 
(priv->hadjustment), gtk_widget_get_allocated_width (priv->scrolled_window), gtk_adjustment_get_lower 
(priv->hadjustment));
+  g_print ("aligment changed %f %f %d %f\n", max_real_adjustment, gtk_adjustment_get_upper 
(priv->hadjustment), gtk_widget_get_allocated_width (priv->bin_window), gtk_adjustment_get_lower 
(priv->hadjustment));
   if (priv->invert_animation)
     {
       if (priv->inverted)
@@ -542,7 +549,7 @@ finish_invert_animation (GtkHidingBox *self)
   idle_update_revealers (self);
   priv->invert_animation = FALSE;
   priv->invert_animation_initial_time = 0;
-  gtk_widget_remove_tick_callback (priv->scrolled_window,
+  gtk_widget_remove_tick_callback (priv->bin_window,
                                    priv->invert_animation_tick_id);
   priv->invert_animation_tick_id = 0;
 }
@@ -559,14 +566,14 @@ invert_animation_on_tick (GtkWidget     *widget,
   gdouble max_real_adjustment;
 
   max_real_adjustment = gtk_adjustment_get_upper (priv->hadjustment) -
-                        (gdouble) gtk_widget_get_allocated_width (priv->scrolled_window);
+                        (gdouble) gtk_widget_get_allocated_width (priv->bin_window);
 
   if (priv->invert_animation_initial_time == 0)
     priv->invert_animation_initial_time = gdk_frame_clock_get_frame_time (frame_clock);
 
   elapsed = gdk_frame_clock_get_frame_time (frame_clock) - priv->invert_animation_initial_time;
   priv->invert_animation_progress = elapsed * INVERT_ANIMATION_SPEED / (1000. * max_real_adjustment);
-  g_print ("################animation progres %d %f %f %f\n", gtk_widget_get_allocated_width 
(priv->scrolled_window), max_real_adjustment, elapsed / 1000., priv->invert_animation_progress);
+  g_print ("################animation progres %d %f %f %f\n", gtk_widget_get_allocated_width 
(priv->bin_window), max_real_adjustment, elapsed / 1000., priv->invert_animation_progress);
   update_hadjustment (self);
 
   if (priv->invert_animation_progress >= 1)
@@ -602,7 +609,7 @@ start_invert_animation (GtkHidingBox *self)
       gtk_revealer_set_reveal_child (GTK_REVEALER (revealer), TRUE);
     }
 
-  priv->invert_animation_tick_id = gtk_widget_add_tick_callback (priv->scrolled_window,
+  priv->invert_animation_tick_id = gtk_widget_add_tick_callback (priv->bin_window,
                                                                  (GtkTickCallback) invert_animation_on_tick,
                                                                  self, NULL);
 }
@@ -613,6 +620,7 @@ hadjustment_on_changed (GtkAdjustment *hadjustment,
 {
   update_hadjustment (GTK_HIDING_BOX (user_data));
 }
+#endif
 
 static void
 gtk_hiding_box_get_preferred_width (GtkWidget *widget,
@@ -698,21 +706,75 @@ on_what (gpointer  data,
 }
 
 static void
+gtk_hiding_box_unrealize (GtkWidget *widget)
+{
+  GtkHidingBox *self = GTK_HIDING_BOX (widget);
+  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
+
+  gtk_widget_unregister_window (widget, priv->bin_window);
+  gdk_window_destroy (priv->bin_window);
+  priv->view_window = NULL;
+
+  GTK_WIDGET_CLASS (gtk_hiding_box_parent_class)->unrealize (widget);
+}
+
+static void
+gtk_hiding_box_realize (GtkWidget *widget)
+{
+  GtkHidingBox *self = GTK_HIDING_BOX (widget);
+  GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (self);
+  GtkAllocation allocation;
+  GtkAllocation child_allocation;
+  GdkWindowAttr attributes = { 0 };
+  GdkWindowAttributesType attributes_mask;
+
+  gtk_widget_set_realized (widget, TRUE);
+
+  gtk_widget_get_allocation (widget, &allocation);
+
+  attributes.x = allocation.x;
+  attributes.y = allocation.y;
+  attributes.width = allocation.width;
+  attributes.height = allocation.height;
+  attributes.window_type = GDK_WINDOW_CHILD;
+  attributes.wclass = GDK_INPUT_OUTPUT;
+  attributes.visual = gtk_widget_get_visual (widget);
+  attributes.event_mask = gtk_widget_get_events (widget);
+  attributes_mask = (GDK_WA_X | GDK_WA_Y) | GDK_WA_VISUAL;
+
+  priv->view_window = gdk_window_new (gtk_widget_get_parent_window ((GtkWidget*) self),
+                                      &attributes, attributes_mask);
+  gtk_widget_set_window (widget, priv->view_window);
+  gtk_widget_register_window (widget, priv->view_window);
+
+  get_children_allocation (self, &allocation, &child_allocation);
+  attributes.x = 0;
+  attributes.y = 0;
+  attributes.width = child_allocation.width;
+  attributes.height = child_allocation.height;
+
+  priv->bin_window = gdk_window_new (priv->view_window, &attributes,
+                                     attributes_mask);
+  gtk_widget_register_window (widget, priv->bin_window);
+
+  gtk_widget_set_parent_window (priv->box, priv->bin_window);
+
+  gdk_window_show (priv->bin_window);
+  gdk_window_show (priv->view_window);
+  gtk_widget_show_all (priv->box);
+}
+
+static void
 gtk_hiding_box_init (GtkHidingBox *box)
 {
   GtkHidingBoxPrivate *priv = gtk_hiding_box_get_instance_private (box);
-  GtkWidget *hscrollbar;
-
-  gtk_widget_set_has_window (GTK_WIDGET (box), FALSE);
-  priv->scrolled_window = gtk_scrolled_window_new (NULL, NULL);
-  priv->hadjustment = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (priv->scrolled_window));
-  g_signal_connect (priv->hadjustment, "changed", (GCallback) hadjustment_on_changed, box);
-  hscrollbar = gtk_scrolled_window_get_hscrollbar (GTK_SCROLLED_WINDOW (priv->scrolled_window));
-  gtk_widget_hide (hscrollbar);
+
+  gtk_widget_set_has_window (GTK_WIDGET (box), TRUE);
+  gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
+
   priv->box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
+  gtk_widget_show (priv->box);
   g_object_weak_ref (G_OBJECT (priv->box), on_what, NULL);
-  gtk_container_add (GTK_CONTAINER (priv->scrolled_window), priv->box);
-  gtk_widget_set_parent (priv->scrolled_window, GTK_WIDGET (box));
 
   priv->invert_animation = FALSE;
   priv->spacing = 0;
@@ -721,8 +783,6 @@ gtk_hiding_box_init (GtkHidingBox *box)
   priv->widgets_to_hide = NULL;
   priv->widgets_to_show = NULL;
   priv->widgets_to_remove = NULL;
-
-  gtk_widget_show_all (priv->scrolled_window);
 }
 
 static void
@@ -730,7 +790,6 @@ gtk_hiding_box_class_init (GtkHidingBoxClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
-  GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
 
   object_class->set_property = gtk_hiding_box_set_property;
   object_class->get_property = gtk_hiding_box_get_property;
@@ -739,10 +798,8 @@ gtk_hiding_box_class_init (GtkHidingBoxClass *class)
   widget_class->get_preferred_width = gtk_hiding_box_get_preferred_width;
   widget_class->get_preferred_height = gtk_hiding_box_get_preferred_height;
   widget_class->get_request_mode = gtk_hiding_box_get_request_mode;
-
-  container_class->add = gtk_hiding_box_add;
-  container_class->remove = gtk_hiding_box_remove;
-  container_class->forall = gtk_hiding_box_forall;
+  widget_class->realize = gtk_hiding_box_realize;
+  widget_class->unrealize = gtk_hiding_box_unrealize;
 
   hiding_box_properties[PROP_SPACING] =
            g_param_spec_int ("spacing",
@@ -839,7 +896,7 @@ gtk_hiding_box_set_inverted (GtkHidingBox *box,
 
       g_object_notify (G_OBJECT (box), "inverted");
 
-      start_invert_animation (box);
+      //start_invert_animation (box);
       gtk_widget_queue_resize (GTK_WIDGET (box));
     }
 }
diff --git a/gtk/gtkhidingbox.h b/gtk/gtkhidingbox.h
index e125495..6c0a874 100644
--- a/gtk/gtkhidingbox.h
+++ b/gtk/gtkhidingbox.h
@@ -42,7 +42,7 @@ typedef struct _GtkHidingBoxPrivate GtkHidingBoxPrivate;
 
 struct _GtkHidingBoxClass
 {
-  GtkContainerClass parent;
+  GtkWidgetClass parent;
 
   /* Padding for future expansion */
   gpointer reserved[10];
@@ -50,7 +50,7 @@ struct _GtkHidingBoxClass
 
 struct _GtkHidingBox
 {
-  GtkContainer parent_instance;
+  GtkWidget parent_instance;
 };
 
 GDK_AVAILABLE_IN_3_20
@@ -72,6 +72,13 @@ gboolean          gtk_hiding_box_get_inverted            (GtkHidingBox      *box
 
 GDK_AVAILABLE_IN_3_20
 GList             *gtk_hiding_box_get_overflow_children  (GtkHidingBox      *box);
+
+GDK_AVAILABLE_IN_3_20
+void               gtk_hiding_box_add                    (GtkHidingBox      *self,
+                                                          GtkWidget         *widget);
+GDK_AVAILABLE_IN_3_20
+void               gtk_hiding_box_remove                 (GtkHidingBox      *self,
+                                                          GtkWidget         *widget);
 G_END_DECLS
 
 #endif /* GTK_HIDING_BOX_H_ */
diff --git a/tests/testhidingbox.c b/tests/testhidingbox.c
index fda67ef..f0e06a6 100644
--- a/tests/testhidingbox.c
+++ b/tests/testhidingbox.c
@@ -34,8 +34,11 @@ static void
 on_button_clicked (GtkWidget *button,
                    gpointer   user_data)
 {
+
+#if 0
   g_print ("button clicked\n");
   gtk_container_remove (GTK_CONTAINER (user_data), button);
+#endif
 }
 
 static void
@@ -43,16 +46,18 @@ on_reset_button_clicked (GtkButton *reset_button)
 {
   GtkWidget *button;
 
+#if 0
   gtk_container_foreach (GTK_CONTAINER (hiding_box), (GtkCallback) gtk_widget_destroy, NULL);
 
   for (int i = 0; i < N_BUTTONS; i++)
     {
       button = gtk_button_new_with_label (get_lorem_ipsum ());
       g_signal_connect (button, "clicked", (GCallback) on_button_clicked, hiding_box);
-      gtk_container_add (GTK_CONTAINER (hiding_box), button);
+      gtk_hiding_box_add (GTK_HIDING_BOX (hiding_box), button);
     }
 
   gtk_widget_show_all (hiding_box);
+#endif
 }
 
 static void
@@ -72,10 +77,12 @@ on_remove_button (gint line)
   GList *children;
   GList *last;
 
+#if 0
   children = gtk_container_get_children (hiding_box);
   last = g_list_last (children);
   if (last)
     gtk_container_remove (hiding_box, GTK_WIDGET (last->data));
+#endif
 }
 
 static void


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