diff --git a/box.go b/box.go index 486456e..65421d4 100644 --- a/box.go +++ b/box.go @@ -57,6 +57,7 @@ func (b *Box) Destroy() { // Append adds the given control to the end of the Box. func (b *Box) Append(child Control, stretchy bool) { c := (*C.uiControl)(nil) + // TODO this part is wrong for Box? if child != nil { c = touiControl(child.LibuiControl()) } diff --git a/form.go b/form.go new file mode 100644 index 0000000..6f8a282 --- /dev/null +++ b/form.go @@ -0,0 +1,71 @@ +// 12 december 2015 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +import "C" + +// Form is a Control that holds a group of Controls vertically +// with labels next to each. By default, each control has its +// preferred height; if a control is marked "stretchy", it will take +// whatever space is left over. If multiple controls are marked +// stretchy, they will be given equal shares of the leftover space. +// There can also be space between each control ("padding"). +type Form struct { + ControlBase + f *C.uiForm + children []Control +} + +// NewForm creates a new horizontal Form. +func NewForm() *Form { + f := new(Form) + + f.f = C.uiNewForm() + + f.ControlBase = NewControlBase(f, uintptr(unsafe.Pointer(f.f))) + return f +} + +// Destroy destroys the Form. If the Form has children, +// Destroy calls Destroy on those Controls as well. +func (f *Form) Destroy() { + for len(f.children) != 0 { + c := f.children[0] + f.Delete(0) + c.Destroy() + } + f.ControlBase.Destroy() +} + +// Append adds the given control to the end of the Form. +func (f *Form) Append(label string, child Control, stretchy bool) { + clabel := C.CString(label) + defer freestr(clabel) + c := touiControl(child.LibuiControl()) + C.uiFormAppend(f.f, clabel, c, frombool(stretchy)) + f.children = append(f.children, child) +} + +// Delete deletes the nth control of the Form. +func (f *Form) Delete(n int) { + f.children = append(f.children[:n], f.children[n + 1:]...) + C.uiFormDelete(f.f, C.int(n)) +} + +// Padded returns whether there is space between each control +// of the Form. +func (f *Form) Padded() bool { + return tobool(C.uiFormPadded(f.f)) +} + +// SetPadded controls whether there is space between each control +// of the Form. The size of the padding is determined by the OS and +// its best practices. +func (f *Form) SetPadded(padded bool) { + C.uiFormSetPadded(f.f, frombool(padded)) +} diff --git a/zz_controls.go b/zz_controls.go index 2875e6e..b879b39 100644 --- a/zz_controls.go +++ b/zz_controls.go @@ -28,32 +28,16 @@ func makeBasicControlsPage() ui.Control { vbox.Append(group, true) group.SetChild(ui.NewNonWrappingMultilineEntry()) -/* - entryForm = uiNewForm(); - uiFormSetPadded(entryForm, 1); - uiGroupSetChild(group, uiControl(entryForm)); - uiFormAppend(entryForm, - "Entry", - uiControl(uiNewEntry()), - 0); - uiFormAppend(entryForm, - "Password Entry", - uiControl(uiNewPasswordEntry()), - 0); - uiFormAppend(entryForm, - "Search Entry", - uiControl(uiNewSearchEntry()), - 0); - uiFormAppend(entryForm, - "Multiline Entry", - uiControl(uiNewMultilineEntry()), - 1); - uiFormAppend(entryForm, - "Multiline Entry No Wrap", - uiControl(uiNewNonWrappingMultilineEntry()), - 1); -*/ + entryForm := ui.NewForm() + entryForm.SetPadded(true) + group.SetChild(entryForm) + + entryForm.Append("Entry", ui.NewEntry(), false) + entryForm.Append("Password Entry", ui.NewPasswordEntry(), false) + entryForm.Append("Search Entry", ui.NewSearchEntry(), false) + entryForm.Append("Multiline Entry", ui.NewMultilineEntry(), true) + entryForm.Append("Multiline Entry No Wrap", ui.NewNonWrappingMultilineEntry(), true) return vbox } @@ -129,14 +113,10 @@ func makeDataChoosersPage() ui.Control { vbox.Append(ui.NewDatePicker(), false) vbox.Append(ui.NewTimePicker(), false) vbox.Append(ui.NewDateTimePicker(), false) -/* - uiBoxAppend(vbox, - uiControl(uiNewFontButton()), - 0); - uiBoxAppend(vbox, - uiControl(uiNewColorButton()), - 0); + vbox.Append(ui.NewFontButton(), false) + vbox.Append(ui.NewColorButton(), false) +/* uiBoxAppend(hbox, uiControl(uiNewVerticalSeparator()), 0); diff --git a/zz_drawtext.go b/zz_drawtext.go index 1317961..a32b14b 100644 --- a/zz_drawtext.go +++ b/zz_drawtext.go @@ -10,9 +10,12 @@ import ( "github.com/andlabs/ui" ) -var fontButton *ui.FontButton +var ( + fontButton *ui.FontButton + alignment *ui.Combobox -var attrstr *ui.AttributedString + attrstr *ui.AttributedString +) func appendWithAttributes(what string, attrs ...ui.Attribute) { start := len(attrstr.String()) @@ -44,7 +47,7 @@ func makeAttributedString() { attrstr.AppendUnattributed(", ") appendWithAttributes("text color", ui.TextColor{0.75, 0.25, 0.5, 0.75}) - attrstr.AppendUnattributed(", "); + attrstr.AppendUnattributed(", ") appendWithAttributes("text background color", ui.TextBackground{0.5, 0.5, 0.25, 0.5}) attrstr.AppendUnattributed(", ") @@ -84,7 +87,7 @@ func (areaHandler) Draw(a *ui.Area, p *ui.AreaDrawParams) { String: attrstr, DefaultFont: fontButton.Font(), Width: p.AreaWidth, - Align: ui.DrawTextAlignLeft, + Align: ui.DrawTextAlign(alignment.Selected()), }) defer tl.Free() p.Context.Text(tl, 0, 0) @@ -138,21 +141,21 @@ func setupUI() { }) vbox.Append(fontButton, false) -/* - form = uiNewForm(); - uiFormSetPadded(form, 1); + form := ui.NewForm() + form.SetPadded(true) // TODO on OS X if this is set to 1 then the window can't resize; does the form not have the concept of stretchy trailing space? - uiBoxAppend(vbox, uiControl(form), 0); + vbox.Append(form, false) - alignment = uiNewCombobox(); + alignment = ui.NewCombobox() // note that the items match with the values of the uiDrawTextAlign values - uiComboboxAppend(alignment, "Left"); - uiComboboxAppend(alignment, "Center"); - uiComboboxAppend(alignment, "Right"); - uiComboboxSetSelected(alignment, 0); // start with left alignment - uiComboboxOnSelected(alignment, onComboboxSelected, NULL); - uiFormAppend(form, "Alignment", uiControl(alignment), 0); -*/ + alignment.Append("Left") + alignment.Append("Center") + alignment.Append("Right") + alignment.SetSelected(0) // start with left alignment + alignment.OnSelected(func(*ui.Combobox) { + area.QueueRedrawAll() + }) + form.Append("Alignment", alignment, false) hbox.Append(area, true)