Code

add #include <io.h> to enable win32 compile
[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
16 #include <gtkmm/stock.h>
17 #include <io.h>
18 #include "print.h"
20 #include "extension/internal/cairo-render-context.h"
21 #include "extension/internal/cairo-renderer.h"
22 #include "ui/widget/rendering-options.h"
24 #include "unit-constants.h"
25 #include "helper/png-write.h"
26 #include "svg/svg-color.h"
28 namespace Inkscape {
29 namespace UI {
30 namespace Dialog {
32 void
33 Print::_draw_page (const Glib::RefPtr<Gtk::PrintContext> &context,
34                      int page_nr)
35 {
36     if (_tab.as_bitmap()) {
37         // Render as exported PNG
38         gdouble width = sp_document_width(_doc);
39         gdouble height = sp_document_height(_doc);
40         gdouble dpi = _tab.bitmap_dpi();
41         std::string tmp_png;
42         std::string tmp_base = "inkscape-print-png-XXXXXX";
44         int tmp_fd;
45         if ( (tmp_fd = Glib::file_open_tmp (tmp_png, tmp_base)) >= 0) {
46             close(tmp_fd);
48             guint32 bgcolor = 0x00000000;
49             Inkscape::XML::Node *nv = sp_repr_lookup_name (_doc->rroot, "sodipodi:namedview");
50             if (nv && nv->attribute("pagecolor"))
51                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
52             if (nv && nv->attribute("inkscape:pageopacity"))
53                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
55             sp_export_png_file(_doc, tmp_png.c_str(), 0.0, 0.0,
56                 width, height,
57                 (unsigned long)width * dpi / PX_PER_IN,
58                 (unsigned long)height * dpi / PX_PER_IN,
59                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
61             // This doesn't seem to work:
62             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
63             //
64             // so we'll use a surface pattern blat instead...
65             //
66             // but the C++ interface isn't implemented in cairomm:
67             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
68             //
69             // so do it in C:
70             {
71                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
72                 cairo_t *cr = context->get_cairo_context ()->cobj();
73                 // FIXME: why is the origin offset??
74                 cairo_set_source_surface(cr, png->cobj(), -20.0, -20.0);
75             }
76             context->get_cairo_context ()->paint ();
78             // Clean up
79             unlink (tmp_png.c_str());
80         }
81         else {
82             g_warning(_("Could not open temporary PNG for bitmap printing"));
83         }
84     }
85     else {
86         // Render as vectors
87         Inkscape::Extension::Internal::CairoRenderer renderer;
88         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
89         bool ret = ctx->setSurfaceTarget (context->get_cairo_context ()->get_target ()->cobj(), true);
90         if (ret) {
91             ret = renderer.setupDocument (ctx, _doc);
92             if (ret) {
93                 renderer.renderItem(ctx, _base);
94                 ret = ctx->finish();
95             }
96             else {
97                 g_warning(_("Could not set up Document"));
98             }
99         }
100         else {
101             g_warning(_("Failed to set CairoRenderContext"));
102         }
104         // Clean up
105         renderer.destroyContext(ctx);
106     }
110 Gtk::Widget *
111 Print::_create_custom_widget ()
113     return &_tab;
116 void
117 Print::_custom_widget_apply (Gtk::Widget *widget)
119     g_warning (_("custom widget apply"));
122 Print::Print(SPDocument *doc, SPItem *base) :
123     _doc (doc),
124     _base (base),
125     _tab ()
127     g_assert (_doc);
128     g_assert (_base);
130     _printop = Gtk::PrintOperation::create();
132     // set up dialog title, based on document name
133     gchar *jobname = _doc->name ? _doc->name : _("SVG Document");
134     Glib::ustring title = _("Print");
135     title += " ";
136     title += jobname;
137     _printop->set_job_name (title);
138     _printop->set_n_pages (1);
140     // set up paper size to match the document size
141     Glib::RefPtr<Gtk::PageSetup> page_setup = Gtk::PageSetup::create();
142     gdouble doc_width = sp_document_width(_doc) * PT_PER_PX;
143     gdouble doc_height = sp_document_height(_doc) * PT_PER_PX;
144     Gtk::PaperSize paper_size(Glib::ustring("custom"), Glib::ustring("custom"),
145                               doc_width, doc_height, Gtk::UNIT_POINTS);
146     page_setup->set_paper_size (paper_size);
147     _printop->set_default_page_setup (page_setup);
149     // build custom preferences tab
150     _printop->set_custom_tab_label (Glib::ustring(_("Rendering")));
151     _printop->signal_create_custom_widget().connect(sigc::mem_fun(*this, &Print::_create_custom_widget));
152 //    _printop->signal_custom_widget_apply().connect(sigc::mem_fun(*this, &Print::_custom_widget_apply));
154     // register actual page surface drawing callback
155     _printop->signal_draw_page().connect(sigc::mem_fun(*this, &Print::_draw_page));
159 Gtk::PrintOperationResult
160 Print::run(Gtk::PrintOperationAction action)
162     Gtk::PrintOperationResult res;
163     res = _printop->run (action);
164     return res;
168 } // namespace Dialog
169 } // namespace UI
170 } // namespace Inkscape