Code

Updated cases for attributes added in <color-profile> support
[inkscape.git] / src / seltrans.cpp
index ca65b5d761a7cd28e223686f71f8cd71456fd7b5..a301c8158324a537d853a0b10958caeddd27e4d4 100644 (file)
@@ -50,6 +50,7 @@ static void sp_remove_handles(SPKnot *knot[], gint num);
 
 static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data);
 static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data);
+static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data);
 static void sp_sel_trans_handle_new_event(SPKnot *knot, NR::Point *position, guint32 state, gpointer data);
 static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *p, guint state, gboolean *data);
 
@@ -100,7 +101,7 @@ Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
 
     _updateVolatileState();
 
-    _center = _box.midpoint();
+    _center_is_set = false; // reread _center from items, or set to bbox midpoint
 
     _updateHandles();
 
@@ -118,7 +119,7 @@ Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
                                "stroke_color", 0x000000a0,
                                "pixbuf", handles[12],
                                NULL);
-    
+
     _grip = sp_canvas_item_new(SP_DT_CONTROLS(desktop),
                                SP_TYPE_CTRL,
                                "anchor", GTK_ANCHOR_CENTER,
@@ -131,7 +132,7 @@ Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
                                "stroke_color", 0xffffffff,
                                "pixbuf", handles[12],
                                NULL);
-    
+
     sp_canvas_item_hide(_grip);
     sp_canvas_item_hide(_norm);
 
@@ -189,6 +190,7 @@ Inkscape::SelTrans::~SelTrans()
     }
 
     _items.clear();
+    _items_centers.clear();
 }
 
 void Inkscape::SelTrans::resetState()
@@ -204,12 +206,24 @@ void Inkscape::SelTrans::increaseState()
         _state = STATE_SCALE;
     }
 
+    _center_is_set = true; // no need to reread center
+
     _updateHandles();
 }
 
 void Inkscape::SelTrans::setCenter(NR::Point const &p)
 {
     _center = p;
+    _center_is_set = true;
+
+    // Write the new center position into all selected items
+    for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
+        SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
+        it->setCenter(p);
+        SP_OBJECT(it)->updateRepr();
+    }
+    sp_document_maybe_done (SP_DT_DOCUMENT(_desktop), "center::move");
+
     _updateHandles();
 }
 
@@ -232,6 +246,7 @@ void Inkscape::SelTrans::grab(NR::Point const &p, gdouble x, gdouble y, bool sho
     for (GSList const *l = selection->itemList(); l; l = l->next) {
         SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
         _items.push_back(std::pair<SPItem *, NR::Matrix>(it, sp_item_i2d_affine(it)));
+        _items_centers.push_back(std::pair<SPItem *, NR::Point>(it, it->getCenter())); // for content-dragging, we need to remember original centers
     }
 
     _current.set_identity();
@@ -301,47 +316,39 @@ void Inkscape::SelTrans::transform(NR::Matrix const &rel_affine, NR::Point const
     _updateHandles();
 }
 
-void Inkscape::SelTrans::_centreTrans(Inkscape::XML::Node *current) const
-{
-    for ( Inkscape::XML::Node *child = sp_repr_children(current) ; child ; child = sp_repr_next(child) ) {
-        _centreTrans(child);
-    }
-    double const cx = sp_repr_get_double_attribute(current, "inkscape:c_rx", 9999999);
-    double const cy = sp_repr_get_double_attribute(current, "inkscape:c_ry", 9999999);
-    if (cx != 9999999) {
-        NR::Point object_centre = NR::Point(cx, cy) * _current;
-        sp_repr_set_svg_double(current, "inkscape:c_rx", object_centre[NR::X]);
-        sp_repr_set_svg_double(current, "inkscape:c_ry", object_centre[NR::Y]);
-    }
- }
-
-
-
-
 void Inkscape::SelTrans::ungrab()
 {
     g_return_if_fail(_grabbed);
 
     Inkscape::Selection *selection = SP_DT_SELECTION(_desktop);
-
     bool updh = true;
     if (!_empty && _changed) {
         sp_selection_apply_affine(selection, _current, (_show == SHOW_OUTLINE)? true : false);
         _center *= _current;
+        _center_is_set = true;
+
+// If dragging showed content live, sp_selection_apply_affine cannot change the centers
+// appropriately - it does not know the original positions of the centers (all objects already have
+// the new bboxes). So we need to reset the centers from our saved array.
+        if (_show != SHOW_OUTLINE && !_current.is_translation()) {
+            for (unsigned i = 0; i < _items_centers.size(); i++) {
+                SPItem *currentItem = _items_centers[i].first;
+                if (currentItem->isCenterSet()) { // only if it's already set
+                    currentItem->setCenter (_items_centers[i].second * _current);
+                    SP_OBJECT(currentItem)->updateRepr();
+                }
+            }
+        }
+
         sp_document_done(SP_DT_DOCUMENT(_desktop));
         updh = false;
     }
 
-    for (unsigned i = 0; i < _items.size(); i++)
-    {
-        Inkscape::XML::Node *current = SP_OBJECT_REPR(_items[i].first);
-        if (current != NULL) {
-            _centreTrans(current);
-        }
-
+    for (unsigned i = 0; i < _items.size(); i++) {
         sp_object_unref(SP_OBJECT(_items[i].first), NULL);
     }
     _items.clear();
+    _items_centers.clear();
 
     _grabbed = false;
     _show_handles = true;
@@ -434,19 +441,17 @@ void Inkscape::SelTrans::_updateHandles()
 
     // center handle
     if ( _chandle == NULL ) {
-        _chandle = sp_knot_new(_desktop);
-        g_object_set(G_OBJECT(_chandle),
-                     "anchor", handle_center.anchor,
-                     "shape", SP_CTRL_SHAPE_BITMAP,
-                     "size", 13,
-                     "mode", SP_CTRL_MODE_XOR,
-                     "fill", 0x00000000,
-                     "fill_mouseover", 0x00000000,
-                     "stroke", 0x000000ff,
-                     "stroke_mouseover", 0xff0000b0,
-                     "pixbuf", handles[handle_center.control],
-                     "tip", _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"),
-                     NULL);
+        _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
+
+        _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
+        _chandle->setSize (13);
+        _chandle->setAnchor (handle_center.anchor);
+        _chandle->setMode (SP_CTRL_MODE_XOR);
+        _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
+        _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
+        _chandle->setPixbuf(handles[handle_center.control]);
+        sp_knot_update_ctrl(_chandle);
+
         g_signal_connect(G_OBJECT(_chandle), "request",
                          G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
         g_signal_connect(G_OBJECT(_chandle), "moved",
@@ -455,6 +460,8 @@ void Inkscape::SelTrans::_updateHandles()
                          G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
         g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
                          G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
+        g_signal_connect(G_OBJECT(_chandle), "clicked",
+                         G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
     }
 
     sp_remove_handles(&_chandle, 1);
@@ -469,16 +476,15 @@ void Inkscape::SelTrans::_updateHandles()
                     _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
                     _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
     }
+
+    if (!_center_is_set) {
+        _center = _desktop->selection->center();
+        _center_is_set = true;
+    }
+
     if ( _state == STATE_SCALE ) {
         sp_knot_hide(_chandle);
     } else {
-        Inkscape::Selection *selection = _desktop->selection;
-        Inkscape::XML::Node *current = selection->singleRepr();
-        if (current != NULL && sp_repr_get_double_attribute(current, "inkscape:c_rx", 99999999) != 99999999) {
-            double cx = sp_repr_get_double_attribute(current, "inkscape:c_rx", _center[NR::X]);
-            double cy = sp_repr_get_double_attribute(current, "inkscape:c_ry", _center[NR::Y]);
-            _center = NR::Point(cx, cy);
-        }
         sp_knot_show(_chandle);
         sp_knot_moveto(_chandle, &_center);
     }
@@ -520,19 +526,16 @@ void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const han
 
     for (int i = 0; i < num; i++) {
         if (knot[i] == NULL) {
-            knot[i] = sp_knot_new(_desktop);
-            g_object_set(G_OBJECT(knot[i]),
-                         "anchor", handle[i].anchor,
-                         "shape", SP_CTRL_SHAPE_BITMAP,
-                         "size", 13,
-                         "mode", SP_KNOT_MODE_XOR,
-                         "fill", 0x000000ff, // inversion
-                         "fill_mouseover", 0x00ff6600, // green
-                         "stroke", 0x000000ff, // inversion
-                         "stroke_mouseover", 0x000000ff, // inversion
-                         "pixbuf", handles[handle[i].control],
-                         "tip", i % 2 ? even_tip : odd_tip,
-                         NULL);
+            knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
+
+            knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
+            knot[i]->setSize (13);
+            knot[i]->setAnchor (handle[i].anchor);
+            knot[i]->setMode (SP_CTRL_MODE_XOR);
+            knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
+            knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
+            knot[i]->setPixbuf(handles[handle[i].control]);
+            sp_knot_update_ctrl(knot[i]);
 
             g_signal_connect(G_OBJECT(knot[i]), "request",
                              G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
@@ -581,6 +584,32 @@ static gboolean sp_sel_trans_handle_request(SPKnot *knot, NR::Point *position, g
         );
 }
 
+static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
+{
+    SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
+        knot, state, *(SPSelTransHandle const *) data
+        );
+}
+
+void Inkscape::SelTrans::handleClick(SPKnot *knot, guint state, SPSelTransHandle const &handle)
+{
+    switch (handle.anchor) {
+        case GTK_ANCHOR_CENTER:
+            if (state & GDK_SHIFT_MASK) {
+                // Unset the  center position for all selected items
+                for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
+                    SPItem *it = (SPItem*)sp_object_ref(SP_OBJECT(l->data), NULL);
+                    it->unsetCenter();
+                    SP_OBJECT(it)->updateRepr();
+                }
+                sp_document_maybe_done (SP_DT_DOCUMENT(_desktop), "center::unset");
+            }
+            break;
+        default:
+            break;
+    }
+}
+
 void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint state, SPSelTransHandle const &handle)
 {
     switch (handle.anchor) {
@@ -633,7 +662,7 @@ gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, NR::Point *position, gu
     knot->desktop->set_coordinate_status(*position);
     knot->desktop->setPosition(*position);
 
-    
+
     if (state & GDK_MOD1_MASK) {
         *position = _point + ( *position - _point ) / 10;
     }
@@ -657,7 +686,7 @@ void Inkscape::SelTrans::_selChanged(Inkscape::Selection *selection)
 {
     if (!_grabbed) {
         _updateVolatileState();
-        _center = _box.midpoint();
+        _center_is_set = false; // center(s) may have changed
         _updateHandles();
     }
 }
@@ -667,20 +696,11 @@ void Inkscape::SelTrans::_selModified(Inkscape::Selection *selection, guint flag
     if (!_grabbed) {
         _updateVolatileState();
 
-        if (
-             (flags != (SP_OBJECT_MODIFIED_FLAG | SP_OBJECT_STYLE_MODIFIED_FLAG)) &&
-             (flags != SP_OBJECT_PARENT_MODIFIED_FLAG) &&
-             (flags != SP_OBJECT_CHILD_MODIFIED_FLAG) &&
-             !_changed) {
-            // Only reset center if object itself is modified (not style, parent or child),
-            // and this is not a local change by seltrans
-            // (still annoyingly recenters on keyboard transforms, fixme)
-            _center = _box.midpoint();
-        }
-
         // reset internal flag
         _changed = false;
 
+        _center_is_set = false;  // center(s) may have changed
+
         _updateHandles();
     }
 }
@@ -852,7 +872,7 @@ gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, NR::
     for (GSList const *i = _selection->itemList(); i != NULL; i = i->next) {
         it.push_back(reinterpret_cast<SPItem*>(i->data));
     }
-    
+
     if ( state & GDK_CONTROL_MASK ) {
         s[perp] = fabs(s[axis]);
 
@@ -1032,14 +1052,6 @@ gboolean Inkscape::SelTrans::centerRequest(NR::Point &pt, guint state)
         }
     }
 
-    Inkscape::Selection *selection = _desktop->selection;
-    Inkscape::XML::Node *current = selection->singleRepr();
-    if (current != NULL){
-        sp_repr_set_svg_double(current, "inkscape:c_rx", pt[X]);
-        sp_repr_set_svg_double(current, "inkscape:c_ry", pt[Y]);
-
-    }
-
     if (!(state & GDK_SHIFT_MASK)) {
         // screen pixels to snap center to bbox
 #define SNAP_DIST 5
@@ -1094,7 +1106,7 @@ void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &,
 {
     seltrans->rotate(pt, state);
 }
-    
+
 void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt, guint state)
 {
     using NR::X;
@@ -1135,11 +1147,12 @@ void Inkscape::SelTrans::stretch(SPSelTransHandle const &handle, NR::Point &pt,
         s[!dim] = fabs(s[dim]);
     }
 
-    NR::Rect new_bbox = _box * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
+    NR::Point new_bbox_min = _box.min() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
+    NR::Point new_bbox_max = _box.max() * (NR::translate(-scale_origin) * NR::Matrix(s) * NR::translate(scale_origin));
 
     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
-    NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke, 
-                   new_bbox.min()[NR::X], new_bbox.min()[NR::Y], new_bbox.max()[NR::X], new_bbox.max()[NR::Y]);
+    NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
+                   new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
 
     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
 }
@@ -1155,11 +1168,12 @@ void Inkscape::SelTrans::scale(NR::Point &pt, guint state)
         if (fabs(s[i]) < 1e-9)
             s[i] = 1e-9;
     }
-    NR::Rect new_bbox = _box * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
+    NR::Point new_bbox_min = _box.min() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
+    NR::Point new_bbox_max = _box.max() * (NR::translate(-_origin) * NR::Matrix(s) * NR::translate(_origin));
 
     int transform_stroke = prefs_get_int_attribute ("options.transform", "stroke", 1);
-    NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke, 
-                   new_bbox.min()[NR::X], new_bbox.min()[NR::Y], new_bbox.max()[NR::X], new_bbox.max()[NR::Y]);
+    NR::Matrix scaler = get_scale_transform_with_stroke (_box, _strokewidth, transform_stroke,
+                   new_bbox_min[NR::X], new_bbox_min[NR::Y], new_bbox_max[NR::X], new_bbox_max[NR::Y]);
 
     transform(scaler, NR::Point(0, 0)); // we have already accounted for origin, so pass 0,0
 }
@@ -1228,7 +1242,7 @@ void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &,
 void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
 {
     SnapManager const m(_desktop->namedview);
-    
+
     /* The amount that we've moved by during this drag */
     NR::Point dxy = xy - _point;
 
@@ -1261,9 +1275,9 @@ void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
 
         /* This will be our list of possible translations */
         std::list<std::pair<NR::Point, bool> > s;
-        
+
         if (control) {
-            
+
             /* Snap to things, and also constrain to horizontal or vertical movement */
 
             for (unsigned int dim = 0; dim < 2; dim++) {
@@ -1274,7 +1288,7 @@ void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
                                                          _snap_points,
                                                          component_vectors[dim], it, dxy));
             }
-            
+
         } else {
 
             /* Snap to things with no constraint */
@@ -1297,7 +1311,7 @@ void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
             }
         }
     }
-    
+
     if (control) {
         /* Ensure that the horizontal and vertical constraint has been applied */
         if (fabs(dxy[NR::X]) > fabs(dxy[NR::Y])) {
@@ -1306,7 +1320,7 @@ void Inkscape::SelTrans::moveTo(NR::Point const &xy, guint state)
             dxy[NR::X] = 0;
         }
     }
-    
+
     NR::Matrix const move((NR::translate(dxy)));
     NR::Point const norm(0, 0);
     transform(move, norm);