Bump version to 6.4-15
[LibreOffice.git] / tools / source / generic / gen.cxx
blob7c182818c460de0057240de43b5ca9583724d8fc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <algorithm>
24 #include <cassert>
25 #include <sstream>
26 #include <o3tl/safeint.hxx>
27 #include <tools/gen.hxx>
28 #include <tools/stream.hxx>
30 OString Pair::toString() const
32 std::stringstream ss;
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;
46 else
47 nRight = RECT_EMPTY;
49 if ( rSize.Height() < 0 )
50 nBottom = nTop + rSize.Height() +1;
51 else if ( rSize.Height() > 0 )
52 nBottom = nTop + rSize.Height() -1;
53 else
54 nBottom = RECT_EMPTY;
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));
63 else
64 nRight = RECT_EMPTY;
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));
70 else
71 nBottom = RECT_EMPTY;
74 void tools::Rectangle::SaturatingSetX(long x)
76 if (nRight != RECT_EMPTY)
77 nRight = o3tl::saturating_add(nRight, x - nLeft);
78 nLeft = x;
81 void tools::Rectangle::SaturatingSetY(long y)
83 if (nBottom != RECT_EMPTY)
84 nBottom = o3tl::saturating_add(nBottom, y - nTop);
85 nTop = y;
88 tools::Rectangle& tools::Rectangle::Union( const tools::Rectangle& rRect )
90 if ( rRect.IsEmpty() )
91 return *this;
93 if ( IsEmpty() )
94 *this = rRect;
95 else
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 ) );
103 return *this;
106 tools::Rectangle& tools::Rectangle::Intersection( const tools::Rectangle& rRect )
108 if ( IsEmpty() )
109 return *this;
110 if ( rRect.IsEmpty() )
112 *this = tools::Rectangle();
113 return *this;
116 // Justify rectangle
117 tools::Rectangle aTmpRect( rRect );
118 Justify();
119 aTmpRect.Justify();
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();
131 return *this;
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
149 if ( IsEmpty() )
150 return false;
152 if ( nLeft <= nRight )
154 if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
155 return false;
157 else
159 if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
160 return false;
162 if ( nTop <= nBottom )
164 if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
165 return false;
167 else
169 if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
170 return false;
172 return true;
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)
198 nLeft -= nExpandBy;
199 nTop -= nExpandBy;
200 if (nRight == RECT_EMPTY)
201 nRight = nLeft + nExpandBy - 1;
202 else
203 nRight += nExpandBy;
204 if (nBottom == RECT_EMPTY)
205 nBottom = nTop + nExpandBy - 1;
206 else
207 nBottom += nExpandBy;
210 void tools::Rectangle::shrink(long nShrinkBy)
212 nLeft += nShrinkBy;
213 nTop += nShrinkBy;
214 if (nRight != RECT_EMPTY)
215 nRight -= nShrinkBy;
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;
224 else
225 nRight += nHorzMoveDelta;
226 return nRight;
229 long tools::Rectangle::AdjustBottom( long nVertMoveDelta )
231 if (nBottom == RECT_EMPTY)
232 nBottom = nTop + nVertMoveDelta - 1;
233 else
234 nBottom += nVertMoveDelta;
235 return nBottom;
238 void tools::Rectangle::setX( long x )
240 if (nRight != RECT_EMPTY)
241 nRight += x - nLeft;
242 nLeft = x;
245 void tools::Rectangle::setY( long y )
247 if (nBottom != RECT_EMPTY)
248 nBottom += y - nTop;
249 nTop = y;
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: */