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) 2006 Johan Engelen <johan@shouraizou.nl>
11 * Copyright (C) 1999-2002 Authors
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 */
16 #include "sp-namedview.h"
17 #include "inkscape.h"
18 #include "desktop.h"
19 #include "display/canvas-grid.h"
20 #include "display/canvas-axonomgrid.h"
22 /**
23 * \return x rounded to the nearest multiple of c1 plus c0.
24 *
25 * \note
26 * If c1==0 (and c0 is finite), then returns +/-inf. This makes grid spacing of zero
27 * mean "ignore the grid in this dimention". We're currently discussing "good" semantics
28 * for guide/grid snapping.
29 */
31 /* FIXME: move this somewhere else, perhaps */
32 static double round_to_nearest_multiple_plus(double x, double const c1, double const c0)
33 {
34 return floor((x - c0) / c1 + .5) * c1 + c0;
35 }
37 Inkscape::GridSnapper::GridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
38 {
40 }
42 Inkscape::LineSnapper::LineList
43 Inkscape::GridSnapper::_getSnapLines(NR::Point const &p) const
44 {
45 LineList s;
47 if ( NULL == _named_view ) {
48 return s;
49 }
51 SPCGrid *griditem = NULL;
52 for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
53 // FIXME : this is a hack since there is only one view for now
54 // but when we'll handle multiple views, snapping should
55 // must be rethought and maybe only the current view
56 // should give back it's SHOWN lines to snap to
57 // For now, the last SPCGrid in _named_view->gridviews will be used.
58 if ( SP_IS_CGRID(GTK_OBJECT(l->data)) ) {
59 griditem = SP_CGRID(l->data);
60 }
61 }
63 g_assert(griditem != NULL);
65 for (unsigned int i = 0; i < 2; ++i) {
67 /* This is to make sure we snap to only visible grid lines */
68 double scaled_spacing = griditem->sw[i]; // this is spacing of visible lines if screen pixels
70 // convert screen pixels to px
71 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
72 if (SP_ACTIVE_DESKTOP) {
73 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
74 }
76 NR::Coord const rounded = round_to_nearest_multiple_plus(p[i],
77 scaled_spacing,
78 _named_view->gridorigin[i]);
80 s.push_back(std::make_pair(NR::Dim2(i), rounded));
81 }
83 return s;
84 }
88 Inkscape::AxonomGridSnapper::AxonomGridSnapper(SPNamedView const *nv, NR::Coord const d) : LineSnapper(nv, d)
89 {
91 }
94 Inkscape::LineSnapper::LineList
95 Inkscape::AxonomGridSnapper::_getSnapLines(NR::Point const &p) const
96 {
97 LineList s;
99 if ( NULL == _named_view ) {
100 return s;
101 }
103 SPCAxonomGrid *griditem = NULL;
104 for (GSList *l = _named_view->gridviews; l != NULL; l = l->next) {
105 // FIXME : this is a hack since there is only one view for now
106 // but when we'll handle multiple views, snapping should
107 // must be rethought and maybe only the current view
108 // should give back it's SHOWN lines to snap to
109 // For now, the last SPCAxonomGrid in _named_view->gridviews will be used.
110 if ( SP_IS_CAXONOMGRID(GTK_OBJECT(l->data)) ) {
111 griditem = SP_CAXONOMGRID(l->data);
112 }
113 }
115 g_assert(griditem != NULL);
117 // add vertical line.
120 // This is to make sure we snap to only visible grid lines
121 double scaled_spacing = griditem->spacing_ylines; // this is spacing of visible lines if screen pixels
122 // convert screen pixels to px
123 // FIXME: after we switch to snapping dist in screen pixels, this will be unnecessary
124 if (SP_ACTIVE_DESKTOP) {
125 scaled_spacing /= SP_ACTIVE_DESKTOP->current_zoom();
126 }
128 NR::Coord const rounded = round_to_nearest_multiple_plus(p[0], scaled_spacing, griditem->origin[0]);
130 /*
131 int a = round(scaled_spacing);
132 int b = round(p[0]);
133 int c = round(rounded);
135 g_message("hier %d; %d; %d",a,b,c);
136 */
138 s.push_back(std::make_pair(NR::Dim2(0), rounded));
141 return s;
142 }
146 /*
147 Local Variables:
148 mode:c++
149 c-file-style:"stroustrup"
150 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
151 indent-tabs-mode:nil
152 fill-column:99
153 End:
154 */
155 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :