summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-02-16 17:09:58 -0500
committerPietro Gagliardi <[email protected]>2014-02-16 17:09:58 -0500
commit1bcbce414287a9eecfd291522f4a8e2dd0c28102 (patch)
tree865fe3950f4c37545748f1f6429bea5f951689bc
parentf5652737ebbd44310a7a19815c88f7f83d67a3a9 (diff)
Cleaned up the idle callback code.
-rw-r--r--callbacks_unix.go13
-rw-r--r--gtkcalls_unix.go22
-rw-r--r--todo.md1
-rw-r--r--uitask_unix.go17
4 files changed, 26 insertions, 27 deletions
diff --git a/callbacks_unix.go b/callbacks_unix.go
index d47d67d..dd29399 100644
--- a/callbacks_unix.go
+++ b/callbacks_unix.go
@@ -9,19 +9,28 @@ import (
/*
cgo doesn't support calling Go functions by default; we have to mark them for export. Not a problem, except arguments to GTK+ callbacks depend on the callback itself. Since we're generating callback functions as simple closures of one type, this file will wrap the generated callbacks in the appropriate callback type. We pass the actual generated pointer to the extra data parameter of the callback.
+
+while we're at it the callback for our idle function will be handled here too for cleanliness purposes
*/
// #cgo pkg-config: gtk+-3.0
// #include <gtk/gtk.h>
+// extern gboolean our_callback(gpointer);
// extern gboolean our_delete_event_callback(GtkWidget *, GdkEvent *, gpointer);
import "C"
-//export our_delete_event_callback
-func our_delete_event_callback(widget *C.GtkWidget, event *C.GdkEvent, what C.gpointer) C.gboolean {
+//export our_callback
+func our_callback(what C.gpointer) C.gboolean {
f := *(*func() bool)(unsafe.Pointer(what))
return togbool(f())
}
+//export our_delete_event_callback
+func our_delete_event_callback(widget *C.GtkWidget, event *C.GdkEvent, what C.gpointer) C.gboolean {
+ return our_callback(what)
+}
+
var callbacks = map[string]C.GCallback{
+ "idle": C.GCallback(C.our_callback),
"delete-event": C.GCallback(C.our_delete_event_callback),
}
diff --git a/gtkcalls_unix.go b/gtkcalls_unix.go
index 84e027f..0f4f818 100644
--- a/gtkcalls_unix.go
+++ b/gtkcalls_unix.go
@@ -12,9 +12,7 @@ import (
// #include <stdlib.h>
// #include <gtk/gtk.h>
// /* because cgo is flaky with macros */
-// static inline void gSignalConnect(GtkWidget *widget, char *signal, GCallback callback, void *data) { g_signal_connect(widget, signal, callback, data); }
-// /* so we can call uistep */
-// extern gboolean our_thread_callback(gpointer);
+// void gSignalConnect(GtkWidget *widget, char *signal, GCallback callback, void *data) { g_signal_connect(widget, signal, callback, data); }
import "C"
type (
@@ -32,21 +30,13 @@ func togbool(b bool) C.gboolean {
return C.FALSE
}
-//export our_thread_callback
-func our_thread_callback(C.gpointer) C.gboolean {
- uistep()
- return C.TRUE
-}
-
func gtk_init() bool {
// TODO allow GTK+ standard command-line argument processing
- b := fromgbool(C.gtk_init_check((*C.int)(nil), (***C.char)(nil)))
- if !b {
- return false
- }
- // thanks to tristan in irc.gimp.net/#gtk
- C.gdk_threads_add_idle(C.GSourceFunc(C.our_thread_callback), C.gpointer(unsafe.Pointer(nil)))
- return true
+ return fromgbool(C.gtk_init_check((*C.int)(nil), (***C.char)(nil)))
+}
+
+func gdk_threads_add_idle(what func() bool) {
+ C.gdk_threads_add_idle(callbacks["idle"], C.gpointer(unsafe.Pointer(&what)))
}
func gtk_main() {
diff --git a/todo.md b/todo.md
index fed8045..1c54382 100644
--- a/todo.md
+++ b/todo.md
@@ -20,7 +20,6 @@ so I don't forget:
- have Combobox.InsertBefore, Listbox.InsertBefore, Combobox.Delete, and Listbox.Delete return an error on invalid index before creation
important things:
-- the C.gdk_threads_add_idle logic is messy and will break when I implement actual connections...
- there's no GTK+ error handling whatsoever; we need to figure out how it works
- make sure GTK+ documentation point differences don't matter
diff --git a/uitask_unix.go b/uitask_unix.go
index 0c0152f..67d815e 100644
--- a/uitask_unix.go
+++ b/uitask_unix.go
@@ -21,17 +21,18 @@ func ui(initDone chan error) {
}
initDone <- nil
+ // thanks to tristan in irc.gimp.net/#gtk
+ gdk_threads_add_idle(func() bool {
+ select {
+ case f := <-uitask:
+ f()
+ default: // do not block
+ }
+ return true // don't destroy the callback
+ })
gtk_main()
}
-func uistep() {
- select {
- case f := <-uitask:
- f()
- default: // do not block
- }
-}
-
// temporary
func MsgBox(string, string, ...interface{}) {}
func MsgBoxError(title string, text string, args ...interface{}) {panic(title+"\n"+fmt.Sprintf(text,args...))}