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 <tools/debug.hxx>
21 : #include <tools/gen.hxx>
22 : #include <tools/stream.hxx>
23 :
24 169066 : SvStream& ReadPair( SvStream& rIStream, Pair& rPair )
25 : {
26 : DBG_ASSERTWARNING( rIStream.GetVersion(), "Pair::>> - Solar-Version not set on rIStream" );
27 :
28 169066 : sal_Int32 nTmpA(0), nTmpB(0);
29 169066 : rIStream.ReadInt32( nTmpA ).ReadInt32( nTmpB );
30 169066 : rPair.nA = nTmpA;
31 169066 : rPair.nB = nTmpB;
32 :
33 169066 : return rIStream;
34 : }
35 :
36 211826 : SvStream& WritePair( SvStream& rOStream, const Pair& rPair )
37 : {
38 : DBG_ASSERTWARNING( rOStream.GetVersion(), "Pair::<< - Solar-Version not set on rOStream" );
39 :
40 211826 : rOStream.WriteInt32( rPair.nA ).WriteInt32( rPair.nB );
41 :
42 211826 : return rOStream;
43 : }
44 :
45 406492 : void Rectangle::SetSize( const Size& rSize )
46 : {
47 406492 : if ( rSize.Width() < 0 )
48 326 : nRight = nLeft + rSize.Width() +1;
49 406166 : else if ( rSize.Width() > 0 )
50 403111 : nRight = nLeft + rSize.Width() -1;
51 : else
52 3055 : nRight = RECT_EMPTY;
53 :
54 406492 : if ( rSize.Height() < 0 )
55 346 : nBottom = nTop + rSize.Height() +1;
56 406146 : else if ( rSize.Height() > 0 )
57 404268 : nBottom = nTop + rSize.Height() -1;
58 : else
59 1878 : nBottom = RECT_EMPTY;
60 406492 : }
61 :
62 1220987 : Rectangle& Rectangle::Union( const Rectangle& rRect )
63 : {
64 1220987 : if ( rRect.IsEmpty() )
65 334487 : return *this;
66 :
67 886500 : if ( IsEmpty() )
68 96105 : *this = rRect;
69 : else
70 : {
71 790395 : nLeft = std::min( std::min( nLeft, rRect.nLeft ), std::min( nRight, rRect.nRight ) );
72 790395 : nRight = std::max( std::max( nLeft, rRect.nLeft ), std::max( nRight, rRect.nRight ) );
73 790395 : nTop = std::min( std::min( nTop, rRect.nTop ), std::min( nBottom, rRect.nBottom ) );
74 790395 : nBottom = std::max( std::max( nTop, rRect.nTop ), std::max( nBottom, rRect.nBottom ) );
75 : }
76 :
77 886500 : return *this;
78 : }
79 :
80 1235851 : Rectangle& Rectangle::Intersection( const Rectangle& rRect )
81 : {
82 1235851 : if ( IsEmpty() )
83 120 : return *this;
84 1235731 : if ( rRect.IsEmpty() )
85 : {
86 22736 : *this = Rectangle();
87 22736 : return *this;
88 : }
89 :
90 : // Justify rectangle
91 1212995 : Rectangle aTmpRect( rRect );
92 1212995 : Justify();
93 1212995 : aTmpRect.Justify();
94 :
95 : // Perform intersection
96 1212995 : nLeft = std::max( nLeft, aTmpRect.nLeft );
97 1212995 : nRight = std::min( nRight, aTmpRect.nRight );
98 1212995 : nTop = std::max( nTop, aTmpRect.nTop );
99 1212995 : nBottom= std::min( nBottom, aTmpRect.nBottom );
100 :
101 : // Determine if intersection is empty
102 1212995 : if ( nRight < nLeft || nBottom < nTop )
103 227345 : *this = Rectangle();
104 :
105 1212995 : return *this;
106 : }
107 :
108 4353183 : void Rectangle::Justify()
109 : {
110 : long nHelp;
111 :
112 4353183 : if ( (nRight < nLeft) && (nRight != RECT_EMPTY) )
113 : {
114 524 : nHelp = nLeft;
115 524 : nLeft = nRight;
116 524 : nRight = nHelp;
117 : }
118 :
119 4353183 : if ( (nBottom < nTop) && (nBottom != RECT_EMPTY) )
120 : {
121 1200 : nHelp = nBottom;
122 1200 : nBottom = nTop;
123 1200 : nTop = nHelp;
124 : }
125 4353183 : }
126 :
127 220804 : bool Rectangle::IsInside( const Point& rPoint ) const
128 : {
129 220804 : if ( IsEmpty() )
130 41149 : return false;
131 :
132 179655 : bool bRet = true;
133 179655 : if ( nLeft <= nRight )
134 : {
135 179555 : if ( (rPoint.X() < nLeft) || (rPoint.X() > nRight) )
136 35494 : bRet = false;
137 : }
138 : else
139 : {
140 100 : if ( (rPoint.X() > nLeft) || (rPoint.X() < nRight) )
141 12 : bRet = false;
142 : }
143 179655 : if ( nTop <= nBottom )
144 : {
145 179555 : if ( (rPoint.Y() < nTop) || (rPoint.Y() > nBottom) )
146 38000 : bRet = false;
147 : }
148 : else
149 : {
150 100 : if ( (rPoint.Y() > nTop) || (rPoint.Y() < nBottom) )
151 12 : bRet = false;
152 : }
153 179655 : return bRet;
154 : }
155 :
156 71878 : bool Rectangle::IsInside( const Rectangle& rRect ) const
157 : {
158 71878 : if ( IsInside( rRect.TopLeft() ) && IsInside( rRect.BottomRight() ) )
159 69488 : return true;
160 : else
161 2390 : return false;
162 : }
163 :
164 389158 : bool Rectangle::IsOver( const Rectangle& rRect ) const
165 : {
166 : // If there's no intersection, they don't overlap
167 389158 : return !GetIntersection( rRect ).IsEmpty();
168 : }
169 :
170 14016 : SvStream& ReadRectangle( SvStream& rIStream, Rectangle& rRect )
171 : {
172 : DBG_ASSERTWARNING( rIStream.GetVersion(), "Rectangle::>> - Solar-Version not set on rIStream" );
173 :
174 14016 : sal_Int32 nTmpL(0), nTmpT(0), nTmpR(0), nTmpB(0);
175 :
176 14016 : rIStream.ReadInt32( nTmpL ).ReadInt32( nTmpT ).ReadInt32( nTmpR ).ReadInt32( nTmpB );
177 :
178 14016 : rRect.nLeft = nTmpL;
179 14016 : rRect.nTop = nTmpT;
180 14016 : rRect.nRight = nTmpR;
181 14016 : rRect.nBottom = nTmpB;
182 :
183 14016 : return rIStream;
184 : }
185 :
186 25536 : SvStream& WriteRectangle( SvStream& rOStream, const Rectangle& rRect )
187 : {
188 : DBG_ASSERTWARNING( rOStream.GetVersion(), "Rectangle::<< - Solar-Version not set on rOStream" );
189 :
190 25536 : rOStream.WriteInt32( rRect.nLeft )
191 51072 : .WriteInt32( rRect.nTop )
192 51072 : .WriteInt32( rRect.nRight )
193 51072 : .WriteInt32( rRect.nBottom );
194 :
195 25536 : return rOStream;
196 : }
197 :
198 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|