[gtk+/wip/csoriano/bookmarks: 2/4] gtkrevealer: add css padding support



commit 15007642183402568d7ab7dfcdc39121e8034f40
Author: Carlos Soriano <csoriano gnome org>
Date:   Wed Jun 3 14:58:04 2015 +0200

    gtkrevealer: add css padding support
    
    Add css padding support to GtkRevealer.
    
    As a future work, GtkRevealer still needs to support the border
    property.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=750338

 gtk/gtkrevealer.c |  172 +++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 142 insertions(+), 30 deletions(-)
---
diff --git a/gtk/gtkrevealer.c b/gtk/gtkrevealer.c
index 5509b75..fd7290a 100644
--- a/gtk/gtkrevealer.c
+++ b/gtk/gtkrevealer.c
@@ -1,6 +1,6 @@
 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /*
- * Copyright (c) 2013 Red Hat, Inc.
+ * Copyright (c) 2013, 2015 Red Hat, Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Lesser General Public License as published by
@@ -17,7 +17,7 @@
  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  * Author: Alexander Larsson <alexl redhat com>
- *
+ *         Carlos Soriano <csoriano gnome org>
  */
 
 #include "config.h"
@@ -116,6 +116,26 @@ static void     gtk_revealer_real_get_preferred_width_for_height (GtkWidget
 G_DEFINE_TYPE_WITH_PRIVATE (GtkRevealer, gtk_revealer, GTK_TYPE_BIN)
 
 static void
+gtk_revealer_get_full_border (GtkRevealer *revealer,
+                              GtkBorder   *full_border)
+{
+  GtkWidget *widget = GTK_WIDGET (revealer);
+  GtkStyleContext *context;
+  GtkStateFlags state;
+  GtkBorder padding;
+
+  context = gtk_widget_get_style_context (widget);
+  state = gtk_style_context_get_state (context);
+
+  gtk_style_context_get_padding (context, state, &padding);
+
+  full_border->left = padding.left;
+  full_border->right = padding.right;
+  full_border->top = padding.top;
+  full_border->bottom = padding.bottom;
+}
+
+static void
 gtk_revealer_init (GtkRevealer *revealer)
 {
   GtkRevealerPrivate *priv = gtk_revealer_get_instance_private (revealer);
@@ -297,10 +317,16 @@ gtk_revealer_get_child_allocation (GtkRevealer   *revealer,
 {
   GtkWidget *child;
   GtkRevealerTransitionType transition;
+  GtkBorder border;
+  gint vertical_border, horizontal_border;
 
   g_return_if_fail (revealer != NULL);
   g_return_if_fail (allocation != NULL);
 
+  gtk_revealer_get_full_border (revealer, &border);
+  vertical_border = border.top + border.bottom;
+  horizontal_border = border.left + border.right;
+
   child_allocation->x = 0;
   child_allocation->y = 0;
   child_allocation->width = 0;
@@ -312,15 +338,15 @@ gtk_revealer_get_child_allocation (GtkRevealer   *revealer,
       transition = effective_transition (revealer);
       if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
           transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
-        gtk_widget_get_preferred_width_for_height (child, allocation->height, NULL,
+        gtk_widget_get_preferred_width_for_height (child, MAX (0, allocation->height - vertical_border), 
NULL,
                                                    &child_allocation->width);
       else
-        gtk_widget_get_preferred_height_for_width (child, allocation->width, NULL,
+        gtk_widget_get_preferred_height_for_width (child, MAX (0, allocation->width - horizontal_border), 
NULL,
                                                    &child_allocation->height);
     }
 
-  child_allocation->width = MAX (child_allocation->width, allocation->width);
-  child_allocation->height = MAX (child_allocation->height, allocation->height);
+  child_allocation->width = MAX (child_allocation->width, allocation->width - horizontal_border);
+  child_allocation->height = MAX (child_allocation->height, allocation->height - vertical_border);
 }
 
 static void
@@ -335,6 +361,7 @@ gtk_revealer_real_realize (GtkWidget *widget)
   GtkWidget *child;
   GtkStyleContext *context;
   GtkRevealerTransitionType transition;
+  GtkBorder border;
 
   gtk_widget_set_realized (widget, TRUE);
 
@@ -359,16 +386,29 @@ gtk_revealer_real_realize (GtkWidget *widget)
 
   gtk_revealer_get_child_allocation (revealer, &allocation, &child_allocation);
 
+  gtk_revealer_get_full_border (revealer, &border);
   attributes.x = 0;
   attributes.y = 0;
   attributes.width = child_allocation.width;
   attributes.height = child_allocation.height;
 
+  /* See explanation on gtk_revealer_real_size_allocate */
   transition = effective_transition (revealer);
   if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
-    attributes.y = allocation.height - child_allocation.height;
+    {
+      attributes.y = allocation.height - child_allocation.height - border.bottom;
+      attributes.x = border.left;
+    }
   else if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
-    attributes.x = allocation.width - child_allocation.width;
+    {
+      attributes.y = border.top;
+      attributes.x = allocation.width - child_allocation.width - border.right;
+    }
+ else
+   {
+     attributes.y = border.top;
+     attributes.x = border.left;
+   }
 
   priv->bin_window =
     gdk_window_new (priv->view_window, &attributes, attributes_mask);
@@ -440,6 +480,7 @@ gtk_revealer_real_size_allocate (GtkWidget     *widget,
   gboolean window_visible;
   int bin_x, bin_y;
   GtkRevealerTransitionType transition;
+  GtkBorder border;
 
   g_return_if_fail (allocation != NULL);
 
@@ -463,17 +504,54 @@ gtk_revealer_real_size_allocate (GtkWidget     *widget,
             gdk_window_show (priv->view_window);
         }
 
+      /* The view window will follow the revealer allocation, which is modified
+       * along the animation */
       gdk_window_move_resize (priv->view_window,
                               allocation->x, allocation->y,
                               allocation->width, allocation->height);
 
+      gtk_revealer_get_full_border (revealer, &border);
       bin_x = 0;
       bin_y = 0;
+
       transition = effective_transition (revealer);
+      /* The child allocation is fixed (it's doesn't modifies with the animation),
+       * and it's added to the bin_window at 0,0.
+       * The bin_window has the same allocation as the child, and then the bin_window
+       * deals with the relative positioning with respect to the revealer taking
+       * into account the borders of the revealer.
+       *
+       * For most of transitions, the bin_window moves along with the revealer,
+       * as its allocation changes.
+       * However for GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN
+       * we need to first move the bin_window upwards and then slide it down in
+       * the revealer.
+       * Otherwise the child would appear as static and the revealer will allocate
+       * following the animation, clipping the child.
+       * To calculate the correct y position for this case:
+       * allocation->height - child_allocation.height is the relative position
+       * towards the revealer taking into account the animation progress with
+       * both vertical borders added, therefore we need to substract the part
+       * that we don't want to take into account for the y position, which
+       * in this case is the bottom border.
+       *
+       * And the same special treatment is needed for GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT.
+       */
       if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
-        bin_y = allocation->height - child_allocation.height;
+        {
+          bin_y = allocation->height - child_allocation.height - border.bottom;
+          bin_x = border.left;
+        }
       else if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
-        bin_x = allocation->width - child_allocation.width;
+        {
+          bin_y = border.top;
+          bin_x = allocation->width - child_allocation.width - border.right;
+        }
+     else
+       {
+         bin_x = border.left;
+         bin_y = border.top;
+       }
 
       gdk_window_move_resize (priv->bin_window,
                               bin_x, bin_y,
@@ -745,18 +823,27 @@ gtk_revealer_real_get_preferred_height (GtkWidget *widget,
   gint minimum_height;
   gint natural_height;
   GtkRevealerTransitionType transition;
+  GtkBorder full_border;
+  gint vertical_border;
 
   GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_height (widget, &minimum_height, 
&natural_height);
 
+  gtk_revealer_get_full_border (revealer, &full_border);
+  vertical_border = full_border.top + full_border.bottom;
+  natural_height = natural_height + vertical_border;
+  minimum_height = minimum_height + vertical_border;
+
   transition = effective_transition (revealer);
   if (transition == GTK_REVEALER_TRANSITION_TYPE_NONE ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
-    natural_height = round (natural_height * priv->current_pos);
-
-  minimum_height = MIN (minimum_height, natural_height);
+    {
+      /* Borders are included in the animation */
+      natural_height = round (natural_height * priv->current_pos);
+      minimum_height = round (minimum_height * priv->current_pos);
+    }
 
-  *minimum_height_out = minimum_height;
+  *minimum_height_out = MIN (minimum_height, natural_height);
   *natural_height_out = natural_height;
 }
 
@@ -771,18 +858,27 @@ gtk_revealer_real_get_preferred_height_for_width (GtkWidget *widget,
   gint minimum_height;
   gint natural_height;
   GtkRevealerTransitionType transition;
+  GtkBorder full_border;
+  gint vertical_border;
 
   GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_height_for_width (widget, width, 
&minimum_height, &natural_height);
 
+  gtk_revealer_get_full_border (revealer, &full_border);
+  vertical_border = full_border.top + full_border.bottom;
+  natural_height = natural_height + vertical_border;
+  minimum_height = minimum_height + vertical_border;
+
   transition = effective_transition (revealer);
   if (transition == GTK_REVEALER_TRANSITION_TYPE_NONE ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN)
-    natural_height = round (natural_height * priv->current_pos);
-
-  minimum_height = MIN (minimum_height, natural_height);
+    {
+      /* Borders are included in the animation */
+      natural_height = round (natural_height * priv->current_pos);
+      minimum_height = round (minimum_height * priv->current_pos);
+    }
 
-  *minimum_height_out = minimum_height;
+  *minimum_height_out = MIN (minimum_height, natural_height);
   *natural_height_out = natural_height;
 }
 
@@ -796,18 +892,26 @@ gtk_revealer_real_get_preferred_width (GtkWidget *widget,
   gint minimum_width;
   gint natural_width;
   GtkRevealerTransitionType transition;
+  GtkBorder full_border;
+  gint horizontal_border;
 
   GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_width (widget, &minimum_width, &natural_width);
 
+  gtk_revealer_get_full_border (revealer, &full_border);
+  horizontal_border = full_border.left + full_border.right;
+  natural_width = natural_width + horizontal_border;
+  minimum_width = minimum_width + horizontal_border;
+
   transition = effective_transition (revealer);
-  if (transition == GTK_REVEALER_TRANSITION_TYPE_NONE ||
-      transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
+  if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
-    natural_width = round (natural_width * priv->current_pos);
-
-  minimum_width = MIN (minimum_width, natural_width);
+    {
+      /* Borders are included in the animation */
+      natural_width = round (natural_width * priv->current_pos);
+      minimum_width = round (minimum_width * priv->current_pos);
+    }
 
-  *minimum_width_out = minimum_width;
+  *minimum_width_out = MIN (minimum_width, natural_width);
   *natural_width_out = natural_width;
 }
 
@@ -822,18 +926,26 @@ gtk_revealer_real_get_preferred_width_for_height (GtkWidget *widget,
   gint minimum_width;
   gint natural_width;
   GtkRevealerTransitionType transition;
+  GtkBorder full_border;
+  gint horizontal_border;
 
   GTK_WIDGET_CLASS (gtk_revealer_parent_class)->get_preferred_width_for_height (widget, height, 
&minimum_width, &natural_width);
 
+  gtk_revealer_get_full_border (revealer, &full_border);
+  horizontal_border = full_border.left + full_border.right;
+  natural_width = natural_width + horizontal_border;
+  minimum_width = minimum_width + horizontal_border;
+
   transition = effective_transition (revealer);
-  if (transition == GTK_REVEALER_TRANSITION_TYPE_NONE ||
-      transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
+  if (transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_LEFT ||
       transition == GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT)
-    natural_width = round (natural_width * priv->current_pos);
-
-  minimum_width = MIN (minimum_width, natural_width);
+    {
+      /* Borders are included in the animation */
+      natural_width = round (natural_width * priv->current_pos);
+      minimum_width = round (minimum_width * priv->current_pos);
+    }
 
-  *minimum_width_out = minimum_width;
+  *minimum_width_out = MIN (minimum_width, natural_width);
   *natural_width_out = natural_width;
 }
 


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