Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <sal/config.h>
21 :
22 : #include <sstream>
23 :
24 : #include <tools/debug.hxx>
25 : #include <tools/gen.hxx>
26 : #include <tools/stream.hxx>
27 :
28 85184 : SvStream& ReadPair( SvStream& rIStream, Pair& rPair )
29 : {
30 : DBG_ASSERTWARNING( rIStream.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
31 :
32 85184 : sal_Int32 nTmpA(0), nTmpB(0);
33 85184 : rIStream.ReadInt32( nTmpA ).ReadInt32( nTmpB );
34 85184 : rPair.nA = nTmpA;
35 85184 : rPair.nB = nTmpB;
36 :
37 85184 : return rIStream;
38 : }
39 :
40 107418 : SvStream& WritePair( SvStream& rOStream, const Pair& rPair )
41 : {
42 : DBG_ASSERTWARNING( rOStream.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
43 :
44 107418 : rOStream.WriteInt32( rPair.nA ).WriteInt32( rPair.nB );
45 :
46 107418 : return rOStream;
47 : }
48 :
49 240933 : void Rectangle::SetSize( const Size& rSize )
50 : {
51 240933 : if ( rSize.Width() < 0 )
52 540 : nRight = nLeft + rSize.Width() +1;
53 240393 : else if ( rSize.Width() > 0 )
54 238535 : nRight = nLeft + rSize.Width() -1;
55 : else
56 1858 : nRight = RECT_EMPTY;
57 :
58 240933 : if ( rSize.Height() < 0 )
59 571 : nBottom = nTop + rSize.Height() +1;
60 240362 : else if ( rSize.Height() > 0 )
61 238934 : nBottom = nTop + rSize.Height() -1;
62 : else
63 1428 : nBottom = RECT_EMPTY;
64 240933 : }
65 :
66 1577017 : Rectangle& Rectangle::Union( const Rectangle& rRect )
67 : {
68 1577017 : if ( rRect.IsEmpty() )
69 284900 : return *this;
70 :
71 1292117 : if ( IsEmpty() )
72 78164 : *this = rRect;
73 : else
74 : {
75 1213953 : nLeft = std::min( std::min( nLeft, rRect.nLeft ), std::min( nRight, rRect.nRight ) );
76 1213953 : nRight = std::max( std::max( nLeft, rRect.nLeft ), std::max( nRight, rRect.nRight ) );
77 1213953 : nTop = std::min( std::min( nTop, rRect.nTop ), std::min( nBottom, rRect.nBottom ) );
78 1213953 : nBottom = std::max( std::max( nTop, rRect.nTop ), std::max( nBottom, rRect.nBottom ) );
79 : }
80 :
81 1292117 : return *this;
82 : }
83 :
84 739270 : Rectangle& Rectangle::Intersection( const Rectangle& rRect )
85 : {
86 739270 : if ( IsEmpty() )
87 74 : return *this;
88 739196 : if ( rRect.IsEmpty() )
89 : {
90 13015 : *this = Rectangle();
91 13015 : return *this;
92 : }
93 :
94 : // Justify rectangle
95 726181 : Rectangle aTmpRect( rRect );
96 726181 : Justify();
97 726181 : aTmpRect.Justify();
98 :
99 : // Perform intersection
100 726181 : nLeft = std::max( nLeft, aTmpRect.nLeft );
101 726181 : nRight = std::min( nRight, aTmpRect.nRight );
102 726181 : nTop = std::max( nTop, aTmpRect.nTop );
103 726181 : nBottom= std::min( nBottom, aTmpRect.nBottom );
104 :
105 : // Determine if intersection is empty
106 726181 : if ( nRight < nLeft || nBottom < nTop )
107 119273 : *this = Rectangle();
108 :
109 726181 : return *this;
110 : }
111 :
112 3542336 : void Rectangle::Justify()
113 : {
114 : long nHelp;
115 :
116 3542336 : if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
117 : {
118 2148 : nHelp = nLeft;
119 2148 : nLeft = nRight;
120 2148 : nRight = nHelp;
121 : }
122 :
123 3542336 : if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
124 : {
125 78583 : nHelp = nBottom;
126 78583 : nBottom = nTop;
127 78583 : nTop = nHelp;
128 : }
129 3542336 : }
130 :
131 127393 : bool Rectangle::IsInside( const Point& rPoint ) const
132 : {
133 127393 : if ( IsEmpty() )
134 22715 : return false;
135 :
136 104678 : bool bRet = true;
137 104678 : if ( nLeft <= nRight )
138 : {
139 104620 : if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
140 22198 : bRet = false;
141 : }
142 : else
143 : {
144 58 : if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
145 10 : bRet = false;
146 : }
147 104678 : if ( nTop <= nBottom )
148 : {
149 104620 : if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
150 24184 : bRet = false;
151 : }
152 : else
153 : {
154 58 : if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
155 10 : bRet = false;
156 : }
157 104678 : return bRet;
158 : }
159 :
160 40498 : bool Rectangle::IsInside( const Rectangle& rRect ) const
161 : {
162 40498 : if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
163 39641 : return true;
164 : else
165 857 : return false;
166 : }
167 :
168 205488 : bool Rectangle::IsOver( const Rectangle& rRect ) const
169 : {
170 : // If there's no intersection, they don't overlap
171 205488 : return !GetIntersection( rRect ).IsEmpty();
172 : }
173 :
174 7054 : SvStream& ReadRectangle( SvStream& rIStream, Rectangle& rRect )
175 : {
176 : DBG_ASSERTWARNING( rIStream.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
177 :
178 7054 : sal_Int32 nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0);
179 :
180 7054 : rIStream.ReadInt32( nTmpL ).ReadInt32( nTmpT ).ReadInt32( nTmpR ).ReadInt32( nTmpB );
181 :
182 7054 : rRect.nLeft = nTmpL;
183 7054 : rRect.nTop = nTmpT;
184 7054 : rRect.nRight = nTmpR;
185 7054 : rRect.nBottom = nTmpB;
186 :
187 7054 : return rIStream;
188 : }
189 :
190 12765 : SvStream& WriteRectangle( SvStream& rOStream, const Rectangle& rRect )
191 : {
192 : DBG_ASSERTWARNING( rOStream.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
193 :
194 12765 : rOStream.WriteInt32( rRect.nLeft )
195 25530 : .WriteInt32( rRect.nTop )
196 25530 : .WriteInt32( rRect.nRight )
197 25530 : .WriteInt32( rRect.nBottom );
198 :
199 12765 : return rOStream;
200 : }
201 :
202 32311 : OString Rectangle::toString() const
203 : {
204 32311 : std::stringstream ss;
205 32311 : ss << getX() << ", " << getY() << ", " << getWidth() << ", " << getHeight();
206 32311 : return ss.str().c_str();
207 : }
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|