Esempio n. 1
0
//export closureMarshal
func closureMarshal(closure *C.GClosure, ret *C.GValue, nParams C.guint, params *C.GValue, hint, data C.gpointer) {
	// callback value
	f := *((*interface{})(unsafe.Pointer(data)))
	fValue := reflect.ValueOf(f)
	fType := fValue.Type()

	// convert GValue to reflect.Value
	var paramSlice []C.GValue
	h := (*reflect.SliceHeader)(unsafe.Pointer(&paramSlice))
	h.Len = int(nParams)
	h.Cap = h.Len
	h.Data = uintptr(unsafe.Pointer(params))
	var arguments []reflect.Value
	for i, gv := range paramSlice {
		if i == fType.NumIn() {
			break
		}
		arguments = append(arguments, reflect.ValueOf(fromGValue(&gv)))
	}

	// call
	r := fValue.Call(arguments[:fType.NumIn()])

	// return
	if len(r) > 0 {
		switch r[0].Type().Kind() {
		case reflect.Ptr:
			C.g_value_set_object(ret, (C.gpointer)(unsafe.Pointer(r[0].Pointer())))
		default:
			panic(fmt.Sprintf("unknown return type %v", r[0].Type()))
		}
	}

}
Esempio n. 2
0
func CreateCGValue(tn GType, object ...unsafe.Pointer) *GValue {
	var cv C.GValue
	C.g_value_init(&cv, C.GType(tn))

	// If no data, then return Gvalue initialized with default
	if len(object) == 0 {
		gv := GValue{tn, &cv}
		return &gv
	}
	obj := object[0]

	// Foundamental types are special
	// TODO: Handle more cases, like creating GValue from GdkEvents
	switch tn {
	case G_TYPE_STRING:
		C.g_value_take_string(&cv, (*C.gchar)(obj))
	case G_TYPE_BOOLEAN:
		C.g_value_set_boolean(&cv, *((*C.gboolean)(obj)))
	case G_TYPE_CHAR:
		C.g_value_set_char(&cv, *((*C.gchar)(obj)))
	case G_TYPE_INT:
		C.g_value_set_int(&cv, *((*C.gint)(obj)))
	case G_TYPE_LONG:
		C.g_value_set_long(&cv, *((*C.glong)(obj)))
	case G_TYPE_INT64:
		C.g_value_set_int64(&cv, *((*C.gint64)(obj)))
	case G_TYPE_UCHAR:
		C.g_value_set_uchar(&cv, *((*C.guchar)(obj)))
	case G_TYPE_UINT:
		C.g_value_set_uint(&cv, *((*C.guint)(obj)))
	case G_TYPE_ULONG:
		C.g_value_set_ulong(&cv, *((*C.gulong)(obj)))
	case G_TYPE_UINT64:
		C.g_value_set_uint64(&cv, *((*C.guint64)(obj)))
	case G_TYPE_FLOAT:
		C.g_value_set_float(&cv, *((*C.gfloat)(obj)))
	case G_TYPE_DOUBLE:
		C.g_value_set_double(&cv, *((*C.gdouble)(obj)))
	default:
		C.g_value_set_object(&cv, C.gpointer(obj))
	}

	gv := GValue{tn, &cv}
	return &gv
}
Esempio n. 3
0
//export goTableModel_do_get_value
func goTableModel_do_get_value(data unsafe.Pointer, row C.gint, col C.gint, value *C.GValue) {
	t := (*table)(data)
	t.RLock()
	defer t.RUnlock()
	d := reflect.Indirect(reflect.ValueOf(t.data))
	datum := d.Index(int(row)).Field(int(col))
	switch {
	case datum.Type() == reflect.TypeOf(ImageIndex(0)):
		d := datum.Interface().(ImageIndex)
		C.g_value_init(value, C.gdk_pixbuf_get_type())
		C.g_value_set_object(value, C.gpointer(unsafe.Pointer(t.pixbufs[d])))
	case datum.Kind() == reflect.Bool:
		d := datum.Interface().(bool)
		C.g_value_init(value, C.G_TYPE_BOOLEAN)
		C.g_value_set_boolean(value, togbool(d))
	default:
		s := fmt.Sprintf("%v", datum)
		str := togstr(s)
		defer freegstr(str)
		C.g_value_init(value, C.G_TYPE_STRING)
		C.g_value_set_string(value, str)
	}
}
Esempio n. 4
0
func toGValue(v interface{}) *C.GValue {
	value := C.gvalue_new()
	switch reflect.TypeOf(v).Kind() {
	case reflect.String:
		C.g_value_init(value, C.G_TYPE_STRING)
		cStr := C.CString(v.(string))
		defer C.free(unsafe.Pointer(cStr))
		C.g_value_set_string(value, (*C.gchar)(unsafe.Pointer(cStr)))
	case reflect.Int:
		C.g_value_init(value, C.G_TYPE_INT)
		C.g_value_set_int(value, C.gint(v.(int)))
	case reflect.Ptr, reflect.UnsafePointer:
		p := unsafe.Pointer(reflect.ValueOf(v).Pointer())
		if IsObject(p) {
			C.g_value_init(value, C.G_TYPE_OBJECT)
			C.g_value_set_object(value, C.gpointer(p))
		} else {
			panic(fmt.Sprintf("unknown pointer type %v", v))
		}
	default:
		panic(fmt.Sprintf("unknown type %v", v)) //TODO
	}
	return value
}
Esempio n. 5
0
File: gobj.go Progetto: hwch/go-gtk
// g_value_set_object
func (this *Value) SetObject(x unsafe.Pointer) {
	C.g_value_set_object(this.asC(), C.gpointer(x))
}
Esempio n. 6
0
func (o *Object) Value() *Value {
	v := NewValue(o.Type())
	C.g_value_set_object(v.g(), o.p)
	return v
}