3 #include <pangomm/fontdescription.h>
4 #include <pangomm/layout.h>
7 #include "pbd/failed_constructor.h"
8 #include "pbd/file_utils.h"
11 #include "canvas/stateful_image.h"
12 #include "canvas/utils.h"
16 using namespace ArdourCanvas;
19 PBD::Searchpath StatefulImage::_image_search_path;
20 StatefulImage::ImageCache StatefulImage::_image_cache;
22 StatefulImage::StatefulImage (Canvas* c, const XMLNode& node)
29 if (load_states (node)) {
30 throw failed_constructor();
34 StatefulImage::~StatefulImage()
40 StatefulImage::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
42 if (_states.empty() || _state >= _states.size()) {
45 ImageHandle image = _states[_state].image;
46 Rect self = item_to_window (Rect (0, 0, image->get_width(), image->get_height()));
48 Rect draw = self.intersection (area);
54 /* move the origin of the image to the right place on the surface
55 ("window" coordinates) and render it.
57 context->set_source (image, self.x0, self.y0);
58 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
62 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
64 layout->set_text (_text);
67 layout->set_font_description (*_font);
70 // layout->set_alignment (_alignment);
71 set_source_rgba (context, _text_color);
72 context->move_to (_text_x, _text_y);
73 layout->show_in_cairo_context (context);
78 StatefulImage::compute_bounding_box () const
80 if (!_states.empty()) {
82 /* all images are assumed to be the same size */
84 _bounding_box = Rect (0, 0, _states[0].image->get_width(), _states[0].image->get_height());
89 StatefulImage::load_states (const XMLNode& node)
91 const XMLNodeList& nodes (node.children());
95 for (XMLNodeList::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
98 const XMLProperty* prop;
100 if ((prop = (*i)->property ("id")) == 0) {
101 error << _("no ID for state") << endmsg;
104 sscanf (prop->value().c_str(), "%" G_GSIZE_FORMAT, &id);
106 if ((prop = (*i)->property ("image")) == 0) {
107 error << _("no image for state") << endmsg;
111 if (!(s.image = find_image (prop->value()))) {
112 error << string_compose (_("image %1 not found for state"), prop->value()) << endmsg;
116 if (_states.size() < id) {
117 _states.reserve (id);
126 StatefulImage::ImageHandle
127 StatefulImage::find_image (const std::string& name)
129 ImageCache::iterator i;
131 if ((i = _image_cache.find (name)) != _image_cache.end()) {
137 if (!find_file (_image_search_path, name, path)) {
138 error << string_compose (_("Image named %1 not found"),
140 return ImageHandle();
143 return Cairo::ImageSurface::create_from_png (path);
147 StatefulImage::set_image_search_path (const std::string& path)
149 _image_search_path = PBD::Searchpath (path);
153 StatefulImage::set_text (const std::string& text)
157 /* never alters bounding box */
163 StatefulImage::set_state (States::size_type n)
165 if (n >= _states.size()) {