gradient-context.cpp revision d46dc7135e50f1af9752c95cf02bf5f25723be17
#define __SP_GRADIENT_CONTEXT_C__
/*
* Gradient drawing and editing tool
*
* Authors:
* bulia byak <buliabyak@users.sf.net>
* Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
*
* Copyright (C) 2007 Johan Engelen
* Copyright (C) 2005 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <gdk/gdkkeysyms.h>
#include "macros.h"
#include "document.h"
#include "selection.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "message-context.h"
#include "message-stack.h"
#include "pixmaps/cursor-gradient.xpm"
#include "pixmaps/cursor-gradient-add.xpm"
#include "pixmaps/cursor-gradient-delete.xpm"
#include "gradient-context.h"
#include "gradient-chemistry.h"
#include "prefs-utils.h"
#include "gradient-drag.h"
#include "gradient-chemistry.h"
#include "sp-item.h"
#include "display/sp-ctrlline.h"
#include "sp-linear-gradient.h"
#include "sp-radial-gradient.h"
#include "sp-stop.h"
#include "svg/css-ostringstream.h"
#include "svg/svg-color.h"
#include "snap.h"
#include "sp-namedview.h"
static void sp_gradient_drag(SPGradientContext &rc, NR::Point const pt, guint state, guint32 etime);
static SPEventContextClass *parent_class;
{
if (!type) {
sizeof(SPGradientContextClass),
sizeof(SPGradientContext),
4,
NULL, /* value_table */
};
}
return type;
}
{
}
{
gr_context->cursor_addnode = false;
event_context->xp = 0;
event_context->yp = 0;
event_context->within_tolerance = false;
}
{
ec->enableGrDrag(false);
if (rc->_message_context) {
delete rc->_message_context;
}
}
{
}
ec->enableSelectionCue();
}
ec->enableGrDrag();
}
void
{
drag->select_next();
}
void
{
drag->select_prev();
}
// FIXME: make global function in libnr or somewhere.
{
if (r < 0.0) return begin;
}
static bool
{
//Translate mouse point into proper coord system
return close;
}
// Fixme : must be able to put this in a general file.
static guint32
{
return SP_RGBA32_U_COMPOSE (r, g, b, a);
}
static double
{
if (r < 0.0) return 0.0;
if (r > length) return 1.0;
return (r / length);
}
static void
sp_gradient_context_add_stop_near_point (SPGradientContext *rc, SPItem *item, NR::Point mouse_p, guint32 etime)
{
// item is the selected item. mouse_p the location in doc coordinates of where to add the stop
bool fill_or_stroke = true;
bool r1_knot = false;
bool addknot = false;
do {
if (SP_IS_LINEARGRADIENT(gradient)) {
if ( dist_screen < tolerance ) {
// add the knot
addknot = true;
break; // break out of the while loop: add only one knot
}
} else if (SP_IS_RADIALGRADIENT(gradient)) {
if ( dist_screen < tolerance ) {
addknot = true;
r1_knot = true;
break; // break out of the while loop: add only one knot
}
if ( dist_screen < tolerance ) {
addknot = true;
r1_knot = false;
break; // break out of the while loop: add only one knot
}
}
} while (!fill_or_stroke && !addknot) ;
if (addknot) {
}
if (!next_stop) {
// logical error: the endstop should have offset 1 and should always be more than this offset here
return;
}
guint32 cnew = average_color (c1, c2, (offset - prev_stop->offset) / (next_stop->offset - prev_stop->offset));
gchar c[64];
_("Add gradient stop"));
int i = 0;
for ( SPObject *ochild = sp_object_first_child (SP_OBJECT(vector)) ; ochild != NULL ; ochild = SP_OBJECT_NEXT(ochild) ) {
if (SP_IS_STOP (ochild)) {
break;
} else {
i++;
}
}
}
if (SP_IS_LINEARGRADIENT(gradient)) {
} else if (SP_IS_RADIALGRADIENT(gradient)) {
}
GrDragger *dragger = SP_EVENT_CONTEXT(rc)->_grdrag->getDraggerFor (item, pointtype, i, fill_or_stroke);
} else {
i,
}
}
}
}
static gint
{
static bool dragging;
event_context->tolerance = prefs_get_int_attribute_limited("options.dragtolerance", "value", 0, 0, 100);
double const nudge = prefs_get_double_attribute_limited("options.nudgedistance", "value", 2, 0, 1000); // in px
case GDK_2BUTTON_PRESS:
bool over_line = false;
over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
}
}
if (over_line) {
sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, event->button.time);
} else {
SPGradientType new_type = (SPGradientType) prefs_get_int_attribute ("tools.gradient", "newgradient", SP_GRADIENT_TYPE_LINEAR);
SPGradient *vector = sp_gradient_vector_for_object(sp_desktop_document(desktop), desktop, SP_OBJECT (item), new_fill);
}
_("Create default gradient"));
}
}
break;
case GDK_BUTTON_PRESS:
// save drag origin
event_context->within_tolerance = true;
// remember clicked item, disregarding groups, honoring Alt; do nothing with Crtl to
// enable Ctrl+doubleclick of exactly the selected item(s)
event_context->item_to_select = sp_event_context_find_item (desktop, button_w, event->button.state & GDK_MOD1_MASK, TRUE);
dragging = true;
/* Position center */
/* Snap center to nearest magnetic point */
rc->origin = m.freeSnap(Inkscape::Snapper::BBOX_POINT | Inkscape::Snapper::SNAP_POINT, button_dt, NULL).getPoint();
}
break;
case GDK_MOTION_NOTIFY:
if ( dragging
{
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)
event_context->within_tolerance = false;
} else {
bool over_line = false;
over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) l->data, NR::Point(event->motion.x, event->motion.y));
}
}
rc->cursor_addnode = false;
rc->cursor_addnode = true;
}
}
break;
case GDK_BUTTON_RELEASE:
bool over_line = false;
over_line |= sp_gradient_context_is_over_line (rc, (SPItem*) line, NR::Point(event->motion.x, event->motion.y));
}
}
if (over_line) {
sp_gradient_context_add_stop_near_point(rc, SP_ITEM(selection->itemList()->data), rc->mousepoint_doc, 0);
}
} else {
dragging = false;
// unless clicked with Ctrl (to enable Ctrl+doubleclick). (don't what this is for (johan))
break;
}
if (!event_context->within_tolerance) {
// we've been dragging, do nothing (grdrag handles that)
} else if (event_context->item_to_select) {
// no dragging, select clicked item if any
} else {
}
} else {
// click in an empty space; do the same as Esc
drag->deselectAll();
} else {
}
}
}
}
break;
case GDK_KEY_PRESS:
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 (at least on my machine)
case GDK_Meta_R:
_("<b>Ctrl</b>: snap gradient angle"),
_("<b>Shift</b>: draw gradient around the starting point"),
NULL);
break;
case GDK_x:
case GDK_X:
if (MOD__ALT_ONLY) {
}
break;
case GDK_Escape:
drag->deselectAll();
} else {
}
//TODO: make dragging escapable by Esc
break;
case GDK_Left: // move handle left
case GDK_KP_Left:
case GDK_KP_4:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_Up: // move handle up
case GDK_KP_Up:
case GDK_KP_8:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_Right: // move handle right
case GDK_KP_Right:
case GDK_KP_6:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_Down: // move handle down
case GDK_KP_Down:
case GDK_KP_2:
if (!MOD__CTRL) { // not ctrl
if (MOD__ALT) { // alt
}
else { // no alt
}
}
break;
case GDK_r:
case GDK_R:
if (MOD__SHIFT_ONLY) {
// First try selected dragger
} else { // If no drag or no dragger selected, act on selection (both fill and stroke gradients)
}
}
// we did an undoable action
_("Invert gradient"));
}
break;
/*
case GDK_Insert:
case GDK_KP_Insert:
// with any modifiers:
// insert mid-stops between selected stops in gradient, or between all stops if none or only one selected
ret = TRUE;
break;
*/
case GDK_Delete:
case GDK_KP_Delete:
case GDK_BackSpace:
}
break;
default:
break;
}
break;
case GDK_KEY_RELEASE:
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:
break;
default:
break;
}
break;
default:
break;
}
if (!ret) {
}
}
return ret;
}
{
if (ec->item_to_select) {
} else {
vector = sp_gradient_vector_for_object(document, desktop, SP_ITEM(selection->itemList()->data), fill_or_stroke);
}
// HACK: reset fill-opacity - that 0.75 is annoying; BUT remove this when we have an opacity slider for all tabs
//FIXME: see above
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);
} 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);
}
}
// prevent regenerating draggers by selection modified signal, which sometimes
// comes too late and thus destroys the knot which we will now grab:
// and therefore are already out of tolerance
0, //point_i
}
// We did an undoable action, but sp_document_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
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),
} 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:encoding=utf-8:textwidth=99 :