8a8acce086052ab414bef231a22a2298ced0c2e1
[ardour.git] / libs / canvas / 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 "pbd/compose.h"
21 #include "pbd/stacktrace.h"
22 #include "pbd/convert.h"
23
24 #include "ardour/utils.h"
25
26 #include "canvas/group.h"
27 #include "canvas/item.h"
28 #include "canvas/canvas.h"
29 #include "canvas/debug.h"
30
31 using namespace std;
32 using namespace PBD;
33 using namespace ArdourCanvas;
34
35 Item::Item (Canvas* canvas)
36         : _canvas (canvas)
37         , _parent (0)
38 {
39         init ();
40 }
41
42 Item::Item (Group* parent)
43         : _canvas (parent->canvas ())
44         , _parent (parent)
45 {
46         init ();
47 }
48
49 Item::Item (Group* parent, Duple position)
50         : _canvas (parent->canvas())
51         , _parent (parent)
52         , _position (position)
53 {
54         init ();
55 }
56
57 void
58 Item::init ()
59 {
60         _visible = true;
61         _bounding_box_dirty = true;
62         _ignore_events = false;
63         
64         if (_parent) {
65                 _parent->add (this);
66         }
67
68         DEBUG_TRACE (DEBUG::CanvasItems, string_compose ("new canvas item %1\n", this));
69 }       
70
71 Item::~Item ()
72 {
73         if (_parent) {
74                 _parent->remove (this);
75         }
76
77         if (_canvas) {
78                 _canvas->item_going_away (this, _bounding_box);
79         }
80 }
81
82 ArdourCanvas::Rect
83 Item::item_to_parent (ArdourCanvas::Rect const & r) const
84 {
85         return r.translate (_position);
86 }
87
88 ArdourCanvas::Rect
89 Item::item_to_canvas (ArdourCanvas::Rect const & r) const
90 {
91         Item const * i = this;
92         Duple offset;
93
94         while (i) {
95                 offset = offset.translate (i->position());
96                 i = i->parent();
97         }
98
99         return r.translate (offset);
100 }
101
102 ArdourCanvas::Duple
103 Item::item_to_canvas (ArdourCanvas::Duple const & d) const
104 {
105         Item const * i = this;
106         Duple offset;
107
108         while (i) {
109                 offset = offset.translate (i->position());
110                 i = i->parent();
111         }
112
113         return d.translate (offset);
114 }
115
116 ArdourCanvas::Duple
117 Item::canvas_to_item (ArdourCanvas::Duple const & d) const
118 {
119         Item const * i = this;
120         Duple offset;
121
122         while (i) {
123                 offset = offset.translate (-(i->position()));
124                 i = i->parent();
125         }
126
127         return d.translate (offset);
128 }
129
130 ArdourCanvas::Rect
131 Item::canvas_to_item (ArdourCanvas::Rect const & d) const
132 {
133         Item const * i = this;
134         Duple offset;
135
136         while (i) {
137                 offset = offset.translate (-(i->position()));
138                 i = i->parent();
139         }
140
141         return d.translate (offset);
142 }
143
144 void
145 Item::item_to_canvas (Coord& x, Coord& y) const
146 {
147         Duple d = item_to_canvas (Duple (x, y));
148                 
149         x = d.x;
150         y = d.y;
151 }
152
153 void
154 Item::canvas_to_item (Coord& x, Coord& y) const
155 {
156         Duple d = canvas_to_item (Duple (x, y));
157
158         x = d.x;
159         y = d.y;
160 }
161
162 Duple
163 Item::item_to_window (ArdourCanvas::Duple const & d) const
164 {
165         return _canvas->canvas_to_window (item_to_canvas (d));
166 }
167
168 Duple
169 Item::window_to_item (ArdourCanvas::Duple const & d) const
170 {
171         return _canvas->window_to_canvas (canvas_to_item (d));
172 }
173
174 ArdourCanvas::Rect
175 Item::item_to_window (ArdourCanvas::Rect const & r) const
176 {
177         return _canvas->canvas_to_window (item_to_canvas (r));
178 }
179
180 /** Set the position of this item in the parent's coordinates */
181 void
182 Item::set_position (Duple p)
183 {
184         if (p == _position) {
185                 return;
186         }
187
188         boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
189         boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
190
191         if (bbox) {
192                 /* see the comment in Canvas::item_moved() to understand
193                  * why we use the parent's bounding box here.
194                  */
195                 pre_change_parent_bounding_box = item_to_parent (bbox.get());
196         }
197         
198         _position = p;
199
200         _canvas->item_moved (this, pre_change_parent_bounding_box);
201
202         if (_parent) {
203                 _parent->child_changed ();
204         }
205 }
206
207 void
208 Item::set_x_position (Coord x)
209 {
210         set_position (Duple (x, _position.y));
211 }
212
213 void
214 Item::set_y_position (Coord y)
215 {
216         set_position (Duple (_position.x, y));
217 }
218
219 void
220 Item::raise_to_top ()
221 {
222         assert (_parent);
223         _parent->raise_child_to_top (this);
224 }
225
226 void
227 Item::raise (int levels)
228 {
229         assert (_parent);
230         _parent->raise_child (this, levels);
231 }
232
233 void
234 Item::lower_to_bottom ()
235 {
236         assert (_parent);
237         _parent->lower_child_to_bottom (this);
238 }
239
240 void
241 Item::hide ()
242 {
243         _visible = false;
244         _canvas->item_shown_or_hidden (this);
245 }
246
247 void
248 Item::show ()
249 {
250         _visible = true;
251         _canvas->item_shown_or_hidden (this);
252 }
253
254 Duple
255 Item::item_to_parent (Duple const & d) const
256 {
257         return d.translate (_position);
258 }
259
260 Duple
261 Item::parent_to_item (Duple const & d) const
262 {
263         return d.translate (- _position);
264 }
265
266 ArdourCanvas::Rect
267 Item::parent_to_item (ArdourCanvas::Rect const & d) const
268 {
269         return d.translate (- _position);
270 }
271
272 void
273 Item::unparent ()
274 {
275         _parent = 0;
276 }
277
278 void
279 Item::reparent (Group* new_parent)
280 {
281         assert (_canvas == _parent->canvas());
282
283         if (_parent) {
284                 _parent->remove (this);
285         }
286
287         assert (new_parent);
288
289         _parent = new_parent;
290         _canvas = _parent->canvas ();
291         _parent->add (this);
292 }
293
294 bool
295 Item::common_ancestor_within (uint32_t limit, const Item& other) const
296 {
297         uint32_t d1 = depth();
298         uint32_t d2 = other.depth();
299         const Item* i1 = this;
300         const Item* i2 = &other;
301         
302         /* move towards root until we are at the same level
303            for both items
304         */
305
306         while (d1 != d2) {
307                 if (d1 > d2) {
308                         i1 = i1->parent();
309                         d1--;
310                         limit--;
311                 } else {
312                         i2 = i2->parent();
313                         d2--;
314                         limit--;
315                 }
316                 if (limit == 0) {
317                         return false;
318                 }
319         }
320
321         /* now see if there is a common parent */
322
323         while (i1 != i2) {
324                 if (i1) {
325                         i1 = i1->parent();
326                 }
327                 if (i2) {
328                         i2 = i2->parent ();
329                 }
330
331                 limit--;
332                 if (limit == 0) {
333                         return false;
334                 }
335         }
336         
337         return true;
338 }
339
340 const Item*
341 Item::closest_ancestor_with (const Item& other) const
342 {
343         uint32_t d1 = depth();
344         uint32_t d2 = other.depth();
345         const Item* i1 = this;
346         const Item* i2 = &other;
347
348         /* move towards root until we are at the same level
349            for both items
350         */
351
352         while (d1 != d2) {
353                 if (d1 > d2) {
354                         i1 = i1->parent();
355                         d1--;
356                 } else {
357                         i2 = i2->parent();
358                         d2--;
359                 }
360         }
361
362         /* now see if there is a common parent */
363
364         while (i1 != i2) {
365                 if (i1) {
366                         i1 = i1->parent();
367                 }
368                 if (i2) {
369                         i2 = i2->parent ();
370                 }
371         }
372         
373         return i1;
374 }
375
376 bool
377 Item::is_descendant_of (const Item& candidate) const
378 {
379         Item const * i = _parent;
380
381         while (i) {
382                 if (i == &candidate) {
383                         return true;
384                 }
385                 i = i->parent();
386         }
387
388         return false;
389 }
390
391 void
392 Item::grab_focus ()
393 {
394         /* XXX */
395 }
396
397 /** @return Bounding box in this item's coordinates */
398 boost::optional<ArdourCanvas::Rect>
399 Item::bounding_box () const
400 {
401         if (_bounding_box_dirty) {
402                 compute_bounding_box ();
403         }
404
405         assert (!_bounding_box_dirty);
406         return _bounding_box;
407 }
408
409 Coord
410 Item::height () const 
411 {
412         boost::optional<ArdourCanvas::Rect> bb  = bounding_box();
413
414         if (bb) {
415                 return bb->height ();
416         }
417         return 0;
418 }
419
420 Coord
421 Item::width () const 
422 {
423         boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
424
425         if (bb) {
426                 return bb->width ();
427         }
428
429         return 0;
430 }
431
432 void
433 Item::redraw () const
434 {
435         if (_visible && _bounding_box && _canvas) {
436                 _canvas->request_redraw (item_to_canvas (_bounding_box.get()));
437         }
438 }       
439
440 void
441 Item::begin_change ()
442 {
443         _pre_change_bounding_box = bounding_box ();
444 }
445
446 void
447 Item::end_change ()
448 {
449         _canvas->item_changed (this, _pre_change_bounding_box);
450         
451         if (_parent) {
452                 _parent->child_changed ();
453         }
454 }
455
456 void
457 Item::begin_visual_change ()
458 {
459 }
460
461 void
462 Item::end_visual_change ()
463 {
464         _canvas->item_visual_property_changed (this);
465 }
466
467 void
468 Item::move (Duple movement)
469 {
470         set_position (position() + movement);
471 }
472
473 void
474 Item::grab ()
475 {
476         assert (_canvas);
477         _canvas->grab (this);
478 }
479
480 void
481 Item::ungrab ()
482 {
483         assert (_canvas);
484         _canvas->ungrab ();
485 }
486
487 void
488 Item::set_data (string const & key, void* data)
489 {
490         _data[key] = data;
491 }
492
493 void *
494 Item::get_data (string const & key) const
495 {
496         map<string, void*>::const_iterator i = _data.find (key);
497         if (i == _data.end ()) {
498                 return 0;
499         }
500         
501         return i->second;
502 }
503
504 void
505 Item::set_ignore_events (bool ignore)
506 {
507         _ignore_events = ignore;
508 }
509
510 void
511 Item::dump (ostream& o) const
512 {
513         boost::optional<ArdourCanvas::Rect> bb = bounding_box();
514
515         o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
516         o << " @ " << position();
517         
518 #ifdef CANVAS_DEBUG
519         if (!name.empty()) {
520                 o << ' ' << name;
521         }
522 #endif
523
524         if (bb) {
525                 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
526                 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
527         } else {
528                 o << " bbox unset";
529         }
530
531         o << endl;
532 }
533
534 std::string
535 Item::whatami () const 
536 {
537         std::string type = demangle (typeid (*this).name());
538         return type.substr (type.find_last_of (':') + 1);
539 }
540
541 uint32_t
542 Item::depth () const
543 {
544         Item* i = _parent;
545         int d = 0;
546         while (i) {
547                 ++d;
548                 i = i->parent();
549         }
550         return d;
551 }
552
553 bool
554 Item::covers (Duple const & point) const
555 {
556         Duple p = canvas_to_item (point);
557
558         if (_bounding_box_dirty) {
559                 compute_bounding_box ();
560         }
561
562         boost::optional<Rect> r = bounding_box();
563
564         if (!r) {
565                 return false;
566         }
567
568         return r.get().contains (p);
569 }
570
571 ostream&
572 ArdourCanvas::operator<< (ostream& o, const Item& i)
573 {
574         i.dump (o);
575         return o;
576 }
577