Back to index

lightning-sunbird  0.9+nobinonly
nsRegionQt.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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  *   Lars Knoll <knoll@kde.org>
00024  *   Zack Rusin <zack@kde.org>
00025  *   John C. Griggs <johng@corel.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsRegionQt.h"
00042 #include "prmem.h"
00043 #include "nsRenderingContextQt.h"
00044 #include <qregion.h>
00045 
00046 #include "qtlog.h"
00047 
00048 nsRegionQt::nsRegionQt() : mRegion()
00049 {
00050 }
00051 
00052 nsRegionQt::~nsRegionQt()
00053 {
00054 }
00055 
00056 NS_IMPL_ISUPPORTS1(nsRegionQt, nsIRegion)
00057 
00058 nsresult nsRegionQt::Init(void)
00059 {
00060     mRegion = QRegion();
00061     return NS_OK;
00062 }
00063 
00064 void nsRegionQt::SetTo(const nsIRegion &aRegion)
00065 {
00066     nsRegionQt *pRegion = (nsRegionQt*)&aRegion;
00067 
00068     mRegion = pRegion->mRegion;
00069 }
00070 
00071 void nsRegionQt::SetTo(const nsRegionQt *aRegion)
00072 {
00073     mRegion = aRegion->mRegion;
00074 }
00075 
00076 void nsRegionQt::SetTo(PRInt32 aX,PRInt32 aY,PRInt32 aWidth,PRInt32 aHeight)
00077 {
00078     mRegion = QRegion(aX, aY, aWidth, aHeight);;
00079 }
00080 
00081 void nsRegionQt::Intersect(const nsIRegion &aRegion)
00082 {
00083     nsRegionQt *pRegion = (nsRegionQt*)&aRegion;
00084 
00085     mRegion = mRegion.intersect(pRegion->mRegion);
00086 }
00087 
00088 void nsRegionQt::Intersect(PRInt32 aX,PRInt32 aY,
00089                            PRInt32 aWidth,PRInt32 aHeight)
00090 {
00091     mRegion = mRegion.intersect(QRect(aX, aY, aWidth, aHeight));
00092 }
00093 
00094 void nsRegionQt::Union(const nsIRegion &aRegion)
00095 {
00096     nsRegionQt *pRegion = (nsRegionQt*)&aRegion;
00097 
00098     mRegion = mRegion.unite(pRegion->mRegion);
00099 }
00100 
00101 void nsRegionQt::Union(PRInt32 aX,PRInt32 aY,PRInt32 aWidth,PRInt32 aHeight)
00102 {
00103     mRegion = mRegion.unite(QRect(aX, aY, aWidth, aHeight));
00104 }
00105 
00106 void nsRegionQt::Subtract(const nsIRegion &aRegion)
00107 {
00108     nsRegionQt *pRegion = (nsRegionQt*)&aRegion;
00109 
00110     mRegion = mRegion.subtract(pRegion->mRegion);
00111 }
00112 
00113 void nsRegionQt::Subtract(PRInt32 aX,PRInt32 aY,PRInt32 aWidth,PRInt32 aHeight)
00114 {
00115     mRegion = mRegion.subtract(QRect(aX, aY, aWidth, aHeight));
00116 }
00117 
00118 PRBool nsRegionQt::IsEmpty(void)
00119 {
00120     return mRegion.isEmpty();
00121 }
00122 
00123 PRBool nsRegionQt::IsEqual(const nsIRegion &aRegion)
00124 {
00125     nsRegionQt *pRegion = (nsRegionQt*)&aRegion;
00126 
00127     return (mRegion == pRegion->mRegion);
00128 }
00129 
00130 void nsRegionQt::GetBoundingBox(PRInt32 *aX,PRInt32 *aY,
00131                                 PRInt32 *aWidth,PRInt32 *aHeight)
00132 {
00133     QRect rect = mRegion.boundingRect();
00134 
00135     *aX = rect.x();
00136     *aY = rect.y();
00137     *aWidth = rect.width();
00138     *aHeight = rect.height();
00139 }
00140 
00141 void nsRegionQt::Offset(PRInt32 aXOffset, PRInt32 aYOffset)
00142 {
00143     mRegion.translate(aXOffset, aYOffset);
00144 }
00145 
00146 PRBool nsRegionQt::ContainsRect(PRInt32 aX,PRInt32 aY,
00147                                 PRInt32 aWidth,PRInt32 aHeight)
00148 {
00149     return mRegion.contains(QRect(aX, aY, aWidth, aHeight));
00150 }
00151 
00152 NS_IMETHODIMP nsRegionQt::GetRects(nsRegionRectSet **aRects)
00153 {
00154     NS_ASSERTION(!(nsnull == aRects), "bad ptr");
00155 
00156     QMemArray<QRect>   array = mRegion.rects();
00157     PRUint32        size  = array.size();
00158     nsRegionRect    *rect  = nsnull;
00159     nsRegionRectSet *rects = *aRects;
00160 
00161     if (nsnull == rects || rects->mRectsLen < (PRUint32)size) {
00162         void *buf = PR_Realloc(rects,
00163                                sizeof(nsRegionRectSet)
00164                                + (sizeof(nsRegionRect) * (size - 1)));
00165 
00166         if (nsnull == buf) {
00167             if (nsnull != rects)
00168                 rects->mNumRects = 0;
00169             return NS_OK;
00170         }
00171         rects = (nsRegionRectSet*)buf;
00172         rects->mRectsLen = size;
00173     }
00174     rects->mNumRects = size;
00175     rects->mArea = 0;
00176     rect = &rects->mRects[0];
00177 
00178     for (PRUint32 i = 0; i < size; i++) {
00179         const QRect &qRect = array.at(i);
00180 
00181         rect->x = qRect.x();
00182         rect->y = qRect.y();
00183         rect->width = qRect.width();
00184         rect->height = qRect.height();
00185 
00186         rects->mArea += rect->width * rect->height;
00187 
00188         rect++;
00189     }
00190     *aRects = rects;
00191     return NS_OK;
00192 }
00193 
00194 NS_IMETHODIMP nsRegionQt::FreeRects(nsRegionRectSet *aRects)
00195 {
00196     if (nsnull != aRects) {
00197         PR_Free((void*)aRects);
00198     }
00199     return NS_OK;
00200 }
00201 
00202 NS_IMETHODIMP nsRegionQt::GetNativeRegion(void *&aRegion) const
00203 {
00204     aRegion = (void*)&mRegion;
00205     return NS_OK;
00206 }
00207 
00208 NS_IMETHODIMP nsRegionQt::GetRegionComplexity(nsRegionComplexity &aComplexity) const
00209 {
00210     // cast to avoid const-ness problems on some compilers
00211     if (mRegion.isEmpty()) {
00212         aComplexity = eRegionComplexity_empty;
00213     }
00214     else {
00215         aComplexity = eRegionComplexity_rect;
00216     }
00217     return NS_OK;
00218 }
00219 
00220 void nsRegionQt::SetRegionEmpty()
00221 {
00222     mRegion = QRegion();
00223 }
00224 
00225 NS_IMETHODIMP nsRegionQt::GetNumRects(PRUint32 *aRects) const
00226 {
00227   *aRects = mRegion.rects().size();
00228 
00229   return NS_OK;
00230 }