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