michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/dom/DOMQuad.h" michael@0: michael@0: #include "mozilla/dom/DOMQuadBinding.h" michael@0: #include "mozilla/dom/DOMPoint.h" michael@0: #include "mozilla/dom/DOMRect.h" michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: using namespace mozilla::gfx; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_6(DOMQuad, mParent, mBounds, mPoints[0], michael@0: mPoints[1], mPoints[2], mPoints[3]) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad, AddRef) michael@0: NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad, Release) michael@0: michael@0: DOMQuad::DOMQuad(nsISupports* aParent, CSSPoint aPoints[4]) michael@0: : mParent(aParent) michael@0: { michael@0: SetIsDOMBinding(); michael@0: for (uint32_t i = 0; i < 4; ++i) { michael@0: mPoints[i] = new DOMPoint(aParent, aPoints[i].x, aPoints[i].y); michael@0: } michael@0: } michael@0: michael@0: DOMQuad::DOMQuad(nsISupports* aParent) michael@0: : mParent(aParent) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: DOMQuad::~DOMQuad() michael@0: { michael@0: } michael@0: michael@0: JSObject* michael@0: DOMQuad::WrapObject(JSContext* aCx) michael@0: { michael@0: return DOMQuadBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DOMQuad::Constructor(const GlobalObject& aGlobal, michael@0: const DOMPointInit& aP1, michael@0: const DOMPointInit& aP2, michael@0: const DOMPointInit& aP3, michael@0: const DOMPointInit& aP4, michael@0: ErrorResult& aRV) michael@0: { michael@0: nsRefPtr obj = new DOMQuad(aGlobal.GetAsSupports()); michael@0: obj->mPoints[0] = DOMPoint::Constructor(aGlobal, aP1, aRV); michael@0: obj->mPoints[1] = DOMPoint::Constructor(aGlobal, aP2, aRV); michael@0: obj->mPoints[2] = DOMPoint::Constructor(aGlobal, aP3, aRV); michael@0: obj->mPoints[3] = DOMPoint::Constructor(aGlobal, aP4, aRV); michael@0: return obj.forget(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: DOMQuad::Constructor(const GlobalObject& aGlobal, const DOMRectReadOnly& aRect, michael@0: ErrorResult& aRV) michael@0: { michael@0: CSSPoint points[4]; michael@0: Float x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height(); michael@0: points[0] = CSSPoint(x, y); michael@0: points[1] = CSSPoint(x + w, y); michael@0: points[2] = CSSPoint(x + w, y + h); michael@0: points[3] = CSSPoint(x, y + h); michael@0: nsRefPtr obj = new DOMQuad(aGlobal.GetAsSupports(), points); michael@0: return obj.forget(); michael@0: } michael@0: michael@0: class DOMQuad::QuadBounds MOZ_FINAL : public DOMRectReadOnly michael@0: { michael@0: public: michael@0: QuadBounds(DOMQuad* aQuad) michael@0: : DOMRectReadOnly(aQuad->GetParentObject()) michael@0: , mQuad(aQuad) michael@0: {} michael@0: michael@0: NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds, DOMRectReadOnly) michael@0: NS_DECL_ISUPPORTS_INHERITED michael@0: michael@0: virtual double X() const michael@0: { michael@0: double x1, x2; michael@0: GetHorizontalMinMax(&x1, &x2); michael@0: return x1; michael@0: } michael@0: virtual double Y() const michael@0: { michael@0: double y1, y2; michael@0: GetVerticalMinMax(&y1, &y2); michael@0: return y1; michael@0: } michael@0: virtual double Width() const michael@0: { michael@0: double x1, x2; michael@0: GetHorizontalMinMax(&x1, &x2); michael@0: return x2 - x1; michael@0: } michael@0: virtual double Height() const michael@0: { michael@0: double y1, y2; michael@0: GetVerticalMinMax(&y1, &y2); michael@0: return y2 - y1; michael@0: } michael@0: michael@0: void GetHorizontalMinMax(double* aX1, double* aX2) const michael@0: { michael@0: double x1, x2; michael@0: x1 = x2 = mQuad->Point(0)->X(); michael@0: for (uint32_t i = 1; i < 4; ++i) { michael@0: double x = mQuad->Point(i)->X(); michael@0: x1 = std::min(x1, x); michael@0: x2 = std::max(x2, x); michael@0: } michael@0: *aX1 = x1; michael@0: *aX2 = x2; michael@0: } michael@0: michael@0: void GetVerticalMinMax(double* aY1, double* aY2) const michael@0: { michael@0: double y1, y2; michael@0: y1 = y2 = mQuad->Point(0)->Y(); michael@0: for (uint32_t i = 1; i < 4; ++i) { michael@0: double y = mQuad->Point(i)->Y(); michael@0: y1 = std::min(y1, y); michael@0: y2 = std::max(y2, y); michael@0: } michael@0: *aY1 = y1; michael@0: *aY2 = y2; michael@0: } michael@0: michael@0: protected: michael@0: nsRefPtr mQuad; michael@0: }; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly, mQuad) michael@0: michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds) michael@0: NS_INTERFACE_MAP_END_INHERITING(DOMRectReadOnly) michael@0: michael@0: NS_IMPL_ADDREF_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) michael@0: NS_IMPL_RELEASE_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) michael@0: michael@0: DOMRectReadOnly* michael@0: DOMQuad::Bounds() const michael@0: { michael@0: if (!mBounds) { michael@0: mBounds = new QuadBounds(const_cast(this)); michael@0: } michael@0: return mBounds; michael@0: }