Bug 574778 - Fix win widget's ConstrainPosition so that it supports full screen windo...
[mozilla-central.git] / gfx / src / nsScriptableRegion.cpp
blobcebc19d9954c464275240ba96893fd99c1fe89b1
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
14 * License.
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.
23 * Contributor(s):
24 * Patrick Beard
25 * Taras Glek
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"
42 #include "nsCOMPtr.h"
43 #include "nsIXPConnect.h"
44 #include "nsServiceManagerUtils.h"
45 #include "jsapi.h"
47 nsScriptableRegion::nsScriptableRegion(nsIRegion* region) : mRegion(nsnull), mRectSet(nsnull)
49 mRegion = region;
50 NS_IF_ADDREF(mRegion);
53 nsScriptableRegion::~nsScriptableRegion()
55 if (mRegion) {
56 mRegion->FreeRects(mRectSet);
57 NS_RELEASE(mRegion);
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);
72 return NS_OK;
75 NS_IMETHODIMP nsScriptableRegion::SetToRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
77 mRegion->SetTo(aX, aY, aWidth, aHeight);
78 return NS_OK;
81 NS_IMETHODIMP nsScriptableRegion::IntersectRegion(nsIScriptableRegion *aRegion)
83 nsCOMPtr<nsIRegion> region(do_QueryInterface(aRegion));
84 mRegion->Intersect(*region);
85 return NS_OK;
88 NS_IMETHODIMP nsScriptableRegion::IntersectRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
90 mRegion->Intersect(aX, aY, aWidth, aHeight);
91 return NS_OK;
94 NS_IMETHODIMP nsScriptableRegion::UnionRegion(nsIScriptableRegion *aRegion)
96 nsCOMPtr<nsIRegion> region(do_QueryInterface(aRegion));
97 mRegion->Union(*region);
98 return NS_OK;
101 NS_IMETHODIMP nsScriptableRegion::UnionRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
103 mRegion->Union(aX, aY, aWidth, aHeight);
104 return NS_OK;
107 NS_IMETHODIMP nsScriptableRegion::SubtractRegion(nsIScriptableRegion *aRegion)
109 nsCOMPtr<nsIRegion> region(do_QueryInterface(aRegion));
110 mRegion->Subtract(*region);
111 return NS_OK;
114 NS_IMETHODIMP nsScriptableRegion::SubtractRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
116 mRegion->Subtract(aX, aY, aWidth, aHeight);
117 return NS_OK;
120 NS_IMETHODIMP nsScriptableRegion::IsEmpty(PRBool *isEmpty)
122 *isEmpty = mRegion->IsEmpty();
123 return NS_OK;
126 NS_IMETHODIMP nsScriptableRegion::IsEqualRegion(nsIScriptableRegion *aRegion, PRBool *isEqual)
128 nsCOMPtr<nsIRegion> region(do_QueryInterface(aRegion));
129 *isEqual = mRegion->IsEqual(*region);
130 return NS_OK;
133 NS_IMETHODIMP nsScriptableRegion::GetBoundingBox(PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight)
135 mRegion->GetBoundingBox(aX, aY, aWidth, aHeight);
136 return NS_OK;
139 NS_IMETHODIMP nsScriptableRegion::Offset(PRInt32 aXOffset, PRInt32 aYOffset)
141 mRegion->Offset(aXOffset, aYOffset);
142 return NS_OK;
145 NS_IMETHODIMP nsScriptableRegion::ContainsRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight, PRBool *containsRect)
147 *containsRect = mRegion->ContainsRect(aX, aY, aWidth, aHeight);
148 return NS_OK;
152 NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIRegion** outRgn)
154 *outRgn = mRegion;
155 NS_IF_ADDREF(*outRgn);
156 return NS_OK;
159 NS_IMETHODIMP nsScriptableRegion::GetRects() {
160 nsAXPCNativeCallContext *ncc = nsnull;
161 nsresult rv;
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);
168 if (!ncc)
169 return NS_ERROR_FAILURE;
171 jsval *retvalPtr;
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);
180 return NS_OK;
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];
194 int n = i*4;
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);
203 return NS_OK;