Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / ui / dialog / print.cpp
1 /** @file
2  * @brief Print dialog
3  */
4 /* Authors:
5  *   Kees Cook <kees@outflux.net>
6  *   Abhishek Sharma
7  *
8  * Copyright (C) 2007 Kees Cook
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 #include <windows.h>
18 #endif
20 #include <gtkmm/stock.h>
21 #include "print.h"
23 #include "extension/internal/cairo-render-context.h"
24 #include "extension/internal/cairo-renderer.h"
25 #include "ui/widget/rendering-options.h"
26 #include "document.h"
28 #include "unit-constants.h"
29 #include "helper/png-write.h"
30 #include "svg/svg-color.h"
31 #include "io/sys.h"
35 static void draw_page(
36 #ifdef WIN32
37                       GtkPrintOperation *operation,
38 #else
39                       GtkPrintOperation *,
40 #endif
41                       GtkPrintContext   *context,
42                       gint               /*page_nr*/,
43                       gpointer           user_data)
44 {
45     struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data;
46     //printf("%s %d\n",__FUNCTION__, page_nr);
48     if (junk->_tab->as_bitmap()) {
49         // Render as exported PNG
50         gdouble width = (junk->_doc)->getWidth();
51         gdouble height = (junk->_doc)->getHeight();
52         gdouble dpi = junk->_tab->bitmap_dpi();
53         std::string tmp_png;
54         std::string tmp_base = "inkscape-print-png-XXXXXX";
56         int tmp_fd;
57         if ( (tmp_fd = Inkscape::IO::file_open_tmp (tmp_png, tmp_base)) >= 0) {
58             close(tmp_fd);
60             guint32 bgcolor = 0x00000000;
61             Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview");
62             if (nv && nv->attribute("pagecolor"))
63                 bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00);
64             if (nv && nv->attribute("inkscape:pageopacity"))
65                 bgcolor |= SP_COLOR_F_TO_U(sp_repr_get_double_attribute (nv, "inkscape:pageopacity", 1.0));
67             sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0,
68                 width, height,
69                 (unsigned long)(width * dpi / PX_PER_IN),
70                 (unsigned long)(height * dpi / PX_PER_IN),
71                 dpi, dpi, bgcolor, NULL, NULL, true, NULL);
73             // This doesn't seem to work:
74             //context->set_cairo_context ( Cairo::Context::create (Cairo::ImageSurface::create_from_png (tmp_png) ), dpi, dpi );
75             //
76             // so we'll use a surface pattern blat instead...
77             //
78             // but the C++ interface isn't implemented in cairomm:
79             //context->get_cairo_context ()->set_source_surface(Cairo::ImageSurface::create_from_png (tmp_png) );
80             //
81             // so do it in C:
82             {
83                 Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png);
84                 cairo_t *cr = gtk_print_context_get_cairo_context (context);
85                 cairo_matrix_t m;
86                 cairo_get_matrix(cr, &m);
87                 cairo_scale(cr, PT_PER_IN / dpi, PT_PER_IN / dpi);
88                 // FIXME: why is the origin offset??
89                 cairo_set_source_surface(cr, png->cobj(), -16.0, -16.0);
90                 cairo_paint(cr);
91                 cairo_set_matrix(cr, &m);
92             }
94             // Clean up
95             unlink (tmp_png.c_str());
96         }
97         else {
98             g_warning(_("Could not open temporary PNG for bitmap printing"));
99         }
100     }
101     else {
102         // Render as vectors
103         Inkscape::Extension::Internal::CairoRenderer renderer;
104         Inkscape::Extension::Internal::CairoRenderContext *ctx = renderer.createContext();
106         // ctx->setPSLevel(CAIRO_PS_LEVEL_3);
107         ctx->setTextToPath(false);
108         ctx->setFilterToBitmap(true);
109         ctx->setBitmapResolution(72);
111         cairo_t *cr = gtk_print_context_get_cairo_context (context);
112         cairo_surface_t *surface = cairo_get_target(cr);
113         cairo_matrix_t ctm;
114         cairo_get_matrix(cr, &ctm);
115 #ifdef WIN32
116         //Gtk+ does not take the non printable area into account
117         //http://bugzilla.gnome.org/show_bug.cgi?id=381371
118         //
119         // This workaround translates the origin from the top left of the
120         // printable area to the top left of the page.
121         GtkPrintSettings *settings = gtk_print_operation_get_print_settings(operation);
122         const gchar *printerName = gtk_print_settings_get_printer(settings);
123         HDC hdc = CreateDC("WINSPOOL", printerName, NULL, NULL);
124         if (hdc) {
125             cairo_matrix_t mat;
126             int x_off = GetDeviceCaps (hdc, PHYSICALOFFSETX);
127             int y_off = GetDeviceCaps (hdc, PHYSICALOFFSETY);
128             cairo_matrix_init_translate(&mat, -x_off, -y_off);
129             cairo_matrix_multiply (&ctm, &ctm, &mat);
130             DeleteDC(hdc);
131         }
132 #endif             
133         bool ret = ctx->setSurfaceTarget (surface, true, &ctm);
134         if (ret) {
135             ret = renderer.setupDocument (ctx, junk->_doc, TRUE, NULL);
136             if (ret) {
137                 renderer.renderItem(ctx, junk->_base);
138                 ret = ctx->finish();
139             }
140             else {
141                 g_warning(_("Could not set up Document"));
142             }
143         }
144         else {
145             g_warning(_("Failed to set CairoRenderContext"));
146         }
148         // Clean up
149         renderer.destroyContext(ctx);
150     }
154 static GObject*
155 create_custom_widget (GtkPrintOperation */*operation*/,
156                       gpointer           user_data)
158     //printf("%s\n",__FUNCTION__);
159     return G_OBJECT(user_data);
162 static void
163 begin_print (GtkPrintOperation *operation,
164              GtkPrintContext   */*context*/,
165              gpointer           /*user_data*/)
167     //printf("%s\n",__FUNCTION__);
168     gtk_print_operation_set_n_pages (operation, 1);
171 namespace Inkscape {
172 namespace UI {
173 namespace Dialog {
175 Print::Print(SPDocument *doc, SPItem *base) :
176     _doc (doc),
177     _base (base)
179     g_assert (_doc);
180     g_assert (_base);
182     _printop = gtk_print_operation_new ();
184     // set up dialog title, based on document name
185     gchar const *jobname = _doc->getName() ? _doc->getName() : _("SVG Document");
186     Glib::ustring title = _("Print");
187     title += " ";
188     title += jobname;
189     gtk_print_operation_set_job_name (_printop, title.c_str());
191     // set up paper size to match the document size
192     gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS);
193     GtkPageSetup *page_setup = gtk_page_setup_new();
194     gdouble doc_width = _doc->getWidth() * PT_PER_PX;
195     gdouble doc_height = _doc->getHeight() * PT_PER_PX;
196     GtkPaperSize *paper_size;
197     if (doc_width > doc_height) {
198         gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_LANDSCAPE);
199         paper_size = gtk_paper_size_new_custom("custom", "custom",
200                                                doc_height, doc_width, GTK_UNIT_POINTS);
201     } else {
202         gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT);
203         paper_size = gtk_paper_size_new_custom("custom", "custom",
204                                                doc_width, doc_height, GTK_UNIT_POINTS);
205     }
207     gtk_page_setup_set_paper_size (page_setup, paper_size);
208     gtk_print_operation_set_default_page_setup (_printop, page_setup);
209     gtk_print_operation_set_use_full_page (_printop, TRUE);
211     // set up signals
212     _workaround._doc = _doc;
213     _workaround._base = _base;
214     _workaround._tab = &_tab;
215     g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj());
216     g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL);
217     g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround);
219     // build custom preferences tab
220     gtk_print_operation_set_custom_tab_label (_printop, _("Rendering"));
223 Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window)
225     gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
226             parent_window.gobj(), NULL);
227     return Gtk::PRINT_OPERATION_RESULT_APPLY;
231 } // namespace Dialog
232 } // namespace UI
233 } // namespace Inkscape
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :