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