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