#include <stdio.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Menu_.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Choice.H>
#include <FL/Fl_Tree.H>
#include <FL/Fl_Tree_Item.H>

enum {
    spacing = 20,
    menu_h = 30,
    row_h = 35,
    button_w = 170,
    choice_w = 180,
    tree_h = 260,
    status_h = 40,
    win_w = 600,

    row_y = menu_h + spacing,
    choice_x = win_w - spacing - choice_w,
    tree_y = row_y + row_h + spacing,
    status_y = tree_y + tree_h + spacing,
    win_h = status_y + status_h + spacing
};

static void menu_cb(Fl_Widget *w, void *data)
{
    Fl_Menu_ *menu = static_cast<Fl_Menu_*>(w);
    Fl_Box *status = static_cast<Fl_Box*>(data);

    const Fl_Menu_Item *item = menu->mvalue();

    if (item) {
        status->copy_label(item->label());
        printf("Menu: %s\n", item->label());
    }
}

static void quit_cb(Fl_Widget *, void *data)
{
    Fl_Window *win = static_cast<Fl_Window*>(data);
    win->hide();
}

static void choice_cb(Fl_Widget *w, void *data)
{
    Fl_Choice *choice = static_cast<Fl_Choice*>(w);
    Fl_Box *status = static_cast<Fl_Box*>(data);

    const Fl_Menu_Item *item = choice->mvalue();

    if (item) {
        status->copy_label(item->label());
        printf("Choice: %s\n", item->label());
    }
}

static void tree_cb(Fl_Widget *w, void *data)
{
    Fl_Tree *tree = static_cast<Fl_Tree*>(w);
    Fl_Box *status = static_cast<Fl_Box*>(data);

    Fl_Tree_Item *item = tree->callback_item();

    if (!item)
        return;

    const char *action = "Changed";

    switch (tree->callback_reason()) {
    case FL_TREE_REASON_SELECTED:
        action = "Selected";
        break;

    case FL_TREE_REASON_DESELECTED:
        action = "Deselected";
        break;

    case FL_TREE_REASON_OPENED:
        action = "Opened";
        break;

    case FL_TREE_REASON_CLOSED:
        action = "Closed";
        break;

    default:
        break;
    }

    char text[256];
    sprintf(text, "%s: %s", action, item->label());
    status->copy_label(text);
    puts(text);
}

int main()
{
    Fl_Window win(win_w, win_h, "FLTK menu examples");

    Fl_Box status(spacing, status_y, win_w - 2 * spacing, status_h, "Ready");
    status.box(FL_DOWN_BOX);
    status.align(FL_ALIGN_LEFT | FL_ALIGN_INSIDE);

    Fl_Menu_Bar bar(0, 0, win_w, menu_h);
    bar.add("&File/&Hello");
    bar.add("&File/&Quit", FL_CTRL + 'q', quit_cb, &win);
    bar.add("&Help/&About");
    bar.callback(menu_cb, &status);

    Fl_Menu_Button button(spacing, row_y, button_w, row_h, "Actions");
    button.add("Show message");
    button.add("Reset message");
    button.callback(menu_cb, &status);

    Fl_Choice choice(choice_x, row_y, choice_w, row_h, "Color:");
    choice.add("Red");
    choice.add("Green");
    choice.add("Blue");
    choice.value(0);
    choice.callback(choice_cb, &status);

    Fl_Tree tree(spacing, tree_y, win_w - 2 * spacing, tree_h);
    tree.root_label("Topics");
    tree.add("Projects/GUI/FLTK");
    tree.add("Projects/GUI/Qt");
    tree.add("Projects/CLI");
    tree.add("Languages/C++");
    tree.add("Languages/Assembly");
    tree.callback(tree_cb, &status);

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

    return Fl::run();
}
