combobox.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import "fmt"
  4. // combbox
  5. type ComboBox struct {
  6. BaseWidget
  7. }
  8. func NewComboBox(parent Widget, attributes ...*WidgetAttr) *ComboBox {
  9. theme := checkInitUseTheme(attributes)
  10. iid := makeNamedWidgetId(parent, "atk_combobox")
  11. attributes = append(attributes, &WidgetAttr{"textvariable", variableId(iid)})
  12. info := CreateWidgetInfo(iid, WidgetTypeComboBox, theme, attributes)
  13. if info == nil {
  14. return nil
  15. }
  16. w := &ComboBox{}
  17. w.id = iid
  18. w.info = info
  19. evalSetValue(variableId(iid), "")
  20. RegisterWidget(w)
  21. return w
  22. }
  23. func (w *ComboBox) Attach(id string) error {
  24. info, err := CheckWidgetInfo(id, WidgetTypeComboBox)
  25. if err != nil {
  26. return err
  27. }
  28. w.id = id
  29. w.info = info
  30. RegisterWidget(w)
  31. return nil
  32. }
  33. func (w *ComboBox) SetFont(font Font) error {
  34. if font == nil {
  35. return ErrInvalid
  36. }
  37. return eval(fmt.Sprintf("%v configure -font {%v}", w.id, font.Id()))
  38. }
  39. func (w *ComboBox) Font() Font {
  40. r, err := evalAsString(fmt.Sprintf("%v cget -font", w.id))
  41. return parserFontResult(r, err)
  42. }
  43. func (w *ComboBox) SetBackground(color string) error {
  44. setObjText("atk_tmp_text", color)
  45. return eval(fmt.Sprintf("%v configure -background $atk_tmp_text", w.id))
  46. }
  47. func (w *ComboBox) Background() string {
  48. r, _ := evalAsString(fmt.Sprintf("%v cget -background", w.id))
  49. return r
  50. }
  51. func (w *ComboBox) SetForground(color string) error {
  52. setObjText("atk_tmp_text", color)
  53. return eval(fmt.Sprintf("%v configure -foreground $atk_tmp_text", w.id))
  54. }
  55. func (w *ComboBox) Forground() string {
  56. r, _ := evalAsString(fmt.Sprintf("%v cget -foreground", w.id))
  57. return r
  58. }
  59. func (w *ComboBox) SetJustify(justify Justify) error {
  60. return eval(fmt.Sprintf("%v configure -justify {%v}", w.id, justify))
  61. }
  62. func (w *ComboBox) Justify() Justify {
  63. r, err := evalAsString(fmt.Sprintf("%v cget -justify", w.id))
  64. return parserJustifyResult(r, err)
  65. }
  66. func (w *ComboBox) SetWidth(width int) error {
  67. return eval(fmt.Sprintf("%v configure -width {%v}", w.id, width))
  68. }
  69. func (w *ComboBox) Width() int {
  70. r, _ := evalAsInt(fmt.Sprintf("%v cget -width", w.id))
  71. return r
  72. }
  73. func (w *ComboBox) SetHeight(height int) error {
  74. return eval(fmt.Sprintf("%v configure -height {%v}", w.id, height))
  75. }
  76. func (w *ComboBox) Height() int {
  77. r, _ := evalAsInt(fmt.Sprintf("%v cget -height", w.id))
  78. return r
  79. }
  80. func (w *ComboBox) SetEcho(echo string) error {
  81. setObjText("atk_tmp_text", echo)
  82. return eval(fmt.Sprintf("%v configure -show $atk_tmp_text", w.id))
  83. }
  84. func (w *ComboBox) Echo() string {
  85. r, _ := evalAsString(fmt.Sprintf("%v cget -show", w.id))
  86. return r
  87. }
  88. func (w *ComboBox) SetState(state State) error {
  89. return eval(fmt.Sprintf("%v configure -state {%v}", w.id, state))
  90. }
  91. func (w *ComboBox) State() State {
  92. r, err := evalAsString(fmt.Sprintf("%v cget -state", w.id))
  93. return parserStateResult(r, err)
  94. }
  95. func (w *ComboBox) SetTakeFocus(takefocus bool) error {
  96. return eval(fmt.Sprintf("%v configure -takefocus {%v}", w.id, boolToInt(takefocus)))
  97. }
  98. func (w *ComboBox) IsTakeFocus() bool {
  99. r, _ := evalAsBool(fmt.Sprintf("%v cget -takefocus", w.id))
  100. return r
  101. }
  102. func (w *ComboBox) SetValues(values []string) error {
  103. setObjTextList("atk_tmp_textlist", values)
  104. return eval(fmt.Sprintf("%v configure -values $atk_tmp_textlist", w.id))
  105. }
  106. func (w *ComboBox) Values() []string {
  107. r, _ := evalAsStringList(fmt.Sprintf("%v cget -values", w.id))
  108. return r
  109. }
  110. func (w *ComboBox) OnSelected(fn func()) error {
  111. if fn == nil {
  112. return ErrInvalid
  113. }
  114. w.BindEvent("<<ComboboxSelected>>", func(e *Event) {
  115. fn()
  116. })
  117. return nil
  118. }
  119. func (w *ComboBox) OnEditReturn(fn func()) error {
  120. if fn == nil {
  121. return ErrInvalid
  122. }
  123. w.BindEvent("<Return>", func(e *Event) {
  124. fn()
  125. })
  126. return nil
  127. }
  128. func (w *ComboBox) Entry() *Entry {
  129. return &Entry{w.BaseWidget, nil}
  130. }
  131. func (w *ComboBox) SetCurrentText(text string) *ComboBox {
  132. setObjText("atk_tmp_text", text)
  133. eval(fmt.Sprintf("%v set $atk_tmp_text", w.id))
  134. return w
  135. }
  136. func (w *ComboBox) CurrentText() string {
  137. r, _ := evalAsString(fmt.Sprintf("%v get", w.id))
  138. return r
  139. }
  140. func (w *ComboBox) SetCurrentIndex(index int) *ComboBox {
  141. eval(fmt.Sprintf("%v current {%v}", w.id, index))
  142. return w
  143. }
  144. func (w *ComboBox) CurrentIndex() int {
  145. r, _ := evalAsInt(fmt.Sprintf("%v current", w.id))
  146. return r
  147. }
  148. func ComboBoxAttrFont(font Font) *WidgetAttr {
  149. if font == nil {
  150. return nil
  151. }
  152. return &WidgetAttr{"font", font.Id()}
  153. }
  154. func ComboBoxAttrBackground(color string) *WidgetAttr {
  155. return &WidgetAttr{"background", color}
  156. }
  157. func ComboBoxAttrForground(color string) *WidgetAttr {
  158. return &WidgetAttr{"foreground", color}
  159. }
  160. func ComboBoxAttrJustify(justify Justify) *WidgetAttr {
  161. return &WidgetAttr{"justify", justify}
  162. }
  163. func ComboBoxAttrWidth(width int) *WidgetAttr {
  164. return &WidgetAttr{"width", width}
  165. }
  166. func ComboBoxAttrHeight(height int) *WidgetAttr {
  167. return &WidgetAttr{"height", height}
  168. }
  169. func ComboBoxAttrEcho(echo string) *WidgetAttr {
  170. return &WidgetAttr{"show", echo}
  171. }
  172. func ComboBoxAttrState(state State) *WidgetAttr {
  173. return &WidgetAttr{"state", state}
  174. }
  175. func ComboBoxAttrTakeFocus(takefocus bool) *WidgetAttr {
  176. return &WidgetAttr{"takefocus", boolToInt(takefocus)}
  177. }
  178. func ComboBoxAttrValues(values []string) *WidgetAttr {
  179. return &WidgetAttr{"values", values}
  180. }