]>
Commit | Line | Data |
---|---|---|
2f90df85 RD |
1 | #---------------------------------------------------------------------- |
2 | # Name: wxPython.lib.GridSizer | |
3 | # Purpose: An example sizer derived from the C++ wxPySizer that | |
4 | # sizes items in a fixed or flexible grid. | |
5 | # | |
6 | # Author: Robin Dunn | |
7 | # | |
8 | # Created: 21-Sept-1999 | |
9 | # RCS-ID: $Id$ | |
10 | # Copyright: (c) 1999 by Total Control Software | |
11 | # Licence: wxWindows license | |
12 | #---------------------------------------------------------------------- | |
13 | ||
14 | """ | |
15 | ||
16 | In this module you will find wxGridSizer and wxFlexgridSizer. | |
17 | wxGridSizer arrainges its items in a grid in which all the widths and | |
18 | heights are the same. wxFlexgridSizer allows different widths and | |
19 | heights, and you can also specify rows and/or columns that are | |
20 | growable. See the demo for a couple examples for how to use them. | |
21 | ||
22 | """ | |
23 | ||
24 | ||
25 | from wxPython.wx import * | |
26 | ||
27 | import operator | |
28 | ||
29 | #---------------------------------------------------------------------- | |
30 | ||
31 | class wxGridSizer(wxPySizer): | |
32 | def __init__(self, rows=0, cols=0, hgap=0, vgap=0): | |
33 | wxPySizer.__init__(self) | |
34 | if rows == 0 and cols == 0: | |
35 | raise ValueError, "rows and cols cannot both be zero" | |
36 | ||
37 | self.rows = rows | |
38 | self.cols = cols | |
39 | self.hgap = hgap | |
40 | self.vgap = vgap | |
41 | ||
42 | ||
43 | def SetRows(self, rows): | |
44 | if rows == 0 and self.cols == 0: | |
45 | raise ValueError, "rows and cols cannot both be zero" | |
46 | self.rows = rows | |
47 | ||
48 | def SetColumns(self, cols): | |
49 | if self.rows == 0 and cols == 0: | |
50 | raise ValueError, "rows and cols cannot both be zero" | |
51 | self.cols = cols | |
52 | ||
53 | def GetRows(self): | |
54 | return self.rows | |
55 | ||
56 | def GetColumns(self): | |
57 | return self.cols | |
58 | ||
59 | def SetHgap(self, hgap): | |
60 | self.hgap = hgap | |
61 | ||
62 | def SetVgap(self, vgap): | |
63 | self.vgap = vgap | |
64 | ||
65 | def GetHgap(self, hgap): | |
66 | return self.hgap | |
67 | ||
68 | def GetVgap(self, vgap): | |
69 | return self.vgap | |
70 | ||
71 | #-------------------------------------------------- | |
72 | def CalcMin(self): | |
73 | items = self.GetChildren() | |
74 | nitems = len(items) | |
75 | nrows = self.rows | |
76 | ncols = self.cols | |
77 | ||
78 | if ncols > 0: | |
79 | nrows = (nitems + ncols-1) / ncols | |
80 | else: | |
81 | ncols = (nitems + nrows-1) / nrows | |
82 | ||
83 | # Find the max width and height for any component. | |
84 | w = 0 | |
85 | h = 0 | |
86 | for item in items: | |
87 | size = item.CalcMin() | |
88 | w = max(w, size.width) | |
89 | h = max(h, size.height) | |
90 | ||
91 | return wxSize(ncols * w + (ncols-1) * self.hgap, | |
92 | nrows * h + (nrows-1) * self.vgap) | |
93 | ||
94 | ||
95 | #-------------------------------------------------- | |
96 | def RecalcSizes(self): | |
97 | items = self.GetChildren() | |
98 | if not items: | |
99 | return | |
100 | ||
101 | nitems = len(items) | |
102 | nrows = self.rows | |
103 | ncols = self.cols | |
104 | ||
105 | if ncols > 0: | |
106 | nrows = (nitems + ncols-1) / ncols | |
107 | else: | |
108 | ncols = (nitems + nrows-1) / nrows | |
109 | ||
110 | ||
111 | sz = self.GetSize() | |
112 | pt = self.GetPosition() | |
113 | w = (sz.width - (ncols - 1) * self.hgap) / ncols; | |
114 | h = (sz.height - (nrows - 1) * self.vgap) / nrows; | |
115 | ||
116 | x = pt.x | |
117 | for c in range(ncols): | |
118 | y = pt.y | |
119 | for r in range(nrows): | |
120 | i = r * ncols + c | |
121 | if i < nitems: | |
122 | self.SetItemBounds(items[i], x, y, w, h) | |
123 | y = y + h + self.vgap | |
124 | x = x + w + self.hgap | |
125 | ||
126 | ||
127 | #-------------------------------------------------- | |
128 | def SetItemBounds(self, item, x, y, w, h): | |
129 | # calculate the item's size and position within | |
130 | # its grid cell | |
131 | ipt = wxPoint(x, y) | |
132 | isz = item.CalcMin() | |
133 | flag = item.GetFlag() | |
134 | ||
135 | if flag & wxEXPAND: | |
136 | isz = wxSize(w, h) | |
137 | ||
138 | elif flag & wxCENTER: | |
139 | ipt.x = x + (w - isz.width) / 2 | |
140 | ipt.y = y + (h - isz.height) / 2 | |
141 | ||
142 | if flag & wxALIGN_LEFT: | |
143 | ipt.x = x | |
144 | elif flag & wxALIGN_RIGHT: | |
145 | ipt.x = x + (w - isz.width) | |
146 | ||
147 | if flag & wxALIGN_TOP: | |
148 | ipt.y = y | |
149 | elif flag & wxALIGN_BOTTOM: | |
150 | ipt.y = y + (h - isz.height) | |
151 | ||
152 | item.SetDimension(ipt, isz) | |
153 | ||
154 | ||
155 | #---------------------------------------------------------------------- | |
156 | ||
157 | ||
158 | ||
159 | class wxFlexGridSizer(wxGridSizer): | |
160 | def __init__(self, rows=0, cols=0, hgap=0, vgap=0): | |
161 | wxGridSizer.__init__(self, rows, cols, hgap, vgap) | |
162 | self.rowHeights = [] | |
163 | self.colWidths = [] | |
164 | self.growableRows = [] | |
165 | self.growableCols = [] | |
166 | ||
167 | def AddGrowableRow(self, idx): | |
168 | self.growableRows.append(idx) | |
169 | ||
170 | def AddGrowableCol(self, idx): | |
171 | self.growableCols.append(idx) | |
172 | ||
173 | #-------------------------------------------------- | |
174 | def CalcMin(self): | |
175 | items = self.GetChildren() | |
176 | nitems = len(items) | |
177 | nrows = self.rows | |
178 | ncols = self.cols | |
179 | ||
180 | if ncols > 0: | |
181 | nrows = (nitems + ncols-1) / ncols | |
182 | else: | |
183 | ncols = (nitems + nrows-1) / nrows | |
184 | ||
185 | # Find the max width and height for any component. | |
186 | self.rowHeights = [0] * nrows | |
187 | self.colWidths = [0] * ncols | |
188 | for i in range(len(items)): | |
189 | size = items[i].CalcMin() | |
190 | row = i / ncols | |
191 | col = i % ncols | |
192 | self.rowHeights[row] = max(size.height, self.rowHeights[row]) | |
193 | self.colWidths[col] = max(size.width, self.colWidths[col]) | |
194 | ||
195 | # Add up all the widths and heights | |
196 | cellsWidth = reduce(operator.__add__, self.colWidths) | |
197 | cellHeight = reduce(operator.__add__, self.rowHeights) | |
198 | ||
199 | return wxSize(cellsWidth + (ncols-1) * self.hgap, | |
200 | cellHeight + (nrows-1) * self.vgap) | |
201 | ||
202 | ||
203 | #-------------------------------------------------- | |
204 | def RecalcSizes(self): | |
205 | items = self.GetChildren() | |
206 | if not items: | |
207 | return | |
208 | ||
209 | nitems = len(items) | |
210 | nrows = self.rows | |
211 | ncols = self.cols | |
212 | ||
213 | if ncols > 0: | |
214 | nrows = (nitems + ncols-1) / ncols | |
215 | else: | |
216 | ncols = (nitems + nrows-1) / nrows | |
217 | ||
218 | minsz = self.CalcMin() | |
219 | sz = self.GetSize() | |
220 | pt = self.GetPosition() | |
221 | ||
222 | # Check for growables | |
223 | if self.growableRows and sz.height > minsz.height: | |
224 | delta = (sz.height - minsz.height) / len(self.growableRows) | |
225 | for idx in self.growableRows: | |
226 | self.rowHeights[idx] = self.rowHeights[idx] + delta | |
227 | ||
228 | if self.growableCols and sz.width > minsz.width: | |
229 | delta = (sz.width - minsz.width) / len(self.growableCols) | |
230 | for idx in self.growableCols: | |
231 | self.colWidths[idx] = self.colWidths[idx] + delta | |
232 | ||
233 | # Layout each cell | |
234 | x = pt.x | |
235 | for c in range(ncols): | |
236 | y = pt.y | |
237 | for r in range(nrows): | |
238 | i = r * ncols + c | |
239 | if i < nitems: | |
240 | w = max(0, min(self.colWidths[c], sz.width - x)) | |
241 | h = max(0, min(self.rowHeights[r], sz.height - y)) | |
242 | self.SetItemBounds(items[i], x, y, w, h) | |
243 | y = y + self.rowHeights[r] + self.vgap | |
244 | x = x + self.colWidths[c] + self.hgap | |
245 | ||
246 | #---------------------------------------------------------------------- | |
247 | ||
248 | ||
249 | ||
250 | ||
251 | ||
252 |