tweaks to improve enter/leave event handling - fixes at least some crashes caused...
[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         ++render_depth;
78                 
79 #ifdef CANVAS_DEBUG
80         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
81                 cerr << string_compose ("%1GROUP %2 render %3 items out of %4\n", 
82                                         _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size());
83         }
84 #endif
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                 /* convert the render area to our child's coordinates */
109                 Rect const item_area = (*i)->parent_to_item (area);
110
111                 /* intersect the child's render area with the child's bounding box */
112                 boost::optional<Rect> r = item_bbox.get().intersection (item_area);
113
114                 if (r) {
115                         /* render the intersection */
116                         context->save ();
117                         context->translate ((*i)->position().x, (*i)->position().y);
118 #ifdef CANVAS_DEBUG
119                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
120                                 cerr << string_compose ("%1render %2 %3\n", _canvas->render_indent(), (*i)->whatami(),
121                                                         (*i)->name);
122                         }
123 #endif
124                         (*i)->render (r.get(), context);
125                         ++render_count;
126                         context->restore ();
127                 } else {
128 #ifdef CANVAS_DEBUG
129                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
130                                 //cerr << string_compose ("%1skip render of %2 %3, no intersection\n", _canvas->render_indent(), (*i)->whatami(),
131                                 // (*i)->name);
132                         }
133 #endif
134                 }
135         }
136
137         --render_depth;
138 }
139
140 void
141 Group::compute_bounding_box () const
142 {
143         Rect bbox;
144         bool have_one = false;
145
146         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
147                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
148                 if (!item_bbox) {
149                         continue;
150                 }
151
152                 Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
153                 if (have_one) {
154                         bbox = bbox.extend (group_bbox);
155                 } else {
156                         bbox = group_bbox;
157                         have_one = true;
158                 }
159         }
160
161         if (!have_one) {
162                 _bounding_box = boost::optional<Rect> ();
163         } else {
164                 _bounding_box = bbox;
165         }
166
167         _bounding_box_dirty = false;
168 }
169
170 void
171 Group::add (Item* i)
172 {
173         /* XXX should really notify canvas about this */
174
175         _items.push_back (i);
176         invalidate_lut ();
177         _bounding_box_dirty = true;
178
179         
180 }
181
182 void
183 Group::remove (Item* i)
184 {
185
186         if (i->parent() != this) {
187                 return;
188         }
189
190         begin_change ();
191
192         i->unparent ();
193         _items.remove (i);
194         invalidate_lut ();
195         _bounding_box_dirty = true;
196         
197         end_change ();
198 }
199
200 void
201 Group::clear (bool with_delete)
202 {
203         begin_change ();
204
205         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
206                 if (with_delete) {
207                         delete *i;
208                 } else {
209                         (*i)->unparent ();
210                 }
211         }
212
213         _items.clear ();
214
215         invalidate_lut ();
216         _bounding_box_dirty = true;
217
218         end_change ();
219 }
220
221 void
222 Group::raise_child_to_top (Item* i)
223 {
224         _items.remove (i);
225         _items.push_back (i);
226         invalidate_lut ();
227 }
228
229 void
230 Group::raise_child (Item* i, int levels)
231 {
232         list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
233         assert (j != _items.end ());
234
235         ++j;
236         _items.remove (i);
237
238         while (levels > 0 && j != _items.end ()) {
239                 ++j;
240                 --levels;
241         }
242
243         _items.insert (j, i);
244         invalidate_lut ();
245 }
246
247 void
248 Group::lower_child_to_bottom (Item* i)
249 {
250         _items.remove (i);
251         _items.push_front (i);
252         invalidate_lut ();
253 }
254
255 void
256 Group::ensure_lut () const
257 {
258         if (!_lut) {
259                 _lut = new DumbLookupTable (*this);
260         }
261 }
262
263 void
264 Group::invalidate_lut () const
265 {
266         delete _lut;
267         _lut = 0;
268 }
269
270 void
271 Group::child_changed ()
272 {
273         invalidate_lut ();
274         _bounding_box_dirty = true;
275
276         if (_parent) {
277                 _parent->child_changed ();
278         }
279 }
280
281 void
282 Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
283 {
284         boost::optional<Rect> const bbox = bounding_box ();
285
286         if (!bbox || !bbox.get().contains (point)) {
287                 return;
288         }
289
290         Item::add_items_at_point (point, items);
291         
292         ensure_lut ();
293         
294         vector<Item*> our_items = _lut->items_at_point (point);
295         for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
296                 (*i)->add_items_at_point (point - (*i)->position(), items);
297         }
298 }
299
300 void
301 Group::dump (ostream& o) const
302 {
303         o << _canvas->indent();
304         o << "Group " << this << " [" << name << ']';
305         o << " @ " << position();
306         o << " Items: " << _items.size();
307         o << " Visible ? " << _visible;
308
309         boost::optional<Rect> bb = bounding_box();
310
311         if (bb) {
312                 o << endl << _canvas->indent() << "  bbox: " << bb.get();
313                 o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
314         } else {
315                 o << "  bbox unset";
316         }
317
318         o << endl;
319
320         ArdourCanvas::dump_depth++;
321
322         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
323                 o << **i;
324         }
325
326         ArdourCanvas::dump_depth--;
327 }