Implemented the new dialog system on GTK+.

This commit is contained in:
Pietro Gagliardi 2014-08-26 13:54:55 -04:00
parent 8fec7176de
commit ecc02ee732
2 changed files with 31 additions and 12 deletions

View File

@ -10,11 +10,12 @@ import (
// #include "gtk_unix.h" // #include "gtk_unix.h"
// #include "modalqueue.h" // #include "modalqueue.h"
// extern void our_openfile_response_callback(GtkDialog *, gint, gpointer);
// /* because cgo doesn't like ... */ // /* because cgo doesn't like ... */
// GtkWidget *newOpenFileDialog(void) // static inline GtkWidget *newOpenFileDialog(GtkWindow *parent)
// { // {
// return gtk_file_chooser_dialog_new(NULL, /* default title */ // return gtk_file_chooser_dialog_new(NULL, /* default title */
// NULL, /* no parent window */ // parent,
// GTK_FILE_CHOOSER_ACTION_OPEN, // GTK_FILE_CHOOSER_ACTION_OPEN,
// GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, // GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
// GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, // GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
@ -22,24 +23,37 @@ import (
// } // }
import "C" import "C"
func openFile() string { func (w *window) openFile(f func(filename string)) {
widget := C.newOpenFileDialog() widget := C.newOpenFileDialog(w.window)
defer C.gtk_widget_destroy(widget) window := (*C.GtkWindow)(unsafe.Pointer(widget))
dialog := (*C.GtkDialog)(unsafe.Pointer(widget)) dialog := (*C.GtkDialog)(unsafe.Pointer(widget))
fc := (*C.GtkFileChooser)(unsafe.Pointer(widget)) fc := (*C.GtkFileChooser)(unsafe.Pointer(widget))
C.gtk_file_chooser_set_local_only(fc, C.FALSE) C.gtk_file_chooser_set_local_only(fc, C.FALSE)
C.gtk_file_chooser_set_select_multiple(fc, C.FALSE) C.gtk_file_chooser_set_select_multiple(fc, C.FALSE)
C.gtk_file_chooser_set_show_hidden(fc, C.TRUE) C.gtk_file_chooser_set_show_hidden(fc, C.TRUE)
C.beginModal() C.gtk_window_set_modal(window, C.TRUE)
response := C.gtk_dialog_run(dialog) g_signal_connect(
C.endModal() 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))
if response != C.GTK_RESPONSE_ACCEPT { if response != C.GTK_RESPONSE_ACCEPT {
return "" (*f)("")
C.gtk_widget_destroy((*C.GtkWidget)(unsafe.Pointer(dialog)))
return
} }
filename := C.gtk_file_chooser_get_filename(fc) filename := C.gtk_file_chooser_get_filename((*C.GtkFileChooser)(unsafe.Pointer(dialog)))
if filename == nil { if filename == nil {
panic("[DEBUG TODO] chosen filename NULL") panic("[DEBUG TODO] chosen filename NULL")
} }
defer C.g_free(C.gpointer(unsafe.Pointer(filename))) realfilename := fromgstr(filename)
return fromgstr(filename) C.g_free(C.gpointer(unsafe.Pointer(filename)))
C.gtk_widget_destroy((*C.GtkWidget)(unsafe.Pointer(dialog)))
(*f)(realfilename)
} }

View File

@ -18,6 +18,8 @@ type window struct {
bin *C.GtkBin bin *C.GtkBin
window *C.GtkWindow window *C.GtkWindow
group *C.GtkWindowGroup
closing *event closing *event
*container *container
@ -43,6 +45,9 @@ func newWindow(title string, width int, height int, control Control) *window {
C.gtk_window_resize(w.window, C.gint(width), C.gint(height)) C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
w.container = newContainer(control) w.container = newContainer(control)
w.container.setParent(&controlParent{w.wc}) w.container.setParent(&controlParent{w.wc})
// for dialogs; otherwise, they will be modal to all windows, not just this one
w.group = C.gtk_window_group_new()
C.gtk_window_group_add_window(w.group, w.window)
return w return w
} }