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 // Factor constructor creating an invalid wxGBSpan: this is mostly supposed
71 // to be used as return value for functions returning wxGBSpan in case of
73 static wxGBSpan
Invalid()
75 return wxGBSpan(NULL
);
78 int GetRowspan() const { return m_rowspan
; }
79 int GetColspan() const { return m_colspan
; }
80 void SetRowspan(int rowspan
)
82 wxCHECK_RET( rowspan
> 0, "Row span should be strictly positive" );
87 void SetColspan(int colspan
)
89 wxCHECK_RET( colspan
> 0, "Column span should be strictly positive" );
94 bool operator==(const wxGBSpan
& o
) const { return m_rowspan
== o
.m_rowspan
&& m_colspan
== o
.m_colspan
; }
95 bool operator!=(const wxGBSpan
& o
) const { return !(*this == o
); }
98 // This private ctor is used by Invalid() only.
99 wxGBSpan(struct InvalidCtorTag
*)
116 extern WXDLLIMPEXP_DATA_CORE(const wxGBSpan
) wxDefaultSpan
;
119 //---------------------------------------------------------------------------
121 //---------------------------------------------------------------------------
123 class WXDLLIMPEXP_FWD_CORE wxGridBagSizer
;
126 class WXDLLIMPEXP_CORE wxGBSizerItem
: public wxSizerItem
130 wxGBSizerItem( int width
,
132 const wxGBPosition
& pos
,
133 const wxGBSpan
& span
=wxDefaultSpan
,
136 wxObject
* userData
=NULL
);
139 wxGBSizerItem( wxWindow
*window
,
140 const wxGBPosition
& pos
,
141 const wxGBSpan
& span
=wxDefaultSpan
,
144 wxObject
* userData
=NULL
);
147 wxGBSizerItem( wxSizer
*sizer
,
148 const wxGBPosition
& pos
,
149 const wxGBSpan
& span
=wxDefaultSpan
,
152 wxObject
* userData
=NULL
);
158 // Get the grid position of the item
159 wxGBPosition
GetPos() const { return m_pos
; }
160 void GetPos(int& row
, int& col
) const;
162 // Get the row and column spanning of the item
163 wxGBSpan
GetSpan() const { return m_span
; }
164 void GetSpan(int& rowspan
, int& colspan
) const;
166 // If the item is already a member of a sizer then first ensure that there
167 // is no other item that would intersect with this one at the new
168 // position, then set the new position. Returns true if the change is
169 // successful and after the next Layout the item will be moved.
170 bool SetPos( const wxGBPosition
& pos
);
172 // If the item is already a member of a sizer then first ensure that there
173 // is no other item that would intersect with this one with its new
174 // spanning size, then set the new spanning. Returns true if the change
175 // is successful and after the next Layout the item will be resized.
176 bool SetSpan( const wxGBSpan
& span
);
178 // Returns true if this item and the other item intersect
179 bool Intersects(const wxGBSizerItem
& other
);
181 // Returns true if the given pos/span would intersect with this item.
182 bool Intersects(const wxGBPosition
& pos
, const wxGBSpan
& span
);
184 // Get the row and column of the endpoint of this item
185 void GetEndPos(int& row
, int& col
);
188 wxGridBagSizer
* GetGBSizer() const { return m_gbsizer
; }
189 void SetGBSizer(wxGridBagSizer
* sizer
) { m_gbsizer
= sizer
; }
195 wxGridBagSizer
* m_gbsizer
; // so SetPos/SetSpan can check for intersects
199 DECLARE_DYNAMIC_CLASS(wxGBSizerItem
)
200 wxDECLARE_NO_COPY_CLASS(wxGBSizerItem
);
204 //---------------------------------------------------------------------------
206 //---------------------------------------------------------------------------
209 class WXDLLIMPEXP_CORE wxGridBagSizer
: public wxFlexGridSizer
212 wxGridBagSizer(int vgap
= 0, int hgap
= 0 );
214 // The Add methods return true if the item was successfully placed at the
215 // given position, false if something was already there.
216 wxSizerItem
* Add( wxWindow
*window
,
217 const wxGBPosition
& pos
,
218 const wxGBSpan
& span
= wxDefaultSpan
,
221 wxObject
* userData
= NULL
);
222 wxSizerItem
* Add( wxSizer
*sizer
,
223 const wxGBPosition
& pos
,
224 const wxGBSpan
& span
= wxDefaultSpan
,
227 wxObject
* userData
= NULL
);
228 wxSizerItem
* Add( int width
,
230 const wxGBPosition
& pos
,
231 const wxGBSpan
& span
= wxDefaultSpan
,
234 wxObject
* userData
= NULL
);
235 wxSizerItem
* Add( wxGBSizerItem
*item
);
238 // Get/Set the size used for cells in the grid with no item.
239 wxSize
GetEmptyCellSize() const { return m_emptyCellSize
; }
240 void SetEmptyCellSize(const wxSize
& sz
) { m_emptyCellSize
= sz
; }
242 // Get the size of the specified cell, including hgap and vgap. Only
243 // valid after a Layout.
244 wxSize
GetCellSize(int row
, int col
) const;
246 // Get the grid position of the specified item (non-recursive)
247 wxGBPosition
GetItemPosition(wxWindow
*window
);
248 wxGBPosition
GetItemPosition(wxSizer
*sizer
);
249 wxGBPosition
GetItemPosition(size_t index
);
251 // Set the grid position of the specified item. Returns true on success.
252 // If the move is not allowed (because an item is already there) then
253 // false is returned. (non-recursive)
254 bool SetItemPosition(wxWindow
*window
, const wxGBPosition
& pos
);
255 bool SetItemPosition(wxSizer
*sizer
, const wxGBPosition
& pos
);
256 bool SetItemPosition(size_t index
, const wxGBPosition
& pos
);
258 // Get the row/col spanning of the specified item (non-recursive)
259 wxGBSpan
GetItemSpan(wxWindow
*window
);
260 wxGBSpan
GetItemSpan(wxSizer
*sizer
);
261 wxGBSpan
GetItemSpan(size_t index
);
263 // Set the row/col spanning of the specified item. Returns true on
264 // success. If the move is not allowed (because an item is already there)
265 // then false is returned. (non-recursive)
266 bool SetItemSpan(wxWindow
*window
, const wxGBSpan
& span
);
267 bool SetItemSpan(wxSizer
*sizer
, const wxGBSpan
& span
);
268 bool SetItemSpan(size_t index
, const wxGBSpan
& span
);
271 // Find the sizer item for the given window or subsizer, returns NULL if
272 // not found. (non-recursive)
273 wxGBSizerItem
* FindItem(wxWindow
* window
);
274 wxGBSizerItem
* FindItem(wxSizer
* sizer
);
277 // Return the sizer item for the given grid cell, or NULL if there is no
278 // item at that position. (non-recursive)
279 wxGBSizerItem
* FindItemAtPosition(const wxGBPosition
& pos
);
282 // Return the sizer item located at the point given in pt, or NULL if
283 // there is no item at that point. The (x,y) coordinates in pt correspond
284 // to the client coordinates of the window using the sizer for
285 // layout. (non-recursive)
286 wxGBSizerItem
* FindItemAtPoint(const wxPoint
& pt
);
289 // Return the sizer item that has a matching user data (it only compares
290 // pointer values) or NULL if not found. (non-recursive)
291 wxGBSizerItem
* FindItemWithData(const wxObject
* userData
);
294 // These are what make the sizer do size calculations and layout
295 virtual void RecalcSizes();
296 virtual wxSize
CalcMin();
299 // Look at all items and see if any intersect (or would overlap) the given
300 // item. Returns true if so, false if there would be no overlap. If an
301 // excludeItem is given then it will not be checked for intersection, for
302 // example it may be the item we are checking the position of.
303 bool CheckForIntersection(wxGBSizerItem
* item
, wxGBSizerItem
* excludeItem
= NULL
);
304 bool CheckForIntersection(const wxGBPosition
& pos
, const wxGBSpan
& span
, wxGBSizerItem
* excludeItem
= NULL
);
307 // The Add base class virtuals should not be used with this class, but
308 // we'll try to make them automatically select a location for the item
310 virtual wxSizerItem
* Add( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
311 virtual wxSizerItem
* Add( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
312 virtual wxSizerItem
* Add( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
314 // The Insert and Prepend base class virtuals that are not appropriate for
315 // this class and should not be used. Their implementation in this class
317 virtual wxSizerItem
* Add( wxSizerItem
*item
);
318 virtual wxSizerItem
* Insert( size_t index
, wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
319 virtual wxSizerItem
* Insert( size_t index
, wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
320 virtual wxSizerItem
* Insert( size_t index
, int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
321 virtual wxSizerItem
* Insert( size_t index
, wxSizerItem
*item
);
322 virtual wxSizerItem
* Prepend( wxWindow
*window
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
323 virtual wxSizerItem
* Prepend( wxSizer
*sizer
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
324 virtual wxSizerItem
* Prepend( int width
, int height
, int proportion
= 0, int flag
= 0, int border
= 0, wxObject
* userData
= NULL
);
325 virtual wxSizerItem
* Prepend( wxSizerItem
*item
);
329 wxGBPosition
FindEmptyCell();
330 void AdjustForOverflow();
332 wxSize m_emptyCellSize
;
337 DECLARE_CLASS(wxGridBagSizer
)
338 wxDECLARE_NO_COPY_CLASS(wxGridBagSizer
);
341 //---------------------------------------------------------------------------