Merge branch 'master' into cairocanvas
[ardour.git] / libs / canvas / rectangle.cc
1 #include <iostream>
2 #include <cairomm/context.h>
3 #include "pbd/stacktrace.h"
4 #include "pbd/xml++.h"
5 #include "pbd/compose.h"
6 #include "canvas/rectangle.h"
7 #include "canvas/debug.h"
8 #include "canvas/utils.h"
9
10 using namespace std;
11 using namespace ArdourCanvas;
12
13 Rectangle::Rectangle (Group* parent)
14         : Item (parent)
15         , Outline (parent)
16         , Fill (parent)
17         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
18 {
19
20 }
21
22 Rectangle::Rectangle (Group* parent, Rect const & rect)
23         : Item (parent)
24         , Outline (parent)
25         , Fill (parent)
26         , _rect (rect)
27         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
28 {
29         
30 }
31
32 void
33 Rectangle::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
34 {
35         Rect plot = _rect;
36
37         plot.x1 = min (plot.x1, CAIRO_MAX);
38         plot.y1 = min (plot.y1, CAIRO_MAX);
39
40         if (_fill) {
41                 setup_fill_context (context);
42                 context->rectangle (plot.x0, plot.y0, plot.width(), plot.height());
43                 context->fill ();
44         }
45
46         if (_outline) {
47                 if (_outline_what & LEFT) {
48                         context->move_to (plot.x0, plot.y0);
49                         context->line_to (plot.x0, plot.y1);
50                 }
51                 
52                 if (_outline_what & BOTTOM) {
53                         context->move_to (plot.x0, plot.y1);
54                         context->line_to (plot.x1, plot.y1);
55                 }
56                 
57                 if (_outline_what & RIGHT) {
58                         context->move_to (plot.x1, plot.y0);
59                         context->line_to (plot.x1, plot.y1);
60                 }
61                 
62                 if (_outline_what & TOP) {
63                         context->move_to (plot.x0, plot.y0);
64                         context->line_to (plot.x0, plot.y1);
65                 }
66                 
67                 setup_outline_context (context);
68                 context->stroke ();
69         }
70 }
71
72 void
73 Rectangle::compute_bounding_box () const
74 {
75         Rect r = _rect.fix ();
76         _bounding_box = boost::optional<Rect> (r.expand (_outline_width / 2));
77         
78         _bounding_box_dirty = false;
79 }
80
81 void
82 Rectangle::set (Rect const & r)
83 {
84         /* We don't update the bounding box here; it's just
85            as cheap to do it when asked.
86         */
87         
88         begin_change ();
89         
90         _rect = r;
91         
92         _bounding_box_dirty = true;
93         end_change ();
94
95         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (set)\n");
96 }
97
98 void
99 Rectangle::set_x0 (Coord x0)
100 {
101         begin_change ();
102
103         _rect.x0 = x0;
104
105         _bounding_box_dirty = true;
106         end_change ();
107
108         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x0)\n");
109 }
110
111 void
112 Rectangle::set_y0 (Coord y0)
113 {
114         begin_change ();
115         
116         _rect.y0 = y0;
117
118         _bounding_box_dirty = true;
119         end_change();
120
121         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y0)\n");
122 }
123
124 void
125 Rectangle::set_x1 (Coord x1)
126 {
127         begin_change ();
128         
129         _rect.x1 = x1;
130
131         _bounding_box_dirty = true;
132         end_change ();
133
134         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x1)\n");
135 }
136
137 void
138 Rectangle::set_y1 (Coord y1)
139 {
140         begin_change ();
141
142         _rect.y1 = y1;
143
144         _bounding_box_dirty = true;
145         end_change ();
146
147         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y1)\n");
148 }
149
150 void
151 Rectangle::set_outline_what (What what)
152 {
153         begin_change ();
154         
155         _outline_what = what;
156
157         end_change ();
158 }
159
160 void
161 Rectangle::set_outline_what (int what)
162 {
163         set_outline_what ((What) what);
164 }
165
166 XMLNode *
167 Rectangle::get_state () const
168 {
169         XMLNode* node = new XMLNode ("Rectangle");
170 #ifdef CANVAS_DEBUG
171         if (!name.empty ()) {
172                 node->add_property ("name", name);
173         }
174 #endif  
175         node->add_property ("x0", string_compose ("%1", _rect.x0));
176         node->add_property ("y0", string_compose ("%1", _rect.y0));
177         node->add_property ("x1", string_compose ("%1", _rect.x1));
178         node->add_property ("y1", string_compose ("%1", _rect.y1));
179         node->add_property ("outline-what", string_compose ("%1", _outline_what));
180
181         add_item_state (node);
182         add_outline_state (node);
183         add_fill_state (node);
184         return node;
185 }
186
187 void
188 Rectangle::set_state (XMLNode const * node)
189 {
190         _rect.x0 = atof (node->property("x0")->value().c_str());
191         _rect.y0 = atof (node->property("y0")->value().c_str());
192         _rect.x1 = atof (node->property("x1")->value().c_str());
193         _rect.y1 = atof (node->property("y1")->value().c_str());
194         _outline_what = (What) atoi (node->property("outline-what")->value().c_str());
195
196         set_item_state (node);
197         set_outline_state (node);
198         set_fill_state (node);
199
200         _bounding_box_dirty = true;
201 }