Began final simplification of size code. spaced is now a global variable; either all controls are given spacing now, or none will. beginResize() is a method on container again. Done on GTK+ and Mac OS X for now. I'm going to go ahead and implement this on Windows in a bit, regardless of whether that Stack Overflow question get answered or not, because ugggggggggh I just want to continue working on this project for fuck's sake!
This commit is contained in:
parent
3f124a016e
commit
bea4df1abf
|
@ -25,7 +25,6 @@ func newTab() Tab {
|
||||||
func (t *tab) Append(name string, control Control) {
|
func (t *tab) Append(name string, control Control) {
|
||||||
// TODO isolate and standardize
|
// TODO isolate and standardize
|
||||||
c := new(container)
|
c := new(container)
|
||||||
// don't set beginResize; this container's resize() will be a recursive call
|
|
||||||
t.containers = append(t.containers, c)
|
t.containers = append(t.containers, c)
|
||||||
cname := C.CString(name)
|
cname := C.CString(name)
|
||||||
defer C.free(unsafe.Pointer(cname))
|
defer C.free(unsafe.Pointer(cname))
|
||||||
|
@ -35,11 +34,7 @@ func (t *tab) Append(name string, control Control) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tab) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
func (t *tab) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
||||||
// set up the recursive calls
|
// only prepared the tabbed control; its children will be reallocated when that one is resized
|
||||||
for _, c := range t.containers {
|
|
||||||
c.d = d
|
|
||||||
}
|
|
||||||
// and prepare the tabbed control itself
|
|
||||||
return t.widgetbase.allocate(x, y, width, height, d)
|
return t.widgetbase.allocate(x, y, width, height, d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,6 @@ func (t *tab) Append(name string, control Control) {
|
||||||
t.layoutcs = append(t.layoutcs, (*C.GtkContainer)(unsafe.Pointer(layout)))
|
t.layoutcs = append(t.layoutcs, (*C.GtkContainer)(unsafe.Pointer(layout)))
|
||||||
t.layouts = append(t.layouts, (*C.GtkLayout)(unsafe.Pointer(layout)))
|
t.layouts = append(t.layouts, (*C.GtkLayout)(unsafe.Pointer(layout)))
|
||||||
c := new(container)
|
c := new(container)
|
||||||
// don't set beginResize; this container's resize() will be a recursive call
|
|
||||||
t.containers = append(t.containers, c)
|
t.containers = append(t.containers, c)
|
||||||
c.child = control
|
c.child = control
|
||||||
c.child.setParent((*C.GtkContainer)(unsafe.Pointer(layout)))
|
c.child.setParent((*C.GtkContainer)(unsafe.Pointer(layout)))
|
||||||
|
@ -60,11 +59,7 @@ func (t *tab) Append(name string, control Control) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tab) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
func (t *tab) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
|
||||||
// set up the recursive calls
|
// only prepared the tabbed control; its children will be reallocated when that one is resized
|
||||||
for _, c := range t.containers {
|
|
||||||
c.d = d
|
|
||||||
}
|
|
||||||
// and prepare the tabbed control itself
|
|
||||||
return t.widgetbase.allocate(x, y, width, height, d)
|
return t.widgetbase.allocate(x, y, width, height, d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,24 +29,20 @@ type controlSizing interface {
|
||||||
// on GTK+ and Mac OS X, one is embedded by window and all containers; beginResize() points to a global function (TODO NOT GOOD; ideally the sizing data should be passed across size-allocate requests)
|
// on GTK+ and Mac OS X, one is embedded by window and all containers; beginResize() points to a global function (TODO NOT GOOD; ideally the sizing data should be passed across size-allocate requests)
|
||||||
type container struct {
|
type container struct {
|
||||||
child Control
|
child Control
|
||||||
spaced bool
|
|
||||||
d *sizing
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set to true to apply spacing to all windows
|
||||||
|
var spaced bool = false
|
||||||
|
|
||||||
func (c *container) resize(width, height int) {
|
func (c *container) resize(width, height int) {
|
||||||
if c.child == nil { // no children; nothing to do
|
if c.child == nil { // no children; nothing to do
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if c.d == nil { // not ready (called early or out of the proper recursive call chain (such as by the underlying system when marking an unparented Tab as shown))
|
d := c.beginResize()
|
||||||
return
|
|
||||||
}
|
|
||||||
d := c.d
|
|
||||||
allocations := c.child.allocate(0 + d.xmargin, 0 + d.ymargin, width - (2 * d.xmargin), height - (2 * d.ymargin), d)
|
allocations := c.child.allocate(0 + d.xmargin, 0 + d.ymargin, width - (2 * d.xmargin), height - (2 * d.ymargin), d)
|
||||||
c.translateAllocationCoords(allocations, width, height)
|
c.translateAllocationCoords(allocations, width, height)
|
||||||
// move in reverse so as to approximate right->left order so neighbors make sense
|
// move in reverse so as to approximate right->left order so neighbors make sense
|
||||||
for i := len(allocations) - 1; i >= 0; i-- {
|
for i := len(allocations) - 1; i >= 0; i-- {
|
||||||
allocations[i].this.commitResize(allocations[i], d)
|
allocations[i].this.commitResize(allocations[i], d)
|
||||||
}
|
}
|
||||||
// always set c.d to nil so it can be garbage-collected
|
|
||||||
c.d = nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,9 +24,9 @@ const (
|
||||||
macYPadding = 12
|
macYPadding = 12
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w *window) beginResize() (d *sizing) {
|
func (c *container) beginResize() (d *sizing) {
|
||||||
d = new(sizing)
|
d = new(sizing)
|
||||||
if w.spaced {
|
if spaced {
|
||||||
d.xmargin = macXMargin
|
d.xmargin = macXMargin
|
||||||
d.ymargin = macYMargin
|
d.ymargin = macYMargin
|
||||||
d.xpadding = macXPadding
|
d.xpadding = macXPadding
|
||||||
|
|
|
@ -24,9 +24,9 @@ const (
|
||||||
gtkYPadding = 6
|
gtkYPadding = 6
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w *window) beginResize() (d *sizing) {
|
func (c *container) beginResize() (d *sizing) {
|
||||||
d = new(sizing)
|
d = new(sizing)
|
||||||
if w.spaced {
|
if spaced {
|
||||||
d.xmargin = gtkXMargin
|
d.xmargin = gtkXMargin
|
||||||
d.ymargin = gtkYMargin
|
d.ymargin = gtkYMargin
|
||||||
d.xpadding = gtkXPadding
|
d.xpadding = gtkXPadding
|
||||||
|
|
|
@ -77,7 +77,6 @@ func windowClosing(xw unsafe.Pointer) C.BOOL {
|
||||||
//export windowResized
|
//export windowResized
|
||||||
func windowResized(xw unsafe.Pointer, width C.uintptr_t, height C.uintptr_t) {
|
func windowResized(xw unsafe.Pointer, width C.uintptr_t, height C.uintptr_t) {
|
||||||
w := (*window)(unsafe.Pointer(xw))
|
w := (*window)(unsafe.Pointer(xw))
|
||||||
w.container.d = w.beginResize()
|
|
||||||
w.resize(int(width), int(height))
|
w.resize(int(width), int(height))
|
||||||
fmt.Printf("new size %d x %d\n", width, height)
|
fmt.Printf("new size %d x %d\n", width, height)
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,6 @@ func windowClosing(wid *C.GtkWidget, e *C.GdkEvent, data C.gpointer) C.gboolean
|
||||||
//export windowResizing
|
//export windowResizing
|
||||||
func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
func windowResizing(wid *C.GtkWidget, r *C.GdkRectangle, data C.gpointer) {
|
||||||
w := (*window)(unsafe.Pointer(data))
|
w := (*window)(unsafe.Pointer(data))
|
||||||
w.container.d = w.beginResize()
|
|
||||||
w.resize(int(r.width), int(r.height))
|
w.resize(int(r.width), int(r.height))
|
||||||
fmt.Printf("new size %d x %d\n", r.width, r.height)
|
fmt.Printf("new size %d x %d\n", r.width, r.height)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var closeOnClick = flag.Bool("close", false, "close on click")
|
var closeOnClick = flag.Bool("close", false, "close on click")
|
||||||
var spaced = flag.Bool("spaced", false, "enable spacing")
|
|
||||||
|
|
||||||
// because Cocoa hates being run off the main thread, even if it's run exclusively off the main thread
|
// because Cocoa hates being run off the main thread, even if it's run exclusively off the main thread
|
||||||
func init() {
|
func init() {
|
||||||
|
flag.BoolVar(&spaced, "spaced", false, "enable spacing")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
go func() {
|
go func() {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
Do(func() {
|
Do(func() {
|
||||||
t := NewTab()
|
t := NewTab()
|
||||||
w := NewWindow("Hello", 320, 240, t)
|
w := NewWindow("Hello", 320, 240, t)
|
||||||
// TODO use a method here
|
|
||||||
w.(*window).spaced = *spaced
|
|
||||||
w.OnClosing(func() bool {
|
w.OnClosing(func() bool {
|
||||||
if *closeOnClick {
|
if *closeOnClick {
|
||||||
panic("window closed normally in close on click mode (should not happen)")
|
panic("window closed normally in close on click mode (should not happen)")
|
||||||
|
|
Loading…
Reference in New Issue