Migrated the Label alignment code back into the Mac OS X port.

This commit is contained in:
Pietro Gagliardi 2014-08-02 09:47:57 -04:00
parent 85fb097ab9
commit c6e47ba21a
5 changed files with 48 additions and 81 deletions

View File

@ -103,7 +103,8 @@ func (t *textField) SetText(text string) {
// cheap trick
type label struct {
*textField
standalone bool
standalone bool
supercommitResize func(c *allocation, d *sizing)
}
func finishNewLabel(text string, standalone bool) *label {
@ -112,6 +113,8 @@ func finishNewLabel(text string, standalone bool) *label {
standalone: standalone,
}
l.SetText(text)
l.supercommitResize = l.fcommitResize
l.fcommitResize = l.labelcommitResize
return l
}
@ -123,4 +126,31 @@ func newStandaloneLabel(text string) Label {
return finishNewLabel(text, true)
}
// TODO label commitResize
func (l *label) labelcommitResize(c *allocation, d *sizing) {
if !l.standalone && c.neighbor != nil {
c.neighbor.getAuxResizeInfo(d)
if d.neighborAlign.baseline != 0 { // no adjustment needed if the given control has no baseline
// in order for the baseline value to be correct, the label MUST BE AT THE HEIGHT THAT OS X WANTS IT TO BE!
// otherwise, the baseline calculation will be relative to the bottom of the control, and everything will be wrong
origsize := C.controlPrefSize(l.id)
c.height = int(origsize.height)
newrect := C.struct_xrect{
x: C.intptr_t(c.x),
y: C.intptr_t(c.y),
width: C.intptr_t(c.width),
height: C.intptr_t(c.height),
}
ourAlign := C.alignmentInfo(l.id, newrect)
// we need to find the exact Y positions of the baselines
// fortunately, this is easy now that (x,y) is the bottom-left corner
thisbasey := ourAlign.rect.y + ourAlign.baseline
neighborbasey := d.neighborAlign.rect.y + d.neighborAlign.baseline
// now the amount we have to move the label down by is easy to find
yoff := neighborbasey - thisbasey
// and we just add that
c.y += int(yoff)
}
// TODO if there's no baseline, the alignment should be to the top /of the alignment rect/, not the frame
}
l.supercommitResize(c, d)
}

View File

@ -34,39 +34,10 @@ func newControl(id C.id) *controlbase {
return int(s.width), int(s.height)
}
c.fcommitResize = func(a *allocation, d *sizing) {
//TODO
/*
if s.ctype == c_label && !s.alternate && c.neighbor != nil {
c.neighbor.getAuxResizeInfo(d)
if d.neighborAlign.baseline != 0 { // no adjustment needed if the given control has no baseline
// in order for the baseline value to be correct, the label MUST BE AT THE HEIGHT THAT OS X WANTS IT TO BE!
// otherwise, the baseline calculation will be relative to the bottom of the control, and everything will be wrong
origsize := C.controlPrefSize(s.id)
c.height = int(origsize.height)
newrect := C.struct_xrect{
x: C.intptr_t(c.x),
y: C.intptr_t(c.y),
width: C.intptr_t(c.width),
height: C.intptr_t(c.height),
}
ourAlign := C.alignmentInfo(s.id, newrect)
// we need to find the exact Y positions of the baselines
// fortunately, this is easy now that (x,y) is the bottom-left corner
thisbasey := ourAlign.alignmentRect.y + ourAlign.baseline
neighborbasey := d.neighborAlign.alignmentRect.y + d.neighborAlign.baseline
// now the amount we have to move the label down by is easy to find
yoff := neighborbasey - thisbasey
// and we just add that
c.y += int(yoff)
}
// TODO if there's no baseline, the alignment should be to the top /of the alignment rect/, not the frame
}
*/
C.moveControl(c.id, C.intptr_t(a.x), C.intptr_t(a.y), C.intptr_t(a.width), C.intptr_t(a.height))
}
c.fgetAuxResizeInfo = func(d *sizing) {
// TODO
// d.neighborAlign = C.alignmentInfo(s.id, C.frame(s.id))
d.neighborAlign = C.alignmentInfo(c.id, C.frame(c.id))
}
return c
}

View File

@ -84,5 +84,6 @@ extern struct xsize controlPrefSize(id);
extern struct xsize tabPrefSize(id);
extern struct xsize areaPrefSize(id);
extern struct xalignment alignmentInfo(id, struct xrect);
extern struct xrect frame(id);
#endif

View File

@ -12,7 +12,7 @@ type sizing struct {
// nothing for mac
// for the actual resizing
// neighborAlign C.struct_xalignment
neighborAlign C.struct_xalignment
}
// THIS IS A GUESS. TODO.
@ -42,51 +42,3 @@ func (s *sizer) translateAllocationCoords(allocations []*allocation, winwidth, w
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
//}

View File

@ -75,3 +75,16 @@ struct xalignment alignmentInfo(id c, struct xrect newrect)
a.baseline = (intptr_t) [v baselineOffsetFromBottom];
return a;
}
struct xrect frame(id c)
{
NSRect r;
struct xrect s;
r = [toNSView(c) frame];
s.x = (intptr_t) r.origin.x;
s.y = (intptr_t) r.origin.y;
s.width = (intptr_t) r.size.width;
s.height = (intptr_t) r.size.height;
return s;
}