97d7b91a1a09bb0dc6c1fe082a94cd30fc30519e
[ardour.git] / libs / canvas / widget.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <iostream>
20 #include <cairomm/context.h>
21 #include "pbd/stacktrace.h"
22 #include "pbd/compose.h"
23
24 #include "canvas/canvas.h"
25 #include "canvas/widget.h"
26 #include "canvas/debug.h"
27 #include "canvas/utils.h"
28
29 using namespace std;
30 using namespace ArdourCanvas;
31
32 Widget::Widget (Group* parent, CairoWidget& w)
33         : Item (parent)
34         , _widget (w)
35 {
36         Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
37 }
38
39 bool
40 Widget::event_proxy (GdkEvent* ev)
41 {
42         return _widget.event (ev);
43 }
44
45 void
46 Widget::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
47 {
48         std::cerr << "Render widget\n";
49
50         if (!_bounding_box) {
51                 std::cerr << "no bbox\n";
52                 return;
53         }
54
55         Rect self = item_to_window (_bounding_box.get());
56         boost::optional<Rect> r = self.intersection (area);
57
58         if (!r) {
59                 std::cerr << "no intersection\n";
60                 return;
61         }
62
63         Rect draw = r.get ();
64         cairo_rectangle_t crect;
65         crect.x = draw.x0;
66         crect.y = draw.y0;
67         crect.height = draw.height();
68         crect.width = draw.width();
69
70         std::cerr << "will draw " << draw << "\n";
71         context->save ();
72         context->translate (-draw.x0, -draw.y0);
73         //context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
74         // context->clip ();
75
76         _widget.render (context->cobj(), &crect);
77
78         context->restore ();
79 }
80
81 void
82 Widget::compute_bounding_box () const
83 {
84         std::cerr << "cbbox for widget\n";
85
86         GtkRequisition req;
87         Gtk::Allocation alloc;
88
89         _widget.size_request (req);
90
91         std::cerr << "widget wants " << req.width << " x " << req.height << "\n";
92
93         _bounding_box = Rect (0, 0, req.width, req.height);
94
95         /* make sure the widget knows that it got what it asked for */
96         alloc.set_x (0);
97         alloc.set_y (0);
98         alloc.set_width (req.width);
99         alloc.set_height (req.height);
100
101         _widget.size_allocate (alloc);
102
103         _bounding_box_dirty = false;
104 }
105