Interface Kit: application menus

Interface Kit: application menus

  • mdo  DigitalBox
  •   KitLab
  •   October 27, 2025

In the previous KitLab entries, we’ve worked with views, layouts, controls, and drag & drop.

Now it’s time to add one of the most essential parts of any desktop application: the menu bar.

Menus are a fundamental part of Haiku’s Interface Kit.

They let you organize commands, like "File → Open" or "Edit → Copy", in a way that’s familiar to all users.


What are menus in Haiku?

Haiku provides three main classes for menus:

  • BMenuBar → the horizontal bar at the top of your window.

  • BMenu → a drop-down menu attached to a menu bar.

  • BMenuItem → an individual command in a menu.

Each menu item can be linked to a BMessage, which is then handled in your BWindow::MessageReceived() method.

The hierarchical view for a typical application is like below:

BWindow
 ├── BMenuBar
 │    └── BMenu
 │         └── BMenuItem
 └── BView (content area)

Example: A Simple Menu Application

Here’s a minimal app with a File menu and a Help menu.

The File menu has New and Quit options.
The Help menu has an About option.

When a menu item is clicked, the application responds with a message.

Source Code: MenuApp.cpp

#include <Application.h>
#include <Alert.h>
#include <MenuBar.h>
#include <Menu.h>
#include <MenuItem.h>
#include <StringView.h>
#include <View.h>
#include <Window.h>

// Message constants
const uint32 MSG_NEW   = 'newf';
const uint32 MSG_ABOUT = 'abou';

// Main content view
class ContentView : public BView
{
public:
    ContentView(BRect frame)
        :
        BView(frame, "content_view", B_FOLLOW_ALL, B_WILL_DRAW)
    {
        SetViewColor(245, 245, 245);    // Light gray background
    }

    void Draw(BRect) override
    {
        SetHighColor(0, 0, 0);
        DrawString("BView content area!", BPoint(20, 40));
    }
};

class MenuWindow : public BWindow
{
public:
    MenuWindow(BRect frame)
        :
        BWindow(frame, "Menu Example", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        // Create the menu bar
        BMenuBar* menuBar = new BMenuBar(BRect(0, 0, Bounds().Width(), 20), "menubar");

        // File menu
        BMenu* fileMenu = new BMenu("File");
        fileMenu->AddItem(new BMenuItem("New", new BMessage(MSG_NEW), 'N'));
        fileMenu->AddSeparatorItem();
        fileMenu->AddItem(new BMenuItem("Quit", new BMessage(B_QUIT_REQUESTED), 'Q'));
        menuBar->AddItem(fileMenu);

        // Help menu
        BMenu* helpMenu = new BMenu("Help");
        helpMenu->AddItem(new BMenuItem("About", new BMessage(MSG_ABOUT)));
        menuBar->AddItem(helpMenu);

        AddChild(menuBar);

        // Add a content view below the menu bar
        BRect contentRect = Bounds();
        contentRect.top = menuBar->Bounds().Height();
        AddChild(new ContentView(contentRect));
    }

    void MessageReceived(BMessage* msg) override
    {
        switch (msg->what) {
            case MSG_NEW:
                (new BAlert("new", "You selected 'New'.", "OK"))->Go();
                break;

            case MSG_ABOUT:
                (new BAlert("about", "Menu Example Application\nKitLab Series", "OK"))->Go();
                break;

            default:
                BWindow::MessageReceived(msg);
                break;
        }
    }

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

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

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

How It Works

  1. BMenuBar
    The BMenuBar is created and added at the top of the window.

  2. BMenu
    Menus like "File" and "Help" are added to the menu bar.

  3. BMenuItem
    Each item (New, Quit, About) is tied to a BMessage.

    • 'N' is a shortcut for New.

    • 'Q' is a shortcut for Quit.

  4. Message Handling
    Inside MessageReceived(), we respond to menu commands:

    • Show a BAlert when "New" or "About" is selected.

    • Quit the app when "Quit" is selected.


Compile and Run

Open Terminal and type:

g++ -o MenuApp MenuApp.cpp -lbe

Then run the application:

MenuApp

You’ll see a window with a menu bar:

Try the File → New

But also the Help → About options.

Only a few lines of code let you create customized menus:)


Why Menus Matter

  • User familiarity → Menus are the standard way users discover app features.

  • Keyboard shortcuts → Power users love them!

  • Organization → Commands stay consistent and predictable.


What’s Next?

Now that we have menus, we can explore contextual menus (right-click menus in views)

Let's review that in an upcoming dedicated article :)


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