Haiku Book: Make the layout introduction somewhat more professional.

This commit is contained in:
Augustin Cavalier
2015-03-25 11:47:29 -04:00
parent 9d6780b587
commit 906283b6d6
+9 -11
View File
@@ -1,9 +1,10 @@
/* /*
* Copyright 2010 Haiku, Inc. All rights reserved. * Copyright 2010-2015 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Alex Wilson, [email protected] * Alex Wilson, [email protected]
* Augustin Cavalier <waddlesplash>
*/ */
@@ -37,7 +38,7 @@
above would work, but we'll use a BGroupLayout, because it suits this above would work, but we'll use a BGroupLayout, because it suits this
purpose the best. purpose the best.
So, let's review the BGroupLayout constructor: The BGroupLayout constructor is:
\code \code
BGroupLayout(orientation orientation, float spacing = B_USE_DEFAULT_SPACING) BGroupLayout(orientation orientation, float spacing = B_USE_DEFAULT_SPACING)
@@ -56,16 +57,16 @@ window->SetLayout(group);
Before we can add anything to our layout, we must attach it to something, Before 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. 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 need By doing this, \c window takes ownership of \c group, so there is no need
to manually <tt> delete group </tt> when we're done with it. to manually <tt>delete group</tt> when we're done with it.
Now that we've got our BGroupLayout in place, we can start adding things Now that we've got our BGroupLayout in place, we can start adding things
to it, let's add a BStringView. to it, so let's add a BStringView.
\code \code
group->AddView(MakeStringView("Haiku rocks!")); group->AddView(MakeStringView("Haiku rocks!"));
\endcode \endcode
That does it! Now we've got a BWindow with a horizontal BGroupLayout holding Now we've got a BWindow with a horizontal BGroupLayout holding
a single BView. However, if we want to ensure that our BStringView is always a single BView. However, if we want to ensure that our BStringView is always
centered in the window, we should give it an explicit BAlignment. So the centered in the window, we should give it an explicit BAlignment. So the
last line becomes: last line becomes:
@@ -79,7 +80,7 @@ stringView->SetExplicitAlignment(BAlignment(B_ALIGN_HORIZONTAL_CENTER,
Now our BStringView will always be right in the middle of the space Now our BStringView will always be right in the middle of the space
allotted to it, which at the moment is the whole of \c window. allotted to it, which at the moment is the whole of \c window.
Now let's take things one step further, and add a BMenuBar into the mix. Now let's add a BMenuBar:
\code \code
group->AddView(0, MakeMenuBar()); group->AddView(0, MakeMenuBar());
@@ -99,10 +100,9 @@ group->SetInsets(0, 0, 0, 0);
little \c window is stretched. Of course, very few interfaces are as simple little \c window is stretched. Of course, very few interfaces are as simple
as this one. as this one.
Luckily, the layout classes can deal with complex layouts. Suppose, for The layout classes can deal with complex layouts. Suppose, for
example, that we wanted to add a grid of BButtons under our BStringView. example, that we wanted to add a grid of BButtons under our BStringView.
We could use a BGridLayout for this. Let's review the BGridLayout We could use a BGridLayout for this. The BGridLayout constructor is:
constructor:
\code \code
BGridLayout(float horizontal = B_USE_DEFAULT_SPACING, BGridLayout(float horizontal = B_USE_DEFAULT_SPACING,
@@ -135,8 +135,6 @@ grid->AddView(MakeSmallButton(), 1, 2);
cells. cells.
\li The cell (0, 2) is empty. \li The cell (0, 2) is empty.
How easy was that? Very easy!
One of the features you'll find incredibly handy in the layout API is the One of the features you'll find incredibly handy in the layout API is the
builders in LayoutBuilder.h. Here's how our whole layout would look if it builders in LayoutBuilder.h. Here's how our whole layout would look if it
were done with these builders: were done with these builders: