add headers to all canvas .cc and .h files
[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 (Group* parent)
35         : Item (parent)
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
47 Text::~Text ()
48 {
49         delete _font_description;
50 }
51
52 void
53 Text::set (string const & text)
54 {
55         begin_change ();
56         
57         _text = text;
58
59         _need_redraw = true;
60         _bounding_box_dirty = true;
61
62         end_change ();
63 }
64
65 void
66 Text::redraw (Cairo::RefPtr<Cairo::Context> context) const
67 {
68         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
69
70         layout->set_text (_text);
71
72         if (_font_description) {
73                 layout->set_font_description (*_font_description);
74         }
75
76         layout->set_alignment (_alignment);
77         
78         Pango::Rectangle ink_rect = layout->get_ink_extents();
79         
80         _origin.x = ink_rect.get_x() / Pango::SCALE;
81         _origin.y = ink_rect.get_y() / Pango::SCALE;
82
83         _width = _origin.x + (ink_rect.get_width() / Pango::SCALE);
84         _height = _origin.y + (ink_rect.get_height() / Pango::SCALE);
85         
86         _image = Cairo::ImageSurface::create (Cairo::FORMAT_ARGB32, _width, _height);
87
88         Cairo::RefPtr<Cairo::Context> img_context = Cairo::Context::create (_image);
89
90         /* and draw, in the appropriate color of course */
91
92         set_source_rgba (img_context, _color);
93         layout->show_in_cairo_context (img_context);
94
95         /* text has now been rendered in _image and is ready for blit in
96          * ::render 
97          */
98
99         _need_redraw = false;
100 }
101
102 void
103 Text::clamp_width (double w)
104 {
105         _clamped_width = w;
106 }
107
108 void
109 Text::compute_bounding_box () const
110 {
111         if (!_canvas || _text.empty()) {
112                 _bounding_box = boost::optional<Rect> ();
113                 _bounding_box_dirty = false;
114                 return;
115         }
116
117         PangoContext* _pc = gdk_pango_context_get ();
118         Glib::RefPtr<Pango::Context> context = Glib::wrap (_pc); // context now owns _pc and will free it
119         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
120         
121         layout->set_text (_text);
122         layout->set_font_description (*_font_description);
123         layout->set_alignment (_alignment);
124         Pango::Rectangle const r = layout->get_ink_extents ();
125         
126         _bounding_box = Rect (
127                 r.get_x() / Pango::SCALE,
128                 r.get_y() / Pango::SCALE,
129                 (r.get_x() + r.get_width()) / Pango::SCALE,
130                 (r.get_y() + r.get_height()) / Pango::SCALE
131                 );
132                 
133         _bounding_box_dirty = false;
134 }
135
136 void
137 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
138 {
139         if (_text.empty()) {
140                 return;
141         }
142
143         if (_need_redraw) {
144                 redraw (context);
145         }
146         
147         context->set_source (_image, 0, 0);
148         context->rectangle (0, 0, min (_clamped_width, _width), _height);
149         context->fill ();
150 }
151
152 void
153 Text::set_alignment (Pango::Alignment alignment)
154 {
155         begin_change ();
156         
157         _alignment = alignment;
158         _need_redraw = true;
159         _bounding_box_dirty = true;
160         end_change ();
161 }
162
163 void
164 Text::set_font_description (Pango::FontDescription font_description)
165 {
166         begin_change ();
167         
168         _font_description = new Pango::FontDescription (font_description);
169         _need_redraw = true;
170
171         _bounding_box_dirty = true;
172         end_change ();
173 }
174
175 void
176 Text::set_color (Color color)
177 {
178         begin_change ();
179
180         _color = color;
181         _need_redraw = true;
182
183         end_change ();
184 }
185
186                 
187 void
188 Text::dump (ostream& o) const
189 {
190         Item::dump (o);
191
192         o << _canvas->indent() << '\t' << " text = " << _text << endl
193           << _canvas->indent() << " color = " << _color;
194
195         o << endl;
196 }