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

enum {
    input_w = 1000,
    input_h = 1000,
    button_h = 100,
    font_size = 40
};

static void print_callback(Fl_Widget *w, void *user)
{
    Fl_Text_Editor *editor = static_cast<Fl_Text_Editor*>(user);
    char* str = editor->buffer()->text();
    fputs(str, stdout);
    fflush(stdout);
    free(str);
    editor->take_focus();
}

static void clear_callback(Fl_Widget *w, void *user)
{
    Fl_Text_Editor *editor = static_cast<Fl_Text_Editor*>(user);
    editor->buffer()->text("");
    editor->take_focus();
}

int main(int argc, char **argv)
{
    Fl_Window *win = new Fl_Window(input_w, input_h + button_h, "input demo");

    Fl_Text_Buffer *buf = new Fl_Text_Buffer;
    Fl_Text_Editor *ed = new Fl_Text_Editor(0, 0, input_w, input_h);
    ed->buffer(buf);
    ed->textsize(font_size);
    ed->textcolor(FL_GRAY);
    ed->color(FL_BLACK);
    ed->cursor_color(FL_GRAY);

    Fl_Button *print_b = new Fl_Button(0, input_h,
                                     input_w / 2, button_h, "Print");
    print_b->callback(print_callback, (void*)ed);
    print_b->labelsize(font_size);

    Fl_Button *clear_b = new Fl_Button(input_w / 2, input_h,
                                     input_w / 2, button_h, "Clear");
    clear_b->callback(clear_callback, (void*)ed);
    clear_b->labelsize(font_size);

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