andlabs-ui/prev/table_windows.go

205 lines
5.7 KiB
Go
Raw Normal View History

// 28 july 2014
package ui
2015-02-19 22:52:18 -06:00
// TODO
// - why are we not getting keyboard input (focus?) on a mouse click?
// - are we getting keyboard input (focus?) on tab?
// - random freezes on Windows 7 when resizing headers or clicking new rows; likely another package ui infrastructure issue though...
// - investigate japanese display in the table headers on wine (it has worked in the past in ANSI mode via Shift-JIS with what I assume is the same font, so huh?)
2015-02-19 22:52:18 -06:00
import (
"fmt"
"reflect"
"unsafe"
2015-02-17 20:29:41 -06:00
"sync"
"image"
)
// #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)
free 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))
2015-02-18 02:45:50 -06:00
// TODO listview didn't need this; someone mentioned (TODO) it uses the small caption font???
C.controlSetControlFont(t.hwnd)
for i := 0; i < ty.NumField(); i++ {
2015-02-17 20:29:41 -06:00
coltype := C.WPARAM(C.tableColumnText)
switch {
2015-02-17 20:29:41 -06:00
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 := ty.Field(i).Tag.Get("uicolumn")
if colname == "" {
colname = ty.Field(i).Name
}
ccolname := toUTF16(colname)
C.SendMessageW(t.hwnd, C.tableAddColumn, coltype, C.LPARAM(uintptr(unsafe.Pointer(ccolname))))
// TODO free ccolname
}
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()
C.gotableSetRowCount(t.hwnd, C.intptr_t(reflect.Indirect(reflect.ValueOf(t.data)).Len()))
})
}()
}
func (t *table) Selected() int {
t.RLock()
defer t.RUnlock()
return int(C.tableSelectedItem(t.hwnd))
}
func (t *table) Select(index int) {
t.RLock()
defer t.RUnlock()
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))
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.Rect.Dx()), C.intptr_t(i.Rect.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()
return C.LRESULT(bitmap)
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(unsafe.Pointer(uintptr(data)))
2015-02-17 20:29:41 -06:00
} 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()
}