fix crash when copy'ing latent plugins
[ardour.git] / libs / canvas / arrow.cc
1 /*
2     Copyright (C) 2011 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
21 /** @file  canvas/arrow.cc
22  *  @brief Implementation of the Arrow canvas object.
23  */
24
25 #include "pbd/compose.h"
26
27 #include "canvas/arrow.h"
28 #include "canvas/debug.h"
29 #include "canvas/polygon.h"
30 #include "canvas/line.h"
31
32 using namespace ArdourCanvas;
33
34 /** Construct an Arrow.
35  *  @param parent Parent canvas group.
36  */
37 Arrow::Arrow (Canvas* c)
38         : Container (c)
39 {
40         setup ();
41 }
42
43 Arrow::Arrow (Item* parent)
44         : Container (parent)
45 {
46         setup ();
47 }
48
49 void
50 Arrow::setup ()
51 {
52         /* set up default arrow heads at each end */
53         for (int i = 0; i < 2; ++i) {
54                 _heads[i].polygon = new Polygon (this);
55                 _heads[i].outward = true;
56                 _heads[i].width = 4;
57                 _heads[i].height = 4;
58                 setup_polygon (i);
59                 CANVAS_DEBUG_NAME (_heads[i].polygon, string_compose ("arrow head %1", i));
60         }
61
62         _line = new Line (this);
63         CANVAS_DEBUG_NAME (_line, "arrow line");
64 }
65
66 void
67 Arrow::compute_bounding_box () const
68 {
69         /* Compute our bounding box manually rather than using the default
70            container algorithm, since having the bounding box with origin other
71            than zero causes strange problems for mysterious reasons. */
72
73         const double outline_pad = 0.5 + (_line->outline_width() / 2.0);
74         const double head_width  = std::max(_heads[0].width, _heads[1].width);
75
76         _bounding_box = Rect(0,
77                              0,
78                              _line->x1() + (head_width / 2.0) + outline_pad,
79                              _line->y1());
80
81         _bounding_box_dirty = false;
82 }
83
84 /** Set whether to show an arrow head at one end or other
85  *  of the line.
86  *  @param which 0 or 1 to specify the arrow head to set up.
87  *  @param true if this arrow head should be shown.
88  */
89 void
90 Arrow::set_show_head (int which, bool show)
91 {
92         assert (which == 0 || which == 1);
93
94         begin_change ();
95
96         if (!show) {
97                 delete _heads[which].polygon;
98                 _heads[which].polygon = 0;
99         } else {
100                 setup_polygon (which);
101         }
102
103         _bounding_box_dirty = true;
104         end_change ();
105 }
106
107 /** Set whether a given arrow head points into the line or
108  *  away from it.
109  *  @param which 0 or 1 to specify the arrow head to set up.
110  *  @param true if this arrow head should point out from the line,
111  *  otherwise false to point in.
112  */
113 void
114 Arrow::set_head_outward (int which, bool outward)
115 {
116         assert (which == 0 || which == 1);
117
118         begin_change ();
119
120         _heads[which].outward = outward;
121
122         setup_polygon (which);
123         _bounding_box_dirty = true;
124         end_change ();
125 }
126
127 /** Set the height of a given arrow head.
128  *  @param which 0 or 1 to specify the arrow head to set up.
129  *  @param height Height of the arrow head in pixels.
130  */
131 void
132 Arrow::set_head_height (int which, Distance height)
133 {
134         assert (which == 0 || which == 1);
135
136         begin_change ();
137
138         _heads[which].height = height;
139
140         setup_polygon (which);
141         _bounding_box_dirty = true;
142         end_change ();
143 }
144
145 /** Set the width of a given arrow head.
146  *  @param which 0 or 1 to specify the arrow head to set up.
147  *  @param width Width of the arrow head in pixels.
148  */
149 void
150 Arrow::set_head_width (int which, Distance width)
151 {
152         assert (which == 0 || which == 1);
153
154         begin_change ();
155
156         _heads[which].width = width;
157
158         setup_polygon (which);
159         _bounding_box_dirty = true;
160         end_change ();
161 }
162
163 /** Set the width of our line, and the outline of our arrow(s).
164  *  @param width New width in pixels.
165  */
166 void
167 Arrow::set_outline_width (Distance width)
168 {
169         _line->set_outline_width (width);
170         if (_heads[0].polygon) {
171                 _heads[0].polygon->set_outline_width (width);
172         }
173         if (_heads[1].polygon) {
174                 _heads[1].polygon->set_outline_width (width);
175         }
176         _bounding_box_dirty = true;
177 }
178
179 /** Set the x position of our line.
180  *  @param x New x position in pixels (in our coordinate system).
181  */
182 void
183 Arrow::set_x (Coord x)
184 {
185         _line->set_x0 (x);
186         _line->set_x1 (x);
187         for (int i = 0; i < 2; ++i) {
188                 if (_heads[i].polygon) {
189                         _heads[i].polygon->set_x_position (x - _heads[i].width / 2);
190                 }
191         }
192         _bounding_box_dirty = true;
193 }
194
195 /** Set the y position of end 0 of our line.
196  *  @param y0 New y0 position in pixels (in our coordinate system).
197  */
198 void
199 Arrow::set_y0 (Coord y0)
200 {
201         _line->set_y0 (y0);
202         if (_heads[0].polygon) {
203                 _heads[0].polygon->set_y_position (y0);
204         }
205         _bounding_box_dirty = true;
206 }
207
208 /** Set the y position of end 1 of our line.
209  *  @param y1 New y1 position in pixels (in our coordinate system).
210  */
211 void
212 Arrow::set_y1 (Coord y1)
213 {
214         _line->set_y1 (y1);
215         if (_heads[1].polygon) {
216                 _heads[1].polygon->set_y_position (y1 - _heads[1].height);
217         }
218         _bounding_box_dirty = true;
219 }
220
221 /** @return x position of our line in pixels (in our coordinate system) */
222 Coord
223 Arrow::x () const
224 {
225         return _line->x0 ();
226 }
227
228 /** @return y position of end 1 of our line in pixels (in our coordinate system) */
229 Coord
230 Arrow::y1 () const
231 {
232         return _line->y1 ();
233 }
234
235 /** Set up the polygon used to represent a particular arrow head.
236  *  @param which 0 or 1 to specify the arrow head to set up.
237  */
238 void
239 Arrow::setup_polygon (int which)
240 {
241         assert (which == 0 || which == 1);
242
243         Points points;
244
245         if ((which == 0 && _heads[which].outward) || (which == 1 && !_heads[which].outward)) {
246                 /* this is an arrow head pointing towards -ve y */
247                 points.push_back (Duple (_heads[which].width / 2, 0));
248                 points.push_back (Duple (_heads[which].width, _heads[which].height));
249                 points.push_back (Duple (0, _heads[which].height));
250         } else {
251                 /* this is an arrow head pointing towards +ve y */
252                 points.push_back (Duple (0, 0));
253                 points.push_back (Duple (_heads[which].width, 0));
254                 points.push_back (Duple (_heads[which].width / 2, _heads[which].height));
255                 points.push_back (Duple (0, 0));
256         }
257
258         _heads[which].polygon->set (points);
259 }
260
261 /** Set the color of our line and arrow heads.
262  *  @param color New color.
263  */
264 void
265 Arrow::set_color (Color color)
266 {
267         _line->set_outline_color (color);
268         for (int i = 0; i < 2; ++i) {
269                 if (_heads[i].polygon) {
270                         _heads[i].polygon->set_outline_color (color);
271                         _heads[i].polygon->set_fill_color (color);
272                 }
273         }
274 }
275
276 bool
277 Arrow::covers (Duple const & point) const
278 {
279         if (_heads[0].polygon && _heads[0].polygon->covers (point)) {
280                 return true;
281         }
282         if (_line && _line->covers (point)) {
283                 return true;
284         }
285
286         if (_heads[1].polygon && _heads[1].polygon->covers (point)) {
287                 return true;
288         }
289
290         return false;
291 }