Back to index

gcompris  8.2.2
canvasmodule.c
Go to the documentation of this file.
00001 /* -*- Mode: C; c-basic-offset: 4 -*- */
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include "config.h"
00005 #endif
00006 
00007 /* include this first, before NO_IMPORT_PYGOBJECT is defined */
00008 #include <pygobject.h>
00009 #include <pygtk/pygtk.h>
00010 #include <libgnomecanvas/libgnomecanvas.h>
00011 
00012 static PyObject *
00013 gnomecanvaspoints_from_value(const GValue *value)
00014 {
00015     GnomeCanvasPoints *points = (GnomeCanvasPoints *)g_value_get_boxed(value);
00016     PyObject *list;
00017     gint i;
00018 
00019     list = PyList_New(0);
00020     if (points)
00021        for (i = 0; i < points->num_points * 2; i++) {
00022            PyObject *item = PyFloat_FromDouble(points->coords[i]);
00023 
00024            PyList_Append(list, item);
00025            Py_DECREF(item);
00026        }
00027     return list;
00028 }
00029 
00030 static int
00031 gnomecanvaspoints_to_value(GValue *value, PyObject *object)
00032 {
00033     GnomeCanvasPoints *points;
00034     gint i, len;
00035 
00036     if (!PySequence_Check(object))
00037        return -1;
00038     len = PySequence_Length(object);
00039     if (len % 2 != 0)
00040        return -1;
00041 
00042     points = gnome_canvas_points_new(len / 2);
00043     for (i = 0; i < len; i++) {
00044        PyObject *item = PySequence_GetItem(object, i);
00045 
00046        points->coords[i] = PyFloat_AsDouble(item);
00047        if (PyErr_Occurred()) {
00048            gnome_canvas_points_unref(points);
00049            PyErr_Clear();
00050            Py_DECREF(item);
00051            return -1;
00052        }
00053        Py_DECREF(item);
00054     }
00055     g_value_set_boxed(value, points);
00056     gnome_canvas_points_unref(points);
00057     return 0;
00058 }
00059 
00060 
00061 void pycanvas_register_classes (PyObject *d);
00062 
00063 extern PyMethodDef pycanvas_functions[];
00064 
00065 
00066 DL_EXPORT(void)
00067 initgnomecanvas (void)
00068 {
00069     PyObject *m, *d;
00070        
00071     init_pygobject ();
00072     init_pygtk ();
00073 
00074     pyg_register_boxed_custom(GNOME_TYPE_CANVAS_POINTS,
00075                            gnomecanvaspoints_from_value,
00076                            gnomecanvaspoints_to_value);
00077 
00078     m = Py_InitModule ("gnomecanvas", pycanvas_functions);
00079     d = PyModule_GetDict (m);
00080 
00081       /* ArtPathcode */
00082 #define add_item(short)\
00083     PyModule_AddIntConstant (m, #short, ART_##short)
00084     add_item(MOVETO);
00085     add_item(MOVETO_OPEN);
00086     add_item(CURVETO);
00087     add_item(LINETO);
00088 #undef add_item
00089        
00090     pycanvas_register_classes (d);
00091     /* pycanvas_add_constants (d, "GNOME_CANVAS_"); */
00092 }