gradient-context.cpp revision c5424ab93868fb819e42ef2daa8f5031cf43d130
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński/*
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Gradient drawing and editing tool
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Authors:
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * bulia byak <buliabyak@users.sf.net>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Abhishek Sharma
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Copyright (C) 2007 Johan Engelen
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Copyright (C) 2005 Authors
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Released under GNU GPL, read the file 'COPYING' for more information
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#ifdef HAVE_CONFIG_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński# include "config.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#endif
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <gdk/gdkkeysyms.h>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "macros.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "document.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "selection.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "desktop.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "desktop-handles.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "message-context.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "message-stack.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "pixmaps/cursor-gradient.xpm"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "pixmaps/cursor-gradient-add.xpm"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "gradient-context.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "gradient-chemistry.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <glibmm/i18n.h>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "preferences.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "gradient-drag.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "gradient-chemistry.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "xml/repr.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "sp-item.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "display/sp-ctrlline.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "sp-linear-gradient.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "sp-radial-gradient.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "sp-stop.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "svg/css-ostringstream.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "svg/svg-color.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "snap.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "sp-namedview.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "rubberband.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "document-undo.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "verbs.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "selection-chemistry.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskiusing Inkscape::DocumentUndo;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskistatic void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint state, guint32 etime);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include "tool-factory.h"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskinamespace {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPEventContext* createGradientContext() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return new SPGradientContext();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński bool gradientContextRegistered = ToolFactory::instance().registerObject("/tools/gradient", createGradientContext);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskiconst std::string& SPGradientContext::getPrefsPath() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return SPGradientContext::prefsPath;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskiconst std::string SPGradientContext::prefsPath = "/tools/gradient";
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof KosińskiSPGradientContext::SPGradientContext() : SPEventContext() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->node_added = false;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->subselcon = 0;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->_message_context = 0;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->selcon = 0;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->cursor_addnode = false;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->cursor_shape = cursor_gradient_xpm;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->hot_x = 4;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->hot_y = 4;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->xp = 0;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->yp = 0;
827bc501341bcc5c344c0422f13422bc368151b4Krzysztof Kosiński this->tolerance = 6;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->within_tolerance = false;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->item_to_select = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof KosińskiSPGradientContext::~SPGradientContext() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->enableGrDrag(false);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (this->_message_context) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński delete this->_message_context;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->selcon->disconnect();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński delete this->selcon;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->subselcon->disconnect();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński delete this->subselcon;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskiconst gchar *gr_handle_descr [] = {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński N_("Linear gradient <b>start</b>"), //POINT_LG_BEGIN
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński N_("Linear gradient <b>end</b>"),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński N_("Linear gradient <b>mid stop</b>"),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński N_("Radial gradient <b>center</b>"),
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński N_("Radial gradient <b>radius</b>"),
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński N_("Radial gradient <b>radius</b>"),
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński N_("Radial gradient <b>focus</b>"), // POINT_RG_FOCUS
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński N_("Radial gradient <b>mid stop</b>"),
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński N_("Radial gradient <b>mid stop</b>")
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński};
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosińskivoid SPGradientContext::selection_changed(Inkscape::Selection*) {
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński SPGradientContext *rc = (SPGradientContext *) this;
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński GrDrag *drag = rc->_grdrag;
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(rc)->desktop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (selection == NULL) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński guint n_obj = g_slist_length((GSList *) selection->itemList());
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (!drag->isNonEmpty() || selection->isEmpty())
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński guint n_tot = drag->numDraggers();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński guint n_sel = drag->numSelected();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //The use of ngettext in the following code is intentional even if the English singular form would never be used
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (n_sel == 1) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (drag->singleSelectedDraggerNumDraggables() == 1) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gchar * message = g_strconcat(
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: %s will be substituted with the point name (see previous messages); This is part of a compound message
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _("%s selected"),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: Mind the space in front. This is part of a compound message
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński message,_(gr_handle_descr[drag->singleSelectedDraggerSingleDraggableType()]), n_tot, n_obj);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gchar * message = g_strconcat(
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: This is a part of a compound message (out of two more indicating: grandint handle count & object count)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext("One handle merging %d stop (drag with <b>Shift</b> to separate) selected",
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński "One handle merging %d stops (drag with <b>Shift</b> to separate) selected",drag->singleSelectedDraggerNumDraggables()),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext(" out of %d gradient handle"," out of %d gradient handles",n_tot),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message,drag->singleSelectedDraggerNumDraggables(), n_tot, n_obj);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else if (n_sel > 1) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: The plural refers to number of selected gradient handles. This is part of a compound message (part two indicates selected object count)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gchar * message = g_strconcat(ngettext("<b>%d</b> gradient handle selected out of %d","<b>%d</b> gradient handles selected out of %d",n_sel),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: Mind the space in front. (Refers to gradient handles selected). This is part of a compound message
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext(" on %d selected object"," on %d selected objects",n_obj),NULL);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,message, n_sel, n_tot, n_obj);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else if (n_sel == 0) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński rc->_message_context->setF(Inkscape::NORMAL_MESSAGE,
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //TRANSLATORS: The plural refers to number of selected objects
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ngettext("<b>No</b> gradient handles selected out of %d on %d selected object",
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński "<b>No</b> gradient handles selected out of %d on %d selected objects",n_obj), n_tot, n_obj);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskivoid SPGradientContext::setup() {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPEventContext::setup();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Inkscape::Preferences *prefs = Inkscape::Preferences::get();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (prefs->getBool("/tools/gradient/selcue", true)) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->enableSelectionCue();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->enableGrDrag();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Inkscape::Selection *selection = sp_desktop_selection(this->desktop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->_message_context = new Inkscape::MessageContext(sp_desktop_message_stack(this->desktop));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->selcon = new sigc::connection(selection->connectChanged(
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński sigc::mem_fun(this, &SPGradientContext::selection_changed)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->subselcon = new sigc::connection(this->desktop->connectToolSubselectionChanged(
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński sigc::hide(sigc::bind(
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński sigc::mem_fun(this, &SPGradientContext::selection_changed),
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński (Inkscape::Selection*)NULL
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ))
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński ));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński this->selection_changed(selection);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskivoid
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskisp_gradient_context_select_next (SPEventContext *event_context)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDrag *drag = event_context->_grdrag;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński g_assert (drag);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDragger *d = drag->select_next();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński event_context->desktop->scroll_to_point(d->point, 1.0);
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński}
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosińskivoid
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosińskisp_gradient_context_select_prev (SPEventContext *event_context)
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński{
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński GrDrag *drag = event_context->_grdrag;
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński g_assert (drag);
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński GrDragger *d = drag->select_prev();
ef494f75ca77f36881ad59e94263a07c146c3649Krzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński event_context->desktop->scroll_to_point(d->point, 1.0);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskistatic bool
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskisp_gradient_context_is_over_line (SPGradientContext *rc, SPItem *item, Geom::Point event_p)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński //Translate mouse point into proper coord system
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński rc->mousepoint_doc = desktop->w2d(event_p);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPCtrlLine* line = SP_CTRLLINE(item);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Geom::LineSegment ls(line->s, line->e);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Geom::Point nearest = ls.pointAt(ls.nearestPoint(rc->mousepoint_doc));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński double dist_screen = Geom::L2 (rc->mousepoint_doc - nearest) * desktop->current_zoom();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński double tolerance = (double) SP_EVENT_CONTEXT(rc)->tolerance;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński bool close = (dist_screen < tolerance);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return close;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskistatic std::vector<Geom::Point>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskisp_gradient_context_get_stop_intervals (GrDrag *drag, GSList **these_stops, GSList **next_stops)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński std::vector<Geom::Point> coords;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // for all selected draggers
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński for (GList *i = drag->selected; i != NULL; i = i->next) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDragger *dragger = (GrDragger *) i->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // remember the coord of the dragger to reselect it later
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński coords.push_back(dragger->point);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // for all draggables of dragger
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDraggable *d = (GrDraggable *) j->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // find the gradient
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPGradient *gradient = getGradient(d->item, d->fill_or_stroke);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // these draggable types cannot have a next draggabe to insert a stop between them
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (d->point_type == POINT_LG_END ||
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński d->point_type == POINT_RG_FOCUS ||
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński d->point_type == POINT_RG_R1 ||
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński d->point_type == POINT_RG_R2) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński continue;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // from draggables to stops
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *next_stop = this_stop->getNextStop();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *last_stop = sp_last_stop (vector);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Inkscape::PaintTarget fs = d->fill_or_stroke;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPItem *item = d->item;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gint type = d->point_type;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gint p_i = d->point_i;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // if there's a next stop,
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (next_stop) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDragger *dnext = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // find its dragger
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // (complex because it may have different types, and because in radial,
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // more than one dragger may correspond to a stop, so we must distinguish)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (type == POINT_LG_BEGIN || type == POINT_LG_MID) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (next_stop == last_stop) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_LG_END, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_LG_MID, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else { // radial
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (type == POINT_RG_CENTER || type == POINT_RG_MID1) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (next_stop == last_stop) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_RG_R1, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_RG_MID1, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if ((type == POINT_RG_MID2) ||
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński (type == POINT_RG_CENTER && dnext && !dnext->isSelected())) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (next_stop == last_stop) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_RG_R2, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński } else {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński dnext = drag->getDraggerFor(item, POINT_RG_MID2, p_i+1, fs);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // if both adjacent draggers selected,
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (!g_slist_find(*these_stops, this_stop) && dnext && dnext->isSelected()) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // remember the coords of the future dragger to select it
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński coords.push_back(0.5*(dragger->point + dnext->point));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // do not insert a stop now, it will confuse the loop;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // just remember the stops
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *these_stops = g_slist_prepend (*these_stops, this_stop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *next_stops = g_slist_prepend (*next_stops, next_stop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return coords;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskivoid
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskisp_gradient_context_add_stops_between_selected_stops (SPGradientContext *rc)
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPDocument *doc = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDrag *drag = rc->_grdrag;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GSList *these_stops = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GSList *next_stops = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (g_slist_length(these_stops) == 0 && drag->numSelected() == 1) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // if a single stop is selected, add between that stop and the next one
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDragger *dragger = (GrDragger *) drag->selected->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński for (GSList const* j = dragger->draggables; j != NULL; j = j->next) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GrDraggable *d = (GrDraggable *) j->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (d->point_type == POINT_RG_FOCUS) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /*
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * There are 2 draggables at the center (start) of a radial gradient
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * To avoid creating 2 seperate stops, ignore this draggable point type
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński continue;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPGradient *gradient = getGradient(d->item, d->fill_or_stroke);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPGradient *vector = sp_gradient_get_forked_vector_if_necessary (gradient, false);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *this_stop = sp_get_stop_i (vector, d->point_i);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *next_stop = this_stop->getNextStop();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (this_stop && next_stop) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński these_stops = g_slist_prepend (these_stops, this_stop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński next_stops = g_slist_prepend (next_stops, next_stop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // now actually create the new stops
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GSList *i = these_stops;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GSList *j = next_stops;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński GSList *new_stops = NULL;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński for (; i != NULL && j != NULL; i = i->next, j = j->next) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *this_stop = (SPStop *) i->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *next_stop = (SPStop *) j->data;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński gfloat offset = 0.5*(this_stop->offset + next_stop->offset);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPObject *parent = this_stop->parent;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (SP_IS_GRADIENT (parent)) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński doc = parent->document;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SPStop *new_stop = sp_vector_add_stop (SP_GRADIENT (parent), this_stop, next_stop, offset);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński new_stops = g_slist_prepend (new_stops, new_stop);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński SP_GRADIENT(parent)->ensureVector();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if (g_slist_length(these_stops) > 0 && doc) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński DocumentUndo::done(doc, SP_VERB_CONTEXT_GRADIENT, _("Add gradient stop"));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński drag->updateDraggers();
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // so that it does not automatically update draggers in idle loop, as this would deselect
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński drag->local_change = true;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński // select the newly created stops
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński for (GSList *s = new_stops; s != NULL; s = s->next) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński drag->selectByStop((SPStop *)s->data);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński g_slist_free (these_stops);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński g_slist_free (next_stops);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński g_slist_free (new_stops);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskistatic double sqr(double x) {return x*x;}
static void
sp_gradient_simplify(SPGradientContext *rc, double tolerance)
{
SPDocument *doc = NULL;
GrDrag *drag = rc->_grdrag;
GSList *these_stops = NULL;
GSList *next_stops = NULL;
std::vector<Geom::Point> coords = sp_gradient_context_get_stop_intervals (drag, &these_stops, &next_stops);
GSList *todel = NULL;
GSList *i = these_stops;
GSList *j = next_stops;
for (; i != NULL && j != NULL; i = i->next, j = j->next) {
SPStop *stop0 = (SPStop *) i->data;
SPStop *stop1 = (SPStop *) j->data;
gint i1 = g_slist_index(these_stops, stop1);
if (i1 != -1) {
GSList *next_next = g_slist_nth (next_stops, i1);
if (next_next) {
SPStop *stop2 = (SPStop *) next_next->data;
if (g_slist_find(todel, stop0) || g_slist_find(todel, stop2))
continue;
guint32 const c0 = sp_stop_get_rgba32(stop0);
guint32 const c2 = sp_stop_get_rgba32(stop2);
guint32 const c1r = sp_stop_get_rgba32(stop1);
guint32 c1 = average_color (c0, c2,
(stop1->offset - stop0->offset) / (stop2->offset - stop0->offset));
double diff =
sqr(SP_RGBA32_R_F(c1) - SP_RGBA32_R_F(c1r)) +
sqr(SP_RGBA32_G_F(c1) - SP_RGBA32_G_F(c1r)) +
sqr(SP_RGBA32_B_F(c1) - SP_RGBA32_B_F(c1r)) +
sqr(SP_RGBA32_A_F(c1) - SP_RGBA32_A_F(c1r));
if (diff < tolerance)
todel = g_slist_prepend (todel, stop1);
}
}
}
for (i = todel; i != NULL; i = i->next) {
SPStop *stop = (SPStop*) i->data;
doc = stop->document;
Inkscape::XML::Node * parent = stop->getRepr()->parent();
parent->removeChild( stop->getRepr() );
}
if (g_slist_length(todel) > 0) {
DocumentUndo::done(doc, SP_VERB_CONTEXT_GRADIENT, _("Simplify gradient"));
drag->local_change = true;
drag->updateDraggers();
drag->selectByCoords(coords);
}
g_slist_free (todel);
g_slist_free (these_stops);
g_slist_free (next_stops);
}
static void
sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, Geom::Point mouse_p, guint32 /*etime*/)
{
// item is the selected item. mouse_p the location in doc coordinates of where to add the stop
SPEventContext *ec = SP_EVENT_CONTEXT(rc);
SPDesktop *desktop = SP_EVENT_CONTEXT (rc)->desktop;
double tolerance = (double) ec->tolerance;
SPStop *newstop = ec->get_drag()->addStopNearPoint (item, mouse_p, tolerance/desktop->current_zoom());
DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
_("Add gradient stop"));
ec->get_drag()->updateDraggers();
ec->get_drag()->local_change = true;
ec->get_drag()->selectByStop(newstop);
}
bool SPGradientContext::root_handler(GdkEvent* event) {
static bool dragging;
Inkscape::Selection *selection = sp_desktop_selection (desktop);
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
this->tolerance = prefs->getIntLimited("/options/dragtolerance/value", 0, 0, 100);
double const nudge = prefs->getDoubleLimited("/options/nudgedistance/value", 2, 0, 1000, "px"); // in px
GrDrag *drag = this->_grdrag;
g_assert (drag);
gint ret = FALSE;
switch (event->type) {
case GDK_2BUTTON_PRESS:
if ( event->button.button == 1 ) {
bool over_line = false;
SPCtrlLine *line = NULL;
if (drag->lines) {
for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
line = (SPCtrlLine*) l->data;
over_line |= sp_gradient_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
}
}
if (over_line) {
// we take the first item in selection, because with doubleclick, the first click
// always resets selection to the single object under cursor
sp_gradient_context_add_stop_near_point(this, SP_ITEM(selection->itemList()->data), this->mousepoint_doc, event->button.time);
} else {
for (GSList const* i = selection->itemList(); i != NULL; i = i->next) {
SPItem *item = SP_ITEM(i->data);
SPGradientType new_type = (SPGradientType) prefs->getInt("/tools/gradient/newgradient", SP_GRADIENT_TYPE_LINEAR);
Inkscape::PaintTarget fsmode = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, item, fsmode);
SPGradient *priv = sp_item_set_gradient(item, vector, new_type, fsmode);
sp_gradient_reset_to_userspace(priv, item);
}
DocumentUndo::done(sp_desktop_document (desktop), SP_VERB_CONTEXT_GRADIENT,
_("Create default gradient"));
}
ret = TRUE;
}
break;
case GDK_BUTTON_PRESS:
if ( event->button.button == 1 && !this->space_panning ) {
Geom::Point button_w(event->button.x, event->button.y);
// save drag origin
this->xp = (gint) button_w[Geom::X];
this->yp = (gint) button_w[Geom::Y];
this->within_tolerance = true;
dragging = true;
Geom::Point button_dt = desktop->w2d(button_w);
if (event->button.state & GDK_SHIFT_MASK) {
Inkscape::Rubberband::get(desktop)->start(desktop, button_dt);
} else {
// remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
// enable Ctrl+doubleclick of exactly the selected item(s)
if (!(event->button.state & GDK_CONTROL_MASK)) {
this->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
}
if (!selection->isEmpty()) {
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
m.freeSnapReturnByRef(button_dt, Inkscape::SNAPSOURCE_NODE_HANDLE);
m.unSetup();
}
this->origin = button_dt;
}
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 const motion_dt = this->desktop->w2d(motion_w);
if (Inkscape::Rubberband::get(desktop)->is_started()) {
Inkscape::Rubberband::get(desktop)->move(motion_dt);
this->defaultMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Draw around</b> handles to select them"));
} else {
sp_gradient_drag(*this, motion_dt, event->motion.state, event->motion.time);
}
gobble_motion_events(GDK_BUTTON1_MASK);
ret = TRUE;
} else {
if (!drag->mouseOver() && !selection->isEmpty()) {
SnapManager &m = desktop->namedview->snap_manager;
m.setup(desktop);
Geom::Point const motion_w(event->motion.x, event->motion.y);
Geom::Point const motion_dt = this->desktop->w2d(motion_w);
m.preSnap(Inkscape::SnapCandidatePoint(motion_dt, Inkscape::SNAPSOURCE_OTHER_HANDLE));
m.unSetup();
}
bool over_line = false;
if (drag->lines) {
for (GSList *l = drag->lines; l != NULL; l = l->next) {
over_line |= sp_gradient_context_is_over_line (this, (SPItem*) l->data, Geom::Point(event->motion.x, event->motion.y));
}
}
if (this->cursor_addnode && !over_line) {
this->cursor_shape = cursor_gradient_xpm;
this->sp_event_context_update_cursor();
this->cursor_addnode = false;
} else if (!this->cursor_addnode && over_line) {
this->cursor_shape = cursor_gradient_add_xpm;
this->sp_event_context_update_cursor();
this->cursor_addnode = true;
}
}
break;
case GDK_BUTTON_RELEASE:
this->xp = this->yp = 0;
if ( event->button.button == 1 && !this->space_panning ) {
bool over_line = false;
SPCtrlLine *line = NULL;
if (drag->lines) {
for (GSList *l = drag->lines; (l != NULL) && (!over_line); l = l->next) {
line = (SPCtrlLine*) l->data;
over_line = sp_gradient_context_is_over_line (this, (SPItem*) line, Geom::Point(event->motion.x, event->motion.y));
if (over_line)
break;
}
}
if ( (event->button.state & GDK_CONTROL_MASK) && (event->button.state & GDK_MOD1_MASK ) ) {
if (over_line && line) {
sp_gradient_context_add_stop_near_point(this, line->item, this->mousepoint_doc, 0);
ret = TRUE;
}
} else {
dragging = false;
// unless clicked with Ctrl (to enable Ctrl+doubleclick).
if (event->button.state & GDK_CONTROL_MASK) {
ret = TRUE;
break;
}
if (!this->within_tolerance) {
// we've been dragging, either do nothing (grdrag handles that),
// or rubberband-select if we have rubberband
Inkscape::Rubberband *r = Inkscape::Rubberband::get(desktop);
if (r->is_started() && !this->within_tolerance) {
// this was a rubberband drag
if (r->getMode() == RUBBERBAND_MODE_RECT) {
Geom::OptRect const b = r->getRectangle();
drag->selectRect(*b);
}
}
} else if (this->item_to_select) {
if (over_line && line) {
// Clicked on an existing gradient line, dont change selection. This stops
// possible change in selection during a double click with overlapping objects
} else {
// no dragging, select clicked item if any
if (event->button.state & GDK_SHIFT_MASK) {
selection->toggle(this->item_to_select);
} else {
drag->deselectAll();
selection->set(this->item_to_select);
}
}
} else {
// click in an empty space; do the same as Esc
if (drag->selected) {
drag->deselectAll();
} else {
selection->clear();
}
}
this->item_to_select = NULL;
ret = TRUE;
}
Inkscape::Rubberband::get(desktop)->stop();
}
break;
case GDK_KEY_PRESS:
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 (at least on my machine)
case GDK_KEY_Meta_R:
sp_event_show_modifier_tip (this->defaultMessageContext(), event,
_("<b>Ctrl</b>: snap gradient angle"),
_("<b>Shift</b>: draw gradient around the starting point"),
NULL);
break;
case GDK_KEY_x:
case GDK_KEY_X:
if (MOD__ALT_ONLY(event)) {
desktop->setToolboxFocusTo ("altx-grad");
ret = TRUE;
}
break;
case GDK_KEY_A:
case GDK_KEY_a:
if (MOD__CTRL_ONLY(event) && drag->isNonEmpty()) {
drag->selectAll();
ret = TRUE;
}
break;
case GDK_KEY_L:
case GDK_KEY_l:
if (MOD__CTRL_ONLY(event) && drag->isNonEmpty() && drag->hasSelection()) {
sp_gradient_simplify(this, 1e-4);
ret = TRUE;
}
break;
case GDK_KEY_Escape:
if (drag->selected) {
drag->deselectAll();
} else {
Inkscape::SelectionHelper::selectNone(desktop);
}
ret = TRUE;
//TODO: make dragging escapable by Esc
break;
case GDK_KEY_Left: // move handle left
case GDK_KEY_KP_Left:
case GDK_KEY_KP_4:
if (!MOD__CTRL(event)) { // not ctrl
gint mul = 1 + gobble_key_events(
get_group0_keyval(&event->key), 0); // with any mask
if (MOD__ALT(event)) { // alt
if (MOD__SHIFT(event)) {
drag->selected_move_screen(mul*-10, 0); // shift
} else {
drag->selected_move_screen(mul*-1, 0); // no shift
}
} else { // no alt
if (MOD__SHIFT(event)) {
drag->selected_move(mul*-10*nudge, 0); // shift
} else {
drag->selected_move(mul*-nudge, 0); // no shift
}
}
ret = TRUE;
}
break;
case GDK_KEY_Up: // move handle up
case GDK_KEY_KP_Up:
case GDK_KEY_KP_8:
if (!MOD__CTRL(event)) { // not ctrl
gint mul = 1 + gobble_key_events(
get_group0_keyval(&event->key), 0); // with any mask
if (MOD__ALT(event)) { // alt
if (MOD__SHIFT(event)) {
drag->selected_move_screen(0, mul*10); // shift
} else {
drag->selected_move_screen(0, mul*1); // no shift
}
} else { // no alt
if (MOD__SHIFT(event)) {
drag->selected_move(0, mul*10*nudge); // shift
} else {
drag->selected_move(0, mul*nudge); // no shift
}
}
ret = TRUE;
}
break;
case GDK_KEY_Right: // move handle right
case GDK_KEY_KP_Right:
case GDK_KEY_KP_6:
if (!MOD__CTRL(event)) { // not ctrl
gint mul = 1 + gobble_key_events(
get_group0_keyval(&event->key), 0); // with any mask
if (MOD__ALT(event)) { // alt
if (MOD__SHIFT(event)) {
drag->selected_move_screen(mul*10, 0); // shift
} else {
drag->selected_move_screen(mul*1, 0); // no shift
}
} else { // no alt
if (MOD__SHIFT(event)) {
drag->selected_move(mul*10*nudge, 0); // shift
} else {
drag->selected_move(mul*nudge, 0); // no shift
}
}
ret = TRUE;
}
break;
case GDK_KEY_Down: // move handle down
case GDK_KEY_KP_Down:
case GDK_KEY_KP_2:
if (!MOD__CTRL(event)) { // not ctrl
gint mul = 1 + gobble_key_events(
get_group0_keyval(&event->key), 0); // with any mask
if (MOD__ALT(event)) { // alt
if (MOD__SHIFT(event)) {
drag->selected_move_screen(0, mul*-10); // shift
} else {
drag->selected_move_screen(0, mul*-1); // no shift
}
} else { // no alt
if (MOD__SHIFT(event)) {
drag->selected_move(0, mul*-10*nudge); // shift
} else {
drag->selected_move(0, mul*-nudge); // no shift
}
}
ret = TRUE;
}
break;
case GDK_KEY_r:
case GDK_KEY_R:
if (MOD__SHIFT_ONLY(event)) {
sp_gradient_reverse_selected_gradients(desktop);
ret = TRUE;
}
break;
case GDK_KEY_Insert:
case GDK_KEY_KP_Insert:
// with any modifiers:
sp_gradient_context_add_stops_between_selected_stops (this);
ret = TRUE;
break;
case GDK_KEY_Delete:
case GDK_KEY_KP_Delete:
case GDK_KEY_BackSpace:
ret = this->deleteSelectedDrag(MOD__CTRL_ONLY(event));
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;
}
static void sp_gradient_drag(SPGradientContext &rc, Geom::Point const pt, guint /*state*/, guint32 etime)
{
SPDesktop *desktop = SP_EVENT_CONTEXT(&rc)->desktop;
Inkscape::Selection *selection = sp_desktop_selection(desktop);
SPDocument *document = sp_desktop_document(desktop);
SPEventContext *ec = SP_EVENT_CONTEXT(&rc);
if (!selection->isEmpty()) {
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
int type = prefs->getInt("/tools/gradient/newgradient", 1);
Inkscape::PaintTarget fill_or_stroke = (prefs->getInt("/tools/gradient/newfillorstroke", 1) != 0) ? Inkscape::FOR_FILL : Inkscape::FOR_STROKE;
SPGradient *vector;
if (ec->item_to_select) {
// pick color from the object where drag started
vector = sp_gradient_vector_for_object(document, desktop, ec->item_to_select, fill_or_stroke);
} else {
// Starting from empty space:
// Sort items so that the topmost comes last
GSList *items = g_slist_copy ((GSList *) selection->itemList());
items = g_slist_sort(items, (GCompareFunc) sp_item_repr_compare_position);
// take topmost
vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(g_slist_last(items)->data), fill_or_stroke);
g_slist_free (items);
}
// HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
SPCSSAttr *css = sp_repr_css_attr_new();
sp_repr_css_set_property(css, "fill-opacity", "1.0");
for (GSList const *i = selection->itemList(); i != NULL; i = i->next) {
//FIXME: see above
sp_repr_css_change_recursive(SP_OBJECT(i->data)->getRepr(), css, "style");
sp_item_set_gradient(SP_ITEM(i->data), vector, (SPGradientType) type, fill_or_stroke);
if (type == SP_GRADIENT_TYPE_LINEAR) {
sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_BEGIN, 0, rc.origin, fill_or_stroke, true, false);
sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_LG_END, 0, pt, fill_or_stroke, true, false);
} else if (type == SP_GRADIENT_TYPE_RADIAL) {
sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_CENTER, 0, rc.origin, fill_or_stroke, true, false);
sp_item_gradient_set_coords (SP_ITEM(i->data), POINT_RG_R1, 0, pt, fill_or_stroke, true, false);
}
SP_OBJECT(i->data)->requestModified(SP_OBJECT_MODIFIED_FLAG);
}
if (ec->_grdrag) {
ec->_grdrag->updateDraggers();
// prevent regenerating draggers by selection modified signal, which sometimes
// comes too late and thus destroys the knot which we will now grab:
ec->_grdrag->local_change = true;
// give the grab out-of-bounds values of xp/yp because we're already dragging
// and therefore are already out of tolerance
ec->_grdrag->grabKnot (SP_ITEM(selection->itemList()->data),
type == SP_GRADIENT_TYPE_LINEAR? POINT_LG_END : POINT_RG_R1,
-1, // ignore number (though it is always 1)
fill_or_stroke, 99999, 99999, etime);
}
// We did an undoable action, but SPDocumentUndo::done will be called by the knot when released
// status text; we do not track coords because this branch is run once, not all the time
// during drag
int n_objects = g_slist_length((GSList *) selection->itemList());
rc._message_context->setF(Inkscape::NORMAL_MESSAGE,
ngettext("<b>Gradient</b> for %d object; with <b>Ctrl</b> to snap angle",
"<b>Gradient</b> for %d objects; with <b>Ctrl</b> to snap angle", n_objects),
n_objects);
} else {
sp_desktop_message_stack(desktop)->flash(Inkscape::WARNING_MESSAGE, _("Select <b>objects</b> on which to create gradient."));
}
}
/*
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 :