Interface Kit: using drag and drop

Interface Kit: using drag and drop

  • mdo  DigitalBox
  •   KitLab
  •   October 13, 2025

In previous KitLab articles, we explored drawing shapes, handling input, and using layouts.

Now it’s time to look at one of the most intuitive forms of interaction: drag and drop.

Drag and drop in Haiku works by sending a BMessage when you start dragging, and receiving it in the destination view.

You can attach custom data to that message — text, files, or even colors.

In this article, we’ll build a playful demo where you drag colored squares into a drop zone, and the zone changes color.


The Example: color swatches and drop zone

Our program has two main parts:

  • ColorSwatch: a small colored square you can drag.

  • DropZone: a rectangular area that changes color when you drop a swatch onto it.

This shows how easy it is to move data between views using drag & drop.


Source Code: DragDropApp.cpp

#include <Application.h>
#include <Bitmap.h>
#include <Message.h>
#include <Rect.h>
#include <View.h>
#include <Window.h>

// Custom message type for dragging colors
const uint32 MSG_COLOR = 'colr';

// ColorSwatch: a draggable square
class ColorSwatch : public BView {
public:
    rgb_color fColor;

    ColorSwatch(BRect frame, rgb_color color)
        : BView(frame, "color_swatch", B_FOLLOW_LEFT | B_FOLLOW_TOP,
            B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
        fColor(color)
    {
        SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
    }

    void Draw(BRect) override
    {
        SetHighColor(fColor);
        FillRect(Bounds());

        // Black border
        SetHighColor(0, 0, 0);
        StrokeRect(Bounds());
    }

    void MouseDown(BPoint point) override
    {
        BMessage dragMsg(MSG_COLOR);
        dragMsg.AddData("RGBColor", B_RGB_COLOR_TYPE, &fColor, sizeof(rgb_color));

        // Start the drag directly (no custom bitmap)
        DragMessage(&dragMsg, Bounds());
    }
};

// DropZone: changes its background color when a swatch is dropped
class DropZone : public BView {
public:
    rgb_color fColor;

    DropZone(BRect frame)
        : BView(frame, "drop_zone", B_FOLLOW_ALL,
            B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE)
    {
        fColor = make_color(245, 245, 245); // Light gray background
        SetViewColor(fColor);
    }

    void Draw(BRect) override
    {
        SetHighColor(0, 0, 0);
        StrokeRect(Bounds());

        DrawString("Drop a color here",
            BPoint(10, Bounds().Height() / 2));
    }

    void MessageReceived(BMessage* msg) override
    {
        if (msg->what == MSG_COLOR) {
            const rgb_color* newColor;
            ssize_t size;
            if (msg->FindData("RGBColor", B_RGB_COLOR_TYPE,
                (const void**)&newColor, &size) == B_OK) {
                fColor = *newColor;
                SetViewColor(fColor);
                Invalidate();
            }
        } else {
            BView::MessageReceived(msg);
        }
    }
};

// Main window
class DragDropWindow : public BWindow {
public:
    DragDropWindow()
        : BWindow(BRect(100, 100, 400, 300), "Drag & Drop App",
            B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
    {
        BRect bounds = Bounds();

        // Three pastel color swatches on the left
        AddChild(new ColorSwatch(BRect(10, 10, 60, 60), make_color(255, 182, 193)));    // Pink
        AddChild(new ColorSwatch(BRect(10, 70, 60, 120), make_color(152, 251, 152)));   // Green
        AddChild(new ColorSwatch(BRect(10, 130, 60, 180), make_color(173, 216, 230)));  // Blue

        // Drop zone on the right
        AddChild(new DropZone(BRect(80, 10, bounds.Width() - 10, bounds.Height() - 10)));
    }
};

// Application
class DragDropApp : public BApplication {
public:
    DragDropApp()
        : BApplication("application/x-vnd.demo-dragdrop")
    {
    }

    void ReadyToRun() override
    {
        (new DragDropWindow())->Show();
    }
};

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

Understanding DragMessage()

The function DragMessage() is what actually starts a drag-and-drop operation in Haiku.

  • First argument: a BMessage that contains the data you want to transfer (in this case, the RGB color).

  • Second argument (optional): a bitmap or a rectangle that defines the drag "visual". Here we simply pass Bounds(), so Haiku shows a ghosted rectangle of the swatch.

  • Third argument (optional): drawing mode (defaults to B_OP_COPY).

  • Fourth argument (optional): the hotspot offset inside the drag image.

Once DragMessage() is called:

  • Haiku tracks the mouse until you release it.

  • If you release the drag over a view or window that accepts the message type, the system sends that BMessage to the target’s MessageReceived().

That’s why drag-and-drop in Haiku is so simple: one line to start the drag, and one method override to handle the drop.


How It Works

  1. Dragging

    • Each ColorSwatch starts a drag operation in MouseDown().

    • A BMessage is created with type MSG_COLOR.

    • The swatch’s rgb_color is added as "RGBColor".

  2. Dropping

    • The DropZone overrides MessageReceived().

    • If the message type is MSG_COLOR, it extracts the color.

    • It updates its background color and redraws itself.

  3. No extra code for visuals

    • The black border and text are drawn in Draw().

    • The background automatically updates via SetViewColor() and Invalidate().


Compile and Run

In Terminal:

g++ -o DragDropApp DragDropApp.cpp -lbe

Then type:

./DragDropApp

You’ll see three pastel-colored squares (pink, green, blue) on the left :

Drag any square into the large drop zone:

It immediately takes on that color!

Select another colored square and drag it into the drop zone again:

Nice, isn't it ? 


Why This Matters

  • Intuitive interaction: drag and drop feels natural to the user.

  • Flexible data transfer: you can pass text, files, or even binary data.

  • Clean code: just a BMessage on the sender, and a MessageReceived() on the receiver.


What’s Next?

In the next KitLab entries, we’ll explore:

  • Dragging files between applications (B_REFS_RECEIVED).

  • Adding application and context menus.

Stay tuned in the coming weeks:)


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