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