diff --git a/glib/glib.go b/glib/glib.go index 212de61..2c69597 100644 --- a/glib/glib.go +++ b/glib/glib.go @@ -534,6 +534,54 @@ func (v *Object) Set(name string, value interface{}) error { return nil } +// GetPropertyType returns the Type of a property of the underlying GObject. +// If the property is missing it will return TYPE_INVALID and an error. +func (v *Object) GetPropertyType(name string) (Type, error) { + cstr := C.CString(name) + defer C.free(unsafe.Pointer(cstr)) + + paramSpec := C.g_object_class_find_property(C._g_object_get_class(v.native()), (*C.gchar)(cstr)) + if paramSpec == nil { + return TYPE_INVALID, errors.New("couldn't find Property") + } + return Type(paramSpec.value_type), nil +} + +// GetProperty is a wrapper around g_object_get_property(). +func (v *Object) GetProperty(name string) (interface{}, error) { + cstr := C.CString(name) + defer C.free(unsafe.Pointer(cstr)) + + t, err := v.GetPropertyType(name) + if err != nil { + return nil, err + } + + p, err := ValueInit(t) + if err != nil { + return nil, errors.New("unable to allocate value") + } + C.g_object_get_property(v.GObject, (*C.gchar)(cstr), p.native()) + return p.GoValue() +} + +// SetProperty is a wrapper around g_object_set_property(). +func (v *Object) SetProperty(name string, value interface{}) error { + cstr := C.CString(name) + defer C.free(unsafe.Pointer(cstr)) + + if _, ok := value.(Object); ok { + value = value.(Object).GObject + } + + p, err := GValue(value) + if err != nil { + return errors.New("Unable to perform type conversion") + } + C.g_object_set_property(v.GObject, (*C.gchar)(cstr), p.native()) + return nil +} + // pointerVal attempts to return an unsafe.Pointer for value. // Not all types are understood, in which case a nil Pointer // is returned. diff --git a/glib/glib.go.h b/glib/glib.go.h index d5d5564..aaf88bb 100644 --- a/glib/glib.go.h +++ b/glib/glib.go.h @@ -92,6 +92,12 @@ _g_value_fundamental(GType type) return (G_TYPE_FUNDAMENTAL(type)); } +static GObjectClass * +_g_object_get_class (GObject *object) +{ + return (G_OBJECT_GET_CLASS(object)); +} + /* * Closure support */