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