~mc.../inkscape/inkscape

« back to all changes in this revision

Viewing changes to src/grid-snapper.cpp

  • Committer: mental
  • Date: 2006-01-16 02:36:01 UTC
  • Revision ID: mental@users.sourceforge.net-20060116023601-wkr0h7edl5veyudq
moving trunk for module inkscape

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  \file grid-snapper.cpp
 
3
 *  \brief Snapping things to grids.
 
4
 *
 
5
 * Authors:
 
6
 *   Lauris Kaplinski <lauris@kaplinski.com>
 
7
 *   Frank Felfe <innerspace@iname.com>
 
8
 *   Carl Hetherington <inkscape@carlh.net>
 
9
 *
 
10
 * Copyright (C) 1999-2002 Authors 
 
11
 *
 
12
 * Released under GNU GPL, read the file 'COPYING' for more information
 
13
 */
 
14
 
 
15
#include "sp-namedview.h"
 
16
#include "display/canvas-grid.h"
 
17
 
 
18
/**
 
19
 * \return x rounded to the nearest multiple of c1 plus c0.
 
20
 *
 
21
 * \note
 
22
 * If c1==0 (and c0 is finite), then returns +/-inf.  This makes grid spacing of zero
 
23
 * mean "ignore the grid in this dimention".  We're currently discussing "good" semantics
 
24
 * for guide/grid snapping.
 
25
 */
 
26
 
 
27
/* FIXME: move this somewhere else, perhaps */
 
28
static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
 
29
{
 
30
    return floor((x - c0) / c1 + .5) * c1 + c0;
 
31
}
 
32
 
 
33
Inkscape::GridSnapper::GridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
 
34
{
 
35
 
 
36
}
 
37
 
 
38
Inkscape::LineSnapper::LineList Inkscape::GridSnapper::_getSnapLines(NR::Point const &p) const
 
39
{
 
40
    LineList s;
 
41
 
 
42
    SPCGrid *griditem = NULL; 
 
43
    for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
 
44
        // FIXME : this is a hack since there is only one view for now
 
45
        //                 but when we'll handle multiple views, snapping should
 
46
        //                 must be rethought and maybe only the current view
 
47
        //                 should give back it's SHOWN lines to snap to
 
48
        //                 For now, the last SPCGrid in _named_view->gridviews will be used.
 
49
        griditem = SP_CGRID(l->data);
 
50
    }
 
51
 
 
52
    g_assert(griditem != NULL);
 
53
    
 
54
    for (unsigned int i = 0; i < 2; ++i) {
 
55
 
 
56
        /* This is to make sure we snap to only visible grid lines */
 
57
        double const scale = griditem->scaled[i] ? griditem->empspacing : 1;
 
58
 
 
59
        NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
 
60
                                                                 _named_view->gridspacing[i] * scale,
 
61
                                                                 _named_view->gridorigin[i]);
 
62
        
 
63
        s.push_back(std::make_pair(NR::Dim2(i), rounded));
 
64
    }
 
65
 
 
66
    return s;
 
67
}
 
68
 
 
69
/*
 
70
  Local Variables:
 
71
  mode:c++
 
72
  c-file-style:"stroustrup"
 
73
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
 
74
  indent-tabs-mode:nil
 
75
  fill-column:99
 
76
  End:
 
77
*/
 
78
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :