Update canvas/UI lib GPL boilerplate and (C) from git log
[ardour.git] / libs / canvas / canvas / canvas.h
1 /*
2  * Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2013-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2014-2015 Ben Loftis <ben@harrisonconsoles.com>
5  * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2017 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /** @file  canvas/canvas.h
24  *  @brief Declaration of the main canvas classes.
25  */
26
27 #ifndef __CANVAS_CANVAS_H__
28 #define __CANVAS_CANVAS_H__
29
30 #include <set>
31
32 #include <gdkmm/window.h>
33 #include <gtkmm/eventbox.h>
34 #include <gtkmm/alignment.h>
35 #include <cairomm/surface.h>
36 #include <cairomm/context.h>
37
38 #include "pbd/signals.h"
39
40 #include "gtkmm2ext/cairo_canvas.h"
41
42 #include "canvas/visibility.h"
43 #include "canvas/root_group.h"
44
45 namespace Gtk {
46         class Window;
47         class Label;
48 }
49
50 namespace Pango {
51         class Context;
52 }
53
54 namespace ArdourCanvas
55 {
56 struct Rect;
57
58 class Item;
59 class ScrollGroup;
60
61 /** The base class for our different types of canvas.
62  *
63  *  A canvas is an area which holds a collection of canvas items, which in
64  *  turn represent shapes, text, etc.
65  *
66  *  The canvas has an arbitrarily large area, and is addressed in coordinates
67  *  of screen pixels, with an origin of (0, 0) at the top left.  x increases
68  *  rightwards and y increases downwards.
69  */
70
71 class LIBCANVAS_API Canvas
72 {
73 public:
74         Canvas ();
75         virtual ~Canvas () {}
76
77         /** called to request a redraw of an area of the canvas in WINDOW coordinates */
78         virtual void request_redraw (Rect const &) = 0;
79         /** called to ask the canvas to request a particular size from its host */
80         virtual void request_size (Duple) = 0;
81         /** called to ask the canvas' host to `grab' an item */
82         virtual void grab (Item *) = 0;
83         /** called to ask the canvas' host to `ungrab' any grabbed item */
84         virtual void ungrab () = 0;
85
86         /** called to ask the canvas' host to keyboard focus on an item */
87         virtual void focus (Item *) = 0;
88         /** called to ask the canvas' host to drop keyboard focus on an item */
89         virtual void unfocus (Item*) = 0;
90
91         void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
92
93         void prepare_for_render (Rect const &) const;
94
95         gint64 get_last_render_start_timestamp () const { return _last_render_start_timestamp; }
96
97         gint64 get_microseconds_since_render_start () const;
98
99         /** @return root group */
100         Item* root () {
101                 return &_root;
102         }
103
104         void set_background_color (Gtkmm2ext::Color);
105         Gtkmm2ext::Color background_color() const { return _bg_color; }
106
107         /** Called when an item is being destroyed */
108         virtual void item_going_away (Item *, Rect) {}
109         virtual void item_shown_or_hidden (Item *);
110         void item_visual_property_changed (Item*);
111         void item_changed (Item *, Rect);
112         void item_moved (Item *, Rect);
113
114         Duple canvas_to_window (Duple const&, bool rounded = true) const;
115         Duple window_to_canvas (Duple const&) const;
116
117         void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
118                 Duple d = canvas_to_window (Duple (cx, cy));
119                 wx = d.x;
120                 wy = d.y;
121         }
122
123         void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
124                 Duple d = window_to_canvas (Duple (wx, wy));
125                 cx = d.x;
126                 cy = d.y;
127         }
128
129         void scroll_to (Coord x, Coord y);
130         void add_scroller (ScrollGroup& i);
131
132         virtual Rect  visible_area () const = 0;
133         virtual Coord width () const = 0;
134         virtual Coord height () const = 0;
135
136         /** Store the coordinates of the mouse pointer in window coordinates in
137            @param winpos. Return true if the position was within the window,
138            false otherwise.
139         */
140         virtual bool get_mouse_position (Duple& winpos) const = 0;
141
142         /** Signal to be used by items that need to track the mouse position
143            within the window.
144         */
145         sigc::signal<void,Duple const&> MouseMotion;
146
147         sigc::signal<void> PreRender;
148
149         /** Ensures that the position given by @param winpos (in window
150             coordinates) is within the current window area, possibly reduced by
151             @param border.
152         */
153         Duple clamp_to_window (Duple const& winpos, Duple border = Duple());
154
155         void zoomed();
156
157         std::string indent() const;
158         std::string render_indent() const;
159         void dump (std::ostream&) const;
160
161         /** Ask the canvas to pick the current item again, and generate
162             an enter event for it.
163         */
164         virtual void re_enter () = 0;
165
166         virtual void start_tooltip_timeout (Item*) {}
167         virtual void stop_tooltip_timeout () {}
168
169         /** Set the timeout used to display tooltips, in milliseconds
170          */
171         static void set_tooltip_timeout (uint32_t msecs);
172
173         virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0;
174
175 protected:
176         Root             _root;
177         Gtkmm2ext::Color _bg_color;
178
179         mutable gint64 _last_render_start_timestamp;
180
181         static uint32_t tooltip_timeout_msecs;
182
183         void queue_draw_item_area (Item *, Rect);
184         virtual void pick_current_item (int state) = 0;
185         virtual void pick_current_item (Duple const &, int state) = 0;
186
187         std::list<ScrollGroup*> scrollers;
188 };
189
190 /** A canvas which renders onto a GTK EventBox */
191 class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox, public Gtkmm2ext::CairoCanvas
192 {
193 public:
194         GtkCanvas ();
195         ~GtkCanvas () { _in_dtor = true ; }
196
197         void use_nsglview ();
198
199         void request_redraw (Rect const &);
200         void request_size (Duple);
201         void grab (Item *);
202         void ungrab ();
203         void focus (Item *);
204         void unfocus (Item*);
205
206         Rect visible_area () const;
207         Coord width() const;
208         Coord height() const;
209
210         bool get_mouse_position (Duple& winpos) const;
211
212         void set_single_exposure (bool s) { _single_exposure = s; }
213         bool single_exposure () { return _single_exposure; }
214
215         void re_enter ();
216
217         void start_tooltip_timeout (Item*);
218         void stop_tooltip_timeout ();
219
220         void queue_draw ();
221         void queue_draw_area (int x, int y, int width, int height);
222
223         Glib::RefPtr<Pango::Context> get_pango_context();
224
225         void render (Cairo::RefPtr<Cairo::Context> const & ctx, cairo_rectangle_t* r)
226         {
227                 ArdourCanvas::Rect rect (r->x, r->y, r->width + r->x, r->height + r->y);
228                 Canvas::render (rect, ctx);
229         }
230
231         void prepare_for_render () const;
232
233         uint32_t background_color() { return Canvas::background_color (); }
234
235 protected:
236         void on_size_allocate (Gtk::Allocation&);
237         bool on_scroll_event (GdkEventScroll *);
238         bool on_expose_event (GdkEventExpose *);
239         bool on_key_press_event (GdkEventKey *);
240         bool on_key_release_event (GdkEventKey *);
241         bool on_button_press_event (GdkEventButton *);
242         bool on_button_release_event (GdkEventButton* event);
243         bool on_motion_notify_event (GdkEventMotion *);
244         bool on_enter_notify_event (GdkEventCrossing*);
245         bool on_leave_notify_event (GdkEventCrossing*);
246         void on_map();
247         void on_unmap();
248
249         void on_realize ();
250
251         bool button_handler (GdkEventButton *);
252         bool motion_notify_handler (GdkEventMotion *);
253         bool deliver_event (GdkEvent *);
254         void deliver_enter_leave (Duple const & point, int state);
255
256         void pick_current_item (int state);
257         void pick_current_item (Duple const &, int state);
258
259 private:
260         void item_going_away (Item *, Rect);
261         void item_shown_or_hidden (Item *);
262         bool send_leave_event (Item const *, double, double) const;
263
264         Cairo::RefPtr<Cairo::Surface> canvas_image;
265
266         /** Item currently chosen for event delivery based on pointer position */
267         Item * _current_item;
268         /** Item pending as _current_item */
269         Item * _new_current_item;
270         /** the item that is currently grabbed, or 0 */
271         Item * _grabbed_item;
272         /** the item that currently has key focus or 0 */
273         Item * _focused_item;
274
275         bool _single_exposure;
276
277         sigc::connection tooltip_timeout_connection;
278         Item* current_tooltip_item;
279         Gtk::Window* tooltip_window;
280         Gtk::Label* tooltip_label;
281         bool show_tooltip ();
282         void hide_tooltip ();
283         bool really_start_tooltip_timeout ();
284
285         bool _in_dtor;
286
287         void* _nsglview;
288 };
289
290 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
291  *   scrolling.
292  *
293  * This provides a GtkCanvas that can be scrolled. It does NOT implement the
294  * Gtk::Scrollable interface.
295  */
296 class LIBCANVAS_API GtkCanvasViewport : public Gtk::Alignment
297 {
298 public:
299         GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
300
301         /** @return our GtkCanvas */
302         GtkCanvas* canvas () {
303                 return &_canvas;
304         }
305
306 protected:
307         void on_size_request (Gtk::Requisition *);
308
309 private:
310         /** our GtkCanvas */
311         GtkCanvas _canvas;
312         Gtk::Adjustment& hadjustment;
313         Gtk::Adjustment& vadjustment;
314
315         void scrolled ();
316 };
317
318 }
319
320 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);
321
322 #endif