Bug 1550519 - Show a translucent parent highlight when a subgrid is highlighted....
[gecko.git] / dom / base / DOMQuad.cpp
blob1613710a5e5978c45028b4fc362b21cdb16d2f3c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/DOMQuad.h"
9 #include "mozilla/dom/DOMQuadBinding.h"
10 #include "mozilla/dom/DOMPoint.h"
11 #include "mozilla/dom/DOMRect.h"
12 #include <algorithm>
14 using namespace mozilla;
15 using namespace mozilla::dom;
16 using namespace mozilla::gfx;
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad, mParent, mBounds, mPoints[0],
19 mPoints[1], mPoints[2], mPoints[3])
21 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad, AddRef)
22 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad, Release)
24 DOMQuad::DOMQuad(nsISupports* aParent, CSSPoint aPoints[4]) : mParent(aParent) {
25 for (uint32_t i = 0; i < 4; ++i) {
26 mPoints[i] = new DOMPoint(aParent, aPoints[i].x, aPoints[i].y);
30 DOMQuad::DOMQuad(nsISupports* aParent) : mParent(aParent) {}
32 DOMQuad::~DOMQuad() {}
34 JSObject* DOMQuad::WrapObject(JSContext* aCx,
35 JS::Handle<JSObject*> aGivenProto) {
36 return DOMQuad_Binding::Wrap(aCx, this, aGivenProto);
39 already_AddRefed<DOMQuad> DOMQuad::Constructor(const GlobalObject& aGlobal,
40 const DOMPointInit& aP1,
41 const DOMPointInit& aP2,
42 const DOMPointInit& aP3,
43 const DOMPointInit& aP4,
44 ErrorResult& aRV) {
45 RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports());
46 obj->mPoints[0] = DOMPoint::FromPoint(aGlobal, aP1);
47 obj->mPoints[1] = DOMPoint::FromPoint(aGlobal, aP2);
48 obj->mPoints[2] = DOMPoint::FromPoint(aGlobal, aP3);
49 obj->mPoints[3] = DOMPoint::FromPoint(aGlobal, aP4);
50 return obj.forget();
53 already_AddRefed<DOMQuad> DOMQuad::Constructor(const GlobalObject& aGlobal,
54 const DOMRectReadOnly& aRect,
55 ErrorResult& aRV) {
56 CSSPoint points[4];
57 Float x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height();
58 points[0] = CSSPoint(x, y);
59 points[1] = CSSPoint(x + w, y);
60 points[2] = CSSPoint(x + w, y + h);
61 points[3] = CSSPoint(x, y + h);
62 RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports(), points);
63 return obj.forget();
66 void DOMQuad::GetHorizontalMinMax(double* aX1, double* aX2) const {
67 double x1, x2;
68 x1 = x2 = Point(0)->X();
69 for (uint32_t i = 1; i < 4; ++i) {
70 double x = Point(i)->X();
71 x1 = std::min(x1, x);
72 x2 = std::max(x2, x);
74 *aX1 = x1;
75 *aX2 = x2;
78 void DOMQuad::GetVerticalMinMax(double* aY1, double* aY2) const {
79 double y1, y2;
80 y1 = y2 = Point(0)->Y();
81 for (uint32_t i = 1; i < 4; ++i) {
82 double y = Point(i)->Y();
83 y1 = std::min(y1, y);
84 y2 = std::max(y2, y);
86 *aY1 = y1;
87 *aY2 = y2;
90 DOMRectReadOnly* DOMQuad::Bounds() {
91 if (!mBounds) {
92 mBounds = GetBounds();
94 return mBounds;
97 already_AddRefed<DOMRectReadOnly> DOMQuad::GetBounds() const {
98 double x1, x2;
99 double y1, y2;
101 GetHorizontalMinMax(&x1, &x2);
102 GetVerticalMinMax(&y1, &y2);
104 RefPtr<DOMRectReadOnly> rval =
105 new DOMRectReadOnly(GetParentObject(), x1, y1, x2 - x1, y2 - y1);
106 return rval.forget();
109 void DOMQuad::ToJSON(DOMQuadJSON& aInit) {
110 aInit.mP1.Construct(RefPtr<DOMPoint>(P1()).forget());
111 aInit.mP2.Construct(RefPtr<DOMPoint>(P2()).forget());
112 aInit.mP3.Construct(RefPtr<DOMPoint>(P3()).forget());
113 aInit.mP4.Construct(RefPtr<DOMPoint>(P4()).forget());