remove all XML related API from canvas. it may have been useful during development...
[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         , _clamped_width (COORD_MAX)
25 {
26
27 }
28
29 Text::~Text ()
30 {
31         delete _font_description;
32 }
33
34 void
35 Text::set (string const & text)
36 {
37         begin_change ();
38         
39         _text = text;
40
41         _need_redraw = true;
42         _bounding_box_dirty = true;
43
44         end_change ();
45 }
46
47 void
48 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
49 {
50         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
51
52         layout->set_text (_text);
53
54         if (_font_description) {
55                 layout->set_font_description (*_font_description);
56         }
57
58         layout->set_alignment (_alignment);
59         
60         Pango::Rectangle ink_rect = layout->get_ink_extents();
61         
62         _origin.x = ink_rect.get_x() / Pango::SCALE;
63         _origin.y = ink_rect.get_y() / Pango::SCALE;
64
65         _width = _origin.x + (ink_rect.get_width() / Pango::SCALE);
66         _height = _origin.y + (ink_rect.get_height() / Pango::SCALE);
67         
68         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
69
70         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
71
72         /* and draw, in the appropriate color of course */
73
74         set_source_rgba (img_context, _color);
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::clamp_width (double w)
86 {
87         _clamped_width = w;
88 }
89
90 void
91 Text::compute_bounding_box () const
92 {
93         if (!_canvas || _text.empty()) {
94                 _bounding_box = boost::optional<Rect> ();
95                 _bounding_box_dirty = false;
96                 return;
97         }
98
99         PangoContext* _pc = gdk_pango_context_get ();
100         Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
101         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
102         
103         layout->set_text (_text);
104         layout->set_font_description (*_font_description);
105         layout->set_alignment (_alignment);
106         Pango::Rectangle const r = layout->get_ink_extents ();
107         
108         _bounding_box = Rect (
109                 r.get_x() / Pango::SCALE,
110                 r.get_y() / Pango::SCALE,
111                 (r.get_x() + r.get_width()) / Pango::SCALE,
112                 (r.get_y() + r.get_height()) / Pango::SCALE
113                 );
114                 
115         _bounding_box_dirty = false;
116 }
117
118 void
119 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
120 {
121         if (_text.empty()) {
122                 return;
123         }
124
125         if (_need_redraw) {
126                 redraw (context);
127         }
128         
129         context->set_source (_image, 0, 0);
130         context->rectangle (0, 0, min (_clamped_width, _width), _height);
131         context->fill ();
132 }
133
134 void
135 Text::set_alignment (Pango::Alignment alignment)
136 {
137         begin_change ();
138         
139         _alignment = alignment;
140         _need_redraw = true;
141         _bounding_box_dirty = true;
142         end_change ();
143 }
144
145 void
146 Text::set_font_description (Pango::FontDescription font_description)
147 {
148         begin_change ();
149         
150         _font_description = new Pango::FontDescription (font_description);
151         _need_redraw = true;
152
153         _bounding_box_dirty = true;
154         end_change ();
155 }
156
157 void
158 Text::set_color (Color color)
159 {
160         begin_change ();
161
162         _color = color;
163         _need_redraw = true;
164
165         end_change ();
166 }
167
168                 
169 void
170 Text::dump (ostream& o) const
171 {
172         Item::dump (o);
173
174         o << _canvas->indent() << '\t' << " text = " << _text << endl
175           << _canvas->indent() << " color = " << _color;
176
177         o << endl;
178 }