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