]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/gbsizer.cpp
Warning fixes, source cleaning.
[wxWidgets.git] / src / common / gbsizer.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: gbsizer.cpp
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
6//
7// Author: Robin Dunn
8// Created: 03-Nov-2003
9// RCS-ID: $Id$
10// Copyright: (c) Robin Dunn
11// Licence: wxWindows licence
12/////////////////////////////////////////////////////////////////////////////
13
14
15#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16#pragma implementation "gbsizer.h"
17#endif
18
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
26#include "wx/gbsizer.h"
27
28//---------------------------------------------------------------------------
29
30IMPLEMENT_DYNAMIC_CLASS(wxGBSizerItem, wxSizerItem)
31IMPLEMENT_CLASS(wxGridBagSizer, wxFlexGridSizer)
32
33const wxGBSpan wxDefaultSpan;
34
35//---------------------------------------------------------------------------
36// wxGBSizerItem
37//---------------------------------------------------------------------------
38
39wxGBSizerItem::wxGBSizerItem( int width,
40 int height,
41 const wxGBPosition& pos,
42 const wxGBSpan& span,
43 int flag,
44 int border,
45 wxObject* userData)
46 : wxSizerItem(width, height, 0, flag, border, userData),
47 m_pos(pos),
48 m_span(span),
49 m_gbsizer(NULL)
50{
51}
52
53
54wxGBSizerItem::wxGBSizerItem( wxWindow *window,
55 const wxGBPosition& pos,
56 const wxGBSpan& span,
57 int flag,
58 int border,
59 wxObject* userData )
60 : wxSizerItem(window, 0, flag, border, userData),
61 m_pos(pos),
62 m_span(span),
63 m_gbsizer(NULL)
64{
65}
66
67
68wxGBSizerItem::wxGBSizerItem( wxSizer *sizer,
69 const wxGBPosition& pos,
70 const wxGBSpan& span,
71 int flag,
72 int border,
73 wxObject* userData )
74 : wxSizerItem(sizer, 0, flag, border, userData),
75 m_pos(pos),
76 m_span(span),
77 m_gbsizer(NULL)
78{
79}
80
81wxGBSizerItem::wxGBSizerItem()
82 : wxSizerItem(),
83 m_pos(-1,-1),
84 m_span(-1,-1),
85 m_gbsizer(NULL)
86{
87}
88
89//---------------------------------------------------------------------------
90
91
92void wxGBSizerItem::GetPos(int& row, int& col) const
93{
94 row = m_pos.GetRow();
95 col = m_pos.GetCol();
96}
97
98void wxGBSizerItem::GetSpan(int& rowspan, int& colspan) const
99{
100 rowspan = m_span.GetRowspan();
101 colspan = m_span.GetColspan();
102}
103
104
105bool wxGBSizerItem::SetPos( const wxGBPosition& pos )
106{
107 if (m_gbsizer)
108 {
109 wxCHECK_MSG( !m_gbsizer->CheckForIntersection(pos, m_span, this), false,
110 wxT("An item is already at that position") );
111 }
112 m_pos = pos;
113 return true;
114}
115
116bool wxGBSizerItem::SetSpan( const wxGBSpan& span )
117{
118 if (m_gbsizer)
119 {
120 wxCHECK_MSG( !m_gbsizer->CheckForIntersection(m_pos, span, this), false,
121 wxT("An item is already at that position") );
122 }
123 m_span = span;
124 return true;
125}
126
127
128inline bool InRange(int val, int min, int max)
129{
130 return (val >= min && val <= max);
131}
132
133bool wxGBSizerItem::Intersects(const wxGBSizerItem& other)
134{
135 return Intersects(other.GetPos(), other.GetSpan());
136}
137
138bool wxGBSizerItem::Intersects(const wxGBPosition& pos, const wxGBSpan& span)
139{
140
141 int row, col, endrow, endcol;
142 int otherrow, othercol, otherendrow, otherendcol;
143
144 GetPos(row, col);
145 GetEndPos(endrow, endcol);
146
147 otherrow = pos.GetRow();
148 othercol = pos.GetCol();
149 otherendrow = otherrow + span.GetRowspan() - 1;
150 otherendcol = othercol + span.GetColspan() - 1;
151
152 // is the other item's start or end in the range of this one?
153 if (( InRange(otherrow, row, endrow) && InRange(othercol, col, endcol) ) ||
154 ( InRange(otherendrow, row, endrow) && InRange(otherendcol, col, endcol) ))
155 return true;
156
157 // is this item's start or end in the range of the other one?
158 if (( InRange(row, otherrow, otherendrow) && InRange(col, othercol, otherendcol) ) ||
159 ( InRange(endrow, otherrow, otherendrow) && InRange(endcol, othercol, otherendcol) ))
160 return true;
161
162 return false;
163}
164
165
166void wxGBSizerItem::GetEndPos(int& row, int& col)
167{
168 row = m_pos.GetRow() + m_span.GetRowspan() - 1;
169 col = m_pos.GetCol() + m_span.GetColspan() - 1;
170}
171
172
173//---------------------------------------------------------------------------
174// wxGridBagSizer
175//---------------------------------------------------------------------------
176
177wxGridBagSizer::wxGridBagSizer(int vgap, int hgap )
178 : wxFlexGridSizer(1, vgap, hgap),
179 m_emptyCellSize(10,20)
180
181{
182}
183
184
185wxSizerItem* wxGridBagSizer::Add( wxWindow *window,
186 const wxGBPosition& pos, const wxGBSpan& span,
187 int flag, int border, wxObject* userData )
188{
189 wxGBSizerItem* item = new wxGBSizerItem(window, pos, span, flag, border, userData);
190 if ( Add(item) )
191 return item;
192 else
193 {
194 delete item;
195 return (wxSizerItem*)NULL;
196 }
197}
198
199wxSizerItem* wxGridBagSizer::Add( wxSizer *sizer,
200 const wxGBPosition& pos, const wxGBSpan& span,
201 int flag, int border, wxObject* userData )
202{
203 wxGBSizerItem* item = new wxGBSizerItem(sizer, pos, span, flag, border, userData);
204 if ( Add(item) )
205 return item;
206 else
207 {
208 delete item;
209 return (wxSizerItem*)NULL;
210 }
211}
212
213wxSizerItem* wxGridBagSizer::Add( int width, int height,
214 const wxGBPosition& pos, const wxGBSpan& span,
215 int flag, int border, wxObject* userData )
216{
217 wxGBSizerItem* item = new wxGBSizerItem(width, height, pos, span, flag, border, userData);
218 if ( Add(item) )
219 return item;
220 else
221 {
222 delete item;
223 return (wxSizerItem*)NULL;
224 }
225}
226
227wxSizerItem* wxGridBagSizer::Add( wxGBSizerItem *item )
228{
229 wxCHECK_MSG( !CheckForIntersection(item), NULL,
230 wxT("An item is already at that position") );
231 m_children.Append(item);
232 item->SetGBSizer(this);
233 if ( item->GetWindow() )
234 item->GetWindow()->SetContainingSizer( this );
235
236 return item;
237}
238
239
240
241//---------------------------------------------------------------------------
242
243wxSize wxGridBagSizer::GetCellSize(int row, int col) const
244{
245 wxCHECK_MSG( (row < m_rows) && (col < m_cols),
246 wxDefaultSize,
247 wxT("Invalid cell."));
248 return wxSize( m_colWidths[col], m_rowHeights[row] );
249}
250
251
252wxGBPosition wxGridBagSizer::GetItemPosition(wxWindow *window)
253{
254 wxGBPosition badpos(-1,-1);
255 wxGBSizerItem* item = FindItem(window);
256 wxCHECK_MSG(item, badpos, wxT("Failed to find item."));
257 return item->GetPos();
258}
259
260
261wxGBPosition wxGridBagSizer::GetItemPosition(wxSizer *sizer)
262{
263 wxGBPosition badpos(-1,-1);
264 wxGBSizerItem* item = FindItem(sizer);
265 wxCHECK_MSG(item, badpos, wxT("Failed to find item."));
266 return item->GetPos();
267}
268
269
270wxGBPosition wxGridBagSizer::GetItemPosition(size_t index)
271{
272 wxGBPosition badpos(-1,-1);
273 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
274 wxCHECK_MSG( node, badpos, _T("Failed to find item.") );
275 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
276 return item->GetPos();
277}
278
279
280
281bool wxGridBagSizer::SetItemPosition(wxWindow *window, const wxGBPosition& pos)
282{
283 wxGBSizerItem* item = FindItem(window);
284 wxCHECK_MSG(item, false, wxT("Failed to find item."));
285 return item->SetPos(pos);
286}
287
288
289bool wxGridBagSizer::SetItemPosition(wxSizer *sizer, const wxGBPosition& pos)
290{
291 wxGBSizerItem* item = FindItem(sizer);
292 wxCHECK_MSG(item, false, wxT("Failed to find item."));
293 return item->SetPos(pos);
294}
295
296
297bool wxGridBagSizer::SetItemPosition(size_t index, const wxGBPosition& pos)
298{
299 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
300 wxCHECK_MSG( node, false, _T("Failed to find item.") );
301 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
302 return item->SetPos(pos);
303}
304
305
306
307wxGBSpan wxGridBagSizer::GetItemSpan(wxWindow *window)
308{
309 wxGBSpan badspan(-1,-1);
310 wxGBSizerItem* item = FindItem(window);
311 wxCHECK_MSG( item, badspan, _T("Failed to find item.") );
312 return item->GetSpan();
313}
314
315
316wxGBSpan wxGridBagSizer::GetItemSpan(wxSizer *sizer)
317{
318 wxGBSpan badspan(-1,-1);
319 wxGBSizerItem* item = FindItem(sizer);
320 wxCHECK_MSG( item, badspan, _T("Failed to find item.") );
321 return item->GetSpan();
322}
323
324
325wxGBSpan wxGridBagSizer::GetItemSpan(size_t index)
326{
327 wxGBSpan badspan(-1,-1);
328 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
329 wxCHECK_MSG( node, badspan, _T("Failed to find item.") );
330 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
331 return item->GetSpan();
332}
333
334
335
336bool wxGridBagSizer::SetItemSpan(wxWindow *window, const wxGBSpan& span)
337{
338 wxGBSizerItem* item = FindItem(window);
339 wxCHECK_MSG(item, false, wxT("Failed to find item."));
340 return item->SetSpan(span);
341}
342
343
344bool wxGridBagSizer::SetItemSpan(wxSizer *sizer, const wxGBSpan& span)
345{
346 wxGBSizerItem* item = FindItem(sizer);
347 wxCHECK_MSG(item, false, wxT("Failed to find item."));
348 return item->SetSpan(span);
349}
350
351
352bool wxGridBagSizer::SetItemSpan(size_t index, const wxGBSpan& span)
353{
354 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
355 wxCHECK_MSG( node, false, _T("Failed to find item.") );
356 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
357 return item->SetSpan(span);
358}
359
360
361
362
363wxGBSizerItem* wxGridBagSizer::FindItem(wxWindow* window)
364{
365 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
366 while (node)
367 {
368 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
369 if ( item->GetWindow() == window )
370 return item;
371 node = node->GetNext();
372 }
373 return NULL;
374}
375
376
377wxGBSizerItem* wxGridBagSizer::FindItem(wxSizer* sizer)
378{
379 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
380 while (node)
381 {
382 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
383 if ( item->GetSizer() == sizer )
384 return item;
385 node = node->GetNext();
386 }
387 return NULL;
388}
389
390
391
392
393wxGBSizerItem* wxGridBagSizer::FindItemAtPosition(const wxGBPosition& pos)
394{
395 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
396 while (node)
397 {
398 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
399 if ( item->Intersects(pos, wxDefaultSpan) )
400 return item;
401 node = node->GetNext();
402 }
403 return NULL;
404}
405
406
407
408
409wxGBSizerItem* wxGridBagSizer::FindItemAtPoint(const wxPoint& pt)
410{
411 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
412 while (node)
413 {
414 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
415 wxRect rect(item->GetPosition(), item->GetSize());
416 rect.Inflate(m_hgap, m_vgap);
417 if ( rect.Inside(pt) )
418 return item;
419 node = node->GetNext();
420 }
421 return NULL;
422}
423
424
425
426
427wxGBSizerItem* wxGridBagSizer::FindItemWithData(const wxObject* userData)
428{
429 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
430 while (node)
431 {
432 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
433 if ( item->GetUserData() == userData )
434 return item;
435 node = node->GetNext();
436 }
437 return NULL;
438}
439
440
441
442
443//---------------------------------------------------------------------------
444
445// Figure out what all the min row heights and col widths are, and calculate
446// min size from that.
447wxSize wxGridBagSizer::CalcMin()
448{
449 int idx;
450
451 if (m_children.GetCount() == 0)
452 return m_emptyCellSize;
453
454 m_rowHeights.Empty();
455 m_colWidths.Empty();
456
457 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
458 while (node)
459 {
460 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
461 if ( item->IsShown() )
462 {
463 int row, col, endrow, endcol;
464
465 item->GetPos(row, col);
466 item->GetEndPos(endrow, endcol);
467
468 // fill heights and widths upto this item if needed
469 while ( m_rowHeights.GetCount() <= (size_t)endrow )
470 m_rowHeights.Add(m_emptyCellSize.GetHeight());
471 while ( m_colWidths.GetCount() <= (size_t)endcol )
472 m_colWidths.Add(m_emptyCellSize.GetWidth());
473
474 // See if this item increases the size of its row(s) or col(s)
475 wxSize size(item->CalcMin());
476 for (idx=row; idx <= endrow; idx++)
477 m_rowHeights[idx] = wxMax(m_rowHeights[idx], size.GetHeight() / (endrow-row+1));
478 for (idx=col; idx <= endcol; idx++)
479 m_colWidths[idx] = wxMax(m_colWidths[idx], size.GetWidth() / (endcol-col+1));
480 }
481 node = node->GetNext();
482 }
483
484 AdjustForFlexDirection();
485
486 // Now traverse the heights and widths arrays calcing the totals, including gaps
487 int width = 0;
488 m_cols = m_colWidths.GetCount();
489 for (idx=0; idx < m_cols; idx++)
490 width += m_colWidths[idx] + ( idx == m_cols-1 ? 0 : m_hgap );
491
492 int height = 0;
493 m_rows = m_rowHeights.GetCount();
494 for (idx=0; idx < m_rows; idx++)
495 height += m_rowHeights[idx] + ( idx == m_rows-1 ? 0 : m_vgap );
496
497 m_calculatedMinSize = wxSize(width, height);
498 return m_calculatedMinSize;
499}
500
501
502
503void wxGridBagSizer::RecalcSizes()
504{
505 if (m_children.GetCount() == 0)
506 return;
507
508 wxPoint pt( GetPosition() );
509 wxSize sz( GetSize() );
510
511 m_rows = m_rowHeights.GetCount();
512 m_cols = m_colWidths.GetCount();
513 int idx, width, height;
514
515 AdjustForGrowables(sz, m_calculatedMinSize, m_rows, m_cols);
516
517 // Find the start positions on the window of the rows and columns
518 wxArrayInt rowpos;
519 rowpos.Add(0, m_rows);
520 int y = pt.y;
521 for (idx=0; idx < m_rows; idx++)
522 {
523 height = m_rowHeights[idx] + m_vgap;
524 rowpos[idx] = y;
525 y += height;
526 }
527
528 wxArrayInt colpos;
529 colpos.Add(0, m_cols);
530 int x = pt.x;
531 for (idx=0; idx < m_cols; idx++)
532 {
533 width = m_colWidths[idx] + m_hgap;
534 colpos[idx] = x;
535 x += width;
536 }
537
538
539 // Now iterate the children, setting each child's dimensions
540 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
541 while (node)
542 {
543 int row, col, endrow, endcol;
544 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
545 item->GetPos(row, col);
546 item->GetEndPos(endrow, endcol);
547
548 height = 0;
549 for(idx=row; idx <= endrow; idx++)
550 height += m_rowHeights[idx];
551 height += (endrow - row) * m_vgap; // add a vgap for every row spanned
552
553 width = 0;
554 for (idx=col; idx <= endcol; idx++)
555 width += m_colWidths[idx];
556 width += (endcol - col) * m_hgap; // add a hgap for every col spanned
557
558 SetItemBounds(item, colpos[col], rowpos[row], width, height);
559
560 node = node->GetNext();
561 }
562}
563
564
565
566//---------------------------------------------------------------------------
567
568bool wxGridBagSizer::CheckForIntersection(wxGBSizerItem* item, wxGBSizerItem* excludeItem)
569{
570 return CheckForIntersection(item->GetPos(), item->GetSpan(), excludeItem);
571}
572
573bool wxGridBagSizer::CheckForIntersection(const wxGBPosition& pos, const wxGBSpan& span, wxGBSizerItem* excludeItem)
574{
575 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
576 while (node)
577 {
578 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
579 node = node->GetNext();
580
581 if ( excludeItem && item == excludeItem )
582 continue;
583
584 if ( item->Intersects(pos, span) )
585 return true;
586
587 }
588 return false;
589}
590
591
592// Assumes a 10x10 grid, and returns the first empty cell found. This is
593// really stupid but it is only used by the Add methods that match the base
594// class virtuals, which should normally not be used anyway...
595wxGBPosition wxGridBagSizer::FindEmptyCell()
596{
597 int row, col;
598
599 for (row=0; row<10; row++)
600 for (col=0; col<10; col++)
601 {
602 wxGBPosition pos(row, col);
603 if ( !CheckForIntersection(pos, wxDefaultSpan) )
604 return pos;
605 }
606 return wxGBPosition(-1, -1);
607}
608
609
610//---------------------------------------------------------------------------
611
612// The Add base class virtuals should not be used with this class, but
613// we'll try to make them automatically select a location for the item
614// anyway.
615
616wxSizerItem* wxGridBagSizer::Add( wxWindow *window, int, int flag, int border, wxObject* userData )
617{
618 return Add(window, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
619}
620
621wxSizerItem* wxGridBagSizer::Add( wxSizer *sizer, int, int flag, int border, wxObject* userData )
622{
623 return Add(sizer, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
624}
625
626wxSizerItem* wxGridBagSizer::Add( int width, int height, int, int flag, int border, wxObject* userData )
627{
628 return Add(width, height, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
629}
630
631
632
633// The Insert nad Prepend base class virtuals that are not appropriate for
634// this class and should not be used. Their implementation in this class
635// simply fails.
636
637wxSizerItem* wxGridBagSizer::Add( wxSizerItem * )
638{
639 wxFAIL_MSG(wxT("Invalid Add form called."));
640 return (wxSizerItem*)NULL;
641}
642
643wxSizerItem* wxGridBagSizer::Prepend( wxWindow *, int, int, int, wxObject* )
644{
645 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
646 return (wxSizerItem*)NULL;
647}
648
649wxSizerItem* wxGridBagSizer::Prepend( wxSizer *, int, int, int, wxObject* )
650{
651 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
652 return (wxSizerItem*)NULL;
653}
654
655wxSizerItem* wxGridBagSizer::Prepend( int, int, int, int, int, wxObject* )
656{
657 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
658 return (wxSizerItem*)NULL;
659}
660
661wxSizerItem* wxGridBagSizer::Prepend( wxSizerItem * )
662{
663 wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer."));
664 return (wxSizerItem*)NULL;
665}
666
667
668wxSizerItem* wxGridBagSizer::Insert( size_t, wxWindow *, int, int, int, wxObject* )
669{
670 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
671 return (wxSizerItem*)NULL;
672}
673
674wxSizerItem* wxGridBagSizer::Insert( size_t, wxSizer *, int, int, int, wxObject* )
675{
676 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
677 return (wxSizerItem*)NULL;
678}
679
680wxSizerItem* wxGridBagSizer::Insert( size_t, int, int, int, int, int, wxObject* )
681{
682 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
683 return (wxSizerItem*)NULL;
684}
685
686wxSizerItem* wxGridBagSizer::Insert( size_t, wxSizerItem * )
687{
688 wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer."));
689 return (wxSizerItem*)NULL;
690}
691
692
693//---------------------------------------------------------------------------
694//---------------------------------------------------------------------------