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/viewport.h>
30 #include <cairomm/surface.h>
31 #include <cairomm/context.h>
32 #include "pbd/signals.h"
33 #include "canvas/root_group.h"
34
35 class XMLTree;
36
37 namespace ArdourCanvas
38 {
39
40 class Rect;
41 class Group;    
42
43 /** The base class for our different types of canvas.
44  *
45  *  A canvas is an area which holds a collection of canvas items, which in
46  *  turn represent shapes, text, etc.
47  *
48  *  The canvas has an arbitrarily large area, and is addressed in coordinates
49  *  of screen pixels, with an origin of (0, 0) at the top left.  x increases
50  *  rightwards and y increases downwards.
51  */
52         
53 class Canvas
54 {
55 public:
56         Canvas ();
57         Canvas (XMLTree const *);
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         void render (Rect const &, Cairo::RefPtr<Cairo::Context> const &) const;
70
71         /** @return root group */
72         Group* root () {
73                 return &_root;
74         }
75
76         /** Called when an item is being destroyed */
77         virtual void item_going_away (Item *, boost::optional<Rect>) {}
78         void item_shown_or_hidden (Item *);
79         void item_changed (Item *, boost::optional<Rect>);
80         void item_moved (Item *, boost::optional<Rect>);
81
82         XMLTree* get_state () const;
83
84         virtual Cairo::RefPtr<Cairo::Context> context () = 0;
85
86         std::list<Rect> const & renders () const {
87                 return _renders;
88         }
89
90         void set_log_renders (bool log) {
91                 _log_renders = log;
92         }
93
94         std::string indent() const;
95         std::string render_indent() const;
96         void dump (std::ostream&) const;
97     
98 protected:
99         void queue_draw_item_area (Item *, Rect);
100         
101         /** our root group */
102         RootGroup _root;
103
104         mutable std::list<Rect> _renders;
105         bool _log_renders;
106 };
107
108 /** A Canvas which renders onto an in-memory pixbuf.  In Ardour's context,
109  *  this is most useful for testing.
110  */
111 class ImageCanvas : public Canvas
112 {
113 public:
114         ImageCanvas (Duple size = Duple (1024, 1024));
115         ImageCanvas (XMLTree const *, Duple size = Duple (1024, 1024));
116
117         void request_redraw (Rect const &) {
118                 /* XXX */
119         }
120
121         void request_size (Duple) {
122                 /* XXX */
123         }
124         
125         void grab (Item *) {}
126         void ungrab () {}
127
128         void render_to_image (Rect const &) const;
129         void clear ();
130         void write_to_png (std::string const &);
131
132         Cairo::RefPtr<Cairo::Context> context ();
133
134 private:
135         /** our Cairo surface */
136         Cairo::RefPtr<Cairo::Surface> _surface;
137         /** our Cairo context */
138         Cairo::RefPtr<Cairo::Context> _context;
139 };
140
141 /** A canvas which renders onto a GTK EventBox */
142 class GtkCanvas : public Canvas, public Gtk::EventBox
143 {
144 public:
145         GtkCanvas ();
146         GtkCanvas (XMLTree const *);
147
148         void request_redraw (Rect const &);
149         void request_size (Duple);
150         void grab (Item *);
151         void ungrab ();
152
153         Cairo::RefPtr<Cairo::Context> context ();
154
155 protected:
156         bool on_expose_event (GdkEventExpose *);
157         bool on_button_press_event (GdkEventButton *);
158         bool on_button_release_event (GdkEventButton* event);
159         bool on_motion_notify_event (GdkEventMotion *);
160         
161         bool button_handler (GdkEventButton *);
162         bool motion_notify_handler (GdkEventMotion *);
163         bool deliver_event (Duple, GdkEvent *);
164
165 private:
166         void item_going_away (Item *, boost::optional<Rect>);
167         bool send_leave_event (Item const *, double, double) const;
168
169         /** the item that the mouse is currently over, or 0 */
170         Item const * _current_item;
171         /** the item that is currently grabbed, or 0 */
172         Item const * _grabbed_item;
173 };
174
175 /** A GTK::Viewport with a GtkCanvas inside it.  This provides a GtkCanvas
176  *  that can be scrolled.
177  */
178 class GtkCanvasViewport : public Gtk::Viewport
179 {
180 public:
181         GtkCanvasViewport (Gtk::Adjustment &, Gtk::Adjustment &);
182
183         /** @return our GtkCanvas */
184         GtkCanvas* canvas () {
185                 return &_canvas;
186         }
187
188         void window_to_canvas (int, int, Coord &, Coord &) const;
189         Rect visible_area () const;
190
191 protected:
192         void on_size_request (Gtk::Requisition *);
193
194 private:
195         /** our GtkCanvas */
196         GtkCanvas _canvas;
197 };
198
199 }
200
201 std::ostream& operator<< (std::ostream&, const ArdourCanvas::Canvas&);
202
203 #endif