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
32 // Note that this is not just used for debugging output but the
33 // format is parsed by external code (passed in callbacks to
34 // LibreOfficeKit clients). So don't change.
35 return OString::number(A()) + ", " + OString::number(B());
38 void tools::Rectangle::SetSize( const Size
& rSize
)
40 if ( rSize
.Width() < 0 )
41 nRight
= nLeft
+ rSize
.Width() +1;
42 else if ( rSize
.Width() > 0 )
43 nRight
= nLeft
+ rSize
.Width() -1;
47 if ( rSize
.Height() < 0 )
48 nBottom
= nTop
+ rSize
.Height() +1;
49 else if ( rSize
.Height() > 0 )
50 nBottom
= nTop
+ rSize
.Height() -1;
55 void tools::Rectangle::SaturatingSetSize(const Size
& rSize
)
57 if (rSize
.Width() < 0)
58 nRight
= o3tl::saturating_add(nLeft
, (rSize
.Width() + 1));
59 else if ( rSize
.Width() > 0 )
60 nRight
= o3tl::saturating_add(nLeft
, (rSize
.Width() - 1));
64 if ( rSize
.Height() < 0 )
65 nBottom
= o3tl::saturating_add(nTop
, (rSize
.Height() + 1));
66 else if ( rSize
.Height() > 0 )
67 nBottom
= o3tl::saturating_add(nTop
, (rSize
.Height() - 1));
72 void tools::Rectangle::SaturatingSetPosX(tools::Long x
)
75 nRight
= o3tl::saturating_add(nRight
, x
- nLeft
);
79 void tools::Rectangle::SaturatingSetPosY(tools::Long y
)
82 nBottom
= o3tl::saturating_add(nBottom
, y
- nTop
);
86 tools::Rectangle
& tools::Rectangle::Union( const tools::Rectangle
& rRect
)
88 if ( rRect
.IsEmpty() )
95 std::tie(nLeft
, nRight
) = std::minmax({ nLeft
, rRect
.nLeft
, nRight
, rRect
.nRight
});
96 std::tie(nTop
, nBottom
) = std::minmax({ nTop
, rRect
.nTop
, nBottom
, rRect
.nBottom
});
102 tools::Rectangle
& tools::Rectangle::Intersection( const tools::Rectangle
& rRect
)
106 if ( rRect
.IsEmpty() )
108 *this = tools::Rectangle();
113 tools::Rectangle
aTmpRect( rRect
);
117 // Perform intersection
118 nLeft
= std::max( nLeft
, aTmpRect
.nLeft
);
119 nRight
= std::min( nRight
, aTmpRect
.nRight
);
120 nTop
= std::max( nTop
, aTmpRect
.nTop
);
121 nBottom
= std::min( nBottom
, aTmpRect
.nBottom
);
123 // Determine if intersection is empty
124 if ( nRight
< nLeft
|| nBottom
< nTop
)
125 *this = tools::Rectangle();
130 void tools::Rectangle::Justify()
132 if ((nRight
< nLeft
) && (!IsWidthEmpty()))
134 std::swap(nLeft
, nRight
);
137 if ((nBottom
< nTop
) && (!IsHeightEmpty()))
139 std::swap(nBottom
, nTop
);
143 bool tools::Rectangle::Contains( const Point
& rPoint
) const
148 if ( nLeft
<= nRight
)
150 if ( (rPoint
.X() < nLeft
) || (rPoint
.X() > nRight
) )
155 if ( (rPoint
.X() > nLeft
) || (rPoint
.X() < nRight
) )
158 if ( nTop
<= nBottom
)
160 if ( (rPoint
.Y() < nTop
) || (rPoint
.Y() > nBottom
) )
165 if ( (rPoint
.Y() > nTop
) || (rPoint
.Y() < nBottom
) )
171 bool tools::Rectangle::Contains( const tools::Rectangle
& rRect
) const
173 return Contains( rRect
.TopLeft() ) && Contains( rRect
.BottomRight() );
176 bool tools::Rectangle::Overlaps( const tools::Rectangle
& rRect
) const
178 // If there's no intersection, they don't overlap
179 return !GetIntersection( rRect
).IsEmpty();
182 OString
tools::Rectangle::toString() const
184 // Note that this is not just used for debugging output but the
185 // format is parsed by external code (passed in callbacks to
186 // LibreOfficeKit clients). So don't change.
187 return OString::number(Left()) + ", " + OString::number(Top()) + ", " + OString::number(getWidth()) + ", " + OString::number(getHeight());
190 void tools::Rectangle::expand(tools::Long nExpandBy
)
192 AdjustLeft(-nExpandBy
);
193 AdjustTop(-nExpandBy
);
194 AdjustRight(nExpandBy
);
195 AdjustBottom(nExpandBy
);
198 void tools::Rectangle::shrink(tools::Long nShrinkBy
)
204 if (!IsHeightEmpty())
205 nBottom
-= nShrinkBy
;
208 tools::Long
tools::Rectangle::AdjustRight(tools::Long nHorzMoveDelta
)
211 nRight
= nLeft
+ nHorzMoveDelta
- 1;
213 nRight
+= nHorzMoveDelta
;
217 tools::Long
tools::Rectangle::AdjustBottom( tools::Long nVertMoveDelta
)
220 nBottom
= nTop
+ nVertMoveDelta
- 1;
222 nBottom
+= nVertMoveDelta
;
226 static_assert( std::is_trivially_copyable
< Pair
>::value
);
227 static_assert( std::is_trivially_copyable
< Point
>::value
);
228 static_assert( std::is_trivially_copyable
< Size
>::value
);
229 static_assert( std::is_trivially_copyable
< Range
>::value
);
230 static_assert( std::is_trivially_copyable
< Selection
>::value
);
231 static_assert( std::is_trivially_copyable
< tools::Rectangle
>::value
);
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */