clui/radio.go

134 lines
3.2 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-29 10:36:18 -07:00
Radio button control. Unite a few radios in one radio group to
make a user select one of available choices.
2015-09-21 20:54:39 -07:00
*/
type Radio struct {
BaseControl
2015-10-16 10:27:43 -07:00
group *RadioGroup
selected bool
2015-09-21 20:54:39 -07:00
}
2015-10-29 10:36:18 -07:00
/*
NewRadio creates a new radio button.
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.
width - is minimal width of the control.
title - radio title.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateRadio(parent Control, width int, title string, scale int) *Radio {
2015-10-16 10:27:43 -07:00
c := new(Radio)
c.BaseControl = NewBaseControl()
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.parent = parent
2015-09-21 20:54:39 -07:00
2015-10-29 10:36:18 -07:00
c.SetSize(width, 1) // TODO: only one line heigth is supported at that moment
2015-10-16 10:27:43 -07:00
c.SetConstraints(width, 1)
c.SetTitle(title)
c.SetTabStop(true)
c.SetScale(scale)
2015-09-21 20:54:39 -07:00
2015-10-16 10:27:43 -07:00
if parent != nil {
parent.AddChild(c)
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-29 10:36:18 -07:00
// Repaint draws the control on its View surface
func (c *Radio) Draw() {
2018-03-13 22:14:40 -07:00
if c.hidden {
return
}
PushAttributes()
defer PopAttributes()
2015-10-16 10:27:43 -07:00
x, y := c.Pos()
w, h := c.Size()
2015-09-21 20:54:39 -07:00
fg, bg := RealColor(c.fg, ColorControlText), RealColor(c.bg, ColorControlBack)
2015-10-16 10:27:43 -07:00
if !c.Enabled() {
fg, bg = RealColor(c.fg, ColorControlDisabledText), RealColor(c.bg, ColorControlDisabledBack)
2015-10-19 12:05:43 -07:00
} else if c.Active() {
fg, bg = RealColor(c.fg, ColorControlActiveText), RealColor(c.bg, ColorControlActiveBack)
2015-09-21 20:54:39 -07:00
}
parts := []rune(SysObject(ObjRadio))
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
SetTextColor(fg)
SetBackColor(bg)
FillRect(x, y, w, h, ' ')
2015-09-21 20:54:39 -07:00
if w < 3 {
return
}
PutChar(x, y, cOpen)
PutChar(x+2, y, cClose)
PutChar(x+1, y, cState)
2015-09-21 20:54:39 -07:00
if w < 5 {
return
}
shift, text := AlignColorizedText(c.title, w-4, c.align)
DrawText(x+4+shift, y, text)
2015-09-21 20:54:39 -07:00
}
2015-10-29 10:36:18 -07:00
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent.
The control processes only space button and mouse clicks to make control selected. Deselecting control is not possible: one has to click another radio of the radio group to deselect this button
*/
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-29 10:36:18 -07:00
// SetSelected makes the button selected. One should not use
// the method directly, it is for RadioGroup control
2015-10-16 10:27:43 -07:00
func (c *Radio) SetSelected(val bool) {
c.selected = val
2015-09-21 20:54:39 -07:00
}
2015-10-29 10:36:18 -07:00
// Selected returns if the radio is selected
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-29 10:36:18 -07:00
// SetGroup sets the radio group to which the radio belongs
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
}