scale.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import "fmt"
  4. // scale
  5. type Scale struct {
  6. BaseWidget
  7. command *Command
  8. }
  9. func NewScale(parent Widget, orient Orient, attributes ...*WidgetAttr) *Scale {
  10. iid := makeNamedWidgetId(parent, "atk_separator")
  11. attributes = append(attributes, &WidgetAttr{"orient", orient})
  12. info := CreateWidgetInfo(iid, WidgetTypeScale, true, attributes)
  13. if info == nil {
  14. return nil
  15. }
  16. w := &Scale{}
  17. w.id = iid
  18. w.info = info
  19. RegisterWidget(w)
  20. return w
  21. }
  22. func (w *Scale) Attach(id string) error {
  23. info, err := CheckWidgetInfo(id, WidgetTypeScale)
  24. if err != nil {
  25. return err
  26. }
  27. w.id = id
  28. w.info = info
  29. RegisterWidget(w)
  30. return nil
  31. }
  32. func (w *Scale) SetOrient(orient Orient) error {
  33. return eval(fmt.Sprintf("%v configure -orient {%v}", w.id, orient))
  34. }
  35. func (w *Scale) Orient() Orient {
  36. r, err := evalAsString(fmt.Sprintf("%v cget -orient", w.id))
  37. return parserOrientResult(r, err)
  38. }
  39. func (w *Scale) SetTakeFocus(takefocus bool) error {
  40. return eval(fmt.Sprintf("%v configure -takefocus {%v}", w.id, boolToInt(takefocus)))
  41. }
  42. func (w *Scale) IsTakeFocus() bool {
  43. r, _ := evalAsBool(fmt.Sprintf("%v cget -takefocus", w.id))
  44. return r
  45. }
  46. func (w *Scale) SetFrom(from float64) error {
  47. return eval(fmt.Sprintf("%v configure -from {%v}", w.id, from))
  48. }
  49. func (w *Scale) From() float64 {
  50. r, _ := evalAsFloat64(fmt.Sprintf("%v cget -from", w.id))
  51. return r
  52. }
  53. func (w *Scale) SetTo(to float64) error {
  54. return eval(fmt.Sprintf("%v configure -to {%v}", w.id, to))
  55. }
  56. func (w *Scale) To() float64 {
  57. r, _ := evalAsFloat64(fmt.Sprintf("%v cget -to", w.id))
  58. return r
  59. }
  60. func (w *Scale) SetValue(value float64) error {
  61. return eval(fmt.Sprintf("%v configure -value {%v}", w.id, value))
  62. }
  63. func (w *Scale) Value() float64 {
  64. r, _ := evalAsFloat64(fmt.Sprintf("%v cget -value", w.id))
  65. return r
  66. }
  67. func (w *Scale) SetLength(length int) error {
  68. return eval(fmt.Sprintf("%v configure -length {%v}", w.id, length))
  69. }
  70. func (w *Scale) Length() int {
  71. r, _ := evalAsInt(fmt.Sprintf("%v cget -length", w.id))
  72. return r
  73. }
  74. func (w *Scale) OnCommand(fn func()) error {
  75. if fn == nil {
  76. return ErrInvalid
  77. }
  78. if w.command == nil {
  79. w.command = &Command{}
  80. bindCommand(w.id, "command", w.command.Invoke)
  81. }
  82. w.command.Bind(fn)
  83. return nil
  84. }
  85. func (w *Scale) SetRange(from, to float64) error {
  86. return eval(fmt.Sprintf("%v configure -from {%v} -to {%v}", w.id, from, to))
  87. }
  88. func (w *Scale) Range() (float64, float64) {
  89. return w.From(), w.To()
  90. }
  91. func ScaleAttrOrient(orient Orient) *WidgetAttr {
  92. return &WidgetAttr{"orient", orient}
  93. }
  94. func ScaleAttrTakeFocus(takefocus bool) *WidgetAttr {
  95. return &WidgetAttr{"takefocus", boolToInt(takefocus)}
  96. }
  97. func ScaleAttrFrom(from float64) *WidgetAttr {
  98. return &WidgetAttr{"from", from}
  99. }
  100. func ScaleAttrTo(to float64) *WidgetAttr {
  101. return &WidgetAttr{"to", to}
  102. }
  103. func ScaleAttrValue(value float64) *WidgetAttr {
  104. return &WidgetAttr{"value", value}
  105. }
  106. func ScaleAttrLength(length int) *WidgetAttr {
  107. return &WidgetAttr{"length", length}
  108. }