Interface Kit: drawing text in a custom view

Interface Kit: drawing text in a custom view

  • mdo  DigitalBox
  •   KitLab
  •   September 8, 2025

In the previous KitLab article, we created a minimal Haiku application with a window.

Let’s take the next step: drawing inside that window. 

In Haiku, all custom rendering happens inside a BView — your canvas for shapes, text, and interactivity.

This is the basis of all custom graphics in Haiku: subclassing BView and implementing the Draw() method.

What is BView?

BView is the core class used to build and draw any user interface in Haiku.

It represents a rectangular region inside a BWindow, and it:

  • Handles drawing (via Draw())
  • Hosts UI controls (like buttons or sliders)
  • Manages user input (mouse, keyboard)

Source Code: HelloWorld.cpp

This example creates a custom view that draws "Hello, World!" using MovePenTo() and DrawString().

#include <Application.h>
#include <Window.h>
#include <View.h>

// Custom view that draws text
class HelloView : public BView
{
public:
    HelloView(BRect frame)
        :
        BView(frame, "hello_view", B_FOLLOW_ALL, B_WILL_DRAW)
    {
        SetViewColor(255, 255, 255);    // White background
    }

    void Draw(BRect updateRect) override
    {
        SetHighColor(0, 0, 0);  // Black text
        MovePenTo(20, 30);
        DrawString("Hello, World!");
    }
};

// Simple window that adds the custom view
class HelloWindow : public BWindow
{
public:
    HelloWindow(BRect frame)
        :
        BWindow(frame, "Hello Window", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        BRect viewRect = Bounds();  // Use full window
        BView* view = new HelloView(viewRect);
        AddChild(view); // Attach view to window
    }
};

// Application class
class HelloApp : public BApplication
{
public:
    HelloApp()
        :
        BApplication("application/x-vnd.example-helloworld")
    {
        BRect rect(100, 100, 400, 300);
        HelloWindow* window = new HelloWindow(rect);
        window->Show();
    }
};

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

How it works

  • HelloView is a subclass of BView with a custom Draw() method.

  • SetViewColor() sets the background color.

  • SetHighColor() sets the color used for drawing.

  • MovePenTo() sets the text position.

  • DrawString() draws the text on the screen.

Note: All drawing in BView::Draw() is relative to the view’s coordinate system.
MovePenTo() sets the current "pen" position, and DrawString() renders text starting from that point.

Compilation

g++ HelloWorld.cpp -o HelloWorld -lbe

Launch the application from a Terminal:

HelloWorld

You’ll see a white window with "Hello, World!" in black text at the top left.

Try this:

  • Change the background color with SetViewColor(220, 240, 255)
  • Try drawing a rectangle with FillRect() or StrokeRect()
  • Change the font size with SetFontSize() (after creating a BFont)

What’s Next?

Now that we’re drawing custom content, the next step is interaction.

Future KitLab articles will explore:

  • Adding a button (BButton) and responding to clicks

  • Handling mouse and keyboard events in your view

  • Drawing shapes, lines, and images

  • Using layout managers (BGroupLayout, BGridLayout)

Conclusion

With just a few lines of code, you've created a fully functional Haiku application that draws custom text.

The BView system is simple yet powerful — and it's the gateway to creating dynamic, responsive interfaces on Haiku.

Stay tuned for the next KitLab article.


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