Bug 477708 - expose nsIRegion::getrects() to scripts r=vlad

This commit is contained in:
Taras Glek 2009-03-18 09:53:34 -07:00
parent c59c7d5701
commit 53e7be3128
4 changed files with 68 additions and 4 deletions

View File

@ -46,7 +46,7 @@ class nsIRegion;
[ptr] native nsIRegion(nsIRegion);
[scriptable, uuid(82d8f400-5bde-11d3-b033-b27a62766bbc)]
[scriptable, uuid(4d179656-a5bd-42a6-a937-c81f820dcf2f)]
interface nsIScriptableRegion : nsISupports
{
void init ( ) ;
@ -187,6 +187,13 @@ interface nsIScriptableRegion : nsISupports
**/
void offset ( in long aXOffset, in long aYOffset ) ;
/**
* @return null if there are no rects,
* @return flat array of rects,ie [x1,y1,width1,height1,x2...].
* The result will contain bogus data if values don't fit in 31 bit
**/
void getRects();
/**
* does the region intersect the rectangle?
*

View File

@ -55,6 +55,8 @@ REQUIRES = xpcom \
view \
unicharutil \
thebes \
js \
xpconnect \
$(NULL)
DIRS = shared thebes

View File

@ -22,6 +22,7 @@
*
* Contributor(s):
* Patrick Beard
* Taras Glek
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -38,10 +39,12 @@
* ***** END LICENSE BLOCK ***** */
#include "nsScriptableRegion.h"
#include "nsIRegion.h"
#include "nsCOMPtr.h"
#include "nsIXPConnect.h"
#include "nsServiceManagerUtils.h"
#include "jsapi.h"
nsScriptableRegion::nsScriptableRegion(nsIRegion* region) : mRegion(nsnull)
nsScriptableRegion::nsScriptableRegion(nsIRegion* region) : mRegion(nsnull), mRectSet(nsnull)
{
mRegion = region;
NS_IF_ADDREF(mRegion);
@ -49,7 +52,10 @@ nsScriptableRegion::nsScriptableRegion(nsIRegion* region) : mRegion(nsnull)
nsScriptableRegion::~nsScriptableRegion()
{
NS_IF_RELEASE(mRegion);
if (mRegion) {
mRegion->FreeRects(mRectSet);
NS_RELEASE(mRegion);
}
}
NS_IMPL_ISUPPORTS1(nsScriptableRegion, nsIScriptableRegion)
@ -149,3 +155,50 @@ NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIRegion** outRgn)
NS_IF_ADDREF(*outRgn);
return NS_OK;
}
NS_IMETHODIMP nsScriptableRegion::GetRects() {
nsAXPCNativeCallContext *ncc = nsnull;
nsIXPConnect *xpConnect;
nsresult rv = CallGetService(nsIXPConnect::GetCID(), &xpConnect);
NS_ENSURE_SUCCESS(rv, rv);
rv = xpConnect->GetCurrentNativeCallContext(&ncc);
NS_ENSURE_SUCCESS(rv, rv);
if (!ncc)
return NS_ERROR_FAILURE;
jsval *retvalPtr;
ncc->GetRetValPtr(&retvalPtr);
rv = mRegion->GetRects(&mRectSet);
NS_ENSURE_SUCCESS(rv, rv);
if (!mRectSet->mNumRects) {
*retvalPtr = JSVAL_NULL;
ncc->SetReturnValueWasSet(PR_TRUE);
return NS_OK;
}
JSContext *cx = nsnull;
rv = ncc->GetJSContext(&cx);
NS_ENSURE_SUCCESS(rv, rv);
JSObject *destArray = JS_NewArrayObject(cx, mRectSet->mNumRects*4, NULL);
*retvalPtr = OBJECT_TO_JSVAL(destArray);
ncc->SetReturnValueWasSet(PR_TRUE);
for(PRUint32 i = 0; i < mRectSet->mNumRects; i++) {
nsRegionRect &rect = mRectSet->mRects[i];
int n = i*4;
// This will contain bogus data if values don't fit in 31 bit
JS_DefineElement(cx, destArray, n, INT_TO_JSVAL(rect.x), NULL, NULL, JSPROP_ENUMERATE);
JS_DefineElement(cx, destArray, n+1, INT_TO_JSVAL(rect.y), NULL, NULL, JSPROP_ENUMERATE);
JS_DefineElement(cx, destArray, n+2, INT_TO_JSVAL(rect.width), NULL, NULL, JSPROP_ENUMERATE);
JS_DefineElement(cx, destArray, n+3, INT_TO_JSVAL(rect.height), NULL, NULL, JSPROP_ENUMERATE);
}
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}

View File

@ -39,6 +39,7 @@
#include "nsIScriptableRegion.h"
#include "gfxCore.h"
#include "nsIRegion.h"
class nsIRegion;
@ -56,4 +57,5 @@ public:
private:
nsIRegion* mRegion;
nsRegionRectSet *mRectSet;
};