#include <gtkmm.h>

#define USE_PAGINATE

class CustomPrintOperation : public Gtk::PrintOperation
{
public:
    CustomPrintOperation() : been_here(false) {}

    bool been_here;

    virtual void on_begin_print (const Glib::RefPtr<Gtk::PrintContext> &context);
    virtual void on_draw_page (const Glib::RefPtr<Gtk::PrintContext> &context,
                               int                                    page_nr);
#ifdef USE_PAGINATE
    virtual bool on_paginate(const Glib::RefPtr<Gtk::PrintContext>& context);
#endif
};

void CustomPrintOperation::on_begin_print (const Glib::RefPtr<Gtk::PrintContext> &context)
{
#ifndef USE_PAGINATE
    set_n_pages (2);
#endif
}

void
CustomPrintOperation::on_draw_page (const Glib::RefPtr<Gtk::PrintContext> &context,
                                    int                                    page_nr)
{
    Glib::RefPtr<Pango::Layout> layout = context->create_pango_layout ();
    layout->set_font_description (Pango::FontDescription ("sans 24"));

    char *page_str = g_strdup_printf ("%d", page_nr);
    layout->set_text (page_str);
    g_free (page_str);

    int text_width, text_height;
    layout->get_pixel_size (text_width, text_height);

    Cairo::RefPtr<Cairo::Context> cr = context->get_cairo_context();
    cr->move_to ((context->get_width () - text_width) / 2, (context->get_height () - text_height) / 2);
    layout->add_to_cairo_context (cr);
}

#ifdef USE_PAGINATE
bool
CustomPrintOperation::on_paginate (const Glib::RefPtr<Gtk::PrintContext>& context)
{
    g_print ("wefwefwef\n");
    if (!been_here)
    {
        been_here = true;
        set_n_pages (1);
        return false;
    }
    else
    {
        set_n_pages (2);
        return true;
    }
}
#endif


int main (int argc, char *argv[])
{
    Gtk::Main kit (argc, argv);

    CustomPrintOperation op;
    op.run (Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG);

    return 0;
}
