fix crash when copy'ing latent plugins
[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 (Canvas* c)
31         : Item (c)
32 {
33 }
34
35 PolyItem::PolyItem (Item* parent)
36         : Item (parent)
37 {
38 }
39
40 void
41 PolyItem::compute_bounding_box () const
42 {
43
44         if (!_points.empty()) {
45
46                 Rect bbox;
47                 Points::const_iterator i = _points.begin();
48
49                 bbox.x0 = bbox.x1 = i->x;
50                 bbox.y0 = bbox.y1 = i->y;
51
52                 ++i;
53
54                 while (i != _points.end()) {
55                         bbox.x0 = min (bbox.x0, i->x);
56                         bbox.y0 = min (bbox.y0, i->y);
57                         bbox.x1 = max (bbox.x1, i->x);
58                         bbox.y1 = max (bbox.y1, i->y);
59                         ++i;
60                 }
61
62                 _bounding_box = bbox.expand (_outline_width + 0.5);
63
64
65         } else {
66                 _bounding_box = boost::optional<Rect> ();
67         }
68
69         _bounding_box_dirty = false;
70 }
71
72 void
73 PolyItem::render_path (Rect const & /* area */, Cairo::RefPtr<Cairo::Context> context) const
74 {
75         if (_points.size() < 2) {
76                 return;
77         }
78
79         Points::const_iterator i = _points.begin();
80         Duple c (item_to_window (Duple (i->x, i->y)));
81         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
82
83         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
84         ++i;
85
86         while (i != _points.end()) {
87                 c = item_to_window (Duple (i->x, i->y));
88                 context->line_to (c.x + pixel_adjust, c.y + pixel_adjust);
89                 ++i;
90         }
91 }
92
93 void
94 PolyItem::render_curve (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Points const & first_control_points, Points const & second_control_points) const
95 {
96         if (_points.size() <= 2) {
97                 render_path (area, context);
98                 return;
99         }
100
101         Points::const_iterator cp1 = first_control_points.begin();
102         Points::const_iterator cp2 = second_control_points.begin();
103         Points::const_iterator p = _points.begin();
104         const double pixel_adjust = (_outline_width == 1.0 ? 0.5 : 0.0);
105
106         Duple c = item_to_window (Duple (p->x, p->y));
107         context->move_to (c.x + pixel_adjust, c.y + pixel_adjust);
108         ++p;
109
110         while (p != _points.end()) {
111
112                 Duple c1 = item_to_window (Duple (cp1->x, cp1->y));
113                 Duple c2 = item_to_window (Duple (cp2->x, cp2->y));
114
115                 c = item_to_window (Duple (p->x, p->y));
116
117                 context->curve_to (c1.x + pixel_adjust,
118                                    c1.y + pixel_adjust,
119                                    c2.x + pixel_adjust,
120                                    c2.y + pixel_adjust,
121                                    c.x + pixel_adjust,
122                                    c.y + pixel_adjust);
123
124                 ++cp1;
125                 ++cp2;
126                 ++p;
127         }
128 }
129
130 void
131 PolyItem::set (Points const & points)
132 {
133         if (_points != points) {
134
135                 begin_change ();
136
137                 _points = points;
138
139                 _bounding_box_dirty = true;
140                 end_change ();
141         }
142 }
143
144 Points const &
145 PolyItem::get () const
146 {
147         return _points;
148 }
149
150 void
151 PolyItem::dump (ostream& o) const
152 {
153         Item::dump (o);
154
155         o << _canvas->indent() << '\t' << _points.size() << " points" << endl;
156         for (Points::const_iterator i = _points.begin(); i != _points.end(); ++i) {
157                 o << _canvas->indent() << "\t\t" << i->x << ", " << i->y << endl;
158         }
159 }