initial commit of hand merging, plus getting "ancient" waf script to work correctly
[ardour.git] / libs / canvas / poly_item.cc
1 #include <algorithm>
2 #include "pbd/xml++.h"
3 #include "pbd/compose.h"
4 #include "canvas/poly_item.h"
5
6 using namespace std;
7 using namespace ArdourCanvas;
8
9 PolyItem::PolyItem (Group* parent)
10         : Item (parent)
11         , Outline (parent)
12 {
13
14 }
15
16 void
17 PolyItem::compute_bounding_box () const
18 {
19         bool have_one = false;
20
21         Rect bbox;
22
23         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
24                 if (have_one) {
25                         bbox.x0 = min (bbox.x0, i->x);
26                         bbox.y0 = min (bbox.y0, i->y);
27                         bbox.x1 = max (bbox.x1, i->x);
28                         bbox.y1 = max (bbox.y1, i->y);
29                 } else {
30                         bbox.x0 = bbox.x1 = i->x;
31                         bbox.y0 = bbox.y1 = i->y;
32                         have_one = true;
33                 }
34         }
35
36         if (!have_one) {
37                 _bounding_box = boost::optional<Rect> ();
38         } else {
39                 _bounding_box = bbox.expand (_outline_width / 2);
40         }
41         
42         _bounding_box_dirty = false;
43 }
44
45 void
46 PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
47 {
48         bool done_first = false;
49         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
50                 if (done_first) {
51                         context->line_to (i->x, i->y);
52                 } else {
53                         context->move_to (i->x, i->y);
54                         done_first = true;
55                 }
56         }
57 }
58
59 void
60 PolyItem::set (Points const & points)
61 {
62         begin_change ();
63         
64         _points = points;
65         
66         _bounding_box_dirty = true;
67         end_change ();
68 }
69
70 Points const &
71 PolyItem::get () const
72 {
73         return _points;
74 }
75
76 void
77 PolyItem::add_poly_item_state (XMLNode* node) const
78 {
79         add_item_state (node);
80         
81         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
82                 XMLNode* p = new XMLNode ("Point");
83                 p->add_property ("x", string_compose ("%1", i->x));
84                 p->add_property ("y", string_compose ("%1", i->y));
85                 node->add_child_nocopy (*p);
86         }
87 }
88
89 void
90 PolyItem::set_poly_item_state (XMLNode const * node)
91 {
92         XMLNodeList const & children = node->children ();
93         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
94                 Duple p;
95                 p.x = atof ((*i)->property("x")->value().c_str());
96                 p.y = atof ((*i)->property("y")->value().c_str());
97                 _points.push_back (p);
98         }
99
100         _bounding_box_dirty = true;
101 }