Back to index

lightning-sunbird  0.9+nobinonly
nsRegionPh.h
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 #ifndef nsRegionPh_h___
00039 #define nsRegionPh_h___
00040 
00041 #include "nsIRegion.h"
00042 #include "prmem.h"
00043 #include <Pt.h>
00044 
00045 class nsRegionPh : public nsIRegion
00046 {
00047 public:
00048   inline nsRegionPh()
00049               {
00050               mRegion = NULL;
00051               mRegionType = eRegionComplexity_empty;
00052               }
00053 
00054   inline nsRegionPh(PhTile_t *tiles)
00055               {
00056               mRegion = tiles; /* assume ownership */
00057               mRegionType = (mRegion == NULL) ? eRegionComplexity_empty : eRegionComplexity_complex;
00058               }
00059 
00060   virtual ~nsRegionPh()
00061               {
00062               if( mRegion ) PhFreeTiles( mRegion );
00063               mRegion = nsnull;
00064               }
00065 
00066   NS_DECL_ISUPPORTS
00067 
00068   virtual nsresult Init()
00069               {
00070               SetRegionEmpty();
00071               return NS_OK;
00072               }
00073 
00074   virtual void SetTo(const nsIRegion &aRegion)
00075               {
00076               PhTile_t *tiles;
00077               aRegion.GetNativeRegion( ( void*& ) tiles );
00078               SetRegionEmpty( );
00079               mRegion = PhCopyTiles( tiles );
00080               }
00081 
00082   virtual void SetTo(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00083               {
00084        SetRegionEmpty( );
00085 
00086        if ( aWidth > 0 && aHeight > 0 ) {
00087          /* Create a temporary tile to  assign to mRegion */
00088          PhTile_t *tile = PhGetTile( );
00089          tile->rect.ul.x = aX;
00090          tile->rect.ul.y = aY;
00091          tile->rect.lr.x = (aX+aWidth-1);
00092          tile->rect.lr.y = (aY+aHeight-1);
00093          tile->next = NULL;
00094          mRegion = tile;
00095          }
00096               }
00097 
00098   virtual void Intersect(const nsIRegion &aRegion)
00099               {
00100        PhTile_t *original = mRegion;
00101        PhTile_t *tiles;
00102        aRegion.GetNativeRegion( ( void*& ) tiles );
00103        mRegion = PhIntersectTilings( original, tiles, NULL);
00104        if( mRegion )
00105          mRegion = PhCoalesceTiles( PhMergeTiles( PhSortTiles( mRegion )));
00106        PhFreeTiles( original );
00107        if ( mRegion == NULL )
00108          SetTo(0, 0, 1, 1);
00109               }
00110 
00111   virtual void Intersect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight);
00112 
00113   virtual void Union(const nsIRegion &aRegion)
00114               {
00115               PhTile_t *tiles;
00116               aRegion.GetNativeRegion( ( void*& ) tiles );
00117               mRegion = PhAddMergeTiles( mRegion, PhCopyTiles( tiles ), NULL );
00118               }
00119 
00120   virtual void Union(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00121               {
00122               if( aWidth > 0 && aHeight > 0 ) {
00123                 /* Create a temporary tile to  assign to mRegion */
00124                 PhTile_t *tile = PhGetTile();
00125                 tile->rect.ul.x = aX;
00126                 tile->rect.ul.y = aY;
00127                 tile->rect.lr.x = (aX+aWidth-1);
00128                 tile->rect.lr.y = (aY+aHeight-1);
00129                 tile->next = NULL;
00130 
00131                 mRegion = PhAddMergeTiles( mRegion, tile, NULL );
00132                 }
00133               }
00134 
00135   virtual void Subtract(const nsIRegion &aRegion)
00136               {
00137               PhTile_t *tiles;
00138               aRegion.GetNativeRegion((void*&)tiles);
00139               mRegion = PhClipTilings( mRegion, tiles, NULL );
00140               }
00141 
00142   virtual void Subtract(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
00143               {
00144               if( aWidth > 0 && aHeight > 0 ) {
00145                 /* Create a temporary tile to  assign to mRegion */
00146                 PhTile_t tile;
00147                 tile.rect.ul.x = aX;
00148                 tile.rect.ul.y = aY;
00149                 tile.rect.lr.x = aX + aWidth - 1;
00150                 tile.rect.lr.y = aY + aHeight - 1;
00151                 tile.next = NULL;
00152 
00153                 mRegion = PhClipTilings( mRegion, &tile, NULL );
00154                      }
00155               }
00156 
00157   virtual PRBool IsEmpty(void)
00158               {
00159               if ( !mRegion )
00160                 return PR_TRUE;
00161               if ( mRegion->rect.ul.x == 0 && mRegion->rect.ul.y == 0 &&
00162                 mRegion->rect.lr.x == 0 && mRegion->rect.lr.y == 0 )
00163                 return PR_TRUE;
00164               return PR_FALSE;
00165               }
00166 
00167   virtual PRBool IsEqual(const nsIRegion &aRegion);
00168   virtual void GetBoundingBox(PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight);
00169 
00170   virtual void Offset(PRInt32 aXOffset, PRInt32 aYOffset)
00171               {
00172        if( ( aXOffset || aYOffset ) && mRegion ) {
00173          PhPoint_t p = { aXOffset, aYOffset };
00174 
00175          /* 99.99% there is only one tile - so simplify things, while allow the general case to work as well */
00176          if( !mRegion->next ) PtTranslateRect( &mRegion->rect, &p );
00177          else PhTranslateTiles( mRegion, &p );
00178          }
00179               }
00180 
00181   virtual PRBool ContainsRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight);
00182   NS_IMETHOD GetRects(nsRegionRectSet **aRects);
00183 
00184   inline NS_IMETHODIMP FreeRects(nsRegionRectSet *aRects)
00185               {
00186               if( nsnull != aRects ) PR_Free( ( void * )aRects );
00187               return NS_OK;
00188               }
00189 
00190   inline NS_IMETHODIMP GetNativeRegion(void *&aRegion) const
00191               {
00192               aRegion = (void *) mRegion;
00193               return NS_OK;
00194               }
00195 
00196   inline NS_IMETHODIMP GetRegionComplexity(nsRegionComplexity &aComplexity) const
00197               {
00198               aComplexity = mRegionType;
00199               return NS_OK;
00200               }
00201 
00202   inline NS_IMETHOD GetNumRects(PRUint32 *aRects) const { *aRects = 0; return NS_OK; }
00203 
00204 private:
00205   virtual void SetRegionEmpty()
00206               {
00207               if( mRegion ) PhFreeTiles( mRegion );
00208               mRegion = NULL;
00209               mRegionType = eRegionComplexity_empty;
00210               }
00211 
00212   PhTile_t             *mRegion;
00213   nsRegionComplexity    mRegionType;             // Not really used!
00214 };
00215 
00216 #endif  // nsRegionPh_h___