From 3f549f0428df90132b5a5ae5d0aa235f400fa13d Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Sun, 11 May 2014 06:40:54 +0300 Subject: [PATCH 1/3] add glib.Object.GetProperty and glib.Object.SetProperty --- glib/glib.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ glib/glib.go.h | 6 ++++++ 2 files changed, 57 insertions(+) diff --git a/glib/glib.go b/glib/glib.go index 212de61..1287bac 100644 --- a/glib/glib.go +++ b/glib/glib.go @@ -534,6 +534,57 @@ func (v *Object) Set(name string, value interface{}) error { return nil } +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)) + fmt.Printf("%+v\n", paramSpec) + if paramSpec == nil { + return TYPE_INVALID, errors.New("couldn't find Property") + + } + return Type(paramSpec.value_type), nil + +} + +// 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()) + fmt.Printf("property: %s, value :%+v\n", name, p) + return p.GoValue() +} + +// 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 */ From 64e1465001e8071ba05a5c03eecc072478d3cc0a Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Sun, 11 May 2014 07:59:01 +0300 Subject: [PATCH 2/3] oops! That wasn't supposed to be left behind --- glib/glib.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/glib/glib.go b/glib/glib.go index 1287bac..0e7df69 100644 --- a/glib/glib.go +++ b/glib/glib.go @@ -539,7 +539,6 @@ func (v *Object) GetPropertyType(name string) (Type, error) { defer C.free(unsafe.Pointer(cstr)) paramSpec := C.g_object_class_find_property(C._g_object_get_class(v.native()), (*C.gchar)(cstr)) - fmt.Printf("%+v\n", paramSpec) if paramSpec == nil { return TYPE_INVALID, errors.New("couldn't find Property") @@ -564,7 +563,6 @@ func (v *Object) GetProperty(name string) (interface{}, error) { return nil, errors.New("Unable to allocate value") } C.g_object_get_property(v.GObject, (*C.gchar)(cstr), p.native()) - fmt.Printf("property: %s, value :%+v\n", name, p) return p.GoValue() } From 822bda50745c4e9eff61428251aa8db7a916f7ad Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Thu, 15 May 2014 12:23:14 +0300 Subject: [PATCH 3/3] style fixes --- glib/glib.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/glib/glib.go b/glib/glib.go index 0e7df69..2c69597 100644 --- a/glib/glib.go +++ b/glib/glib.go @@ -534,6 +534,8 @@ 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)) @@ -541,13 +543,11 @@ func (v *Object) GetPropertyType(name string) (Type, error) { 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 - } -// Wrapper around g_object_get_property +// 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)) @@ -558,15 +558,14 @@ func (v *Object) GetProperty(name string) (interface{}, error) { } p, err := ValueInit(t) - if err != nil { - return nil, errors.New("Unable to allocate value") + return nil, errors.New("unable to allocate value") } C.g_object_get_property(v.GObject, (*C.gchar)(cstr), p.native()) return p.GoValue() } -// Wrapper around g_object_set_property +// 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))