2 Copyright (C) 2011-2013 Paul Davis
3 Author: Carl Hetherington <cth@carlh.net>
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.
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.
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.
21 #include <cairomm/context.h>
22 #include "pbd/stacktrace.h"
23 #include "pbd/compose.h"
25 #include "canvas/canvas.h"
26 #include "canvas/rectangle.h"
27 #include "canvas/debug.h"
28 #include "canvas/utils.h"
31 using namespace ArdourCanvas;
33 Rectangle::Rectangle (Group* parent)
37 , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
41 Rectangle::Rectangle (Group* parent, Rect const & rect)
46 , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
52 Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
54 Rect self = item_to_window (_rect);
55 boost::optional<Rect> r = self.intersection (area);
58 std::cerr << whatami() << '/' << name << " not covered by render area! ... " << self << " vs. " << area << std::endl;
66 setup_fill_context (context);
68 setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
70 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
76 setup_outline_context (context);
78 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
80 context->rectangle (self.x0 + 0.5, self.y0 + 0.5, self.width(), self.height());
84 context->set_line_cap (Cairo::LINE_CAP_SQUARE);
86 /* see the cairo FAQ on single pixel lines to see why we do
87 * this expansion of the perimeter.
90 if (_outline_what & LEFT) {
91 /* vertical line: move x-coordinate by 0.5 pixels */
92 context->move_to (self.x0 + 0.5, self.y0);
93 context->line_to (self.x0 + 0.5, self.y1);
96 if (_outline_what & BOTTOM) {
97 /* horizontal line: move y-coordinate by 0.5 pixels */
98 context->move_to (self.x0, self.y1 - 0.5);
99 context->line_to (self.x1, self.y1 - 0.5);
102 if (_outline_what & RIGHT) {
103 /* vertical line: move x-coordinate by 0.5 pixels */
104 context->move_to (self.x1 - 0.5, self.y0);
105 context->line_to (self.x1 - 0.5, self.y1);
108 if (_outline_what & TOP) {
109 /* horizontal line: move y-coordinate by 0.5 pixels */
110 context->move_to (self.x0, self.y0 + 0.5);
111 context->line_to (self.x1, self.y0 + 0.5);
120 Rectangle::compute_bounding_box () const
122 if (!_rect.empty()) {
123 Rect r = _rect.fix ();
125 /* our outlines are always inside our coordinates, but we have
126 * to ensure that our bounding box fully *contains* the
129 * XXX: or something like that, waffle.
132 _bounding_box = r.expand (1.0);
135 _bounding_box_dirty = false;
139 Rectangle::set (Rect const & r)
141 /* We don't update the bounding box here; it's just
142 as cheap to do it when asked.
149 _bounding_box_dirty = true;
152 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (set)\n");
156 Rectangle::set_x0 (Coord x0)
162 _bounding_box_dirty = true;
165 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x0)\n");
169 Rectangle::set_y0 (Coord y0)
175 _bounding_box_dirty = true;
178 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y0)\n");
182 Rectangle::set_x1 (Coord x1)
188 _bounding_box_dirty = true;
191 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x1)\n");
195 Rectangle::set_y1 (Coord y1)
201 _bounding_box_dirty = true;
204 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y1)\n");
208 Rectangle::set_outline_what (What what)
212 _outline_what = what;
218 Rectangle::set_outline_what (int what)
220 set_outline_what ((What) what);