57aa92d040ae2b6b3bd6825613252430c9104f22
[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 (Group* parent)
34         : Item (parent)
35         , Outline (parent)
36         , Fill (parent)
37         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
38 {
39 }
40
41 Rectangle::Rectangle (Group* parent, Rect const & rect)
42         : Item (parent)
43         , Outline (parent)
44         , Fill (parent)
45         , _rect (rect)
46         , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
47 {
48         
49 }
50
51 void
52 Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
53 {
54         Rect self = item_to_window (_rect);
55         boost::optional<Rect> r = self.intersection (area);
56
57         if (!r) {
58                 return;
59         }
60
61         Rect draw = r.get ();
62
63         if (_fill) {
64                 if (_stops.empty()) {
65                         setup_fill_context (context);
66                 } else {
67                         setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
68                 }
69
70                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
71                 context->fill ();
72         } 
73         
74         if (_outline) {
75
76                 setup_outline_context (context);
77                 
78                 /* see the cairo FAQ on single pixel lines to see why we do
79                  * the 0.5 pixel additions.
80                  */
81
82                 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
83                         
84                         context->rectangle (self.x0 + 0.5, self.y0 + 0.5, self.width() - 1.0, self.height() - 1.0);
85
86                 } else {
87
88                         if (_outline_what & LEFT) {
89                                 /* vertical line: move x-coordinate by 0.5 pixels */
90                                 context->move_to (self.x0 + 0.5, self.y0);
91                                 context->line_to (self.x0 + 0.5, self.y1);
92                         }
93                         
94                         if (_outline_what & TOP) {
95                                 /* horizontal line: move y-coordinate by 0.5 pixels */
96                                 context->move_to (self.x0, self.y0 + 0.5);
97                                 context->line_to (self.x1, self.y0 + 0.5);
98                         }
99
100                         /* in theory, you'd expect us to adjust these two by
101                          * MINUS 0.5 pixels. But the way that Cairo apparently
102                          * does rounding can lead that approach to draw on the
103                          * wrong pixel coordinate. So we add 0.5 even here.
104                          */
105
106                         if (_outline_what & BOTTOM) {
107                                 /* horizontal line: move y-coordinate by 0.5 pixels */
108                                 context->move_to (self.x0, self.y1 + 0.5);
109                                 context->line_to (self.x1, self.y1 + 0.5);
110                         }
111                         
112                         if (_outline_what & RIGHT) {
113                                 /* vertical line: move x-coordinate by 0.5 pixels */
114                                 context->move_to (self.x1 + 0.5, self.y0);
115                                 context->line_to (self.x1 + 0.5, self.y1);
116                         }
117                         
118                 }
119                 
120                 context->stroke ();
121         }
122 }
123
124 void
125 Rectangle::compute_bounding_box () const
126 {
127         if (!_rect.empty()) {
128                 Rect r = _rect.fix ();
129                 /* take into acount the 0.5 addition to the bounding
130                    box for the right and bottom edges, see ::render() above
131                 */
132
133                 r.x1 += 0.5;
134                 r.y1 += 0.5;
135
136                 _bounding_box = r;
137         }
138
139         _bounding_box_dirty = false;
140 }
141
142 void
143 Rectangle::set (Rect const & r)
144 {
145         /* We don't update the bounding box here; it's just
146            as cheap to do it when asked.
147         */
148         
149         begin_change ();
150         
151         _rect = r;
152         
153         _bounding_box_dirty = true;
154         end_change ();
155
156         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (set)\n");
157 }
158
159 void
160 Rectangle::set_x0 (Coord x0)
161 {
162         begin_change ();
163
164         _rect.x0 = x0;
165
166         _bounding_box_dirty = true;
167         end_change ();
168
169         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x0)\n");
170 }
171
172 void
173 Rectangle::set_y0 (Coord y0)
174 {
175         begin_change ();
176         
177         _rect.y0 = y0;
178
179         _bounding_box_dirty = true;
180         end_change();
181
182         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y0)\n");
183 }
184
185 void
186 Rectangle::set_x1 (Coord x1)
187 {
188         begin_change ();
189         
190         _rect.x1 = x1;
191
192         _bounding_box_dirty = true;
193         end_change ();
194
195         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x1)\n");
196 }
197
198 void
199 Rectangle::set_y1 (Coord y1)
200 {
201         begin_change ();
202
203         _rect.y1 = y1;
204
205         _bounding_box_dirty = true;
206         end_change ();
207
208         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y1)\n");
209 }
210
211 void
212 Rectangle::set_outline_what (What what)
213 {
214         begin_change ();
215         
216         _outline_what = what;
217
218         end_change ();
219 }
220
221 void
222 Rectangle::set_outline_what (int what)
223 {
224         set_outline_what ((What) what);
225 }
226