c28fc36e19331cba032c1dc7073faea163fb33ea
[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 #include "canvas/root_group.h"
37
38 namespace ArdourCanvas
39 {
40
41 class Rect;
42 class Group;    
43
44 /** The base class for our different types of canvas.
45  *
46  *  A canvas is an area which holds a collection of canvas items, which in
47  *  turn represent shapes, text, etc.
48  *
49  *  The canvas has an arbitrarily large area, and is addressed in coordinates
50  *  of screen pixels, with an origin of (0, 0) at the top left.  x increases
51  *  rightwards and y increases downwards.
52  */
53         
54 class Canvas
55 {
56 public:
57         Canvas ();
58         virtual ~Canvas () {}
59
60         /** called to request a redraw of an area of the canvas */
61         virtual void request_redraw (Rect const &) = 0;
62         /** called to ask the canvas to request a particular size from its host */
63         virtual void request_size (Duple) = 0;
64         /** called to ask the canvas' host to `grab' an item */
65         virtual void grab (Item *) = 0;
66         /** called to ask the canvas' host to `ungrab' any grabbed item */
67         virtual void ungrab () = 0;
68
69         /** called to ask the canvas' host to keyboard focus on an item */
70         virtual void focus (Item *) = 0;
71         /** called to ask the canvas' host to drop keyboard focus on an item */
72         virtual void unfocus (Item*) = 0;
73
74         void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
75
76         /** @return root group */
77         Group* root () {
78                 return &_root;
79         }
80
81         /** Called when an item is being destroyed */
82         virtual void item_going_away (Item *, boost::optional<Rect>) {}
83         void item_shown_or_hidden (Item *);
84         void item_visual_property_changed (Item*);
85         void item_changed (Item *, boost::optional<Rect>);
86         void item_moved (Item *, boost::optional<Rect>);
87
88         virtual Cairo::RefPtr<Cairo::Context> context () = 0;
89
90         Rect canvas_to_window (Rect const&) const;
91         Rect window_to_canvas (Rect const&) const;
92         Duple canvas_to_window (Duple const&) const;
93         Duple window_to_canvas (Duple const&) const;
94
95         void canvas_to_window (Coord cx, Coord cy, Coord& wx, Coord& wy) {
96                 Duple d = canvas_to_window (Duple (cx, cy));
97                 wx = d.x;
98                 wy = d.y;
99         }
100
101         void window_to_canvas (Coord wx, Coord wy, Coord& cx, Coord& cy) {
102                 Duple d = window_to_canvas (Duple (wx, wy));
103                 cx = d.x;
104                 cy = d.y;
105         }
106
107         void scroll_to (Coord x, Coord y);
108         virtual Rect visible_area () const = 0;
109
110         void zoomed();
111     
112         std::string indent() const;
113         std::string render_indent() const;
114         void dump (std::ostream&) const;
115     
116 protected:
117         void queue_draw_item_area (Item *, Rect);
118         
119         /** our root group */
120         RootGroup _root;
121
122         Coord _scroll_offset_x;
123         Coord _scroll_offset_y;
124
125         virtual void pick_current_item (int state) = 0;
126         virtual void pick_current_item (Duple const &, int state) = 0;
127 };
128
129 /** A canvas which renders onto a GTK EventBox */
130 class GtkCanvas : public Canvas, public Gtk::EventBox
131 {
132 public:
133         GtkCanvas ();
134
135         void request_redraw (Rect const &);
136         void request_size (Duple);
137         void grab (Item *);
138         void ungrab ();
139         void focus (Item *);
140         void unfocus (Item*);
141
142         Cairo::RefPtr<Cairo::Context> context ();
143
144         Rect visible_area () const;
145
146 protected:
147         bool on_expose_event (GdkEventExpose *);
148         bool on_button_press_event (GdkEventButton *);
149         bool on_button_release_event (GdkEventButton* event);
150         bool on_motion_notify_event (GdkEventMotion *);
151         bool on_enter_notify_event (GdkEventCrossing*);
152         bool on_leave_notify_event (GdkEventCrossing*);
153         
154         bool button_handler (GdkEventButton *);
155         bool motion_notify_handler (GdkEventMotion *);
156         bool deliver_event (GdkEvent *);
157         void deliver_enter_leave (Duple const & point, int state);
158     
159         void pick_current_item (int state);
160         void pick_current_item (Duple const &, int state);
161
162 private:
163         void item_going_away (Item *, boost::optional<Rect>);
164         bool send_leave_event (Item const *, double, double) const;
165
166         /** Item currently chosen for event delivery based on pointer position */
167         Item * _current_item;
168         /** Item pending as _current_item */
169         Item * _new_current_item;
170         /** the item that is currently grabbed, or 0 */
171         Item * _grabbed_item;
172         /** the item that currently has key focus or 0 */
173         Item * _focused_item;
174 };
175
176 /** A GTK::Alignment with a GtkCanvas inside it plus some Gtk::Adjustments for
177  *   scrolling. 
178  *
179  * This provides a GtkCanvas that can be scrolled. It does NOT implement the
180  * Gtk::Scrollable interface.
181  */
182 class GtkCanvasViewport : public Gtk::Alignment
183 {
184 public:
185         GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
186
187         /** @return our GtkCanvas */
188         GtkCanvas* canvas () {
189                 return &_canvas;
190         }
191
192 protected:
193         void on_size_request (Gtk::Requisition *);
194
195 private:
196         /** our GtkCanvas */
197         GtkCanvas _canvas;
198         Gtk::Adjustment& hadjustment;
199         Gtk::Adjustment& vadjustment;
200
201         void scrolled ();
202 };
203
204 }
205
206 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);
207
208 #endif