layout/xul/tree/nsTreeImageListener.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/xul/tree/nsTreeImageListener.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     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 "nsTreeImageListener.h"
    1.10 +#include "nsITreeBoxObject.h"
    1.11 +#include "imgIRequest.h"
    1.12 +#include "imgIContainer.h"
    1.13 +
    1.14 +NS_IMPL_ISUPPORTS(nsTreeImageListener, imgINotificationObserver)
    1.15 +
    1.16 +nsTreeImageListener::nsTreeImageListener(nsTreeBodyFrame* aTreeFrame)
    1.17 +  : mTreeFrame(aTreeFrame),
    1.18 +    mInvalidationSuppressed(true),
    1.19 +    mInvalidationArea(nullptr)
    1.20 +{
    1.21 +}
    1.22 +
    1.23 +nsTreeImageListener::~nsTreeImageListener()
    1.24 +{
    1.25 +  delete mInvalidationArea;
    1.26 +}
    1.27 +
    1.28 +NS_IMETHODIMP
    1.29 +nsTreeImageListener::Notify(imgIRequest *aRequest, int32_t aType, const nsIntRect* aData)
    1.30 +{
    1.31 +  if (aType == imgINotificationObserver::IS_ANIMATED) {
    1.32 +    return mTreeFrame ? mTreeFrame->OnImageIsAnimated(aRequest) : NS_OK;
    1.33 +  }
    1.34 +
    1.35 +  if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
    1.36 +    // Ensure the animation (if any) is started. Note: There is no
    1.37 +    // corresponding call to Decrement for this. This Increment will be
    1.38 +    // 'cleaned up' by the Request when it is destroyed, but only then.
    1.39 +    aRequest->IncrementAnimationConsumers();
    1.40 +  }
    1.41 +
    1.42 +  if (aType == imgINotificationObserver::FRAME_UPDATE) {
    1.43 +    Invalidate();
    1.44 +  }
    1.45 +
    1.46 +  return NS_OK;
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +nsTreeImageListener::AddCell(int32_t aIndex, nsITreeColumn* aCol)
    1.51 +{
    1.52 +  if (!mInvalidationArea) {
    1.53 +    mInvalidationArea = new InvalidationArea(aCol);
    1.54 +    mInvalidationArea->AddRow(aIndex);
    1.55 +  }
    1.56 +  else {
    1.57 +    InvalidationArea* currArea;
    1.58 +    for (currArea = mInvalidationArea; currArea; currArea = currArea->GetNext()) {
    1.59 +      if (currArea->GetCol() == aCol) {
    1.60 +        currArea->AddRow(aIndex);
    1.61 +        break;
    1.62 +      }
    1.63 +    }
    1.64 +    if (!currArea) {
    1.65 +      currArea = new InvalidationArea(aCol);
    1.66 +      currArea->SetNext(mInvalidationArea);
    1.67 +      mInvalidationArea = currArea;
    1.68 +      mInvalidationArea->AddRow(aIndex);
    1.69 +    }
    1.70 +  }
    1.71 +}
    1.72 +
    1.73 +
    1.74 +void
    1.75 +nsTreeImageListener::Invalidate()
    1.76 +{
    1.77 +  if (!mInvalidationSuppressed) {
    1.78 +    for (InvalidationArea* currArea = mInvalidationArea; currArea;
    1.79 +         currArea = currArea->GetNext()) {
    1.80 +      // Loop from min to max, invalidating each cell that was listening for this image.
    1.81 +      for (int32_t i = currArea->GetMin(); i <= currArea->GetMax(); ++i) {
    1.82 +        if (mTreeFrame) {
    1.83 +          nsITreeBoxObject* tree = mTreeFrame->GetTreeBoxObject();
    1.84 +          if (tree) {
    1.85 +            tree->InvalidateCell(i, currArea->GetCol());
    1.86 +          }
    1.87 +        }
    1.88 +      }
    1.89 +    }
    1.90 +  }
    1.91 +}
    1.92 +
    1.93 +nsTreeImageListener::InvalidationArea::InvalidationArea(nsITreeColumn* aCol)
    1.94 +  : mCol(aCol),
    1.95 +    mMin(-1), // min should start out "undefined"
    1.96 +    mMax(0),
    1.97 +    mNext(nullptr)
    1.98 +{
    1.99 +}
   1.100 +
   1.101 +void
   1.102 +nsTreeImageListener::InvalidationArea::AddRow(int32_t aIndex)
   1.103 +{
   1.104 +  if (mMin == -1)
   1.105 +    mMin = mMax = aIndex;
   1.106 +  else if (aIndex < mMin)
   1.107 +    mMin = aIndex;
   1.108 +  else if (aIndex > mMax)
   1.109 +    mMax = aIndex;
   1.110 +}
   1.111 +
   1.112 +NS_IMETHODIMP
   1.113 +nsTreeImageListener::ClearFrame()
   1.114 +{
   1.115 +  mTreeFrame = nullptr;
   1.116 +  return NS_OK;
   1.117 +}

mercurial