Interface kit : using layouts

Interface kit : using layouts

  • mdo  DigitalBox
  •   KitLab
  •   October 6, 2025

In the previous KitLab articles, we explored drawing, controls, and handling input.

But real applications also need responsive, flexible user interfaces.

Hardcoding rectangles is not sustainable.

Haiku provides layout builders to declare UI structure in a clean, concise way.

The modern BLayoutBuilder class lets you nest vertical, horizontal, and grid layouts without the boilerplate.


Example: User Information Form

We’ll build a simple form with:

  • A label at the top.

  • A grid of fields (name + email).

  • A horizontal row of OK/Cancel buttons.

  • Padding and glue for spacing.


Source Code: LayoutApp.cpp

#include <Application.h>
#include <Button.h>
#include <GridLayout.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <LayoutBuilder.h>
#include <SpaceLayoutItem.h>
#include <StringView.h>
#include <TextControl.h>
#include <View.h>
#include <Window.h>

class LayoutWindow : public BWindow {
public:
    LayoutWindow(BRect frame)
        : BWindow(frame, "Layout Example", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        BLayoutBuilder::Group<>(this, B_VERTICAL, 10)
            .AddGlue()
            .AddGrid(10, 10)
                .Add(BSpaceLayoutItem::CreateGlue(), 0, 0)
                .Add(new BStringView("title", "User Information Form"), 0, 1)
                .Add(new BStringView("name_label", "Name:"), 0, 2)
                .Add(new BTextControl("", "", NULL), 1, 2)
                .Add(new BStringView("email_label", "Email:"), 0, 3)
                .Add(new BTextControl("", "", NULL), 1, 3)
            .End()
            .AddGlue()
            .AddGroup(B_HORIZONTAL, 10)
                .Add(new BButton("ok_btn", "OK", NULL))
                .Add(new BButton("cancel_btn", "Cancel", NULL))
            .End()
            .AddGlue()
            .SetInsets(5, 5, 5, 5);
    }

    bool QuitRequested() override
    {
        be_app->PostMessage(B_QUIT_REQUESTED);
        return true;
    }
};

class LayoutApp : public BApplication {
public:
    LayoutApp()
        : BApplication("application/x-vnd.example-layout")
    {
        BRect r(100, 100, 400, 300);
        LayoutWindow* w = new LayoutWindow(r);
        w->Show();
    }
};

int main()
{
    LayoutApp app;
    app.Run();
    return 0;
}

How It Works

  • BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
    Declares the window’s root layout: a vertical stack with 10px spacing (ie B_USE_DEFAULT_SPACING).

  • .AddGrid(10, 10)
    Starts a grid with 10px spacing in rows/columns. Perfect for aligning labels and text fields.

  • .AddGroup(B_HORIZONTAL, 10)
    Declares a horizontal group, used here for the OK/Cancel button row.

  • .AddGlue()
    Flexible space that expands and pushes items apart.

  • .SetInsets(5, 5, 5, 5)
    Adds padding around the window edges.

Please note it's better to use constants for the spacing and insets from InterfaceDefs.h for the BLayoutBuilder.

For instance, use B_USE_DEFAULT_SPACING instead of the hardcoded value of 10 pixels.


Layout Tree (Visual)

Here’s a simplified view of the layout hierarchy:


Compile and Run

In a Terminal, type the below command to compile the program:
g++ -o LayoutApp LayoutApp.cpp -lbe

Then launch the application: 

./LayoutApp

As you can see the initial form is as below:

Now, resize the window:

Automatic Resizing
One of the biggest advantages of using BLayoutBuilder is that resizing works nicely.

In our example, notice how the text fields expand when you resize the window, while the buttons remain neatly aligned at the bottom.

You don’t need to write any extra code — this behavior comes for free from Haiku’s layout system.

Really great:)


Why It Matters

  • Clean code: LayoutBuilder reduces boilerplate.

  • Responsive UIs: resizing just works.

  • Consistency: your apps feel native on Haiku.


What’s Next?

In the next KitLab entries, we’ll cover:

  • Handling of drag and drop messages in a view
  • Adding application & contextual menus

Stay tuned :)


Powered by Bludit - Hosted by Planet Hoster
© 2025 Haiku Insider