2013-04-15 22:10:18 -04:00
|
|
|
/*
|
2019-08-02 22:40:09 -04:00
|
|
|
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
|
|
|
|
* Copyright (C) 2013-2018 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-04-15 22:10:18 -04:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include <iostream>
|
|
|
|
#include <cairomm/context.h>
|
2018-11-14 22:42:59 -05:00
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "pbd/compose.h"
|
2013-04-17 10:53:17 -04:00
|
|
|
|
2018-11-14 22:42:59 -05:00
|
|
|
#include "gtkmm2ext/utils.h"
|
|
|
|
|
2013-04-17 10:53:17 -04:00
|
|
|
#include "canvas/canvas.h"
|
2013-04-04 00:32:52 -04:00
|
|
|
#include "canvas/rectangle.h"
|
|
|
|
#include "canvas/debug.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ArdourCanvas;
|
|
|
|
|
2018-11-25 19:08:51 -05:00
|
|
|
/* Rectangle notes
|
|
|
|
*
|
|
|
|
* A Rectangle is defined by an ArdourCanvas::Rect which encloses every pixel
|
|
|
|
* that the Rectangle might draw. If the Rect is (0, 0, 10, 10) then a filled
|
|
|
|
* version of the Rectangle will paint every pixel up to and including those
|
|
|
|
* edges.
|
|
|
|
*
|
|
|
|
* If there is a border to the Rectangle, it will be drawn *INSIDE* the
|
|
|
|
* boundary given by the Rect. The border will include (at minimum) the outer
|
|
|
|
* pixel edge(s) of the Rectangle.
|
|
|
|
*
|
|
|
|
* This makes ArdourCanvas::Rectangle follow the semantics implied by CSS's
|
|
|
|
* box-sizing: border-box, rather than box-sizing: content-box
|
|
|
|
*/
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
Rectangle::Rectangle (Canvas* c)
|
|
|
|
: Item (c)
|
2013-04-04 00:32:52 -04:00
|
|
|
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
|
2018-11-14 22:42:59 -05:00
|
|
|
, _corner_radius (0.0)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-12 14:53:44 -04:00
|
|
|
Rectangle::Rectangle (Canvas* c, Rect const & rect)
|
|
|
|
: Item (c)
|
|
|
|
, _rect (rect)
|
|
|
|
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
|
2018-11-14 22:42:59 -05:00
|
|
|
, _corner_radius (0.0)
|
2014-06-12 14:53:44 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
Rectangle::Rectangle (Item* parent)
|
|
|
|
: Item (parent)
|
2014-06-12 14:53:44 -04:00
|
|
|
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
|
2018-11-14 22:42:59 -05:00
|
|
|
, _corner_radius (0.0)
|
2014-06-12 14:53:44 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-06-21 11:43:42 -04:00
|
|
|
Rectangle::Rectangle (Item* parent, Rect const & rect)
|
|
|
|
: Item (parent)
|
2013-04-04 00:32:52 -04:00
|
|
|
, _rect (rect)
|
|
|
|
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
|
2018-11-14 22:42:59 -05:00
|
|
|
, _corner_radius (0.0)
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-03 09:22:18 -04:00
|
|
|
void
|
|
|
|
Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
|
2013-04-04 00:32:52 -04:00
|
|
|
{
|
2018-10-21 22:14:44 -04:00
|
|
|
/* Note that item_to_window() already takes _position into account (as
|
|
|
|
part of item_to_canvas()
|
2014-06-06 08:32:05 -04:00
|
|
|
*/
|
2014-11-03 21:33:54 -05:00
|
|
|
|
2018-11-25 19:08:51 -05:00
|
|
|
Rect self (item_to_window (_rect));
|
2018-07-03 09:22:18 -04:00
|
|
|
const Rect draw = self.intersection (area);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2018-07-03 09:22:18 -04:00
|
|
|
if (!draw) {
|
2013-06-18 13:46:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-14 22:42:59 -05:00
|
|
|
if (_corner_radius) {
|
|
|
|
context->save ();
|
|
|
|
Gtkmm2ext::rounded_rectangle (context, self.x0, self.y0, self.width(), self.height(), _corner_radius);
|
|
|
|
context->clip ();
|
|
|
|
}
|
|
|
|
|
2014-03-07 12:29:26 -05:00
|
|
|
if (_fill && !_transparent) {
|
2013-06-27 10:41:00 -04:00
|
|
|
if (_stops.empty()) {
|
|
|
|
setup_fill_context (context);
|
|
|
|
} else {
|
2023-02-04 12:19:04 -05:00
|
|
|
setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
|
2013-06-27 10:41:00 -04:00
|
|
|
}
|
2014-01-10 14:36:05 -05:00
|
|
|
|
2018-11-14 22:42:59 -05:00
|
|
|
if (_corner_radius) {
|
|
|
|
Gtkmm2ext::rounded_rectangle (context, draw.x0, draw.y0, draw.width(), draw.height(), _corner_radius);
|
|
|
|
} else {
|
|
|
|
context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
|
|
|
|
}
|
2013-06-24 16:28:53 -04:00
|
|
|
context->fill ();
|
2015-10-04 14:51:05 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2016-09-25 15:45:10 -04:00
|
|
|
if (_outline && _outline_width && _outline_what) {
|
2013-06-24 16:28:53 -04:00
|
|
|
|
2013-05-02 18:21:35 -04:00
|
|
|
setup_outline_context (context);
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2018-11-25 19:08:51 -05:00
|
|
|
const double shift = _outline_width * 0.5;
|
|
|
|
self = self.translate (Duple (shift, shift));
|
|
|
|
|
2021-07-27 18:54:08 -04:00
|
|
|
if (_outline_what == ALL) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2018-11-14 22:42:59 -05:00
|
|
|
if (_corner_radius) {
|
2018-11-25 19:08:51 -05:00
|
|
|
Gtkmm2ext::rounded_rectangle (context, self.x0, self.y0, self.width() - _outline_width, self.height() - _outline_width, _corner_radius);
|
2018-11-14 22:42:59 -05:00
|
|
|
} else {
|
2018-11-25 19:08:51 -05:00
|
|
|
context->rectangle (self.x0, self.y0, self.width() - _outline_width, self.height() - _outline_width);
|
2018-11-14 22:42:59 -05:00
|
|
|
}
|
2013-10-31 11:49:36 -04:00
|
|
|
|
2014-01-07 20:56:36 -05:00
|
|
|
} else {
|
|
|
|
|
|
|
|
if (_outline_what & LEFT) {
|
2015-02-11 21:56:29 -05:00
|
|
|
context->move_to (self.x0, self.y0);
|
|
|
|
context->line_to (self.x0, self.y1);
|
2014-01-07 20:56:36 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-02-14 09:49:16 -05:00
|
|
|
if (_outline_what & TOP) {
|
2015-02-11 21:56:29 -05:00
|
|
|
context->move_to (self.x0, self.y0);
|
|
|
|
context->line_to (self.x1, self.y0);
|
2014-02-14 09:49:16 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 20:56:36 -05:00
|
|
|
if (_outline_what & BOTTOM) {
|
2015-02-11 21:56:29 -05:00
|
|
|
context->move_to (self.x0, self.y1);
|
|
|
|
context->line_to (self.x1, self.y1);
|
2014-01-07 20:56:36 -05:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-01-07 20:56:36 -05:00
|
|
|
if (_outline_what & RIGHT) {
|
2015-02-11 21:56:29 -05:00
|
|
|
context->move_to (self.x1, self.y0);
|
|
|
|
context->line_to (self.x1, self.y1);
|
2014-01-07 20:56:36 -05:00
|
|
|
}
|
2013-06-24 16:28:53 -04:00
|
|
|
}
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-06-24 16:28:53 -04:00
|
|
|
context->stroke ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2018-11-10 21:51:53 -05:00
|
|
|
|
2018-11-14 22:42:59 -05:00
|
|
|
if (_corner_radius) {
|
|
|
|
context->restore ();
|
|
|
|
}
|
|
|
|
|
2018-11-10 21:51:53 -05:00
|
|
|
render_children (area, context);
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
2018-11-21 14:06:47 -05:00
|
|
|
void
|
|
|
|
Rectangle::size_request (double& w, double& h) const
|
|
|
|
{
|
|
|
|
w = _rect.width();
|
|
|
|
h = _rect.height();
|
|
|
|
}
|
|
|
|
|
2013-04-04 00:32:52 -04:00
|
|
|
void
|
|
|
|
Rectangle::compute_bounding_box () const
|
|
|
|
{
|
2023-09-06 20:42:25 -04:00
|
|
|
if (_rect.empty ()) {
|
|
|
|
_bounding_box = Rect ();
|
|
|
|
} else if (_outline && _outline_width && _outline_what) {
|
|
|
|
_bounding_box = _rect.fix().expand (ceil (_outline_width * 0.5));
|
|
|
|
} else {
|
|
|
|
_bounding_box = _rect.fix();
|
2014-01-08 10:42:14 -05:00
|
|
|
}
|
|
|
|
|
2022-04-27 00:02:22 -04:00
|
|
|
set_bbox_clean ();
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set (Rect const & r)
|
|
|
|
{
|
|
|
|
/* We don't update the bounding box here; it's just
|
|
|
|
as cheap to do it when asked.
|
|
|
|
*/
|
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
if (r != _rect) {
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_rect = r;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_x0 (Coord x0)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (x0 != _rect.x0) {
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_rect.x0 = x0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_y0 (Coord y0)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (y0 != _rect.y0) {
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_rect.y0 = y0;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_x1 (Coord x1)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (x1 != _rect.x1) {
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_rect.x1 = x1;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_y1 (Coord y1)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (y1 != _rect.y1) {
|
|
|
|
begin_change ();
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2014-03-11 07:36:09 -04:00
|
|
|
_rect.y1 = y1;
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2022-04-26 23:57:39 -04:00
|
|
|
set_bbox_dirty ();
|
2014-03-11 07:36:09 -04:00
|
|
|
end_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_outline_what (What what)
|
|
|
|
{
|
2014-03-11 07:36:09 -04:00
|
|
|
if (what != _outline_what) {
|
|
|
|
begin_visual_change ();
|
|
|
|
_outline_what = what;
|
|
|
|
end_visual_change ();
|
|
|
|
}
|
2013-04-04 00:32:52 -04:00
|
|
|
}
|
2014-11-24 06:09:09 -05:00
|
|
|
|
|
|
|
double
|
|
|
|
Rectangle::vertical_fraction (double y) const
|
|
|
|
{
|
|
|
|
/* y is in canvas coordinates */
|
|
|
|
|
|
|
|
Duple i (canvas_to_item (Duple (0, y)));
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect r = bounding_box();
|
2014-11-24 06:09:09 -05:00
|
|
|
if (!r) {
|
|
|
|
return 0; /* not really correct, but what else can we do? */
|
|
|
|
}
|
|
|
|
|
2017-01-19 14:54:24 -05:00
|
|
|
Rect bbox (r);
|
2014-11-24 06:09:09 -05:00
|
|
|
|
|
|
|
if (i.y < bbox.y0 || i.y >= bbox.y1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-24 06:30:58 -05:00
|
|
|
/* convert to fit Cairo origin model (origin at upper left)
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 1.0 - ((i.y - bbox.y0) / bbox.height());
|
2014-11-24 06:09:09 -05:00
|
|
|
}
|
2020-06-10 21:37:05 -04:00
|
|
|
|
|
|
|
void
|
2021-08-04 01:21:55 -04:00
|
|
|
Rectangle::_size_allocate (Rect const & r)
|
2020-06-10 21:37:05 -04:00
|
|
|
{
|
2021-08-04 01:21:55 -04:00
|
|
|
Item::_size_allocate (r);
|
2020-06-19 16:01:53 -04:00
|
|
|
|
|
|
|
if (_layout_sensitive) {
|
2021-08-01 23:18:35 -04:00
|
|
|
/* Set _position use the upper left of the Rect, and then set
|
2020-07-09 18:38:52 -04:00
|
|
|
the _rect member with values that use _position as the
|
|
|
|
origin.
|
2020-07-08 23:49:31 -04:00
|
|
|
*/
|
|
|
|
Rect r2 (0, 0, r.x1 - r.x0, r.y1 - r.y0);
|
|
|
|
set (r2);
|
2020-06-19 16:01:53 -04:00
|
|
|
}
|
2020-06-10 21:37:05 -04:00
|
|
|
}
|
2018-11-14 22:42:59 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
Rectangle::set_corner_radius (double r)
|
|
|
|
{
|
|
|
|
/* note: this does not change the bounding box */
|
|
|
|
|
|
|
|
begin_change ();
|
|
|
|
_corner_radius = r;
|
|
|
|
end_change ();
|
|
|
|
}
|
2020-07-08 23:49:31 -04:00
|
|
|
|
2021-07-27 15:25:31 -04:00
|
|
|
void
|
|
|
|
Rectangle::dump (std::ostream & o) const
|
|
|
|
{
|
|
|
|
Item::dump (o);
|
|
|
|
|
|
|
|
o << _canvas->indent() << " outline: w " << outline_width() << " color " << outline_color() << " what 0x" << std::hex << _outline_what << std::dec << endl;
|
|
|
|
}
|