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
10 // Copyright: (c) Robin Dunn
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 #ifndef __WXGBSIZER_H__
15 #define __WXGBSIZER_H__
20 //---------------------------------------------------------------------------
21 // Classes to represent a position in the grid and a size of an item in the
22 // grid, IOW, the number of rows and columns it occupies. I chose to use these
23 // instead of wxPoint and wxSize because they are (x,y) and usually pixel
24 // oriented while grids and tables are usually thought of as (row,col) so some
25 // confusion would definitely result in using wxPoint...
27 // NOTE: This should probably be refactored to a common RowCol data type which
28 // is used for this and also for wxGridCellCoords.
29 //---------------------------------------------------------------------------
31 class WXDLLIMPEXP_CORE wxGBPosition
34 wxGBPosition() : m_row(0), m_col(0) {}
35 wxGBPosition(int row
, int col
) : m_row(row
), m_col(col
) {}
37 // default copy ctor and assignment operator are okay.
39 int GetRow() const { return m_row
; }
40 int GetCol() const { return m_col
; }
41 void SetRow(int row
) { m_row
= row
; }
42 void SetCol(int col
) { m_col
= col
; }
44 bool operator==(const wxGBPosition
& p
) const { return m_row
== p
.m_row
&& m_col
== p
.m_col
; }
45 bool operator!=(const wxGBPosition
& p
) const { return !(*this == p
); }
53 class WXDLLIMPEXP_CORE wxGBSpan
56 wxGBSpan() { Init(); }
57 wxGBSpan(int rowspan
, int colspan
)
59 // Initialize the members to valid values as not doing it may result in
60 // infinite loop in wxGBSizer code if the user passed 0 for any of
68 // default copy ctor and assignment operator are okay.
70 int GetRowspan() const { return m_rowspan
; }
71 int GetColspan() const { return m_colspan
; }
72 void SetRowspan(int rowspan
)
74 wxCHECK_RET( rowspan
> 0, "Row span should be strictly positive" );
79 void SetColspan(int colspan
)
81 wxCHECK_RET( colspan
> 0, "Column span should be strictly positive" );
86 bool operator==(const wxGBSpan
& o
) const { return m_rowspan
== o
.m_rowspan
&& m_colspan
== o
.m_colspan
; }
87 bool operator!=(const wxGBSpan
& o
) const { return !(*this == o
); }
101 extern WXDLLIMPEXP_DATA_CORE(const wxGBSpan
) wxDefaultSpan
;
104 //---------------------------------------------------------------------------
106 //---------------------------------------------------------------------------
108 class WXDLLIMPEXP_FWD_CORE wxGridBagSizer
;
111 class WXDLLIMPEXP_CORE wxGBSizerItem
: public wxSizerItem
115 wxGBSizerItem( int width
,
117 const wxGBPosition
& pos
,
118 const wxGBSpan
& span
=wxDefaultSpan
,
121 wxObject
* userData
=NULL
);
124 wxGBSizerItem( wxWindow
*window
,
125 const wxGBPosition
& pos
,
126 const wxGBSpan
& span
=wxDefaultSpan
,
129 wxObject
* userData
=NULL
);
132 wxGBSizerItem( wxSizer
*sizer
,
133 const wxGBPosition
& pos
,
134 const wxGBSpan
& span
=wxDefaultSpan
,
137 wxObject
* userData
=NULL
);
143 // Get the grid position of the item
144 wxGBPosition
GetPos() const { return m_pos
; }
145 void GetPos(int& row
, int& col
) const;
147 // Get the row and column spanning of the item
148 wxGBSpan
GetSpan() const { return m_span
; }
149 void GetSpan(int& rowspan
, int& colspan
) const;
151 // If the item is already a member of a sizer then first ensure that there
152 // is no other item that would intersect with this one at the new
153 // position, then set the new position. Returns true if the change is
154 // successful and after the next Layout the item will be moved.
155 bool SetPos( const wxGBPosition
& pos
);
157 // If the item is already a member of a sizer then first ensure that there
158 // is no other item that would intersect with this one with its new
159 // spanning size, then set the new spanning. Returns true if the change
160 // is successful and after the next Layout the item will be resized.
161 bool SetSpan( const wxGBSpan
& span
);
163 // Returns true if this item and the other item intersect
164 bool Intersects(const wxGBSizerItem
& other
);
166 // Returns true if the given pos/span would intersect with this item.
167 bool Intersects(const wxGBPosition
& pos
, const wxGBSpan
& span
);
169 // Get the row and column of the endpoint of this item
170 void GetEndPos(int& row
, int& col
);
173 wxGridBagSizer
* GetGBSizer() const { return m_gbsizer
; }
174 void SetGBSizer(wxGridBagSizer
* sizer
) { m_gbsizer
= sizer
; }
180 wxGridBagSizer
* m_gbsizer
; // so SetPos/SetSpan can check for intersects
184 DECLARE_DYNAMIC_CLASS(wxGBSizerItem
)
185 wxDECLARE_NO_COPY_CLASS(wxGBSizerItem
);
189 //---------------------------------------------------------------------------
191 //---------------------------------------------------------------------------
194 class WXDLLIMPEXP_CORE wxGridBagSizer
: public wxFlexGridSizer
197 wxGridBagSizer(int vgap
= 0, int hgap
= 0 );
199 // The Add methods return true if the item was successfully placed at the
200 // given position, false if something was already there.
201 wxSizerItem
* Add( wxWindow
*window
,
202 const wxGBPosition
& pos
,
203 const wxGBSpan
& span
= wxDefaultSpan
,
206 wxObject
* userData
= NULL
);
207 wxSizerItem
* Add( wxSizer
*sizer
,
208 const wxGBPosition
& pos
,
209 const wxGBSpan
& span
= wxDefaultSpan
,
212 wxObject
* userData
= NULL
);
213 wxSizerItem
* Add( int width
,
215 const wxGBPosition
& pos
,
216 const wxGBSpan
& span
= wxDefaultSpan
,
219 wxObject
* userData
= NULL
);
220 wxSizerItem
* Add( wxGBSizerItem
*item
);
223 // Get/Set the size used for cells in the grid with no item.
224 wxSize
GetEmptyCellSize() const { return m_emptyCellSize
; }
225 void SetEmptyCellSize(const wxSize
& sz
) { m_emptyCellSize
= sz
; }
227 // Get the size of the specified cell, including hgap and vgap. Only
228 // valid after a Layout.
229 wxSize
GetCellSize(int row
, int col
) const;
231 // Get the grid position of the specified item (non-recursive)
232 wxGBPosition
GetItemPosition(wxWindow
*window
);
233 wxGBPosition
GetItemPosition(wxSizer
*sizer
);
234 wxGBPosition
GetItemPosition(size_t index
);
236 // Set the grid position of the specified item. Returns true on success.
237 // If the move is not allowed (because an item is already there) then
238 // false is returned. (non-recursive)
239 bool SetItemPosition(wxWindow
*window
, const wxGBPosition
& pos
);
240 bool SetItemPosition(wxSizer
*sizer
, const wxGBPosition
& pos
);
241 bool SetItemPosition(size_t index
, const wxGBPosition
& pos
);
243 // Get the row/col spanning of the specified item (non-recursive)
244 wxGBSpan
GetItemSpan(wxWindow
*window
);
245 wxGBSpan
GetItemSpan(wxSizer
*sizer
);
246 wxGBSpan
GetItemSpan(size_t index
);
248 // Set the row/col spanning of the specified item. Returns true on
249 // success. If the move is not allowed (because an item is already there)
250 // then false is returned. (non-recursive)
251 bool SetItemSpan(wxWindow
*window
, const wxGBSpan
& span
);
252 bool SetItemSpan(wxSizer
*sizer
, const wxGBSpan
& span
);
253 bool SetItemSpan(size_t index
, const wxGBSpan
& span
);
256 // Find the sizer item for the given window or subsizer, returns NULL if
257 // not found. (non-recursive)
258 wxGBSizerItem
* FindItem(wxWindow
* window
);
259 wxGBSizerItem
* FindItem(wxSizer
* sizer
);
262 // Return the sizer item for the given grid cell, or NULL if there is no
263 // item at that position. (non-recursive)
264 wxGBSizerItem
* FindItemAtPosition(const wxGBPosition
& pos
);
267 // Return the sizer item located at the point given in pt, or NULL if
268 // there is no item at that point. The (x,y) coordinates in pt correspond
269 // to the client coordinates of the window using the sizer for
270 // layout. (non-recursive)
271 wxGBSizerItem
* FindItemAtPoint(const wxPoint
& pt
);
274 // Return the sizer item that has a matching user data (it only compares
275 // pointer values) or NULL if not found. (non-recursive)
276 wxGBSizerItem
* FindItemWithData(const wxObject
* userData
);
279 // These are what make the sizer do size calculations and layout
280 virtual void RecalcSizes();
281 virtual wxSize
CalcMin();
284 // Look at all items and see if any intersect (or would overlap) the given
285 // item. Returns true if so, false if there would be no overlap. If an
286 // excludeItem is given then it will not be checked for intersection, for
287 // example it may be the item we are checking the position of.
288 bool CheckForIntersection(wxGBSizerItem
* item
, wxGBSizerItem
* excludeItem
= NULL
);
289 bool CheckForIntersection(const wxGBPosition
& pos
, const wxGBSpan
& span
, wxGBSizerItem
* excludeItem
= NULL
);
292 // The Add base class virtuals should not be used with this class, but
293 // we'll try to make them automatically select a location for the item
295 virtual wxSizerItem
* Add( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
296 virtual wxSizerItem
* Add( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
297 virtual wxSizerItem
* Add( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
299 // The Insert and Prepend base class virtuals that are not appropriate for
300 // this class and should not be used. Their implementation in this class
302 virtual wxSizerItem
* Add( wxSizerItem
*item
);
303 virtual wxSizerItem
* Insert( size_t index
, wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
304 virtual wxSizerItem
* Insert( size_t index
, wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
305 virtual wxSizerItem
* Insert( size_t index
, int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
306 virtual wxSizerItem
* Insert( size_t index
, wxSizerItem
*item
);
307 virtual wxSizerItem
* Prepend( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
308 virtual wxSizerItem
* Prepend( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
309 virtual wxSizerItem
* Prepend( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
310 virtual wxSizerItem
* Prepend( wxSizerItem
*item
);
314 wxGBPosition
FindEmptyCell();
315 void AdjustForOverflow();
317 wxSize m_emptyCellSize
;
322 DECLARE_CLASS(wxGridBagSizer
)
323 wxDECLARE_NO_COPY_CLASS(wxGridBagSizer
);
326 //---------------------------------------------------------------------------