summaryrefslogtreecommitdiff
path: root/demos
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2006-05-11 19:02:05 +0000
committerMatthias Clasen <matthiasc@src.gnome.org>2006-05-11 19:02:05 +0000
commit51275b656583cecf8e2ba8fafe93529e77b284c9 (patch)
treeb71b52b811cd909ea1d40bb10a8786d2667e18ec /demos
parent2b8e4e109baef1b9a3f86bf0208e4059eeb955f9 (diff)
downloadgtk+-51275b656583cecf8e2ba8fafe93529e77b284c9.tar.gz
Add it here.
2006-05-11 Matthias Clasen <mclasen@redhat.com> * demos/gtk-demo/Makefile.am (demos): Add it here. * demos/gtk-demo/printing.c (do_printing): Add a GtkPrintOperation demo.
Diffstat (limited to 'demos')
-rw-r--r--demos/gtk-demo/Makefile.am1
-rw-r--r--demos/gtk-demo/printing.c182
2 files changed, 183 insertions, 0 deletions
diff --git a/demos/gtk-demo/Makefile.am b/demos/gtk-demo/Makefile.am
index f51fdacd68..e1adb57b63 100644
--- a/demos/gtk-demo/Makefile.am
+++ b/demos/gtk-demo/Makefile.am
@@ -26,6 +26,7 @@ demos = \
panes.c \
pickers.c \
pixbufs.c \
+ printing.c \
rotated_text.c \
sizegroup.c \
stock_browser.c \
diff --git a/demos/gtk-demo/printing.c b/demos/gtk-demo/printing.c
new file mode 100644
index 0000000000..9940d3351b
--- /dev/null
+++ b/demos/gtk-demo/printing.c
@@ -0,0 +1,182 @@
+/* Printing
+ *
+ * GtkPrintOperation offers a simple API to support printing
+ * in a cross-platform way.
+ *
+ */
+
+#include <math.h>
+#include <gtk/gtk.h>
+#include "demo-common.h"
+
+/* In points */
+#define HEADER_HEIGHT (10*72/25.4)
+#define HEADER_GAP (3*72/25.4)
+
+typedef struct
+{
+ gchar *filename;
+ gdouble font_size;
+
+ gint lines_per_page;
+ gchar **lines;
+ gint num_lines;
+ gint num_pages;
+} PrintData;
+
+static void
+begin_print (GtkPrintOperation *operation,
+ GtkPrintContext *context,
+ gpointer user_data)
+{
+ PrintData *data = (PrintData *)user_data;
+ char *contents;
+ int i;
+ double height;
+
+ height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
+
+ data->lines_per_page = floor (height / data->font_size);
+
+ g_file_get_contents (data->filename, &contents, NULL, NULL);
+
+ data->lines = g_strsplit (contents, "\n", 0);
+ g_free (contents);
+
+ i = 0;
+ while (data->lines[i] != NULL)
+ i++;
+
+ data->num_lines = i;
+ data->num_pages = (data->num_lines - 1) / data->lines_per_page + 1;
+ gtk_print_operation_set_nr_of_pages (operation, data->num_pages);
+}
+
+static void
+draw_page (GtkPrintOperation *operation,
+ GtkPrintContext *context,
+ gint page_nr,
+ gpointer user_data)
+{
+ PrintData *data = (PrintData *)user_data;
+ cairo_t *cr;
+ PangoLayout *layout;
+ gdouble width, text_height;
+ gint line, i, layout_height;
+ PangoFontDescription *desc;
+ gchar *page_str;
+
+ cr = gtk_print_context_get_cairo (context);
+ width = gtk_print_context_get_width (context);
+
+ cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
+
+ cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
+ cairo_fill_preserve (cr);
+
+ cairo_set_source_rgb (cr, 0, 0, 0);
+ cairo_set_line_width (cr, 1);
+ cairo_stroke (cr);
+
+ layout = gtk_print_context_create_layout (context);
+
+ desc = pango_font_description_from_string ("sans 14");
+ pango_layout_set_font_description (layout, desc);
+ pango_font_description_free (desc);
+
+ pango_layout_set_text (layout, data->filename, -1);
+ pango_layout_set_width (layout, width);
+ pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
+
+ pango_layout_get_size (layout, NULL, &layout_height);
+ text_height = (gdouble)layout_height / PANGO_SCALE;
+
+ cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
+ pango_cairo_show_layout (cr, layout);
+
+ page_str = g_strdup_printf ("%d/%d", page_nr + 1, data->num_pages);
+ pango_layout_set_text (layout, page_str, -1);
+ g_free (page_str);
+ pango_layout_set_alignment (layout, PANGO_ALIGN_RIGHT);
+
+ cairo_move_to (cr, width - 2, (HEADER_HEIGHT - text_height) / 2);
+ pango_cairo_show_layout (cr, layout);
+
+ g_object_unref (layout);
+
+ layout = gtk_print_context_create_layout (context);
+
+ desc = pango_font_description_from_string ("mono");
+ pango_font_description_set_size (desc, data->font_size * PANGO_SCALE);
+ pango_layout_set_font_description (layout, desc);
+ pango_font_description_free (desc);
+
+ cairo_move_to (cr, 0, HEADER_HEIGHT + HEADER_GAP);
+ line = page_nr * data->lines_per_page;
+ for (i = 0; i < data->lines_per_page && line < data->num_lines; i++)
+ {
+ pango_layout_set_text (layout, data->lines[line], -1);
+ pango_cairo_show_layout (cr, layout);
+ cairo_rel_move_to (cr, 0, data->font_size);
+ line++;
+ }
+
+ g_object_unref (layout);
+}
+
+static void
+end_print (GtkPrintOperation *operation,
+ GtkPrintContext *context,
+ gpointer user_data)
+{
+ PrintData *data = (PrintData *)user_data;
+
+ g_free (data->filename);
+ g_strfreev (data->lines);
+ g_free (data);
+}
+
+
+GtkWidget *
+do_printing (GtkWidget *do_widget)
+{
+ GtkPrintOperation *operation;
+ PrintData *data;
+ GError *error = NULL;
+
+ operation = gtk_print_operation_new ();
+ data = g_new0 (PrintData, 1);
+ data->filename = demo_find_file ("printing.c", NULL);
+ data->font_size = 12.0;
+
+ g_signal_connect (G_OBJECT (operation), "begin-print",
+ G_CALLBACK (begin_print), data);
+ g_signal_connect (G_OBJECT (operation), "draw-page",
+ G_CALLBACK (draw_page), data);
+ g_signal_connect (G_OBJECT (operation), "end-print",
+ G_CALLBACK (end_print), data);
+
+ gtk_print_operation_run (operation, GTK_WINDOW (do_widget), &error);
+
+ g_object_unref (operation);
+
+ if (error)
+ {
+ GtkWidget *dialog;
+
+ dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
+ GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE,
+ "%s", error->message);
+ g_error_free (error);
+
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (gtk_widget_destroy), NULL);
+
+ gtk_widget_show (dialog);
+ }
+
+
+ return NULL;
+}