GtkPrintUnixDialog and Cairo problem



Hi again,

Well, looking into the archives, when people asks about printing problems, they are never answered :-(

I'll try again. :-)

There's not much info on the net, about this subject.

I have made the following code working, when printing a pdf-file to a printer. Remarking this code and put code for cairo layout, then nothing prints out. The code doesn't make an error, but it results in an empty spooljob, accordig to CUPS.


Here's the code:
Compile with: gcc ctest4.c -o ctest4 -export-dynamic `pkg-config --cflags --libs gtk+-unix-print-2.0`

*****************************************
#include <gtk/gtk.h>
#include <gtk/gtkunixprint.h>


static void print_end(GtkPrintJob *print_job, char *tr, GError *err)
{
    if (err == NULL)
        return;
    g_assert(err == NULL);
}

static void print_page(GtkWidget *widget, gpointer user_data)
{
    GtkWindow        *parent = user_data;
    GtkPrintJob        *print_job;
    GtkPrinter        *printer;
    GtkPrintSettings    *settings;
    GtkPageSetup        *page_setup;
    GtkWidget        *dialog;
    GError            *err = NULL;
    gboolean        status;
    GtkPrintUnixDialog *print_dialog;
    gint result;

    PangoLayout *layout;
    gdouble width, text_height;
    gint layout_height;
    PangoFontDescription *desc;

    cairo_surface_t *cs;
    cairo_t *cr;


    dialog = gtk_print_unix_dialog_new("test",parent);
    if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_OK) {
        gtk_widget_destroy(dialog);
        puts("Abort");
        return;
    }
    print_dialog = GTK_PRINT_UNIX_DIALOG (dialog);
//gtk_print_unix_dialog_set_page_setup (print_dialog, printing_get_page_setup()); //gtk_print_unix_dialog_set_settings (print_dialog, printing_get_settings());

//gtk_print_unix_dialog_set_manual_capabilities(print_dialog, GTK_PRINT_CAPABILITY_GENERATE_PS); //gtk_print_unix_dialog_set_manual_capabilities(print_dialog, GTK_PRINT_CAPABILITY_PREVIEW);

     //result = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_hide (dialog);

printer = gtk_print_unix_dialog_get_selected_printer( GTK_PRINT_UNIX_DIALOG(print_dialog));
    if (!gtk_printer_accepts_ps(printer)) {
printf("Printer %s doesn't accept PostScript files.", gtk_printer_get_name(printer));
         return;
    }

settings = gtk_print_unix_dialog_get_settings( GTK_PRINT_UNIX_DIALOG(dialog)); page_setup = gtk_print_unix_dialog_get_page_setup( GTK_PRINT_UNIX_DIALOG(dialog));
    //gtk_widget_destroy(dialog);

print_job = gtk_print_job_new("Example title", printer, settings, page_setup);

    /* Print file works !!! */
    /*
status = gtk_print_job_set_source_file(print_job, "/home/oospooler/oospooler/tr/1000.pdf", &err);
    if (err != NULL) {
        g_assert(status != FALSE);
        fprintf(stderr, "Unable to print file: %s\n", err->message);
        g_error_free(err);
        err = NULL;
    }
    */

    /* Cairo doesn't work !!!  vvv */
    cs=gtk_print_job_get_surface(print_job,&err);
    cr=cairo_create(cs);

    cairo_set_source_rgb(cr, 0.1, 0.1, 0.1);

cairo_select_font_face(cr, "Purisa", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);

    cairo_set_font_size(cr, 13);

    cairo_move_to(cr, 20, 30);
    cairo_show_text(cr, "Most relationships seem so transitory");
    cairo_move_to(cr, 20, 60);
    cairo_show_text(cr, "They're all good but not the permanent one");

    cairo_move_to(cr, 20, 120);
    cairo_show_text(cr, "Who doesn't long for someone to hold");

    cairo_move_to(cr, 20, 150);
    cairo_show_text(cr, "Who knows how to love you without being told");
    cairo_move_to(cr, 20, 180);
    cairo_show_text(cr, "Somebody tell me why I'm on my own");
    cairo_move_to(cr, 20, 210);
    cairo_show_text(cr, "If there's a soulmate for everyone");
    /* Cairo ^^^ */


gtk_print_job_send(print_job, (GtkPrintJobCompleteFunc)print_end, NULL, NULL);

    g_object_unref(print_job);
    g_object_unref(page_setup);
    g_object_unref(settings);
    g_object_unref(printer);
}

main(int argc, char **argv)
{
    GtkWidget        *window;
    GtkWidget        *button;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);

    button = gtk_button_new_with_label("Print");
    gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(button, "clicked", G_CALLBACK(print_page), GTK_WINDOW(window));
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}
*****************************************

Am I missing something ?


Regards

Thomas




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