/** Copyright 2010-2015 Haiku, Inc. All rights reserved.* Distributed under the terms of the MIT License.** Authors:* Alex Wilson, yourpalal2@gmail.com* Augustin Cavalier <waddlesplash>*//*!\page layout_intro Introduction to the Layout APIHaiku's Layout API is centered around the BLayoutItem and BLayout classes.The BLayoutItem class represents thing that can be managed by a BLayout,which is itself a BLayoutItem. Before we go any further, it is a good ideato familiarize yourself with the different BLayout classes available inHaiku:\li BGroupLayout\li BGridLayout\li BCardLayout\li BSplitViewYou'll notice that BSplitView is not actually a BLayout, but a BView. TheBSplitView class uses a custom BLayout behind the scenes, but because itmust also be able to draw, a BView is required. Other BLayout objects haveBView objects that can be used for convenience.\li BGroupLayout : BGroupView\li BGridLayout : BGridView\li BCardLayout : BTabView (also provides on-screen tabs)Although it is not necessary to use these classes to make use of thecorresponding layouts, it does make things easier.Once you have an understanding of what each BLayout does, you can startdesigning an interface with them. Let's consider a very simple window,with a single item in the center. For this, any of the layouts mentionedabove would work, but we'll use a BGroupLayout, because it suits thispurpose the best.The BGroupLayout constructor is:\codeBGroupLayout(orientation orientation, float spacing = B_USE_DEFAULT_SPACING)\endcodeBecause we only have one item in this layout, \c orientation and \c spacingbecome irrelevant. Let's choose B_VERTICAL for \c orientation, and leave\c spacing at its default.\codeBGroupLayout* group = new BGroupLayout(B_VERTICAL);BWindow* window = MakeWindow();window->SetLayout(group);\endcodeBefore we can add anything to our layout, we must attach it to something,and here we've used the BWindow::SetLayout() method to accomplish that.By doing this, \c window takes ownership of \c group, so there is no needto manually <tt>delete group</tt> when we're done with it.Now that we've got our BGroupLayout in place, we can start adding thingsto it, so let's add a BStringView.\codegroup->AddView(MakeStringView("Haiku rocks!"));\endcodeNow we've got a BWindow with a horizontal BGroupLayout holdinga single BView. However, if we want to ensure that our BStringView is alwayscentered in the window, we should give it an explicit BAlignment. So thelast line becomes:\codeBLayoutItem* stringView = group->AddView(MakeStringView("Haiku rocks!"));stringView->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,B_ALIGN_VERTICAL_CENTER);\endcodeNow our BStringView will always be right in the middle of the spaceallotted to it, which at the moment is the whole of \c window.Now let's add a BMenuBar:\codegroup->AddView(0, MakeMenuBar());group->SetInsets(0, 0, 0, 0);\endcodeBecause we want our BMenuBar to appear at the very top of the window, wehave to insert it at index \c 0, above the BStringView we added earlier.We also use BTwoDimensionalLayout::SetInsets() to make sure that ourBMenuBar is flush to the edges of \c window. We also want a bit ofspace between our BMenuBar and our BStringView, but \c group's spacing hasalready been set by the BGroupLayout constructor, so we don't need to dothat.Now that we've put our BGroupLayout to good use, we can rest easy, assuredthat GUI will always look nice, no matter what font is used, or how big orlittle \c window is stretched. Of course, very few interfaces are as simpleas this one.The layout classes can deal with complex layouts. Suppose, forexample, that we wanted to add a grid of BButtons under our BStringView.We could use a BGridLayout for this. The BGridLayout constructor is:\codeBGridLayout(float horizontal = B_USE_DEFAULT_SPACING,float vertical = B_USE_DEFAULT_SPACING);\endcodeBecause we want a bit of breathing room between our buttons, we'll leavevertical and horizontal spacing as is.\codeBGridLayout* grid = new BGridLayout();group->AddItem(grid);\endcodeYou'll notice that we've added \c grid directly to \c group. This means thatany BView objects we add to \c grid will become children of \c window, butwill be positioned by \c grid.\codegrid->AddView(MakeSmallButton(), 0, 0);grid->AddView(MakeSmallButton(), 1, 0);grid->AddView(MakeBigButton(), 0, 1, 2, 1);grid->AddView(MakeSmallButton(), 1, 2);\endcodeNow we've got a nice grid of BButton objects, let's go over it quickly:\li \c grid has two columns and three rows.\li The cells (0, 0), (1, 0), and (1, 2) hold small buttons\li The cells (0, 1) and (1, 1) hold a single button that spans bothcells.\li The cell (0, 2) is empty.One of the features you'll find incredibly handy in the layout API is thebuilders in LayoutBuilder.h. Here's how our whole layout would look if itwere done with these builders:\codeBLayoutBuilder::Group<>(window, B_VERTICAL).SetInsets(0, 0, 0, 0).Add(MakeMenuBar()).Add(MakeStringView("Haiku rocks!")).AddGrid().Add(MakeSmallButton(), 0, 0).Add(MakeSmallButton(), 1, 0).Add(MakeBigButton(), 0, 1, 2, 1).Add(MakeSmallButton(), 1, 2);\endcodeThis is only one way that you could build this layout, but it is probablythe most succinct. Functionally, this is equivalent to all the previouscode in this introduction.\par Special Handling for BBoxBBox is a "container" view that can contain other views.The use of the layout manager within aninstance of BBox is a special case. Code such as is shown below isnecessary to automatically layout views within a BBox.\codeBBox *box = new BBox("box-example");BGroupLayout *boxLayout = BLayoutBuilder::Group<>(B_HORIZONTAL).Add(button1).Add(button2);box->AddChild(boxLayout->View());\endcode*/