#include <gtk/gtk.h>

typedef GtkPrintOperation CustomPrintOperation;
typedef GtkPrintOperationClass CustomPrintOperationClass;

G_DEFINE_TYPE(CustomPrintOperation, custom_print_operation, GTK_TYPE_PRINT_OPERATION)

static void
custom_print_operation_draw_page (GtkPrintOperation *operation,
                                  GtkPrintContext   *context,
                                  gint               page_nr)
{
    cairo_t *cr;
    PangoLayout *layout;
    gint text_width, text_height;
    gdouble width, height;
    PangoFontDescription *desc;
    gchar *page_str;

    cr = gtk_print_context_get_cairo_context (context);
    width = gtk_print_context_get_width (context);
    height = gtk_print_context_get_height (context);

    layout = gtk_print_context_create_pango_layout (context);

    desc = pango_font_description_from_string ("sans 24");
    pango_layout_set_font_description (layout, desc);
    pango_font_description_free (desc);

    page_str = g_strdup_printf ("%d", page_nr);
    pango_layout_set_text (layout, page_str, -1);
    pango_layout_get_pixel_size (layout, &text_width, &text_height);

    cairo_move_to (cr, (width - text_width) / 2, (height - text_height) / 2);
    pango_cairo_show_layout (cr, layout);

    g_object_unref (layout);
}

static gboolean
custom_print_operation_paginate (GtkPrintOperation *operation,
				 GtkPrintContext   *context)
{
    if (!g_object_get_data (G_OBJECT (operation), "pages"))
    {
        g_object_set_data (G_OBJECT (operation), "pages", operation);
        gtk_print_operation_set_n_pages (operation, 1);
        return FALSE;
    }
    else
    {
        gtk_print_operation_set_n_pages (operation, 2);
        return TRUE;
    }
}

static void
custom_print_operation_init (CustomPrintOperation *op)
{
}

static void
custom_print_operation_class_init (CustomPrintOperationClass *klass)
{
    klass->draw_page = custom_print_operation_draw_page;
    klass->paginate = custom_print_operation_paginate;
}


int main (void)
{
  GtkPrintOperation *operation;
  GError *error = NULL;

  gtk_init (NULL, NULL);

  operation = g_object_new (custom_print_operation_get_type (), NULL);
  gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, NULL);
  g_object_unref (operation);

  return 0;
}
