2013-03-17 00:55:17 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-03-11 17:51:12 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
#ifndef MOZILLA_DOMRECT_H_
|
|
|
|
#define MOZILLA_DOMRECT_H_
|
2008-03-11 17:51:12 -07:00
|
|
|
|
|
|
|
#include "nsIDOMClientRect.h"
|
|
|
|
#include "nsIDOMClientRectList.h"
|
2013-03-17 00:55:17 -07:00
|
|
|
#include "nsTArray.h"
|
2008-10-22 07:31:14 -07:00
|
|
|
#include "nsCOMPtr.h"
|
2013-03-17 00:55:17 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2011-08-22 02:14:13 -07:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-03-17 00:55:17 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-09-20 03:21:03 -07:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include <algorithm>
|
2008-09-18 02:47:21 -07:00
|
|
|
|
2013-09-06 19:15:49 -07:00
|
|
|
struct nsRect;
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
class DOMRectReadOnly : public nsISupports
|
|
|
|
, public nsWrapperCache
|
2008-03-11 17:51:12 -07:00
|
|
|
{
|
2014-06-24 19:09:15 -07:00
|
|
|
protected:
|
|
|
|
virtual ~DOMRectReadOnly() {}
|
|
|
|
|
2008-03-11 17:51:12 -07:00
|
|
|
public:
|
2013-09-20 03:21:03 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
|
|
|
|
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DOMRectReadOnly(nsISupports* aParent)
|
2013-09-20 03:21:03 -07:00
|
|
|
: mParent(aParent)
|
2013-03-17 00:55:17 -07:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
|
2014-03-20 13:34:25 -07:00
|
|
|
nsISupports* GetParentObject() const
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2014-03-20 13:34:25 -07:00
|
|
|
MOZ_ASSERT(mParent);
|
|
|
|
return mParent;
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-09-20 03:21:03 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
virtual double X() const = 0;
|
|
|
|
virtual double Y() const = 0;
|
|
|
|
virtual double Width() const = 0;
|
|
|
|
virtual double Height() const = 0;
|
2014-03-19 01:46:09 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
double Left() const
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2013-09-20 03:21:03 -07:00
|
|
|
double x = X(), w = Width();
|
|
|
|
return std::min(x, x + w);
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
double Top() const
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2013-09-20 03:21:03 -07:00
|
|
|
double y = Y(), h = Height();
|
|
|
|
return std::min(y, y + h);
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
double Right() const
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2013-09-20 03:21:03 -07:00
|
|
|
double x = X(), w = Width();
|
|
|
|
return std::max(x, x + w);
|
|
|
|
}
|
|
|
|
double Bottom() const
|
|
|
|
{
|
|
|
|
double y = Y(), h = Height();
|
|
|
|
return std::max(y, y + h);
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2014-03-20 13:34:25 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
protected:
|
|
|
|
nsCOMPtr<nsISupports> mParent;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DOMRect MOZ_FINAL : public DOMRectReadOnly
|
|
|
|
, public nsIDOMClientRect
|
|
|
|
{
|
|
|
|
public:
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
|
|
|
|
double aWidth = 0, double aHeight = 0)
|
2013-09-20 03:21:03 -07:00
|
|
|
: DOMRectReadOnly(aParent)
|
|
|
|
, mX(aX)
|
|
|
|
, mY(aY)
|
|
|
|
, mWidth(aWidth)
|
|
|
|
, mHeight(aHeight)
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_NSIDOMCLIENTRECT
|
|
|
|
|
|
|
|
static already_AddRefed<DOMRect>
|
|
|
|
Constructor(const GlobalObject& aGlobal, ErrorResult& aRV);
|
|
|
|
static already_AddRefed<DOMRect>
|
|
|
|
Constructor(const GlobalObject& aGlobal, double aX, double aY,
|
|
|
|
double aWidth, double aHeight, ErrorResult& aRV);
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
|
2013-09-20 03:21:03 -07:00
|
|
|
|
|
|
|
void SetRect(float aX, float aY, float aWidth, float aHeight) {
|
|
|
|
mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
|
|
|
|
}
|
|
|
|
void SetLayoutRect(const nsRect& aLayoutRect);
|
2014-03-20 13:34:25 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
virtual double X() const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mX;
|
|
|
|
}
|
|
|
|
virtual double Y() const MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
return mY;
|
|
|
|
}
|
|
|
|
virtual double Width() const MOZ_OVERRIDE
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2014-03-20 13:34:25 -07:00
|
|
|
return mWidth;
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
virtual double Height() const MOZ_OVERRIDE
|
2013-09-20 03:21:03 -07:00
|
|
|
{
|
2014-03-20 13:34:25 -07:00
|
|
|
return mHeight;
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
void SetX(double aX)
|
|
|
|
{
|
|
|
|
mX = aX;
|
|
|
|
}
|
|
|
|
void SetY(double aY)
|
|
|
|
{
|
|
|
|
mY = aY;
|
|
|
|
}
|
|
|
|
void SetWidth(double aWidth)
|
|
|
|
{
|
|
|
|
mWidth = aWidth;
|
|
|
|
}
|
|
|
|
void SetHeight(double aHeight)
|
|
|
|
{
|
|
|
|
mHeight = aHeight;
|
|
|
|
}
|
|
|
|
|
2008-03-11 17:51:12 -07:00
|
|
|
protected:
|
2013-09-20 03:21:03 -07:00
|
|
|
double mX, mY, mWidth, mHeight;
|
2008-03-11 17:51:12 -07:00
|
|
|
};
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
class DOMRectList MOZ_FINAL : public nsIDOMClientRectList,
|
|
|
|
public nsWrapperCache
|
2008-03-11 17:51:12 -07:00
|
|
|
{
|
2014-06-24 19:09:15 -07:00
|
|
|
~DOMRectList() {}
|
|
|
|
|
2008-03-11 17:51:12 -07:00
|
|
|
public:
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DOMRectList(nsISupports *aParent) : mParent(aParent)
|
2011-08-22 02:14:13 -07:00
|
|
|
{
|
2012-03-16 08:44:09 -07:00
|
|
|
SetIsDOMBinding();
|
2011-08-22 02:14:13 -07:00
|
|
|
}
|
2008-03-11 17:51:12 -07:00
|
|
|
|
2011-08-22 02:14:13 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2013-09-20 03:21:03 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
|
2008-03-11 17:51:12 -07:00
|
|
|
|
|
|
|
NS_DECL_NSIDOMCLIENTRECTLIST
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
|
2008-03-11 17:51:12 -07:00
|
|
|
|
2011-08-22 02:14:13 -07:00
|
|
|
nsISupports* GetParentObject()
|
2008-10-22 07:31:14 -07:00
|
|
|
{
|
2011-08-22 02:14:13 -07:00
|
|
|
return mParent;
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
|
2011-08-22 02:14:13 -07:00
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
static DOMRectList* FromSupports(nsISupports* aSupports)
|
2008-10-22 07:31:14 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
|
|
|
|
|
|
|
|
// If this assertion fires the QI implementation for the object in
|
|
|
|
// question doesn't use the nsIDOMClientRectList pointer as the nsISupports
|
|
|
|
// pointer. That must be fixed, or we'll crash...
|
|
|
|
NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
|
|
|
|
"Uh, fix QI!");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
return static_cast<DOMRectList*>(aSupports);
|
2008-10-22 07:31:14 -07:00
|
|
|
}
|
|
|
|
|
2012-09-05 13:49:53 -07:00
|
|
|
uint32_t Length()
|
|
|
|
{
|
2013-03-17 00:55:17 -07:00
|
|
|
return mArray.Length();
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
DOMRect* Item(uint32_t aIndex)
|
2012-09-05 13:49:53 -07:00
|
|
|
{
|
2013-03-17 00:55:17 -07:00
|
|
|
return mArray.SafeElementAt(aIndex);
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
2013-09-20 03:21:03 -07:00
|
|
|
DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
|
2012-09-05 13:49:53 -07:00
|
|
|
{
|
2013-03-17 00:55:17 -07:00
|
|
|
aFound = aIndex < mArray.Length();
|
|
|
|
if (!aFound) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mArray[aIndex];
|
2012-09-05 13:49:53 -07:00
|
|
|
}
|
|
|
|
|
2008-03-11 17:51:12 -07:00
|
|
|
protected:
|
2013-09-20 03:21:03 -07:00
|
|
|
nsTArray<nsRefPtr<DOMRect> > mArray;
|
2011-08-22 02:14:13 -07:00
|
|
|
nsCOMPtr<nsISupports> mParent;
|
2008-03-11 17:51:12 -07:00
|
|
|
};
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
2014-07-08 14:23:16 -07:00
|
|
|
|
|
|
|
template<>
|
|
|
|
struct HasDangerousPublicDestructor<dom::DOMRect>
|
|
|
|
{
|
|
|
|
static const bool value = true;
|
|
|
|
};
|
|
|
|
|
2013-09-20 03:21:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /*MOZILLA_DOMRECT_H_*/
|