content/base/src/DOMRect.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/DOMRect.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,125 @@
     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 +#include "mozilla/dom/DOMRect.h"
    1.10 +
    1.11 +#include "nsPresContext.h"
    1.12 +#include "mozilla/dom/DOMRectListBinding.h"
    1.13 +#include "mozilla/dom/DOMRectBinding.h"
    1.14 +
    1.15 +using namespace mozilla;
    1.16 +using namespace mozilla::dom;
    1.17 +
    1.18 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMRectReadOnly, mParent)
    1.19 +NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly)
    1.20 +NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly)
    1.21 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly)
    1.22 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.23 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.24 +NS_INTERFACE_MAP_END
    1.25 +
    1.26 +JSObject*
    1.27 +DOMRectReadOnly::WrapObject(JSContext* aCx)
    1.28 +{
    1.29 +  MOZ_ASSERT(mParent);
    1.30 +  return DOMRectReadOnlyBinding::Wrap(aCx, this);
    1.31 +}
    1.32 +
    1.33 +// -----------------------------------------------------------------------------
    1.34 +
    1.35 +NS_IMPL_ISUPPORTS_INHERITED(DOMRect, DOMRectReadOnly, nsIDOMClientRect)
    1.36 +
    1.37 +#define FORWARD_GETTER(_name)                                                   \
    1.38 +  NS_IMETHODIMP                                                                 \
    1.39 +  DOMRect::Get ## _name(float* aResult)                                         \
    1.40 +  {                                                                             \
    1.41 +    *aResult = float(_name());                                                  \
    1.42 +    return NS_OK;                                                               \
    1.43 +  }
    1.44 +
    1.45 +FORWARD_GETTER(Left)
    1.46 +FORWARD_GETTER(Top)
    1.47 +FORWARD_GETTER(Right)
    1.48 +FORWARD_GETTER(Bottom)
    1.49 +FORWARD_GETTER(Width)
    1.50 +FORWARD_GETTER(Height)
    1.51 +
    1.52 +JSObject*
    1.53 +DOMRect::WrapObject(JSContext* aCx)
    1.54 +{
    1.55 +  MOZ_ASSERT(mParent);
    1.56 +  return DOMRectBinding::Wrap(aCx, this);
    1.57 +}
    1.58 +
    1.59 +already_AddRefed<DOMRect>
    1.60 +DOMRect::Constructor(const GlobalObject& aGlobal, ErrorResult& aRV)
    1.61 +{
    1.62 +  nsRefPtr<DOMRect> obj =
    1.63 +    new DOMRect(aGlobal.GetAsSupports(), 0.0, 0.0, 0.0, 0.0);
    1.64 +  return obj.forget();
    1.65 +}
    1.66 +
    1.67 +already_AddRefed<DOMRect>
    1.68 +DOMRect::Constructor(const GlobalObject& aGlobal, double aX, double aY,
    1.69 +                     double aWidth, double aHeight, ErrorResult& aRV)
    1.70 +{
    1.71 +  nsRefPtr<DOMRect> obj =
    1.72 +    new DOMRect(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight);
    1.73 +  return obj.forget();
    1.74 +}
    1.75 +
    1.76 +// -----------------------------------------------------------------------------
    1.77 +
    1.78 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(DOMRectList, mParent, mArray)
    1.79 +
    1.80 +NS_INTERFACE_TABLE_HEAD(DOMRectList)
    1.81 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.82 +  NS_INTERFACE_TABLE(DOMRectList, nsIDOMClientRectList)
    1.83 +  NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList)
    1.84 +NS_INTERFACE_MAP_END
    1.85 +
    1.86 +NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList)
    1.87 +NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList)
    1.88 +
    1.89 +
    1.90 +NS_IMETHODIMP
    1.91 +DOMRectList::GetLength(uint32_t* aLength)
    1.92 +{
    1.93 +  *aLength = Length();
    1.94 +  return NS_OK;
    1.95 +}
    1.96 +
    1.97 +NS_IMETHODIMP    
    1.98 +DOMRectList::Item(uint32_t aIndex, nsIDOMClientRect** aReturn)
    1.99 +{
   1.100 +  NS_IF_ADDREF(*aReturn = Item(aIndex));
   1.101 +  return NS_OK;
   1.102 +}
   1.103 +
   1.104 +JSObject*
   1.105 +DOMRectList::WrapObject(JSContext *cx)
   1.106 +{
   1.107 +  return mozilla::dom::DOMRectListBinding::Wrap(cx, this);
   1.108 +}
   1.109 +
   1.110 +static double
   1.111 +RoundFloat(double aValue)
   1.112 +{
   1.113 +  return floor(aValue + 0.5);
   1.114 +}
   1.115 +
   1.116 +void
   1.117 +DOMRect::SetLayoutRect(const nsRect& aLayoutRect)
   1.118 +{
   1.119 +  double scale = 65536.0;
   1.120 +  // Round to the nearest 1/scale units. We choose scale so it can be represented
   1.121 +  // exactly by machine floating point.
   1.122 +  double scaleInv = 1/scale;
   1.123 +  double t2pScaled = scale/nsPresContext::AppUnitsPerCSSPixel();
   1.124 +  double x = RoundFloat(aLayoutRect.x*t2pScaled)*scaleInv;
   1.125 +  double y = RoundFloat(aLayoutRect.y*t2pScaled)*scaleInv;
   1.126 +  SetRect(x, y, RoundFloat(aLayoutRect.XMost()*t2pScaled)*scaleInv - x,
   1.127 +          RoundFloat(aLayoutRect.YMost()*t2pScaled)*scaleInv - y);
   1.128 +}

mercurial