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