Only show user-presets in favorite sidebar
[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 "gtkmm2ext/cairo_canvas.h"
38
39 #include "canvas/visibility.h"
40 #include "canvas/root_group.h"
41
42 namespace Gtk {
43         class Window;
44         class Label;
45 }
46
47 namespace Pango {
48         class Context;
49 }
50
51 namespace ArdourCanvas
52 {
53 struct Rect;
54
55 class Item;
56 class ScrollGroup;
57
58 /** The base class for our different types of canvas.
59  *
60  *  A canvas is an area which holds a collection of canvas items, which in
61  *  turn represent shapes, text, etc.
62  *
63  *  The canvas has an arbitrarily large area, and is addressed in coordinates
64  *  of screen pixels, with an origin of (0, 0) at the top left.  x increases
65  *  rightwards and y increases downwards.
66  */
67
68 class LIBCANVAS_API Canvas
69 {
70 public:
71         Canvas ();
72         virtual ~Canvas () {}
73
74         /** called to request a redraw of an area of the canvas in WINDOW coordinates */
75         virtual void request_redraw (Rect const &) = 0;
76         /** called to ask the canvas to request a particular size from its host */
77         virtual void request_size (Duple) = 0;
78         /** called to ask the canvas' host to `grab' an item */
79         virtual void grab (Item *) = 0;
80         /** called to ask the canvas' host to `ungrab' any grabbed item */
81         virtual void ungrab () = 0;
82
83         /** called to ask the canvas' host to keyboard focus on an item */
84         virtual void focus (Item *) = 0;
85         /** called to ask the canvas' host to drop keyboard focus on an item */
86         virtual void unfocus (Item*) = 0;
87
88         void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
89
90         void prepare_for_render (Rect const &) const;
91
92         gint64 get_last_render_start_timestamp () const { return _last_render_start_timestamp; }
93
94         gint64 get_microseconds_since_render_start () const;
95
96         /** @return root group */
97         Item* root () {
98                 return &_root;
99         }
100
101         void set_background_color (Gtkmm2ext::Color);
102         Gtkmm2ext::Color background_color() const { return _bg_color; }
103
104         /** Called when an item is being destroyed */
105         virtual void item_going_away (Item *, Rect) {}
106         virtual void item_shown_or_hidden (Item *);
107         void item_visual_property_changed (Item*);
108         void item_changed (Item *, Rect);
109         void item_moved (Item *, Rect);
110
111         Duple canvas_to_window (Duple const&, bool rounded = true) const;
112         Duple window_to_canvas (Duple const&) const;
113
114         void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
115                 Duple d = canvas_to_window (Duple (cx, cy));
116                 wx = d.x;
117                 wy = d.y;
118         }
119
120         void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
121                 Duple d = window_to_canvas (Duple (wx, wy));
122                 cx = d.x;
123                 cy = d.y;
124         }
125
126         void scroll_to (Coord x, Coord y);
127         void add_scroller (ScrollGroup& i);
128
129         virtual Rect  visible_area () const = 0;
130         virtual Coord width () const = 0;
131         virtual Coord height () const = 0;
132
133         /** Store the coordinates of the mouse pointer in window coordinates in
134            @param winpos. Return true if the position was within the window,
135            false otherwise.
136         */
137         virtual bool get_mouse_position (Duple& winpos) const = 0;
138
139         /** Signal to be used by items that need to track the mouse position
140            within the window.
141         */
142         sigc::signal<void,Duple const&> MouseMotion;
143
144         sigc::signal<void> PreRender;
145
146         /** Ensures that the position given by @param winpos (in window
147             coordinates) is within the current window area, possibly reduced by
148             @param border.
149         */
150         Duple clamp_to_window (Duple const& winpos, Duple border = Duple());
151
152         void zoomed();
153
154         std::string indent() const;
155         std::string render_indent() const;
156         void dump (std::ostream&) const;
157
158         /** Ask the canvas to pick the current item again, and generate
159             an enter event for it.
160         */
161         virtual void re_enter () = 0;
162
163         virtual void start_tooltip_timeout (Item*) {}
164         virtual void stop_tooltip_timeout () {}
165
166         /** Set the timeout used to display tooltips, in milliseconds
167          */
168         static void set_tooltip_timeout (uint32_t msecs);
169
170         virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0;
171
172 protected:
173         Root             _root;
174         Gtkmm2ext::Color _bg_color;
175
176         mutable gint64 _last_render_start_timestamp;
177
178         static uint32_t tooltip_timeout_msecs;
179
180         void queue_draw_item_area (Item *, Rect);
181         virtual void pick_current_item (int state) = 0;
182         virtual void pick_current_item (Duple const &, int state) = 0;
183
184         std::list<ScrollGroup*> scrollers;
185 };
186
187 /** A canvas which renders onto a GTK EventBox */
188 class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox, public Gtkmm2ext::CairoCanvas
189 {
190 public:
191         GtkCanvas ();
192         ~GtkCanvas () { _in_dtor = true ; }
193
194         void use_nsglview ();
195
196         void request_redraw (Rect const &);
197         void request_size (Duple);
198         void grab (Item *);
199         void ungrab ();
200         void focus (Item *);
201         void unfocus (Item*);
202
203         Rect visible_area () const;
204         Coord width() const;
205         Coord height() const;
206
207         bool get_mouse_position (Duple& winpos) const;
208
209         void set_single_exposure (bool s) { _single_exposure = s; }
210         bool single_exposure () { return _single_exposure; }
211
212         void re_enter ();
213
214         void start_tooltip_timeout (Item*);
215         void stop_tooltip_timeout ();
216
217         void queue_draw ();
218         void queue_draw_area (int x, int y, int width, int height);
219
220         Glib::RefPtr<Pango::Context> get_pango_context();
221
222         void render (Cairo::RefPtr<Cairo::Context> const & ctx, cairo_rectangle_t* r)
223         {
224                 ArdourCanvas::Rect rect (r->x, r->y, r->width + r->x, r->height + r->y);
225                 Canvas::render (rect, ctx);
226         }
227
228         void prepare_for_render () const;
229
230         uint32_t background_color() { return Canvas::background_color (); }
231
232 protected:
233         void on_size_allocate (Gtk::Allocation&);
234         bool on_scroll_event (GdkEventScroll *);
235         bool on_expose_event (GdkEventExpose *);
236         bool on_key_press_event (GdkEventKey *);
237         bool on_key_release_event (GdkEventKey *);
238         bool on_button_press_event (GdkEventButton *);
239         bool on_button_release_event (GdkEventButton* event);
240         bool on_motion_notify_event (GdkEventMotion *);
241         bool on_enter_notify_event (GdkEventCrossing*);
242         bool on_leave_notify_event (GdkEventCrossing*);
243         void on_map();
244         void on_unmap();
245
246         void on_realize ();
247
248         bool button_handler (GdkEventButton *);
249         bool motion_notify_handler (GdkEventMotion *);
250         bool deliver_event (GdkEvent *);
251         void deliver_enter_leave (Duple const & point, int state);
252
253         void pick_current_item (int state);
254         void pick_current_item (Duple const &, int state);
255
256 private:
257         void item_going_away (Item *, Rect);
258         void item_shown_or_hidden (Item *);
259         bool send_leave_event (Item const *, double, double) const;
260
261         Cairo::RefPtr<Cairo::Surface> canvas_image;
262
263         /** Item currently chosen for event delivery based on pointer position */
264         Item * _current_item;
265         /** Item pending as _current_item */
266         Item * _new_current_item;
267         /** the item that is currently grabbed, or 0 */
268         Item * _grabbed_item;
269         /** the item that currently has key focus or 0 */
270         Item * _focused_item;
271
272         bool _single_exposure;
273
274         sigc::connection tooltip_timeout_connection;
275         Item* current_tooltip_item;
276         Gtk::Window* tooltip_window;
277         Gtk::Label* tooltip_label;
278         bool show_tooltip ();
279         void hide_tooltip ();
280         bool really_start_tooltip_timeout ();
281
282         bool _in_dtor;
283
284         void* _nsglview;
285 };
286
287 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
288  *   scrolling.
289  *
290  * This provides a GtkCanvas that can be scrolled. It does NOT implement the
291  * Gtk::Scrollable interface.
292  */
293 class LIBCANVAS_API GtkCanvasViewport : public Gtk::Alignment
294 {
295 public:
296         GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
297
298         /** @return our GtkCanvas */
299         GtkCanvas* canvas () {
300                 return &_canvas;
301         }
302
303 protected:
304         void on_size_request (Gtk::Requisition *);
305
306 private:
307         /** our GtkCanvas */
308         GtkCanvas _canvas;
309         Gtk::Adjustment& hadjustment;
310         Gtk::Adjustment& vadjustment;
311
312         void scrolled ();
313 };
314
315 }
316
317 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);
318
319 #endif