Code

clean up print dialog for better integration with gtkmm
[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"
30 namespace Inkscape {
31 namespace UI {
32 namespace Dialog {
34 void
35 Print::on_begin_print(const Glib::RefPtr<Gtk::PrintContext> &context)
36 {
37     //printf("%s\n",__FUNCTION__);
38     set_n_pages (1);
39 }
41 bool
42 Print::on_paginate(const Glib::RefPtr<Gtk::PrintContext> &context)
43 {
44     /* this should not be called since begin-print sets the n pages */
45     //printf("%s\n",__FUNCTION__);
46     set_n_pages (1);
47     return false;
48 }
50 void
51 Print::on_draw_page (const Glib::RefPtr<Gtk::PrintContext> &context, int page_nr)
52 {
53     //printf("%s %d\n",__FUNCTION__, page_nr);
54     if (_tab.as_bitmap()) {
55         // Render as exported PNG
56         gdouble width = sp_document_width(_doc);
57         gdouble height = sp_document_height(_doc);
58         gdouble dpi = _tab.bitmap_dpi();
59         std::string tmp_png;
60         std::string tmp_base = "inkscape-print-png-XXXXXX";
62         int tmp_fd;
63         if ( (tmp_fd = Glib::file_open_tmp (tmp_png, tmp_base)) >= 0) {
64             close(tmp_fd);
66             guint32 bgcolor = 0x00000000;
67             Inkscape::XML::Node *nv = sp_repr_lookup_name (_doc->rroot, "sodipodi:namedview");
68             if (nv && nv->attribute("pagecolor"))
69                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
70             if (nv && nv->attribute("inkscape:pageopacity"))
71                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
73             sp_export_png_file(_doc, tmp_png.c_str(), 0.0, 0.0,
74                 width, height,
75                 (unsigned long)(width * dpi / PX_PER_IN),
76                 (unsigned long)(height * dpi / PX_PER_IN),
77                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
79             // This doesn't seem to work:
80             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
81             //
82             // so we'll use a surface pattern blat instead...
83             //
84             // but the C++ interface isn't implemented in cairomm:
85             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
86             //
87             // so do it in C:
88             {
89                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
90                 cairo_t *cr = context->get_cairo_context ()->cobj();
91                 // FIXME: why is the origin offset??
92                 cairo_set_source_surface(cr, png->cobj(), -20.0, -20.0);
93             }
94             context->get_cairo_context ()->paint ();
96             // Clean up
97             unlink (tmp_png.c_str());
98         }
99         else {
100             g_warning(_("Could not open temporary PNG for bitmap printing"));
101         }
102     }
103     else {
104         // Render as vectors
105         Inkscape::Extension::Internal::CairoRenderer renderer;
106         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
107         bool ret = ctx->setSurfaceTarget (context->get_cairo_context ()->get_target ()->cobj(), true);
108         if (ret) {
109             ret = renderer.setupDocument (ctx, _doc);
110             if (ret) {
111                 renderer.renderItem(ctx, _base);
112                 ret = ctx->finish();
113             }
114             else {
115                 g_warning(_("Could not set up Document"));
116             }
117         }
118         else {
119             g_warning(_("Failed to set CairoRenderContext"));
120         }
122         // Clean up
123         renderer.destroyContext(ctx);
124     }
128 Gtk::Widget *
129 Print::on_create_custom_widget ()
131     return &_tab;
134 Print::Print(SPDocument *doc, SPItem *base) :
135     Gtk::PrintOperation (),
136     _doc (doc),
137     _base (base),
138     _tab ()
140     g_assert (_doc);
141     g_assert (_base);
143     // set up dialog title, based on document name
144     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
145     Glib::ustring title = _("Print");
146     title += " ";
147     title += jobname;
148     set_job_name (title);
149     set_n_pages (1);
151     // set up paper size to match the document size
152     Glib::RefPtr<Gtk::PageSetup> page_setup = Gtk::PageSetup::create();
153     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
154     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
155     Gtk::PaperSize paper_size(Glib::ustring("custom"), Glib::ustring("custom"),
156                               doc_width, doc_height, Gtk::UNIT_POINTS);
157     page_setup->set_paper_size (paper_size);
158     set_default_page_setup (page_setup);
160     // build custom preferences tab
161     set_custom_tab_label (Glib::ustring(_("Rendering")));
165 } // namespace Dialog
166 } // namespace UI
167 } // namespace Inkscape