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