X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fcanvas%2Farrow.cc;h=f95c6ecc8ff81bab6b035cfab60cb1622b066b6b;hb=af30a6f001f0758155b3ece040fc2baa643a29de;hp=f82f2d96deda13bb26556dc149063db73c0573f7;hpb=fa71d82dda08558caf4a9c5102016f2746883d10;p=ardour.git diff --git a/libs/canvas/arrow.cc b/libs/canvas/arrow.cc index f82f2d96de..f95c6ecc8f 100644 --- a/libs/canvas/arrow.cc +++ b/libs/canvas/arrow.cc @@ -1,28 +1,32 @@ /* - Copyright (C) 2011 Paul Davis - Author: Carl Hetherington - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -*/ + * Copyright (C) 2012 Carl Hetherington + * Copyright (C) 2013-2014 Paul Davis + * Copyright (C) 2015-2017 Robin Gareus + * Copyright (C) 2015 David Robillard + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ /** @file canvas/arrow.cc * @brief Implementation of the Arrow canvas object. */ +#include "pbd/compose.h" + #include "canvas/arrow.h" +#include "canvas/debug.h" #include "canvas/polygon.h" #include "canvas/line.h" @@ -31,22 +35,51 @@ using namespace ArdourCanvas; /** Construct an Arrow. * @param parent Parent canvas group. */ -Arrow::Arrow (Group* parent) - : Group (parent) +Arrow::Arrow (Canvas* c) + : Container (c) { - assert (parent); + setup (); +} + +Arrow::Arrow (Item* parent) + : Container (parent) +{ + setup (); +} +void +Arrow::setup () +{ /* set up default arrow heads at each end */ for (int i = 0; i < 2; ++i) { _heads[i].polygon = new Polygon (this); - _heads[i].show = true; _heads[i].outward = true; _heads[i].width = 4; _heads[i].height = 4; setup_polygon (i); + CANVAS_DEBUG_NAME (_heads[i].polygon, string_compose ("arrow head %1", i)); } - + _line = new Line (this); + CANVAS_DEBUG_NAME (_line, "arrow line"); +} + +void +Arrow::compute_bounding_box () const +{ + /* Compute our bounding box manually rather than using the default + container algorithm, since having the bounding box with origin other + than zero causes strange problems for mysterious reasons. */ + + const double outline_pad = 0.5 + (_line->outline_width() / 2.0); + const double head_width = std::max(_heads[0].width, _heads[1].width); + + _bounding_box = Rect(0, + 0, + _line->x1() + (head_width / 2.0) + outline_pad, + _line->y1()); + + _bounding_box_dirty = false; } /** Set whether to show an arrow head at one end or other @@ -58,12 +91,16 @@ void Arrow::set_show_head (int which, bool show) { assert (which == 0 || which == 1); - + begin_change (); - - _heads[which].show = show; - setup_polygon (which); + if (!show) { + delete _heads[which].polygon; + _heads[which].polygon = 0; + } else { + setup_polygon (which); + } + _bounding_box_dirty = true; end_change (); } @@ -78,7 +115,7 @@ void Arrow::set_head_outward (int which, bool outward) { assert (which == 0 || which == 1); - + begin_change (); _heads[which].outward = outward; @@ -96,9 +133,9 @@ void Arrow::set_head_height (int which, Distance height) { assert (which == 0 || which == 1); - + begin_change (); - + _heads[which].height = height; setup_polygon (which); @@ -114,9 +151,9 @@ void Arrow::set_head_width (int which, Distance width) { assert (which == 0 || which == 1); - + begin_change (); - + _heads[which].width = width; setup_polygon (which); @@ -131,8 +168,13 @@ void Arrow::set_outline_width (Distance width) { _line->set_outline_width (width); - _heads[0].polygon->set_outline_width (width); - _heads[1].polygon->set_outline_width (width); + if (_heads[0].polygon) { + _heads[0].polygon->set_outline_width (width); + } + if (_heads[1].polygon) { + _heads[1].polygon->set_outline_width (width); + } + _bounding_box_dirty = true; } /** Set the x position of our line. @@ -144,9 +186,11 @@ Arrow::set_x (Coord x) _line->set_x0 (x); _line->set_x1 (x); for (int i = 0; i < 2; ++i) { - _heads[i].polygon->set_x_position (x - _heads[i].width / 2); + if (_heads[i].polygon) { + _heads[i].polygon->set_x_position (x - _heads[i].width / 2); + } } - + _bounding_box_dirty = true; } /** Set the y position of end 0 of our line. @@ -156,7 +200,10 @@ void Arrow::set_y0 (Coord y0) { _line->set_y0 (y0); - _heads[0].polygon->set_y_position (y0); + if (_heads[0].polygon) { + _heads[0].polygon->set_y_position (y0); + } + _bounding_box_dirty = true; } /** Set the y position of end 1 of our line. @@ -166,7 +213,10 @@ void Arrow::set_y1 (Coord y1) { _line->set_y1 (y1); - _heads[1].polygon->set_y_position (y1 - _heads[1].height); + if (_heads[1].polygon) { + _heads[1].polygon->set_y_position (y1 - _heads[1].height); + } + _bounding_box_dirty = true; } /** @return x position of our line in pixels (in our coordinate system) */ @@ -190,7 +240,7 @@ void Arrow::setup_polygon (int which) { assert (which == 0 || which == 1); - + Points points; if ((which == 0 && _heads[which].outward) || (which == 1 && !_heads[which].outward)) { @@ -213,11 +263,30 @@ Arrow::setup_polygon (int which) * @param color New color. */ void -Arrow::set_color (Color color) +Arrow::set_color (Gtkmm2ext::Color color) { _line->set_outline_color (color); for (int i = 0; i < 2; ++i) { - _heads[i].polygon->set_outline_color (color); - _heads[i].polygon->set_fill_color (color); + if (_heads[i].polygon) { + _heads[i].polygon->set_outline_color (color); + _heads[i].polygon->set_fill_color (color); + } } } + +bool +Arrow::covers (Duple const & point) const +{ + if (_heads[0].polygon && _heads[0].polygon->covers (point)) { + return true; + } + if (_line && _line->covers (point)) { + return true; + } + + if (_heads[1].polygon && _heads[1].polygon->covers (point)) { + return true; + } + + return false; +}