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 #----------------------------------------------------------------------
15 In this module you will find wxGridSizer and wxFlexGridSizer.
17 wxGridSizer: Sizes and positions items such that all rows are the same
18 height and all columns are the same width. You can specify a gap in
19 pixels to be used between the rows and/or the columns. When you
20 create the sizer you specify the number of rows or the number of
21 columns and then as you add items it figures out the other dimension
22 automatically. Like other sizers, items can be set to fill their
23 available space, or to be aligned on a side, in a corner, or in the
24 center of the space. When the sizer is resized, all the items are
25 resized the same amount so all rows and all columns remain the same
28 wxFlexGridSizer: Derives from wxGridSizer and adds the ability for
29 particular rows and/or columns to be marked as growable. This means
30 that when the sizer changes size, the growable rows and colums are the
31 ones that stretch. The others remain at their initial size.
33 See the demo for a couple examples for how to use them.
37 from wxPython
.wx
import *
41 #----------------------------------------------------------------------
43 class wxGridSizer(wxPySizer
):
44 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
45 wxPySizer
.__init
__(self
)
46 if rows
== 0 and cols
== 0:
47 raise ValueError, "rows and cols cannot both be zero"
55 def SetRows(self
, rows
):
56 if rows
== 0 and self
.cols
== 0:
57 raise ValueError, "rows and cols cannot both be zero"
60 def SetColumns(self
, cols
):
61 if self
.rows
== 0 and cols
== 0:
62 raise ValueError, "rows and cols cannot both be zero"
71 def SetHgap(self
, hgap
):
74 def SetVgap(self
, vgap
):
77 def GetHgap(self
, hgap
):
80 def GetVgap(self
, vgap
):
83 #--------------------------------------------------
85 items
= self
.GetChildren()
91 nrows
= (nitems
+ ncols
-1) / ncols
93 ncols
= (nitems
+ nrows
-1) / nrows
95 # Find the max width and height for any component.
100 w
= max(w
, size
.width
)
101 h
= max(h
, size
.height
)
103 return wxSize(ncols
* w
+ (ncols
-1) * self
.hgap
,
104 nrows
* h
+ (nrows
-1) * self
.vgap
)
107 #--------------------------------------------------
108 def RecalcSizes(self
):
109 items
= self
.GetChildren()
118 nrows
= (nitems
+ ncols
-1) / ncols
120 ncols
= (nitems
+ nrows
-1) / nrows
124 pt
= self
.GetPosition()
125 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
126 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
129 for c
in range(ncols
):
131 for r
in range(nrows
):
134 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
135 y
= y
+ h
+ self
.vgap
136 x
= x
+ w
+ self
.hgap
139 #--------------------------------------------------
140 def SetItemBounds(self
, item
, x
, y
, w
, h
):
141 # calculate the item's size and position within
145 flag
= item
.GetFlag()
147 if flag
& wxEXPAND
or flag
& wxSHAPED
:
150 if flag
& wxALIGN_CENTER_HORIZONTAL
:
151 ipt
.x
= x
+ (w
- isz
.width
) / 2
152 elif flag
& wxALIGN_RIGHT
:
153 ipt
.x
= x
+ (w
- isz
.width
)
155 if flag
& wxALIGN_CENTER_VERTICAL
:
156 ipt
.y
= y
+ (h
- isz
.height
) / 2
157 elif flag
& wxALIGN_BOTTOM
:
158 ipt
.y
= y
+ (h
- isz
.height
)
160 item
.SetDimension(ipt
, isz
)
163 #----------------------------------------------------------------------
167 class wxFlexGridSizer(wxGridSizer
):
168 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
169 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
172 self
.growableRows
= []
173 self
.growableCols
= []
175 def AddGrowableRow(self
, idx
):
176 self
.growableRows
.append(idx
)
178 def AddGrowableCol(self
, idx
):
179 self
.growableCols
.append(idx
)
181 #--------------------------------------------------
183 items
= self
.GetChildren()
189 nrows
= (nitems
+ ncols
-1) / ncols
191 ncols
= (nitems
+ nrows
-1) / nrows
193 # Find the max width and height for any component.
194 self
.rowHeights
= [0] * nrows
195 self
.colWidths
= [0] * ncols
196 for i
in range(len(items
)):
197 size
= items
[i
].CalcMin()
200 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
201 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
203 # Add up all the widths and heights
204 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
205 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
207 return wxSize(cellsWidth
+ (ncols
-1) * self
.hgap
,
208 cellHeight
+ (nrows
-1) * self
.vgap
)
211 #--------------------------------------------------
212 def RecalcSizes(self
):
213 items
= self
.GetChildren()
222 nrows
= (nitems
+ ncols
-1) / ncols
224 ncols
= (nitems
+ nrows
-1) / nrows
226 minsz
= self
.CalcMin()
228 pt
= self
.GetPosition()
230 # Check for growables
231 if self
.growableRows
and sz
.height
> minsz
.height
:
232 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
233 for idx
in self
.growableRows
:
234 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
236 if self
.growableCols
and sz
.width
> minsz
.width
:
237 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
238 for idx
in self
.growableCols
:
239 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
241 # bottom right corner
242 sz
= wxSize(pt
.x
+ sz
.width
, pt
.y
+ sz
.height
)
246 for c
in range(ncols
):
248 for r
in range(nrows
):
251 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
252 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
253 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
254 y
= y
+ self
.rowHeights
[r
] + self
.vgap
255 x
= x
+ self
.colWidths
[c
] + self
.hgap
257 #----------------------------------------------------------------------