func tree_view_get_selected_path(tree_view *gtk.GtkTreeView, tree_model *gtk.GtkTreeModel, col int, shift bool) string { var path *gtk.GtkTreePath var column *gtk.GtkTreeViewColumn tree_view.GetCursor(&path, &column) if nil == path.TreePath { return "" } var iter gtk.GtkTreeIter tree_model.GetIterFromString(&iter, path.String()) var ans string ans = "" for { var val glib.GValue var next gtk.GtkTreeIter tree_model.GetValue(&iter, col, &val) if shift { ans = val.GetString()[1:] + ans } else { ans = val.GetString() + ans } if false == tree_model.IterParent(&next, &iter) { break } iter.Assign(&next) } return ans }
func main() { gtk.Init(&os.Args) window := gtk.Window(gtk.GTK_WINDOW_TOPLEVEL) window.SetTitle("GTK Folder View") window.Connect("destroy", gtk.MainQuit) swin := gtk.ScrolledWindow(nil, nil) store := gtk.TreeStore(gdkpixbuf.GetGdkPixbufType(), glib.G_TYPE_STRING) treeview := gtk.TreeView() swin.Add(treeview) treeview.SetModel(store.ToTreeModel()) treeview.AppendColumn(gtk.TreeViewColumnWithAttributes("pixbuf", gtk.CellRendererPixbuf(), "pixbuf", 0)) treeview.AppendColumn(gtk.TreeViewColumnWithAttributes("text", gtk.CellRendererText(), "text", 1)) for n := 1; n <= 10; n++ { var iter1, iter2, iter3 gtk.GtkTreeIter store.Append(&iter1, nil) store.Set(&iter1, gtk.Image().RenderIcon(gtk.GTK_STOCK_DIRECTORY, gtk.GTK_ICON_SIZE_SMALL_TOOLBAR, "").Pixbuf, "Folder"+strconv.Itoa(n)) store.Append(&iter2, &iter1) store.Set(&iter2, gtk.Image().RenderIcon(gtk.GTK_STOCK_DIRECTORY, gtk.GTK_ICON_SIZE_SMALL_TOOLBAR, "").Pixbuf, "SubFolder"+strconv.Itoa(n)) store.Append(&iter3, &iter2) store.Set(&iter3, gtk.Image().RenderIcon(gtk.GTK_STOCK_FILE, gtk.GTK_ICON_SIZE_SMALL_TOOLBAR, "").Pixbuf, "File"+strconv.Itoa(n)) } treeview.Connect("row_activated", func() { var path *gtk.GtkTreePath var column *gtk.GtkTreeViewColumn treeview.GetCursor(&path, &column) mes := "TreePath is: " + path.String() dialog := gtk.MessageDialog( treeview.GetTopLevelAsWindow(), gtk.GTK_DIALOG_MODAL, gtk.GTK_MESSAGE_INFO, gtk.GTK_BUTTONS_OK, mes) dialog.SetTitle("TreePath") dialog.Response(func() { dialog.Destroy() }) dialog.Run() }) window.Add(swin) window.SetSizeRequest(400, 200) window.ShowAll() gtk.Main() }