Fix method hiding in Canvas::Text
[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 #include "canvas/colors.h"
31
32 using namespace std;
33 using namespace ArdourCanvas;
34
35 Text::Text (Canvas* c)
36         : Item (c)
37         , _color (0x000000ff)
38         , _font_description (0)
39         , _alignment (Pango::ALIGN_LEFT)
40         , _width (0)
41         , _height (0)
42         , _need_redraw (false)
43         , _clamped_width (COORD_MAX)
44 {
45         _outline = false;
46 }
47
48 Text::Text (Item* parent)
49         : Item (parent)
50         , _color (0x000000ff)
51         , _font_description (0)
52         , _alignment (Pango::ALIGN_LEFT)
53         , _width (0)
54         , _height (0)
55         , _need_redraw (false)
56         , _clamped_width (COORD_MAX)
57 {
58         _outline = false;
59 }
60
61 Text::~Text ()
62 {
63         delete _font_description;
64 }
65
66 void
67 Text::set (string const & text)
68 {
69         begin_change ();
70         
71         _text = text;
72
73         _need_redraw = true;
74         _bounding_box_dirty = true;
75
76         end_change ();
77 }
78
79 void
80 Text::_redraw (Cairo::RefPtr<Cairo::Context> context) const
81 {
82         if (_text.empty()) {
83                 return;
84         }
85
86         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
87
88         __redraw (layout);
89 }
90
91 void
92 Text::_redraw (Glib::RefPtr<Pango::Context> context) const
93 {
94         if (_text.empty()) {
95                 return;
96         }
97
98         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
99         __redraw (layout);
100 }
101
102 void
103 Text::__redraw (Glib::RefPtr<Pango::Layout> layout) const
104 {
105         layout->set_text (_text);
106
107         if (_font_description) {
108                 layout->set_font_description (*_font_description);
109         }
110
111         layout->set_alignment (_alignment);
112
113         int w;
114         int h;
115
116         layout->get_pixel_size (w, h);
117
118         _width = w;
119         _height = h;
120
121         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
122
123         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
124
125         /* and draw, in the appropriate color of course */
126
127         if (_outline) {
128                 set_source_rgba (img_context, _outline_color);
129                 layout->update_from_cairo_context (img_context);
130                 pango_cairo_layout_path (img_context->cobj(), layout->gobj());
131                 img_context->stroke_preserve ();
132                 set_source_rgba (img_context, _color);
133                 img_context->fill ();
134         } else {
135                 set_source_rgba (img_context, _color);
136                 layout->show_in_cairo_context (img_context);
137         }
138
139         /* text has now been rendered in _image and is ready for blit in
140          * ::render 
141          */
142
143         _need_redraw = false;
144 }
145
146 void
147 Text::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
148 {
149         if (_text.empty()) {
150                 return;
151         }
152
153         Rect self = item_to_window (Rect (0, 0, min (_clamped_width, (double)_image->get_width ()), _image->get_height ()));
154         boost::optional<Rect> i = self.intersection (area);
155         
156         if (!i) {
157                 return;
158         }
159
160         if (_need_redraw) {
161                 _redraw (context);
162         }
163         
164         Rect intersection (i.get());
165
166         context->rectangle (intersection.x0, intersection.y0, intersection.width(), intersection.height());
167         context->set_source (_image, self.x0, self.y0);
168         context->fill ();
169 }
170
171 void
172 Text::clamp_width (double w)
173 {
174         begin_change ();
175         _clamped_width = w;
176         _bounding_box_dirty = true;
177         end_change ();
178 }
179
180 void
181 Text::compute_bounding_box () const
182 {
183         if (!_canvas || _text.empty()) {
184                 _bounding_box = boost::optional<Rect> ();
185                 _bounding_box_dirty = false;
186                 return;
187         }
188
189         if (_bounding_box_dirty) {
190                 if (_need_redraw || !_image) {
191                         Glib::RefPtr<Pango::Context> context = Glib::wrap (gdk_pango_context_get()); // context now owns C object and will free it
192                         _redraw (context);
193                 }
194                 _bounding_box = Rect (0, 0, min (_clamped_width, (double) _image->get_width()), _image->get_height());
195                 _bounding_box_dirty = false;
196         }
197 }
198
199 void
200 Text::set_alignment (Pango::Alignment alignment)
201 {
202         begin_change ();
203         
204         _alignment = alignment;
205         _need_redraw = true;
206         _bounding_box_dirty = true;
207         end_change ();
208 }
209
210 void
211 Text::set_font_description (Pango::FontDescription font_description)
212 {
213         begin_change ();
214         
215         _font_description = new Pango::FontDescription (font_description);
216         _need_redraw = true;
217
218         _bounding_box_dirty = true;
219         end_change ();
220 }
221
222 void
223 Text::set_color (Color color)
224 {
225         begin_change ();
226
227         _color = color;
228         if (_outline) {
229                 set_outline_color (contrasting_text_color (_color));
230         }
231         _need_redraw = true;
232
233         end_change ();
234 }
235
236                 
237 void
238 Text::dump (ostream& o) const
239 {
240         Item::dump (o);
241
242         o << _canvas->indent() << '\t' << " text = " << _text << endl
243           << _canvas->indent() << " color = " << _color;
244
245         o << endl;
246 }