1 #define __RUBBERBAND_C__
3 /**
4 * \file src/rubberband.cpp
5 * \brief Rubberbanding selector
6 *
7 * Author:
8 * Lauris Kaplinski <lauris@kaplinski.com>
9 *
10 * Copyright (C) 1999-2002 Lauris Kaplinski
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
15 #include "display/sodipodi-ctrlrect.h"
16 #include "desktop.h"
17 #include "desktop-handles.h"
18 #include "rubberband.h"
19 #include "display/canvas-bpath.h"
20 #include "display/curve.h"
21 #include "libnr/nr-point.h"
23 Inkscape::Rubberband *Inkscape::Rubberband::_instance = NULL;
25 Inkscape::Rubberband::Rubberband(SPDesktop *dt)
26 : _desktop(dt), _rect(NULL), _touchpath(NULL), _started(false)
27 {
28 _points.clear();
29 _mode = RUBBERBAND_MODE_RECT;
30 _touchpath_curve = new SPCurve();
31 }
33 void Inkscape::Rubberband::delete_canvas_items()
34 {
35 if (_rect) {
36 GtkObject *temp = _rect;
37 _rect = NULL;
38 gtk_object_destroy(temp);
39 }
40 if (_touchpath) {
41 GtkObject *temp = _touchpath;
42 _touchpath = NULL;
43 gtk_object_destroy(temp);
44 }
45 }
48 void Inkscape::Rubberband::start(SPDesktop *d, NR::Point const &p)
49 {
50 _points.clear();
51 _touchpath_curve->reset();
52 delete_canvas_items();
53 _desktop = d;
54 _start = p;
55 _started = true;
56 _points.push_back(_desktop->d2w(p));
57 _touchpath_curve->moveto(p);
59 sp_canvas_force_full_redraw_after_interruptions(_desktop->canvas, 5);
60 }
62 void Inkscape::Rubberband::stop()
63 {
64 _started = false;
65 _mode = RUBBERBAND_MODE_RECT; // restore the default
67 _points.clear();
68 _touchpath_curve->reset();
70 delete_canvas_items();
72 if (_desktop)
73 sp_canvas_end_forced_full_redraws(_desktop->canvas);
74 }
76 void Inkscape::Rubberband::move(NR::Point const &p)
77 {
78 if (!_started)
79 return;
81 _end = p;
82 _desktop->scroll_to_point(&p);
83 _touchpath_curve->lineto(p);
85 NR::Point next = _desktop->d2w(p);
86 // we want the points to be at most 0.5 screen pixels apart,
87 // so that we don't lose anything small;
88 // if they are farther apart, we interpolate more points
89 if (_points.size() > 0 && NR::L2(next-_points.back()) > 0.5) {
90 NR::Point prev = _points.back();
91 int subdiv = 2 * (int) round(NR::L2(next-prev) + 0.5);
92 for (int i = 1; i <= subdiv; i ++) {
93 _points.push_back(prev + ((double)i/subdiv) * (next - prev));
94 }
95 } else {
96 _points.push_back(next);
97 }
99 if (_mode == RUBBERBAND_MODE_RECT) {
100 if (_rect == NULL) {
101 _rect = static_cast<CtrlRect *>(sp_canvas_item_new(sp_desktop_controls(_desktop), SP_TYPE_CTRLRECT, NULL));
102 }
103 _rect->setRectangle(NR::Rect(_start, _end));
105 sp_canvas_item_show(_rect);
106 if (_touchpath)
107 sp_canvas_item_hide(_touchpath);
109 } else if (_mode == RUBBERBAND_MODE_TOUCHPATH) {
110 if (_touchpath == NULL) {
111 _touchpath = sp_canvas_bpath_new(sp_desktop_sketch(_desktop), NULL);
112 sp_canvas_bpath_set_stroke(SP_CANVAS_BPATH(_touchpath), 0xff0000ff, 1.0, SP_STROKE_LINEJOIN_MITER, SP_STROKE_LINECAP_BUTT);
113 sp_canvas_bpath_set_fill(SP_CANVAS_BPATH(_touchpath), 0, SP_WIND_RULE_NONZERO);
114 }
115 sp_canvas_bpath_set_bpath(SP_CANVAS_BPATH(_touchpath), _touchpath_curve);
117 sp_canvas_item_show(_touchpath);
118 if (_rect)
119 sp_canvas_item_hide(_rect);
120 }
121 }
123 void Inkscape::Rubberband::setMode(int mode)
124 {
125 _mode = mode;
126 }
128 boost::optional<NR::Rect> Inkscape::Rubberband::getRectangle() const
129 {
130 if (!_started) {
131 return boost::optional<NR::Rect>();
132 }
134 return NR::Rect(_start, _end);
135 }
137 Inkscape::Rubberband *Inkscape::Rubberband::get(SPDesktop *desktop)
138 {
139 if (_instance == NULL) {
140 _instance = new Inkscape::Rubberband(desktop);
141 }
143 return _instance;
144 }
146 bool Inkscape::Rubberband::is_started()
147 {
148 return _started;
149 }
151 /*
152 Local Variables:
153 mode:c++
154 c-file-style:"stroustrup"
155 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
156 indent-tabs-mode:nil
157 fill-column:99
158 End:
159 */
160 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :