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