fix issue with initialization of a BBT_Time variable.
[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
28 using namespace std;
29 using namespace ArdourCanvas;
30
31 Widget::Widget (Canvas* c, CairoWidget& w)
32         : Item (c)
33         , _widget (w)
34 {
35         Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
36         w.set_canvas_widget ();
37         w.QueueDraw.connect (sigc::mem_fun(*this, &Widget::queue_draw));
38         w.QueueResize.connect (sigc::mem_fun(*this, &Widget::queue_resize));
39 }
40
41 Widget::Widget (Item* parent, CairoWidget& w)
42         : Item (parent)
43         , _widget (w)
44 {
45         Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
46         w.set_canvas_widget ();
47         w.QueueDraw.connect (sigc::mem_fun(*this, &Widget::queue_draw));
48         w.QueueResize.connect (sigc::mem_fun(*this, &Widget::queue_resize));
49 }
50
51 bool
52 Widget::event_proxy (GdkEvent* ev)
53 {
54         /* XXX need to translate coordinate into widget's own coordinate space */
55         return _widget.event (ev);
56 }
57
58 bool
59 Widget::queue_draw ()
60 {
61         begin_visual_change ();
62         end_visual_change ();
63         return true;
64 }
65
66 bool
67 Widget::queue_resize ()
68 {
69         begin_change ();
70         end_change ();
71         return true;
72 }
73
74 void
75 Widget::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
76 {
77         //std::cerr << "Render widget " << name << " @ " << position() << endl;
78
79         if (!_bounding_box) {
80                 std::cerr << "no bbox\n";
81                 return;
82         }
83
84         Rect self = item_to_window (_bounding_box);
85         Rect r = self.intersection (area);
86
87         if (!r) {
88                 std::cerr << "no intersection\n";
89                 return;
90         }
91
92         Rect draw = r;
93         cairo_rectangle_t crect;
94         crect.x = draw.x0;
95         crect.y = draw.y0;
96         crect.height = draw.height();
97         crect.width = draw.width();
98
99         Duple p = position_offset();
100
101         context->save ();
102         context->translate (p.x, p.y);
103         //context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
104         //context->clip ();
105
106         _widget.render (context, &crect);
107
108         context->restore ();
109 }
110
111 void
112 Widget::size_allocate (Rect const & r)
113 {
114         Item::size_allocate (r);
115         Gtk::Allocation alloc;
116         alloc.set_x (0);
117         alloc.set_y (0);
118         alloc.set_width (r.width());
119         alloc.set_height (r.height());
120         _widget.size_allocate (alloc);
121 }
122
123 void
124 Widget::compute_bounding_box () const
125 {
126         std::cerr << "cbbox for widget\n";
127
128         GtkRequisition req = { 0, 0 };
129         Gtk::Allocation alloc;
130
131         _widget.size_request (req);
132
133         std::cerr << "widget wants " << req.width << " x " << req.height << "\n";
134
135         _bounding_box = Rect (0, 0, req.width, req.height);
136
137         /* make sure the widget knows that it got what it asked for */
138         alloc.set_x (0);
139         alloc.set_y (0);
140         alloc.set_width (req.width);
141         alloc.set_height (req.height);
142
143         _widget.size_allocate (alloc);
144
145         _bounding_box_dirty = false;
146 }