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"
|
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-18 18:36:29 -05:00
|
|
|
// extern void windowResizing(GtkWidget *, GdkRectangle *, gpointer);
|
2014-07-07 14:30:55 -05:00
|
|
|
import "C"
|
|
|
|
|
|
|
|
type window struct {
|
|
|
|
widget *C.GtkWidget
|
2014-07-25 18:44:32 -05:00
|
|
|
wc *C.GtkContainer
|
2014-07-07 14:30:55 -05:00
|
|
|
bin *C.GtkBin
|
|
|
|
window *C.GtkWindow
|
2014-07-08 11:04:51 -05:00
|
|
|
|
2014-07-21 09:45:37 -05:00
|
|
|
layoutw *C.GtkWidget
|
2014-07-14 15:32:45 -05:00
|
|
|
layoutc *C.GtkContainer
|
2014-07-21 09:48:56 -05:00
|
|
|
layout *C.GtkLayout
|
2014-07-14 15:32:45 -05:00
|
|
|
|
2014-07-08 11:04:51 -05:00
|
|
|
closing *event
|
2014-07-16 20:30:19 -05:00
|
|
|
|
2014-07-25 18:44:32 -05:00
|
|
|
*container
|
|
|
|
}
|
|
|
|
|
|
|
|
type controlParent interface {
|
|
|
|
setParent(*C.GtkContainer)
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
|
|
|
|
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)
|
2014-07-21 09:48:56 -05:00
|
|
|
layoutw := C.gtk_layout_new(nil, nil)
|
2014-07-19 08:44:32 -05:00
|
|
|
w := &window{
|
|
|
|
widget: widget,
|
2014-07-25 18:44:32 -05:00
|
|
|
wc: (*C.GtkContainer)(unsafe.Pointer(widget)),
|
2014-07-19 08:44:32 -05:00
|
|
|
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
|
|
|
|
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
|
2014-07-21 09:45:37 -05:00
|
|
|
layoutw: layoutw,
|
2014-07-19 08:44:32 -05:00
|
|
|
layoutc: (*C.GtkContainer)(unsafe.Pointer(layoutw)),
|
2014-07-21 09:48:56 -05:00
|
|
|
layout: (*C.GtkLayout)(unsafe.Pointer(layoutw)),
|
2014-07-19 08:44:32 -05:00
|
|
|
closing: newEvent(),
|
2014-07-25 18:44:32 -05:00
|
|
|
container: new(container),
|
2014-07-07 14:30:55 -05:00
|
|
|
}
|
2014-07-25 21:13:09 -05:00
|
|
|
w.container.beginResize = w.beginResize
|
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)))
|
|
|
|
// we connect to the layout's size-allocate, not to the window's configure-event
|
|
|
|
// this allows us to handle client-side decoration-based configurations (such as GTK+ on Wayland) properly
|
|
|
|
// also see commitResize() in sizing_unix.go for additional notes
|
|
|
|
// thanks to many people in irc.gimp.net/#gtk+ for help (including tristan for suggesting g_signal_connect_after())
|
|
|
|
g_signal_connect_after(
|
|
|
|
C.gpointer(unsafe.Pointer(layoutw)),
|
|
|
|
"size-allocate",
|
|
|
|
C.GCallback(C.windowResizing),
|
|
|
|
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-07-25 18:44:32 -05:00
|
|
|
C.gtk_container_add(w.wc, layoutw)
|
|
|
|
w.child = control
|
|
|
|
w.child.setParent(w.layoutc)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//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
|
2014-07-18 18:36:29 -05:00
|
|
|
func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
2014-07-14 21:54:57 -05:00
|
|
|
w := (*window)(unsafe.Pointer(data))
|
2014-07-25 18:44:32 -05:00
|
|
|
w.resize(int(r.width), int(r.height))
|
2014-07-18 18:36:29 -05:00
|
|
|
fmt.Printf("new size %d x %d\n", r.width, r.height)
|
2014-07-14 21:54:57 -05:00
|
|
|
}
|