1/* 2 * Copyright 2010 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Alex Wilson, yourpalal2@gmail.com 7 * 8 * Corresponds to: 9 * headers/os/interface/Layout.h rev 38207 10 * src/kits/interface/Layout.cpp rev 38207 11 */ 12 13 14/*! 15 \file Layout.h 16 \ingroup layout 17 \ingroup libbe 18 \brief Defines the BLayout class. 19*/ 20 21 22/*! 23 \class BLayout 24 \ingroup layout 25 \ingroup libbe 26 \brief The BLayout class provides an interface, and some basic 27 implementation to manage the positioning and sizing of BLayoutItem s. 28 29 BLayouts can be attached to a BView, managing the BLayoutItem's and 30 BView's that reside in that view, or can be nested within another 31 BLayout as a BLayoutItem. 32 33 Before adding a BLayoutItem to a BLayout, that layout must have a target 34 view. When a BLayout is attached directly to a BView via BView::SetLayout() 35 then that BView becomes the target of the layout. When a BLayout is nested 36 in another BLayout (via BLayout::AddItem()) the nested BLayout inherits the 37 target of the layout its nested in, if it does not have a target already. 38 You can retrieve the target view for a layout with the TargetView() method. 39 When adding a BLayoutItem to a BLayout, the item's view (as returned by 40 BLayoutItem::View()) is added to the BLayout's target view. 41 42\code 43BView* topView = new BGroupView(); 44BLayout* topLayout = topView->GetLayout(); 45 46BLayout* nestedLayout = new BGroupLayout(B_HORIZONTAL); 47topLayout->AddItem(nestedLayout); 48 49BLayout* veryNestedLayout = new BGroupLayout(B_VERTICAL); 50nestedLayout->AddItem(veryNestedLayout); 51\endcode 52 53 After executing this code, \c veryNestedLayout, \c nestedLayout, and 54 \c topLayout all have the same target view: \c topView. 55 56 Continuing with the same objects... 57 58\code 59BLayout* nestedLayoutWithView = (new BGroupView())->GetLayout(); 60topLayout->AddItem(nestedLayoutWithView); 61\endcode 62 63 \c nestedLayoutWithView does have a target view of \c topView. This is 64 because \c nestedLayoutWithView is attached directly to a BView. 65 66 \warning This class is not yet finalized, if you use it in your software 67 assume that it will break some time in the future. 68 69 \since Haiku R1 70*/ 71 72 73/*! 74 \fn BLayout::BLayout() 75 \brief Default constructor. 76 77 After this constructor has finished, this BLayout holds no 78 BLayoutItem's and does not have a target BView. 79 80 \warning Because a new BLayout does not have a target BView, calls to the 81 AddItem() and AddView() methods will fail. 82 83 \since Haiku R1 84*/ 85 86 87/*! 88 \fn BLayout::BLayout(BMessage* archive) 89 \brief Archive constructor. 90 91 \param archive The archive message. 92 93 \since Haiku R1 94*/ 95 96 97/*! 98 \fn BLayout::~BLayout() 99 \brief Destructor, deletes all BLayoutItem's that this layout manages, 100 and detaches from this BLayout's owner view if there is one. 101 102 Each BLayoutItem's BView (as returned by BLayoutItem::View()) is also 103 removed from their parent. 104 105 \note Because nested BLayout's are treated as BLayoutItem's, 106 any layouts nested in this BLayout will be deleted. 107 108 \since Haiku R1 109*/ 110 111 112/*! 113 \name BView Targeting and Attachment Information 114*/ 115 116 117//! @{ 118 119 120/*! 121 \fn BView* BLayout::Owner() const 122 \brief Returns the Owner of this layout, i.e. the view this layout manages. 123 124 \since Haiku R1 125*/ 126 127 128/*! 129 \fn BView* BLayout::TargetView() const 130 \brief Returns the target view of this layout. 131 132 The target view of a layout becomes the parent of any BView's in this 133 layout, as well as the BView's returned by BLayoutItem::View() for 134 each BLayoutItem in this layout. 135 136 \since Haiku R1 137*/ 138 139 140/*! 141 \fn BView* BLayout::View() 142 \brief Returns the same BView* as BLayout::Owner(), this method is 143 inherited from BLayoutItem. 144 145 \since Haiku R1 146*/ 147 148 149//! @} 150 151 152/*! 153 \name Adding, Removing, Counting and Accessing Children 154*/ 155 156 157//! @{ 158 159 160/*! 161 \fn BLayoutItem* BLayout::AddView(BView* child) 162 \brief Creates a BLayoutItem to represent a BView, and adds that item to 163 this layout. 164 165 \a child is added to this BLayout's target view. 166 167 \returns The BLayoutItem created to represent \a child, or \c NULL if there 168 was an error. 169 170 \param child The BView to be added to this BLayout. 171 172 \since Haiku R1 173*/ 174 175 176/*! 177 \fn BLayoutItem* BLayout::AddView(int32 index, BView* child) 178 \brief Creates a BLayoutItem to represent \a child, and adds that item at 179 \a index to this layout. \a child is added to this BLayout's target view. 180 181 \since Haiku R1 182*/ 183 184 185/*! 186 \fn bool BLayout::AddItem(BLayoutItem* item) 187 \brief Adds a BLayoutItem to this layout, and adds the BView it represents 188 to this BLayout's target view. 189 190 \param item The BLayoutItem to be added. 191 192 \return \c true if the item was added, \c false otherwise. 193 194 \since Haiku R1 195*/ 196 197 198/*! 199 \fn bool BLayout::AddItem(int32 index, BLayoutItem* item) 200 \brief Adds \a item to this layout, and adds the BView \a item represents 201 to this BLayout's target view. 202 203 \param item The BLayoutItem to be added. 204 \param index The index at which to add \c item. 205 206 If \a index is out of bounds, \a item will be added at the end. If \a index 207 is somewhere between the first and last indices, then items from \a index 208 to the end will be shuffled over by one. 209 210 \return \c true if the item was added, \c false otherwise. 211 212 \since Haiku R1 213*/ 214 215 216/*! 217 \fn bool BLayout::RemoveView(BView* child) 218 \brief Removes and deletes all BLayoutItem representing a BView from 219 this layout. 220 221 \param child The BView to be removed. 222 223 \return \c true if the item was removed, \c false otherwise. 224 225 \since Haiku R1 226*/ 227 228 229/*! 230 \fn bool BLayout::RemoveItem(BLayoutItem* item) 231 \brief Removes a BLayoutItem from this layout, and also removes the view 232 it represents from this BLayout's target view. 233 234 \param item The BLayoutItem to be removed 235 236 \warning \a item is not deleted, you must delete it manually, or add it to 237 another BLayout. 238 \warning \a item->View(), even when it is removed from the target view, 239 is not deleted. If you want it deleted, you must delete it yourself! 240 241 \return \c true if the item was removed, \c false otherwise. 242 243 \since Haiku R1 244*/ 245 246 247/*! 248 \fn BLayoutItem* BLayout::RemoveItem(int32 index) 249 \brief Remove the BLayoutItem at \a index. 250 251 \see RemoveItem(BLayoutItem*) 252 253 \returns The BLayoutItem that was removed. 254 255 \since Haiku R1 256*/ 257 258 259/*! 260 \fn BLayoutItem* BLayout::ItemAt(int32 index) const 261 \brief Get the BLayoutItem at \a index. Returns \c NULL if \a index is 262 out of bounds. 263 264 \since Haiku R1 265*/ 266 267 268/*! 269 \fn int32 BLayout::CountItems() const 270 \brief Get the number of BLayoutItem s in this layout. 271 272 \since Haiku R1 273*/ 274 275 276/*! 277 \fn int32 BLayout::IndexOfItem(const BLayoutItem* item) const 278 \brief Get the index of a BLayoutItem in this layout. 279 280 \param item The BLayoutItem whose index you want. 281 282 \retval -1 \a item was not found in this BLayout. 283 284 \since Haiku R1 285*/ 286 287 288/*! 289 \fn int32 BLayout::IndexOfView(BView* child) const 290 \brief Get the index of \a child in this layout. 291 292 \note This finds the index of views added through BLayout::AddView(), not 293 the index of an item which represents \a child that was added through 294 BLayout::AddItem(). 295 296 \since Haiku R1 297*/ 298 299 300//! @} 301 302 303/*! 304 \name Subclass Helpers 305 306 These methods are meant to ease the development of BLayout 307 subclasses. 308*/ 309 310 311//! @{ 312 313 314/*! 315 \fn bool BLayout::AncestorsVisible() const 316 \brief Get the visibility of the ancestors of this layout. 317 318 If a BLayout is connected to a BView, this will always return \c true. 319 If a BLayout is nested in another layout (it was passed to AddItem()), then 320 this will reflect the visibility of this BLayout's parent layout. If 321 any layout is hidden (by BLayout::SetVisible()) between this layout and its 322 target BView's layout, then this method will return \c false. 323 324 \since Haiku R1 325*/ 326 327 328/*! 329 \fn BRect BLayout::LayoutArea() 330 \brief Returns the on-screen area this layout has received to lay out its 331 items in. 332 333 The return value is in the coordinate space of this BLayout's target 334 view. If this BLayout is attached directly to a BView, then 335 <tt> LayoutArea().LeftTop() == B_ORIGIN </tt>. 336 337 \since Haiku R1 338*/ 339 340 341/*! 342 \fn void BLayout::VisibilityChanged(bool show) 343 \brief Method to be called by derived classes in their SetVisible() 344 implementation. Calls AncestorVisibilityChanged() on the items in this 345 BLayout. 346 347 \param show \c true to show, \c false to hide. 348 349 \since Haiku R1 350*/ 351 352 353//! @} 354 355 356/*! 357 \name Methods Triggering or Related to Laying Out the BLayout 358*/ 359 360 361//! @{ 362 363 364/*! 365 \fn void BLayout::Relayout(bool immediate = false) 366 \brief Request this BLayout to reposition and resize its items as required. 367 368 If \a immediate is \c false, and there is already a request to have the 369 window this layout resides in re-laid-out, then the layout will happen at 370 that time. If \a immediate is \c true, and there is no such pending 371 request, nor is this BLayout's parent layout in the process of laying 372 out its items, then this BLayout will now layout its items. 373 374 \param immediate Whether or not to Relayout immediately or wait for pending 375 requests first. 376 377 \since Haiku R1 378*/ 379 380 381/*! 382 \fn void BLayout::LayoutItems(bool force = false) 383 \brief If there is no layout currently ongoing, and \a force is \c false, 384 creates a new BLayoutContext and calls the DoLayout() method 385 of this BLayout and any BLayout's nested in this BLayout. 386 387 This method also guarantees that the owner view of this layout (as returned 388 by BLayout::Owner()) performs a layout as well (if it is suitable to do so). 389 390 \param force Force the LayoutItems. 391 392 \since Haiku R1 393*/ 394 395 396/*! 397 \fn BLayoutContext* BLayout::LayoutContext() const 398 \brief Returns the BLayoutContext this BLayout is currently operating in, 399 or \c NULL. 400 401 \since Haiku R1 402*/ 403 404 405//! @} 406 407 408/*! 409 \name Invalidation and State Mutators and Accessors 410*/ 411 412 413//! @{ 414 415 416/*! 417 \fn void BLayout::RequireLayout() 418 \brief Flag this layout as stale, i.e. any cached data may still be valid, 419 but the items need to be repositioned or resized. 420 421 \since Haiku R1 422*/ 423 424 425/*! 426 \fn void BLayout::InvalidateLayout(bool children = false) 427 \brief Invalidate this layout and any cached data this layout has relating 428 to positioning and sizing of its items. 429 430 Invalidating a BLayout also invalidates the view it is connected to 431 (if there is one) and the BLayout this layout (or this BLayout's view) 432 resides in. 433 434 Although this method is virtual, you should not override it, override the 435 BLayout::LayoutInvalidated() hook instead. 436 437 This method should be called whenever the layout becomes invalid. This might 438 happen if the size constraints of an item in this layout change, this layout 439 is given more or less space than it previously had, or an object in this 440 layout has had its InvalidateLayout() method called. 441 442 \sa BView::InvalidateLayout(), BLayoutItem::InvalidateLayout(), 443 BLayout::LayoutInvalidated() 444 445 \since Haiku R1 446*/ 447 448 449/*! 450 \fn bool BLayout::IsValid() 451 \brief Returns whether this layout has been invalidated (via 452 BLayout::InvalidateLayout()) and has not yet been validated 453 (by doing a layout, or by its ResetLayoutInvalidation() method. 454 455 \since Haiku R1 456*/ 457 458 459/*! 460 \fn void BLayout::EnableLayoutInvalidation() 461 \brief Re-enable layout invalidation after a call to 462 DisableLayoutInvalidation(). 463 464 \since Haiku R1 465*/ 466 467 468/*! 469 \fn void BLayout::DisableLayoutInvalidation() 470 \brief Disable layout invalidation notifications, i.e. calls to 471 this object's InvalidateLayout() method. 472 473 \since Haiku R1 474*/ 475 476 477/*! 478 \fn void BLayout::ResetLayoutInvalidation() 479 \brief Reset layout invalidation, causing InvalidateLayout calls to proceed 480 again. This method should be called once any cached data has been 481 validated, or updated to valid values. 482 483 \since Haiku R1 484*/ 485 486 487//! @} 488 489 490/*! 491 \name Archiving 492 493 These methods relate to the archiving or unarchiving of this object 494 and the BLayoutItem's it contains 495 496 \since Haiku R1 497*/ 498 499 500//! @{ 501 502 503/*! 504 \fn status_t BLayout::Archive(BMessage* archive, bool deep = true) const 505 \brief Archives this layout into \a archive. If deep is true, also archives 506 the items in this layout, calling ItemArchived() for each one. 507 508 \since Haiku R1 509*/ 510 511 512/*! 513 \fn status_t BLayout::AllUnarchived(const BMessage* from) 514 \brief Unarchives the BLayoutItem's for this layout, calling 515 ItemUnarchived() for each one. 516 517 \since Haiku R1 518*/ 519 520 521/*! 522 \fn status_t BLayout::ItemArchived(BMessage* into, BLayoutItem* item, 523 int32 index) const 524 \brief Hook for derived classes to add data specific to \a item to the 525 \a into BMessage. \a item resides at \a index. 526 527 \note The same archive is passed to BLayout::ItemArchived() for all items, 528 so any data added for each item will be stored in an array. 529 530 \since Haiku R1 531*/ 532 533 534/*! 535 \fn status_t BLayout::ItemUnarchived(const BMessage* from, 536 BLayoutItem* item, int32 index) 537 \brief Hook for derived classes to retrieve data specific to \a item from 538 the \a from BMessage. \a item resides at \a index. 539 540 \note The same archive is passed to BLayout::ItemArchived() for all items, 541 so any data added for each item will be stored in an array. You 542 should pass \a index to the BMessage methods you will be using in 543 this method. 544 545 \since Haiku R1 546*/ 547 548 549//! @} 550 551 552/*! 553 \name Hook Methods 554*/ 555 556 557//! @{ 558 559 560/*! 561 \fn bool BLayout::ItemAdded(BLayoutItem* item, int32 atIndex) 562 \brief Hook method called when \a item is added to this layout. 563 564 \param item The BLayoutItem that is being added. 565 \param atIndex The index of the BLayoutItem. 566 567 \return \c true on succcess, false if \a item will not be added. 568 569 \note This is a good time to allocate data for a BLayoutItem and attach it 570 to \a item via BLayoutItem::SetLayoutData(). 571 572 \since Haiku R1 573*/ 574 575 576/*! 577 \fn void BLayout::ItemRemoved(BLayoutItem* item, int32 fromIndex) 578 \brief Hook method called when \a item is removed from this layout. 579 580 \param item The BLayoutItem being removed. 581 \param fromIndex The index where \a item used to reside. 582 583 When this hook is called, \a item is not yet completely removed. It can 584 no longer be accessed with LayoutItemAt(), nor does it contribute to the 585 value of CountItems(), but the item has not yet had its ItemDetached() 586 hook called. 587 588 \note This is a good time to delete the data you've attached to \a item 589 via BLayoutItem::SetLayoutData(). 590 591 \since Haiku R1 592*/ 593 594 595/*! 596 \fn void BLayout::DoLayout() = 0 597 \brief Implemented by derived classes to position and resize the items in 598 this layout. 599 600 \since Haiku R1 601*/ 602 603 604/*! 605 \fn void BLayout::LayoutInvalidated(bool children) 606 607 Hook method called when this layout becomes invalid. This is a good place 608 to clear any caches your object might hold. 609 610 \param children Whether or not child layouts have also been invalidated. 611 612 \since Haiku R1 613*/ 614 615 616/*! 617 \fn void BLayout::OwnerChanged(BView* was) 618 \brief Hook method called when this layout is attached to a BView. 619 620 \param was The previous owner of this BLayout, for new BLayout s, this 621 will be \c NULL. 622 623 \since Haiku R1 624*/ 625 626 627/*! 628 \fn void BLayout::AttachedToLayout() 629 \brief Hook method inherited from BLayoutItem, classes derived from 630 BLayout must include the BLayout version of this method in their 631 implementation. 632 633 \since Haiku R1 634*/ 635 636 637/*! 638 \fn void BLayout::DetachedFromLayout(BLayout* layout) 639 \brief Hook method inherited from BLayoutItem, classes derived from 640 BLayout must include the BLayout version of this method in their 641 implementation. 642 643 \param layout The BLayout that this BLayout was detached from. 644 645 \since Haiku R1 646*/ 647 648 649/*! 650 \fn void BLayout::AncestorVisibilityChanged(bool shown) 651 \brief Hook method inherited from BLayoutItem, classes derived from 652 BLayout must include the BLayout version of this method in their 653 implementation. 654 655 \since Haiku R1 656*/ 657 658 659//! @} 660