]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/sizers/box.py
wxPython 2.1b1 for wxMSW (wxGTK coming soon)
[wxWidgets.git] / utils / wxPython / lib / sizers / box.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.sizers.box
3 # Purpose: A sizer/layout managers for wxPython that places items in
4 # a stretchable box
5 #
6 # Author: Robin Dunn and Dirk Holtwick
7 #
8 # Created: 17-May-1999
9 # RCS-ID: $Id$
10 # Copyright: (c) 1998 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
13
14 from sizer import wxSizer
15 from wxPython.wx import wxVERTICAL, wxHORIZONTAL
16
17 #----------------------------------------------------------------------
18
19
20 class wxBoxSizer(wxSizer):
21 """
22 wxBoxSizer
23
24 A Sizer that lays components out in a box, in the order they are
25 added to the layout manager, with a given orientation. The
26 orientation is specified in the constructor with either wxVERTICAL
27 or wxHORIZONTAL.
28
29 The optional parameter to the Add method (for this sizer it's
30 called the stretch flag) can be used to flag one or more components
31 as stretchable, meaning that they will expand to fill available
32 space in the given orientation. The default is zero, or not
33 stretchable.
34
35 If the stretch flag is non-zero then the widget will stretch. If
36 the sizer holds more than one item that is stretchable then they
37 share the available space.
38
39 If the strech flag is greater than 1 then it serves as a weighting
40 factor. Widgets with a flag of 2 will get twice as much space as
41 widgets with 1, etc.
42 """
43 def __init__(self, orientation, size = None):
44 wxSizer.__init__(self, size)
45 self.orientation = orientation
46
47
48 def CalcMin(self):
49 self.stretchable = 0 # number of stretchable items
50 self.minWidth = 0 # minimal size
51 self.minHeight = 0
52 self.fixedWidth = 0 # size without stretched widgets
53 self.fixedHeight = 0
54
55 # iterate through children
56 for (isSizer, widget, width, height, stretch) in self.children:
57 weight = 1
58 if stretch:
59 weight = stretch
60
61 if isSizer:
62 # let sub-sizers recalc their required space
63 width, height = widget.CalcMin()
64
65 # minimal size
66 if self.orientation == wxVERTICAL:
67 self.minHeight = self.minHeight + (height * weight)
68 self.minWidth = max(self.minWidth, width)
69 else:
70 self.minWidth = self.minWidth + (width * weight)
71 self.minHeight = max(self.minHeight, height)
72
73 # stretchable items
74 if stretch:
75 self.stretchable = self.stretchable + weight
76 else:
77 if self.orientation == wxVERTICAL:
78 self.fixedHeight = self.fixedHeight + height
79 self.fixedWidth = max(self.fixedWidth, width)
80 else:
81 self.fixedWidth = self.fixedWidth + width
82 self.fixedHeight = max(self.fixedHeight, height)
83
84 return self.minWidth, self.minHeight
85
86
87
88 def RecalcSizes(self):
89 # get current dimensions, for performance
90 myWidth = self.size.width
91 myHeight = self.size.height
92
93 # relative recent positions & sizes
94 px = self.origin.x
95 py = self.origin.y
96 newWidth = 0
97 newHeight = 0
98
99 # calculate space for one stretched item
100 if self.stretchable:
101 if self.orientation == wxHORIZONTAL:
102 delta = (myWidth - self.fixedWidth) / self.stretchable
103 extra = (myWidth - self.fixedWidth) % self.stretchable
104 else:
105 delta = (myHeight - self.fixedHeight) / self.stretchable
106 extra = (myHeight - self.fixedHeight) % self.stretchable
107
108 # iterate children ...
109 for (isSizer, widget, width, height, stretch) in self.children:
110 weight = 1
111 if stretch:
112 weight = stretch
113
114 if isSizer:
115 width, height = widget.CalcMin()
116
117 # ... vertical
118 if self.orientation == wxVERTICAL:
119 newHeight = height
120 if stretch:
121 newHeight = (delta * weight) + extra # first stretchable gets extra pixels
122 extra = 0
123 widget.SetDimensions(px, py, myWidth, newHeight)
124
125 # ... horizontal
126 elif self.orientation == wxHORIZONTAL:
127 newWidth = width
128 if stretch:
129 newWidth = (delta * weight) + extra # first stretchable gets extra pixels
130 extra = 0
131 widget.SetDimensions(px, py, newWidth, myHeight)
132
133 px = px + newWidth
134 py = py + newHeight
135
136
137 #----------------------------------------------------------------------