Skip to content
This repository

Support for GtkTreeSelection.GetSelectedRows #114

Open
rapropos opened this Issue October 10, 2012 · 4 comments

2 participants

rapropos mattn
rapropos

Apologies in advance for problems with this code, as I am unfamiliar with both go and GTK, but at first blush it appears to work for me, so I thought I would contribute it and see if it is useful for others. I couldn't find any way to retrieve multiple selections from a GtkTreeSelection, so I tried to write a wrapper for gtk_tree_selection_get_selected_rows. Callers should call FreeTreePaths on the returned slice once finished with it. I also added a GValue.Unset method that I found necessary to avoid either leaking memory or getting warnings about reinitializing GValues when in a loop. Hope some of this helps somebody.

--- /usr/local/src/stock/github.com/mattn/gtk-go/gtk/gtk.go     2012-10-10 12:36:32.246574165 -0700
+++ /opt/go/src/pkg/github.com/mattn/go-gtk/gtk/gtk.go  2012-10-10 13:05:30.478061126 -0700
@@ -4408,7 +4408,7 @@

 // gtk_tree_model_get_column_type
 func (v *GtkTreeModel) GetIter(iter *GtkTreeIter, path *GtkTreePath) bool {
-       return gboolean2bool(C._gtk_tree_model_get_iter(v.TreeModel, &iter.TreeIter, unsafe.Pointer(&path.TreePath)))
+       return gboolean2bool(C._gtk_tree_model_get_iter(v.TreeModel, &iter.TreeIter, unsafe.Pointer(path.TreePath)))
 }
 func (v *GtkTreeModel) GetIterFromString(iter *GtkTreeIter, path_string string) bool {
        ptr := C.CString(path_string)
@@ -4498,6 +4498,23 @@
 //gtk_tree_selection_selected_foreach (GtkTreeSelection *selection, GtkTreeSelectionForeachFunc func, gpointer data);
 //gtk_tree_selection_get_selected_rows (GtkTreeSelection *selection, GtkTreeModel **model);

+func (v *GtkTreeSelection) GetSelectedRows() []GtkTreePath {
+       glist := C.gtk_tree_selection_get_selected_rows( v.TreeSelection, nil )
+       nrows := v.CountSelectedRows()
+       rv := make( []GtkTreePath, nrows )
+       for lelem, i := glist, 0; i < nrows; lelem, i = lelem.next, i+1 {
+               rv[i] = GtkTreePath{C.to_GtkTreePath(lelem.data)}
+       }
+       C.g_list_free( glist )
+       return rv
+}
+
+func FreeTreePaths( paths []GtkTreePath ) {
+       for _, path := range paths {
+               path.Free()
+       }
+}
+
 func (v *GtkTreeSelection) CountSelectedRows() int {
        return int(C.gtk_tree_selection_count_selected_rows(v.TreeSelection))
 }
--- /usr/local/src/stock/github.com/mattn/gtk-go/glib/glib.go   2012-10-10 12:34:04.124055659 -0700
+++ /opt/go/src/pkg/github.com/mattn/go-gtk/glib/glib.go    2012-10-10 12:48:00.637100728 -0700
@@ -691,6 +691,10 @@
    return int(C.g_value_get_int(&v.Value))
 }

+func (v *GValue) Unset() {
+   C.g_value_unset( &v.Value )
+}
+
 //-----------------------------------------------------------------------
 // WrappedObject
 //-----------------------------------------------------------------------
@@ -872,3 +876,4 @@
    ctx.sfi = unsafe.Pointer(C._g_timeout_add(C.guint(interval), C.int(len(sourcefunc_contexts))))
    sourcefunc_contexts = append(sourcefunc_contexts, ctx)
 }
+
rapropos

Bah, forgot a line when porting to the most recent version:

--- /usr/local/src/stock/github.com/mattn/gtk-go/gtk/gtk.go     2012-10-10 12:36:32.246574165 -0700
+++ /opt/go/src/pkg/github.com/mattn/go-gtk/gtk/gtk.go  2012-10-10 13:21:20.581640498 -0700
@@ -778,6 +778,7 @@
 static GtkFixed* to_GtkFixed(GtkWidget* w) { return GTK_FIXED(w); }
 static GtkCheckMenuItem* to_GtkCheckMenuItem(GtkWidget* w) { return GTK_CHECK_MENU_ITEM(w); }
 static GtkFileFilter* to_GtkFileFilter(gpointer p) { return GTK_FILE_FILTER(p); }
+static GtkTreePath* to_GtkTreePath(gpointer p) { return (GtkTreePath *)p; }
 */
 // #cgo pkg-config: gtk+-2.0
 import "C"
mattn
Owner

This change does not effect to applications that we written?

mattn
Owner

same to #113 ?

rapropos

I believe this issue should not affect existing applications. However, #113 would affect any existing application using GtkTreeModel.GetIter (and the patch in #113 is the first hunk of the patch here). If GetIter is correct as written, then I need to understand why it doesn't work for me, because unless I make the change in #113, I get incorrect results with GetIter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.