Commit d78f449d authored by Matthias Clasen's avatar Matthias Clasen

Improve printing example

Handle Documents folder being NULL, and handle output formats.
parent 8cca80d6
......@@ -13,19 +13,19 @@
#define HEADER_HEIGHT (10*72/25.4)
#define HEADER_GAP (3*72/25.4)
typedef struct
typedef struct
{
gchar *filename;
gdouble font_size;
gint lines_per_page;
gint lines_per_page;
gchar **lines;
gint num_lines;
gint num_pages;
} PrintData;
static void
begin_print (GtkPrintOperation *operation,
begin_print (GtkPrintOperation *operation,
GtkPrintContext *context,
gpointer user_data)
{
......@@ -35,9 +35,9 @@ begin_print (GtkPrintOperation *operation,
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);
......@@ -46,7 +46,7 @@ begin_print (GtkPrintOperation *operation,
i = 0;
while (data->lines[i] != NULL)
i++;
data->num_lines = i;
data->num_pages = (data->num_lines - 1) / data->lines_per_page + 1;
......@@ -72,10 +72,10 @@ draw_page (GtkPrintOperation *operation,
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);
......@@ -107,19 +107,19 @@ draw_page (GtkPrintOperation *operation,
pango_layout_get_pixel_size (layout, &text_width, &text_height);
cairo_move_to (cr, width - text_width - 4, (HEADER_HEIGHT - text_height) / 2);
pango_cairo_show_layout (cr, layout);
g_object_unref (layout);
layout = gtk_print_context_create_pango_layout (context);
desc = pango_font_description_from_string ("monospace");
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++)
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);
......@@ -131,7 +131,7 @@ draw_page (GtkPrintOperation *operation,
}
static void
end_print (GtkPrintOperation *operation,
end_print (GtkPrintOperation *operation,
GtkPrintContext *context,
gpointer user_data)
{
......@@ -149,7 +149,7 @@ do_printing (GtkWidget *do_widget)
GtkPrintOperation *operation;
GtkPrintSettings *settings;
PrintData *data;
gchar *uri;
gchar *uri, *dir, *ext;
GError *error = NULL;
operation = gtk_print_operation_new ();
......@@ -168,9 +168,15 @@ do_printing (GtkWidget *do_widget)
gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
settings = gtk_print_settings_new ();
uri = g_strconcat ("file://",
g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS),
"/gtk-demo-printing-example.pdf", NULL);
dir = g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS);
if (dir == NULL)
dir = g_get_home_dir ();
if (g_strcmp0 (gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT), "ps") == 0)
ext = ".ps";
else
ext = ".pdf";
uri = g_strconcat ("file://", dir, "/", "gtk-demo", ext, NULL);
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_URI, uri);
gtk_print_operation_set_print_settings (operation, settings);
......@@ -183,20 +189,20 @@ do_printing (GtkWidget *do_widget)
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);
gtk_widget_show (dialog);
}
return NULL;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment