layout/generic/nsImageFrame.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /* rendering object for replaced elements with bitmap image data */
michael@0 7
michael@0 8 #ifndef nsImageFrame_h___
michael@0 9 #define nsImageFrame_h___
michael@0 10
michael@0 11 #include "nsSplittableFrame.h"
michael@0 12 #include "nsIIOService.h"
michael@0 13 #include "nsIObserver.h"
michael@0 14
michael@0 15 #include "imgINotificationObserver.h"
michael@0 16
michael@0 17 #include "nsDisplayList.h"
michael@0 18 #include "imgIContainer.h"
michael@0 19 #include "mozilla/Attributes.h"
michael@0 20 #include "mozilla/DebugOnly.h"
michael@0 21 #include "nsIReflowCallback.h"
michael@0 22
michael@0 23 class nsImageMap;
michael@0 24 class nsIURI;
michael@0 25 class nsILoadGroup;
michael@0 26 struct nsHTMLReflowState;
michael@0 27 struct nsHTMLReflowMetrics;
michael@0 28 class nsDisplayImage;
michael@0 29 class nsPresContext;
michael@0 30 class nsImageFrame;
michael@0 31 class nsTransform2D;
michael@0 32 class nsImageLoadingContent;
michael@0 33
michael@0 34 namespace mozilla {
michael@0 35 namespace layers {
michael@0 36 class ImageContainer;
michael@0 37 class ImageLayer;
michael@0 38 class LayerManager;
michael@0 39 }
michael@0 40 }
michael@0 41
michael@0 42 class nsImageListener : public imgINotificationObserver
michael@0 43 {
michael@0 44 public:
michael@0 45 nsImageListener(nsImageFrame *aFrame);
michael@0 46 virtual ~nsImageListener();
michael@0 47
michael@0 48 NS_DECL_ISUPPORTS
michael@0 49 NS_DECL_IMGINOTIFICATIONOBSERVER
michael@0 50
michael@0 51 void SetFrame(nsImageFrame *frame) { mFrame = frame; }
michael@0 52
michael@0 53 private:
michael@0 54 nsImageFrame *mFrame;
michael@0 55 };
michael@0 56
michael@0 57 typedef nsSplittableFrame ImageFrameSuper;
michael@0 58
michael@0 59 class nsImageFrame : public ImageFrameSuper,
michael@0 60 public nsIReflowCallback {
michael@0 61 public:
michael@0 62 typedef mozilla::layers::ImageContainer ImageContainer;
michael@0 63 typedef mozilla::layers::ImageLayer ImageLayer;
michael@0 64 typedef mozilla::layers::LayerManager LayerManager;
michael@0 65
michael@0 66 NS_DECL_FRAMEARENA_HELPERS
michael@0 67
michael@0 68 nsImageFrame(nsStyleContext* aContext);
michael@0 69
michael@0 70 NS_DECL_QUERYFRAME_TARGET(nsImageFrame)
michael@0 71 NS_DECL_QUERYFRAME
michael@0 72
michael@0 73 virtual void DestroyFrom(nsIFrame* aDestructRoot) MOZ_OVERRIDE;
michael@0 74 virtual void Init(nsIContent* aContent,
michael@0 75 nsIFrame* aParent,
michael@0 76 nsIFrame* aPrevInFlow) MOZ_OVERRIDE;
michael@0 77 virtual void BuildDisplayList(nsDisplayListBuilder* aBuilder,
michael@0 78 const nsRect& aDirtyRect,
michael@0 79 const nsDisplayListSet& aLists) MOZ_OVERRIDE;
michael@0 80 virtual nscoord GetMinWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
michael@0 81 virtual nscoord GetPrefWidth(nsRenderingContext *aRenderingContext) MOZ_OVERRIDE;
michael@0 82 virtual mozilla::IntrinsicSize GetIntrinsicSize() MOZ_OVERRIDE;
michael@0 83 virtual nsSize GetIntrinsicRatio() MOZ_OVERRIDE;
michael@0 84 virtual nsresult Reflow(nsPresContext* aPresContext,
michael@0 85 nsHTMLReflowMetrics& aDesiredSize,
michael@0 86 const nsHTMLReflowState& aReflowState,
michael@0 87 nsReflowStatus& aStatus) MOZ_OVERRIDE;
michael@0 88
michael@0 89 virtual nsresult GetContentForEvent(mozilla::WidgetEvent* aEvent,
michael@0 90 nsIContent** aContent) MOZ_OVERRIDE;
michael@0 91 virtual nsresult HandleEvent(nsPresContext* aPresContext,
michael@0 92 mozilla::WidgetGUIEvent* aEvent,
michael@0 93 nsEventStatus* aEventStatus) MOZ_OVERRIDE;
michael@0 94 virtual nsresult GetCursor(const nsPoint& aPoint,
michael@0 95 nsIFrame::Cursor& aCursor) MOZ_OVERRIDE;
michael@0 96 virtual nsresult AttributeChanged(int32_t aNameSpaceID,
michael@0 97 nsIAtom* aAttribute,
michael@0 98 int32_t aModType) MOZ_OVERRIDE;
michael@0 99
michael@0 100 #ifdef ACCESSIBILITY
michael@0 101 virtual mozilla::a11y::AccType AccessibleType() MOZ_OVERRIDE;
michael@0 102 #endif
michael@0 103
michael@0 104 virtual nsIAtom* GetType() const MOZ_OVERRIDE;
michael@0 105
michael@0 106 virtual bool IsFrameOfType(uint32_t aFlags) const MOZ_OVERRIDE
michael@0 107 {
michael@0 108 return ImageFrameSuper::IsFrameOfType(aFlags & ~(nsIFrame::eReplaced));
michael@0 109 }
michael@0 110
michael@0 111 #ifdef DEBUG_FRAME_DUMP
michael@0 112 virtual nsresult GetFrameName(nsAString& aResult) const MOZ_OVERRIDE;
michael@0 113 void List(FILE* out = stderr, const char* aPrefix = "",
michael@0 114 uint32_t aFlags = 0) const MOZ_OVERRIDE;
michael@0 115 #endif
michael@0 116
michael@0 117 virtual int GetLogicalSkipSides(const nsHTMLReflowState* aReflowState = nullptr) const MOZ_OVERRIDE;
michael@0 118
michael@0 119 nsresult GetIntrinsicImageSize(nsSize& aSize);
michael@0 120
michael@0 121 static void ReleaseGlobals() {
michael@0 122 if (gIconLoad) {
michael@0 123 gIconLoad->Shutdown();
michael@0 124 NS_RELEASE(gIconLoad);
michael@0 125 }
michael@0 126 NS_IF_RELEASE(sIOService);
michael@0 127 }
michael@0 128
michael@0 129 nsresult Notify(imgIRequest *aRequest, int32_t aType, const nsIntRect* aData);
michael@0 130
michael@0 131 /**
michael@0 132 * Function to test whether aContent, which has aStyleContext as its style,
michael@0 133 * should get an image frame. Note that this method is only used by the
michael@0 134 * frame constructor; it's only here because it uses gIconLoad for now.
michael@0 135 */
michael@0 136 static bool ShouldCreateImageFrameFor(mozilla::dom::Element* aElement,
michael@0 137 nsStyleContext* aStyleContext);
michael@0 138
michael@0 139 void DisplayAltFeedback(nsRenderingContext& aRenderingContext,
michael@0 140 const nsRect& aDirtyRect,
michael@0 141 imgIRequest* aRequest,
michael@0 142 nsPoint aPt);
michael@0 143
michael@0 144 nsRect GetInnerArea() const;
michael@0 145
michael@0 146 /**
michael@0 147 * Return a map element associated with this image.
michael@0 148 */
michael@0 149 mozilla::dom::Element* GetMapElement() const
michael@0 150 {
michael@0 151 nsAutoString usemap;
michael@0 152 if (mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::usemap, usemap)) {
michael@0 153 return mContent->OwnerDoc()->FindImageMap(usemap);
michael@0 154 }
michael@0 155 return nullptr;
michael@0 156 }
michael@0 157
michael@0 158 /**
michael@0 159 * Return true if the image has associated image map.
michael@0 160 */
michael@0 161 bool HasImageMap() const { return mImageMap || GetMapElement(); }
michael@0 162
michael@0 163 nsImageMap* GetImageMap();
michael@0 164 nsImageMap* GetExistingImageMap() const { return mImageMap; }
michael@0 165
michael@0 166 virtual void AddInlineMinWidth(nsRenderingContext *aRenderingContext,
michael@0 167 InlineMinWidthData *aData) MOZ_OVERRIDE;
michael@0 168
michael@0 169 void DisconnectMap();
michael@0 170
michael@0 171 // nsIReflowCallback
michael@0 172 virtual bool ReflowFinished() MOZ_OVERRIDE;
michael@0 173 virtual void ReflowCallbackCanceled() MOZ_OVERRIDE;
michael@0 174
michael@0 175 protected:
michael@0 176 virtual ~nsImageFrame();
michael@0 177
michael@0 178 void EnsureIntrinsicSizeAndRatio(nsPresContext* aPresContext);
michael@0 179
michael@0 180 virtual nsSize ComputeSize(nsRenderingContext *aRenderingContext,
michael@0 181 nsSize aCBSize, nscoord aAvailableWidth,
michael@0 182 nsSize aMargin, nsSize aBorder, nsSize aPadding,
michael@0 183 uint32_t aFlags) MOZ_OVERRIDE;
michael@0 184
michael@0 185 bool IsServerImageMap();
michael@0 186
michael@0 187 void TranslateEventCoords(const nsPoint& aPoint,
michael@0 188 nsIntPoint& aResult);
michael@0 189
michael@0 190 bool GetAnchorHREFTargetAndNode(nsIURI** aHref, nsString& aTarget,
michael@0 191 nsIContent** aNode);
michael@0 192 /**
michael@0 193 * Computes the width of the string that fits into the available space
michael@0 194 *
michael@0 195 * @param in aLength total length of the string in PRUnichars
michael@0 196 * @param in aMaxWidth width not to be exceeded
michael@0 197 * @param out aMaxFit length of the string that fits within aMaxWidth
michael@0 198 * in PRUnichars
michael@0 199 * @return width of the string that fits within aMaxWidth
michael@0 200 */
michael@0 201 nscoord MeasureString(const char16_t* aString,
michael@0 202 int32_t aLength,
michael@0 203 nscoord aMaxWidth,
michael@0 204 uint32_t& aMaxFit,
michael@0 205 nsRenderingContext& aContext);
michael@0 206
michael@0 207 void DisplayAltText(nsPresContext* aPresContext,
michael@0 208 nsRenderingContext& aRenderingContext,
michael@0 209 const nsString& aAltText,
michael@0 210 const nsRect& aRect);
michael@0 211
michael@0 212 void PaintImage(nsRenderingContext& aRenderingContext, nsPoint aPt,
michael@0 213 const nsRect& aDirtyRect, imgIContainer* aImage,
michael@0 214 uint32_t aFlags);
michael@0 215
michael@0 216 protected:
michael@0 217 friend class nsImageListener;
michael@0 218 friend class nsImageLoadingContent;
michael@0 219 nsresult OnStartContainer(imgIRequest *aRequest, imgIContainer *aImage);
michael@0 220 nsresult OnDataAvailable(imgIRequest *aRequest, const nsIntRect *rect);
michael@0 221 nsresult OnStopRequest(imgIRequest *aRequest,
michael@0 222 nsresult aStatus);
michael@0 223 nsresult FrameChanged(imgIRequest *aRequest,
michael@0 224 imgIContainer *aContainer);
michael@0 225 /**
michael@0 226 * Notification that aRequest will now be the current request.
michael@0 227 */
michael@0 228 void NotifyNewCurrentRequest(imgIRequest *aRequest, nsresult aStatus);
michael@0 229
michael@0 230 private:
michael@0 231 // random helpers
michael@0 232 inline void SpecToURI(const nsAString& aSpec, nsIIOService *aIOService,
michael@0 233 nsIURI **aURI);
michael@0 234
michael@0 235 inline void GetLoadGroup(nsPresContext *aPresContext,
michael@0 236 nsILoadGroup **aLoadGroup);
michael@0 237 nscoord GetContinuationOffset() const;
michael@0 238 void GetDocumentCharacterSet(nsACString& aCharset) const;
michael@0 239 bool ShouldDisplaySelection();
michael@0 240
michael@0 241 /**
michael@0 242 * Recalculate mIntrinsicSize from the image.
michael@0 243 *
michael@0 244 * @return whether aImage's size did _not_
michael@0 245 * match our previous intrinsic size.
michael@0 246 */
michael@0 247 bool UpdateIntrinsicSize(imgIContainer* aImage);
michael@0 248
michael@0 249 /**
michael@0 250 * Recalculate mIntrinsicRatio from the image.
michael@0 251 *
michael@0 252 * @return whether aImage's ratio did _not_
michael@0 253 * match our previous intrinsic ratio.
michael@0 254 */
michael@0 255 bool UpdateIntrinsicRatio(imgIContainer* aImage);
michael@0 256
michael@0 257 /**
michael@0 258 * This function calculates the transform for converting between
michael@0 259 * source space & destination space. May fail if our image has a
michael@0 260 * percent-valued or zero-valued height or width.
michael@0 261 *
michael@0 262 * @param aTransform The transform object to populate.
michael@0 263 *
michael@0 264 * @return whether we succeeded in creating the transform.
michael@0 265 */
michael@0 266 bool GetSourceToDestTransform(nsTransform2D& aTransform);
michael@0 267
michael@0 268 /**
michael@0 269 * Helper functions to check whether the request or image container
michael@0 270 * corresponds to a load we don't care about. Most of the decoder
michael@0 271 * observer methods will bail early if these return true.
michael@0 272 */
michael@0 273 bool IsPendingLoad(imgIRequest* aRequest) const;
michael@0 274 bool IsPendingLoad(imgIContainer* aContainer) const;
michael@0 275
michael@0 276 /**
michael@0 277 * Function to convert a dirty rect in the source image to a dirty
michael@0 278 * rect for the image frame.
michael@0 279 */
michael@0 280 nsRect SourceRectToDest(const nsIntRect & aRect);
michael@0 281
michael@0 282 nsImageMap* mImageMap;
michael@0 283
michael@0 284 nsCOMPtr<imgINotificationObserver> mListener;
michael@0 285
michael@0 286 nsCOMPtr<imgIContainer> mImage;
michael@0 287 nsSize mComputedSize;
michael@0 288 mozilla::IntrinsicSize mIntrinsicSize;
michael@0 289 nsSize mIntrinsicRatio;
michael@0 290
michael@0 291 bool mDisplayingIcon;
michael@0 292 bool mFirstFrameComplete;
michael@0 293 bool mReflowCallbackPosted;
michael@0 294
michael@0 295 static nsIIOService* sIOService;
michael@0 296
michael@0 297 /* loading / broken image icon support */
michael@0 298
michael@0 299 // XXXbz this should be handled by the prescontext, I think; that
michael@0 300 // way we would have a single iconload per mozilla session instead
michael@0 301 // of one per document...
michael@0 302
michael@0 303 // LoadIcons: initiate the loading of the static icons used to show
michael@0 304 // loading / broken images
michael@0 305 nsresult LoadIcons(nsPresContext *aPresContext);
michael@0 306 nsresult LoadIcon(const nsAString& aSpec, nsPresContext *aPresContext,
michael@0 307 imgRequestProxy **aRequest);
michael@0 308
michael@0 309 class IconLoad MOZ_FINAL : public nsIObserver,
michael@0 310 public imgINotificationObserver {
michael@0 311 // private class that wraps the data and logic needed for
michael@0 312 // broken image and loading image icons
michael@0 313 public:
michael@0 314 IconLoad();
michael@0 315
michael@0 316 void Shutdown();
michael@0 317
michael@0 318 NS_DECL_ISUPPORTS
michael@0 319 NS_DECL_NSIOBSERVER
michael@0 320 NS_DECL_IMGINOTIFICATIONOBSERVER
michael@0 321
michael@0 322 void AddIconObserver(nsImageFrame *frame) {
michael@0 323 NS_ABORT_IF_FALSE(!mIconObservers.Contains(frame),
michael@0 324 "Observer shouldn't aleady be in array");
michael@0 325 mIconObservers.AppendElement(frame);
michael@0 326 }
michael@0 327
michael@0 328 void RemoveIconObserver(nsImageFrame *frame) {
michael@0 329 mozilla::DebugOnly<bool> didRemove = mIconObservers.RemoveElement(frame);
michael@0 330 NS_ABORT_IF_FALSE(didRemove, "Observer not in array");
michael@0 331 }
michael@0 332
michael@0 333 private:
michael@0 334 void GetPrefs();
michael@0 335 nsTObserverArray<nsImageFrame*> mIconObservers;
michael@0 336
michael@0 337
michael@0 338 public:
michael@0 339 nsRefPtr<imgRequestProxy> mLoadingImage;
michael@0 340 nsRefPtr<imgRequestProxy> mBrokenImage;
michael@0 341 bool mPrefForceInlineAltText;
michael@0 342 bool mPrefShowPlaceholders;
michael@0 343 };
michael@0 344
michael@0 345 public:
michael@0 346 static IconLoad* gIconLoad; // singleton pattern: one LoadIcons instance is used
michael@0 347
michael@0 348 friend class nsDisplayImage;
michael@0 349 };
michael@0 350
michael@0 351 /**
michael@0 352 * Note that nsDisplayImage does not receive events. However, an image element
michael@0 353 * is replaced content so its background will be z-adjacent to the
michael@0 354 * image itself, and hence receive events just as if the image itself
michael@0 355 * received events.
michael@0 356 */
michael@0 357 class nsDisplayImage : public nsDisplayImageContainer {
michael@0 358 public:
michael@0 359 typedef mozilla::layers::LayerManager LayerManager;
michael@0 360
michael@0 361 nsDisplayImage(nsDisplayListBuilder* aBuilder, nsImageFrame* aFrame,
michael@0 362 imgIContainer* aImage)
michael@0 363 : nsDisplayImageContainer(aBuilder, aFrame), mImage(aImage) {
michael@0 364 MOZ_COUNT_CTOR(nsDisplayImage);
michael@0 365 }
michael@0 366 virtual ~nsDisplayImage() {
michael@0 367 MOZ_COUNT_DTOR(nsDisplayImage);
michael@0 368 }
michael@0 369 virtual void ComputeInvalidationRegion(nsDisplayListBuilder* aBuilder,
michael@0 370 const nsDisplayItemGeometry* aGeometry,
michael@0 371 nsRegion* aInvalidRegion) MOZ_OVERRIDE;
michael@0 372 virtual void Paint(nsDisplayListBuilder* aBuilder,
michael@0 373 nsRenderingContext* aCtx) MOZ_OVERRIDE;
michael@0 374
michael@0 375 /**
michael@0 376 * Returns an ImageContainer for this image if the image type
michael@0 377 * supports it (TYPE_RASTER only).
michael@0 378 */
michael@0 379 virtual already_AddRefed<ImageContainer> GetContainer(LayerManager* aManager,
michael@0 380 nsDisplayListBuilder* aBuilder) MOZ_OVERRIDE;
michael@0 381
michael@0 382 gfxRect GetDestRect();
michael@0 383
michael@0 384 virtual LayerState GetLayerState(nsDisplayListBuilder* aBuilder,
michael@0 385 LayerManager* aManager,
michael@0 386 const ContainerLayerParameters& aParameters) MOZ_OVERRIDE;
michael@0 387 virtual nsRect GetBounds(nsDisplayListBuilder* aBuilder, bool* aSnap) MOZ_OVERRIDE
michael@0 388 {
michael@0 389 *aSnap = true;
michael@0 390 return nsRect(ToReferenceFrame(), Frame()->GetSize());
michael@0 391 }
michael@0 392
michael@0 393 virtual already_AddRefed<Layer> BuildLayer(nsDisplayListBuilder* aBuilder,
michael@0 394 LayerManager* aManager,
michael@0 395 const ContainerLayerParameters& aContainerParameters) MOZ_OVERRIDE;
michael@0 396
michael@0 397 /**
michael@0 398 * Configure an ImageLayer for this display item.
michael@0 399 * Set the required filter and scaling transform.
michael@0 400 */
michael@0 401 virtual void ConfigureLayer(ImageLayer* aLayer, const nsIntPoint& aOffset) MOZ_OVERRIDE;
michael@0 402
michael@0 403 NS_DISPLAY_DECL_NAME("Image", TYPE_IMAGE)
michael@0 404 private:
michael@0 405 nsCOMPtr<imgIContainer> mImage;
michael@0 406 };
michael@0 407
michael@0 408 #endif /* nsImageFrame_h___ */

mercurial