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