]>
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
19 In this module you will find wxGridSizer and wxFlexGridSizer. Please
20 note that these sizers have since been ported to C++ and those
21 versions are now exposed in the regular wxPython wrappers. However I
22 am also leaving them here in the library so they can serve as an
23 example of how to implement sizers in Python.
25 wxGridSizer: Sizes and positions items such that all rows are the same
26 height and all columns are the same width. You can specify a gap in
27 pixels to be used between the rows and/or the columns. When you
28 create the sizer you specify the number of rows or the number of
29 columns and then as you add items it figures out the other dimension
30 automatically. Like other sizers, items can be set to fill their
31 available space, or to be aligned on a side, in a corner, or in the
32 center of the space. When the sizer is resized, all the items are
33 resized the same amount so all rows and all columns remain the same
36 wxFlexGridSizer: Derives from wxGridSizer and adds the ability for
37 particular rows and/or columns to be marked as growable. This means
38 that when the sizer changes size, the growable rows and colums are the
39 ones that stretch. The others remain at their initial size.
46 #----------------------------------------------------------------------
48 class wxGridSizer(wx
.PySizer
):
49 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
50 wx
.PySizer
.__init
__(self
)
51 if rows
== 0 and cols
== 0:
52 raise ValueError, "rows and cols cannot both be zero"
60 def SetRows(self
, rows
):
61 if rows
== 0 and self
.cols
== 0:
62 raise ValueError, "rows and cols cannot both be zero"
65 def SetColumns(self
, cols
):
66 if self
.rows
== 0 and cols
== 0:
67 raise ValueError, "rows and cols cannot both be zero"
76 def SetHgap(self
, hgap
):
79 def SetVgap(self
, vgap
):
82 def GetHgap(self
, hgap
):
85 def GetVgap(self
, vgap
):
88 #--------------------------------------------------
90 items
= self
.GetChildren()
96 nrows
= (nitems
+ ncols
-1) / ncols
98 ncols
= (nitems
+ nrows
-1) / nrows
100 # Find the max width and height for any component.
104 size
= item
.CalcMin()
105 w
= max(w
, size
.width
)
106 h
= max(h
, size
.height
)
108 return wx
.Size(ncols
* w
+ (ncols
-1) * self
.hgap
,
109 nrows
* h
+ (nrows
-1) * self
.vgap
)
112 #--------------------------------------------------
113 def RecalcSizes(self
):
114 items
= self
.GetChildren()
123 nrows
= (nitems
+ ncols
-1) / ncols
125 ncols
= (nitems
+ nrows
-1) / nrows
129 pt
= self
.GetPosition()
130 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
131 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
134 for c
in range(ncols
):
136 for r
in range(nrows
):
139 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
141 y
= y
+ h
+ self
.vgap
143 x
= x
+ w
+ self
.hgap
146 #--------------------------------------------------
147 def SetItemBounds(self
, item
, x
, y
, w
, h
):
148 # calculate the item's size and position within
152 flag
= item
.GetFlag()
154 if flag
& wx
.EXPAND
or flag
& wx
.SHAPED
:
157 if flag
& wx
.ALIGN_CENTER_HORIZONTAL
:
158 ipt
.x
= x
+ (w
- isz
.width
) / 2
159 elif flag
& wx
.ALIGN_RIGHT
:
160 ipt
.x
= x
+ (w
- isz
.width
)
162 if flag
& wx
.ALIGN_CENTER_VERTICAL
:
163 ipt
.y
= y
+ (h
- isz
.height
) / 2
164 elif flag
& wx
.ALIGN_BOTTOM
:
165 ipt
.y
= y
+ (h
- isz
.height
)
167 item
.SetDimension(ipt
, isz
)
170 #----------------------------------------------------------------------
174 class wxFlexGridSizer(wxGridSizer
):
175 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
176 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
179 self
.growableRows
= []
180 self
.growableCols
= []
182 def AddGrowableRow(self
, idx
):
183 self
.growableRows
.append(idx
)
185 def AddGrowableCol(self
, idx
):
186 self
.growableCols
.append(idx
)
188 #--------------------------------------------------
190 items
= self
.GetChildren()
196 nrows
= (nitems
+ ncols
-1) / ncols
198 ncols
= (nitems
+ nrows
-1) / nrows
200 # Find the max width and height for any component.
201 self
.rowHeights
= [0] * nrows
202 self
.colWidths
= [0] * ncols
204 for i
in range(len(items
)):
205 size
= items
[i
].CalcMin()
208 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
209 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
211 # Add up all the widths and heights
212 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
213 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
215 return wx
.Size(cellsWidth
+ (ncols
-1) * self
.hgap
,
216 cellHeight
+ (nrows
-1) * self
.vgap
)
219 #--------------------------------------------------
220 def RecalcSizes(self
):
221 items
= self
.GetChildren()
230 nrows
= (nitems
+ ncols
-1) / ncols
232 ncols
= (nitems
+ nrows
-1) / nrows
234 minsz
= self
.CalcMin()
236 pt
= self
.GetPosition()
238 # Check for growables
239 if self
.growableRows
and sz
.height
> minsz
.height
:
240 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
241 for idx
in self
.growableRows
:
242 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
244 if self
.growableCols
and sz
.width
> minsz
.width
:
245 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
246 for idx
in self
.growableCols
:
247 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
249 # bottom right corner
250 sz
= wx
.Size(pt
.x
+ sz
.width
, pt
.y
+ sz
.height
)
254 for c
in range(ncols
):
256 for r
in range(nrows
):
260 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
261 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
262 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
264 y
= y
+ self
.rowHeights
[r
] + self
.vgap
266 x
= x
+ self
.colWidths
[c
] + self
.hgap
268 #----------------------------------------------------------------------