clui/frame.go

95 lines
1.9 KiB
Go
Raw Normal View History

2015-09-21 20:54:39 -07:00
package clui
import (
xs "github.com/huandu/xstrings"
)
2015-09-21 20:54:39 -07:00
/*
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 {
BaseControl
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.
*/
func CreateFrame(parent Control, width, height int, bs BorderStyle, scale int) *Frame {
2015-09-21 20:54:39 -07:00
f := new(Frame)
f.BaseControl = NewBaseControl()
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.parent = parent
f.SetTabStop(false)
f.scale = scale
2015-10-16 10:27:43 -07:00
f.gapX, f.gapY = 0, 0
if bs == BorderNone {
f.padX, f.padY = 0, 0
2015-10-16 10:27:43 -07:00
} else {
f.padX, f.padY = 1, 1
2015-10-16 10:27:43 -07:00
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if parent != nil {
parent.AddChild(f)
2015-10-16 10:27:43 -07:00
}
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-28 16:43:42 -07:00
// Repaint draws the control on its View surface
func (f *Frame) Draw() {
2018-03-13 22:14:40 -07:00
if f.hidden {
return
}
PushAttributes()
defer PopAttributes()
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if f.border == BorderNone {
f.DrawChildren()
2015-10-16 10:27:43 -07:00
return
}
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
fg, bg := RealColor(f.fg, ColorViewText), RealColor(f.bg, ColorViewBack)
2015-09-21 20:54:39 -07:00
SetTextColor(fg)
SetBackColor(bg)
DrawFrame(x, y, w, h, f.border)
2015-09-21 20:54:39 -07:00
if f.title != "" {
str := f.title
raw := UnColorizeText(str)
if xs.Len(raw) > w-2 {
str = SliceColorized(str, 0, w-2-3) + "..."
}
DrawText(x+1, y, str)
2015-09-21 20:54:39 -07:00
}
f.DrawChildren()
2015-09-21 20:54:39 -07:00
}