2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2023-06-15 22:08:35 -04:00
|
|
|
#include <cmath>
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/line_set.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
|
|
|
class LineSorter {
|
|
|
|
public:
|
2014-06-09 15:39:19 -04:00
|
|
|
bool operator() (LineSet::Line const & a, LineSet::Line const & b) {
|
|
|
|
return a.pos < b.pos;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
LineSet::LineSet (Canvas* c, Orientation o)
|
|
|
|
: Item (c)
|
|
|
|
, _extent (0)
|
|
|
|
, _orientation (o)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
LineSet::LineSet (Item* parent, Orientation o)
|
|
|
|
: Item (parent)
|
2014-06-09 15:39:19 -04:00
|
|
|
, _extent (0)
|
|
|
|
, _orientation (o)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::compute_bounding_box () const
|
|
|
|
{
|
|
|
|
if (_lines.empty ()) {
|
2017-01-19 14:54:24 -05:00
|
|
|
_bounding_box = Rect ();
|
2013-10-24 17:13:22 -04:00
|
|
|
} else {
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
if (_orientation == Horizontal) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-06-15 22:08:35 -04:00
|
|
|
double y0 = _lines.front().pos - (_lines.front().width/2.0);
|
2023-09-25 21:51:33 -04:00
|
|
|
double y1 = _lines.back().pos + (_lines.back().width/2.0);
|
2023-06-15 22:08:35 -04:00
|
|
|
|
|
|
|
if (fmod (_lines.front().width, 2.)) {
|
|
|
|
y0 -= _lines.front().width * 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bounding_box = Rect (0, y0, _extent, y1);
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
} else {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-06-15 22:08:35 -04:00
|
|
|
double x0 = _lines.front().pos - _lines.front().width/2.0;
|
|
|
|
double x1 = _lines.back().pos + _lines.back().width/2.0;
|
|
|
|
|
|
|
|
if (fmod (_lines.front().width, 2.)) {
|
|
|
|
x0 -= _lines.front().width * 0.5;
|
|
|
|
}
|
|
|
|
|
|
|
|
_bounding_box = Rect (x0, 0, x1, _extent);
|
2014-06-09 15:39:19 -04:00
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2014-06-09 15:39:19 -04:00
|
|
|
|
2022-04-27 00:02:22 -04:00
|
|
|
set_bbox_clean ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-09 15:39:19 -04:00
|
|
|
LineSet::set_extent (Distance e)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
_extent = e;
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-06-09 15:39:19 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
2013-10-22 21:12:47 -04:00
|
|
|
/* area is in window coordinates */
|
|
|
|
|
2023-04-09 19:07:57 -04:00
|
|
|
for (auto const & l : _lines) {
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
Rect self;
|
2023-07-17 17:28:36 -04:00
|
|
|
const double shift = 0.5;
|
2023-06-15 22:08:35 -04:00
|
|
|
|
2014-06-09 15:39:19 -04:00
|
|
|
if (_orientation == Horizontal) {
|
2023-06-15 22:08:35 -04:00
|
|
|
self = Rect (0, l.pos - (l.width/2.0), _extent, l.pos + (l.width/2.0));
|
|
|
|
if (fmod (l.width, 2.)) {
|
2023-09-25 22:01:21 -04:00
|
|
|
self.y0 += shift;
|
2023-06-15 22:08:35 -04:00
|
|
|
}
|
2014-06-09 15:39:19 -04:00
|
|
|
} else {
|
2023-06-15 22:08:35 -04:00
|
|
|
self = Rect (l.pos - (l.width/2.0), 0, l.pos + (l.width/2.0), _extent);
|
|
|
|
if (fmod (l.width, 2.)) {
|
2023-09-25 22:01:21 -04:00
|
|
|
self.x0 += shift;
|
2023-06-15 22:08:35 -04:00
|
|
|
}
|
2014-06-09 15:39:19 -04:00
|
|
|
}
|
|
|
|
|
2023-06-16 11:16:53 -04:00
|
|
|
self = item_to_window (self);
|
2023-09-15 11:58:59 -04:00
|
|
|
Rect intersection = self.intersection (area);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-09-15 11:58:59 -04:00
|
|
|
if (!intersection) {
|
2015-10-05 10:17:49 -04:00
|
|
|
continue;
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2013-10-24 17:13:22 -04:00
|
|
|
|
2023-04-09 19:07:57 -04:00
|
|
|
Gtkmm2ext::set_source_rgba (context, l.color);
|
|
|
|
context->set_line_width (l.width);
|
2014-06-09 15:39:19 -04:00
|
|
|
|
|
|
|
/* Not 100% sure that the computation of the invariant
|
|
|
|
* positions (y and x) below work correctly if the line width
|
|
|
|
* is not 1.0, but visual inspection suggests it is OK.
|
2023-09-25 21:52:10 -04:00
|
|
|
* See doc/cairo-single-pixel-lines lines to understand why we add 0.5
|
2014-06-09 15:39:19 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (_orientation == Horizontal) {
|
2023-06-15 22:08:35 -04:00
|
|
|
const double y = item_to_window (Duple (0, l.pos)).y;
|
2023-07-17 22:54:33 -04:00
|
|
|
context->move_to (intersection.x0, y + shift);
|
|
|
|
context->line_to (intersection.x1, y + shift);
|
2014-06-09 15:39:19 -04:00
|
|
|
} else {
|
2023-06-15 22:08:35 -04:00
|
|
|
const double x = item_to_window (Duple (l.pos, 0)).x;
|
2023-07-17 22:54:33 -04:00
|
|
|
context->move_to (x + shift, intersection.y0);
|
|
|
|
context->line_to (x + shift, intersection.y1);
|
2014-06-09 15:39:19 -04:00
|
|
|
}
|
|
|
|
|
2013-10-24 17:13:22 -04:00
|
|
|
context->stroke ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-04-09 16:34:22 -04:00
|
|
|
LineSet::add_coord (Coord pos, Distance width, Gtkmm2ext::Color color)
|
|
|
|
{
|
|
|
|
_lines.push_back (Line (pos, width, color));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::begin_add ()
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
begin_change ();
|
2023-04-09 16:34:22 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2023-04-09 16:34:22 -04:00
|
|
|
void
|
|
|
|
LineSet::end_add ()
|
|
|
|
{
|
|
|
|
if (!_lines.empty()) {
|
|
|
|
sort (_lines.begin(), _lines.end(), LineSorter());
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2013-04-04 00:32:52 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LineSet::clear ()
|
|
|
|
{
|
|
|
|
begin_change ();
|
|
|
|
_lines.clear ();
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2013-04-04 00:32:52 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-11-04 11:56:10 -05:00
|
|
|
|
|
|
|
bool
|
|
|
|
LineSet::covers (Duple const & /*point*/) const
|
|
|
|
{
|
2014-06-09 15:39:19 -04:00
|
|
|
/* lines are ordered by position along primary axis, so binary search
|
|
|
|
* to find the place to start looking.
|
|
|
|
*
|
|
|
|
* XXX but not yet.
|
|
|
|
*/
|
|
|
|
|
2013-11-04 11:56:10 -05:00
|
|
|
return false;
|
|
|
|
}
|