[cairo] Re: capture window for printing

Fabian Förg fabian.foerg at gmx.de
Wed Jan 24 06:38:17 PST 2007


Fabian Förg wrote:
> Hello,
>
> as the topic suggests, I am trying to capture a window which should be 
> printed afterwards.
>
> This is my code:
>
> /* appl_t is a struct created by me which contains "GtkWidget 
> *print_win",
> * the window I want to print. Moreover, appl_t contains gints for the
> * width and height of the window */
>
> /* callback for my print button */
> static void print_cb(appl_t * appl)
> {
>    values_t *val = &appl->values;
>    GtkPrintOperation *op;
>    GtkPrintOperationResult res;
>
>    output_print(appl); /* This function creates the window which I 
> want to print */
>
>    op = gtk_print_operation_new();
>
>    gtk_print_operation_set_n_pages(op, 1);
>    gtk_print_operation_set_unit(op, GTK_UNIT_MM);
>    g_signal_connect(op, "draw_page", G_CALLBACK(draw_page), appl);
>    res =
>        gtk_print_operation_run(op, 
> GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
>                    NULL, NULL);
>    }
> }
>
> static void draw_page(GtkPrintOperation * operation,
>              GtkPrintContext * print_context, gint page_nr,
>              appl_t * appl)
> {
>    gdouble print_width, print_height, scale;
>    cairo_t *cr, *cr_win;
>    cairo_surface_t *surface;
>    values_t *val = &appl->values;
>
>    /* create a surface of the print window */
>    cr_win = gdk_cairo_create(appl->print_win->window);
>
>    /* get width and height of the print window */
>    gdk_drawable_get_size(appl->print_win->window, &(val->width), 
> &(val->height));
>
>    gtk_widget_hide_all(appl->print_win);
>
>    /* create the cairo surface for cr_win */
>    surface = cairo_get_target(cr_win);
>    cairo_surface_reference(surface);
>    cairo_destroy(cr_win);
>
>    /* get width and heigth of print_context */
>    print_width = gtk_print_context_get_width(print_context);
>    print_height = gtk_print_context_get_height(print_context);
>
>    /* transform the print_context into a cairo_context */
>    cr = gtk_print_context_get_cairo_context(print_context);
>
>    scale = (gdouble) print_width / val->width;
>
>    /* scale, keep aspect ratio */
>    cairo_scale(cr, scale, scale);
>
>    cairo_set_source_surface(cr, surface, 0., 0.);
>    cairo_surface_destroy(surface);
>
>    cairo_paint(cr);
> }
>
> The problem is that the window is often captured with the print dialog.
This is most likely due to the fact that the window is captured when the 
draw_page signal is emitted. I. e. gdk_cairo_create creates a cairo_t * 
when the print button of the print dialog is pressed, and so the print 
dialog is also on the resulting paper. Is this correct?
When the gdk_cairo_create function is inserted after output print, but 
before gtk_print_operation_new, it is the same. Thus, the window is 
supposed to be captured BEFORE even creating the Gtk print dialog, but 
why doesn't it make any difference?
Moreover, the window is maximized, but the correct width and height of 
the window are only retrieved if gdk_drawable_get_size is called in the 
draw_page callback funtion draw_page. If it is called after output_win, 
gdk_drawable_get_size retrieves the width and height of the "shrinked" 
window. Why?
> [...]
>
> Questions:
> - Are there any better ways to achieve what I want?
> - Can I capture the window without showing it?
?
> Regards,
> Fabian


More information about the cairo mailing list