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

enum {
    spacing = 20,
    box_w = 400,
    box_h = 220,
    font_size = 20,
    win_w = box_w + 2 * spacing,
    win_h = box_h + 2 * spacing
};

class EventBox : public Fl_Box {
public:
    EventBox(int x, int y, int w, int h, const char *title)
        : Fl_Box(x, y, w, h, title)
    {
        box(FL_DOWN_BOX);
        color(FL_DARK_GREEN);
        labelsize(font_size);
    }

    int handle(int event)
    {
        char text[100];

        switch (event) {
        case FL_ENTER:
        case FL_MOVE:
            sprintf(text, "Mouse: %d, %d",
                     Fl::event_x() - x(), Fl::event_y() - y());
            copy_label(text);
            return 1;

        case FL_DRAG:
            copy_label("Dragging!");
            return 1;

        case FL_PUSH:
            sprintf(text, "Mouse button %d pressed\n",
                    Fl::event_button());
            copy_label(text);
            return 1;

        case FL_KEYDOWN:
            sprintf(text, "Key: %d, text: %s",
                     Fl::event_key(), Fl::event_text());
            copy_label(text);
            return 1;
        }

        return Fl_Box::handle(event);
    }
};

int main()
{
    Fl_Window win(win_w, win_h, "Event handling");
    win.color(FL_BLACK);

    EventBox box(spacing, spacing, box_w, box_h, "Move mouse here");

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