summaryrefslogtreecommitdiff
path: root/label_darwin.go
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-08-30 23:02:02 -0400
committerPietro Gagliardi <[email protected]>2014-08-30 23:02:02 -0400
commit77bf566ebbcb62acd4d08d905d9542d6ff9b6b80 (patch)
treeeeb8e72bc3bf57f5be7f0c0af4319189ac6de838 /label_darwin.go
parent155899c65ed32245e2ccad4197a10c77017d835b (diff)
...in with the new.
Diffstat (limited to 'label_darwin.go')
-rw-r--r--label_darwin.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/label_darwin.go b/label_darwin.go
new file mode 100644
index 0000000..0508931
--- /dev/null
+++ b/label_darwin.go
@@ -0,0 +1,95 @@
+// 16 july 2014
+
+package ui
+
+import (
+ "unsafe"
+)
+
+// #include "objc_darwin.h"
+import "C"
+
+type label struct {
+ _id C.id
+ standalone bool
+}
+
+func finishNewLabel(text string, standalone bool) *label {
+ l := &label{
+ _id: C.newLabel(),
+ standalone: standalone,
+ }
+ l.SetText(text)
+ return l
+}
+
+func newLabel(text string) Label {
+ return finishNewLabel(text, false)
+}
+
+func newStandaloneLabel(text string) Label {
+ return finishNewLabel(text, true)
+}
+
+func (l *label) Text() string {
+ return C.GoString(C.textfieldText(l._id))
+}
+
+func (l *label) SetText(text string) {
+ ctext := C.CString(text)
+ defer C.free(unsafe.Pointer(ctext))
+ C.textfieldSetText(l._id, ctext)
+}
+
+func (l *label) isStandalone() bool {
+ return l.standalone
+}
+
+func (l *label) id() C.id {
+ return l._id
+}
+
+func (l *label) setParent(p *controlParent) {
+ basesetParent(l, p)
+}
+
+func (l *label) allocate(x int, y int, width int, height int, d *sizing) []*allocation {
+ return baseallocate(l, x, y, width, height, d)
+}
+
+func (l *label) preferredSize(d *sizing) (width, height int) {
+ return basepreferredSize(l, d)
+}
+
+func (l *label) commitResize(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.controlPreferredSize(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)
+ }
+ // in the other case, the most correct thing would be for Label to be aligned to the alignment rect, but I can't get this working, and it looks fine as it is anyway
+ }
+ basecommitResize(l, c, d)
+}
+
+func (l *label) getAuxResizeInfo(d *sizing) {
+ basegetAuxResizeInfo(l, d)
+}