Back to index

lightning-sunbird  0.9+nobinonly
nsRegionBeOS.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 <Region.h>
00039 
00040 #include "nsRegionBeOS.h"
00041 #include "prmem.h"
00042 
00043 #ifdef DEBUG_REGIONS 
00044 static int nRegions; 
00045 #endif 
00046 
00047 
00048 nsRegionBeOS :: nsRegionBeOS()
00049 {
00050 #ifdef DEBUG_REGIONS 
00051   ++nRegions; 
00052   printf("REGIONS+ = %i\n", nRegions); 
00053 #endif 
00054  
00055   mRegion.MakeEmpty();
00056   mRegionType = eRegionComplexity_empty;
00057 }
00058 
00059 nsRegionBeOS :: ~nsRegionBeOS()
00060 {
00061 #ifdef DEBUG_REGIONS 
00062   --nRegions; 
00063   printf("REGIONS- = %i\n", nRegions); 
00064 #endif 
00065  
00066   mRegion.MakeEmpty();
00067 }
00068 
00069 NS_IMPL_ISUPPORTS1(nsRegionBeOS, nsIRegion)
00070 
00071 nsresult nsRegionBeOS :: Init(void)
00072 {
00073   mRegion.MakeEmpty();
00074   mRegionType = eRegionComplexity_empty;
00075   return NS_OK;
00076 }
00077 
00078 void nsRegionBeOS :: SetTo(const nsIRegion &aRegion)
00079 {
00080   Init();
00081   
00082   nsRegionBeOS *pRegion = (nsRegionBeOS *)&aRegion;
00083 
00084   mRegion = pRegion->mRegion;
00085   SetRegionType();
00086 }
00087 
00088 void nsRegionBeOS :: SetTo(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00089 {
00090   Init();
00091   
00092        mRegion.Set(BRect(aX, aY, aX + aWidth - 1, aY + aHeight - 1));
00093        SetRegionType();
00094 }
00095 
00096 void nsRegionBeOS :: Intersect(const nsIRegion &aRegion)
00097 {
00098        nsRegionBeOS *pRegion = (nsRegionBeOS*)&aRegion;
00099 
00100        mRegion.IntersectWith(&pRegion->mRegion);
00101        SetRegionType();
00102 }
00103 
00104 void nsRegionBeOS :: Intersect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00105 {
00106   BRegion tRegion;
00107   tRegion.Set( BRect( aX, aY, aX + aWidth - 1, aY + aHeight - 1 ) );
00108   mRegion.IntersectWith(&tRegion);
00109   SetRegionType();
00110 }
00111 
00112 void nsRegionBeOS :: Union(const nsIRegion &aRegion)
00113 {
00114        nsRegionBeOS *pRegion = (nsRegionBeOS*)&aRegion;
00115 
00116        mRegion.Include(&pRegion->mRegion);
00117        SetRegionType();
00118 }
00119 
00120 void nsRegionBeOS :: Union(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00121 {
00122        mRegion.Include(BRect(aX, aY, aX + aWidth - 1, aY + aHeight - 1));
00123        SetRegionType();
00124 }
00125 
00126 void nsRegionBeOS :: Subtract(const nsIRegion &aRegion)
00127 {
00128   nsRegionBeOS *pRegion = (nsRegionBeOS*)&aRegion;
00129 
00130   mRegion.Exclude(&pRegion->mRegion);
00131   SetRegionType();
00132 }
00133 
00134 void nsRegionBeOS :: Subtract(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00135 {
00136        mRegion.Exclude(BRect(aX, aY, aX + aWidth - 1, aY + aHeight - 1));
00137        SetRegionType();
00138 }
00139 
00140 PRBool nsRegionBeOS :: IsEmpty(void)
00141 {
00142   if( mRegionType == eRegionComplexity_empty )
00143     return PR_TRUE;
00144   return PR_FALSE;
00145 }
00146 
00147 PRBool nsRegionBeOS :: IsEqual(const nsIRegion &aRegion)
00148 {
00149 #ifdef DEBUG
00150   printf(" - nsRegionBeOS :: IsEqual not implemented!\n");
00151 #endif
00152   return PR_FALSE;
00153 }
00154 
00155 void nsRegionBeOS :: GetBoundingBox(PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight)
00156 {
00157   if( mRegion.CountRects() ) {
00158     BRect rect = mRegion.Frame();
00159     *aX = nscoord(rect.left);
00160     *aY = nscoord(rect.top);
00161     *aWidth = nscoord(rect.Width()+1);
00162     *aHeight = nscoord(rect.Height()+1);
00163   }
00164   else
00165   {
00166        *aX = *aY = *aWidth = *aHeight = 0;
00167   }
00168 }
00169 
00170 void nsRegionBeOS :: Offset(PRInt32 aXOffset, PRInt32 aYOffset)
00171 {
00172        mRegion.OffsetBy( aXOffset, aYOffset );
00173 }
00174 
00175 void nsRegionBeOS :: SetRegionType(void)
00176 {
00177   if(mRegion.CountRects() == 1)
00178     mRegionType = eRegionComplexity_rect ;
00179   else if(mRegion.CountRects() > 1)
00180     mRegionType = eRegionComplexity_complex ;
00181   else
00182     mRegionType = eRegionComplexity_empty;
00183 }
00184 
00185 PRBool nsRegionBeOS :: ContainsRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00186 {
00187        return mRegion.Intersects(BRect(aX, aY, aX + aWidth - 1, aY + aHeight - 1));
00188 }
00189 
00190 NS_IMETHODIMP nsRegionBeOS :: GetRects(nsRegionRectSet **aRects)
00191 {
00192        nsRegionRectSet   *rects;
00193        int               nbox;
00194        nsRegionRect      *rect;
00195        
00196        NS_ASSERTION(!(nsnull == aRects), "bad ptr");
00197        
00198        //code lifted from old xfe. MMP
00199        
00200        nbox = mRegion.CountRects();
00201        
00202        rects = *aRects;
00203        
00204        if ((nsnull == rects) || (rects->mRectsLen < (PRUint32)nbox))
00205        {
00206               void *buf = PR_Realloc(rects, sizeof(nsRegionRectSet) + (sizeof(nsRegionRect) * (nbox - 1)));
00207 
00208               if(nsnull == buf)
00209               {
00210                      if(nsnull != rects)
00211                             rects->mNumRects = 0;
00212 
00213                      return NS_OK;
00214               }
00215 
00216               rects = (nsRegionRectSet *)buf;
00217               rects->mRectsLen = nbox;
00218        }
00219 
00220        rects->mNumRects = nbox;
00221        rects->mArea = 0;
00222        rect = &rects->mRects[0];
00223 
00224        for(int32 i = 0; i < nbox; i++)
00225        {
00226               BRect r = mRegion.RectAt(i);
00227     rect->x = nscoord(r.left); 
00228     rect->width = nscoord(r.right - r.left + 1); 
00229     rect->y = nscoord(r.top); 
00230     rect->height = nscoord(r.bottom - r.top + 1); 
00231 
00232               rects->mArea += rect->width * rect->height;
00233 
00234               rect++;
00235        }
00236 
00237        *aRects = rects;
00238 
00239        return NS_OK;
00240 }
00241 
00242 NS_IMETHODIMP nsRegionBeOS :: FreeRects(nsRegionRectSet *aRects)
00243 {
00244        if(nsnull != aRects)
00245               PR_Free((void *)aRects);
00246 
00247        return NS_OK;
00248 }
00249 
00250 NS_IMETHODIMP nsRegionBeOS :: GetNativeRegion(void *&aRegion) const
00251 {
00252        aRegion = (void *)&mRegion;
00253        return NS_OK;
00254 }
00255 
00256 NS_IMETHODIMP nsRegionBeOS :: GetRegionComplexity(nsRegionComplexity &aComplexity) const
00257 {
00258        aComplexity = mRegionType;
00259        return NS_OK;
00260 }