Re: Printing

Enrico Tröger wrote:
You will find the code attached, these are the issues:
I don't ;-(.

Let's try inline then:

/* February 2008, Bastiaan Veelo, Bastiaan Veelo net */

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

static GtkWidget *window = NULL;
static GtkPrintSettings *settings = NULL;
static GtkPageSetup *page_setup = NULL;
static gdouble scale100 = -1;
static GtkWidget *font_button = NULL;

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

/* We draw in mm, with (0,0) being the top left corner of the text area. */
static void
draw_page (cairo_t *cr)
 g_return_if_fail (page_setup != NULL);

 cairo_set_source_rgb (cr, 0, 0, 0);
 cairo_rectangle (cr,
 cairo_set_line_width (cr, .2);
 cairo_stroke (cr);

 /* Draw some lines */
 cairo_move_to (cr, 0, 0);
 cairo_line_to (cr,
                gtk_page_setup_get_page_width (page_setup, GTK_UNIT_MM),
                gtk_page_setup_get_page_height (page_setup, GTK_UNIT_MM));
cairo_move_to (cr, 0, gtk_page_setup_get_page_height (page_setup, GTK_UNIT_MM)); cairo_line_to (cr, gtk_page_setup_get_page_width (page_setup, GTK_UNIT_MM), 0);

 cairo_set_line_width (cr, 1);
 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
 cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);

 cairo_stroke (cr);

 PangoFontDescription *desc =
pango_font_description_from_string (gtk_font_button_get_font_name (GTK_FONT_BUTTON (font_button)));

 const gdouble dpi = 27.2;     /* ???? Misterious empirical value. */
 PangoLayout *pango_cairo_layout = pango_cairo_create_layout (cr);
 pango_layout_set_font_description (pango_cairo_layout, desc);
pango_cairo_context_set_resolution (pango_layout_get_context (pango_cairo_layout), dpi);
 cairo_font_options_t *options = cairo_font_options_create ();
 cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
 cairo_font_options_set_hint_style (options, CAIRO_HINT_STYLE_NONE);
pango_cairo_context_set_font_options (pango_layout_get_context (pango_cairo_layout), options);
 cairo_font_options_destroy (options);
 pango_layout_set_text (pango_cairo_layout,
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
                        "the quick brown fox jumped over the lazy dog "
"the quick brown fox jumped over the lazy dog", -1);
 pango_layout_set_width (pango_cairo_layout, 100*PANGO_SCALE);
 cairo_move_to (cr, 0, 0);
 pango_cairo_show_layout (cr, pango_cairo_layout);
 g_object_unref (pango_cairo_layout);
 pango_font_description_free (desc);

 cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
 cairo_set_font_size (cr, 12.0);       /* size em in mm */
 cairo_move_to(cr, 0., 120.);
 cairo_show_text(cr, "m Hello World! Printing...");
 cairo_move_to(cr, 0., 120.);
 cairo_line_to(cr, 12., 120.);
 cairo_set_line_width (cr, .2);
 cairo_stroke (cr);

 cairo_show_page (cr);

static void
print_page (GtkPrintOperation *operation, GtkPrintContext *context,
           gint page_nr, gpointer user_data)
g_debug("Print dpi = %f, scale = %f.", gtk_print_context_get_dpi_y (context), gtk_print_context_get_dpi_y (context)/25.4);
 cairo_t *cr = gtk_print_context_get_cairo_context (context);

 cairo_scale (cr,
              gtk_print_context_get_dpi_x (context)/25.4,
              gtk_print_context_get_dpi_y (context)/25.4);
 draw_page (cr);

void YGTK_page_setup()
 if (settings == NULL)
   settings = gtk_print_settings_new ();

 GtkPageSetup *new_page_setup =
gtk_print_run_page_setup_dialog (GTK_WINDOW (window), page_setup, settings);

 if (page_setup)
   g_object_unref (page_setup);

 page_setup = new_page_setup;
 gtk_widget_queue_draw (window);

void YGTK_print_dialog()
 GtkPrintOperation *print;
 GtkPrintOperationResult result;
 GError *error;

 print = gtk_print_operation_new ();

 if (settings != NULL)
   gtk_print_operation_set_print_settings (print, settings);
 g_return_if_fail (page_setup != NULL);
 gtk_print_operation_set_default_page_setup (print, page_setup);

 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 (window), &error);

   GtkWidget *error_dialog = gtk_message_dialog_new (GTK_WINDOW (window),
"Error printing file:\n%s",
   g_signal_connect (error_dialog, "response",
                     G_CALLBACK (gtk_widget_destroy), NULL);
   gtk_widget_show (error_dialog);
   g_error_free (error);
   if (settings != NULL)
     g_object_unref (settings);
settings = g_object_ref (gtk_print_operation_get_print_settings (print));

 g_object_unref (print);

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

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

 g_return_val_if_fail (page_setup != NULL, FALSE);

 cairo_translate (cr,
gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM) * scale100, gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM) * scale100);
    /* See also cairo_surface_set_device_offset() */

 cairo_scale (cr, scale100, scale100);

 draw_page (cr);


 return FALSE;

static void
export_to_PDF (GtkButton *button, gpointer data)
 GtkWidget *dialog =
     gtk_file_chooser_dialog_new ("Export PDF File",
                                   GTK_WINDOW (window),
                                   GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                   GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);

gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), "printout.pdf");
 GtkFileFilter *pdf_filter = gtk_file_filter_new();
 gtk_file_filter_set_name( pdf_filter, "Portable Document Format Files" );
 gtk_file_filter_add_pattern( pdf_filter, "*.pdf" );
 gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( dialog ), pdf_filter );
 GtkFileFilter *all_files_filter = gtk_file_filter_new();
 gtk_file_filter_set_name( all_files_filter, "All Files" );
 gtk_file_filter_add_pattern( all_files_filter, "*" );
gtk_file_chooser_add_filter( GTK_FILE_CHOOSER( dialog ), all_files_filter );

 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
   if (!g_str_has_suffix (filename, ".pdf"))
     gchar *result = g_strconcat (filename, ".pdf", NULL);
     g_free (filename);
     filename = result;

   g_return_if_fail (page_setup != NULL);
   cairo_surface_t *surface =
       cairo_pdf_surface_create (filename,
gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS), gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS));
   cairo_t *cr = cairo_create (surface);
   g_debug("PDF dpi = %f (points per inch), scale = %f.", 72., 72./25.4);

   cairo_translate (cr,
gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM) * 72 / 25.4, gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM) * 72 / 25.4);
      /* See also cairo_surface_set_device_offset() */

   cairo_scale (cr, 72/25.4, 72/25.4);

   draw_page (cr);

   cairo_surface_destroy (surface);
   g_free (filename);

 gtk_widget_destroy (dialog);

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

 page_setup = gtk_page_setup_new();
 GtkPaperSize *A4 = gtk_paper_size_new (GTK_PAPER_NAME_A4);
 gtk_page_setup_set_paper_size_and_default_margins (page_setup, A4);
 gtk_page_setup_set_top_margin (page_setup, 25., GTK_UNIT_MM);
 gtk_page_setup_set_bottom_margin (page_setup, 25., GTK_UNIT_MM);
 gtk_page_setup_set_left_margin (page_setup, 25., GTK_UNIT_MM);
 gtk_page_setup_set_right_margin (page_setup, 25., GTK_UNIT_MM);

 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 *page_button = gtk_button_new_with_mnemonic ("Page _Setup");
 gtk_box_pack_start (GTK_BOX (hbox), page_button, FALSE, FALSE, 0);
 g_signal_connect (page_button, "clicked",
                   G_CALLBACK (YGTK_page_setup), NULL);

 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 (YGTK_print_dialog), NULL);

 GtkWidget *pdf_button = gtk_button_new_with_mnemonic ("_Export to PDF");
 gtk_box_pack_start (GTK_BOX (hbox), pdf_button, FALSE, FALSE, 0);
 g_signal_connect (pdf_button, "clicked",
                   G_CALLBACK (export_to_PDF), NULL);
 gtk_widget_set_sensitive (pdf_button, FALSE);

 font_button = gtk_font_button_new_with_font("Sans 12");
 gtk_box_pack_start (GTK_BOX (hbox), font_button, FALSE, FALSE, 0);

 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),

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);

 scale100 = gdk_screen_get_resolution (gdk_screen_get_default()) / 25.4;
 if (scale100 < 0) scale100 = 96. / 25.4;    /* Fall back to default. */
 g_debug("Screen dpi = %f, scale = %f.", scale100*25.4, scale100);

 gtk_layout_set_size (GTK_LAYOUT (drawing),
scale100 * gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM), scale100 * gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM));

 g_signal_connect(drawing, "expose-event",
                  G_CALLBACK (on_expose_event), NULL);
 g_signal_connect_swapped (font_button, "font-set",
                           G_CALLBACK(gtk_widget_queue_draw), drawing);
 GtkRequisition scrollbar_size;
gtk_widget_size_request (gtk_scrolled_window_get_vscrollbar (GTK_SCROLLED_WINDOW (scrolled)),
 gtk_window_set_default_size (GTK_WINDOW (window),
scale100 * gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM) +
                                  scrollbar_size.width + 5,
gdk_screen_get_height (gdk_screen_get_default()));

 gtk_widget_show_all (window);


 gtk_paper_size_free (A4);
 return 0;

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]