merge (with conflict fixes) with master (even against rgareus' recommendation)
[ardour.git] / libs / canvas / poly_item.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <algorithm>
21
22 #include "pbd/compose.h"
23
24 #include "canvas/poly_item.h"
25 #include "canvas/canvas.h"
26
27 using namespace std;
28 using namespace ArdourCanvas;
29
30 PolyItem::PolyItem (Group* parent)
31         : Item (parent)
32         , Outline (parent)
33 {
34
35 }
36
37 void
38 PolyItem::compute_bounding_box () const
39 {
40         bool have_one = false;
41         Rect bbox;
42
43         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
44                 if (have_one) {
45                         bbox.x0 = min (bbox.x0, i->x);
46                         bbox.y0 = min (bbox.y0, i->y);
47                         bbox.x1 = max (bbox.x1, i->x);
48                         bbox.y1 = max (bbox.y1, i->y);
49                 } else {
50                         bbox.x0 = bbox.x1 = i->x;
51                         bbox.y0 = bbox.y1 = i->y;
52                         have_one = true;
53                 }
54         }
55
56
57         if (!have_one) {
58                 _bounding_box = boost::optional<Rect> ();
59         } else {
60                 _bounding_box = bbox.expand (_outline_width / 2);
61         }
62         
63         _bounding_box_dirty = false;
64 }
65
66 void
67 PolyItem::render_path (Rect const & /* area */, Cairo::RefPtr<Cairo::Context> context) const
68 {
69         if (_points.size() < 2) {
70                 return;
71         }
72
73         Points::const_iterator i = _points.begin();
74         Duple c (item_to_window (Duple (i->x, i->y)));
75         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
76
77         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
78         ++i;
79
80         while (i != _points.end()) {
81                 c = item_to_window (Duple (i->x, i->y));
82                 context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
83                 ++i;
84         }
85 }
86
87 void
88 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
89 {
90         if (_points.size() <= 2) {
91                 render_path (area, context);
92                 return;
93         }
94
95         Points::const_iterator cp1 = first_control_points.begin();
96         Points::const_iterator cp2 = second_control_points.begin();
97         Points::const_iterator p = _points.begin();
98         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
99
100         Duple c = item_to_window (Duple (p->x, p->y));
101         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
102         ++p;
103
104         while (p != _points.end()) {
105
106                 Duple c1 = item_to_window (Duple (cp1->x, cp1->y));
107                 Duple c2 = item_to_window (Duple (cp2->x, cp2->y));
108
109                 c = item_to_window (Duple (p->x, p->y));
110                 
111                 context->curve_to (c1.x + pixel_adjust, 
112                                    c1.y + pixel_adjust, 
113                                    c2.x + pixel_adjust, 
114                                    c2.y + pixel_adjust, 
115                                    c.x + pixel_adjust, 
116                                    c.y + pixel_adjust);
117                 
118                 ++cp1;
119                 ++cp2;
120                 ++p;
121         }
122 }
123
124 void
125 PolyItem::set (Points const & points)
126 {
127         begin_change ();
128         
129         _points = points;
130         
131         _bounding_box_dirty = true;
132         end_change ();
133 }
134
135 Points const &
136 PolyItem::get () const
137 {
138         return _points;
139 }
140
141 void
142 PolyItem::dump (ostream& o) const
143 {
144         Item::dump (o);
145
146         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
147         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
148                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
149         }
150 }