The return of threaded waveform rendering and related crashes.
[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 (Canvas* c, CairoWidget& w)
33         : Item (c)
34         , _widget (w)
35 {
36         Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
37 }
38
39 Widget::Widget (Item* parent, CairoWidget& w)
40         : Item (parent)
41         , _widget (w)
42 {
43         Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
44 }
45
46 bool
47 Widget::event_proxy (GdkEvent* ev)
48 {
49         /* XXX need to translate coordinate into widget's own coordinate space */
50         return _widget.event (ev);
51 }
52
53 void
54 Widget::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
55 {
56         // std::cerr << "Render widget\n";
57
58         if (!_bounding_box) {
59                 std::cerr << "no bbox\n";
60                 return;
61         }
62
63         Rect self = item_to_window (_bounding_box.get());
64         boost::optional<Rect> r = self.intersection (area);
65
66         if (!r) {
67                 std::cerr << "no intersection\n";
68                 return;
69         }
70
71         Rect draw = r.get ();
72         cairo_rectangle_t crect;
73         crect.x = draw.x0;
74         crect.y = draw.y0;
75         crect.height = draw.height();
76         crect.width = draw.width();
77
78         // std::cerr << "will draw " << draw << "\n";
79         context->save ();
80         context->translate (-draw.x0, -draw.y0);
81         //context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
82         // context->clip ();
83
84         _widget.render (context->cobj(), &crect);
85
86         context->restore ();
87 }
88
89 void
90 Widget::compute_bounding_box () const
91 {
92         std::cerr << "cbbox for widget\n";
93
94         GtkRequisition req = { 0, 0 };
95         Gtk::Allocation alloc;
96
97         _widget.size_request (req);
98
99         std::cerr << "widget wants " << req.width << " x " << req.height << "\n";
100
101         _bounding_box = Rect (0, 0, req.width, req.height);
102
103         /* make sure the widget knows that it got what it asked for */
104         alloc.set_x (0);
105         alloc.set_y (0);
106         alloc.set_width (req.width);
107         alloc.set_height (req.height);
108
109         _widget.size_allocate (alloc);
110
111         _bounding_box_dirty = false;
112 }
113