]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/grids.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.grids
3 # Purpose: An example sizer derived from the C++ wxPySizer that
4 # sizes items in a fixed or flexible grid.
8 # Created: 21-Sept-1999
10 # Copyright: (c) 1999 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
13 # 12/07/2003 - Jeff Grimmett (grimmtooth@softhome.net)
15 # o 2.5 Compatability changes
17 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
19 # o In keeping with the common idiom, the sizers in this module
20 # have been given the 'Py' prefix to avoid confusion with the
21 # native sizers of the same name. However, the reverse renamer
22 # still has the old wx*Sizer since the whole point of the
23 # reverse renamer is backward compatability.
24 # o wxGridSizer -> PyGridSizer
25 # o wxFlexGridSizer -> PyFlexGridSizer
26 # o Deprecation warning added.
30 In this module you will find PyGridSizer and PyFlexGridSizer. Please
31 note that these sizers have since been ported to C++ (as wx.GridSizer
32 and wx.FlexGridSizer) and those versions are now exposed in the regular
33 wxPython wrappers. However I am also leaving them here in the library
34 so they can serve as an example of how to implement sizers in Python.
36 PyGridSizer: Sizes and positions items such that all rows are the same
37 height and all columns are the same width. You can specify a gap in
38 pixels to be used between the rows and/or the columns. When you
39 create the sizer you specify the number of rows or the number of
40 columns and then as you add items it figures out the other dimension
41 automatically. Like other sizers, items can be set to fill their
42 available space, or to be aligned on a side, in a corner, or in the
43 center of the space. When the sizer is resized, all the items are
44 resized the same amount so all rows and all columns remain the same
47 PyFlexGridSizer: Derives from PyGridSizer and adds the ability for
48 particular rows and/or columns to be marked as growable. This means
49 that when the sizer changes size, the growable rows and colums are the
50 ones that stretch. The others remain at their initial size.
60 ################################################\
61 # THIS MODULE IS DEPRECATED |
63 # You should use the native wx.GridSizer and |
64 # wx.FlexGridSizer unless there is a compelling |
65 # need to use this module. |
66 ################################################/
71 warnings
.warn(warningmsg
, DeprecationWarning, stacklevel
=2)
74 #----------------------------------------------------------------------
76 class PyGridSizer(wx
.PySizer
):
77 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
78 wx
.PySizer
.__init
__(self
)
79 if rows
== 0 and cols
== 0:
80 raise ValueError, "rows and cols cannot both be zero"
88 def SetRows(self
, rows
):
89 if rows
== 0 and self
.cols
== 0:
90 raise ValueError, "rows and cols cannot both be zero"
93 def SetColumns(self
, cols
):
94 if self
.rows
== 0 and cols
== 0:
95 raise ValueError, "rows and cols cannot both be zero"
101 def GetColumns(self
):
104 def SetHgap(self
, hgap
):
107 def SetVgap(self
, vgap
):
110 def GetHgap(self
, hgap
):
113 def GetVgap(self
, vgap
):
116 #--------------------------------------------------
118 items
= self
.GetChildren()
124 nrows
= (nitems
+ ncols
-1) / ncols
126 ncols
= (nitems
+ nrows
-1) / nrows
128 # Find the max width and height for any component.
132 size
= item
.CalcMin()
133 w
= max(w
, size
.width
)
134 h
= max(h
, size
.height
)
136 return wx
.Size(ncols
* w
+ (ncols
-1) * self
.hgap
,
137 nrows
* h
+ (nrows
-1) * self
.vgap
)
140 #--------------------------------------------------
141 def RecalcSizes(self
):
142 items
= self
.GetChildren()
151 nrows
= (nitems
+ ncols
-1) / ncols
153 ncols
= (nitems
+ nrows
-1) / nrows
157 pt
= self
.GetPosition()
158 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
159 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
162 for c
in range(ncols
):
164 for r
in range(nrows
):
167 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
169 y
= y
+ h
+ self
.vgap
171 x
= x
+ w
+ self
.hgap
174 #--------------------------------------------------
175 def SetItemBounds(self
, item
, x
, y
, w
, h
):
176 # calculate the item's size and position within
180 flag
= item
.GetFlag()
182 if flag
& wx
.EXPAND
or flag
& wx
.SHAPED
:
185 if flag
& wx
.ALIGN_CENTER_HORIZONTAL
:
186 ipt
.x
= x
+ (w
- isz
.width
) / 2
187 elif flag
& wx
.ALIGN_RIGHT
:
188 ipt
.x
= x
+ (w
- isz
.width
)
190 if flag
& wx
.ALIGN_CENTER_VERTICAL
:
191 ipt
.y
= y
+ (h
- isz
.height
) / 2
192 elif flag
& wx
.ALIGN_BOTTOM
:
193 ipt
.y
= y
+ (h
- isz
.height
)
195 item
.SetDimension(ipt
, isz
)
198 #----------------------------------------------------------------------
202 class PyFlexGridSizer(PyGridSizer
):
203 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
204 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
207 self
.growableRows
= []
208 self
.growableCols
= []
210 def AddGrowableRow(self
, idx
):
211 self
.growableRows
.append(idx
)
213 def AddGrowableCol(self
, idx
):
214 self
.growableCols
.append(idx
)
216 #--------------------------------------------------
218 items
= self
.GetChildren()
224 nrows
= (nitems
+ ncols
-1) / ncols
226 ncols
= (nitems
+ nrows
-1) / nrows
228 # Find the max width and height for any component.
229 self
.rowHeights
= [0] * nrows
230 self
.colWidths
= [0] * ncols
232 for i
in range(len(items
)):
233 size
= items
[i
].CalcMin()
236 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
237 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
239 # Add up all the widths and heights
240 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
241 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
243 return wx
.Size(cellsWidth
+ (ncols
-1) * self
.hgap
,
244 cellHeight
+ (nrows
-1) * self
.vgap
)
247 #--------------------------------------------------
248 def RecalcSizes(self
):
249 items
= self
.GetChildren()
258 nrows
= (nitems
+ ncols
-1) / ncols
260 ncols
= (nitems
+ nrows
-1) / nrows
262 minsz
= self
.CalcMin()
264 pt
= self
.GetPosition()
266 # Check for growables
267 if self
.growableRows
and sz
.height
> minsz
.height
:
268 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
269 for idx
in self
.growableRows
:
270 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
272 if self
.growableCols
and sz
.width
> minsz
.width
:
273 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
274 for idx
in self
.growableCols
:
275 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
277 # bottom right corner
278 sz
= wx
.Size(pt
.x
+ sz
.width
, pt
.y
+ sz
.height
)
282 for c
in range(ncols
):
284 for r
in range(nrows
):
288 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
289 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
290 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
292 y
= y
+ self
.rowHeights
[r
] + self
.vgap
294 x
= x
+ self
.colWidths
[c
] + self
.hgap
296 #----------------------------------------------------------------------