Applied the container change to the GTK+ backend. Woo!

This commit is contained in:
Pietro Gagliardi 2014-08-04 21:08:18 -04:00
parent 91f1a34003
commit 23baffe55e
3 changed files with 48 additions and 56 deletions

View File

@ -10,9 +10,45 @@ import (
) )
// #include "gtk_unix.h" // #include "gtk_unix.h"
// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer); // extern void containerResizing(GtkWidget *, GdkRectangle *, gpointer);
import "C" import "C"
type container struct {
containerbase
layoutwidget *C.GtkWidget
layoutcontainer *C.GtkContainer
layout *C.GtkLayout
}
func newContainer(child Control) *container {
widget := C.gtk_layout_new(nil, nil)
c := &container{
layoutwidget: widget,
layoutcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)),
layout: (*C.GtkLayout)(unsafe.Pointer(widget)),
}
c.child = child
c.child.setParent(&controlParent{c.layoutcontainer})
// 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 basecommitResize() in control_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(c.layout)),
"size-allocate",
C.GCallback(C.containerResizing),
C.gpointer(unsafe.Pointer(c)))
return c
}
//export containerResizing
func containerResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
c := (*container)(unsafe.Pointer(data))
// the GtkLayout's coordinate system is localized, so the origin is (0, 0)
c.resize(0, 0, int(r.width), int(r.height))
fmt.Printf("new size %d x %d\n", r.width, r.height)
}
type sizing struct { type sizing struct {
sizingbase sizingbase
@ -30,7 +66,7 @@ const (
gtkYPadding = 6 gtkYPadding = 6
) )
func (s *sizer) beginResize() (d *sizing) { func (c *container) beginResize() (d *sizing) {
d = new(sizing) d = new(sizing)
if spaced { if spaced {
d.xmargin = gtkXMargin d.xmargin = gtkXMargin
@ -41,44 +77,6 @@ func (s *sizer) beginResize() (d *sizing) {
return d return d
} }
func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
// no need for coordinate conversion with gtk+ // no need for coordinate conversion with gtk+
} }
// layout maintains the widget hierarchy by containing all of a sizer's children in a single layout widget
type layout struct {
*sizer
layoutwidget *C.GtkWidget
layoutcontainer *C.GtkContainer
layout *C.GtkLayout
}
func newLayout(child Control) *layout {
widget := C.gtk_layout_new(nil, nil)
l := &layout{
sizer: new(sizer),
layoutwidget: widget,
layoutcontainer: (*C.GtkContainer)(unsafe.Pointer(widget)),
layout: (*C.GtkLayout)(unsafe.Pointer(widget)),
}
l.child = child
l.child.setParent(&controlParent{l.layoutcontainer})
// 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(l.layout)),
"size-allocate",
C.GCallback(C.layoutResizing),
C.gpointer(unsafe.Pointer(l)))
return l
}
//export layoutResizing
func layoutResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
l := (*layout)(unsafe.Pointer(data))
// the layout's coordinate system is localized, so the origin is (0, 0)
l.resize(0, 0, int(r.width), int(r.height))
fmt.Printf("new size %d x %d\n", r.width, r.height)
}

View File

@ -15,7 +15,7 @@ type tab struct {
_widget *C.GtkWidget _widget *C.GtkWidget
notebook *C.GtkNotebook notebook *C.GtkNotebook
tabs []*layout tabs []*container
} }
func newTab() Tab { func newTab() Tab {
@ -30,12 +30,13 @@ func newTab() Tab {
} }
func (t *tab) Append(name string, control Control) { func (t *tab) Append(name string, control Control) {
tl := newLayout(control) c := newContainer(control)
t.tabs = append(t.tabs, tl) t.tabs = append(t.tabs, c)
cname := togstr(name) cname := togstr(name)
defer freegstr(cname) defer freegstr(cname)
tab := C.gtk_notebook_append_page(t.notebook, tab := C.gtk_notebook_append_page(t.notebook,
tl.layoutwidget, // TODO figure out how to keep this private
c.layoutwidget,
C.gtk_label_new(cname)) C.gtk_label_new(cname))
if tab == -1 { if tab == -1 {
panic("gtk_notebook_append_page() failed") panic("gtk_notebook_append_page() failed")

View File

@ -10,7 +10,6 @@ import (
// #include "gtk_unix.h" // #include "gtk_unix.h"
// extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer); // extern gboolean windowClosing(GtkWidget *, GdkEvent *, gpointer);
// extern void windowResizing(GtkWidget *, GdkRectangle *, gpointer);
import "C" import "C"
type window struct { type window struct {
@ -21,7 +20,7 @@ type window struct {
closing *event closing *event
*layout *container
} }
func newWindow(title string, width int, height int, control Control) *window { func newWindow(title string, width int, height int, control Control) *window {
@ -42,8 +41,9 @@ func newWindow(title string, width int, height int, control Control) *window {
C.GCallback(C.windowClosing), C.GCallback(C.windowClosing),
C.gpointer(unsafe.Pointer(w))) C.gpointer(unsafe.Pointer(w)))
C.gtk_window_resize(w.window, C.gint(width), C.gint(height)) C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
w.layout = newLayout(control) w.container = newContainer(control)
C.gtk_container_add(w.wc, w.layout.layoutwidget) // TODO make a method of container
C.gtk_container_add(w.wc, w.container.layoutwidget)
return w return w
} }
@ -82,10 +82,3 @@ func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean
} }
return C.GDK_EVENT_STOP // keeps window alive return C.GDK_EVENT_STOP // keeps window alive
} }
//export windowResizing
func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
w := (*window)(unsafe.Pointer(data))
// the origin of the window's content area is always (0, 0)
w.resize(0, 0, int(r.width), int(r.height))
}