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))
42 Rectangle::Rectangle (Group* parent, Rect const & rect)
47 , _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
53 Rectangle::render (Rect const & /*area*/, Cairo::RefPtr<Cairo::Context> context) const
55 /* Cairo goes a little (!) wrong when asked to fill/stroke rectangles that
56 * extend way beyond the surface boundaries. To avoid this issue,
57 * clamp what we are drawing using the absolute end of the visible
58 * canvas, converting to item-space coordinates, of course.
62 Rect visible = _canvas->visible_area();
63 Duple visible_end = canvas_to_item (Duple (visible.x1, visible.y1));
65 plot.x1 = min (plot.x1, visible_end.x);
66 plot.y1 = min (plot.y1, visible_end.y);
69 setup_fill_context (context);
70 context->rectangle (plot.x0, plot.y0, plot.width(), plot.height());
76 /* special/common case: outline the entire rectangle is
77 * requested, so just use the same path for the fill
81 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
82 context->fill_preserve();
83 setup_outline_context (context);
93 if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
95 /* if we filled and use full outline, we are already done */
98 context->rectangle (plot.x0, plot.y0, plot.width(), plot.height());
99 setup_outline_context (context);
105 if (_outline_what & LEFT) {
106 context->move_to (plot.x0, plot.y0);
107 context->line_to (plot.x0, plot.y1);
110 if (_outline_what & BOTTOM) {
111 context->move_to (plot.x0, plot.y1);
112 context->line_to (plot.x1, plot.y1);
115 if (_outline_what & RIGHT) {
116 context->move_to (plot.x1, plot.y0);
117 context->line_to (plot.x1, plot.y1);
120 if (_outline_what & TOP) {
121 context->move_to (plot.x0, plot.y0);
122 context->line_to (plot.x1, plot.y0);
125 setup_outline_context (context);
132 Rectangle::compute_bounding_box () const
134 Rect r = _rect.fix ();
135 _bounding_box = boost::optional<Rect> (r.expand (_outline_width / 2));
137 _bounding_box_dirty = false;
141 Rectangle::set (Rect const & r)
143 /* We don't update the bounding box here; it's just
144 as cheap to do it when asked.
151 _bounding_box_dirty = true;
154 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (set)\n");
158 Rectangle::set_x0 (Coord x0)
164 _bounding_box_dirty = true;
167 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x0)\n");
171 Rectangle::set_y0 (Coord y0)
177 _bounding_box_dirty = true;
180 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y0)\n");
184 Rectangle::set_x1 (Coord x1)
190 _bounding_box_dirty = true;
193 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (x1)\n");
197 Rectangle::set_y1 (Coord y1)
203 _bounding_box_dirty = true;
206 DEBUG_TRACE (PBD::DEBUG::CanvasItemsDirtied, "canvas item dirty: rectangle change (y1)\n");
210 Rectangle::set_outline_what (What what)
214 _outline_what = what;
220 Rectangle::set_outline_what (int what)
222 set_outline_what ((What) what);