]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/sizer.cpp | |
3 | // Purpose: provide new wxSizer class for layout | |
4 | // Author: Robert Roebling and Robin Dunn, contributions by | |
5 | // Dirk Holtwick, Ron Lee | |
6 | // Modified by: Ron Lee | |
7 | // Created: | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) Robin Dunn, Robert Roebling | |
10 | // Licence: wxWindows licence | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
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/display.h" | |
21 | #include "wx/sizer.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/string.h" | |
25 | #include "wx/intl.h" | |
26 | #include "wx/math.h" | |
27 | #include "wx/utils.h" | |
28 | #include "wx/settings.h" | |
29 | #include "wx/button.h" | |
30 | #include "wx/statbox.h" | |
31 | #include "wx/toplevel.h" | |
32 | #endif // WX_PRECOMP | |
33 | ||
34 | #include "wx/listimpl.cpp" | |
35 | ||
36 | ||
37 | //--------------------------------------------------------------------------- | |
38 | ||
39 | IMPLEMENT_CLASS(wxSizerItem, wxObject) | |
40 | IMPLEMENT_CLASS(wxSizer, wxObject) | |
41 | IMPLEMENT_CLASS(wxGridSizer, wxSizer) | |
42 | IMPLEMENT_CLASS(wxFlexGridSizer, wxGridSizer) | |
43 | IMPLEMENT_CLASS(wxBoxSizer, wxSizer) | |
44 | #if wxUSE_STATBOX | |
45 | IMPLEMENT_CLASS(wxStaticBoxSizer, wxBoxSizer) | |
46 | #endif | |
47 | #if wxUSE_BUTTON | |
48 | IMPLEMENT_CLASS(wxStdDialogButtonSizer, wxBoxSizer) | |
49 | #endif | |
50 | ||
51 | WX_DEFINE_EXPORTED_LIST( wxSizerItemList ) | |
52 | ||
53 | /* | |
54 | TODO PROPERTIES | |
55 | sizeritem | |
56 | object | |
57 | object_ref | |
58 | minsize | |
59 | option | |
60 | flag | |
61 | border | |
62 | spacer | |
63 | option | |
64 | flag | |
65 | borfder | |
66 | boxsizer | |
67 | orient | |
68 | staticboxsizer | |
69 | orient | |
70 | label | |
71 | gridsizer | |
72 | rows | |
73 | cols | |
74 | vgap | |
75 | hgap | |
76 | flexgridsizer | |
77 | rows | |
78 | cols | |
79 | vgap | |
80 | hgap | |
81 | growablerows | |
82 | growablecols | |
83 | minsize | |
84 | */ | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // wxSizerItem | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
90 | void wxSizerItem::Init(const wxSizerFlags& flags) | |
91 | { | |
92 | Init(); | |
93 | ||
94 | m_proportion = flags.GetProportion(); | |
95 | m_flag = flags.GetFlags(); | |
96 | m_border = flags.GetBorderInPixels(); | |
97 | } | |
98 | ||
99 | wxSizerItem::wxSizerItem() | |
100 | { | |
101 | Init(); | |
102 | ||
103 | m_proportion = 0; | |
104 | m_border = 0; | |
105 | m_flag = 0; | |
106 | m_id = wxID_NONE; | |
107 | } | |
108 | ||
109 | // window item | |
110 | void wxSizerItem::DoSetWindow(wxWindow *window) | |
111 | { | |
112 | wxCHECK_RET( window, _T("NULL window in wxSizerItem::SetWindow()") ); | |
113 | ||
114 | m_kind = Item_Window; | |
115 | m_window = window; | |
116 | ||
117 | // window doesn't become smaller than its initial size, whatever happens | |
118 | m_minSize = window->GetSize(); | |
119 | ||
120 | if ( m_flag & wxFIXED_MINSIZE ) | |
121 | window->SetMinSize(m_minSize); | |
122 | ||
123 | // aspect ratio calculated from initial size | |
124 | SetRatio(m_minSize); | |
125 | } | |
126 | ||
127 | wxSizerItem::wxSizerItem(wxWindow *window, | |
128 | int proportion, | |
129 | int flag, | |
130 | int border, | |
131 | wxObject* userData) | |
132 | : m_kind(Item_None), | |
133 | m_proportion(proportion), | |
134 | m_border(border), | |
135 | m_flag(flag), | |
136 | m_id(wxID_NONE), | |
137 | m_userData(userData) | |
138 | { | |
139 | DoSetWindow(window); | |
140 | } | |
141 | ||
142 | // sizer item | |
143 | void wxSizerItem::DoSetSizer(wxSizer *sizer) | |
144 | { | |
145 | m_kind = Item_Sizer; | |
146 | m_sizer = sizer; | |
147 | } | |
148 | ||
149 | wxSizerItem::wxSizerItem(wxSizer *sizer, | |
150 | int proportion, | |
151 | int flag, | |
152 | int border, | |
153 | wxObject* userData) | |
154 | : m_kind(Item_None), | |
155 | m_sizer(NULL), | |
156 | m_proportion(proportion), | |
157 | m_border(border), | |
158 | m_flag(flag), | |
159 | m_id(wxID_NONE), | |
160 | m_ratio(0.0), | |
161 | m_userData(userData) | |
162 | { | |
163 | DoSetSizer(sizer); | |
164 | ||
165 | // m_minSize is set later | |
166 | } | |
167 | ||
168 | // spacer item | |
169 | void wxSizerItem::DoSetSpacer(const wxSize& size) | |
170 | { | |
171 | m_kind = Item_Spacer; | |
172 | m_spacer = new wxSizerSpacer(size); | |
173 | m_minSize = size; | |
174 | SetRatio(size); | |
175 | } | |
176 | ||
177 | wxSizerItem::wxSizerItem(int width, | |
178 | int height, | |
179 | int proportion, | |
180 | int flag, | |
181 | int border, | |
182 | wxObject* userData) | |
183 | : m_kind(Item_None), | |
184 | m_sizer(NULL), | |
185 | m_minSize(width, height), // minimal size is the initial size | |
186 | m_proportion(proportion), | |
187 | m_border(border), | |
188 | m_flag(flag), | |
189 | m_id(wxID_NONE), | |
190 | m_userData(userData) | |
191 | { | |
192 | DoSetSpacer(wxSize(width, height)); | |
193 | } | |
194 | ||
195 | wxSizerItem::~wxSizerItem() | |
196 | { | |
197 | delete m_userData; | |
198 | Free(); | |
199 | } | |
200 | ||
201 | void wxSizerItem::Free() | |
202 | { | |
203 | switch ( m_kind ) | |
204 | { | |
205 | case Item_None: | |
206 | break; | |
207 | ||
208 | case Item_Window: | |
209 | m_window->SetContainingSizer(NULL); | |
210 | break; | |
211 | ||
212 | case Item_Sizer: | |
213 | delete m_sizer; | |
214 | break; | |
215 | ||
216 | case Item_Spacer: | |
217 | delete m_spacer; | |
218 | break; | |
219 | ||
220 | case Item_Max: | |
221 | default: | |
222 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
223 | } | |
224 | ||
225 | m_kind = Item_None; | |
226 | } | |
227 | ||
228 | wxSize wxSizerItem::GetSpacer() const | |
229 | { | |
230 | wxSize size; | |
231 | if ( m_kind == Item_Spacer ) | |
232 | size = m_spacer->GetSize(); | |
233 | ||
234 | return size; | |
235 | } | |
236 | ||
237 | ||
238 | wxSize wxSizerItem::GetSize() const | |
239 | { | |
240 | wxSize ret; | |
241 | switch ( m_kind ) | |
242 | { | |
243 | case Item_None: | |
244 | break; | |
245 | ||
246 | case Item_Window: | |
247 | ret = m_window->GetSize(); | |
248 | break; | |
249 | ||
250 | case Item_Sizer: | |
251 | ret = m_sizer->GetSize(); | |
252 | break; | |
253 | ||
254 | case Item_Spacer: | |
255 | ret = m_spacer->GetSize(); | |
256 | break; | |
257 | ||
258 | case Item_Max: | |
259 | default: | |
260 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
261 | } | |
262 | ||
263 | if (m_flag & wxWEST) | |
264 | ret.x += m_border; | |
265 | if (m_flag & wxEAST) | |
266 | ret.x += m_border; | |
267 | if (m_flag & wxNORTH) | |
268 | ret.y += m_border; | |
269 | if (m_flag & wxSOUTH) | |
270 | ret.y += m_border; | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | bool wxSizerItem::InformFirstDirection(int direction, int size, int availableOtherDir) | |
276 | { | |
277 | // The size that come here will be including borders. Child items should get it | |
278 | // without borders. | |
279 | if( size>0 ) | |
280 | { | |
281 | if( direction==wxHORIZONTAL ) | |
282 | { | |
283 | if (m_flag & wxWEST) | |
284 | size -= m_border; | |
285 | if (m_flag & wxEAST) | |
286 | size -= m_border; | |
287 | } | |
288 | else if( direction==wxVERTICAL ) | |
289 | { | |
290 | if (m_flag & wxNORTH) | |
291 | size -= m_border; | |
292 | if (m_flag & wxSOUTH) | |
293 | size -= m_border; | |
294 | } | |
295 | } | |
296 | ||
297 | bool didUse = false; | |
298 | // Pass the information along to the held object | |
299 | if (IsSizer()) | |
300 | { | |
301 | didUse = GetSizer()->InformFirstDirection(direction,size,availableOtherDir); | |
302 | if (didUse) | |
303 | m_minSize = GetSizer()->CalcMin(); | |
304 | } | |
305 | else if (IsWindow()) | |
306 | { | |
307 | didUse = GetWindow()->InformFirstDirection(direction,size,availableOtherDir); | |
308 | if (didUse) | |
309 | m_minSize = m_window->GetEffectiveMinSize(); | |
310 | ||
311 | // This information is useful for items with wxSHAPED flag, since | |
312 | // we can request an optimal min size for such an item. Even if | |
313 | // we overwrite the m_minSize member here, we can read it back from | |
314 | // the owned window (happens automatically). | |
315 | if( (m_flag & wxSHAPED) && (m_flag & wxEXPAND) && direction ) | |
316 | { | |
317 | if( !wxIsNullDouble(m_ratio) ) | |
318 | { | |
319 | wxCHECK_MSG( (m_proportion==0), false, _T("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") ); | |
320 | if( direction==wxHORIZONTAL && !wxIsNullDouble(m_ratio) ) | |
321 | { | |
322 | // Clip size so that we don't take too much | |
323 | if( availableOtherDir>=0 && int(size/m_ratio)-m_minSize.y>availableOtherDir ) | |
324 | size = int((availableOtherDir+m_minSize.y)*m_ratio); | |
325 | m_minSize = wxSize(size,int(size/m_ratio)); | |
326 | } | |
327 | else if( direction==wxVERTICAL ) | |
328 | { | |
329 | // Clip size so that we don't take too much | |
330 | if( availableOtherDir>=0 && int(size*m_ratio)-m_minSize.x>availableOtherDir ) | |
331 | size = int((availableOtherDir+m_minSize.x)/m_ratio); | |
332 | m_minSize = wxSize(int(size*m_ratio),size); | |
333 | } | |
334 | didUse = true; | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
339 | return didUse; | |
340 | } | |
341 | ||
342 | wxSize wxSizerItem::CalcMin() | |
343 | { | |
344 | if (IsSizer()) | |
345 | { | |
346 | m_minSize = m_sizer->GetMinSize(); | |
347 | ||
348 | // if we have to preserve aspect ratio _AND_ this is | |
349 | // the first-time calculation, consider ret to be initial size | |
350 | if ( (m_flag & wxSHAPED) && wxIsNullDouble(m_ratio) ) | |
351 | SetRatio(m_minSize); | |
352 | } | |
353 | else if ( IsWindow() ) | |
354 | { | |
355 | // Since the size of the window may change during runtime, we | |
356 | // should use the current minimal/best size. | |
357 | m_minSize = m_window->GetEffectiveMinSize(); | |
358 | } | |
359 | ||
360 | return GetMinSizeWithBorder(); | |
361 | } | |
362 | ||
363 | wxSize wxSizerItem::GetMinSizeWithBorder() const | |
364 | { | |
365 | wxSize ret = m_minSize; | |
366 | ||
367 | if (m_flag & wxWEST) | |
368 | ret.x += m_border; | |
369 | if (m_flag & wxEAST) | |
370 | ret.x += m_border; | |
371 | if (m_flag & wxNORTH) | |
372 | ret.y += m_border; | |
373 | if (m_flag & wxSOUTH) | |
374 | ret.y += m_border; | |
375 | ||
376 | return ret; | |
377 | } | |
378 | ||
379 | ||
380 | void wxSizerItem::SetDimension( const wxPoint& pos_, const wxSize& size_ ) | |
381 | { | |
382 | wxPoint pos = pos_; | |
383 | wxSize size = size_; | |
384 | if (m_flag & wxSHAPED) | |
385 | { | |
386 | // adjust aspect ratio | |
387 | int rwidth = (int) (size.y * m_ratio); | |
388 | if (rwidth > size.x) | |
389 | { | |
390 | // fit horizontally | |
391 | int rheight = (int) (size.x / m_ratio); | |
392 | // add vertical space | |
393 | if (m_flag & wxALIGN_CENTER_VERTICAL) | |
394 | pos.y += (size.y - rheight) / 2; | |
395 | else if (m_flag & wxALIGN_BOTTOM) | |
396 | pos.y += (size.y - rheight); | |
397 | // use reduced dimensions | |
398 | size.y =rheight; | |
399 | } | |
400 | else if (rwidth < size.x) | |
401 | { | |
402 | // add horizontal space | |
403 | if (m_flag & wxALIGN_CENTER_HORIZONTAL) | |
404 | pos.x += (size.x - rwidth) / 2; | |
405 | else if (m_flag & wxALIGN_RIGHT) | |
406 | pos.x += (size.x - rwidth); | |
407 | size.x = rwidth; | |
408 | } | |
409 | } | |
410 | ||
411 | // This is what GetPosition() returns. Since we calculate | |
412 | // borders afterwards, GetPosition() will be the left/top | |
413 | // corner of the surrounding border. | |
414 | m_pos = pos; | |
415 | ||
416 | if (m_flag & wxWEST) | |
417 | { | |
418 | pos.x += m_border; | |
419 | size.x -= m_border; | |
420 | } | |
421 | if (m_flag & wxEAST) | |
422 | { | |
423 | size.x -= m_border; | |
424 | } | |
425 | if (m_flag & wxNORTH) | |
426 | { | |
427 | pos.y += m_border; | |
428 | size.y -= m_border; | |
429 | } | |
430 | if (m_flag & wxSOUTH) | |
431 | { | |
432 | size.y -= m_border; | |
433 | } | |
434 | ||
435 | if (size.x < 0) | |
436 | size.x = 0; | |
437 | if (size.y < 0) | |
438 | size.y = 0; | |
439 | ||
440 | m_rect = wxRect(pos, size); | |
441 | ||
442 | switch ( m_kind ) | |
443 | { | |
444 | case Item_None: | |
445 | wxFAIL_MSG( _T("can't set size of uninitialized sizer item") ); | |
446 | break; | |
447 | ||
448 | case Item_Window: | |
449 | m_window->SetSize(pos.x, pos.y, size.x, size.y, | |
450 | wxSIZE_ALLOW_MINUS_ONE); | |
451 | break; | |
452 | ||
453 | case Item_Sizer: | |
454 | m_sizer->SetDimension(pos.x, pos.y, size.x, size.y); | |
455 | break; | |
456 | ||
457 | case Item_Spacer: | |
458 | m_spacer->SetSize(size); | |
459 | break; | |
460 | ||
461 | case Item_Max: | |
462 | default: | |
463 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
464 | } | |
465 | } | |
466 | ||
467 | void wxSizerItem::DeleteWindows() | |
468 | { | |
469 | switch ( m_kind ) | |
470 | { | |
471 | case Item_None: | |
472 | case Item_Spacer: | |
473 | break; | |
474 | ||
475 | case Item_Window: | |
476 | //We are deleting the window from this sizer - normally | |
477 | //the window destroys the sizer associated with it, | |
478 | //which might destroy this, which we don't want | |
479 | m_window->SetContainingSizer(NULL); | |
480 | m_window->Destroy(); | |
481 | //Putting this after the switch will result in a spacer | |
482 | //not being deleted properly on destruction | |
483 | m_kind = Item_None; | |
484 | break; | |
485 | ||
486 | case Item_Sizer: | |
487 | m_sizer->DeleteWindows(); | |
488 | break; | |
489 | ||
490 | case Item_Max: | |
491 | default: | |
492 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
493 | } | |
494 | ||
495 | } | |
496 | ||
497 | void wxSizerItem::Show( bool show ) | |
498 | { | |
499 | switch ( m_kind ) | |
500 | { | |
501 | case Item_None: | |
502 | wxFAIL_MSG( _T("can't show uninitialized sizer item") ); | |
503 | break; | |
504 | ||
505 | case Item_Window: | |
506 | m_window->Show(show); | |
507 | break; | |
508 | ||
509 | case Item_Sizer: | |
510 | m_sizer->Show(show); | |
511 | break; | |
512 | ||
513 | case Item_Spacer: | |
514 | m_spacer->Show(show); | |
515 | break; | |
516 | ||
517 | case Item_Max: | |
518 | default: | |
519 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
520 | } | |
521 | } | |
522 | ||
523 | bool wxSizerItem::IsShown() const | |
524 | { | |
525 | switch ( m_kind ) | |
526 | { | |
527 | case Item_None: | |
528 | // we may be called from CalcMin(), just return false so that we're | |
529 | // not used | |
530 | break; | |
531 | ||
532 | case Item_Window: | |
533 | return m_window->IsShown(); | |
534 | ||
535 | case Item_Sizer: | |
536 | // arbitrarily decide that if at least one of our elements is | |
537 | // shown, so are we (this arbitrariness is the reason for | |
538 | // deprecating this function) | |
539 | { | |
540 | // Some apps (such as dialog editors) depend on an empty sizer still | |
541 | // being laid out correctly and reporting the correct size and position. | |
542 | if (m_sizer->GetChildren().GetCount() == 0) | |
543 | return true; | |
544 | ||
545 | for ( wxSizerItemList::compatibility_iterator | |
546 | node = m_sizer->GetChildren().GetFirst(); | |
547 | node; | |
548 | node = node->GetNext() ) | |
549 | { | |
550 | if ( node->GetData()->IsShown() ) | |
551 | return true; | |
552 | } | |
553 | } | |
554 | return false; | |
555 | ||
556 | case Item_Spacer: | |
557 | return m_spacer->IsShown(); | |
558 | ||
559 | case Item_Max: | |
560 | default: | |
561 | wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") ); | |
562 | } | |
563 | ||
564 | return false; | |
565 | } | |
566 | ||
567 | #if WXWIN_COMPATIBILITY_2_6 | |
568 | void wxSizerItem::SetOption( int option ) | |
569 | { | |
570 | SetProportion( option ); | |
571 | } | |
572 | ||
573 | int wxSizerItem::GetOption() const | |
574 | { | |
575 | return GetProportion(); | |
576 | } | |
577 | #endif // WXWIN_COMPATIBILITY_2_6 | |
578 | ||
579 | ||
580 | //--------------------------------------------------------------------------- | |
581 | // wxSizer | |
582 | //--------------------------------------------------------------------------- | |
583 | ||
584 | wxSizer::~wxSizer() | |
585 | { | |
586 | WX_CLEAR_LIST(wxSizerItemList, m_children); | |
587 | } | |
588 | ||
589 | wxSizerItem* wxSizer::Insert( size_t index, wxSizerItem *item ) | |
590 | { | |
591 | m_children.Insert( index, item ); | |
592 | ||
593 | if ( item->GetWindow() ) | |
594 | item->GetWindow()->SetContainingSizer( this ); | |
595 | ||
596 | if ( item->GetSizer() ) | |
597 | item->GetSizer()->SetContainingWindow( m_containingWindow ); | |
598 | ||
599 | return item; | |
600 | } | |
601 | ||
602 | void wxSizer::SetContainingWindow(wxWindow *win) | |
603 | { | |
604 | if ( win == m_containingWindow ) | |
605 | return; | |
606 | ||
607 | m_containingWindow = win; | |
608 | ||
609 | // set the same window for all nested sizers as well, they also are in the | |
610 | // same window | |
611 | for ( wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
612 | node; | |
613 | node = node->GetNext() ) | |
614 | { | |
615 | wxSizerItem *const item = node->GetData(); | |
616 | wxSizer *const sizer = item->GetSizer(); | |
617 | ||
618 | if ( sizer ) | |
619 | { | |
620 | sizer->SetContainingWindow(win); | |
621 | } | |
622 | } | |
623 | } | |
624 | ||
625 | #if WXWIN_COMPATIBILITY_2_6 | |
626 | bool wxSizer::Remove( wxWindow *window ) | |
627 | { | |
628 | return Detach( window ); | |
629 | } | |
630 | #endif // WXWIN_COMPATIBILITY_2_6 | |
631 | ||
632 | bool wxSizer::Remove( wxSizer *sizer ) | |
633 | { | |
634 | wxASSERT_MSG( sizer, _T("Removing NULL sizer") ); | |
635 | ||
636 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
637 | while (node) | |
638 | { | |
639 | wxSizerItem *item = node->GetData(); | |
640 | ||
641 | if (item->GetSizer() == sizer) | |
642 | { | |
643 | delete item; | |
644 | m_children.Erase( node ); | |
645 | return true; | |
646 | } | |
647 | ||
648 | node = node->GetNext(); | |
649 | } | |
650 | ||
651 | return false; | |
652 | } | |
653 | ||
654 | bool wxSizer::Remove( int index ) | |
655 | { | |
656 | wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(), | |
657 | false, | |
658 | _T("Remove index is out of range") ); | |
659 | ||
660 | wxSizerItemList::compatibility_iterator node = m_children.Item( index ); | |
661 | ||
662 | wxCHECK_MSG( node, false, _T("Failed to find child node") ); | |
663 | ||
664 | delete node->GetData(); | |
665 | m_children.Erase( node ); | |
666 | ||
667 | return true; | |
668 | } | |
669 | ||
670 | bool wxSizer::Detach( wxSizer *sizer ) | |
671 | { | |
672 | wxASSERT_MSG( sizer, _T("Detaching NULL sizer") ); | |
673 | ||
674 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
675 | while (node) | |
676 | { | |
677 | wxSizerItem *item = node->GetData(); | |
678 | ||
679 | if (item->GetSizer() == sizer) | |
680 | { | |
681 | item->DetachSizer(); | |
682 | delete item; | |
683 | m_children.Erase( node ); | |
684 | return true; | |
685 | } | |
686 | node = node->GetNext(); | |
687 | } | |
688 | ||
689 | return false; | |
690 | } | |
691 | ||
692 | bool wxSizer::Detach( wxWindow *window ) | |
693 | { | |
694 | wxASSERT_MSG( window, _T("Detaching NULL window") ); | |
695 | ||
696 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
697 | while (node) | |
698 | { | |
699 | wxSizerItem *item = node->GetData(); | |
700 | ||
701 | if (item->GetWindow() == window) | |
702 | { | |
703 | delete item; | |
704 | m_children.Erase( node ); | |
705 | return true; | |
706 | } | |
707 | node = node->GetNext(); | |
708 | } | |
709 | ||
710 | return false; | |
711 | } | |
712 | ||
713 | bool wxSizer::Detach( int index ) | |
714 | { | |
715 | wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(), | |
716 | false, | |
717 | _T("Detach index is out of range") ); | |
718 | ||
719 | wxSizerItemList::compatibility_iterator node = m_children.Item( index ); | |
720 | ||
721 | wxCHECK_MSG( node, false, _T("Failed to find child node") ); | |
722 | ||
723 | wxSizerItem *item = node->GetData(); | |
724 | ||
725 | if ( item->IsSizer() ) | |
726 | item->DetachSizer(); | |
727 | ||
728 | delete item; | |
729 | m_children.Erase( node ); | |
730 | return true; | |
731 | } | |
732 | ||
733 | bool wxSizer::Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive ) | |
734 | { | |
735 | wxASSERT_MSG( oldwin, _T("Replacing NULL window") ); | |
736 | wxASSERT_MSG( newwin, _T("Replacing with NULL window") ); | |
737 | ||
738 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
739 | while (node) | |
740 | { | |
741 | wxSizerItem *item = node->GetData(); | |
742 | ||
743 | if (item->GetWindow() == oldwin) | |
744 | { | |
745 | item->AssignWindow(newwin); | |
746 | newwin->SetContainingSizer( this ); | |
747 | return true; | |
748 | } | |
749 | else if (recursive && item->IsSizer()) | |
750 | { | |
751 | if (item->GetSizer()->Replace( oldwin, newwin, true )) | |
752 | return true; | |
753 | } | |
754 | ||
755 | node = node->GetNext(); | |
756 | } | |
757 | ||
758 | return false; | |
759 | } | |
760 | ||
761 | bool wxSizer::Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive ) | |
762 | { | |
763 | wxASSERT_MSG( oldsz, _T("Replacing NULL sizer") ); | |
764 | wxASSERT_MSG( newsz, _T("Replacing with NULL sizer") ); | |
765 | ||
766 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
767 | while (node) | |
768 | { | |
769 | wxSizerItem *item = node->GetData(); | |
770 | ||
771 | if (item->GetSizer() == oldsz) | |
772 | { | |
773 | item->AssignSizer(newsz); | |
774 | return true; | |
775 | } | |
776 | else if (recursive && item->IsSizer()) | |
777 | { | |
778 | if (item->GetSizer()->Replace( oldsz, newsz, true )) | |
779 | return true; | |
780 | } | |
781 | ||
782 | node = node->GetNext(); | |
783 | } | |
784 | ||
785 | return false; | |
786 | } | |
787 | ||
788 | bool wxSizer::Replace( size_t old, wxSizerItem *newitem ) | |
789 | { | |
790 | wxCHECK_MSG( old < m_children.GetCount(), false, _T("Replace index is out of range") ); | |
791 | wxASSERT_MSG( newitem, _T("Replacing with NULL item") ); | |
792 | ||
793 | wxSizerItemList::compatibility_iterator node = m_children.Item( old ); | |
794 | ||
795 | wxCHECK_MSG( node, false, _T("Failed to find child node") ); | |
796 | ||
797 | wxSizerItem *item = node->GetData(); | |
798 | node->SetData(newitem); | |
799 | delete item; | |
800 | ||
801 | return true; | |
802 | } | |
803 | ||
804 | void wxSizer::Clear( bool delete_windows ) | |
805 | { | |
806 | // First clear the ContainingSizer pointers | |
807 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
808 | while (node) | |
809 | { | |
810 | wxSizerItem *item = node->GetData(); | |
811 | ||
812 | if (item->IsWindow()) | |
813 | item->GetWindow()->SetContainingSizer( NULL ); | |
814 | node = node->GetNext(); | |
815 | } | |
816 | ||
817 | // Destroy the windows if needed | |
818 | if (delete_windows) | |
819 | DeleteWindows(); | |
820 | ||
821 | // Now empty the list | |
822 | WX_CLEAR_LIST(wxSizerItemList, m_children); | |
823 | } | |
824 | ||
825 | void wxSizer::DeleteWindows() | |
826 | { | |
827 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
828 | while (node) | |
829 | { | |
830 | wxSizerItem *item = node->GetData(); | |
831 | ||
832 | item->DeleteWindows(); | |
833 | node = node->GetNext(); | |
834 | } | |
835 | } | |
836 | ||
837 | wxSize wxSizer::Fit( wxWindow *window ) | |
838 | { | |
839 | // take the min size by default and limit it by max size | |
840 | wxSize size = GetMinClientSize(window); | |
841 | wxSize sizeMax; | |
842 | ||
843 | wxTopLevelWindow *tlw = wxDynamicCast(window, wxTopLevelWindow); | |
844 | if ( tlw ) | |
845 | { | |
846 | // hack for small screen devices where TLWs are always full screen | |
847 | if ( tlw->IsAlwaysMaximized() ) | |
848 | { | |
849 | // do nothing | |
850 | return tlw->GetSize(); | |
851 | } | |
852 | ||
853 | // limit the window to the size of the display it is on | |
854 | int disp = wxDisplay::GetFromWindow(window); | |
855 | if ( disp == wxNOT_FOUND ) | |
856 | { | |
857 | // or, if we don't know which one it is, of the main one | |
858 | disp = 0; | |
859 | } | |
860 | ||
861 | sizeMax = wxDisplay(disp).GetClientArea().GetSize(); | |
862 | ||
863 | // space for decorations and toolbars etc. | |
864 | sizeMax = tlw->WindowToClientSize(sizeMax); | |
865 | } | |
866 | else | |
867 | { | |
868 | sizeMax = GetMaxClientSize(window); | |
869 | } | |
870 | ||
871 | if ( sizeMax.x != wxDefaultCoord && size.x > sizeMax.x ) | |
872 | size.x = sizeMax.x; | |
873 | if ( sizeMax.y != wxDefaultCoord && size.y > sizeMax.y ) | |
874 | size.y = sizeMax.y; | |
875 | ||
876 | // set client size | |
877 | window->SetClientSize( size ); | |
878 | ||
879 | // return entire size | |
880 | return window->GetSize(); | |
881 | } | |
882 | ||
883 | void wxSizer::FitInside( wxWindow *window ) | |
884 | { | |
885 | wxSize size; | |
886 | if (window->IsTopLevel()) | |
887 | size = VirtualFitSize( window ); | |
888 | else | |
889 | size = GetMinClientSize( window ); | |
890 | ||
891 | window->SetVirtualSize( size ); | |
892 | } | |
893 | ||
894 | void wxSizer::Layout() | |
895 | { | |
896 | // (re)calculates minimums needed for each item and other preparations | |
897 | // for layout | |
898 | CalcMin(); | |
899 | ||
900 | // Applies the layout and repositions/resizes the items | |
901 | RecalcSizes(); | |
902 | } | |
903 | ||
904 | void wxSizer::SetSizeHints( wxWindow *window ) | |
905 | { | |
906 | // Preserve the window's max size hints, but set the | |
907 | // lower bound according to the sizer calculations. | |
908 | ||
909 | wxSize size = Fit( window ); | |
910 | ||
911 | window->SetSizeHints( size.x, | |
912 | size.y, | |
913 | window->GetMaxWidth(), | |
914 | window->GetMaxHeight() ); | |
915 | } | |
916 | ||
917 | #if WXWIN_COMPATIBILITY_2_8 | |
918 | void wxSizer::SetVirtualSizeHints( wxWindow *window ) | |
919 | { | |
920 | FitInside( window ); | |
921 | } | |
922 | #endif // WXWIN_COMPATIBILITY_2_8 | |
923 | ||
924 | wxSize wxSizer::GetMaxWindowSize( wxWindow *window ) const | |
925 | { | |
926 | return window->GetMaxSize(); | |
927 | } | |
928 | ||
929 | wxSize wxSizer::GetMinWindowSize( wxWindow *window ) | |
930 | { | |
931 | return window->ClientToWindowSize(GetMinSize()); | |
932 | } | |
933 | ||
934 | // TODO on mac we need a function that determines how much free space this | |
935 | // min size contains, in order to make sure that we have 20 pixels of free | |
936 | // space around the controls | |
937 | wxSize wxSizer::GetMaxClientSize( wxWindow *window ) const | |
938 | { | |
939 | return window->WindowToClientSize(window->GetMaxSize()); | |
940 | } | |
941 | ||
942 | wxSize wxSizer::GetMinClientSize( wxWindow *WXUNUSED(window) ) | |
943 | { | |
944 | return GetMinSize(); // Already returns client size. | |
945 | } | |
946 | ||
947 | wxSize wxSizer::VirtualFitSize( wxWindow *window ) | |
948 | { | |
949 | wxSize size = GetMinClientSize( window ); | |
950 | wxSize sizeMax = GetMaxClientSize( window ); | |
951 | ||
952 | // Limit the size if sizeMax != wxDefaultSize | |
953 | ||
954 | if ( size.x > sizeMax.x && sizeMax.x != wxDefaultCoord ) | |
955 | size.x = sizeMax.x; | |
956 | if ( size.y > sizeMax.y && sizeMax.y != wxDefaultCoord ) | |
957 | size.y = sizeMax.y; | |
958 | ||
959 | return size; | |
960 | } | |
961 | ||
962 | void wxSizer::SetDimension( int x, int y, int width, int height ) | |
963 | { | |
964 | m_position.x = x; | |
965 | m_position.y = y; | |
966 | m_size.x = width; | |
967 | m_size.y = height; | |
968 | Layout(); | |
969 | } | |
970 | ||
971 | wxSize wxSizer::GetMinSize() | |
972 | { | |
973 | wxSize ret( CalcMin() ); | |
974 | if (ret.x < m_minSize.x) ret.x = m_minSize.x; | |
975 | if (ret.y < m_minSize.y) ret.y = m_minSize.y; | |
976 | return ret; | |
977 | } | |
978 | ||
979 | void wxSizer::DoSetMinSize( int width, int height ) | |
980 | { | |
981 | m_minSize.x = width; | |
982 | m_minSize.y = height; | |
983 | } | |
984 | ||
985 | bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height ) | |
986 | { | |
987 | wxASSERT_MSG( window, _T("SetMinSize for NULL window") ); | |
988 | ||
989 | // Is it our immediate child? | |
990 | ||
991 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
992 | while (node) | |
993 | { | |
994 | wxSizerItem *item = node->GetData(); | |
995 | ||
996 | if (item->GetWindow() == window) | |
997 | { | |
998 | item->SetMinSize( width, height ); | |
999 | return true; | |
1000 | } | |
1001 | node = node->GetNext(); | |
1002 | } | |
1003 | ||
1004 | // No? Search any subsizers we own then | |
1005 | ||
1006 | node = m_children.GetFirst(); | |
1007 | while (node) | |
1008 | { | |
1009 | wxSizerItem *item = node->GetData(); | |
1010 | ||
1011 | if ( item->GetSizer() && | |
1012 | item->GetSizer()->DoSetItemMinSize( window, width, height ) ) | |
1013 | { | |
1014 | // A child sizer found the requested windw, exit. | |
1015 | return true; | |
1016 | } | |
1017 | node = node->GetNext(); | |
1018 | } | |
1019 | ||
1020 | return false; | |
1021 | } | |
1022 | ||
1023 | bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) | |
1024 | { | |
1025 | wxASSERT_MSG( sizer, _T("SetMinSize for NULL sizer") ); | |
1026 | ||
1027 | // Is it our immediate child? | |
1028 | ||
1029 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1030 | while (node) | |
1031 | { | |
1032 | wxSizerItem *item = node->GetData(); | |
1033 | ||
1034 | if (item->GetSizer() == sizer) | |
1035 | { | |
1036 | item->GetSizer()->DoSetMinSize( width, height ); | |
1037 | return true; | |
1038 | } | |
1039 | node = node->GetNext(); | |
1040 | } | |
1041 | ||
1042 | // No? Search any subsizers we own then | |
1043 | ||
1044 | node = m_children.GetFirst(); | |
1045 | while (node) | |
1046 | { | |
1047 | wxSizerItem *item = node->GetData(); | |
1048 | ||
1049 | if ( item->GetSizer() && | |
1050 | item->GetSizer()->DoSetItemMinSize( sizer, width, height ) ) | |
1051 | { | |
1052 | // A child found the requested sizer, exit. | |
1053 | return true; | |
1054 | } | |
1055 | node = node->GetNext(); | |
1056 | } | |
1057 | ||
1058 | return false; | |
1059 | } | |
1060 | ||
1061 | bool wxSizer::DoSetItemMinSize( size_t index, int width, int height ) | |
1062 | { | |
1063 | wxSizerItemList::compatibility_iterator node = m_children.Item( index ); | |
1064 | ||
1065 | wxCHECK_MSG( node, false, _T("Failed to find child node") ); | |
1066 | ||
1067 | wxSizerItem *item = node->GetData(); | |
1068 | ||
1069 | if (item->GetSizer()) | |
1070 | { | |
1071 | // Sizers contains the minimal size in them, if not calculated ... | |
1072 | item->GetSizer()->DoSetMinSize( width, height ); | |
1073 | } | |
1074 | else | |
1075 | { | |
1076 | // ... but the minimal size of spacers and windows is stored via the item | |
1077 | item->SetMinSize( width, height ); | |
1078 | } | |
1079 | ||
1080 | return true; | |
1081 | } | |
1082 | ||
1083 | wxSizerItem* wxSizer::GetItem( wxWindow *window, bool recursive ) | |
1084 | { | |
1085 | wxASSERT_MSG( window, _T("GetItem for NULL window") ); | |
1086 | ||
1087 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1088 | while (node) | |
1089 | { | |
1090 | wxSizerItem *item = node->GetData(); | |
1091 | ||
1092 | if (item->GetWindow() == window) | |
1093 | { | |
1094 | return item; | |
1095 | } | |
1096 | else if (recursive && item->IsSizer()) | |
1097 | { | |
1098 | wxSizerItem *subitem = item->GetSizer()->GetItem( window, true ); | |
1099 | if (subitem) | |
1100 | return subitem; | |
1101 | } | |
1102 | ||
1103 | node = node->GetNext(); | |
1104 | } | |
1105 | ||
1106 | return NULL; | |
1107 | } | |
1108 | ||
1109 | wxSizerItem* wxSizer::GetItem( wxSizer *sizer, bool recursive ) | |
1110 | { | |
1111 | wxASSERT_MSG( sizer, _T("GetItem for NULL sizer") ); | |
1112 | ||
1113 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1114 | while (node) | |
1115 | { | |
1116 | wxSizerItem *item = node->GetData(); | |
1117 | ||
1118 | if (item->GetSizer() == sizer) | |
1119 | { | |
1120 | return item; | |
1121 | } | |
1122 | else if (recursive && item->IsSizer()) | |
1123 | { | |
1124 | wxSizerItem *subitem = item->GetSizer()->GetItem( sizer, true ); | |
1125 | if (subitem) | |
1126 | return subitem; | |
1127 | } | |
1128 | ||
1129 | node = node->GetNext(); | |
1130 | } | |
1131 | ||
1132 | return NULL; | |
1133 | } | |
1134 | ||
1135 | wxSizerItem* wxSizer::GetItem( size_t index ) | |
1136 | { | |
1137 | wxCHECK_MSG( index < m_children.GetCount(), | |
1138 | NULL, | |
1139 | _T("GetItem index is out of range") ); | |
1140 | ||
1141 | return m_children.Item( index )->GetData(); | |
1142 | } | |
1143 | ||
1144 | wxSizerItem* wxSizer::GetItemById( int id, bool recursive ) | |
1145 | { | |
1146 | // This gets a sizer item by the id of the sizer item | |
1147 | // and NOT the id of a window if the item is a window. | |
1148 | ||
1149 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1150 | while (node) | |
1151 | { | |
1152 | wxSizerItem *item = node->GetData(); | |
1153 | ||
1154 | if (item->GetId() == id) | |
1155 | { | |
1156 | return item; | |
1157 | } | |
1158 | else if (recursive && item->IsSizer()) | |
1159 | { | |
1160 | wxSizerItem *subitem = item->GetSizer()->GetItemById( id, true ); | |
1161 | if (subitem) | |
1162 | return subitem; | |
1163 | } | |
1164 | ||
1165 | node = node->GetNext(); | |
1166 | } | |
1167 | ||
1168 | return NULL; | |
1169 | } | |
1170 | ||
1171 | bool wxSizer::Show( wxWindow *window, bool show, bool recursive ) | |
1172 | { | |
1173 | wxSizerItem *item = GetItem( window, recursive ); | |
1174 | ||
1175 | if ( item ) | |
1176 | { | |
1177 | item->Show( show ); | |
1178 | return true; | |
1179 | } | |
1180 | ||
1181 | return false; | |
1182 | } | |
1183 | ||
1184 | bool wxSizer::Show( wxSizer *sizer, bool show, bool recursive ) | |
1185 | { | |
1186 | wxSizerItem *item = GetItem( sizer, recursive ); | |
1187 | ||
1188 | if ( item ) | |
1189 | { | |
1190 | item->Show( show ); | |
1191 | return true; | |
1192 | } | |
1193 | ||
1194 | return false; | |
1195 | } | |
1196 | ||
1197 | bool wxSizer::Show( size_t index, bool show) | |
1198 | { | |
1199 | wxSizerItem *item = GetItem( index ); | |
1200 | ||
1201 | if ( item ) | |
1202 | { | |
1203 | item->Show( show ); | |
1204 | return true; | |
1205 | } | |
1206 | ||
1207 | return false; | |
1208 | } | |
1209 | ||
1210 | void wxSizer::ShowItems( bool show ) | |
1211 | { | |
1212 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1213 | while (node) | |
1214 | { | |
1215 | node->GetData()->Show( show ); | |
1216 | node = node->GetNext(); | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | bool wxSizer::IsShown( wxWindow *window ) const | |
1221 | { | |
1222 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1223 | while (node) | |
1224 | { | |
1225 | wxSizerItem *item = node->GetData(); | |
1226 | ||
1227 | if (item->GetWindow() == window) | |
1228 | { | |
1229 | return item->IsShown(); | |
1230 | } | |
1231 | node = node->GetNext(); | |
1232 | } | |
1233 | ||
1234 | wxFAIL_MSG( _T("IsShown failed to find sizer item") ); | |
1235 | ||
1236 | return false; | |
1237 | } | |
1238 | ||
1239 | bool wxSizer::IsShown( wxSizer *sizer ) const | |
1240 | { | |
1241 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1242 | while (node) | |
1243 | { | |
1244 | wxSizerItem *item = node->GetData(); | |
1245 | ||
1246 | if (item->GetSizer() == sizer) | |
1247 | { | |
1248 | return item->IsShown(); | |
1249 | } | |
1250 | node = node->GetNext(); | |
1251 | } | |
1252 | ||
1253 | wxFAIL_MSG( _T("IsShown failed to find sizer item") ); | |
1254 | ||
1255 | return false; | |
1256 | } | |
1257 | ||
1258 | bool wxSizer::IsShown( size_t index ) const | |
1259 | { | |
1260 | wxCHECK_MSG( index < m_children.GetCount(), | |
1261 | false, | |
1262 | _T("IsShown index is out of range") ); | |
1263 | ||
1264 | return m_children.Item( index )->GetData()->IsShown(); | |
1265 | } | |
1266 | ||
1267 | ||
1268 | //--------------------------------------------------------------------------- | |
1269 | // wxGridSizer | |
1270 | //--------------------------------------------------------------------------- | |
1271 | ||
1272 | wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap ) | |
1273 | : m_rows( ( cols == 0 && rows == 0 ) ? 1 : rows ) | |
1274 | , m_cols( cols ) | |
1275 | , m_vgap( vgap ) | |
1276 | , m_hgap( hgap ) | |
1277 | { | |
1278 | } | |
1279 | ||
1280 | wxGridSizer::wxGridSizer( int cols, int vgap, int hgap ) | |
1281 | : m_rows( cols == 0 ? 1 : 0 ) | |
1282 | , m_cols( cols ) | |
1283 | , m_vgap( vgap ) | |
1284 | , m_hgap( hgap ) | |
1285 | { | |
1286 | } | |
1287 | ||
1288 | int wxGridSizer::CalcRowsCols(int& nrows, int& ncols) const | |
1289 | { | |
1290 | int nitems = m_children.GetCount(); | |
1291 | if ( nitems) | |
1292 | { | |
1293 | if ( m_cols ) | |
1294 | { | |
1295 | ncols = m_cols; | |
1296 | nrows = (nitems + m_cols - 1) / m_cols; | |
1297 | } | |
1298 | else if ( m_rows ) | |
1299 | { | |
1300 | ncols = (nitems + m_rows - 1) / m_rows; | |
1301 | nrows = m_rows; | |
1302 | } | |
1303 | else // 0 columns, 0 rows? | |
1304 | { | |
1305 | wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") ); | |
1306 | ||
1307 | nrows = ncols = 0; | |
1308 | } | |
1309 | } | |
1310 | ||
1311 | return nitems; | |
1312 | } | |
1313 | ||
1314 | void wxGridSizer::RecalcSizes() | |
1315 | { | |
1316 | int nitems, nrows, ncols; | |
1317 | if ( (nitems = CalcRowsCols(nrows, ncols)) == 0 ) | |
1318 | return; | |
1319 | ||
1320 | wxSize sz( GetSize() ); | |
1321 | wxPoint pt( GetPosition() ); | |
1322 | ||
1323 | int w = (sz.x - (ncols - 1) * m_hgap) / ncols; | |
1324 | int h = (sz.y - (nrows - 1) * m_vgap) / nrows; | |
1325 | ||
1326 | int x = pt.x; | |
1327 | for (int c = 0; c < ncols; c++) | |
1328 | { | |
1329 | int y = pt.y; | |
1330 | for (int r = 0; r < nrows; r++) | |
1331 | { | |
1332 | int i = r * ncols + c; | |
1333 | if (i < nitems) | |
1334 | { | |
1335 | wxSizerItemList::compatibility_iterator node = m_children.Item( i ); | |
1336 | ||
1337 | wxASSERT_MSG( node, _T("Failed to find SizerItemList node") ); | |
1338 | ||
1339 | SetItemBounds( node->GetData(), x, y, w, h); | |
1340 | } | |
1341 | y = y + h + m_vgap; | |
1342 | } | |
1343 | x = x + w + m_hgap; | |
1344 | } | |
1345 | } | |
1346 | ||
1347 | wxSize wxGridSizer::CalcMin() | |
1348 | { | |
1349 | int nrows, ncols; | |
1350 | if ( CalcRowsCols(nrows, ncols) == 0 ) | |
1351 | return wxSize(); | |
1352 | ||
1353 | // Find the max width and height for any component | |
1354 | int w = 0; | |
1355 | int h = 0; | |
1356 | ||
1357 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
1358 | while (node) | |
1359 | { | |
1360 | wxSizerItem *item = node->GetData(); | |
1361 | wxSize sz( item->CalcMin() ); | |
1362 | ||
1363 | w = wxMax( w, sz.x ); | |
1364 | h = wxMax( h, sz.y ); | |
1365 | ||
1366 | node = node->GetNext(); | |
1367 | } | |
1368 | ||
1369 | // In case we have a nested sizer with a two step algo , give it | |
1370 | // a chance to adjust to that (we give it width component) | |
1371 | node = m_children.GetFirst(); | |
1372 | bool didChangeMinSize = false; | |
1373 | while (node) | |
1374 | { | |
1375 | wxSizerItem *item = node->GetData(); | |
1376 | didChangeMinSize |= item->InformFirstDirection( wxHORIZONTAL, w, -1 ); | |
1377 | ||
1378 | node = node->GetNext(); | |
1379 | } | |
1380 | ||
1381 | // And redo iteration in case min size changed | |
1382 | if( didChangeMinSize ) | |
1383 | { | |
1384 | node = m_children.GetFirst(); | |
1385 | w = h = 0; | |
1386 | while (node) | |
1387 | { | |
1388 | wxSizerItem *item = node->GetData(); | |
1389 | wxSize sz( item->GetMinSizeWithBorder() ); | |
1390 | ||
1391 | w = wxMax( w, sz.x ); | |
1392 | h = wxMax( h, sz.y ); | |
1393 | ||
1394 | node = node->GetNext(); | |
1395 | } | |
1396 | } | |
1397 | ||
1398 | return wxSize( ncols * w + (ncols-1) * m_hgap, | |
1399 | nrows * h + (nrows-1) * m_vgap ); | |
1400 | } | |
1401 | ||
1402 | void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h ) | |
1403 | { | |
1404 | wxPoint pt( x,y ); | |
1405 | wxSize sz( item->GetMinSizeWithBorder() ); | |
1406 | int flag = item->GetFlag(); | |
1407 | ||
1408 | if ((flag & wxEXPAND) || (flag & wxSHAPED)) | |
1409 | { | |
1410 | sz = wxSize(w, h); | |
1411 | } | |
1412 | else | |
1413 | { | |
1414 | if (flag & wxALIGN_CENTER_HORIZONTAL) | |
1415 | { | |
1416 | pt.x = x + (w - sz.x) / 2; | |
1417 | } | |
1418 | else if (flag & wxALIGN_RIGHT) | |
1419 | { | |
1420 | pt.x = x + (w - sz.x); | |
1421 | } | |
1422 | ||
1423 | if (flag & wxALIGN_CENTER_VERTICAL) | |
1424 | { | |
1425 | pt.y = y + (h - sz.y) / 2; | |
1426 | } | |
1427 | else if (flag & wxALIGN_BOTTOM) | |
1428 | { | |
1429 | pt.y = y + (h - sz.y); | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | item->SetDimension(pt, sz); | |
1434 | } | |
1435 | ||
1436 | //--------------------------------------------------------------------------- | |
1437 | // wxFlexGridSizer | |
1438 | //--------------------------------------------------------------------------- | |
1439 | ||
1440 | wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap ) | |
1441 | : wxGridSizer( rows, cols, vgap, hgap ), | |
1442 | m_flexDirection(wxBOTH), | |
1443 | m_growMode(wxFLEX_GROWMODE_SPECIFIED) | |
1444 | { | |
1445 | } | |
1446 | ||
1447 | wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap ) | |
1448 | : wxGridSizer( cols, vgap, hgap ), | |
1449 | m_flexDirection(wxBOTH), | |
1450 | m_growMode(wxFLEX_GROWMODE_SPECIFIED) | |
1451 | { | |
1452 | } | |
1453 | ||
1454 | wxFlexGridSizer::~wxFlexGridSizer() | |
1455 | { | |
1456 | } | |
1457 | ||
1458 | void wxFlexGridSizer::RecalcSizes() | |
1459 | { | |
1460 | int nrows, ncols; | |
1461 | if ( !CalcRowsCols(nrows, ncols) ) | |
1462 | return; | |
1463 | ||
1464 | const wxPoint pt(GetPosition()); | |
1465 | const wxSize sz(GetSize()); | |
1466 | ||
1467 | AdjustForGrowables(sz); | |
1468 | ||
1469 | wxSizerItemList::const_iterator i = m_children.begin(); | |
1470 | const wxSizerItemList::const_iterator end = m_children.end(); | |
1471 | ||
1472 | int y = 0; | |
1473 | for ( int r = 0; r < nrows; r++ ) | |
1474 | { | |
1475 | if ( m_rowHeights[r] == -1 ) | |
1476 | { | |
1477 | // this row is entirely hidden, skip it | |
1478 | for ( int c = 0; c < ncols; c++ ) | |
1479 | { | |
1480 | if ( i == end ) | |
1481 | return; | |
1482 | ||
1483 | ++i; | |
1484 | } | |
1485 | ||
1486 | continue; | |
1487 | } | |
1488 | ||
1489 | const int hrow = m_rowHeights[r]; | |
1490 | int h = sz.y - y; // max remaining height, don't overflow it | |
1491 | if ( hrow < h ) | |
1492 | h = hrow; | |
1493 | ||
1494 | int x = 0; | |
1495 | for ( int c = 0; c < ncols && i != end; c++, ++i ) | |
1496 | { | |
1497 | const int wcol = m_colWidths[c]; | |
1498 | ||
1499 | if ( wcol == -1 ) | |
1500 | continue; | |
1501 | ||
1502 | int w = sz.x - x; // max possible value, ensure we don't overflow | |
1503 | if ( wcol < w ) | |
1504 | w = wcol; | |
1505 | ||
1506 | SetItemBounds(*i, pt.x + x, pt.y + y, w, h); | |
1507 | ||
1508 | x += wcol + m_hgap; | |
1509 | } | |
1510 | ||
1511 | if ( i == end ) | |
1512 | return; | |
1513 | ||
1514 | y += hrow + m_vgap; | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | // helper function used in CalcMin() to sum up the sizes of non-hidden items | |
1519 | static int SumArraySizes(const wxArrayInt& sizes, int gap) | |
1520 | { | |
1521 | // Sum total minimum size, including gaps between rows/columns. | |
1522 | // -1 is used as a magic number meaning empty row/column. | |
1523 | int total = 0; | |
1524 | ||
1525 | const size_t count = sizes.size(); | |
1526 | for ( size_t n = 0; n < count; n++ ) | |
1527 | { | |
1528 | if ( sizes[n] != -1 ) | |
1529 | { | |
1530 | if ( total ) | |
1531 | total += gap; // separate from the previous column | |
1532 | ||
1533 | total += sizes[n]; | |
1534 | } | |
1535 | } | |
1536 | ||
1537 | return total; | |
1538 | } | |
1539 | ||
1540 | void wxFlexGridSizer::FindWidthsAndHeights(int nrows, int ncols) | |
1541 | { | |
1542 | // We have to recalculate the sizes in case the item minimum size has | |
1543 | // changed since the previous layout, or the item has been hidden using | |
1544 | // wxSizer::Show(). If all the items in a row/column are hidden, the final | |
1545 | // dimension of the row/column will be -1, indicating that the column | |
1546 | // itself is hidden. | |
1547 | m_rowHeights.assign(nrows, -1); | |
1548 | m_colWidths.assign(ncols, -1); | |
1549 | ||
1550 | // n is the index of the item in left-to-right top-to-bottom order | |
1551 | size_t n = 0; | |
1552 | for ( wxSizerItemList::iterator i = m_children.begin(); | |
1553 | i != m_children.end(); | |
1554 | ++i, ++n ) | |
1555 | { | |
1556 | wxSizerItem * const item = *i; | |
1557 | if ( item->IsShown() ) | |
1558 | { | |
1559 | // NOTE: Not doing the calculation here, this is just | |
1560 | // for finding max values. | |
1561 | const wxSize sz(item->GetMinSizeWithBorder()); | |
1562 | ||
1563 | const int row = n / ncols; | |
1564 | const int col = n % ncols; | |
1565 | ||
1566 | if ( sz.y > m_rowHeights[row] ) | |
1567 | m_rowHeights[row] = sz.y; | |
1568 | if ( sz.x > m_colWidths[col] ) | |
1569 | m_colWidths[col] = sz.x; | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | AdjustForFlexDirection(); | |
1574 | ||
1575 | m_calculatedMinSize = wxSize(SumArraySizes(m_colWidths, m_hgap), | |
1576 | SumArraySizes(m_rowHeights, m_vgap)); | |
1577 | } | |
1578 | ||
1579 | wxSize wxFlexGridSizer::CalcMin() | |
1580 | { | |
1581 | int nrows, | |
1582 | ncols; | |
1583 | ||
1584 | // Number of rows/columns can change as items are added or removed. | |
1585 | if ( !CalcRowsCols(nrows, ncols) ) | |
1586 | return wxSize(); | |
1587 | ||
1588 | ||
1589 | // We have to recalculate the sizes in case the item minimum size has | |
1590 | // changed since the previous layout, or the item has been hidden using | |
1591 | // wxSizer::Show(). If all the items in a row/column are hidden, the final | |
1592 | // dimension of the row/column will be -1, indicating that the column | |
1593 | // itself is hidden. | |
1594 | m_rowHeights.assign(nrows, -1); | |
1595 | m_colWidths.assign(ncols, -1); | |
1596 | ||
1597 | // n is the index of the item in left-to-right top-to-bottom order | |
1598 | size_t n = 0; | |
1599 | for ( wxSizerItemList::iterator i = m_children.begin(); | |
1600 | i != m_children.end(); | |
1601 | ++i, ++n ) | |
1602 | { | |
1603 | wxSizerItem * const item = *i; | |
1604 | if ( item->IsShown() ) | |
1605 | { | |
1606 | item->CalcMin(); | |
1607 | } | |
1608 | } | |
1609 | ||
1610 | // The stage of looking for max values in each row/column has been | |
1611 | // made a separate function, since it's reused in AdjustForGrowables. | |
1612 | FindWidthsAndHeights(nrows,ncols); | |
1613 | ||
1614 | return m_calculatedMinSize; | |
1615 | } | |
1616 | ||
1617 | void wxFlexGridSizer::AdjustForFlexDirection() | |
1618 | { | |
1619 | // the logic in CalcMin works when we resize flexibly in both directions | |
1620 | // but maybe this is not the case | |
1621 | if ( m_flexDirection != wxBOTH ) | |
1622 | { | |
1623 | // select the array corresponding to the direction in which we do *not* | |
1624 | // resize flexibly | |
1625 | wxArrayInt& array = m_flexDirection == wxVERTICAL ? m_colWidths | |
1626 | : m_rowHeights; | |
1627 | ||
1628 | const size_t count = array.GetCount(); | |
1629 | ||
1630 | // find the largest value in this array | |
1631 | size_t n; | |
1632 | int largest = 0; | |
1633 | ||
1634 | for ( n = 0; n < count; ++n ) | |
1635 | { | |
1636 | if ( array[n] > largest ) | |
1637 | largest = array[n]; | |
1638 | } | |
1639 | ||
1640 | // and now fill it with the largest value | |
1641 | for ( n = 0; n < count; ++n ) | |
1642 | { | |
1643 | // don't touch hidden rows | |
1644 | if ( array[n] != -1 ) | |
1645 | array[n] = largest; | |
1646 | } | |
1647 | } | |
1648 | } | |
1649 | ||
1650 | // helper of AdjustForGrowables() which is called for rows/columns separately | |
1651 | // | |
1652 | // parameters: | |
1653 | // delta: the extra space, we do nothing unless it's positive | |
1654 | // growable: indices or growable rows/cols in sizes array | |
1655 | // sizes: the height/widths of rows/cols to adjust | |
1656 | // proportions: proportions of the growable rows/cols or NULL if they all | |
1657 | // should be assumed to have proportion of 1 | |
1658 | static void | |
1659 | DoAdjustForGrowables(int delta, | |
1660 | const wxArrayInt& growable, | |
1661 | wxArrayInt& sizes, | |
1662 | const wxArrayInt *proportions) | |
1663 | { | |
1664 | if ( delta <= 0 ) | |
1665 | return; | |
1666 | ||
1667 | // total sum of proportions of all non-hidden rows | |
1668 | int sum_proportions = 0; | |
1669 | ||
1670 | // number of currently shown growable rows | |
1671 | int num = 0; | |
1672 | ||
1673 | const int max_idx = sizes.size(); | |
1674 | ||
1675 | const size_t count = growable.size(); | |
1676 | size_t idx; | |
1677 | for ( idx = 0; idx < count; idx++ ) | |
1678 | { | |
1679 | // Since the number of rows/columns can change as items are | |
1680 | // inserted/deleted, we need to verify at runtime that the | |
1681 | // requested growable rows/columns are still valid. | |
1682 | if ( growable[idx] >= max_idx ) | |
1683 | continue; | |
1684 | ||
1685 | // If all items in a row/column are hidden, that row/column will | |
1686 | // have a dimension of -1. This causes the row/column to be | |
1687 | // hidden completely. | |
1688 | if ( sizes[growable[idx]] == -1 ) | |
1689 | continue; | |
1690 | ||
1691 | if ( proportions ) | |
1692 | sum_proportions += (*proportions)[idx]; | |
1693 | ||
1694 | num++; | |
1695 | } | |
1696 | ||
1697 | if ( !num ) | |
1698 | return; | |
1699 | ||
1700 | // the remaining extra free space, adjusted during each iteration | |
1701 | for ( idx = 0; idx < count; idx++ ) | |
1702 | { | |
1703 | if ( growable[idx] >= max_idx ) | |
1704 | continue; | |
1705 | ||
1706 | if ( sizes[ growable[idx] ] == -1 ) | |
1707 | continue; | |
1708 | ||
1709 | int cur_delta; | |
1710 | if ( sum_proportions == 0 ) | |
1711 | { | |
1712 | // no growable rows -- divide extra space evenly among all | |
1713 | cur_delta = delta/num; | |
1714 | num--; | |
1715 | } | |
1716 | else // allocate extra space proportionally | |
1717 | { | |
1718 | const int cur_prop = (*proportions)[idx]; | |
1719 | cur_delta = (delta*cur_prop)/sum_proportions; | |
1720 | sum_proportions -= cur_prop; | |
1721 | } | |
1722 | ||
1723 | sizes[growable[idx]] += cur_delta; | |
1724 | delta -= cur_delta; | |
1725 | } | |
1726 | } | |
1727 | ||
1728 | void wxFlexGridSizer::AdjustForGrowables(const wxSize& sz) | |
1729 | { | |
1730 | if ( (m_flexDirection & wxHORIZONTAL) || (m_growMode != wxFLEX_GROWMODE_NONE) ) | |
1731 | { | |
1732 | DoAdjustForGrowables | |
1733 | ( | |
1734 | sz.x - m_calculatedMinSize.x, | |
1735 | m_growableCols, | |
1736 | m_colWidths, | |
1737 | m_growMode == wxFLEX_GROWMODE_SPECIFIED ? &m_growableColsProportions | |
1738 | : NULL | |
1739 | ); | |
1740 | ||
1741 | // This gives nested objects that benefit from knowing one size | |
1742 | // component in advance the chance to use that. | |
1743 | bool didAdjustMinSize = false; | |
1744 | int nrows, ncols; | |
1745 | CalcRowsCols(nrows, ncols); | |
1746 | ||
1747 | // Iterate over all items and inform about column width | |
1748 | size_t n = 0; | |
1749 | for ( wxSizerItemList::iterator i = m_children.begin(); | |
1750 | i != m_children.end(); | |
1751 | ++i, ++n ) | |
1752 | { | |
1753 | const int col = n % ncols; | |
1754 | didAdjustMinSize |= (*i)->InformFirstDirection(wxHORIZONTAL, m_colWidths[col], sz.y - m_calculatedMinSize.y); | |
1755 | } | |
1756 | ||
1757 | // Only redo if info was actually used | |
1758 | if( didAdjustMinSize ) | |
1759 | { | |
1760 | DoAdjustForGrowables | |
1761 | ( | |
1762 | sz.x - m_calculatedMinSize.x, | |
1763 | m_growableCols, | |
1764 | m_colWidths, | |
1765 | m_growMode == wxFLEX_GROWMODE_SPECIFIED ? &m_growableColsProportions | |
1766 | : NULL | |
1767 | ); | |
1768 | } | |
1769 | } | |
1770 | ||
1771 | if ( (m_flexDirection & wxVERTICAL) || (m_growMode != wxFLEX_GROWMODE_NONE) ) | |
1772 | { | |
1773 | // pass NULL instead of proportions if the grow mode is ALL as we | |
1774 | // should treat all rows as having proportion of 1 then | |
1775 | DoAdjustForGrowables | |
1776 | ( | |
1777 | sz.y - m_calculatedMinSize.y, | |
1778 | m_growableRows, | |
1779 | m_rowHeights, | |
1780 | m_growMode == wxFLEX_GROWMODE_SPECIFIED ? &m_growableRowsProportions | |
1781 | : NULL | |
1782 | ); | |
1783 | } | |
1784 | } | |
1785 | ||
1786 | ||
1787 | void wxFlexGridSizer::AddGrowableRow( size_t idx, int proportion ) | |
1788 | { | |
1789 | m_growableRows.Add( idx ); | |
1790 | m_growableRowsProportions.Add( proportion ); | |
1791 | } | |
1792 | ||
1793 | void wxFlexGridSizer::AddGrowableCol( size_t idx, int proportion ) | |
1794 | { | |
1795 | m_growableCols.Add( idx ); | |
1796 | m_growableColsProportions.Add( proportion ); | |
1797 | } | |
1798 | ||
1799 | // helper function for RemoveGrowableCol/Row() | |
1800 | static void | |
1801 | DoRemoveFromArrays(size_t idx, wxArrayInt& items, wxArrayInt& proportions) | |
1802 | { | |
1803 | const size_t count = items.size(); | |
1804 | for ( size_t n = 0; n < count; n++ ) | |
1805 | { | |
1806 | if ( (size_t)items[n] == idx ) | |
1807 | { | |
1808 | items.RemoveAt(n); | |
1809 | proportions.RemoveAt(n); | |
1810 | return; | |
1811 | } | |
1812 | } | |
1813 | ||
1814 | wxFAIL_MSG( _T("column/row is already not growable") ); | |
1815 | } | |
1816 | ||
1817 | void wxFlexGridSizer::RemoveGrowableCol( size_t idx ) | |
1818 | { | |
1819 | DoRemoveFromArrays(idx, m_growableCols, m_growableColsProportions); | |
1820 | } | |
1821 | ||
1822 | void wxFlexGridSizer::RemoveGrowableRow( size_t idx ) | |
1823 | { | |
1824 | DoRemoveFromArrays(idx, m_growableRows, m_growableRowsProportions); | |
1825 | } | |
1826 | ||
1827 | //--------------------------------------------------------------------------- | |
1828 | // wxBoxSizer | |
1829 | //--------------------------------------------------------------------------- | |
1830 | ||
1831 | void wxBoxSizer::RecalcSizes() | |
1832 | { | |
1833 | if ( m_children.empty() ) | |
1834 | return; | |
1835 | ||
1836 | const wxCoord totalMinorSize = GetSizeInMinorDir(m_size); | |
1837 | ||
1838 | // the amount of free space which we should redistribute among the | |
1839 | // stretchable items (i.e. those with non zero proportion) | |
1840 | int delta = GetSizeInMajorDir(m_size) - GetSizeInMajorDir(m_minSize); | |
1841 | ||
1842 | ||
1843 | // Inform child items about the size in minor direction, that can | |
1844 | // change how much free space we have in major dir and how to distribute it. | |
1845 | int majorMinSum = 0; | |
1846 | wxSizerItemList::const_iterator i ; | |
1847 | for ( i = m_children.begin(); | |
1848 | i != m_children.end(); | |
1849 | ++i ) | |
1850 | { | |
1851 | wxSizerItem * const item = *i; | |
1852 | ||
1853 | if ( !item->IsShown() ) | |
1854 | continue; | |
1855 | ||
1856 | wxSize szMinPrev = item->GetMinSizeWithBorder(); | |
1857 | item->InformFirstDirection(m_orient^wxBOTH,totalMinorSize,delta); | |
1858 | wxSize szMin = item->GetMinSizeWithBorder(); | |
1859 | int deltaChange = GetSizeInMajorDir(szMin-szMinPrev); | |
1860 | if( deltaChange ) | |
1861 | { | |
1862 | // Since we passed available space along to the item, it should not | |
1863 | // take too much, so delta should not become negative. | |
1864 | delta -= deltaChange; | |
1865 | } | |
1866 | majorMinSum += GetSizeInMajorDir(item->GetMinSizeWithBorder()); | |
1867 | } | |
1868 | // And update our min size | |
1869 | SizeInMajorDir(m_minSize) = majorMinSum; | |
1870 | ||
1871 | ||
1872 | // might have a new delta now | |
1873 | delta = GetSizeInMajorDir(m_size) - GetSizeInMajorDir(m_minSize); | |
1874 | ||
1875 | // the position at which we put the next child | |
1876 | wxPoint pt(m_position); | |
1877 | ||
1878 | int totalProportion = m_totalProportion; | |
1879 | for ( i = m_children.begin(); | |
1880 | i != m_children.end(); | |
1881 | ++i ) | |
1882 | { | |
1883 | wxSizerItem * const item = *i; | |
1884 | ||
1885 | if ( !item->IsShown() ) | |
1886 | continue; | |
1887 | ||
1888 | const wxSize sizeThis(item->GetMinSizeWithBorder()); | |
1889 | ||
1890 | // adjust the size in the major direction using the proportion | |
1891 | wxCoord majorSize = GetSizeInMajorDir(sizeThis); | |
1892 | const int propItem = item->GetProportion(); | |
1893 | if ( propItem ) | |
1894 | { | |
1895 | const int deltaItem = (delta * propItem) / totalProportion; | |
1896 | ||
1897 | majorSize += deltaItem; | |
1898 | ||
1899 | delta -= deltaItem; | |
1900 | totalProportion -= propItem; | |
1901 | } | |
1902 | ||
1903 | ||
1904 | // apply the alignment in the minor direction | |
1905 | wxPoint posChild(pt); | |
1906 | ||
1907 | wxCoord minorSize = GetSizeInMinorDir(sizeThis); | |
1908 | const int flag = item->GetFlag(); | |
1909 | if ( flag & (wxEXPAND | wxSHAPED) ) | |
1910 | { | |
1911 | minorSize = totalMinorSize; | |
1912 | } | |
1913 | else if ( flag & (IsVertical() ? wxALIGN_RIGHT : wxALIGN_BOTTOM) ) | |
1914 | { | |
1915 | PosInMinorDir(posChild) += totalMinorSize - minorSize; | |
1916 | } | |
1917 | // NB: wxCENTRE is used here only for backwards compatibility, | |
1918 | // wxALIGN_CENTRE should be used in new code | |
1919 | else if ( flag & (wxCENTER | wxALIGN_CENTRE) ) | |
1920 | { | |
1921 | PosInMinorDir(posChild) += (totalMinorSize - minorSize) / 2; | |
1922 | } | |
1923 | ||
1924 | ||
1925 | // apply RTL adjustment for horizontal sizers: | |
1926 | if ( !IsVertical() && m_containingWindow ) | |
1927 | { | |
1928 | posChild.x = m_containingWindow->AdjustForLayoutDirection | |
1929 | ( | |
1930 | posChild.x, | |
1931 | majorSize, | |
1932 | m_size.x | |
1933 | ); | |
1934 | } | |
1935 | ||
1936 | // finally set size of this child and advance to the next one | |
1937 | item->SetDimension(posChild, SizeFromMajorMinor(majorSize, minorSize)); | |
1938 | ||
1939 | PosInMajorDir(pt) += majorSize; | |
1940 | } | |
1941 | } | |
1942 | ||
1943 | wxSize wxBoxSizer::CalcMin() | |
1944 | { | |
1945 | m_totalProportion = 0; | |
1946 | m_minSize = wxSize(0, 0); | |
1947 | ||
1948 | // calculate the minimal sizes for all items and count sum of proportions | |
1949 | for ( wxSizerItemList::const_iterator i = m_children.begin(); | |
1950 | i != m_children.end(); | |
1951 | ++i ) | |
1952 | { | |
1953 | wxSizerItem * const item = *i; | |
1954 | ||
1955 | if ( !item->IsShown() ) | |
1956 | continue; | |
1957 | ||
1958 | const wxSize sizeMinThis = item->CalcMin(); | |
1959 | SizeInMajorDir(m_minSize) += GetSizeInMajorDir(sizeMinThis); | |
1960 | if ( GetSizeInMinorDir(sizeMinThis) > GetSizeInMinorDir(m_minSize) ) | |
1961 | SizeInMinorDir(m_minSize) = GetSizeInMinorDir(sizeMinThis); | |
1962 | ||
1963 | m_totalProportion += item->GetProportion(); | |
1964 | } | |
1965 | ||
1966 | return m_minSize; | |
1967 | } | |
1968 | ||
1969 | //--------------------------------------------------------------------------- | |
1970 | // wxWrapSizer | |
1971 | //--------------------------------------------------------------------------- | |
1972 | ||
1973 | #define wxDEFAULT_PROPORTION_LAST 1000000 | |
1974 | ||
1975 | // User data to hold old proportion for last item on line | |
1976 | // (which might be extended) | |
1977 | struct wxPropHolder : public wxObject | |
1978 | { | |
1979 | wxPropHolder( ) : m_item(0), m_propOld(0) { } | |
1980 | void Init( wxSizerItem *item, int propOld ) { m_item=item; m_propOld=propOld; } | |
1981 | ||
1982 | wxSizerItem *m_item; | |
1983 | int m_propOld; | |
1984 | }; | |
1985 | ||
1986 | IMPLEMENT_DYNAMIC_CLASS(wxWrapSizer, wxBoxSizer); | |
1987 | ||
1988 | wxWrapSizer::wxWrapSizer( int orient, int flags ) | |
1989 | : wxBoxSizer(orient), | |
1990 | m_prim_size_last( -1 ), | |
1991 | m_rows(orient^wxBOTH), | |
1992 | m_flags(flags) | |
1993 | { | |
1994 | } | |
1995 | ||
1996 | wxWrapSizer::~wxWrapSizer() | |
1997 | { | |
1998 | // Have to clear grand child items so that they're not deleted twice | |
1999 | for( int ix=m_rows.GetChildren().GetCount()-1; ix>=0; ix-- ) | |
2000 | { | |
2001 | wxSizer *psz = m_rows.GetItem((size_t)ix)->GetSizer(); | |
2002 | wxSizerItemList &sl = psz->GetChildren(); | |
2003 | while( sl.GetLast() ) | |
2004 | sl.Erase( sl.GetLast() ); | |
2005 | } | |
2006 | } | |
2007 | ||
2008 | ||
2009 | bool wxWrapSizer::InformFirstDirection( int direction, int size, int WXUNUSED(availableOtherDir) ) | |
2010 | { | |
2011 | if( !direction ) | |
2012 | { | |
2013 | // Better to keep value, then CalcMin will work better | |
2014 | //m_prim_size_last = -1; | |
2015 | return false; | |
2016 | } | |
2017 | if( direction==m_orient ) | |
2018 | { | |
2019 | // The direction is same as our primary, so we can make use of it | |
2020 | m_prim_size_last = size; | |
2021 | return true; | |
2022 | } | |
2023 | else | |
2024 | return false; | |
2025 | } | |
2026 | ||
2027 | ||
2028 | void wxWrapSizer::AdjustPropLastItem(wxSizer *psz, wxSizerItem *itemLast) | |
2029 | { | |
2030 | wxSizerItem *psi = m_rows.GetItem(psz); | |
2031 | wxASSERT(psi); | |
2032 | wxPropHolder *pph = (wxPropHolder*)psi->GetUserData(); | |
2033 | if ( !pph ) | |
2034 | psi->SetUserData( pph=new wxPropHolder ); | |
2035 | ||
2036 | pph->Init( itemLast, itemLast->GetProportion() ); | |
2037 | itemLast->SetProportion( wxDEFAULT_PROPORTION_LAST ); | |
2038 | } | |
2039 | ||
2040 | void wxWrapSizer::RecalcSizes() | |
2041 | { | |
2042 | wxASSERT( m_orient&wxBOTH ); | |
2043 | if (m_children.GetCount() == 0) | |
2044 | return; | |
2045 | ||
2046 | // What we do here is to put our items into child box sizers, | |
2047 | // as many of them as we have lines. | |
2048 | ||
2049 | // Empty all items in all rows in owned sizer. | |
2050 | // We have to access the list directly, since we don't want to | |
2051 | // destroy the wxSizerItems. | |
2052 | for( int ix=m_rows.GetChildren().GetCount()-1; ix>=0; ix-- ){ | |
2053 | wxSizerItem *psi = m_rows.GetItem( (size_t)ix ); | |
2054 | ||
2055 | // Restore proportion for last item on line (if item has not been deleted) | |
2056 | wxPropHolder *pph = (wxPropHolder*)psi->GetUserData(); | |
2057 | if( pph && GetChildren().Find(pph->m_item) ) | |
2058 | pph->m_item->SetProportion(pph->m_propOld); | |
2059 | ||
2060 | wxSizer *psz = psi->GetSizer(); | |
2061 | wxASSERT( psz ); | |
2062 | wxSizerItemList &sl = psz->GetChildren(); | |
2063 | while( sl.GetLast() ) | |
2064 | sl.Erase( sl.GetLast() ); | |
2065 | } | |
2066 | ||
2067 | int lineSumMajor = 0; | |
2068 | int majorSize = GetSizeInMajorDir(m_size); | |
2069 | ||
2070 | // Make sure we have at least one child sizer | |
2071 | m_n_line = 1; | |
2072 | if( !m_rows.GetChildren().GetCount() ) | |
2073 | m_rows.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND ); | |
2074 | ||
2075 | // The sizer where to insert items in | |
2076 | wxSizer *psz = m_rows.GetItem((size_t)0)->GetSizer(); | |
2077 | wxASSERT( psz ); | |
2078 | ||
2079 | // Now put our child items into child sizers instead | |
2080 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
2081 | wxSizerItem *item = NULL, *itemLast=NULL; | |
2082 | while (node) | |
2083 | { | |
2084 | item = node->GetData(); | |
2085 | if ( item->IsShown() ) | |
2086 | { | |
2087 | wxSize minSz = item->GetMinSize(); | |
2088 | int minSzMajor = GetSizeInMajorDir(minSz); | |
2089 | ||
2090 | // More space on this line? | |
2091 | if( !lineSumMajor || lineSumMajor+minSzMajor<=majorSize ) | |
2092 | { | |
2093 | lineSumMajor += minSzMajor; | |
2094 | } | |
2095 | else | |
2096 | { | |
2097 | lineSumMajor = minSzMajor; | |
2098 | // Get a new empty sizer to insert into | |
2099 | if( (int)m_rows.GetChildren().GetCount()<=m_n_line ) | |
2100 | m_rows.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND ); | |
2101 | ||
2102 | // If we have extend-last-on-each-line mode, then do so now | |
2103 | // Note: We must store old proportion value then. | |
2104 | if( m_flags&wxEXTEND_LAST_ON_EACH_LINE ) | |
2105 | AdjustPropLastItem(psz,itemLast); | |
2106 | ||
2107 | // The sizer where to insert items in | |
2108 | psz = m_rows.GetItem(m_n_line++)->GetSizer(); | |
2109 | } | |
2110 | itemLast = item; | |
2111 | psz->Add( item ); | |
2112 | // If item is a window, it now has a pointer to the child sizer, | |
2113 | // which is wrong. Set it to point to us. | |
2114 | if( item->GetWindow() ) | |
2115 | item->GetWindow()->SetContainingSizer( this ); | |
2116 | } | |
2117 | node = node->GetNext(); | |
2118 | } | |
2119 | ||
2120 | // If we have extend-last-on-each-line mode, then do so now | |
2121 | if( m_flags&wxEXTEND_LAST_ON_EACH_LINE ) | |
2122 | AdjustPropLastItem(psz,itemLast); | |
2123 | ||
2124 | // If we have more sizers than lines, remove them | |
2125 | while( (int)m_rows.GetChildren().GetCount()>m_n_line ) | |
2126 | m_rows.Remove( m_n_line ); | |
2127 | ||
2128 | // Now do layout on row sizer | |
2129 | m_rows.SetDimension( m_position.x, m_position.y, m_size.x, m_size.y ); | |
2130 | ||
2131 | // Remember this to next time (will be overridden by InformFirstDirection if used) | |
2132 | m_prim_size_last = GetSizeInMajorDir(m_size); | |
2133 | } | |
2134 | ||
2135 | ||
2136 | wxSize wxWrapSizer::CalcMin() | |
2137 | { | |
2138 | if (m_children.GetCount() == 0) | |
2139 | return wxSize(); | |
2140 | ||
2141 | // Algorithm for calculating min size: (assuming horizontal orientation) | |
2142 | // X: Max width of all members | |
2143 | // Y: Based on last X, calculate how many lines needed | |
2144 | // First time around, assume all items fits on one line | |
2145 | ||
2146 | int maxMajor = 0; | |
2147 | int minorSum = 0; | |
2148 | int lineMaxMinor = 0; | |
2149 | int lineSumMajor = 0; | |
2150 | m_n_line = 0; | |
2151 | ||
2152 | // precalc item minsizes and fit on lines (preliminary) | |
2153 | wxSizerItemList::compatibility_iterator node = m_children.GetFirst(); | |
2154 | while (node) | |
2155 | { | |
2156 | wxSizerItem *item = node->GetData(); | |
2157 | if ( item->IsShown() ) | |
2158 | { | |
2159 | wxSize minSz = item->CalcMin(); | |
2160 | int szMajor = GetSizeInMajorDir(minSz); | |
2161 | int szMinor = GetSizeInMinorDir(minSz); | |
2162 | if( szMajor>maxMajor ) maxMajor = szMajor; | |
2163 | // More space on this line? | |
2164 | if( m_prim_size_last<0 || !lineSumMajor || | |
2165 | lineSumMajor+szMajor<=m_prim_size_last ) | |
2166 | { | |
2167 | lineSumMajor += szMajor; | |
2168 | if( szMinor>lineMaxMinor ) | |
2169 | lineMaxMinor = szMinor; | |
2170 | } | |
2171 | else | |
2172 | { | |
2173 | minorSum += lineMaxMinor; // Add height of highest item on last line | |
2174 | m_n_line++; | |
2175 | lineMaxMinor = szMinor; | |
2176 | lineSumMajor = szMajor; | |
2177 | } | |
2178 | } | |
2179 | node = node->GetNext(); | |
2180 | } | |
2181 | minorSum += lineMaxMinor; // Add height of highest item on last line | |
2182 | ||
2183 | m_minSize = SizeFromMajorMinor(maxMajor, minorSum); | |
2184 | return m_minSize; | |
2185 | } | |
2186 | ||
2187 | //--------------------------------------------------------------------------- | |
2188 | // wxStaticBoxSizer | |
2189 | //--------------------------------------------------------------------------- | |
2190 | ||
2191 | #if wxUSE_STATBOX | |
2192 | ||
2193 | wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient ) | |
2194 | : wxBoxSizer( orient ), | |
2195 | m_staticBox( box ) | |
2196 | { | |
2197 | wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") ); | |
2198 | ||
2199 | // do this so that our Detach() is called if the static box is destroyed | |
2200 | // before we are | |
2201 | m_staticBox->SetContainingSizer(this); | |
2202 | } | |
2203 | ||
2204 | wxStaticBoxSizer::wxStaticBoxSizer(int orient, wxWindow *win, const wxString& s) | |
2205 | : wxBoxSizer(orient), | |
2206 | m_staticBox(new wxStaticBox(win, wxID_ANY, s)) | |
2207 | { | |
2208 | // same as above | |
2209 | m_staticBox->SetContainingSizer(this); | |
2210 | } | |
2211 | ||
2212 | wxStaticBoxSizer::~wxStaticBoxSizer() | |
2213 | { | |
2214 | delete m_staticBox; | |
2215 | } | |
2216 | ||
2217 | static void GetStaticBoxBorders( wxStaticBox *box, | |
2218 | int *borderTop, | |
2219 | int *borderOther) | |
2220 | { | |
2221 | // this has to be done platform by platform as there is no way to | |
2222 | // guess the thickness of a wxStaticBox border | |
2223 | box->GetBordersForSizer(borderTop, borderOther); | |
2224 | } | |
2225 | ||
2226 | void wxStaticBoxSizer::RecalcSizes() | |
2227 | { | |
2228 | int top_border, other_border; | |
2229 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); | |
2230 | ||
2231 | m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); | |
2232 | ||
2233 | wxPoint old_pos( m_position ); | |
2234 | m_position.x += other_border; | |
2235 | m_position.y += top_border; | |
2236 | wxSize old_size( m_size ); | |
2237 | m_size.x -= 2*other_border; | |
2238 | m_size.y -= top_border + other_border; | |
2239 | ||
2240 | wxBoxSizer::RecalcSizes(); | |
2241 | ||
2242 | m_position = old_pos; | |
2243 | m_size = old_size; | |
2244 | } | |
2245 | ||
2246 | wxSize wxStaticBoxSizer::CalcMin() | |
2247 | { | |
2248 | int top_border, other_border; | |
2249 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); | |
2250 | ||
2251 | wxSize ret( wxBoxSizer::CalcMin() ); | |
2252 | ret.x += 2*other_border; | |
2253 | ret.y += other_border + top_border; | |
2254 | ||
2255 | return ret; | |
2256 | } | |
2257 | ||
2258 | void wxStaticBoxSizer::ShowItems( bool show ) | |
2259 | { | |
2260 | m_staticBox->Show( show ); | |
2261 | wxBoxSizer::ShowItems( show ); | |
2262 | } | |
2263 | ||
2264 | bool wxStaticBoxSizer::Detach( wxWindow *window ) | |
2265 | { | |
2266 | // avoid deleting m_staticBox in our dtor if it's being detached from the | |
2267 | // sizer (which can happen because it's being already destroyed for | |
2268 | // example) | |
2269 | if ( window == m_staticBox ) | |
2270 | { | |
2271 | m_staticBox = NULL; | |
2272 | return true; | |
2273 | } | |
2274 | ||
2275 | return wxSizer::Detach( window ); | |
2276 | } | |
2277 | ||
2278 | #endif // wxUSE_STATBOX | |
2279 | ||
2280 | #if wxUSE_BUTTON | |
2281 | ||
2282 | wxStdDialogButtonSizer::wxStdDialogButtonSizer() | |
2283 | : wxBoxSizer(wxHORIZONTAL) | |
2284 | { | |
2285 | // Vertical buttons with lots of space on either side | |
2286 | // looks rubbish on WinCE, so let's not do this for now. | |
2287 | // If we are going to use vertical buttons, we should | |
2288 | // put the sizer to the right of other controls in the dialog, | |
2289 | // and that's beyond the scope of this sizer. | |
2290 | #ifndef __WXWINCE__ | |
2291 | bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); | |
2292 | // If we have a PDA screen, put yes/no button over | |
2293 | // all other buttons, otherwise on the left side. | |
2294 | if (is_pda) | |
2295 | m_orient = wxVERTICAL; | |
2296 | #endif | |
2297 | ||
2298 | m_buttonAffirmative = NULL; | |
2299 | m_buttonApply = NULL; | |
2300 | m_buttonNegative = NULL; | |
2301 | m_buttonCancel = NULL; | |
2302 | m_buttonHelp = NULL; | |
2303 | } | |
2304 | ||
2305 | void wxStdDialogButtonSizer::AddButton(wxButton *mybutton) | |
2306 | { | |
2307 | switch (mybutton->GetId()) | |
2308 | { | |
2309 | case wxID_OK: | |
2310 | case wxID_YES: | |
2311 | case wxID_SAVE: | |
2312 | m_buttonAffirmative = mybutton; | |
2313 | break; | |
2314 | case wxID_APPLY: | |
2315 | m_buttonApply = mybutton; | |
2316 | break; | |
2317 | case wxID_NO: | |
2318 | m_buttonNegative = mybutton; | |
2319 | break; | |
2320 | case wxID_CANCEL: | |
2321 | case wxID_CLOSE: | |
2322 | m_buttonCancel = mybutton; | |
2323 | break; | |
2324 | case wxID_HELP: | |
2325 | case wxID_CONTEXT_HELP: | |
2326 | m_buttonHelp = mybutton; | |
2327 | break; | |
2328 | default: | |
2329 | break; | |
2330 | } | |
2331 | } | |
2332 | ||
2333 | void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton *button ) | |
2334 | { | |
2335 | m_buttonAffirmative = button; | |
2336 | } | |
2337 | ||
2338 | void wxStdDialogButtonSizer::SetNegativeButton( wxButton *button ) | |
2339 | { | |
2340 | m_buttonNegative = button; | |
2341 | } | |
2342 | ||
2343 | void wxStdDialogButtonSizer::SetCancelButton( wxButton *button ) | |
2344 | { | |
2345 | m_buttonCancel = button; | |
2346 | } | |
2347 | ||
2348 | void wxStdDialogButtonSizer::Realize() | |
2349 | { | |
2350 | #ifdef __WXMAC__ | |
2351 | Add(0, 0, 0, wxLEFT, 6); | |
2352 | if (m_buttonHelp) | |
2353 | Add((wxWindow*)m_buttonHelp, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 6); | |
2354 | ||
2355 | if (m_buttonNegative){ | |
2356 | // HIG POLICE BULLETIN - destructive buttons need extra padding | |
2357 | // 24 pixels on either side | |
2358 | Add((wxWindow*)m_buttonNegative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 12); | |
2359 | } | |
2360 | ||
2361 | // extra whitespace between help/negative and cancel/ok buttons | |
2362 | Add(0, 0, 1, wxEXPAND, 0); | |
2363 | ||
2364 | if (m_buttonCancel){ | |
2365 | Add((wxWindow*)m_buttonCancel, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 6); | |
2366 | // Cancel or help should be default | |
2367 | // m_buttonCancel->SetDefaultButton(); | |
2368 | } | |
2369 | ||
2370 | // Ugh, Mac doesn't really have apply dialogs, so I'll just | |
2371 | // figure the best place is between Cancel and OK | |
2372 | if (m_buttonApply) | |
2373 | Add((wxWindow*)m_buttonApply, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 6); | |
2374 | ||
2375 | if (m_buttonAffirmative){ | |
2376 | Add((wxWindow*)m_buttonAffirmative, 0, wxALIGN_CENTRE | wxLEFT, 6); | |
2377 | ||
2378 | if (m_buttonAffirmative->GetId() == wxID_SAVE){ | |
2379 | // these buttons have set labels under Mac so we should use them | |
2380 | m_buttonAffirmative->SetLabel(_("Save")); | |
2381 | if (m_buttonNegative) | |
2382 | m_buttonNegative->SetLabel(_("Don't Save")); | |
2383 | } | |
2384 | } | |
2385 | ||
2386 | // Extra space around and at the right | |
2387 | Add(12, 24); | |
2388 | #elif defined(__WXGTK20__) | |
2389 | Add(0, 0, 0, wxLEFT, 9); | |
2390 | if (m_buttonHelp) | |
2391 | Add((wxWindow*)m_buttonHelp, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 3); | |
2392 | ||
2393 | // extra whitespace between help and cancel/ok buttons | |
2394 | Add(0, 0, 1, wxEXPAND, 0); | |
2395 | ||
2396 | if (m_buttonNegative){ | |
2397 | Add((wxWindow*)m_buttonNegative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 3); | |
2398 | } | |
2399 | ||
2400 | // according to HIG, in explicit apply windows the order is: | |
2401 | // [ Help Apply Cancel OK ] | |
2402 | if (m_buttonApply) | |
2403 | Add((wxWindow*)m_buttonApply, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 3); | |
2404 | ||
2405 | if (m_buttonCancel){ | |
2406 | Add((wxWindow*)m_buttonCancel, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, 3); | |
2407 | // Cancel or help should be default | |
2408 | // m_buttonCancel->SetDefaultButton(); | |
2409 | } | |
2410 | ||
2411 | if (m_buttonAffirmative) | |
2412 | Add((wxWindow*)m_buttonAffirmative, 0, wxALIGN_CENTRE | wxLEFT, 6); | |
2413 | #elif defined(__WXMSW__) | |
2414 | // Windows | |
2415 | ||
2416 | // right-justify buttons | |
2417 | Add(0, 0, 1, wxEXPAND, 0); | |
2418 | ||
2419 | if (m_buttonAffirmative){ | |
2420 | Add((wxWindow*)m_buttonAffirmative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonAffirmative->ConvertDialogToPixels(wxSize(2, 0)).x); | |
2421 | } | |
2422 | ||
2423 | if (m_buttonNegative){ | |
2424 | Add((wxWindow*)m_buttonNegative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonNegative->ConvertDialogToPixels(wxSize(2, 0)).x); | |
2425 | } | |
2426 | ||
2427 | if (m_buttonCancel){ | |
2428 | Add((wxWindow*)m_buttonCancel, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonCancel->ConvertDialogToPixels(wxSize(2, 0)).x); | |
2429 | } | |
2430 | if (m_buttonApply) | |
2431 | Add((wxWindow*)m_buttonApply, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonApply->ConvertDialogToPixels(wxSize(2, 0)).x); | |
2432 | ||
2433 | if (m_buttonHelp) | |
2434 | Add((wxWindow*)m_buttonHelp, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonHelp->ConvertDialogToPixels(wxSize(2, 0)).x); | |
2435 | #else | |
2436 | // GTK+1 and any other platform | |
2437 | ||
2438 | // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog | |
2439 | if (m_buttonHelp) | |
2440 | Add((wxWindow*)m_buttonHelp, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonHelp->ConvertDialogToPixels(wxSize(4, 0)).x); | |
2441 | ||
2442 | // extra whitespace between help and cancel/ok buttons | |
2443 | Add(0, 0, 1, wxEXPAND, 0); | |
2444 | ||
2445 | if (m_buttonApply) | |
2446 | Add((wxWindow*)m_buttonApply, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonApply->ConvertDialogToPixels(wxSize(4, 0)).x); | |
2447 | ||
2448 | if (m_buttonAffirmative){ | |
2449 | Add((wxWindow*)m_buttonAffirmative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonAffirmative->ConvertDialogToPixels(wxSize(4, 0)).x); | |
2450 | } | |
2451 | ||
2452 | if (m_buttonNegative){ | |
2453 | Add((wxWindow*)m_buttonNegative, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonNegative->ConvertDialogToPixels(wxSize(4, 0)).x); | |
2454 | } | |
2455 | ||
2456 | if (m_buttonCancel){ | |
2457 | Add((wxWindow*)m_buttonCancel, 0, wxALIGN_CENTRE | wxLEFT | wxRIGHT, m_buttonCancel->ConvertDialogToPixels(wxSize(4, 0)).x); | |
2458 | // Cancel or help should be default | |
2459 | // m_buttonCancel->SetDefaultButton(); | |
2460 | } | |
2461 | ||
2462 | #endif | |
2463 | } | |
2464 | ||
2465 | #endif // wxUSE_BUTTON |