Fixed most compilation errors and added GTK+ string helper functions to common_unix.go.

This commit is contained in:
Pietro Gagliardi 2014-07-07 22:46:23 -04:00
parent dccf548ffa
commit d874148760
6 changed files with 47 additions and 23 deletions

22
redo/common_unix.go Normal file
View File

@ -0,0 +1,22 @@
// 7 july 2014
package ui
import (
"unsafe"
)
// #include "gtk_unix.h"
import "C"
func fromgstr(s *C.gchar) string {
return C.GoString((*C.char)(unsafe.Pointer(s)))
}
func togstr(s string) *C.gchar {
return (*C.gchar)(unsafe.Pointer(C.CString(s)))
}
func freegstr(s *C.gchar) {
C.free(unsafe.Pointer(s))
}

View File

@ -32,6 +32,7 @@ func NewButton(text string) *Request {
// Example:
// b := ui.GetNewButton(ui.Do, "OK")
func GetNewButton(c Doer, text string) Button {
c <- newButton(text)
return (<-c.resp).(Button)
req := newButton(text)
c <- req
return (<-req.resp).(Button)
}

View File

@ -13,8 +13,8 @@ type widgetbase struct {
widget *C.GtkWidget
}
func newWidget(w *C.GtkWidget) *widget {
return &widget{
func newWidget(w *C.GtkWidget) *widgetbase {
return &widgetbase{
widget: w,
}
}
@ -32,7 +32,7 @@ func newButton(text string) *Request {
defer freegstr(ctext)
widget := C.gtk_button_new_with_label(ctext)
c <- &button{
widget: newWidget(widget),
widgetbase: newWidget(widget),
button: (*C.GtkButton)(unsafe.Pointer(widget)),
}
},
@ -40,7 +40,7 @@ func newButton(text string) *Request {
}
}
func (b *Button) OnClicked(func e(c Doer)) *Request {
func (b *button) OnClicked(e func(c Doer)) *Request {
// TODO
return nil
}

View File

@ -12,8 +12,8 @@ import (
import "C"
func uiinit() error {
// TODO replace with the eerror-checking version
C.gtk_init()
// TODO replace with the error-checking version
C.gtk_init(nil, nil)
return nil
}
@ -29,6 +29,6 @@ func issue(req *Request) {
func doissue(data C.gpointer) C.gboolean {
req := (*Request)(unsafe.Pointer(data))
req.op()
close(req.done)
close(req.resp)
return C.FALSE // don't repeat
}

View File

@ -41,6 +41,7 @@ func NewWindow(title string, width int, height int) *Request {
// Example:
// w := ui.GetNewWindow(ui.Do, "Main Window")
func GetNewWindow(c Doer, title string, width int, height int) Window {
c <- newWindow(title, width, height)
return (<-c.resp).(Window)
req := newWindow(title, width, height)
c <- req
return (<-req.resp).(Window)
}

View File

@ -21,15 +21,15 @@ func newWindow(title string, width int, height int) *Request {
return &Request{
op: func() {
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
ctext := togstr(text)
defer freegstr(ctext)
ctitle := togstr(title)
defer freegstr(ctitle)
w := &window{
widget: widget,
container: (*C.GtkContainer)(unsafe.Pointer(widget)),
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
}
C.gtk_window_set_title(w.window, ctext)
C.gtk_window_set_title(w.window, ctitle)
// TODO size
// TODO content
c <- w
@ -38,7 +38,7 @@ func newWindow(title string, width int, height int) *Request {
}
}
func (w *window) SetControl(c Control) *Request {
func (w *window) SetControl(control Control) *Request {
c := make(chan interface{})
return &Request{
op: func() {
@ -46,7 +46,7 @@ func (w *window) SetControl(c Control) *Request {
// TODO reparent
c <- struct{}{}
},
done: c,
resp: c,
}
}
@ -64,9 +64,9 @@ func (w *window) SetTitle(title string) *Request {
c := make(chan interface{})
return &Request{
op: func() {
ctext := togstr(text)
defer freegstr(ctext)
C.gtk_window_set_title(w.window, ctext)
ctitle := togstr(title)
defer freegstr(ctitle)
C.gtk_window_set_title(w.window, ctitle)
c <- struct{}{}
},
resp: c,
@ -107,7 +107,7 @@ func (w *window) Close() *Request {
}
}
func (w *window) OnClosing(func e(c Doer) bool) *Request {
func (w *window) OnClosing(e func(c Doer) bool) *Request {
// TODO
return nil
}