2014-07-29 02:11:21 -05:00
|
|
|
// +build !windows,!darwin
|
|
|
|
|
2014-07-29 00:06:56 -05:00
|
|
|
// 29 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
2014-07-29 02:07:11 -05:00
|
|
|
"fmt"
|
2014-07-29 00:06:56 -05:00
|
|
|
"reflect"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "gtk_unix.h"
|
2014-08-17 06:23:18 -05:00
|
|
|
// extern void goTableModel_toggled(GtkCellRendererToggle *, gchar *, gpointer);
|
2014-07-29 00:06:56 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type table struct {
|
|
|
|
*tablebase
|
|
|
|
|
2014-08-03 15:28:21 -05:00
|
|
|
_widget *C.GtkWidget
|
2014-07-29 00:06:56 -05:00
|
|
|
treeview *C.GtkTreeView
|
2014-08-03 15:28:21 -05:00
|
|
|
scroller *scroller
|
2014-07-29 00:06:56 -05:00
|
|
|
|
2014-07-29 02:07:11 -05:00
|
|
|
model *C.goTableModel
|
|
|
|
modelgtk *C.GtkTreeModel
|
2014-08-18 01:58:30 -05:00
|
|
|
selection *C.GtkTreeSelection
|
2014-07-29 02:07:11 -05:00
|
|
|
|
2014-08-16 19:48:35 -05:00
|
|
|
pixbufs []*C.GdkPixbuf
|
|
|
|
|
2014-07-29 02:07:11 -05:00
|
|
|
// stuff required by GtkTreeModel
|
|
|
|
nColumns C.gint
|
2014-07-29 02:43:01 -05:00
|
|
|
old C.gint
|
2014-08-16 19:48:35 -05:00
|
|
|
types []C.GType
|
2014-08-17 06:23:18 -05:00
|
|
|
crtocol map[*C.GtkCellRendererToggle]int
|
2014-07-29 00:06:56 -05:00
|
|
|
}
|
|
|
|
|
2014-08-16 19:48:35 -05:00
|
|
|
var (
|
|
|
|
attribText = togstr("text")
|
|
|
|
attribPixbuf = togstr("pixbuf")
|
2014-08-17 06:23:18 -05:00
|
|
|
attribActive = togstr("active")
|
2014-08-16 19:48:35 -05:00
|
|
|
)
|
|
|
|
|
2014-07-29 00:06:56 -05:00
|
|
|
func finishNewTable(b *tablebase, ty reflect.Type) Table {
|
|
|
|
widget := C.gtk_tree_view_new()
|
|
|
|
t := &table{
|
2014-08-08 19:31:15 -05:00
|
|
|
scroller: newScroller(widget, true, true), // natively scrollable; has a border
|
2014-07-30 16:32:10 -05:00
|
|
|
tablebase: b,
|
2014-08-03 15:28:21 -05:00
|
|
|
_widget: widget,
|
2014-07-30 16:32:10 -05:00
|
|
|
treeview: (*C.GtkTreeView)(unsafe.Pointer(widget)),
|
2014-08-17 06:23:18 -05:00
|
|
|
crtocol: make(map[*C.GtkCellRendererToggle]int),
|
2014-07-30 16:32:10 -05:00
|
|
|
}
|
2014-07-29 02:07:11 -05:00
|
|
|
model := C.newTableModel(unsafe.Pointer(t))
|
|
|
|
t.model = model
|
|
|
|
t.modelgtk = (*C.GtkTreeModel)(unsafe.Pointer(model))
|
2014-08-18 01:58:30 -05:00
|
|
|
t.selection = C.gtk_tree_view_get_selection(t.treeview)
|
2014-07-29 02:07:11 -05:00
|
|
|
C.gtk_tree_view_set_model(t.treeview, t.modelgtk)
|
2014-07-29 00:06:56 -05:00
|
|
|
for i := 0; i < ty.NumField(); i++ {
|
|
|
|
cname := togstr(ty.Field(i).Name)
|
2014-08-17 06:23:18 -05:00
|
|
|
switch {
|
|
|
|
case ty.Field(i).Type == reflect.TypeOf(ImageIndex(0)):
|
2014-08-16 19:48:35 -05:00
|
|
|
// can't use GDK_TYPE_PIXBUF here because it's a macro that expands to a function and cgo hates that
|
|
|
|
t.types = append(t.types, C.gdk_pixbuf_get_type())
|
|
|
|
C.tableAppendColumn(t.treeview, C.gint(i), cname,
|
|
|
|
C.gtk_cell_renderer_pixbuf_new(), attribPixbuf)
|
2014-08-17 06:23:18 -05:00
|
|
|
case ty.Field(i).Type.Kind() == reflect.Bool:
|
|
|
|
t.types = append(t.types, C.G_TYPE_BOOLEAN)
|
|
|
|
cr := C.gtk_cell_renderer_toggle_new()
|
|
|
|
crt := (*C.GtkCellRendererToggle)(unsafe.Pointer(cr))
|
|
|
|
t.crtocol[crt] = i
|
|
|
|
g_signal_connect(C.gpointer(unsafe.Pointer(cr)),
|
|
|
|
"toggled",
|
|
|
|
C.GCallback(C.goTableModel_toggled),
|
|
|
|
C.gpointer(unsafe.Pointer(t)))
|
|
|
|
C.tableAppendColumn(t.treeview, C.gint(i), cname,
|
|
|
|
cr, attribActive)
|
2014-08-16 19:48:35 -05:00
|
|
|
default:
|
|
|
|
t.types = append(t.types, C.G_TYPE_STRING)
|
|
|
|
C.tableAppendColumn(t.treeview, C.gint(i), cname,
|
|
|
|
C.gtk_cell_renderer_text_new(), attribText)
|
|
|
|
}
|
2014-07-29 00:06:56 -05:00
|
|
|
freegstr(cname) // free now (not deferred) to conserve memory
|
|
|
|
}
|
2014-07-29 02:07:11 -05:00
|
|
|
// and for some GtkTreeModel boilerplate
|
|
|
|
t.nColumns = C.gint(ty.NumField())
|
2014-07-29 00:06:56 -05:00
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:43:01 -05:00
|
|
|
func (t *table) Lock() {
|
|
|
|
t.tablebase.Lock()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
t.old = C.gint(d.Len())
|
|
|
|
}
|
2014-07-29 00:06:56 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
new := C.gint(d.Len())
|
|
|
|
C.tableUpdate(t.model, t.old, new)
|
|
|
|
})
|
|
|
|
}()
|
2014-07-29 00:06:56 -05:00
|
|
|
}
|
2014-07-29 02:07:11 -05:00
|
|
|
|
2014-08-16 19:48:35 -05:00
|
|
|
func (t *table) LoadImageList(i ImageList) {
|
|
|
|
i.apply(&t.pixbufs)
|
|
|
|
}
|
|
|
|
|
2014-08-18 01:58:30 -05:00
|
|
|
func (t *table) Selected() int {
|
|
|
|
var iter C.GtkTreeIter
|
|
|
|
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
if C.gtk_tree_selection_get_selected(t.selection, nil, &iter) == C.FALSE {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
path := C.gtk_tree_model_get_path(t.modelgtk, &iter)
|
|
|
|
if path == nil {
|
|
|
|
panic(fmt.Errorf("invalid iter in Table.Selected()"))
|
|
|
|
}
|
|
|
|
defer C.gtk_tree_path_free(path)
|
|
|
|
return int(*C.gtk_tree_path_get_indices(path))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) Select(index int) {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
C.gtk_tree_selection_unselect_all(t.selection)
|
|
|
|
if index == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
path := C.gtk_tree_path_new()
|
|
|
|
defer C.gtk_tree_path_free(path)
|
|
|
|
C.gtk_tree_path_append_index(path, C.gint(index))
|
|
|
|
C.gtk_tree_selection_select_path(t.selection, path)
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:07:11 -05:00
|
|
|
//export goTableModel_get_n_columns
|
|
|
|
func goTableModel_get_n_columns(model *C.GtkTreeModel) C.gint {
|
|
|
|
tm := (*C.goTableModel)(unsafe.Pointer(model))
|
|
|
|
t := (*table)(tm.gotable)
|
|
|
|
return t.nColumns
|
|
|
|
}
|
|
|
|
|
2014-08-16 19:48:35 -05:00
|
|
|
//export goTableModel_get_column_type
|
|
|
|
func goTableModel_get_column_type(model *C.GtkTreeModel, column C.gint) C.GType {
|
|
|
|
tm := (*C.goTableModel)(unsafe.Pointer(model))
|
|
|
|
t := (*table)(tm.gotable)
|
|
|
|
return t.types[column]
|
|
|
|
}
|
|
|
|
|
2014-07-29 02:07:11 -05:00
|
|
|
//export goTableModel_do_get_value
|
2014-08-16 19:48:35 -05:00
|
|
|
func goTableModel_do_get_value(data unsafe.Pointer, row C.gint, col C.gint, value *C.GValue) {
|
2014-07-29 02:07:11 -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 06:23:18 -05:00
|
|
|
switch {
|
|
|
|
case datum.Type() == reflect.TypeOf(ImageIndex(0)):
|
|
|
|
d := datum.Interface().(ImageIndex)
|
2014-08-16 19:48:35 -05:00
|
|
|
C.g_value_init(value, C.gdk_pixbuf_get_type())
|
|
|
|
C.g_value_set_object(value, C.gpointer(unsafe.Pointer(t.pixbufs[d])))
|
2014-08-17 06:23:18 -05:00
|
|
|
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))
|
2014-08-16 19:48:35 -05:00
|
|
|
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)
|
|
|
|
}
|
2014-07-29 02:07:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//export goTableModel_getRowCount
|
|
|
|
func goTableModel_getRowCount(data unsafe.Pointer) C.gint {
|
|
|
|
t := (*table)(data)
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
return C.gint(d.Len())
|
|
|
|
}
|
2014-08-03 15:28:21 -05:00
|
|
|
|
2014-08-17 06:23:18 -05:00
|
|
|
//export goTableModel_toggled
|
|
|
|
func goTableModel_toggled(cr *C.GtkCellRendererToggle, pathstr *C.gchar, data C.gpointer) {
|
|
|
|
t := (*table)(unsafe.Pointer(data))
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
path := C.gtk_tree_path_new_from_string(pathstr)
|
|
|
|
if len := C.gtk_tree_path_get_depth(path); len != 1 {
|
|
|
|
panic(fmt.Errorf("invalid path of depth %d given to goTableModel_toggled()", len))
|
|
|
|
}
|
|
|
|
// dereference return value to get our sole member
|
|
|
|
row := *C.gtk_tree_path_get_indices(path)
|
|
|
|
col := t.crtocol[cr]
|
|
|
|
d := reflect.Indirect(reflect.ValueOf(t.data))
|
|
|
|
datum := d.Index(int(row)).Field(int(col))
|
|
|
|
datum.SetBool(!datum.Bool())
|
|
|
|
}
|
|
|
|
|
2014-08-03 15:28:21 -05:00
|
|
|
func (t *table) widget() *C.GtkWidget {
|
|
|
|
return t._widget
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
return basepreferredSize(t, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) commitResize(c *allocation, d *sizing) {
|
|
|
|
t.scroller.commitResize(c, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *table) getAuxResizeInfo(d *sizing) {
|
|
|
|
// a Label to the left of a Table should be vertically aligned to the top
|
|
|
|
d.shouldVAlignTop = true
|
|
|
|
}
|