2014-07-28 19:52:32 -05:00
// 28 july 2014
package ui
import (
2014-07-28 22:29:06 -05:00
"fmt"
2014-07-28 19:52:32 -05:00
"reflect"
2014-10-02 09:05:53 -05:00
"unsafe"
2014-07-28 19:52:32 -05:00
)
// #include "winapi_windows.h"
import "C"
type table struct {
* tablebase
2014-10-18 16:03:07 -05:00
* controlSingleHWND
2014-10-02 09:05:53 -05:00
noautosize bool
colcount C . int
hotrow C . int
hotcol C . int
pushedrow C . int
pushedcol C . int
selected * event
2014-10-18 16:03:07 -05:00
chainresize func ( x int , y int , width int , height int , d * sizing )
2014-07-28 19:52:32 -05:00
}
func finishNewTable ( b * tablebase , ty reflect . Type ) Table {
2014-10-18 16:03:07 -05:00
hwnd := C . newControl ( C . xWC_LISTVIEW ,
C . LVS_REPORT | C . LVS_OWNERDATA | C . LVS_NOSORTHEADER | C . LVS_SHOWSELALWAYS | C . LVS_SINGLESEL | C . WS_HSCROLL | C . WS_VSCROLL | C . WS_TABSTOP ,
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)
2014-07-28 19:52:32 -05:00
t := & table {
2014-10-18 16:03:07 -05:00
controlSingleHWND : newControlSingleHWND ( hwnd ) ,
2014-10-02 09:05:53 -05:00
tablebase : b ,
hotrow : - 1 ,
hotcol : - 1 ,
pushedrow : - 1 ,
pushedcol : - 1 ,
selected : newEvent ( ) ,
2014-07-28 19:52:32 -05:00
}
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 ) )
2014-07-28 22:47:45 -05:00
// LVS_EX_FULLROWSELECT gives us selection across the whole row, not just the leftmost column; this makes the list view work like on other platforms
// LVS_EX_SUBITEMIMAGES gives us images in subitems, which will be important when both images and checkboxes are added
2014-10-18 16:03:07 -05:00
C . tableAddExtendedStyles ( t . hwnd , C . LVS_EX_FULLROWSELECT | C . LVS_EX_SUBITEMIMAGES )
2014-08-25 10:51:38 -05:00
// this must come after the subclass because it uses one of our private messages
2014-10-18 16:03:07 -05:00
C . SendMessageW ( t . hwnd , C . msgTableMakeInitialCheckboxImageList , 0 , 0 )
2014-07-28 19:52:32 -05:00
for i := 0 ; i < ty . NumField ( ) ; i ++ {
2014-10-18 16:03:07 -05:00
C . tableAppendColumn ( t . hwnd , C . int ( i ) , toUTF16 ( ty . Field ( i ) . Name ) )
2014-07-28 19:52:32 -05:00
}
2014-08-06 09:42:26 -05:00
t . colcount = C . int ( ty . NumField ( ) )
2014-07-28 19:52:32 -05:00
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 ( )
2014-10-18 16:03:07 -05:00
C . tableUpdate ( t . hwnd , C . int ( reflect . Indirect ( reflect . ValueOf ( t . data ) ) . Len ( ) ) )
2014-08-11 18:38:21 -05:00
} )
} ( )
2014-07-28 19:52:32 -05:00
}
2014-07-28 22:29:06 -05:00
2014-08-16 16:37:21 -05:00
func ( t * table ) LoadImageList ( il ImageList ) {
2014-10-18 16:03:07 -05:00
il . apply ( t . hwnd , C . msgLoadImageList )
2014-08-16 16:37:21 -05:00
}
2014-08-18 01:22:27 -05:00
func ( t * table ) Selected ( ) int {
t . RLock ( )
defer t . RUnlock ( )
2014-10-18 16:03:07 -05:00
return int ( C . tableSelectedItem ( t . hwnd ) )
2014-08-18 01:22:27 -05:00
}
func ( t * table ) Select ( index int ) {
t . RLock ( )
defer t . RUnlock ( )
2014-10-18 16:03:07 -05:00
C . tableSelectItem ( t . hwnd , C . intptr_t ( index ) )
2014-08-18 01:22:27 -05:00
}
2014-08-20 20:21:45 -05:00
func ( t * table ) OnSelected ( f func ( ) ) {
t . selected . set ( f )
}
2014-08-16 16:37:21 -05:00
//export tableGetCell
func tableGetCell ( data unsafe . Pointer , item * C . LVITEMW ) {
2014-07-28 22:29:06 -05:00
t := ( * table ) ( data )
t . RLock ( )
defer t . RUnlock ( )
d := reflect . Indirect ( reflect . ValueOf ( t . data ) )
2014-08-16 16:37:21 -05:00
datum := d . Index ( int ( item . iItem ) ) . Field ( int ( item . iSubItem ) )
2014-08-30 10:46:13 -05:00
isText := true
2014-10-02 09:05:53 -05:00
if item . mask & C . LVIF_IMAGE != 0 {
2014-08-30 10:46:13 -05:00
if datum . Type ( ) == reflect . TypeOf ( ImageIndex ( 0 ) ) {
item . iImage = C . int ( datum . Interface ( ) . ( ImageIndex ) )
isText = false
2014-08-17 14:30:10 -05:00
}
2014-08-30 10:46:13 -05:00
// else let the default behavior work
}
2014-10-02 09:05:53 -05:00
if item . mask & C . LVIF_INDENT != 0 {
2014-08-30 10:46:13 -05:00
// always have an indent of zero
item . iIndent = 0
}
2014-10-02 09:05:53 -05:00
if item . mask & C . LVIF_STATE != 0 {
2014-08-30 10:46:13 -05:00
// start by not changing any state
item . stateMask = 0
if datum . Kind ( ) == reflect . Bool {
item . stateMask = C . LVIS_STATEIMAGEMASK
// state image index is 1-based
curstate := ( ( item . state & C . LVIS_STATEIMAGEMASK ) >> 12 )
if curstate > 0 {
curstate --
}
if datum . Bool ( ) == true {
curstate |= C . checkboxStateChecked
} else {
curstate &^= C . checkboxStateChecked
}
if item . iItem == t . hotrow && item . iSubItem == t . hotcol {
curstate |= C . checkboxStateHot
} else {
curstate &^= C . checkboxStateHot
}
if item . iItem == t . pushedrow && item . iSubItem == t . pushedcol {
curstate |= C . checkboxStatePushed
} else {
curstate &^= C . checkboxStatePushed
}
item . state = ( curstate + 1 ) << 12
isText = false
2014-08-17 16:22:47 -05:00
}
2014-08-30 10:46:13 -05:00
}
2014-10-02 09:05:53 -05:00
if item . mask & C . LVIF_TEXT != 0 {
2014-08-30 10:46:13 -05:00
if isText {
s := fmt . Sprintf ( "%v" , datum )
item . pszText = toUTF16 ( s )
2014-08-17 17:06:36 -05:00
}
2014-08-30 10:46:13 -05:00
// else let the default handler work
2014-08-16 16:37:21 -05:00
}
2014-07-28 22:29:06 -05:00
}
2014-08-03 08:18:35 -05:00
2014-08-14 08:20:20 -05:00
// 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 {
2014-10-18 16:03:07 -05:00
C . tableAutosizeColumns ( t . hwnd , t . colcount )
2014-08-14 08:20:20 -05:00
}
}
2014-08-06 09:42:26 -05:00
//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
}
2014-08-17 16:22:47 -05:00
//export tableSetHot
func tableSetHot ( data unsafe . Pointer , row C . int , col C . int ) {
t := ( * table ) ( data )
redraw := ( row != t . hotrow || col != t . hotcol )
t . hotrow = row
t . hotcol = col
if redraw {
2014-10-18 16:03:07 -05:00
C . tableUpdate ( t . hwnd , C . int ( reflect . Indirect ( reflect . ValueOf ( t . data ) ) . Len ( ) ) )
2014-08-17 16:22:47 -05:00
}
}
2014-08-17 17:06:36 -05:00
//export tablePushed
func tablePushed ( data unsafe . Pointer , row C . int , col C . int ) {
t := ( * table ) ( data )
t . pushedrow = row
t . pushedcol = col
2014-10-18 16:03:07 -05:00
C . tableUpdate ( t . hwnd , C . int ( reflect . Indirect ( reflect . ValueOf ( t . data ) ) . Len ( ) ) )
2014-08-17 17:06:36 -05:00
}
2014-08-17 16:44:33 -05:00
//export tableToggled
func tableToggled ( data unsafe . Pointer , row C . int , col C . int ) {
t := ( * table ) ( data )
2014-08-17 17:06:36 -05:00
t . Lock ( )
defer func ( ) {
// reset for next time
t . pushedrow = - 1
t . pushedcol = - 1
// and THEN unlock so the reset takes effect
t . Unlock ( )
} ( )
2014-10-02 09:05:53 -05:00
if row == - 1 || col == - 1 { // discard extras sent by handle() in table_windows.c
2014-08-17 16:44:33 -05:00
return
}
2014-10-02 09:05:53 -05:00
if row != t . pushedrow || col != t . pushedcol { // mouse moved out
2014-08-17 17:06:36 -05:00
return
}
2014-08-17 16:44:33 -05:00
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 ) )
}
2014-08-20 20:37:44 -05:00
//export tableSelectionChanged
func tableSelectionChanged ( data unsafe . Pointer ) {
2014-08-20 20:21:45 -05:00
t := ( * table ) ( data )
t . selected . fire ( )
}
2014-08-03 08:18:35 -05:00
const (
2014-08-13 15:49:30 -05:00
// 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...
2014-10-02 09:05:53 -05:00
tableWidth = 183
2014-08-03 08:18:35 -05:00
tableHeight = 50
)
2014-10-18 16:03:07 -05:00
func ( t * table ) xpreferredSize ( d * sizing ) ( width , height int ) {
2014-08-03 08:18:35 -05:00
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 )
2014-08-14 08:20:20 -05:00
t . RLock ( )
defer t . RUnlock ( )
t . autoresize ( )
2014-08-03 08:18:35 -05:00
}