a better, more general fix for the previous Canvas::item_going_away() issue. There...
[ardour.git] / libs / canvas / group.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 <iostream>
21 #include <cairomm/context.h>
22
23 #include "pbd/stacktrace.h"
24 #include "pbd/compose.h"
25
26 #include "canvas/group.h"
27 #include "canvas/types.h"
28 #include "canvas/debug.h"
29 #include "canvas/item.h"
30 #include "canvas/canvas.h"
31
32 using namespace std;
33 using namespace ArdourCanvas;
34
35 int Group::default_items_per_cell = 64;
36
37
38 Group::Group (Canvas* canvas)
39         : Item (canvas)
40         , _lut (0)
41 {
42         
43 }
44
45 Group::Group (Group* parent)
46         : Item (parent)
47         , _lut (0)
48 {
49         
50 }
51
52 Group::Group (Group* parent, Duple position)
53         : Item (parent, position)
54         , _lut (0)
55 {
56         
57 }
58
59 Group::~Group ()
60 {
61         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
62                 (*i)->unparent ();
63         }
64
65         _items.clear ();
66 }
67
68 /** @param area Area to draw in this group's coordinates.
69  *  @param context Context, set up with its origin at this group's position.
70  */
71 void
72 Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
73 {
74         ensure_lut ();
75         vector<Item*> items = _lut->get (area);
76
77 #ifdef CANVAS_DEBUG
78         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
79                 cerr << string_compose ("%1GROUP %2 @ %7 render %5 @ %6 %3 items out of %4\n", 
80                                         _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size(), area, _position, this);
81         }
82 #endif
83
84         ++render_depth;
85                 
86         for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
87
88                 if (!(*i)->visible ()) {
89 #ifdef CANVAS_DEBUG
90                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
91                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] invisible - skipped\n";
92                         }
93 #endif
94                         continue;
95                 }
96                 
97                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
98
99                 if (!item_bbox) {
100 #ifdef CANVAS_DEBUG
101                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
102                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] empty - skipped\n";
103                         }
104 #endif
105                         continue;
106                 }
107                 
108                 Rect item = (*i)->item_to_window (item_bbox.get());
109                 boost::optional<Rect> d = item.intersection (area);
110                 
111                 if (d) {
112                         Rect draw = d.get();
113                         if (draw.width() && draw.height()) {
114 #ifdef CANVAS_DEBUG
115                                 if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
116                                         if (dynamic_cast<Group*>(*i) == 0) {
117                                                 cerr << _canvas->render_indent() << "render "
118                                                      << ' ' 
119                                                      << (*i)
120                                                      << ' '
121                                                      << (*i)->whatami()
122                                                      << ' '
123                                                      << (*i)->name
124                                                      << " item = " 
125                                                      << item
126                                                      << " intersect = "
127                                                      << draw
128                                                      << " @ " 
129                                                      << _position
130                                                      << endl;
131                                         }
132                                 }
133 #endif
134
135                                 (*i)->render (area, context);
136                                 ++render_count;
137                         }
138
139                 } else {
140
141 #ifdef CANVAS_DEBUG
142                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
143                                 cerr << string_compose ("%1skip render of %2 %3, no intersection\n", _canvas->render_indent(), (*i)->whatami(),
144                                                         (*i)->name);
145                         }
146 #endif
147
148                 }
149         }
150
151         --render_depth;
152 }
153
154 void
155 Group::compute_bounding_box () const
156 {
157         Rect bbox;
158         bool have_one = false;
159
160         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
161
162                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
163
164                 if (!item_bbox) {
165                         continue;
166                 }
167
168                 Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
169                 if (have_one) {
170                         bbox = bbox.extend (group_bbox);
171                 } else {
172                         bbox = group_bbox;
173                         have_one = true;
174                 }
175         }
176
177         if (!have_one) {
178                 _bounding_box = boost::optional<Rect> ();
179         } else {
180                 _bounding_box = bbox;
181         }
182
183         _bounding_box_dirty = false;
184 }
185
186 void
187 Group::add (Item* i)
188 {
189         /* XXX should really notify canvas about this */
190
191         _items.push_back (i);
192         invalidate_lut ();
193         _bounding_box_dirty = true;
194
195         
196 }
197
198 void
199 Group::remove (Item* i)
200 {
201
202         if (i->parent() != this) {
203                 return;
204         }
205
206         begin_change ();
207
208         i->unparent ();
209         _items.remove (i);
210         invalidate_lut ();
211         _bounding_box_dirty = true;
212         
213         end_change ();
214 }
215
216 void
217 Group::clear (bool with_delete)
218 {
219         begin_change ();
220
221         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
222                 if (with_delete) {
223                         delete *i;
224                 } else {
225                         (*i)->unparent ();
226                 }
227         }
228
229         _items.clear ();
230
231         invalidate_lut ();
232         _bounding_box_dirty = true;
233
234         end_change ();
235 }
236
237 void
238 Group::raise_child_to_top (Item* i)
239 {
240         if (!_items.empty()) {
241                 if (_items.back() == i) {
242                         return;
243                 }
244         }
245
246         _items.remove (i);
247         _items.push_back (i);
248         invalidate_lut ();
249 }
250
251 void
252 Group::raise_child (Item* i, int levels)
253 {
254         list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
255         assert (j != _items.end ());
256
257         ++j;
258         _items.remove (i);
259
260         while (levels > 0 && j != _items.end ()) {
261                 ++j;
262                 --levels;
263         }
264
265         _items.insert (j, i);
266         invalidate_lut ();
267 }
268
269 void
270 Group::lower_child_to_bottom (Item* i)
271 {
272         if (!_items.empty()) {
273                 if (_items.front() == i) {
274                         return;
275                 }
276         }
277         _items.remove (i);
278         _items.push_front (i);
279         invalidate_lut ();
280 }
281
282 void
283 Group::ensure_lut () const
284 {
285         if (!_lut) {
286                 _lut = new DumbLookupTable (*this);
287         }
288 }
289
290 void
291 Group::invalidate_lut () const
292 {
293         delete _lut;
294         _lut = 0;
295 }
296
297 void
298 Group::child_changed ()
299 {
300         invalidate_lut ();
301         _bounding_box_dirty = true;
302
303         if (_parent) {
304                 _parent->child_changed ();
305         }
306 }
307
308 void
309 Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
310 {
311         boost::optional<Rect> const bbox = bounding_box ();
312
313         /* Point is in canvas coordinate system */
314
315         if (!bbox || !item_to_canvas (bbox.get()).contains (point)) {
316                 return;
317         }
318
319         /* now recurse and add any items within our group that contain point */
320
321         ensure_lut ();
322         vector<Item*> our_items = _lut->items_at_point (point);
323
324         if (!our_items.empty()) {
325                 /* this adds this group itself to the list of items at point */
326                 Item::add_items_at_point (point, items);
327         }
328
329         for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
330                 (*i)->add_items_at_point (point, items);
331         }
332 }
333
334 void
335 Group::dump (ostream& o) const
336 {
337 #ifdef CANVAS_DEBUG
338         o << _canvas->indent();
339         o << "Group " << this << " [" << name << ']';
340         o << " @ " << position();
341         o << " Items: " << _items.size();
342         o << " Visible ? " << _visible;
343
344         boost::optional<Rect> bb = bounding_box();
345
346         if (bb) {
347                 o << endl << _canvas->indent() << "  bbox: " << bb.get();
348                 o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
349         } else {
350                 o << "  bbox unset";
351         }
352
353         o << endl;
354 #endif
355
356         ArdourCanvas::dump_depth++;
357
358         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
359                 o << **i;
360         }
361
362         ArdourCanvas::dump_depth--;
363 }