781N/A/*
919N/A * Rectangle drawing context
0N/A *
0N/A * Author:
919N/A * Lauris Kaplinski <lauris@kaplinski.com>
919N/A * bulia byak <buliabyak@users.sf.net>
919N/A * Jon A. Cruz <jon@joncruz.org>
919N/A * Abhishek Sharma
919N/A *
0N/A * Copyright (C) 2006 Johan Engelen <johan@shouraizou.nl>
919N/A * Copyright (C) 2000-2005 authors
919N/A * Copyright (C) 2000-2001 Ximian, Inc.
919N/A *
0N/A * Released under GNU GPL, read the file 'COPYING' for more information
919N/A */
919N/A
919N/A#include "config.h"
919N/A
919N/A#include <gdk/gdkkeysyms.h>
919N/A#include <cstring>
919N/A#include <string>
0N/A
0N/A#include "macros.h"
0N/A#include "display/sp-canvas.h"
419N/A#include "sp-rect.h"
781N/A#include "document.h"
781N/A#include "document-undo.h"
781N/A#include "sp-namedview.h"
0N/A#include "selection.h"
419N/A#include "selection-chemistry.h"
781N/A
781N/A#include "snap.h"
781N/A#include "desktop.h"
0N/A#include "desktop-style.h"
781N/A#include "message-context.h"
0N/A#include "pixmaps/cursor-rect.xpm"
781N/A#include "ui/tools/rect-tool.h"
781N/A#include <glibmm/i18n.h>
781N/A#include "xml/repr.h"
781N/A#include "xml/node-event-vector.h"
781N/A#include "preferences.h"
0N/A#include "context-fns.h"
0N/A#include "ui/shape-editor.h"
98N/A#include "verbs.h"
0N/A#include "display/sp-canvas-item.h"
0N/A
0N/Ausing Inkscape::DocumentUndo;
0N/A
0N/Anamespace Inkscape {
0N/Anamespace UI {
0N/Anamespace Tools {
0N/A
419N/Aconst std::string& RectTool::getPrefsPath() {
98N/A return RectTool::prefsPath;
98N/A}
0N/A
98N/Aconst std::string RectTool::prefsPath = "/tools/shapes/rect";
0N/A
0N/ARectTool::RectTool()
0N/A : ToolBase(cursor_rect_xpm, 4, 4)
0N/A , rect(NULL)
0N/A , rx(0)
0N/A , ry(0)
0N/A{
0N/A}
0N/A
0N/Avoid RectTool::finish() {
766N/A sp_canvas_item_ungrab(SP_CANVAS_ITEM(this->desktop->acetate), GDK_CURRENT_TIME);
0N/A
419N/A this->finishItem();
0N/A this->sel_changed_connection.disconnect();
0N/A
0N/A ToolBase::finish();
766N/A}
0N/A
0N/ARectTool::~RectTool() {
0N/A this->enableGrDrag(false);
0N/A
781N/A this->sel_changed_connection.disconnect();
781N/A
781N/A delete this->shape_editor;
781N/A this->shape_editor = NULL;
781N/A
781N/A /* fixme: This is necessary because we do not grab */
781N/A if (this->rect) {
781N/A this->finishItem();
781N/A }
781N/A}
419N/A
419N/A/**
419N/A * Callback that processes the "changed" signal on the selection;
419N/A * destroys old and creates new knotholder.
419N/A */
419N/Avoid RectTool::selection_changed(Inkscape::Selection* selection) {
781N/A this->shape_editor->unset_item();
419N/A this->shape_editor->set_item(selection->singleItem());
0N/A}
781N/A
781N/Avoid RectTool::setup() {
781N/A ToolBase::setup();
781N/A
781N/A this->shape_editor = new ShapeEditor(this->desktop);
781N/A
781N/A SPItem *item = this->desktop->getSelection()->singleItem();
781N/A if (item) {
781N/A this->shape_editor->set_item(item);
781N/A }
781N/A
0N/A this->sel_changed_connection.disconnect();
0N/A this->sel_changed_connection = this->desktop->getSelection()->connectChanged(
419N/A sigc::mem_fun(this, &RectTool::selection_changed)
419N/A );
419N/A
781N/A sp_event_context_read(this, "rx");
419N/A sp_event_context_read(this, "ry");
0N/A
781N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
781N/A if (prefs->getBool("/tools/shapes/selcue")) {
781N/A this->enableSelectionCue();
0N/A }
0N/A
0N/A if (prefs->getBool("/tools/shapes/gradientdrag")) {
781N/A this->enableGrDrag();
0N/A }
0N/A}
419N/A
0N/Avoid RectTool::set(const Inkscape::Preferences::Entry& val) {
0N/A /* fixme: Proper error handling for non-numeric data. Use a locale-independent function like
0N/A * g_ascii_strtod (or a thin wrapper that does the right thing for invalid values inf/nan). */
419N/A Glib::ustring name = val.getEntryName();
0N/A
419N/A if ( name == "rx" ) {
781N/A this->rx = val.getDoubleLimited(); // prevents NaN and +/-Inf from messing up
781N/A } else if ( name == "ry" ) {
781N/A this->ry = val.getDoubleLimited();
781N/A }
781N/A}
781N/A
0N/Abool RectTool::item_handler(SPItem* item, GdkEvent* event) {
419N/A gint ret = FALSE;
419N/A
419N/A switch (event->type) {
781N/A case GDK_BUTTON_PRESS:
781N/A if ( event->button.button == 1 && !this->space_panning) {
781N/A Inkscape::setup_for_drag_start(desktop, this, event);
419N/A }
419N/A break;
781N/A // motion and release are always on root (why?)
781N/A default:
781N/A break;
781N/A }
781N/A
0N/A ret = ToolBase::item_handler(item, event);
0N/A
419N/A return ret;
0N/A}
419N/A
419N/Abool RectTool::root_handler(GdkEvent* event) {
419N/A static bool dragging;
419N/A
419N/A SPDesktop *desktop = this->desktop;
781N/A Inkscape::Selection *selection = desktop->getSelection();
781N/A
419N/A Inkscape::Preferences *prefs = Inkscape::Preferences::get();
419N/A
419N/A this->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
0N/A
0N/A gint ret = FALSE;
781N/A
781N/A switch (event->type) {
781N/A case GDK_BUTTON_PRESS:
781N/A if (event->button.button == 1 && !this->space_panning) {
781N/A Geom::Point const button_w(event->button.x, event->button.y);
781N/A
781N/A // save drag origin
781N/A this->xp = (gint) button_w[Geom::X];
781N/A this->yp = (gint) button_w[Geom::Y];
781N/A this->within_tolerance = true;
781N/A
781N/A // remember clicked item, disregarding groups, honoring Alt
781N/A this->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
781N/A
781N/A dragging = true;
781N/A
781N/A /* Position center */
781N/A Geom::Point button_dt(desktop->w2d(button_w));
781N/A this->center = button_dt;
781N/A
781N/A /* Snap center */
781N/A SnapManager &m = desktop->namedview->snap_manager;
781N/A m.setup(desktop);
781N/A m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
781N/A m.unSetup();
781N/A this->center = button_dt;
781N/A
781N/A sp_canvas_item_grab(SP_CANVAS_ITEM(desktop->acetate),
781N/A ( GDK_KEY_PRESS_MASK |
781N/A GDK_BUTTON_RELEASE_MASK |
781N/A GDK_POINTER_MOTION_MASK |
781N/A GDK_POINTER_MOTION_HINT_MASK |
781N/A GDK_BUTTON_PRESS_MASK ),
781N/A NULL, event->button.time);
781N/A
781N/A ret = TRUE;
781N/A }
0N/A break;
419N/A case GDK_MOTION_NOTIFY:
419N/A if ( dragging
419N/A && (event->motion.state & GDK_BUTTON1_MASK) && !this->space_panning)
419N/A {
0N/A if ( this->within_tolerance
419N/A && ( abs( (gint) event->motion.x - this->xp ) < this->tolerance )
0N/A && ( abs( (gint) event->motion.y - this->yp ) < this->tolerance ) ) {
781N/A break; // do not drag if we're within tolerance from origin
419N/A }
0N/A // Once the user has moved farther than tolerance from the original location
0N/A // (indicating they intend to draw, not click), then always process the
0N/A // motion notify coordinates as given (no snapping back to origin)
419N/A this->within_tolerance = false;
419N/A
419N/A Geom::Point const motion_w(event->motion.x, event->motion.y);
419N/A Geom::Point motion_dt(desktop->w2d(motion_w));
0N/A
419N/A this->drag(motion_dt, event->motion.state); // this will also handle the snapping
0N/A gobble_motion_events(GDK_BUTTON1_MASK);
0N/A ret = TRUE;
0N/A } else if (!this->sp_event_context_knot_mouseover()) {
0N/A SnapManager &m = desktop->namedview->snap_manager;
781N/A m.setup(desktop);
781N/A
781N/A Geom::Point const motion_w(event->motion.x, event->motion.y);
781N/A Geom::Point motion_dt(desktop->w2d(motion_w));
419N/A
419N/A m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_NODE_HANDLE));
781N/A m.unSetup();
0N/A }
0N/A break;
0N/A case GDK_BUTTON_RELEASE:
0N/A this->xp = this->yp = 0;
419N/A if (event->button.button == 1 && !this->space_panning) {
0N/A dragging = false;
0N/A sp_event_context_discard_delayed_snap_event(this);
0N/A
419N/A if (!this->within_tolerance) {
0N/A // we've been dragging, finish the rect
419N/A this->finishItem();
419N/A } else if (this->item_to_select) {
781N/A // no dragging, select clicked item if any
781N/A if (event->button.state & GDK_SHIFT_MASK) {
419N/A selection->toggle(this->item_to_select);
419N/A } else {
419N/A selection->set(this->item_to_select);
419N/A }
781N/A } else {
781N/A // click in an empty space
419N/A selection->clear();
781N/A }
419N/A
0N/A this->item_to_select = NULL;
781N/A ret = TRUE;
781N/A sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
781N/A event->button.time);
419N/A }
419N/A break;
781N/A case GDK_KEY_PRESS:
419N/A switch (get_group0_keyval (&event->key)) {
0N/A case GDK_KEY_Alt_L:
419N/A case GDK_KEY_Alt_R:
0N/A case GDK_KEY_Control_L:
0N/A case GDK_KEY_Control_R:
419N/A case GDK_KEY_Shift_L:
0N/A case GDK_KEY_Shift_R:
419N/A case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt (at least on my machine)
0N/A case GDK_KEY_Meta_R:
0N/A if (!dragging){
419N/A sp_event_show_modifier_tip (this->defaultMessageContext(), event,
0N/A _("<b>Ctrl</b>: make square or integer-ratio rect, lock a rounded corner circular"),
781N/A _("<b>Shift</b>: draw around the starting point"),
419N/A NULL);
419N/A }
419N/A break;
419N/A case GDK_KEY_Up:
419N/A case GDK_KEY_Down:
419N/A case GDK_KEY_KP_Up:
781N/A case GDK_KEY_KP_Down:
419N/A // prevent the zoom field from activation
781N/A if (!MOD__CTRL_ONLY(event))
781N/A ret = TRUE;
781N/A break;
781N/A
0N/A case GDK_KEY_x:
0N/A case GDK_KEY_X:
0N/A if (MOD__ALT_ONLY(event)) {
419N/A desktop->setToolboxFocusTo ("altx-rect");
0N/A ret = TRUE;
419N/A }
419N/A break;
419N/A
781N/A case GDK_KEY_g:
419N/A case GDK_KEY_G:
419N/A if (MOD__SHIFT_ONLY(event)) {
419N/A sp_selection_to_guides(desktop);
419N/A ret = true;
419N/A }
781N/A break;
419N/A
419N/A case GDK_KEY_Escape:
419N/A if (dragging) {
0N/A dragging = false;
419N/A sp_event_context_discard_delayed_snap_event(this);
419N/A // if drawing, cancel, otherwise pass it up for deselecting
419N/A this->cancel();
419N/A ret = TRUE;
419N/A }
419N/A break;
419N/A
419N/A case GDK_KEY_space:
0N/A if (dragging) {
419N/A sp_canvas_item_ungrab(SP_CANVAS_ITEM(desktop->acetate),
419N/A event->button.time);
0N/A dragging = false;
419N/A sp_event_context_discard_delayed_snap_event(this);
419N/A
419N/A if (!this->within_tolerance) {
419N/A // we've been dragging, finish the rect
419N/A this->finishItem();
419N/A }
419N/A // do not return true, so that space would work switching to selector
419N/A }
419N/A break;
419N/A
0N/A case GDK_KEY_Delete:
419N/A case GDK_KEY_KP_Delete:
419N/A case GDK_KEY_BackSpace:
419N/A ret = this->deleteSelectedDrag(MOD__CTRL_ONLY(event));
419N/A break;
781N/A
419N/A default:
419N/A break;
0N/A }
419N/A break;
0N/A case GDK_KEY_RELEASE:
419N/A switch (get_group0_keyval (&event->key)) {
419N/A case GDK_KEY_Alt_L:
419N/A case GDK_KEY_Alt_R:
419N/A case GDK_KEY_Control_L:
419N/A case GDK_KEY_Control_R:
419N/A case GDK_KEY_Shift_L:
419N/A case GDK_KEY_Shift_R:
419N/A case GDK_KEY_Meta_L: // Meta is when you press Shift+Alt
419N/A case GDK_KEY_Meta_R:
419N/A this->defaultMessageContext()->clear();
419N/A break;
0N/A default:
419N/A break;
419N/A }
419N/A break;
419N/A default:
419N/A break;
0N/A }
0N/A
0N/A if (!ret) {
419N/A ret = ToolBase::root_handler(event);
781N/A }
419N/A
419N/A return ret;
419N/A}
419N/A
419N/Avoid RectTool::drag(Geom::Point const pt, guint state) {
419N/A SPDesktop *desktop = this->desktop;
419N/A
419N/A if (!this->rect) {
419N/A if (Inkscape::have_viable_layer(desktop, this->message_context) == false) {
419N/A return;
419N/A }
419N/A
419N/A // Create object
419N/A Inkscape::XML::Document *xml_doc = this->desktop->doc()->getReprDoc();
419N/A Inkscape::XML::Node *repr = xml_doc->createElement("svg:rect");
419N/A
419N/A // Set style
419N/A sp_desktop_apply_style_tool (desktop, repr, "/tools/shapes/rect", false);
419N/A
419N/A this->rect = SP_RECT(desktop->currentLayer()->appendChildRepr(repr));
419N/A Inkscape::GC::release(repr);
419N/A
419N/A this->rect->transform = SP_ITEM(desktop->currentLayer())->i2doc_affine().inverse();
419N/A this->rect->updateRepr();
419N/A
419N/A desktop->canvas->forceFullRedrawAfterInterruptions(5);
419N/A }
419N/A
419N/A Geom::Rect const r = Inkscape::snap_rectangular_box(desktop, this->rect, pt, this->center, state);
419N/A
419N/A this->rect->setPosition(r.min()[Geom::X], r.min()[Geom::Y], r.dimensions()[Geom::X], r.dimensions()[Geom::Y]);
419N/A
419N/A if (this->rx != 0.0) {
419N/A this->rect->setRx(true, this->rx);
419N/A }
781N/A
419N/A if (this->ry != 0.0) {
419N/A if (this->rx == 0.0)
419N/A this->rect->setRy(true, CLAMP(this->ry, 0, MIN(r.dimensions()[Geom::X], r.dimensions()[Geom::Y])/2));
419N/A else
419N/A this->rect->setRy(true, CLAMP(this->ry, 0, r.dimensions()[Geom::Y]));
419N/A }
419N/A
419N/A // status text
419N/A double rdimx = r.dimensions()[Geom::X];
419N/A double rdimy = r.dimensions()[Geom::Y];
419N/A
419N/A Inkscape::Util::Quantity rdimx_q = Inkscape::Util::Quantity(rdimx, "px");
419N/A Inkscape::Util::Quantity rdimy_q = Inkscape::Util::Quantity(rdimy, "px");
419N/A GString *xs = g_string_new(rdimx_q.string(desktop->namedview->display_units).c_str());
419N/A GString *ys = g_string_new(rdimy_q.string(desktop->namedview->display_units).c_str());
781N/A
419N/A if (state & GDK_CONTROL_MASK) {
419N/A int ratio_x, ratio_y;
419N/A bool is_golden_ratio = false;
781N/A
419N/A if (fabs (rdimx) > fabs (rdimy)) {
419N/A if (fabs(rdimx / rdimy - goldenratio) < 1e-6) {
419N/A is_golden_ratio = true;
781N/A }
419N/A
419N/A ratio_x = (int) rint (rdimx / rdimy);
419N/A ratio_y = 1;
419N/A } else {
419N/A if (fabs(rdimy / rdimx - goldenratio) < 1e-6) {
419N/A is_golden_ratio = true;
419N/A }
419N/A
419N/A ratio_x = 1;
419N/A ratio_y = (int) rint (rdimy / rdimx);
419N/A }
419N/A
419N/A if (!is_golden_ratio) {
419N/A this->message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to ratio %d:%d); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str, ratio_x, ratio_y);
781N/A } else {
419N/A if (ratio_y == 1) {
419N/A this->message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to golden ratio 1.618 : 1); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
419N/A } else {
419N/A this->message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s (constrained to golden ratio 1 : 1.618); with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
419N/A }
419N/A }
781N/A } else {
419N/A this->message_context->setF(Inkscape::IMMEDIATE_MESSAGE, _("<b>Rectangle</b>: %s &#215; %s; with <b>Ctrl</b> to make square or integer-ratio rectangle; with <b>Shift</b> to draw around the starting point"), xs->str, ys->str);
781N/A }
781N/A
419N/A g_string_free(xs, FALSE);
781N/A g_string_free(ys, FALSE);
419N/A}
419N/A
781N/Avoid RectTool::finishItem() {
781N/A this->message_context->clear();
419N/A
419N/A if (this->rect != NULL) {
419N/A if (this->rect->width.computed == 0 || this->rect->height.computed == 0) {
419N/A this->cancel(); // Don't allow the creating of zero sized rectangle, for example when the start and and point snap to the snap grid point
419N/A return;
419N/A }
419N/A
419N/A this->rect->updateRepr();
419N/A this->rect->doWriteTransform(this->rect->getRepr(), this->rect->transform, NULL, true);
419N/A
419N/A this->desktop->canvas->endForcedFullRedraws();
419N/A
419N/A this->desktop->getSelection()->set(this->rect);
781N/A
419N/A DocumentUndo::done(this->desktop->getDocument(), SP_VERB_CONTEXT_RECT, _("Create rectangle"));
419N/A
419N/A this->rect = NULL;
419N/A }
419N/A}
781N/A
419N/Avoid RectTool::cancel(){
419N/A this->desktop->getSelection()->clear();
419N/A sp_canvas_item_ungrab(SP_CANVAS_ITEM(this->desktop->acetate), 0);
781N/A
781N/A if (this->rect != NULL) {
419N/A this->rect->deleteObject();
419N/A this->rect = NULL;
419N/A }
419N/A
419N/A this->within_tolerance = false;
781N/A this->xp = 0;
419N/A this->yp = 0;
419N/A this->item_to_select = NULL;
419N/A
419N/A this->desktop->canvas->endForcedFullRedraws();
781N/A
781N/A DocumentUndo::cancel(this->desktop->getDocument());
781N/A}
781N/A
781N/A}
419N/A}
781N/A}
781N/A
419N/A/*
781N/A Local Variables:
781N/A mode:c++
781N/A c-file-style:"stroustrup"
781N/A c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
781N/A indent-tabs-mode:nil
419N/A fill-column:99
781N/A End:
419N/A*/
419N/A// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
419N/A