new API for TrackingText and similar items
[ardour.git] / libs / canvas / canvas / canvas.h
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 /** @file  canvas/canvas.h
21  *  @brief Declaration of the main canvas classes.
22  */
23
24 #ifndef __CANVAS_CANVAS_H__
25 #define __CANVAS_CANVAS_H__
26
27 #include <set>
28
29 #include <gdkmm/window.h>
30 #include <gtkmm/eventbox.h>
31 #include <gtkmm/alignment.h>
32 #include <cairomm/surface.h>
33 #include <cairomm/context.h>
34
35 #include "pbd/signals.h"
36
37 #include "canvas/visibility.h"
38
39 #include "canvas/root_group.h"
40
41 namespace ArdourCanvas
42 {
43
44 class Rect;
45 class Item;
46 class ScrollGroup;
47
48 /** The base class for our different types of canvas.
49  *
50  *  A canvas is an area which holds a collection of canvas items, which in
51  *  turn represent shapes, text, etc.
52  *
53  *  The canvas has an arbitrarily large area, and is addressed in coordinates
54  *  of screen pixels, with an origin of (0, 0) at the top left.  x increases
55  *  rightwards and y increases downwards.
56  */
57         
58 class LIBCANVAS_API Canvas
59 {
60 public:
61         Canvas ();
62         virtual ~Canvas () {}
63
64         /** called to request a redraw of an area of the canvas in WINDOW coordinates */
65         virtual void request_redraw (Rect const &) = 0;
66         /** called to ask the canvas to request a particular size from its host */
67         virtual void request_size (Duple) = 0;
68         /** called to ask the canvas' host to `grab' an item */
69         virtual void grab (Item *) = 0;
70         /** called to ask the canvas' host to `ungrab' any grabbed item */
71         virtual void ungrab () = 0;
72
73         /** called to ask the canvas' host to keyboard focus on an item */
74         virtual void focus (Item *) = 0;
75         /** called to ask the canvas' host to drop keyboard focus on an item */
76         virtual void unfocus (Item*) = 0;
77
78         void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
79
80         /** @return root group */
81         Item* root () {
82                 return &_root;
83         }
84
85         /** Called when an item is being destroyed */
86         virtual void item_going_away (Item *, boost::optional<Rect>) {}
87         void item_shown_or_hidden (Item *);
88         void item_visual_property_changed (Item*);
89         void item_changed (Item *, boost::optional<Rect>);
90         void item_moved (Item *, boost::optional<Rect>);
91
92         virtual Cairo::RefPtr<Cairo::Context> context () = 0;
93
94         Duple canvas_to_window (Duple const&, bool rounded = true) const;
95         Duple window_to_canvas (Duple const&) const;
96
97         void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
98                 Duple d = canvas_to_window (Duple (cx, cy));
99                 wx = d.x;
100                 wy = d.y;
101         }
102
103         void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
104                 Duple d = window_to_canvas (Duple (wx, wy));
105                 cx = d.x;
106                 cy = d.y;
107         }
108
109         void scroll_to (Coord x, Coord y);
110         void add_scroller (ScrollGroup& i);
111         
112         virtual Rect  visible_area () const = 0;
113         virtual Coord width () const = 0;
114         virtual Coord height () const = 0;
115
116         /** Store the coordinates of the mouse pointer in window coordinates in
117            @param winpos. Return true if the position was within the window,
118            false otherwise.
119         */
120         virtual bool get_mouse_position (Duple& winpos) const = 0;
121
122         /** Signal to be used by items that need to track the mouse position
123            within the window.
124         */
125         sigc::signal<void,Duple const&> MouseMotion;
126
127         /** Ensures that the position given by @param winpos (in window
128             coordinates) is within the current window area, possibly reduced by
129             @param border.
130         */
131         Duple clamp_to_window (Duple const& winpos, Duple border = Duple());
132
133         void zoomed();
134     
135         std::string indent() const;
136         std::string render_indent() const;
137         void dump (std::ostream&) const;
138     
139 protected:
140         void queue_draw_item_area (Item *, Rect);
141         
142         /** our root item */
143         Root _root;
144
145         virtual void pick_current_item (int state) = 0;
146         virtual void pick_current_item (Duple const &, int state) = 0;
147
148         std::list<ScrollGroup*> scrollers;
149 };
150
151 /** A canvas which renders onto a GTK EventBox */
152 class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox
153 {
154 public:
155         GtkCanvas ();
156
157         void request_redraw (Rect const &);
158         void request_size (Duple);
159         void grab (Item *);
160         void ungrab ();
161         void focus (Item *);
162         void unfocus (Item*);
163
164         Cairo::RefPtr<Cairo::Context> context ();
165
166         Rect visible_area () const;
167         Coord width() const;
168         Coord height() const;
169
170         bool get_mouse_position (Duple& winpos) const;
171
172 protected:
173         bool on_scroll_event (GdkEventScroll *);
174         bool on_expose_event (GdkEventExpose *);
175         bool on_button_press_event (GdkEventButton *);
176         bool on_button_release_event (GdkEventButton* event);
177         bool on_motion_notify_event (GdkEventMotion *);
178         bool on_enter_notify_event (GdkEventCrossing*);
179         bool on_leave_notify_event (GdkEventCrossing*);
180         
181         bool button_handler (GdkEventButton *);
182         bool motion_notify_handler (GdkEventMotion *);
183         bool deliver_event (GdkEvent *);
184         void deliver_enter_leave (Duple const & point, int state);
185     
186         void pick_current_item (int state);
187         void pick_current_item (Duple const &, int state);
188
189 private:
190         void item_going_away (Item *, boost::optional<Rect>);
191         bool send_leave_event (Item const *, double, double) const;
192
193         /** Item currently chosen for event delivery based on pointer position */
194         Item * _current_item;
195         /** Item pending as _current_item */
196         Item * _new_current_item;
197         /** the item that is currently grabbed, or 0 */
198         Item * _grabbed_item;
199         /** the item that currently has key focus or 0 */
200         Item * _focused_item;
201 };
202
203 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
204  *   scrolling. 
205  *
206  * This provides a GtkCanvas that can be scrolled. It does NOT implement the
207  * Gtk::Scrollable interface.
208  */
209 class LIBCANVAS_API GtkCanvasViewport : public Gtk::Alignment
210 {
211 public:
212         GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
213
214         /** @return our GtkCanvas */
215         GtkCanvas* canvas () {
216                 return &_canvas;
217         }
218
219 protected:
220         void on_size_request (Gtk::Requisition *);
221
222 private:
223         /** our GtkCanvas */
224         GtkCanvas _canvas;
225         Gtk::Adjustment& hadjustment;
226         Gtk::Adjustment& vadjustment;
227
228         void scrolled ();
229 };
230
231 }
232
233 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);
234
235 #endif