From d5b3d877fe80616bfe4bc97a1d083c29fd21b4e7 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 14 Jun 2016 11:43:00 +0200 Subject: [PATCH] Adds Form and Grid --- form.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++++ grid.go | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 227 insertions(+) create mode 100644 form.go create mode 100644 grid.go diff --git a/form.go b/form.go new file mode 100644 index 0000000..3a3afc1 --- /dev/null +++ b/form.go @@ -0,0 +1,106 @@ +// 14 june 2016 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +import "C" + +type Form struct { + c *C.uiControl + f *C.uiForm + + children []Control +} + +// NewForm creates a new Form. +func NewForm() *Form { + f := new(Form) + + f.f = C.uiNewForm() + f.c = (*C.uiControl)(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() + } + C.uiControlDestroy(f.c) +} + +// LibuiControl returns the libui uiControl pointer that backs +// the Form. This is only used by package ui itself and should +// not be called by programs. +func (f *Form) LibuiControl() uintptr { + return uintptr(unsafe.Pointer(f.c)) +} + +// Handle returns the OS-level handle associated with this Form. +func (f *Form) Handle() uintptr { + return uintptr(C.uiControlHandle(f.c)) +} + +// Show shows the Form. +func (f *Form) Show() { + C.uiControlShow(f.c) +} + +// Hide hides the Form. +func (f *Form) Hide() { + C.uiControlHide(f.c) +} + +// Enable enables the Form. +func (f *Form) Enable() { + C.uiControlEnable(f.c) +} + +// Disable disables the Form. +func (f *Form) Disable() { + C.uiControlDisable(f.c) +} + +// 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) + + c := (*C.uiControl)(nil) + if child != nil { + c = touiControl(child.LibuiControl()) + } + + C.uiFormAppend(f.f, clabel, c, frombool(stretchy)) + freestr(clabel) + + 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)) +} + +// TODO: InsertAt + +// 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/grid.go b/grid.go new file mode 100644 index 0000000..4f52a70 --- /dev/null +++ b/grid.go @@ -0,0 +1,121 @@ +// 14 june 2016 + +package ui + +import ( + "unsafe" +) + +// #include "ui.h" +import "C" + +type Align int +const ( + AlignFill Align = iota + AlignStart + AlignCenter + AlignEnd +) + +type At int +const ( + AtLeading At = iota + AtTop + AtTrailing + AtBottom +) + +// Grid is a container Control that arranges controls in rows and columns, with +// stretchy ("expanding") rows, stretchy ("expanding") columns, cells that span +// rows and columns, and cells whose content is aligned in either direction +// rather than just filling. +type Grid struct { + c *C.uiControl + g *C.uiGrid + + children []Control +} + +// NewGrid creates a new Grid. +func NewGrid() *Grid { + g := new(Grid) + + g.g = C.uiNewGrid() + g.c = (*C.uiControl)(unsafe.Pointer(g.g)) + + return g +} + +// Destroy destroys the Grid. If the Grid has children, +// Destroy calls Destroy on those Controls as well. +func (g *Grid) Destroy() { + for len(g.children) != 0 { + c := g.children[0] + g.Delete(0) + c.Destroy() + } + C.uiControlDestroy(g.c) +} + +// LibuiControl returns the libui uiControl pointer that backs +// the Grid. This is only used by package ui itself and should +// not be called by programs. +func (g *Grid) LibuiControl() uintptr { + return uintptr(unsafe.Pointer(g.c)) +} + +// Handle returns the OS-level handle associated with this Grid. +func (g *Grid) Handle() uintptr { + return uintptr(C.uiControlHandle(g.c)) +} + +// Show shows the Grid. +func (g *Grid) Show() { + C.uiControlShow(g.c) +} + +// Hide hides the Grid. +func (g *Grid) Hide() { + C.uiControlHide(g.c) +} + +// Enable enables the Grid. +func (g *Grid) Enable() { + C.uiControlEnable(g.c) +} + +// Disable disables the Grid. +func (g *Grid) Disable() { + C.uiControlDisable(g.c) +} + +// Append adds the given control to the end of the Grid. +func (g *Grid) Append(child Control, left, top, xspan, yspan int, hexpand bool, uialign Align, vexpand bool, valign Align) { + c := (*C.uiControl)(nil) + if child != nil { + c = touiControl(child.LibuiControl()) + } + C.uiGridAppend(g.g, c, C.int(left), C.int(top), C.int(xspan), C.int(yspan), frombool(hexpand), C.uiAlign(uialign), frombool(vexpand), C.uiAlign(valign)) + g.children = append(g.children, child) +} + +// Delete deletes the nth control of the Grid. +func (g *Grid) Delete(n int) { + g.children = append(g.children[:n], g.children[n + 1:]...) + //C.uiGridDelete(g.g, C.int(n)) +} + +// TODO: InsertAt + +// Padded returns whether there is space between each control +// of the Grid. +func (g *Grid) Padded() bool { + return tobool(C.uiGridPadded(g.g)) +} + +// SetPadded controls whether there is space between each control +// of the Grid. The size of the padding is determined by the OS and +// its best practices. +func (g *Grid) SetPadded(padded bool) { + C.uiGridSetPadded(g.g, frombool(padded)) +}