]>
git.saurik.com Git - wxWidgets.git/blob - utils/wxPython/lib/grids.py
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()
138 elif flag
& wxCENTER
:
139 ipt
.x
= x
+ (w
- isz
.width
) / 2
140 ipt
.y
= y
+ (h
- isz
.height
) / 2
142 if flag
& wxALIGN_LEFT
:
144 elif flag
& wxALIGN_RIGHT
:
145 ipt
.x
= x
+ (w
- isz
.width
)
147 if flag
& wxALIGN_TOP
:
149 elif flag
& wxALIGN_BOTTOM
:
150 ipt
.y
= y
+ (h
- isz
.height
)
152 item
.SetDimension(ipt
, isz
)
155 #----------------------------------------------------------------------
159 class wxFlexGridSizer(wxGridSizer
):
160 def __init__(self
, rows
=0, cols
=0, hgap
=0, vgap
=0):
161 wxGridSizer
.__init
__(self
, rows
, cols
, hgap
, vgap
)
164 self
.growableRows
= []
165 self
.growableCols
= []
167 def AddGrowableRow(self
, idx
):
168 self
.growableRows
.append(idx
)
170 def AddGrowableCol(self
, idx
):
171 self
.growableCols
.append(idx
)
173 #--------------------------------------------------
175 items
= self
.GetChildren()
181 nrows
= (nitems
+ ncols
-1) / ncols
183 ncols
= (nitems
+ nrows
-1) / nrows
185 # Find the max width and height for any component.
186 self
.rowHeights
= [0] * nrows
187 self
.colWidths
= [0] * ncols
188 for i
in range(len(items
)):
189 size
= items
[i
].CalcMin()
192 self
.rowHeights
[row
] = max(size
.height
, self
.rowHeights
[row
])
193 self
.colWidths
[col
] = max(size
.width
, self
.colWidths
[col
])
195 # Add up all the widths and heights
196 cellsWidth
= reduce(operator
.__add
__, self
.colWidths
)
197 cellHeight
= reduce(operator
.__add
__, self
.rowHeights
)
199 return wxSize(cellsWidth
+ (ncols
-1) * self
.hgap
,
200 cellHeight
+ (nrows
-1) * self
.vgap
)
203 #--------------------------------------------------
204 def RecalcSizes(self
):
205 items
= self
.GetChildren()
214 nrows
= (nitems
+ ncols
-1) / ncols
216 ncols
= (nitems
+ nrows
-1) / nrows
218 minsz
= self
.CalcMin()
220 pt
= self
.GetPosition()
222 # Check for growables
223 if self
.growableRows
and sz
.height
> minsz
.height
:
224 delta
= (sz
.height
- minsz
.height
) / len(self
.growableRows
)
225 for idx
in self
.growableRows
:
226 self
.rowHeights
[idx
] = self
.rowHeights
[idx
] + delta
228 if self
.growableCols
and sz
.width
> minsz
.width
:
229 delta
= (sz
.width
- minsz
.width
) / len(self
.growableCols
)
230 for idx
in self
.growableCols
:
231 self
.colWidths
[idx
] = self
.colWidths
[idx
] + delta
235 for c
in range(ncols
):
237 for r
in range(nrows
):
240 w
= max(0, min(self
.colWidths
[c
], sz
.width
- x
))
241 h
= max(0, min(self
.rowHeights
[r
], sz
.height
- y
))
242 self
.SetItemBounds(items
[i
], x
, y
, w
, h
)
243 y
= y
+ self
.rowHeights
[r
] + self
.vgap
244 x
= x
+ self
.colWidths
[c
] + self
.hgap
246 #----------------------------------------------------------------------