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 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #include "wx/gbsizer.h"
23 //---------------------------------------------------------------------------
25 IMPLEMENT_DYNAMIC_CLASS(wxGBSizerItem
, wxSizerItem
)
26 IMPLEMENT_CLASS(wxGridBagSizer
, wxFlexGridSizer
)
28 const wxGBSpan wxDefaultSpan
;
30 //---------------------------------------------------------------------------
32 //---------------------------------------------------------------------------
34 wxGBSizerItem::wxGBSizerItem( int width
,
36 const wxGBPosition
& pos
,
41 : wxSizerItem(width
, height
, 0, flag
, border
, userData
),
49 wxGBSizerItem::wxGBSizerItem( wxWindow
*window
,
50 const wxGBPosition
& pos
,
55 : wxSizerItem(window
, 0, flag
, border
, userData
),
63 wxGBSizerItem::wxGBSizerItem( wxSizer
*sizer
,
64 const wxGBPosition
& pos
,
69 : wxSizerItem(sizer
, 0, flag
, border
, userData
),
76 wxGBSizerItem::wxGBSizerItem()
84 //---------------------------------------------------------------------------
87 void wxGBSizerItem::GetPos(int& row
, int& col
) const
93 void wxGBSizerItem::GetSpan(int& rowspan
, int& colspan
) const
95 rowspan
= m_span
.GetRowspan();
96 colspan
= m_span
.GetColspan();
100 bool wxGBSizerItem::SetPos( const wxGBPosition
& pos
)
104 wxCHECK_MSG( !m_gbsizer
->CheckForIntersection(pos
, m_span
, this), false,
105 wxT("An item is already at that position") );
111 bool wxGBSizerItem::SetSpan( const wxGBSpan
& span
)
115 wxCHECK_MSG( !m_gbsizer
->CheckForIntersection(m_pos
, span
, this), false,
116 wxT("An item is already at that position") );
123 inline bool InRange(int val
, int min
, int max
)
125 return (val
>= min
&& val
<= max
);
128 bool wxGBSizerItem::Intersects(const wxGBSizerItem
& other
)
130 return Intersects(other
.GetPos(), other
.GetSpan());
133 bool wxGBSizerItem::Intersects(const wxGBPosition
& pos
, const wxGBSpan
& span
)
136 int row
, col
, endrow
, endcol
;
137 int otherrow
, othercol
, otherendrow
, otherendcol
;
140 GetEndPos(endrow
, endcol
);
142 otherrow
= pos
.GetRow();
143 othercol
= pos
.GetCol();
144 otherendrow
= otherrow
+ span
.GetRowspan() - 1;
145 otherendcol
= othercol
+ span
.GetColspan() - 1;
147 // is the other item's start or end in the range of this one?
148 if (( InRange(otherrow
, row
, endrow
) && InRange(othercol
, col
, endcol
) ) ||
149 ( InRange(otherendrow
, row
, endrow
) && InRange(otherendcol
, col
, endcol
) ))
152 // is this item's start or end in the range of the other one?
153 if (( InRange(row
, otherrow
, otherendrow
) && InRange(col
, othercol
, otherendcol
) ) ||
154 ( InRange(endrow
, otherrow
, otherendrow
) && InRange(endcol
, othercol
, otherendcol
) ))
161 void wxGBSizerItem::GetEndPos(int& row
, int& col
)
163 row
= m_pos
.GetRow() + m_span
.GetRowspan() - 1;
164 col
= m_pos
.GetCol() + m_span
.GetColspan() - 1;
168 //---------------------------------------------------------------------------
170 //---------------------------------------------------------------------------
172 wxGridBagSizer::wxGridBagSizer(int vgap
, int hgap
)
173 : wxFlexGridSizer(1, vgap
, hgap
),
174 m_emptyCellSize(10,20)
180 wxSizerItem
* wxGridBagSizer::Add( wxWindow
*window
,
181 const wxGBPosition
& pos
, const wxGBSpan
& span
,
182 int flag
, int border
, wxObject
* userData
)
184 wxGBSizerItem
* item
= new wxGBSizerItem(window
, pos
, span
, flag
, border
, userData
);
194 wxSizerItem
* wxGridBagSizer::Add( wxSizer
*sizer
,
195 const wxGBPosition
& pos
, const wxGBSpan
& span
,
196 int flag
, int border
, wxObject
* userData
)
198 wxGBSizerItem
* item
= new wxGBSizerItem(sizer
, pos
, span
, flag
, border
, userData
);
208 wxSizerItem
* wxGridBagSizer::Add( int width
, int height
,
209 const wxGBPosition
& pos
, const wxGBSpan
& span
,
210 int flag
, int border
, wxObject
* userData
)
212 wxGBSizerItem
* item
= new wxGBSizerItem(width
, height
, pos
, span
, flag
, border
, userData
);
222 wxSizerItem
* wxGridBagSizer::Add( wxGBSizerItem
*item
)
224 wxCHECK_MSG( !CheckForIntersection(item
), NULL
,
225 wxT("An item is already at that position") );
226 m_children
.Append(item
);
227 item
->SetGBSizer(this);
228 if ( item
->GetWindow() )
229 item
->GetWindow()->SetContainingSizer( this );
231 // extend the number of rows/columns of the underlying wxFlexGridSizer if
234 item
->GetEndPos(row
, col
);
238 if ( row
> GetRows() )
240 if ( col
> GetCols() )
248 //---------------------------------------------------------------------------
250 wxSize
wxGridBagSizer::GetCellSize(int row
, int col
) const
252 wxCHECK_MSG( (row
< m_rows
) && (col
< m_cols
),
254 wxT("Invalid cell."));
255 return wxSize( m_colWidths
[col
], m_rowHeights
[row
] );
259 wxGBPosition
wxGridBagSizer::GetItemPosition(wxWindow
*window
)
261 wxGBPosition
badpos(-1,-1);
262 wxGBSizerItem
* item
= FindItem(window
);
263 wxCHECK_MSG(item
, badpos
, wxT("Failed to find item."));
264 return item
->GetPos();
268 wxGBPosition
wxGridBagSizer::GetItemPosition(wxSizer
*sizer
)
270 wxGBPosition
badpos(-1,-1);
271 wxGBSizerItem
* item
= FindItem(sizer
);
272 wxCHECK_MSG(item
, badpos
, wxT("Failed to find item."));
273 return item
->GetPos();
277 wxGBPosition
wxGridBagSizer::GetItemPosition(size_t index
)
279 wxGBPosition
badpos(-1,-1);
280 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
281 wxCHECK_MSG( node
, badpos
, wxT("Failed to find item.") );
282 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
283 return item
->GetPos();
288 bool wxGridBagSizer::SetItemPosition(wxWindow
*window
, const wxGBPosition
& pos
)
290 wxGBSizerItem
* item
= FindItem(window
);
291 wxCHECK_MSG(item
, false, wxT("Failed to find item."));
292 return item
->SetPos(pos
);
296 bool wxGridBagSizer::SetItemPosition(wxSizer
*sizer
, const wxGBPosition
& pos
)
298 wxGBSizerItem
* item
= FindItem(sizer
);
299 wxCHECK_MSG(item
, false, wxT("Failed to find item."));
300 return item
->SetPos(pos
);
304 bool wxGridBagSizer::SetItemPosition(size_t index
, const wxGBPosition
& pos
)
306 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
307 wxCHECK_MSG( node
, false, wxT("Failed to find item.") );
308 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
309 return item
->SetPos(pos
);
314 wxGBSpan
wxGridBagSizer::GetItemSpan(wxWindow
*window
)
316 wxGBSpan
badspan(-1,-1);
317 wxGBSizerItem
* item
= FindItem(window
);
318 wxCHECK_MSG( item
, badspan
, wxT("Failed to find item.") );
319 return item
->GetSpan();
323 wxGBSpan
wxGridBagSizer::GetItemSpan(wxSizer
*sizer
)
325 wxGBSpan
badspan(-1,-1);
326 wxGBSizerItem
* item
= FindItem(sizer
);
327 wxCHECK_MSG( item
, badspan
, wxT("Failed to find item.") );
328 return item
->GetSpan();
332 wxGBSpan
wxGridBagSizer::GetItemSpan(size_t index
)
334 wxGBSpan
badspan(-1,-1);
335 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
336 wxCHECK_MSG( node
, badspan
, wxT("Failed to find item.") );
337 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
338 return item
->GetSpan();
343 bool wxGridBagSizer::SetItemSpan(wxWindow
*window
, const wxGBSpan
& span
)
345 wxGBSizerItem
* item
= FindItem(window
);
346 wxCHECK_MSG(item
, false, wxT("Failed to find item."));
347 return item
->SetSpan(span
);
351 bool wxGridBagSizer::SetItemSpan(wxSizer
*sizer
, const wxGBSpan
& span
)
353 wxGBSizerItem
* item
= FindItem(sizer
);
354 wxCHECK_MSG(item
, false, wxT("Failed to find item."));
355 return item
->SetSpan(span
);
359 bool wxGridBagSizer::SetItemSpan(size_t index
, const wxGBSpan
& span
)
361 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
362 wxCHECK_MSG( node
, false, wxT("Failed to find item.") );
363 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
364 return item
->SetSpan(span
);
370 wxGBSizerItem
* wxGridBagSizer::FindItem(wxWindow
* window
)
372 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
375 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
376 if ( item
->GetWindow() == window
)
378 node
= node
->GetNext();
384 wxGBSizerItem
* wxGridBagSizer::FindItem(wxSizer
* sizer
)
386 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
389 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
390 if ( item
->GetSizer() == sizer
)
392 node
= node
->GetNext();
400 wxGBSizerItem
* wxGridBagSizer::FindItemAtPosition(const wxGBPosition
& pos
)
402 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
405 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
406 if ( item
->Intersects(pos
, wxDefaultSpan
) )
408 node
= node
->GetNext();
416 wxGBSizerItem
* wxGridBagSizer::FindItemAtPoint(const wxPoint
& pt
)
418 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
421 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
422 wxRect
rect(item
->GetPosition(), item
->GetSize());
423 rect
.Inflate(m_hgap
, m_vgap
);
424 if ( rect
.Contains(pt
) )
426 node
= node
->GetNext();
434 wxGBSizerItem
* wxGridBagSizer::FindItemWithData(const wxObject
* userData
)
436 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
439 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
440 if ( item
->GetUserData() == userData
)
442 node
= node
->GetNext();
450 //---------------------------------------------------------------------------
452 // Figure out what all the min row heights and col widths are, and calculate
453 // min size from that.
454 wxSize
wxGridBagSizer::CalcMin()
458 if (m_children
.GetCount() == 0)
459 return m_emptyCellSize
;
461 m_rowHeights
.Empty();
464 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
467 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
468 if ( item
->IsShown() )
470 int row
, col
, endrow
, endcol
;
472 item
->GetPos(row
, col
);
473 item
->GetEndPos(endrow
, endcol
);
475 // fill heights and widths upto this item if needed
476 while ( (int)m_rowHeights
.GetCount() <= endrow
)
477 m_rowHeights
.Add(m_emptyCellSize
.GetHeight());
478 while ( (int)m_colWidths
.GetCount() <= endcol
)
479 m_colWidths
.Add(m_emptyCellSize
.GetWidth());
481 // See if this item increases the size of its row(s) or col(s)
482 wxSize
size(item
->CalcMin());
483 for (idx
=row
; idx
<= endrow
; idx
++)
484 m_rowHeights
[idx
] = wxMax(m_rowHeights
[idx
], size
.GetHeight() / (endrow
-row
+1));
485 for (idx
=col
; idx
<= endcol
; idx
++)
486 m_colWidths
[idx
] = wxMax(m_colWidths
[idx
], size
.GetWidth() / (endcol
-col
+1));
488 node
= node
->GetNext();
492 AdjustForFlexDirection();
494 // Now traverse the heights and widths arrays calcing the totals, including gaps
496 m_cols
= m_colWidths
.GetCount();
497 for (idx
=0; idx
< m_cols
; idx
++)
498 width
+= m_colWidths
[idx
] + ( idx
== m_cols
-1 ? 0 : m_hgap
);
501 m_rows
= m_rowHeights
.GetCount();
502 for (idx
=0; idx
< m_rows
; idx
++)
503 height
+= m_rowHeights
[idx
] + ( idx
== m_rows
-1 ? 0 : m_vgap
);
505 m_calculatedMinSize
= wxSize(width
, height
);
506 return m_calculatedMinSize
;
511 void wxGridBagSizer::RecalcSizes()
513 if (m_children
.GetCount() == 0)
516 wxPoint
pt( GetPosition() );
517 wxSize
sz( GetSize() );
519 m_rows
= m_rowHeights
.GetCount();
520 m_cols
= m_colWidths
.GetCount();
521 int idx
, width
, height
;
523 AdjustForGrowables(sz
);
525 // Find the start positions on the window of the rows and columns
527 rowpos
.Add(0, m_rows
);
529 for (idx
=0; idx
< m_rows
; idx
++)
531 height
= m_rowHeights
[idx
] + m_vgap
;
537 colpos
.Add(0, m_cols
);
539 for (idx
=0; idx
< m_cols
; idx
++)
541 width
= m_colWidths
[idx
] + m_hgap
;
547 // Now iterate the children, setting each child's dimensions
548 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
551 int row
, col
, endrow
, endcol
;
552 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
554 if ( item
->IsShown() )
556 item
->GetPos(row
, col
);
557 item
->GetEndPos(endrow
, endcol
);
560 for(idx
=row
; idx
<= endrow
; idx
++)
561 height
+= m_rowHeights
[idx
];
562 height
+= (endrow
- row
) * m_vgap
; // add a vgap for every row spanned
565 for (idx
=col
; idx
<= endcol
; idx
++)
566 width
+= m_colWidths
[idx
];
567 width
+= (endcol
- col
) * m_hgap
; // add a hgap for every col spanned
569 SetItemBounds(item
, colpos
[col
], rowpos
[row
], width
, height
);
572 node
= node
->GetNext();
577 // Sometimes CalcMin can result in some rows or cols having too much space in
578 // them because as it traverses the items it makes some assumptions when
579 // items span to other cells. But those assumptions can become invalid later
580 // on when other items are fitted into the same rows or columns that the
581 // spanning item occupies. This method tries to find those situations and
583 void wxGridBagSizer::AdjustForOverflow()
587 for (row
=0; row
<(int)m_rowHeights
.GetCount(); row
++)
589 int rowExtra
=INT_MAX
;
590 int rowHeight
= m_rowHeights
[row
];
591 for (col
=0; col
<(int)m_colWidths
.GetCount(); col
++)
593 wxGBPosition
pos(row
,col
);
594 wxGBSizerItem
* item
= FindItemAtPosition(pos
);
595 if ( !item
|| !item
->IsShown() )
599 item
->GetEndPos(endrow
, endcol
);
601 // If the item starts in this position and doesn't span rows, then
602 // just look at the whole item height
603 if ( item
->GetPos() == pos
&& endrow
== row
)
605 int itemHeight
= item
->CalcMin().GetHeight();
606 rowExtra
= wxMin(rowExtra
, rowHeight
- itemHeight
);
610 // Otherwise, only look at spanning items if they end on this row
613 // first deduct the portions of the item that are on prior rows
614 int itemHeight
= item
->CalcMin().GetHeight();
615 for (int r
=item
->GetPos().GetRow(); r
<row
; r
++)
616 itemHeight
-= (m_rowHeights
[r
] + GetHGap());
618 if ( itemHeight
< 0 )
621 // and check how much is left
622 rowExtra
= wxMin(rowExtra
, rowHeight
- itemHeight
);
625 if ( rowExtra
&& rowExtra
!= INT_MAX
)
626 m_rowHeights
[row
] -= rowExtra
;
629 // Now do the same thing for columns
630 for (col
=0; col
<(int)m_colWidths
.GetCount(); col
++)
632 int colExtra
=INT_MAX
;
633 int colWidth
= m_colWidths
[col
];
634 for (row
=0; row
<(int)m_rowHeights
.GetCount(); row
++)
636 wxGBPosition
pos(row
,col
);
637 wxGBSizerItem
* item
= FindItemAtPosition(pos
);
638 if ( !item
|| !item
->IsShown() )
642 item
->GetEndPos(endrow
, endcol
);
644 if ( item
->GetPos() == pos
&& endcol
== col
)
646 int itemWidth
= item
->CalcMin().GetWidth();
647 colExtra
= wxMin(colExtra
, colWidth
- itemWidth
);
653 int itemWidth
= item
->CalcMin().GetWidth();
654 for (int c
=item
->GetPos().GetCol(); c
<col
; c
++)
655 itemWidth
-= (m_colWidths
[c
] + GetVGap());
660 colExtra
= wxMin(colExtra
, colWidth
- itemWidth
);
663 if ( colExtra
&& colExtra
!= INT_MAX
)
664 m_colWidths
[col
] -= colExtra
;
670 //---------------------------------------------------------------------------
672 bool wxGridBagSizer::CheckForIntersection(wxGBSizerItem
* item
, wxGBSizerItem
* excludeItem
)
674 return CheckForIntersection(item
->GetPos(), item
->GetSpan(), excludeItem
);
677 bool wxGridBagSizer::CheckForIntersection(const wxGBPosition
& pos
, const wxGBSpan
& span
, wxGBSizerItem
* excludeItem
)
679 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
682 wxGBSizerItem
* item
= (wxGBSizerItem
*)node
->GetData();
683 node
= node
->GetNext();
685 if ( excludeItem
&& item
== excludeItem
)
688 if ( item
->Intersects(pos
, span
) )
696 // Assumes a 10x10 grid, and returns the first empty cell found. This is
697 // really stupid but it is only used by the Add methods that match the base
698 // class virtuals, which should normally not be used anyway...
699 wxGBPosition
wxGridBagSizer::FindEmptyCell()
703 for (row
=0; row
<10; row
++)
704 for (col
=0; col
<10; col
++)
706 wxGBPosition
pos(row
, col
);
707 if ( !CheckForIntersection(pos
, wxDefaultSpan
) )
710 return wxGBPosition(-1, -1);
714 //---------------------------------------------------------------------------
716 // The Add base class virtuals should not be used with this class, but
717 // we'll try to make them automatically select a location for the item
720 wxSizerItem
* wxGridBagSizer::Add( wxWindow
*window
, int, int flag
, int border
, wxObject
* userData
)
722 return Add(window
, FindEmptyCell(), wxDefaultSpan
, flag
, border
, userData
);
725 wxSizerItem
* wxGridBagSizer::Add( wxSizer
*sizer
, int, int flag
, int border
, wxObject
* userData
)
727 return Add(sizer
, FindEmptyCell(), wxDefaultSpan
, flag
, border
, userData
);
730 wxSizerItem
* wxGridBagSizer::Add( int width
, int height
, int, int flag
, int border
, wxObject
* userData
)
732 return Add(width
, height
, FindEmptyCell(), wxDefaultSpan
, flag
, border
, userData
);
737 // The Insert nad Prepend base class virtuals that are not appropriate for
738 // this class and should not be used. Their implementation in this class
741 wxSizerItem
* wxGridBagSizer::Add( wxSizerItem
* )
743 wxFAIL_MSG(wxT("Invalid Add form called."));
747 wxSizerItem
* wxGridBagSizer::Prepend( wxWindow
*, int, int, int, wxObject
* )
749 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
753 wxSizerItem
* wxGridBagSizer::Prepend( wxSizer
*, int, int, int, wxObject
* )
755 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
759 wxSizerItem
* wxGridBagSizer::Prepend( int, int, int, int, int, wxObject
* )
761 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
765 wxSizerItem
* wxGridBagSizer::Prepend( wxSizerItem
* )
767 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
772 wxSizerItem
* wxGridBagSizer::Insert( size_t, wxWindow
*, int, int, int, wxObject
* )
774 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
778 wxSizerItem
* wxGridBagSizer::Insert( size_t, wxSizer
*, int, int, int, wxObject
* )
780 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
784 wxSizerItem
* wxGridBagSizer::Insert( size_t, int, int, int, int, int, wxObject
* )
786 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
790 wxSizerItem
* wxGridBagSizer::Insert( size_t, wxSizerItem
* )
792 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
797 //---------------------------------------------------------------------------
798 //---------------------------------------------------------------------------