gboolean PyDia_export_data(DiagramData *data, DiaContext *ctx, const gchar *filename, const gchar *diafilename, void* user_data) { DiaPyRenderer *renderer; { FILE *file; file = g_fopen(filename, "w"); /* "wb" for binary! */ if (file == NULL) { dia_context_add_message_with_errno(ctx, errno, _("Couldn't open '%s' for writing.\n"), dia_context_get_filename(ctx)); return FALSE; } else fclose (file); } renderer = g_object_new (DIA_TYPE_PY_RENDERER, NULL); renderer->filename = g_strdup (filename); renderer->diagram_data = PyDiaDiagramData_New(data); /* The Python Renderer object was created at PyDia_Register */ renderer->self = (PyObject*)user_data; /* this will call the required callback functions above */ data_render(data, DIA_RENDERER(renderer), NULL, NULL, NULL); g_object_unref(renderer); return TRUE; }
/*! * Callback for "object_add" and "object_remove "signal, used by the connect_after method, * it's a proxy for the python function, creating the values it needs. * Params are those of the signals on the Diagram object. * @param dia The DiagramData that emitted the signal * @param layer The Layer that the object is removed or added to. * @param obj The DiaObject that the signal is about. * @param user_data The python function to be called by the callback. */ static void PyDiaDiagramData_CallbackObject(DiagramData *dia,Layer *layer,DiaObject *obj,void *user_data) { PyObject *pydata,*pylayer,*pyobj,*res,*arg; PyObject *func = user_data; /* Check that we got a function */ if (!func || !PyCallable_Check (func)) { g_warning ("Callback called without valid callback function."); return; } /* Create a new PyDiaDiagramData object. */ if (dia) pydata = PyDiaDiagramData_New (dia); else { pydata = Py_None; Py_INCREF (pydata); } /* * Create PyDiaLayer */ if (layer) pylayer = PyDiaLayer_New (layer); else { pylayer = Py_None; Py_INCREF (pylayer); } /* * Create PyDiaObject */ if (layer) pyobj = PyDiaObject_New (obj); else { pyobj = Py_None; Py_INCREF (pyobj); } Py_INCREF(func); /* Call the callback. */ arg = Py_BuildValue ("(OOO)", pydata,pylayer,pyobj); if (arg) { res = PyEval_CallObject (func, arg); ON_RES(res, FALSE); } /* Cleanup */ Py_XDECREF (arg); Py_DECREF(func); Py_XDECREF(pydata); Py_XDECREF(pylayer); Py_XDECREF(pyobj); }
static PyObject * PyDiaDiagram_GetAttr(PyDiaDiagram *self, gchar *attr) { if (!strcmp(attr, "__members__")) return Py_BuildValue("[sssss]", "data", "displays", "filename", "modified", "selected", "unsaved"); else if (!strcmp(attr, "data")) return PyDiaDiagramData_New (self->dia->data); else if (!strcmp(attr, "filename")) return PyString_FromString(self->dia->filename); else if (!strcmp(attr, "unsaved")) return PyInt_FromLong(self->dia->unsaved); else if (!strcmp(attr, "modified")) return PyInt_FromLong(diagram_is_modified(self->dia)); else if (!strcmp(attr, "selected")) { guint i, len = g_list_length (self->dia->data->selected); PyObject *ret = PyTuple_New(len); GList *tmp; for (i = 0, tmp = self->dia->data->selected; tmp; i++, tmp = tmp->next) PyTuple_SetItem(ret, i, PyDiaObject_New((DiaObject *)tmp->data)); return ret; } else if (!strcmp(attr, "displays")) { PyObject *ret; GSList *tmp; gint i; ret = PyTuple_New(g_slist_length(self->dia->displays)); for (i = 0, tmp = self->dia->displays; tmp; i++, tmp = tmp->next) PyTuple_SetItem(ret, i, PyDiaDisplay_New((DDisplay *)tmp->data)); return ret; } return Py_FindMethod(PyDiaDiagram_Methods, (PyObject *)self, attr); }