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 "inkscape.h"
18 #include "desktop-handles.h"
19 #include "rubberband.h"
20 #include "display/canvas-bpath.h"
21 #include "display/curve.h"
22 #include "libnr/nr-point.h"
24 Inkscape::Rubberband *Inkscape::Rubberband::_instance = NULL;
26 Inkscape::Rubberband::Rubberband()
27 : _desktop(SP_ACTIVE_DESKTOP), _rect(NULL), _touchpath(NULL), _started(false)
28 {
29 _points.clear();
30 _mode = RUBBERBAND_MODE_RECT;
31 _touchpath_curve = sp_curve_new_sized(2000);
32 }
34 void Inkscape::Rubberband::delete_canvas_items()
35 {
36 if (_rect) {
37 GtkObject *temp = _rect;
38 _rect = NULL;
39 gtk_object_destroy(temp);
40 }
41 if (_touchpath) {
42 GtkObject *temp = _touchpath;
43 _touchpath = NULL;
44 gtk_object_destroy(temp);
45 }
46 }
49 void Inkscape::Rubberband::start(SPDesktop *d, NR::Point const &p)
50 {
51 _points.clear();
52 sp_curve_reset(_touchpath_curve);
53 delete_canvas_items();
54 _desktop = d;
55 _start = p;
56 _started = true;
57 _points.push_back(_desktop->d2w(p));
58 sp_curve_moveto(_touchpath_curve, p);
60 sp_canvas_force_full_redraw_after_interruptions(_desktop->canvas, 5);
61 }
63 void Inkscape::Rubberband::stop()
64 {
65 _started = false;
66 _mode = RUBBERBAND_MODE_RECT; // restore the default
68 _points.clear();
69 sp_curve_reset(_touchpath_curve);
71 delete_canvas_items();
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 sp_curve_lineto (_touchpath_curve, 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 NR::Maybe<NR::Rect> Inkscape::Rubberband::getRectangle() const
129 {
130 if (!_started) {
131 return NR::Nothing();
132 }
134 return NR::Rect(_start, _end);
135 }
137 Inkscape::Rubberband *Inkscape::Rubberband::get()
138 {
139 if (_instance == NULL) {
140 _instance = new Inkscape::Rubberband;
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 :