merge from master
[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         boost::optional<ArdourCanvas::Rect> bbox = bounding_box ();
185         boost::optional<ArdourCanvas::Rect> pre_change_parent_bounding_box;
186
187         if (bbox) {
188                 /* see the comment in Canvas::item_moved() to understand
189                  * why we use the parent's bounding box here.
190                  */
191                 pre_change_parent_bounding_box = item_to_parent (bbox.get());
192         }
193         
194         _position = p;
195
196         _canvas->item_moved (this, pre_change_parent_bounding_box);
197
198         if (_parent) {
199                 _parent->child_changed ();
200         }
201 }
202
203 void
204 Item::set_x_position (Coord x)
205 {
206         set_position (Duple (x, _position.y));
207 }
208
209 void
210 Item::set_y_position (Coord y)
211 {
212         set_position (Duple (_position.x, y));
213 }
214
215 void
216 Item::raise_to_top ()
217 {
218         assert (_parent);
219         _parent->raise_child_to_top (this);
220 }
221
222 void
223 Item::raise (int levels)
224 {
225         assert (_parent);
226         _parent->raise_child (this, levels);
227 }
228
229 void
230 Item::lower_to_bottom ()
231 {
232         assert (_parent);
233         _parent->lower_child_to_bottom (this);
234 }
235
236 void
237 Item::hide ()
238 {
239         _visible = false;
240         _canvas->item_shown_or_hidden (this);
241 }
242
243 void
244 Item::show ()
245 {
246         _visible = true;
247         _canvas->item_shown_or_hidden (this);
248 }
249
250 Duple
251 Item::item_to_parent (Duple const & d) const
252 {
253         return d.translate (_position);
254 }
255
256 Duple
257 Item::parent_to_item (Duple const & d) const
258 {
259         return d.translate (- _position);
260 }
261
262 ArdourCanvas::Rect
263 Item::parent_to_item (ArdourCanvas::Rect const & d) const
264 {
265         return d.translate (- _position);
266 }
267
268 void
269 Item::unparent ()
270 {
271         _parent = 0;
272 }
273
274 void
275 Item::reparent (Group* new_parent)
276 {
277         assert (_canvas == _parent->canvas());
278
279         if (_parent) {
280                 _parent->remove (this);
281         }
282
283         assert (new_parent);
284
285         _parent = new_parent;
286         _canvas = _parent->canvas ();
287         _parent->add (this);
288 }
289
290 void
291 Item::grab_focus ()
292 {
293         /* XXX */
294 }
295
296 /** @return Bounding box in this item's coordinates */
297 boost::optional<ArdourCanvas::Rect>
298 Item::bounding_box () const
299 {
300         if (_bounding_box_dirty) {
301                 compute_bounding_box ();
302         }
303
304         assert (!_bounding_box_dirty);
305         return _bounding_box;
306 }
307
308 Coord
309 Item::height () const 
310 {
311         boost::optional<ArdourCanvas::Rect> bb  = bounding_box();
312
313         if (bb) {
314                 return bb->height ();
315         }
316         return 0;
317 }
318
319 Coord
320 Item::width () const 
321 {
322         boost::optional<ArdourCanvas::Rect> bb = bounding_box().get();
323
324         if (bb) {
325                 return bb->width ();
326         }
327
328         return 0;
329 }
330
331 void
332 Item::begin_change ()
333 {
334         _pre_change_bounding_box = bounding_box ();
335 }
336
337 void
338 Item::end_change ()
339 {
340         _canvas->item_changed (this, _pre_change_bounding_box);
341         
342         if (_parent) {
343                 _parent->child_changed ();
344         }
345 }
346
347 void
348 Item::begin_visual_change ()
349 {
350 }
351
352 void
353 Item::end_visual_change ()
354 {
355         _canvas->item_visual_property_changed (this);
356 }
357
358 void
359 Item::move (Duple movement)
360 {
361         set_position (position() + movement);
362 }
363
364 void
365 Item::grab ()
366 {
367         assert (_canvas);
368         _canvas->grab (this);
369 }
370
371 void
372 Item::ungrab ()
373 {
374         assert (_canvas);
375         _canvas->ungrab ();
376 }
377
378 void
379 Item::set_data (string const & key, void* data)
380 {
381         _data[key] = data;
382 }
383
384 void *
385 Item::get_data (string const & key) const
386 {
387         map<string, void*>::const_iterator i = _data.find (key);
388         if (i == _data.end ()) {
389                 return 0;
390         }
391         
392         return i->second;
393 }
394
395 void
396 Item::set_ignore_events (bool ignore)
397 {
398         _ignore_events = ignore;
399 }
400
401 void
402 Item::dump (ostream& o) const
403 {
404         boost::optional<ArdourCanvas::Rect> bb = bounding_box();
405
406         o << _canvas->indent() << whatami() << ' ' << this << " Visible ? " << _visible;
407         o << " @ " << position();
408         
409 #ifdef CANVAS_DEBUG
410         if (!name.empty()) {
411                 o << ' ' << name;
412         }
413 #endif
414
415         if (bb) {
416                 o << endl << _canvas->indent() << "\tbbox: " << bb.get();
417                 o << endl << _canvas->indent() << "\tCANVAS bbox: " << item_to_canvas (bb.get());
418         } else {
419                 o << " bbox unset";
420         }
421
422         o << endl;
423 }
424
425 std::string
426 Item::whatami () const 
427 {
428         std::string type = demangle (typeid (*this).name());
429         return type.substr (type.find_last_of (':') + 1);
430 }
431
432 ostream&
433 ArdourCanvas::operator<< (ostream& o, const Item& i)
434 {
435         i.dump (o);
436         return o;
437 }