diff options
| author | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2018-05-20 08:32:29 +0000 |
|---|---|---|
| committer | Patrick McDermott <patrick.mcdermott@libiquity.com> | 2018-05-20 08:32:29 +0000 |
| commit | a139e677581d6f82123a660e713958747753b8e0 (patch) | |
| tree | b579b3a0a5ac786ad259daeb65379fbff20d6150 /src | |
| parent | Fis maren improvements to embed/import (diff) | |
| download | inkscape-a139e677581d6f82123a660e713958747753b8e0.tar.gz inkscape-a139e677581d6f82123a660e713958747753b8e0.zip | |
Print: C++ify
Diffstat (limited to 'src')
| -rw-r--r-- | src/ui/dialog/print.cpp | 142 | ||||
| -rw-r--r-- | src/ui/dialog/print.h | 6 |
2 files changed, 71 insertions, 77 deletions
diff --git a/src/ui/dialog/print.cpp b/src/ui/dialog/print.cpp index ea99ffa19..934477343 100644 --- a/src/ui/dialog/print.cpp +++ b/src/ui/dialog/print.cpp @@ -30,25 +30,71 @@ #include <glibmm/i18n.h> -static void draw_page(GtkPrintOperation *, - GtkPrintContext *context, - gint /*page_nr*/, - gpointer user_data) +namespace Inkscape { +namespace UI { +namespace Dialog { + +Print::Print(SPDocument *doc, SPItem *base) : + _doc (doc), + _base (base) +{ + g_assert (_doc); + g_assert (_base); + + _printop = Gtk::PrintOperation::create(); + + // set up dialog title, based on document name + const Glib::ustring jobname = _doc->getName() ? _doc->getName() : _("SVG Document"); + Glib::ustring title = _("Print"); + title += " "; + title += jobname; + _printop->set_job_name(title); + + // set up paper size to match the document size + _printop->set_unit(Gtk::UNIT_POINTS); + Glib::RefPtr<Gtk::PageSetup> page_setup = Gtk::PageSetup::create(); + gdouble doc_width = _doc->getWidth().value("pt"); + gdouble doc_height = _doc->getHeight().value("pt"); + Gtk::PaperSize paper_size; + if (doc_width > doc_height) { + page_setup->set_orientation(Gtk::PAGE_ORIENTATION_PORTRAIT); + paper_size = Gtk::PaperSize("custom", "custom", doc_height, doc_width, Gtk::UNIT_POINTS); + } else { + page_setup->set_orientation(Gtk::PAGE_ORIENTATION_PORTRAIT); + paper_size = Gtk::PaperSize("custom", "custom", doc_width, doc_height, Gtk::UNIT_POINTS); + } + + page_setup->set_paper_size(paper_size); + _printop->set_default_page_setup(page_setup); + _printop->set_use_full_page(true); + + // set up signals + _workaround._doc = _doc; + _workaround._base = _base; + _workaround._tab = &_tab; + _printop->signal_create_custom_widget().connect(sigc::mem_fun(*this, &Print::create_custom_widget)); + _printop->signal_begin_print().connect(sigc::mem_fun(*this, &Print::begin_print)); + _printop->signal_draw_page().connect(sigc::mem_fun(*this, &Print::draw_page)); + + // build custom preferences tab + _printop->set_custom_tab_label(_("Rendering")); +} + +void Print::draw_page(const Glib::RefPtr<Gtk::PrintContext>& context, int /*page_nr*/) { // TODO: If the user prints multiple copies we render the whole page for each copy // It would be more efficient to render the page once (e.g. in "begin_print") // and simply print this result as often as necessary Inkscape::Preferences *prefs = Inkscape::Preferences::get(); - struct workaround_gtkmm *junk = (struct workaround_gtkmm*)user_data; //printf("%s %d\n",__FUNCTION__, page_nr); - if (junk->_tab->as_bitmap()) { + if (_workaround._tab->as_bitmap()) { // Render as exported PNG prefs->setBool("/dialogs/printing/asbitmap", true); - gdouble width = (junk->_doc)->getWidth().value("px"); - gdouble height = (junk->_doc)->getHeight().value("px"); - gdouble dpi = junk->_tab->bitmap_dpi(); + gdouble width = (_workaround._doc)->getWidth().value("px"); + gdouble height = (_workaround._doc)->getHeight().value("px"); + gdouble dpi = _workaround._tab->bitmap_dpi(); prefs->setDouble("/dialogs/printing/dpi", dpi); std::string tmp_png; @@ -59,7 +105,7 @@ static void draw_page(GtkPrintOperation *, close(tmp_fd); guint32 bgcolor = 0x00000000; - Inkscape::XML::Node *nv = sp_repr_lookup_name (junk->_doc->rroot, "sodipodi:namedview"); + Inkscape::XML::Node *nv = sp_repr_lookup_name (_workaround._doc->rroot, "sodipodi:namedview"); if (nv && nv->attribute("pagecolor")){ bgcolor = sp_svg_read_color(nv->attribute("pagecolor"), 0xffffff00); } @@ -69,7 +115,7 @@ static void draw_page(GtkPrintOperation *, bgcolor |= SP_COLOR_F_TO_U(opacity); } - sp_export_png_file(junk->_doc, tmp_png.c_str(), 0.0, 0.0, + sp_export_png_file(_workaround._doc, tmp_png.c_str(), 0.0, 0.0, width, height, (unsigned long)(Inkscape::Util::Quantity::convert(width, "px", "in") * dpi), (unsigned long)(Inkscape::Util::Quantity::convert(height, "px", "in") * dpi), @@ -86,7 +132,7 @@ static void draw_page(GtkPrintOperation *, // so do it in C: { Cairo::RefPtr<Cairo::ImageSurface> png = Cairo::ImageSurface::create_from_png (tmp_png); - cairo_t *cr = gtk_print_context_get_cairo_context (context); + cairo_t *cr = gtk_print_context_get_cairo_context (context->gobj()); cairo_matrix_t m; cairo_get_matrix(cr, &m); cairo_scale(cr, Inkscape::Util::Quantity::convert(1, "in", "pt") / dpi, Inkscape::Util::Quantity::convert(1, "in", "pt") / dpi); @@ -114,16 +160,16 @@ static void draw_page(GtkPrintOperation *, ctx->setFilterToBitmap(true); ctx->setBitmapResolution(72); - cairo_t *cr = gtk_print_context_get_cairo_context (context); + cairo_t *cr = gtk_print_context_get_cairo_context (context->gobj()); cairo_surface_t *surface = cairo_get_target(cr); cairo_matrix_t ctm; cairo_get_matrix(cr, &ctm); bool ret = ctx->setSurfaceTarget (surface, true, &ctm); if (ret) { - ret = renderer.setupDocument (ctx, junk->_doc, TRUE, 0., NULL); + ret = renderer.setupDocument (ctx, _workaround._doc, TRUE, 0., NULL); if (ret) { - renderer.renderItem(ctx, junk->_base); + renderer.renderItem(ctx, _workaround._base); ctx->finish(false); // do not finish the cairo_surface_t - it's owned by our GtkPrintContext! } else { @@ -140,77 +186,21 @@ static void draw_page(GtkPrintOperation *, } -static GObject* create_custom_widget (GtkPrintOperation */*operation*/, - gpointer user_data) +Gtk::Widget *Print::create_custom_widget() { //printf("%s\n",__FUNCTION__); - return G_OBJECT(user_data); + return &_tab; } -static void begin_print (GtkPrintOperation *operation, - GtkPrintContext */*context*/, - gpointer /*user_data*/) +void Print::begin_print(const Glib::RefPtr<Gtk::PrintContext>&) { //printf("%s\n",__FUNCTION__); - gtk_print_operation_set_n_pages (operation, 1); -} - -namespace Inkscape { -namespace UI { -namespace Dialog { - -Print::Print(SPDocument *doc, SPItem *base) : - _doc (doc), - _base (base) -{ - g_assert (_doc); - g_assert (_base); - - _printop = gtk_print_operation_new (); - - // set up dialog title, based on document name - gchar const *jobname = _doc->getName() ? _doc->getName() : _("SVG Document"); - Glib::ustring title = _("Print"); - title += " "; - title += jobname; - gtk_print_operation_set_job_name (_printop, title.c_str()); - - // set up paper size to match the document size - gtk_print_operation_set_unit (_printop, GTK_UNIT_POINTS); - GtkPageSetup *page_setup = gtk_page_setup_new(); - gdouble doc_width = _doc->getWidth().value("pt"); - gdouble doc_height = _doc->getHeight().value("pt"); - GtkPaperSize *paper_size; - if (doc_width > doc_height) { - gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_LANDSCAPE); - paper_size = gtk_paper_size_new_custom("custom", "custom", - doc_height, doc_width, GTK_UNIT_POINTS); - } else { - gtk_page_setup_set_orientation (page_setup, GTK_PAGE_ORIENTATION_PORTRAIT); - paper_size = gtk_paper_size_new_custom("custom", "custom", - doc_width, doc_height, GTK_UNIT_POINTS); - } - - gtk_page_setup_set_paper_size (page_setup, paper_size); - gtk_print_operation_set_default_page_setup (_printop, page_setup); - gtk_print_operation_set_use_full_page (_printop, TRUE); - - // set up signals - _workaround._doc = _doc; - _workaround._base = _base; - _workaround._tab = &_tab; - g_signal_connect (_printop, "create-custom-widget", G_CALLBACK (create_custom_widget), _tab.gobj()); - g_signal_connect (_printop, "begin-print", G_CALLBACK (begin_print), NULL); - g_signal_connect (_printop, "draw-page", G_CALLBACK (draw_page), &_workaround); - - // build custom preferences tab - gtk_print_operation_set_custom_tab_label (_printop, _("Rendering")); + _printop->set_n_pages(1); } Gtk::PrintOperationResult Print::run(Gtk::PrintOperationAction, Gtk::Window &parent_window) { - gtk_print_operation_run (_printop, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, - parent_window.gobj(), NULL); + _printop->run(Gtk::PRINT_OPERATION_ACTION_PRINT_DIALOG, parent_window); return Gtk::PRINT_OPERATION_RESULT_APPLY; } diff --git a/src/ui/dialog/print.h b/src/ui/dialog/print.h index f81829d86..0ce9e811c 100644 --- a/src/ui/dialog/print.h +++ b/src/ui/dialog/print.h @@ -45,12 +45,16 @@ public: protected: private: - GtkPrintOperation *_printop; + Glib::RefPtr<Gtk::PrintOperation> _printop; SPDocument *_doc; SPItem *_base; Inkscape::UI::Widget::RenderingOptions _tab; struct workaround_gtkmm _workaround; + + void draw_page(const Glib::RefPtr<Gtk::PrintContext>& context, int /*page_nr*/); + Gtk::Widget *create_custom_widget(); + void begin_print(const Glib::RefPtr<Gtk::PrintContext>&); }; } // namespace Dialog |
