c6381776e4ff4f554df5c8c694e5c1241d48763c
[ardour.git] / libs / canvas / rectangle.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 <iostream>
21 #include <cairomm/context.h>
22 #include "pbd/stacktrace.h"
23 #include "pbd/compose.h"
24
25 #include "canvas/canvas.h"
26 #include "canvas/rectangle.h"
27 #include "canvas/debug.h"
28 #include "canvas/utils.h"
29
30 using namespace std;
31 using namespace ArdourCanvas;
32
33 Rectangle::Rectangle (Canvas* c)
34         : Item (c)
35         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
36 {
37 }
38
39 Rectangle::Rectangle (Canvas* c, Rect const & rect)
40         : Item (c)
41         , _rect (rect)
42         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
43 {
44 }
45
46 Rectangle::Rectangle (Item* parent)
47         : Item (parent)
48         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
49 {
50 }
51
52 Rectangle::Rectangle (Item* parent, Rect const & rect)
53         : Item (parent)
54         , _rect (rect)
55         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
56 {
57 }
58
59 Rect
60 Rectangle::get_self_for_render () const
61 {
62         /* In general, a Rectangle will have a _position of (0,0) within its
63            parent, and its extent is actually defined by _rect. But in the
64            unusual case that _position is set to something other than (0,0),
65            we should take that into account when rendering.
66         */
67
68         return item_to_window (_rect.translate (_position));
69 }
70
71 void
72 Rectangle::render_self (Rect const & area, Cairo::RefPtr<Cairo::Context> context, Rect self) const
73 {
74         boost::optional<Rect> r = self.intersection (area);
75
76         if (!r) {
77                 return;
78         }
79
80         Rect draw = r.get ();
81
82         if (_fill && !_transparent) {
83                 if (_stops.empty()) {
84                         setup_fill_context (context);
85                 } else {
86                         setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
87                 }
88
89                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
90                 context->fill ();
91         } 
92         
93         if (_outline) {
94
95                 setup_outline_context (context);
96                 
97                 /* the goal here is that if the border is 1 pixel
98                  * thick, it will precisely align with the corner
99                  * coordinates of the rectangle. So if the rectangle
100                  * has a left edge at 0 and a right edge at 10, then
101                  * the left edge must span -0.5..+0.5, the right edge
102                  * must span 9.5..10.5 (i.e. the single full color
103                  * pixel is precisely aligned with 0 and 10
104                  * respectively).
105                  *
106                  * we have to shift left/up in all cases, which means
107                  * subtraction along both axes (i.e. edge at
108                  * N, outline must start at N-0.5). 
109                  *
110                  * see the cairo FAQ on single pixel lines to see why we do
111                  * the 0.5 pixel additions.
112                  */
113
114                 self = self.translate (Duple (0.5, 0.5));
115
116                 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
117                         
118                         context->rectangle (self.x0, self.y0, self.width(), self.height());
119
120                 } else {
121
122                         if (_outline_what & LEFT) {
123                                 context->move_to (self.x0, self.y0);
124                                 context->line_to (self.x0, self.y1);
125                         }
126                         
127                         if (_outline_what & TOP) {
128                                 context->move_to (self.x0, self.y0);
129                                 context->line_to (self.x1, self.y0);
130                         }
131
132                         if (_outline_what & BOTTOM) {
133                                 context->move_to (self.x0, self.y1);
134                                 context->line_to (self.x1, self.y1);
135                         }
136                         
137                         if (_outline_what & RIGHT) {
138                                 context->move_to (self.x1, self.y0);
139                                 context->line_to (self.x1, self.y1);
140                         }
141                 }
142                 
143                 context->stroke ();
144         }
145 }
146
147 void
148 Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
149 {
150         render_self (area, context, get_self_for_render ());
151 }
152
153 void
154 Rectangle::compute_bounding_box () const
155 {
156         if (!_rect.empty()) {
157                 Rect r = _rect.fix ();
158
159                 /* take into acount the 0.5 addition to the bounding
160                    box for the right and bottom edges, see ::render() above
161                 */
162
163                 r = r.expand (1.0);
164
165                 _bounding_box = r;
166         }
167
168         _bounding_box_dirty = false;
169 }
170
171 void
172 Rectangle::set (Rect const & r)
173 {
174         /* We don't update the bounding box here; it's just
175            as cheap to do it when asked.
176         */
177
178         if (r != _rect) {
179                 
180                 begin_change ();
181                 
182                 _rect = r;
183                 
184                 _bounding_box_dirty = true;
185                 end_change ();
186         }
187 }
188
189 void
190 Rectangle::set_x0 (Coord x0)
191 {
192         if (x0 != _rect.x0) {
193                 begin_change ();
194                 
195                 _rect.x0 = x0;
196                 
197                 _bounding_box_dirty = true;
198                 end_change ();
199         }
200 }
201
202 void
203 Rectangle::set_y0 (Coord y0)
204 {
205         if (y0 != _rect.y0) {
206                 begin_change ();
207                 
208                 _rect.y0 = y0;
209                 
210                 _bounding_box_dirty = true;
211                 end_change();
212         }
213 }
214
215 void
216 Rectangle::set_x1 (Coord x1)
217 {
218         if (x1 != _rect.x1) {
219                 begin_change ();
220                 
221                 _rect.x1 = x1;
222                 
223                 _bounding_box_dirty = true;
224                 end_change ();
225         }
226 }
227
228 void
229 Rectangle::set_y1 (Coord y1)
230 {
231         if (y1 != _rect.y1) {
232                 begin_change ();
233                 
234                 _rect.y1 = y1;
235                 
236                 _bounding_box_dirty = true;
237                 end_change ();
238         }
239 }
240
241 void
242 Rectangle::set_outline_what (What what)
243 {
244         if (what != _outline_what) {
245                 begin_visual_change ();
246                 _outline_what = what;
247                 end_visual_change ();
248         }
249 }
250
251
252 /*-------------------*/
253
254 void
255 TimeRectangle::compute_bounding_box () const
256 {
257         Rectangle::compute_bounding_box ();
258         assert (_bounding_box);
259
260         Rect r = _bounding_box.get ();
261
262         /* This is a TimeRectangle, so its right edge is drawn 1 pixel beyond
263          * (larger x-axis coordinates) than a normal Rectangle.
264          */
265         
266         r.x1 += 1.0; /* this should be using safe_add() */
267         
268         _bounding_box = r;
269 }
270
271 void 
272 TimeRectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
273 {
274         Rect self = get_self_for_render ();
275
276
277         /* This is a TimeRectangle, so its right edge is drawn 1 pixel beyond
278          * (larger x-axis coordinates) than a normal Rectangle.
279          */
280
281         self.x1 += 1.0; /* this should be using safe_add() */
282         render_self (area, context, self);
283 }