widget-factory: Implement the print action

Implement enough here to show a print dialog.
This commit is contained in:
Matthias Clasen 2020-04-26 10:44:55 -04:00
parent 8de774dde1
commit f6af2d63b0

View File

@ -323,12 +323,95 @@ activate_inspector (GSimpleAction *action,
gtk_window_set_interactive_debugging (TRUE);
}
static void
print_operation_done (GtkPrintOperation *op,
GtkPrintOperationResult res,
gpointer data)
{
GError *error = NULL;
switch (res)
{
case GTK_PRINT_OPERATION_RESULT_ERROR:
gtk_print_operation_get_error (op, &error);
g_print ("Printing failed: %s\n", error->message);
g_clear_error (&error);
break;
case GTK_PRINT_OPERATION_RESULT_APPLY:
break;
case GTK_PRINT_OPERATION_RESULT_CANCEL:
g_print ("Printing was canceled\n");
break;
case GTK_PRINT_OPERATION_RESULT_IN_PROGRESS:
return;
default:
g_assert_not_reached ();
break;
}
g_object_unref (op);
}
static void
print_operation_begin (GtkPrintOperation *op,
GtkPrintContext *context,
gpointer data)
{
gtk_print_operation_set_n_pages (op, 1);
}
static void
print_operation_page (GtkPrintOperation *op,
GtkPrintContext *context,
int page,
gpointer data)
{
cairo_t *cr;
double width;
double aspect_ratio;
GdkSnapshot *snapshot;
GdkPaintable *paintable;
GskRenderNode *node;
g_print ("Save the trees!\n");
cr = gtk_print_context_get_cairo_context (context);
width = gtk_print_context_get_width (context);
snapshot = gtk_snapshot_new ();
paintable = gtk_widget_paintable_new (GTK_WIDGET (data));
aspect_ratio = gdk_paintable_get_intrinsic_aspect_ratio (paintable);
gdk_paintable_snapshot (paintable, snapshot, width, width / aspect_ratio);
node = gtk_snapshot_free_to_node (snapshot);
gsk_render_node_draw (node, cr);
gsk_render_node_unref (node);
g_object_unref (paintable);
}
static void
activate_print (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_print ("Activate action %s\n", g_action_get_name (G_ACTION (action)));
GtkWindow *window = GTK_WINDOW (user_data);
GtkPrintOperation *op;
GtkPrintOperationResult res;
op = gtk_print_operation_new ();
gtk_print_operation_set_allow_async (op, TRUE);
g_signal_connect (op, "begin-print", G_CALLBACK (print_operation_begin), NULL);
g_signal_connect (op, "draw-page", G_CALLBACK (print_operation_page), window);
g_signal_connect (op, "done", G_CALLBACK (print_operation_done), NULL);
res = gtk_print_operation_run (op, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, window, NULL);
if (res == GTK_PRINT_OPERATION_RESULT_IN_PROGRESS)
return;
print_operation_done (op, res, NULL);
}
static void