2 * Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2013-2014 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
5 * Copyright (C) 2015 David Robillard <d@drobilla.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /** @file canvas/arrow.cc
23 * @brief Implementation of the Arrow canvas object.
26 #include "pbd/compose.h"
28 #include "canvas/arrow.h"
29 #include "canvas/debug.h"
30 #include "canvas/polygon.h"
31 #include "canvas/line.h"
33 using namespace ArdourCanvas;
35 /** Construct an Arrow.
36 * @param parent Parent canvas group.
38 Arrow::Arrow (Canvas* c)
44 Arrow::Arrow (Item* parent)
53 /* set up default arrow heads at each end */
54 for (int i = 0; i < 2; ++i) {
55 _heads[i].polygon = new Polygon (this);
56 _heads[i].outward = true;
60 CANVAS_DEBUG_NAME (_heads[i].polygon, string_compose ("arrow head %1", i));
63 _line = new Line (this);
64 CANVAS_DEBUG_NAME (_line, "arrow line");
68 Arrow::compute_bounding_box () const
70 /* Compute our bounding box manually rather than using the default
71 container algorithm, since having the bounding box with origin other
72 than zero causes strange problems for mysterious reasons. */
74 const double outline_pad = 0.5 + (_line->outline_width() / 2.0);
75 const double head_width = std::max(_heads[0].width, _heads[1].width);
77 _bounding_box = Rect(0,
79 _line->x1() + (head_width / 2.0) + outline_pad,
82 _bounding_box_dirty = false;
85 /** Set whether to show an arrow head at one end or other
87 * @param which 0 or 1 to specify the arrow head to set up.
88 * @param true if this arrow head should be shown.
91 Arrow::set_show_head (int which, bool show)
93 assert (which == 0 || which == 1);
98 delete _heads[which].polygon;
99 _heads[which].polygon = 0;
101 setup_polygon (which);
104 _bounding_box_dirty = true;
108 /** Set whether a given arrow head points into the line or
110 * @param which 0 or 1 to specify the arrow head to set up.
111 * @param true if this arrow head should point out from the line,
112 * otherwise false to point in.
115 Arrow::set_head_outward (int which, bool outward)
117 assert (which == 0 || which == 1);
121 _heads[which].outward = outward;
123 setup_polygon (which);
124 _bounding_box_dirty = true;
128 /** Set the height of a given arrow head.
129 * @param which 0 or 1 to specify the arrow head to set up.
130 * @param height Height of the arrow head in pixels.
133 Arrow::set_head_height (int which, Distance height)
135 assert (which == 0 || which == 1);
139 _heads[which].height = height;
141 setup_polygon (which);
142 _bounding_box_dirty = true;
146 /** Set the width of a given arrow head.
147 * @param which 0 or 1 to specify the arrow head to set up.
148 * @param width Width of the arrow head in pixels.
151 Arrow::set_head_width (int which, Distance width)
153 assert (which == 0 || which == 1);
157 _heads[which].width = width;
159 setup_polygon (which);
160 _bounding_box_dirty = true;
164 /** Set the width of our line, and the outline of our arrow(s).
165 * @param width New width in pixels.
168 Arrow::set_outline_width (Distance width)
170 _line->set_outline_width (width);
171 if (_heads[0].polygon) {
172 _heads[0].polygon->set_outline_width (width);
174 if (_heads[1].polygon) {
175 _heads[1].polygon->set_outline_width (width);
177 _bounding_box_dirty = true;
180 /** Set the x position of our line.
181 * @param x New x position in pixels (in our coordinate system).
184 Arrow::set_x (Coord x)
188 for (int i = 0; i < 2; ++i) {
189 if (_heads[i].polygon) {
190 _heads[i].polygon->set_x_position (x - _heads[i].width / 2);
193 _bounding_box_dirty = true;
196 /** Set the y position of end 0 of our line.
197 * @param y0 New y0 position in pixels (in our coordinate system).
200 Arrow::set_y0 (Coord y0)
203 if (_heads[0].polygon) {
204 _heads[0].polygon->set_y_position (y0);
206 _bounding_box_dirty = true;
209 /** Set the y position of end 1 of our line.
210 * @param y1 New y1 position in pixels (in our coordinate system).
213 Arrow::set_y1 (Coord y1)
216 if (_heads[1].polygon) {
217 _heads[1].polygon->set_y_position (y1 - _heads[1].height);
219 _bounding_box_dirty = true;
222 /** @return x position of our line in pixels (in our coordinate system) */
229 /** @return y position of end 1 of our line in pixels (in our coordinate system) */
236 /** Set up the polygon used to represent a particular arrow head.
237 * @param which 0 or 1 to specify the arrow head to set up.
240 Arrow::setup_polygon (int which)
242 assert (which == 0 || which == 1);
246 if ((which == 0 && _heads[which].outward) || (which == 1 && !_heads[which].outward)) {
247 /* this is an arrow head pointing towards -ve y */
248 points.push_back (Duple (_heads[which].width / 2, 0));
249 points.push_back (Duple (_heads[which].width, _heads[which].height));
250 points.push_back (Duple (0, _heads[which].height));
252 /* this is an arrow head pointing towards +ve y */
253 points.push_back (Duple (0, 0));
254 points.push_back (Duple (_heads[which].width, 0));
255 points.push_back (Duple (_heads[which].width / 2, _heads[which].height));
256 points.push_back (Duple (0, 0));
259 _heads[which].polygon->set (points);
262 /** Set the color of our line and arrow heads.
263 * @param color New color.
266 Arrow::set_color (Gtkmm2ext::Color color)
268 _line->set_outline_color (color);
269 for (int i = 0; i < 2; ++i) {
270 if (_heads[i].polygon) {
271 _heads[i].polygon->set_outline_color (color);
272 _heads[i].polygon->set_fill_color (color);
278 Arrow::covers (Duple const & point) const
280 if (_heads[0].polygon && _heads[0].polygon->covers (point)) {
283 if (_line && _line->covers (point)) {
287 if (_heads[1].polygon && _heads[1].polygon->covers (point)) {