star-context.cpp revision 82ea1619194ed558b2fd7bbbde4c6925cba1669e
1117N/A/*
1117N/A * Star drawing context
1117N/A *
1117N/A * Authors:
1117N/A * Mitsuru Oka <oka326@parkcity.ne.jp>
1117N/A * Lauris Kaplinski <lauris@kaplinski.com>
1117N/A * bulia byak <buliabyak@users.sf.net>
1117N/A * Jon A. Cruz <jon@joncruz.org>
1117N/A * Abhishek Sharma
1117N/A *
1117N/A * Copyright (C) 1999-2002 Lauris Kaplinski
1117N/A * Copyright (C) 2001-2002 Mitsuru Oka
1117N/A *
1117N/A * Released under GNU GPL, read the file 'COPYING' for more information
1117N/A */
1117N/A
1117N/A#ifdef HAVE_CONFIG_H
1117N/A# include "config.h"
1117N/A#endif
1117N/A
1117N/A#include <cstring>
1117N/A#include <string>
1117N/A
1117N/A#include <gdk/gdkkeysyms.h>
1117N/A
1117N/A#include "macros.h"
1117N/A#include "display/sp-canvas.h"
1117N/A#include "sp-star.h"
1117N/A#include "document.h"
1117N/A#include "document-undo.h"
1117N/A#include "sp-namedview.h"
1117N/A#include "selection.h"
1117N/A#include "desktop-handles.h"
1117N/A#include "snap.h"
1117N/A#include "desktop.h"
1117N/A#include "desktop-style.h"
1117N/A#include "message-context.h"
1117N/A#include "pixmaps/cursor-star.xpm"
1117N/A#include "sp-metrics.h"
1117N/A#include <glibmm/i18n.h>
1117N/A#include "preferences.h"
1117N/A#include "xml/repr.h"
1117N/A#include "xml/node-event-vector.h"
1117N/A#include "context-fns.h"
1117N/A#include "shape-editor.h"
1117N/A#include "verbs.h"
1117N/A#include "display/sp-canvas-item.h"
1117N/A
1117N/A#include "star-context.h"
1117N/A
1117N/Ausing Inkscape::DocumentUndo;
1117N/A
1117N/A#include "tool-factory.h"
1117N/A
1117N/Anamespace {
1117N/A SPEventContext* createStarContext() {
1117N/A return new SPStarContext();
1117N/A }
1117N/A
1117N/A bool starContextRegistered = ToolFactory::instance().registerObject("/tools/shapes/star", createStarContext);
1117N/A}
1117N/A
1117N/Aconst std::string& SPStarContext::getPrefsPath() {
1117N/A return SPStarContext::prefsPath;
1117N/A}
1117N/A
1117N/Aconst std::string SPStarContext::prefsPath = "/tools/shapes/star";
1117N/A
1117N/ASPStarContext::SPStarContext() : SPEventContext() {
1117N/A SPStarContext* star_context = this;
1117N/A
1117N/A star_context->randomized = 0;
1117N/A star_context->_message_context = 0;
1117N/A star_context->rounded = 0;
1117N/A
1117N/A SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
1117N/A
1117N/A event_context->cursor_shape = cursor_star_xpm;
1117N/A event_context->hot_x = 4;
1117N/A event_context->hot_y = 4;
1117N/A event_context->xp = 0;
1117N/A event_context->yp = 0;
1117N/A event_context->tolerance = 0;
1117N/A event_context->within_tolerance = false;
1117N/A event_context->item_to_select = NULL;
1117N/A //event_context->tool_url = "/tools/shapes/star";
1117N/A
1117N/A star_context->star = NULL;
1117N/A
1117N/A star_context->magnitude = 5;
1117N/A star_context->proportion = 0.5;
1117N/A star_context->isflatsided = false;
1117N/A}
1117N/A
1117N/Avoid SPStarContext::finish() {
1117N/A SPDesktop *desktop = this->desktop;
1117N/A
1117N/A sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), GDK_CURRENT_TIME);
1117N/A this->finishItem();
1117N/A this->sel_changed_connection.disconnect();
1117N/A
1117N/A SPEventContext::finish();
1117N/A}
1117N/A
1117N/ASPStarContext::~SPStarContext() {
1117N/A this->enableGrDrag(false);
1117N/A
1117N/A this->sel_changed_connection.disconnect();
1117N/A
1117N/A delete this->shape_editor;
1117N/A this->shape_editor = NULL;
1117N/A
1117N/A /* fixme: This is necessary because we do not grab */
1117N/A if (this->star) {
1117N/A this->finishItem();
1117N/A }
1117N/A
1117N/A if (this->_message_context) {
1117N/A delete this->_message_context;
1117N/A }
1117N/A}
1117N/A
1117N/A/**
1117N/A * Callback that processes the "changed" signal on the selection;
1117N/A * destroys old and creates new knotholder.
1117N/A *
1117N/A * @param selection Should not be NULL.
1117N/A */
1117N/Avoid SPStarContext::selection_changed(Inkscape::Selection* selection) {
1117N/A g_assert (selection != NULL);
1117N/A
1117N/A this->shape_editor->unset_item(SH_KNOTHOLDER);
1117N/A this->shape_editor->set_item(selection->singleItem(), SH_KNOTHOLDER);
1117N/A}
1117N/A
1117N/Avoid SPStarContext::setup() {
1117N/A SPEventContext::setup();
1117N/A
1117N/A sp_event_context_read(this, "magnitude");
1117N/A sp_event_context_read(this, "proportion");
1117N/A sp_event_context_read(this, "isflatsided");
1117N/A sp_event_context_read(this, "rounded");
1117N/A sp_event_context_read(this, "randomized");
1117N/A
1117N/A this->shape_editor = new ShapeEditor(this->desktop);
1117N/A
1117N/A SPItem *item = sp_desktop_selection(this->desktop)->singleItem();
1117N/A if (item) {
1117N/A this->shape_editor->set_item(item, SH_KNOTHOLDER);
1117N/A }
1117N/A
1117N/A Inkscape::Selection *selection = sp_desktop_selection(this->desktop);
1117N/A
1117N/A this->sel_changed_connection.disconnect();
1117N/A
1117N/A this->sel_changed_connection = selection->connectChanged(sigc::mem_fun(this, &SPStarContext::selection_changed));
1117N/A
1117N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
1117N/A if (prefs->getBool("/tools/shapes/selcue")) {
1117N/A this->enableSelectionCue();
1117N/A }
1117N/A
1117N/A if (prefs->getBool("/tools/shapes/gradientdrag")) {
1117N/A this->enableGrDrag();
1117N/A }
1117N/A
1117N/A this->_message_context = new Inkscape::MessageContext(this->desktop->messageStack());
1117N/A}
1117N/A
1117N/Avoid SPStarContext::set(Inkscape::Preferences::Entry* val) {
1117N/A Glib::ustring path = val->getEntryName();
1117N/A
1117N/A if (path == "magnitude") {
1117N/A this->magnitude = CLAMP(val->getInt(5), 3, 1024);
1117N/A } else if (path == "proportion") {
1117N/A this->proportion = CLAMP(val->getDouble(0.5), 0.01, 2.0);
1117N/A } else if (path == "isflatsided") {
1117N/A this->isflatsided = val->getBool();
1117N/A } else if (path == "rounded") {
1117N/A this->rounded = val->getDouble();
1117N/A } else if (path == "randomized") {
1117N/A this->randomized = val->getDouble();
1117N/A }
1117N/A}
1117N/A
1117N/Agint SPStarContext::root_handler(GdkEvent* event) {
static bool dragging;
SPDesktop *desktop = this->desktop;
Inkscape::Selection *selection = sp_desktop_selection (desktop);
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
this->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
gint ret = FALSE;
switch (event->type) {
case GDK_BUTTON_PRESS:
if (event->button.button == 1 && !this->space_panning) {
dragging = true;
this->center = Inkscape::setup_for_drag_start(desktop, this, event);
/* Snap center */
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop, true);
m.freeSnapReturnByRef(this->center, Inkscape::SNAPSOURCE_NODE_HANDLE);
m.unSetup();
sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
GDK_POINTER_MOTION_MASK |
GDK_POINTER_MOTION_HINT_MASK |
GDK_BUTTON_PRESS_MASK,
NULL, event->button.time);
ret = TRUE;
}
break;
case GDK_MOTION_NOTIFY:
if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !this->space_panning) {
if ( this->within_tolerance
&& ( abs( (gint) event->motion.x - this->xp ) < this->tolerance )
&& ( abs( (gint) event->motion.y - this->yp ) < this->tolerance ) ) {
break; // do not drag if we're within tolerance from origin
}
// Once the user has moved farther than tolerance from the original location
// (indicating they intend to draw, not click), then always process the
// motion notify coordinates as given (no snapping back to origin)
this->within_tolerance = false;
Geom::Point const motion_w(event->motion.x, event->motion.y);
Geom::Point motion_dt(desktop->w2d(motion_w));
this->drag(motion_dt, event->motion.state);
gobble_motion_events(GDK_BUTTON1_MASK);
ret = TRUE;
} else if (!sp_event_context_knot_mouseover(this)) {
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
Geom::Point const motion_w(event->motion.x, event->motion.y);
Geom::Point motion_dt(desktop->w2d(motion_w));
m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
m.unSetup();
}
break;
case GDK_BUTTON_RELEASE:
this->xp = this->yp = 0;
if (event->button.button == 1 && !this->space_panning) {
dragging = false;
sp_event_context_discard_delayed_snap_event(this);
if (!this->within_tolerance) {
// we've been dragging, finish the star
this->finishItem();
} else if (this->item_to_select) {
// no dragging, select clicked item if any
if (event->button.state & GDK_SHIFT_MASK) {
selection->toggle(this->item_to_select);
} else {
selection->set(this->item_to_select);
}
} else {
// click in an empty space
selection->clear();
}
this->item_to_select = NULL;
ret = TRUE;
sp_canvas_item_ungrab(SP_CANVAS_ITEM (desktop->acetate), event->button.time);
}
break;
case GDK_KEY_PRESS:
switch (get_group0_keyval(&event->key)) {
case GDK_KEY_Alt_R:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
case GDK_KEY_Meta_R:
sp_event_show_modifier_tip(this->defaultMessageContext(), event,
_("<b>Ctrl</b>: snap angle; keep rays radial"),
NULL,
NULL);
break;
case GDK_KEY_Up:
case GDK_KEY_Down:
case GDK_KEY_KP_Up:
case GDK_KEY_KP_Down:
// prevent the zoom field from activation
if (!MOD__CTRL_ONLY)
ret = TRUE;
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY) {
desktop->setToolboxFocusTo ("altx-star");
ret = TRUE;
}
break;
case GDK_KEY_Escape:
if (dragging) {
dragging = false;
sp_event_context_discard_delayed_snap_event(this);
// if drawing, cancel, otherwise pass it up for deselecting
this->cancel();
ret = TRUE;
}
break;
case GDK_KEY_space:
if (dragging) {
sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), event->button.time);
dragging = false;
sp_event_context_discard_delayed_snap_event(this);
if (!this->within_tolerance) {
// we've been dragging, finish the star
this->finishItem();
}
// do not return true, so that space would work switching to selector
}
break;
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
case GDK_KEY_BackSpace:
ret = this->deleteSelectedDrag(MOD__CTRL_ONLY);
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
switch (get_group0_keyval (&event->key)) {
case GDK_KEY_Alt_L:
case GDK_KEY_Alt_R:
case GDK_KEY_Control_L:
case GDK_KEY_Control_R:
case GDK_KEY_Shift_L:
case GDK_KEY_Shift_R:
case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt
case GDK_KEY_Meta_R:
this->defaultMessageContext()->clear();
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
ret = SPEventContext::root_handler(event);
}
return ret;
}
void SPStarContext::drag(Geom::Point p, guint state)
{
SPDesktop *desktop = this->desktop;
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
int const snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
if (!this->star) {
if (Inkscape::have_viable_layer(desktop, this->_message_context) == false) {
return;
}
// Create object
Inkscape::XML::Document *xml_doc = this->desktop->doc()->getReprDoc();
Inkscape::XML::Node *repr = xml_doc->createElement("svg:path");
repr->setAttribute("sodipodi:type", "star");
// Set style
sp_desktop_apply_style_tool(desktop, repr, "/tools/shapes/star", false);
this->star = SP_STAR(desktop->currentLayer()->appendChildRepr(repr));
Inkscape::GC::release(repr);
this->star->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
this->star->updateRepr();
desktop->canvas->forceFullRedrawAfterInterruptions(5);
}
/* Snap corner point with no constraints */
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop, true, this->star);
Geom::Point pt2g = p;
m.freeSnapReturnByRef(pt2g, Inkscape::SNAPSOURCE_NODE_HANDLE);
m.unSetup();
Geom::Point const p0 = desktop->dt2doc(this->center);
Geom::Point const p1 = desktop->dt2doc(pt2g);
double const sides = (gdouble) this->magnitude;
Geom::Point const d = p1 - p0;
Geom::Coord const r1 = Geom::L2(d);
double arg1 = atan2(d);
if (state & GDK_CONTROL_MASK) {
/* Snap angle */
arg1 = sp_round(arg1, M_PI / snaps);
}
sp_star_position_set(this->star, this->magnitude, p0, r1, r1 * this->proportion,
arg1, arg1 + M_PI / sides, this->isflatsided, this->rounded, this->randomized);
/* status text */
GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
this->_message_context->setF(Inkscape::IMMEDIATE_MESSAGE,
( this->isflatsided?
_("<b>Polygon</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle")
: _("<b>Star</b>: radius %s, angle %5g&#176;; with <b>Ctrl</b> to snap angle") ),
rads->str, sp_round((arg1) * 180 / M_PI, 0.0001));
g_string_free(rads, FALSE);
}
void SPStarContext::finishItem() {
this->_message_context->clear();
if (this->star != NULL) {
if (this->star->r[1] == 0) {
// Don't allow the creating of zero sized arc, for example
// when the start and and point snap to the snap grid point
this->cancel();
return;
}
// Set transform center, so that odd stars rotate correctly
// LP #462157
this->star->setCenter(this->center);
this->star->set_shape();
this->star->updateRepr(SP_OBJECT_WRITE_EXT);
desktop->canvas->endForcedFullRedraws();
sp_desktop_selection(desktop)->set(this->star);
DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
_("Create star"));
this->star = NULL;
}
}
void SPStarContext::cancel() {
sp_desktop_selection(desktop)->clear();
sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate), 0);
if (this->star != NULL) {
this->star->deleteObject();
this->star = NULL;
}
this->within_tolerance = false;
this->xp = 0;
this->yp = 0;
this->item_to_select = NULL;
desktop->canvas->endForcedFullRedraws();
DocumentUndo::cancel(sp_desktop_document(desktop));
}
/*
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 :