initial commit of hand merging, plus getting "ancient" waf script to work correctly
[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 XMLNode *
25 LineSet::get_state () const
26 {
27         /* XXX */
28         return new XMLNode ("LineSet");
29 }
30
31 void
32 LineSet::compute_bounding_box () const
33 {
34         if (_lines.empty ()) {
35                 _bounding_box = boost::optional<Rect> ();
36                 _bounding_box_dirty = false;
37                 return;
38         }
39         
40         _bounding_box = Rect (0, _lines.front().y, COORD_MAX, min (_height, _lines.back().y));
41         _bounding_box_dirty = false;
42 }
43
44 void
45 LineSet::set_height (Distance height)
46 {
47         begin_change ();
48
49         _height = height;
50
51         _bounding_box_dirty = true;
52         end_change ();
53 }
54
55 void
56 LineSet::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
57 {
58         for (list<Line>::const_iterator i = _lines.begin(); i != _lines.end(); ++i) {
59                 if (i->y > area.y1) {
60                         break;
61                 } else if (i->y > area.y0) {
62                         set_source_rgba (context, i->color);
63                         context->set_line_width (i->width);
64                         context->move_to (area.x0, i->y);
65                         context->line_to (area.x1, i->y);
66                         context->stroke ();
67                 }
68         }
69 }
70
71 void
72 LineSet::add (Coord y, Distance width, Color color)
73 {
74         begin_change ();
75         
76         _lines.push_back (Line (y, width, color));
77         _lines.sort (LineSorter ());
78
79         _bounding_box_dirty = true;
80         end_change ();
81 }
82
83 void
84 LineSet::clear ()
85 {
86         begin_change ();
87         _lines.clear ();
88         _bounding_box_dirty = true;
89         end_change ();
90 }