guides.cpp revision 78dc91208197e93470978ee7a53ac5d893b640b7
/** @file
* @brief Simple guideline dialog
*/
/* Authors:
* Lauris Kaplinski <lauris@kaplinski.com>
* Andrius R. <knutux@gmail.com>
* Johan Engelen
* Abhishek Sharma
*
* Copyright (C) 1999-2007 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "guides.h"
#include "display/guideline.h"
#include "desktop.h"
#include "document.h"
#include "sp-guide.h"
#include "sp-namedview.h"
#include "desktop-handles.h"
#include "event-context.h"
#include "widgets/desktop-widget.h"
#include <glibmm/i18n.h>
#include "dialogs/dialog-events.h"
#include "message-context.h"
#include "xml/repr.h"
#include <2geom/point.h>
#include <2geom/angle.h>
namespace Inkscape {
namespace UI {
namespace Dialogs {
GuidelinePropertiesDialog::GuidelinePropertiesDialog(SPGuide *guide, SPDesktop *desktop)
: _desktop(desktop), _guide(guide),
_relative_toggle(_("Rela_tive change"), _("Move and/or rotate the guide relative to current settings")),
_spin_button_x(_("X:"), "", UNIT_TYPE_LINEAR, "", "", &_unit_menu),
_spin_button_y(_("Y:"), "", UNIT_TYPE_LINEAR, "", "", &_unit_menu),
_spin_angle(_("Angle:"), "", UNIT_TYPE_RADIAL),
_mode(true), _oldpos(0.,0.), _oldangle(0.0)
{
}
bool GuidelinePropertiesDialog::_relative_toggle_status = false; // initialize relative checkbox status for when this dialog is opened for first time
Glib::ustring GuidelinePropertiesDialog::_angle_unit_status = "deg"; // initialize angle unit status
GuidelinePropertiesDialog::~GuidelinePropertiesDialog() {
// save current status
_relative_toggle_status = _relative_toggle.get_active();
_angle_unit_status = _spin_angle.getUnit().abbr;
}
void GuidelinePropertiesDialog::showDialog(SPGuide *guide, SPDesktop *desktop) {
GuidelinePropertiesDialog dialog(guide, desktop);
dialog._setup();
dialog.run();
}
void GuidelinePropertiesDialog::_modeChanged()
{
_mode = !_relative_toggle.get_active();
if (!_mode) {
// relative
_spin_angle.setValue(0);
_spin_button_y.setValue(0);
_spin_button_x.setValue(0);
} else {
// absolute
_spin_angle.setValueKeepUnit(_oldangle, "deg");
_spin_button_x.setValueKeepUnit(_oldpos[Geom::X], "px");
_spin_button_y.setValueKeepUnit(_oldpos[Geom::Y], "px");
}
}
void GuidelinePropertiesDialog::_onApply()
{
double deg_angle = _spin_angle.getValue("deg");
if (!_mode)
deg_angle += _oldangle;
Geom::Point normal;
if ( deg_angle == 90. || deg_angle == 270. || deg_angle == -90. || deg_angle == -270.) {
normal = Geom::Point(1.,0.);
} else if ( deg_angle == 0. || deg_angle == 180. || deg_angle == -180.) {
normal = Geom::Point(0.,1.);
} else {
double rad_angle = Geom::deg_to_rad( deg_angle );
normal = Geom::rot90(Geom::Point::polar(rad_angle, 1.0));
}
sp_guide_set_normal(*_guide, normal, true);
double const points_x = _spin_button_x.getValue("px");
double const points_y = _spin_button_y.getValue("px");
Geom::Point newpos(points_x, points_y);
if (!_mode)
newpos += _oldpos;
sp_guide_moveto(*_guide, newpos, true);
DocumentUndo::done(_guide->document, SP_VERB_NONE,
_("Set guide properties"));
}
void GuidelinePropertiesDialog::_onOK()
{
_onApply();
}
void GuidelinePropertiesDialog::_onDelete()
{
SPDocument *doc = _guide->document;
sp_guide_remove(_guide);
DocumentUndo::done(doc, SP_VERB_NONE,
_("Delete guide"));
}
void GuidelinePropertiesDialog::_response(gint response)
{
switch (response) {
case Gtk::RESPONSE_OK:
_onOK();
break;
case -12:
_onDelete();
break;
case Gtk::RESPONSE_CANCEL:
break;
case Gtk::RESPONSE_DELETE_EVENT:
break;
/* case GTK_RESPONSE_APPLY:
_onApply();
break;
*/
default:
g_assert_not_reached();
}
}
void GuidelinePropertiesDialog::_setup() {
set_title(_("Guideline"));
add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);
add_button(Gtk::Stock::DELETE, -12);
add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
Gtk::VBox *mainVBox = get_vbox();
_layout_table.set_spacings(4);
_layout_table.resize (3, 4);
mainVBox->pack_start(_layout_table, false, false, 0);
_label_name.set_label("foo0");
_layout_table.attach(_label_name,
0, 3, 0, 1, Gtk::FILL, Gtk::FILL);
_label_name.set_alignment(0, 0.5);
_label_descr.set_label("foo1");
_layout_table.attach(_label_descr,
0, 3, 1, 2, Gtk::FILL, Gtk::FILL);
_label_descr.set_alignment(0, 0.5);
// indent
_layout_table.attach(*manage(new Gtk::Label(" ")),
0, 1, 2, 3, Gtk::FILL, Gtk::FILL, 10);
// unitmenus
/* fixme: We should allow percents here too, as percents of the canvas size */
_unit_menu.setUnitType(UNIT_TYPE_LINEAR);
_unit_menu.setUnit("px");
if (_desktop->namedview->doc_units) {
_unit_menu.setUnit( sp_unit_get_abbreviation(_desktop->namedview->doc_units) );
}
_spin_angle.setUnit(_angle_unit_status);
// position spinbuttons
_spin_button_x.setDigits(3);
_spin_button_x.setIncrements(1.0, 10.0);
_spin_button_x.setRange(-1e6, 1e6);
_spin_button_y.setDigits(3);
_spin_button_y.setIncrements(1.0, 10.0);
_spin_button_y.setRange(-1e6, 1e6);
_layout_table.attach(_spin_button_x,
1, 2, 4, 5, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
_layout_table.attach(_spin_button_y,
1, 2, 5, 6, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
_layout_table.attach(_unit_menu,
2, 3, 4, 5, Gtk::FILL, Gtk::FILL);
// angle spinbutton
_spin_angle.setDigits(3);
_spin_angle.setIncrements(1.0, 10.0);
_spin_angle.setRange(-3600., 3600.);
_layout_table.attach(_spin_angle,
1, 3, 6, 7, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
// mode radio button
_layout_table.attach(_relative_toggle,
1, 3, 7, 8, Gtk::EXPAND | Gtk::FILL, Gtk::FILL);
_relative_toggle.signal_toggled().connect(sigc::mem_fun(*this, &GuidelinePropertiesDialog::_modeChanged));
_relative_toggle.set_active(_relative_toggle_status);
// don't know what this exactly does, but it results in that the dialog closes when entering a value and pressing enter (see LP bug 484187)
gtk_signal_connect_object(GTK_OBJECT(_spin_button_x.getWidget()->gobj()), "activate",
GTK_SIGNAL_FUNC(gtk_window_activate_default), gobj());
gtk_signal_connect_object(GTK_OBJECT(_spin_button_y.getWidget()->gobj()), "activate",
GTK_SIGNAL_FUNC(gtk_window_activate_default), gobj());
gtk_signal_connect_object(GTK_OBJECT(_spin_angle.getWidget()->gobj()), "activate",
GTK_SIGNAL_FUNC(gtk_window_activate_default), gobj());
// dialog
set_default_response(Gtk::RESPONSE_OK);
signal_response().connect(sigc::mem_fun(*this, &GuidelinePropertiesDialog::_response));
// initialize dialog
_oldpos = _guide->point_on_line;
if (_guide->isVertical()) {
_oldangle = 90;
} else if (_guide->isHorizontal()) {
_oldangle = 0;
} else {
_oldangle = Geom::rad_to_deg( std::atan2( - _guide->normal_to_line[Geom::X], _guide->normal_to_line[Geom::Y] ) );
}
{
Inkscape::XML::Node *repr = _guide->getRepr();
const gchar *guide_id = repr->attribute("id");
gchar *label = g_strdup_printf(_("Guideline ID: %s"), guide_id);
_label_name.set_label(label);
g_free(label);
}
{
gchar *guide_description = sp_guide_description(_guide, false);
gchar *label = g_strdup_printf(_("Current: %s"), guide_description);
g_free(guide_description);
_label_descr.set_markup(label);
g_free(label);
}
_modeChanged(); // sets values of spinboxes.
if ( _oldangle == 90. || _oldangle == 270. || _oldangle == -90. || _oldangle == -270.) {
_spin_button_x.grabFocusAndSelectEntry();
} else if ( _oldangle == 0. || _oldangle == 180. || _oldangle == -180.) {
_spin_button_y.grabFocusAndSelectEntry();
} else {
_spin_angle.grabFocusAndSelectEntry();
}
set_position(Gtk::WIN_POS_MOUSE);
show_all_children();
set_modal(true);
_desktop->setWindowTransient (gobj());
property_destroy_with_parent() = true;
}
}
}
}
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :