fix up some issues with precise placement of ArdourCanvas::Rectangle frame/outline
[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 #ifdef CANVAS_DEBUG
59                 std::cerr << whatami() << '/' << name << " not covered by render area! ... " << self << " vs. " << area << std::endl;
60 #endif
61                 return;
62         }
63
64         Rect draw = r.get ();
65
66         if (_fill) {
67                 if (_stops.empty()) {
68                         setup_fill_context (context);
69                 } else {
70                         setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
71                 }
72
73                 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
74                 context->fill ();
75         } 
76         
77         if (_outline) {
78
79                 setup_outline_context (context);
80                 
81                 /* see the cairo FAQ on single pixel lines to see why we do
82                  * the 0.5 pixel additions.
83                  */
84
85                 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
86                         
87                         context->rectangle (self.x0 + 0.5, self.y0 + 0.5, self.width() - 1.0, self.height() - 1.0);
88
89                 } else {
90
91                         // context->set_line_cap (Cairo::LINE_CAP_SQUARE);
92                         
93                         if (_outline_what & LEFT) {
94                                 /* vertical line: move x-coordinate by 0.5 pixels */
95                                 context->move_to (self.x0 + 0.5, self.y0);
96                                 context->line_to (self.x0 + 0.5, self.y1);
97                         }
98                         
99                         if (_outline_what & BOTTOM) {
100                                 /* horizontal line: move y-coordinate by 0.5 pixels */
101                                 context->move_to (self.x0, self.y1 + 0.5);
102                                 context->line_to (self.x1, self.y1 + 0.5);
103                         }
104                         
105                         if (_outline_what & RIGHT) {
106                                 /* vertical line: move x-coordinate by 0.5 pixels */
107                                 context->move_to (self.x1 + 0.5, self.y0);
108                                 context->line_to (self.x1 + 0.5, self.y1);
109                         }
110                         
111                         if (_outline_what & TOP) {
112                                 /* horizontal line: move y-coordinate by 0.5 pixels */
113                                 context->move_to (self.x0, self.y0 + 0.5);
114                                 context->line_to (self.x1, self.y0 + 0.5);
115                         }
116                 }
117                 
118                 context->stroke ();
119         }
120 }
121
122 void
123 Rectangle::compute_bounding_box () const
124 {
125         if (!_rect.empty()) {
126                 Rect r = _rect.fix ();
127
128                 /* our outlines are always inside our coordinates, but we have
129                  * to ensure that our bounding box fully *contains* the
130                  * rectangle
131                  *
132                  * XXX: or something like that, waffle.
133                  *
134                  */
135                 _bounding_box = _rect.fix ();
136         }
137
138         _bounding_box_dirty = false;
139 }
140
141 void
142 Rectangle::set (Rect const & r)
143 {
144         /* We don't update the bounding box here; it's just
145            as cheap to do it when asked.
146         */
147         
148         begin_change ();
149         
150         _rect = r;
151         
152         _bounding_box_dirty = true;
153         end_change ();
154
155         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (set)\n");
156 }
157
158 void
159 Rectangle::set_x0 (Coord x0)
160 {
161         begin_change ();
162
163         _rect.x0 = x0;
164
165         _bounding_box_dirty = true;
166         end_change ();
167
168         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x0)\n");
169 }
170
171 void
172 Rectangle::set_y0 (Coord y0)
173 {
174         begin_change ();
175         
176         _rect.y0 = y0;
177
178         _bounding_box_dirty = true;
179         end_change();
180
181         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y0)\n");
182 }
183
184 void
185 Rectangle::set_x1 (Coord x1)
186 {
187         begin_change ();
188         
189         _rect.x1 = x1;
190
191         _bounding_box_dirty = true;
192         end_change ();
193
194         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x1)\n");
195 }
196
197 void
198 Rectangle::set_y1 (Coord y1)
199 {
200         begin_change ();
201
202         _rect.y1 = y1;
203
204         _bounding_box_dirty = true;
205         end_change ();
206
207         DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y1)\n");
208 }
209
210 void
211 Rectangle::set_outline_what (What what)
212 {
213         begin_change ();
214         
215         _outline_what = what;
216
217         end_change ();
218 }
219
220 void
221 Rectangle::set_outline_what (int what)
222 {
223         set_outline_what ((What) what);
224 }
225