add support for rounded corners to Canvas::Rectangle

This commit is contained in:
Paul Davis 2018-11-14 22:42:59 -05:00
parent fcc07cb3d0
commit 9869b0266b
2 changed files with 41 additions and 2 deletions

View file

@ -20,8 +20,11 @@
#include <iostream>
#include <cairomm/context.h>
#include "pbd/compose.h"
#include "gtkmm2ext/utils.h"
#include "canvas/canvas.h"
#include "canvas/rectangle.h"
#include "canvas/debug.h"
@ -32,6 +35,7 @@ using namespace ArdourCanvas;
Rectangle::Rectangle (Canvas* c)
: Item (c)
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
, _corner_radius (0.0)
{
}
@ -39,12 +43,14 @@ Rectangle::Rectangle (Canvas* c, Rect const & rect)
: Item (c)
, _rect (rect)
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
, _corner_radius (0.0)
{
}
Rectangle::Rectangle (Item* parent)
: Item (parent)
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
, _corner_radius (0.0)
{
}
@ -52,6 +58,7 @@ Rectangle::Rectangle (Item* parent, Rect const & rect)
: Item (parent)
, _rect (rect)
, _outline_what ((What) (LEFT | RIGHT | TOP | BOTTOM))
, _corner_radius (0.0)
{
}
@ -69,6 +76,13 @@ Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) con
return;
}
if (_corner_radius) {
context->save ();
Gtkmm2ext::rounded_rectangle (context, self.x0, self.y0, self.width(), self.height(), _corner_radius);
context->clip ();
}
if (_fill && !_transparent) {
if (_stops.empty()) {
setup_fill_context (context);
@ -76,7 +90,11 @@ Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) con
setup_gradient_context (context, self, Duple (draw.x0, draw.y0));
}
context->rectangle (draw.x0, draw.y0, draw.width(), draw.height());
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());
}
context->fill ();
}
@ -104,7 +122,11 @@ Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) con
if (_outline_what == What (LEFT|RIGHT|BOTTOM|TOP)) {
context->rectangle (self.x0, self.y0, self.width(), self.height());
if (_corner_radius) {
Gtkmm2ext::rounded_rectangle (context, self.x0, self.y0, self.width(), self.height(), _corner_radius);
} else {
context->rectangle (self.x0, self.y0, self.width(), self.height());
}
} else {
@ -132,6 +154,10 @@ Rectangle::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) con
context->stroke ();
}
if (_corner_radius) {
context->restore ();
}
render_children (area, context);
}
@ -287,3 +313,13 @@ Rectangle::size_allocate (Rect const & r)
set (r);
}
}
void
Rectangle::set_corner_radius (double r)
{
/* note: this does not change the bounding box */
begin_change ();
_corner_radius = r;
end_change ();
}