#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Group.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>

enum {
    spacing = 10,
    cell_w = 80,
    cell_h = 70,
    button_w = 120,
    button_h = 40,
    font_size = 25,
    num_boxes = 4,

    group_w = num_boxes * cell_w + (num_boxes + 1) * spacing,
    group_h = cell_h + 2 * spacing,

    win_w = group_w + 2 * spacing,
    win_h = group_h + button_h + 3 * spacing
};

static void add_callback(Fl_Widget *w, void *user)
{
    Fl_Group *group = static_cast<Fl_Group*>(user);

    static int count = 0;
    static const char *const labels[] = {"A", "B", "C", "D"};

    if (count >= num_boxes)
        return;

    int x = group->x() + spacing + count * (cell_w + spacing);
    int y = group->y() + spacing;

    Fl_Box *box = new Fl_Box(x, y, cell_w, cell_h, labels[count]);
    box->box(FL_UP_BOX);
    box->color(FL_YELLOW);
    box->labelsize(font_size);

    group->add(box);
    group->redraw();

    count++;

    if (count == num_boxes)
        w->deactivate();
}

int main()
{
    Fl_Window win(win_w, win_h, "Dynamic group");

    Fl_Group group(spacing, spacing, group_w, group_h);
    group.box(FL_ENGRAVED_BOX);
    group.end();

    Fl_Button button(spacing, group_h + 2 * spacing, button_w, button_h,
                     "Add box");
    button.callback(add_callback, &group);

    win.end();
    win.show();
    return Fl::run();
}
