]> git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/grids.py
wizard.h added to the list of headers
[wxWidgets.git] / utils / wxPython / lib / grids.py
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 or flag & wxSHAPED:
136 isz = wxSize(w, h)
137 else:
138 if flag & wxALIGN_CENTER_HORIZONTAL:
139 ipt.x = x + (w - isz.width) / 2
140 elif flag & wxALIGN_RIGHT:
141 ipt.x = x + (w - isz.width)
142
143 if flag & wxALIGN_CENTER_VERTICAL:
144 ipt.y = y + (h - isz.height) / 2
145 elif flag & wxALIGN_BOTTOM:
146 ipt.y = y + (h - isz.height)
147
148 item.SetDimension(ipt, isz)
149
150
151 #----------------------------------------------------------------------
152
153
154
155 class wxFlexGridSizer(wxGridSizer):
156 def __init__(self, rows=0, cols=0, hgap=0, vgap=0):
157 wxGridSizer.__init__(self, rows, cols, hgap, vgap)
158 self.rowHeights = []
159 self.colWidths = []
160 self.growableRows = []
161 self.growableCols = []
162
163 def AddGrowableRow(self, idx):
164 self.growableRows.append(idx)
165
166 def AddGrowableCol(self, idx):
167 self.growableCols.append(idx)
168
169 #--------------------------------------------------
170 def CalcMin(self):
171 items = self.GetChildren()
172 nitems = len(items)
173 nrows = self.rows
174 ncols = self.cols
175
176 if ncols > 0:
177 nrows = (nitems + ncols-1) / ncols
178 else:
179 ncols = (nitems + nrows-1) / nrows
180
181 # Find the max width and height for any component.
182 self.rowHeights = [0] * nrows
183 self.colWidths = [0] * ncols
184 for i in range(len(items)):
185 size = items[i].CalcMin()
186 row = i / ncols
187 col = i % ncols
188 self.rowHeights[row] = max(size.height, self.rowHeights[row])
189 self.colWidths[col] = max(size.width, self.colWidths[col])
190
191 # Add up all the widths and heights
192 cellsWidth = reduce(operator.__add__, self.colWidths)
193 cellHeight = reduce(operator.__add__, self.rowHeights)
194
195 return wxSize(cellsWidth + (ncols-1) * self.hgap,
196 cellHeight + (nrows-1) * self.vgap)
197
198
199 #--------------------------------------------------
200 def RecalcSizes(self):
201 items = self.GetChildren()
202 if not items:
203 return
204
205 nitems = len(items)
206 nrows = self.rows
207 ncols = self.cols
208
209 if ncols > 0:
210 nrows = (nitems + ncols-1) / ncols
211 else:
212 ncols = (nitems + nrows-1) / nrows
213
214 minsz = self.CalcMin()
215 sz = self.GetSize()
216 pt = self.GetPosition()
217
218 # Check for growables
219 if self.growableRows and sz.height > minsz.height:
220 delta = (sz.height - minsz.height) / len(self.growableRows)
221 for idx in self.growableRows:
222 self.rowHeights[idx] = self.rowHeights[idx] + delta
223
224 if self.growableCols and sz.width > minsz.width:
225 delta = (sz.width - minsz.width) / len(self.growableCols)
226 for idx in self.growableCols:
227 self.colWidths[idx] = self.colWidths[idx] + delta
228
229 # Layout each cell
230 x = pt.x
231 for c in range(ncols):
232 y = pt.y
233 for r in range(nrows):
234 i = r * ncols + c
235 if i < nitems:
236 w = max(0, min(self.colWidths[c], sz.width - x))
237 h = max(0, min(self.rowHeights[r], sz.height - y))
238 self.SetItemBounds(items[i], x, y, w, h)
239 y = y + self.rowHeights[r] + self.vgap
240 x = x + self.colWidths[c] + self.hgap
241
242 #----------------------------------------------------------------------
243
244
245
246
247
248