page-sizer.cpp revision bc5ab6c60f7d73fd883b4208ea63119cd2fabfd6
1N/A/** \file
1N/A *
1N/A * Paper-size widget and helper functions
1N/A *
1N/A * Authors:
1N/A * bulia byak <buliabyak@users.sf.net>
1N/A * Lauris Kaplinski <lauris@kaplinski.com>
1N/A * Jon Phillips <jon@rejon.org>
1N/A * Ralf Stephan <ralf@ark.in-berlin.de> (Gtkmm)
1N/A *
1N/A * Copyright (C) 2000 - 2005 Authors
1N/A *
1N/A * Released under GNU GPL. Read the file 'COPYING' for more information
1N/A */
1N/A
1N/A#ifdef HAVE_CONFIG_H
1N/A# include <config.h>
1N/A#endif
1N/A
1N/A#include <cmath>
1N/A#include <gtkmm.h>
1N/A#include <gtkmm/optionmenu.h>
1N/A#include <gtkmm/frame.h>
1N/A#include <gtkmm/table.h>
1N/A#include "ui/widget/button.h"
1N/A
1N/A#include "ui/widget/scalar-unit.h"
1N/A
1N/A#include "helper/units.h"
1N/A#include "inkscape.h"
1N/A#include "verbs.h"
1N/A#include "desktop-handles.h"
1N/A#include "document.h"
1N/A#include "desktop.h"
1N/A#include "page-sizer.h"
1N/A#include "helper/action.h"
1N/A
1N/Ausing std::pair;
1N/A
1N/Anamespace Inkscape {
1N/Anamespace UI {
1N/Anamespace Widget {
1N/A
1N/Astruct PaperSize {
1N/A char const * const name;
1N/A double const smaller;
1N/A double const larger;
1N/A SPUnitId const unit;
1N/A};
1N/A
1N/A /** \note
1N/A * The ISO page sizes in the table below differ from ghostscript's idea of page sizes (by
1N/A * less than 1pt). Being off by <1pt should be OK for most purposes, but may cause fuzziness
1N/A * (antialiasing) problems when printing to 72dpi or 144dpi printers or bitmap files due to
1N/A * postscript's different coordinate system (y=0 meaning bottom of page in postscript and top
1N/A * of page in SVG). I haven't looked into whether this does in fact cause fuzziness, I merely
1N/A * note the possibility. Rounding done by extension/internal/ps.cpp (e.g. floor/ceil calls)
1N/A * will also affect whether fuzziness occurs.
1N/A *
1N/A * The remainder of this comment discusses the origin of the numbers used for ISO page sizes in
1N/A * this table and in ghostscript.
1N/A *
1N/A * The versions here, in mm, are the official sizes according to
1N/A * <a href="http://en.wikipedia.org/wiki/Paper_sizes">http://en.wikipedia.org/wiki/Paper_sizes</a>
1N/A * at 2005-01-25. (The ISO entries in the below table
1N/A * were produced mechanically from the table on that page.)
1N/A *
1N/A * (The rule seems to be that A0, B0, ..., D0. sizes are rounded to the nearest number of mm
1N/A * from the "theoretical size" (i.e. 1000 * sqrt(2) or pow(2.0, .25) or the like), whereas
1N/A * going from e.g. A0 to A1 always take the floor of halving -- which by chance coincides
1N/A * exactly with flooring the "theoretical size" for n != 0 instead of the rounding to nearest
1N/A * done for n==0.)
1N/A *
1N/A * Ghostscript paper sizes are given in gs_statd.ps according to gs(1). gs_statd.ps always
1N/A * uses an integer number of pt: sometimes gs_statd.ps rounds to nearest (e.g. a1), sometimes
1N/A * floors (e.g. a10), sometimes ceils (e.g. a8).
1N/A *
1N/A * I'm not sure how ghostscript's gs_statd.ps was calculated: it isn't just rounding the
1N/A * "theoretical size" of each page to pt (see a0), nor is it rounding the a0 size times an
1N/A * appropriate power of two (see a1). Possibly it was prepared manually, with a human applying
1N/A * inconsistent rounding rules when converting from mm to pt.
1N/A */
1N/A /** \todo
1N/A * Should we include the JIS B series (used in Japan)
1N/A * (JIS B0 is sometimes called JB0, and similarly for JB1 etc)?
1N/A * Should we exclude B7--B10 and A7--10 to make the list smaller ?
1N/A * Should we include any of the ISO C, D and E series (see below) ?
1N/A */
1N/A
1N/Astatic PaperSize const inkscape_papers[] = {
1N/A { "A4", 210, 297, SP_UNIT_MM },
1N/A { "US Letter", 8.5, 11, SP_UNIT_IN },
1N/A { "US Legal", 8.5, 14, SP_UNIT_IN },
1N/A { "US Executive", 7.25, 10.5, SP_UNIT_IN },
1N/A { "A0", 841, 1189, SP_UNIT_MM },
1N/A { "A1", 594, 841, SP_UNIT_MM },
1N/A { "A2", 420, 594, SP_UNIT_MM },
1N/A { "A3", 297, 420, SP_UNIT_MM },
1N/A { "A5", 148, 210, SP_UNIT_MM },
1N/A { "A6", 105, 148, SP_UNIT_MM },
1N/A { "A7", 74, 105, SP_UNIT_MM },
1N/A { "A8", 52, 74, SP_UNIT_MM },
1N/A { "A9", 37, 52, SP_UNIT_MM },
1N/A { "A10", 26, 37, SP_UNIT_MM },
1N/A { "B0", 1000, 1414, SP_UNIT_MM },
1N/A { "B1", 707, 1000, SP_UNIT_MM },
1N/A { "B2", 500, 707, SP_UNIT_MM },
1N/A { "B3", 353, 500, SP_UNIT_MM },
1N/A { "B4", 250, 353, SP_UNIT_MM },
1N/A { "B5", 176, 250, SP_UNIT_MM },
1N/A { "B6", 125, 176, SP_UNIT_MM },
1N/A { "B7", 88, 125, SP_UNIT_MM },
1N/A { "B8", 62, 88, SP_UNIT_MM },
1N/A { "B9", 44, 62, SP_UNIT_MM },
1N/A { "B10", 31, 44, SP_UNIT_MM },
1N/A
1N/A#if 0 /* Whether to include or exclude these depends on how big we mind our page size menu
1N/A becoming. C series is used for envelopes; don't know what D and E series are used for. */
1N/A { "C0", 917, 1297, SP_UNIT_MM },
1N/A { "C1", 648, 917, SP_UNIT_MM },
1N/A { "C2", 458, 648, SP_UNIT_MM },
1N/A { "C3", 324, 458, SP_UNIT_MM },
1N/A { "C4", 229, 324, SP_UNIT_MM },
1N/A { "C5", 162, 229, SP_UNIT_MM },
1N/A { "C6", 114, 162, SP_UNIT_MM },
1N/A { "C7", 81, 114, SP_UNIT_MM },
1N/A { "C8", 57, 81, SP_UNIT_MM },
1N/A { "C9", 40, 57, SP_UNIT_MM },
1N/A { "C10", 28, 40, SP_UNIT_MM },
1N/A { "D1", 545, 771, SP_UNIT_MM },
1N/A { "D2", 385, 545, SP_UNIT_MM },
1N/A { "D3", 272, 385, SP_UNIT_MM },
1N/A { "D4", 192, 272, SP_UNIT_MM },
1N/A { "D5", 136, 192, SP_UNIT_MM },
1N/A { "D6", 96, 136, SP_UNIT_MM },
1N/A { "D7", 68, 96, SP_UNIT_MM },
1N/A { "E3", 400, 560, SP_UNIT_MM },
1N/A { "E4", 280, 400, SP_UNIT_MM },
1N/A { "E5", 200, 280, SP_UNIT_MM },
1N/A { "E6", 140, 200, SP_UNIT_MM },
1N/A#endif
1N/A
1N/A { "CSE", 462, 649, SP_UNIT_PT },
1N/A { "US #10 Envelope", 4.125, 9.5, SP_UNIT_IN }, // TODO: Select landscape by default.
1N/A /* See http://www.hbp.com/content/PCR_envelopes.cfm for a much larger list of US envelope
1N/A sizes. */
1N/A { "DL Envelope", 110, 220, SP_UNIT_MM }, // TODO: Select landscape by default.
1N/A { "Ledger/Tabloid", 11, 17, SP_UNIT_IN },
1N/A /* Note that `Folio' (used in QPrinter/KPrinter) is deliberately absent from this list, as it
1N/A means different sizes to different people: different people may expect the width to be
1N/A either 8, 8.25 or 8.5 inches, and the height to be either 13 or 13.5 inches, even
1N/A restricting our interpretation to foolscap folio. If you wish to introduce a folio-like
1N/A page size to the list, then please consider using a name more specific than just `Folio' or
1N/A `Foolscap Folio'. */
1N/A { "Banner 468x60", 60, 468, SP_UNIT_PX }, // TODO: Select landscape by default.
1N/A { "Icon 16x16", 16, 16, SP_UNIT_PX },
1N/A { "Icon 32x32", 32, 32, SP_UNIT_PX },
1N/A { NULL, 0, 0, SP_UNIT_PX },
1N/A};
1N/A
1N/A//===================================================
1N/Astatic const SPUnit _px_unit = sp_unit_get_by_id (SP_UNIT_PX);
1N/A
1N/Aclass SizeMenuItem : public Gtk::MenuItem {
1N/Apublic:
1N/A SizeMenuItem (PaperSize const * paper, PageSizer * widget)
1N/A : Gtk::MenuItem (paper ? paper->name : _("Custom")),
1N/A _paper(paper), _parent(widget) {}
1N/Aprotected:
1N/A PaperSize const * _paper;
1N/A PageSizer *_parent;
1N/A void on_activate();
1N/A};
1N/A
1N/Avoid
1N/ASizeMenuItem::on_activate()
1N/A{
1N/A if (_parent == 0) // handle Custom entry
1N/A return;
1N/A
1N/A double w = _paper->smaller, h = _paper->larger;
1N/A SPUnit const &src_unit = sp_unit_get_by_id (_paper->unit);
1N/A sp_convert_distance (&w, &src_unit, &_px_unit);
1N/A sp_convert_distance (&h, &src_unit, &_px_unit);
1N/A if (_parent->_landscape)
1N/A _parent->setDim (h, w);
1N/A else
1N/A _parent->setDim (w, h);
1N/A}
1N/A
1N/A//---------------------------------------------------
1N/A
1N/APageSizer::PageSizer()
1N/A: Gtk::VBox(false,4)
1N/A{
1N/A Gtk::HBox *hbox_size = manage (new Gtk::HBox (false, 4));
1N/A pack_start (*hbox_size, false, false, 0);
1N/A Gtk::Label *label_size = manage (new Gtk::Label (_("P_age size:"), 1.0, 0.5));
1N/A label_size->set_use_underline();
1N/A hbox_size->pack_start (*label_size, false, false, 0);
1N/A _omenu_size = manage (new Gtk::OptionMenu);
1N/A label_size->set_mnemonic_widget (*_omenu_size);
1N/A hbox_size->pack_start (*_omenu_size, true, true, 0);
1N/A Gtk::Menu *menu_size = manage (new Gtk::Menu);
1N/A
1N/A for (PaperSize const *paper = inkscape_papers; paper->name; paper++) {
1N/A SizeMenuItem *item = manage (new SizeMenuItem (paper, this));
1N/A menu_size->append (*item);
1N/A }
1N/A SizeMenuItem *item = manage (new SizeMenuItem (0, 0));
1N/A menu_size->prepend (*item);
1N/A _omenu_size->set_menu (*menu_size);
1N/A}
1N/A
1N/APageSizer::~PageSizer()
1N/A{
1N/A _portrait_connection.disconnect();
1N/A _landscape_connection.disconnect();
1N/A _changedw_connection.disconnect();
1N/A _changedh_connection.disconnect();
1N/A}
1N/A
1N/Avoid
1N/APageSizer::init (Registry& reg)
1N/A{
1N/A Gtk::HBox *hbox_ori = manage (new Gtk::HBox);
1N/A pack_start (*hbox_ori, false, false, 0);
1N/A Gtk::Label *label_ori = manage (new Gtk::Label (_("Page orientation:"), 0.0, 0.5));
1N/A hbox_ori->pack_start (*label_ori, false, false, 0);
1N/A _rb_land = manage (new Gtk::RadioButton (_("_Landscape"), true));
1N/A Gtk::RadioButton::Group group = _rb_land->get_group();
1N/A hbox_ori->pack_end (*_rb_land, false, false, 5);
1N/A _rb_port = manage (new Gtk::RadioButton (_("_Portrait"), true));
1N/A hbox_ori->pack_end (*_rb_port, false, false, 5);
1N/A _rb_port->set_group (group);
1N/A _rb_port->set_active (true);
1N/A
1N/A /* Custom paper frame */
1N/A Gtk::Frame *frame = manage (new Gtk::Frame(_("Custom size")));
1N/A pack_start (*frame, false, false, 0);
1N/A Gtk::Table *table = manage (new Gtk::Table (5, 2, false));
1N/A table->set_border_width (4);
1N/A table->set_row_spacings (4);
1N/A table->set_col_spacings (4);
1N/A
1N/A Inkscape::UI::Widget::Button* fit_canv = manage(new Inkscape::UI::Widget::Button(_("_Fit page to selection"),
1N/A _("Resize the page to fit the current selection, or the entire drawing if there is no selection")));
1N/A // prevent fit_canv from expanding
1N/A Gtk::Alignment *fit_canv_cont = manage(new Gtk::Alignment(1.0,0.5,0.0,0.0));
1N/A fit_canv_cont->add(*fit_canv);
1N/A
1N/A frame->add (*table);
1N/A
1N/A _wr = &reg;
1N/A
1N/A _rum.init (_("U_nits:"), "units", *_wr);
1N/A _rusw.init (_("_Width:"), _("Width of paper"), "width", _rum, *_wr);
1N/A _rush.init (_("_Height:"), _("Height of paper"), "height", _rum, *_wr);
1N/A
1N/A table->attach (*_rum._label, 0,1,0,1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
1N/A table->attach (*_rum._sel, 1,2,0,1, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
1N/A table->attach (*_rusw.getSU(), 0,2,1,2, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
1N/A table->attach (*_rush.getSU(), 0,2,2,3, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
1N/A table->attach (*fit_canv_cont, 0,2,3,4, Gtk::FILL|Gtk::EXPAND, (Gtk::AttachOptions)0,0,0);
1N/A
1N/A _landscape_connection = _rb_land->signal_toggled().connect (sigc::mem_fun (*this, &PageSizer::on_landscape));
1N/A _portrait_connection = _rb_port->signal_toggled().connect (sigc::mem_fun (*this, &PageSizer::on_portrait));
1N/A _changedw_connection = _rusw.getSU()->signal_value_changed().connect (sigc::mem_fun (*this, &PageSizer::on_value_changed));
1N/A _changedh_connection = _rush.getSU()->signal_value_changed().connect (sigc::mem_fun (*this, &PageSizer::on_value_changed));
1N/A fit_canv->signal_clicked().connect(sigc::mem_fun(*this, &PageSizer::fire_fit_canvas_to_selection_or_drawing));
1N/A
1N/A show_all_children();
1N/A}
1N/A
1N/A
1N/A/**
1N/A * Set document dimensions (if not called by Doc prop's update()) and
1N/A * set the PageSizer's widgets and text entries accordingly. This is
1N/A * somewhat slow, is there something done too often invisibly?
1N/A *
1N/A * \param w, h given in px
1N/A */
1N/Avoid
1N/APageSizer::setDim (double w, double h)
1N/A{
1N/A static bool _called = false;
1N/A if (_called) return;
1N/A
1N/A _called = true;
1N/A
1N/A _landscape_connection.block();
1N/A _portrait_connection.block();
1N/A _changedw_connection.block();
1N/A _changedh_connection.block();
1N/A
1N/A if (SP_ACTIVE_DESKTOP && !_wr->isUpdating()) {
1N/A SPDocument *doc = sp_desktop_document(SP_ACTIVE_DESKTOP);
1N/A sp_document_set_width (doc, w, &_px_unit);
1N/A sp_document_set_height (doc, h, &_px_unit);
1N/A sp_document_done (doc);
1N/A }
1N/A
1N/A _landscape = w>h;
1N/A _rb_land->set_active (_landscape ? true : false);
1N/A _rb_port->set_active (_landscape ? false : true);
1N/A
1N/A _omenu_size->set_history (1 + find_paper_size (w, h));
1N/A
1N/A Unit const& unit = _rum._sel->getUnit();
1N/A _rusw.setValue (w / unit.factor);
1N/A _rush.setValue (h / unit.factor);
1N/A
1N/A _landscape_connection.unblock();
1N/A _portrait_connection.unblock();
1N/A _changedw_connection.unblock();
1N/A _changedh_connection.unblock();
1N/A
1N/A _called = false;
1N/A}
1N/A
1N/A/**
1N/A * Returns an index into inkscape_papers of a paper of the specified
1N/A * size (specified in px), or -1 if there's no such paper.
1N/A */
1N/Aint
1N/APageSizer::find_paper_size (double w, double h) const
1N/A{
1N/A double given[2];
1N/A if ( w < h ) {
1N/A given[0] = w; given[1] = h;
1N/A } else {
1N/A given[0] = h; given[1] = w;
1N/A }
1N/A g_return_val_if_fail(given[0] <= given[1], -1);
1N/A for (unsigned i = 0; i < G_N_ELEMENTS(inkscape_papers) - 1; ++i) {
1N/A SPUnit const &i_unit = sp_unit_get_by_id(inkscape_papers[i].unit);
1N/A double const i_sizes[2] = { sp_units_get_pixels(inkscape_papers[i].smaller, i_unit),
1N/A sp_units_get_pixels(inkscape_papers[i].larger, i_unit) };
1N/A g_return_val_if_fail(i_sizes[0] <= i_sizes[1], -1);
1N/A if ((std::abs(given[0] - i_sizes[0]) <= .1) &&
1N/A (std::abs(given[1] - i_sizes[1]) <= .1) )
1N/A {
1N/A return (int) i;
1N/A }
1N/A }
1N/A return -1;
1N/A}
1N/A
1N/Avoid
1N/APageSizer::fire_fit_canvas_to_selection_or_drawing() {
1N/A SPDesktop *dt = SP_ACTIVE_DESKTOP;
1N/A if (!dt) return;
1N/A Verb *verb = Verb::get( SP_VERB_FIT_CANVAS_TO_SELECTION_OR_DRAWING );
1N/A if (verb) {
1N/A SPAction *action = verb->get_action(dt);
1N/A if (action) {
1N/A sp_action_perform(action, NULL);
1N/A }
1N/A }
1N/A}
1N/A
1N/Avoid
1N/APageSizer::on_portrait()
1N/A{
1N/A if (!_rb_port->get_active())
1N/A return;
1N/A double w = _rusw.getSU()->getValue ("px");
1N/A double h = _rush.getSU()->getValue ("px");
1N/A if (h<w) setDim (h, w);
1N/A}
1N/A
1N/Avoid
1N/APageSizer::on_landscape()
1N/A{
1N/A if (!_rb_land->get_active())
1N/A return;
1N/A double w = _rusw.getSU()->getValue ("px");
1N/A double h = _rush.getSU()->getValue ("px");
1N/A if (w<h) setDim (h, w);
1N/A}
1N/A
1N/Avoid
1N/APageSizer::on_value_changed()
1N/A{
1N/A if (_wr->isUpdating()) return;
1N/A
1N/A setDim (_rusw.getSU()->getValue("px"), _rush.getSU()->getValue("px"));
1N/A}
1N/A
1N/A} // namespace Widget
1N/A} // namespace UI
1N/A} // namespace Inkscape
1N/A
1N/A/*
1N/A Local Variables:
1N/A mode:c++
1N/A c-file-style:"stroustrup"
1N/A c-file-offsets:((innamespace . 0)(inline-open . 0))
1N/A indent-tabs-mode:nil
1N/A fill-column:99
1N/A End:
1N/A*/
1N/A// vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
1N/A