2015-05-12 12:20:35 +00:00
|
|
|
/* Printing/Printing
|
2006-05-11 19:02:05 +00:00
|
|
|
*
|
|
|
|
* GtkPrintOperation offers a simple API to support printing
|
|
|
|
* in a cross-platform way.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
/* In points */
|
|
|
|
#define HEADER_HEIGHT (10*72/25.4)
|
|
|
|
#define HEADER_GAP (3*72/25.4)
|
|
|
|
|
2009-06-28 20:58:00 +00:00
|
|
|
typedef struct
|
2006-05-11 19:02:05 +00:00
|
|
|
{
|
2013-01-29 16:02:16 +00:00
|
|
|
gchar *resourcename;
|
2006-05-11 19:02:05 +00:00
|
|
|
gdouble font_size;
|
|
|
|
|
2009-06-28 20:58:00 +00:00
|
|
|
gint lines_per_page;
|
2006-05-11 19:02:05 +00:00
|
|
|
gchar **lines;
|
|
|
|
gint num_lines;
|
|
|
|
gint num_pages;
|
|
|
|
} PrintData;
|
|
|
|
|
|
|
|
static void
|
2009-06-28 20:58:00 +00:00
|
|
|
begin_print (GtkPrintOperation *operation,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkPrintContext *context,
|
|
|
|
gpointer user_data)
|
2006-05-11 19:02:05 +00:00
|
|
|
{
|
|
|
|
PrintData *data = (PrintData *)user_data;
|
2013-01-29 16:02:16 +00:00
|
|
|
GBytes *bytes;
|
2006-05-11 19:02:05 +00:00
|
|
|
int i;
|
|
|
|
double height;
|
|
|
|
|
|
|
|
height = gtk_print_context_get_height (context) - HEADER_HEIGHT - HEADER_GAP;
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
data->lines_per_page = floor (height / data->font_size);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2013-01-29 16:02:16 +00:00
|
|
|
bytes = g_resources_lookup_data (data->resourcename, 0, NULL);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
2013-01-29 16:02:16 +00:00
|
|
|
data->lines = g_strsplit (g_bytes_get_data (bytes, NULL), "\n", 0);
|
|
|
|
g_bytes_unref (bytes);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (data->lines[i] != NULL)
|
|
|
|
i++;
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
data->num_lines = i;
|
|
|
|
data->num_pages = (data->num_lines - 1) / data->lines_per_page + 1;
|
2008-03-31 21:45:22 +00:00
|
|
|
|
2006-05-12 09:47:58 +00:00
|
|
|
gtk_print_operation_set_n_pages (operation, data->num_pages);
|
2006-05-11 19:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
draw_page (GtkPrintOperation *operation,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkPrintContext *context,
|
|
|
|
gint page_nr,
|
|
|
|
gpointer user_data)
|
2006-05-11 19:02:05 +00:00
|
|
|
{
|
|
|
|
PrintData *data = (PrintData *)user_data;
|
|
|
|
cairo_t *cr;
|
|
|
|
PangoLayout *layout;
|
2007-04-28 14:06:25 +00:00
|
|
|
gint text_width, text_height;
|
|
|
|
gdouble width;
|
|
|
|
gint line, i;
|
2006-05-11 19:02:05 +00:00
|
|
|
PangoFontDescription *desc;
|
|
|
|
gchar *page_str;
|
|
|
|
|
2006-05-31 14:06:02 +00:00
|
|
|
cr = gtk_print_context_get_cairo_context (context);
|
2006-05-11 19:02:05 +00:00
|
|
|
width = gtk_print_context_get_width (context);
|
|
|
|
|
|
|
|
cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
|
|
|
|
cairo_fill_preserve (cr);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
cairo_set_source_rgb (cr, 0, 0, 0);
|
|
|
|
cairo_set_line_width (cr, 1);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
|
2006-05-31 14:06:02 +00:00
|
|
|
layout = gtk_print_context_create_pango_layout (context);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
|
|
|
desc = pango_font_description_from_string ("sans 14");
|
|
|
|
pango_layout_set_font_description (layout, desc);
|
|
|
|
pango_font_description_free (desc);
|
|
|
|
|
2013-01-29 16:02:16 +00:00
|
|
|
pango_layout_set_text (layout, data->resourcename, -1);
|
2007-04-28 14:06:25 +00:00
|
|
|
pango_layout_get_pixel_size (layout, &text_width, &text_height);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
2007-04-28 14:06:25 +00:00
|
|
|
if (text_width > width)
|
|
|
|
{
|
|
|
|
pango_layout_set_width (layout, width);
|
|
|
|
pango_layout_set_ellipsize (layout, PANGO_ELLIPSIZE_START);
|
|
|
|
pango_layout_get_pixel_size (layout, &text_width, &text_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_move_to (cr, (width - text_width) / 2, (HEADER_HEIGHT - text_height) / 2);
|
2006-05-11 19:02:05 +00:00
|
|
|
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);
|
2007-04-28 14:06:25 +00:00
|
|
|
|
|
|
|
pango_layout_set_width (layout, -1);
|
|
|
|
pango_layout_get_pixel_size (layout, &text_width, &text_height);
|
|
|
|
cairo_move_to (cr, width - text_width - 4, (HEADER_HEIGHT - text_height) / 2);
|
2006-05-11 19:02:05 +00:00
|
|
|
pango_cairo_show_layout (cr, layout);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
g_object_unref (layout);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-31 14:06:02 +00:00
|
|
|
layout = gtk_print_context_create_pango_layout (context);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2008-07-03 17:35:50 +00:00
|
|
|
desc = pango_font_description_from_string ("monospace");
|
2006-05-11 19:02:05 +00:00
|
|
|
pango_font_description_set_size (desc, data->font_size * PANGO_SCALE);
|
|
|
|
pango_layout_set_font_description (layout, desc);
|
|
|
|
pango_font_description_free (desc);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
cairo_move_to (cr, 0, HEADER_HEIGHT + HEADER_GAP);
|
|
|
|
line = page_nr * data->lines_per_page;
|
2009-06-28 20:58:00 +00:00
|
|
|
for (i = 0; i < data->lines_per_page && line < data->num_lines; i++)
|
2006-05-11 19:02:05 +00:00
|
|
|
{
|
|
|
|
pango_layout_set_text (layout, data->lines[line], -1);
|
|
|
|
pango_cairo_show_layout (cr, layout);
|
2008-07-03 17:37:07 +00:00
|
|
|
cairo_rel_move_to (cr, 0, data->font_size);
|
2006-05-11 19:02:05 +00:00
|
|
|
line++;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_object_unref (layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-06-28 20:58:00 +00:00
|
|
|
end_print (GtkPrintOperation *operation,
|
2011-09-02 03:55:47 +00:00
|
|
|
GtkPrintContext *context,
|
|
|
|
gpointer user_data)
|
2006-05-11 19:02:05 +00:00
|
|
|
{
|
|
|
|
PrintData *data = (PrintData *)user_data;
|
|
|
|
|
2013-01-29 16:02:16 +00:00
|
|
|
g_free (data->resourcename);
|
2006-05-11 19:02:05 +00:00
|
|
|
g_strfreev (data->lines);
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_printing (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
GtkPrintOperation *operation;
|
2009-06-25 05:23:50 +00:00
|
|
|
GtkPrintSettings *settings;
|
2006-05-11 19:02:05 +00:00
|
|
|
PrintData *data;
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
operation = gtk_print_operation_new ();
|
|
|
|
data = g_new0 (PrintData, 1);
|
2013-01-29 16:02:16 +00:00
|
|
|
data->resourcename = g_strdup ("/sources/printing.c");
|
2006-05-11 19:02:05 +00:00
|
|
|
data->font_size = 12.0;
|
|
|
|
|
2009-06-25 05:23:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (operation), "begin-print",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (begin_print), data);
|
2009-06-25 05:23:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (operation), "draw-page",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (draw_page), data);
|
2009-06-25 05:23:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (operation), "end-print",
|
2011-09-02 03:55:47 +00:00
|
|
|
G_CALLBACK (end_print), data);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
2008-02-22 22:38:47 +00:00
|
|
|
gtk_print_operation_set_use_full_page (operation, FALSE);
|
|
|
|
gtk_print_operation_set_unit (operation, GTK_UNIT_POINTS);
|
2009-07-10 09:28:31 +00:00
|
|
|
gtk_print_operation_set_embed_page_setup (operation, TRUE);
|
2008-02-22 22:38:47 +00:00
|
|
|
|
2009-06-25 05:23:50 +00:00
|
|
|
settings = gtk_print_settings_new ();
|
2012-05-25 03:12:36 +00:00
|
|
|
|
|
|
|
gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BASENAME, "gtk-demo");
|
2009-06-25 05:23:50 +00:00
|
|
|
gtk_print_operation_set_print_settings (operation, settings);
|
|
|
|
|
2006-06-07 15:09:03 +00:00
|
|
|
gtk_print_operation_run (operation, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (do_widget), &error);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
|
|
|
g_object_unref (operation);
|
2009-06-25 05:23:50 +00:00
|
|
|
g_object_unref (settings);
|
2006-05-11 19:02:05 +00:00
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog;
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
dialog = gtk_message_dialog_new (GTK_WINDOW (do_widget),
|
2011-09-02 03:55:47 +00:00
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_MESSAGE_ERROR,
|
|
|
|
GTK_BUTTONS_CLOSE,
|
|
|
|
"%s", error->message);
|
2006-05-11 19:02:05 +00:00
|
|
|
g_error_free (error);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
g_signal_connect (dialog, "response",
|
2020-05-09 14:26:22 +00:00
|
|
|
G_CALLBACK (gtk_window_destroy), NULL);
|
2009-06-28 20:58:00 +00:00
|
|
|
|
|
|
|
gtk_widget_show (dialog);
|
2006-05-11 19:02:05 +00:00
|
|
|
}
|
2009-06-28 20:58:00 +00:00
|
|
|
|
2006-05-11 19:02:05 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|