1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _BGFX_RANGE_BASICBOX_HXX
25 #define _BGFX_RANGE_BASICBOX_HXX
26 
27 #include <basegfx/range/basicrange.hxx>
28 #include <basegfx/basegfxdllapi.h>
29 
30 
31 namespace basegfx
32 {
33     /** Specialization of BasicRange, handling the inside predicates
34         differently.
35 
36         This template considers the rightmost and bottommost border as
37         <em>outside</em> of the range, in contrast to BasicRange,
38         which considers them inside.
39      */
40 	class BASEGFX_DLLPUBLIC BasicBox : public BasicRange< sal_Int32, Int32Traits >
41 	{
42         typedef BasicRange< sal_Int32, Int32Traits > Base;
43 	public:
BasicBox()44         BasicBox() :
45             Base()
46         {
47         }
48 
BasicBox(sal_Int32 nValue)49         BasicBox( sal_Int32 nValue ) :
50             Base( nValue )
51 		{
52 		}
53 
BasicBox(const BasicBox & rBox)54 		BasicBox(const BasicBox& rBox) :
55             Base( rBox )
56 		{
57 		}
58 
getCenter() const59 		double getCenter() const
60 		{
61 			if(isEmpty())
62 			{
63 				return 0.0;
64 			}
65 			else
66 			{
67 				return ((mnMaximum + mnMinimum - 1.0) / 2.0);
68 			}
69 		}
70 
71         using Base::isInside;
72 
isInside(sal_Int32 nValue) const73 		bool isInside(sal_Int32 nValue) const
74 		{
75 			if(isEmpty())
76 			{
77 				return false;
78 			}
79 			else
80 			{
81 				return (nValue >= mnMinimum) && (nValue < mnMaximum);
82 			}
83 		}
84 
85         using Base::overlaps;
86 
overlaps(const BasicBox & rBox) const87 		bool overlaps(const BasicBox& rBox) const
88 		{
89 			if(isEmpty())
90 			{
91 				return false;
92 			}
93 			else
94 			{
95 				if(rBox.isEmpty())
96 				{
97 					return false;
98 				}
99 				else
100 				{
101 					return !((rBox.mnMaximum <= mnMinimum) || (rBox.mnMinimum >= mnMaximum));
102 				}
103 			}
104 		}
105 
grow(sal_Int32 nValue)106 		void grow(sal_Int32 nValue)
107 		{
108 			if(!isEmpty())
109 			{
110 				bool bLessThanZero(nValue < 0);
111 
112 				if(nValue > 0 || bLessThanZero)
113 				{
114 					mnMinimum -= nValue;
115 					mnMaximum += nValue;
116 
117 					if(bLessThanZero)
118 					{
119 						// test if range did collapse
120 						if(mnMinimum > mnMaximum)
121 						{
122 							// if yes, collapse to center
123 							mnMinimum = mnMaximum = ((mnMaximum + mnMinimum - 1) / 2);
124 						}
125 					}
126 				}
127 			}
128 		}
129 	};
130 
131 } // end of namespace basegfx
132 
133 #endif /* _BGFX_RANGE_BASICBOX_HXX */
134