From 37cf0a20c06d457755f209d3fc393f9593c534af Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 2 Aug 2014 07:28:20 -0400 Subject: Migrated the Mac OS X backend to sizer. Once I get this control sizing stuff working there and the height of a Tab tab on Windows, I'm going to chuck this whole embedding thing and sacrifice a non-cluttered directory structure for CODE SIMPLICITY. --- redo/containerctrls_darwin.go | 15 ++++--- redo/sizer_darwin.go | 92 +++++++++++++++++++++++++++++++++++++++++++ redo/sizing_darwin.go | 92 ------------------------------------------- redo/window_darwin.go | 4 +- 4 files changed, 101 insertions(+), 102 deletions(-) create mode 100644 redo/sizer_darwin.go delete mode 100644 redo/sizing_darwin.go diff --git a/redo/containerctrls_darwin.go b/redo/containerctrls_darwin.go index 7249134..4c70cce 100644 --- a/redo/containerctrls_darwin.go +++ b/redo/containerctrls_darwin.go @@ -12,7 +12,7 @@ import "C" type tab struct { *controlbase - containers []*container + tabs []*sizer } func newTab() Tab { @@ -23,14 +23,13 @@ func newTab() Tab { } func (t *tab) Append(name string, control Control) { - // TODO isolate and standardize - c := new(container) - t.containers = append(t.containers, c) + s := new(sizer) + t.tabs = append(t.tabs, s) cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) tabview := C.tabAppend(t.id, cname) - c.child = control - c.child.setParent(&controlParent{tabview}) + s.child = control + s.child.setParent(&controlParent{tabview}) } // no need to override Control.allocate() as only prepared the tabbed control; its children will be reallocated when that one is resized @@ -38,8 +37,8 @@ func (t *tab) Append(name string, control Control) { //export tabResized func tabResized(data unsafe.Pointer, width C.intptr_t, height C.intptr_t) { t := (*tab)(unsafe.Pointer(data)) - for _, c := range t.containers { + for _, s := range t.tabs { // the tab area's coordinate system is localized, so the origin is (0, 0) - c.resize(0, 0, int(width), int(height)) + s.resize(0, 0, int(width), int(height)) } } diff --git a/redo/sizer_darwin.go b/redo/sizer_darwin.go new file mode 100644 index 0000000..1aa345a --- /dev/null +++ b/redo/sizer_darwin.go @@ -0,0 +1,92 @@ +// 1 march 2014 + +package ui + +// #include "objc_darwin.h" +import "C" + +type sizing struct { + sizingbase + + // for size calculations + // nothing for mac + + // for the actual resizing +// neighborAlign C.struct_xalignment +} + +// THIS IS A GUESS. TODO. +// The only indication that this is remotely correct is the Auto Layout Guide implying that 12 pixels is the "Aqua space". +const ( + macXMargin = 12 + macYMargin = 12 + macXPadding = 12 + macYPadding = 12 +) + +func (s *sizer) beginResize() (d *sizing) { + d = new(sizing) + if spaced { + d.xmargin = macXMargin + d.ymargin = macYMargin + d.xpadding = macXPadding + d.ypadding = macYPadding + } + return d +} + +func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { + for _, a := range allocations { + // winheight - y because (0,0) is the bottom-left corner of the window and not the top-left corner + // (winheight - y) - height because (x, y) is the bottom-left corner of the control and not the top-left + a.y = (winheight - a.y) - a.height + } +} + +/* +Cocoa doesn't provide a reliable way to get the preferred size of a control (you're supposed to use Interface Builder and have it set up autoresizing for you). The best we can do is call [control sizeToFit] (which is defined for NSControls and has a custom implementation for the other types here) and read the preferred size. Though this changes the size, we're immediately overriding the change on return from sysData.preferredSize(), so no harm done. (This is similar to what we are doing with GTK+, except GTK+ does not actually change the size.) +*/ + +//TODO +/* +// standard case: control immediately passed in +func controlPrefSize(control C.id) (width int, height int) { + r := C.controlPrefSize(control) + return int(r.width), int(r.height) +} + +// NSTableView is actually in a NSScrollView so we have to get it out first +func listboxPrefSize(control C.id) (width int, height int) { + r := C.listboxPrefSize(control) + return int(r.width), int(r.height) +} + +// and for type checking reasons, progress bars are separate despite responding to -[sizeToFit] +func pbarPrefSize(control C.id) (width int, height int) { + r := C.pbarPrefSize(control) + return int(r.width), int(r.height) +} + +// Areas know their own preferred size +func areaPrefSize(control C.id) (width int, height int) { + r := C.areaPrefSize(control) + return int(r.width), int(r.height) +} + +var prefsizefuncs = [nctypes]func(C.id) (int, int){ + c_button: controlPrefSize, + c_checkbox: controlPrefSize, + c_combobox: controlPrefSize, + c_lineedit: controlPrefSize, + c_label: controlPrefSize, + c_listbox: listboxPrefSize, + c_progressbar: pbarPrefSize, + c_area: areaPrefSize, +} +*/ + +//func (w *widgetbase) preferredSize(d *sizing) (width int, height int) { +//TODO +// return prefsizefuncs[s.ctype](s.id) +//return 0,0 +//} diff --git a/redo/sizing_darwin.go b/redo/sizing_darwin.go deleted file mode 100644 index 6eb29b2..0000000 --- a/redo/sizing_darwin.go +++ /dev/null @@ -1,92 +0,0 @@ -// 1 march 2014 - -package ui - -// #include "objc_darwin.h" -import "C" - -type sizing struct { - sizingbase - - // for size calculations - // nothing for mac - - // for the actual resizing -// neighborAlign C.struct_xalignment -} - -// THIS IS A GUESS. TODO. -// The only indication that this is remotely correct is the Auto Layout Guide implying that 12 pixels is the "Aqua space". -const ( - macXMargin = 12 - macYMargin = 12 - macXPadding = 12 - macYPadding = 12 -) - -func (c *container) beginResize() (d *sizing) { - d = new(sizing) - if spaced { - d.xmargin = macXMargin - d.ymargin = macYMargin - d.xpadding = macXPadding - d.ypadding = macYPadding - } - return d -} - -func (c *container) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) { - for _, a := range allocations { - // winheight - y because (0,0) is the bottom-left corner of the window and not the top-left corner - // (winheight - y) - height because (x, y) is the bottom-left corner of the control and not the top-left - a.y = (winheight - a.y) - a.height - } -} - -/* -Cocoa doesn't provide a reliable way to get the preferred size of a control (you're supposed to use Interface Builder and have it set up autoresizing for you). The best we can do is call [control sizeToFit] (which is defined for NSControls and has a custom implementation for the other types here) and read the preferred size. Though this changes the size, we're immediately overriding the change on return from sysData.preferredSize(), so no harm done. (This is similar to what we are doing with GTK+, except GTK+ does not actually change the size.) -*/ - -//TODO -/* -// standard case: control immediately passed in -func controlPrefSize(control C.id) (width int, height int) { - r := C.controlPrefSize(control) - return int(r.width), int(r.height) -} - -// NSTableView is actually in a NSScrollView so we have to get it out first -func listboxPrefSize(control C.id) (width int, height int) { - r := C.listboxPrefSize(control) - return int(r.width), int(r.height) -} - -// and for type checking reasons, progress bars are separate despite responding to -[sizeToFit] -func pbarPrefSize(control C.id) (width int, height int) { - r := C.pbarPrefSize(control) - return int(r.width), int(r.height) -} - -// Areas know their own preferred size -func areaPrefSize(control C.id) (width int, height int) { - r := C.areaPrefSize(control) - return int(r.width), int(r.height) -} - -var prefsizefuncs = [nctypes]func(C.id) (int, int){ - c_button: controlPrefSize, - c_checkbox: controlPrefSize, - c_combobox: controlPrefSize, - c_lineedit: controlPrefSize, - c_label: controlPrefSize, - c_listbox: listboxPrefSize, - c_progressbar: pbarPrefSize, - c_area: areaPrefSize, -} -*/ - -//func (w *widgetbase) preferredSize(d *sizing) (width int, height int) { -//TODO -// return prefsizefuncs[s.ctype](s.id) -//return 0,0 -//} diff --git a/redo/window_darwin.go b/redo/window_darwin.go index 80d1a51..8393cf7 100644 --- a/redo/window_darwin.go +++ b/redo/window_darwin.go @@ -15,7 +15,7 @@ type window struct { closing *event - *container + *sizer } func newWindow(title string, width int, height int, control Control) *window { @@ -26,7 +26,7 @@ func newWindow(title string, width int, height int, control Control) *window { w := &window{ id: id, closing: newEvent(), - container: new(container), + sizer: new(sizer), } C.windowSetDelegate(id, unsafe.Pointer(w)) w.child = control -- cgit v1.2.3