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