From 0351cf27fefd59148eee3f5f98ecfbaef2b607ac Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sat, 18 Oct 2014 15:44:56 -0400 Subject: [PATCH] Migrated the Mac OS X basic controls. --- newctrl/button_darwin.go | 47 +++++++++++++++++++++++ newctrl/checkbox_darwin.go | 55 +++++++++++++++++++++++++++ newctrl/label_darwin.go | 71 +++++++++++++++++++++++++++++++++++ newctrl/textfield_darwin.go | 75 +++++++++++++++++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 newctrl/button_darwin.go create mode 100644 newctrl/checkbox_darwin.go create mode 100644 newctrl/label_darwin.go create mode 100644 newctrl/textfield_darwin.go diff --git a/newctrl/button_darwin.go b/newctrl/button_darwin.go new file mode 100644 index 0000000..65262c1 --- /dev/null +++ b/newctrl/button_darwin.go @@ -0,0 +1,47 @@ +// 16 july 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type button struct { + *controlSingleObject + clicked *event +} + +func newButton(text string) *button { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + b := &button{ + controlSingleObject: newControlSingleObject(C.newButton()), + clicked: newEvent(), + } + C.buttonSetText(b._id, ctext) + C.buttonSetDelegate(b._id, unsafe.Pointer(b)) + return b +} + +func (b *button) OnClicked(e func()) { + b.clicked.set(e) +} + +func (b *button) Text() string { + return C.GoString(C.buttonText(b._id)) +} + +func (b *button) SetText(text string) { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + C.buttonSetText(b._id, ctext) +} + +//export buttonClicked +func buttonClicked(xb unsafe.Pointer) { + b := (*button)(unsafe.Pointer(xb)) + b.clicked.fire() +} diff --git a/newctrl/checkbox_darwin.go b/newctrl/checkbox_darwin.go new file mode 100644 index 0000000..741b163 --- /dev/null +++ b/newctrl/checkbox_darwin.go @@ -0,0 +1,55 @@ +// 16 july 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type checkbox struct { + *controlSingleObject + toggled *event +} + +func newCheckbox(text string) *checkbox { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + c := &checkbox{ + controlSingleObject: newControlSingleObject(C.newCheckbox()), + toggled: newEvent(), + } + C.buttonSetText(c._id, ctext) + C.checkboxSetDelegate(c._id, unsafe.Pointer(c)) + return c +} + +func (c *checkbox) OnToggled(e func()) { + c.toggled.set(e) +} + +func (c *checkbox) Text() string { + return C.GoString(C.buttonText(c._id)) +} + +func (c *checkbox) SetText(text string) { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + C.buttonSetText(c._id, ctext) +} + +func (c *checkbox) Checked() bool { + return fromBOOL(C.checkboxChecked(c._id)) +} + +func (c *checkbox) SetChecked(checked bool) { + C.checkboxSetChecked(c._id, toBOOL(checked)) +} + +//export checkboxToggled +func checkboxToggled(xc unsafe.Pointer) { + c := (*checkbox)(unsafe.Pointer(xc)) + c.toggled.fire() +} diff --git a/newctrl/label_darwin.go b/newctrl/label_darwin.go new file mode 100644 index 0000000..1233735 --- /dev/null +++ b/newctrl/label_darwin.go @@ -0,0 +1,71 @@ +// 16 july 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type label struct { + *controlSingleObject +} + +func newLabel(text string) Label { + l := &label{ + controlSingleObject: newControlSingleObject(C.newLabel()), + } + l.SetText(text) + return l +} + +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 +} + +/*TODO +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) +} +*/ diff --git a/newctrl/textfield_darwin.go b/newctrl/textfield_darwin.go new file mode 100644 index 0000000..c9ca78b --- /dev/null +++ b/newctrl/textfield_darwin.go @@ -0,0 +1,75 @@ +// 16 july 2014 + +package ui + +import ( + "unsafe" +) + +// #include "objc_darwin.h" +import "C" + +type textfield struct { + *controlSingleObject + changed *event + invalid C.id + chainpreferredSize func(d *sizing) (int, int) +} + +func finishNewTextField(id C.id) *textfield { + t := &textfield{ + controlSingleObject: newControlSingleObject(id), + changed: newEvent(), + } + C.textfieldSetDelegate(t._id, unsafe.Pointer(t)) + t.chainpreferredSize = t.fpreferredSize + t.fpreferredSize = t.xpreferredSize + return t +} + +func newTextField() *textfield { + return finishNewTextField(C.newTextField()) +} + +func newPasswordField() *textfield { + return finishNewTextField(C.newPasswordField()) +} + +func (t *textfield) Text() string { + return C.GoString(C.textfieldText(t._id)) +} + +func (t *textfield) SetText(text string) { + ctext := C.CString(text) + defer C.free(unsafe.Pointer(ctext)) + C.textfieldSetText(t._id, ctext) +} + +func (t *textfield) OnChanged(f func()) { + t.changed.set(f) +} + +func (t *textfield) Invalid(reason string) { + if t.invalid != nil { + C.textfieldCloseInvalidPopover(t.invalid) + t.invalid = nil + } + if reason == "" { + return + } + creason := C.CString(reason) + defer C.free(unsafe.Pointer(creason)) + t.invalid = C.textfieldOpenInvalidPopover(t._id, creason) +} + +//export textfieldChanged +func textfieldChanged(data unsafe.Pointer) { + t := (*textfield)(data) + t.changed.fire() +} + +func (t *textfield) xpreferredSize(d *sizing) (width, height int) { + _, height = t.chainpreferredSize(d) + // the returned width is based on the contents; use this instead + return C.textfieldWidth, height +}