clui/radio.go

112 lines
2.4 KiB
Go
Raw Normal View History

2015-09-21 20:54:39 -07:00
package clui
import (
2015-10-20 09:43:56 -07:00
xs "github.com/huandu/xstrings"
2015-10-16 10:27:43 -07:00
term "github.com/nsf/termbox-go"
2015-09-21 20:54:39 -07:00
)
/*
2015-10-16 10:27:43 -07:00
CheckBox control. It can be two-state one(on and off) - it is default mode - or tree-state.
State values are 0=off, 1=on, 2=third state
Minimal width of a checkbox cannot be less than 3
Own methods:
Get3State, Set3State, GetState, SetState
2015-09-21 20:54:39 -07:00
*/
type Radio struct {
2015-10-16 10:27:43 -07:00
ControlBase
group *RadioGroup
selected bool
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func NewRadio(view View, parent Control, width int, title string, scale int) *Radio {
c := new(Radio)
2015-10-20 09:43:56 -07:00
if width == AutoSize {
width = xs.Len(title) + 4
}
2015-10-16 10:27:43 -07:00
c.view = view
c.parent = parent
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
c.SetSize(width, 1) // TODO: only one line checkboxes are supported at that moment
c.SetConstraints(width, 1)
c.SetTitle(title)
c.SetTabStop(true)
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if parent != nil {
parent.AddChild(c, scale)
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
return c
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func (c *Radio) Repaint() {
x, y := c.Pos()
w, h := c.Size()
canvas := c.view.Canvas()
tm := c.view.Screen().Theme()
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
fg, bg := RealColor(tm, c.fg, ColorControlText), RealColor(tm, c.bg, ColorControlBack)
if !c.Enabled() {
fg, bg = RealColor(tm, c.fg, ColorControlDisabledText), RealColor(tm, c.bg, ColorControlDisabledBack)
2015-10-19 12:05:43 -07:00
} else if c.Active() {
fg, bg = RealColor(tm, c.fg, ColorControlActiveText), RealColor(tm, c.bg, ColorControlActiveBack)
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
parts := []rune(tm.SysObject(ObjRadio))
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
cOpen, cClose, cEmpty, cCheck := parts[0], parts[1], parts[2], parts[3]
cState := cEmpty
if c.selected {
cState = cCheck
}
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
canvas.FillRect(x, y, w, h, term.Cell{Ch: ' ', Bg: bg})
2015-09-21 20:54:39 -07:00
if w < 3 {
return
}
2015-10-16 10:27:43 -07:00
canvas.PutSymbol(x, y, term.Cell{Ch: cOpen, Fg: fg, Bg: bg})
canvas.PutSymbol(x+2, y, term.Cell{Ch: cClose, Fg: fg, Bg: bg})
canvas.PutSymbol(x+1, y, term.Cell{Ch: cState, Fg: fg, Bg: bg})
2015-09-21 20:54:39 -07:00
if w < 5 {
return
}
2015-10-16 10:27:43 -07:00
shift, text := AlignText(c.title, w-4, c.align)
canvas.PutText(x+4+shift, y, text, fg, bg)
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func (c *Radio) ProcessEvent(event Event) bool {
if (!c.Active() && event.Type == EventKey) || !c.Enabled() {
2015-09-21 20:54:39 -07:00
return false
}
2015-10-16 10:27:43 -07:00
if (event.Type == EventKey && event.Key == term.KeySpace) || event.Type == EventMouse {
if c.group == nil {
c.SetSelected(true)
} else {
c.group.SelectItem(c)
2015-09-21 20:54:39 -07:00
}
return true
}
return false
}
2015-10-16 10:27:43 -07:00
// Sets the current state of CheckBox
// Value must be 0/1 if 3State is off
// or 0/1/2 if 3State is on
func (c *Radio) SetSelected(val bool) {
c.selected = val
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func (c *Radio) Selected() bool {
return c.selected
2015-09-21 20:54:39 -07:00
}
2015-10-16 10:27:43 -07:00
func (c *Radio) SetGroup(group *RadioGroup) {
c.group = group
2015-09-21 20:54:39 -07:00
}