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.
8 # Created: 21-Sept-1999
10 # Copyright: (c) 1999 by Total Control Software
11 # Licence: wxWindows license
12 #----------------------------------------------------------------------
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.
25 from wxPython
.wx
import *
29 #----------------------------------------------------------------------
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"
43 def SetRows(self
, rows
):
44 if rows
== 0 and self
.cols
== 0:
45 raise ValueError, "rows and cols cannot both be zero"
48 def SetColumns(self
, cols
):
49 if self
.rows
== 0 and cols
== 0:
50 raise ValueError, "rows and cols cannot both be zero"
59 def SetHgap(self
, hgap
):
62 def SetVgap(self
, vgap
):
65 def GetHgap(self
, hgap
):
68 def GetVgap(self
, vgap
):
71 #--------------------------------------------------
73 items
= self
.GetChildren()
79 nrows
= (nitems
+ ncols
-1) / ncols
81 ncols
= (nitems
+ nrows
-1) / nrows
83 # Find the max width and height for any component.
88 w
= max(w
, size
.width
)
89 h
= max(h
, size
.height
)
91 return wxSize(ncols
* w
+ (ncols
-1) * self
.hgap
,
92 nrows
* h
+ (nrows
-1) * self
.vgap
)
95 #--------------------------------------------------
96 def RecalcSizes(self
):
97 items
= self
.GetChildren()
106 nrows
= (nitems
+ ncols
-1) / ncols
108 ncols
= (nitems
+ nrows
-1) / nrows
112 pt
= self
.GetPosition()
113 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
114 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
117 for c
in range(ncols
):
119 for r
in range(nrows
):
122 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
123 y
= y
+ h
+ self
.vgap
124 x
= x
+ w
+ self
.hgap
127 #--------------------------------------------------
128 def SetItemBounds(self
, item
, x
, y
, w
, h
):
129 # calculate the item's size and position within
133 flag
= item
.GetFlag()
135 if flag
& wxEXPAND
or flag
& wxSHAPED
:
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
)
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
)
148 item
.SetDimension(ipt
, isz
)
151 #----------------------------------------------------------------------
155 class wxFlexGridSizer(wxGridSizer
):
156 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
157 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
160 self
.growableRows
= []
161 self
.growableCols
= []
163 def AddGrowableRow(self
, idx
):
164 self
.growableRows
.append(idx
)
166 def AddGrowableCol(self
, idx
):
167 self
.growableCols
.append(idx
)
169 #--------------------------------------------------
171 items
= self
.GetChildren()
177 nrows
= (nitems
+ ncols
-1) / ncols
179 ncols
= (nitems
+ nrows
-1) / nrows
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()
188 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
189 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
191 # Add up all the widths and heights
192 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
193 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
195 return wxSize(cellsWidth
+ (ncols
-1) * self
.hgap
,
196 cellHeight
+ (nrows
-1) * self
.vgap
)
199 #--------------------------------------------------
200 def RecalcSizes(self
):
201 items
= self
.GetChildren()
210 nrows
= (nitems
+ ncols
-1) / ncols
212 ncols
= (nitems
+ nrows
-1) / nrows
214 minsz
= self
.CalcMin()
216 pt
= self
.GetPosition()
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
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
231 for c
in range(ncols
):
233 for r
in range(nrows
):
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
242 #----------------------------------------------------------------------