[gtk+/wip/css: 110/163] win32: Add a CssImage implementation



commit 86f1dfefeb5cb885ac9a28980493cd7ab16c0d51
Author: Benjamin Otte <otte redhat com>
Date:   Tue Dec 20 14:44:53 2011 +0100

    win32: Add a CssImage implementation

 gtk/Makefile.am               |    2 +
 gtk/gtkcssimage.c             |    4 +-
 gtk/gtkcssimagewin32.c        |  234 +++++++++++++++++++++++++++++++++++++++++
 gtk/gtkcssimagewin32private.h |   65 +++++++++++
 4 files changed, 304 insertions(+), 1 deletions(-)
---
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index 082abd1..313d9af 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -413,6 +413,7 @@ gtk_private_h_sources =		\
 	gtkcssimagegradientprivate.h	\
 	gtkcssimageprivate.h	\
 	gtkcssimageurlprivate.h	\
+	gtkcssimagewin32private.h	\
 	gtkcsslookupprivate.h	\
 	gtkcssparserprivate.h	\
 	gtkcssproviderprivate.h	\
@@ -591,6 +592,7 @@ gtk_base_c_sources = 		\
 	gtkcssimage.c		\
 	gtkcssimagegradient.c	\
 	gtkcssimageurl.c	\
+	gtkcssimagewin32.c	\
 	gtkcsslookup.c		\
 	gtkcssparser.c		\
 	gtkcssprovider.c	\
diff --git a/gtk/gtkcssimage.c b/gtk/gtkcssimage.c
index adc7859..d907e64 100644
--- a/gtk/gtkcssimage.c
+++ b/gtk/gtkcssimage.c
@@ -25,6 +25,7 @@
 /* for the types only */
 #include "gtk/gtkcssimagegradientprivate.h"
 #include "gtk/gtkcssimageurlprivate.h"
+#include "gtk/gtkcssimagewin32private.h"
 
 G_DEFINE_ABSTRACT_TYPE (GtkCssImage, _gtk_css_image, G_TYPE_OBJECT)
 
@@ -168,7 +169,8 @@ _gtk_css_image_new_parse (GtkCssParser *parser,
     GType (* type_func) (void);
   } image_types[] = {
     { "url", _gtk_css_image_url_get_type },
-    { "-gtk-gradient", _gtk_css_image_gradient_get_type }
+    { "-gtk-gradient", _gtk_css_image_gradient_get_type },
+    { "-gtk-win32-theme-part", _gtk_css_image_win32_get_type }
   };
   guint i;
 
diff --git a/gtk/gtkcssimagewin32.c b/gtk/gtkcssimagewin32.c
new file mode 100644
index 0000000..3b89f90
--- /dev/null
+++ b/gtk/gtkcssimagewin32.c
@@ -0,0 +1,234 @@
+/*
+ * Copyright  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.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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#include "config.h"
+
+#include "gtkcssimagewin32private.h"
+
+#include "gtkcssprovider.h"
+
+G_DEFINE_TYPE (GtkCssImageWin32, _gtk_css_image_win32, GTK_TYPE_CSS_IMAGE)
+
+static void
+gtk_css_image_win32_draw (GtkCssImage        *image,
+                          cairo_t            *cr,
+                          double              width,
+                          double              height)
+{
+  GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
+  cairo_surface_t *surface;
+
+  surface = _gtk_win32_theme_part_create_surface (wimage->theme, wimage->part, wimage->state, wimage->margins,
+						  width, height);
+  
+  if (wimage->state2 >= 0)
+    {
+      cairo_surface_t *surface2;
+      cairo_t *cr;
+
+      surface2 = _gtk_win32_theme_part_create_surface (wimage->theme, wimage->part2, wimage->state2, wimage->margins,
+						       width, height);
+
+      cr = cairo_create (surface);
+
+      cairo_set_source_surface (cr, surface2, 0, 0);
+      cairo_paint_with_alpha (cr, wimage->over_alpha);
+      
+      cairo_destroy (cr);
+
+      cairo_surface_destroy (surface2);
+    }
+
+  cairo_set_source_surface (cr, surface, 0, 0);
+  cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_PAD);
+  cairo_rectangle (cr, 0, 0, width, height);
+  cairo_fill (cr);
+
+  cairo_surface_destroy (surface);
+}
+
+static gboolean
+gtk_css_image_win32_parse (GtkCssImage  *image,
+                           GtkCssParser *parser,
+                           GFile        *base)
+{
+  GtkCssImageWin32 *wimage = GTK_CSS_IMAGE_WIN32 (image);
+  char *class;
+
+  if (!_gtk_css_parser_try (parser, "-gtk-win32-theme-part", TRUE))
+    {
+      _gtk_css_parser_error (parser, "'-gtk-win32-theme-part'");
+      return FALSE;
+    }
+  
+  if (!_gtk_css_parser_try (parser, "(", TRUE))
+    {
+      _gtk_css_parser_error (parser,
+                             "Expected '(' after '-gtk-win32-theme-part'");
+      return FALSE;
+    }
+  
+  class = _gtk_css_parser_try_name (parser, TRUE);
+  if (class == NULL)
+    {
+      _gtk_css_parser_error (parser,
+                             "Expected name as first argument to  '-gtk-win32-theme-part'");
+      return FALSE;
+    }
+  wimage->theme = _gtk_win32_lookup_htheme_by_classname (class);
+  g_free (class);
+
+  if (! _gtk_css_parser_try (parser, ",", TRUE))
+    {
+      _gtk_css_parser_error (parser, "Expected ','");
+      return FALSE;
+    }
+
+  if (!_gtk_css_parser_try_int (parser, &wimage->part))
+    {
+      _gtk_css_parser_error (parser, "Expected a valid integer value");
+      return FALSE;
+    }
+
+  if (!_gtk_css_parser_try_int (parser, &wimage->state))
+    {
+      _gtk_css_parser_error (parser, "Expected a valid integer value");
+      return FALSE;
+    }
+
+  while ( _gtk_css_parser_try (parser, ",", TRUE))
+    {
+      if ( _gtk_css_parser_try (parser, "over", TRUE))
+        {
+          if (!_gtk_css_parser_try (parser, "(", TRUE))
+            {
+              _gtk_css_parser_error (parser,
+                                     "Expected '(' after 'over'");
+              return FALSE;
+            }
+
+          if (!_gtk_css_parser_try_int (parser, &wimage->part2))
+            {
+              _gtk_css_parser_error (parser, "Expected a valid integer value");
+              return FALSE;
+            }
+
+          if (!_gtk_css_parser_try_int (parser, &wimage->state2))
+            {
+              _gtk_css_parser_error (parser, "Expected a valid integer value");
+              return FALSE;
+            }
+
+          if ( _gtk_css_parser_try (parser, ",", TRUE))
+            {
+              if (!_gtk_css_parser_try_double (parser, &wimage->over_alpha))
+                {
+                  _gtk_css_parser_error (parser, "Expected a valid double value");
+                  return FALSE;
+                }
+            }
+
+          if (!_gtk_css_parser_try (parser, ")", TRUE))
+            {
+              _gtk_css_parser_error (parser,
+                                     "Expected ')' at end of 'over'");
+              return FALSE;
+            }
+        }
+      else if ( _gtk_css_parser_try (parser, "margins", TRUE))
+        {
+          guint i;
+
+          if (!_gtk_css_parser_try (parser, "(", TRUE))
+            {
+              _gtk_css_parser_error (parser,
+                                     "Expected '(' after 'margins'");
+              return FALSE;
+            }
+
+          for (i = 0; i < 4; i++)
+            {
+              if (!_gtk_css_parser_try_int (parser, &wimage->margins[i]))
+                break;
+            }
+          
+          if (i == 0)
+            {
+              _gtk_css_parser_error (parser, "Expected valid margins");
+              return FALSE;
+            }
+
+          if (i == 1)
+            wimage->margins[1] = wimage->margins[0];
+          if (i <= 2)
+            wimage->margins[2] = wimage->margins[1];
+          if (i <= 3)
+            wimage->margins[3] = wimage->margins[2];
+          
+          if (!_gtk_css_parser_try (parser, ")", TRUE))
+            {
+              _gtk_css_parser_error (parser,
+                                     "Expected ')' at end of 'margins'");
+              return FALSE;
+            }
+        }
+      else
+        {
+          _gtk_css_parser_error (parser,
+                                 "Expected identifier");
+          return FALSE;
+        }
+    }
+
+  if (!_gtk_css_parser_try (parser, ")", TRUE))
+    {
+      _gtk_css_parser_error (parser,
+			     "Expected ')'");
+      return FALSE;
+    }
+  
+  return TRUE;
+}
+
+static void
+gtk_css_image_win32_print (GtkCssImage *image,
+                           GString     *string)
+{
+  g_string_append (string, "none /* printing win32 theme components is not implemented */");
+}
+
+static void
+_gtk_css_image_win32_class_init (GtkCssImageWin32Class *klass)
+{
+  GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
+
+  image_class->draw = gtk_css_image_win32_draw;
+  image_class->parse = gtk_css_image_win32_parse;
+  image_class->print = gtk_css_image_win32_print;
+}
+
+static void
+_gtk_css_image_win32_init (GtkCssImageWin32 *wimage)
+{
+  wimage->over_alpha = 1.0;
+  wimage->part2 = -1;
+  wimage->state2 = -1;
+}
+
diff --git a/gtk/gtkcssimagewin32private.h b/gtk/gtkcssimagewin32private.h
new file mode 100644
index 0000000..2c9e6e9
--- /dev/null
+++ b/gtk/gtkcssimagewin32private.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright  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.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, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors: Benjamin Otte <otte gnome org>
+ */
+
+#ifndef __GTK_CSS_IMAGE_WIN32_PRIVATE_H__
+#define __GTK_CSS_IMAGE_WIN32_PRIVATE_H__
+
+#include "gtk/gtkcssimageprivate.h"
+#include "gtk/gtkwin32themeprivate.h"
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_CSS_IMAGE_WIN32           (_gtk_css_image_win32_get_type ())
+#define GTK_CSS_IMAGE_WIN32(obj)           (G_TYPE_CHECK_INSTANCE_CAST (obj, GTK_TYPE_CSS_IMAGE_WIN32, GtkCssImageWin32))
+#define GTK_CSS_IMAGE_WIN32_CLASS(cls)     (G_TYPE_CHECK_CLASS_CAST (cls, GTK_TYPE_CSS_IMAGE_WIN32, GtkCssImageWin32Class))
+#define GTK_IS_CSS_IMAGE_WIN32(obj)        (G_TYPE_CHECK_INSTANCE_TYPE (obj, GTK_TYPE_CSS_IMAGE_WIN32))
+#define GTK_IS_CSS_IMAGE_WIN32_CLASS(obj)  (G_TYPE_CHECK_CLASS_TYPE (obj, GTK_TYPE_CSS_IMAGE_WIN32))
+#define GTK_CSS_IMAGE_WIN32_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_CSS_IMAGE_WIN32, GtkCssImageWin32Class))
+
+typedef struct _GtkCssImageWin32           GtkCssImageWin32;
+typedef struct _GtkCssImageWin32Class      GtkCssImageWin32Class;
+
+struct _GtkCssImageWin32
+{
+  GtkCssImage parent;
+
+  int part;
+  int state;
+
+  double over_alpha;
+  int part2;
+  int state2;
+
+  gint margins[4];
+
+  HTHEME theme;
+};
+
+struct _GtkCssImageWin32Class
+{
+  GtkCssImageClass parent_class;
+};
+
+GType          _gtk_css_image_win32_get_type             (void) G_GNUC_CONST;
+
+
+G_END_DECLS
+
+#endif /* __GTK_CSS_IMAGE_WIN32_PRIVATE_H__ */



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