2 Copyright (C) 2011-2013 Paul Davis
3 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 /** @file canvas/canvas.h
21 * @brief Declaration of the main canvas classes.
24 #ifndef __CANVAS_CANVAS_H__
25 #define __CANVAS_CANVAS_H__
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>
35 #include "pbd/signals.h"
37 #include "gtkmm2ext/cairo_canvas.h"
39 #include "canvas/visibility.h"
40 #include "canvas/root_group.h"
51 namespace ArdourCanvas
58 /** The base class for our different types of canvas.
60 * A canvas is an area which holds a collection of canvas items, which in
61 * turn represent shapes, text, etc.
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.
68 class LIBCANVAS_API Canvas
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;
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;
88 void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
90 /** @return root group */
95 void set_background_color (ArdourCanvas::Color);
96 ArdourCanvas::Color background_color() const { return _bg_color; }
98 /** Called when an item is being destroyed */
99 virtual void item_going_away (Item *, Rect) {}
100 virtual void item_shown_or_hidden (Item *);
101 void item_visual_property_changed (Item*);
102 void item_changed (Item *, Rect);
103 void item_moved (Item *, Rect);
105 Duple canvas_to_window (Duple const&, bool rounded = true) const;
106 Duple window_to_canvas (Duple const&) const;
108 void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
109 Duple d = canvas_to_window (Duple (cx, cy));
114 void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
115 Duple d = window_to_canvas (Duple (wx, wy));
120 void scroll_to (Coord x, Coord y);
121 void add_scroller (ScrollGroup& i);
123 virtual Rect visible_area () const = 0;
124 virtual Coord width () const = 0;
125 virtual Coord height () const = 0;
127 /** Store the coordinates of the mouse pointer in window coordinates in
128 @param winpos. Return true if the position was within the window,
131 virtual bool get_mouse_position (Duple& winpos) const = 0;
133 /** Signal to be used by items that need to track the mouse position
136 sigc::signal<void,Duple const&> MouseMotion;
138 sigc::signal<void> PreRender;
140 /** Ensures that the position given by @param winpos (in window
141 coordinates) is within the current window area, possibly reduced by
144 Duple clamp_to_window (Duple const& winpos, Duple border = Duple());
148 std::string indent() const;
149 std::string render_indent() const;
150 void dump (std::ostream&) const;
152 /** Ask the canvas to pick the current item again, and generate
153 an enter event for it.
155 virtual void re_enter () = 0;
157 virtual void start_tooltip_timeout (Item*) {}
158 virtual void stop_tooltip_timeout () {}
160 /** Set the timeout used to display tooltips, in milliseconds
162 static void set_tooltip_timeout (uint32_t msecs);
164 virtual Glib::RefPtr<Pango::Context> get_pango_context() = 0;
170 static uint32_t tooltip_timeout_msecs;
172 void queue_draw_item_area (Item *, Rect);
173 virtual void pick_current_item (int state) = 0;
174 virtual void pick_current_item (Duple const &, int state) = 0;
176 std::list<ScrollGroup*> scrollers;
179 /** A canvas which renders onto a GTK EventBox */
180 class LIBCANVAS_API GtkCanvas : public Canvas, public Gtk::EventBox, public Gtkmm2ext::CairoCanvas
184 ~GtkCanvas () { _in_dtor = true ; }
186 void use_nsglview ();
188 void request_redraw (Rect const &);
189 void request_size (Duple);
193 void unfocus (Item*);
195 Rect visible_area () const;
197 Coord height() const;
199 bool get_mouse_position (Duple& winpos) const;
201 void set_single_exposure (bool s) { _single_exposure = s; }
202 bool single_exposure () { return _single_exposure; }
206 void start_tooltip_timeout (Item*);
207 void stop_tooltip_timeout ();
209 Glib::RefPtr<Pango::Context> get_pango_context();
211 void render (Cairo::RefPtr<Cairo::Context> const & ctx, cairo_rectangle_t* r)
213 ArdourCanvas::Rect rect (r->x, r->y, r->width + r->x, r->height + r->y);
214 Canvas::render (rect, ctx);
217 uint32_t background_color() { return Canvas::background_color (); }
220 void on_size_allocate (Gtk::Allocation&);
221 bool on_scroll_event (GdkEventScroll *);
222 bool on_expose_event (GdkEventExpose *);
223 bool on_key_press_event (GdkEventKey *);
224 bool on_key_release_event (GdkEventKey *);
225 bool on_button_press_event (GdkEventButton *);
226 bool on_button_release_event (GdkEventButton* event);
227 bool on_motion_notify_event (GdkEventMotion *);
228 bool on_enter_notify_event (GdkEventCrossing*);
229 bool on_leave_notify_event (GdkEventCrossing*);
235 bool button_handler (GdkEventButton *);
236 bool motion_notify_handler (GdkEventMotion *);
237 bool deliver_event (GdkEvent *);
238 void deliver_enter_leave (Duple const & point, int state);
240 void pick_current_item (int state);
241 void pick_current_item (Duple const &, int state);
244 void item_going_away (Item *, Rect);
245 void item_shown_or_hidden (Item *);
246 bool send_leave_event (Item const *, double, double) const;
248 Cairo::RefPtr<Cairo::Surface> canvas_image;
250 /** Item currently chosen for event delivery based on pointer position */
251 Item * _current_item;
252 /** Item pending as _current_item */
253 Item * _new_current_item;
254 /** the item that is currently grabbed, or 0 */
255 Item * _grabbed_item;
256 /** the item that currently has key focus or 0 */
257 Item * _focused_item;
259 bool _single_exposure;
261 sigc::connection tooltip_timeout_connection;
262 Item* current_tooltip_item;
263 Gtk::Window* tooltip_window;
264 Gtk::Label* tooltip_label;
265 bool show_tooltip ();
266 void hide_tooltip ();
267 bool really_start_tooltip_timeout ();
274 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
277 * This provides a GtkCanvas that can be scrolled. It does NOT implement the
278 * Gtk::Scrollable interface.
280 class LIBCANVAS_API GtkCanvasViewport : public Gtk::Alignment
283 GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
285 /** @return our GtkCanvas */
286 GtkCanvas* canvas () {
291 void on_size_request (Gtk::Requisition *);
296 Gtk::Adjustment& hadjustment;
297 Gtk::Adjustment& vadjustment;
304 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);