[gtk+/wip/css-is-awesome: 5/6] Add support for min-width, min-height, max-width and max-height



commit 810f73381f4d01519d599f1a042bb84dbf27a1b7
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Nov 1 14:51:34 2013 -0400

    Add support for min-width, min-height, max-width and max-height
    
    Do this through the new helper classes. Yes, this means that if the
    widget isn't using the new helper methods, it won't get constrainment
    support... the price we pay for backwards compatibility...

 gtk/gtkcssstylepropertyimpl.c |   43 +++++++++++++++++++++++++++++++
 gtk/gtkcsstypesprivate.h      |    4 +++
 gtk/gtkwidget.c               |   57 +++++++++++++++++++++++++++++++++++++++-
 3 files changed, 102 insertions(+), 2 deletions(-)
---
diff --git a/gtk/gtkcssstylepropertyimpl.c b/gtk/gtkcssstylepropertyimpl.c
index 3ff3d76..9ecd2c6 100644
--- a/gtk/gtkcssstylepropertyimpl.c
+++ b/gtk/gtkcssstylepropertyimpl.c
@@ -844,6 +844,16 @@ parse_border_width (GtkCssStyleProperty *property,
 }
 
 static GtkCssValue *
+parse_size (GtkCssStyleProperty *property,
+            GtkCssParser        *parser)
+{
+  return _gtk_css_number_value_parse (parser,
+                                      GTK_CSS_POSITIVE_ONLY
+                                      | GTK_CSS_NUMBER_AS_PIXELS
+                                      | GTK_CSS_PARSE_LENGTH);
+}
+
+static GtkCssValue *
 background_repeat_value_parse_one (GtkCssParser *parser)
 {
   GtkCssValue *value = _gtk_css_background_repeat_value_try_parse (parser);
@@ -975,6 +985,39 @@ _gtk_css_style_property_init_properties (void)
                                           NULL,
                                           _gtk_css_shadows_value_new_none ());
 
+  gtk_css_style_property_register        ("min-width",
+                                          GTK_CSS_PROPERTY_MIN_WIDTH,
+                                          G_TYPE_INT,
+                                          GTK_STYLE_PROPERTY_ANIMATED,
+                                          parse_size,
+                                          query_length_as_int,
+                                          assign_length_from_int,
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
+  gtk_css_style_property_register        ("min-height",
+                                          GTK_CSS_PROPERTY_MIN_HEIGHT,
+                                          G_TYPE_INT,
+                                          GTK_STYLE_PROPERTY_ANIMATED,
+                                          parse_size,
+                                          query_length_as_int,
+                                          assign_length_from_int,
+                                          _gtk_css_number_value_new (0.0, GTK_CSS_PX));
+  gtk_css_style_property_register        ("max-width",
+                                          GTK_CSS_PROPERTY_MAX_WIDTH,
+                                          G_TYPE_INT,
+                                          GTK_STYLE_PROPERTY_ANIMATED,
+                                          parse_size,
+                                          query_length_as_int,
+                                          assign_length_from_int,
+                                          _gtk_css_number_value_new (G_MAXDOUBLE, GTK_CSS_PX));
+  gtk_css_style_property_register        ("max-height",
+                                          GTK_CSS_PROPERTY_MAX_HEIGHT,
+                                          G_TYPE_INT,
+                                          GTK_STYLE_PROPERTY_ANIMATED,
+                                          parse_size,
+                                          query_length_as_int,
+                                          assign_length_from_int,
+                                          _gtk_css_number_value_new (G_MAXDOUBLE, GTK_CSS_PX));
+
   gtk_css_style_property_register        ("margin-top",
                                           GTK_CSS_PROPERTY_MARGIN_TOP,
                                           G_TYPE_INT,
diff --git a/gtk/gtkcsstypesprivate.h b/gtk/gtkcsstypesprivate.h
index 72511d9..1d0424a 100644
--- a/gtk/gtkcsstypesprivate.h
+++ b/gtk/gtkcsstypesprivate.h
@@ -82,6 +82,10 @@ enum { /*< skip >*/
   GTK_CSS_PROPERTY_TEXT_SHADOW,
   GTK_CSS_PROPERTY_ICON_SHADOW,
   GTK_CSS_PROPERTY_BOX_SHADOW,
+  GTK_CSS_PROPERTY_MIN_WIDTH,
+  GTK_CSS_PROPERTY_MIN_HEIGHT,
+  GTK_CSS_PROPERTY_MAX_WIDTH,
+  GTK_CSS_PROPERTY_MAX_HEIGHT,
   GTK_CSS_PROPERTY_MARGIN_TOP,
   GTK_CSS_PROPERTY_MARGIN_LEFT,
   GTK_CSS_PROPERTY_MARGIN_BOTTOM,
diff --git a/gtk/gtkwidget.c b/gtk/gtkwidget.c
index 459c4a0..b8bd904 100644
--- a/gtk/gtkwidget.c
+++ b/gtk/gtkwidget.c
@@ -16044,6 +16044,42 @@ get_content_offset_y (GtkStyleContext *context)
           _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 0));
 }
 
+static void
+constrain_width (GtkStyleContext *context,
+                 int             *width)
+{
+  double min_width, max_width;
+
+  if (!width)
+    return;
+
+  min_width = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MIN_WIDTH), 0);
+  max_width = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MAX_WIDTH), 0);
+
+  if (*width < min_width)
+    *width = min_width;
+  if (*width > max_width)
+    *width = max_width;
+}
+
+static void
+constrain_height (GtkStyleContext *context,
+                 int             *height)
+{
+  double min_height, max_height;
+
+  if (!height)
+    return;
+
+  min_height = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MIN_HEIGHT), 0);
+  max_height = _gtk_css_number_value_get (_gtk_style_context_peek_property (context, 
GTK_CSS_PROPERTY_MAX_HEIGHT), 0);
+
+  if (*height < min_height)
+    *height = min_height;
+  if (*height > max_height)
+    *height = max_height;
+}
+
 /**
  * _gtk_widget_get_context_box:
  * @context: A #GtkWidget
@@ -16070,8 +16106,19 @@ _gtk_widget_get_content_box (GtkWidget           *widget,
   content_box->x = get_content_offset_x (context);
   content_box->y = get_content_offset_y (context);
 
-  content_box->width = MAX (box->width - get_width_inc (context), 0);
-  content_box->height = MAX (box->height - get_height_inc (context), 0);
+  content_box->width = box->width;
+  content_box->height = box->height;
+
+  constrain_width (context, &content_box->width);
+  constrain_height (context, &content_box->height);
+
+  content_box->width -= get_width_inc (context);
+  content_box->height -= get_height_inc (context);
+
+  if (content_box->width < 0)
+    content_box->width = 0;
+  if (content_box->height < 0)
+    content_box->height = 0;
 }
 
 /**
@@ -16125,6 +16172,9 @@ _gtk_widget_adjust_preferred_width (GtkWidget *widget,
     *minimum_width += width_inc;
   if (natural_width)
     *natural_width += width_inc;
+
+  constrain_width (context, minimum_width);
+  constrain_width (context, natural_width);
 }
 
 /**
@@ -16177,6 +16227,9 @@ _gtk_widget_adjust_preferred_height (GtkWidget *widget,
     *minimum_height += height_inc;
   if (natural_height)
     *natural_height += height_inc;
+
+  constrain_height (context, minimum_height);
+  constrain_height (context, natural_height);
 }
 
 /**


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