commit 1014d633f7be0113b1a5e8bf018f53af095631b9
parent 62255b57ee74f178acf0a542b02556e532ecb982
Author: Nick Econopouly <wry@mm.st>
Date: Sun, 5 Apr 2020 01:44:29 -0400
Improve file selection filtering for adding datasets
Diffstat:
M | main.go | | | 68 | +++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 53 insertions(+), 15 deletions(-)
diff --git a/main.go b/main.go
@@ -36,6 +36,58 @@ func windowSetup() *gtk.Window {
return window
}
+func addButtonSetup(v view, filepaths *[]string, window *gtk.Window) {
+
+ var err error
+
+ // native file chooser
+ v.addDatasetDialog, err = gtk.FileChooserNativeDialogNew("open",window,gtk.FILE_CHOOSER_ACTION_OPEN,"open","cancel")
+ if err != nil {
+ log.Fatal("Can't make addDatasetDialog: ", err)
+ }
+ // user can add multiple datasets
+ v.addDatasetDialog.SetSelectMultiple(true)
+
+ // filters for acceptable filetypes to open
+ CsvFilter, err := gtk.FileFilterNew(); CsvFilter.AddPattern("*.csv"); CsvFilter.SetName("Comma Separated Values")
+ ExcelFilter, err := gtk.FileFilterNew(); ExcelFilter.AddPattern("*.xlsx"); ExcelFilter.SetName("Microsoft Excel")
+ OdsFilter, err := gtk.FileFilterNew(); OdsFilter.AddPattern("*.ods"); OdsFilter.SetName("Open Document Spreadsheet")
+ AllFilter, err := gtk.FileFilterNew(); AllFilter.AddPattern("*"); AllFilter.SetName("All File Types")
+ SpreadsheetFilter, err := gtk.FileFilterNew();
+ SpreadsheetFilter.AddPattern("*.csv");
+ SpreadsheetFilter.AddPattern("*.ods");
+ SpreadsheetFilter.AddPattern("*.xlsx");
+ SpreadsheetFilter.SetName("All Spreadsheets")
+
+ v.addDatasetDialog.AddFilter(CsvFilter)
+ v.addDatasetDialog.AddFilter(ExcelFilter)
+ v.addDatasetDialog.AddFilter(OdsFilter)
+ v.addDatasetDialog.AddFilter(AllFilter)
+ v.addDatasetDialog.AddFilter(SpreadsheetFilter)
+ v.addDatasetDialog.SetFilter(SpreadsheetFilter)
+
+ _, err = v.addButton.Connect("clicked", func() {
+ var response gtk.ResponseType
+ response = gtk.ResponseType(v.addDatasetDialog.Run())
+ list, err := v.addDatasetDialog.GetFilenames()
+ if err == nil && response == gtk.RESPONSE_ACCEPT {
+ // list is a *glib.SList returned by GetFilenames.
+ // glib.SList.Foreach iterates over items in a list
+ // and provides unsafe.Pointers to the C data. Here we
+ // can convert the C []chars to Golang strings using
+ // cgo
+ list.Foreach(func(ptr unsafe.Pointer) {
+ filename := C.GoString((*C.char)(ptr))
+ // add path to list
+ *filepaths = append(*filepaths, filename)
+ })
+ // refresh the view
+ rebuildDatasetListBox(v.datasetListBox, filepaths, window)
+ window.ShowAll()
+ }
+ })
+}
+
func rebuildDatasetListBox(list *gtk.ListBox, filenames *[]string, window *gtk.Window) {
// clear list
list.GetChildren().Foreach(func(item interface{}) {
@@ -295,21 +347,7 @@ func main() {
AllFilter.SetName("All File Types")
ExcelFilter, err := gtk.FileFilterNew()
- ExcelFilter.AddPattern(
-
-
-
-
-
-
-
-
-
-
-
-
-
- "*.xlsx")
+ ExcelFilter.AddPattern("*.xlsx")
ExcelFilter.SetName("Microsoft Excel")
// saveFilter.AddMimeType("ods")
// saveFilter.AddMimeType("xlsx")