1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Mozilla Communicator.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Patrick Beard
25 : * Taras Glek
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsScriptableRegion.h"
42 : #include "nsCOMPtr.h"
43 : #include "nsIXPConnect.h"
44 : #include "nsServiceManagerUtils.h"
45 : #include "jsapi.h"
46 :
47 1 : nsScriptableRegion::nsScriptableRegion()
48 : {
49 1 : }
50 :
51 21 : NS_IMPL_ISUPPORTS1(nsScriptableRegion, nsIScriptableRegion)
52 :
53 0 : NS_IMETHODIMP nsScriptableRegion::Init()
54 : {
55 0 : return NS_OK;
56 : }
57 :
58 0 : NS_IMETHODIMP nsScriptableRegion::SetToRegion(nsIScriptableRegion *aRegion)
59 : {
60 0 : aRegion->GetRegion(&mRegion);
61 0 : return NS_OK;
62 : }
63 :
64 0 : NS_IMETHODIMP nsScriptableRegion::SetToRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
65 : {
66 0 : mRegion = nsIntRect(aX, aY, aWidth, aHeight);
67 0 : return NS_OK;
68 : }
69 :
70 0 : NS_IMETHODIMP nsScriptableRegion::IntersectRegion(nsIScriptableRegion *aRegion)
71 : {
72 0 : nsIntRegion region;
73 0 : aRegion->GetRegion(®ion);
74 0 : mRegion.And(mRegion, region);
75 0 : return NS_OK;
76 : }
77 :
78 0 : NS_IMETHODIMP nsScriptableRegion::IntersectRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
79 : {
80 0 : mRegion.And(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
81 0 : return NS_OK;
82 : }
83 :
84 0 : NS_IMETHODIMP nsScriptableRegion::UnionRegion(nsIScriptableRegion *aRegion)
85 : {
86 0 : nsIntRegion region;
87 0 : aRegion->GetRegion(®ion);
88 0 : mRegion.Or(mRegion, region);
89 0 : return NS_OK;
90 : }
91 :
92 2 : NS_IMETHODIMP nsScriptableRegion::UnionRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
93 : {
94 2 : mRegion.Or(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
95 2 : return NS_OK;
96 : }
97 :
98 0 : NS_IMETHODIMP nsScriptableRegion::SubtractRegion(nsIScriptableRegion *aRegion)
99 : {
100 0 : nsIntRegion region;
101 0 : aRegion->GetRegion(®ion);
102 0 : mRegion.Sub(mRegion, region);
103 0 : return NS_OK;
104 : }
105 :
106 0 : NS_IMETHODIMP nsScriptableRegion::SubtractRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight)
107 : {
108 0 : mRegion.Sub(mRegion, nsIntRect(aX, aY, aWidth, aHeight));
109 0 : return NS_OK;
110 : }
111 :
112 0 : NS_IMETHODIMP nsScriptableRegion::IsEmpty(bool *isEmpty)
113 : {
114 0 : *isEmpty = mRegion.IsEmpty();
115 0 : return NS_OK;
116 : }
117 :
118 0 : NS_IMETHODIMP nsScriptableRegion::IsEqualRegion(nsIScriptableRegion *aRegion, bool *isEqual)
119 : {
120 0 : nsIntRegion region;
121 0 : aRegion->GetRegion(®ion);
122 0 : *isEqual = mRegion.IsEqual(region);
123 0 : return NS_OK;
124 : }
125 :
126 0 : NS_IMETHODIMP nsScriptableRegion::GetBoundingBox(PRInt32 *aX, PRInt32 *aY, PRInt32 *aWidth, PRInt32 *aHeight)
127 : {
128 0 : nsIntRect boundRect = mRegion.GetBounds();
129 0 : *aX = boundRect.x;
130 0 : *aY = boundRect.y;
131 0 : *aWidth = boundRect.width;
132 0 : *aHeight = boundRect.height;
133 0 : return NS_OK;
134 : }
135 :
136 0 : NS_IMETHODIMP nsScriptableRegion::Offset(PRInt32 aXOffset, PRInt32 aYOffset)
137 : {
138 0 : mRegion.MoveBy(aXOffset, aYOffset);
139 0 : return NS_OK;
140 : }
141 :
142 0 : NS_IMETHODIMP nsScriptableRegion::ContainsRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight, bool *containsRect)
143 : {
144 0 : *containsRect = mRegion.Contains(nsIntRect(aX, aY, aWidth, aHeight));
145 0 : return NS_OK;
146 : }
147 :
148 :
149 0 : NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIntRegion* outRgn)
150 : {
151 0 : *outRgn = mRegion;
152 0 : return NS_OK;
153 : }
154 :
155 3 : NS_IMETHODIMP nsScriptableRegion::GetRects(JSContext* aCx, JS::Value* aRects)
156 : {
157 3 : PRUint32 numRects = mRegion.GetNumRects();
158 :
159 3 : if (!numRects) {
160 1 : *aRects = JSVAL_NULL;
161 1 : return NS_OK;
162 : }
163 :
164 2 : JSObject* destArray = JS_NewArrayObject(aCx, numRects * 4, NULL);
165 2 : if (!destArray) {
166 0 : return NS_ERROR_OUT_OF_MEMORY;
167 : }
168 :
169 2 : *aRects = OBJECT_TO_JSVAL(destArray);
170 :
171 2 : uint32 n = 0;
172 2 : nsIntRegionRectIterator iter(mRegion);
173 : const nsIntRect *rect;
174 :
175 7 : while ((rect = iter.Next())) {
176 12 : if (!JS_DefineElement(aCx, destArray, n, INT_TO_JSVAL(rect->x), NULL, NULL, JSPROP_ENUMERATE) ||
177 3 : !JS_DefineElement(aCx, destArray, n + 1, INT_TO_JSVAL(rect->y), NULL, NULL, JSPROP_ENUMERATE) ||
178 3 : !JS_DefineElement(aCx, destArray, n + 2, INT_TO_JSVAL(rect->width), NULL, NULL, JSPROP_ENUMERATE) ||
179 3 : !JS_DefineElement(aCx, destArray, n + 3, INT_TO_JSVAL(rect->height), NULL, NULL, JSPROP_ENUMERATE)) {
180 0 : return NS_ERROR_FAILURE;
181 : }
182 3 : n += 4;
183 : }
184 :
185 2 : return NS_OK;
186 : }
|