Resolve the cWindow reference using Ruby.
[ruby-ecore.git] / src / ecore_evas / rb_software_x11.c
index 817c28770d6f798fdd2eede00893b254ca7eb79e..e8c38338ffac1ac44dbdbd3356df523b74d0847d 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * $Id$
+ * $Id: rb_software_x11.c 389 2006-11-11 11:40:24Z tilman $
  *
- * Copyright (C) 2004 Tilman Sauerbeck (tilman at code-monkey de)
+ * Copyright (C) 2004 ruby-ecore team (see AUTHORS)
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
 
 #include <ruby.h>
 
+#include <Ecore.h>
 #include <Ecore_Evas.h>
 
+#include "../ecore/rb_ecore.h"
+#include "../ecore_x/rb_window.h"
 #include "rb_ecore_evas_main.h"
 #include "rb_ecore_evas.h"
 
-VALUE cGlX11;
+typedef struct {
+       RbEcoreEvas ee;
+       VALUE parent_window;
+       VALUE window;
+} RbEcoreEvasSoftwareX11;
 
+static void c_mark (RbEcoreEvasSoftwareX11 *ee)
+{
+       c_ecore_evas_mark (&ee->ee);
+
+       if (!NIL_P (ee->parent_window))
+               rb_gc_mark (ee->parent_window);
+
+       if (!NIL_P (ee->window))
+               rb_gc_mark (ee->window);
+}
+
+static void c_free (RbEcoreEvasSoftwareX11 *ee)
+{
+       c_ecore_evas_free (&ee->ee, false);
+
+       free (ee);
+}
+
+static VALUE c_alloc (VALUE klass)
+{
+       RbEcoreEvasSoftwareX11 *ee = NULL;
+
+       ecore_init ();
+       ecore_evas_init ();
+
+       return Data_Make_Struct (klass, RbEcoreEvasSoftwareX11,
+                                c_mark, c_free, ee);
+}
+
+/*
+ * call-seq:
+ *  Ecore::Evas::SoftwareX11.new([display, parent, x, y, w, h]) => swx11
+ *
+ * Creates an Ecore::Evas::SoftwareX11 object.
+ */
 static VALUE c_init (int argc, VALUE *argv, VALUE self)
 {
        VALUE disp, parent, geom[4];
-       Ecore_Evas **ee = NULL;
+       RbWindow *win = NULL;
        char *cdisp = NULL;
-       int i, igeom[4] = {0};
+       int i, igeom[4] = {0, 0, 0, 0};
 
-       Data_Get_Struct (self, Ecore_Evas *, ee);
+       GET_OBJ (self, RbEcoreEvasSoftwareX11, ee);
 
        rb_scan_args (argc, argv, "06", &disp, &parent,
                      &geom[0], &geom[1], &geom[2], &geom[3]);
 
        if (!NIL_P (disp)) {
                Check_Type (disp, T_STRING);
-               cdisp = STR2CSTR (disp);
+               cdisp = StringValuePtr (disp);
+       }
+
+       if (!NIL_P (parent)) {
+               CHECK_CLASS (parent, cWindow);
+               Data_Get_Struct (parent, RbWindow, win);
        }
 
        for (i = 0; i < 4; i++)
                if (!NIL_P (geom[i])) {
                        Check_Type (geom[i], T_FIXNUM);
-                       igeom[i] = NUM2INT (geom[i]);
+                       igeom[i] = FIX2INT (geom[i]);
                }
 
-       *ee = ecore_evas_gl_x11_new (cdisp, 0,
-                                    igeom[0], igeom[1],
-                                    igeom[2], igeom[3]);
+       ee->ee.real = ecore_evas_software_x11_new (cdisp,
+                                                  win ? win->real : 0,
+                                                  igeom[0], igeom[1],
+                                                  igeom[2], igeom[3]);
+       ee->parent_window = parent;
+       ee->window = Qnil;
+
+       rb_call_super (argc, argv);
 
        return self;
 }
 
-static VALUE c_new (int argc, VALUE *argv, VALUE klass)
+/*
+ * call-seq:
+ *  swx11.window => window
+ *
+ * Returns the <code>Ecore::X::Window</code> object for <i>swx11</i>.
+ */
+static VALUE c_window_get (VALUE self)
 {
-       VALUE self;
-       Ecore_Evas **ee;
+       Ecore_X_Window w;
 
-       self = Data_Make_Struct (klass, Ecore_Evas *,
-                                NULL, c_ecore_evas_free, ee);
+       GET_OBJ (self, RbEcoreEvasSoftwareX11, ee);
 
-       rb_obj_call_init (self, argc, argv);
+       if (NIL_P (ee->window)) {
+               w = ecore_evas_software_x11_window_get (ee->ee.real);
+               ee->window = TO_ECORE_X_WINDOW (self, w);
+       }
 
-       return self;
+       return ee->window;
 }
 
-void Init_GlX11 (void)
+void Init_SoftwareX11 (void)
 {
-       cGlX11 = rb_define_class_under (mEvas, "GlX11", cEcoreEvas);
+       VALUE c;
 
-       rb_define_singleton_method (cGlX11, "new", c_new, -1);
-       rb_define_method (cGlX11, "initialize", c_init, -1);
-}
+       c = rb_define_class_under (mEvas, "SoftwareX11", cEcoreEvas);
 
+       rb_define_alloc_func (c, c_alloc);
+       rb_define_method (c, "initialize", c_init, -1);
+       rb_define_method (c, "window", c_window_get, 0);
+}