1 /* -*- Mode: C; c-basic-offset: 4 -*-
2 * pygtk- Python bindings for the GTK toolkit.
3 * Copyright (C) 1998-2003 James Henstridge
4 * Copyright (C) 2004 Johan Dahlin
5 *
6 * pygenum.c: GEnum and GFlag wrappers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23
24 #include <Python.h>
25 #include <glib-object.h>
26
27 #include "pygenum.h"
28 #include "pygflags.h"
29 #include "pygi-type.h"
30 #include "pygparamspec.h"
31 #include "pygi-util.h"
32 #include "pygi-basictype.h"
33
34 PYGI_DEFINE_TYPE("gobject.GParamSpec", PyGParamSpec_Type, PyGParamSpec);
35
36 static PyObject*
pyg_param_spec_richcompare(PyObject * self,PyObject * other,int op)37 pyg_param_spec_richcompare(PyObject *self, PyObject *other, int op)
38 {
39 if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGParamSpec_Type)
40 return pyg_ptr_richcompare (pyg_param_spec_get (self),
41 pyg_param_spec_get (other),
42 op);
43 else {
44 Py_INCREF(Py_NotImplemented);
45 return Py_NotImplemented;
46 }
47 }
48
49 static Py_hash_t
pyg_param_spec_hash(PyGParamSpec * self)50 pyg_param_spec_hash(PyGParamSpec *self)
51 {
52 return (Py_hash_t)(gintptr)(pyg_param_spec_get (self));
53 }
54
55 static PyObject *
pyg_param_spec_repr(PyGParamSpec * self)56 pyg_param_spec_repr(PyGParamSpec *self)
57 {
58 char buf[80];
59
60 g_snprintf(buf, sizeof(buf), "<%s '%s'>",
61 G_PARAM_SPEC_TYPE_NAME (pyg_param_spec_get (self)),
62 g_param_spec_get_name (pyg_param_spec_get (self)));
63 return PyUnicode_FromString (buf);
64 }
65
66 static void
pyg_param_spec_dealloc(PyGParamSpec * self)67 pyg_param_spec_dealloc(PyGParamSpec *self)
68 {
69 g_param_spec_unref (pyg_param_spec_get (self));
70 PyObject_DEL(self);
71 }
72
73
74 static PyObject *
pygenum_from_pspec(GParamSpec * pspec)75 pygenum_from_pspec(GParamSpec *pspec)
76 {
77 PyObject *pyclass;
78 GParamSpecEnum *enum_pspec;
79 GType enum_type;
80
81 enum_pspec = G_PARAM_SPEC_ENUM(pspec);
82 enum_type = G_ENUM_CLASS_TYPE(enum_pspec->enum_class);
83 pyclass = (PyObject*)g_type_get_qdata(enum_type, pygenum_class_key);
84 if (pyclass == NULL) {
85 pyclass = pyg_enum_add(NULL, g_type_name(enum_type), NULL, enum_type);
86 if (pyclass == NULL)
87 pyclass = Py_None;
88 }
89
90 Py_INCREF(pyclass);
91 return pyclass;
92 }
93
94 static PyObject *
pygflags_from_pspec(GParamSpec * pspec)95 pygflags_from_pspec(GParamSpec *pspec)
96 {
97 PyObject *pyclass;
98 GParamSpecFlags *flag_pspec;
99 GType flag_type;
100
101 flag_pspec = G_PARAM_SPEC_FLAGS(pspec);
102 flag_type = G_FLAGS_CLASS_TYPE(flag_pspec->flags_class);
103 pyclass = (PyObject*)g_type_get_qdata(flag_type, pygflags_class_key);
104 if (pyclass == NULL) {
105 pyclass = pyg_flags_add(NULL, g_type_name(flag_type), NULL, flag_type);
106 if (pyclass == NULL)
107 pyclass = Py_None;
108 }
109 Py_INCREF(pyclass);
110 return pyclass;
111 }
112
113 static PyObject *
pyg_param_spec_getattr(PyGParamSpec * self,const gchar * attr)114 pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
115 {
116 GParamSpec *pspec;
117
118 pspec = pyg_param_spec_get (self);
119
120 /* common attributes */
121 if (!strcmp(attr, "__gtype__")) {
122 return pyg_type_wrapper_new(G_PARAM_SPEC_TYPE(pspec));
123 } else if (!strcmp(attr, "name")) {
124 return Py_BuildValue("s", g_param_spec_get_name(pspec));
125 } else if (!strcmp(attr, "nick")) {
126 return Py_BuildValue("s", g_param_spec_get_nick(pspec));
127 } else if (!strcmp(attr, "blurb") || !strcmp(attr, "__doc__")) {
128 return Py_BuildValue("s", g_param_spec_get_blurb(pspec));
129 } else if (!strcmp(attr, "flags")) {
130 return pygi_guint_to_py (pspec->flags);
131 } else if (!strcmp(attr, "value_type")) {
132 return pyg_type_wrapper_new(pspec->value_type);
133 } else if (!strcmp(attr, "owner_type")) {
134 return pyg_type_wrapper_new(pspec->owner_type);
135 }
136
137 if (G_IS_PARAM_SPEC_CHAR(pspec)) {
138 if (!strcmp(attr, "default_value")) {
139 return PyUnicode_FromFormat(
140 "%c", G_PARAM_SPEC_CHAR(pspec)->default_value);
141 } else if (!strcmp(attr, "minimum")) {
142 return pygi_gint8_to_py (G_PARAM_SPEC_CHAR(pspec)->minimum);
143 } else if (!strcmp(attr, "maximum")) {
144 return pygi_gint8_to_py (G_PARAM_SPEC_CHAR(pspec)->maximum);
145 }
146 } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
147 if (!strcmp(attr, "default_value")) {
148 return PyUnicode_FromFormat(
149 "%c", G_PARAM_SPEC_UCHAR(pspec)->default_value);
150 } else if (!strcmp(attr, "minimum")) {
151 return pygi_guint8_to_py (G_PARAM_SPEC_UCHAR(pspec)->minimum);
152 } else if (!strcmp(attr, "maximum")) {
153 return pygi_guint8_to_py (G_PARAM_SPEC_UCHAR(pspec)->maximum);
154 }
155 } else if (G_IS_PARAM_SPEC_BOOLEAN(pspec)) {
156 if (!strcmp(attr, "default_value")) {
157 return pygi_gboolean_to_py (G_PARAM_SPEC_BOOLEAN(pspec)->default_value);
158 }
159 } else if (G_IS_PARAM_SPEC_INT(pspec)) {
160 if (!strcmp(attr, "default_value")) {
161 return pygi_gint_to_py (G_PARAM_SPEC_INT(pspec)->default_value);
162 } else if (!strcmp(attr, "minimum")) {
163 return pygi_gint_to_py (G_PARAM_SPEC_INT(pspec)->minimum);
164 } else if (!strcmp(attr, "maximum")) {
165 return pygi_gint_to_py (G_PARAM_SPEC_INT(pspec)->maximum);
166 }
167 } else if (G_IS_PARAM_SPEC_UINT(pspec)) {
168 if (!strcmp(attr, "default_value")) {
169 return pygi_guint_to_py (G_PARAM_SPEC_UINT(pspec)->default_value);
170 } else if (!strcmp(attr, "minimum")) {
171 return pygi_guint_to_py (G_PARAM_SPEC_UINT(pspec)->minimum);
172 } else if (!strcmp(attr, "maximum")) {
173 return pygi_guint_to_py (G_PARAM_SPEC_UINT(pspec)->maximum);
174 }
175 } else if (G_IS_PARAM_SPEC_LONG(pspec)) {
176 if (!strcmp(attr, "default_value")) {
177 return pygi_glong_to_py (G_PARAM_SPEC_LONG(pspec)->default_value);
178 } else if (!strcmp(attr, "minimum")) {
179 return pygi_glong_to_py (G_PARAM_SPEC_LONG(pspec)->minimum);
180 } else if (!strcmp(attr, "maximum")) {
181 return pygi_glong_to_py (G_PARAM_SPEC_LONG(pspec)->maximum);
182 }
183 } else if (G_IS_PARAM_SPEC_ULONG(pspec)) {
184 if (!strcmp(attr, "default_value")) {
185 return pygi_gulong_to_py (G_PARAM_SPEC_ULONG(pspec)->default_value);
186 } else if (!strcmp(attr, "minimum")) {
187 return pygi_gulong_to_py (G_PARAM_SPEC_ULONG(pspec)->minimum);
188 } else if (!strcmp(attr, "maximum")) {
189 return pygi_gulong_to_py (G_PARAM_SPEC_ULONG(pspec)->maximum);
190 }
191 } else if (G_IS_PARAM_SPEC_INT64(pspec)) {
192 if (!strcmp(attr, "default_value")) {
193 return pygi_gint64_to_py (G_PARAM_SPEC_INT64(pspec)->default_value);
194 } else if (!strcmp(attr, "minimum")) {
195 return pygi_gint64_to_py (G_PARAM_SPEC_INT64(pspec)->minimum);
196 } else if (!strcmp(attr, "maximum")) {
197 return pygi_gint64_to_py (G_PARAM_SPEC_INT64(pspec)->maximum);
198 }
199 } else if (G_IS_PARAM_SPEC_UINT64(pspec)) {
200 if (!strcmp(attr, "default_value")) {
201 return pygi_guint64_to_py (G_PARAM_SPEC_UINT64(pspec)->default_value);
202 } else if (!strcmp(attr, "minimum")) {
203 return pygi_guint64_to_py (G_PARAM_SPEC_UINT64(pspec)->minimum);
204 } else if (!strcmp(attr, "maximum")) {
205 return pygi_guint64_to_py (G_PARAM_SPEC_UINT64(pspec)->maximum);
206 }
207 } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
208 if (!strcmp(attr, "default_value")) {
209 return PyUnicode_FromFormat(
210 "%c", G_PARAM_SPEC_UNICHAR(pspec)->default_value);
211 }
212 } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
213 if (!strcmp(attr, "default_value")) {
214 return pyg_enum_from_gtype(
215 pspec->value_type, G_PARAM_SPEC_ENUM(pspec)->default_value);
216 } else if (!strcmp(attr, "enum_class")) {
217 return pygenum_from_pspec(pspec);
218 }
219 } else if (G_IS_PARAM_SPEC_FLAGS(pspec)) {
220 if (!strcmp(attr, "default_value")) {
221 return pyg_flags_from_gtype(
222 pspec->value_type, G_PARAM_SPEC_FLAGS(pspec)->default_value);
223 } else if (!strcmp(attr, "flags_class")) {
224 return pygflags_from_pspec(pspec);
225 }
226 } else if (G_IS_PARAM_SPEC_FLOAT(pspec)) {
227 if (!strcmp(attr, "default_value")) {
228 return pygi_gfloat_to_py (G_PARAM_SPEC_FLOAT(pspec)->default_value);
229 } else if (!strcmp(attr, "minimum")) {
230 return pygi_gfloat_to_py (G_PARAM_SPEC_FLOAT(pspec)->minimum);
231 } else if (!strcmp(attr, "maximum")) {
232 return pygi_gfloat_to_py (G_PARAM_SPEC_FLOAT(pspec)->maximum);
233 } else if (!strcmp(attr, "epsilon")) {
234 return pygi_gfloat_to_py (G_PARAM_SPEC_FLOAT(pspec)->epsilon);
235 }
236 } else if (G_IS_PARAM_SPEC_DOUBLE(pspec)) {
237 if (!strcmp(attr, "default_value")) {
238 return pygi_gdouble_to_py (
239 G_PARAM_SPEC_DOUBLE(pspec)->default_value);
240 } else if (!strcmp(attr, "minimum")) {
241 return pygi_gdouble_to_py (G_PARAM_SPEC_DOUBLE(pspec)->minimum);
242 } else if (!strcmp(attr, "maximum")) {
243 return pygi_gdouble_to_py (G_PARAM_SPEC_DOUBLE(pspec)->maximum);
244 } else if (!strcmp(attr, "epsilon")) {
245 return pygi_gdouble_to_py (G_PARAM_SPEC_DOUBLE(pspec)->epsilon);
246 }
247 } else if (G_IS_PARAM_SPEC_STRING(pspec)) {
248 if (!strcmp(attr, "default_value")) {
249 return Py_BuildValue(
250 "s", G_PARAM_SPEC_STRING(pspec)->default_value);
251 } else if (!strcmp(attr, "cset_first")) {
252 return Py_BuildValue(
253 "s", G_PARAM_SPEC_STRING(pspec)->cset_first);
254 } else if (!strcmp(attr, "cset_nth")) {
255 return Py_BuildValue(
256 "s", G_PARAM_SPEC_STRING(pspec)->cset_nth);
257 } else if (!strcmp(attr, "substitutor")) {
258 return Py_BuildValue(
259 "c", G_PARAM_SPEC_STRING(pspec)->substitutor);
260 } else if (!strcmp(attr, "null_fold_if_empty")) {
261 return pygi_gboolean_to_py (
262 G_PARAM_SPEC_STRING(pspec)->null_fold_if_empty);
263 } else if (!strcmp(attr, "ensure_non_null")) {
264 return pygi_gboolean_to_py (
265 G_PARAM_SPEC_STRING(pspec)->ensure_non_null);
266 }
267 } else {
268 /* This is actually not what's exported by GObjects paramspecs,
269 * But we exported this in earlier versions, so it's better to keep it here
270 * compatibility. But don't return it in __dir__, to "hide" it.
271 */
272 if (!strcmp(attr, "default_value")) {
273 /* XXX: Raise deprecation warning */
274 Py_INCREF(Py_None);
275 return Py_None;
276 }
277 }
278
279 PyErr_SetString(PyExc_AttributeError, attr);
280 return NULL;
281 }
282
283
284 static PyObject *
pyg_param_spec_dir(PyGParamSpec * self,PyObject * dummy)285 pyg_param_spec_dir(PyGParamSpec *self, PyObject *dummy)
286 {
287 GParamSpec *pspec = pyg_param_spec_get (self);
288
289 if (G_IS_PARAM_SPEC_CHAR(pspec)) {
290 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
291 "blurb", "default_value", "flags",
292 "maximum", "minimum", "name", "nick",
293 "owner_type", "value_type");
294 } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
295 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
296 "blurb", "default_value",
297 "flags", "maximum", "minimum",
298 "name", "nick", "owner_type",
299 "value_type");
300 } else if (G_IS_PARAM_SPEC_BOOLEAN(pspec)) {
301 return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
302 "blurb", "default_value",
303 "flags", "name", "nick", "owner_type",
304 "value_type");
305 } else if (G_IS_PARAM_SPEC_INT(pspec)) {
306 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
307 "blurb", "default_value",
308 "flags", "maximum", "minimum", "name",
309 "nick", "owner_type", "value_type");
310 } else if (G_IS_PARAM_SPEC_UINT(pspec)) {
311 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
312 "blurb", "default_value",
313 "flags", "maximum", "minimum",
314 "name", "nick", "owner_type",
315 "value_type");
316 } else if (G_IS_PARAM_SPEC_LONG(pspec)) {
317 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
318 "blurb", "default_value",
319 "flags", "maximum", "minimum", "name",
320 "nick", "owner_type", "value_type");
321 } else if (G_IS_PARAM_SPEC_ULONG(pspec)) {
322 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
323 "blurb", "default_value",
324 "flags", "maximum", "minimum", "name",
325 "nick", "owner_type", "value_type");
326 } else if (G_IS_PARAM_SPEC_INT64(pspec)) {
327 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
328 "blurb", "default_value",
329 "flags", "maximum", "minimum", "name",
330 "nick", "owner_type", "value_type");
331 } else if (G_IS_PARAM_SPEC_UINT64(pspec)) {
332 return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
333 "blurb", "default_value",
334 "flags", "maximum", "minimum",
335 "name", "nick", "owner_type",
336 "value_type");
337 } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
338 return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
339 "blurb", "default_value",
340 "flags", "name", "nick", "owner_type",
341 "value_type");
342 } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
343 return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
344 "blurb", "default_value", "enum_class",
345 "flags", "name", "nick", "owner_type",
346 "value_type");
347 } else if (G_IS_PARAM_SPEC_FLAGS(pspec)) {
348 return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
349 "blurb", "default_value",
350 "flags", "flags_class", "name", "nick",
351 "owner_type", "value_type");
352 } else if (G_IS_PARAM_SPEC_FLOAT(pspec)) {
353 return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
354 "blurb", "epsilon",
355 "flags", "maximum", "minimum", "name", "nick", "owner_type",
356 "value_type",
357 "default_value");
358 } else if (G_IS_PARAM_SPEC_DOUBLE(pspec)) {
359 return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
360 "blurb", "default_value", "epsilon",
361 "flags", "maximum", "minimum", "name", "nick",
362 "owner_type", "value_type");
363 } else if (G_IS_PARAM_SPEC_STRING(pspec)) {
364 return Py_BuildValue("[ssssssssssssss]", "__doc__", "__gtype__",
365 "blurb", "cset_first", "cset_nth", "default_value",
366 "ensure_non_null", "flags", "name", "nick",
367 "null_fold_if_empty", "owner_type", "substitutor",
368 "value_type");
369 } else {
370 return Py_BuildValue("[ssssssss]", "__doc__", "__gtype__", "blurb",
371 "flags", "name", "nick",
372 "owner_type", "value_type");
373 }
374 }
375
376 static PyMethodDef pyg_param_spec_methods[] = {
377 { "__dir__", (PyCFunction)pyg_param_spec_dir, METH_NOARGS},
378 { NULL, NULL, 0}
379 };
380
381 /**
382 * pyg_param_spec_new:
383 * @pspec: a GParamSpec.
384 *
385 * Creates a wrapper for a GParamSpec.
386 *
387 * Returns: the GParamSpec wrapper.
388 */
389 PyObject *
pyg_param_spec_new(GParamSpec * pspec)390 pyg_param_spec_new(GParamSpec *pspec)
391 {
392 PyGParamSpec *self;
393
394 self = (PyGParamSpec *)PyObject_NEW(PyGParamSpec,
395 &PyGParamSpec_Type);
396 if (self == NULL)
397 return NULL;
398
399 pyg_param_spec_set (self, g_param_spec_ref (pspec));
400 return (PyObject *)self;
401 }
402
403 /**
404 * Returns 0 on success, or -1 and sets an exception.
405 */
406 int
pygi_paramspec_register_types(PyObject * d)407 pygi_paramspec_register_types(PyObject *d)
408 {
409 Py_TYPE(&PyGParamSpec_Type) = &PyType_Type;
410 PyGParamSpec_Type.tp_dealloc = (destructor)pyg_param_spec_dealloc;
411 PyGParamSpec_Type.tp_getattr = (getattrfunc)pyg_param_spec_getattr;
412 PyGParamSpec_Type.tp_richcompare = pyg_param_spec_richcompare;
413 PyGParamSpec_Type.tp_flags = Py_TPFLAGS_DEFAULT;
414 PyGParamSpec_Type.tp_repr = (reprfunc)pyg_param_spec_repr;
415 PyGParamSpec_Type.tp_hash = (hashfunc)pyg_param_spec_hash;
416 PyGParamSpec_Type.tp_methods = pyg_param_spec_methods;
417
418 if (PyType_Ready(&PyGParamSpec_Type))
419 return -1;
420 PyDict_SetItemString(d, "GParamSpec", (PyObject *)&PyGParamSpec_Type);
421
422 return 0;
423 }
424