Finished implementing the new control sizing system on Mac OS X. The new label behavior needs to be reimplemented next.

This commit is contained in:
Pietro Gagliardi 2014-06-26 03:24:27 -04:00
parent 5e17d64b09
commit 0b4c6a9228
3 changed files with 59 additions and 88 deletions

View File

@ -5,6 +5,61 @@ package ui
// #include "objc_darwin.h" // #include "objc_darwin.h"
import "C" import "C"
type sysSizeData struct {
cSysSizeData
// for size calculations
// nothing for mac
// for the actual resizing
// neighbor control alignment rect/baseline info
}
// 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 *sysData) beginResize() (d *sysSizeData) {
d = new(sysSizeData)
if s.spaced {
d.xmargin = macXMargin
d.ymargin = macYMargin
d.xpadding = macXPadding
d.ypadding = macYPadding
}
return d
}
func (s *sysData) endResize(d *sysSizeData) {
// redraw
}
func (s *sysData) 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
}
}
func (s *sysData) commitResize(c *allocation, d *sysSizeData) {
if s.ctype == c_label && !s.alternate && c.neighbor != nil {
c.neighbor.getAuxResizeInfo(d)
// get this control's alignment rect and baseline
// align
}
C.setRect(s.id, C.intptr_t(c.x), C.intptr_t(c.y), C.intptr_t(c.width), C.intptr_t(c.height))
}
func (s *sysData) getAuxResizeInfo(d *sysSizeData) {
// get this control's alignment rect and baseline
}
/* /*
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.) 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.)
*/ */
@ -17,7 +72,7 @@ func controlPrefSize(control C.id) (width int, height int) {
// NSTableView is actually in a NSScrollView so we have to get it out first // NSTableView is actually in a NSScrollView so we have to get it out first
func listboxPrefSize(control C.id) (width int, height int) { func listboxPrefSize(control C.id) (width int, height int) {
r := C.listboxPrefSize(control, alternate) r := C.listboxPrefSize(control)
return int(r.width), int(r.height) return int(r.width), int(r.height)
} }
@ -44,6 +99,6 @@ var prefsizefuncs = [nctypes]func(C.id) (int, int){
c_area: areaPrefSize, c_area: areaPrefSize,
} }
func (s *sysData) preferredSize() (width int, height int) { func (s *sysData) preferredSize(d *sysSizeData) (width int, height int) {
return prefsizefuncs[s.ctype](s.id) return prefsizefuncs[s.ctype](s.id)
} }

View File

@ -1,84 +0,0 @@
// +build SKIP
// 25 june 2014
package ui
type sysSizeData struct {
// for size calculations
// all platforms
margin int // windows: calculated
spacing int // gtk+, cocoa: constants
// windows
baseX int
baseY int
// gtk, mac os x: nothing
// for the actual resizing
// windows
// possibly also the HDWP
// gtk
shouldVAlignTop bool
// mac os x
// neighbor control alignment rect/baseline info
}
func (s *sysData) beginResize() *sysSizeData {
// windows: get baseX/baseY for window and compute margin and spacing
// gtk, mac: return zero
}
func (s *sysData) endResize(d *sysSizeData) {
// redraw
}
func (s *sysData) translateAllocationCoords(allocations []*allocation, winwidth, winheight int) {
// windows, gtk: nothing
// mac
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
}
}
// windows
func (s *sysData) doResize(c *allocation, d *sysSizeData) {
if s.ctype == c_label {
// add additional offset of 4 dialog units
}
// resize
}
func (s *sysData) getAuxResizeInfo(d *sysSizeData) {
// do nothing
}
// gtk+
func (s *sysData) doResize(c *allocation, d *sysSizeData) {
if s.ctype == c_label && !s.alternate && c.neighbor != nil {
c.neighbor.getAuxResizeInfo(d)
if d.shouldVAlignTop {
// TODO should it be center-aligned to the first line or not
gtk_misc_set_align(s.widget, 0, 0)
} else {
gtk_misc_set_align(s.widget, 0, 0.5)
}
}
// resize
}
func (s *sysData) getAuxResizeInfo(d *sysSizeData) {
d.shouldVAlignTop = (s.ctype == c_listbox) || (s.ctype == c_area)
}
// cocoa
func (s *sysData) doResize(c *allocation, d *sysSizeData) {
if s.ctype == c_label && !s.alternate && c.neighbor != nil {
c.neighbor.getAuxResizeInfo(d)
// get this control's alignment rect and baseline
// align
}
// resize
}
func (s *sysData) getAuxResizeInfo(d *sysSizeData) {
// get this control's alignment rect and baseline
}

View File

@ -34,8 +34,8 @@ func appDelegate_windowDidResize(win C.id) {
s := getSysData(win) s := getSysData(win)
wincv := C.windowGetContentView(win) // we want the content view's size, not the window's wincv := C.windowGetContentView(win) // we want the content view's size, not the window's
r := C.frame(wincv) r := C.frame(wincv)
// winheight is used here because (0,0) is the bottom-left corner, not the top-left corner // (0,0) is the bottom left corner but this is handled in sysData.translateAllocationCoords()
s.doResize(0, 0, int(r.width), int(r.height), int(r.height)) s.resizeWindow(int(r.width), int(r.height))
C.display(win) // redraw everything C.display(win) // redraw everything
} }