menubutton.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import "fmt"
  4. // menubutton
  5. type MenuButton struct {
  6. BaseWidget
  7. }
  8. func NewMenuButton(parent Widget, text string, attributes ...*WidgetAttr) *MenuButton {
  9. theme := checkInitUseTheme(attributes)
  10. iid := makeNamedWidgetId(parent, "atk_menubutton")
  11. attributes = append(attributes, &WidgetAttr{"text", text})
  12. info := CreateWidgetInfo(iid, WidgetTypeMenuButton, theme, attributes)
  13. if info == nil {
  14. return nil
  15. }
  16. w := &MenuButton{}
  17. w.id = iid
  18. w.info = info
  19. RegisterWidget(w)
  20. return w
  21. }
  22. func (w *MenuButton) Attach(id string) error {
  23. info, err := CheckWidgetInfo(id, WidgetTypeMenuButton)
  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 *MenuButton) SetText(text string) error {
  33. setObjText("atk_tmp_text", text)
  34. return eval(fmt.Sprintf("%v configure -text $atk_tmp_text", w.id))
  35. }
  36. func (w *MenuButton) Text() string {
  37. r, _ := evalAsString(fmt.Sprintf("%v cget -text", w.id))
  38. return r
  39. }
  40. func (w *MenuButton) SetWidth(width int) error {
  41. return eval(fmt.Sprintf("%v configure -width {%v}", w.id, width))
  42. }
  43. func (w *MenuButton) Width() int {
  44. r, _ := evalAsInt(fmt.Sprintf("%v cget -width", w.id))
  45. return r
  46. }
  47. func (w *MenuButton) SetImage(image *Image) error {
  48. if image == nil {
  49. return ErrInvalid
  50. }
  51. return eval(fmt.Sprintf("%v configure -image {%v}", w.id, image.Id()))
  52. }
  53. func (w *MenuButton) Image() *Image {
  54. r, err := evalAsString(fmt.Sprintf("%v cget -image", w.id))
  55. return parserImageResult(r, err)
  56. }
  57. func (w *MenuButton) SetCompound(compound Compound) error {
  58. return eval(fmt.Sprintf("%v configure -compound {%v}", w.id, compound))
  59. }
  60. func (w *MenuButton) Compound() Compound {
  61. r, err := evalAsString(fmt.Sprintf("%v cget -compound", w.id))
  62. return parserCompoundResult(r, err)
  63. }
  64. func (w *MenuButton) SetPaddingN(padx int, pady int) error {
  65. if w.info.IsTtk {
  66. return eval(fmt.Sprintf("%v configure -padding {%v %v}", w.id, padx, pady))
  67. }
  68. return eval(fmt.Sprintf("%v configure -padx {%v} -pady {%v}", w.id, padx, pady))
  69. }
  70. func (w *MenuButton) PaddingN() (int, int) {
  71. var r string
  72. var err error
  73. if w.info.IsTtk {
  74. r, err = evalAsString(fmt.Sprintf("%v cget -padding", w.id))
  75. } else {
  76. r1, _ := evalAsString(fmt.Sprintf("%v cget -padx", w.id))
  77. r2, _ := evalAsString(fmt.Sprintf("%v cget -pady", w.id))
  78. r = r1 + " " + r2
  79. }
  80. return parserPaddingResult(r, err)
  81. }
  82. func (w *MenuButton) SetPadding(pad Pad) error {
  83. return w.SetPaddingN(pad.X, pad.Y)
  84. }
  85. func (w *MenuButton) Padding() Pad {
  86. x, y := w.PaddingN()
  87. return Pad{x, y}
  88. }
  89. func (w *MenuButton) SetState(state State) error {
  90. return eval(fmt.Sprintf("%v configure -state {%v}", w.id, state))
  91. }
  92. func (w *MenuButton) State() State {
  93. r, err := evalAsString(fmt.Sprintf("%v cget -state", w.id))
  94. return parserStateResult(r, err)
  95. }
  96. func (w *MenuButton) SetTakeFocus(takefocus bool) error {
  97. return eval(fmt.Sprintf("%v configure -takefocus {%v}", w.id, boolToInt(takefocus)))
  98. }
  99. func (w *MenuButton) IsTakeFocus() bool {
  100. r, _ := evalAsBool(fmt.Sprintf("%v cget -takefocus", w.id))
  101. return r
  102. }
  103. func (w *MenuButton) SetDirection(direction Direction) error {
  104. return eval(fmt.Sprintf("%v configure -direction {%v}", w.id, direction))
  105. }
  106. func (w *MenuButton) Direction() Direction {
  107. r, err := evalAsString(fmt.Sprintf("%v cget -direction", w.id))
  108. return parserDirectionResult(r, err)
  109. }
  110. func (w *MenuButton) SetMenu(menu *Menu) error {
  111. if menu == nil {
  112. return ErrInvalid
  113. }
  114. return eval(fmt.Sprintf("%v configure -menu {%v}", w.id, menu.Id()))
  115. }
  116. func (w *MenuButton) Menu() *Menu {
  117. r, err := evalAsString(fmt.Sprintf("%v cget -menu", w.id))
  118. return parserMenuResult(r, err)
  119. }
  120. func MenuButtonAttrText(text string) *WidgetAttr {
  121. return &WidgetAttr{"text", text}
  122. }
  123. func MenuButtonAttrWidth(width int) *WidgetAttr {
  124. return &WidgetAttr{"width", width}
  125. }
  126. func MenuButtonAttrImage(image *Image) *WidgetAttr {
  127. if image == nil {
  128. return nil
  129. }
  130. return &WidgetAttr{"image", image.Id()}
  131. }
  132. func MenuButtonAttrCompound(compound Compound) *WidgetAttr {
  133. return &WidgetAttr{"compound", compound}
  134. }
  135. func MenuButtonAttrPadding(padding Pad) *WidgetAttr {
  136. return &WidgetAttr{"padding", padding}
  137. }
  138. func MenuButtonAttrState(state State) *WidgetAttr {
  139. return &WidgetAttr{"state", state}
  140. }
  141. func MenuButtonAttrTakeFocus(takefocus bool) *WidgetAttr {
  142. return &WidgetAttr{"takefocus", boolToInt(takefocus)}
  143. }
  144. func MenuButtonAttrDirection(direction Direction) *WidgetAttr {
  145. return &WidgetAttr{"direction", direction}
  146. }
  147. func MenuButtonAttrMenu(menu *Menu) *WidgetAttr {
  148. if menu == nil {
  149. return nil
  150. }
  151. return &WidgetAttr{"menu", menu.Id()}
  152. }