2 Copyright (C) 2011-2013 Paul Davis
3 Original Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #ifndef __CANVAS_ITEM_H__
21 #define __CANVAS_ITEM_H__
27 #include <cairomm/context.h>
29 #include "pbd/signals.h"
31 #include "canvas/visibility.h"
32 #include "canvas/types.h"
34 namespace ArdourCanvas
42 /** The parent class for anything that goes on the canvas.
44 * Items have a position, which is expressed in the coordinates of the parent.
45 * They also have a bounding box, which describes the area in which they have
46 * drawable content, which is expressed in their own coordinates (whose origin
47 * is at the item position).
49 * Any item that is being displayed on a canvas has a pointer to that canvas,
50 * and all except the `root group' have a pointer to their parent group.
53 class LIBCANVAS_API Item
58 Item (Group *, Duple);
63 /** Render this item to a Cairo context.
64 * @param area Area to draw, in **window** coordinates
66 * Items must convert their own coordinates into window coordinates
67 * because Cairo is limited to a fixed point coordinate space that
68 * does not extend as far as the Ardour timeline. All rendering must
69 * be done using coordinates that do not exceed the (rough) limits
70 * of the canvas' window, to avoid odd errors within Cairo as it
71 * converts doubles into its fixed point format and then tesselates
74 virtual void render (Rect const & area, Cairo::RefPtr<Cairo::Context>) const = 0;
76 virtual void add_items_at_point (Duple, std::vector<Item const *>& items) const {
77 items.push_back (this);
80 virtual bool covers (Duple const &) const;
82 /** Update _bounding_box and _bounding_box_dirty */
83 virtual void compute_bounding_box () const = 0;
89 void reparent (Group *);
91 /** @return Parent group, or 0 if this is the root group */
92 Group* parent () const {
96 uint32_t depth() const;
97 const Item* closest_ancestor_with (const Item& other) const;
98 bool common_ancestor_within (uint32_t, const Item& other) const;
100 /** returns true if this item is an ancestor of @param candidate,
101 * and false otherwise.
103 bool is_ancestor_of (const Item& candidate) const {
104 return candidate.is_descendant_of (*this);
106 /** returns true if this Item is a descendant of @param candidate,
107 * and false otherwise.
109 bool is_descendant_of (const Item& candidate) const;
111 void set_position (Duple);
112 void set_x_position (Coord);
113 void set_y_position (Coord);
116 virtual void scroll_to (Duple const&) {}
118 /** @return Position of this item in the parent's coordinates */
119 Duple position () const {
123 boost::optional<Rect> bounding_box () const;
124 Coord height() const;
127 Duple item_to_parent (Duple const &) const;
128 Rect item_to_parent (Rect const &) const;
129 Duple parent_to_item (Duple const &) const;
130 Rect parent_to_item (Rect const &) const;
132 /* XXX: it's a pity these two aren't the same form as item_to_parent etc.,
133 but it makes a bit of a mess in the rest of the code if they are not.
135 void canvas_to_item (Coord &, Coord &) const;
136 void item_to_canvas (Coord &, Coord &) const;
138 Duple item_to_window (Duple const&, bool rounded = true) const;
139 Duple window_to_item (Duple const&) const;
140 Rect item_to_window (Rect const&) const;
141 Rect window_to_item (Rect const&) const;
143 void raise_to_top ();
145 void lower_to_bottom ();
150 /** @return true if this item is visible (ie it will be rendered),
153 bool visible () const {
157 /** @return Our canvas, or 0 if we are not attached to one */
158 Canvas* canvas () const {
162 void set_ignore_events (bool);
163 bool ignore_events () const {
164 return _ignore_events;
167 void set_data (std::string const &, void *);
168 void* get_data (std::string const &) const;
170 /* This is a sigc++ signal because it is solely
171 concerned with GUI stuff and is thus single-threaded
175 struct EventAccumulator {
176 typedef T result_type;
178 result_type operator () (U first, U last) {
179 while (first != last) {
189 sigc::signal1<bool, GdkEvent*, EventAccumulator<bool> > Event;
195 #ifdef CANVAS_COMPATIBILITY
199 virtual void dump (std::ostream&) const;
200 std::string whatami() const;
204 /** To be called at the beginning of any property change that
205 * may alter the bounding box of this item
207 void begin_change ();
208 /** To be called at the endof any property change that
209 * may alter the bounding box of this item
212 /** To be called at the beginning of any property change that
213 * does NOT alter the bounding box of this item
215 void begin_visual_change ();
216 /** To be called at the endof any property change that
217 * does NOT alter the bounding box of this item
219 void end_visual_change ();
222 /** parent group; may be 0 if we are the root group or if we have been unparent()ed */
224 /** scroll parent group; may be 0 if we are the root group or if we have been unparent()ed */
225 ScrollGroup* _scroll_parent;
226 /** position of this item in parent coordinates */
228 /** true if this item is visible (ie to be drawn), otherwise false */
230 /** our bounding box before any change that is currently in progress */
231 boost::optional<Rect> _pre_change_bounding_box;
233 /** our bounding box; may be out of date if _bounding_box_dirty is true */
234 mutable boost::optional<Rect> _bounding_box;
235 /** true if _bounding_box might be out of date, false if its definitely not */
236 mutable bool _bounding_box_dirty;
238 /* XXX: this is a bit grubby */
239 std::map<std::string, void *> _data;
246 Duple scroll_offset() const;
247 Duple position_offset() const;
249 void find_scroll_parent ();
252 extern LIBCANVAS_API std::ostream& operator<< (std::ostream&, const ArdourCanvas::Item&);