[gtk+/wip/cssvalue: 125/165] cssparser: Move symbolic color parser to gtksymboliccolor.c
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/wip/cssvalue: 125/165] cssparser: Move symbolic color parser to gtksymboliccolor.c
- Date: Fri, 13 Apr 2012 23:13:17 +0000 (UTC)
commit ee195b8b66487062b77bee8ac37a55d868712e18
Author: Benjamin Otte <otte redhat com>
Date: Thu Apr 5 21:09:35 2012 +0200
cssparser: Move symbolic color parser to gtksymboliccolor.c
gtk/gtkcssimagegradient.c | 1 +
gtk/gtkcssimagelinear.c | 2 +-
gtk/gtkcssparser.c | 264 ++---------------------------------------
gtk/gtkcssparserprivate.h | 5 +-
gtk/gtksymboliccolor.c | 235 ++++++++++++++++++++++++++++++++++++
gtk/gtksymboliccolorprivate.h | 3 +
gtk/gtkwin32theme.c | 2 +
7 files changed, 257 insertions(+), 255 deletions(-)
---
diff --git a/gtk/gtkcssimagegradient.c b/gtk/gtkcssimagegradient.c
index 719edc0..288ca0e 100644
--- a/gtk/gtkcssimagegradient.c
+++ b/gtk/gtkcssimagegradient.c
@@ -22,6 +22,7 @@
#include "gtkcssimagegradientprivate.h"
#include "gtkcssprovider.h"
+#include "gtksymboliccolorprivate.h"
G_DEFINE_TYPE (GtkCssImageGradient, _gtk_css_image_gradient, GTK_TYPE_CSS_IMAGE)
diff --git a/gtk/gtkcssimagelinear.c b/gtk/gtkcssimagelinear.c
index 4354055..6cc88e4 100644
--- a/gtk/gtkcssimagelinear.c
+++ b/gtk/gtkcssimagelinear.c
@@ -26,7 +26,7 @@
#include "gtkcssnumbervalueprivate.h"
#include "gtkcssrgbavalueprivate.h"
#include "gtkcssprovider.h"
-#include "gtkstylecontextprivate.h"
+#include "gtksymboliccolorprivate.h"
G_DEFINE_TYPE (GtkCssImageLinear, _gtk_css_image_linear, GTK_TYPE_CSS_IMAGE)
diff --git a/gtk/gtkcssparser.c b/gtk/gtkcssparser.c
index 96c367e..62d9113 100644
--- a/gtk/gtkcssparser.c
+++ b/gtk/gtkcssparser.c
@@ -736,278 +736,40 @@ _gtk_css_parser_try_enum (GtkCssParser *parser,
return result;
}
-typedef enum {
- COLOR_RGBA,
- COLOR_RGB,
- COLOR_LIGHTER,
- COLOR_DARKER,
- COLOR_SHADE,
- COLOR_ALPHA,
- COLOR_MIX,
- COLOR_WIN32
-} ColorType;
-
-static GtkSymbolicColor *
-gtk_css_parser_read_symbolic_color_function (GtkCssParser *parser,
- ColorType color)
-{
- GtkSymbolicColor *symbolic;
- GtkSymbolicColor *child1, *child2;
- double value;
-
- if (!_gtk_css_parser_try (parser, "(", TRUE))
- {
- _gtk_css_parser_error (parser, "Missing opening bracket in color definition");
- return NULL;
- }
-
- if (color == COLOR_RGB || color == COLOR_RGBA)
- {
- GdkRGBA rgba;
- double tmp;
- guint i;
-
- for (i = 0; i < 3; i++)
- {
- if (i > 0 && !_gtk_css_parser_try (parser, ",", TRUE))
- {
- _gtk_css_parser_error (parser, "Expected ',' in color definition");
- return NULL;
- }
-
- if (!_gtk_css_parser_try_double (parser, &tmp))
- {
- _gtk_css_parser_error (parser, "Invalid number for color value");
- return NULL;
- }
- if (_gtk_css_parser_try (parser, "%", TRUE))
- tmp /= 100.0;
- else
- tmp /= 255.0;
- if (i == 0)
- rgba.red = tmp;
- else if (i == 1)
- rgba.green = tmp;
- else if (i == 2)
- rgba.blue = tmp;
- else
- g_assert_not_reached ();
- }
-
- if (color == COLOR_RGBA)
- {
- if (i > 0 && !_gtk_css_parser_try (parser, ",", TRUE))
- {
- _gtk_css_parser_error (parser, "Expected ',' in color definition");
- return NULL;
- }
-
- if (!_gtk_css_parser_try_double (parser, &rgba.alpha))
- {
- _gtk_css_parser_error (parser, "Invalid number for alpha value");
- return NULL;
- }
- }
- else
- rgba.alpha = 1.0;
-
- symbolic = gtk_symbolic_color_new_literal (&rgba);
- }
- else if (color == COLOR_WIN32)
- {
- symbolic = _gtk_win32_theme_color_parse (parser);
- if (symbolic == NULL)
- return NULL;
- }
- else
- {
- child1 = _gtk_css_parser_read_symbolic_color (parser);
- if (child1 == NULL)
- return NULL;
-
- if (color == COLOR_MIX)
- {
- if (!_gtk_css_parser_try (parser, ",", TRUE))
- {
- _gtk_css_parser_error (parser, "Expected ',' in color definition");
- gtk_symbolic_color_unref (child1);
- return NULL;
- }
-
- child2 = _gtk_css_parser_read_symbolic_color (parser);
- if (child2 == NULL)
- {
- gtk_symbolic_color_unref (child1);
- return NULL;
- }
- }
- else
- child2 = NULL;
-
- if (color == COLOR_LIGHTER)
- value = 1.3;
- else if (color == COLOR_DARKER)
- value = 0.7;
- else
- {
- if (!_gtk_css_parser_try (parser, ",", TRUE))
- {
- _gtk_css_parser_error (parser, "Expected ',' in color definition");
- gtk_symbolic_color_unref (child1);
- if (child2)
- gtk_symbolic_color_unref (child2);
- return NULL;
- }
-
- if (!_gtk_css_parser_try_double (parser, &value))
- {
- _gtk_css_parser_error (parser, "Expected number in color definition");
- gtk_symbolic_color_unref (child1);
- if (child2)
- gtk_symbolic_color_unref (child2);
- return NULL;
- }
- }
-
- switch (color)
- {
- case COLOR_LIGHTER:
- case COLOR_DARKER:
- case COLOR_SHADE:
- symbolic = gtk_symbolic_color_new_shade (child1, value);
- break;
- case COLOR_ALPHA:
- symbolic = gtk_symbolic_color_new_alpha (child1, value);
- break;
- case COLOR_MIX:
- symbolic = gtk_symbolic_color_new_mix (child1, child2, value);
- break;
- default:
- g_assert_not_reached ();
- symbolic = NULL;
- }
-
- gtk_symbolic_color_unref (child1);
- if (child2)
- gtk_symbolic_color_unref (child2);
- }
-
- if (!_gtk_css_parser_try (parser, ")", TRUE))
- {
- _gtk_css_parser_error (parser, "Expected ')' in color definition");
- gtk_symbolic_color_unref (symbolic);
- return NULL;
- }
-
- return symbolic;
-}
-
-static GtkSymbolicColor *
-gtk_css_parser_try_hash_color (GtkCssParser *parser)
+gboolean
+_gtk_css_parser_try_hash_color (GtkCssParser *parser,
+ GdkRGBA *rgba)
{
if (parser->data[0] == '#' &&
g_ascii_isxdigit (parser->data[1]) &&
g_ascii_isxdigit (parser->data[2]) &&
g_ascii_isxdigit (parser->data[3]))
{
- GdkRGBA rgba;
-
if (g_ascii_isxdigit (parser->data[4]) &&
g_ascii_isxdigit (parser->data[5]) &&
g_ascii_isxdigit (parser->data[6]))
{
- rgba.red = ((get_xdigit (parser->data[1]) << 4) + get_xdigit (parser->data[2])) / 255.0;
- rgba.green = ((get_xdigit (parser->data[3]) << 4) + get_xdigit (parser->data[4])) / 255.0;
- rgba.blue = ((get_xdigit (parser->data[5]) << 4) + get_xdigit (parser->data[6])) / 255.0;
- rgba.alpha = 1.0;
+ rgba->red = ((get_xdigit (parser->data[1]) << 4) + get_xdigit (parser->data[2])) / 255.0;
+ rgba->green = ((get_xdigit (parser->data[3]) << 4) + get_xdigit (parser->data[4])) / 255.0;
+ rgba->blue = ((get_xdigit (parser->data[5]) << 4) + get_xdigit (parser->data[6])) / 255.0;
+ rgba->alpha = 1.0;
parser->data += 7;
}
else
{
- rgba.red = get_xdigit (parser->data[1]) / 15.0;
- rgba.green = get_xdigit (parser->data[2]) / 15.0;
- rgba.blue = get_xdigit (parser->data[3]) / 15.0;
- rgba.alpha = 1.0;
+ rgba->red = get_xdigit (parser->data[1]) / 15.0;
+ rgba->green = get_xdigit (parser->data[2]) / 15.0;
+ rgba->blue = get_xdigit (parser->data[3]) / 15.0;
+ rgba->alpha = 1.0;
parser->data += 4;
}
_gtk_css_parser_skip_whitespace (parser);
- return gtk_symbolic_color_new_literal (&rgba);
- }
-
- return NULL;
-}
-
-GtkSymbolicColor *
-_gtk_css_parser_read_symbolic_color (GtkCssParser *parser)
-{
- GtkSymbolicColor *symbolic;
- guint color;
- const char *names[] = {"rgba", "rgb", "lighter", "darker", "shade", "alpha", "mix",
- GTK_WIN32_THEME_SYMBOLIC_COLOR_NAME};
- char *name;
-
- g_return_val_if_fail (GTK_IS_CSS_PARSER (parser), NULL);
-
- if (_gtk_css_parser_try (parser, "transparent", TRUE))
- {
- GdkRGBA transparent = { 0, 0, 0, 0 };
-
- return gtk_symbolic_color_new_literal (&transparent);
- }
-
- if (_gtk_css_parser_try (parser, "@", FALSE))
- {
- name = _gtk_css_parser_try_name (parser, TRUE);
-
- if (name)
- {
- symbolic = gtk_symbolic_color_new_name (name);
- }
- else
- {
- _gtk_css_parser_error (parser, "'%s' is not a valid symbolic color name", name);
- symbolic = NULL;
- }
-
- g_free (name);
- return symbolic;
- }
-
- for (color = 0; color < G_N_ELEMENTS (names); color++)
- {
- if (_gtk_css_parser_try (parser, names[color], TRUE))
- break;
- }
-
- if (color < G_N_ELEMENTS (names))
- return gtk_css_parser_read_symbolic_color_function (parser, color);
-
- symbolic = gtk_css_parser_try_hash_color (parser);
- if (symbolic)
- return symbolic;
-
- name = _gtk_css_parser_try_name (parser, TRUE);
- if (name)
- {
- GdkRGBA rgba;
-
- if (gdk_rgba_parse (&rgba, name))
- {
- symbolic = gtk_symbolic_color_new_literal (&rgba);
- }
- else
- {
- _gtk_css_parser_error (parser, "'%s' is not a valid color name", name);
- symbolic = NULL;
- }
- g_free (name);
- return symbolic;
+ return TRUE;
}
- _gtk_css_parser_error (parser, "Not a color definition");
- return NULL;
+ return FALSE;
}
GFile *
diff --git a/gtk/gtkcssparserprivate.h b/gtk/gtkcssparserprivate.h
index d676c28..2c83589 100644
--- a/gtk/gtkcssparserprivate.h
+++ b/gtk/gtkcssparserprivate.h
@@ -20,7 +20,6 @@
#include "gtk/gtkcsstypesprivate.h"
#include <gtk/gtkcssprovider.h>
-#include <gtk/gtksymboliccolor.h>
G_BEGIN_DECLS
@@ -81,12 +80,12 @@ gboolean _gtk_css_parser_try_length (GtkCssParser *parser
gboolean _gtk_css_parser_try_enum (GtkCssParser *parser,
GType enum_type,
int *value);
+gboolean _gtk_css_parser_try_hash_color (GtkCssParser *parser,
+ GdkRGBA *rgba);
gboolean _gtk_css_parser_has_number (GtkCssParser *parser);
char * _gtk_css_parser_read_string (GtkCssParser *parser);
char * _gtk_css_parser_read_value (GtkCssParser *parser);
-GtkSymbolicColor *_gtk_css_parser_read_symbolic_color
- (GtkCssParser *parser);
GFile * _gtk_css_parser_read_url (GtkCssParser *parser,
GFile *base);
diff --git a/gtk/gtksymboliccolor.c b/gtk/gtksymboliccolor.c
index 6201b41..fdbcce7 100644
--- a/gtk/gtksymboliccolor.c
+++ b/gtk/gtksymboliccolor.c
@@ -850,5 +850,240 @@ gtk_symbolic_color_to_string (GtkSymbolicColor *color)
}
return s;
+}
+
+typedef enum {
+ COLOR_RGBA,
+ COLOR_RGB,
+ COLOR_LIGHTER,
+ COLOR_DARKER,
+ COLOR_SHADE,
+ COLOR_ALPHA,
+ COLOR_MIX,
+ COLOR_WIN32
+} ColorParseType;
+
+static GtkSymbolicColor *
+gtk_css_parser_read_symbolic_color_function (GtkCssParser *parser,
+ ColorParseType color)
+{
+ GtkSymbolicColor *symbolic;
+ GtkSymbolicColor *child1, *child2;
+ double value;
+
+ if (!_gtk_css_parser_try (parser, "(", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Missing opening bracket in color definition");
+ return NULL;
+ }
+
+ if (color == COLOR_RGB || color == COLOR_RGBA)
+ {
+ GdkRGBA rgba;
+ double tmp;
+ guint i;
+
+ for (i = 0; i < 3; i++)
+ {
+ if (i > 0 && !_gtk_css_parser_try (parser, ",", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Expected ',' in color definition");
+ return NULL;
+ }
+
+ if (!_gtk_css_parser_try_double (parser, &tmp))
+ {
+ _gtk_css_parser_error (parser, "Invalid number for color value");
+ return NULL;
+ }
+ if (_gtk_css_parser_try (parser, "%", TRUE))
+ tmp /= 100.0;
+ else
+ tmp /= 255.0;
+ if (i == 0)
+ rgba.red = tmp;
+ else if (i == 1)
+ rgba.green = tmp;
+ else if (i == 2)
+ rgba.blue = tmp;
+ else
+ g_assert_not_reached ();
+ }
+
+ if (color == COLOR_RGBA)
+ {
+ if (i > 0 && !_gtk_css_parser_try (parser, ",", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Expected ',' in color definition");
+ return NULL;
+ }
+
+ if (!_gtk_css_parser_try_double (parser, &rgba.alpha))
+ {
+ _gtk_css_parser_error (parser, "Invalid number for alpha value");
+ return NULL;
+ }
+ }
+ else
+ rgba.alpha = 1.0;
+
+ symbolic = gtk_symbolic_color_new_literal (&rgba);
+ }
+ else if (color == COLOR_WIN32)
+ {
+ symbolic = _gtk_win32_theme_color_parse (parser);
+ if (symbolic == NULL)
+ return NULL;
+ }
+ else
+ {
+ child1 = _gtk_css_parser_read_symbolic_color (parser);
+ if (child1 == NULL)
+ return NULL;
+
+ if (color == COLOR_MIX)
+ {
+ if (!_gtk_css_parser_try (parser, ",", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Expected ',' in color definition");
+ gtk_symbolic_color_unref (child1);
+ return NULL;
+ }
+
+ child2 = _gtk_css_parser_read_symbolic_color (parser);
+ if (child2 == NULL)
+ {
+ gtk_symbolic_color_unref (child1);
+ return NULL;
+ }
+ }
+ else
+ child2 = NULL;
+
+ if (color == COLOR_LIGHTER)
+ value = 1.3;
+ else if (color == COLOR_DARKER)
+ value = 0.7;
+ else
+ {
+ if (!_gtk_css_parser_try (parser, ",", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Expected ',' in color definition");
+ gtk_symbolic_color_unref (child1);
+ if (child2)
+ gtk_symbolic_color_unref (child2);
+ return NULL;
+ }
+
+ if (!_gtk_css_parser_try_double (parser, &value))
+ {
+ _gtk_css_parser_error (parser, "Expected number in color definition");
+ gtk_symbolic_color_unref (child1);
+ if (child2)
+ gtk_symbolic_color_unref (child2);
+ return NULL;
+ }
+ }
+
+ switch (color)
+ {
+ case COLOR_LIGHTER:
+ case COLOR_DARKER:
+ case COLOR_SHADE:
+ symbolic = gtk_symbolic_color_new_shade (child1, value);
+ break;
+ case COLOR_ALPHA:
+ symbolic = gtk_symbolic_color_new_alpha (child1, value);
+ break;
+ case COLOR_MIX:
+ symbolic = gtk_symbolic_color_new_mix (child1, child2, value);
+ break;
+ default:
+ g_assert_not_reached ();
+ symbolic = NULL;
+ }
+
+ gtk_symbolic_color_unref (child1);
+ if (child2)
+ gtk_symbolic_color_unref (child2);
+ }
+
+ if (!_gtk_css_parser_try (parser, ")", TRUE))
+ {
+ _gtk_css_parser_error (parser, "Expected ')' in color definition");
+ gtk_symbolic_color_unref (symbolic);
+ return NULL;
+ }
+ return symbolic;
}
+
+GtkSymbolicColor *
+_gtk_css_parser_read_symbolic_color (GtkCssParser *parser)
+{
+ GtkSymbolicColor *symbolic;
+ GdkRGBA rgba;
+ guint color;
+ const char *names[] = {"rgba", "rgb", "lighter", "darker", "shade", "alpha", "mix",
+ GTK_WIN32_THEME_SYMBOLIC_COLOR_NAME};
+ char *name;
+
+ g_return_val_if_fail (parser != NULL, NULL);
+
+ if (_gtk_css_parser_try (parser, "transparent", TRUE))
+ {
+ GdkRGBA transparent = { 0, 0, 0, 0 };
+
+ return gtk_symbolic_color_new_literal (&transparent);
+ }
+
+ if (_gtk_css_parser_try (parser, "@", FALSE))
+ {
+ name = _gtk_css_parser_try_name (parser, TRUE);
+
+ if (name)
+ {
+ symbolic = gtk_symbolic_color_new_name (name);
+ }
+ else
+ {
+ _gtk_css_parser_error (parser, "'%s' is not a valid symbolic color name", name);
+ symbolic = NULL;
+ }
+
+ g_free (name);
+ return symbolic;
+ }
+
+ for (color = 0; color < G_N_ELEMENTS (names); color++)
+ {
+ if (_gtk_css_parser_try (parser, names[color], TRUE))
+ break;
+ }
+
+ if (color < G_N_ELEMENTS (names))
+ return gtk_css_parser_read_symbolic_color_function (parser, color);
+
+ if (_gtk_css_parser_try_hash_color (parser, &rgba))
+ return gtk_symbolic_color_new_literal (&rgba);
+
+ name = _gtk_css_parser_try_name (parser, TRUE);
+ if (name)
+ {
+ if (gdk_rgba_parse (&rgba, name))
+ {
+ symbolic = gtk_symbolic_color_new_literal (&rgba);
+ }
+ else
+ {
+ _gtk_css_parser_error (parser, "'%s' is not a valid color name", name);
+ symbolic = NULL;
+ }
+ g_free (name);
+ return symbolic;
+ }
+
+ _gtk_css_parser_error (parser, "Not a color definition");
+ return NULL;
+}
+
diff --git a/gtk/gtksymboliccolorprivate.h b/gtk/gtksymboliccolorprivate.h
index 869af69..2e9514c 100644
--- a/gtk/gtksymboliccolorprivate.h
+++ b/gtk/gtksymboliccolorprivate.h
@@ -19,6 +19,7 @@
#define __GTK_SYMBOLIC_COLOR_PRIVATE_H__
#include "gtk/gtksymboliccolor.h"
+#include "gtk/gtkcssparserprivate.h"
#include "gtk/gtkcssvalueprivate.h"
G_BEGIN_DECLS
@@ -31,6 +32,8 @@ GtkCssValue * _gtk_symbolic_color_resolve_full (GtkSymbolicColor
GtkSymbolicColor * _gtk_symbolic_color_get_current_color (void);
+GtkSymbolicColor * _gtk_css_parser_read_symbolic_color (GtkCssParser *parser);
+
G_END_DECLS
#endif /* __GTK_SYMBOLIC_COLOR_PRIVATE_H__ */
diff --git a/gtk/gtkwin32theme.c b/gtk/gtkwin32theme.c
index 0f385f9..ebc7e85 100644
--- a/gtk/gtkwin32theme.c
+++ b/gtk/gtkwin32theme.c
@@ -23,6 +23,8 @@
#include "gtkwin32themeprivate.h"
+#include <gtksymboliccolor.h>
+
#ifdef G_OS_WIN32
#include <cairo-win32.h>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]