scrollbar.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import (
  4. "fmt"
  5. "strings"
  6. )
  7. // scrollbar
  8. type ScrollBar struct {
  9. BaseWidget
  10. command *CommandEx
  11. }
  12. func NewScrollBar(parent Widget, orient Orient, attributes ...*WidgetAttr) *ScrollBar {
  13. theme := checkInitUseTheme(attributes)
  14. iid := makeNamedWidgetId(parent, "atk_scrollbar")
  15. attributes = append(attributes, &WidgetAttr{"orient", orient})
  16. info := CreateWidgetInfo(iid, WidgetTypeScrollBar, theme, attributes)
  17. if info == nil {
  18. return nil
  19. }
  20. w := &ScrollBar{}
  21. w.id = iid
  22. w.info = info
  23. RegisterWidget(w)
  24. return w
  25. }
  26. func (w *ScrollBar) Attach(id string) error {
  27. info, err := CheckWidgetInfo(id, WidgetTypeScrollBar)
  28. if err != nil {
  29. return err
  30. }
  31. w.id = id
  32. w.info = info
  33. RegisterWidget(w)
  34. return nil
  35. }
  36. func (w *ScrollBar) SetOrient(orient Orient) error {
  37. return eval(fmt.Sprintf("%v configure -orient {%v}", w.id, orient))
  38. }
  39. func (w *ScrollBar) Orient() Orient {
  40. r, err := evalAsString(fmt.Sprintf("%v cget -orient", w.id))
  41. return parserOrientResult(r, err)
  42. }
  43. func (w *ScrollBar) SetTakeFocus(takefocus bool) error {
  44. return eval(fmt.Sprintf("%v configure -takefocus {%v}", w.id, boolToInt(takefocus)))
  45. }
  46. func (w *ScrollBar) IsTakeFocus() bool {
  47. r, _ := evalAsBool(fmt.Sprintf("%v cget -takefocus", w.id))
  48. return r
  49. }
  50. func (w *ScrollBar) SetScroll(first float64, last float64) error {
  51. err := eval(fmt.Sprintf("%v set %v %v", w.id, first, last))
  52. return err
  53. }
  54. func (w *ScrollBar) SetScrollArgs(args []string) error {
  55. err := eval(fmt.Sprintf("%v set %v", w.id, strings.Join(args, " ")))
  56. return err
  57. }
  58. func (w *ScrollBar) OnCommandEx(fn func([]string) error) error {
  59. if fn == nil {
  60. return ErrInvalid
  61. }
  62. if w.command == nil {
  63. w.command = &CommandEx{}
  64. bindCommandEx(w.id, "command", w.command.Invoke)
  65. }
  66. w.command.Bind(fn)
  67. return nil
  68. }
  69. func ScrollBarAttrOrient(orient Orient) *WidgetAttr {
  70. return &WidgetAttr{"orient", orient}
  71. }
  72. func ScrollBarAttrTakeFocus(takefocus bool) *WidgetAttr {
  73. return &WidgetAttr{"takefocus", boolToInt(takefocus)}
  74. }