| 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 | void wxSizerItem::DeleteWindows() |
| 236 | { |
| 237 | if (m_window) |
| 238 | m_window->Destroy(); |
| 239 | |
| 240 | if (m_sizer) |
| 241 | m_sizer->DeleteWindows(); |
| 242 | } |
| 243 | |
| 244 | bool wxSizerItem::IsWindow() |
| 245 | { |
| 246 | return (m_window != NULL); |
| 247 | } |
| 248 | |
| 249 | bool wxSizerItem::IsSizer() |
| 250 | { |
| 251 | return (m_sizer != NULL); |
| 252 | } |
| 253 | |
| 254 | bool wxSizerItem::IsSpacer() |
| 255 | { |
| 256 | return (m_window == NULL) && (m_sizer == NULL); |
| 257 | } |
| 258 | |
| 259 | //--------------------------------------------------------------------------- |
| 260 | // wxSizer |
| 261 | //--------------------------------------------------------------------------- |
| 262 | |
| 263 | wxSizer::wxSizer() |
| 264 | { |
| 265 | m_children.DeleteContents( TRUE ); |
| 266 | m_minSize.x = 0; |
| 267 | m_minSize.y = 0; |
| 268 | } |
| 269 | |
| 270 | wxSizer::~wxSizer() |
| 271 | { |
| 272 | Clear(); |
| 273 | } |
| 274 | |
| 275 | void wxSizer::Add( wxWindow *window, int option, int flag, int border, wxObject* userData ) |
| 276 | { |
| 277 | m_children.Append( new wxSizerItem( window, option, flag, border, userData ) ); |
| 278 | window->SetContainingSizer(this); |
| 279 | } |
| 280 | |
| 281 | void wxSizer::Add( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) |
| 282 | { |
| 283 | m_children.Append( new wxSizerItem( sizer, option, flag, border, userData ) ); |
| 284 | } |
| 285 | |
| 286 | void wxSizer::Add( int width, int height, int option, int flag, int border, wxObject* userData ) |
| 287 | { |
| 288 | m_children.Append( new wxSizerItem( width, height, option, flag, border, userData ) ); |
| 289 | } |
| 290 | |
| 291 | void wxSizer::Prepend( wxWindow *window, int option, int flag, int border, wxObject* userData ) |
| 292 | { |
| 293 | m_children.Insert( new wxSizerItem( window, option, flag, border, userData ) ); |
| 294 | window->SetContainingSizer(this); |
| 295 | } |
| 296 | |
| 297 | void wxSizer::Prepend( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) |
| 298 | { |
| 299 | m_children.Insert( new wxSizerItem( sizer, option, flag, border, userData ) ); |
| 300 | } |
| 301 | |
| 302 | void wxSizer::Prepend( int width, int height, int option, int flag, int border, wxObject* userData ) |
| 303 | { |
| 304 | m_children.Insert( new wxSizerItem( width, height, option, flag, border, userData ) ); |
| 305 | } |
| 306 | |
| 307 | void wxSizer::Insert( int before, wxWindow *window, int option, int flag, int border, wxObject* userData ) |
| 308 | { |
| 309 | m_children.Insert( before, new wxSizerItem( window, option, flag, border, userData ) ); |
| 310 | window->SetContainingSizer(this); |
| 311 | } |
| 312 | |
| 313 | void wxSizer::Insert( int before, wxSizer *sizer, int option, int flag, int border, wxObject* userData ) |
| 314 | { |
| 315 | m_children.Insert( before, new wxSizerItem( sizer, option, flag, border, userData ) ); |
| 316 | } |
| 317 | |
| 318 | void wxSizer::Insert( int before, int width, int height, int option, int flag, int border, wxObject* userData ) |
| 319 | { |
| 320 | m_children.Insert( before, new wxSizerItem( width, height, option, flag, border, userData ) ); |
| 321 | } |
| 322 | |
| 323 | bool wxSizer::Remove( wxWindow *window ) |
| 324 | { |
| 325 | wxASSERT( window ); |
| 326 | |
| 327 | wxNode *node = m_children.First(); |
| 328 | while (node) |
| 329 | { |
| 330 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 331 | if (item->GetWindow() == window) |
| 332 | { |
| 333 | item->GetWindow()->SetContainingSizer(NULL); |
| 334 | m_children.DeleteNode( node ); |
| 335 | return TRUE; |
| 336 | } |
| 337 | node = node->Next(); |
| 338 | } |
| 339 | |
| 340 | return FALSE; |
| 341 | } |
| 342 | |
| 343 | bool wxSizer::Remove( wxSizer *sizer ) |
| 344 | { |
| 345 | wxASSERT( sizer ); |
| 346 | |
| 347 | wxNode *node = m_children.First(); |
| 348 | while (node) |
| 349 | { |
| 350 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 351 | if (item->GetSizer() == sizer) |
| 352 | { |
| 353 | m_children.DeleteNode( node ); |
| 354 | return TRUE; |
| 355 | } |
| 356 | node = node->Next(); |
| 357 | } |
| 358 | |
| 359 | return FALSE; |
| 360 | } |
| 361 | |
| 362 | bool wxSizer::Remove( int pos ) |
| 363 | { |
| 364 | wxNode *node = m_children.Nth( pos ); |
| 365 | if (!node) return FALSE; |
| 366 | |
| 367 | m_children.DeleteNode( node ); |
| 368 | |
| 369 | return TRUE; |
| 370 | } |
| 371 | |
| 372 | void wxSizer::Clear( bool delete_windows ) |
| 373 | { |
| 374 | // First clear the ContainingSizer pointers |
| 375 | wxNode *node = m_children.First(); |
| 376 | while (node) |
| 377 | { |
| 378 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 379 | if (item->IsWindow()) |
| 380 | item->GetWindow()->SetContainingSizer(NULL); |
| 381 | node = node->Next(); |
| 382 | } |
| 383 | |
| 384 | // Destroy the windows if needed |
| 385 | if (delete_windows) |
| 386 | DeleteWindows(); |
| 387 | |
| 388 | // Now empty the list |
| 389 | m_children.Clear(); |
| 390 | } |
| 391 | |
| 392 | void wxSizer::DeleteWindows() |
| 393 | { |
| 394 | wxNode *node = m_children.First(); |
| 395 | while (node) |
| 396 | { |
| 397 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 398 | item->DeleteWindows(); |
| 399 | node = node->Next(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void wxSizer::Fit( wxWindow *window ) |
| 404 | { |
| 405 | wxSize size; |
| 406 | if (window->IsTopLevel()) |
| 407 | size = FitSize( window ); |
| 408 | else |
| 409 | size = GetMinWindowSize( window ); |
| 410 | |
| 411 | window->SetSize( size ); |
| 412 | } |
| 413 | |
| 414 | void wxSizer::Layout() |
| 415 | { |
| 416 | CalcMin(); |
| 417 | RecalcSizes(); |
| 418 | } |
| 419 | |
| 420 | void wxSizer::SetSizeHints( wxWindow *window ) |
| 421 | { |
| 422 | // Preserve the window's max size hints, but set the |
| 423 | // lower bound according to the sizer calculations. |
| 424 | |
| 425 | wxSize size = FitSize( window ); |
| 426 | window->SetSizeHints( size.x, |
| 427 | size.y, |
| 428 | window->GetMaxWidth(), |
| 429 | window->GetMaxHeight() ); |
| 430 | } |
| 431 | |
| 432 | wxSize wxSizer::GetMaxWindowSize( wxWindow *window ) |
| 433 | { |
| 434 | return window->GetMaxSize(); |
| 435 | } |
| 436 | |
| 437 | wxSize wxSizer::GetMinWindowSize( wxWindow *window ) |
| 438 | { |
| 439 | wxSize minSize( GetMinSize() ); |
| 440 | wxSize size( window->GetSize() ); |
| 441 | wxSize client_size( window->GetClientSize() ); |
| 442 | return wxSize( minSize.x+size.x-client_size.x, |
| 443 | minSize.y+size.y-client_size.y ); |
| 444 | } |
| 445 | |
| 446 | // Return a window size that will fit within the screens dimensions |
| 447 | wxSize wxSizer::FitSize( wxWindow *window ) |
| 448 | { |
| 449 | wxSize size = GetMinWindowSize( window ); |
| 450 | wxSize sizeMax = GetMaxWindowSize( window ); |
| 451 | |
| 452 | // Limit the size if sizeMax != wxDefaultSize |
| 453 | |
| 454 | if ( size.x > sizeMax.x && sizeMax.x != -1 ) |
| 455 | size.x = sizeMax.x; |
| 456 | if ( size.y > sizeMax.y && sizeMax.y != -1 ) |
| 457 | size.y = sizeMax.y; |
| 458 | |
| 459 | return size; |
| 460 | } |
| 461 | |
| 462 | void wxSizer::SetDimension( int x, int y, int width, int height ) |
| 463 | { |
| 464 | m_position.x = x; |
| 465 | m_position.y = y; |
| 466 | m_size.x = width; |
| 467 | m_size.y = height; |
| 468 | CalcMin(); |
| 469 | RecalcSizes(); |
| 470 | } |
| 471 | |
| 472 | wxSize wxSizer::GetMinSize() |
| 473 | { |
| 474 | wxSize ret( CalcMin() ); |
| 475 | if (ret.x < m_minSize.x) ret.x = m_minSize.x; |
| 476 | if (ret.y < m_minSize.y) ret.y = m_minSize.y; |
| 477 | return ret; |
| 478 | } |
| 479 | |
| 480 | void wxSizer::DoSetMinSize( int width, int height ) |
| 481 | { |
| 482 | m_minSize.x = width; |
| 483 | m_minSize.y = height; |
| 484 | } |
| 485 | |
| 486 | bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height ) |
| 487 | { |
| 488 | wxASSERT( window ); |
| 489 | |
| 490 | wxNode *node = m_children.First(); |
| 491 | while (node) |
| 492 | { |
| 493 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 494 | if (item->GetWindow() == window) |
| 495 | { |
| 496 | item->SetInitSize( width, height ); |
| 497 | return TRUE; |
| 498 | } |
| 499 | node = node->Next(); |
| 500 | } |
| 501 | |
| 502 | node = m_children.First(); |
| 503 | while (node) |
| 504 | { |
| 505 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 506 | if (item->GetSizer()) |
| 507 | { |
| 508 | /* It's a sizer, so lets search recursively. */ |
| 509 | if (item->GetSizer()->DoSetItemMinSize( window, width, height )) |
| 510 | { |
| 511 | /* A child sizer found the requested windw, exit. */ |
| 512 | return TRUE; |
| 513 | } |
| 514 | } |
| 515 | node = node->Next(); |
| 516 | } |
| 517 | |
| 518 | return FALSE; |
| 519 | } |
| 520 | |
| 521 | bool wxSizer::DoSetItemMinSize( wxSizer *sizer, int width, int height ) |
| 522 | { |
| 523 | wxASSERT( sizer ); |
| 524 | |
| 525 | wxNode *node = m_children.First(); |
| 526 | while (node) |
| 527 | { |
| 528 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 529 | if (item->GetSizer() == sizer) |
| 530 | { |
| 531 | item->GetSizer()->DoSetMinSize( width, height ); |
| 532 | return TRUE; |
| 533 | } |
| 534 | node = node->Next(); |
| 535 | } |
| 536 | |
| 537 | node = m_children.First(); |
| 538 | while (node) |
| 539 | { |
| 540 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 541 | if (item->GetSizer()) |
| 542 | { |
| 543 | /* It's a sizer, so lets search recursively. */ |
| 544 | if (item->GetSizer()->DoSetItemMinSize( sizer, width, height )) |
| 545 | { |
| 546 | /* A child sizer found the requested windw, exit. */ |
| 547 | return TRUE; |
| 548 | } |
| 549 | } |
| 550 | node = node->Next(); |
| 551 | } |
| 552 | |
| 553 | return FALSE; |
| 554 | } |
| 555 | |
| 556 | bool wxSizer::DoSetItemMinSize( int pos, int width, int height ) |
| 557 | { |
| 558 | wxNode *node = m_children.Nth( pos ); |
| 559 | if (!node) return FALSE; |
| 560 | |
| 561 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
| 562 | if (item->GetSizer()) |
| 563 | { |
| 564 | /* Sizers contains the minimal size in them, if not calculated ... */ |
| 565 | item->GetSizer()->DoSetMinSize( width, height ); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | /* ... whereas the minimal size of spacers and windows in stored |
| 570 | in the item */ |
| 571 | item->SetInitSize( width, height ); |
| 572 | } |
| 573 | |
| 574 | return TRUE; |
| 575 | } |
| 576 | |
| 577 | //--------------------------------------------------------------------------- |
| 578 | // wxGridSizer |
| 579 | //--------------------------------------------------------------------------- |
| 580 | |
| 581 | wxGridSizer::wxGridSizer( int rows, int cols, int vgap, int hgap ) |
| 582 | { |
| 583 | m_rows = rows; |
| 584 | m_cols = cols; |
| 585 | m_vgap = vgap; |
| 586 | m_hgap = hgap; |
| 587 | } |
| 588 | |
| 589 | wxGridSizer::wxGridSizer( int cols, int vgap, int hgap ) |
| 590 | { |
| 591 | m_rows = 0; |
| 592 | m_cols = cols; |
| 593 | m_vgap = vgap; |
| 594 | m_hgap = hgap; |
| 595 | } |
| 596 | |
| 597 | void wxGridSizer::RecalcSizes() |
| 598 | { |
| 599 | if (m_children.GetCount() == 0) |
| 600 | return; |
| 601 | |
| 602 | int nitems = m_children.GetCount(); |
| 603 | int nrows = m_rows; |
| 604 | int ncols = m_cols; |
| 605 | |
| 606 | if (ncols > 0) |
| 607 | nrows = (nitems + ncols-1) / ncols; |
| 608 | else |
| 609 | ncols = (nitems + nrows-1) / nrows; |
| 610 | |
| 611 | wxSize sz( GetSize() ); |
| 612 | wxPoint pt( GetPosition() ); |
| 613 | |
| 614 | int w = (sz.x - (ncols - 1) * m_hgap) / ncols; |
| 615 | int h = (sz.y - (nrows - 1) * m_vgap) / nrows; |
| 616 | |
| 617 | int x = pt.x; |
| 618 | for (int c = 0; c < ncols; c++) |
| 619 | { |
| 620 | int y = pt.y; |
| 621 | for (int r = 0; r < nrows; r++) |
| 622 | { |
| 623 | int i = r * ncols + c; |
| 624 | if (i < nitems) |
| 625 | { |
| 626 | wxNode *node = m_children.Nth( i ); |
| 627 | wxASSERT( node ); |
| 628 | |
| 629 | SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h); |
| 630 | } |
| 631 | y = y + h + m_vgap; |
| 632 | } |
| 633 | x = x + w + m_hgap; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | wxSize wxGridSizer::CalcMin() |
| 638 | { |
| 639 | if (m_children.GetCount() == 0) |
| 640 | return wxSize(10,10); |
| 641 | |
| 642 | int nitems = m_children.GetCount(); |
| 643 | int nrows = m_rows; |
| 644 | int ncols = m_cols; |
| 645 | |
| 646 | if (ncols > 0) |
| 647 | nrows = (nitems + ncols-1) / ncols; |
| 648 | else |
| 649 | ncols = (nitems + nrows-1) / nrows; |
| 650 | |
| 651 | /* Find the max width and height for any component */ |
| 652 | int w = 0; |
| 653 | int h = 0; |
| 654 | |
| 655 | wxNode *node = m_children.First(); |
| 656 | while (node) |
| 657 | { |
| 658 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 659 | wxSize sz( item->CalcMin() ); |
| 660 | w = wxMax( w, sz.x ); |
| 661 | h = wxMax( h, sz.y ); |
| 662 | |
| 663 | node = node->Next(); |
| 664 | } |
| 665 | |
| 666 | return wxSize(ncols * w + (ncols-1) * m_hgap, |
| 667 | nrows * h + (nrows-1) * m_vgap); |
| 668 | } |
| 669 | |
| 670 | void wxGridSizer::SetItemBounds( wxSizerItem *item, int x, int y, int w, int h ) |
| 671 | { |
| 672 | wxPoint pt( x,y ); |
| 673 | wxSize sz( item->CalcMin() ); |
| 674 | int flag = item->GetFlag(); |
| 675 | |
| 676 | if ((flag & wxEXPAND) || (flag & wxSHAPED)) |
| 677 | { |
| 678 | sz = wxSize(w, h); |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | if (flag & wxALIGN_CENTER_HORIZONTAL) |
| 683 | { |
| 684 | pt.x = x + (w - sz.x) / 2; |
| 685 | } |
| 686 | else if (flag & wxALIGN_RIGHT) |
| 687 | { |
| 688 | pt.x = x + (w - sz.x); |
| 689 | } |
| 690 | |
| 691 | if (flag & wxALIGN_CENTER_VERTICAL) |
| 692 | { |
| 693 | pt.y = y + (h - sz.y) / 2; |
| 694 | } |
| 695 | else if (flag & wxALIGN_BOTTOM) |
| 696 | { |
| 697 | pt.y = y + (h - sz.y); |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | item->SetDimension(pt, sz); |
| 702 | } |
| 703 | |
| 704 | //--------------------------------------------------------------------------- |
| 705 | // wxFlexGridSizer |
| 706 | //--------------------------------------------------------------------------- |
| 707 | |
| 708 | wxFlexGridSizer::wxFlexGridSizer( int rows, int cols, int vgap, int hgap ) |
| 709 | : wxGridSizer( rows, cols, vgap, hgap ) |
| 710 | { |
| 711 | m_rowHeights = (int*) NULL; |
| 712 | m_colWidths = (int*) NULL; |
| 713 | } |
| 714 | |
| 715 | wxFlexGridSizer::wxFlexGridSizer( int cols, int vgap, int hgap ) |
| 716 | : wxGridSizer( cols, vgap, hgap ) |
| 717 | { |
| 718 | m_rowHeights = (int*) NULL; |
| 719 | m_colWidths = (int*) NULL; |
| 720 | } |
| 721 | |
| 722 | wxFlexGridSizer::~wxFlexGridSizer() |
| 723 | { |
| 724 | if (m_rowHeights) |
| 725 | delete[] m_rowHeights; |
| 726 | if (m_colWidths) |
| 727 | delete[] m_colWidths; |
| 728 | } |
| 729 | |
| 730 | void wxFlexGridSizer::CreateArrays() |
| 731 | { |
| 732 | if (m_rowHeights) |
| 733 | delete[] m_rowHeights; |
| 734 | if (m_colWidths) |
| 735 | delete[] m_colWidths; |
| 736 | |
| 737 | if (m_children.GetCount() == 0) |
| 738 | return; |
| 739 | |
| 740 | int nitems = m_children.GetCount(); |
| 741 | int nrows = m_rows; |
| 742 | int ncols = m_cols; |
| 743 | |
| 744 | if (ncols > 0) |
| 745 | nrows = (nitems + ncols-1) / ncols; |
| 746 | else |
| 747 | ncols = (nitems + nrows-1) / nrows; |
| 748 | |
| 749 | m_rowHeights = new int[nrows]; |
| 750 | m_colWidths = new int[ncols]; |
| 751 | |
| 752 | for (int col = 0; col < ncols; col++) |
| 753 | m_colWidths[ col ] = 0; |
| 754 | for (int row = 0; row < nrows; row++) |
| 755 | m_rowHeights[ row ] = 0; |
| 756 | } |
| 757 | |
| 758 | void wxFlexGridSizer::RecalcSizes() |
| 759 | { |
| 760 | if (m_children.GetCount() == 0) |
| 761 | return; |
| 762 | |
| 763 | int nitems = m_children.GetCount(); |
| 764 | int nrows = m_rows; |
| 765 | int ncols = m_cols; |
| 766 | |
| 767 | if (ncols > 0) |
| 768 | nrows = (nitems + ncols-1) / ncols; |
| 769 | else |
| 770 | ncols = (nitems + nrows-1) / nrows; |
| 771 | |
| 772 | wxSize sz( GetSize() ); |
| 773 | wxSize minsz( CalcMin() ); |
| 774 | wxPoint pt( GetPosition() ); |
| 775 | int delta; |
| 776 | size_t idx; |
| 777 | |
| 778 | if ((m_growableRows.GetCount() > 0) && (sz.y > minsz.y)) |
| 779 | { |
| 780 | delta = (sz.y - minsz.y) / m_growableRows.GetCount(); |
| 781 | for (idx = 0; idx < m_growableRows.GetCount(); idx++) |
| 782 | m_rowHeights[ m_growableRows[idx] ] += delta; |
| 783 | } |
| 784 | |
| 785 | if ((m_growableCols.GetCount() > 0) && (sz.x > minsz.x)) |
| 786 | { |
| 787 | delta = (sz.x - minsz.x) / m_growableCols.GetCount(); |
| 788 | for (idx = 0; idx < m_growableCols.GetCount(); idx++) |
| 789 | m_colWidths[ m_growableCols[idx] ] += delta; |
| 790 | } |
| 791 | |
| 792 | sz = wxSize( pt.x + sz.x, pt.y + sz.y ); |
| 793 | |
| 794 | int x = pt.x; |
| 795 | for (int c = 0; c < ncols; c++) |
| 796 | { |
| 797 | int y = pt.y; |
| 798 | for (int r = 0; r < nrows; r++) |
| 799 | { |
| 800 | int i = r * ncols + c; |
| 801 | if (i < nitems) |
| 802 | { |
| 803 | wxNode *node = m_children.Nth( i ); |
| 804 | wxASSERT( node ); |
| 805 | |
| 806 | int w = wxMax( 0, wxMin( m_colWidths[c], sz.x - x ) ); |
| 807 | int h = wxMax( 0, wxMin( m_rowHeights[r], sz.y - y ) ); |
| 808 | |
| 809 | SetItemBounds( (wxSizerItem*) node->Data(), x, y, w, h); |
| 810 | } |
| 811 | y = y + m_rowHeights[r] + m_vgap; |
| 812 | } |
| 813 | x = x + m_colWidths[c] + m_hgap; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | wxSize wxFlexGridSizer::CalcMin() |
| 818 | { |
| 819 | if (m_children.GetCount() == 0) |
| 820 | return wxSize(10,10); |
| 821 | |
| 822 | int nitems = m_children.GetCount(); |
| 823 | int nrows = m_rows; |
| 824 | int ncols = m_cols; |
| 825 | |
| 826 | if (ncols > 0) |
| 827 | nrows = (nitems + ncols-1) / ncols; |
| 828 | else |
| 829 | ncols = (nitems + nrows-1) / nrows; |
| 830 | |
| 831 | CreateArrays(); |
| 832 | |
| 833 | int col; |
| 834 | int row; |
| 835 | |
| 836 | int i = 0; |
| 837 | wxNode *node = m_children.First(); |
| 838 | while (node) |
| 839 | { |
| 840 | wxSizerItem *item = (wxSizerItem*)node->Data(); |
| 841 | wxSize sz( item->CalcMin() ); |
| 842 | row = i / ncols; |
| 843 | col = i % ncols; |
| 844 | m_rowHeights[ row ] = wxMax( sz.y, m_rowHeights[ row ] ); |
| 845 | m_colWidths[ col ] = wxMax( sz.x, m_colWidths[ col ] ); |
| 846 | |
| 847 | node = node->Next(); |
| 848 | i++; |
| 849 | } |
| 850 | |
| 851 | int width = 0; |
| 852 | for (col = 0; col < ncols; col++) |
| 853 | width += m_colWidths[ col ]; |
| 854 | |
| 855 | int height = 0; |
| 856 | for (row = 0; row < nrows; row++) |
| 857 | height += m_rowHeights[ row ]; |
| 858 | |
| 859 | return wxSize( width + (ncols-1) * m_hgap, |
| 860 | height + (nrows-1) * m_vgap); |
| 861 | } |
| 862 | |
| 863 | void wxFlexGridSizer::AddGrowableRow( size_t idx ) |
| 864 | { |
| 865 | m_growableRows.Add( idx ); |
| 866 | } |
| 867 | |
| 868 | void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx) ) |
| 869 | { |
| 870 | } |
| 871 | |
| 872 | void wxFlexGridSizer::AddGrowableCol( size_t idx ) |
| 873 | { |
| 874 | m_growableCols.Add( idx ); |
| 875 | } |
| 876 | |
| 877 | void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx) ) |
| 878 | { |
| 879 | } |
| 880 | |
| 881 | //--------------------------------------------------------------------------- |
| 882 | // wxBoxSizer |
| 883 | //--------------------------------------------------------------------------- |
| 884 | |
| 885 | wxBoxSizer::wxBoxSizer( int orient ) |
| 886 | { |
| 887 | m_orient = orient; |
| 888 | } |
| 889 | |
| 890 | void wxBoxSizer::RecalcSizes() |
| 891 | { |
| 892 | if (m_children.GetCount() == 0) |
| 893 | return; |
| 894 | |
| 895 | int delta = 0; |
| 896 | int extra = 0; |
| 897 | if (m_stretchable) |
| 898 | { |
| 899 | if (m_orient == wxHORIZONTAL) |
| 900 | { |
| 901 | delta = (m_size.x - m_fixedWidth) / m_stretchable; |
| 902 | extra = (m_size.x - m_fixedWidth) % m_stretchable; |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | delta = (m_size.y - m_fixedHeight) / m_stretchable; |
| 907 | extra = (m_size.y - m_fixedHeight) % m_stretchable; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | wxPoint pt( m_position ); |
| 912 | |
| 913 | wxNode *node = m_children.GetFirst(); |
| 914 | while (node) |
| 915 | { |
| 916 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
| 917 | |
| 918 | int weight = 1; |
| 919 | if (item->GetOption()) |
| 920 | weight = item->GetOption(); |
| 921 | |
| 922 | wxSize size( item->CalcMin() ); |
| 923 | |
| 924 | if (m_orient == wxVERTICAL) |
| 925 | { |
| 926 | wxCoord height = size.y; |
| 927 | if (item->GetOption()) |
| 928 | { |
| 929 | height = (delta * weight) + extra; |
| 930 | extra = 0; // only the first item will get the remainder as extra size |
| 931 | } |
| 932 | |
| 933 | wxPoint child_pos( pt ); |
| 934 | wxSize child_size( wxSize( size.x, height) ); |
| 935 | |
| 936 | if (item->GetFlag() & (wxEXPAND | wxSHAPED)) |
| 937 | child_size.x = m_size.x; |
| 938 | else if (item->GetFlag() & wxALIGN_RIGHT) |
| 939 | child_pos.x += m_size.x - size.x; |
| 940 | else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_HORIZONTAL)) |
| 941 | // XXX wxCENTER is added for backward compatibility; |
| 942 | // wxALIGN_CENTER should be used in new code |
| 943 | child_pos.x += (m_size.x - size.x) / 2; |
| 944 | |
| 945 | item->SetDimension( child_pos, child_size ); |
| 946 | |
| 947 | pt.y += height; |
| 948 | } |
| 949 | else |
| 950 | { |
| 951 | wxCoord width = size.x; |
| 952 | if (item->GetOption()) |
| 953 | { |
| 954 | width = (delta * weight) + extra; |
| 955 | extra = 0; // only the first item will get the remainder as extra size |
| 956 | } |
| 957 | |
| 958 | wxPoint child_pos( pt ); |
| 959 | wxSize child_size( wxSize(width, size.y) ); |
| 960 | |
| 961 | if (item->GetFlag() & (wxEXPAND | wxSHAPED)) |
| 962 | child_size.y = m_size.y; |
| 963 | else if (item->GetFlag() & wxALIGN_BOTTOM) |
| 964 | child_pos.y += m_size.y - size.y; |
| 965 | else if (item->GetFlag() & (wxCENTER | wxALIGN_CENTER_VERTICAL)) |
| 966 | // XXX wxCENTER is added for backward compatibility; |
| 967 | // wxALIGN_CENTER should be used in new code |
| 968 | child_pos.y += (m_size.y - size.y) / 2; |
| 969 | |
| 970 | item->SetDimension( child_pos, child_size ); |
| 971 | |
| 972 | pt.x += width; |
| 973 | } |
| 974 | |
| 975 | node = node->Next(); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | wxSize wxBoxSizer::CalcMin() |
| 980 | { |
| 981 | if (m_children.GetCount() == 0) |
| 982 | return wxSize(10,10); |
| 983 | |
| 984 | m_stretchable = 0; |
| 985 | m_minWidth = 0; |
| 986 | m_minHeight = 0; |
| 987 | m_fixedWidth = 0; |
| 988 | m_fixedHeight = 0; |
| 989 | |
| 990 | // Find how long each stretch unit needs to be |
| 991 | int stretchSize = 1; |
| 992 | wxNode *node = m_children.GetFirst(); |
| 993 | while (node) |
| 994 | { |
| 995 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
| 996 | if (item->GetOption() != 0) |
| 997 | { |
| 998 | int stretch = item->GetOption(); |
| 999 | wxSize size( item->CalcMin() ); |
| 1000 | int sizePerStretch; |
| 1001 | // Integer division rounded up is (a + b - 1) / b |
| 1002 | if (m_orient == wxHORIZONTAL) |
| 1003 | sizePerStretch = ( size.x + stretch - 1 ) / stretch; |
| 1004 | else |
| 1005 | sizePerStretch = ( size.y + stretch - 1 ) / stretch; |
| 1006 | if (sizePerStretch > stretchSize) |
| 1007 | stretchSize = sizePerStretch; |
| 1008 | } |
| 1009 | node = node->Next(); |
| 1010 | } |
| 1011 | // Calculate overall minimum size |
| 1012 | node = m_children.GetFirst(); |
| 1013 | while (node) |
| 1014 | { |
| 1015 | wxSizerItem *item = (wxSizerItem*) node->Data(); |
| 1016 | |
| 1017 | m_stretchable += item->GetOption(); |
| 1018 | |
| 1019 | wxSize size( item->CalcMin() ); |
| 1020 | if (item->GetOption() != 0) |
| 1021 | { |
| 1022 | if (m_orient == wxHORIZONTAL) |
| 1023 | size.x = stretchSize * item->GetOption(); |
| 1024 | else |
| 1025 | size.y = stretchSize * item->GetOption(); |
| 1026 | } |
| 1027 | |
| 1028 | if (m_orient == wxHORIZONTAL) |
| 1029 | { |
| 1030 | m_minWidth += size.x; |
| 1031 | m_minHeight = wxMax( m_minHeight, size.y ); |
| 1032 | } |
| 1033 | else |
| 1034 | { |
| 1035 | m_minHeight += size.y; |
| 1036 | m_minWidth = wxMax( m_minWidth, size.x ); |
| 1037 | } |
| 1038 | |
| 1039 | if (item->GetOption() == 0) |
| 1040 | { |
| 1041 | if (m_orient == wxVERTICAL) |
| 1042 | { |
| 1043 | m_fixedHeight += size.y; |
| 1044 | m_fixedWidth = wxMax( m_fixedWidth, size.x ); |
| 1045 | } |
| 1046 | else |
| 1047 | { |
| 1048 | m_fixedWidth += size.x; |
| 1049 | m_fixedHeight = wxMax( m_fixedHeight, size.y ); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | node = node->Next(); |
| 1054 | } |
| 1055 | |
| 1056 | return wxSize( m_minWidth, m_minHeight ); |
| 1057 | } |
| 1058 | |
| 1059 | //--------------------------------------------------------------------------- |
| 1060 | // wxStaticBoxSizer |
| 1061 | //--------------------------------------------------------------------------- |
| 1062 | |
| 1063 | #if wxUSE_STATBOX |
| 1064 | |
| 1065 | wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox *box, int orient ) |
| 1066 | : wxBoxSizer( orient ) |
| 1067 | { |
| 1068 | wxASSERT_MSG( box, wxT("wxStaticBoxSizer needs a static box") ); |
| 1069 | |
| 1070 | m_staticBox = box; |
| 1071 | } |
| 1072 | |
| 1073 | static void GetStaticBoxBorders(wxStaticBox *box, |
| 1074 | int *borderTop, int *borderOther) |
| 1075 | { |
| 1076 | // this has to be done platform by platform as there is no way to |
| 1077 | // guess the thickness of a wxStaticBox border |
| 1078 | #ifdef __WXGTK__ |
| 1079 | if ( box->GetLabel().IsEmpty() ) |
| 1080 | *borderTop = 5; |
| 1081 | else |
| 1082 | #endif // __WXGTK__ |
| 1083 | *borderTop = 15; |
| 1084 | (void)box; |
| 1085 | *borderOther = 5; |
| 1086 | } |
| 1087 | |
| 1088 | void wxStaticBoxSizer::RecalcSizes() |
| 1089 | { |
| 1090 | int top_border, other_border; |
| 1091 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); |
| 1092 | |
| 1093 | m_staticBox->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); |
| 1094 | |
| 1095 | wxPoint old_pos( m_position ); |
| 1096 | m_position.x += other_border; |
| 1097 | m_position.y += top_border; |
| 1098 | wxSize old_size( m_size ); |
| 1099 | m_size.x -= 2*other_border; |
| 1100 | m_size.y -= top_border + other_border; |
| 1101 | |
| 1102 | wxBoxSizer::RecalcSizes(); |
| 1103 | |
| 1104 | m_position = old_pos; |
| 1105 | m_size = old_size; |
| 1106 | } |
| 1107 | |
| 1108 | wxSize wxStaticBoxSizer::CalcMin() |
| 1109 | { |
| 1110 | int top_border, other_border; |
| 1111 | GetStaticBoxBorders(m_staticBox, &top_border, &other_border); |
| 1112 | |
| 1113 | wxSize ret( wxBoxSizer::CalcMin() ); |
| 1114 | ret.x += 2*other_border; |
| 1115 | ret.y += other_border + top_border; |
| 1116 | |
| 1117 | return ret; |
| 1118 | } |
| 1119 | |
| 1120 | #endif // wxUSE_STATBOX |
| 1121 | |
| 1122 | //--------------------------------------------------------------------------- |
| 1123 | // wxNotebookSizer |
| 1124 | //--------------------------------------------------------------------------- |
| 1125 | |
| 1126 | #if wxUSE_NOTEBOOK |
| 1127 | |
| 1128 | wxNotebookSizer::wxNotebookSizer( wxNotebook *nb ) |
| 1129 | { |
| 1130 | wxASSERT_MSG( nb, wxT("wxNotebookSizer needs a notebook") ); |
| 1131 | |
| 1132 | m_notebook = nb; |
| 1133 | } |
| 1134 | |
| 1135 | void wxNotebookSizer::RecalcSizes() |
| 1136 | { |
| 1137 | m_notebook->SetSize( m_position.x, m_position.y, m_size.x, m_size.y ); |
| 1138 | } |
| 1139 | |
| 1140 | wxSize wxNotebookSizer::CalcMin() |
| 1141 | { |
| 1142 | wxSize sizeBorder = m_notebook->CalcSizeFromPage(wxSize(0, 0)); |
| 1143 | |
| 1144 | sizeBorder.x += 5; |
| 1145 | sizeBorder.y += 5; |
| 1146 | |
| 1147 | if (m_notebook->GetChildren().GetCount() == 0) |
| 1148 | { |
| 1149 | return wxSize(sizeBorder.x + 10, sizeBorder.y + 10); |
| 1150 | } |
| 1151 | |
| 1152 | int maxX = 0; |
| 1153 | int maxY = 0; |
| 1154 | |
| 1155 | wxWindowList::Node *node = m_notebook->GetChildren().GetFirst(); |
| 1156 | while (node) |
| 1157 | { |
| 1158 | wxWindow *item = node->GetData(); |
| 1159 | wxSizer *itemsizer = item->GetSizer(); |
| 1160 | |
| 1161 | if (itemsizer) |
| 1162 | { |
| 1163 | wxSize subsize( itemsizer->CalcMin() ); |
| 1164 | |
| 1165 | if (subsize.x > maxX) |
| 1166 | maxX = subsize.x; |
| 1167 | if (subsize.y > maxY) |
| 1168 | maxY = subsize.y; |
| 1169 | } |
| 1170 | |
| 1171 | node = node->GetNext(); |
| 1172 | } |
| 1173 | |
| 1174 | return wxSize( maxX, maxY ) + sizeBorder; |
| 1175 | } |
| 1176 | |
| 1177 | #endif // wxUSE_NOTEBOOK |
| 1178 | |
| 1179 | // vi:sts=4:sw=4:et |