andlabs-ui/table_windows.go

193 lines
5.1 KiB
Go
Raw Normal View History

// 28 july 2014
package ui
import (
"fmt"
"reflect"
"unsafe"
2015-02-17 20:29:41 -06:00
"sync"
)
// #include "winapi_windows.h"
import "C"
type table struct {
*tablebase
2014-10-18 16:03:07 -05:00
*controlSingleHWND
noautosize bool
colcount C.int
selected *event
2014-10-18 16:03:07 -05:00
chainresize func(x int, y int, width int, height int, d *sizing)
2015-02-17 21:00:16 -06:00
freeTexts map[C.uintptr_t]bool
2015-02-17 20:29:41 -06:00
freeLock sync.Mutex
}
func finishNewTable(b *tablebase, ty reflect.Type) Table {
2015-02-17 20:29:41 -06:00
hwnd := C.newControl(C.xtableWindowClass,
C.WS_HSCROLL|C.WS_VSCROLL|C.WS_TABSTOP,
2014-10-18 16:03:07 -05:00
C.WS_EX_CLIENTEDGE) // WS_EX_CLIENTEDGE without WS_BORDER will show the canonical visual styles border (thanks to MindChild in irc.efnet.net/#winprog)
t := &table{
2014-10-18 16:03:07 -05:00
controlSingleHWND: newControlSingleHWND(hwnd),
tablebase: b,
selected: newEvent(),
2015-02-17 21:00:16 -06:00
free: make(map[C.uintptr_t]bool),
}
2014-10-18 16:03:07 -05:00
t.fpreferredSize = t.xpreferredSize
t.chainresize = t.fresize
t.fresize = t.xresize
C.setTableSubclass(t.hwnd, unsafe.Pointer(t))
for i := 0; i < ty.NumField(); i++ {
2015-02-17 20:29:41 -06:00
coltype := C.WPARAM(C.tableColumnText)
switch ty.Field(i).Type {
case ty.Field(i).Type == reflect.TypeOf((*image.RGBA)(nil)):
coltype = C.tableColumnImage
case ty.Field(i).Type.Kind() == reflect.Bool:
coltype = C.tableColumnCheckbox
}
colname := toUTF16(ty.Field(i).Name)
C.SendMessageW(t.hwnd, C.tableAddColumn, coltype, C.LPARAM(uintptr(unsafe.Pointer(colname))))
}
t.colcount = C.int(ty.NumField())
return t
}
func (t *table) Unlock() {
t.unlock()
// 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()
2015-02-17 20:29:41 -06:00
C.SendMessageW(t.hwnd, C.tableSetRowCount, 0, C.LPARAM(C.intptr_t(reflect.Indirect(reflect.ValueOf(t.data)).Len())))
})
}()
}
func (t *table) Selected() int {
t.RLock()
defer t.RUnlock()
2015-02-17 20:29:41 -06:00
//TODO return int(C.tableSelectedItem(t.hwnd))
return -1
}
func (t *table) Select(index int) {
t.RLock()
defer t.RUnlock()
2015-02-17 20:29:41 -06:00
//TODO C.tableSelectItem(t.hwnd, C.intptr_t(index))
}
func (t *table) OnSelected(f func()) {
t.selected.set(f)
}
//export tableGetCell
2015-02-17 21:00:16 -06:00
func tableGetCell(data unsafe.Pointer, tnm *C.tableNM) C.LRESULT {
t := (*table)(data)
t.RLock()
defer t.RUnlock()
d := reflect.Indirect(reflect.ValueOf(t.data))
2015-02-17 21:00:16 -06:00
datum := d.Index(int(tnm.row)).Field(int(tnm.column))
isText := true
2015-02-17 20:29:41 -06:00
switch {
case datum.Type() == reflect.TypeOf((*image.RGBA)(nil)):
i := datum.Interface().(*image.RGBA)
hbitmap := C.toBitmap(unsafe.Pointer(i), C.intptr_t(i.Dx()), C.intptr_t(i.Dy()))
2015-02-17 21:00:16 -06:00
bitmap := C.uintptr_t(uintptr(unsafe.Pointer(hbitmap)))
2015-02-17 20:29:41 -06:00
t.freeLock.Lock()
t.free[bitmap] = true // bitmap freed with C.freeBitmap()
t.freeLock.Unlock()
2015-02-17 21:00:16 -06:00
return C.LRESULT(bmp)
2015-02-17 20:29:41 -06:00
case datum.Kind() == reflect.Bool:
if datum.Bool() == true {
return C.TRUE
}
2015-02-17 20:29:41 -06:00
return C.FALSE
default:
s := fmt.Sprintf("%v", datum)
2015-02-17 21:00:16 -06:00
text := C.uintptr_t(uintptr(unsafe.Pointer(toUTF16(s))))
2015-02-17 20:29:41 -06:00
t.freeLock.Lock()
t.free[text] = false // text freed with C.free()
t.freeLock.Unlock()
2015-02-17 21:00:16 -06:00
return C.LRESULT(text)
}
2015-02-17 20:29:41 -06:00
}
2015-02-17 21:00:16 -06:00
//export tableFreeCellData
func tableFreeCellData(gotable unsafe.Pointer, data C.uintptr_t) {
2015-02-17 20:29:41 -06:00
t := (*table)(gotable)
t.freeLock.Lock()
defer t.freeLock.Unlock()
b, ok := t.free[data]
if !ok {
panic(fmt.Errorf("undefined data %p in tableFreeData()", data))
}
2015-02-17 20:29:41 -06:00
if b == false {
C.free(data)
} else {
C.freeBitmap(data)
}
2015-02-17 20:29:41 -06:00
delete(t.free, data)
}
// the column autoresize policy is simple:
// on every table.commitResize() call, if the columns have not been resized by the user, autoresize
func (t *table) autoresize() {
t.RLock()
defer t.RUnlock()
if !t.noautosize {
2015-02-17 20:29:41 -06:00
//TODO C.tableAutosizeColumns(t.hwnd, t.colcount)
}
}
//export tableStopColumnAutosize
func tableStopColumnAutosize(data unsafe.Pointer) {
t := (*table)(data)
t.noautosize = true
}
//export tableColumnCount
func tableColumnCount(data unsafe.Pointer) C.int {
t := (*table)(data)
return t.colcount
}
//export tableToggled
2015-02-17 21:00:16 -06:00
func tableToggled(data unsafe.Pointer, row C.intptr_t, col C.intptr_t) {
t := (*table)(data)
t.Lock()
2015-02-17 20:29:41 -06:00
defer t.Unlock()
d := reflect.Indirect(reflect.ValueOf(t.data))
datum := d.Index(int(row)).Field(int(col))
if datum.Kind() == reflect.Bool {
datum.SetBool(!datum.Bool())
return
}
panic(fmt.Errorf("tableSetHot() on non-checkbox at (%d, %d)", row, col))
}
//export tableSelectionChanged
func tableSelectionChanged(data unsafe.Pointer) {
t := (*table)(data)
t.selected.fire()
}
const (
// from C++ Template 05 in http://msdn.microsoft.com/en-us/library/windows/desktop/bb226818%28v=vs.85%29.aspx as this is the best I can do for now
// there IS a message LVM_APPROXIMATEVIEWRECT that can do calculations, but it doesn't seem to work right when asked to base its calculations on the current width/height on Windows and wine...
tableWidth = 183
tableHeight = 50
)
2014-10-18 16:03:07 -05:00
func (t *table) xpreferredSize(d *sizing) (width, height int) {
return fromdlgunitsX(tableWidth, d), fromdlgunitsY(tableHeight, d)
}
2014-10-18 16:03:07 -05:00
func (t *table) xresize(x int, y int, width int, height int, d *sizing) {
t.chainresize(x, y, width, height, d)
t.RLock()
defer t.RUnlock()
t.autoresize()
}