content/base/src/DOMRect.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/DOMRect.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,211 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef MOZILLA_DOMRECT_H_
    1.10 +#define MOZILLA_DOMRECT_H_
    1.11 +
    1.12 +#include "nsIDOMClientRect.h"
    1.13 +#include "nsIDOMClientRectList.h"
    1.14 +#include "nsTArray.h"
    1.15 +#include "nsCOMPtr.h"
    1.16 +#include "nsAutoPtr.h"
    1.17 +#include "nsWrapperCache.h"
    1.18 +#include "nsCycleCollectionParticipant.h"
    1.19 +#include "mozilla/Attributes.h"
    1.20 +#include "mozilla/dom/BindingDeclarations.h"
    1.21 +#include "mozilla/ErrorResult.h"
    1.22 +#include <algorithm>
    1.23 +
    1.24 +struct nsRect;
    1.25 +
    1.26 +namespace mozilla {
    1.27 +namespace dom {
    1.28 +
    1.29 +class DOMRectReadOnly : public nsISupports
    1.30 +                      , public nsWrapperCache
    1.31 +{
    1.32 +public:
    1.33 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.34 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
    1.35 +
    1.36 +  virtual ~DOMRectReadOnly() {}
    1.37 +
    1.38 +  DOMRectReadOnly(nsISupports* aParent)
    1.39 +    : mParent(aParent)
    1.40 +  {
    1.41 +    SetIsDOMBinding();
    1.42 +  }
    1.43 +
    1.44 +  nsISupports* GetParentObject() const
    1.45 +  {
    1.46 +    MOZ_ASSERT(mParent);
    1.47 +    return mParent;
    1.48 +  }
    1.49 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    1.50 +
    1.51 +  virtual double X() const = 0;
    1.52 +  virtual double Y() const = 0;
    1.53 +  virtual double Width() const = 0;
    1.54 +  virtual double Height() const = 0;
    1.55 +
    1.56 +  double Left() const
    1.57 +  {
    1.58 +    double x = X(), w = Width();
    1.59 +    return std::min(x, x + w);
    1.60 +  }
    1.61 +  double Top() const
    1.62 +  {
    1.63 +    double y = Y(), h = Height();
    1.64 +    return std::min(y, y + h);
    1.65 +  }
    1.66 +  double Right() const
    1.67 +  {
    1.68 +    double x = X(), w = Width();
    1.69 +    return std::max(x, x + w);
    1.70 +  }
    1.71 +  double Bottom() const
    1.72 +  {
    1.73 +    double y = Y(), h = Height();
    1.74 +    return std::max(y, y + h);
    1.75 +  }
    1.76 +
    1.77 +protected:
    1.78 +  nsCOMPtr<nsISupports> mParent;
    1.79 +};
    1.80 +
    1.81 +class DOMRect MOZ_FINAL : public DOMRectReadOnly
    1.82 +                        , public nsIDOMClientRect
    1.83 +{
    1.84 +public:
    1.85 +  DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
    1.86 +          double aWidth = 0, double aHeight = 0)
    1.87 +    : DOMRectReadOnly(aParent)
    1.88 +    , mX(aX)
    1.89 +    , mY(aY)
    1.90 +    , mWidth(aWidth)
    1.91 +    , mHeight(aHeight)
    1.92 +  {
    1.93 +  }
    1.94 +  
    1.95 +  NS_DECL_ISUPPORTS_INHERITED
    1.96 +  NS_DECL_NSIDOMCLIENTRECT
    1.97 +
    1.98 +  static already_AddRefed<DOMRect>
    1.99 +  Constructor(const GlobalObject& aGlobal, ErrorResult& aRV);
   1.100 +  static already_AddRefed<DOMRect>
   1.101 +  Constructor(const GlobalObject& aGlobal, double aX, double aY,
   1.102 +              double aWidth, double aHeight, ErrorResult& aRV);
   1.103 +
   1.104 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   1.105 +
   1.106 +  void SetRect(float aX, float aY, float aWidth, float aHeight) {
   1.107 +    mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
   1.108 +  }
   1.109 +  void SetLayoutRect(const nsRect& aLayoutRect);
   1.110 +
   1.111 +  virtual double X() const MOZ_OVERRIDE
   1.112 +  {
   1.113 +    return mX;
   1.114 +  }
   1.115 +  virtual double Y() const MOZ_OVERRIDE
   1.116 +  {
   1.117 +    return mY;
   1.118 +  }
   1.119 +  virtual double Width() const MOZ_OVERRIDE
   1.120 +  {
   1.121 +    return mWidth;
   1.122 +  }
   1.123 +  virtual double Height() const MOZ_OVERRIDE
   1.124 +  {
   1.125 +    return mHeight;
   1.126 +  }
   1.127 +
   1.128 +  void SetX(double aX)
   1.129 +  {
   1.130 +    mX = aX;
   1.131 +  }
   1.132 +  void SetY(double aY)
   1.133 +  {
   1.134 +    mY = aY;
   1.135 +  }
   1.136 +  void SetWidth(double aWidth)
   1.137 +  {
   1.138 +    mWidth = aWidth;
   1.139 +  }
   1.140 +  void SetHeight(double aHeight)
   1.141 +  {
   1.142 +    mHeight = aHeight;
   1.143 +  }
   1.144 +
   1.145 +protected:
   1.146 +  double mX, mY, mWidth, mHeight;
   1.147 +};
   1.148 +
   1.149 +class DOMRectList MOZ_FINAL : public nsIDOMClientRectList,
   1.150 +                              public nsWrapperCache
   1.151 +{
   1.152 +public:
   1.153 +  DOMRectList(nsISupports *aParent) : mParent(aParent)
   1.154 +  {
   1.155 +    SetIsDOMBinding();
   1.156 +  }
   1.157 +
   1.158 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   1.159 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
   1.160 +
   1.161 +  NS_DECL_NSIDOMCLIENTRECTLIST
   1.162 +  
   1.163 +  virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
   1.164 +
   1.165 +  nsISupports* GetParentObject()
   1.166 +  {
   1.167 +    return mParent;
   1.168 +  }
   1.169 +
   1.170 +  void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
   1.171 +
   1.172 +  static DOMRectList* FromSupports(nsISupports* aSupports)
   1.173 +  {
   1.174 +#ifdef DEBUG
   1.175 +    {
   1.176 +      nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
   1.177 +
   1.178 +      // If this assertion fires the QI implementation for the object in
   1.179 +      // question doesn't use the nsIDOMClientRectList pointer as the nsISupports
   1.180 +      // pointer. That must be fixed, or we'll crash...
   1.181 +      NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
   1.182 +                   "Uh, fix QI!");
   1.183 +    }
   1.184 +#endif
   1.185 +
   1.186 +    return static_cast<DOMRectList*>(aSupports);
   1.187 +  }
   1.188 +
   1.189 +  uint32_t Length()
   1.190 +  {
   1.191 +    return mArray.Length();
   1.192 +  }
   1.193 +  DOMRect* Item(uint32_t aIndex)
   1.194 +  {
   1.195 +    return mArray.SafeElementAt(aIndex);
   1.196 +  }
   1.197 +  DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
   1.198 +  {
   1.199 +    aFound = aIndex < mArray.Length();
   1.200 +    if (!aFound) {
   1.201 +      return nullptr;
   1.202 +    }
   1.203 +    return mArray[aIndex];
   1.204 +  }
   1.205 +
   1.206 +protected:
   1.207 +  nsTArray<nsRefPtr<DOMRect> > mArray;
   1.208 +  nsCOMPtr<nsISupports> mParent;
   1.209 +};
   1.210 +
   1.211 +}
   1.212 +}
   1.213 +
   1.214 +#endif /*MOZILLA_DOMRECT_H_*/

mercurial