remove all XML related API from canvas. it may have been useful during development...
[ardour.git] / libs / canvas / group.cc
1 #include <iostream>
2 #include <cairomm/context.h>
3
4 #include "pbd/stacktrace.h"
5 #include "pbd/compose.h"
6 #include "pbd/xml++.h"
7
8 #include "canvas/group.h"
9 #include "canvas/types.h"
10 #include "canvas/debug.h"
11 #include "canvas/item.h"
12 #include "canvas/canvas.h"
13
14 using namespace std;
15 using namespace ArdourCanvas;
16
17 int Group::default_items_per_cell = 64;
18
19
20 Group::Group (Canvas* canvas)
21         : Item (canvas)
22         , _lut (0)
23 {
24         
25 }
26
27 Group::Group (Group* parent)
28         : Item (parent)
29         , _lut (0)
30 {
31         
32 }
33
34 Group::Group (Group* parent, Duple position)
35         : Item (parent, position)
36         , _lut (0)
37 {
38         
39 }
40
41 Group::~Group ()
42 {
43         for (list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
44                 (*i)->unparent ();
45         }
46
47         _items.clear ();
48 }
49
50 /** @param area Area to draw in this group's coordinates.
51  *  @param context Context, set up with its origin at this group's position.
52  */
53 void
54 Group::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
55 {
56         ensure_lut ();
57         vector<Item*> items = _lut->get (area);
58
59         ++render_depth;
60                 
61 #ifdef CANVAS_DEBUG
62         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
63                 cerr << string_compose ("%1GROUP %2 render %3 items out of %4\n", 
64                                         _canvas->render_indent(), (name.empty() ? string ("[unnamed]") : name), items.size(), _items.size());
65         }
66 #endif
67
68         for (vector<Item*>::const_iterator i = items.begin(); i != items.end(); ++i) {
69
70                 if (!(*i)->visible ()) {
71 #ifdef CANVAS_DEBUG
72                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
73                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] invisible - skipped\n";
74                         }
75 #endif
76                         continue;
77                 }
78                 
79                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
80
81                 if (!item_bbox) {
82 #ifdef CANVAS_DEBUG
83                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
84                                 cerr << _canvas->render_indent() << "Item " << (*i)->whatami() << " [" << (*i)->name << "] empty - skipped\n";
85                         }
86 #endif
87                         continue;
88                 }
89
90                 /* convert the render area to our child's coordinates */
91                 Rect const item_area = (*i)->parent_to_item (area);
92
93                 /* intersect the child's render area with the child's bounding box */
94                 boost::optional<Rect> r = item_bbox.get().intersection (item_area);
95
96                 if (r) {
97                         /* render the intersection */
98                         context->save ();
99                         context->translate ((*i)->position().x, (*i)->position().y);
100 #ifdef CANVAS_DEBUG
101                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
102                                 cerr << string_compose ("%1render %2 %3\n", _canvas->render_indent(), (*i)->whatami(),
103                                                         (*i)->name);
104                         }
105 #endif
106                         (*i)->render (r.get(), context);
107                         ++render_count;
108                         context->restore ();
109                 } else {
110 #ifdef CANVAS_DEBUG
111                         if (DEBUG_ENABLED(PBD::DEBUG::CanvasRender)) {
112                                 cerr << string_compose ("%1skip render of %2 %3, no intersection\n", _canvas->render_indent(), (*i)->whatami(),
113                                                         (*i)->name);
114                         }
115 #endif
116                 }
117         }
118
119         --render_depth;
120 }
121
122 void
123 Group::compute_bounding_box () const
124 {
125         Rect bbox;
126         bool have_one = false;
127
128         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
129                 boost::optional<Rect> item_bbox = (*i)->bounding_box ();
130                 if (!item_bbox) {
131                         continue;
132                 }
133
134                 Rect group_bbox = (*i)->item_to_parent (item_bbox.get ());
135                 if (have_one) {
136                         bbox = bbox.extend (group_bbox);
137                 } else {
138                         bbox = group_bbox;
139                         have_one = true;
140                 }
141         }
142
143         if (!have_one) {
144                 _bounding_box = boost::optional<Rect> ();
145         } else {
146                 _bounding_box = bbox;
147         }
148
149         _bounding_box_dirty = false;
150 }
151
152 void
153 Group::add (Item* i)
154 {
155         _items.push_back (i);
156         invalidate_lut ();
157         _bounding_box_dirty = true;
158         
159         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: group add\n");
160 }
161
162 void
163 Group::remove (Item* i)
164 {
165         _items.remove (i);
166         invalidate_lut ();
167         _bounding_box_dirty = true;
168         
169         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: group remove\n");
170 }
171
172 void
173 Group::raise_child_to_top (Item* i)
174 {
175         _items.remove (i);
176         _items.push_back (i);
177         invalidate_lut ();
178 }
179
180 void
181 Group::raise_child (Item* i, int levels)
182 {
183         list<Item*>::iterator j = find (_items.begin(), _items.end(), i);
184         assert (j != _items.end ());
185
186         ++j;
187         _items.remove (i);
188
189         while (levels > 0 && j != _items.end ()) {
190                 ++j;
191                 --levels;
192         }
193
194         _items.insert (j, i);
195         invalidate_lut ();
196 }
197
198 void
199 Group::lower_child_to_bottom (Item* i)
200 {
201         _items.remove (i);
202         _items.push_front (i);
203         invalidate_lut ();
204 }
205
206 void
207 Group::ensure_lut () const
208 {
209         if (!_lut) {
210                 _lut = new DumbLookupTable (*this);
211         }
212 }
213
214 void
215 Group::invalidate_lut () const
216 {
217         delete _lut;
218         _lut = 0;
219 }
220
221 void
222 Group::child_changed ()
223 {
224         invalidate_lut ();
225         _bounding_box_dirty = true;
226
227         if (_parent) {
228                 _parent->child_changed ();
229         }
230 }
231
232 void
233 Group::add_items_at_point (Duple const point, vector<Item const *>& items) const
234 {
235         boost::optional<Rect> const bbox = bounding_box ();
236
237         if (!bbox || !bbox.get().contains (point)) {
238                 return;
239         }
240
241         Item::add_items_at_point (point, items);
242         
243         ensure_lut ();
244         
245         vector<Item*> our_items = _lut->items_at_point (point);
246         for (vector<Item*>::iterator i = our_items.begin(); i != our_items.end(); ++i) {
247                 (*i)->add_items_at_point (point - (*i)->position(), items);
248         }
249 }
250
251 void
252 Group::dump (ostream& o) const
253 {
254         o << _canvas->indent();
255         o << "Group " << this << " [" << name << ']';
256         o << " @ " << position();
257         o << " Items: " << _items.size();
258         o << " Visible ? " << _visible;
259
260         boost::optional<Rect> bb = bounding_box();
261
262         if (bb) {
263                 o << endl << _canvas->indent() << "  bbox: " << bb.get();
264                 o << endl << _canvas->indent() << "  CANVAS bbox: " << item_to_canvas (bb.get());
265         } else {
266                 o << "  bbox unset";
267         }
268
269         o << endl;
270
271         ArdourCanvas::dump_depth++;
272
273         for (list<Item*>::const_iterator i = _items.begin(); i != _items.end(); ++i) {
274                 o << **i;
275         }
276
277         ArdourCanvas::dump_depth--;
278 }