Merge branch 'canvasredesign' into cairocanvas
[ardour.git] / libs / canvas / text.cc
1 /*
2     Copyright (C) 2011-2013 Paul Davis
3     Author: Carl Hetherington <cth@carlh.net>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gdk/gdk.h>
21
22 #include <cairomm/cairomm.h>
23 #include <gtkmm/label.h>
24
25 #include "pbd/stacktrace.h"
26
27 #include "canvas/text.h"
28 #include "canvas/canvas.h"
29 #include "canvas/utils.h"
30
31 using namespace std;
32 using namespace ArdourCanvas;
33
34 Text::Text (Canvas* c)
35         : Item (c)
36         , _color (0x000000ff)
37         , _font_description (0)
38         , _alignment (Pango::ALIGN_LEFT)
39         , _width (0)
40         , _height (0)
41         , _need_redraw (false)
42         , _clamped_width (COORD_MAX)
43 {
44 }
45
46 Text::Text (Item* parent)
47         : Item (parent)
48         , _color (0x000000ff)
49         , _font_description (0)
50         , _alignment (Pango::ALIGN_LEFT)
51         , _width (0)
52         , _height (0)
53         , _need_redraw (false)
54         , _clamped_width (COORD_MAX)
55 {
56 }
57
58 Text::~Text ()
59 {
60         delete _font_description;
61 }
62
63 void
64 Text::set (string const & text)
65 {
66         begin_change ();
67         
68         _text = text;
69
70         _need_redraw = true;
71         _bounding_box_dirty = true;
72
73         end_change ();
74 }
75
76 void
77 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
78 {
79         if (_text.empty()) {
80                 return;
81         }
82
83         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
84
85         _redraw (layout);
86 }
87
88 void
89 Text::redraw (Glib::RefPtr<Pango::Context> context) const
90 {
91         if (_text.empty()) {
92                 return;
93         }
94
95         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
96         _redraw (layout);
97 }
98
99 void
100 Text::_redraw (Glib::RefPtr<Pango::Layout> layout) const
101 {
102         layout->set_text (_text);
103
104         if (_font_description) {
105                 layout->set_font_description (*_font_description);
106         }
107
108         layout->set_alignment (_alignment);
109
110         int w;
111         int h;
112
113         layout->get_pixel_size (w, h);
114
115         _width = w;
116         _height = h;
117
118         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
119
120         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
121
122         /* and draw, in the appropriate color of course */
123
124         set_source_rgba (img_context, _color);
125
126         layout->show_in_cairo_context (img_context);
127
128         /* text has now been rendered in _image and is ready for blit in
129          * ::render 
130          */
131
132         _need_redraw = false;
133 }
134
135 void
136 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
137 {
138         if (_text.empty()) {
139                 return;
140         }
141
142         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
143         boost::optional<Rect> i = self.intersection (area);
144         
145         if (!i) {
146                 return;
147         }
148
149         if (_need_redraw) {
150                 redraw (context);
151         }
152         
153         Rect intersection (i.get());
154
155         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
156         context->set_source (_image, self.x0, self.y0);
157         context->fill ();
158 }
159
160 void
161 Text::clamp_width (double w)
162 {
163         _clamped_width = w;
164 }
165
166 void
167 Text::compute_bounding_box () const
168 {
169         if (!_canvas || _text.empty()) {
170                 _bounding_box = boost::optional<Rect> ();
171                 _bounding_box_dirty = false;
172                 return;
173         }
174
175         if (_bounding_box_dirty) {
176                 if (_need_redraw || !_image) {
177                         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
178                         redraw (context);
179                 }
180                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
181                 _bounding_box_dirty = false;
182         }
183 }
184
185 void
186 Text::set_alignment (Pango::Alignment alignment)
187 {
188         begin_change ();
189         
190         _alignment = alignment;
191         _need_redraw = true;
192         _bounding_box_dirty = true;
193         end_change ();
194 }
195
196 void
197 Text::set_font_description (Pango::FontDescription font_description)
198 {
199         begin_change ();
200         
201         _font_description = new Pango::FontDescription (font_description);
202         _need_redraw = true;
203
204         _bounding_box_dirty = true;
205         end_change ();
206 }
207
208 void
209 Text::set_color (Color color)
210 {
211         begin_change ();
212
213         _color = color;
214         _need_redraw = true;
215
216         end_change ();
217 }
218
219                 
220 void
221 Text::dump (ostream& o) const
222 {
223         Item::dump (o);
224
225         o << _canvas->indent() << '\t' << " text = " << _text << endl
226           << _canvas->indent() << " color = " << _color;
227
228         o << endl;
229 }