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. Please
16 note that these sizers have since been ported to C++ and those
17 versions are now exposed in the regular wxPython wrappers. However I
18 am also leaving them here in the library so they can serve as an
19 example of how to implement sizers in Python.
21 wxGridSizer: Sizes and positions items such that all rows are the same
22 height and all columns are the same width. You can specify a gap in
23 pixels to be used between the rows and/or the columns. When you
24 create the sizer you specify the number of rows or the number of
25 columns and then as you add items it figures out the other dimension
26 automatically. Like other sizers, items can be set to fill their
27 available space, or to be aligned on a side, in a corner, or in the
28 center of the space. When the sizer is resized, all the items are
29 resized the same amount so all rows and all columns remain the same
32 wxFlexGridSizer: Derives from wxGridSizer and adds the ability for
33 particular rows and/or columns to be marked as growable. This means
34 that when the sizer changes size, the growable rows and colums are the
35 ones that stretch. The others remain at their initial size.
37 See the demo for a couple examples for how to use them.
41 from wxPython
.wx
import *
45 #----------------------------------------------------------------------
47 class wxGridSizer(wxPySizer
):
48 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
49 wxPySizer
.__init
__(self
)
50 if rows
== 0 and cols
== 0:
51 raise ValueError, "rows and cols cannot both be zero"
59 def SetRows(self
, rows
):
60 if rows
== 0 and self
.cols
== 0:
61 raise ValueError, "rows and cols cannot both be zero"
64 def SetColumns(self
, cols
):
65 if self
.rows
== 0 and cols
== 0:
66 raise ValueError, "rows and cols cannot both be zero"
75 def SetHgap(self
, hgap
):
78 def SetVgap(self
, vgap
):
81 def GetHgap(self
, hgap
):
84 def GetVgap(self
, vgap
):
87 #--------------------------------------------------
89 items
= self
.GetChildren()
95 nrows
= (nitems
+ ncols
-1) / ncols
97 ncols
= (nitems
+ nrows
-1) / nrows
99 # Find the max width and height for any component.
103 size
= item
.CalcMin()
104 w
= max(w
, size
.width
)
105 h
= max(h
, size
.height
)
107 return wxSize(ncols
* w
+ (ncols
-1) * self
.hgap
,
108 nrows
* h
+ (nrows
-1) * self
.vgap
)
111 #--------------------------------------------------
112 def RecalcSizes(self
):
113 items
= self
.GetChildren()
122 nrows
= (nitems
+ ncols
-1) / ncols
124 ncols
= (nitems
+ nrows
-1) / nrows
128 pt
= self
.GetPosition()
129 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
130 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
133 for c
in range(ncols
):
135 for r
in range(nrows
):
138 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
139 y
= y
+ h
+ self
.vgap
140 x
= x
+ w
+ self
.hgap
143 #--------------------------------------------------
144 def SetItemBounds(self
, item
, x
, y
, w
, h
):
145 # calculate the item's size and position within
149 flag
= item
.GetFlag()
151 if flag
& wxEXPAND
or flag
& wxSHAPED
:
154 if flag
& wxALIGN_CENTER_HORIZONTAL
:
155 ipt
.x
= x
+ (w
- isz
.width
) / 2
156 elif flag
& wxALIGN_RIGHT
:
157 ipt
.x
= x
+ (w
- isz
.width
)
159 if flag
& wxALIGN_CENTER_VERTICAL
:
160 ipt
.y
= y
+ (h
- isz
.height
) / 2
161 elif flag
& wxALIGN_BOTTOM
:
162 ipt
.y
= y
+ (h
- isz
.height
)
164 item
.SetDimension(ipt
, isz
)
167 #----------------------------------------------------------------------
171 class wxFlexGridSizer(wxGridSizer
):
172 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
173 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
176 self
.growableRows
= []
177 self
.growableCols
= []
179 def AddGrowableRow(self
, idx
):
180 self
.growableRows
.append(idx
)
182 def AddGrowableCol(self
, idx
):
183 self
.growableCols
.append(idx
)
185 #--------------------------------------------------
187 items
= self
.GetChildren()
193 nrows
= (nitems
+ ncols
-1) / ncols
195 ncols
= (nitems
+ nrows
-1) / nrows
197 # Find the max width and height for any component.
198 self
.rowHeights
= [0] * nrows
199 self
.colWidths
= [0] * ncols
200 for i
in range(len(items
)):
201 size
= items
[i
].CalcMin()
204 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
205 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
207 # Add up all the widths and heights
208 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
209 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
211 return wxSize(cellsWidth
+ (ncols
-1) * self
.hgap
,
212 cellHeight
+ (nrows
-1) * self
.vgap
)
215 #--------------------------------------------------
216 def RecalcSizes(self
):
217 items
= self
.GetChildren()
226 nrows
= (nitems
+ ncols
-1) / ncols
228 ncols
= (nitems
+ nrows
-1) / nrows
230 minsz
= self
.CalcMin()
232 pt
= self
.GetPosition()
234 # Check for growables
235 if self
.growableRows
and sz
.height
> minsz
.height
:
236 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
237 for idx
in self
.growableRows
:
238 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
240 if self
.growableCols
and sz
.width
> minsz
.width
:
241 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
242 for idx
in self
.growableCols
:
243 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
245 # bottom right corner
246 sz
= wxSize(pt
.x
+ sz
.width
, pt
.y
+ sz
.height
)
250 for c
in range(ncols
):
252 for r
in range(nrows
):
255 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
256 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
257 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
258 y
= y
+ self
.rowHeights
[r
] + self
.vgap
259 x
= x
+ self
.colWidths
[c
] + self
.hgap
261 #----------------------------------------------------------------------