Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

add glib.Object.GetProperty and glib.Object.SetProperty #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions glib/glib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment here for godoc.

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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not capitalize error strings.

}
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.
Expand Down
6 changes: 6 additions & 0 deletions glib/glib.go.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down