layouts.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2012 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 declarative
  6. import (
  7. "github.com/lxn/walk"
  8. )
  9. type Orientation byte
  10. const (
  11. Horizontal Orientation = Orientation(walk.Horizontal)
  12. Vertical Orientation = Orientation(walk.Vertical)
  13. )
  14. type Margins struct {
  15. Left int
  16. Top int
  17. Right int
  18. Bottom int
  19. }
  20. func (m Margins) isZero() bool {
  21. return m.Left == 0 && m.Top == 0 && m.Right == 0 && m.Bottom == 0
  22. }
  23. func (m Margins) toW() walk.Margins {
  24. return walk.Margins{m.Left, m.Top, m.Right, m.Bottom}
  25. }
  26. type Size struct {
  27. Width int
  28. Height int
  29. }
  30. func (s Size) toW() walk.Size {
  31. return walk.Size{s.Width, s.Height}
  32. }
  33. func setLayoutMargins(layout walk.Layout, margins Margins, marginsZero bool) error {
  34. if !marginsZero && margins.isZero() {
  35. margins = Margins{9, 9, 9, 9}
  36. }
  37. return layout.SetMargins(margins.toW())
  38. }
  39. func setLayoutSpacing(layout walk.Layout, spacing int, spacingZero bool) error {
  40. if !spacingZero && spacing == 0 {
  41. spacing = 6
  42. }
  43. return layout.SetSpacing(spacing)
  44. }
  45. type HBox struct {
  46. Margins Margins
  47. Spacing int
  48. MarginsZero bool
  49. SpacingZero bool
  50. }
  51. func (hb HBox) Create() (walk.Layout, error) {
  52. l := walk.NewHBoxLayout()
  53. if err := setLayoutMargins(l, hb.Margins, hb.MarginsZero); err != nil {
  54. return nil, err
  55. }
  56. if err := setLayoutSpacing(l, hb.Spacing, hb.SpacingZero); err != nil {
  57. return nil, err
  58. }
  59. return l, nil
  60. }
  61. type VBox struct {
  62. Margins Margins
  63. Spacing int
  64. MarginsZero bool
  65. SpacingZero bool
  66. }
  67. func (vb VBox) Create() (walk.Layout, error) {
  68. l := walk.NewVBoxLayout()
  69. if err := setLayoutMargins(l, vb.Margins, vb.MarginsZero); err != nil {
  70. return nil, err
  71. }
  72. if err := setLayoutSpacing(l, vb.Spacing, vb.SpacingZero); err != nil {
  73. return nil, err
  74. }
  75. return l, nil
  76. }
  77. type Grid struct {
  78. Columns int
  79. Margins Margins
  80. Spacing int
  81. MarginsZero bool
  82. SpacingZero bool
  83. }
  84. func (g Grid) Create() (walk.Layout, error) {
  85. l := walk.NewGridLayout()
  86. if err := setLayoutMargins(l, g.Margins, g.MarginsZero); err != nil {
  87. return nil, err
  88. }
  89. if err := setLayoutSpacing(l, g.Spacing, g.SpacingZero); err != nil {
  90. return nil, err
  91. }
  92. return l, nil
  93. }