fix crash when copy'ing latent plugins
[ardour.git] / libs / canvas / line_set.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 "canvas/line_set.h"
21 #include "canvas/utils.h"
22
23 using namespace std;
24 using namespace ArdourCanvas;
25
26 class LineSorter {
27 public:
28         bool operator() (LineSet::Line const & a, LineSet::Line const & b) {
29                 return a.pos < b.pos;
30         }
31 };
32
33 LineSet::LineSet (Canvas* c, Orientation o)
34         : Item (c)
35         , _extent (0)
36         , _orientation (o)
37 {
38
39 }
40
41 LineSet::LineSet (Item* parent, Orientation o)
42         : Item (parent)
43         , _extent (0)
44         , _orientation (o)
45 {
46
47 }
48
49 void
50 LineSet::compute_bounding_box () const
51 {
52         if (_lines.empty ()) {
53                 _bounding_box = boost::optional<Rect> ();
54         } else {
55
56                 if (_orientation == Horizontal) {
57
58                         _bounding_box = Rect (0, /* x0 */
59                                               _lines.front().pos - (_lines.front().width/2.0), /* y0 */
60                                               _extent, /* x1 */
61                                               _lines.back().pos - (_lines.back().width/2.0) /* y1 */
62                                 );
63
64                 } else {
65
66                         _bounding_box = Rect (_lines.front().pos - _lines.front().width/2.0, /* x0 */
67                                               0, /* y0 */
68                                               _lines.back().pos + _lines.back().width/2.0, /* x1 */
69                                               _extent /* y1 */
70                                 );
71                 }
72         }
73
74         _bounding_box_dirty = false;
75 }
76
77 void
78 LineSet::set_extent (Distance e)
79 {
80         begin_change ();
81
82         _extent = e;
83         _bounding_box_dirty = true;
84
85         end_change ();
86 }
87
88 void
89 LineSet::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
90 {
91         /* area is in window coordinates */
92
93         for (vector<Line>::const_iterator i = _lines.begin(); i != _lines.end(); ++i) {
94
95                 Rect self;
96
97                 if (_orientation == Horizontal) {
98                         self = item_to_window (Rect (0, i->pos - (i->width/2.0), _extent, i->pos + (i->width/2.0)));
99                 } else {
100                         self = item_to_window (Rect (i->pos - (i->width/2.0), 0, i->pos + (i->width/2.0), _extent));
101                 }
102
103                 boost::optional<Rect> isect = self.intersection (area);
104
105                 if (!isect) {
106                         continue;
107                 }
108
109                 Rect intersection (isect.get());
110
111                 set_source_rgba (context, i->color);
112                 context->set_line_width (i->width);
113
114                 /* Not 100% sure that the computation of the invariant
115                  * positions (y and x) below work correctly if the line width
116                  * is not 1.0, but visual inspection suggests it is OK.
117                  */
118
119                 if (_orientation == Horizontal) {
120                         double y = self.y0 + ((self.y1 - self.y0)/2.0);
121                         context->move_to (intersection.x0, y);
122                         context->line_to (intersection.x1, y);
123                 } else {
124                         double x = self.x0 + ((self.x1 - self.x0)/2.0);
125                         context->move_to (x, intersection.y0);
126                         context->line_to (x, intersection.y1);
127                 }
128
129                 context->stroke ();
130         }
131 }
132
133 void
134 LineSet::add (Coord y, Distance width, Color color)
135 {
136         begin_change ();
137
138         _lines.push_back (Line (y, width, color));
139         sort (_lines.begin(), _lines.end(), LineSorter());
140
141         _bounding_box_dirty = true;
142         end_change ();
143 }
144
145 void
146 LineSet::clear ()
147 {
148         begin_change ();
149         _lines.clear ();
150         _bounding_box_dirty = true;
151         end_change ();
152 }
153
154 bool
155 LineSet::covers (Duple const & /*point*/) const
156 {
157         /* lines are ordered by position along primary axis, so binary search
158          * to find the place to start looking.
159          *
160          * XXX but not yet.
161          */
162
163         return false;
164 }