Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <gtk/gtk.h>
- #include <gtk/gtkunixprint.h>
- #include <webkit2/webkit2.h>
- void gtk_wait() {
- while (gtk_events_pending()) {
- gtk_main_iteration_do(true);
- }
- }
- gboolean find_file_printer(GtkPrinter* printer, char** data) {
- printf("check_printer\n");
- if (!g_strcmp0(G_OBJECT_TYPE_NAME(gtk_printer_get_backend(printer)), "GtkPrintBackendFile")) {
- *data = g_strdup(gtk_printer_get_name(printer));
- return TRUE;
- }
- return FALSE;
- }
- void printFinished(WebKitPrintOperation* op, gpointer data) {
- printf("finished\n");
- }
- void printFailed(WebKitPrintOperation* op, gpointer error, gpointer data) {
- printf("failed\n");
- }
- int main(int argc, char** argv) {
- printf("gtk_init\n");
- gtk_init(0, NULL);
- printf("new_window\n");
- GtkWidget* window = gtk_offscreen_window_new();
- gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);
- printf("create_webview\n");
- WebKitWebView* webView = WEBKIT_WEB_VIEW(webkit_web_view_new());
- gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(webView));
- printf("load_uri\n");
- webkit_web_view_load_uri(webView, "http://http.rip/");
- // Make sure that when the browser area becomes visible, it will get mouse
- // and keyboard events
- printf("get_focus\n");
- gtk_widget_grab_focus(GTK_WIDGET(webView));
- // Make sure the main window and all its contents are visible
- printf("show\n");
- gtk_widget_show_all(window);
- printf("wait\n");
- // Run the main GTK+ event loop
- gtk_wait();
- printf("page_setup\n");
- WebKitPrintOperation* op = webkit_print_operation_new(webView);
- GtkPageSetup* setup = gtk_page_setup_new();
- GtkPaperSize* paperSize = gtk_paper_size_new(gtk_paper_size_get_default());
- gtk_page_setup_set_paper_size_and_default_margins(setup, paperSize);
- gtk_page_setup_set_left_margin(setup, 0, GTK_UNIT_POINTS);
- gtk_page_setup_set_right_margin(setup, 0, GTK_UNIT_POINTS);
- gtk_page_setup_set_top_margin(setup, 0, GTK_UNIT_POINTS);
- gtk_page_setup_set_bottom_margin(setup, 0, GTK_UNIT_POINTS);
- webkit_print_operation_set_page_setup(op, setup);
- // settings
- printf("settings\n");
- GtkPrintSettings* settings = gtk_print_settings_new();
- gtk_print_settings_set_orientation(settings, GTK_PAGE_ORIENTATION_PORTRAIT);
- gtk_print_settings_set_quality(settings, GTK_PRINT_QUALITY_HIGH);
- printf("printer\n");
- char* printer = NULL;
- gtk_enumerate_printers((GtkPrinterFunc)find_file_printer, &printer, NULL, TRUE);
- printf("found_printer %s\n", printer);
- gtk_print_settings_set_printer(settings, printer);
- //delete printer;
- printf("set_output\n");
- // gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BASENAME, "gtk-demo");
- // gtk_print_settings_set(settings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT, "PDF");
- // webkit_print_operation_set_print_settings(op, settings);
- // print
- g_signal_connect(op, "failed", G_CALLBACK(printFailed), NULL);
- g_signal_connect(op, "finished", G_CALLBACK(printFinished), NULL);
- printf("do_print\n");
- webkit_print_operation_print(op);
- printf("cleanup\n");
- gtk_wait();
- g_object_unref(op);
- g_object_unref(settings);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement