commit immediately post linking
[ardour.git] / libs / canvas / text.cc
1 #include <gtkmm/label.h>
2 #include "pbd/xml++.h"
3 #include "canvas/text.h"
4 #include "canvas/canvas.h"
5 #include "canvas/utils.h"
6
7 using namespace std;
8 using namespace ArdourCanvas;
9
10 Text::Text (Group* parent)
11         : Item (parent)
12         , _font_description (0)
13         , _color (0x000000ff)
14         , _alignment (Pango::ALIGN_LEFT)
15 {
16
17 }
18
19 Text::~Text ()
20 {
21         delete _font_description;
22 }
23
24 void
25 Text::set (string const & text)
26 {
27         begin_change ();
28         
29         _text = text;
30         
31         _bounding_box_dirty = true;
32         end_change ();
33 }
34
35 void
36 Text::compute_bounding_box () const
37 {
38         if (!_canvas || !_canvas->context ()) {
39                 _bounding_box = boost::optional<Rect> ();
40                 _bounding_box_dirty = false;
41                 return;
42         }
43         
44         Pango::Rectangle const r = layout (_canvas->context())->get_ink_extents ();
45         
46         _bounding_box = Rect (
47                 r.get_x() / Pango::SCALE,
48                 r.get_y() / Pango::SCALE,
49                 (r.get_x() + r.get_width()) / Pango::SCALE,
50                 (r.get_y() + r.get_height()) / Pango::SCALE
51                 );
52         
53         _bounding_box_dirty = false;
54 }
55
56 Glib::RefPtr<Pango::Layout>
57 Text::layout (Cairo::RefPtr<Cairo::Context> context) const
58 {
59         Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (context);
60         layout->set_text (_text);
61         if (_font_description) {
62                 layout->set_font_description (*_font_description);
63         }
64         layout->set_alignment (_alignment);
65         return layout;
66 }
67
68 void
69 Text::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
70 {
71         set_source_rgba (context, _color);
72         layout (context)->show_in_cairo_context (context);
73 }
74
75 XMLNode *
76 Text::get_state () const
77 {
78         XMLNode* node = new XMLNode ("Text");
79 #ifdef CANVAS_DEBUG
80         if (!name.empty ()) {
81                 node->add_property ("name", name);
82         }
83 #endif
84         return node;
85 }
86
87 void
88 Text::set_state (XMLNode const * /*node*/)
89 {
90         /* XXX */
91 }
92
93 void
94 Text::set_alignment (Pango::Alignment alignment)
95 {
96         begin_change ();
97         
98         _alignment = alignment;
99
100         _bounding_box_dirty = true;
101         end_change ();
102 }
103
104 void
105 Text::set_font_description (Pango::FontDescription font_description)
106 {
107         begin_change ();
108         
109         _font_description = new Pango::FontDescription (font_description);
110
111         _bounding_box_dirty = true;
112         end_change ();
113 }
114
115 void
116 Text::set_color (Color color)
117 {
118         begin_change ();
119
120         _color = color;
121
122         end_change ();
123 }
124
125