]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: sizer.cpp | |
3 | // Purpose: provide new wxSizer class for layout | |
4 | // Author: Robert Roebling and Robin Dunn | |
5 | // Modified by: | |
6 | // Created: | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Robin Dunn, Dirk Holtwick and Robert Roebling | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "sizer.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #include "wx/sizer.h" | |
24 | #include "wx/utils.h" | |
25 | #include "wx/statbox.h" | |
26 | #include "wx/notebook.h" | |
27 | ||
28 | //--------------------------------------------------------------------------- | |
29 | ||
30 | IMPLEMENT_ABSTRACT_CLASS(wxSizerItem, wxObject); | |
31 | IMPLEMENT_ABSTRACT_CLASS(wxSizer, wxObject); | |
32 | IMPLEMENT_ABSTRACT_CLASS(wxGridSizer, wxSizer); | |
33 | IMPLEMENT_ABSTRACT_CLASS(wxFlexGridSizer, wxGridSizer); | |
34 | IMPLEMENT_ABSTRACT_CLASS(wxBoxSizer, wxSizer); | |
35 | #if wxUSE_STATBOX | |
36 | IMPLEMENT_ABSTRACT_CLASS(wxStaticBoxSizer, wxBoxSizer); | |
37 | #endif | |
38 | #if wxUSE_NOTEBOOK | |
39 | IMPLEMENT_ABSTRACT_CLASS(wxNotebookSizer, wxSizer); | |
40 | #endif | |
41 | ||
42 | //--------------------------------------------------------------------------- | |
43 | // wxSizerItem | |
44 | //--------------------------------------------------------------------------- | |
45 | ||
46 | wxSizerItem::wxSizerItem( int width, int height, int option, int flag, int border, wxObject* userData ) | |
47 | { | |
48 | m_window = (wxWindow *) NULL; | |
49 | m_sizer = (wxSizer *) NULL; | |
50 | m_option = option; | |
51 | m_border = border; | |
52 | m_flag = flag; | |
53 | m_userData = userData; | |
54 | ||
55 | // minimal size is the initial size | |
56 | m_minSize.x = width; | |
57 | m_minSize.y = height; | |
58 | ||
59 | SetRatio(width, height); | |
60 | ||
61 | // size is set directly | |
62 | m_size = m_minSize; | |
63 | } | |
64 | ||
65 | wxSizerItem::wxSizerItem( wxWindow *window, int option, int flag, int border, wxObject* userData ) | |
66 | { | |
67 | m_window = window; | |
68 | m_sizer = (wxSizer *) NULL; | |
69 | m_option = option; | |
70 | m_border = border; | |
71 | m_flag = flag; | |
72 | m_userData = userData; | |
73 | ||
74 | // minimal size is the initial size | |
75 | m_minSize = window->GetSize(); | |
76 | ||
77 | // aspect ratio calculated from initial size | |
78 | SetRatio(m_minSize); | |
79 | ||
80 | // size is calculated later | |
81 | // m_size = ... | |
82 | } | |
83 | ||
84 | wxSizerItem::wxSizerItem( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) | |
85 | { | |
86 | m_window = (wxWindow *) NULL; | |
87 | m_sizer = sizer; | |
88 | m_option = option; | |
89 | m_border = border; | |
90 | m_flag = flag; | |
91 | m_userData = userData; | |
92 | ||
93 | // minimal size is calculated later | |
94 | // m_minSize = ... | |
95 | m_ratio = 0; | |
96 | ||
97 | // size is calculated later | |
98 | // m_size = ... | |
99 | } | |
100 | ||
101 | wxSizerItem::~wxSizerItem() | |
102 | { | |
103 | if (m_userData) | |
104 | delete m_userData; | |
105 | if (m_sizer) | |
106 | delete m_sizer; | |
107 | } | |
108 | ||
109 | ||
110 | wxSize wxSizerItem::GetSize() | |
111 | { | |
112 | wxSize ret; | |
113 | if (IsSizer()) | |
114 | ret = m_sizer->GetSize(); | |
115 | else | |
116 | if (IsWindow()) | |
117 | ret = m_window->GetSize(); | |
118 | else ret = m_size; | |
119 | ||
120 | if (m_flag & wxWEST) | |
121 | ret.x += m_border; | |
122 | if (m_flag & wxEAST) | |
123 | ret.x += m_border; | |
124 | if (m_flag & wxNORTH) | |
125 | ret.y += m_border; | |
126 | if (m_flag & wxSOUTH) | |
127 | ret.y += m_border; | |
128 | ||
129 | return ret; | |
130 | } | |
131 | ||
132 | wxSize wxSizerItem::CalcMin() | |
133 | { | |
134 | wxSize ret; | |
135 | if (IsSizer()) | |
136 | { | |
137 | ret = m_sizer->GetMinSize(); | |
138 | ||
139 | // if we have to preserve aspect ratio _AND_ this is | |
140 | // the first-time calculation, consider ret to be initial size | |
141 | if ((m_flag & wxSHAPED) && !m_ratio) | |
142 | SetRatio(ret); | |
143 | } | |
144 | else | |
145 | { | |
146 | if ( IsWindow() && (m_flag & wxADJUST_MINSIZE) ) | |
147 | { | |
148 | // check if the best (minimal, in fact) window size hadn't changed | |
149 | // by chance: this may happen for, e.g. static text if its label | |
150 | // changed | |
151 | wxSize size = m_window->GetBestSize(); | |
152 | if ( size.x > m_minSize.x ) | |
153 | m_minSize.x = size.x; | |
154 | if ( size.y > m_minSize.y ) | |
155 | m_minSize.y = size.y; | |
156 | } | |
157 | ||
158 | ret = m_minSize; | |
159 | } | |
160 | ||
161 | if (m_flag & wxWEST) | |
162 | ret.x += m_border; | |
163 | if (m_flag & wxEAST) | |
164 | ret.x += m_border; | |
165 | if (m_flag & wxNORTH) | |
166 | ret.y += m_border; | |
167 | if (m_flag & wxSOUTH) | |
168 | ret.y += m_border; | |
169 | ||
170 | return ret; | |
171 | } | |
172 | ||
173 | void wxSizerItem::SetDimension( wxPoint pos, wxSize size ) | |
174 | { | |
175 | if (m_flag & wxSHAPED) | |
176 | { | |
177 | // adjust aspect ratio | |
178 | int rwidth = (int) (size.y * m_ratio); | |
179 | if (rwidth > size.x) | |
180 | { | |
181 | // fit horizontally | |
182 | int rheight = (int) (size.x / m_ratio); | |
183 | // add vertical space | |
184 | if (m_flag & wxALIGN_CENTER_VERTICAL) | |
185 | pos.y += (size.y - rheight) / 2; | |
186 | else if (m_flag & wxALIGN_BOTTOM) | |
187 | pos.y += (size.y - rheight); | |
188 | // use reduced dimensions | |
189 | size.y =rheight; | |
190 | } | |
191 | else if (rwidth < size.x) | |
192 | { | |
193 | // add horizontal space | |
194 | if (m_flag & wxALIGN_CENTER_HORIZONTAL) | |
195 | pos.x += (size.x - rwidth) / 2; | |
196 | else if (m_flag & wxALIGN_RIGHT) | |
197 | pos.x += (size.x - rwidth); | |
198 | size.x = rwidth; | |
199 | } | |
200 | } | |
201 | ||
202 | // This is what GetPosition() returns. Since we calculate | |
203 | // borders afterwards, GetPosition() will be the left/top | |
204 | // corner of the surrounding border. | |
205 | m_pos = pos; | |
206 | ||
207 | if (m_flag & wxWEST) | |
208 | { | |
209 | pos.x += m_border; | |
210 | size.x -= m_border; | |
211 | } | |
212 | if (m_flag & wxEAST) | |
213 | { | |
214 | size.x -= m_border; | |
215 | } | |
216 | if (m_flag & wxNORTH) | |
217 | { | |
218 | pos.y += m_border; | |
219 | size.y -= m_border; | |
220 | } | |
221 | if (m_flag & wxSOUTH) | |
222 | { | |
223 | size.y -= m_border; | |
224 | } | |
225 | ||
226 | if (IsSizer()) | |
227 | m_sizer->SetDimension( pos.x, pos.y, size.x, size.y ); | |
228 | ||
229 | if (IsWindow()) | |
230 | m_window->SetSize( pos.x, pos.y, size.x, size.y, wxSIZE_ALLOW_MINUS_ONE ); | |
231 | ||
232 | m_size = size; | |
233 | } | |
234 | ||
235 | bool wxSizerItem::IsWindow() | |
236 | { | |
237 | return (m_window != NULL); | |
238 | } | |
239 | ||
240 | bool wxSizerItem::IsSizer() | |
241 | { | |
242 | return (m_sizer != NULL); | |
243 | } | |
244 | ||
245 | bool wxSizerItem::IsSpacer() | |
246 | { | |
247 | return (m_window == NULL) && (m_sizer == NULL); | |
248 | } | |
249 | ||
250 | //--------------------------------------------------------------------------- | |
251 | // wxSizer | |
252 | //--------------------------------------------------------------------------- | |
253 | ||
254 | wxSizer::wxSizer() | |
255 | { | |
256 | m_children.DeleteContents( TRUE ); | |
257 | m_minSize.x = 0; | |
258 | m_minSize.y = 0; | |
259 | } | |
260 | ||
261 | wxSizer::~wxSizer() | |
262 | { | |
263 | } | |
264 | ||
265 | void wxSizer::Add( wxWindow *window, int option, int flag, int border, wxObject* userData ) | |
266 | { | |
267 | m_children.Append( new wxSizerItem( window, option, flag, border, userData ) ); | |
268 | } | |
269 | ||
270 | void wxSizer::Add( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) | |
271 | { | |
272 | m_children.Append( new wxSizerItem( sizer, option, flag, border, userData ) ); | |
273 | } | |
274 | ||
275 | void wxSizer::Add( int width, int height, int option, int flag, int border, wxObject* userData ) | |
276 | { | |
277 | m_children.Append( new wxSizerItem( width, height, option, flag, border, userData ) ); | |
278 | } | |
279 | ||
280 | void wxSizer::Prepend( wxWindow *window, int option, int flag, int border, wxObject* userData ) | |
281 | { | |
282 | m_children.Insert( new wxSizerItem( window, option, flag, border, userData ) ); | |
283 | } | |
284 | ||
285 | void wxSizer::Prepend( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) | |
286 | { | |
287 | m_children.Insert( new wxSizerItem( sizer, option, flag, border, userData ) ); | |
288 | } | |
289 | ||
290 | void wxSizer::Prepend( int width, int height, int option, int flag, int border, wxObject* userData ) | |
291 | { | |
292 | m_children.Insert( new wxSizerItem( width, height, option, flag, border, userData ) ); | |
293 | } | |
294 | ||
295 | void wxSizer::Insert( int before, wxWindow *window, int option, int flag, int border, wxObject* userData ) | |
296 | { | |
297 | m_children.Insert( before, new wxSizerItem( window, option, flag, border, userData ) ); | |
298 | } | |
299 | ||
300 | void wxSizer::Insert( int before, wxSizer *sizer, int option, int flag, int border, wxObject* userData ) | |
301 | { | |
302 | m_children.Insert( before, new wxSizerItem( sizer, option, flag, border, userData ) ); | |
303 | } | |
304 | ||
305 | void wxSizer::Insert( int before, int width, int height, int option, int flag, int border, wxObject* userData ) | |
306 | { | |
307 | m_children.Insert( before, new wxSizerItem( width, height, option, flag, border, userData ) ); | |
308 | } | |
309 | ||
310 | bool wxSizer::Remove( wxWindow *window ) | |
311 | { | |
312 | wxASSERT( window ); | |
313 | ||
314 | wxNode *node = m_children.First(); | |
315 | while (node) | |
316 | { | |
317 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
318 | if (item->GetWindow() == window) | |
319 | { | |
320 | m_children.DeleteNode( node ); | |
321 | return TRUE; | |
322 | } | |
323 | node = node->Next(); | |
324 | } | |
325 | ||
326 | return FALSE; | |
327 | } | |
328 | ||
329 | bool wxSizer::Remove( wxSizer *sizer ) | |
330 | { | |
331 | wxASSERT( sizer ); | |
332 | ||
333 | wxNode *node = m_children.First(); | |
334 | while (node) | |
335 | { | |
336 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
337 | if (item->GetSizer() == sizer) | |
338 | { | |
339 | m_children.DeleteNode( node ); | |
340 | return TRUE; | |
341 | } | |
342 | node = node->Next(); | |
343 | } | |
344 | ||
345 | return FALSE; | |
346 | } | |
347 | ||
348 | bool wxSizer::Remove( int pos ) | |
349 | { | |
350 | wxNode *node = m_children.Nth( pos ); | |
351 | if (!node) return FALSE; | |
352 | ||
353 | m_children.DeleteNode( node ); | |
354 | ||
355 | return TRUE; | |
356 | } | |
357 | ||
358 | void wxSizer::Fit( wxWindow *window ) | |
359 | { | |
360 | wxSize size; | |
361 | if (window->IsTopLevel()) | |
362 | size = FitSize( window ); | |
363 | else | |
364 | size = GetMinWindowSize( window ); | |
365 | ||
366 | window->SetSize( size ); | |
367 | } | |
368 | ||
369 | void wxSizer::Layout() | |
370 | { | |
371 | CalcMin(); | |
372 | RecalcSizes(); | |
373 | } | |
374 | ||
375 | void wxSizer::SetSizeHints( wxWindow *window ) | |
376 | { | |
377 | wxSize size = FitSize( window ); | |
378 | window->SetSizeHints( size.x, size.y ); | |
379 | } | |
380 | ||
381 | wxSize wxSizer::GetMaxWindowSize( wxWindow *WXUNUSED(window) ) | |
382 | { | |
383 | wxRect rect = wxGetClientDisplayRect(); | |
384 | wxSize sizeMax (rect.width,rect.height); | |
385 | ||
386 | // Sorry, but this bit is wrong -- it makes a window that should just be | |
387 | // able to fit onto the screen, not fit on the screen. -- JACS | |
388 | #if 0 | |
389 | // Make the max size a bit smaller than the visible portion of | |
390 | // the screen. A window which takes the entire screen doesn't | |
391 | // look very nice either | |
392 | sizeMax.x *= 9; | |
393 | sizeMax.x /= 10; | |
394 | ||
395 | sizeMax.y *= 9; | |
396 | sizeMax.y /= 10; | |
397 | #endif | |
398 | ||
399 | return sizeMax; | |
400 | } | |
401 | ||
402 | wxSize wxSizer::GetMinWindowSize( wxWindow *window ) | |
403 | { | |
404 | wxSize minSize( GetMinSize() ); | |
405 | wxSize size( window->GetSize() ); | |
406 | wxSize client_size( window->GetClientSize() ); | |
407 | return wxSize( minSize.x+size.x-client_size.x, | |
408 | minSize.y+size.y-client_size.y ); | |
409 | } | |
410 | ||
411 | // Return a window size that will fit within the screens dimensions | |
412 | wxSize wxSizer::FitSize( wxWindow *window ) | |
413 | { | |
414 | wxSize size = GetMinWindowSize( window ); | |
415 | wxSize sizeMax = GetMaxWindowSize( window ); | |
416 | ||
417 | if ( size.x > sizeMax.x ) | |
418 | size.x = sizeMax.x; | |
419 | if ( size.y > sizeMax.y ) | |
420 | size.y = sizeMax.y; | |
421 | ||
422 | return size; | |
423 | } | |
424 | ||
425 | void wxSizer::SetDimension( int x, int y, int width, int height ) | |
426 | { | |
427 | m_position.x = x; | |
428 | m_position.y = y; | |
429 | m_size.x = width; | |
430 | m_size.y = height; | |
431 | CalcMin(); | |
432 | RecalcSizes(); | |
433 | } | |
434 | ||
435 | wxSize wxSizer::GetMinSize() | |
436 | { | |
437 | wxSize ret( CalcMin() ); | |
438 | if (ret.x < m_minSize.x) ret.x = m_minSize.x; | |
439 | if (ret.y < m_minSize.y) ret.y = m_minSize.y; | |
440 | return ret; | |
441 | } | |
442 | ||
443 | void wxSizer::DoSetMinSize( int width, int height ) | |
444 | { | |
445 | m_minSize.x = width; | |
446 | m_minSize.y = height; | |
447 | } | |
448 | ||
449 | bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height ) | |
450 | { | |
451 | wxASSERT( window ); | |
452 | ||
453 | wxNode *node = m_children.First(); | |
454 | while (node) | |
455 | { | |
456 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
457 | if (item->GetWindow() == window) | |
458 | { | |
459 | item->SetInitSize( width, height ); | |
460 | return TRUE; | |
461 | } | |
462 | node = node->Next(); | |
463 | } | |
464 | ||
465 | node = m_children.First(); | |
466 | while (node) | |
467 | { | |
468 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
469 | if (item->GetSizer()) | |
470 | { | |
471 | /* It's a sizer, so lets search recursively. */ | |
472 | if (item->GetSizer()->DoSetItemMinSize( window, width, height )) | |
473 | { | |
474 | /* A child sizer found the requested windw, exit. */ | |
475 | return TRUE; | |
476 | } | |
477 | } | |
478 | node = node->Next(); | |
479 | } | |
480 | ||
481 | return FALSE; | |
482 | } | |
483 | ||
484 | bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) | |
485 | { | |
486 | wxASSERT( sizer ); | |
487 | ||
488 | wxNode *node = m_children.First(); | |
489 | while (node) | |
490 | { | |
491 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
492 | if (item->GetSizer() == sizer) | |
493 | { | |
494 | item->GetSizer()->DoSetMinSize( width, height ); | |
495 | return TRUE; | |
496 | } | |
497 | node = node->Next(); | |
498 | } | |
499 | ||
500 | node = m_children.First(); | |
501 | while (node) | |
502 | { | |
503 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
504 | if (item->GetSizer()) | |
505 | { | |
506 | /* It's a sizer, so lets search recursively. */ | |
507 | if (item->GetSizer()->DoSetItemMinSize( sizer, width, height )) | |
508 | { | |
509 | /* A child sizer found the requested windw, exit. */ | |
510 | return TRUE; | |
511 | } | |
512 | } | |
513 | node = node->Next(); | |
514 | } | |
515 | ||
516 | return FALSE; | |
517 | } | |
518 | ||
519 | bool wxSizer::DoSetItemMinSize( int pos, int width, int height ) | |
520 | { | |
521 | wxNode *node = m_children.Nth( pos ); | |
522 | if (!node) return FALSE; | |
523 | ||
524 | wxSizerItem *item = (wxSizerItem*) node->Data(); | |
525 | if (item->GetSizer()) | |
526 | { | |
527 | /* Sizers contains the minimal size in them, if not calculated ... */ | |
528 | item->GetSizer()->DoSetMinSize( width, height ); | |
529 | } | |
530 | else | |
531 | { | |
532 | /* ... whereas the minimal size of spacers and windows in stored | |
533 | in the item */ | |
534 | item->SetInitSize( width, height ); | |
535 | } | |
536 | ||
537 | return TRUE; | |
538 | } | |
539 | ||
540 | //--------------------------------------------------------------------------- | |
541 | // wxGridSizer | |
542 | //--------------------------------------------------------------------------- | |
543 | ||
544 | wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap ) | |
545 | { | |
546 | m_rows = rows; | |
547 | m_cols = cols; | |
548 | m_vgap = vgap; | |
549 | m_hgap = hgap; | |
550 | } | |
551 | ||
552 | wxGridSizer::wxGridSizer( int cols, int vgap, int hgap ) | |
553 | { | |
554 | m_rows = 0; | |
555 | m_cols = cols; | |
556 | m_vgap = vgap; | |
557 | m_hgap = hgap; | |
558 | } | |
559 | ||
560 | void wxGridSizer::RecalcSizes() | |
561 | { | |
562 | if (m_children.GetCount() == 0) | |
563 | return; | |
564 | ||
565 | int nitems = m_children.GetCount(); | |
566 | int nrows = m_rows; | |
567 | int ncols = m_cols; | |
568 | ||
569 | if (ncols > 0) | |
570 | nrows = (nitems + ncols-1) / ncols; | |
571 | else | |
572 | ncols = (nitems + nrows-1) / nrows; | |
573 | ||
574 | wxSize sz( GetSize() ); | |
575 | wxPoint pt( GetPosition() ); | |
576 | ||
577 | int w = (sz.x - (ncols - 1) * m_hgap) / ncols; | |
578 | int h = (sz.y - (nrows - 1) * m_vgap) / nrows; | |
579 | ||
580 | int x = pt.x; | |
581 | for (int c = 0; c < ncols; c++) | |
582 | { | |
583 | int y = pt.y; | |
584 | for (int r = 0; r < nrows; r++) | |
585 | { | |
586 | int i = r * ncols + c; | |
587 | if (i < nitems) | |
588 | { | |
589 | wxNode *node = m_children.Nth( i ); | |
590 | wxASSERT( node ); | |
591 | ||
592 | SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h); | |
593 | } | |
594 | y = y + h + m_vgap; | |
595 | } | |
596 | x = x + w + m_hgap; | |
597 | } | |
598 | } | |
599 | ||
600 | wxSize wxGridSizer::CalcMin() | |
601 | { | |
602 | if (m_children.GetCount() == 0) | |
603 | return wxSize(10,10); | |
604 | ||
605 | int nitems = m_children.GetCount(); | |
606 | int nrows = m_rows; | |
607 | int ncols = m_cols; | |
608 | ||
609 | if (ncols > 0) | |
610 | nrows = (nitems + ncols-1) / ncols; | |
611 | else | |
612 | ncols = (nitems + nrows-1) / nrows; | |
613 | ||
614 | /* Find the max width and height for any component */ | |
615 | int w = 0; | |
616 | int h = 0; | |
617 | ||
618 | wxNode *node = m_children.First(); | |
619 | while (node) | |
620 | { | |
621 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
622 | wxSize sz( item->CalcMin() ); | |
623 | w = wxMax( w, sz.x ); | |
624 | h = wxMax( h, sz.y ); | |
625 | ||
626 | node = node->Next(); | |
627 | } | |
628 | ||
629 | return wxSize(ncols * w + (ncols-1) * m_hgap, | |
630 | nrows * h + (nrows-1) * m_vgap); | |
631 | } | |
632 | ||
633 | void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h ) | |
634 | { | |
635 | wxPoint pt( x,y ); | |
636 | wxSize sz( item->CalcMin() ); | |
637 | int flag = item->GetFlag(); | |
638 | ||
639 | if ((flag & wxEXPAND) || (flag & wxSHAPED)) | |
640 | { | |
641 | sz = wxSize(w, h); | |
642 | } | |
643 | else | |
644 | { | |
645 | if (flag & wxALIGN_CENTER_HORIZONTAL) | |
646 | { | |
647 | pt.x = x + (w - sz.x) / 2; | |
648 | } | |
649 | else if (flag & wxALIGN_RIGHT) | |
650 | { | |
651 | pt.x = x + (w - sz.x); | |
652 | } | |
653 | ||
654 | if (flag & wxALIGN_CENTER_VERTICAL) | |
655 | { | |
656 | pt.y = y + (h - sz.y) / 2; | |
657 | } | |
658 | else if (flag & wxALIGN_BOTTOM) | |
659 | { | |
660 | pt.y = y + (h - sz.y); | |
661 | } | |
662 | } | |
663 | ||
664 | item->SetDimension(pt, sz); | |
665 | } | |
666 | ||
667 | //--------------------------------------------------------------------------- | |
668 | // wxFlexGridSizer | |
669 | //--------------------------------------------------------------------------- | |
670 | ||
671 | wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap ) | |
672 | : wxGridSizer( rows, cols, vgap, hgap ) | |
673 | { | |
674 | m_rowHeights = (int*) NULL; | |
675 | m_colWidths = (int*) NULL; | |
676 | } | |
677 | ||
678 | wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap ) | |
679 | : wxGridSizer( cols, vgap, hgap ) | |
680 | { | |
681 | m_rowHeights = (int*) NULL; | |
682 | m_colWidths = (int*) NULL; | |
683 | } | |
684 | ||
685 | wxFlexGridSizer::~wxFlexGridSizer() | |
686 | { | |
687 | if (m_rowHeights) | |
688 | delete[] m_rowHeights; | |
689 | if (m_colWidths) | |
690 | delete[] m_colWidths; | |
691 | } | |
692 | ||
693 | void wxFlexGridSizer::CreateArrays() | |
694 | { | |
695 | if (m_rowHeights) | |
696 | delete[] m_rowHeights; | |
697 | if (m_colWidths) | |
698 | delete[] m_colWidths; | |
699 | ||
700 | if (m_children.GetCount() == 0) | |
701 | return; | |
702 | ||
703 | int nitems = m_children.GetCount(); | |
704 | int nrows = m_rows; | |
705 | int ncols = m_cols; | |
706 | ||
707 | if (ncols > 0) | |
708 | nrows = (nitems + ncols-1) / ncols; | |
709 | else | |
710 | ncols = (nitems + nrows-1) / nrows; | |
711 | ||
712 | m_rowHeights = new int[nrows]; | |
713 | m_colWidths = new int[ncols]; | |
714 | ||
715 | for (int col = 0; col < ncols; col++) | |
716 | m_colWidths[ col ] = 0; | |
717 | for (int row = 0; row < nrows; row++) | |
718 | m_rowHeights[ row ] = 0; | |
719 | } | |
720 | ||
721 | void wxFlexGridSizer::RecalcSizes() | |
722 | { | |
723 | if (m_children.GetCount() == 0) | |
724 | return; | |
725 | ||
726 | int nitems = m_children.GetCount(); | |
727 | int nrows = m_rows; | |
728 | int ncols = m_cols; | |
729 | ||
730 | if (ncols > 0) | |
731 | nrows = (nitems + ncols-1) / ncols; | |
732 | else | |
733 | ncols = (nitems + nrows-1) / nrows; | |
734 | ||
735 | wxSize sz( GetSize() ); | |
736 | wxSize minsz( CalcMin() ); | |
737 | wxPoint pt( GetPosition() ); | |
738 | int delta; | |
739 | size_t idx; | |
740 | ||
741 | if ((m_growableRows.GetCount() > 0) && (sz.y > minsz.y)) | |
742 | { | |
743 | delta = (sz.y - minsz.y) / m_growableRows.GetCount(); | |
744 | for (idx = 0; idx < m_growableRows.GetCount(); idx++) | |
745 | m_rowHeights[ m_growableRows[idx] ] += delta; | |
746 | } | |
747 | ||
748 | if ((m_growableCols.GetCount() > 0) && (sz.x > minsz.x)) | |
749 | { | |
750 | delta = (sz.x - minsz.x) / m_growableCols.GetCount(); | |
751 | for (idx = 0; idx < m_growableCols.GetCount(); idx++) | |
752 | m_colWidths[ m_growableCols[idx] ] += delta; | |
753 | } | |
754 | ||
755 | sz = wxSize( pt.x + sz.x, pt.y + sz.y ); | |
756 | ||
757 | int x = pt.x; | |
758 | for (int c = 0; c < ncols; c++) | |
759 | { | |
760 | int y = pt.y; | |
761 | for (int r = 0; r < nrows; r++) | |
762 | { | |
763 | int i = r * ncols + c; | |
764 | if (i < nitems) | |
765 | { | |
766 | wxNode *node = m_children.Nth( i ); | |
767 | wxASSERT( node ); | |
768 | ||
769 | int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) ); | |
770 | int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) ); | |
771 | ||
772 | SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h); | |
773 | } | |
774 | y = y + m_rowHeights[r] + m_vgap; | |
775 | } | |
776 | x = x + m_colWidths[c] + m_hgap; | |
777 | } | |
778 | } | |
779 | ||
780 | wxSize wxFlexGridSizer::CalcMin() | |
781 | { | |
782 | if (m_children.GetCount() == 0) | |
783 | return wxSize(10,10); | |
784 | ||
785 | int nitems = m_children.GetCount(); | |
786 | int nrows = m_rows; | |
787 | int ncols = m_cols; | |
788 | ||
789 | if (ncols > 0) | |
790 | nrows = (nitems + ncols-1) / ncols; | |
791 | else | |
792 | ncols = (nitems + nrows-1) / nrows; | |
793 | ||
794 | CreateArrays(); | |
795 | ||
796 | int col; | |
797 | int row; | |
798 | ||
799 | int i = 0; | |
800 | wxNode *node = m_children.First(); | |
801 | while (node) | |
802 | { | |
803 | wxSizerItem *item = (wxSizerItem*)node->Data(); | |
804 | wxSize sz( item->CalcMin() ); | |
805 | row = i / ncols; | |
806 | col = i % ncols; | |
807 | m_rowHeights[ row ] = wxMax( sz.y, m_rowHeights[ row ] ); | |
808 | m_colWidths[ col ] = wxMax( sz.x, m_colWidths[ col ] ); | |
809 | ||
810 | node = node->Next(); | |
811 | i++; | |
812 | } | |
813 | ||
814 | int width = 0; | |
815 | for (col = 0; col < ncols; col++) | |
816 | width += m_colWidths[ col ]; | |
817 | ||
818 | int height = 0; | |
819 | for (row = 0; row < nrows; row++) | |
820 | height += m_rowHeights[ row ]; | |
821 | ||
822 | return wxSize( width + (ncols-1) * m_hgap, | |
823 | height + (nrows-1) * m_vgap); | |
824 | } | |
825 | ||
826 | void wxFlexGridSizer::AddGrowableRow( size_t idx ) | |
827 | { | |
828 | m_growableRows.Add( idx ); | |
829 | } | |
830 | ||
831 | void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) ) | |
832 | { | |
833 | } | |
834 | ||
835 | void wxFlexGridSizer::AddGrowableCol( size_t idx ) | |
836 | { | |
837 | m_growableCols.Add( idx ); | |
838 | } | |
839 | ||
840 | void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) ) | |
841 | { | |
842 | } | |
843 | ||
844 | //--------------------------------------------------------------------------- | |
845 | // wxBoxSizer | |
846 | //--------------------------------------------------------------------------- | |
847 | ||
848 | wxBoxSizer::wxBoxSizer( int orient ) | |
849 | { | |
850 | m_orient = orient; | |
851 | } | |
852 | ||
853 | void wxBoxSizer::RecalcSizes() | |
854 | { | |
855 | if (m_children.GetCount() == 0) | |
856 | return; | |
857 | ||
858 | int delta = 0; | |
859 | int extra = 0; | |
860 | if (m_stretchable) | |
861 | { | |
862 | if (m_orient == wxHORIZONTAL) | |
863 | { | |
864 | delta = (m_size.x - m_fixedWidth) / m_stretchable; | |
865 | extra = (m_size.x - m_fixedWidth) % m_stretchable; | |
866 | } | |
867 | else | |
868 | { | |
869 | delta = (m_size.y - m_fixedHeight) / m_stretchable; | |
870 | extra = (m_size.y - m_fixedHeight) % m_stretchable; | |
871 | } | |
872 | } | |
873 | ||
874 | wxPoint pt( m_position ); | |
875 | ||
876 | wxNode *node = m_children.GetFirst(); | |
877 | while (node) | |
878 | { | |
879 | wxSizerItem *item = (wxSizerItem*) node->Data(); | |
880 | ||
881 | int weight = 1; | |
882 | if (item->GetOption()) | |
883 | weight = item->GetOption(); | |
884 | ||
885 | wxSize size( item->CalcMin() ); | |
886 | ||
887 | if (m_orient == wxVERTICAL) | |
888 | { | |
889 | wxCoord height = size.y; | |
890 | if (item->GetOption()) | |
891 | { | |
892 | height = (delta * weight) + extra; | |
893 | extra = 0; // only the first item will get the remainder as extra size | |
894 | } | |
895 | ||
896 | wxPoint child_pos( pt ); | |
897 | wxSize child_size( wxSize( size.x, height) ); | |
898 | ||
899 | if (item->GetFlag() & (wxEXPAND | wxSHAPED)) | |
900 | child_size.x = m_size.x; | |
901 | else if (item->GetFlag() & wxALIGN_RIGHT) | |
902 | child_pos.x += m_size.x - size.x; | |
903 | else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL)) | |
904 | // XXX wxCENTER is added for backward compatibility; | |
905 | // wxALIGN_CENTER should be used in new code | |
906 | child_pos.x += (m_size.x - size.x) / 2; | |
907 | ||
908 | item->SetDimension( child_pos, child_size ); | |
909 | ||
910 | pt.y += height; | |
911 | } | |
912 | else | |
913 | { | |
914 | wxCoord width = size.x; | |
915 | if (item->GetOption()) | |
916 | { | |
917 | width = (delta * weight) + extra; | |
918 | extra = 0; // only the first item will get the remainder as extra size | |
919 | } | |
920 | ||
921 | wxPoint child_pos( pt ); | |
922 | wxSize child_size( wxSize(width, size.y) ); | |
923 | ||
924 | if (item->GetFlag() & (wxEXPAND | wxSHAPED)) | |
925 | child_size.y = m_size.y; | |
926 | else if (item->GetFlag() & wxALIGN_BOTTOM) | |
927 | child_pos.y += m_size.y - size.y; | |
928 | else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL)) | |
929 | // XXX wxCENTER is added for backward compatibility; | |
930 | // wxALIGN_CENTER should be used in new code | |
931 | child_pos.y += (m_size.y - size.y) / 2; | |
932 | ||
933 | item->SetDimension( child_pos, child_size ); | |
934 | ||
935 | pt.x += width; | |
936 | } | |
937 | ||
938 | node = node->Next(); | |
939 | } | |
940 | } | |
941 | ||
942 | wxSize wxBoxSizer::CalcMin() | |
943 | { | |
944 | if (m_children.GetCount() == 0) | |
945 | return wxSize(10,10); | |
946 | ||
947 | m_stretchable = 0; | |
948 | m_minWidth = 0; | |
949 | m_minHeight = 0; | |
950 | m_fixedWidth = 0; | |
951 | m_fixedHeight = 0; | |
952 | ||
953 | // Find how long each stretch unit needs to be | |
954 | int stretchSize = 1; | |
955 | wxNode *node = m_children.GetFirst(); | |
956 | while (node) | |
957 | { | |
958 | wxSizerItem *item = (wxSizerItem*) node->Data(); | |
959 | if (item->GetOption() != 0) | |
960 | { | |
961 | int stretch = item->GetOption(); | |
962 | wxSize size( item->CalcMin() ); | |
963 | int sizePerStretch; | |
964 | // Integer division rounded up is (a + b - 1) / b | |
965 | if (m_orient == wxHORIZONTAL) | |
966 | sizePerStretch = ( size.x + stretch - 1 ) / stretch; | |
967 | else | |
968 | sizePerStretch = ( size.y + stretch - 1 ) / stretch; | |
969 | if (sizePerStretch > stretchSize) | |
970 | stretchSize = sizePerStretch; | |
971 | } | |
972 | node = node->Next(); | |
973 | } | |
974 | // Calculate overall minimum size | |
975 | node = m_children.GetFirst(); | |
976 | while (node) | |
977 | { | |
978 | wxSizerItem *item = (wxSizerItem*) node->Data(); | |
979 | ||
980 | m_stretchable += item->GetOption(); | |
981 | ||
982 | wxSize size( item->CalcMin() ); | |
983 | if (item->GetOption() != 0) | |
984 | { | |
985 | if (m_orient == wxHORIZONTAL) | |
986 | size.x = stretchSize * item->GetOption(); | |
987 | else | |
988 | size.y = stretchSize * item->GetOption(); | |
989 | } | |
990 | ||
991 | if (m_orient == wxHORIZONTAL) | |
992 | { | |
993 | m_minWidth += size.x; | |
994 | m_minHeight = wxMax( m_minHeight, size.y ); | |
995 | } | |
996 | else | |
997 | { | |
998 | m_minHeight += size.y; | |
999 | m_minWidth = wxMax( m_minWidth, size.x ); | |
1000 | } | |
1001 | ||
1002 | if (item->GetOption() == 0) | |
1003 | { | |
1004 | if (m_orient == wxVERTICAL) | |
1005 | { | |
1006 | m_fixedHeight += size.y; | |
1007 | m_fixedWidth = wxMax( m_fixedWidth, size.x ); | |
1008 | } | |
1009 | else | |
1010 | { | |
1011 | m_fixedWidth += size.x; | |
1012 | m_fixedHeight = wxMax( m_fixedHeight, size.y ); | |
1013 | } | |
1014 | } | |
1015 | ||
1016 | node = node->Next(); | |
1017 | } | |
1018 | ||
1019 | return wxSize( m_minWidth, m_minHeight ); | |
1020 | } | |
1021 | ||
1022 | //--------------------------------------------------------------------------- | |
1023 | // wxStaticBoxSizer | |
1024 | //--------------------------------------------------------------------------- | |
1025 | ||
1026 | #if wxUSE_STATBOX | |
1027 | ||
1028 | wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient ) | |
1029 | : wxBoxSizer( orient ) | |
1030 | { | |
1031 | wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") ); | |
1032 | ||
1033 | m_staticBox = box; | |
1034 | } | |
1035 | ||
1036 | static void GetStaticBoxBorders(wxStaticBox *box, | |
1037 | int *borderTop, int *borderOther) | |
1038 | { | |
1039 | // this has to be done platform by platform as there is no way to | |
1040 | // guess the thickness of a wxStaticBox border | |
1041 | #ifdef __WXGTK__ | |
1042 | if ( box->GetLabel().IsEmpty() ) | |
1043 | *borderTop = 5; | |
1044 | else | |
1045 | #endif // __WXGTK__ | |
1046 | *borderTop = 15; | |
1047 | (void)box; | |
1048 | *borderOther = 5; | |
1049 | } | |
1050 | ||
1051 | void wxStaticBoxSizer::RecalcSizes() | |
1052 | { | |
1053 | int top_border, other_border; | |
1054 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); | |
1055 | ||
1056 | m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); | |
1057 | ||
1058 | wxPoint old_pos( m_position ); | |
1059 | m_position.x += other_border; | |
1060 | m_position.y += top_border; | |
1061 | wxSize old_size( m_size ); | |
1062 | m_size.x -= 2*other_border; | |
1063 | m_size.y -= top_border + other_border; | |
1064 | ||
1065 | wxBoxSizer::RecalcSizes(); | |
1066 | ||
1067 | m_position = old_pos; | |
1068 | m_size = old_size; | |
1069 | } | |
1070 | ||
1071 | wxSize wxStaticBoxSizer::CalcMin() | |
1072 | { | |
1073 | int top_border, other_border; | |
1074 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); | |
1075 | ||
1076 | wxSize ret( wxBoxSizer::CalcMin() ); | |
1077 | ret.x += 2*other_border; | |
1078 | ret.y += other_border + top_border; | |
1079 | ||
1080 | return ret; | |
1081 | } | |
1082 | ||
1083 | #endif // wxUSE_STATBOX | |
1084 | ||
1085 | //--------------------------------------------------------------------------- | |
1086 | // wxNotebookSizer | |
1087 | //--------------------------------------------------------------------------- | |
1088 | ||
1089 | #if wxUSE_NOTEBOOK | |
1090 | ||
1091 | wxNotebookSizer::wxNotebookSizer( wxNotebook *nb ) | |
1092 | { | |
1093 | wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") ); | |
1094 | ||
1095 | m_notebook = nb; | |
1096 | } | |
1097 | ||
1098 | void wxNotebookSizer::RecalcSizes() | |
1099 | { | |
1100 | m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); | |
1101 | } | |
1102 | ||
1103 | wxSize wxNotebookSizer::CalcMin() | |
1104 | { | |
1105 | wxSize sizeBorder = m_notebook->CalcSizeFromPage(wxSize(0, 0)); | |
1106 | ||
1107 | sizeBorder.x += 5; | |
1108 | sizeBorder.y += 5; | |
1109 | ||
1110 | if (m_notebook->GetChildren().GetCount() == 0) | |
1111 | { | |
1112 | return wxSize(sizeBorder.x + 10, sizeBorder.y + 10); | |
1113 | } | |
1114 | ||
1115 | int maxX = 0; | |
1116 | int maxY = 0; | |
1117 | ||
1118 | wxWindowList::Node *node = m_notebook->GetChildren().GetFirst(); | |
1119 | while (node) | |
1120 | { | |
1121 | wxWindow *item = node->GetData(); | |
1122 | wxSizer *itemsizer = item->GetSizer(); | |
1123 | ||
1124 | if (itemsizer) | |
1125 | { | |
1126 | wxSize subsize( itemsizer->CalcMin() ); | |
1127 | ||
1128 | if (subsize.x > maxX) | |
1129 | maxX = subsize.x; | |
1130 | if (subsize.y > maxY) | |
1131 | maxY = subsize.y; | |
1132 | } | |
1133 | ||
1134 | node = node->GetNext(); | |
1135 | } | |
1136 | ||
1137 | return wxSize( maxX, maxY ) + sizeBorder; | |
1138 | } | |
1139 | ||
1140 | #endif // wxUSE_NOTEBOOK |