fix issue with initialization of a BBT_Time variable.
[ardour.git] / libs / canvas / stateful_image.cc
1 #include <string>
2
3 #include <pangomm/fontdescription.h>
4 #include <pangomm/layout.h>
5
6 #include "pbd/error.h"
7 #include "pbd/failed_constructor.h"
8 #include "pbd/file_utils.h"
9 #include "pbd/xml++.h"
10
11 #include "canvas/stateful_image.h"
12
13 #include "pbd/i18n.h"
14
15 using namespace ArdourCanvas;
16 using PBD::error;
17
18 PBD::Searchpath StatefulImage::_image_search_path;
19 StatefulImage::ImageCache StatefulImage::_image_cache;
20
21 StatefulImage::StatefulImage (Canvas* c, const XMLNode& node)
22         : Item (c)
23         , _state (0)
24         , _font (0)
25         , _text_x (0)
26         , _text_y (0)
27 {
28         if (load_states (node)) {
29                 throw failed_constructor();
30         }
31 }
32
33 StatefulImage::~StatefulImage()
34 {
35         delete _font;
36 }
37
38 void
39 StatefulImage::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
40 {
41         if (_states.empty() || _state >= _states.size()) {
42                 return;
43         }
44         ImageHandle image = _states[_state].image;
45         Rect self = item_to_window (Rect (0, 0, image->get_width(), image->get_height()));
46
47         Rect draw = self.intersection (area);
48
49         if (!draw) {
50                 return;
51         }
52
53         /* move the origin of the image to the right place on the surface
54            ("window" coordinates) and render it.
55         */
56         context->set_source (image, self.x0, self.y0);
57         context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
58         context->fill ();
59
60         if (!_text.empty()) {
61                 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
62
63                 layout->set_text (_text);
64
65                 if (_font) {
66                         layout->set_font_description (*_font);
67                 }
68
69                 // layout->set_alignment (_alignment);
70                 Gtkmm2ext::set_source_rgba (context, _text_color);
71                 context->move_to (_text_x, _text_y);
72                 layout->show_in_cairo_context (context);
73         }
74 }
75
76 void
77 StatefulImage::compute_bounding_box () const
78 {
79         if (!_states.empty()) {
80
81                 /* all images are assumed to be the same size */
82
83                 _bounding_box = Rect (0, 0, _states[0].image->get_width(), _states[0].image->get_height());
84         }
85 }
86
87 int
88 StatefulImage::load_states (const XMLNode& node)
89 {
90         const XMLNodeList& nodes (node.children());
91
92         _states.clear ();
93
94         for (XMLNodeList::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
95                 State s;
96                 States::size_type id;
97                 const XMLProperty* prop;
98
99                 if ((prop = (*i)->property ("id")) == 0) {
100                         error << _("no ID for state") << endmsg;
101                         continue;
102                 }
103                 sscanf (prop->value().c_str(), "%" G_GSIZE_FORMAT, &id);
104
105                 if ((prop = (*i)->property ("image")) == 0) {
106                         error << _("no image for state") << endmsg;
107                         continue;
108                 }
109
110                 if (!(s.image = find_image (prop->value()))) {
111                         error << string_compose (_("image %1 not found for state"), prop->value()) << endmsg;
112                         continue;
113                 }
114
115                 if (_states.size() < id) {
116                         _states.reserve (id);
117                 }
118
119                 _states[id] = s;
120         }
121
122         return 0;
123 }
124
125 StatefulImage::ImageHandle
126 StatefulImage::find_image (const std::string& name)
127 {
128         ImageCache::iterator i;
129
130         if ((i = _image_cache.find (name)) != _image_cache.end()) {
131                 return i->second;
132         }
133
134         std::string path;
135
136         if (!find_file (_image_search_path, name, path)) {
137                 error << string_compose (_("Image named %1 not found"),
138                                          name) << endmsg;
139                 return ImageHandle();
140         }
141
142         return Cairo::ImageSurface::create_from_png (path);
143 }
144
145 void
146 StatefulImage::set_image_search_path (const std::string& path)
147 {
148         _image_search_path = PBD::Searchpath (path);
149 }
150
151 void
152 StatefulImage::set_text (const std::string& text)
153 {
154         _text = text;
155
156         /* never alters bounding box */
157
158         redraw ();
159 }
160
161 bool
162 StatefulImage::set_state (States::size_type n)
163 {
164         if (n >= _states.size()) {
165                 return false;
166         }
167
168         _state = n;
169         redraw ();
170
171         return true;
172 }