clui/frame.go

154 lines
3.4 KiB
Go
Raw Normal View History

2015-09-21 20:54:39 -07:00
package clui
/*
2015-10-28 16:43:42 -07:00
Frame is a decorative control and container - frame with optional title.
All area inside a frame is transparent. Frame can be used as spacer element
- set border to BorderNone and use that control in any place where a spacer
is required
2015-09-21 20:54:39 -07:00
*/
type Frame struct {
2015-10-16 10:27:43 -07:00
ControlBase
2015-10-19 15:09:25 -07:00
border BorderStyle
children []Control
pack PackType
2015-09-21 20:54:39 -07:00
}
2015-10-28 16:43:42 -07:00
/*
NewFrame creates a new frame.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
2015-10-29 10:36:18 -07:00
width and heigth - are minimal size of the control.
2015-10-28 16:43:42 -07:00
bs - type of border: no border, single or double.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
2015-10-16 10:27:43 -07:00
func NewFrame(view View, parent Control, width, height int, bs BorderStyle, scale int) *Frame {
2015-09-21 20:54:39 -07:00
f := new(Frame)
2015-10-20 09:43:56 -07:00
if width == AutoSize {
width = 5
}
if height == AutoSize {
height = 3
}
2015-09-21 20:54:39 -07:00
f.SetSize(width, height)
2015-10-16 10:27:43 -07:00
f.SetConstraints(width, height)
f.border = bs
f.view = view
f.parent = parent
f.SetTabStop(false)
f.padX, f.padY = 0, 0
if bs != BorderNone {
f.padSide, f.padTop = 1, 1
} else {
f.padSide, f.padTop = 0, 0
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if parent != nil {
parent.AddChild(f, scale)
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
return f
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func (f *Frame) repaintChildren() {
for _, ctrl := range f.children {
ctrl.Repaint()
2015-09-21 20:54:39 -07:00
}
}
2015-10-28 16:43:42 -07:00
// Repaint draws the control on its View surface
2015-10-16 10:27:43 -07:00
func (f *Frame) Repaint() {
f.repaintChildren()
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if f.border == BorderNone {
return
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
tm := f.view.Screen().Theme()
canvas := f.view.Canvas()
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
x, y := f.Pos()
w, h := f.Size()
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
fg, bg := RealColor(tm, f.fg, ColorViewText), RealColor(tm, f.bg, ColorViewBack)
2015-10-26 13:58:54 -07:00
var chars string
2015-10-16 10:27:43 -07:00
if f.border == BorderDouble {
chars = tm.SysObject(ObjDoubleBorder)
} else {
chars = tm.SysObject(ObjSingleBorder)
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
canvas.DrawFrame(x, y, w, h, fg, bg, chars)
2015-09-21 20:54:39 -07:00
if f.title != "" {
text := Ellipsize(f.title, w-2)
2015-10-16 10:27:43 -07:00
canvas.PutText(x+1, y, text, fg, bg)
2015-09-21 20:54:39 -07:00
}
}
2015-10-28 16:43:42 -07:00
// RecalculateConstraints used by containers to recalculate new minimal size
// depending on its children constraints after a new child is added
2015-10-16 10:27:43 -07:00
func (f *Frame) RecalculateConstraints() {
width, height := f.Constraints()
minW, minH := CalculateMinimalSize(f)
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
newW, newH := width, height
if minW > newW {
newW = minW
}
if minH > newH {
newH = minH
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if newW != width || newH != height {
f.SetConstraints(newW, newH)
}
2015-10-17 01:16:19 -07:00
if f.parent != nil {
f.parent.RecalculateConstraints()
} else if f.view != nil {
f.view.RecalculateConstraints()
}
2015-09-21 20:54:39 -07:00
}
2015-10-28 16:43:42 -07:00
// AddChild adds a new child to a container. After adding
// a new child the frame automatically recalculates its
// its minimal size
2015-10-16 10:27:43 -07:00
func (f *Frame) AddChild(c Control, scale int) {
if f.view.ChildExists(c) {
// Frame: Cannot add the same control twice
return
2015-10-16 10:27:43 -07:00
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
c.SetScale(scale)
f.children = append(f.children, c)
f.RecalculateConstraints()
f.view.RegisterControl(c)
2015-09-21 20:54:39 -07:00
}
2015-10-28 16:43:42 -07:00
// Children returns the list of container child controls
2015-10-16 10:27:43 -07:00
func (f *Frame) Children() []Control {
return f.children
2015-09-21 20:54:39 -07:00
}
2015-10-28 16:43:42 -07:00
// SetPack changes the direction of children packing.
// Changing pack type on the fly is not always possible:
// it does nothing if a frame already contains children
2015-10-16 10:27:43 -07:00
func (f *Frame) SetPack(pk PackType) {
if len(f.children) > 0 {
// Control already has children
return
2015-10-16 10:27:43 -07:00
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
f.pack = pk
2015-09-21 20:54:39 -07:00
}
2015-10-28 16:43:42 -07:00
// Pack returns direction in which a container packs
// its children: horizontal or vertical
2015-10-16 10:27:43 -07:00
func (f *Frame) Pack() PackType {
return f.pack
2015-09-21 20:54:39 -07:00
}