fix crash when copy'ing latent plugins
[ardour.git] / libs / canvas / line.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21 #include <cairomm/context.h>
22 #include "pbd/compose.h"
23 #include "canvas/line.h"
24 #include "canvas/types.h"
25 #include "canvas/debug.h"
26 #include "canvas/utils.h"
27 #include "canvas/canvas.h"
28
29 using namespace std;
30 using namespace ArdourCanvas;
31
32 Line::Line (Canvas* c)
33         : Item (c)
34 {
35 }
36
37 Line::Line (Item* parent)
38         : Item (parent)
39 {
40 }
41
42 void
43 Line::compute_bounding_box () const
44 {
45         Rect bbox;
46
47         bbox.x0 = min (_points[0].x, _points[1].x);
48         bbox.y0 = min (_points[0].y, _points[1].y);
49         bbox.x1 = max (_points[0].x, _points[1].x);
50         bbox.y1 = max (_points[0].y, _points[1].y);
51
52         bbox = bbox.expand (0.5 + (_outline_width / 2));
53
54         _bounding_box = bbox;
55         _bounding_box_dirty = false;
56 }
57
58 void
59 Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
60 {
61         setup_outline_context (context);
62
63         Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y));
64         Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y));
65
66         if (_outline_width <= 1.0) {
67                 /* See Cairo FAQ on single pixel lines to understand why we add 0.5
68                  */
69
70                 const Duple half_a_pixel (0.5, 0.5);
71                 p0 = p0.translate (half_a_pixel);
72                 p1 = p1.translate (half_a_pixel);
73         }
74
75         context->move_to (p0.x, p0.y);
76         context->line_to (p1.x, p1.y);
77         context->stroke ();
78 }
79
80 void
81 Line::set (Duple a, Duple b)
82 {
83         if (a != _points[0] || b != _points[1]) {
84                 begin_change ();
85
86                 _points[0] = a;
87                 _points[1] = b;
88
89                 _bounding_box_dirty = true;
90                 end_change ();
91         }
92 }
93
94 void
95 Line::set_x (Coord x0, Coord x1)
96 {
97         if (x0 != _points[0].x || x1 != _points[1].x) {
98                 begin_change ();
99
100                 _points[0].x = x0;
101                 _points[1].x = x1;
102
103                 _bounding_box_dirty = true;
104                 end_change ();
105         }
106 }
107
108 void
109 Line::set_x0 (Coord x0)
110 {
111         if (x0 != _points[0].x) {
112                 begin_change ();
113
114                 _points[0].x = x0;
115
116                 _bounding_box_dirty = true;
117                 end_change ();
118         }
119 }
120
121 void
122 Line::set_y0 (Coord y0)
123 {
124         if (y0 != _points[0].y) {
125                 begin_change ();
126
127                 _points[0].y = y0;
128
129                 _bounding_box_dirty = true;
130                 end_change ();
131         }
132
133         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
134 }
135
136 void
137 Line::set_x1 (Coord x1)
138 {
139         if (x1 != _points[1].x) {
140                 begin_change ();
141
142                 _points[1].x = x1;
143
144                 _bounding_box_dirty = true;
145                 end_change ();
146         }
147 }
148
149 void
150 Line::set_y1 (Coord y1)
151 {
152         if (y1 != _points[1].y) {
153                 begin_change ();
154
155                 _points[1].y = y1;
156
157                 _bounding_box_dirty = true;
158                 end_change ();
159         }
160 }
161
162 bool
163 Line::covers (Duple const & point) const
164 {
165         const Duple p = window_to_item (point);
166         static const Distance threshold = 2.0;
167
168         /* this quick check works for vertical and horizontal lines, which are
169          * common.
170          */
171
172         if (_points[0].x == _points[1].x) {
173                 /* line is vertical, just check x coordinate */
174                 return fabs (_points[0].x - p.x) <= threshold;
175         }
176
177         if (_points[0].y == _points[1].y) {
178                 /* line is horizontal, just check y coordinate */
179                 return fabs (_points[0].y - p.y) <= threshold;
180         }
181
182         Duple at;
183         double t;
184         Duple a (_points[0]);
185         Duple b (_points[1]);
186         const Rect visible (window_to_item (_canvas->visible_area()));
187
188         /*
189            Clamp the line endpoints to the visible area of the canvas. If we do
190            not do this, we have a line segment extending to COORD_MAX and our
191            math goes wrong.
192         */
193
194         a.x = min (a.x, visible.x1);
195         a.y = min (a.y, visible.y1);
196         b.x = min (b.x, visible.x1);
197         b.y = min (b.y, visible.y1);
198
199         double d = distance_to_segment_squared (p, a, b, t, at);
200
201         if (t < 0.0 || t > 1.0) {
202                 return false;
203         }
204
205         if (d < threshold) {
206                 return true;
207         }
208
209         return false;
210 }