2014-07-17 22:50:14 -05:00
|
|
|
// +build !windows,!darwin
|
2014-07-08 15:47:28 -05:00
|
|
|
|
2014-07-07 14:30:55 -05:00
|
|
|
// 7 july 2014
|
|
|
|
|
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
|
|
|
// #include "gtk_unix.h"
|
2014-07-08 11:04:51 -05:00
|
|
|
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
|
2014-10-18 16:03:07 -05:00
|
|
|
// extern void windowResized(GtkWidget *, GdkRectangle *, gpointer);
|
2014-07-07 14:30:55 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type window struct {
|
2014-10-02 09:05:53 -05:00
|
|
|
widget *C.GtkWidget
|
|
|
|
wc *C.GtkContainer
|
|
|
|
bin *C.GtkBin
|
|
|
|
window *C.GtkWindow
|
2014-07-08 11:04:51 -05:00
|
|
|
|
2014-10-02 09:05:53 -05:00
|
|
|
group *C.GtkWindowGroup
|
2014-08-26 12:54:55 -05:00
|
|
|
|
2014-10-02 09:05:53 -05:00
|
|
|
closing *event
|
2014-07-16 20:30:19 -05:00
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
child Control
|
|
|
|
container *container
|
|
|
|
|
|
|
|
margined bool
|
2014-07-25 18:44:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWindow(title string, width int, height int, control Control) *window {
|
2014-07-19 08:44:32 -05:00
|
|
|
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
|
|
|
|
ctitle := togstr(title)
|
|
|
|
defer freegstr(ctitle)
|
|
|
|
w := &window{
|
2014-10-02 09:05:53 -05:00
|
|
|
widget: widget,
|
|
|
|
wc: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
|
|
|
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
|
|
|
|
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
|
|
|
|
closing: newEvent(),
|
2014-10-18 16:03:07 -05:00
|
|
|
child: control,
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
2014-07-19 08:44:32 -05:00
|
|
|
C.gtk_window_set_title(w.window, ctitle)
|
|
|
|
g_signal_connect(
|
|
|
|
C.gpointer(unsafe.Pointer(w.window)),
|
|
|
|
"delete-event",
|
|
|
|
C.GCallback(C.windowClosing),
|
|
|
|
C.gpointer(unsafe.Pointer(w)))
|
2014-07-21 09:37:15 -05:00
|
|
|
C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
|
2014-10-18 16:03:07 -05:00
|
|
|
w.container = newContainer()
|
|
|
|
w.child.setParent(w.container.parent())
|
2014-08-04 21:21:58 -05:00
|
|
|
w.container.setParent(&controlParent{w.wc})
|
2014-10-18 16:03:07 -05:00
|
|
|
// notice that we connect this to the container
|
|
|
|
g_signal_connect_after( // so we get it after the child container has been allocated
|
|
|
|
C.gpointer(unsafe.Pointer(w.container.widget)),
|
|
|
|
"size-allocate",
|
|
|
|
C.GCallback(C.windowResized),
|
|
|
|
C.gpointer(unsafe.Pointer(w)))
|
2014-08-26 12:54:55 -05:00
|
|
|
// 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)
|
2014-07-19 08:44:32 -05:00
|
|
|
return w
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) Title() string {
|
|
|
|
return fromgstr(C.gtk_window_get_title(w.window))
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) SetTitle(title string) {
|
|
|
|
ctitle := togstr(title)
|
|
|
|
defer freegstr(ctitle)
|
|
|
|
C.gtk_window_set_title(w.window, ctitle)
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) Show() {
|
|
|
|
C.gtk_widget_show_all(w.widget)
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) Hide() {
|
|
|
|
C.gtk_widget_hide(w.widget)
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) Close() {
|
|
|
|
C.gtk_widget_destroy(w.widget)
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
2014-07-19 08:44:32 -05:00
|
|
|
func (w *window) OnClosing(e func() bool) {
|
|
|
|
w.closing.setbool(e)
|
2014-07-08 11:04:51 -05:00
|
|
|
}
|
|
|
|
|
2014-10-18 16:03:07 -05:00
|
|
|
func (w *window) Margined() bool {
|
|
|
|
return w.margined
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *window) SetMargined(margined bool) {
|
|
|
|
w.margined = margined
|
|
|
|
}
|
|
|
|
|
2014-07-08 11:04:51 -05:00
|
|
|
//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 {
|
2014-10-02 09:05:53 -05:00
|
|
|
return C.GDK_EVENT_PROPAGATE // will do gtk_widget_destroy(), which is what we want (thanks ebassi in irc.gimp.net/#gtk+)
|
2014-07-08 11:04:51 -05:00
|
|
|
}
|
2014-10-02 09:05:53 -05:00
|
|
|
return C.GDK_EVENT_STOP // keeps window alive
|
2014-07-07 15:51:17 -05:00
|
|
|
}
|
2014-10-18 16:03:07 -05:00
|
|
|
|
|
|
|
//export windowResized
|
|
|
|
func windowResized(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
|
|
|
w := (*window)(unsafe.Pointer(data))
|
|
|
|
a := w.container.allocation(w.margined)
|
|
|
|
d := w.beginResize()
|
|
|
|
w.child.resize(int(a.x), int(a.y), int(a.width), int(a.height), d)
|
|
|
|
}
|