Implemented Table.Selected()/Select() on GTK+, more TODOs, and a quick Windows fix.
This commit is contained in:
parent
58e95f2399
commit
a8f711dfad
|
@ -44,6 +44,7 @@ type Table interface {
|
|||
// Selected and Select get and set the currently selected item in the Table.
|
||||
// Selected returns -1 if no item is selected.
|
||||
// Pass -1 to Select to deselect all items.
|
||||
// TODO bounds checking
|
||||
Selected() int
|
||||
Select(index int)
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ type table struct {
|
|||
|
||||
model *C.goTableModel
|
||||
modelgtk *C.GtkTreeModel
|
||||
selection *C.GtkTreeSelection
|
||||
|
||||
pixbufs []*C.GdkPixbuf
|
||||
|
||||
|
@ -51,6 +52,7 @@ func finishNewTable(b *tablebase, ty reflect.Type) Table {
|
|||
model := C.newTableModel(unsafe.Pointer(t))
|
||||
t.model = model
|
||||
t.modelgtk = (*C.GtkTreeModel)(unsafe.Pointer(model))
|
||||
t.selection = C.gtk_tree_view_get_selection(t.treeview)
|
||||
C.gtk_tree_view_set_model(t.treeview, t.modelgtk)
|
||||
for i := 0; i < ty.NumField(); i++ {
|
||||
cname := togstr(ty.Field(i).Name)
|
||||
|
@ -108,6 +110,35 @@ func (t *table) LoadImageList(i ImageList) {
|
|||
i.apply(&t.pixbufs)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
//export goTableModel_get_n_columns
|
||||
func goTableModel_get_n_columns(model *C.GtkTreeModel) C.gint {
|
||||
tm := (*C.goTableModel)(unsafe.Pointer(model))
|
||||
|
|
|
@ -145,6 +145,8 @@ void tableSelectItem(HWND hwnd, intptr_t index)
|
|||
if (SendMessageW(hwnd, LVM_SETITEMSTATE, (WPARAM) current, (LPARAM) (&item)) == FALSE)
|
||||
xpanic("error deselecting current Table item", GetLastError());
|
||||
}
|
||||
if (index == -1) // select nothing
|
||||
return;
|
||||
ZeroMemory(&item, sizeof (LVITEMW));
|
||||
item.mask = LVIF_STATE;
|
||||
item.state = LVIS_FOCUSED | LVIS_SELECTED;
|
||||
|
|
Loading…
Reference in New Issue