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

enum {
    spacing = 12,
    gap = 8,

    sidebar_w = 130,
    side_box_h = 44,

    edge_w = 80,
    center_w = 190,
    row_h = 58,
    center_h = 165,

    header_h = 40,
    footer_h = 30,
    font_size = 22,

    main_w = 2 * edge_w + center_w + 4 * gap,
    main_h = 2 * row_h + center_h + 4 * gap,

    main_x = 2 * spacing + sidebar_w,
    main_y = 2 * spacing + header_h,

    win_w = main_x + main_w + spacing,
    footer_y = main_y + main_h + spacing,
    win_h = footer_y + footer_h + spacing
};

static Fl_Box *mkbox(int x, int y, int w, int h, const char *label, Fl_Color color)
{
    Fl_Box *box = new Fl_Box(x, y, w, h, label);
    box->box(FL_UP_BOX);
    box->color(color);
    box->labelsize(font_size);
    return box;
}

int main()
{
    Fl_Window *win = new Fl_Window(win_w, win_h, "Nested Groups and Resizing");

    mkbox(spacing, spacing, win_w - 2 * spacing, header_h,
          "Resize the window!", fl_rgb_color(190, 190, 190));

    Fl_Group *sidebar = new Fl_Group(spacing, main_y, sidebar_w, main_h);
    sidebar->box(FL_ENGRAVED_BOX);

    for (int i = 0; i < 3; i++) {
        static const char *names[] = {"1", "2", "3"};
        mkbox(spacing + gap, main_y + gap + i * (side_box_h + gap),
              sidebar_w - 2 * gap, side_box_h, names[i],
              fl_rgb_color(160, 195, 230));
    }

    sidebar->resizable(0);
    sidebar->end();

    Fl_Group *main_group = new Fl_Group(main_x, main_y, main_w, main_h);
    main_group->box(FL_ENGRAVED_BOX);

    int left_x = main_x + gap;
    int center_x = left_x + edge_w + gap;
    int right_x = center_x + center_w + gap;

    int top_y = main_y + gap;
    int center_y = top_y + row_h + gap;
    int bottom_y = center_y + center_h + gap;

    Fl_Color fixed_color = fl_rgb_color(165, 220, 215);
    Fl_Color grow_color = fl_rgb_color(245, 210, 140);

    mkbox(left_x, top_y, main_w - 2 * gap, row_h, "TOP", fixed_color);
    mkbox(left_x, center_y, edge_w, center_h, "L", fixed_color);

    Fl_Box *center = mkbox(center_x, center_y, center_w, center_h, "R",
                           grow_color);
    mkbox(right_x, center_y, edge_w, center_h, "R2", fixed_color);
    mkbox(left_x, bottom_y, main_w - 2 * gap, row_h, "BOTTOM", fixed_color);

    main_group->resizable(center);
    main_group->end();

    Fl_Box *footer = mkbox(spacing, footer_y, win_w - 2 * spacing, footer_h,
                           "The bottom bar moves with the window",
                           fl_rgb_color(190, 190, 190));
    footer->labelsize(16);

    win->resizable(main_group);
    win->size_range(win_w, win_h, 2 * win_w, 2 * win_h, 50, 50, 1);

    win->end();
    win->show();
    return Fl::run();
}
