summaryrefslogtreecommitdiff
path: root/gi/pygparamspec.c
blob: 4ace96c4e32089416131aa14c38752959b668144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* -*- Mode: C; c-basic-offset: 4 -*-
 * pygtk- Python bindings for the GTK toolkit.
 * Copyright (C) 1998-2003  James Henstridge
 * Copyright (C) 2004       Johan Dahlin
 *
 *   pygenum.c: GEnum and GFlag wrappers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <pyglib.h>
#include <glib-object.h>

#include "pygenum.h"
#include "pygflags.h"
#include "pygtype.h"
#include "pygparamspec.h"

PYGLIB_DEFINE_TYPE("gobject.GParamSpec", PyGParamSpec_Type, PyGParamSpec);

static PyObject*
pyg_param_spec_richcompare(PyObject *self, PyObject *other, int op)
{
    if (Py_TYPE(self) == Py_TYPE(other) && Py_TYPE(self) == &PyGParamSpec_Type)
        return _pyglib_generic_ptr_richcompare (pyg_param_spec_get (self),
                                                pyg_param_spec_get (other),
                                                op);
    else {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }
}

static PYGLIB_Py_hash_t
pyg_param_spec_hash(PyGParamSpec *self)
{
    return PYGLIB_Py_hash_t_FromVoidPtr (pyg_param_spec_get (self));
}

static PyObject *
pyg_param_spec_repr(PyGParamSpec *self)
{
    char buf[80];

    g_snprintf(buf, sizeof(buf), "<%s '%s'>",
	       G_PARAM_SPEC_TYPE_NAME (pyg_param_spec_get (self)),
	       g_param_spec_get_name (pyg_param_spec_get (self)));
    return PYGLIB_PyUnicode_FromString(buf);
}

static void
pyg_param_spec_dealloc(PyGParamSpec *self)
{
    g_param_spec_unref (pyg_param_spec_get (self));
    PyObject_DEL(self);
}


static PyObject *
pygenum_from_pspec(GParamSpec *pspec)
{
    PyObject *pyclass;
    GParamSpecEnum *enum_pspec;
    GType enum_type;
    
    enum_pspec = G_PARAM_SPEC_ENUM(pspec);
    enum_type = G_ENUM_CLASS_TYPE(enum_pspec->enum_class);
    pyclass = (PyObject*)g_type_get_qdata(enum_type, pygenum_class_key);
    if (pyclass == NULL) {
	pyclass = pyg_enum_add(NULL, g_type_name(enum_type), NULL, enum_type);
	if (pyclass == NULL)
	    pyclass = Py_None;
    }
    
    Py_INCREF(pyclass);
    return pyclass;
}

static PyObject *
pygflags_from_pspec(GParamSpec *pspec)
{
    PyObject *pyclass;
    GParamSpecFlags *flag_pspec;
    GType flag_type;

    flag_pspec = G_PARAM_SPEC_FLAGS(pspec);
    flag_type = G_FLAGS_CLASS_TYPE(flag_pspec->flags_class);
    pyclass = (PyObject*)g_type_get_qdata(flag_type, pygflags_class_key);
    if (pyclass == NULL) {
	pyclass = pyg_flags_add(NULL, g_type_name(flag_type), NULL, flag_type);
	if (pyclass == NULL)
	    pyclass = Py_None;
    }
    Py_INCREF(pyclass);
    return pyclass;
}    

static PyObject *
pyg_param_spec_getattr(PyGParamSpec *self, const gchar *attr)
{
    GParamSpec *pspec;

    pspec = pyg_param_spec_get (self);

    /* common attributes */
    if (!strcmp(attr, "__gtype__")) {
	return pyg_type_wrapper_new(G_PARAM_SPEC_TYPE(pspec));
    } else if (!strcmp(attr, "name")) {
	return Py_BuildValue("s", g_param_spec_get_name(pspec));
    } else if (!strcmp(attr, "nick")) {
	return Py_BuildValue("s", g_param_spec_get_nick(pspec));
    } else if (!strcmp(attr, "blurb") || !strcmp(attr, "__doc__")) {
	return Py_BuildValue("s", g_param_spec_get_blurb(pspec));
    } else if (!strcmp(attr, "flags")) {
	return PYGLIB_PyLong_FromLong(pspec->flags);
    } else if (!strcmp(attr, "value_type")) {
	return pyg_type_wrapper_new(pspec->value_type);
    } else if (!strcmp(attr, "owner_type")) {
	return pyg_type_wrapper_new(pspec->owner_type);
    }

    if (G_IS_PARAM_SPEC_CHAR(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PYGLIB_PyUnicode_FromFormat(
		"%c", G_PARAM_SPEC_CHAR(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_CHAR(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_CHAR(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PYGLIB_PyUnicode_FromFormat(
		"%c", G_PARAM_SPEC_UCHAR(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_UCHAR(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_UCHAR(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_BOOLEAN(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyBool_FromLong(G_PARAM_SPEC_BOOLEAN(pspec)->default_value);
	}
    } else if (G_IS_PARAM_SPEC_INT(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PYGLIB_PyLong_FromLong(G_PARAM_SPEC_INT(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_UINT(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_UINT(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_LONG(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyLong_FromLong(G_PARAM_SPEC_LONG(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_ULONG(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyLong_FromUnsignedLong(G_PARAM_SPEC_ULONG(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_INT64(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyLong_FromLongLong(G_PARAM_SPEC_INT64(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_UINT64(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyLong_FromUnsignedLongLong(G_PARAM_SPEC_UINT64(pspec)->maximum);
	}
    } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PYGLIB_PyUnicode_FromFormat(
		"%c", G_PARAM_SPEC_UNICHAR(pspec)->default_value);
	}
    } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return pyg_enum_from_gtype(
		pspec->value_type, G_PARAM_SPEC_ENUM(pspec)->default_value);
	} else if (!strcmp(attr, "enum_class")) {
	    return pygenum_from_pspec(pspec);
	}
    } else if (G_IS_PARAM_SPEC_FLAGS(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return pyg_flags_from_gtype(
		pspec->value_type, G_PARAM_SPEC_FLAGS(pspec)->default_value);
	} else if (!strcmp(attr, "flags_class")) {
	    return pygflags_from_pspec(pspec);
	}
    } else if (G_IS_PARAM_SPEC_FLOAT(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->maximum);
	} else if (!strcmp(attr, "epsilon")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_FLOAT(pspec)->epsilon);
	}
    } else if (G_IS_PARAM_SPEC_DOUBLE(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return PyFloat_FromDouble(
		G_PARAM_SPEC_DOUBLE(pspec)->default_value);
	} else if (!strcmp(attr, "minimum")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->minimum);
	} else if (!strcmp(attr, "maximum")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->maximum);
	} else if (!strcmp(attr, "epsilon")) {
	    return PyFloat_FromDouble(G_PARAM_SPEC_DOUBLE(pspec)->epsilon);
	}
    } else if (G_IS_PARAM_SPEC_STRING(pspec)) {
	if (!strcmp(attr, "default_value")) {
	    return Py_BuildValue(
		"s", G_PARAM_SPEC_STRING(pspec)->default_value);
	} else if (!strcmp(attr, "cset_first")) {
	    return Py_BuildValue(
		"s", G_PARAM_SPEC_STRING(pspec)->cset_first);
	} else if (!strcmp(attr, "cset_nth")) {
	    return Py_BuildValue(
		"s", G_PARAM_SPEC_STRING(pspec)->cset_nth);
	} else if (!strcmp(attr, "substitutor")) {
	    return Py_BuildValue(
		"c", G_PARAM_SPEC_STRING(pspec)->substitutor);
	} else if (!strcmp(attr, "null_fold_if_empty")) {
	    return PyBool_FromLong(
		G_PARAM_SPEC_STRING(pspec)->null_fold_if_empty);
	} else if (!strcmp(attr, "ensure_non_null")) {
	    return PyBool_FromLong(
		G_PARAM_SPEC_STRING(pspec)->ensure_non_null);
	}
    } else {
	/* This is actually not what's exported by GObjects paramspecs,
	 * But we exported this in earlier versions, so it's better to keep it here
	 * compatibility. But don't return it in __dir__, to "hide" it.
	 */
	if (!strcmp(attr, "default_value")) {
	    /* XXX: Raise deprecation warning */
	    Py_INCREF(Py_None);
	    return Py_None;
	}
    }

    PyErr_SetString(PyExc_AttributeError, attr);
    return NULL;
}


static PyObject *
pyg_param_spec_dir(PyGParamSpec *self, PyObject *dummy)
{
    GParamSpec *pspec = pyg_param_spec_get (self);

    if (G_IS_PARAM_SPEC_CHAR(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value", "flags",
                             "maximum", "minimum", "name", "nick",
                             "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_UCHAR(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum",
                             "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_BOOLEAN(pspec)) {
        return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_INT(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum", "name",
                             "nick", "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_UINT(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum",
                             "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_LONG(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum", "name",
                             "nick", "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_ULONG(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum", "name",
                             "nick", "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_INT64(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum", "name",
                             "nick", "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_UINT64(pspec)) {
        return Py_BuildValue("[sssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "maximum", "minimum",
                             "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_UNICHAR(pspec)) {
        return Py_BuildValue("[sssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_ENUM(pspec)) {
        return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value", "enum_class",
                             "flags", "name", "nick", "owner_type",
                             "value_type");
    } else if (G_IS_PARAM_SPEC_FLAGS(pspec)) {
        return Py_BuildValue("[ssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value",
                             "flags", "flags_class", "name", "nick",
                             "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_FLOAT(pspec)) {
        return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
                             "blurb", "epsilon",
                             "flags", "maximum", "minimum", "name", "nick", "owner_type",
                             "value_type",
                             "default_value");
    } else if (G_IS_PARAM_SPEC_DOUBLE(pspec)) {
        return Py_BuildValue("[ssssssssssss]", "__doc__", "__gtype__",
                             "blurb", "default_value", "epsilon",
                             "flags", "maximum", "minimum", "name", "nick",
                             "owner_type", "value_type");
    } else if (G_IS_PARAM_SPEC_STRING(pspec)) {
        return Py_BuildValue("[ssssssssssssss]", "__doc__", "__gtype__",
                             "blurb", "cset_first", "cset_nth", "default_value",
                             "ensure_non_null", "flags", "name", "nick",
                             "null_fold_if_empty", "owner_type", "substitutor",
                             "value_type");
    } else {
        return Py_BuildValue("[ssssssss]", "__doc__", "__gtype__", "blurb",
                             "flags", "name", "nick",
                             "owner_type", "value_type");
    }
}

static PyMethodDef pyg_param_spec_methods[] = {
    { "__dir__", (PyCFunction)pyg_param_spec_dir, METH_NOARGS},
    { NULL, NULL, 0}
};

/**
 * pyg_param_spec_new:
 * @pspec: a GParamSpec.
 *
 * Creates a wrapper for a GParamSpec.
 *
 * Returns: the GParamSpec wrapper.
 */
PyObject *
pyg_param_spec_new(GParamSpec *pspec)
{
    PyGParamSpec *self;

    self = (PyGParamSpec *)PyObject_NEW(PyGParamSpec,
					&PyGParamSpec_Type);
    if (self == NULL)
	return NULL;

    pyg_param_spec_set (self, g_param_spec_ref (pspec));
    return (PyObject *)self;
}

void
pygobject_paramspec_register_types(PyObject *d)
{
    Py_TYPE(&PyGParamSpec_Type) = &PyType_Type;
    PyGParamSpec_Type.tp_dealloc = (destructor)pyg_param_spec_dealloc;
    PyGParamSpec_Type.tp_getattr = (getattrfunc)pyg_param_spec_getattr;
    PyGParamSpec_Type.tp_richcompare = pyg_param_spec_richcompare;
    PyGParamSpec_Type.tp_flags = Py_TPFLAGS_DEFAULT;
    PyGParamSpec_Type.tp_repr = (reprfunc)pyg_param_spec_repr;
    PyGParamSpec_Type.tp_hash = (hashfunc)pyg_param_spec_hash;
    PyGParamSpec_Type.tp_methods = pyg_param_spec_methods;


    if (PyType_Ready(&PyGParamSpec_Type))
	return;
    PyDict_SetItemString(d, "GParamSpec", (PyObject *)&PyGParamSpec_Type);
}