From a8f711dfad63c5b1949439ac33ab4a162ab90ded Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Mon, 18 Aug 2014 02:58:30 -0400 Subject: [PATCH] Implemented Table.Selected()/Select() on GTK+, more TODOs, and a quick Windows fix. --- redo/table.go | 1 + redo/table_unix.go | 31 +++++++++++++++++++++++++++++++ redo/table_windows.c | 2 ++ 3 files changed, 34 insertions(+) diff --git a/redo/table.go b/redo/table.go index 8720c14..2b69d6a 100644 --- a/redo/table.go +++ b/redo/table.go @@ -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) } diff --git a/redo/table_unix.go b/redo/table_unix.go index f080a8f..95bafdf 100644 --- a/redo/table_unix.go +++ b/redo/table_unix.go @@ -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)) diff --git a/redo/table_windows.c b/redo/table_windows.c index a34e47a..6f0faae 100644 --- a/redo/table_windows.c +++ b/redo/table_windows.c @@ -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;