1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/stream.hxx>
30 OString
Pair::toString() const
33 // Note that this is not just used for debugging output but the
34 // format is parsed by external code (passed in callbacks to
35 // LibreOfficeKit clients). So don't change.
36 ss
<< A() << ", " << B();
37 return ss
.str().c_str();
40 void tools::Rectangle::SetSize( const Size
& rSize
)
42 if ( rSize
.Width() < 0 )
43 nRight
= nLeft
+ rSize
.Width() +1;
44 else if ( rSize
.Width() > 0 )
45 nRight
= nLeft
+ rSize
.Width() -1;
49 if ( rSize
.Height() < 0 )
50 nBottom
= nTop
+ rSize
.Height() +1;
51 else if ( rSize
.Height() > 0 )
52 nBottom
= nTop
+ rSize
.Height() -1;
57 void tools::Rectangle::SaturatingSetSize(const Size
& rSize
)
59 if (rSize
.Width() < 0)
60 nRight
= o3tl::saturating_add(nLeft
, (rSize
.Width() + 1));
61 else if ( rSize
.Width() > 0 )
62 nRight
= o3tl::saturating_add(nLeft
, (rSize
.Width() - 1));
66 if ( rSize
.Height() < 0 )
67 nBottom
= o3tl::saturating_add(nTop
, (rSize
.Height() + 1));
68 else if ( rSize
.Height() > 0 )
69 nBottom
= o3tl::saturating_add(nTop
, (rSize
.Height() - 1));
74 void tools::Rectangle::SaturatingSetX(long x
)
76 if (nRight
!= RECT_EMPTY
)
77 nRight
= o3tl::saturating_add(nRight
, x
- nLeft
);
81 void tools::Rectangle::SaturatingSetY(long y
)
83 if (nBottom
!= RECT_EMPTY
)
84 nBottom
= o3tl::saturating_add(nBottom
, y
- nTop
);
88 tools::Rectangle
& tools::Rectangle::Union( const tools::Rectangle
& rRect
)
90 if ( rRect
.IsEmpty() )
97 nLeft
= std::min( std::min( nLeft
, rRect
.nLeft
), std::min( nRight
, rRect
.nRight
) );
98 nRight
= std::max( std::max( nLeft
, rRect
.nLeft
), std::max( nRight
, rRect
.nRight
) );
99 nTop
= std::min( std::min( nTop
, rRect
.nTop
), std::min( nBottom
, rRect
.nBottom
) );
100 nBottom
= std::max( std::max( nTop
, rRect
.nTop
), std::max( nBottom
, rRect
.nBottom
) );
106 tools::Rectangle
& tools::Rectangle::Intersection( const tools::Rectangle
& rRect
)
110 if ( rRect
.IsEmpty() )
112 *this = tools::Rectangle();
117 tools::Rectangle
aTmpRect( rRect
);
121 // Perform intersection
122 nLeft
= std::max( nLeft
, aTmpRect
.nLeft
);
123 nRight
= std::min( nRight
, aTmpRect
.nRight
);
124 nTop
= std::max( nTop
, aTmpRect
.nTop
);
125 nBottom
= std::min( nBottom
, aTmpRect
.nBottom
);
127 // Determine if intersection is empty
128 if ( nRight
< nLeft
|| nBottom
< nTop
)
129 *this = tools::Rectangle();
134 void tools::Rectangle::Justify()
136 if ( (nRight
< nLeft
) && (nRight
!= RECT_EMPTY
) )
138 std::swap(nLeft
, nRight
);
141 if ( (nBottom
< nTop
) && (nBottom
!= RECT_EMPTY
) )
143 std::swap(nBottom
, nTop
);
147 bool tools::Rectangle::IsInside( const Point
& rPoint
) const
152 if ( nLeft
<= nRight
)
154 if ( (rPoint
.X() < nLeft
) || (rPoint
.X() > nRight
) )
159 if ( (rPoint
.X() > nLeft
) || (rPoint
.X() < nRight
) )
162 if ( nTop
<= nBottom
)
164 if ( (rPoint
.Y() < nTop
) || (rPoint
.Y() > nBottom
) )
169 if ( (rPoint
.Y() > nTop
) || (rPoint
.Y() < nBottom
) )
175 bool tools::Rectangle::IsInside( const tools::Rectangle
& rRect
) const
177 return IsInside( rRect
.TopLeft() ) && IsInside( rRect
.BottomRight() );
180 bool tools::Rectangle::IsOver( const tools::Rectangle
& rRect
) const
182 // If there's no intersection, they don't overlap
183 return !GetIntersection( rRect
).IsEmpty();
186 OString
tools::Rectangle::toString() const
188 std::stringstream ss
;
189 // Note that this is not just used for debugging output but the
190 // format is parsed by external code (passed in callbacks to
191 // LibreOfficeKit clients). So don't change.
192 ss
<< getX() << ", " << getY() << ", " << getWidth() << ", " << getHeight();
193 return ss
.str().c_str();
196 void tools::Rectangle::expand(long nExpandBy
)
200 if (nRight
== RECT_EMPTY
)
201 nRight
= nLeft
+ nExpandBy
- 1;
204 if (nBottom
== RECT_EMPTY
)
205 nBottom
= nTop
+ nExpandBy
- 1;
207 nBottom
+= nExpandBy
;
210 void tools::Rectangle::shrink(long nShrinkBy
)
214 if (nRight
!= RECT_EMPTY
)
216 if (nBottom
!= RECT_EMPTY
)
217 nBottom
-= nShrinkBy
;
220 long tools::Rectangle::AdjustRight(long nHorzMoveDelta
)
222 if (nRight
== RECT_EMPTY
)
223 nRight
= nLeft
+ nHorzMoveDelta
- 1;
225 nRight
+= nHorzMoveDelta
;
229 long tools::Rectangle::AdjustBottom( long nVertMoveDelta
)
231 if (nBottom
== RECT_EMPTY
)
232 nBottom
= nTop
+ nVertMoveDelta
- 1;
234 nBottom
+= nVertMoveDelta
;
238 void tools::Rectangle::setX( long x
)
240 if (nRight
!= RECT_EMPTY
)
245 void tools::Rectangle::setY( long y
)
247 if (nBottom
!= RECT_EMPTY
)
252 long tools::Rectangle::Right() const
254 return nRight
== RECT_EMPTY
? nLeft
: nRight
;
257 long tools::Rectangle::Bottom() const
259 return nBottom
== RECT_EMPTY
? nTop
: nBottom
;
262 /// Returns the difference between right and left, assuming the range includes one end, but not the other.
263 long tools::Rectangle::getWidth() const
265 return nRight
== RECT_EMPTY
? 0 : nRight
- nLeft
;
268 /// Returns the difference between bottom and top, assuming the range includes one end, but not the other.
269 long tools::Rectangle::getHeight() const
271 return nBottom
== RECT_EMPTY
? 0 : nBottom
- nTop
;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */