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