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>
24 #include <tools/debug.hxx>
25 #include <tools/gen.hxx>
26 #include <tools/stream.hxx>
28 SvStream
& ReadPair( SvStream
& rIStream
, Pair
& rPair
)
30 DBG_ASSERTWARNING( rIStream
.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
32 sal_Int32
nTmpA(0), nTmpB(0);
33 rIStream
.ReadInt32( nTmpA
).ReadInt32( nTmpB
);
40 SvStream
& WritePair( SvStream
& rOStream
, const Pair
& rPair
)
42 DBG_ASSERTWARNING( rOStream
.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
44 rOStream
.WriteInt32( rPair
.nA
).WriteInt32( rPair
.nB
);
49 void Rectangle::SetSize( const Size
& rSize
)
51 if ( rSize
.Width() < 0 )
52 nRight
= nLeft
+ rSize
.Width() +1;
53 else if ( rSize
.Width() > 0 )
54 nRight
= nLeft
+ rSize
.Width() -1;
58 if ( rSize
.Height() < 0 )
59 nBottom
= nTop
+ rSize
.Height() +1;
60 else if ( rSize
.Height() > 0 )
61 nBottom
= nTop
+ rSize
.Height() -1;
66 Rectangle
& Rectangle::Union( const Rectangle
& rRect
)
68 if ( rRect
.IsEmpty() )
75 nLeft
= std::min( std::min( nLeft
, rRect
.nLeft
), std::min( nRight
, rRect
.nRight
) );
76 nRight
= std::max( std::max( nLeft
, rRect
.nLeft
), std::max( nRight
, rRect
.nRight
) );
77 nTop
= std::min( std::min( nTop
, rRect
.nTop
), std::min( nBottom
, rRect
.nBottom
) );
78 nBottom
= std::max( std::max( nTop
, rRect
.nTop
), std::max( nBottom
, rRect
.nBottom
) );
84 Rectangle
& Rectangle::Intersection( const Rectangle
& rRect
)
88 if ( rRect
.IsEmpty() )
95 Rectangle
aTmpRect( rRect
);
99 // Perform intersection
100 nLeft
= std::max( nLeft
, aTmpRect
.nLeft
);
101 nRight
= std::min( nRight
, aTmpRect
.nRight
);
102 nTop
= std::max( nTop
, aTmpRect
.nTop
);
103 nBottom
= std::min( nBottom
, aTmpRect
.nBottom
);
105 // Determine if intersection is empty
106 if ( nRight
< nLeft
|| nBottom
< nTop
)
112 void Rectangle::Justify()
116 if ( (nRight
< nLeft
) && (nRight
!= RECT_EMPTY
) )
123 if ( (nBottom
< nTop
) && (nBottom
!= RECT_EMPTY
) )
131 bool Rectangle::IsInside( const Point
& rPoint
) const
137 if ( nLeft
<= nRight
)
139 if ( (rPoint
.X() < nLeft
) || (rPoint
.X() > nRight
) )
144 if ( (rPoint
.X() > nLeft
) || (rPoint
.X() < nRight
) )
147 if ( nTop
<= nBottom
)
149 if ( (rPoint
.Y() < nTop
) || (rPoint
.Y() > nBottom
) )
154 if ( (rPoint
.Y() > nTop
) || (rPoint
.Y() < nBottom
) )
160 bool Rectangle::IsInside( const Rectangle
& rRect
) const
162 if ( IsInside( rRect
.TopLeft() ) && IsInside( rRect
.BottomRight() ) )
168 bool Rectangle::IsOver( const Rectangle
& rRect
) const
170 // If there's no intersection, they don't overlap
171 return !GetIntersection( rRect
).IsEmpty();
174 SvStream
& ReadRectangle( SvStream
& rIStream
, Rectangle
& rRect
)
176 DBG_ASSERTWARNING( rIStream
.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
178 sal_Int32
nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0);
180 rIStream
.ReadInt32( nTmpL
).ReadInt32( nTmpT
).ReadInt32( nTmpR
).ReadInt32( nTmpB
);
184 rRect
.nRight
= nTmpR
;
185 rRect
.nBottom
= nTmpB
;
190 SvStream
& WriteRectangle( SvStream
& rOStream
, const Rectangle
& rRect
)
192 DBG_ASSERTWARNING( rOStream
.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
194 rOStream
.WriteInt32( rRect
.nLeft
)
195 .WriteInt32( rRect
.nTop
)
196 .WriteInt32( rRect
.nRight
)
197 .WriteInt32( rRect
.nBottom
);
202 OString
Rectangle::toString() const
204 std::stringstream ss
;
205 ss
<< getX() << ", " << getY() << ", " << getWidth() << ", " << getHeight();
206 return ss
.str().c_str();
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */