remove all xml++.h inclusion by canvas implementations
[ardour.git] / libs / canvas / poly_item.cc
1 #include <algorithm>
2
3 #include "pbd/compose.h"
4
5 #include "canvas/poly_item.h"
6 #include "canvas/canvas.h"
7
8 using namespace std;
9 using namespace ArdourCanvas;
10
11 PolyItem::PolyItem (Group* parent)
12         : Item (parent)
13         , Outline (parent)
14 {
15
16 }
17
18 void
19 PolyItem::compute_bounding_box () const
20 {
21         bool have_one = false;
22
23         Rect bbox;
24
25         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
26                 if (have_one) {
27                         bbox.x0 = min (bbox.x0, i->x);
28                         bbox.y0 = min (bbox.y0, i->y);
29                         bbox.x1 = max (bbox.x1, i->x);
30                         bbox.y1 = max (bbox.y1, i->y);
31                 } else {
32                         bbox.x0 = bbox.x1 = i->x;
33                         bbox.y0 = bbox.y1 = i->y;
34                         have_one = true;
35                 }
36         }
37
38
39         if (!have_one) {
40                 _bounding_box = boost::optional<Rect> ();
41         } else {
42                 _bounding_box = bbox.expand (_outline_width / 2);
43         }
44         
45         _bounding_box_dirty = false;
46 }
47
48 void
49 PolyItem::render_path (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
50 {
51         bool done_first = false;
52         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
53                 if (done_first) {
54                         context->line_to (i->x, i->y);
55                 } else {
56                         context->move_to (i->x, i->y);
57                         done_first = true;
58                 }
59         }
60 }
61
62 void
63 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
64 {
65         bool done_first = false;
66
67         if (_points.size() <= 2) {
68                 render_path (area, context);
69                 return;
70         }
71
72         Points::const_iterator cp1 = first_control_points.begin();
73         Points::const_iterator cp2 = second_control_points.begin();
74
75         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
76
77                 if (done_first) {
78
79                         context->curve_to (cp1->x, cp1->y,
80                                            cp2->x, cp2->y,
81                                            i->x, i->y);
82
83                         cp1++;
84                         cp2++;
85                         
86                 } else {
87
88                         context->move_to (i->x, i->y);
89                         done_first = true;
90                 }
91         }
92 }
93
94 void
95 PolyItem::set (Points const & points)
96 {
97         begin_change ();
98         
99         _points = points;
100         
101         _bounding_box_dirty = true;
102         end_change ();
103 }
104
105 Points const &
106 PolyItem::get () const
107 {
108         return _points;
109 }
110
111 void
112 PolyItem::dump (ostream& o) const
113 {
114         Item::dump (o);
115
116         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
117         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
118                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
119         }
120 }