content/base/src/DOMRect.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #ifndef MOZILLA_DOMRECT_H_
     7 #define MOZILLA_DOMRECT_H_
     9 #include "nsIDOMClientRect.h"
    10 #include "nsIDOMClientRectList.h"
    11 #include "nsTArray.h"
    12 #include "nsCOMPtr.h"
    13 #include "nsAutoPtr.h"
    14 #include "nsWrapperCache.h"
    15 #include "nsCycleCollectionParticipant.h"
    16 #include "mozilla/Attributes.h"
    17 #include "mozilla/dom/BindingDeclarations.h"
    18 #include "mozilla/ErrorResult.h"
    19 #include <algorithm>
    21 struct nsRect;
    23 namespace mozilla {
    24 namespace dom {
    26 class DOMRectReadOnly : public nsISupports
    27                       , public nsWrapperCache
    28 {
    29 public:
    30   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    31   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
    33   virtual ~DOMRectReadOnly() {}
    35   DOMRectReadOnly(nsISupports* aParent)
    36     : mParent(aParent)
    37   {
    38     SetIsDOMBinding();
    39   }
    41   nsISupports* GetParentObject() const
    42   {
    43     MOZ_ASSERT(mParent);
    44     return mParent;
    45   }
    46   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    48   virtual double X() const = 0;
    49   virtual double Y() const = 0;
    50   virtual double Width() const = 0;
    51   virtual double Height() const = 0;
    53   double Left() const
    54   {
    55     double x = X(), w = Width();
    56     return std::min(x, x + w);
    57   }
    58   double Top() const
    59   {
    60     double y = Y(), h = Height();
    61     return std::min(y, y + h);
    62   }
    63   double Right() const
    64   {
    65     double x = X(), w = Width();
    66     return std::max(x, x + w);
    67   }
    68   double Bottom() const
    69   {
    70     double y = Y(), h = Height();
    71     return std::max(y, y + h);
    72   }
    74 protected:
    75   nsCOMPtr<nsISupports> mParent;
    76 };
    78 class DOMRect MOZ_FINAL : public DOMRectReadOnly
    79                         , public nsIDOMClientRect
    80 {
    81 public:
    82   DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
    83           double aWidth = 0, double aHeight = 0)
    84     : DOMRectReadOnly(aParent)
    85     , mX(aX)
    86     , mY(aY)
    87     , mWidth(aWidth)
    88     , mHeight(aHeight)
    89   {
    90   }
    92   NS_DECL_ISUPPORTS_INHERITED
    93   NS_DECL_NSIDOMCLIENTRECT
    95   static already_AddRefed<DOMRect>
    96   Constructor(const GlobalObject& aGlobal, ErrorResult& aRV);
    97   static already_AddRefed<DOMRect>
    98   Constructor(const GlobalObject& aGlobal, double aX, double aY,
    99               double aWidth, double aHeight, ErrorResult& aRV);
   101   virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   103   void SetRect(float aX, float aY, float aWidth, float aHeight) {
   104     mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight;
   105   }
   106   void SetLayoutRect(const nsRect& aLayoutRect);
   108   virtual double X() const MOZ_OVERRIDE
   109   {
   110     return mX;
   111   }
   112   virtual double Y() const MOZ_OVERRIDE
   113   {
   114     return mY;
   115   }
   116   virtual double Width() const MOZ_OVERRIDE
   117   {
   118     return mWidth;
   119   }
   120   virtual double Height() const MOZ_OVERRIDE
   121   {
   122     return mHeight;
   123   }
   125   void SetX(double aX)
   126   {
   127     mX = aX;
   128   }
   129   void SetY(double aY)
   130   {
   131     mY = aY;
   132   }
   133   void SetWidth(double aWidth)
   134   {
   135     mWidth = aWidth;
   136   }
   137   void SetHeight(double aHeight)
   138   {
   139     mHeight = aHeight;
   140   }
   142 protected:
   143   double mX, mY, mWidth, mHeight;
   144 };
   146 class DOMRectList MOZ_FINAL : public nsIDOMClientRectList,
   147                               public nsWrapperCache
   148 {
   149 public:
   150   DOMRectList(nsISupports *aParent) : mParent(aParent)
   151   {
   152     SetIsDOMBinding();
   153   }
   155   NS_DECL_CYCLE_COLLECTING_ISUPPORTS
   156   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
   158   NS_DECL_NSIDOMCLIENTRECTLIST
   160   virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
   162   nsISupports* GetParentObject()
   163   {
   164     return mParent;
   165   }
   167   void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
   169   static DOMRectList* FromSupports(nsISupports* aSupports)
   170   {
   171 #ifdef DEBUG
   172     {
   173       nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports);
   175       // If this assertion fires the QI implementation for the object in
   176       // question doesn't use the nsIDOMClientRectList pointer as the nsISupports
   177       // pointer. That must be fixed, or we'll crash...
   178       NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports),
   179                    "Uh, fix QI!");
   180     }
   181 #endif
   183     return static_cast<DOMRectList*>(aSupports);
   184   }
   186   uint32_t Length()
   187   {
   188     return mArray.Length();
   189   }
   190   DOMRect* Item(uint32_t aIndex)
   191   {
   192     return mArray.SafeElementAt(aIndex);
   193   }
   194   DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound)
   195   {
   196     aFound = aIndex < mArray.Length();
   197     if (!aFound) {
   198       return nullptr;
   199     }
   200     return mArray[aIndex];
   201   }
   203 protected:
   204   nsTArray<nsRefPtr<DOMRect> > mArray;
   205   nsCOMPtr<nsISupports> mParent;
   206 };
   208 }
   209 }
   211 #endif /*MOZILLA_DOMRECT_H_*/

mercurial