boxlayout.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // Copyright 2010 The Walk Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // +build windows
  5. package walk
  6. import (
  7. "sort"
  8. )
  9. import (
  10. "github.com/lxn/win"
  11. )
  12. type Orientation byte
  13. const (
  14. Horizontal Orientation = iota
  15. Vertical
  16. )
  17. type BoxLayout struct {
  18. container Container
  19. margins Margins
  20. spacing int
  21. orientation Orientation
  22. hwnd2StretchFactor map[win.HWND]int
  23. resetNeeded bool
  24. }
  25. func newBoxLayout(orientation Orientation) *BoxLayout {
  26. return &BoxLayout{
  27. orientation: orientation,
  28. hwnd2StretchFactor: make(map[win.HWND]int),
  29. }
  30. }
  31. func NewHBoxLayout() *BoxLayout {
  32. return newBoxLayout(Horizontal)
  33. }
  34. func NewVBoxLayout() *BoxLayout {
  35. return newBoxLayout(Vertical)
  36. }
  37. func (l *BoxLayout) Container() Container {
  38. return l.container
  39. }
  40. func (l *BoxLayout) SetContainer(value Container) {
  41. if value != l.container {
  42. if l.container != nil {
  43. l.container.SetLayout(nil)
  44. }
  45. l.container = value
  46. if value != nil && value.Layout() != Layout(l) {
  47. value.SetLayout(l)
  48. l.Update(true)
  49. }
  50. }
  51. }
  52. func (l *BoxLayout) Margins() Margins {
  53. return l.margins
  54. }
  55. func (l *BoxLayout) SetMargins(value Margins) error {
  56. if value.HNear < 0 || value.VNear < 0 || value.HFar < 0 || value.VFar < 0 {
  57. return newError("margins must be positive")
  58. }
  59. l.margins = value
  60. l.Update(false)
  61. return nil
  62. }
  63. func (l *BoxLayout) Orientation() Orientation {
  64. return l.orientation
  65. }
  66. func (l *BoxLayout) SetOrientation(value Orientation) error {
  67. if value != l.orientation {
  68. switch value {
  69. case Horizontal, Vertical:
  70. default:
  71. return newError("invalid Orientation value")
  72. }
  73. l.orientation = value
  74. l.Update(false)
  75. }
  76. return nil
  77. }
  78. func (l *BoxLayout) Spacing() int {
  79. return l.spacing
  80. }
  81. func (l *BoxLayout) SetSpacing(value int) error {
  82. if value != l.spacing {
  83. if value < 0 {
  84. return newError("spacing cannot be negative")
  85. }
  86. l.spacing = value
  87. l.Update(false)
  88. }
  89. return nil
  90. }
  91. func (l *BoxLayout) StretchFactor(widget Widget) int {
  92. if factor, ok := l.hwnd2StretchFactor[widget.Handle()]; ok {
  93. return factor
  94. }
  95. return 1
  96. }
  97. func (l *BoxLayout) SetStretchFactor(widget Widget, factor int) error {
  98. if factor != l.StretchFactor(widget) {
  99. if l.container == nil {
  100. return newError("container required")
  101. }
  102. handle := widget.Handle()
  103. if !l.container.Children().containsHandle(handle) {
  104. return newError("unknown widget")
  105. }
  106. if factor < 1 {
  107. return newError("factor must be >= 1")
  108. }
  109. l.hwnd2StretchFactor[handle] = factor
  110. l.Update(false)
  111. }
  112. return nil
  113. }
  114. func (l *BoxLayout) cleanupStretchFactors() {
  115. widgets := l.container.Children()
  116. for handle, _ := range l.hwnd2StretchFactor {
  117. if !widgets.containsHandle(handle) {
  118. delete(l.hwnd2StretchFactor, handle)
  119. }
  120. }
  121. }
  122. type widgetInfo struct {
  123. index int
  124. minSize int
  125. maxSize int
  126. stretch int
  127. greedy bool
  128. widget Widget
  129. }
  130. type widgetInfoList []widgetInfo
  131. func (l widgetInfoList) Len() int {
  132. return len(l)
  133. }
  134. func (l widgetInfoList) Less(i, j int) bool {
  135. _, iIsSpacer := l[i].widget.(*Spacer)
  136. _, jIsSpacer := l[j].widget.(*Spacer)
  137. if l[i].greedy == l[j].greedy {
  138. if iIsSpacer == jIsSpacer {
  139. minDiff := l[i].minSize - l[j].minSize
  140. if minDiff == 0 {
  141. return l[i].maxSize/l[i].stretch < l[j].maxSize/l[j].stretch
  142. }
  143. return minDiff > 0
  144. }
  145. return jIsSpacer
  146. }
  147. return l[i].greedy
  148. }
  149. func (l widgetInfoList) Swap(i, j int) {
  150. l[i], l[j] = l[j], l[i]
  151. }
  152. func (l *BoxLayout) widgets() []Widget {
  153. children := l.container.Children()
  154. widgets := make([]Widget, 0, children.Len())
  155. for i := 0; i < cap(widgets); i++ {
  156. widget := children.At(i)
  157. if !shouldLayoutWidget(widget) {
  158. continue
  159. }
  160. ps := widget.SizeHint()
  161. if ps.Width == 0 && ps.Height == 0 && widget.LayoutFlags() == 0 {
  162. continue
  163. }
  164. widgets = append(widgets, widget)
  165. }
  166. return widgets
  167. }
  168. func (l *BoxLayout) LayoutFlags() LayoutFlags {
  169. if l.container == nil {
  170. return 0
  171. }
  172. var flags LayoutFlags
  173. var hasNonShrinkableHorz bool
  174. var hasNonShrinkableVert bool
  175. children := l.container.Children()
  176. count := children.Len()
  177. if count == 0 {
  178. return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert
  179. } else {
  180. for i := 0; i < count; i++ {
  181. widget := children.At(i)
  182. if !shouldLayoutWidget(widget) {
  183. continue
  184. }
  185. f := widget.LayoutFlags()
  186. flags |= f
  187. if f&ShrinkableHorz == 0 {
  188. hasNonShrinkableHorz = true
  189. }
  190. if f&ShrinkableVert == 0 {
  191. hasNonShrinkableVert = true
  192. }
  193. }
  194. }
  195. if l.orientation == Horizontal {
  196. flags |= GrowableHorz
  197. if hasNonShrinkableVert {
  198. flags &^= ShrinkableVert
  199. }
  200. } else {
  201. flags |= GrowableVert
  202. if hasNonShrinkableHorz {
  203. flags &^= ShrinkableHorz
  204. }
  205. }
  206. return flags
  207. }
  208. func (l *BoxLayout) MinSize() Size {
  209. if l.container == nil {
  210. return Size{}
  211. }
  212. widgets := l.widgets()
  213. var s Size
  214. for _, widget := range widgets {
  215. min := minSizeEffective(widget)
  216. if l.orientation == Horizontal {
  217. s.Width += min.Width
  218. s.Height = maxi(s.Height, min.Height)
  219. } else {
  220. s.Height += min.Height
  221. s.Width = maxi(s.Width, min.Width)
  222. }
  223. }
  224. if l.orientation == Horizontal {
  225. s.Width += l.spacing * (len(widgets) - 1)
  226. s.Width += l.margins.HNear + l.margins.HFar
  227. s.Height += l.margins.VNear + l.margins.VFar
  228. } else {
  229. s.Height += l.spacing * (len(widgets) - 1)
  230. s.Height += l.margins.VNear + l.margins.VFar
  231. s.Width += l.margins.HNear + l.margins.HFar
  232. }
  233. return s
  234. }
  235. func (l *BoxLayout) Update(reset bool) error {
  236. if l.container == nil {
  237. return nil
  238. }
  239. if reset {
  240. l.resetNeeded = true
  241. }
  242. if l.container.Suspended() {
  243. return nil
  244. }
  245. if l.resetNeeded {
  246. l.resetNeeded = false
  247. // Make GC happy.
  248. l.cleanupStretchFactors()
  249. }
  250. // Begin by finding out which widgets we care about.
  251. widgets := l.widgets()
  252. // Prepare some useful data.
  253. var greedyNonSpacerCount int
  254. var greedySpacerCount int
  255. var stretchFactorsTotal [3]int
  256. stretchFactors := make([]int, len(widgets))
  257. var minSizesRemaining int
  258. minSizes := make([]int, len(widgets))
  259. maxSizes := make([]int, len(widgets))
  260. sizes := make([]int, len(widgets))
  261. prefSizes2 := make([]int, len(widgets))
  262. growable2 := make([]bool, len(widgets))
  263. sortedWidgetInfo := widgetInfoList(make([]widgetInfo, len(widgets)))
  264. for i, widget := range widgets {
  265. sf := l.hwnd2StretchFactor[widget.Handle()]
  266. if sf == 0 {
  267. sf = 1
  268. }
  269. stretchFactors[i] = sf
  270. flags := widget.LayoutFlags()
  271. min := widget.MinSize()
  272. max := widget.MaxSize()
  273. minHint := widget.MinSizeHint()
  274. pref := widget.SizeHint()
  275. if l.orientation == Horizontal {
  276. growable2[i] = flags&GrowableVert > 0
  277. minSizes[i] = maxi(min.Width, minHint.Width)
  278. if max.Width > 0 {
  279. maxSizes[i] = max.Width
  280. } else if pref.Width > 0 && flags&GrowableHorz == 0 {
  281. maxSizes[i] = pref.Width
  282. } else {
  283. maxSizes[i] = 32768
  284. }
  285. prefSizes2[i] = pref.Height
  286. sortedWidgetInfo[i].greedy = flags&GreedyHorz > 0
  287. } else {
  288. growable2[i] = flags&GrowableHorz > 0
  289. minSizes[i] = maxi(min.Height, minHint.Height)
  290. if max.Height > 0 {
  291. maxSizes[i] = max.Height
  292. } else if pref.Height > 0 && flags&GrowableVert == 0 {
  293. maxSizes[i] = pref.Height
  294. } else {
  295. maxSizes[i] = 32768
  296. }
  297. prefSizes2[i] = pref.Width
  298. sortedWidgetInfo[i].greedy = flags&GreedyVert > 0
  299. }
  300. sortedWidgetInfo[i].index = i
  301. sortedWidgetInfo[i].minSize = minSizes[i]
  302. sortedWidgetInfo[i].maxSize = maxSizes[i]
  303. sortedWidgetInfo[i].stretch = sf
  304. sortedWidgetInfo[i].widget = widget
  305. minSizesRemaining += minSizes[i]
  306. if sortedWidgetInfo[i].greedy {
  307. if _, isSpacer := widget.(*Spacer); !isSpacer {
  308. greedyNonSpacerCount++
  309. stretchFactorsTotal[0] += sf
  310. } else {
  311. greedySpacerCount++
  312. stretchFactorsTotal[1] += sf
  313. }
  314. } else {
  315. stretchFactorsTotal[2] += sf
  316. }
  317. }
  318. sort.Stable(sortedWidgetInfo)
  319. cb := l.container.ClientBounds()
  320. var start1, start2, space1, space2 int
  321. if l.orientation == Horizontal {
  322. start1 = cb.X + l.margins.HNear
  323. start2 = cb.Y + l.margins.VNear
  324. space1 = cb.Width - l.margins.HNear - l.margins.HFar
  325. space2 = cb.Height - l.margins.VNear - l.margins.VFar
  326. } else {
  327. start1 = cb.Y + l.margins.VNear
  328. start2 = cb.X + l.margins.HNear
  329. space1 = cb.Height - l.margins.VNear - l.margins.VFar
  330. space2 = cb.Width - l.margins.HNear - l.margins.HFar
  331. }
  332. // Now calculate widget primary axis sizes.
  333. spacingRemaining := l.spacing * (len(widgets) - 1)
  334. offsets := [3]int{0, greedyNonSpacerCount, greedyNonSpacerCount + greedySpacerCount}
  335. counts := [3]int{greedyNonSpacerCount, greedySpacerCount, len(widgets) - greedyNonSpacerCount - greedySpacerCount}
  336. for i := 0; i < 3; i++ {
  337. stretchFactorsRemaining := stretchFactorsTotal[i]
  338. for j := 0; j < counts[i]; j++ {
  339. info := sortedWidgetInfo[offsets[i]+j]
  340. k := info.index
  341. stretch := stretchFactors[k]
  342. min := info.minSize
  343. max := info.maxSize
  344. size := min
  345. if min < max {
  346. excessSpace := float64(space1 - minSizesRemaining - spacingRemaining)
  347. size += int(excessSpace * float64(stretch) / float64(stretchFactorsRemaining))
  348. if size < min {
  349. size = min
  350. } else if size > max {
  351. size = max
  352. }
  353. }
  354. sizes[k] = size
  355. minSizesRemaining -= min
  356. stretchFactorsRemaining -= stretch
  357. space1 -= (size + l.spacing)
  358. spacingRemaining -= l.spacing
  359. }
  360. }
  361. // Finally position widgets.
  362. hdwp := win.BeginDeferWindowPos(int32(len(widgets)))
  363. if hdwp == 0 {
  364. return lastError("BeginDeferWindowPos")
  365. }
  366. excessTotal := space1 - minSizesRemaining - spacingRemaining
  367. excessShare := excessTotal / (len(widgets) + 1)
  368. p1 := start1
  369. for i, widget := range widgets {
  370. p1 += excessShare
  371. s1 := sizes[i]
  372. var s2 int
  373. if growable2[i] {
  374. s2 = space2
  375. } else {
  376. s2 = prefSizes2[i]
  377. }
  378. p2 := start2 + (space2-s2)/2
  379. var x, y, w, h int
  380. if l.orientation == Horizontal {
  381. x, y, w, h = p1, p2, s1, s2
  382. } else {
  383. x, y, w, h = p2, p1, s2, s1
  384. }
  385. if hdwp = win.DeferWindowPos(
  386. hdwp,
  387. widget.Handle(),
  388. 0,
  389. int32(x),
  390. int32(y),
  391. int32(w),
  392. int32(h),
  393. win.SWP_NOACTIVATE|win.SWP_NOOWNERZORDER|win.SWP_NOZORDER); hdwp == 0 {
  394. return lastError("DeferWindowPos")
  395. }
  396. p1 += s1 + l.spacing
  397. }
  398. if !win.EndDeferWindowPos(hdwp) {
  399. return lastError("EndDeferWindowPos")
  400. }
  401. return nil
  402. }