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