merge with master and fix 4 conflicts by hand
[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 (Group* parent)
33         : Item (parent)
34         , Outline (parent)
35 {
36
37 }
38
39 void
40 Line::compute_bounding_box () const
41 {
42         Rect bbox;
43         
44         bbox.x0 = min (_points[0].x, _points[1].x);
45         bbox.y0 = min (_points[0].y, _points[1].y);
46         bbox.x1 = max (_points[0].x, _points[1].x);
47         bbox.y1 = max (_points[0].y, _points[1].y);
48
49         bbox = bbox.expand (0.5 + (_outline_width / 2));
50
51         _bounding_box = bbox;
52         _bounding_box_dirty = false;
53 }
54
55 void
56 Line::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
57 {
58         setup_outline_context (context);
59
60         Duple p0 = item_to_window (Duple (_points[0].x, _points[0].y));
61         Duple p1 = item_to_window (Duple (_points[1].x, _points[1].y));
62
63         if (_outline_width <= 1.0) {
64                 /* See Cairo FAQ on single pixel lines to understand why we add 0.5
65                  */
66                 
67                 const Duple half_a_pixel (0.5, 0.5);
68                 p0 = p0.translate (half_a_pixel);
69                 p1 = p1.translate (half_a_pixel);
70         }
71
72         context->move_to (p0.x, p0.y);
73         context->line_to (p1.x, p1.y);
74         context->stroke ();
75 }
76
77 void
78 Line::set (Duple a, Duple b)
79 {
80         if (a != _points[0] || b != _points[1]) {
81                 begin_change ();
82                 
83                 _points[0] = a;
84                 _points[1] = b;
85                 
86                 _bounding_box_dirty = true;
87                 end_change ();
88         }
89 }
90
91 void
92 Line::set_x (Coord x0, Coord x1)
93 {
94         if (x0 != _points[0].x || x1 != _points[1].x) {
95                 begin_change ();
96                 
97                 _points[0].x = x0;
98                 _points[1].x = x1;
99                 
100                 _bounding_box_dirty = true;
101                 end_change ();
102         }
103 }       
104
105 void
106 Line::set_x0 (Coord x0)
107 {
108         if (x0 != _points[0].x) {
109                 begin_change ();
110                 
111                 _points[0].x = x0;
112                 
113                 _bounding_box_dirty = true;
114                 end_change ();
115         }
116 }
117
118 void
119 Line::set_y0 (Coord y0)
120 {
121         if (y0 != _points[0].y) {
122                 begin_change ();
123                 
124                 _points[0].y = y0;
125                 
126                 _bounding_box_dirty = true;
127                 end_change ();
128         }
129
130         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: line change\n");
131 }
132
133 void
134 Line::set_x1 (Coord x1)
135 {
136         if (x1 != _points[1].x) {
137                 begin_change ();
138                 
139                 _points[1].x = x1;
140                 
141                 _bounding_box_dirty = true;
142                 end_change ();
143         }
144 }
145
146 void
147 Line::set_y1 (Coord y1)
148 {
149         if (y1 != _points[1].y) {
150                 begin_change ();
151                 
152                 _points[1].y = y1;
153                 
154                 _bounding_box_dirty = true;
155                 end_change ();
156         }
157 }
158
159 bool
160 Line::covers (Duple const & point) const
161 {
162         const Duple p = canvas_to_item (point);
163         static const Distance threshold = 2.0;
164
165         /* this quick check works for vertical and horizontal lines, which are
166          * common.
167          */
168
169         if (_points[0].x == _points[1].x) {
170                 /* line is vertical, just check x coordinate */
171                 return fabs (_points[0].x - p.x) <= threshold;
172         }
173
174         if (_points[0].y == _points[1].y) {
175                 /* line is horizontal, just check y coordinate */
176                 return fabs (_points[0].y - p.y) <= threshold;
177         }
178
179         Duple at;
180         double t;
181         Duple a (_points[0]);
182         Duple b (_points[1]);
183         const Rect visible (_canvas->visible_area());
184
185         /*
186            Clamp the line endpoints to the visible area of the canvas. If we do
187            not do this, we have a line segment extending to COORD_MAX and our
188            math goes wrong.
189         */
190
191         a.x = min (a.x, visible.x1);
192         a.y = min (a.y, visible.y1);
193         b.x = min (b.x, visible.x1);
194         b.y = min (b.y, visible.y1);
195
196         double d = distance_to_segment_squared (p, a, b, t, at);
197
198         if (t < 0.0 || t > 1.0) {
199                 return false;
200         }
201
202         if (d < threshold) {
203                 return true;
204         }
205
206         return false;
207 }