css: Add a token aprser for define-color

This commit is contained in:
Benjamin Otte
2016-03-20 04:21:17 +01:00
parent 16dff3909b
commit 33b2684c29
4 changed files with 191 additions and 0 deletions

View File

@@ -398,6 +398,7 @@ gtk_private_h_sources = \
gtkcsscustomgadgetprivate.h \
gtkcsscustompropertyprivate.h \
gtkcssdeclarationprivate.h \
gtkcssdefinecolorruleprivate.h \
gtkcssdimensionvalueprivate.h \
gtkcsseasevalueprivate.h \
gtkcssenginevalueprivate.h \
@@ -676,6 +677,7 @@ gtk_base_c_sources = \
gtkcsscustomgadget.c \
gtkcsscustomproperty.c \
gtkcssdeclaration.c \
gtkcssdefinecolorrule.c \
gtkcssdimensionvalue.c \
gtkcsseasevalue.c \
gtkcssenumvalue.c \

127
gtk/gtkcssdefinecolorrule.c Normal file
View File

@@ -0,0 +1,127 @@
/*
* Copyright © 2016 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: Benjamin Otte <otte@gnome.org>
*/
#include "config.h"
#include "gtkcssdefinecolorruleprivate.h"
#include "gtkcsscolorvalueprivate.h"
#include "gtkcssstylesheetprivate.h"
typedef struct _GtkCssDefineColorRulePrivate GtkCssDefineColorRulePrivate;
struct _GtkCssDefineColorRulePrivate {
char *name;
GtkCssValue *color;
};
G_DEFINE_TYPE_WITH_PRIVATE (GtkCssDefineColorRule, gtk_css_define_color_rule, GTK_TYPE_CSS_RULE)
static void
gtk_css_define_color_rule_finalize (GObject *object)
{
GtkCssDefineColorRule *define_color_rule = GTK_CSS_DEFINE_COLOR_RULE (object);
GtkCssDefineColorRulePrivate *priv = gtk_css_define_color_rule_get_instance_private (define_color_rule);
g_free (priv->name);
if (priv->color)
_gtk_css_value_unref (priv->color);
G_OBJECT_CLASS (gtk_css_define_color_rule_parent_class)->finalize (object);
}
static void
gtk_css_define_color_rule_class_init (GtkCssDefineColorRuleClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = gtk_css_define_color_rule_finalize;
}
static void
gtk_css_define_color_rule_init (GtkCssDefineColorRule *define_color_rule)
{
}
static GtkCssRule *
gtk_css_define_color_rule_new (GtkCssRule *parent_rule,
GtkCssStyleSheet *parent_style_sheet,
const char *name,
GtkCssValue *color)
{
return g_object_new (GTK_TYPE_CSS_DEFINE_COLOR_RULE, NULL);
}
GtkCssRule *
gtk_css_define_color_rule_new_parse (GtkCssTokenSource *source,
GtkCssRule *parent_rule,
GtkCssStyleSheet *parent_style_sheet)
{
const GtkCssToken *token;
GtkCssRule *result;
GtkCssValue *color;
char *name;
g_return_val_if_fail (source != NULL, NULL);
g_return_val_if_fail (parent_rule == NULL || GTK_IS_CSS_RULE (parent_rule), NULL);
g_return_val_if_fail (GTK_IS_CSS_STYLE_SHEET (parent_style_sheet), NULL);
token = gtk_css_token_source_get_token (source);
if (token->type != GTK_CSS_TOKEN_AT_KEYWORD ||
g_ascii_strcasecmp (token->string.string, "define-color") != 0)
{
gtk_css_token_source_error (source, "Expected '@define-color'");
gtk_css_token_source_consume_all (source);
return NULL;
}
gtk_css_token_source_consume_token (source);
token = gtk_css_token_source_get_token (source);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_IDENT))
{
gtk_css_token_source_error (source, "Expected name of color");
gtk_css_token_source_consume_all (source);
return NULL;
}
name = g_strdup (token->string.string);
gtk_css_token_source_consume_token (source);
color = gtk_css_color_value_token_parse (source);
if (color == NULL)
{
g_free (name);
return NULL;
}
token = gtk_css_token_source_get_token (source);
if (!gtk_css_token_is (token, GTK_CSS_TOKEN_SEMICOLON))
{
gtk_css_token_source_error (source, "Expected ';' at end of @define-color");
gtk_css_token_source_consume_all (source);
g_free (name);
_gtk_css_value_unref (color);
return NULL;
}
gtk_css_token_source_consume_token (source);
result = gtk_css_define_color_rule_new (parent_rule, parent_style_sheet, name, color);
g_free (name);
_gtk_css_value_unref (color);
return result;
}

View File

@@ -0,0 +1,57 @@
/*
* Copyright © 2016 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: Benjamin Otte <otte@gnome.org>
*/
#ifndef __GTK_CSS_DEFINE_COLOR_RULE_PRIVATE_H__
#define __GTK_CSS_DEFINE_COLOR_RULE_PRIVATE_H__
#include "gtk/gtkcssruleprivate.h"
#include "gtk/gtkcssvalueprivate.h"
G_BEGIN_DECLS
#define GTK_TYPE_CSS_DEFINE_COLOR_RULE (gtk_css_define_color_rule_get_type ())
#define GTK_CSS_DEFINE_COLOR_RULE(obj) (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_CSS_DEFINE_COLOR_RULE, GtkCssDefineColorRule))
#define GTK_CSS_DEFINE_COLOR_RULE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_CSS_DEFINE_COLOR_RULE, GtkCssDefineColorRuleClass))
#define GTK_IS_CSS_DEFINE_COLOR_RULE(obj) (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_CSS_DEFINE_COLOR_RULE))
#define GTK_IS_CSS_DEFINE_COLOR_RULE_CLASS(obj) (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_CSS_DEFINE_COLOR_RULE))
#define GTK_CSS_DEFINE_COLOR_RULE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CSS_DEFINE_COLOR_RULE, GtkCssDefineColorRuleClass))
typedef struct _GtkCssDefineColorRule GtkCssDefineColorRule;
typedef struct _GtkCssDefineColorRuleClass GtkCssDefineColorRuleClass;
struct _GtkCssDefineColorRule
{
GtkCssRule parent;
};
struct _GtkCssDefineColorRuleClass
{
GtkCssRuleClass parent_class;
};
GType gtk_css_define_color_rule_get_type (void) G_GNUC_CONST;
GtkCssRule * gtk_css_define_color_rule_new_parse (GtkCssTokenSource *source,
GtkCssRule *parent_rule,
GtkCssStyleSheet *parent_style_sheet);
G_END_DECLS
#endif /* __GTK_CSS_DEFINE_COLOR_RULE_PRIVATE_H__ */

View File

@@ -21,6 +21,7 @@
#include "gtkcssruleprivate.h"
#include "gtkcssdefinecolorruleprivate.h"
#include "gtkcssimportruleprivate.h"
#include "gtkcssstylesheetprivate.h"
@@ -181,6 +182,10 @@ gtk_css_rule_new_from_at_rule (GtkCssTokenSource *source,
{
rule = gtk_css_import_rule_new_parse (at_source, parent_rule, parent_style_sheet);
}
else if (g_ascii_strcasecmp (token->string.string, "define-color") == 0)
{
rule = gtk_css_define_color_rule_new_parse (at_source, parent_rule, parent_style_sheet);
}
else
{
gtk_css_token_source_unknown (at_source, "Unknown rule @%s", token->string.string);