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>
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.
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.
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.
23 /** @file canvas/canvas.h
24 * @brief Declaration of the main canvas classes.
27 #ifndef __CANVAS_CANVAS_H__
28 #define __CANVAS_CANVAS_H__
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>
38 #include "pbd/signals.h"
40 #include "gtkmm2ext/cairo_canvas.h"
42 #include "canvas/visibility.h"
43 #include "canvas/root_group.h"
54 namespace ArdourCanvas
61 /** The base class for our different types of canvas.
63 * A canvas is an area which holds a collection of canvas items, which in
64 * turn represent shapes, text, etc.
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.
71 class LIBCANVAS_API Canvas
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;
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;
91 void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
93 void prepare_for_render (Rect const &) const;
95 gint64 get_last_render_start_timestamp () const { return _last_render_start_timestamp; }
97 gint64 get_microseconds_since_render_start () const;
99 /** @return root group */
104 void set_background_color (Gtkmm2ext::Color);
105 Gtkmm2ext::Color background_color() const { return _bg_color; }
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);
114 Duple canvas_to_window (Duple const&, bool rounded = true) const;
115 Duple window_to_canvas (Duple const&) const;
117 void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
118 Duple d = canvas_to_window (Duple (cx, cy));
123 void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
124 Duple d = window_to_canvas (Duple (wx, wy));
129 void scroll_to (Coord x, Coord y);
130 void add_scroller (ScrollGroup& i);
132 virtual Rect visible_area () const = 0;
133 virtual Coord width () const = 0;
134 virtual Coord height () const = 0;
136 /** Store the coordinates of the mouse pointer in window coordinates in
137 @param winpos. Return true if the position was within the window,
140 virtual bool get_mouse_position (Duple& winpos) const = 0;
142 /** Signal to be used by items that need to track the mouse position
145 sigc::signal<void,Duple const&> MouseMotion;
147 sigc::signal<void> PreRender;
149 /** Ensures that the position given by @param winpos (in window
150 coordinates) is within the current window area, possibly reduced by
153 Duple clamp_to_window (Duple const& winpos, Duple border = Duple());
157 std::string indent() const;
158 std::string render_indent() const;
159 void dump (std::ostream&) const;
161 /** Ask the canvas to pick the current item again, and generate
162 an enter event for it.
164 virtual void re_enter () = 0;
166 virtual void start_tooltip_timeout (Item*) {}
167 virtual void stop_tooltip_timeout () {}
169 /** Set the timeout used to display tooltips, in milliseconds
171 static void set_tooltip_timeout (uint32_t msecs);
173 virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0;
177 Gtkmm2ext::Color _bg_color;
179 mutable gint64 _last_render_start_timestamp;
181 static uint32_t tooltip_timeout_msecs;
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;
187 std::list<ScrollGroup*> scrollers;
190 /** A canvas which renders onto a GTK EventBox */
191 class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox, public Gtkmm2ext::CairoCanvas
195 ~GtkCanvas () { _in_dtor = true ; }
197 void use_nsglview ();
199 void request_redraw (Rect const &);
200 void request_size (Duple);
204 void unfocus (Item*);
206 Rect visible_area () const;
208 Coord height() const;
210 bool get_mouse_position (Duple& winpos) const;
212 void set_single_exposure (bool s) { _single_exposure = s; }
213 bool single_exposure () { return _single_exposure; }
217 void start_tooltip_timeout (Item*);
218 void stop_tooltip_timeout ();
221 void queue_draw_area (int x, int y, int width, int height);
223 Glib::RefPtr<Pango::Context> get_pango_context();
225 void render (Cairo::RefPtr<Cairo::Context> const & ctx, cairo_rectangle_t* r)
227 ArdourCanvas::Rect rect (r->x, r->y, r->width + r->x, r->height + r->y);
228 Canvas::render (rect, ctx);
231 void prepare_for_render () const;
233 uint32_t background_color() { return Canvas::background_color (); }
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*);
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);
256 void pick_current_item (int state);
257 void pick_current_item (Duple const &, int state);
260 void item_going_away (Item *, Rect);
261 void item_shown_or_hidden (Item *);
262 bool send_leave_event (Item const *, double, double) const;
264 Cairo::RefPtr<Cairo::Surface> canvas_image;
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;
275 bool _single_exposure;
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 ();
290 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
293 * This provides a GtkCanvas that can be scrolled. It does NOT implement the
294 * Gtk::Scrollable interface.
296 class LIBCANVAS_API GtkCanvasViewport : public Gtk::Alignment
299 GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
301 /** @return our GtkCanvas */
302 GtkCanvas* canvas () {
307 void on_size_request (Gtk::Requisition *);
312 Gtk::Adjustment& hadjustment;
313 Gtk::Adjustment& vadjustment;
320 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);