Add wxRect::MakeCenteredIn() modifying the rectangle in place

This can be more convenient than calling CenterIn().
This commit is contained in:
Vadim Zeitlin 2023-05-10 00:09:41 +01:00
parent 1a22adb80d
commit 5fd6be2f64
3 changed files with 32 additions and 3 deletions

View file

@ -848,11 +848,20 @@ public:
// centre this rectangle in the given (usually, but not necessarily,
// larger) one
void MakeCenteredIn(const wxRect& r, int dir = wxBOTH)
{
if ( dir & wxHORIZONTAL )
x = r.x + (r.width - width)/2;
if ( dir & wxVERTICAL )
y = r.y + (r.height - height)/2;
}
// same as above but returns the new rectangle instead of modifying this one
wxRect CentreIn(const wxRect& r, int dir = wxBOTH) const
{
return wxRect(dir & wxHORIZONTAL ? r.x + (r.width - width)/2 : x,
dir & wxVERTICAL ? r.y + (r.height - height)/2 : y,
width, height);
wxRect rect(*this);
rect.MakeCenteredIn(r, dir);
return rect;
}
wxRect CenterIn(const wxRect& r, int dir = wxBOTH) const

View file

@ -307,6 +307,8 @@ public:
centred in both directions but if @a dir includes only @c wxVERTICAL or
only @c wxHORIZONTAL, then it is only centered in this direction while
the other component of its position remains unchanged.
@see MakeCenteredIn()
*/
wxRect CentreIn(const wxRect& r, int dir = wxBOTH) const;
wxRect CenterIn(const wxRect& r, int dir = wxBOTH) const;
@ -475,6 +477,20 @@ public:
*/
bool IsEmpty() const;
/**
Center this rectangle inside the given rectangle @a r.
By default, rectangle is centred in both directions but if @a dir
includes only @c wxVERTICAL or only @c wxHORIZONTAL, then it is only
centered in this direction while the other component of its position
remains unchanged.
@see CenterIn()
@since 3.3.0
*/
void MakeCenteredIn(const wxRect& r, int dir = wxBOTH);
///@{
/**
Moves the rectangle by the specified offset. If @a dx is positive, the

View file

@ -31,6 +31,10 @@ TEST_CASE("wxRect::CentreIn", "[rect]")
CHECK( R(0, 0, 10, 10).CentreIn(R(0, 0, 100, 100)) == R(45, 45, 10, 10) );
CHECK( R(0, 0, 20, 20).CentreIn(R(0, 0, 10, 10)) == R(-5, -5, 20, 20) );
R r(-10, -10, 20, 20);
r.MakeCenteredIn(R(0, 0, 100, 100), wxHORIZONTAL);
CHECK( r == R(40, -10, 20, 20) );
}
TEST_CASE("wxRect::InflateDeflate", "[rect]")