cssvalue: Add a cssvalue for background-size

This commit is contained in:
Benjamin Otte 2012-04-04 12:39:55 +02:00
parent f785f7177c
commit cfc6462730
9 changed files with 317 additions and 170 deletions

View File

@ -424,6 +424,7 @@ gtk_private_h_sources = \
gtkcolorchooserprivate.h \
gtkcontainerprivate.h \
gtkcssarrayvalueprivate.h \
gtkcssbgsizevalueprivate.h \
gtkcsscomputedvaluesprivate.h \
gtkcsscornervalueprivate.h \
gtkcsscustompropertyprivate.h \
@ -636,6 +637,7 @@ gtk_base_c_sources = \
gtkcomboboxtext.c \
gtkcontainer.c \
gtkcssarrayvalue.c \
gtkcssbgsizevalue.c \
gtkcsscomputedvalues.c \
gtkcsscornervalue.c \
gtkcsscustomproperty.c \

258
gtk/gtkcssbgsizevalue.c Normal file
View File

@ -0,0 +1,258 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2011 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkcssbgsizevalueprivate.h"
#include "gtkcssnumbervalueprivate.h"
struct _GtkCssValue {
GTK_CSS_VALUE_BASE
guint cover :1;
guint contain :1;
GtkCssValue *x;
GtkCssValue *y;
};
static void
gtk_css_value_bg_size_free (GtkCssValue *value)
{
if (value->x)
_gtk_css_value_unref (value->x);
if (value->y)
_gtk_css_value_unref (value->y);
g_slice_free (GtkCssValue, value);
}
static gboolean
gtk_css_value_bg_size_equal (const GtkCssValue *value1,
const GtkCssValue *value2)
{
return value1->cover == value2->cover &&
value2->contain == value2->contain &&
(value1->x == value2->x ||
(value1->x != NULL && value2->x != NULL &&
_gtk_css_value_equal (value1->x, value2->x))) &&
(value1->y == value2->y ||
(value1->y != NULL && value2->y != NULL &&
_gtk_css_value_equal (value1->y, value2->y)));
}
static GtkCssValue *
gtk_css_value_bg_size_transition (GtkCssValue *start,
GtkCssValue *end,
double progress)
{
GtkCssValue *x, *y;
if (start->cover)
return end->cover ? _gtk_css_value_ref (end) : NULL;
if (start->contain)
return end->contain ? _gtk_css_value_ref (end) : NULL;
if ((start->x != NULL) ^ (end->x != NULL) ||
(start->y != NULL) ^ (end->y != NULL))
return NULL;
if (start->x)
{
x = _gtk_css_value_transition (start->x, end->x, progress);
if (x == NULL)
return NULL;
}
else
x = NULL;
if (start->y)
{
y = _gtk_css_value_transition (start->y, end->y, progress);
if (y == NULL)
{
_gtk_css_value_unref (x);
return NULL;
}
}
else
y = NULL;
return _gtk_css_bg_size_value_new (x, y);
}
static void
gtk_css_value_bg_size_print (const GtkCssValue *value,
GString *string)
{
if (value->cover)
g_string_append (string, "cover");
else if (value->contain)
g_string_append (string, "contain");
else
{
if (value->x == NULL)
g_string_append (string, "auto");
else
_gtk_css_value_print (value->x, string);
if (value->y)
{
g_string_append_c (string, ' ');
_gtk_css_value_print (value->y, string);
}
}
}
static const GtkCssValueClass GTK_CSS_VALUE_BG_SIZE = {
gtk_css_value_bg_size_free,
gtk_css_value_bg_size_equal,
gtk_css_value_bg_size_transition,
gtk_css_value_bg_size_print
};
static GtkCssValue auto_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, FALSE, NULL, NULL };
static GtkCssValue cover_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, TRUE, FALSE, NULL, NULL };
static GtkCssValue contain_singleton = { &GTK_CSS_VALUE_BG_SIZE, 1, FALSE, TRUE, NULL, NULL };
GtkCssValue *
_gtk_css_bg_size_value_new (GtkCssValue *x,
GtkCssValue *y)
{
GtkCssValue *result;
if (x == NULL && y == NULL)
return _gtk_css_value_ref (&auto_singleton);
result = _gtk_css_value_new (GtkCssValue, &GTK_CSS_VALUE_BG_SIZE);
result->x = x;
result->y = y;
return result;
}
GtkCssValue *
_gtk_css_bg_size_value_parse (GtkCssParser *parser)
{
GtkCssValue *x, *y;
if (_gtk_css_parser_try (parser, "cover", TRUE))
return _gtk_css_value_ref (&cover_singleton);
else if (_gtk_css_parser_try (parser, "contain", TRUE))
return _gtk_css_value_ref (&contain_singleton);
if (_gtk_css_parser_try (parser, "auto", TRUE))
x = NULL;
else
{
x = _gtk_css_number_value_parse (parser,
GTK_CSS_POSITIVE_ONLY
| GTK_CSS_PARSE_PERCENT
| GTK_CSS_PARSE_LENGTH);
if (x == NULL)
return NULL;
}
if (_gtk_css_parser_try (parser, "auto", TRUE))
y = NULL;
else if (!_gtk_css_parser_has_number (parser))
y = NULL;
else
{
y = _gtk_css_number_value_parse (parser,
GTK_CSS_POSITIVE_ONLY
| GTK_CSS_PARSE_PERCENT
| GTK_CSS_PARSE_LENGTH);
if (y == NULL)
{
_gtk_css_value_unref (x);
return NULL;
}
}
return _gtk_css_bg_size_value_new (x, y);
}
static void
gtk_css_bg_size_compute_size_for_cover_contain (gboolean cover,
GtkCssImage *image,
double width,
double height,
double *concrete_width,
double *concrete_height)
{
double aspect, image_aspect;
image_aspect = _gtk_css_image_get_aspect_ratio (image);
if (image_aspect == 0.0)
{
*concrete_width = width;
*concrete_height = height;
return;
}
aspect = width / height;
if ((aspect >= image_aspect && cover) ||
(aspect < image_aspect && !cover))
{
*concrete_width = width;
*concrete_height = width / image_aspect;
}
else
{
*concrete_height = height;
*concrete_width = height * image_aspect;
}
}
void
_gtk_css_bg_size_value_compute_size (const GtkCssValue *value,
GtkCssImage *image,
double area_width,
double area_height,
double *out_width,
double *out_height)
{
g_return_if_fail (value->class == &GTK_CSS_VALUE_BG_SIZE);
if (value->contain || value->cover)
gtk_css_bg_size_compute_size_for_cover_contain (value->cover,
image,
area_width, area_height,
out_width, out_height);
else
_gtk_css_image_get_concrete_size (image,
/* note: 0 does the right thing here for 'auto' */
value->x ? _gtk_css_number_value_get (value->x, area_width) : 0,
value->y ? _gtk_css_number_value_get (value->y, area_height) : 0,
area_width, area_height,
out_width, out_height);
}
GtkCssValue *
_gtk_css_bg_size_value_compute (GtkCssValue *value,
GtkStyleContext *context)
{
g_return_val_if_fail (value->class == &GTK_CSS_VALUE_BG_SIZE, NULL);
if (value->x == NULL && value->y == NULL)
return _gtk_css_value_ref (value);
return _gtk_css_bg_size_value_new (value->x ? _gtk_css_number_value_compute (value->x, context) : NULL,
value->y ? _gtk_css_number_value_compute (value->y, context) : NULL);
}

View File

@ -0,0 +1,45 @@
/*
* Copyright © 2012 Red Hat Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Alexander Larsson <alexl@gnome.org>
*/
#ifndef __GTK_CSS_BG_SIZE_VALUE_PRIVATE_H__
#define __GTK_CSS_BG_SIZE_VALUE_PRIVATE_H__
#include "gtkcssparserprivate.h"
#include "gtkcssimageprivate.h"
#include "gtkcssvalueprivate.h"
G_BEGIN_DECLS
GtkCssValue * _gtk_css_bg_size_value_new (GtkCssValue *x,
GtkCssValue *y);
GtkCssValue * _gtk_css_bg_size_value_parse (GtkCssParser *parser);
void _gtk_css_bg_size_value_compute_size (const GtkCssValue *bg_size,
GtkCssImage *image,
double area_width,
double area_height,
double *out_width,
double *out_height);
GtkCssValue * _gtk_css_bg_size_value_compute (GtkCssValue *bg_size,
GtkStyleContext *context);
G_END_DECLS
#endif /* __GTK_CSS_BG_SIZE_VALUE_PRIVATE_H__ */

View File

@ -41,6 +41,7 @@
#include "gtkanimationdescription.h"
#include "gtkbindings.h"
#include "gtkcssarrayvalueprivate.h"
#include "gtkcssbgsizevalueprivate.h"
#include "gtkcsscornervalueprivate.h"
#include "gtkcsseasevalueprivate.h"
#include "gtkcssenginevalueprivate.h"
@ -898,65 +899,7 @@ background_size_parse (GtkCssStyleProperty *property,
GtkCssParser *parser,
GFile *base)
{
GtkCssBackgroundSize size = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), FALSE, FALSE};
if (_gtk_css_parser_try (parser, "cover", TRUE))
size.cover = TRUE;
else if (_gtk_css_parser_try (parser, "contain", TRUE))
size.contain = TRUE;
else
{
if (_gtk_css_parser_try (parser, "auto", TRUE))
_gtk_css_number_init (&size.width, 0, GTK_CSS_PX);
else if (!_gtk_css_parser_read_number (parser,
&size.width,
GTK_CSS_POSITIVE_ONLY
| GTK_CSS_PARSE_PERCENT
| GTK_CSS_PARSE_LENGTH))
return NULL;
if (_gtk_css_parser_try (parser, "auto", TRUE))
_gtk_css_number_init (&size.height, 0, GTK_CSS_PX);
else if (_gtk_css_parser_has_number (parser))
{
if (!_gtk_css_parser_read_number (parser,
&size.height,
GTK_CSS_POSITIVE_ONLY
| GTK_CSS_PARSE_PERCENT
| GTK_CSS_PARSE_LENGTH))
return NULL;
}
else
_gtk_css_number_init (&size.height, 0, GTK_CSS_PX);
}
return _gtk_css_value_new_from_background_size (&size);
}
static void
background_size_print (GtkCssStyleProperty *property,
const GtkCssValue *value,
GString *string)
{
const GtkCssBackgroundSize *size = _gtk_css_value_get_background_size (value);
if (size->cover)
g_string_append (string, "cover");
else if (size->contain)
g_string_append (string, "contain");
else
{
if (size->width.value == 0)
g_string_append (string, "auto");
else
_gtk_css_number_print (&size->width, string);
if (size->height.value != 0)
{
g_string_append (string, " ");
_gtk_css_number_print (&size->height, string);
}
}
return _gtk_css_bg_size_value_parse (parser);
}
static GtkCssValue *
@ -964,21 +907,7 @@ background_size_compute (GtkCssStyleProperty *property,
GtkStyleContext *context,
GtkCssValue *specified)
{
const GtkCssBackgroundSize *ssize = _gtk_css_value_get_background_size (specified);
GtkCssBackgroundSize csize;
gboolean changed;
csize.cover = ssize->cover;
csize.contain = ssize->contain;
changed = _gtk_css_number_compute (&csize.width,
&ssize->width,
context);
changed |= _gtk_css_number_compute (&csize.height,
&ssize->height,
context);
if (changed)
return _gtk_css_value_new_from_background_size (&csize);
return _gtk_css_value_ref (specified);
return _gtk_css_bg_size_value_compute (specified, context);
}
static GtkCssValue *
@ -1013,7 +942,6 @@ gtk_symbolic_color_new_rgba (double red,
void
_gtk_css_style_property_init_properties (void)
{
GtkCssBackgroundSize default_background_size = { GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), GTK_CSS_NUMBER_INIT (0, GTK_CSS_PX), FALSE, FALSE };
GtkBorder border_of_ones = { 1, 1, 1, 1 };
/* Initialize "color" and "font-size" first,
@ -1431,12 +1359,12 @@ _gtk_css_style_property_init_properties (void)
G_TYPE_NONE,
0,
background_size_parse,
background_size_print,
NULL,
background_size_compute,
NULL,
NULL,
NULL,
_gtk_css_value_new_from_background_size (&default_background_size));
_gtk_css_bg_size_value_new (NULL, NULL));
gtk_css_style_property_register ("background-position",
GTK_CSS_PROPERTY_BACKGROUND_POSITION,
G_TYPE_NONE,

View File

@ -22,18 +22,6 @@
#include "gtkcssnumbervalueprivate.h"
#include "gtkstylecontextprivate.h"
#define DEFINE_BOXED_TYPE_WITH_COPY_FUNC(TypeName, type_name) \
\
static TypeName * \
type_name ## _copy (const TypeName *foo) \
{ \
return g_memdup (foo, sizeof (TypeName)); \
} \
\
G_DEFINE_BOXED_TYPE (TypeName, type_name, type_name ## _copy, g_free)
DEFINE_BOXED_TYPE_WITH_COPY_FUNC (GtkCssBackgroundSize, _gtk_css_background_size)
typedef struct _GtkCssChangeTranslation GtkCssChangeTranslation;
struct _GtkCssChangeTranslation {
GtkCssChange from;

View File

@ -160,24 +160,12 @@ typedef enum /*< skip >*/ {
} GtkCssUnit;
typedef struct _GtkCssNumber GtkCssNumber;
typedef struct _GtkCssBackgroundSize GtkCssBackgroundSize;
struct _GtkCssNumber {
gdouble value;
GtkCssUnit unit;
};
struct _GtkCssBackgroundSize {
GtkCssNumber width; /* 0 means auto here */
GtkCssNumber height; /* 0 means auto here */
guint cover :1;
guint contain :1;
};
#define GTK_TYPE_CSS_BACKGROUND_SIZE _gtk_css_background_size_get_type ()
GType _gtk_css_background_size_get_type (void);
GtkCssChange _gtk_css_change_for_sibling (GtkCssChange match);
GtkCssChange _gtk_css_change_for_child (GtkCssChange match);

View File

@ -278,17 +278,6 @@ _gtk_css_value_new_from_color (const GdkColor *v)
return value;
}
GtkCssValue *
_gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v)
{
GtkCssValue *value;
value = gtk_css_value_new (GTK_TYPE_CSS_BACKGROUND_SIZE);
value->u.ptr = g_boxed_copy0 (GTK_TYPE_CSS_BACKGROUND_SIZE, v);
return value;
}
GtkCssValue *
_gtk_css_value_new_from_border_style (GtkBorderStyle style)
{
@ -514,13 +503,6 @@ _gtk_css_value_get_border_style (const GtkCssValue *value)
return value->u.gint;
}
const GtkCssBackgroundSize *
_gtk_css_value_get_background_size (const GtkCssValue *value)
{
g_return_val_if_fail (_gtk_css_value_holds (value, GTK_TYPE_CSS_BACKGROUND_SIZE), NULL);
return value->u.ptr;
}
GtkGradient *
_gtk_css_value_get_gradient (const GtkCssValue *value)
{

View File

@ -88,7 +88,6 @@ GtkCssValue *_gtk_css_value_new_from_color (const GdkColor
GtkCssValue *_gtk_css_value_new_take_symbolic_color (GtkSymbolicColor *v);
GtkCssValue *_gtk_css_value_new_take_pattern (cairo_pattern_t *v);
GtkCssValue *_gtk_css_value_new_take_binding_sets (GPtrArray *array);
GtkCssValue *_gtk_css_value_new_from_background_size (const GtkCssBackgroundSize *v);
void _gtk_css_value_init_gvalue (const GtkCssValue *value,
GValue *g_value);
@ -99,7 +98,6 @@ gpointer _gtk_css_value_get_object (const
gpointer _gtk_css_value_get_boxed (const GtkCssValue *value);
const char ** _gtk_css_value_get_strv (const GtkCssValue *value);
GtkSymbolicColor *_gtk_css_value_get_symbolic_color (const GtkCssValue *value);
const GtkCssBackgroundSize *_gtk_css_value_get_background_size (const GtkCssValue *value);
GtkGradient *_gtk_css_value_get_gradient (const GtkCssValue *value);
G_END_DECLS

View File

@ -24,6 +24,7 @@
#include "gtkthemingbackgroundprivate.h"
#include "gtkcssarrayvalueprivate.h"
#include "gtkcssbgsizevalueprivate.h"
#include "gtkcssenumvalueprivate.h"
#include "gtkcssimagevalueprivate.h"
#include "gtkcssshadowsvalueprivate.h"
@ -112,39 +113,6 @@ _gtk_theming_background_apply_clip (GtkThemingBackground *bg)
}
}
static void
_gtk_theming_background_get_cover_contain (GtkCssImage *image,
gboolean cover,
double width,
double height,
double *concrete_width,
double *concrete_height)
{
double aspect, image_aspect;
image_aspect = _gtk_css_image_get_aspect_ratio (image);
if (image_aspect == 0.0)
{
*concrete_width = width;
*concrete_height = height;
return;
}
aspect = width / height;
if ((aspect >= image_aspect && cover) ||
(aspect < image_aspect && !cover))
{
*concrete_width = width;
*concrete_height = width / image_aspect;
}
else
{
*concrete_height = height;
*concrete_width = height * image_aspect;
}
}
static void
_gtk_theming_background_paint (GtkThemingBackground *bg,
cairo_t *cr)
@ -161,13 +129,11 @@ _gtk_theming_background_paint (GtkThemingBackground *bg,
&& bg->image_rect.width > 0
&& bg->image_rect.height > 0)
{
const GtkCssBackgroundSize *size;
const GtkCssValue *pos, *repeat;
double image_width, image_height;
double width, height;
GtkCssRepeatStyle hrepeat, vrepeat;
size = _gtk_css_value_get_background_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE));
pos = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_POSITION);
repeat = _gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_REPEAT);
hrepeat = _gtk_css_background_repeat_value_get_x (repeat);
@ -175,20 +141,12 @@ _gtk_theming_background_paint (GtkThemingBackground *bg,
width = bg->image_rect.width;
height = bg->image_rect.height;
if (size->contain || size->cover)
_gtk_theming_background_get_cover_contain (bg->image,
size->cover,
width,
height,
&image_width,
&image_height);
else
_gtk_css_image_get_concrete_size (bg->image,
/* note: 0 does the right thing here for 'auto' */
_gtk_css_number_get (&size->width, width),
_gtk_css_number_get (&size->height, height),
width, height,
&image_width, &image_height);
_gtk_css_bg_size_value_compute_size (_gtk_style_context_peek_property (bg->context, GTK_CSS_PROPERTY_BACKGROUND_SIZE),
bg->image,
width,
height,
&image_width,
&image_height);
/* optimization */
if (image_width == width)