]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/lib/rcsizer.py
1 #----------------------------------------------------------------------
2 # Name: wxPython.lib.rcsizer
3 # Purpose: RowColSizer:
5 # Author: Robin Dunn, adapted from code by Niki Spahiev
9 # Copyright: (c) 2002 by Total Control Software
10 # Licence: wxWindows license
11 #----------------------------------------------------------------------
12 # 12/10/2003 - Jeff Grimmett (grimmtooth@softhome.net)
14 # o 2.5 compatability update.
15 # o There appears to be a prob with the wx.PySizer.GetSize() method.
17 # 12/23/2003 - Jeff Grimmett (grimmtooth@softhome.net)
19 # o wx.PySizer.GetSize() method working right now.
23 A pure-Python Sizer that lays out items in a grid similar to
24 wx.FlexGridSizer but item position is not implicit but explicitly
25 specified by row and col, and row/col spanning is supported.
27 Adapted from code by Niki Spahiev.
29 NOTE: There is now a C++ version of this class that has been wrapped
30 as wx.GridBagSizer. It is quicker and more capable so you are
38 # After the lib and demo no longer uses this sizer enable this warning...
43 ## #####################################################\
44 ## # THIS MODULE IS NOW DEPRECATED |
46 ## # The core wx library now contains a similar class |
47 ## # wrapped as wx.GridBagSizer. |
48 ## #####################################################/
52 ## warnings.warn(warningmsg, DeprecationWarning, stacklevel=2)
54 #----------------------------------------------------------------------
56 class RowColSizer(wx
.PySizer
):
58 # default sizes for cells with no item
63 wx
.PySizer
.__init
__(self
)
64 self
.growableRows
= []
65 self
.growableCols
= []
68 def AddGrowableRow(self
, idx
):
69 self
.growableRows
.append(idx
)
71 def AddGrowableCol(self
, idx
):
72 self
.growableCols
.append(idx
)
76 #--------------------------------------------------
77 def Add(self
, item
, option
=0, flag
=0, border
=0,
78 # row, col and spanning can be specified individually...
81 # or as tuples (row,col) and (rowspan,colspan)
88 rowspan
, colspan
= size
90 assert row
!= -1, "Row must be specified"
91 assert col
!= -1, "Column must be specified"
93 # Do I really want to do this? Probably not...
94 #if rowspan > 1 or colspan > 1:
95 # flag = flag | wx.EXPAND
97 wx
.PySizer
.Add(self
, item
, option
, flag
, border
,
98 userData
=(row
, col
, row
+rowspan
, col
+colspan
))
103 def AddSpacer(self
, width
, height
, option
=0, flag
=0, border
=0,
105 rowspan
=1, colspan
=1,
111 rowspan
, colspan
= size
113 assert row
!= -1, "Row must be specified"
114 assert col
!= -1, "Column must be specified"
116 wx
.PySizer
.AddSpacer(self
, (width
, height
), option
, flag
, border
,
117 userData
=(row
, col
, row
+rowspan
, col
+colspan
))
119 #--------------------------------------------------
120 def _add( self
, size
, dim
):
121 r
, c
, r2
, c2
= dim
# unpack coords and spanning
123 # are the widths and heights lists long enough?
124 if r2
> len(self
.rowHeights
):
125 x
= [self
.row_h
] * (r2
-len(self
.rowHeights
))
126 self
.rowHeights
.extend( x
)
127 if c2
> len(self
.colWidths
):
128 x
= [self
.col_w
] * (c2
-len(self
.colWidths
))
129 self
.colWidths
.extend( x
)
131 # set the widths and heights lists for this item
133 for i
in range(r
, r2
):
134 self
.rowHeights
[i
] = max( self
.rowHeights
[i
], size
.height
/ scale
)
136 for i
in range(c
, c2
):
137 self
.colWidths
[i
] = max( self
.colWidths
[i
], size
.width
/ scale
)
140 #--------------------------------------------------
145 items
= self
.GetChildren()
147 return wx
.Size(10, 10)
150 self
._add
( item
.CalcMin(), item
.GetUserData() )
152 size
= wx
.Size( reduce( operator
.add
, self
.colWidths
),
153 reduce( operator
.add
, self
.rowHeights
) )
157 #--------------------------------------------------
158 def RecalcSizes( self
):
159 # save current dimensions, etc.
160 curWidth
, curHeight
= self
.GetSize()
161 px
, py
= self
.GetPosition()
162 minWidth
, minHeight
= self
.CalcMin()
164 # Check for growables
165 if self
.growableRows
and curHeight
> minHeight
:
166 delta
= (curHeight
- minHeight
) / len(self
.growableRows
)
167 extra
= (curHeight
- minHeight
) % len(self
.growableRows
)
168 for idx
in self
.growableRows
:
169 self
.rowHeights
[idx
] += delta
170 self
.rowHeights
[self
.growableRows
[0]] += extra
172 if self
.growableCols
and curWidth
> minWidth
:
173 delta
= (curWidth
- minWidth
) / len(self
.growableCols
)
174 extra
= (curWidth
- minWidth
) % len(self
.growableCols
)
175 for idx
in self
.growableCols
:
176 self
.colWidths
[idx
] += delta
177 self
.colWidths
[self
.growableCols
[0]] += extra
179 rpos
= [0] * len(self
.rowHeights
)
180 cpos
= [0] * len(self
.colWidths
)
182 for i
in range(len(self
.rowHeights
)):
183 height
= self
.rowHeights
[i
]
187 for i
in range(len(self
.colWidths
)):
188 width
= self
.colWidths
[i
]
192 # iterate children and set dimensions...
193 for item
in self
.GetChildren():
194 r
, c
, r2
, c2
= item
.GetUserData()
195 width
= reduce( operator
.add
, self
.colWidths
[c
:c2
] )
196 height
= reduce( operator
.add
, self
.rowHeights
[r
:r2
] )
197 self
.SetItemBounds( item
, cpos
[c
], rpos
[r
], width
, height
)
200 #--------------------------------------------------
201 def SetItemBounds(self
, item
, x
, y
, w
, h
):
202 # calculate the item's actual size and position within
206 flag
= item
.GetFlag()
208 if flag
& wx
.EXPAND
or flag
& wx
.SHAPED
:
211 if flag
& wx
.ALIGN_CENTER_HORIZONTAL
:
212 ipt
.x
= x
+ (w
- isz
.width
) / 2
213 elif flag
& wx
.ALIGN_RIGHT
:
214 ipt
.x
= x
+ (w
- isz
.width
)
216 if flag
& wx
.ALIGN_CENTER_VERTICAL
:
217 ipt
.y
= y
+ (h
- isz
.height
) / 2
218 elif flag
& wx
.ALIGN_BOTTOM
:
219 ipt
.y
= y
+ (h
- isz
.height
)
221 item
.SetDimension(ipt
, isz
)
224 #----------------------------------------------------------------------
225 #----------------------------------------------------------------------