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"
15 using namespace ArdourCanvas;
18 PBD::Searchpath StatefulImage::_image_search_path;
19 StatefulImage::ImageCache StatefulImage::_image_cache;
21 StatefulImage::StatefulImage (Canvas* c, const XMLNode& node)
28 if (load_states (node)) {
29 throw failed_constructor();
33 StatefulImage::~StatefulImage()
39 StatefulImage::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
41 if (_states.empty() || _state >= _states.size()) {
44 ImageHandle image = _states[_state].image;
45 Rect self = item_to_window (Rect (0, 0, image->get_width(), image->get_height()));
47 Rect draw = self.intersection (area);
53 /* move the origin of the image to the right place on the surface
54 ("window" coordinates) and render it.
56 context->set_source (image, self.x0, self.y0);
57 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
61 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
63 layout->set_text (_text);
66 layout->set_font_description (*_font);
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);
77 StatefulImage::compute_bounding_box () const
79 if (!_states.empty()) {
81 /* all images are assumed to be the same size */
83 _bounding_box = Rect (0, 0, _states[0].image->get_width(), _states[0].image->get_height());
88 StatefulImage::load_states (const XMLNode& node)
90 const XMLNodeList& nodes (node.children());
94 for (XMLNodeList::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
97 const XMLProperty* prop;
99 if ((prop = (*i)->property ("id")) == 0) {
100 error << _("no ID for state") << endmsg;
103 sscanf (prop->value().c_str(), "%" G_GSIZE_FORMAT, &id);
105 if ((prop = (*i)->property ("image")) == 0) {
106 error << _("no image for state") << endmsg;
110 if (!(s.image = find_image (prop->value()))) {
111 error << string_compose (_("image %1 not found for state"), prop->value()) << endmsg;
115 if (_states.size() < id) {
116 _states.reserve (id);
125 StatefulImage::ImageHandle
126 StatefulImage::find_image (const std::string& name)
128 ImageCache::iterator i;
130 if ((i = _image_cache.find (name)) != _image_cache.end()) {
136 if (!find_file (_image_search_path, name, path)) {
137 error << string_compose (_("Image named %1 not found"),
139 return ImageHandle();
142 return Cairo::ImageSurface::create_from_png (path);
146 StatefulImage::set_image_search_path (const std::string& path)
148 _image_search_path = PBD::Searchpath (path);
152 StatefulImage::set_text (const std::string& text)
156 /* never alters bounding box */
162 StatefulImage::set_state (States::size_type n)
164 if (n >= _states.size()) {