2014-07-29 09:32:49 -05:00
|
|
|
// 29 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2014-07-29 09:54:52 -05:00
|
|
|
"fmt"
|
2014-07-29 09:32:49 -05:00
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "objc_darwin.h"
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
type table struct {
|
|
|
|
*tablebase
|
2014-08-03 19:08:25 -05:00
|
|
|
|
|
|
|
_id C.id
|
|
|
|
scroller *scroller
|
2014-08-16 20:49:43 -05:00
|
|
|
|
|
|
|
images []C.id
|
2014-08-20 20:43:28 -05:00
|
|
|
selected *event
|
2014-07-29 09:32:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func finishNewTable(b *tablebase, ty reflect.Type) Table {
|
|
|
|
id := C.newTable()
|
|
|
|
t := &table{
|
2014-08-03 19:08:25 -05:00
|
|
|
_id: id,
|
2014-08-10 13:37:34 -05:00
|
|
|
scroller: newScroller(id, true), // border on Table
|
2014-08-03 19:08:25 -05:00
|
|
|
tablebase: b,
|
2014-08-20 20:43:28 -05:00
|
|
|
selected: newEvent(),
|
2014-07-29 09:32:49 -05:00
|
|
|
}
|
2014-08-20 20:43:28 -05:00
|
|
|
// also sets the delegate
|
2014-08-03 19:08:25 -05:00
|
|
|
C.tableMakeDataSource(t._id, unsafe.Pointer(t))
|
2014-07-29 09:32:49 -05:00
|
|
|
for i := 0; i < ty.NumField(); i++ {
|
|
|
|
cname := C.CString(ty.Field(i).Name)
|
2014-08-16 20:49:43 -05:00
|
|
|
coltype := C.colTypeText
|
2014-08-17 09:13:28 -05:00
|
|
|
editable := false
|
|
|
|
switch {
|
|
|
|
case ty.Field(i).Type == reflect.TypeOf(ImageIndex(0)):
|
2014-08-16 20:49:43 -05:00
|
|
|
coltype = C.colTypeImage
|
2014-08-17 09:13:28 -05:00
|
|
|
case ty.Field(i).Type.Kind() == reflect.Bool:
|
|
|
|
coltype = C.colTypeCheckbox
|
|
|
|
editable = true
|
2014-08-16 20:49:43 -05:00
|
|
|
}
|
2014-08-17 09:13:28 -05:00
|
|
|
C.tableAppendColumn(t._id, C.intptr_t(i), cname, C.int(coltype), toBOOL(editable))
|
2014-07-29 09:32:49 -05:00
|
|
|
C.free(unsafe.Pointer(cname)) // free now (not deferred) to conserve memory
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) Unlock() {
|
|
|
|
t.unlock()
|
2014-08-11 18:38:21 -05:00
|
|
|
// there's a possibility that user actions can happen at this point, before the view is updated
|
|
|
|
// alas, this is something we have to deal with, because Unlock() can be called from any thread
|
|
|
|
go func() {
|
|
|
|
Do(func() {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
C.tableUpdate(t._id)
|
|
|
|
})
|
|
|
|
}()
|
2014-07-29 09:32:49 -05:00
|
|
|
}
|
2014-07-29 09:54:52 -05:00
|
|
|
|
2014-08-16 20:49:43 -05:00
|
|
|
func (t *table) LoadImageList(i ImageList) {
|
|
|
|
i.apply(&t.images)
|
|
|
|
}
|
|
|
|
|
2014-08-18 09:41:58 -05:00
|
|
|
func (t *table) Selected() int {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
return int(C.tableSelected(t._id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) Select(index int) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
C.tableSelect(t._id, C.intptr_t(index))
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:43:28 -05:00
|
|
|
func (t *table) OnSelected(f func()) {
|
|
|
|
t.selected.set(f)
|
|
|
|
}
|
|
|
|
|
2014-07-29 09:54:52 -05:00
|
|
|
//export goTableDataSource_getValue
|
2014-08-17 09:13:28 -05:00
|
|
|
func goTableDataSource_getValue(data unsafe.Pointer, row C.intptr_t, col C.intptr_t, outtype *C.int) unsafe.Pointer {
|
2014-07-29 09:54:52 -05:00
|
|
|
t := (*table)(data)
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
datum := d.Index(int(row)).Field(int(col))
|
2014-08-17 09:13:28 -05:00
|
|
|
switch {
|
|
|
|
case datum.Type() == reflect.TypeOf(ImageIndex(0)):
|
|
|
|
*outtype = C.colTypeImage
|
|
|
|
d := datum.Interface().(ImageIndex)
|
2014-08-16 20:49:43 -05:00
|
|
|
return unsafe.Pointer(t.images[d])
|
2014-08-17 09:13:28 -05:00
|
|
|
case datum.Kind() == reflect.Bool:
|
|
|
|
*outtype = C.colTypeCheckbox
|
|
|
|
if datum.Bool() == true {
|
|
|
|
// return a non-nil pointer
|
|
|
|
// outtype isn't Go-side so it'll work
|
|
|
|
return unsafe.Pointer(outtype)
|
|
|
|
}
|
|
|
|
return nil
|
2014-08-16 20:49:43 -05:00
|
|
|
default:
|
|
|
|
s := fmt.Sprintf("%v", datum)
|
|
|
|
return unsafe.Pointer(C.CString(s))
|
|
|
|
}
|
2014-07-29 09:54:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export goTableDataSource_getRowCount
|
|
|
|
func goTableDataSource_getRowCount(data unsafe.Pointer) C.intptr_t {
|
|
|
|
t := (*table)(data)
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
return C.intptr_t(d.Len())
|
|
|
|
}
|
2014-08-03 19:08:25 -05:00
|
|
|
|
2014-08-17 09:13:28 -05:00
|
|
|
//export goTableDataSource_toggled
|
|
|
|
func goTableDataSource_toggled(data unsafe.Pointer, row C.intptr_t, col C.intptr_t, checked C.BOOL) {
|
|
|
|
t := (*table)(data)
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
datum := d.Index(int(row)).Field(int(col))
|
|
|
|
datum.SetBool(fromBOOL(checked))
|
|
|
|
}
|
|
|
|
|
2014-08-20 20:43:28 -05:00
|
|
|
//export tableSelectionChanged
|
|
|
|
func tableSelectionChanged(data unsafe.Pointer) {
|
|
|
|
t := (*table)(data)
|
|
|
|
t.selected.fire()
|
|
|
|
}
|
|
|
|
|
2014-08-03 19:08:25 -05:00
|
|
|
func (t *table) id() C.id {
|
|
|
|
return t._id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) setParent(p *controlParent) {
|
|
|
|
t.scroller.setParent(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
|
|
|
return baseallocate(t, x, y, width, height, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) preferredSize(d *sizing) (width, height int) {
|
2014-08-10 14:56:59 -05:00
|
|
|
s := C.tablePreferredSize(t._id)
|
|
|
|
return int(s.width), int(s.height)
|
2014-08-03 19:08:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) commitResize(c *allocation, d *sizing) {
|
|
|
|
t.scroller.commitResize(c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) getAuxResizeInfo(d *sizing) {
|
|
|
|
basegetAuxResizeInfo(t, d)
|
|
|
|
}
|