testprint.c 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/* GTK - The GIMP Toolkit
 * testprint.c: Print example
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <math.h>
#include <pango/pangocairo.h>
#include <gtk/gtk.h>
#include <gtk/gtkprintoperation.h>
#include "testprintfileoperation.h"

static void
request_page_setup (GtkPrintOperation *operation,
		    GtkPrintContext *context,
		    int page_nr,
		    GtkPageSetup *setup)
{
  /* Make the second page landscape mode a5 */
  if (page_nr == 1)
    {
      GtkPaperSize *a5_size = gtk_paper_size_new ("iso_a5");
      
      gtk_page_setup_set_orientation (setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
      gtk_page_setup_set_paper_size (setup, a5_size);
      gtk_paper_size_free (a5_size);
    }
}

static void
draw_page (GtkPrintOperation *operation,
	   GtkPrintContext *context,
	   int page_nr)
{
  cairo_t *cr;
  PangoLayout *layout;
  PangoFontDescription *desc;
  
54
  cr = gtk_print_context_get_cairo_context (context);
55 56 57 58 59 60 61 62 63 64

  /* Draw a red rectangle, as wide as the paper (inside the margins) */
  cairo_set_source_rgb (cr, 1.0, 0, 0);
  cairo_rectangle (cr, 0, 0, gtk_print_context_get_width (context), 50);
  
  cairo_fill (cr);

  /* Draw some lines */
  cairo_move_to (cr, 20, 10);
  cairo_line_to (cr, 40, 20);
65
  cairo_arc (cr, 60, 60, 20, 0, G_PI);
66 67 68 69 70 71 72 73 74 75 76
  cairo_line_to (cr, 80, 20);
  
  cairo_set_source_rgb (cr, 0, 0, 0);
  cairo_set_line_width (cr, 5);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);
  
  cairo_stroke (cr);

  /* Draw some text */
  
77
  layout = gtk_print_context_create_pango_layout (context);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  pango_layout_set_text (layout, "Hello World! Printing is easy", -1);
  desc = pango_font_description_from_string ("sans 28");
  pango_layout_set_font_description (layout, desc);
  pango_font_description_free (desc);

  cairo_move_to (cr, 30, 20);
  pango_cairo_layout_path (cr, layout);

  /* Font Outline */
  cairo_set_source_rgb (cr, 0.93, 1.0, 0.47);
  cairo_set_line_width (cr, 0.5);
  cairo_stroke_preserve (cr);

  /* Font Fill */
  cairo_set_source_rgb (cr, 0, 0.0, 1.0);
  cairo_fill (cr);
  
  g_object_unref (layout);
}

int
main (int argc, char **argv)
{
  GtkPrintOperation *print;
  GtkPrintOperationResult res;
  TestPrintFileOperation *print_file;

  gtk_init (&argc, &argv);

  /* Test some random drawing, with per-page paper settings */
  print = gtk_print_operation_new ();
Alexander Larsson's avatar
Alexander Larsson committed
109
  gtk_print_operation_set_n_pages (print, 2);
110
  gtk_print_operation_set_unit (print, GTK_UNIT_MM);
Matthias Clasen's avatar
Matthias Clasen committed
111
  gtk_print_operation_set_export_filename (print, "test.pdf");
112 113
  g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
  g_signal_connect (print, "request_page_setup", G_CALLBACK (request_page_setup), NULL);
114
  res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL);
115 116 117 118

  /* Test subclassing of GtkPrintOperation */
  print_file = test_print_file_operation_new ("testprint.c");
  test_print_file_operation_set_font_size (print_file, 12.0);
Matthias Clasen's avatar
Matthias Clasen committed
119
  gtk_print_operation_set_export_filename (GTK_PRINT_OPERATION (print_file), "test2.pdf");
120
  res = gtk_print_operation_run (GTK_PRINT_OPERATION (print_file), GTK_PRINT_OPERATION_ACTION_EXPORT, NULL, NULL);
121 122 123
  
  return 0;
}