remove all xml++.h inclusion by canvas implementations
[ardour.git] / libs / canvas / line_set.cc
1 #include "canvas/line_set.h"
2 #include "canvas/utils.h"
3
4 using namespace std;
5 using namespace ArdourCanvas;
6
7 /* XXX: hard-wired to horizontal only */
8
9 class LineSorter {
10 public:
11         bool operator() (LineSet::Line& a, LineSet::Line& b) {
12                 return a.y < b.y;
13         }
14 };
15
16 LineSet::LineSet (Group* parent)
17         : Item (parent)
18         , _height (0)
19 {
20
21 }
22
23
24 void
25 LineSet::compute_bounding_box () const
26 {
27         if (_lines.empty ()) {
28                 _bounding_box = boost::optional<Rect> ();
29                 _bounding_box_dirty = false;
30                 return;
31         }
32         
33         _bounding_box = Rect (0, _lines.front().y, COORD_MAX, min (_height, _lines.back().y));
34         _bounding_box_dirty = false;
35 }
36
37 void
38 LineSet::set_height (Distance height)
39 {
40         begin_change ();
41
42         _height = height;
43
44         _bounding_box_dirty = true;
45         end_change ();
46 }
47
48 void
49 LineSet::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
50 {
51         for (list<Line>::const_iterator i = _lines.begin(); i != _lines.end(); ++i) {
52                 if (i->y > area.y1) {
53                         break;
54                 } else if (i->y > area.y0) {
55                         set_source_rgba (context, i->color);
56                         context->set_line_width (i->width);
57                         context->move_to (area.x0, i->y);
58                         context->line_to (area.x1, i->y);
59                         context->stroke ();
60                 }
61         }
62 }
63
64 void
65 LineSet::add (Coord y, Distance width, Color color)
66 {
67         begin_change ();
68         
69         _lines.push_back (Line (y, width, color));
70         _lines.sort (LineSorter ());
71
72         _bounding_box_dirty = true;
73         end_change ();
74 }
75
76 void
77 LineSet::clear ()
78 {
79         begin_change ();
80         _lines.clear ();
81         _bounding_box_dirty = true;
82         end_change ();
83 }