]> git.saurik.com Git - wxWidgets.git/blame - src/common/gbsizer.cpp
A little narrower initial layout
[wxWidgets.git] / src / common / gbsizer.cpp
CommitLineData
20b35a69
RD
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
1b52f7ab 30IMPLEMENT_DYNAMIC_CLASS(wxGBSizerItem, wxSizerItem)
20b35a69
RD
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_sizer(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_sizer(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_sizer(NULL)
78{
79}
80
1b52f7ab
RD
81wxGBSizerItem::wxGBSizerItem()
82 : wxSizerItem(),
83 m_pos(-1,-1),
84 m_span(-1,-1),
85 m_sizer(NULL)
86{
87}
20b35a69
RD
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_sizer)
108 {
109 wxCHECK_MSG( !m_sizer->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_sizer)
119 {
120 wxCHECK_MSG( !m_sizer->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
185bool 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 true;
192 else
193 {
194 delete item;
195 return false;
196 }
197}
198
199bool 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 true;
206 else
207 {
208 delete item;
209 return false;
210 }
211}
212
213bool 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 true;
220 else
221 {
222 delete item;
223 return false;
224 }
225}
226
227bool wxGridBagSizer::Add( wxGBSizerItem *item )
228{
229 m_children.Append(item);
230 item->SetSizer(this);
231 if ( item->GetWindow() )
232 item->GetWindow()->SetContainingSizer( this );
233
234 return true;
235}
236
237
238
239//---------------------------------------------------------------------------
240
6217b9aa
RD
241wxSize wxGridBagSizer::GetCellSize(int row, int col) const
242{
243 wxCHECK_MSG( (row < m_rows) && (col < m_cols),
244 wxDefaultSize,
245 wxT("Invalid cell."));
246 return wxSize( m_colWidths[col], m_rowHeights[row] );
247}
248
249
20b35a69
RD
250wxGBPosition wxGridBagSizer::GetItemPosition(wxWindow *window)
251{
252 wxGBPosition badpos(-1,-1);
253 wxGBSizerItem* item = FindItem(window);
254 wxCHECK_MSG(item, badpos, wxT("Failed to find item."));
255 return item->GetPos();
256}
257
258
259wxGBPosition wxGridBagSizer::GetItemPosition(wxSizer *sizer)
260{
261 wxGBPosition badpos(-1,-1);
262 wxGBSizerItem* item = FindItem(sizer);
263 wxCHECK_MSG(item, badpos, wxT("Failed to find item."));
264 return item->GetPos();
265}
266
267
268wxGBPosition wxGridBagSizer::GetItemPosition(size_t index)
269{
270 wxGBPosition badpos(-1,-1);
271 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
272 wxCHECK_MSG( node, badpos, _T("Failed to find item.") );
273 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
274 return item->GetPos();
275}
276
277
278
279bool wxGridBagSizer::SetItemPosition(wxWindow *window, const wxGBPosition& pos)
280{
281 wxGBSizerItem* item = FindItem(window);
282 wxCHECK_MSG(item, false, wxT("Failed to find item."));
283 return item->SetPos(pos);
284}
285
286
287bool wxGridBagSizer::SetItemPosition(wxSizer *sizer, const wxGBPosition& pos)
288{
289 wxGBSizerItem* item = FindItem(sizer);
290 wxCHECK_MSG(item, false, wxT("Failed to find item."));
291 return item->SetPos(pos);
292}
293
294
295bool wxGridBagSizer::SetItemPosition(size_t index, const wxGBPosition& pos)
296{
297 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
298 wxCHECK_MSG( node, false, _T("Failed to find item.") );
299 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
300 return item->SetPos(pos);
301}
302
303
304
305wxGBSpan wxGridBagSizer::GetItemSpan(wxWindow *window)
306{
307 wxGBSpan badspan(-1,-1);
308 wxGBSizerItem* item = FindItem(window);
309 wxCHECK_MSG( item, badspan, _T("Failed to find item.") );
310 return item->GetSpan();
311}
312
313
314wxGBSpan wxGridBagSizer::GetItemSpan(wxSizer *sizer)
315{
316 wxGBSpan badspan(-1,-1);
317 wxGBSizerItem* item = FindItem(sizer);
318 wxCHECK_MSG( item, badspan, _T("Failed to find item.") );
319 return item->GetSpan();
320}
321
322
323wxGBSpan wxGridBagSizer::GetItemSpan(size_t index)
324{
325 wxGBSpan badspan(-1,-1);
326 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
327 wxCHECK_MSG( node, badspan, _T("Failed to find item.") );
328 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
329 return item->GetSpan();
330}
331
332
333
334bool wxGridBagSizer::SetItemSpan(wxWindow *window, const wxGBSpan& span)
335{
336 wxGBSizerItem* item = FindItem(window);
337 wxCHECK_MSG(item, false, wxT("Failed to find item."));
338 return item->SetSpan(span);
339}
340
341
342bool wxGridBagSizer::SetItemSpan(wxSizer *sizer, const wxGBSpan& span)
343{
344 wxGBSizerItem* item = FindItem(sizer);
345 wxCHECK_MSG(item, false, wxT("Failed to find item."));
346 return item->SetSpan(span);
347}
348
349
350bool wxGridBagSizer::SetItemSpan(size_t index, const wxGBSpan& span)
351{
352 wxSizerItemList::compatibility_iterator node = m_children.Item( index );
353 wxCHECK_MSG( node, false, _T("Failed to find item.") );
354 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
355 return item->SetSpan(span);
356}
357
358
359
360
361wxGBSizerItem* wxGridBagSizer::FindItem(wxWindow* window)
362{
363 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
364 while (node)
365 {
366 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
367 if ( item->GetWindow() == window )
368 return item;
369 node = node->GetNext();
370 }
371 return NULL;
372}
373
374
375wxGBSizerItem* wxGridBagSizer::FindItem(wxSizer* sizer)
376{
377 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
378 while (node)
379 {
380 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
381 if ( item->GetSizer() == sizer )
382 return item;
383 node = node->GetNext();
384 }
385 return NULL;
386}
387
388
389
390
391wxGBSizerItem* wxGridBagSizer::FindItemAtPosition(const wxGBPosition& pos)
392{
393 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
394 while (node)
395 {
396 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
397 if ( item->Intersects(pos, wxDefaultSpan) )
398 return item;
399 node = node->GetNext();
400 }
401 return NULL;
402}
403
404
405
406
407wxGBSizerItem* wxGridBagSizer::FindItemWithData(const wxObject* userData)
408{
409 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
410 while (node)
411 {
412 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
413 if ( item->GetUserData() == userData )
414 return item;
415 node = node->GetNext();
416 }
417 return NULL;
418}
419
420
421
422
423//---------------------------------------------------------------------------
424
425// Figure out what all the min row heights and col widths are, and calculate
426// min size from that.
427wxSize wxGridBagSizer::CalcMin()
428{
429 int idx;
430
431 if (m_children.GetCount() == 0)
432 return m_emptyCellSize;
433
434 m_rowHeights.Empty();
435 m_colWidths.Empty();
436
437 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
438 while (node)
439 {
440 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
441 if ( item->IsShown() )
442 {
443 int row, col, endrow, endcol;
444
445 item->GetPos(row, col);
446 item->GetEndPos(endrow, endcol);
447
448 // fill heights and widths upto this item if needed
449 while ( m_rowHeights.GetCount() <= (size_t)endrow )
450 m_rowHeights.Add(m_emptyCellSize.GetHeight());
451 while ( m_colWidths.GetCount() <= (size_t)endcol )
452 m_colWidths.Add(m_emptyCellSize.GetWidth());
453
454 // See if this item increases the size of its row(s) or col(s)
455 wxSize size(item->CalcMin());
456 for (idx=row; idx <= endrow; idx++)
457 m_rowHeights[idx] = wxMax(m_rowHeights[idx], size.GetHeight() / (endrow-row+1));
458 for (idx=col; idx <= endcol; idx++)
459 m_colWidths[idx] = wxMax(m_colWidths[idx], size.GetWidth() / (endcol-col+1));
460 }
461 node = node->GetNext();
462 }
463
464 AdjustForFlexDirection();
465
466 // Now traverse the heights and widths arrays calcing the totals, including gaps
467 int width = 0;
6217b9aa
RD
468 m_cols = m_colWidths.GetCount();
469 for (idx=0; idx < m_cols; idx++)
470 width += m_colWidths[idx] + ( idx == m_cols-1 ? 0 : m_hgap );
20b35a69
RD
471
472 int height = 0;
6217b9aa
RD
473 m_rows = m_rowHeights.GetCount();
474 for (idx=0; idx < m_rows; idx++)
475 height += m_rowHeights[idx] + ( idx == m_rows-1 ? 0 : m_vgap );
20b35a69
RD
476
477 return wxSize(width, height);
478}
479
480
481
482void wxGridBagSizer::RecalcSizes()
483{
484 if (m_children.GetCount() == 0)
485 return;
486
487 // Calculates minsize and populates m_rowHeights and m_colWidths
488 wxSize minsz( CalcMin() );
489
490 wxPoint pt( GetPosition() );
491 wxSize sz( GetSize() );
492
6217b9aa
RD
493 m_rows = m_rowHeights.GetCount();
494 m_cols = m_colWidths.GetCount();
20b35a69
RD
495 int idx, width, height;
496
6217b9aa 497 AdjustForGrowables(sz, minsz, m_rows, m_cols);
20b35a69
RD
498
499 // Find the start positions on the window of the rows and columns
500 wxArrayInt rowpos;
6217b9aa 501 rowpos.Add(0, m_rows);
20b35a69 502 int y = pt.y;
6217b9aa 503 for (idx=0; idx < m_rows; idx++)
20b35a69
RD
504 {
505 height = m_rowHeights[idx] + m_vgap;
506 rowpos[idx] = y;
507 y += height;
508 }
509
510 wxArrayInt colpos;
6217b9aa 511 colpos.Add(0, m_cols);
20b35a69 512 int x = pt.x;
6217b9aa 513 for (idx=0; idx < m_cols; idx++)
20b35a69
RD
514 {
515 width = m_colWidths[idx] + m_hgap;
516 colpos[idx] = x;
517 x += width;
518 }
519
520
521 // Now iterate the children, setting each child's dimensions
522 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
523 while (node)
524 {
525 int row, col, endrow, endcol;
526 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
527 item->GetPos(row, col);
528 item->GetEndPos(endrow, endcol);
529
530 height = 0;
531 for(idx=row; idx <= endrow; idx++)
532 height += m_rowHeights[idx] + m_vgap;
533
534 width = 0;
535 for (idx=col; idx <= endcol; idx++)
536 width += m_colWidths[idx] + m_hgap;
537
538 SetItemBounds(item, colpos[col], rowpos[row], width, height);
539
540 node = node->GetNext();
541 }
542}
543
544
545
546//---------------------------------------------------------------------------
547
548bool wxGridBagSizer::CheckForIntersection(wxGBSizerItem* item, wxGBSizerItem* excludeItem)
549{
550 return CheckForIntersection(item->GetPos(), item->GetSpan(), excludeItem);
551}
552
553bool wxGridBagSizer::CheckForIntersection(const wxGBPosition& pos, const wxGBSpan& span, wxGBSizerItem* excludeItem)
554{
555 wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
556 while (node)
557 {
558 wxGBSizerItem* item = (wxGBSizerItem*)node->GetData();
559 node = node->GetNext();
560
561 if ( excludeItem && item == excludeItem )
562 continue;
563
564 if ( item->Intersects(pos, span) )
565 return true;
566
567 }
568 return false;
569}
570
571
572// Assumes a 10x10 grid, and returns the first empty cell found. This is
573// really stupid but it is only used by the Add methods that match the base
574// class virtuals, which should normally not be used anyway...
575wxGBPosition wxGridBagSizer::FindEmptyCell()
576{
577 int row, col;
578
579 for (row=0; row<10; row++)
580 for (col=0; col<10; col++)
581 {
582 wxGBPosition pos(row, col);
583 if ( !CheckForIntersection(pos, wxDefaultSpan) )
584 return pos;
585 }
586 return wxGBPosition(-1, -1);
587}
588
589
590//---------------------------------------------------------------------------
591
592// The Add base class virtuals should not be used with this class, but
593// we'll try to make them automatically select a location for the item
594// anyway.
595
596void wxGridBagSizer::Add( wxWindow *window, int, int flag, int border, wxObject* userData )
597{
598 Add(window, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
599}
600
601void wxGridBagSizer::Add( wxSizer *sizer, int, int flag, int border, wxObject* userData )
602{
603 Add(sizer, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
604}
605
606void wxGridBagSizer::Add( int width, int height, int, int flag, int border, wxObject* userData )
607{
608 Add(width, height, FindEmptyCell(), wxDefaultSpan, flag, border, userData);
609}
610
611
612
613// The Insert nad Prepend base class virtuals that are not appropriate for
614// this class and should not be used. Their implementation in this class
615// simply fails.
616
d2eaa86b 617void wxGridBagSizer::Add( wxSizerItem * )
20b35a69
RD
618{ wxFAIL_MSG(wxT("Invalid Add form called.")); }
619
620void wxGridBagSizer::Prepend( wxWindow *, int, int, int, wxObject* )
621{ wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer.")); }
622
623void wxGridBagSizer::Prepend( wxSizer *, int, int, int, wxObject* )
624{ wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer.")); }
625
626void wxGridBagSizer::Prepend( int, int, int, int, int, wxObject* )
627{ wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer.")); }
628
629void wxGridBagSizer::Prepend( wxSizerItem * )
630{ wxFAIL_MSG(wxT("Prepend should not be used with wxGridBagSizer.")); }
631
632
633void wxGridBagSizer::Insert( size_t, wxWindow *, int, int, int, wxObject* )
634{ wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer.")); }
635
636void wxGridBagSizer::Insert( size_t, wxSizer *, int, int, int, wxObject* )
637{ wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer.")); }
638
639void wxGridBagSizer::Insert( size_t, int, int, int, int, int, wxObject* )
640{ wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer.")); }
641
642void wxGridBagSizer::Insert( size_t, wxSizerItem * )
643{ wxFAIL_MSG(wxT("Insert should not be used with wxGridBagSizer.")); }
644
645
646//---------------------------------------------------------------------------
647//---------------------------------------------------------------------------