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