button.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package widget
  2. import "gitlab.com/beoran/ebsgo/zori/types"
  3. import "gitlab.com/beoran/ebsgo/zori/event"
  4. // import _ "gitlab.com/beoran/ebsgo/zori/backend"
  5. // import "gitlab.com/beoran/ebsgo/zori/state"
  6. // import "gitlab.com/beoran/ebsgo/zori/style"
  7. import "gitlab.com/beoran/ebsgo/monolog"
  8. type Button struct {
  9. * Captioned
  10. }
  11. /** Handles a mouse axis event and pass it on. */
  12. func (bu * Button) MouseAxes(ev event.Mouse) event.Result {
  13. if bu.IsInside(ev.X(), ev.Y()) {
  14. monolog.Info("Button motion: %d %d.\n", ev.X(), ev.Y())
  15. bu.SetHovered(true)
  16. } else {
  17. bu.SetHovered(false)
  18. }
  19. return event.Pass
  20. }
  21. /** Handles a mouse click or activation and set the
  22. * button and it's parent's result. */
  23. func (bu * Button) Action(ev event.Action) event.Result {
  24. bu.Result = interface{}(bu.ID)
  25. monolog.Info("Button clicked: %d.\n", bu.ID)
  26. menu, is_menu := bu.Parent().(*Menu)
  27. if is_menu {
  28. /* If the parent is a menu, also set it's result. */
  29. menu.Result = bu.ID
  30. }
  31. /* Basic.Action will call the action callback. */
  32. return bu.Basic.Action(ev)
  33. }
  34. /** Handles a mouse click event and pass it on. */
  35. func (bu* Button) MouseButtonPress(ev event.Mouse) event.Result {
  36. if bu.IsInside(ev.X(), ev.Y()) {
  37. monolog.Info("Mouse clicked: %v!\n", bu)
  38. ae := event.Action{}
  39. return bu.Dispatch(ae)
  40. } else {
  41. monolog.Info("Mouse NOT clicked: %d %d in %d %d %d %d!\n", ev.X(), ev.Y(), bu.X, bu.Y, bu.W, bu.H )
  42. }
  43. return event.Pass
  44. }
  45. func (bu * Button) Dispatch(ev event.Event) event.Result {
  46. return DispatchRecursive(bu, ev)
  47. }
  48. func (bu * Button) Draw(ev event.Draw) event.Result {
  49. bu.DrawBackground()
  50. bu.Captioned.Draw(ev)
  51. return event.Pass
  52. }
  53. func NewButton(parent Widget, bounds * types.Box, text string, cb ActionCallback) * Button {
  54. but := &Button{}
  55. but.Captioned = NewCaptioned(parent, bounds, text)
  56. return but
  57. }