Code

Warning and whitespace cleanup
[inkscape.git] / src / ui / dialog / print.cpp
1 /**
2  * \brief Print dialog
3  *
4  * Authors:
5  *   Kees Cook <kees@outflux.net>
6  *
7  * Copyright (C) 2007 Kees Cook
8  *
9  * Released under GNU GPL.  Read the file 'COPYING' for more information.
10  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
15 #ifdef WIN32
16 #include <io.h>
17 #endif
19 #include <gtkmm/stock.h>
20 #include "print.h"
22 #include "extension/internal/cairo-render-context.h"
23 #include "extension/internal/cairo-renderer.h"
24 #include "ui/widget/rendering-options.h"
26 #include "unit-constants.h"
27 #include "helper/png-write.h"
28 #include "svg/svg-color.h"
31 static void
32 draw_page (GtkPrintOperation */*operation*/,
33            GtkPrintContext   *context,
34            gint               /*page_nr*/,
35            gpointer           user_data)
36 {
37     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
38     //printf("%s %d\n",__FUNCTION__, page_nr);
40     if (junk->_tab->as_bitmap()) {
41         // Render as exported PNG
42         gdouble width = sp_document_width(junk->_doc);
43         gdouble height = sp_document_height(junk->_doc);
44         gdouble dpi = junk->_tab->bitmap_dpi();
45         std::string tmp_png;
46         std::string tmp_base = "inkscape-print-png-XXXXXX";
48         int tmp_fd;
49         if ( (tmp_fd = Glib::file_open_tmp (tmp_png, tmp_base)) >= 0) {
50             close(tmp_fd);
52             guint32 bgcolor = 0x00000000;
53             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
54             if (nv && nv->attribute("pagecolor"))
55                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
56             if (nv && nv->attribute("inkscape:pageopacity"))
57                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
59             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
60                 width, height,
61                 (unsigned long)(width * PT_PER_IN / PX_PER_IN),
62                 (unsigned long)(height * PT_PER_IN / PX_PER_IN),
63                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
65             // This doesn't seem to work:
66             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
67             //
68             // so we'll use a surface pattern blat instead...
69             //
70             // but the C++ interface isn't implemented in cairomm:
71             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
72             //
73             // so do it in C:
74             {
75                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
76                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
77                 // FIXME: why is the origin offset??
78                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
79             }
80             cairo_paint(gtk_print_context_get_cairo_context (context));
82             // Clean up
83             unlink (tmp_png.c_str());
84         }
85         else {
86             g_warning(_("Could not open temporary PNG for bitmap printing"));
87         }
88     }
89     else {
90         // Render as vectors
91         Inkscape::Extension::Internal::CairoRenderer renderer;
92         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
93         bool ret = ctx->setSurfaceTarget (cairo_get_target (gtk_print_context_get_cairo_context (context)), true);
94         if (ret) {
95             ret = renderer.setupDocument (ctx, junk->_doc);
96             if (ret) {
97                 renderer.renderItem(ctx, junk->_base);
98                 ret = ctx->finish();
99             }
100             else {
101                 g_warning(_("Could not set up Document"));
102             }
103         }
104         else {
105             g_warning(_("Failed to set CairoRenderContext"));
106         }
108         // Clean up
109         renderer.destroyContext(ctx);
110     }
114 static GObject*
115 create_custom_widget (GtkPrintOperation */*operation*/,
116                       gpointer           user_data)
118     //printf("%s\n",__FUNCTION__);
119     return G_OBJECT(user_data);
122 static void
123 begin_print (GtkPrintOperation *operation,
124              GtkPrintContext   */*context*/,
125              gpointer           /*user_data*/)
127     //printf("%s\n",__FUNCTION__);
128     gtk_print_operation_set_n_pages (operation, 1);
131 namespace Inkscape {
132 namespace UI {
133 namespace Dialog {
135 Print::Print(SPDocument *doc, SPItem *base) :
136     _doc (doc),
137     _base (base)
139     g_assert (_doc);
140     g_assert (_base);
142     _printop = gtk_print_operation_new ();
144     // set up dialog title, based on document name
145     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
146     Glib::ustring title = _("Print");
147     title += " ";
148     title += jobname;
149     gtk_print_operation_set_job_name (_printop, title.c_str());
151     // set up paper size to match the document size
152     GtkPageSetup *page_setup = gtk_page_setup_new();
153     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
154     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
155     GtkPaperSize *paper_size = gtk_paper_size_new_custom("custom", "custom",
156                                 doc_width, doc_height, GTK_UNIT_POINTS);
157     gtk_page_setup_set_paper_size (page_setup, paper_size);
158     gtk_print_operation_set_default_page_setup (_printop, page_setup);
160     // set up signals
161     _workaround._doc = _doc;
162     _workaround._base = _base;
163     _workaround._tab = &_tab;
164     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
165     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
166     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
168     // build custom preferences tab
169     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
172 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window&)
174     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, NULL, NULL);
175     return Gtk::PRINT_OPERATION_RESULT_APPLY;
179 } // namespace Dialog
180 } // namespace UI
181 } // namespace Inkscape