capture window for printing



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.
Furthermore, the window is sometimes "incomplete". The PDF/PS output shows just parts of the window - it seems that print_win is captured before it is completely rendered.

Questions:
- Are there any better ways to achieve what I want?
- Can I capture the window without showing it?

Regards,
Fabian



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