grid.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2018 visualfc. All rights reserved.
  2. package tk
  3. import (
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. func GridAttrColumn(n int) *LayoutAttr {
  9. return &LayoutAttr{"column", n}
  10. }
  11. func GridAttrColumnSpan(n int) *LayoutAttr {
  12. return &LayoutAttr{"columnspan", n}
  13. }
  14. func GridAttrRow(n int) *LayoutAttr {
  15. return &LayoutAttr{"row", n}
  16. }
  17. func GridAttrRowSpan(n int) *LayoutAttr {
  18. return &LayoutAttr{"rowspan", n}
  19. }
  20. func GridAttrInMaster(w Widget) *LayoutAttr {
  21. if !IsValidWidget(w) {
  22. return nil
  23. }
  24. return &LayoutAttr{"in", w.Id()}
  25. }
  26. func GridAttrIpadx(padx int) *LayoutAttr {
  27. return &LayoutAttr{"ipadx", padx}
  28. }
  29. func GridAttrIpady(pady int) *LayoutAttr {
  30. return &LayoutAttr{"ipady", pady}
  31. }
  32. func GridAttrPadx(padx int) *LayoutAttr {
  33. return &LayoutAttr{"padx", padx}
  34. }
  35. func GridAttrPady(pady int) *LayoutAttr {
  36. return &LayoutAttr{"pady", pady}
  37. }
  38. func GridAttrSticky(v Sticky) *LayoutAttr {
  39. return &LayoutAttr{"sticky", v}
  40. }
  41. type GridIndexAttr struct {
  42. key string
  43. value interface{}
  44. }
  45. func GridIndexAttrMinSize(amount int) *GridIndexAttr {
  46. return &GridIndexAttr{"minsize", amount}
  47. }
  48. func GridIndexAttrPad(amount int) *GridIndexAttr {
  49. return &GridIndexAttr{"pad", amount}
  50. }
  51. func GridIndexAttrWeight(value int) *GridIndexAttr {
  52. return &GridIndexAttr{"weight", value}
  53. }
  54. func GridIndexAttrUniform(groupname string) *GridIndexAttr {
  55. return &GridIndexAttr{"uniform", groupname}
  56. }
  57. func Grid(widget Widget, attributes ...*LayoutAttr) error {
  58. return GridList([]Widget{widget}, attributes...)
  59. }
  60. func GridRemove(widget Widget) error {
  61. if !IsValidWidget(widget) {
  62. return ErrInvalid
  63. }
  64. return eval("grid forget " + widget.Id())
  65. }
  66. var (
  67. gridAttrKeys = []string{
  68. "column", "columnspan",
  69. "row", "rowspan",
  70. "in",
  71. "ipadx", "ipady",
  72. "padx", "pady",
  73. "sticky",
  74. }
  75. gridIndexAttrKeys = []string{
  76. "minsize",
  77. "pad",
  78. "weight",
  79. "uniform",
  80. }
  81. )
  82. func GridList(widgets []Widget, attributes ...*LayoutAttr) error {
  83. var idList []string
  84. for _, w := range widgets {
  85. if IsValidWidget(w) {
  86. w = checkLayoutWidget(w)
  87. idList = append(idList, w.Id())
  88. } else {
  89. idList = append(idList, "x")
  90. }
  91. }
  92. if len(idList) == 0 {
  93. return ErrInvalid
  94. }
  95. var attrList []string
  96. for _, attr := range attributes {
  97. if attr == nil || !isValidKey(attr.key, gridAttrKeys) {
  98. continue
  99. }
  100. attrList = append(attrList, fmt.Sprintf("-%v {%v}", attr.key, attr.value))
  101. }
  102. script := fmt.Sprintf("grid %v", strings.Join(idList, " "))
  103. if len(attrList) > 0 {
  104. script += " " + strings.Join(attrList, " ")
  105. }
  106. return eval(script)
  107. }
  108. // row index from 0; -1=all
  109. func GridRowIndex(master Widget, index int, attributes ...*GridIndexAttr) error {
  110. return gridIndex(master, true, index, attributes)
  111. }
  112. // column index from 0; -1=all
  113. func GridColumnIndex(master Widget, index int, attributes ...*GridIndexAttr) error {
  114. return gridIndex(master, false, index, attributes)
  115. }
  116. func gridIndex(master Widget, row bool, index int, attributes []*GridIndexAttr) error {
  117. if master == nil {
  118. master = rootWindow
  119. }
  120. var sindex string
  121. if index < 0 {
  122. sindex = "all"
  123. } else {
  124. sindex = strconv.Itoa(index)
  125. }
  126. var attrList []string
  127. for _, attr := range attributes {
  128. if attr == nil || !isValidKey(attr.key, gridIndexAttrKeys) {
  129. continue
  130. }
  131. attrList = append(attrList, fmt.Sprintf("-%v {%v}", attr.key, attr.value))
  132. }
  133. var script string
  134. if row {
  135. script = fmt.Sprintf("grid rowconfigure %v %v", master.Id(), sindex)
  136. } else {
  137. script = fmt.Sprintf("grid columnconfigure %v %v", master.Id(), sindex)
  138. }
  139. if len(attrList) > 0 {
  140. script += " " + strings.Join(attrList, " ")
  141. }
  142. return eval(script)
  143. }