Back to index

lightning-sunbird  0.9+nobinonly
nsRegionWin.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsRegionWin.h"
00039 #include "prmem.h"
00040 
00041 nsRegionWin :: nsRegionWin()
00042 {
00043   mRegion = NULL;
00044   mRegionType = NULLREGION;
00045   mData = NULL;
00046   mDataSize = 0;
00047 }
00048 
00049 nsRegionWin :: ~nsRegionWin()
00050 {
00051   if (NULL != mRegion)
00052   {
00053     ::DeleteObject(mRegion);
00054     mRegion = NULL;
00055   }
00056 
00057   if (NULL != mData)
00058   {
00059     PR_Free(mData);
00060 
00061     mData = NULL;
00062     mDataSize = 0;
00063   }
00064 }
00065 
00066 NS_IMPL_ISUPPORTS1(nsRegionWin, nsIRegion)
00067 
00068 nsresult nsRegionWin :: Init(void)
00069 {
00070   if (NULL != mRegion) {
00071     ::SetRectRgn(mRegion, 0, 0, 0, 0);
00072     FreeRects(nsnull);
00073   }
00074   else {
00075     mRegion = ::CreateRectRgn(0, 0, 0, 0);
00076   }
00077   
00078   mRegionType = NULLREGION;
00079 
00080   return NS_OK;
00081 }
00082 
00083 void nsRegionWin :: SetTo(const nsIRegion &aRegion)
00084 {
00085   nsRegionWin *pRegion = (nsRegionWin *)&aRegion;
00086 
00087   mRegionType = ::CombineRgn(mRegion, pRegion->mRegion, NULL, RGN_COPY);
00088 }
00089 
00090 void nsRegionWin :: SetTo(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00091 {
00092   if (NULL != mRegion)
00093     ::SetRectRgn(mRegion, aX, aY, aX + aWidth, aY + aHeight);
00094   else
00095     mRegion = ::CreateRectRgn(aX, aY, aX + aWidth, aY + aHeight);
00096 
00097   if ((aWidth == 0) || (aHeight == 0))
00098     mRegionType = NULLREGION;
00099   else
00100     mRegionType = SIMPLEREGION;
00101 }
00102 
00103 void nsRegionWin :: Intersect(const nsIRegion &aRegion)
00104 {
00105   nsRegionWin *pRegion = (nsRegionWin *)&aRegion;
00106 
00107   mRegionType = ::CombineRgn(mRegion, mRegion, pRegion->mRegion, RGN_AND);
00108 }
00109 
00110 void nsRegionWin :: Intersect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00111 {
00112   HRGN tRegion;
00113 
00114   tRegion = ::CreateRectRgn(aX, aY, aX + aWidth, aY + aHeight);
00115   mRegionType = ::CombineRgn(mRegion, mRegion, tRegion, RGN_AND);
00116 
00117   ::DeleteObject(tRegion);
00118 }
00119 
00120 void nsRegionWin :: Union(const nsIRegion &aRegion)
00121 {
00122   nsRegionWin *pRegion = (nsRegionWin *)&aRegion;
00123 
00124   mRegionType = ::CombineRgn(mRegion, mRegion, pRegion->mRegion, RGN_OR);
00125 }
00126 
00127 void nsRegionWin :: Union(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00128 {
00129   HRGN tRegion;
00130 
00131   tRegion = ::CreateRectRgn(aX, aY, aX + aWidth, aY + aHeight);
00132   mRegionType = ::CombineRgn(mRegion, mRegion, tRegion, RGN_OR);
00133 
00134   ::DeleteObject(tRegion);
00135 }
00136 
00137 void nsRegionWin :: Subtract(const nsIRegion &aRegion)
00138 {
00139   nsRegionWin *pRegion = (nsRegionWin *)&aRegion;
00140 
00141   mRegionType = ::CombineRgn(mRegion, mRegion, pRegion->mRegion, RGN_DIFF);
00142 }
00143 
00144 void nsRegionWin :: Subtract(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00145 {
00146   HRGN tRegion;
00147 
00148   tRegion = ::CreateRectRgn(aX, aY, aX + aWidth, aY + aHeight);
00149   mRegionType = ::CombineRgn(mRegion, mRegion, tRegion, RGN_DIFF);
00150 
00151   ::DeleteObject(tRegion);
00152 }
00153 
00154 PRBool nsRegionWin :: IsEmpty(void)
00155 {
00156   return (mRegionType == NULLREGION) ? PR_TRUE : PR_FALSE;
00157 }
00158 
00159 PRBool nsRegionWin :: IsEqual(const nsIRegion &aRegion)
00160 {
00161   nsRegionWin *pRegion = (nsRegionWin *)&aRegion;
00162 
00163   return ::EqualRgn(mRegion, pRegion->mRegion) ? PR_TRUE : PR_FALSE;
00164 }
00165 
00166 void nsRegionWin :: GetBoundingBox(PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight)
00167 {
00168   RECT  bounds;
00169 
00170   if (mRegionType != NULLREGION)
00171   {
00172     ::GetRgnBox(mRegion, &bounds);
00173 
00174     *aX = bounds.left;
00175     *aY = bounds.top;
00176     *aWidth = bounds.right - bounds.left;
00177     *aHeight = bounds.bottom - bounds.top;
00178   }
00179   else
00180     *aX = *aY = *aWidth = *aHeight = 0;
00181 }
00182 
00183 void nsRegionWin :: Offset(PRInt32 aXOffset, PRInt32 aYOffset)
00184 {
00185   ::OffsetRgn(mRegion, aXOffset, aYOffset);
00186 }
00187 
00188 PRBool nsRegionWin :: ContainsRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00189 {
00190   RECT  trect;
00191 
00192   trect.left = aX;
00193   trect.top = aY;
00194   trect.right = aX + aWidth;
00195   trect.bottom = aY + aHeight;
00196 
00197   return ::RectInRegion(mRegion, &trect) ? PR_TRUE : PR_FALSE;
00198 }
00199 
00200 NS_IMETHODIMP nsRegionWin :: GetRects(nsRegionRectSet **aRects)
00201 {
00202   nsRegionRectSet *rects;
00203   nsRegionRect    *rect;
00204        LPRECT          pRects;
00205        DWORD           dwCount, dwResult;
00206        unsigned int    num_rects;
00207 
00208   NS_ASSERTION(!(nsnull == aRects), "bad ptr");
00209 
00210   rects = *aRects;
00211 
00212   if (nsnull != rects)
00213     rects->mNumRects = 0;
00214 
00215   // code lifted from old winfe. MMP
00216 
00217        /* Get the size of the region data */
00218        dwCount = GetRegionData(mRegion, 0, NULL);
00219 
00220        NS_ASSERTION(!(dwCount == 0), "bad region");
00221 
00222        if (dwCount == 0)
00223          return NS_OK;
00224 
00225   if (dwCount > mDataSize)
00226   {
00227     if (NULL != mData)
00228       PR_Free(mData);
00229 
00230          mData = (LPRGNDATA)PR_Malloc(dwCount);
00231   }
00232 
00233        NS_ASSERTION(!(nsnull == mData), "failed allocation");
00234 
00235        if (mData == NULL)
00236          return NS_OK;
00237 
00238        dwResult = GetRegionData(mRegion, dwCount, mData);
00239 
00240        NS_ASSERTION(!(dwResult == 0), "get data failed");
00241 
00242        if (dwResult == 0)
00243               return NS_OK;
00244 
00245   if ((nsnull == rects) || (rects->mRectsLen < mData->rdh.nCount))
00246   {
00247     void *buf = PR_Realloc(rects, sizeof(nsRegionRectSet) + (sizeof(nsRegionRect) * (mData->rdh.nCount - 1)));
00248 
00249     if (nsnull == buf)
00250     {
00251       if (nsnull != rects)
00252         rects->mNumRects = 0;
00253 
00254       return NS_OK;
00255     }
00256 
00257     rects = (nsRegionRectSet *)buf;
00258     rects->mRectsLen = mData->rdh.nCount;
00259   }
00260 
00261   rects->mNumRects = mData->rdh.nCount;
00262   rects->mArea = 0;
00263   rect = &rects->mRects[0];
00264 
00265   for (pRects = (LPRECT)mData->Buffer, num_rects = 0; 
00266                  num_rects < mData->rdh.nCount; 
00267                  num_rects++, pRects++, rect++)
00268   {
00269               rect->x = pRects->left;
00270               rect->y = pRects->top;
00271               rect->width = pRects->right - rect->x;
00272               rect->height = pRects->bottom - rect->y;
00273 
00274     rects->mArea += rect->width * rect->height;
00275        }
00276 
00277   *aRects = rects;
00278 
00279   return NS_OK;
00280 }
00281 
00282 NS_IMETHODIMP nsRegionWin :: FreeRects(nsRegionRectSet *aRects)
00283 {
00284   if (nsnull != aRects)
00285     PR_Free((void *)aRects);
00286 
00287   if (NULL != mData)
00288   {
00289     PR_Free(mData);
00290 
00291     mData = NULL;
00292     mDataSize = 0;
00293   }
00294 
00295   return NS_OK;
00296 }
00297 
00298 NS_IMETHODIMP nsRegionWin :: GetNativeRegion(void *&aRegion) const
00299 {
00300   aRegion = (void *)mRegion;
00301   return NS_OK;
00302 }
00303 
00304 NS_IMETHODIMP nsRegionWin :: GetRegionComplexity(nsRegionComplexity &aComplexity) const
00305 {
00306   switch (mRegionType)
00307   {
00308     case NULLREGION:
00309       aComplexity = eRegionComplexity_empty;
00310       break;
00311 
00312     case SIMPLEREGION:
00313       aComplexity = eRegionComplexity_rect;
00314       break;
00315 
00316     default:
00317     case COMPLEXREGION:
00318       aComplexity = eRegionComplexity_complex;
00319       break;
00320   }
00321 
00322   return NS_OK;
00323 }