2014-08-19 11:44:42 -05:00
// +build !windows,!darwin
2014-08-19 09:31:12 -05:00
// 19 august 2014
package ui
import (
"unsafe"
)
// #include "gtk_unix.h"
2014-08-26 12:54:55 -05:00
// extern void our_openfile_response_callback(GtkDialog *, gint, gpointer);
2014-08-19 09:31:12 -05:00
// /* because cgo doesn't like ... */
2014-08-26 12:54:55 -05:00
// static inline GtkWidget *newOpenFileDialog(GtkWindow *parent)
2014-08-19 09:31:12 -05:00
// {
// return gtk_file_chooser_dialog_new(NULL, /* default title */
2014-08-26 12:54:55 -05:00
// parent,
2014-08-19 09:31:12 -05:00
// GTK_FILE_CHOOSER_ACTION_OPEN,
// GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
// GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
// NULL);
// }
import "C"
2014-08-26 12:54:55 -05:00
func ( w * window ) openFile ( f func ( filename string ) ) {
widget := C . newOpenFileDialog ( w . window )
window := ( * C . GtkWindow ) ( unsafe . Pointer ( widget ) )
2014-10-02 09:05:53 -05:00
dialog := ( * C . GtkDialog ) ( unsafe . Pointer ( widget ) )
2014-08-19 09:31:12 -05:00
fc := ( * C . GtkFileChooser ) ( unsafe . Pointer ( widget ) )
2014-08-31 18:07:55 -05:00
// non-local filenames are relevant mainly to GIO where we can open *anything*, not to Go os.File; see https://twitter.com/braket/status/506142849654870016
C . gtk_file_chooser_set_local_only ( fc , C . TRUE )
2014-08-19 09:31:12 -05:00
C . gtk_file_chooser_set_select_multiple ( fc , C . FALSE )
C . gtk_file_chooser_set_show_hidden ( fc , C . TRUE )
2014-08-26 12:54:55 -05:00
C . gtk_window_set_modal ( window , C . TRUE )
g_signal_connect (
C . gpointer ( unsafe . Pointer ( dialog ) ) ,
"response" ,
C . GCallback ( C . our_openfile_response_callback ) ,
C . gpointer ( unsafe . Pointer ( & f ) ) )
C . gtk_widget_show_all ( widget )
}
//export our_openfile_response_callback
func our_openfile_response_callback ( dialog * C . GtkDialog , response C . gint , data C . gpointer ) {
f := ( * func ( string ) ) ( unsafe . Pointer ( data ) )
2014-08-19 09:31:12 -05:00
if response != C . GTK_RESPONSE_ACCEPT {
2014-08-26 12:54:55 -05:00
( * f ) ( "" )
C . gtk_widget_destroy ( ( * C . GtkWidget ) ( unsafe . Pointer ( dialog ) ) )
return
2014-08-19 09:31:12 -05:00
}
2014-08-26 12:54:55 -05:00
filename := C . gtk_file_chooser_get_filename ( ( * C . GtkFileChooser ) ( unsafe . Pointer ( dialog ) ) )
2014-08-19 09:31:12 -05:00
if filename == nil {
2014-08-31 18:07:55 -05:00
panic ( "chosen filename NULL in OpenFile()" )
2014-08-19 09:31:12 -05:00
}
2014-08-26 12:54:55 -05:00
realfilename := fromgstr ( filename )
C . g_free ( C . gpointer ( unsafe . Pointer ( filename ) ) )
C . gtk_widget_destroy ( ( * C . GtkWidget ) ( unsafe . Pointer ( dialog ) ) )
( * f ) ( realfilename )
2014-08-19 09:31:12 -05:00
}