Back to index

lightning-sunbird  0.9+nobinonly
nsRegionImpl.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Dainis Jonitis, <Dainis_Jonitis@swh-t.lv>.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsRegion.h"
00038 #include "nsRegionImpl.h"
00039 
00040 
00041 nsresult nsRegionImpl::Init (void)
00042 {
00043   mRegion.SetEmpty ();
00044   return NS_OK;
00045 }
00046 
00047 void nsRegionImpl::SetTo (const nsIRegion &aRegion)
00048 {
00049   const nsRegionImpl* pRegion = NS_STATIC_CAST (const nsRegionImpl*, &aRegion);
00050   mRegion = pRegion->mRegion;
00051 }
00052 
00053 void nsRegionImpl::SetTo (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00054 {
00055   mRegion = nsRect (aX, aY, aWidth, aHeight);
00056 }
00057 
00058 void nsRegionImpl::Intersect (const nsIRegion &aRegion)
00059 {
00060   const nsRegionImpl* pRegion = NS_STATIC_CAST (const nsRegionImpl*, &aRegion);
00061   mRegion.And (mRegion, pRegion->mRegion);
00062 }
00063 
00064 void nsRegionImpl::Intersect (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00065 {
00066   mRegion.And (mRegion, nsRect (aX, aY, aWidth, aHeight));
00067 }
00068 
00069 void nsRegionImpl::Union (const nsIRegion &aRegion)
00070 {
00071   const nsRegionImpl* pRegion = NS_STATIC_CAST (const nsRegionImpl*, &aRegion);
00072   mRegion.Or (mRegion, pRegion->mRegion);
00073 }
00074 
00075 void nsRegionImpl::Union (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00076 {
00077   mRegion.Or (mRegion, nsRect (aX, aY, aWidth, aHeight));
00078 }
00079 
00080 void nsRegionImpl::Subtract (const nsIRegion &aRegion)
00081 {
00082   const nsRegionImpl* pRegion = NS_STATIC_CAST (const nsRegionImpl*, &aRegion);
00083   mRegion.Sub (mRegion, pRegion->mRegion);
00084 }
00085 
00086 void nsRegionImpl::Subtract (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00087 {
00088   mRegion.Sub (mRegion, nsRect (aX, aY, aWidth, aHeight));
00089 }
00090 
00091 PRBool nsRegionImpl::IsEmpty (void)
00092 {
00093   return mRegion.IsEmpty ();
00094 }
00095 
00096 PRBool nsRegionImpl::IsEqual (const nsIRegion &aRegion)
00097 {
00098   const nsRegionImpl* pRegion = NS_STATIC_CAST (const nsRegionImpl*, &aRegion);
00099   return mRegion.IsEqual (pRegion->mRegion);
00100 }
00101 
00102 void nsRegionImpl::GetBoundingBox (PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight)
00103 {
00104   const nsRect& BoundRect = mRegion.GetBounds();
00105   *aX = BoundRect.x;
00106   *aY = BoundRect.y;
00107   *aWidth  = BoundRect.width;
00108   *aHeight = BoundRect.height;
00109 }
00110 
00111 void nsRegionImpl::Offset (PRInt32 aXOffset, PRInt32 aYOffset)
00112 {
00113   mRegion.MoveBy (aXOffset, aYOffset);
00114 }
00115 
00116 PRBool nsRegionImpl::ContainsRect (PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00117 {
00118   nsRegion TmpRegion;
00119   TmpRegion.And (mRegion, nsRect (aX, aY, aWidth, aHeight));
00120   return (!TmpRegion.IsEmpty ());
00121 }
00122 
00123 nsresult nsRegionImpl::GetRects (nsRegionRectSet **aRects)
00124 {
00125   if (!aRects)
00126     return NS_ERROR_NULL_POINTER;
00127 
00128   nsRegionRectSet* pRegionSet = *aRects;
00129   PRUint32 NumRects = mRegion.GetNumRects ();
00130 
00131   if (!pRegionSet)                          // Not yet allocated
00132   {
00133     PRUint8* pBuf = new PRUint8 [sizeof (nsRegionRectSet) + NumRects * sizeof (nsRegionRect)];
00134     pRegionSet = NS_REINTERPRET_CAST (nsRegionRectSet*, pBuf);
00135     pRegionSet->mRectsLen = NumRects + 1;
00136   } else                                    // Already allocated in previous call
00137   {
00138     if (NumRects > pRegionSet->mRectsLen)   // passed array is not big enough - reallocate it.
00139     {
00140       delete [] NS_REINTERPRET_CAST (PRUint8*, pRegionSet);
00141       PRUint8* pBuf = new PRUint8 [sizeof (nsRegionRectSet) + NumRects * sizeof (nsRegionRect)];
00142       pRegionSet = NS_REINTERPRET_CAST (nsRegionRectSet*, pBuf);
00143       pRegionSet->mRectsLen = NumRects + 1;
00144     }
00145   }
00146   pRegionSet->mNumRects = NumRects;
00147   *aRects = pRegionSet;
00148 
00149 
00150   nsRegionRectIterator ri (mRegion);
00151   nsRegionRect* pDest = &pRegionSet->mRects [0];
00152   const nsRect* pSrc;
00153 
00154   while ((pSrc = ri.Next ()) != nsnull)
00155   {
00156     pDest->x = pSrc->x;
00157     pDest->y = pSrc->y;
00158     pDest->width  = pSrc->width;
00159     pDest->height = pSrc->height;
00160 
00161     ++pDest;
00162   }
00163 
00164   return NS_OK;
00165 }
00166 
00167 nsresult nsRegionImpl::FreeRects (nsRegionRectSet *aRects)
00168 {
00169   if (!aRects)
00170     return NS_ERROR_NULL_POINTER;
00171 
00172   delete [] NS_REINTERPRET_CAST (PRUint8*, aRects);
00173   return NS_OK;
00174 }
00175 
00176 nsresult nsRegionImpl::GetNativeRegion (void *&aRegion) const
00177 {
00178   aRegion = 0;
00179   return NS_OK;
00180 }
00181 
00182 nsresult nsRegionImpl::GetRegionComplexity (nsRegionComplexity &aComplexity) const
00183 {
00184   switch (mRegion.GetNumRects ())
00185   {
00186     case 0:   aComplexity = eRegionComplexity_empty;    break;
00187     case 1:   aComplexity = eRegionComplexity_rect;     break;
00188     default:  aComplexity = eRegionComplexity_complex;  break;
00189   }
00190 
00191   return NS_OK;
00192 }
00193 
00194 nsresult nsRegionImpl::GetNumRects (PRUint32 *aRects) const
00195 {
00196   *aRects = mRegion.GetNumRects ();
00197   return NS_OK;
00198 }