1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corp.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "nsScriptableRegion.h"
43 #include "nsIXPConnect.h"
44 #include "nsServiceManagerUtils.h"
47 nsScriptableRegion::nsScriptableRegion(nsIRegion
* region
) : mRegion(nsnull
), mRectSet(nsnull
)
50 NS_IF_ADDREF(mRegion
);
53 nsScriptableRegion::~nsScriptableRegion()
56 mRegion
->FreeRects(mRectSet
);
61 NS_IMPL_ISUPPORTS1(nsScriptableRegion
, nsIScriptableRegion
)
63 NS_IMETHODIMP
nsScriptableRegion::Init()
65 return mRegion
->Init();
68 NS_IMETHODIMP
nsScriptableRegion::SetToRegion(nsIScriptableRegion
*aRegion
)
70 nsCOMPtr
<nsIRegion
> region(do_QueryInterface(aRegion
));
71 mRegion
->SetTo(*region
);
75 NS_IMETHODIMP
nsScriptableRegion::SetToRect(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
)
77 mRegion
->SetTo(aX
, aY
, aWidth
, aHeight
);
81 NS_IMETHODIMP
nsScriptableRegion::IntersectRegion(nsIScriptableRegion
*aRegion
)
83 nsCOMPtr
<nsIRegion
> region(do_QueryInterface(aRegion
));
84 mRegion
->Intersect(*region
);
88 NS_IMETHODIMP
nsScriptableRegion::IntersectRect(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
)
90 mRegion
->Intersect(aX
, aY
, aWidth
, aHeight
);
94 NS_IMETHODIMP
nsScriptableRegion::UnionRegion(nsIScriptableRegion
*aRegion
)
96 nsCOMPtr
<nsIRegion
> region(do_QueryInterface(aRegion
));
97 mRegion
->Union(*region
);
101 NS_IMETHODIMP
nsScriptableRegion::UnionRect(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
)
103 mRegion
->Union(aX
, aY
, aWidth
, aHeight
);
107 NS_IMETHODIMP
nsScriptableRegion::SubtractRegion(nsIScriptableRegion
*aRegion
)
109 nsCOMPtr
<nsIRegion
> region(do_QueryInterface(aRegion
));
110 mRegion
->Subtract(*region
);
114 NS_IMETHODIMP
nsScriptableRegion::SubtractRect(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
)
116 mRegion
->Subtract(aX
, aY
, aWidth
, aHeight
);
120 NS_IMETHODIMP
nsScriptableRegion::IsEmpty(PRBool
*isEmpty
)
122 *isEmpty
= mRegion
->IsEmpty();
126 NS_IMETHODIMP
nsScriptableRegion::IsEqualRegion(nsIScriptableRegion
*aRegion
, PRBool
*isEqual
)
128 nsCOMPtr
<nsIRegion
> region(do_QueryInterface(aRegion
));
129 *isEqual
= mRegion
->IsEqual(*region
);
133 NS_IMETHODIMP
nsScriptableRegion::GetBoundingBox(PRInt32
*aX
, PRInt32
*aY
, PRInt32
*aWidth
, PRInt32
*aHeight
)
135 mRegion
->GetBoundingBox(aX
, aY
, aWidth
, aHeight
);
139 NS_IMETHODIMP
nsScriptableRegion::Offset(PRInt32 aXOffset
, PRInt32 aYOffset
)
141 mRegion
->Offset(aXOffset
, aYOffset
);
145 NS_IMETHODIMP
nsScriptableRegion::ContainsRect(PRInt32 aX
, PRInt32 aY
, PRInt32 aWidth
, PRInt32 aHeight
, PRBool
*containsRect
)
147 *containsRect
= mRegion
->ContainsRect(aX
, aY
, aWidth
, aHeight
);
152 NS_IMETHODIMP
nsScriptableRegion::GetRegion(nsIRegion
** outRgn
)
155 NS_IF_ADDREF(*outRgn
);
159 NS_IMETHODIMP
nsScriptableRegion::GetRects() {
160 nsAXPCNativeCallContext
*ncc
= nsnull
;
162 nsCOMPtr
<nsIXPConnect
> xpConnect
= do_GetService(nsIXPConnect::GetCID(), &rv
);
163 NS_ENSURE_SUCCESS(rv
, rv
);
165 rv
= xpConnect
->GetCurrentNativeCallContext(&ncc
);
166 NS_ENSURE_SUCCESS(rv
, rv
);
169 return NS_ERROR_FAILURE
;
172 ncc
->GetRetValPtr(&retvalPtr
);
174 rv
= mRegion
->GetRects(&mRectSet
);
175 NS_ENSURE_SUCCESS(rv
, rv
);
177 if (!mRectSet
->mNumRects
) {
178 *retvalPtr
= JSVAL_NULL
;
179 ncc
->SetReturnValueWasSet(PR_TRUE
);
183 JSContext
*cx
= nsnull
;
185 rv
= ncc
->GetJSContext(&cx
);
186 NS_ENSURE_SUCCESS(rv
, rv
);
188 JSObject
*destArray
= JS_NewArrayObject(cx
, mRectSet
->mNumRects
*4, NULL
);
189 *retvalPtr
= OBJECT_TO_JSVAL(destArray
);
190 ncc
->SetReturnValueWasSet(PR_TRUE
);
192 for(PRUint32 i
= 0; i
< mRectSet
->mNumRects
; i
++) {
193 nsRegionRect
&rect
= mRectSet
->mRects
[i
];
195 // This will contain bogus data if values don't fit in 31 bit
196 JS_DefineElement(cx
, destArray
, n
, INT_TO_JSVAL(rect
.x
), NULL
, NULL
, JSPROP_ENUMERATE
);
197 JS_DefineElement(cx
, destArray
, n
+1, INT_TO_JSVAL(rect
.y
), NULL
, NULL
, JSPROP_ENUMERATE
);
198 JS_DefineElement(cx
, destArray
, n
+2, INT_TO_JSVAL(rect
.width
), NULL
, NULL
, JSPROP_ENUMERATE
);
199 JS_DefineElement(cx
, destArray
, n
+3, INT_TO_JSVAL(rect
.height
), NULL
, NULL
, JSPROP_ENUMERATE
);
202 NS_ENSURE_SUCCESS(rv
, rv
);