Merge branch 'cairocanvas' of git.ardour.org:ardour/ardour into cairocanvas
[ardour.git] / libs / canvas / poly_item.cc
1 #include <algorithm>
2
3 #include "pbd/xml++.h"
4 #include "pbd/compose.h"
5
6 #include "canvas/poly_item.h"
7 #include "canvas/canvas.h"
8
9 using namespace std;
10 using namespace ArdourCanvas;
11
12 PolyItem::PolyItem (Group* parent)
13         : Item (parent)
14         , Outline (parent)
15 {
16
17 }
18
19 void
20 PolyItem::compute_bounding_box () const
21 {
22         bool have_one = false;
23
24         Rect bbox;
25
26         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
27                 if (have_one) {
28                         bbox.x0 = min (bbox.x0, i->x);
29                         bbox.y0 = min (bbox.y0, i->y);
30                         bbox.x1 = max (bbox.x1, i->x);
31                         bbox.y1 = max (bbox.y1, i->y);
32                 } else {
33                         bbox.x0 = bbox.x1 = i->x;
34                         bbox.y0 = bbox.y1 = i->y;
35                         have_one = true;
36                 }
37         }
38
39
40         if (!have_one) {
41                 _bounding_box = boost::optional<Rect> ();
42         } else {
43                 _bounding_box = bbox.expand (_outline_width / 2);
44         }
45         
46         _bounding_box_dirty = false;
47 }
48
49 void
50 PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
51 {
52         bool done_first = false;
53         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
54                 if (done_first) {
55                         context->line_to (i->x, i->y);
56                 } else {
57                         context->move_to (i->x, i->y);
58                         done_first = true;
59                 }
60         }
61 }
62
63 void
64 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
65 {
66         bool done_first = false;
67
68         if (_points.size() <= 2) {
69                 render_path (area, context);
70                 return;
71         }
72
73         Points::const_iterator cp1 = first_control_points.begin();
74         Points::const_iterator cp2 = second_control_points.begin();
75
76         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
77
78                 if (done_first) {
79
80                         context->curve_to (cp1->x, cp1->y,
81                                            cp2->x, cp2->y,
82                                            i->x, i->y);
83
84                         cp1++;
85                         cp2++;
86                         
87                 } else {
88
89                         context->move_to (i->x, i->y);
90                         done_first = true;
91                 }
92         }
93 }
94
95 void
96 PolyItem::set (Points const & points)
97 {
98         begin_change ();
99         
100         _points = points;
101         
102         _bounding_box_dirty = true;
103         end_change ();
104 }
105
106 Points const &
107 PolyItem::get () const
108 {
109         return _points;
110 }
111
112 void
113 PolyItem::add_poly_item_state (XMLNode* node) const
114 {
115         add_item_state (node);
116         
117         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
118                 XMLNode* p = new XMLNode ("Point");
119                 p->add_property ("x", string_compose ("%1", i->x));
120                 p->add_property ("y", string_compose ("%1", i->y));
121                 node->add_child_nocopy (*p);
122         }
123 }
124
125 void
126 PolyItem::set_poly_item_state (XMLNode const * node)
127 {
128         XMLNodeList const & children = node->children ();
129         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
130                 Duple p;
131                 p.x = atof ((*i)->property("x")->value().c_str());
132                 p.y = atof ((*i)->property("y")->value().c_str());
133                 _points.push_back (p);
134         }
135
136         _bounding_box_dirty = true;
137 }
138
139 void
140 PolyItem::dump (ostream& o) const
141 {
142         Item::dump (o);
143
144         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
145         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
146                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
147         }
148 }