2014-07-08 15:47:28 -05:00
|
|
|
// +build !darwin
|
|
|
|
|
2014-07-07 14:30:55 -05:00
|
|
|
// 7 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
2014-07-14 21:54:57 -05:00
|
|
|
"fmt"
|
2014-07-07 14:30:55 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// #include "gtk_unix.h"
|
2014-07-08 11:04:51 -05:00
|
|
|
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
|
2014-07-14 21:54:57 -05:00
|
|
|
// extern gboolean windowResizing(GtkWidget *, GdkEvent *, gpointer);
|
2014-07-07 14:30:55 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type window struct {
|
|
|
|
widget *C.GtkWidget
|
|
|
|
container *C.GtkContainer
|
|
|
|
bin *C.GtkBin
|
|
|
|
window *C.GtkWindow
|
2014-07-08 11:04:51 -05:00
|
|
|
|
2014-07-14 15:32:45 -05:00
|
|
|
layoutc *C.GtkContainer
|
|
|
|
layout *C.GtkLayout
|
|
|
|
|
2014-07-15 19:48:16 -05:00
|
|
|
child Control
|
|
|
|
|
2014-07-08 11:04:51 -05:00
|
|
|
closing *event
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWindow(title string, width int, height int) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
|
2014-07-07 21:46:23 -05:00
|
|
|
ctitle := togstr(title)
|
|
|
|
defer freegstr(ctitle)
|
2014-07-14 15:32:45 -05:00
|
|
|
layoutw := C.gtk_layout_new(nil, nil)
|
2014-07-07 14:30:55 -05:00
|
|
|
w := &window{
|
|
|
|
widget: widget,
|
|
|
|
container: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
|
|
|
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
|
|
|
|
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
|
2014-07-14 15:32:45 -05:00
|
|
|
layoutc: (*C.GtkContainer)(unsafe.Pointer(layoutw)),
|
|
|
|
layout: (*C.GtkLayout)(unsafe.Pointer(layoutw)),
|
2014-07-08 11:04:51 -05:00
|
|
|
closing: newEvent(),
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
2014-07-07 21:46:23 -05:00
|
|
|
C.gtk_window_set_title(w.window, ctitle)
|
2014-07-08 11:04:51 -05:00
|
|
|
g_signal_connect(
|
|
|
|
C.gpointer(unsafe.Pointer(w.window)),
|
|
|
|
"delete-event",
|
|
|
|
C.GCallback(C.windowClosing),
|
|
|
|
C.gpointer(unsafe.Pointer(w)))
|
2014-07-14 21:54:57 -05:00
|
|
|
g_signal_connect(
|
|
|
|
C.gpointer(unsafe.Pointer(w.window)),
|
|
|
|
"configure-event",
|
|
|
|
C.GCallback(C.windowResizing),
|
|
|
|
C.gpointer(unsafe.Pointer(w)))
|
2014-07-07 14:30:55 -05:00
|
|
|
// TODO size
|
2014-07-14 15:32:45 -05:00
|
|
|
C.gtk_container_add(w.container, layoutw)
|
2014-07-07 21:46:23 -05:00
|
|
|
c <- w
|
2014-07-07 14:30:55 -05:00
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 21:46:23 -05:00
|
|
|
func (w *window) SetControl(control Control) *Request {
|
2014-07-07 14:30:55 -05:00
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-15 21:34:12 -05:00
|
|
|
if w.child != nil { // unparent existing control
|
|
|
|
w.child.unparent()
|
|
|
|
}
|
2014-07-15 19:48:16 -05:00
|
|
|
control.unparent()
|
|
|
|
control.parent(w)
|
|
|
|
w.child = control
|
2014-07-07 14:30:55 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
2014-07-07 21:46:23 -05:00
|
|
|
resp: c,
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Title() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
c <- fromgstr(C.gtk_window_get_title(w.window))
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetTitle(title string) *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
2014-07-07 21:46:23 -05:00
|
|
|
ctitle := togstr(title)
|
|
|
|
defer freegstr(ctitle)
|
|
|
|
C.gtk_window_set_title(w.window, ctitle)
|
2014-07-07 14:30:55 -05:00
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w *window) Show() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
C.gtk_widget_show_all(w.widget)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Hide() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
C.gtk_widget_hide(w.widget)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) Close() *Request {
|
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
C.gtk_widget_destroy(w.widget)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-07 21:46:23 -05:00
|
|
|
func (w *window) OnClosing(e func(c Doer) bool) *Request {
|
2014-07-08 11:04:51 -05:00
|
|
|
c := make(chan interface{})
|
|
|
|
return &Request{
|
|
|
|
op: func() {
|
|
|
|
w.closing.setbool(e)
|
|
|
|
c <- struct{}{}
|
|
|
|
},
|
|
|
|
resp: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//export windowClosing
|
|
|
|
func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean {
|
|
|
|
w := (*window)(unsafe.Pointer(data))
|
|
|
|
close := w.closing.fire()
|
|
|
|
if close {
|
|
|
|
return C.GDK_EVENT_PROPAGATE // will do gtk_widget_destroy(), which is what we want (thanks ebassi in irc.gimp.net/#gtk+)
|
|
|
|
}
|
|
|
|
return C.GDK_EVENT_STOP // keeps window alive
|
2014-07-07 15:51:17 -05:00
|
|
|
}
|
2014-07-14 21:54:57 -05:00
|
|
|
|
|
|
|
//export windowResizing
|
|
|
|
func windowResizing(wid *C.GtkWidget, event *C.GdkEvent, data C.gpointer) C.gboolean {
|
|
|
|
w := (*window)(unsafe.Pointer(data))
|
|
|
|
e := (*C.GdkEventConfigure)(unsafe.Pointer(event))
|
|
|
|
_ = w // TODO
|
|
|
|
// TODO this does not take CSD into account; my attempts at doing so so far have failed to work correctly in the face of rapid live resizing
|
|
|
|
// TODO triggered twice on each resize or maximize for some reason???
|
|
|
|
fmt.Printf("new size %d x %d\n", e.width, e.height)
|
|
|
|
return C.GDK_EVENT_PROPAGATE // let's be safe
|
|
|
|
}
|