Migrated the GTK+ backend to the new sizer system.

This commit is contained in:
Pietro Gagliardi 2014-08-02 01:14:09 -04:00
parent 0356d0fd70
commit 99b6b47a49
4 changed files with 92 additions and 92 deletions

View File

@ -9,17 +9,13 @@ import (
)
// #include "gtk_unix.h"
// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer);
import "C"
type tab struct {
*controlbase
notebook *C.GtkNotebook
containers []*container
layoutws []*C.GtkWidget
layoutcs []*C.GtkContainer
layouts []*C.GtkLayout
tabs []*layout
}
func newTab() Tab {
@ -34,35 +30,16 @@ func newTab() Tab {
}
func (t *tab) Append(name string, control Control) {
// TODO isolate and standardize
layout := C.gtk_layout_new(nil, nil)
t.layoutws = append(t.layoutws, layout)
t.layoutcs = append(t.layoutcs, (*C.GtkContainer)(unsafe.Pointer(layout)))
t.layouts = append(t.layouts, (*C.GtkLayout)(unsafe.Pointer(layout)))
c := new(container)
t.containers = append(t.containers, c)
c.child = control
c.child.setParent(&controlParent{(*C.GtkContainer)(unsafe.Pointer(layout))})
g_signal_connect_after(
C.gpointer(unsafe.Pointer(layout)),
"size-allocate",
C.GCallback(C.layoutResizing),
C.gpointer(unsafe.Pointer(c)))
tl := newLayout(control)
t.tabs = append(t.tabs, tl)
cname := togstr(name)
defer freegstr(cname)
tab := C.gtk_notebook_append_page(t.notebook,
layout,
tl.layoutwidget,
C.gtk_label_new(cname))
if tab == -1 {
panic("gtk_notebook_append_page() failed")
}
}
// no need to override Control.allocate() as only prepared the tabbed control; its children will be reallocated when that one is resized
//export layoutResizing
func layoutResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
c := (*container)(unsafe.Pointer(data))
// the layout's coordinate system is localized, so the origin is (0, 0)
c.resize(0, 0, int(r.width), int(r.height))
}
// no need to override Control.commitResize() as only prepared the tabbed control; its children will be reallocated when that one is resized

84
redo/sizer_unix.go Normal file
View File

@ -0,0 +1,84 @@
// +build !windows,!darwin
// 23 february 2014
package ui
import (
"unsafe"
"fmt"
)
// #include "gtk_unix.h"
// extern void layoutResizing(GtkWidget *, GdkRectangle *, gpointer);
import "C"
type sizing struct {
sizingbase
// for size calculations
// gtk+ needs nothing
// for the actual resizing
shouldVAlignTop bool
}
const (
gtkXMargin = 12
gtkYMargin = 12
gtkXPadding = 12
gtkYPadding = 6
)
func (s *sizer) beginResize() (d *sizing) {
d = new(sizing)
if spaced {
d.xmargin = gtkXMargin
d.ymargin = gtkYMargin
d.xpadding = gtkXPadding
d.ypadding = gtkYPadding
}
return d
}
func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
// 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

@ -1,40 +0,0 @@
// +build !windows,!darwin
// 23 february 2014
package ui
// #include "gtk_unix.h"
import "C"
type sizing struct {
sizingbase
// for size calculations
// gtk+ needs nothing
// for the actual resizing
shouldVAlignTop bool
}
const (
gtkXMargin = 12
gtkYMargin = 12
gtkXPadding = 12
gtkYPadding = 6
)
func (c *container) beginResize() (d *sizing) {
d = new(sizing)
if spaced {
d.xmargin = gtkXMargin
d.ymargin = gtkYMargin
d.xpadding = gtkXPadding
d.ypadding = gtkYPadding
}
return d
}
func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
// no need for coordinate conversion with gtk+
}

View File

@ -6,7 +6,6 @@ package ui
import (
"unsafe"
"fmt"
)
// #include "gtk_unix.h"
@ -20,30 +19,21 @@ type window struct {
bin *C.GtkBin
window *C.GtkWindow
layoutw *C.GtkWidget
layoutc *C.GtkContainer
layout *C.GtkLayout
closing *event
*container
*layout
}
func newWindow(title string, width int, height int, control Control) *window {
widget := C.gtk_window_new(C.GTK_WINDOW_TOPLEVEL)
ctitle := togstr(title)
defer freegstr(ctitle)
layoutw := C.gtk_layout_new(nil, nil)
w := &window{
widget: widget,
wc: (*C.GtkContainer)(unsafe.Pointer(widget)),
bin: (*C.GtkBin)(unsafe.Pointer(widget)),
window: (*C.GtkWindow)(unsafe.Pointer(widget)),
layoutw: layoutw,
layoutc: (*C.GtkContainer)(unsafe.Pointer(layoutw)),
layout: (*C.GtkLayout)(unsafe.Pointer(layoutw)),
closing: newEvent(),
container: new(container),
}
C.gtk_window_set_title(w.window, ctitle)
g_signal_connect(
@ -51,19 +41,9 @@ func newWindow(title string, width int, height int, control Control) *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)))
C.gtk_window_resize(w.window, C.gint(width), C.gint(height))
C.gtk_container_add(w.wc, layoutw)
w.child = control
w.child.setParent(&controlParent{w.layoutc})
w.layout = newLayout(control)
C.gtk_container_add(w.wc, w.layout.layoutwidget)
return w
}
@ -108,5 +88,4 @@ 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))
fmt.Printf("new size %d x %d\n", r.width, r.height)
}