User interface toolkit for Go with support for SDL2 and HTML Canvas render targets.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

64 lines
1.5 KiB

  1. package ui
  2. // Checkbox combines a CheckButton with a widget like a Label.
  3. type Checkbox struct {
  4. Frame
  5. button *CheckButton
  6. child Widget
  7. }
  8. // NewCheckbox creates a new Checkbox.
  9. func NewCheckbox(name string, boolVar *bool, child Widget) *Checkbox {
  10. return makeCheckbox(name, boolVar, nil, "", child)
  11. }
  12. // NewRadiobox creates a new Checkbox in radio mode.
  13. func NewRadiobox(name string, stringVar *string, value string, child Widget) *Checkbox {
  14. return makeCheckbox(name, nil, stringVar, value, child)
  15. }
  16. // makeCheckbox constructs an appropriate type of checkbox.
  17. func makeCheckbox(name string, boolVar *bool, stringVar *string, value string, child Widget) *Checkbox {
  18. // Our custom checkbutton widget.
  19. mark := NewFrame(name + "_mark")
  20. w := &Checkbox{
  21. child: child,
  22. }
  23. if boolVar != nil {
  24. w.button = NewCheckButton(name+"_button", boolVar, mark)
  25. } else if stringVar != nil {
  26. w.button = NewRadioButton(name+"_button", stringVar, value, mark)
  27. }
  28. w.Frame.Setup()
  29. // Forward clicks on the child widget to the CheckButton.
  30. for _, e := range []Event{MouseOver, MouseOut, MouseUp, MouseDown} {
  31. func(e Event) {
  32. w.child.Handle(e, func(ed EventData) error {
  33. return w.button.Event(e, ed)
  34. })
  35. }(e)
  36. }
  37. w.Pack(w.button, Pack{
  38. Side: W,
  39. })
  40. w.Pack(w.child, Pack{
  41. Side: W,
  42. })
  43. return w
  44. }
  45. // Child returns the child widget.
  46. func (w *Checkbox) Child() Widget {
  47. return w.child
  48. }
  49. // Supervise the checkbutton inside the widget.
  50. func (w *Checkbox) Supervise(s *Supervisor) {
  51. s.Add(w.button)
  52. s.Add(w.child)
  53. }