1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGridBagSizer: A sizer that can lay out items in a grid,
4 // with items at specified cells, and with the option of row
5 // and/or column spanning
8 // Created: 03-Nov-2003
9 // Copyright: (c) Robin Dunn
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #ifndef __WXGBSIZER_H__
14 #define __WXGBSIZER_H__
19 //---------------------------------------------------------------------------
20 // Classes to represent a position in the grid and a size of an item in the
21 // grid, IOW, the number of rows and columns it occupies. I chose to use these
22 // instead of wxPoint and wxSize because they are (x,y) and usually pixel
23 // oriented while grids and tables are usually thought of as (row,col) so some
24 // confusion would definitely result in using wxPoint...
26 // NOTE: This should probably be refactored to a common RowCol data type which
27 // is used for this and also for wxGridCellCoords.
28 //---------------------------------------------------------------------------
30 class WXDLLIMPEXP_CORE wxGBPosition
33 wxGBPosition() : m_row(0), m_col(0) {}
34 wxGBPosition(int row
, int col
) : m_row(row
), m_col(col
) {}
36 // default copy ctor and assignment operator are okay.
38 int GetRow() const { return m_row
; }
39 int GetCol() const { return m_col
; }
40 void SetRow(int row
) { m_row
= row
; }
41 void SetCol(int col
) { m_col
= col
; }
43 bool operator==(const wxGBPosition
& p
) const { return m_row
== p
.m_row
&& m_col
== p
.m_col
; }
44 bool operator!=(const wxGBPosition
& p
) const { return !(*this == p
); }
52 class WXDLLIMPEXP_CORE wxGBSpan
55 wxGBSpan() { Init(); }
56 wxGBSpan(int rowspan
, int colspan
)
58 // Initialize the members to valid values as not doing it may result in
59 // infinite loop in wxGBSizer code if the user passed 0 for any of
67 // default copy ctor and assignment operator are okay.
69 // Factor constructor creating an invalid wxGBSpan: this is mostly supposed
70 // to be used as return value for functions returning wxGBSpan in case of
72 static wxGBSpan
Invalid()
74 return wxGBSpan(NULL
);
77 int GetRowspan() const { return m_rowspan
; }
78 int GetColspan() const { return m_colspan
; }
79 void SetRowspan(int rowspan
)
81 wxCHECK_RET( rowspan
> 0, "Row span should be strictly positive" );
86 void SetColspan(int colspan
)
88 wxCHECK_RET( colspan
> 0, "Column span should be strictly positive" );
93 bool operator==(const wxGBSpan
& o
) const { return m_rowspan
== o
.m_rowspan
&& m_colspan
== o
.m_colspan
; }
94 bool operator!=(const wxGBSpan
& o
) const { return !(*this == o
); }
97 // This private ctor is used by Invalid() only.
98 wxGBSpan(struct InvalidCtorTag
*)
115 extern WXDLLIMPEXP_DATA_CORE(const wxGBSpan
) wxDefaultSpan
;
118 //---------------------------------------------------------------------------
120 //---------------------------------------------------------------------------
122 class WXDLLIMPEXP_FWD_CORE wxGridBagSizer
;
125 class WXDLLIMPEXP_CORE wxGBSizerItem
: public wxSizerItem
129 wxGBSizerItem( int width
,
131 const wxGBPosition
& pos
,
132 const wxGBSpan
& span
=wxDefaultSpan
,
135 wxObject
* userData
=NULL
);
138 wxGBSizerItem( wxWindow
*window
,
139 const wxGBPosition
& pos
,
140 const wxGBSpan
& span
=wxDefaultSpan
,
143 wxObject
* userData
=NULL
);
146 wxGBSizerItem( wxSizer
*sizer
,
147 const wxGBPosition
& pos
,
148 const wxGBSpan
& span
=wxDefaultSpan
,
151 wxObject
* userData
=NULL
);
157 // Get the grid position of the item
158 wxGBPosition
GetPos() const { return m_pos
; }
159 void GetPos(int& row
, int& col
) const;
161 // Get the row and column spanning of the item
162 wxGBSpan
GetSpan() const { return m_span
; }
163 void GetSpan(int& rowspan
, int& colspan
) const;
165 // If the item is already a member of a sizer then first ensure that there
166 // is no other item that would intersect with this one at the new
167 // position, then set the new position. Returns true if the change is
168 // successful and after the next Layout the item will be moved.
169 bool SetPos( const wxGBPosition
& pos
);
171 // If the item is already a member of a sizer then first ensure that there
172 // is no other item that would intersect with this one with its new
173 // spanning size, then set the new spanning. Returns true if the change
174 // is successful and after the next Layout the item will be resized.
175 bool SetSpan( const wxGBSpan
& span
);
177 // Returns true if this item and the other item intersect
178 bool Intersects(const wxGBSizerItem
& other
);
180 // Returns true if the given pos/span would intersect with this item.
181 bool Intersects(const wxGBPosition
& pos
, const wxGBSpan
& span
);
183 // Get the row and column of the endpoint of this item
184 void GetEndPos(int& row
, int& col
);
187 wxGridBagSizer
* GetGBSizer() const { return m_gbsizer
; }
188 void SetGBSizer(wxGridBagSizer
* sizer
) { m_gbsizer
= sizer
; }
194 wxGridBagSizer
* m_gbsizer
; // so SetPos/SetSpan can check for intersects
198 DECLARE_DYNAMIC_CLASS(wxGBSizerItem
)
199 wxDECLARE_NO_COPY_CLASS(wxGBSizerItem
);
203 //---------------------------------------------------------------------------
205 //---------------------------------------------------------------------------
208 class WXDLLIMPEXP_CORE wxGridBagSizer
: public wxFlexGridSizer
211 wxGridBagSizer(int vgap
= 0, int hgap
= 0 );
213 // The Add methods return true if the item was successfully placed at the
214 // given position, false if something was already there.
215 wxSizerItem
* Add( wxWindow
*window
,
216 const wxGBPosition
& pos
,
217 const wxGBSpan
& span
= wxDefaultSpan
,
220 wxObject
* userData
= NULL
);
221 wxSizerItem
* Add( wxSizer
*sizer
,
222 const wxGBPosition
& pos
,
223 const wxGBSpan
& span
= wxDefaultSpan
,
226 wxObject
* userData
= NULL
);
227 wxSizerItem
* Add( int width
,
229 const wxGBPosition
& pos
,
230 const wxGBSpan
& span
= wxDefaultSpan
,
233 wxObject
* userData
= NULL
);
234 wxSizerItem
* Add( wxGBSizerItem
*item
);
237 // Get/Set the size used for cells in the grid with no item.
238 wxSize
GetEmptyCellSize() const { return m_emptyCellSize
; }
239 void SetEmptyCellSize(const wxSize
& sz
) { m_emptyCellSize
= sz
; }
241 // Get the size of the specified cell, including hgap and vgap. Only
242 // valid after a Layout.
243 wxSize
GetCellSize(int row
, int col
) const;
245 // Get the grid position of the specified item (non-recursive)
246 wxGBPosition
GetItemPosition(wxWindow
*window
);
247 wxGBPosition
GetItemPosition(wxSizer
*sizer
);
248 wxGBPosition
GetItemPosition(size_t index
);
250 // Set the grid position of the specified item. Returns true on success.
251 // If the move is not allowed (because an item is already there) then
252 // false is returned. (non-recursive)
253 bool SetItemPosition(wxWindow
*window
, const wxGBPosition
& pos
);
254 bool SetItemPosition(wxSizer
*sizer
, const wxGBPosition
& pos
);
255 bool SetItemPosition(size_t index
, const wxGBPosition
& pos
);
257 // Get the row/col spanning of the specified item (non-recursive)
258 wxGBSpan
GetItemSpan(wxWindow
*window
);
259 wxGBSpan
GetItemSpan(wxSizer
*sizer
);
260 wxGBSpan
GetItemSpan(size_t index
);
262 // Set the row/col spanning of the specified item. Returns true on
263 // success. If the move is not allowed (because an item is already there)
264 // then false is returned. (non-recursive)
265 bool SetItemSpan(wxWindow
*window
, const wxGBSpan
& span
);
266 bool SetItemSpan(wxSizer
*sizer
, const wxGBSpan
& span
);
267 bool SetItemSpan(size_t index
, const wxGBSpan
& span
);
270 // Find the sizer item for the given window or subsizer, returns NULL if
271 // not found. (non-recursive)
272 wxGBSizerItem
* FindItem(wxWindow
* window
);
273 wxGBSizerItem
* FindItem(wxSizer
* sizer
);
276 // Return the sizer item for the given grid cell, or NULL if there is no
277 // item at that position. (non-recursive)
278 wxGBSizerItem
* FindItemAtPosition(const wxGBPosition
& pos
);
281 // Return the sizer item located at the point given in pt, or NULL if
282 // there is no item at that point. The (x,y) coordinates in pt correspond
283 // to the client coordinates of the window using the sizer for
284 // layout. (non-recursive)
285 wxGBSizerItem
* FindItemAtPoint(const wxPoint
& pt
);
288 // Return the sizer item that has a matching user data (it only compares
289 // pointer values) or NULL if not found. (non-recursive)
290 wxGBSizerItem
* FindItemWithData(const wxObject
* userData
);
293 // These are what make the sizer do size calculations and layout
294 virtual void RecalcSizes();
295 virtual wxSize
CalcMin();
298 // Look at all items and see if any intersect (or would overlap) the given
299 // item. Returns true if so, false if there would be no overlap. If an
300 // excludeItem is given then it will not be checked for intersection, for
301 // example it may be the item we are checking the position of.
302 bool CheckForIntersection(wxGBSizerItem
* item
, wxGBSizerItem
* excludeItem
= NULL
);
303 bool CheckForIntersection(const wxGBPosition
& pos
, const wxGBSpan
& span
, wxGBSizerItem
* excludeItem
= NULL
);
306 // The Add base class virtuals should not be used with this class, but
307 // we'll try to make them automatically select a location for the item
309 virtual wxSizerItem
* Add( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
310 virtual wxSizerItem
* Add( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
311 virtual wxSizerItem
* Add( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
313 // The Insert and Prepend base class virtuals that are not appropriate for
314 // this class and should not be used. Their implementation in this class
316 virtual wxSizerItem
* Add( wxSizerItem
*item
);
317 virtual wxSizerItem
* Insert( size_t index
, wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
318 virtual wxSizerItem
* Insert( size_t index
, wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
319 virtual wxSizerItem
* Insert( size_t index
, int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
320 virtual wxSizerItem
* Insert( size_t index
, wxSizerItem
*item
);
321 virtual wxSizerItem
* Prepend( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
322 virtual wxSizerItem
* Prepend( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
323 virtual wxSizerItem
* Prepend( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
324 virtual wxSizerItem
* Prepend( wxSizerItem
*item
);
328 wxGBPosition
FindEmptyCell();
329 void AdjustForOverflow();
331 wxSize m_emptyCellSize
;
336 DECLARE_CLASS(wxGridBagSizer
)
337 wxDECLARE_NO_COPY_CLASS(wxGridBagSizer
);
340 //---------------------------------------------------------------------------