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.
16 wxGridSizer arrainges its items in a grid in which all the widths and
17 heights are the same. wxFlexgridSizer allows different widths and
18 heights, and you can also specify rows and/or columns that are
19 growable. See the demo for a couple examples for how to use them.
23 from wxPython
.wx
import *
27 #----------------------------------------------------------------------
29 class wxGridSizer(wxPySizer
):
30 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
31 wxPySizer
.__init
__(self
)
32 if rows
== 0 and cols
== 0:
33 raise ValueError, "rows and cols cannot both be zero"
41 def SetRows(self
, rows
):
42 if rows
== 0 and self
.cols
== 0:
43 raise ValueError, "rows and cols cannot both be zero"
46 def SetColumns(self
, cols
):
47 if self
.rows
== 0 and cols
== 0:
48 raise ValueError, "rows and cols cannot both be zero"
57 def SetHgap(self
, hgap
):
60 def SetVgap(self
, vgap
):
63 def GetHgap(self
, hgap
):
66 def GetVgap(self
, vgap
):
69 #--------------------------------------------------
71 items
= self
.GetChildren()
77 nrows
= (nitems
+ ncols
-1) / ncols
79 ncols
= (nitems
+ nrows
-1) / nrows
81 # Find the max width and height for any component.
86 w
= max(w
, size
.width
)
87 h
= max(h
, size
.height
)
89 return wxSize(ncols
* w
+ (ncols
-1) * self
.hgap
,
90 nrows
* h
+ (nrows
-1) * self
.vgap
)
93 #--------------------------------------------------
94 def RecalcSizes(self
):
95 items
= self
.GetChildren()
104 nrows
= (nitems
+ ncols
-1) / ncols
106 ncols
= (nitems
+ nrows
-1) / nrows
110 pt
= self
.GetPosition()
111 w
= (sz
.width
- (ncols
- 1) * self
.hgap
) / ncols
;
112 h
= (sz
.height
- (nrows
- 1) * self
.vgap
) / nrows
;
115 for c
in range(ncols
):
117 for r
in range(nrows
):
120 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
121 y
= y
+ h
+ self
.vgap
122 x
= x
+ w
+ self
.hgap
125 #--------------------------------------------------
126 def SetItemBounds(self
, item
, x
, y
, w
, h
):
127 # calculate the item's size and position within
131 flag
= item
.GetFlag()
133 if flag
& wxEXPAND
or flag
& wxSHAPED
:
136 if flag
& wxALIGN_CENTER_HORIZONTAL
:
137 ipt
.x
= x
+ (w
- isz
.width
) / 2
138 elif flag
& wxALIGN_RIGHT
:
139 ipt
.x
= x
+ (w
- isz
.width
)
141 if flag
& wxALIGN_CENTER_VERTICAL
:
142 ipt
.y
= y
+ (h
- isz
.height
) / 2
143 elif flag
& wxALIGN_BOTTOM
:
144 ipt
.y
= y
+ (h
- isz
.height
)
146 item
.SetDimension(ipt
, isz
)
149 #----------------------------------------------------------------------
153 class wxFlexGridSizer(wxGridSizer
):
154 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
155 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
158 self
.growableRows
= []
159 self
.growableCols
= []
161 def AddGrowableRow(self
, idx
):
162 self
.growableRows
.append(idx
)
164 def AddGrowableCol(self
, idx
):
165 self
.growableCols
.append(idx
)
167 #--------------------------------------------------
169 items
= self
.GetChildren()
175 nrows
= (nitems
+ ncols
-1) / ncols
177 ncols
= (nitems
+ nrows
-1) / nrows
179 # Find the max width and height for any component.
180 self
.rowHeights
= [0] * nrows
181 self
.colWidths
= [0] * ncols
182 for i
in range(len(items
)):
183 size
= items
[i
].CalcMin()
186 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
187 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
189 # Add up all the widths and heights
190 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
191 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
193 return wxSize(cellsWidth
+ (ncols
-1) * self
.hgap
,
194 cellHeight
+ (nrows
-1) * self
.vgap
)
197 #--------------------------------------------------
198 def RecalcSizes(self
):
199 items
= self
.GetChildren()
208 nrows
= (nitems
+ ncols
-1) / ncols
210 ncols
= (nitems
+ nrows
-1) / nrows
212 minsz
= self
.CalcMin()
214 pt
= self
.GetPosition()
216 # Check for growables
217 if self
.growableRows
and sz
.height
> minsz
.height
:
218 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
219 for idx
in self
.growableRows
:
220 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
222 if self
.growableCols
and sz
.width
> minsz
.width
:
223 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
224 for idx
in self
.growableCols
:
225 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
229 for c
in range(ncols
):
231 for r
in range(nrows
):
234 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
235 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
236 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
237 y
= y
+ self
.rowHeights
[r
] + self
.vgap
238 x
= x
+ self
.colWidths
[c
] + self
.hgap
240 #----------------------------------------------------------------------