2018-08-02 02:52:09 +00:00
|
|
|
package ui
|
|
|
|
|
2018-12-30 22:22:50 +00:00
|
|
|
import "git.kirsle.net/apps/doodle/lib/render"
|
2018-08-02 02:52:09 +00:00
|
|
|
|
|
|
|
// Checkbox combines a CheckButton with a widget like a Label.
|
|
|
|
type Checkbox struct {
|
|
|
|
Frame
|
|
|
|
button *CheckButton
|
|
|
|
child Widget
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCheckbox creates a new Checkbox.
|
|
|
|
func NewCheckbox(name string, boolVar *bool, child Widget) *Checkbox {
|
2018-08-11 00:19:47 +00:00
|
|
|
return makeCheckbox(name, boolVar, nil, "", child)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRadiobox creates a new Checkbox in radio mode.
|
|
|
|
func NewRadiobox(name string, stringVar *string, value string, child Widget) *Checkbox {
|
|
|
|
return makeCheckbox(name, nil, stringVar, value, child)
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeCheckbox constructs an appropriate type of checkbox.
|
|
|
|
func makeCheckbox(name string, boolVar *bool, stringVar *string, value string, child Widget) *Checkbox {
|
2018-08-02 02:52:09 +00:00
|
|
|
// Our custom checkbutton widget.
|
|
|
|
mark := NewFrame(name + "_mark")
|
|
|
|
|
|
|
|
w := &Checkbox{
|
2018-08-11 00:19:47 +00:00
|
|
|
child: child,
|
|
|
|
}
|
|
|
|
if boolVar != nil {
|
|
|
|
w.button = NewCheckButton(name+"_button", boolVar, mark)
|
|
|
|
} else if stringVar != nil {
|
|
|
|
w.button = NewRadioButton(name+"_button", stringVar, value, mark)
|
2018-08-02 02:52:09 +00:00
|
|
|
}
|
|
|
|
w.Frame.Setup()
|
|
|
|
|
|
|
|
// Forward clicks on the child widget to the CheckButton.
|
2018-08-17 03:37:19 +00:00
|
|
|
for _, e := range []Event{MouseOver, MouseOut, MouseUp, MouseDown} {
|
|
|
|
func(e Event) {
|
2018-08-02 02:52:09 +00:00
|
|
|
w.child.Handle(e, func(p render.Point) {
|
|
|
|
w.button.Event(e, p)
|
|
|
|
})
|
|
|
|
}(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Pack(w.button, Pack{
|
|
|
|
Anchor: W,
|
|
|
|
})
|
|
|
|
w.Pack(w.child, Pack{
|
|
|
|
Anchor: W,
|
|
|
|
})
|
|
|
|
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2018-08-11 00:19:47 +00:00
|
|
|
// Child returns the child widget.
|
|
|
|
func (w *Checkbox) Child() Widget {
|
|
|
|
return w.child
|
|
|
|
}
|
|
|
|
|
2018-08-02 02:52:09 +00:00
|
|
|
// Supervise the checkbutton inside the widget.
|
|
|
|
func (w *Checkbox) Supervise(s *Supervisor) {
|
|
|
|
s.Add(w.button)
|
|
|
|
s.Add(w.child)
|
|
|
|
}
|