~valavanisalex/ubuntu/precise/inkscape/fix-943984

« back to all changes in this revision

Viewing changes to inkscape-0.47pre1/src/seltrans.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bryce Harrington
  • Date: 2009-07-02 17:09:45 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090702170945-nn6d6zswovbwju1t
Tags: 0.47~pre1-0ubuntu1
* New upstream release.
  - Don't constrain maximization on small resolution devices (pre0)
    (LP: #348842)
  - Fixes segfault on startup (pre0)
    (LP: #391149)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 * @brief Helper object for transforming selected items
 
3
 */
 
4
/* Authors:
 
5
 *   Lauris Kaplinski <lauris@kaplinski.com>
 
6
 *   bulia byak <buliabyak@users.sf.net>
 
7
 *   Carl Hetherington <inkscape@carlh.net>
 
8
 *   Diederik van Lierop <mail@diedenrezi.nl>
 
9
 *
 
10
 * Copyright (C) 1999-2002 Lauris Kaplinski
 
11
 * Copyright (C) 1999-2008 Authors
 
12
 *
 
13
 * Released under GNU GPL, read the file 'COPYING' for more information
 
14
 */
 
15
 
 
16
#ifdef HAVE_CONFIG_H
 
17
# include <config.h>
 
18
#endif
 
19
#include <cstring>
 
20
#include <string>
 
21
 
 
22
#include <2geom/transforms.h>
 
23
#include <gdk/gdkkeysyms.h>
 
24
#include "document.h"
 
25
#include "sp-namedview.h"
 
26
#include "desktop.h"
 
27
#include "desktop-handles.h"
 
28
#include "desktop-style.h"
 
29
#include "knot.h"
 
30
#include "snap.h"
 
31
#include "selection.h"
 
32
#include "select-context.h"
 
33
#include "sp-item.h"
 
34
#include "sp-item-transform.h"
 
35
#include "seltrans-handles.h"
 
36
#include "seltrans.h"
 
37
#include "selection-chemistry.h"
 
38
#include "sp-metrics.h"
 
39
#include "verbs.h"
 
40
#include <glibmm/i18n.h>
 
41
#include "display/sp-ctrlline.h"
 
42
#include "preferences.h"
 
43
#include "xml/repr.h"
 
44
#include "mod360.h"
 
45
#include <2geom/angle.h>
 
46
#include "display/snap-indicator.h"
 
47
 
 
48
 
 
49
static void sp_remove_handles(SPKnot *knot[], gint num);
 
50
 
 
51
static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data);
 
52
static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint state, gpointer data);
 
53
static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data);
 
54
static void sp_sel_trans_handle_new_event(SPKnot *knot, Geom::Point *position, guint32 state, gpointer data);
 
55
static gboolean sp_sel_trans_handle_request(SPKnot *knot, Geom::Point *p, guint state, gboolean *data);
 
56
 
 
57
extern GdkPixbuf *handles[];
 
58
 
 
59
static gboolean sp_seltrans_handle_event(SPKnot *knot, GdkEvent *event, gpointer)
 
60
{
 
61
    switch (event->type) {
 
62
        case GDK_MOTION_NOTIFY:
 
63
            break;
 
64
        case GDK_KEY_PRESS:
 
65
            if (get_group0_keyval (&event->key) == GDK_space) {
 
66
                /* stamping mode: both mode(show content and outline) operation with knot */
 
67
                if (!SP_KNOT_IS_GRABBED(knot)) {
 
68
                    return FALSE;
 
69
                }
 
70
                SPDesktop *desktop = knot->desktop;
 
71
                Inkscape::SelTrans *seltrans = SP_SELECT_CONTEXT(desktop->event_context)->_seltrans;
 
72
                seltrans->stamp();
 
73
                return TRUE;
 
74
            }
 
75
            break;
 
76
        default:
 
77
            break;
 
78
    }
 
79
 
 
80
    return FALSE;
 
81
}
 
82
 
 
83
Inkscape::SelTrans::SelTrans(SPDesktop *desktop) :
 
84
    _desktop(desktop),
 
85
    _selcue(desktop),
 
86
    _state(STATE_SCALE),
 
87
    _show(SHOW_CONTENT),
 
88
    _grabbed(false),
 
89
    _show_handles(true),
 
90
    _bbox(),
 
91
    _approximate_bbox(),
 
92
    _absolute_affine(Geom::Scale(1,1)),
 
93
    _opposite(Geom::Point(0,0)),
 
94
    _opposite_for_specpoints(Geom::Point(0,0)),
 
95
    _opposite_for_bboxpoints(Geom::Point(0,0)),
 
96
    _origin_for_specpoints(Geom::Point(0,0)),
 
97
    _origin_for_bboxpoints(Geom::Point(0,0)),
 
98
    _chandle(NULL),
 
99
    _stamp_cache(NULL),
 
100
    _message_context(desktop->messageStack())
 
101
{
 
102
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
103
    int prefs_bbox = prefs->getBool("/tools/bounding_box");
 
104
    _snap_bbox_type = !prefs_bbox ?
 
105
        SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
 
106
 
 
107
    g_return_if_fail(desktop != NULL);
 
108
 
 
109
    for (int i = 0; i < 8; i++) {
 
110
        _shandle[i] = NULL;
 
111
        _rhandle[i] = NULL;
 
112
    }
 
113
 
 
114
    _updateVolatileState();
 
115
    _current_relative_affine.setIdentity();
 
116
 
 
117
    _center_is_set = false; // reread _center from items, or set to bbox midpoint
 
118
 
 
119
    _updateHandles();
 
120
 
 
121
    _selection = sp_desktop_selection(desktop);
 
122
 
 
123
    _norm = sp_canvas_item_new(sp_desktop_controls(desktop),
 
124
                               SP_TYPE_CTRL,
 
125
                               "anchor", GTK_ANCHOR_CENTER,
 
126
                               "mode", SP_CTRL_MODE_COLOR,
 
127
                               "shape", SP_CTRL_SHAPE_BITMAP,
 
128
                               "size", 13.0,
 
129
                               "filled", TRUE,
 
130
                               "fill_color", 0x00000000,
 
131
                               "stroked", TRUE,
 
132
                               "stroke_color", 0x000000a0,
 
133
                               "pixbuf", handles[12],
 
134
                               NULL);
 
135
 
 
136
    _grip = sp_canvas_item_new(sp_desktop_controls(desktop),
 
137
                               SP_TYPE_CTRL,
 
138
                               "anchor", GTK_ANCHOR_CENTER,
 
139
                               "mode", SP_CTRL_MODE_XOR,
 
140
                               "shape", SP_CTRL_SHAPE_CROSS,
 
141
                               "size", 7.0,
 
142
                               "filled", TRUE,
 
143
                               "fill_color", 0xffffff7f,
 
144
                               "stroked", TRUE,
 
145
                               "stroke_color", 0xffffffff,
 
146
                               "pixbuf", handles[12],
 
147
                               NULL);
 
148
 
 
149
    sp_canvas_item_hide(_grip);
 
150
    sp_canvas_item_hide(_norm);
 
151
 
 
152
    for (int i = 0; i < 4; i++) {
 
153
        _l[i] = sp_canvas_item_new(sp_desktop_controls(desktop), SP_TYPE_CTRLLINE, NULL);
 
154
        sp_canvas_item_hide(_l[i]);
 
155
    }
 
156
 
 
157
    _sel_changed_connection = _selection->connectChanged(
 
158
        sigc::mem_fun(*this, &Inkscape::SelTrans::_selChanged)
 
159
        );
 
160
 
 
161
    _sel_modified_connection = _selection->connectModified(
 
162
        sigc::mem_fun(*this, &Inkscape::SelTrans::_selModified)
 
163
        );
 
164
}
 
165
 
 
166
Inkscape::SelTrans::~SelTrans()
 
167
{
 
168
    _sel_changed_connection.disconnect();
 
169
    _sel_modified_connection.disconnect();
 
170
 
 
171
    for (unsigned int i = 0; i < 8; i++) {
 
172
        if (_shandle[i]) {
 
173
            g_object_unref(G_OBJECT(_shandle[i]));
 
174
            _shandle[i] = NULL;
 
175
        }
 
176
        if (_rhandle[i]) {
 
177
            g_object_unref(G_OBJECT(_rhandle[i]));
 
178
            _rhandle[i] = NULL;
 
179
        }
 
180
    }
 
181
    if (_chandle) {
 
182
        g_object_unref(G_OBJECT(_chandle));
 
183
        _chandle = NULL;
 
184
    }
 
185
 
 
186
    if (_norm) {
 
187
        gtk_object_destroy(GTK_OBJECT(_norm));
 
188
        _norm = NULL;
 
189
    }
 
190
    if (_grip) {
 
191
        gtk_object_destroy(GTK_OBJECT(_grip));
 
192
        _grip = NULL;
 
193
    }
 
194
    for (int i = 0; i < 4; i++) {
 
195
        if (_l[i]) {
 
196
            gtk_object_destroy(GTK_OBJECT(_l[i]));
 
197
            _l[i] = NULL;
 
198
        }
 
199
    }
 
200
 
 
201
    for (unsigned i = 0; i < _items.size(); i++) {
 
202
        sp_object_unref(SP_OBJECT(_items[i]), NULL);
 
203
    }
 
204
 
 
205
    _items.clear();
 
206
    _items_const.clear();
 
207
    _items_affines.clear();
 
208
    _items_centers.clear();
 
209
}
 
210
 
 
211
void Inkscape::SelTrans::resetState()
 
212
{
 
213
    _state = STATE_SCALE;
 
214
}
 
215
 
 
216
void Inkscape::SelTrans::increaseState()
 
217
{
 
218
    if (_state == STATE_SCALE) {
 
219
        _state = STATE_ROTATE;
 
220
    } else {
 
221
        _state = STATE_SCALE;
 
222
    }
 
223
 
 
224
    _center_is_set = true; // no need to reread center
 
225
 
 
226
    _updateHandles();
 
227
}
 
228
 
 
229
void Inkscape::SelTrans::setCenter(Geom::Point const &p)
 
230
{
 
231
    _center = p;
 
232
    _center_is_set = true;
 
233
 
 
234
    // Write the new center position into all selected items
 
235
    for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
 
236
        SPItem *it = (SPItem*)SP_OBJECT(l->data);
 
237
        it->setCenter(p);
 
238
        // only set the value; updating repr and document_done will be done once, on ungrab
 
239
    }
 
240
 
 
241
    _updateHandles();
 
242
}
 
243
 
 
244
void Inkscape::SelTrans::grab(Geom::Point const &p, gdouble x, gdouble y, bool show_handles)
 
245
{
 
246
    Inkscape::Selection *selection = sp_desktop_selection(_desktop);
 
247
 
 
248
    g_return_if_fail(!_grabbed);
 
249
 
 
250
    _grabbed = true;
 
251
    _show_handles = show_handles;
 
252
    _updateVolatileState();
 
253
    _current_relative_affine.setIdentity();
 
254
 
 
255
    _changed = false;
 
256
 
 
257
    if (_empty) {
 
258
        return;
 
259
    }
 
260
 
 
261
    for (GSList const *l = selection->itemList(); l; l = l->next) {
 
262
        SPItem *it = (SPItem *)sp_object_ref(SP_OBJECT(l->data), NULL);
 
263
        _items.push_back(it);
 
264
        _items_const.push_back(it);
 
265
        _items_affines.push_back(sp_item_i2d_affine(it));
 
266
        _items_centers.push_back(it->getCenter()); // for content-dragging, we need to remember original centers
 
267
    }
 
268
 
 
269
    _handle_x = x;
 
270
    _handle_y = y;
 
271
 
 
272
    // The selector tool should snap the bbox, special snappoints, and path nodes
 
273
    // (The special points are the handles, center, rotation axis, font baseline, ends of spiral, etc.)
 
274
 
 
275
    // First, determine the bounding box for snapping ...
 
276
    _bbox = selection->bounds(_snap_bbox_type);
 
277
    _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX); // Used for correctly scaling the strokewidth
 
278
    _geometric_bbox = selection->bounds(SPItem::GEOMETRIC_BBOX);
 
279
    _point = p;
 
280
    if (_geometric_bbox) {
 
281
        _point_geom = _geometric_bbox->min() + _geometric_bbox->dimensions() * Geom::Scale(x, y);
 
282
    } else {
 
283
        _point_geom = p;
 
284
    }
 
285
 
 
286
    // Next, get all points to consider for snapping
 
287
    SnapManager const &m = _desktop->namedview->snap_manager;
 
288
        Inkscape::SnapPreferences local_snapprefs = m.snapprefs;
 
289
        local_snapprefs.setSnapToItemNode(true); // We should get at least the cusp nodes here. This might
 
290
        // have been turned off because (for example) the user only want paths as a snap target, not nodes
 
291
        // but as a snap source we still need some nodes though!
 
292
    _snap_points.clear();
 
293
        _snap_points = selection->getSnapPoints(&local_snapprefs);
 
294
        std::vector<std::pair<Geom::Point, int> > snap_points_hull = selection->getSnapPointsConvexHull(&local_snapprefs);
 
295
        if (_snap_points.size() > 100) {
 
296
                /* Snapping a huge number of nodes will take way too long, so limit the number of snappable nodes
 
297
                An average user would rarely ever try to snap such a large number of nodes anyway, because
 
298
                (s)he could hardly discern which node would be snapping */
 
299
                _snap_points = snap_points_hull;
 
300
                // Unfortunately, by now we will have lost the font-baseline snappoints :-(
 
301
        }
 
302
 
 
303
    // Find bbox hulling all special points, which excludes stroke width. Here we need to include the
 
304
    // path nodes, for example because a rectangle which has been converted to a path doesn't have
 
305
    // any other special points
 
306
    Geom::Rect snap_points_bbox;
 
307
    if ( snap_points_hull.empty() == false ) {
 
308
        std::vector<std::pair<Geom::Point, int> >::iterator i = snap_points_hull.begin();
 
309
        snap_points_bbox = Geom::Rect((*i).first, (*i).first);
 
310
        i++;
 
311
        while (i != snap_points_hull.end()) {
 
312
            snap_points_bbox.expandTo((*i).first);
 
313
            i++;
 
314
        }
 
315
    }
 
316
 
 
317
    _bbox_points.clear();
 
318
    if (_bbox) {
 
319
        if (m.snapprefs.getSnapModeBBox()) {
 
320
                getBBoxPoints(_bbox, &_bbox_points, false, true, m.snapprefs.getSnapBBoxEdgeMidpoints(), m.snapprefs.getSnapBBoxMidpoints());
 
321
        }
 
322
        // There are two separate "opposites" (i.e. opposite w.r.t. the handle being dragged):
 
323
        //  - one for snapping the boundingbox, which can be either visual or geometric
 
324
        //  - one for snapping the special points
 
325
        // The "opposite" in case of a geometric boundingbox always coincides with the "opposite" for the special points
 
326
        // These distinct "opposites" are needed in the snapmanager to avoid bugs such as #sf1540195 (in which
 
327
        // a box is caught between two guides)
 
328
        _opposite_for_bboxpoints = _bbox->min() + _bbox->dimensions() * Geom::Scale(1-x, 1-y);
 
329
        _opposite_for_specpoints = snap_points_bbox.min() + snap_points_bbox.dimensions() * Geom::Scale(1-x, 1-y);
 
330
        _opposite = _opposite_for_bboxpoints;
 
331
    }
 
332
 
 
333
    // When snapping the node closest to the mouse pointer is absolutely preferred over the closest snap
 
334
    // (i.e. when weight == 1), then we will not even try to snap to other points and discard those other
 
335
    // points immediately.
 
336
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
337
        if (prefs->getBool("/options/snapclosestonly/value", false)) {
 
338
        if (m.snapprefs.getSnapModeNode()) {
 
339
                        _keepClosestPointOnly(_snap_points, p);
 
340
        } else {
 
341
                _snap_points.clear(); // don't keep any point
 
342
        }
 
343
 
 
344
        if (m.snapprefs.getSnapModeBBox()) {
 
345
                        _keepClosestPointOnly(_bbox_points, p);
 
346
                } else {
 
347
                        _bbox_points.clear(); // don't keep any point
 
348
                }
 
349
 
 
350
        g_assert(_bbox_points.size() < 2 && _snap_points.size() < 2);
 
351
        if (_snap_points.size() == 1 && _bbox_points.size() == 1) { //both vectors can only have either one or zero elements
 
352
                // So we have exactly one bbox corner and one node left; now find out which is closest and delete the other one
 
353
                if (Geom::L2((_snap_points.at(0)).first - p) < Geom::L2((_bbox_points.at(0)).first - p)) {
 
354
                        _bbox_points.clear();
 
355
                } else {
 
356
                        _snap_points.clear();
 
357
                }
 
358
                }
 
359
 
 
360
 
 
361
        // Optionally, show the snap source
 
362
        if (!(_state == STATE_ROTATE && x != 0.5 && y != 0.5)) { // but not when we're draging a rotation handle, because that won't snap
 
363
                        // Now either _bbox_points or _snap_points has a single element, the other one has zero..... or both have zero elements
 
364
                        g_assert((_bbox_points.size() + _snap_points.size()) < 2);
 
365
                        if (m.snapprefs.getSnapEnabledGlobally()) {
 
366
                                if (_bbox_points.size() == 1) {
 
367
                                        _desktop->snapindicator->set_new_snapsource(_bbox_points.at(0));
 
368
                                } else if (_snap_points.size() == 1){
 
369
                                        _desktop->snapindicator->set_new_snapsource(_snap_points.at(0));
 
370
                                }
 
371
                        }
 
372
        }
 
373
    }
 
374
 
 
375
        //sp_event_context_snap_window_open(_desktop->event_context);
 
376
 
 
377
    if ((x != -1) && (y != -1)) {
 
378
        sp_canvas_item_show(_norm);
 
379
        sp_canvas_item_show(_grip);
 
380
    }
 
381
 
 
382
    if (_show == SHOW_OUTLINE) {
 
383
        for (int i = 0; i < 4; i++)
 
384
            sp_canvas_item_show(_l[i]);
 
385
    }
 
386
 
 
387
    _updateHandles();
 
388
    g_return_if_fail(_stamp_cache == NULL);
 
389
}
 
390
 
 
391
void Inkscape::SelTrans::transform(Geom::Matrix const &rel_affine, Geom::Point const &norm)
 
392
{
 
393
    g_return_if_fail(_grabbed);
 
394
    g_return_if_fail(!_empty);
 
395
 
 
396
    Geom::Matrix const affine( Geom::Translate(-norm) * rel_affine * Geom::Translate(norm) );
 
397
 
 
398
    if (_show == SHOW_CONTENT) {
 
399
        // update the content
 
400
        for (unsigned i = 0; i < _items.size(); i++) {
 
401
            SPItem &item = *_items[i];
 
402
            Geom::Matrix const &prev_transform = _items_affines[i];
 
403
            sp_item_set_i2d_affine(&item, prev_transform * affine);
 
404
        }
 
405
    } else {
 
406
        if (_bbox) {
 
407
            Geom::Point p[4];
 
408
            /* update the outline */
 
409
            for (unsigned i = 0 ; i < 4 ; i++) {
 
410
                p[i] = _bbox->corner(i) * affine;
 
411
            }
 
412
            for (unsigned i = 0 ; i < 4 ; i++) {
 
413
                sp_ctrlline_set_coords(SP_CTRLLINE(_l[i]), p[i], p[(i+1)%4]);
 
414
            }
 
415
        }
 
416
    }
 
417
 
 
418
    _current_relative_affine = affine;
 
419
    _changed = true;
 
420
    _updateHandles();
 
421
}
 
422
 
 
423
void Inkscape::SelTrans::ungrab()
 
424
{
 
425
    g_return_if_fail(_grabbed);
 
426
    _grabbed = false;
 
427
    _show_handles = true;
 
428
 
 
429
    //sp_event_context_snap_window_closed(_desktop->event_context);
 
430
 
 
431
    _desktop->snapindicator->remove_snapsource();
 
432
 
 
433
    Inkscape::Selection *selection = sp_desktop_selection(_desktop);
 
434
    _updateVolatileState();
 
435
 
 
436
    for (unsigned i = 0; i < _items.size(); i++) {
 
437
        sp_object_unref(SP_OBJECT(_items[i]), NULL);
 
438
    }
 
439
 
 
440
    sp_canvas_item_hide(_norm);
 
441
    sp_canvas_item_hide(_grip);
 
442
 
 
443
    if (_show == SHOW_OUTLINE) {
 
444
        for (int i = 0; i < 4; i++)
 
445
            sp_canvas_item_hide(_l[i]);
 
446
    }
 
447
 
 
448
    if (_stamp_cache) {
 
449
        g_slist_free(_stamp_cache);
 
450
        _stamp_cache = NULL;
 
451
    }
 
452
 
 
453
    _message_context.clear();
 
454
 
 
455
    if (!_empty && _changed) {
 
456
        sp_selection_apply_affine(selection, _current_relative_affine, (_show == SHOW_OUTLINE)? true : false);
 
457
        if (_center) {
 
458
            *_center *= _current_relative_affine;
 
459
            _center_is_set = true;
 
460
        }
 
461
 
 
462
// If dragging showed content live, sp_selection_apply_affine cannot change the centers
 
463
// appropriately - it does not know the original positions of the centers (all objects already have
 
464
// the new bboxes). So we need to reset the centers from our saved array.
 
465
        if (_show != SHOW_OUTLINE && !_current_relative_affine.isTranslation()) {
 
466
            for (unsigned i = 0; i < _items_centers.size(); i++) {
 
467
                SPItem *currentItem = _items[i];
 
468
                if (currentItem->isCenterSet()) { // only if it's already set
 
469
                    currentItem->setCenter (_items_centers[i] * _current_relative_affine);
 
470
                    SP_OBJECT(currentItem)->updateRepr();
 
471
                }
 
472
            }
 
473
        }
 
474
 
 
475
        _items.clear();
 
476
        _items_const.clear();
 
477
        _items_affines.clear();
 
478
        _items_centers.clear();
 
479
 
 
480
        if (_current_relative_affine.isTranslation()) {
 
481
            sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
482
                             _("Move"));
 
483
        } else if (_current_relative_affine.isScale()) {
 
484
            sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
485
                             _("Scale"));
 
486
        } else if (_current_relative_affine.isRotation()) {
 
487
            sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
488
                             _("Rotate"));
 
489
        } else {
 
490
            sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
491
                             _("Skew"));
 
492
        }
 
493
 
 
494
    } else {
 
495
 
 
496
        if (_center_is_set) {
 
497
            // we were dragging center; update reprs and commit undoable action
 
498
            for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
 
499
                SPItem *it = (SPItem*)SP_OBJECT(l->data);
 
500
                SP_OBJECT(it)->updateRepr();
 
501
            }
 
502
            sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
503
                            _("Set center"));
 
504
        }
 
505
 
 
506
        _items.clear();
 
507
        _items_const.clear();
 
508
        _items_affines.clear();
 
509
        _items_centers.clear();
 
510
        _updateHandles();
 
511
    }
 
512
}
 
513
 
 
514
/* fixme: This is really bad, as we compare positions for each stamp (Lauris) */
 
515
/* fixme: IMHO the best way to keep sort cache would be to implement timestamping at last */
 
516
 
 
517
void Inkscape::SelTrans::stamp()
 
518
{
 
519
    Inkscape::Selection *selection = sp_desktop_selection(_desktop);
 
520
 
 
521
    bool fixup = !_grabbed;
 
522
    if ( fixup && _stamp_cache ) {
 
523
        // TODO - give a proper fix. Simple temproary work-around for the grab() issue
 
524
        g_slist_free(_stamp_cache);
 
525
        _stamp_cache = NULL;
 
526
    }
 
527
 
 
528
    /* stamping mode */
 
529
    if (!_empty) {
 
530
        GSList *l;
 
531
        if (_stamp_cache) {
 
532
            l = _stamp_cache;
 
533
        } else {
 
534
            /* Build cache */
 
535
            l  = g_slist_copy((GSList *) selection->itemList());
 
536
            l  = g_slist_sort(l, (GCompareFunc) sp_object_compare_position);
 
537
            _stamp_cache = l;
 
538
        }
 
539
 
 
540
        while (l) {
 
541
            SPItem *original_item = SP_ITEM(l->data);
 
542
            Inkscape::XML::Node *original_repr = SP_OBJECT_REPR(original_item);
 
543
 
 
544
            // remember the position of the item
 
545
            gint pos = original_repr->position();
 
546
            // remember parent
 
547
            Inkscape::XML::Node *parent = sp_repr_parent(original_repr);
 
548
 
 
549
            Inkscape::XML::Node *copy_repr = original_repr->duplicate(parent->document());
 
550
 
 
551
            // add the new repr to the parent
 
552
            parent->appendChild(copy_repr);
 
553
            // move to the saved position
 
554
            copy_repr->setPosition(pos > 0 ? pos : 0);
 
555
 
 
556
            SPItem *copy_item = (SPItem *) sp_desktop_document(_desktop)->getObjectByRepr(copy_repr);
 
557
 
 
558
            Geom::Matrix const *new_affine;
 
559
            if (_show == SHOW_OUTLINE) {
 
560
                Geom::Matrix const i2d(sp_item_i2d_affine(original_item));
 
561
                Geom::Matrix const i2dnew( i2d * _current_relative_affine );
 
562
                sp_item_set_i2d_affine(copy_item, i2dnew);
 
563
                new_affine = &copy_item->transform;
 
564
            } else {
 
565
                new_affine = &original_item->transform;
 
566
            }
 
567
 
 
568
            sp_item_write_transform(copy_item, copy_repr, *new_affine);
 
569
 
 
570
            if ( copy_item->isCenterSet() && _center ) {
 
571
                copy_item->setCenter(*_center * _current_relative_affine);
 
572
            }
 
573
 
 
574
            Inkscape::GC::release(copy_repr);
 
575
            l = l->next;
 
576
        }
 
577
        sp_document_done(sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
578
                         _("Stamp"));
 
579
    }
 
580
 
 
581
    if ( fixup && _stamp_cache ) {
 
582
        // TODO - give a proper fix. Simple temproary work-around for the grab() issue
 
583
        g_slist_free(_stamp_cache);
 
584
        _stamp_cache = NULL;
 
585
    }
 
586
}
 
587
 
 
588
void Inkscape::SelTrans::_updateHandles()
 
589
{
 
590
    if ( !_show_handles || _empty )
 
591
    {
 
592
        sp_remove_handles(_shandle, 8);
 
593
        sp_remove_handles(_rhandle, 8);
 
594
        sp_remove_handles(&_chandle, 1);
 
595
        return;
 
596
    }
 
597
 
 
598
    // center handle
 
599
    if ( _chandle == NULL ) {
 
600
        _chandle = sp_knot_new(_desktop, _("<b>Center</b> of rotation and skewing: drag to reposition; scaling with Shift also uses this center"));
 
601
 
 
602
        _chandle->setShape (SP_CTRL_SHAPE_BITMAP);
 
603
        _chandle->setSize (13);
 
604
        _chandle->setAnchor (handle_center.anchor);
 
605
        _chandle->setMode (SP_CTRL_MODE_XOR);
 
606
        _chandle->setFill(0x00000000, 0x00000000, 0x00000000);
 
607
        _chandle->setStroke(0x000000ff, 0xff0000b0, 0xff0000b0);
 
608
        _chandle->setPixbuf(handles[handle_center.control]);
 
609
        sp_knot_update_ctrl(_chandle);
 
610
 
 
611
        g_signal_connect(G_OBJECT(_chandle), "request",
 
612
                         G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle_center);
 
613
        g_signal_connect(G_OBJECT(_chandle), "moved",
 
614
                         G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle_center);
 
615
        g_signal_connect(G_OBJECT(_chandle), "grabbed",
 
616
                         G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle_center);
 
617
        g_signal_connect(G_OBJECT(_chandle), "ungrabbed",
 
618
                         G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle_center);
 
619
        g_signal_connect(G_OBJECT(_chandle), "clicked",
 
620
                         G_CALLBACK(sp_sel_trans_handle_click), (gpointer) &handle_center);
 
621
    }
 
622
 
 
623
    sp_remove_handles(&_chandle, 1);
 
624
    if ( _state == STATE_SCALE ) {
 
625
        sp_remove_handles(_rhandle, 8);
 
626
        _showHandles(_shandle, handles_scale, 8,
 
627
                    _("<b>Squeeze or stretch</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"),
 
628
                    _("<b>Scale</b> selection; with <b>Ctrl</b> to scale uniformly; with <b>Shift</b> to scale around rotation center"));
 
629
    } else {
 
630
        sp_remove_handles(_shandle, 8);
 
631
        _showHandles(_rhandle, handles_rotate, 8,
 
632
                    _("<b>Skew</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to skew around the opposite side"),
 
633
                    _("<b>Rotate</b> selection; with <b>Ctrl</b> to snap angle; with <b>Shift</b> to rotate around the opposite corner"));
 
634
    }
 
635
 
 
636
    if (!_center_is_set) {
 
637
        _center = _desktop->selection->center();
 
638
        _center_is_set = true;
 
639
    }
 
640
 
 
641
    if ( _state == STATE_SCALE || !_center ) {
 
642
        sp_knot_hide(_chandle);
 
643
    } else {
 
644
        sp_knot_show(_chandle);
 
645
        sp_knot_moveto(_chandle, *_center);
 
646
    }
 
647
}
 
648
 
 
649
void Inkscape::SelTrans::_updateVolatileState()
 
650
{
 
651
    Inkscape::Selection *selection = sp_desktop_selection(_desktop);
 
652
    _empty = selection->isEmpty();
 
653
 
 
654
    if (_empty) {
 
655
        return;
 
656
    }
 
657
 
 
658
    //Update the bboxes
 
659
    _bbox = selection->bounds(_snap_bbox_type);
 
660
    _approximate_bbox = selection->bounds(SPItem::APPROXIMATE_BBOX);
 
661
 
 
662
    if (!_bbox) {
 
663
        _empty = true;
 
664
        return;
 
665
    }
 
666
 
 
667
    _strokewidth = stroke_average_width (selection->itemList());
 
668
}
 
669
 
 
670
static void sp_remove_handles(SPKnot *knot[], gint num)
 
671
{
 
672
    for (int i = 0; i < num; i++) {
 
673
        if (knot[i] != NULL) {
 
674
            sp_knot_hide(knot[i]);
 
675
        }
 
676
    }
 
677
}
 
678
 
 
679
void Inkscape::SelTrans::_showHandles(SPKnot *knot[], SPSelTransHandle const handle[], gint num,
 
680
                             gchar const *even_tip, gchar const *odd_tip)
 
681
{
 
682
    g_return_if_fail( !_empty );
 
683
 
 
684
    for (int i = 0; i < num; i++) {
 
685
        if (knot[i] == NULL) {
 
686
            knot[i] = sp_knot_new(_desktop, i % 2 ? even_tip : odd_tip);
 
687
 
 
688
            knot[i]->setShape (SP_CTRL_SHAPE_BITMAP);
 
689
            knot[i]->setSize (13);
 
690
            knot[i]->setAnchor (handle[i].anchor);
 
691
            knot[i]->setMode (SP_CTRL_MODE_XOR);
 
692
            knot[i]->setFill(0x000000ff, 0x00ff6600, 0x00ff6600); // inversion, green, green
 
693
            knot[i]->setStroke(0x000000ff, 0x000000ff, 0x000000ff); // inversion
 
694
            knot[i]->setPixbuf(handles[handle[i].control]);
 
695
            sp_knot_update_ctrl(knot[i]);
 
696
 
 
697
            g_signal_connect(G_OBJECT(knot[i]), "request",
 
698
                             G_CALLBACK(sp_sel_trans_handle_request), (gpointer) &handle[i]);
 
699
            g_signal_connect(G_OBJECT(knot[i]), "moved",
 
700
                             G_CALLBACK(sp_sel_trans_handle_new_event), (gpointer) &handle[i]);
 
701
            g_signal_connect(G_OBJECT(knot[i]), "grabbed",
 
702
                             G_CALLBACK(sp_sel_trans_handle_grab), (gpointer) &handle[i]);
 
703
            g_signal_connect(G_OBJECT(knot[i]), "ungrabbed",
 
704
                             G_CALLBACK(sp_sel_trans_handle_ungrab), (gpointer) &handle[i]);
 
705
            g_signal_connect(G_OBJECT(knot[i]), "event", G_CALLBACK(sp_seltrans_handle_event), (gpointer) &handle[i]);
 
706
        }
 
707
        sp_knot_show(knot[i]);
 
708
 
 
709
        Geom::Point const handle_pt(handle[i].x, handle[i].y);
 
710
        // shouldn't have nullary bbox, but knots
 
711
        g_assert(_bbox);
 
712
        Geom::Point p( _bbox->min()
 
713
                     + ( _bbox->dimensions()
 
714
                         * Geom::Scale(handle_pt) ) );
 
715
 
 
716
        sp_knot_moveto(knot[i], p);
 
717
    }
 
718
}
 
719
 
 
720
static void sp_sel_trans_handle_grab(SPKnot *knot, guint state, gpointer data)
 
721
{
 
722
    SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleGrab(
 
723
        knot, state, *(SPSelTransHandle const *) data
 
724
        );
 
725
}
 
726
 
 
727
static void sp_sel_trans_handle_ungrab(SPKnot *knot, guint /*state*/, gpointer /*data*/)
 
728
{
 
729
    SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->ungrab();
 
730
}
 
731
 
 
732
static void sp_sel_trans_handle_new_event(SPKnot *knot, Geom::Point *position, guint state, gpointer data)
 
733
{
 
734
    SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleNewEvent(
 
735
        knot, position, state, *(SPSelTransHandle const *) data
 
736
        );
 
737
}
 
738
 
 
739
static gboolean sp_sel_trans_handle_request(SPKnot *knot, Geom::Point *position, guint state, gboolean *data)
 
740
{
 
741
    return SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleRequest(
 
742
        knot, position, state, *(SPSelTransHandle const *) data
 
743
        );
 
744
}
 
745
 
 
746
static void sp_sel_trans_handle_click(SPKnot *knot, guint state, gpointer data)
 
747
{
 
748
    SP_SELECT_CONTEXT(knot->desktop->event_context)->_seltrans->handleClick(
 
749
        knot, state, *(SPSelTransHandle const *) data
 
750
        );
 
751
}
 
752
 
 
753
void Inkscape::SelTrans::handleClick(SPKnot */*knot*/, guint state, SPSelTransHandle const &handle)
 
754
{
 
755
    switch (handle.anchor) {
 
756
        case GTK_ANCHOR_CENTER:
 
757
            if (state & GDK_SHIFT_MASK) {
 
758
                // Unset the  center position for all selected items
 
759
                for (GSList const *l = _desktop->selection->itemList(); l; l = l->next) {
 
760
                    SPItem *it = (SPItem*)(SP_OBJECT(l->data));
 
761
                    it->unsetCenter();
 
762
                    SP_OBJECT(it)->updateRepr();
 
763
                    _center_is_set = false;  // center has changed
 
764
                    _updateHandles();
 
765
                }
 
766
                sp_document_done (sp_desktop_document(_desktop), SP_VERB_CONTEXT_SELECT,
 
767
                                        _("Reset center"));
 
768
            }
 
769
            break;
 
770
        default:
 
771
            break;
 
772
    }
 
773
}
 
774
 
 
775
void Inkscape::SelTrans::handleGrab(SPKnot *knot, guint /*state*/, SPSelTransHandle const &handle)
 
776
{
 
777
    switch (handle.anchor) {
 
778
        case GTK_ANCHOR_CENTER:
 
779
            g_object_set(G_OBJECT(_grip),
 
780
                         "shape", SP_CTRL_SHAPE_BITMAP,
 
781
                         "size", 13.0,
 
782
                         NULL);
 
783
            sp_canvas_item_show(_grip);
 
784
            break;
 
785
        default:
 
786
            g_object_set(G_OBJECT(_grip),
 
787
                         "shape", SP_CTRL_SHAPE_CROSS,
 
788
                         "size", 7.0,
 
789
                         NULL);
 
790
            sp_canvas_item_show(_norm);
 
791
            sp_canvas_item_show(_grip);
 
792
 
 
793
            break;
 
794
    }
 
795
 
 
796
    grab(sp_knot_position(knot), handle.x, handle.y, FALSE);
 
797
}
 
798
 
 
799
 
 
800
void Inkscape::SelTrans::handleNewEvent(SPKnot *knot, Geom::Point *position, guint state, SPSelTransHandle const &handle)
 
801
{
 
802
    if (!SP_KNOT_IS_GRABBED(knot)) {
 
803
        return;
 
804
    }
 
805
 
 
806
    // in case items have been unhooked from the document, don't
 
807
    // try to continue processing events for them.
 
808
    for (unsigned int i = 0; i < _items.size(); i++) {
 
809
        if (!SP_OBJECT_DOCUMENT(SP_OBJECT(_items[i])) ) {
 
810
            return;
 
811
        }
 
812
    }
 
813
 
 
814
    handle.action(this, handle, *position, state);
 
815
}
 
816
 
 
817
 
 
818
gboolean Inkscape::SelTrans::handleRequest(SPKnot *knot, Geom::Point *position, guint state, SPSelTransHandle const &handle)
 
819
{
 
820
    if (!SP_KNOT_IS_GRABBED(knot)) {
 
821
        return TRUE;
 
822
    }
 
823
 
 
824
    knot->desktop->setPosition(*position);
 
825
 
 
826
    // When holding shift while rotating or skewing, the transformation will be
 
827
    // relative to the point opposite of the handle; otherwise it will be relative
 
828
    // to the center as set for the selection
 
829
    if ((!(state & GDK_SHIFT_MASK) == !(_state == STATE_ROTATE)) && (&handle != &handle_center)) {
 
830
        _origin = _opposite;
 
831
        _origin_for_bboxpoints = _opposite_for_bboxpoints;
 
832
        _origin_for_specpoints = _opposite_for_specpoints;
 
833
    } else if (_center) {
 
834
        _origin = *_center;
 
835
        _origin_for_bboxpoints = *_center;
 
836
        _origin_for_specpoints = *_center;
 
837
    } else {
 
838
        // FIXME
 
839
        return TRUE;
 
840
    }
 
841
    if (handle.request(this, handle, *position, state)) {
 
842
        sp_knot_set_position(knot, *position, state);
 
843
        SP_CTRL(_grip)->moveto(*position);
 
844
        SP_CTRL(_norm)->moveto(_origin);
 
845
    }
 
846
 
 
847
    return TRUE;
 
848
}
 
849
 
 
850
 
 
851
void Inkscape::SelTrans::_selChanged(Inkscape::Selection */*selection*/)
 
852
{
 
853
    if (!_grabbed) {
 
854
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
855
        // reread in case it changed on the fly:
 
856
        int prefs_bbox = prefs->getBool("/tools/bounding_box");
 
857
         _snap_bbox_type = !prefs_bbox ?
 
858
            SPItem::APPROXIMATE_BBOX : SPItem::GEOMETRIC_BBOX;
 
859
        //SPItem::APPROXIMATE_BBOX will be replaced by SPItem::VISUAL_BBOX, as soon as the latter is implemented properly
 
860
 
 
861
        _updateVolatileState();
 
862
        _current_relative_affine.setIdentity();
 
863
        _center_is_set = false; // center(s) may have changed
 
864
        _updateHandles();
 
865
    }
 
866
}
 
867
 
 
868
void Inkscape::SelTrans::_selModified(Inkscape::Selection */*selection*/, guint /*flags*/)
 
869
{
 
870
    if (!_grabbed) {
 
871
        _updateVolatileState();
 
872
        _current_relative_affine.setIdentity();
 
873
 
 
874
        // reset internal flag
 
875
        _changed = false;
 
876
 
 
877
        _center_is_set = false;  // center(s) may have changed
 
878
 
 
879
        _updateHandles();
 
880
    }
 
881
}
 
882
 
 
883
/*
 
884
 * handlers for handle move-request
 
885
 */
 
886
 
 
887
/** Returns -1 or 1 according to the sign of x.  Returns 1 for 0 and NaN. */
 
888
static double sign(double const x)
 
889
{
 
890
    return ( x < 0
 
891
             ? -1
 
892
             : 1 );
 
893
}
 
894
 
 
895
gboolean sp_sel_trans_scale_request(Inkscape::SelTrans *seltrans,
 
896
                                    SPSelTransHandle const &, Geom::Point &pt, guint state)
 
897
{
 
898
    return seltrans->scaleRequest(pt, state);
 
899
}
 
900
 
 
901
gboolean sp_sel_trans_stretch_request(Inkscape::SelTrans *seltrans,
 
902
                                      SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
903
{
 
904
    return seltrans->stretchRequest(handle, pt, state);
 
905
}
 
906
 
 
907
gboolean sp_sel_trans_skew_request(Inkscape::SelTrans *seltrans,
 
908
                                   SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
909
{
 
910
    return seltrans->skewRequest(handle, pt, state);
 
911
}
 
912
 
 
913
gboolean sp_sel_trans_rotate_request(Inkscape::SelTrans *seltrans,
 
914
                                     SPSelTransHandle const &, Geom::Point &pt, guint state)
 
915
{
 
916
    return seltrans->rotateRequest(pt, state);
 
917
}
 
918
 
 
919
gboolean sp_sel_trans_center_request(Inkscape::SelTrans *seltrans,
 
920
                                     SPSelTransHandle const &, Geom::Point &pt, guint state)
 
921
{
 
922
    return seltrans->centerRequest(pt, state);
 
923
}
 
924
 
 
925
gboolean Inkscape::SelTrans::scaleRequest(Geom::Point &pt, guint state)
 
926
{
 
927
 
 
928
    // Calculate the scale factors, which can be either visual or geometric
 
929
    // depending on which type of bbox is currently being used (see preferences -> selector tool)
 
930
    Geom::Scale default_scale = calcScaleFactors(_point, pt, _origin);
 
931
 
 
932
    // Find the scale factors for the geometric bbox
 
933
    Geom::Point pt_geom = _getGeomHandlePos(pt);
 
934
    Geom::Scale geom_scale = calcScaleFactors(_point_geom, pt_geom, _origin_for_specpoints);
 
935
 
 
936
    _absolute_affine = Geom::identity(); //Initialize the scaler
 
937
 
 
938
    if (state & GDK_MOD1_MASK) { // scale by an integer multiplier/divider
 
939
        // We're scaling either the visual or the geometric bbox here (see the comment above)
 
940
        for ( unsigned int i = 0 ; i < 2 ; i++ ) {
 
941
            if (fabs(default_scale[i]) > 1) {
 
942
                default_scale[i] = round(default_scale[i]);
 
943
            } else if (default_scale[i] != 0) {
 
944
                default_scale[i] = 1/round(1/(MIN(default_scale[i], 10)));
 
945
            }
 
946
        }
 
947
        // Update the knot position
 
948
        pt = _calcAbsAffineDefault(default_scale);
 
949
        // When scaling by an integer, snapping is not needed
 
950
    } else {
 
951
        // In all other cases we should try to snap now
 
952
        SnapManager &m = _desktop->namedview->snap_manager;
 
953
        m.setup(_desktop, false, _items_const);
 
954
 
 
955
        Inkscape::SnappedPoint bb, sn;
 
956
        Geom::Coord bd(NR_HUGE);
 
957
        Geom::Coord sd(NR_HUGE);
 
958
 
 
959
        if ((state & GDK_CONTROL_MASK) || _desktop->isToolboxButtonActive ("lock")) {
 
960
            // Scale is locked to a 1:1 aspect ratio, so that s[X] must be made to equal s[Y].
 
961
            //
 
962
            // The aspect-ratio must be locked before snapping
 
963
            if (fabs(default_scale[Geom::X]) > fabs(default_scale[Geom::Y])) {
 
964
                default_scale[Geom::X] = fabs(default_scale[Geom::Y]) * sign(default_scale[Geom::X]);
 
965
                geom_scale[Geom::X] = fabs(geom_scale[Geom::Y]) * sign(geom_scale[Geom::X]);
 
966
            } else {
 
967
                default_scale[Geom::Y] = fabs(default_scale[Geom::X]) * sign(default_scale[Geom::Y]);
 
968
                geom_scale[Geom::Y] = fabs(geom_scale[Geom::X]) * sign(geom_scale[Geom::Y]);
 
969
            }
 
970
 
 
971
            // Snap along a suitable constraint vector from the origin.
 
972
            bb = m.constrainedSnapScale(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, default_scale, _origin_for_bboxpoints);
 
973
            sn = m.constrainedSnapScale(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, geom_scale, _origin_for_specpoints);
 
974
 
 
975
            /* Choose the smaller difference in scale.  Since s[X] == s[Y] we can
 
976
            ** just compare difference in s[X].
 
977
            */
 
978
            bd = bb.getSnapped() ? fabs(bb.getTransformation()[Geom::X] - default_scale[Geom::X]) : NR_HUGE;
 
979
            sd = sn.getSnapped() ? fabs(sn.getTransformation()[Geom::X] - geom_scale[Geom::X]) : NR_HUGE;
 
980
        } else {
 
981
            /* Scale aspect ratio is unlocked */
 
982
            bb = m.freeSnapScale(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, default_scale, _origin_for_bboxpoints);
 
983
            sn = m.freeSnapScale(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, geom_scale, _origin_for_specpoints);
 
984
 
 
985
            /* Pick the snap that puts us closest to the original scale */
 
986
            bd = bb.getSnapped() ? fabs(Geom::L2(bb.getTransformation()) - Geom::L2(Geom::Point(default_scale[Geom::X], default_scale[Geom::Y]))) : NR_HUGE;
 
987
            sd = sn.getSnapped() ? fabs(Geom::L2(sn.getTransformation()) - Geom::L2(Geom::Point(geom_scale[Geom::X], geom_scale[Geom::Y]))) : NR_HUGE;
 
988
        }
 
989
 
 
990
        if (!(bb.getSnapped() || sn.getSnapped())) {
 
991
            // We didn't snap at all! Don't update the handle position, just calculate the new transformation
 
992
            _calcAbsAffineDefault(default_scale);
 
993
            _desktop->snapindicator->remove_snaptarget();
 
994
        } else if (bd < sd) {
 
995
            // We snapped the bbox (which is either visual or geometric)
 
996
            _desktop->snapindicator->set_new_snaptarget(bb);
 
997
            default_scale = Geom::Scale(bb.getTransformation());
 
998
            // Calculate the new transformation and update the handle position
 
999
            pt = _calcAbsAffineDefault(default_scale);
 
1000
        } else {
 
1001
            _desktop->snapindicator->set_new_snaptarget(sn);
 
1002
            // We snapped the special points (e.g. nodes), which are not at the visual bbox
 
1003
            // The handle location however (pt) might however be at the visual bbox, so we
 
1004
            // will have to calculate pt taking the stroke width into account
 
1005
            geom_scale = Geom::Scale(sn.getTransformation());
 
1006
            pt = _calcAbsAffineGeom(geom_scale);
 
1007
        }
 
1008
    }
 
1009
 
 
1010
    /* Status text */
 
1011
    _message_context.setF(Inkscape::IMMEDIATE_MESSAGE,
 
1012
                          _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
 
1013
                          100 * _absolute_affine[0], 100 * _absolute_affine[3]);
 
1014
 
 
1015
    return TRUE;
 
1016
}
 
1017
 
 
1018
gboolean Inkscape::SelTrans::stretchRequest(SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
1019
{
 
1020
    Geom::Dim2 axis, perp;
 
1021
    switch (handle.cursor) {
 
1022
        case GDK_TOP_SIDE:
 
1023
        case GDK_BOTTOM_SIDE:
 
1024
            axis = Geom::Y;
 
1025
            perp = Geom::X;
 
1026
            break;
 
1027
        case GDK_LEFT_SIDE:
 
1028
        case GDK_RIGHT_SIDE:
 
1029
            axis = Geom::X;
 
1030
            perp = Geom::Y;
 
1031
            break;
 
1032
        default:
 
1033
            g_assert_not_reached();
 
1034
            return TRUE;
 
1035
    };
 
1036
 
 
1037
    // Calculate the scale factors, which can be either visual or geometric
 
1038
    // depending on which type of bbox is currently being used (see preferences -> selector tool)
 
1039
    Geom::Scale default_scale = calcScaleFactors(_point, pt, _origin);
 
1040
    default_scale[perp] = 1;
 
1041
 
 
1042
    // Find the scale factors for the geometric bbox
 
1043
    Geom::Point pt_geom = _getGeomHandlePos(pt);
 
1044
    Geom::Scale geom_scale = calcScaleFactors(_point_geom, pt_geom, _origin_for_specpoints);
 
1045
    geom_scale[perp] = 1;
 
1046
 
 
1047
    _absolute_affine = Geom::identity(); //Initialize the scaler
 
1048
 
 
1049
    if (state & GDK_MOD1_MASK) { // stretch by an integer multiplier/divider
 
1050
        if (fabs(default_scale[axis]) > 1) {
 
1051
            default_scale[axis] = round(default_scale[axis]);
 
1052
        } else if (default_scale[axis] != 0) {
 
1053
            default_scale[axis] = 1/round(1/(MIN(default_scale[axis], 10)));
 
1054
        }
 
1055
        // Calculate the new transformation and update the handle position
 
1056
        pt = _calcAbsAffineDefault(default_scale);
 
1057
        // When stretching by an integer, snapping is not needed
 
1058
    } else {
 
1059
        // In all other cases we should try to snap now
 
1060
 
 
1061
        SnapManager &m = _desktop->namedview->snap_manager;
 
1062
        m.setup(_desktop, false, _items_const);
 
1063
 
 
1064
        Inkscape::SnappedPoint bb, sn;
 
1065
        g_assert(bb.getSnapped() == false); // Check initialization to catch any regression
 
1066
        Geom::Coord bd(NR_HUGE);
 
1067
        Geom::Coord sd(NR_HUGE);
 
1068
 
 
1069
        bool symmetrical = state & GDK_CONTROL_MASK;
 
1070
 
 
1071
        bb = m.constrainedSnapStretch(SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, Geom::Coord(default_scale[axis]), _origin_for_bboxpoints, Geom::Dim2(axis), symmetrical);
 
1072
        sn = m.constrainedSnapStretch(SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, Geom::Coord(geom_scale[axis]), _origin_for_specpoints, Geom::Dim2(axis), symmetrical);
 
1073
 
 
1074
        if (bb.getSnapped()) {
 
1075
            // We snapped the bbox (which is either visual or geometric)
 
1076
            bd = fabs(bb.getTransformation()[axis] - default_scale[axis]);
 
1077
            default_scale[axis] = bb.getTransformation()[axis];
 
1078
        }
 
1079
 
 
1080
        if (sn.getSnapped()) {
 
1081
            sd = fabs(sn.getTransformation()[axis] - geom_scale[axis]);
 
1082
            geom_scale[axis] = sn.getTransformation()[axis];
 
1083
        }
 
1084
 
 
1085
        if (symmetrical) {
 
1086
            // on ctrl, apply symmetrical scaling instead of stretching
 
1087
            // Preserve aspect ratio, but never flip in the dimension not being edited (by using fabs())
 
1088
            default_scale[perp] = fabs(default_scale[axis]);
 
1089
            geom_scale[perp] = fabs(geom_scale[axis]);
 
1090
        }
 
1091
 
 
1092
        if (!(bb.getSnapped() || sn.getSnapped())) {
 
1093
            // We didn't snap at all! Don't update the handle position, just calculate the new transformation
 
1094
            _calcAbsAffineDefault(default_scale);
 
1095
            _desktop->snapindicator->remove_snaptarget();
 
1096
        } else if (bd < sd) {
 
1097
            _desktop->snapindicator->set_new_snaptarget(bb);
 
1098
            // Calculate the new transformation and update the handle position
 
1099
            pt = _calcAbsAffineDefault(default_scale);
 
1100
        } else {
 
1101
            _desktop->snapindicator->set_new_snaptarget(sn);
 
1102
            // We snapped the special points (e.g. nodes), which are not at the visual bbox
 
1103
            // The handle location however (pt) might however be at the visual bbox, so we
 
1104
            // will have to calculate pt taking the stroke width into account
 
1105
            pt = _calcAbsAffineGeom(geom_scale);
 
1106
        }
 
1107
    }
 
1108
 
 
1109
    // status text
 
1110
    _message_context.setF(Inkscape::IMMEDIATE_MESSAGE,
 
1111
                          _("<b>Scale</b>: %0.2f%% x %0.2f%%; with <b>Ctrl</b> to lock ratio"),
 
1112
                          100 * _absolute_affine[0], 100 * _absolute_affine[3]);
 
1113
 
 
1114
    return TRUE;
 
1115
}
 
1116
 
 
1117
gboolean Inkscape::SelTrans::skewRequest(SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
1118
{
 
1119
    /* When skewing (or rotating):
 
1120
     * 1) the stroke width will not change. This makes life much easier because we don't have to
 
1121
     *    account for that (like for scaling or stretching). As a consequence, all points will
 
1122
     *    have the same origin for the transformation and for the snapping.
 
1123
     * 2) When holding shift, the transformation will be relative to the point opposite of
 
1124
     *    the handle; otherwise it will be relative to the center as set for the selection
 
1125
     */
 
1126
 
 
1127
    Geom::Dim2 dim_a;
 
1128
    Geom::Dim2 dim_b;
 
1129
 
 
1130
    switch (handle.cursor) {
 
1131
        case GDK_SB_H_DOUBLE_ARROW:
 
1132
            dim_a = Geom::Y;
 
1133
            dim_b = Geom::X;
 
1134
            break;
 
1135
        case GDK_SB_V_DOUBLE_ARROW:
 
1136
            dim_a = Geom::X;
 
1137
            dim_b = Geom::Y;
 
1138
            break;
 
1139
        default:
 
1140
            g_assert_not_reached();
 
1141
            abort();
 
1142
            break;
 
1143
    }
 
1144
 
 
1145
    Geom::Point const initial_delta = _point - _origin;
 
1146
 
 
1147
    if (fabs(initial_delta[dim_a]) < 1e-15) {
 
1148
        return false;
 
1149
    }
 
1150
 
 
1151
    // Calculate the scale factors, which can be either visual or geometric
 
1152
    // depending on which type of bbox is currently being used (see preferences -> selector tool)
 
1153
    Geom::Scale scale = calcScaleFactors(_point, pt, _origin, false);
 
1154
    Geom::Scale skew = calcScaleFactors(_point, pt, _origin, true);
 
1155
    scale[dim_b] = 1;
 
1156
    skew[dim_b] = 1;
 
1157
 
 
1158
    if (fabs(scale[dim_a]) < 1) {
 
1159
        // Prevent shrinking of the selected object, while allowing mirroring
 
1160
        scale[dim_a] = sign(scale[dim_a]);
 
1161
    } else {
 
1162
        // Allow expanding of the selected object by integer multiples
 
1163
        scale[dim_a] = floor(scale[dim_a] + 0.5);
 
1164
    }
 
1165
 
 
1166
    double radians = atan(skew[dim_a] / scale[dim_a]);
 
1167
 
 
1168
    if (state & GDK_CONTROL_MASK) {
 
1169
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
1170
        // Snap to defined angle increments
 
1171
        int snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
1172
        if (snaps) {
 
1173
            double sections = floor(radians * snaps / M_PI + .5);
 
1174
            if (fabs(sections) >= snaps / 2) {
 
1175
                sections = sign(sections) * (snaps / 2 - 1);
 
1176
            }
 
1177
            radians = (M_PI / snaps) * sections;
 
1178
        }
 
1179
        skew[dim_a] = tan(radians) * scale[dim_a];
 
1180
    } else {
 
1181
        // Snap to objects, grids, guides
 
1182
 
 
1183
        SnapManager &m = _desktop->namedview->snap_manager;
 
1184
        m.setup(_desktop, false, _items_const);
 
1185
 
 
1186
        Inkscape::Snapper::ConstraintLine const constraint(component_vectors[dim_b]);
 
1187
        // When skewing, we cannot snap the corners of the bounding box, see the comment in "constrainedSnapSkew" for details
 
1188
        Geom::Point const s(skew[dim_a], scale[dim_a]);
 
1189
        Inkscape::SnappedPoint sn = m.constrainedSnapSkew(Inkscape::SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, constraint, s, _origin, Geom::Dim2(dim_b));
 
1190
 
 
1191
        if (sn.getSnapped()) {
 
1192
            // We snapped something, so change the skew to reflect it
 
1193
            Geom::Coord const sd = sn.getSnapped() ? sn.getTransformation()[0] : NR_HUGE;
 
1194
             _desktop->snapindicator->set_new_snaptarget(sn);
 
1195
            skew[dim_a] = sd;
 
1196
        } else {
 
1197
            _desktop->snapindicator->remove_snaptarget();
 
1198
        }
 
1199
    }
 
1200
 
 
1201
    // Update the handle position
 
1202
    pt[dim_b] = initial_delta[dim_a] * skew[dim_a] + _point[dim_b];
 
1203
    pt[dim_a] = initial_delta[dim_a] * scale[dim_a] + _origin[dim_a];
 
1204
 
 
1205
    // Calculate the relative affine
 
1206
    _relative_affine = Geom::identity();
 
1207
    _relative_affine[2*dim_a + dim_a] = (pt[dim_a] - _origin[dim_a]) / initial_delta[dim_a];
 
1208
    _relative_affine[2*dim_a + (dim_b)] = (pt[dim_b] - _point[dim_b]) / initial_delta[dim_a];
 
1209
    _relative_affine[2*(dim_b) + (dim_a)] = 0;
 
1210
    _relative_affine[2*(dim_b) + (dim_b)] = 1;
 
1211
 
 
1212
    for (int i = 0; i < 2; i++) {
 
1213
        if (fabs(_relative_affine[3*i]) < 1e-15) {
 
1214
            _relative_affine[3*i] = 1e-15;
 
1215
        }
 
1216
    }
 
1217
 
 
1218
    // Update the status text
 
1219
    double degrees = mod360symm(Geom::rad_to_deg(radians));
 
1220
    _message_context.setF(Inkscape::IMMEDIATE_MESSAGE,
 
1221
                          // TRANSLATORS: don't modify the first ";"
 
1222
                          // (it will NOT be displayed as ";" - only the second one will be)
 
1223
                          _("<b>Skew</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"),
 
1224
                          degrees);
 
1225
 
 
1226
    return TRUE;
 
1227
}
 
1228
 
 
1229
gboolean Inkscape::SelTrans::rotateRequest(Geom::Point &pt, guint state)
 
1230
{
 
1231
    /* When rotating (or skewing):
 
1232
     * 1) the stroke width will not change. This makes life much easier because we don't have to
 
1233
     *    account for that (like for scaling or stretching). As a consequence, all points will
 
1234
     *    have the same origin for the transformation and for the snapping.
 
1235
     * 2) When holding shift, the transformation will be relative to the point opposite of
 
1236
     *    the handle; otherwise it will be relative to the center as set for the selection
 
1237
     */
 
1238
 
 
1239
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
1240
    int snaps = prefs->getInt("/options/rotationsnapsperpi/value", 12);
 
1241
 
 
1242
    // rotate affine in rotate
 
1243
    Geom::Point const d1 = _point - _origin;
 
1244
    Geom::Point const d2 = pt     - _origin;
 
1245
 
 
1246
    Geom::Coord const h1 = Geom::L2(d1); // initial radius
 
1247
    if (h1 < 1e-15) return FALSE;
 
1248
    Geom::Point q1 = d1 / h1; // normalized initial vector to handle
 
1249
    Geom::Coord const h2 = Geom::L2(d2); // new radius
 
1250
    if (fabs(h2) < 1e-15) return FALSE;
 
1251
    Geom::Point q2 = d2 / h2; // normalized new vector to handle
 
1252
 
 
1253
    double radians;
 
1254
    if (state & GDK_CONTROL_MASK) {
 
1255
        // Snap to defined angle increments
 
1256
        double cos_t = Geom::dot(q1, q2);
 
1257
        double sin_t = Geom::dot(Geom::rot90(q1), q2);
 
1258
        radians = atan2(sin_t, cos_t);
 
1259
        if (snaps) {
 
1260
            radians = ( M_PI / snaps ) * floor( radians * snaps / M_PI + .5 );
 
1261
        }
 
1262
        q1 = Geom::Point(1, 0);
 
1263
        q2 = Geom::Point(cos(radians), sin(radians));
 
1264
    } else {
 
1265
        radians = atan2(Geom::dot(Geom::rot90(d1), d2),
 
1266
                        Geom::dot(d1, d2));
 
1267
    }
 
1268
 
 
1269
    Geom::Rotate const r1(q1);
 
1270
    Geom::Rotate const r2(q2);
 
1271
 
 
1272
    // Calculate the relative affine
 
1273
    _relative_affine = r2 * r1.inverse();
 
1274
 
 
1275
    // Update the handle position
 
1276
    pt = _point * Geom::Translate(-_origin) * _relative_affine * Geom::Translate(_origin);
 
1277
 
 
1278
    // Update the status text
 
1279
    double degrees = mod360symm(Geom::rad_to_deg(radians));
 
1280
    _message_context.setF(Inkscape::IMMEDIATE_MESSAGE,
 
1281
                          // TRANSLATORS: don't modify the first ";"
 
1282
                          // (it will NOT be displayed as ";" - only the second one will be)
 
1283
                          _("<b>Rotate</b>: %0.2f&#176;; with <b>Ctrl</b> to snap angle"), degrees);
 
1284
 
 
1285
    return TRUE;
 
1286
}
 
1287
 
 
1288
// Move the item's transformation center
 
1289
gboolean Inkscape::SelTrans::centerRequest(Geom::Point &pt, guint state)
 
1290
{
 
1291
    SnapManager &m = _desktop->namedview->snap_manager;
 
1292
    m.setup(_desktop);
 
1293
    m.freeSnapReturnByRef(SnapPreferences::SNAPPOINT_NODE, pt, Inkscape::SNAPSOURCE_HANDLE);
 
1294
 
 
1295
    if (state & GDK_CONTROL_MASK) {
 
1296
        if ( fabs(_point[Geom::X] - pt[Geom::X]) > fabs(_point[Geom::Y] - pt[Geom::Y]) ) {
 
1297
            pt[Geom::Y] = _point[Geom::Y];
 
1298
        } else {
 
1299
            pt[Geom::X] = _point[Geom::X];
 
1300
        }
 
1301
    }
 
1302
 
 
1303
    if ( !(state & GDK_SHIFT_MASK) && _bbox ) {
 
1304
        // screen pixels to snap center to bbox
 
1305
#define SNAP_DIST 5
 
1306
        // FIXME: take from prefs
 
1307
        double snap_dist = SNAP_DIST / _desktop->current_zoom();
 
1308
 
 
1309
        for (int i = 0; i < 2; i++) {
 
1310
            if (fabs(pt[i] - _bbox->min()[i]) < snap_dist) {
 
1311
                pt[i] = _bbox->min()[i];
 
1312
            }
 
1313
            if (fabs(pt[i] - _bbox->midpoint()[i]) < snap_dist) {
 
1314
                pt[i] = _bbox->midpoint()[i];
 
1315
            }
 
1316
            if (fabs(pt[i] - _bbox->max()[i]) < snap_dist) {
 
1317
                pt[i] = _bbox->max()[i];
 
1318
            }
 
1319
        }
 
1320
    }
 
1321
 
 
1322
    // status text
 
1323
    GString *xs = SP_PX_TO_METRIC_STRING(pt[Geom::X], _desktop->namedview->getDefaultMetric());
 
1324
    GString *ys = SP_PX_TO_METRIC_STRING(pt[Geom::Y], _desktop->namedview->getDefaultMetric());
 
1325
    _message_context.setF(Inkscape::NORMAL_MESSAGE, _("Move <b>center</b> to %s, %s"), xs->str, ys->str);
 
1326
    g_string_free(xs, FALSE);
 
1327
    g_string_free(ys, FALSE);
 
1328
 
 
1329
    return TRUE;
 
1330
}
 
1331
 
 
1332
/*
 
1333
 * handlers for handle movement
 
1334
 *
 
1335
 */
 
1336
 
 
1337
void sp_sel_trans_stretch(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
1338
{
 
1339
    seltrans->stretch(handle, pt, state);
 
1340
}
 
1341
 
 
1342
void sp_sel_trans_scale(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, Geom::Point &pt, guint state)
 
1343
{
 
1344
    seltrans->scale(pt, state);
 
1345
}
 
1346
 
 
1347
void sp_sel_trans_skew(Inkscape::SelTrans *seltrans, SPSelTransHandle const &handle, Geom::Point &pt, guint state)
 
1348
{
 
1349
    seltrans->skew(handle, pt, state);
 
1350
}
 
1351
 
 
1352
void sp_sel_trans_rotate(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, Geom::Point &pt, guint state)
 
1353
{
 
1354
    seltrans->rotate(pt, state);
 
1355
}
 
1356
 
 
1357
void Inkscape::SelTrans::stretch(SPSelTransHandle const &/*handle*/, Geom::Point &/*pt*/, guint /*state*/)
 
1358
{
 
1359
    transform(_absolute_affine, Geom::Point(0, 0)); // we have already accounted for origin, so pass 0,0
 
1360
}
 
1361
 
 
1362
void Inkscape::SelTrans::scale(Geom::Point &/*pt*/, guint /*state*/)
 
1363
{
 
1364
    transform(_absolute_affine, Geom::Point(0, 0)); // we have already accounted for origin, so pass 0,0
 
1365
}
 
1366
 
 
1367
void Inkscape::SelTrans::skew(SPSelTransHandle const &/*handle*/, Geom::Point &/*pt*/, guint /*state*/)
 
1368
{
 
1369
    transform(_relative_affine, _origin);
 
1370
}
 
1371
 
 
1372
void Inkscape::SelTrans::rotate(Geom::Point &/*pt*/, guint /*state*/)
 
1373
{
 
1374
    transform(_relative_affine, _origin);
 
1375
}
 
1376
 
 
1377
void sp_sel_trans_center(Inkscape::SelTrans *seltrans, SPSelTransHandle const &, Geom::Point &pt, guint /*state*/)
 
1378
{
 
1379
    seltrans->setCenter(pt);
 
1380
}
 
1381
 
 
1382
 
 
1383
void Inkscape::SelTrans::moveTo(Geom::Point const &xy, guint state)
 
1384
{
 
1385
    SnapManager &m = _desktop->namedview->snap_manager;
 
1386
 
 
1387
    /* The amount that we've moved by during this drag */
 
1388
    Geom::Point dxy = xy - _point;
 
1389
 
 
1390
    bool const alt = (state & GDK_MOD1_MASK);
 
1391
    bool const control = (state & GDK_CONTROL_MASK);
 
1392
    bool const shift = (state & GDK_SHIFT_MASK);
 
1393
 
 
1394
    if (alt) {
 
1395
 
 
1396
        /* Alt pressed means keep offset: snap the moved distance to the grid.
 
1397
        ** FIXME: this will snap to more than just the grid, nowadays.
 
1398
        */
 
1399
 
 
1400
        m.setup(_desktop, true, _items_const);
 
1401
        m.freeSnapReturnByRef(SnapPreferences::SNAPPOINT_NODE, dxy, Inkscape::SNAPSOURCE_UNDEFINED);
 
1402
 
 
1403
    } else if (shift) {
 
1404
        if (control) { // shift & control: constrained movement without snapping
 
1405
                if (fabs(dxy[Geom::X]) > fabs(dxy[Geom::Y])) {
 
1406
                                dxy[Geom::Y] = 0;
 
1407
                        } else {
 
1408
                                dxy[Geom::X] = 0;
 
1409
                        }
 
1410
        }
 
1411
    } else { //!shift: with snapping
 
1412
 
 
1413
        /* We're snapping to things, possibly with a constraint to horizontal or
 
1414
        ** vertical movement.  Obtain a list of possible translations and then
 
1415
        ** pick the smallest.
 
1416
        */
 
1417
 
 
1418
        m.setup(_desktop, false, _items_const);
 
1419
 
 
1420
        /* This will be our list of possible translations */
 
1421
        std::list<Inkscape::SnappedPoint> s;
 
1422
 
 
1423
        if (control) { // constrained movement with snapping
 
1424
 
 
1425
            /* Snap to things, and also constrain to horizontal or vertical movement */
 
1426
 
 
1427
            unsigned int dim = fabs(dxy[Geom::X]) > fabs(dxy[Geom::Y]) ? Geom::X : Geom::Y;
 
1428
                        // When doing a constrained translation, all points will move in the same direction, i.e.
 
1429
                        // either horizontally or vertically. Therefore we only have to specify the direction of
 
1430
                        // the constraint-line once. The constraint lines are parallel, but might not be colinear.
 
1431
                        // Therefore we will have to set the point through which the constraint-line runs
 
1432
                        // individually for each point to be snapped; this will be handled however by _snapTransformed()
 
1433
                        s.push_back(m.constrainedSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_BBOX,
 
1434
                                                                                                         _bbox_points,
 
1435
                                                                                                         _point,
 
1436
                                                                                                         Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
 
1437
                                                                                                         dxy));
 
1438
 
 
1439
                        s.push_back(m.constrainedSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_NODE,
 
1440
                                                                                                         _snap_points,
 
1441
                                                                                                         _point,
 
1442
                                                                                                         Inkscape::Snapper::ConstraintLine(component_vectors[dim]),
 
1443
                                                                                                         dxy));
 
1444
        } else { // !control
 
1445
 
 
1446
            // Let's leave this timer code here for a while. I'll probably need it in the near future (Diederik van Lierop)
 
1447
            /* GTimeVal starttime;
 
1448
            GTimeVal endtime;
 
1449
                g_get_current_time(&starttime); */
 
1450
 
 
1451
            /* Snap to things with no constraint */
 
1452
                s.push_back(m.freeSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_BBOX, _bbox_points, _point, dxy));
 
1453
                s.push_back(m.freeSnapTranslation(Inkscape::SnapPreferences::SNAPPOINT_NODE, _snap_points, _point, dxy));
 
1454
 
 
1455
                /*g_get_current_time(&endtime);
 
1456
                double elapsed = ((((double)endtime.tv_sec - starttime.tv_sec) * G_USEC_PER_SEC + (endtime.tv_usec - starttime.tv_usec))) / 1000.0;
 
1457
                std::cout << "Time spent snapping: " << elapsed << std::endl; */
 
1458
        }
 
1459
 
 
1460
        /* Pick one */
 
1461
        Inkscape::SnappedPoint best_snapped_point;
 
1462
        for (std::list<Inkscape::SnappedPoint>::const_iterator i = s.begin(); i != s.end(); i++) {
 
1463
            if (i->getSnapped()) {
 
1464
                if (best_snapped_point.isOtherSnapBetter(*i, true)) {
 
1465
                    best_snapped_point = *i;
 
1466
                    dxy = i->getTransformation();
 
1467
                }
 
1468
            }
 
1469
        }
 
1470
        if (best_snapped_point.getSnapped()) {
 
1471
            _desktop->snapindicator->set_new_snaptarget(best_snapped_point);
 
1472
        } else {
 
1473
            // We didn't snap, so remove any previous snap indicator
 
1474
            _desktop->snapindicator->remove_snaptarget();
 
1475
            if (control) {
 
1476
                // If we didn't snap, then we should still constrain horizontally or vertically
 
1477
                // (When we did snap, then this constraint has already been enforced by
 
1478
                // calling constrainedSnapTranslation() above)
 
1479
                if (fabs(dxy[Geom::X]) > fabs(dxy[Geom::Y])) {
 
1480
                    dxy[Geom::Y] = 0;
 
1481
                } else {
 
1482
                    dxy[Geom::X] = 0;
 
1483
                }
 
1484
            }
 
1485
        }
 
1486
    }
 
1487
 
 
1488
    Geom::Matrix const move((Geom::Translate(dxy)));
 
1489
    Geom::Point const norm(0, 0);
 
1490
    transform(move, norm);
 
1491
 
 
1492
    // status text
 
1493
    GString *xs = SP_PX_TO_METRIC_STRING(dxy[Geom::X], _desktop->namedview->getDefaultMetric());
 
1494
    GString *ys = SP_PX_TO_METRIC_STRING(dxy[Geom::Y], _desktop->namedview->getDefaultMetric());
 
1495
    _message_context.setF(Inkscape::NORMAL_MESSAGE, _("<b>Move</b> by %s, %s; with <b>Ctrl</b> to restrict to horizontal/vertical; with <b>Shift</b> to disable snapping"), xs->str, ys->str);
 
1496
    g_string_free(xs, TRUE);
 
1497
    g_string_free(ys, TRUE);
 
1498
}
 
1499
 
 
1500
// Given a location of a handle at the visual bounding box, find the corresponding location at the
 
1501
// geometrical bounding box
 
1502
Geom::Point Inkscape::SelTrans::_getGeomHandlePos(Geom::Point const &visual_handle_pos)
 
1503
{
 
1504
    if ( _snap_bbox_type == SPItem::GEOMETRIC_BBOX) {
 
1505
        // When the selector tool is using geometric bboxes, then the handle is already
 
1506
        // located at one of the geometric bbox corners
 
1507
        return visual_handle_pos;
 
1508
    }
 
1509
 
 
1510
    if (!_geometric_bbox) {
 
1511
        //_getGeomHandlePos() can only be used after _geometric_bbox has been defined!
 
1512
        return visual_handle_pos;
 
1513
    }
 
1514
 
 
1515
    // Using the Geom::Rect constructor below ensures that "min() < max()", which is important
 
1516
    // because this will also hold for _bbox, and which is required for get_scale_transform_with_stroke()
 
1517
    Geom::Rect new_bbox = Geom::Rect(_origin_for_bboxpoints, visual_handle_pos); // new visual bounding box
 
1518
    // Please note that the new_bbox might in fact be just a single line, for example when stretching (in
 
1519
    // which case the handle and origin will be aligned vertically or horizontally)
 
1520
    Geom::Point normalized_handle_pos = (visual_handle_pos - new_bbox.min()) * Geom::Scale(new_bbox.dimensions()).inverse();
 
1521
 
 
1522
    // Calculate the absolute affine while taking into account the scaling of the stroke width
 
1523
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
1524
    bool transform_stroke = prefs->getBool("/options/transform/stroke", true);
 
1525
    Geom::Matrix abs_affine = get_scale_transform_with_stroke (*_bbox, _strokewidth, transform_stroke,
 
1526
                    new_bbox.min()[Geom::X], new_bbox.min()[Geom::Y], new_bbox.max()[Geom::X], new_bbox.max()[Geom::Y]);
 
1527
 
 
1528
    // Calculate the scaled geometrical bbox
 
1529
    Geom::Rect new_geom_bbox = Geom::Rect(_geometric_bbox->min() * abs_affine, _geometric_bbox->max() * abs_affine);
 
1530
    // Find the location of the handle on this new geometrical bbox
 
1531
    return normalized_handle_pos * Geom::Scale(new_geom_bbox.dimensions()) + new_geom_bbox.min(); //new position of the geometric handle
 
1532
}
 
1533
 
 
1534
Geom::Scale Inkscape::calcScaleFactors(Geom::Point const &initial_point, Geom::Point const &new_point, Geom::Point const &origin, bool const skew)
 
1535
{
 
1536
    // Work out the new scale factors for the bbox
 
1537
 
 
1538
    Geom::Point const initial_delta = initial_point - origin;
 
1539
    Geom::Point const new_delta = new_point - origin;
 
1540
    Geom::Point const offset = new_point - initial_point;
 
1541
    Geom::Scale scale(1, 1);
 
1542
 
 
1543
    for ( unsigned int i = 0 ; i < 2 ; i++ ) {
 
1544
        if ( fabs(initial_delta[i]) > 1e-6 ) {
 
1545
            if (skew) {
 
1546
                scale[i] = offset[1-i] / initial_delta[i];
 
1547
            } else {
 
1548
                scale[i] = new_delta[i] / initial_delta[i];
 
1549
            }
 
1550
        }
 
1551
    }
 
1552
 
 
1553
    return scale;
 
1554
}
 
1555
 
 
1556
// Only for scaling/stretching
 
1557
Geom::Point Inkscape::SelTrans::_calcAbsAffineDefault(Geom::Scale const default_scale)
 
1558
{
 
1559
    Geom::Matrix abs_affine = Geom::Translate(-_origin) * Geom::Matrix(default_scale) * Geom::Translate(_origin);
 
1560
    Geom::Point new_bbox_min = _approximate_bbox->min() * abs_affine;
 
1561
    Geom::Point new_bbox_max = _approximate_bbox->max() * abs_affine;
 
1562
 
 
1563
    bool transform_stroke = false;
 
1564
    gdouble strokewidth = 0;
 
1565
 
 
1566
    if ( _snap_bbox_type != SPItem::GEOMETRIC_BBOX) {
 
1567
        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
1568
        transform_stroke = prefs->getBool("/options/transform/stroke", true);
 
1569
        strokewidth = _strokewidth;
 
1570
    }
 
1571
 
 
1572
    _absolute_affine = get_scale_transform_with_stroke (*_approximate_bbox, strokewidth, transform_stroke,
 
1573
                    new_bbox_min[Geom::X], new_bbox_min[Geom::Y], new_bbox_max[Geom::X], new_bbox_max[Geom::Y]);
 
1574
 
 
1575
    // return the new handle position
 
1576
    return ( _point - _origin ) * default_scale + _origin;
 
1577
}
 
1578
 
 
1579
// Only for scaling/stretching
 
1580
Geom::Point Inkscape::SelTrans::_calcAbsAffineGeom(Geom::Scale const geom_scale)
 
1581
{
 
1582
    _relative_affine = Geom::Matrix(geom_scale);
 
1583
    _absolute_affine = Geom::Translate(-_origin_for_specpoints) * _relative_affine * Geom::Translate(_origin_for_specpoints);
 
1584
 
 
1585
    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
 
1586
    bool const transform_stroke = prefs->getBool("/options/transform/stroke", true);
 
1587
    Geom::Rect visual_bbox = get_visual_bbox(_geometric_bbox, _absolute_affine, _strokewidth, transform_stroke);
 
1588
 
 
1589
    // return the new handle position
 
1590
    return visual_bbox.min() + visual_bbox.dimensions() * Geom::Scale(_handle_x, _handle_y);
 
1591
}
 
1592
 
 
1593
void Inkscape::SelTrans::_keepClosestPointOnly(std::vector<std::pair<Geom::Point, int> > &points, const Geom::Point &reference)
 
1594
{
 
1595
        if (points.size() < 2) return;
 
1596
 
 
1597
        std::pair<Geom::Point, int> closest_point = std::make_pair(Geom::Point(NR_HUGE, NR_HUGE), SNAPSOURCE_UNDEFINED);
 
1598
        Geom::Coord closest_dist = NR_HUGE;
 
1599
 
 
1600
        for(std::vector<std::pair<Geom::Point, int> >::const_iterator i = points.begin(); i != points.end(); i++) {
 
1601
                Geom::Coord dist = Geom::L2((*i).first - reference);
 
1602
                if (i == points.begin() || dist < closest_dist) {
 
1603
                        closest_point = *i;
 
1604
                        closest_dist = dist;
 
1605
                }
 
1606
    }
 
1607
 
 
1608
        points.clear();
 
1609
        points.push_back(closest_point);
 
1610
}
 
1611
 
 
1612
/*
 
1613
  Local Variables:
 
1614
  mode:c++
 
1615
  c-file-style:"stroustrup"
 
1616
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
1617
  indent-tabs-mode:nil
 
1618
  fill-column:99
 
1619
  End:
 
1620
*/
 
1621
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :