star-context.cpp revision 9b64c78f3873403775d80ce62fcec325a92a62a7
1607N/A#define __SP_STAR_CONTEXT_C__
1607N/A
1607N/A/*
1607N/A * Star drawing context
1607N/A *
1607N/A * Authors:
1607N/A * Mitsuru Oka <oka326@parkcity.ne.jp>
1607N/A * Lauris Kaplinski <lauris@kaplinski.com>
1607N/A * bulia byak <buliabyak@users.sf.net>
1607N/A *
1607N/A * Copyright (C) 1999-2002 Lauris Kaplinski
1607N/A * Copyright (C) 2001-2002 Mitsuru Oka
1607N/A *
1607N/A * Released under GNU GPL, read the file 'COPYING' for more information
1607N/A */
1607N/A
1607N/A#include "config.h"
1607N/A
1607N/A#include <gdk/gdkkeysyms.h>
1607N/A
1607N/A#include "macros.h"
1607N/A#include "display/sp-canvas.h"
1607N/A#include "sp-star.h"
1607N/A#include "document.h"
1607N/A#include "sp-namedview.h"
1607N/A#include "selection.h"
1607N/A#include "desktop-handles.h"
1607N/A#include "desktop-affine.h"
2847N/A#include "snap.h"
2457N/A#include "desktop.h"
2478N/A#include "desktop-style.h"
1607N/A#include "message-context.h"
2284N/A#include "pixmaps/cursor-star.xpm"
2284N/A#include "sp-metrics.h"
2375N/A#include <glibmm/i18n.h>
1607N/A#include "prefs-utils.h"
1607N/A#include "xml/repr.h"
1607N/A#include "xml/node-event-vector.h"
1607N/A#include "object-edit.h"
2375N/A#include "context-fns.h"
2375N/A
1607N/A#include "star-context.h"
1607N/A
1607N/Astatic void sp_star_context_class_init (SPStarContextClass * klass);
1607N/Astatic void sp_star_context_init (SPStarContext * star_context);
1607N/Astatic void sp_star_context_dispose (GObject *object);
2375N/A
1607N/Astatic void sp_star_context_setup (SPEventContext *ec);
1607N/Astatic void sp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val);
2433N/Astatic gint sp_star_context_root_handler (SPEventContext *ec, GdkEvent *event);
2284N/A
1607N/Astatic void sp_star_drag (SPStarContext * sc, NR::Point p, guint state);
2433N/Astatic void sp_star_finish (SPStarContext * sc);
2433N/A
2433N/Astatic SPEventContextClass * parent_class;
2433N/A
2433N/AGtkType
1607N/Asp_star_context_get_type (void)
1607N/A{
1607N/A static GType type = 0;
1607N/A if (!type) {
1607N/A GTypeInfo info = {
1607N/A sizeof (SPStarContextClass),
1607N/A NULL, NULL,
1607N/A (GClassInitFunc) sp_star_context_class_init,
2375N/A NULL, NULL,
2375N/A sizeof (SPStarContext),
1607N/A 4,
1607N/A (GInstanceInitFunc) sp_star_context_init,
1607N/A NULL, /* value_table */
1607N/A };
2415N/A type = g_type_register_static (SP_TYPE_EVENT_CONTEXT, "SPStarContext", &info, (GTypeFlags)0);
1607N/A }
1607N/A return type;
1607N/A}
1607N/A
1607N/Astatic void
1607N/Asp_star_context_class_init (SPStarContextClass * klass)
1607N/A{
1607N/A GObjectClass *object_class = (GObjectClass *) klass;
1607N/A SPEventContextClass *event_context_class = (SPEventContextClass *) klass;
1929N/A
2415N/A parent_class = (SPEventContextClass*)g_type_class_peek_parent (klass);
2415N/A
1607N/A object_class->dispose = sp_star_context_dispose;
2415N/A
2415N/A event_context_class->setup = sp_star_context_setup;
2810N/A event_context_class->set = sp_star_context_set;
2810N/A event_context_class->root_handler = sp_star_context_root_handler;
2810N/A}
2810N/A
2810N/Astatic void
2810N/Asp_star_context_init (SPStarContext * star_context)
2810N/A{
2810N/A SPEventContext *event_context = SP_EVENT_CONTEXT (star_context);
2810N/A
2810N/A event_context->cursor_shape = cursor_star_xpm;
2810N/A event_context->hot_x = 4;
2810N/A event_context->hot_y = 4;
2810N/A event_context->xp = 0;
2810N/A event_context->yp = 0;
2810N/A event_context->tolerance = 0;
2810N/A event_context->within_tolerance = false;
2810N/A event_context->item_to_select = NULL;
2810N/A
2810N/A event_context->shape_repr = NULL;
2810N/A event_context->shape_knot_holder = NULL;
2810N/A
2810N/A star_context->item = NULL;
2810N/A
2810N/A star_context->magnitude = 5;
2810N/A star_context->proportion = 0.5;
2810N/A star_context->isflatsided = false;
2810N/A
2810N/A new (&star_context->sel_changed_connection) sigc::connection();
2810N/A}
2810N/A
2810N/Astatic void
2810N/Asp_star_context_dispose (GObject *object)
2433N/A{
2433N/A SPEventContext *ec = SP_EVENT_CONTEXT (object);
2433N/A SPStarContext *sc = SP_STAR_CONTEXT (object);
2433N/A
2433N/A ec->enableGrDrag(false);
2433N/A
2433N/A sc->sel_changed_connection.disconnect();
2433N/A sc->sel_changed_connection.~connection();
1607N/A
2375N/A if (ec->shape_knot_holder) {
2375N/A sp_knot_holder_destroy (ec->shape_knot_holder);
2433N/A ec->shape_knot_holder = NULL;
2375N/A }
2433N/A
2375N/A if (ec->shape_repr) { // remove old listener
2457N/A sp_repr_remove_listener_by_data (ec->shape_repr, ec);
2375N/A Inkscape::GC::release(ec->shape_repr);
2457N/A ec->shape_repr = 0;
2457N/A }
2457N/A
2457N/A /* fixme: This is necessary because we do not grab */
2457N/A if (sc->item) sp_star_finish (sc);
2507N/A
2457N/A if (sc->_message_context) {
2457N/A delete sc->_message_context;
2457N/A }
2457N/A
2457N/A G_OBJECT_CLASS (parent_class)->dispose (object);
2457N/A}
2457N/A
2457N/Astatic Inkscape::XML::NodeEventVector ec_shape_repr_events = {
2457N/A NULL, /* child_added */
2478N/A NULL, /* child_removed */
2457N/A ec_shape_event_attr_changed,
2478N/A NULL, /* content_changed */
2478N/A NULL /* order_changed */
2847N/A};
2847N/A
2847N/A/**
2847N/A\brief Callback that processes the "changed" signal on the selection;
2847N/Adestroys old and creates new knotholder
2847N/A\param selection Should not be NULL.
2847N/A*/
2847N/Avoid
2847N/Asp_star_context_selection_changed (Inkscape::Selection * selection, gpointer data)
2457N/A{
2457N/A g_assert (selection != NULL);
2457N/A
2457N/A SPStarContext *sc = SP_STAR_CONTEXT (data);
2457N/A SPEventContext *ec = SP_EVENT_CONTEXT (sc);
2457N/A
2457N/A if (ec->shape_knot_holder) { // desktroy knotholder
2457N/A sp_knot_holder_destroy (ec->shape_knot_holder);
2457N/A ec->shape_knot_holder = NULL;
2457N/A }
2457N/A
2457N/A if (ec->shape_repr) { // remove old listener
2457N/A sp_repr_remove_listener_by_data (ec->shape_repr, ec);
2457N/A Inkscape::GC::release(ec->shape_repr);
2457N/A ec->shape_repr = 0;
2457N/A }
2457N/A
2457N/A SPItem *item = selection->singleItem();
2457N/A if (item) {
2457N/A ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
2457N/A Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
2375N/A if (shape_repr) {
1607N/A ec->shape_repr = shape_repr;
2433N/A Inkscape::GC::anchor(shape_repr);
2375N/A sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
2433N/A }
2375N/A }
2457N/A}
2375N/A
2457N/Astatic void
2457N/Asp_star_context_setup (SPEventContext *ec)
2457N/A{
2457N/A SPStarContext *sc = SP_STAR_CONTEXT (ec);
2457N/A
2507N/A if (((SPEventContextClass *) parent_class)->setup)
2457N/A ((SPEventContextClass *) parent_class)->setup (ec);
2457N/A
2457N/A sp_event_context_read (ec, "magnitude");
2457N/A sp_event_context_read (ec, "proportion");
2457N/A sp_event_context_read (ec, "isflatsided");
2457N/A sp_event_context_read (ec, "rounded");
2457N/A sp_event_context_read (ec, "randomized");
2457N/A
2457N/A Inkscape::Selection *selection = sp_desktop_selection(ec->desktop);
2478N/A
2457N/A SPItem *item = selection->singleItem();
2847N/A if (item) {
2847N/A ec->shape_knot_holder = sp_item_knot_holder (item, ec->desktop);
2847N/A Inkscape::XML::Node *shape_repr = SP_OBJECT_REPR (item);
2847N/A if (shape_repr) {
2847N/A ec->shape_repr = shape_repr;
2847N/A Inkscape::GC::anchor(shape_repr);
2847N/A sp_repr_add_listener (shape_repr, &ec_shape_repr_events, ec);
2478N/A }
2478N/A }
2847N/A
2847N/A sc->sel_changed_connection.disconnect();
2457N/A sc->sel_changed_connection = selection->connectChanged(sigc::bind(sigc::ptr_fun(&sp_star_context_selection_changed), (gpointer)sc));
2457N/A
2457N/A if (prefs_get_int_attribute("tools.shapes", "selcue", 0) != 0) {
2457N/A ec->enableSelectionCue();
2457N/A }
2457N/A
2457N/A if (prefs_get_int_attribute("tools.shapes", "gradientdrag", 0) != 0) {
2457N/A ec->enableGrDrag();
2457N/A }
2457N/A
2457N/A sc->_message_context = new Inkscape::MessageContext((ec->desktop)->messageStack());
2457N/A}
2457N/A
2457N/Astatic void
2457N/Asp_star_context_set (SPEventContext *ec, const gchar *key, const gchar *val)
2457N/A{
2457N/A SPStarContext *sc = SP_STAR_CONTEXT (ec);
2457N/A if (!strcmp (key, "magnitude")) {
2457N/A sc->magnitude = (val) ? atoi (val) : 5;
2457N/A sc->magnitude = CLAMP (sc->magnitude, 3, 1024);
2457N/A } else if (!strcmp (key, "proportion")) {
2375N/A sc->proportion = (val) ? g_ascii_strtod (val, NULL) : 0.5;
2375N/A sc->proportion = CLAMP (sc->proportion, 0.01, 2.0);
2375N/A } else if (!strcmp (key, "isflatsided")) {
2375N/A if (val && !strcmp(val, "true"))
2375N/A sc->isflatsided = true;
2375N/A else
2375N/A sc->isflatsided = false;
2375N/A } else if (!strcmp (key, "rounded")) {
2375N/A sc->rounded = (val) ? g_ascii_strtod (val, NULL) : 0.0;
2375N/A } else if (!strcmp (key, "randomized")) {
2375N/A sc->randomized = (val) ? g_ascii_strtod (val, NULL) : 0.0;
2375N/A }
2375N/A}
2375N/A
2375N/Astatic gint sp_star_context_root_handler(SPEventContext *event_context, GdkEvent *event)
2375N/A{
2375N/A static gboolean dragging;
2375N/A
2375N/A SPDesktop *desktop = event_context->desktop;
2375N/A Inkscape::Selection *selection = sp_desktop_selection (desktop);
2375N/A
2375N/A SPStarContext *sc = SP_STAR_CONTEXT (event_context);
2375N/A
2375N/A event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
2375N/A
2375N/A gint ret = FALSE;
2375N/A
2375N/A switch (event->type) {
2375N/A case GDK_BUTTON_PRESS:
2375N/A if (event->button.button == 1 && !event_context->space_panning) {
2375N/A
2375N/A dragging = TRUE;
2375N/A
1607N/A sc->center = Inkscape::setup_for_drag_start(desktop, event_context, event);
1607N/A
1607N/A SnapManager const &m = desktop->namedview->snap_manager;
1607N/A sc->center = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, sc->center, sc->item).getPoint();
1607N/A
1607N/A sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
1607N/A GDK_KEY_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
1607N/A GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK,
1607N/A NULL, event->button.time);
1607N/A ret = TRUE;
1607N/A }
1607N/A break;
2375N/A case GDK_MOTION_NOTIFY:
2375N/A if (dragging && (event->motion.state & GDK_BUTTON1_MASK) && !event_context->space_panning) {
1607N/A
1607N/A if ( event_context->within_tolerance
1607N/A && ( abs( (gint) event->motion.x - event_context->xp ) < event_context->tolerance )
2375N/A && ( abs( (gint) event->motion.y - event_context->yp ) < event_context->tolerance ) ) {
2375N/A break; // do not drag if we're within tolerance from origin
2375N/A }
2375N/A // Once the user has moved farther than tolerance from the original location
2375N/A // (indicating they intend to draw, not click), then always process the
1607N/A // motion notify coordinates as given (no snapping back to origin)
1607N/A event_context->within_tolerance = false;
1607N/A
1607N/A NR::Point const motion_w(event->motion.x, event->motion.y);
1607N/A NR::Point motion_dt(event_context->desktop->w2d(motion_w));
1607N/A
1607N/A SnapManager const &m = desktop->namedview->snap_manager;
1607N/A motion_dt = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, motion_dt, sc->item).getPoint();
1607N/A
1607N/A sp_star_drag (sc, motion_dt, event->motion.state);
2375N/A ret = TRUE;
2375N/A }
2375N/A break;
2375N/A case GDK_BUTTON_RELEASE:
2375N/A event_context->xp = event_context->yp = 0;
2375N/A if (event->button.button == 1 && !event_context->space_panning) {
2375N/A dragging = FALSE;
2375N/A if (!event_context->within_tolerance) {
1607N/A // we've been dragging, finish the star
sp_star_finish (sc);
} else if (event_context->item_to_select) {
// no dragging, select clicked item if any
if (event->button.state & GDK_SHIFT_MASK) {
selection->toggle(event_context->item_to_select);
} else {
selection->set(event_context->item_to_select);
}
} else {
// click in an empty space
selection->clear();
}
event_context->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_Alt_R:
case GDK_Control_L:
case GDK_Control_R:
case GDK_Shift_L:
case GDK_Shift_R:
case GDK_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
case GDK_Meta_R:
sp_event_show_modifier_tip(event_context->defaultMessageContext(), event,
_("<b>Ctrl</b>: snap angle; keep rays radial"),
NULL,
NULL);
break;
case GDK_Up:
case GDK_Down:
case GDK_KP_Up:
case GDK_KP_Down:
// prevent the zoom field from activation
if (!MOD__CTRL_ONLY)
ret = TRUE;
break;
case GDK_x:
case GDK_X:
if (MOD__ALT_ONLY) {
desktop->setToolboxFocusTo ("altx-star");
ret = TRUE;
}
break;
case GDK_Escape:
sp_desktop_selection(desktop)->clear();
//TODO: make dragging escapable by Esc
break;
case GDK_space:
if (dragging) {
sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
event->button.time);
dragging = false;
if (!event_context->within_tolerance) {
// we've been dragging, finish the rect
sp_star_finish(sc);
}
// do not return true, so that space would work switching to selector
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
switch (get_group0_keyval (&event->key)) {
case GDK_Alt_L:
case GDK_Alt_R:
case GDK_Control_L:
case GDK_Control_R:
case GDK_Shift_L:
case GDK_Shift_R:
case GDK_Meta_L: // Meta is when you press Shift+Alt
case GDK_Meta_R:
event_context->defaultMessageContext()->clear();
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
if (((SPEventContextClass *) parent_class)->root_handler)
ret = ((SPEventContextClass *) parent_class)->root_handler (event_context, event);
}
return ret;
}
static void sp_star_drag(SPStarContext *sc, NR::Point p, guint state)
{
SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
int const snaps = prefs_get_int_attribute ("options.rotationsnapsperpi", "value", 12);
if (!sc->item) {
if (Inkscape::have_viable_layer(desktop, sc->_message_context) == false) {
return;
}
/* Create object */
Inkscape::XML::Document *xml_doc = sp_document_repr_doc(SP_EVENT_CONTEXT_DOCUMENT(sc));
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);
sc->item = SP_ITEM(desktop->currentLayer()->appendChildRepr(repr));
Inkscape::GC::release(repr);
sc->item->transform = SP_ITEM(desktop->currentRoot())->getRelativeTransform(desktop->currentLayer());
sc->item->updateRepr();
sp_canvas_force_full_redraw_after_interruptions(desktop->canvas, 5);
}
NR::Point const p0 = sp_desktop_dt2root_xy_point(desktop, sc->center);
NR::Point p1 = sp_desktop_dt2root_xy_point(desktop, p);
/* Snap corner point with no constraints */
SnapManager const &m = desktop->namedview->snap_manager;
p1 = m.freeSnap(Inkscape::Snapper::SNAPPOINT_NODE, p1, sc->item).getPoint();
SPStar *star = SP_STAR(sc->item);
double const sides = (gdouble) sc->magnitude;
NR::Point const d = p1 - p0;
NR::Coord const r1 = NR::L2(d);
double arg1 = atan2(d);
if (state & GDK_CONTROL_MASK) {
/* Snap angle */
arg1 = sp_round(arg1, M_PI / snaps);
}
sp_star_position_set(star, sc->magnitude, p0, r1, r1 * sc->proportion,
arg1, arg1 + M_PI / sides, sc->isflatsided, sc->rounded, sc->randomized);
/* status text */
GString *rads = SP_PX_TO_METRIC_STRING(r1, desktop->namedview->getDefaultMetric());
sc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
( sc->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);
}
static void
sp_star_finish (SPStarContext * sc)
{
sc->_message_context->clear();
if (sc->item != NULL) {
SPDesktop *desktop = SP_EVENT_CONTEXT(sc)->desktop;
SPObject *object = SP_OBJECT(sc->item);
sp_shape_set_shape(SP_SHAPE(sc->item));
object->updateRepr(SP_OBJECT_WRITE_EXT);
sp_canvas_end_forced_full_redraws(desktop->canvas);
sp_desktop_selection(desktop)->set(sc->item);
sp_document_done(sp_desktop_document(desktop), SP_VERB_CONTEXT_STAR,
_("Create star"));
sc->item = NULL;
}
}
/*
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:encoding=utf-8:textwidth=99 :