Back to index

gcompris  8.2.2
py-mod-utils.c
Go to the documentation of this file.
00001 #include <Python.h>
00002 #include <pygobject.h>
00003 #include "gcompris/gcompris.h"
00004 #include "py-mod-utils.h"
00005 #include "py-gcompris-board.h"
00006 
00007 /* All functions provided by this python module
00008  * wraps a gcompris function. Each "py_*" function wraps the
00009  * "*" C function.
00010  */
00011 
00012 
00013 /* GdkPixbuf *gc_pixmap_load(char *pixmapfile); */
00014 static PyObject*
00015 py_gc_pixmap_load(PyObject* self, PyObject* args)
00016 {
00017   char* pixmapfile;
00018   GdkPixbuf* result;
00019   PyObject* pyresult;
00020 
00021   /* Parse arguments */
00022 
00023   if(!PyArg_ParseTuple(args, "s:gc_pixmap_load", &pixmapfile))
00024     return NULL;
00025 
00026   /* Call the corresponding C function */
00027   result = gc_pixmap_load(pixmapfile);
00028 
00029   /* Create and return the result */
00030   pyresult = (PyObject*) pygobject_new((GObject*) result);
00031 
00032   gdk_pixbuf_unref(result);
00033 
00034   return(pyresult);
00035 
00036 }
00037 
00038 
00039 /* void       gc_item_focus_set(GnomeCanvasItem *item, gboolean focus); */
00040 static PyObject*
00041 py_gc_item_focus_set(PyObject* self, PyObject* args)
00042 {
00043   PyObject* pyitem;
00044   GnomeCanvasItem* item;
00045   gint pyfocus;
00046   gboolean focus;
00047 
00048   /* Parse arguments */
00049   if(!PyArg_ParseTuple(args, "Oi:gc_item_focus_set", &pyitem, &pyfocus))
00050     return NULL;
00051 
00052   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00053   if(pyfocus>0) focus = TRUE;
00054   else focus = FALSE;
00055 
00056   /* Call the corresponding C function */
00057   gc_item_focus_set(item, focus);
00058 
00059   /* Create and return the result */
00060   Py_INCREF(Py_None);
00061   return Py_None;
00062 }
00063 
00064 
00065 /* gint       gc_item_focus_event(GnomeCanvasItem *item,
00066                                   GdkEvent *event,
00067                                   GnomeCanvasItem *dest_item);
00068 */
00069 static PyObject*
00070 py_gc_item_focus_event(PyObject* self, PyObject* args)
00071 {
00072   PyObject* pyitem;
00073   GnomeCanvasItem* item;
00074   PyObject* pyevent;
00075   GdkEvent* event;
00076   gint result;
00077 
00078   /* Parse arguments */
00079   if(!PyArg_ParseTuple(args, "OO:gc_item_focus_event", &pyitem, &pyevent))
00080     return NULL;
00081   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00082   event = (GdkEvent*) pygobject_get(pyevent);
00083 
00084   /* Call the corresponding C function */
00085   result = gc_item_focus_event(item, event, NULL);
00086 
00087   /* Create and return the result */
00088   return Py_BuildValue("i", result);
00089 }
00090 
00091 
00092 /* void gc_item_absolute_move(GnomeCanvasItem *item, int x, int y); */
00093 static PyObject*
00094 py_gc_item_absolute_move(PyObject* self, PyObject* args)
00095 {
00096   PyObject* pyitem;
00097   GnomeCanvasItem* item;
00098   int x, y;
00099 
00100   /* Parse arguments */
00101   if(!PyArg_ParseTuple(args, "Oii:gc_item_absolute_move", &pyitem, &x, &y))
00102     return NULL;
00103   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00104 
00105   /* Call the corresponding C function */
00106   gc_item_absolute_move(item, x, y);
00107 
00108   /* Create and return the result */
00109   Py_INCREF(Py_None);
00110   return Py_None;
00111 }
00112 
00113 
00114 /* void gc_item_rotate(GnomeCanvasItem *item, double angle); */
00115 static PyObject*
00116 py_gc_item_rotate(PyObject* self, PyObject* args)
00117 {
00118   PyObject* pyitem;
00119   GnomeCanvasItem* item;
00120   double angle;
00121 
00122   /* Parse arguments */
00123   if(!PyArg_ParseTuple(args, "Od:gc_item_rotate_relative", &pyitem, &angle))
00124     return NULL;
00125   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00126 
00127   /* Call the corresponding C function */
00128   gc_item_rotate(item, angle);
00129 
00130   /* Create and return the result */
00131   Py_INCREF(Py_None);
00132   return Py_None;
00133 }
00134 
00135 
00136 /* void gc_item_rotate_relative(GnomeCanvasItem *item, double angle); */
00137 static PyObject*
00138 py_gc_item_rotate_relative(PyObject* self, PyObject* args)
00139 {
00140   PyObject* pyitem;
00141   GnomeCanvasItem* item;
00142   double angle;
00143 
00144   /* Parse arguments */
00145   if(!PyArg_ParseTuple(args, "Od:gc_item_rotate_relative", &pyitem, &angle))
00146     return NULL;
00147   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00148 
00149   /* Call the corresponding C function */
00150   gc_item_rotate_relative(item, angle);
00151 
00152   /* Create and return the result */
00153   Py_INCREF(Py_None);
00154   return Py_None;
00155 }
00156 
00157 
00158 /* void gc_item_rotate_with_center(GnomeCanvasItem *item, double angle, int x, int y); */
00159 static PyObject*
00160 py_gc_item_rotate_with_center(PyObject* self, PyObject* args)
00161 {
00162   PyObject* pyitem;
00163   GnomeCanvasItem* item;
00164   double angle;
00165   int x,y;
00166 
00167   /* Parse arguments */
00168   if(!PyArg_ParseTuple(args, "Odii:gc_item_rotate_with_center", &pyitem, &angle, &x, &y))
00169     return NULL;
00170   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00171 
00172   /* Call the corresponding C function */
00173   gc_item_rotate_with_center(item, angle, x, y);
00174 
00175   /* Create and return the result */
00176   Py_INCREF(Py_None);
00177   return Py_None;
00178 }
00179 
00180 
00181 /* void gc_item_rotate_relative_with_center(GnomeCanvasItem *item, double angle, int x, int y); */
00182 static PyObject*
00183 py_gc_item_rotate_relative_with_center(PyObject* self, PyObject* args)
00184 {
00185   PyObject* pyitem;
00186   GnomeCanvasItem* item;
00187   double angle;
00188   int x,y;
00189 
00190   /* Parse arguments */
00191   if(!PyArg_ParseTuple(args, "Odii:gc_item_rotate_relative_with_center", &pyitem, &angle, &x, &y))
00192     return NULL;
00193   item = (GnomeCanvasItem*) pygobject_get(pyitem);
00194 
00195   /* Call the corresponding C function */
00196   gc_item_rotate_relative_with_center(item, angle, x, y);
00197 
00198   /* Create and return the result */
00199   Py_INCREF(Py_None);
00200   return Py_None;
00201 }
00202 
00203 
00204 /* Dialog callback wrapper */
00205 static PyObject* pyDialogBoxCallBackFunc = NULL;
00206 
00207 /* typedef void (*DialogBoxCallBack) (); */
00208 void pyDialogBoxCallBack(){
00209   PyObject* result;
00210   if(pyDialogBoxCallBackFunc==NULL) return;
00211 
00212   /* Build arguments */
00213   result = PyObject_CallObject(pyDialogBoxCallBackFunc, NULL);
00214   if(result==NULL){
00215     PyErr_Print();
00216   } else {
00217     Py_DECREF(result);
00218   }
00219 }
00220 
00221 
00222 /* void       gc_dialog(gchar *str, DialogBoxCallBack dbcb); */
00223 static PyObject*
00224 py_gc_dialog(PyObject* self, PyObject* args)
00225 {
00226   PyObject* pyCallback;
00227   gchar* str;
00228 
00229   /* Parse arguments */
00230   if(!PyArg_ParseTuple(args, "sO:gc_dialog", &str, &pyCallback))
00231     return NULL;
00232   if(!PyCallable_Check(pyCallback)){
00233     gc_dialog(str,NULL);
00234   }
00235   else {
00236   /* Call the corresponding C function */
00237   pyDialogBoxCallBackFunc = pyCallback ;
00238   gc_dialog(str, pyDialogBoxCallBack);
00239   }
00240   /* Create and return the result */
00241   Py_INCREF(Py_None);
00242   return Py_None;
00243 }
00244 
00245 
00246 /* void       gc_dialog_close(); */
00247 static PyObject*
00248 py_gc_dialog_close(PyObject* self, PyObject* args)
00249 {
00250   /* Parse arguments */
00251   if(!PyArg_ParseTuple(args, ":gc_dialog"))
00252     return NULL;
00253 
00254   /* Call the corresponding C function */
00255   gc_dialog_close();
00256 
00257   /* Create and return the result */
00258   Py_INCREF(Py_None);
00259   return Py_None;
00260 }
00261 
00262 
00263 /* void filename_pass(PyObject *pyitem, PyObject *pystring) */
00264 static PyObject*
00265 py_gcompris_filename_pass(PyObject* self, PyObject* args)
00266 {
00267   PyObject* pyitem;
00268   GnomeCanvasItem *item;
00269   char *string;
00270 
00271 
00272   /* Parse arguments */
00273   if(!PyArg_ParseTuple(args, "Os:gcompris_filename_pass", &pyitem, &string))
00274     return NULL;
00275 
00276   /* pass parameter from python */
00277   item = (GnomeCanvasItem *) pygobject_get(pyitem);
00278 
00279   /* gcompris_filename_pass( item, string); */
00280   g_object_set_data( G_OBJECT(item), "filename", string);
00281 
00282   /* Create and return the result */
00283   Py_INCREF(Py_None);
00284   return Py_None;
00285 
00286 }
00287 
00288 /*
00289  * Set a property in a canvas object
00290  * ---------------------------------
00291  */
00292 /* void canvas_set_property(GnomeCanvasItem *item, gchar *property, gchar* value) */
00293 static PyObject*
00294 py_gcompris_canvas_set_property(PyObject* self, PyObject* args)
00295 {
00296   PyObject* pyitem;
00297   GnomeCanvasItem *item;
00298   char *property;
00299   char *value;
00300 
00301 
00302   /* Parse arguments */
00303   if(!PyArg_ParseTuple(args, "Oss:gcompris_canvas_set_property", &pyitem, &property, &value))
00304     return NULL;
00305 
00306   /* pass parameter from python */
00307   item = (GnomeCanvasItem *) pygobject_get(pyitem);
00308 
00309   /* gcompris_filename_pass( item, string); */
00310   g_object_set_data( G_OBJECT(item), property, value);
00311 
00312   /* Create and return the result */
00313   Py_INCREF(Py_None);
00314   return Py_None;
00315 
00316 }
00317 
00318 /*
00319  * Get a property in a canvas object
00320  * ---------------------------------
00321  */
00322 /* gchar *canvas_get_property(GnomeCanvasItem *item, gchar *property) */
00323 static PyObject*
00324 py_gcompris_canvas_get_property(PyObject* self, PyObject* args)
00325 {
00326   PyObject* pyitem;
00327   GnomeCanvasItem *item;
00328   char  *property;
00329   gchar *result;
00330 
00331   /* Parse arguments */
00332   if(!PyArg_ParseTuple(args, "Os:gcompris_canvas_get_property", &pyitem, &property))
00333     return NULL;
00334 
00335   /* pass parameter from python */
00336   item = (GnomeCanvasItem *) pygobject_get(pyitem);
00337 
00338   /* gcompris_filename_pass( item, string); */
00339   result = g_object_get_data( G_OBJECT(item), property);
00340 
00341   /* Create and return the result */
00342   if(result==NULL) {
00343     Py_INCREF(Py_None);
00344     return Py_None;
00345   } else {
00346     return Py_BuildValue("s", result);
00347   }
00348 
00349 }
00350 
00351 
00352 static PyMethodDef PythonGcomprisUtilsModule[] = {
00353   { "load_pixmap",  py_gc_pixmap_load, METH_VARARGS, "gc_pixmap_load" },
00354   { "set_image_focus",  py_gc_item_focus_set, METH_VARARGS, "gc_item_focus_set" },
00355   { "item_event_focus",  py_gc_item_focus_event, METH_VARARGS, "gc_item_focus_event" },
00356   { "item_absolute_move",  py_gc_item_absolute_move, METH_VARARGS, "gc_item_absolute_move" },
00357   { "item_rotate",  py_gc_item_rotate, METH_VARARGS, "gc_item_rotate" },
00358   { "item_rotate_relative",  py_gc_item_rotate_relative, METH_VARARGS, "gc_item_rotate_relative" },
00359   { "item_rotate__with_center",  py_gc_item_rotate_with_center, METH_VARARGS,
00360     "item_rotate_with_center" },
00361   { "item_rotate_relative_with_center",  py_gc_item_rotate_relative_with_center, METH_VARARGS,
00362     "item_rotate_relative_with_center" },
00363   { "dialog",  py_gc_dialog, METH_VARARGS, "gc_dialog" },
00364   { "dialog_close",  py_gc_dialog_close, METH_VARARGS, "gc_dialog_close" },
00365   { "filename_pass",  py_gcompris_filename_pass, METH_VARARGS, "gcompris_filename_pass" },
00366   { "canvas_set_property",  py_gcompris_canvas_set_property, METH_VARARGS, "gcompris_canvas_set_property" },
00367   { "canvas_get_property",  py_gcompris_canvas_get_property, METH_VARARGS, "gcompris_canvas_get_property" },
00368   { NULL, NULL, 0, NULL}
00369 };
00370 
00371 
00372 void python_gcompris_utils_module_init(void)
00373 {
00374   PyObject* module;
00375   module = Py_InitModule("_gcompris_utils", PythonGcomprisUtilsModule);
00376 }
00377 
00378 /* Some usefull code parts ... */
00379 /*
00380 static PyObject*
00381 py_gcompris_(PyObject* self, PyObject* args)
00382 {
00383 */  /* Parse arguments */
00384 /*  if(!PyArg_ParseTuple(args, ":gcompris_"))
00385     return NULL;
00386 */
00387   /* Call the corresponding C function */
00388 /*  gcompris_();
00389 */
00390   /* Create and return the result */
00391 /*  Py_INCREF(Py_None);
00392   return Py_None;
00393 }
00394 */
00395 /*
00396   { "",  py_gcompris_, METH_VARARGS, "gcompris_" },
00397 */