Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / tools / source / generic / gen.cxx
blobea855f98e9f04e4121c5216aec13bb9dee45165f
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>
22 #include <sstream>
23 #include <o3tl/safeint.hxx>
24 #include <tools/gen.hxx>
25 #include <tools/stream.hxx>
27 SvStream& ReadPair( SvStream& rIStream, Pair& rPair )
29 sal_Int32 nTmpA(0), nTmpB(0);
30 rIStream.ReadInt32( nTmpA ).ReadInt32( nTmpB );
31 rPair.nA = nTmpA;
32 rPair.nB = nTmpB;
34 return rIStream;
37 SvStream& WritePair( SvStream& rOStream, const Pair& rPair )
39 rOStream.WriteInt32( rPair.nA ).WriteInt32( rPair.nB );
41 return rOStream;
44 rtl::OString Pair::toString() const
46 std::stringstream ss;
47 // Note that this is not just used for debugging output but the
48 // format is parsed by external code (passed in callbacks to
49 // LibreOfficeKit clients). So don't change.
50 ss << A() << ", " << B();
51 return ss.str().c_str();
54 void tools::Rectangle::SetSize( const Size& rSize )
56 if ( rSize.Width() < 0 )
57 nRight = nLeft + rSize.Width() +1;
58 else if ( rSize.Width() > 0 )
59 nRight = nLeft + rSize.Width() -1;
60 else
61 nRight = RECT_EMPTY;
63 if ( rSize.Height() < 0 )
64 nBottom = nTop + rSize.Height() +1;
65 else if ( rSize.Height() > 0 )
66 nBottom = nTop + rSize.Height() -1;
67 else
68 nBottom = RECT_EMPTY;
71 void tools::Rectangle::SaturatingSetSize(const Size& rSize)
73 if (rSize.Width() < 0)
74 nRight = o3tl::saturating_add(nLeft, (rSize.Width() + 1));
75 else if ( rSize.Width() > 0 )
76 nRight = o3tl::saturating_add(nLeft, (rSize.Width() - 1));
77 else
78 nRight = RECT_EMPTY;
80 if ( rSize.Height() < 0 )
81 nBottom = o3tl::saturating_add(nTop, (rSize.Height() + 1));
82 else if ( rSize.Height() > 0 )
83 nBottom = o3tl::saturating_add(nTop, (rSize.Height() - 1));
84 else
85 nBottom = RECT_EMPTY;
88 void tools::Rectangle::SaturatingSetX(long x)
90 nRight = o3tl::saturating_add(nRight, x - nLeft);
91 nLeft = x;
94 void tools::Rectangle::SaturatingSetY(long y)
96 nBottom = o3tl::saturating_add(nBottom, y - nTop);
97 nTop = y;
100 tools::Rectangle& tools::Rectangle::Union( const tools::Rectangle& rRect )
102 if ( rRect.IsEmpty() )
103 return *this;
105 if ( IsEmpty() )
106 *this = rRect;
107 else
109 nLeft = std::min( std::min( nLeft, rRect.nLeft ), std::min( nRight, rRect.nRight ) );
110 nRight = std::max( std::max( nLeft, rRect.nLeft ), std::max( nRight, rRect.nRight ) );
111 nTop = std::min( std::min( nTop, rRect.nTop ), std::min( nBottom, rRect.nBottom ) );
112 nBottom = std::max( std::max( nTop, rRect.nTop ), std::max( nBottom, rRect.nBottom ) );
115 return *this;
118 tools::Rectangle& tools::Rectangle::Intersection( const tools::Rectangle& rRect )
120 if ( IsEmpty() )
121 return *this;
122 if ( rRect.IsEmpty() )
124 *this = tools::Rectangle();
125 return *this;
128 // Justify rectangle
129 tools::Rectangle aTmpRect( rRect );
130 Justify();
131 aTmpRect.Justify();
133 // Perform intersection
134 nLeft = std::max( nLeft, aTmpRect.nLeft );
135 nRight = std::min( nRight, aTmpRect.nRight );
136 nTop = std::max( nTop, aTmpRect.nTop );
137 nBottom= std::min( nBottom, aTmpRect.nBottom );
139 // Determine if intersection is empty
140 if ( nRight < nLeft || nBottom < nTop )
141 *this = tools::Rectangle();
143 return *this;
146 void tools::Rectangle::Justify()
148 long nHelp;
150 if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
152 nHelp = nLeft;
153 nLeft = nRight;
154 nRight = nHelp;
157 if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
159 nHelp = nBottom;
160 nBottom = nTop;
161 nTop = nHelp;
165 bool tools::Rectangle::IsInside( const Point& rPoint ) const
167 if ( IsEmpty() )
168 return false;
170 if ( nLeft <= nRight )
172 if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
173 return false;
175 else
177 if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
178 return false;
180 if ( nTop <= nBottom )
182 if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
183 return false;
185 else
187 if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
188 return false;
190 return true;
193 bool tools::Rectangle::IsInside( const tools::Rectangle& rRect ) const
195 if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
196 return true;
197 else
198 return false;
201 bool tools::Rectangle::IsOver( const tools::Rectangle& rRect ) const
203 // If there's no intersection, they don't overlap
204 return !GetIntersection( rRect ).IsEmpty();
207 namespace tools
209 SvStream& ReadRectangle( SvStream& rIStream, tools::Rectangle& rRect )
211 sal_Int32 nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0);
213 rIStream.ReadInt32( nTmpL ).ReadInt32( nTmpT ).ReadInt32( nTmpR ).ReadInt32( nTmpB );
215 rRect.nLeft = nTmpL;
216 rRect.nTop = nTmpT;
217 rRect.nRight = nTmpR;
218 rRect.nBottom = nTmpB;
220 return rIStream;
223 SvStream& WriteRectangle( SvStream& rOStream, const tools::Rectangle& rRect )
225 rOStream.WriteInt32( rRect.nLeft )
226 .WriteInt32( rRect.nTop )
227 .WriteInt32( rRect.nRight )
228 .WriteInt32( rRect.nBottom );
230 return rOStream;
234 OString tools::Rectangle::toString() const
236 std::stringstream ss;
237 // Note that this is not just used for debugging output but the
238 // format is parsed by external code (passed in callbacks to
239 // LibreOfficeKit clients). So don't change.
240 ss << getX() << ", " << getY() << ", " << getWidth() << ", " << getHeight();
241 return ss.str().c_str();
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */