[gtk+/wip/css: 137/154] cssimage: Add _gtk_css_image_can_parse()



commit f19b26b68a587b064f9d7b12edee174708bb1081
Author: Benjamin Otte <otte redhat com>
Date:   Wed Jan 4 18:42:38 2012 +0100

    cssimage: Add _gtk_css_image_can_parse()
    
    Looks at the first token of the stream to see if this looks like an
    image declaration. If it does: return %TRUE.

 gtk/gtkcssimage.c        |   68 ++++++++++++++++++++++++++++++++-------------
 gtk/gtkcssimageprivate.h |    1 +
 2 files changed, 49 insertions(+), 20 deletions(-)
---
diff --git a/gtk/gtkcssimage.c b/gtk/gtkcssimage.c
index 232c2ae..97be237 100644
--- a/gtk/gtkcssimage.c
+++ b/gtk/gtkcssimage.c
@@ -313,9 +313,8 @@ _gtk_css_image_get_surface (GtkCssImage     *image,
   return result;
 }
 
-GtkCssImage *
-_gtk_css_image_new_parse (GtkCssParser *parser,
-                          GFile        *base)
+GType
+gtk_css_image_get_parser_type (GtkCssParser *parser)
 {
   static const struct {
     const char *prefix;
@@ -327,30 +326,59 @@ _gtk_css_image_new_parse (GtkCssParser *parser,
   };
   guint i;
 
-  g_return_val_if_fail (parser != NULL, NULL);
-  g_return_val_if_fail (G_IS_FILE (base), NULL);
-
   for (i = 0; i < G_N_ELEMENTS (image_types); i++)
     {
       if (_gtk_css_parser_has_prefix (parser, image_types[i].prefix))
-        {
-          GtkCssImage *image;
-          GtkCssImageClass *klass;
+        return image_types[i].type_func ();
+    }
 
-          image = g_object_new (image_types[i].type_func (), NULL);
+  return G_TYPE_INVALID;
+}
 
-          klass = GTK_CSS_IMAGE_GET_CLASS (image);
-          if (!klass->parse (image, parser, base))
-            {
-              g_object_unref (image);
-              return NULL;
-            }
+/**
+ * _gtk_css_image_can_parse:
+ * @parser: a css parser
+ *
+ * Checks if the parser can potentially parse the given stream as an
+ * image from looking at the first token of @parser. This is useful for
+ * implementing shorthand properties. A successful parse of an image
+ * can not be guaranteed.
+ *
+ * Returns: %TURE if it looks like an image.
+ **/
+gboolean
+_gtk_css_image_can_parse (GtkCssParser *parser)
+{
+  return gtk_css_image_get_parser_type (parser) != G_TYPE_INVALID;
+}
 
-          return image;
-        }
+GtkCssImage *
+_gtk_css_image_new_parse (GtkCssParser *parser,
+                          GFile        *base)
+{
+  GtkCssImageClass *klass;
+  GtkCssImage *image;
+  GType image_type;
+
+  g_return_val_if_fail (parser != NULL, NULL);
+  g_return_val_if_fail (G_IS_FILE (base), NULL);
+
+  image_type = gtk_css_image_get_parser_type (parser);
+  if (image_type == G_TYPE_INVALID)
+    {
+      _gtk_css_parser_error (parser, "Not a valid image");
+      return NULL;
+    }
+
+  image = g_object_new (image_type, NULL);
+
+  klass = GTK_CSS_IMAGE_GET_CLASS (image);
+  if (!klass->parse (image, parser, base))
+    {
+      g_object_unref (image);
+      return NULL;
     }
 
-  _gtk_css_parser_error (parser, "Not a valid image");
-  return NULL;
+  return image;
 }
 
diff --git a/gtk/gtkcssimageprivate.h b/gtk/gtkcssimageprivate.h
index 1b0fc98..ebd977e 100644
--- a/gtk/gtkcssimageprivate.h
+++ b/gtk/gtkcssimageprivate.h
@@ -75,6 +75,7 @@ struct _GtkCssImageClass
 
 GType          _gtk_css_image_get_type             (void) G_GNUC_CONST;
 
+gboolean       _gtk_css_image_can_parse            (GtkCssParser       *parser);
 GtkCssImage *  _gtk_css_image_new_parse            (GtkCssParser       *parser,
                                                     GFile              *base);
 



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