4d240db3fd81da436a978e9cb1485422ac4bc4f4
[ardour.git] / libs / canvas / text.cc
1 #include <gdk/gdk.h>
2
3 #include <cairomm/cairomm.h>
4 #include <gtkmm/label.h>
5
6 #include "pbd/xml++.h"
7 #include "pbd/stacktrace.h"
8
9 #include "canvas/text.h"
10 #include "canvas/canvas.h"
11 #include "canvas/utils.h"
12
13 using namespace std;
14 using namespace ArdourCanvas;
15
16 Text::Text (Group* parent)
17         : Item (parent)
18         , _color (0x000000ff)
19         , _font_description (0)
20         , _alignment (Pango::ALIGN_LEFT)
21         , _width (0)
22         , _height (0)
23         , _need_redraw (false)
24 {
25
26 }
27
28 Text::~Text ()
29 {
30         delete _font_description;
31 }
32
33 void
34 Text::set (string const & text)
35 {
36         begin_change ();
37         
38         _text = text;
39
40         _need_redraw = true;
41         _bounding_box_dirty = true;
42
43         end_change ();
44 }
45
46 void
47 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
48 {
49         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
50
51         layout->set_text (_text);
52
53         if (_font_description) {
54                 layout->set_font_description (*_font_description);
55         }
56
57         layout->set_alignment (_alignment);
58         
59         Pango::Rectangle ink_rect = layout->get_ink_extents();
60         
61         _origin.x = ink_rect.get_x() / Pango::SCALE;
62         _origin.y = ink_rect.get_y() / Pango::SCALE;
63
64         _width = _origin.x + ((ink_rect.get_width() + Pango::SCALE / 2) / Pango::SCALE);
65         _height = _origin.y + ((ink_rect.get_height() + Pango::SCALE / 2) / Pango::SCALE);
66         
67         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
68
69         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
70
71         /* and draw, in the appropriate color of course */
72
73         set_source_rgba (img_context, _color);
74
75         layout->show_in_cairo_context (img_context);
76
77         /* text has now been rendered in _image and is ready for blit in
78          * ::render 
79          */
80
81         _need_redraw = false;
82 }
83
84 void
85 Text::compute_bounding_box () const
86 {
87         if (!_canvas || _text.empty()) {
88                 _bounding_box = boost::optional<Rect> ();
89                 _bounding_box_dirty = false;
90                 return;
91         }
92
93         PangoContext* _pc = gdk_pango_context_get ();
94         Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
95         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
96         
97         layout->set_text (_text);
98         layout->set_font_description (*_font_description);
99         layout->set_alignment (_alignment);
100         Pango::Rectangle const r = layout->get_ink_extents ();
101         
102         _bounding_box = Rect (
103                 r.get_x() / Pango::SCALE,
104                 r.get_y() / Pango::SCALE,
105                 (r.get_x() + r.get_width()) / Pango::SCALE,
106                 (r.get_y() + r.get_height()) / Pango::SCALE
107                 );
108                 
109         _bounding_box_dirty = false;
110 }
111
112 void
113 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
114 {
115         if (_text.empty()) {
116                 return;
117         }
118
119         if (_need_redraw) {
120                 redraw (context);
121         }
122         
123         context->set_source (_image, 0, 0);
124         context->rectangle (0, 0, _width, _height);
125         context->fill ();
126 }
127
128 XMLNode *
129 Text::get_state () const
130 {
131         XMLNode* node = new XMLNode ("Text");
132 #ifdef CANVAS_DEBUG
133         if (!name.empty ()) {
134                 node->add_property ("name", name);
135         }
136 #endif
137         return node;
138 }
139
140 void
141 Text::set_state (XMLNode const * /*node*/)
142 {
143         /* XXX */
144 }
145
146 void
147 Text::set_alignment (Pango::Alignment alignment)
148 {
149         begin_change ();
150         
151         _alignment = alignment;
152         _need_redraw = true;
153         _bounding_box_dirty = true;
154         end_change ();
155 }
156
157 void
158 Text::set_font_description (Pango::FontDescription font_description)
159 {
160         begin_change ();
161         
162         _font_description = new Pango::FontDescription (font_description);
163         _need_redraw = true;
164
165         _bounding_box_dirty = true;
166         end_change ();
167 }
168
169 void
170 Text::set_color (Color color)
171 {
172         begin_change ();
173
174         _color = color;
175         _need_redraw = true;
176
177         end_change ();
178 }
179
180                 
181 void
182 Text::dump (ostream& o) const
183 {
184         Item::dump (o);
185
186         o << _canvas->indent() << '\t' << " text = " << _text << endl
187           << _canvas->indent() << " color = " << _color;
188
189         o << endl;
190 }