[seed] cairo: Implement skeleton cairo.Context class + constructor



commit b373123acfc732473d631f886b4712aa35f1b7ed
Author: Robert Carr <racarr svn gnome org>
Date:   Thu May 14 04:17:07 2009 -0400

    cairo: Implement skeleton cairo.Context class + constructor
---
 modules/cairo/seed-cairo-image-surface.c |    2 +-
 modules/cairo/seed-cairo-surface.c       |   17 ++++---
 modules/cairo/seed-cairo.c               |   80 ++++++++++++++++++++++++++++++
 modules/cairo/seed-cairo.h               |    4 +-
 4 files changed, 93 insertions(+), 10 deletions(-)

diff --git a/modules/cairo/seed-cairo-image-surface.c b/modules/cairo/seed-cairo-image-surface.c
index b8ed9b7..aa58f58 100644
--- a/modules/cairo/seed-cairo-image-surface.c
+++ b/modules/cairo/seed-cairo-image-surface.c
@@ -95,7 +95,7 @@ seed_cairo_construct_image_surface (SeedContext ctx,
   cairo_format_t format;
   if (argument_count != 3)
     {
-      EXPECTED_EXCEPTION("ImageSurface", "3 arguments", null);
+      EXPECTED_EXCEPTION("ImageSurface", "3 arguments");
     }
   
   format = seed_value_to_long (ctx, arguments[0], exception);
diff --git a/modules/cairo/seed-cairo-surface.c b/modules/cairo/seed-cairo-surface.c
index bab6a58..14f15db 100644
--- a/modules/cairo/seed-cairo-surface.c
+++ b/modules/cairo/seed-cairo-surface.c
@@ -32,7 +32,8 @@ static void
 seed_cairo_surface_finalize (SeedObject obj)
 {
   cairo_surface_t *s = CAIRO_SURFACE_PRIV(obj);
-  cairo_surface_destroy (s);
+  if (s)
+    cairo_surface_destroy (s);
 }
 
 cairo_surface_t *
@@ -72,10 +73,12 @@ seed_cairo_surface_create_similar (SeedContext ctx,
   CHECK_THIS(null);
   if (argument_count != 3)
     {
-      EXPECTED_EXCEPTION("create_similar", "3 arguments", null);
+      EXPECTED_EXCEPTION("create_similar", "3 arguments");
     }
   
   surface = seed_object_to_cairo_surface (ctx, this_object, exception);
+  if (!surface)
+    return seed_make_undefined (ctx);
   content = seed_value_to_long (ctx, arguments[0], exception);
   width = seed_value_to_int (ctx, arguments[1], exception);
   height = seed_value_to_int (ctx, arguments[2], exception);
@@ -147,7 +150,7 @@ seed_cairo_surface_mark_dirty_rectangle(SeedContext ctx,
   CHECK_THIS(undefined);
   if (argument_count != 4)
     {
-      EXPECTED_EXCEPTION("mark_dirty_rectangle", "4 arguments", undefined);
+      EXPECTED_EXCEPTION("mark_dirty_rectangle", "4 arguments");
     }
   surf = seed_object_to_cairo_surface (ctx, this_object, exception);
   x = seed_value_to_int (ctx, arguments[0], exception);
@@ -186,7 +189,7 @@ seed_cairo_surface_set_device_offset(SeedContext ctx,
   CHECK_THIS(null);
   if (argument_count != 2)
     {
-      EXPECTED_EXCEPTION("set_device_offset", "2 arguments", undefined);
+      EXPECTED_EXCEPTION("set_device_offset", "2 arguments");
     }
   surf = seed_object_to_cairo_surface (ctx, this_object, exception);
   x = seed_value_to_double (ctx, arguments[0], exception);
@@ -211,7 +214,7 @@ seed_cairo_surface_get_device_offset(SeedContext ctx,
   CHECK_THIS(null);
   if (argument_count != 0)
     {
-      EXPECTED_EXCEPTION("get_device_offset", "no arguments", null);
+      EXPECTED_EXCEPTION("get_device_offset", "no arguments");
     }
   surf = seed_object_to_cairo_surface (ctx, this_object, exception);
   cairo_surface_get_device_offset (surf, &x, &y);
@@ -235,7 +238,7 @@ seed_cairo_surface_set_fallback_resolution(SeedContext ctx,
   CHECK_THIS(null);
   if (argument_count != 2)
     {
-      EXPECTED_EXCEPTION("set_fallback_resolution", "2 arguments", undefined);
+      EXPECTED_EXCEPTION("set_fallback_resolution", "2 arguments");
     }
   surf = seed_object_to_cairo_surface (ctx, this_object, exception);
   x = seed_value_to_double (ctx, arguments[0], exception);
@@ -260,7 +263,7 @@ seed_cairo_surface_get_fallback_resolution(SeedContext ctx,
   CHECK_THIS(null);
   if (argument_count != 0)
     {
-      EXPECTED_EXCEPTION("get_fallback_resolution", "no arguments", null);
+      EXPECTED_EXCEPTION("get_fallback_resolution", "no arguments");
     }
   surf = seed_object_to_cairo_surface (ctx, this_object, exception);
   cairo_surface_get_fallback_resolution (surf, &x, &y);
diff --git a/modules/cairo/seed-cairo.c b/modules/cairo/seed-cairo.c
index db6ceab..bc400f6 100644
--- a/modules/cairo/seed-cairo.c
+++ b/modules/cairo/seed-cairo.c
@@ -1,9 +1,28 @@
 #include <seed.h>
 #include <cairo/cairo.h>
 #include "seed-cairo-surface.h"
+#include "seed-cairo-enums.h"
+
 SeedEngine *eng;
 SeedObject namespace_ref;
 
+#define CAIRO_CONTEXT_PRIV(obj) ((cairo_t *)seed_object_get_private(obj))
+
+#define CHECK_CAIRO(obj) ({						\
+    if (!seed_object_is_of_class (ctx, obj, seed_cairo_class)){	\
+      seed_make_exception (ctx, exception, "ArgumentError", "Object is not a Cairo Context"); \
+      return seed_make_undefined (ctx);\
+    }									\
+    if (!seed_object_get_private (obj)){				\
+      seed_make_exception (ctx, exception, "ArgumentError", "Cairo Context has been destroyed"); \
+      return seed_make_undefined (ctx);}})
+
+#define CHECK_THIS() if (!seed_object_get_private (this_object)){	\
+    seed_make_exception (ctx, exception, "ArgumentError", "Cairo Context has been destroyed"); \
+      return seed_make_undefined (ctx);}
+
+SeedClass seed_cairo_context_class;
+
 cairo_user_data_key_t *
 seed_get_cairo_key ()
 {
@@ -19,9 +38,61 @@ seed_cairo_destroy_func (void *obj)
   seed_object_set_private (object, NULL);
 }
 
+cairo_t *
+seed_object_to_cairo_context (SeedContext ctx, SeedObject obj, SeedException *exception)
+{
+  if (seed_object_is_of_class (ctx, obj, seed_cairo_context_class))
+    return CAIRO_CONTEXT_PRIV (obj);
+  seed_make_exception (ctx, exception, "ArgumentError", "Object is not a Cairo Context");
+  return NULL;
+}
+
+SeedObject
+seed_object_from_cairo_context (SeedContext ctx, cairo_t *cr)
+{
+  SeedObject jsobj;
+  
+  jsobj = cairo_get_user_data (cr, seed_get_cairo_key());
+  if (jsobj)
+    return jsobj;
+  
+  jsobj = seed_make_object (ctx, seed_cairo_context_class, cr);
+  cairo_set_user_data (cr, seed_get_cairo_key(), jsobj, seed_cairo_destroy_func);
+  return jsobj;
+}
+
+void
+seed_cairo_context_finalize (SeedObject obj)
+{
+  cairo_t *cr = CAIRO_CONTEXT_PRIV (obj);
+  if (cr)
+    cairo_destroy (cr);
+}
+
+static SeedObject
+seed_cairo_construct_context (SeedContext ctx,
+			      SeedObject constructor,
+			      size_t argument_count,
+			      const SeedValue arguments[],
+			      SeedException * exception)
+{
+  cairo_surface_t *surf;
+  if (argument_count != 1)
+    {
+      EXPECTED_EXCEPTION ("Context", "1 argument");
+    }
+  surf = seed_object_to_cairo_surface (ctx, arguments[0], exception);
+  if (!surf)
+    return seed_make_undefined (ctx);
+  return seed_object_from_cairo_context (ctx, cairo_create (surf));
+}
+
+
 SeedObject
 seed_module_init(SeedEngine * local_eng)
 {
+  SeedObject constructor_ref;
+  seed_class_definition cairo_def = seed_empty_class;
   eng = local_eng;
   namespace_ref = seed_make_object (eng->context, NULL, NULL);
   
@@ -30,5 +101,14 @@ seed_module_init(SeedEngine * local_eng)
   seed_define_cairo_surface (eng->context, namespace_ref);
   seed_define_cairo_enums (eng->context, namespace_ref);
   
+  cairo_def.class_name = "CairoContext";
+  cairo_def.finalize = seed_cairo_context_finalize;
+  seed_cairo_context_class = seed_create_class (&cairo_def);
+
+  constructor_ref = seed_make_constructor (eng->context,
+					   seed_cairo_context_class,
+					   seed_cairo_construct_context);
+  seed_object_set_property (eng->context, namespace_ref, "ImageSurface", constructor_ref);
+  
   return namespace_ref;
 }
diff --git a/modules/cairo/seed-cairo.h b/modules/cairo/seed-cairo.h
index d490a07..719fb8f 100644
--- a/modules/cairo/seed-cairo.h
+++ b/modules/cairo/seed-cairo.h
@@ -3,9 +3,9 @@
 #include <seed.h>
 #include <cairo/cairo.h>
 
-#define EXPECTED_EXCEPTION(name, argnum, res)				\
+#define EXPECTED_EXCEPTION(name, argnum)				\
   seed_make_exception (ctx, exception, "ArgumentError", name " expected " argnum " got %Zd", argument_count); \
-  return seed_make_##res (ctx);
+  return seed_make_undefined (ctx);
 
 cairo_user_data_key_t *seed_get_cairo_key ();
 void seed_cairo_destroy_func (void *obj);



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