/* February 2008, Bastiaan Veelo, Bastiaan@SARC.nl */

#include <cairo.h>
#include <gtk/gtk.h>

static GtkPrintSettings *settings = NULL;

void cairo_draw(cairo_t *cr)
{
  cairo_move_to(cr, 0.0, 0.0);
  cairo_line_to(cr, 1000.0, 250.0);
  cairo_stroke(cr);
}

static gboolean
on_expose_event(GtkWidget      *widget,
                GdkEventExpose *event,
                gpointer        data)
{
  cairo_t *cr;

  cr = gdk_cairo_create(GTK_LAYOUT(widget)->bin_window);

  cairo_draw (cr);

  cairo_destroy(cr);

  return FALSE;
}

static void
begin_print (GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data)
{
  gtk_print_operation_set_n_pages (operation, 1);
}


static void
print_page (GtkPrintOperation *operation, GtkPrintContext *context,
            gint page_nr, gpointer user_data)
{
  cairo_t *cr = gtk_print_context_get_cairo_context (context);
  cairo_draw (cr);
}


void
print_dialog(GtkWidget *button, gpointer parent)
{
  GtkPrintOperation *print;
  GtkPrintOperationResult result;
  GError *error;

  print = gtk_print_operation_new ();

  if (settings != NULL)
    gtk_print_operation_set_print_settings (print, settings);

  g_signal_connect (print, "begin-print", G_CALLBACK (begin_print), NULL);
  g_signal_connect (print, "draw-page", G_CALLBACK (print_page), NULL);

  result = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
                                    GTK_WINDOW (parent), &error);

  if (result == GTK_PRINT_OPERATION_RESULT_ERROR)
  {
    GtkWidget *error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
        GTK_DIALOG_DESTROY_WITH_PARENT,
        GTK_MESSAGE_ERROR,
        GTK_BUTTONS_CLOSE,
        "Error printing file:\n%s",
        error->message);
    g_signal_connect (error_dialog, "response",
                      G_CALLBACK (gtk_widget_destroy), NULL);
    gtk_widget_show (error_dialog);
    g_error_free (error);
  }
  else if (result == GTK_PRINT_OPERATION_RESULT_APPLY)
  {
    if (settings != NULL)
      g_object_unref (settings);
    settings = g_object_ref (gtk_print_operation_get_print_settings (print));
  }

  g_object_unref (print);
}

int
main (int argc, char *argv[])
{
  gtk_init (&argc, &argv);

  GtkWidget *window;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect(window, "destroy",
                   G_CALLBACK (gtk_main_quit), NULL);

  GtkWidget *vbox = gtk_vbox_new (FALSE, 5);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  GtkWidget *hbox = gtk_hbox_new (FALSE, 5);
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);

  GtkWidget *print_button = gtk_button_new_from_stock (GTK_STOCK_PRINT);
  gtk_box_pack_start (GTK_BOX (hbox), print_button, FALSE, FALSE, 0);
  g_signal_connect (print_button, "clicked",
                    G_CALLBACK (print_dialog), window);

  GtkWidget *scrolled = gtk_scrolled_window_new(NULL, NULL);
  gtk_box_pack_start_defaults (GTK_BOX (vbox), scrolled);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  GtkWidget *drawing = gtk_layout_new (gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (scrolled)),
                                       gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (scrolled)));
  gtk_container_add (GTK_CONTAINER (scrolled), drawing);
  GdkColor white;
  gdk_color_parse ("#FFF", &white);
  gtk_widget_modify_bg (drawing, GTK_STATE_NORMAL, &white);

  gtk_layout_set_size (GTK_LAYOUT (drawing), 1000, 250);

  g_signal_connect(drawing, "expose-event",
                   G_CALLBACK (on_expose_event), NULL);

  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 90);
  gtk_widget_set_app_paintable(window, TRUE);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}
