2014-02-17 15:28:18 -05:00
|
|
|
#include <string>
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
#include <pangomm/fontdescription.h>
|
|
|
|
#include <pangomm/layout.h>
|
|
|
|
|
|
|
|
#include "pbd/error.h"
|
|
|
|
#include "pbd/failed_constructor.h"
|
2014-02-17 15:28:18 -05:00
|
|
|
#include "pbd/file_utils.h"
|
2014-02-17 16:05:52 -05:00
|
|
|
#include "pbd/xml++.h"
|
|
|
|
|
|
|
|
#include "canvas/stateful_image.h"
|
|
|
|
#include "canvas/utils.h"
|
|
|
|
|
|
|
|
#include "i18n.h"
|
|
|
|
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
using PBD::error;
|
2014-02-17 15:28:18 -05:00
|
|
|
|
|
|
|
PBD::Searchpath StatefulImage::_image_search_path;
|
2014-02-17 16:05:52 -05:00
|
|
|
StatefulImage::ImageCache StatefulImage::_image_cache;
|
2014-02-17 15:28:18 -05:00
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
StatefulImage::StatefulImage (Canvas* c, const XMLNode& node)
|
|
|
|
: Item (c)
|
2014-02-17 16:05:52 -05:00
|
|
|
, _state (0)
|
|
|
|
, _font (0)
|
2014-02-17 15:28:18 -05:00
|
|
|
, _text_x (0)
|
|
|
|
, _text_y (0)
|
|
|
|
{
|
|
|
|
if (load_states (node)) {
|
|
|
|
throw failed_constructor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StatefulImage::~StatefulImage()
|
|
|
|
{
|
2014-02-17 16:05:52 -05:00
|
|
|
delete _font;
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulImage::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
|
|
|
{
|
|
|
|
if (_states.empty() || _state >= _states.size()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ImageHandle image = _states[_state].image;
|
|
|
|
Rect self = item_to_window (Rect (0, 0, image->get_width(), image->get_height()));
|
|
|
|
|
|
|
|
boost::optional<Rect> draw = self.intersection (area);
|
|
|
|
|
|
|
|
if (!draw) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* move the origin of the image to the right place on the surface
|
|
|
|
("window" coordinates) and render it.
|
|
|
|
*/
|
|
|
|
context->set_source (image, self.x0, self.y0);
|
2014-02-17 16:05:52 -05:00
|
|
|
context->rectangle (draw->x0, draw->y0, draw->width(), draw->height());
|
2014-02-17 15:28:18 -05:00
|
|
|
context->fill ();
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
if (!_text.empty()) {
|
2014-02-17 15:28:18 -05:00
|
|
|
Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
|
|
|
|
|
|
|
|
layout->set_text (_text);
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
if (_font) {
|
|
|
|
layout->set_font_description (*_font);
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// layout->set_alignment (_alignment);
|
|
|
|
set_source_rgba (context, _text_color);
|
|
|
|
context->move_to (_text_x, _text_y);
|
|
|
|
layout->show_in_cairo_context (context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulImage::compute_bounding_box () const
|
|
|
|
{
|
|
|
|
if (!_states.empty()) {
|
|
|
|
|
|
|
|
/* all images are assumed to be the same size */
|
|
|
|
|
|
|
|
_bounding_box = Rect (0, 0, _states[0].image->get_width(), _states[0].image->get_height());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
StatefulImage::load_states (const XMLNode& node)
|
|
|
|
{
|
|
|
|
const XMLNodeList& nodes (node.children());
|
|
|
|
|
|
|
|
_states.clear ();
|
|
|
|
|
|
|
|
for (XMLNodeList::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
|
|
|
|
State s;
|
2014-02-17 16:05:52 -05:00
|
|
|
States::size_type id;
|
2014-02-17 15:28:18 -05:00
|
|
|
const XMLProperty* prop;
|
|
|
|
|
|
|
|
if ((prop = (*i)->property ("id")) == 0) {
|
|
|
|
error << _("no ID for state") << endmsg;
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-30 11:00:03 -04:00
|
|
|
sscanf (prop->value().c_str(), "%" G_GSIZE_FORMAT, &id);
|
2014-02-17 15:28:18 -05:00
|
|
|
|
|
|
|
if ((prop = (*i)->property ("image")) == 0) {
|
|
|
|
error << _("no image for state") << endmsg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s.image = find_image (prop->value())) == 0) {
|
|
|
|
error << string_compose (_("image %1 not found for state"), prop->value()) << endmsg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
if (_states.size() < id) {
|
|
|
|
_states.reserve (id);
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
_states[id] = s;
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
return 0;
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
StatefulImage::ImageHandle
|
|
|
|
StatefulImage::find_image (const std::string& name)
|
|
|
|
{
|
|
|
|
ImageCache::iterator i;
|
|
|
|
|
|
|
|
if ((i = _image_cache.find (name)) != _image_cache.end()) {
|
2014-02-17 16:05:52 -05:00
|
|
|
return i->second;
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
|
2014-06-19 07:23:12 -04:00
|
|
|
if (!find_file (_image_search_path, name, path)) {
|
2014-02-17 15:28:18 -05:00
|
|
|
error << string_compose (_("Image named %1 not found"),
|
|
|
|
name) << endmsg;
|
|
|
|
return ImageHandle();
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
return Cairo::ImageSurface::create_from_png (path);
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulImage::set_image_search_path (const std::string& path)
|
|
|
|
{
|
2014-02-17 16:05:52 -05:00
|
|
|
_image_search_path = PBD::Searchpath (path);
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StatefulImage::set_text (const std::string& text)
|
|
|
|
{
|
|
|
|
_text = text;
|
|
|
|
|
|
|
|
/* never alters bounding box */
|
|
|
|
|
|
|
|
redraw ();
|
|
|
|
}
|
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
bool
|
2014-02-17 15:28:18 -05:00
|
|
|
StatefulImage::set_state (States::size_type n)
|
|
|
|
{
|
2014-02-17 16:05:52 -05:00
|
|
|
if (n >= _states.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-17 15:28:18 -05:00
|
|
|
|
|
|
|
_state = n;
|
2014-02-17 16:05:52 -05:00
|
|
|
redraw ();
|
2014-02-17 15:28:18 -05:00
|
|
|
|
2014-02-17 16:05:52 -05:00
|
|
|
return true;
|
2014-02-17 15:28:18 -05:00
|
|
|
}
|