diff options
author | James Henstridge <james@daa.com.au> | 2001-09-14 17:14:10 +0000 |
---|---|---|
committer | James Henstridge <jamesh@src.gnome.org> | 2001-09-14 17:14:10 +0000 |
commit | 7c9fece492ca63668f5ce3cd64553811adf4f6c2 (patch) | |
tree | 57601b7f05526cd472e11c3f9b83d6f5b207ddd4 /gobject | |
parent | e0a5d37e65edd79ba27bdaf6e993b4e588de06c1 (diff) | |
download | pygobject-7c9fece492ca63668f5ce3cd64553811adf4f6c2.tar.gz |
add test use of add_constants.
2001-09-15 James Henstridge <james@daa.com.au>
* gtk/gtkmodule.c (init_gtk): add test use of add_constants.
* pygobject.h (_PyGObject_Functions): add new functions to header.
* gobjectmodule.c (pyg_enum_add_constants): new function for
adding constants to a module dictionary from a particular GType.
(pyg_flags_add_constants): similar for flag types.
(functions): add to export function vtable.
Diffstat (limited to 'gobject')
-rw-r--r-- | gobject/gobjectmodule.c | 70 | ||||
-rw-r--r-- | gobject/pygobject.h | 7 |
2 files changed, 77 insertions, 0 deletions
diff --git a/gobject/gobjectmodule.c b/gobject/gobjectmodule.c index bc53e680..7adbb8bc 100644 --- a/gobject/gobjectmodule.c +++ b/gobject/gobjectmodule.c @@ -2116,6 +2116,73 @@ static PyMethodDef pygobject_functions[] = { }; +/* ----------------- Constant extraction ------------------------ */ + +static void +pyg_enum_add_constants(PyObject *module, GType enum_type, + const gchar *strip_prefix) +{ + GEnumClass *eclass; + guint i, j; + gint prefix_len; + + g_return_if_fail (G_TYPE_IS_ENUM (enum_type)); + g_return_if_fail (strip_prefix != NULL); + + prefix_len = strlen(strip_prefix); + eclass = G_ENUM_CLASS(g_type_class_ref(enum_type)); + + for (i = 0; i < eclass->n_values; i++) { + gchar *name = eclass->values[i].value_name; + gint value = eclass->values[i].value; + + /* strip off prefix from value name, while keeping it a valid + * identifier */ + for (j = prefix_len; j >= 0; j--) { + if (g_ascii_isalpha(name[j])) { + name = &name[j]; + break; + } + } + PyModule_AddIntConstant(module, name, (long) value); + } + + g_type_class_unref(eclass); +} + +static void +pyg_flags_add_constants(PyObject *module, GType flags_type, + const gchar *strip_prefix) +{ + GFlagsClass *fclass; + guint i, j; + gint prefix_len; + + g_return_if_fail (G_TYPE_IS_FLAGS (flags_type)); + g_return_if_fail (strip_prefix != NULL); + + prefix_len = strlen(strip_prefix); + fclass = G_FLAGS_CLASS(g_type_class_ref(flags_type)); + + for (i = 0; i < fclass->n_values; i++) { + gchar *name = fclass->values[i].value_name; + guint value = fclass->values[i].value; + + /* strip off prefix from value name, while keeping it a valid + * identifier */ + for (j = prefix_len; j >= 0; j--) { + if (g_ascii_isalpha(name[j])) { + name = &name[j]; + break; + } + } + PyModule_AddIntConstant(module, name, (long) value); + } + + g_type_class_unref(fclass); +} + + /* ----------------- gobject module initialisation -------------- */ static struct _PyGObject_Functions functions = { @@ -2136,6 +2203,9 @@ static struct _PyGObject_Functions functions = { &PyGBoxed_Type, pyg_register_boxed, pyg_boxed_new, + + pyg_enum_add_constants, + pyg_flags_add_constants, }; DL_EXPORT(void) diff --git a/gobject/pygobject.h b/gobject/pygobject.h index 61d579fe..db8e2848 100644 --- a/gobject/pygobject.h +++ b/gobject/pygobject.h @@ -55,6 +55,11 @@ struct _PyGObject_Functions { GType boxed_type, PyExtensionClass *ec); PyObject *(* boxed_new)(GType boxed_type, gpointer boxed, gboolean copy_boxed, gboolean own_ref); + + void (* enum_add_constants)(PyObject *module, GType enum_type, + const gchar *strip_prefix); + void (* flags_add_constants)(PyObject *module, GType flags_type, + const gchar *strip_prefix); }; #ifndef _INSIDE_PYGOBJECT_ @@ -80,6 +85,8 @@ struct _PyGObject_Functions *_PyGObject_API; #define PyGBoxed_Type (*_PyGObject_API->boxed_type) #define pyg_register_boxed (_PyGObject_API->register_boxed) #define pyg_boxed_new (_PyGObject_API->boxed_new) +#define pyg_enum_add_constants (_PyGObject_API->enum_add_constants) +#define pyg_flags_add_constants (_PyGObject_API->flags_add_constants) #define init_pygobject() { \ PyObject *gobject = PyImport_ImportModule("gobject"); \ |