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 /* In general, a Rectangle will have a _position of (0,0) within its
55 parent, and its extent is actually defined by _rect. But in the
56 unusual case that _position is set to something other than (0,0),
57 we should take that into account when rendering.
59 Rect self = item_to_window (_rect.translate (_position));
60 boost::optional<Rect> r = self.intersection (area);
68 if (_fill && !_transparent) {
70 setup_fill_context (context);
72 setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
75 context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
81 setup_outline_context (context);
83 /* see the cairo FAQ on single pixel lines to see why we do
84 * the 0.5 pixel additions.
87 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
89 context->rectangle (self.x0 + 0.5, self.y0 + 0.5, self.width() - 1.0, self.height() - 1.0);
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);
99 if (_outline_what & TOP) {
100 /* horizontal line: move y-coordinate by 0.5 pixels */
101 context->move_to (self.x0, self.y0 + 0.5);
102 context->line_to (self.x1, self.y0 + 0.5);
105 if (_outline_what & BOTTOM) {
106 /* horizontal line: move y-coordinate by 0.5 pixels */
107 context->move_to (self.x0, self.y1 - 0.5);
108 context->line_to (self.x1, self.y1 - 0.5);
111 if (_outline_what & RIGHT) {
112 /* vertical line: move x-coordinate by 0.5 pixels */
113 context->move_to (self.x1 + 0.5, self.y0);
114 context->line_to (self.x1 + 0.5, self.y1);
124 Rectangle::compute_bounding_box () const
126 if (!_rect.empty()) {
127 Rect r = _rect.fix ();
128 /* take into acount the 0.5 addition to the bounding
129 box for the right and bottom edges, see ::render() above
132 r.x1 += 1.0; // XXX this makes no sense but is necessary
138 _bounding_box_dirty = false;
142 Rectangle::set (Rect const & r)
144 /* We don't update the bounding box here; it's just
145 as cheap to do it when asked.
154 _bounding_box_dirty = true;
160 Rectangle::set_x0 (Coord x0)
162 if (x0 != _rect.x0) {
167 _bounding_box_dirty = true;
173 Rectangle::set_y0 (Coord y0)
175 if (y0 != _rect.y0) {
180 _bounding_box_dirty = true;
186 Rectangle::set_x1 (Coord x1)
188 if (x1 != _rect.x1) {
193 _bounding_box_dirty = true;
199 Rectangle::set_y1 (Coord y1)
201 if (y1 != _rect.y1) {
206 _bounding_box_dirty = true;
212 Rectangle::set_outline_what (What what)
214 if (what != _outline_what) {
215 begin_visual_change ();
216 _outline_what = what;
217 end_visual_change ();