#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Scroll.H>
#include <FL/Fl_Pack.H>

enum {
    spacing = 15,
    gap = 8,
    title_h = 30,
    row_h = 42,
    row_count = 15,
    scroll_w = 460,
    scroll_h = 260,
    pack_w = scroll_w + 200,
    pack_h = row_count * row_h + (row_count - 1) * gap,
    toolbar_h = 45,
    toolbar_button_w = 135,

    scroll_x = spacing,
    scroll_y = spacing + title_h + spacing,
    toolbar_y = scroll_y + scroll_h + spacing,

    win_w = scroll_w + 2 * spacing,
    win_h = toolbar_y + toolbar_h + spacing
};

static void button_cb(Fl_Widget *w, void *)
{
    printf("Clicked: %s\n", w->label());
    fflush(stdout);
}

int main()
{
    Fl_Window win(win_w, win_h, "Fl_Scroll and Fl_Pack");

    Fl_Box title(spacing, spacing, scroll_w, title_h,
                 "Scroll in both directions");

    Fl_Scroll scroll(scroll_x, scroll_y, scroll_w, scroll_h);
    scroll.box(FL_DOWN_FRAME);
    scroll.type(Fl_Scroll::BOTH);

    Fl_Pack vertical(scroll_x + gap, scroll_y + gap, pack_w, pack_h);
    vertical.type(Fl_Pack::VERTICAL);
    vertical.spacing(gap);

    for (int i = 0; i < row_count; ++i) {
        char label[80];
        sprintf(label, "Button %d - Click me!", i + 1);

        Fl_Button *button = new Fl_Button(scroll_x + gap, scroll_y + gap,
                                          pack_w, row_h);
        button->copy_label(label);
        button->callback(button_cb);
    }

    vertical.end();
    scroll.end();

    Fl_Pack horizontal(spacing, toolbar_y, scroll_w, toolbar_h);
    horizontal.type(Fl_Pack::HORIZONTAL);
    horizontal.spacing(gap);

    Fl_Button first(spacing, toolbar_y, toolbar_button_w, toolbar_h, "First");
    first.callback(button_cb);

    Fl_Button second(spacing, toolbar_y, toolbar_button_w, toolbar_h, "Second");
    second.callback(button_cb);

    Fl_Button third(spacing, toolbar_y, toolbar_button_w, toolbar_h, "Third");
    third.callback(button_cb);

    horizontal.end();

    win.end();
    win.show();

    return Fl::run();
}
