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>
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
);
37 SvStream
& WritePair( SvStream
& rOStream
, const Pair
& rPair
)
39 rOStream
.WriteInt32( rPair
.nA
).WriteInt32( rPair
.nB
);
44 rtl::OString
Pair::toString() const
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;
63 if ( rSize
.Height() < 0 )
64 nBottom
= nTop
+ rSize
.Height() +1;
65 else if ( rSize
.Height() > 0 )
66 nBottom
= nTop
+ rSize
.Height() -1;
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));
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));
88 void tools::Rectangle::SaturatingSetX(long x
)
90 nRight
= o3tl::saturating_add(nRight
, x
- nLeft
);
94 void tools::Rectangle::SaturatingSetY(long y
)
96 nBottom
= o3tl::saturating_add(nBottom
, y
- nTop
);
100 tools::Rectangle
& tools::Rectangle::Union( const tools::Rectangle
& rRect
)
102 if ( rRect
.IsEmpty() )
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
) );
118 tools::Rectangle
& tools::Rectangle::Intersection( const tools::Rectangle
& rRect
)
122 if ( rRect
.IsEmpty() )
124 *this = tools::Rectangle();
129 tools::Rectangle
aTmpRect( rRect
);
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();
146 void tools::Rectangle::Justify()
150 if ( (nRight
< nLeft
) && (nRight
!= RECT_EMPTY
) )
157 if ( (nBottom
< nTop
) && (nBottom
!= RECT_EMPTY
) )
165 bool tools::Rectangle::IsInside( const Point
& rPoint
) const
170 if ( nLeft
<= nRight
)
172 if ( (rPoint
.X() < nLeft
) || (rPoint
.X() > nRight
) )
177 if ( (rPoint
.X() > nLeft
) || (rPoint
.X() < nRight
) )
180 if ( nTop
<= nBottom
)
182 if ( (rPoint
.Y() < nTop
) || (rPoint
.Y() > nBottom
) )
187 if ( (rPoint
.Y() > nTop
) || (rPoint
.Y() < nBottom
) )
193 bool tools::Rectangle::IsInside( const tools::Rectangle
& rRect
) const
195 if ( IsInside( rRect
.TopLeft() ) && IsInside( rRect
.BottomRight() ) )
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();
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
);
217 rRect
.nRight
= nTmpR
;
218 rRect
.nBottom
= nTmpB
;
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
);
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: */