docklayout.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2016 The G3N 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. package gui
  5. // DockLayout is the layout for docking panels to the internal edges of their parent.
  6. type DockLayout struct {
  7. }
  8. // DockLayoutParams specifies the edge to dock to.
  9. type DockLayoutParams struct {
  10. Edge int
  11. }
  12. // The different types of docking.
  13. const (
  14. DockTop = iota + 1
  15. DockRight
  16. DockBottom
  17. DockLeft
  18. DockCenter
  19. )
  20. // NewDockLayout returns a pointer to a new DockLayout.
  21. func NewDockLayout() *DockLayout {
  22. return new(DockLayout)
  23. }
  24. // Recalc (which satisfies the ILayout interface) recalculates the positions and sizes of the children panels.
  25. func (dl *DockLayout) Recalc(ipan IPanel) {
  26. pan := ipan.GetPanel()
  27. width := pan.Width()
  28. topY := float32(0)
  29. bottomY := pan.Height()
  30. leftX := float32(0)
  31. rightX := width
  32. // Top and bottom first
  33. for _, iobj := range pan.Children() {
  34. child := iobj.(IPanel).GetPanel()
  35. if child.layoutParams == nil {
  36. continue
  37. }
  38. params := child.layoutParams.(*DockLayoutParams)
  39. if params.Edge == DockTop {
  40. child.SetPosition(0, topY)
  41. topY += child.Height()
  42. child.SetWidth(width)
  43. continue
  44. }
  45. if params.Edge == DockBottom {
  46. child.SetPosition(0, bottomY-child.Height())
  47. bottomY -= child.Height()
  48. child.SetWidth(width)
  49. continue
  50. }
  51. }
  52. // Left and right
  53. for _, iobj := range pan.Children() {
  54. child := iobj.(IPanel).GetPanel()
  55. if child.layoutParams == nil {
  56. continue
  57. }
  58. params := child.layoutParams.(*DockLayoutParams)
  59. if params.Edge == DockLeft {
  60. child.SetPosition(leftX, topY)
  61. leftX += child.Width()
  62. child.SetHeight(bottomY - topY)
  63. continue
  64. }
  65. if params.Edge == DockRight {
  66. child.SetPosition(rightX-child.Width(), topY)
  67. rightX -= child.Width()
  68. child.SetHeight(bottomY - topY)
  69. continue
  70. }
  71. }
  72. // Center (only the first found)
  73. for _, iobj := range pan.Children() {
  74. child := iobj.(IPanel).GetPanel()
  75. if child.layoutParams == nil {
  76. continue
  77. }
  78. params := child.layoutParams.(*DockLayoutParams)
  79. if params.Edge == DockCenter {
  80. child.SetPosition(leftX, topY)
  81. child.SetSize(rightX-leftX, bottomY-topY)
  82. break
  83. }
  84. }
  85. }