#include #include #define GTK_PARAM_WRITABLE G_PARAM_WRITABLE|G_PARAM_STATIC_NAME|G_PARAM_STATIC_NICK|G_PARAM_STATIC_BLURB #define P_(x) (x) typedef struct { GObject parent_instance; } Custom; typedef struct { GObjectClass parent_class; } CustomClass; typedef GObject * (*CustomCreationFunction) (const gchar *name, const gchar *string1, const gchar *string2, int int1, int int2); static GObject * custom_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties); static void custom_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec); #define TYPE_CUSTOM (custom_get_type ()) #define CUSTOM(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CUSTOM, Custom)) G_DEFINE_TYPE (Custom, custom, G_TYPE_OBJECT) enum { PROP_0, PROP_CREATION_FUNCTION, PROP_STRING1, PROP_STRING2, PROP_INT1, PROP_INT2, PROP_LAST_MODIFICATION_TIME, PROP_VISIBLE }; static void custom_class_init (CustomClass *klass) { GObjectClass *gobject_class; gobject_class = G_OBJECT_CLASS (klass); gobject_class->constructor = custom_constructor; gobject_class->set_property = custom_set_property; g_object_class_install_property (gobject_class, PROP_CREATION_FUNCTION, g_param_spec_string ("creation-function", P_("Creation function"), P_("This function will be called to create the object"), NULL, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_STRING1, g_param_spec_string ("string1", P_("String1"), NULL, NULL, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_STRING1, g_param_spec_string ("string2", P_("String2"), NULL, NULL, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_INT1, g_param_spec_int ("int1", P_("Int1"), NULL, -G_MAXINT, G_MAXINT, 0, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_INT2, g_param_spec_int ("int2", P_("Int2"), NULL, -G_MAXINT, G_MAXINT, 0, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_LAST_MODIFICATION_TIME, g_param_spec_string ("last-modification-time", P_("Last modification time"), NULL, NULL, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); g_object_class_install_property (gobject_class, PROP_VISIBLE, g_param_spec_boolean ("visible", P_("Visible"), NULL, FALSE, GTK_PARAM_WRITABLE|G_PARAM_CONSTRUCT_ONLY)); } static void custom_init (Custom *custom) { } static void custom_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) { /* dummy implementation, it's never called, GObject just checks if its set */ } static GObject * custom_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_properties) { GModule *module = NULL; CustomCreationFunction func; GObject *obj = NULL; const gchar *creation_function; const gchar *string1 = NULL; const gchar *string2 = NULL; int int1 = 0; int int2 = 0; int i; gboolean visible = FALSE; for (i = 0; i < n_construct_properties; i++) { GParamSpec *param = construct_properties[i].pspec; GValue * value = construct_properties[i].value; switch (param->param_id) { case PROP_CREATION_FUNCTION: creation_function = g_value_get_string (value); break; case PROP_STRING1: string1 = g_value_get_string (value); break; case PROP_STRING2: string2 = g_value_get_string (value); break; case PROP_INT1: int1 = g_value_get_int (value); break; case PROP_INT2: int2 = g_value_get_int (value); break; case PROP_LAST_MODIFICATION_TIME: break; case PROP_VISIBLE: visible = g_value_get_boolean (value); break; default: g_warning ("%s: invalid property id %u for \"%s\" of type `%s' in `%s'", G_STRLOC, param->param_id, param->name, g_type_name (G_PARAM_SPEC_TYPE (param)), g_type_name (type)); break; } } module = g_module_open (NULL, 0); if (g_module_symbol (module, creation_function, (gpointer)&func)) obj = func (creation_function, string1, string2, int1, int2); if (!obj) g_error ("Custom creation function `%s' did not return a GObject\n", creation_function); else if (GTK_IS_WIDGET (obj) && visible) gtk_widget_show (GTK_WIDGET (obj)); g_module_close (module); return obj; } #if 0 /* test */ GObject* test_foo (const gchar *name, const gchar *string1, const gchar *string2, int int1, int int2) { return G_OBJECT (gtk_window_new (GTK_WINDOW_TOPLEVEL)); } int main(int argc, char **argv) { GObject *obj; gtk_init (&argc, &argv); obj = g_object_new (TYPE_CUSTOM, "creation-function", "test_foo", "string1", "ettan", "string2", "tvÄan", "int1", 10, "int2", 42, "visible", TRUE, NULL); g_return_val_if_fail(obj != NULL, 1); g_print ("%s\n", g_type_name (G_OBJECT_TYPE (obj))); gtk_widget_destroy (GTK_WIDGET (obj)); return 0; } #endif