Back to index

lightning-sunbird  0.9+nobinonly
nsImageFrame.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #ifndef nsImageFrame_h___
00038 #define nsImageFrame_h___
00039 
00040 #include "nsSplittableFrame.h"
00041 #include "nsString.h"
00042 #include "nsAString.h"
00043 #include "nsPresContext.h"
00044 #include "nsIImageFrame.h"
00045 #include "nsIIOService.h"
00046 #include "nsIObserver.h"
00047 
00048 #include "nsTransform2D.h"
00049 #include "imgIRequest.h"
00050 #include "imgIDecoderObserver.h"
00051 #include "imgIContainerObserver.h"
00052 
00053 class nsIFrame;
00054 class nsImageMap;
00055 class nsIURI;
00056 class nsILoadGroup;
00057 struct nsHTMLReflowState;
00058 struct nsHTMLReflowMetrics;
00059 struct nsSize;
00060 
00061 class nsImageFrame;
00062 
00063 class nsImageListener : public imgIDecoderObserver
00064 {
00065 public:
00066   nsImageListener(nsImageFrame *aFrame);
00067   virtual ~nsImageListener();
00068 
00069   NS_DECL_ISUPPORTS
00070   NS_DECL_IMGIDECODEROBSERVER
00071   NS_DECL_IMGICONTAINEROBSERVER
00072 
00073   void SetFrame(nsImageFrame *frame) { mFrame = frame; }
00074 
00075 private:
00076   nsImageFrame *mFrame;
00077 };
00078 
00079 #define IMAGE_SIZECONSTRAINED       0x00100000
00080 #define IMAGE_GOTINITIALREFLOW      0x00200000
00081 
00082 #define ImageFrameSuper nsSplittableFrame
00083 
00084 class nsImageFrame : public ImageFrameSuper, public nsIImageFrame {
00085 public:
00086   nsImageFrame();
00087 
00088   // nsISupports 
00089   NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
00090 
00091   NS_IMETHOD Destroy(nsPresContext* aPresContext);
00092   NS_IMETHOD Init(nsPresContext*  aPresContext,
00093                   nsIContent*      aContent,
00094                   nsIFrame*        aParent,
00095                   nsStyleContext*  aContext,
00096                   nsIFrame*        aPrevInFlow);
00097   NS_IMETHOD Paint(nsPresContext*      aPresContext,
00098                    nsIRenderingContext& aRenderingContext,
00099                    const nsRect&        aDirtyRect,
00100                    nsFramePaintLayer    aWhichLayer,
00101                    PRUint32             aFlags = 0);
00102   NS_IMETHOD Reflow(nsPresContext*          aPresContext,
00103                     nsHTMLReflowMetrics&     aDesiredSize,
00104                     const nsHTMLReflowState& aReflowState,
00105                     nsReflowStatus&          aStatus);
00106   
00107   NS_IMETHOD CanContinueTextRun(PRBool& aContinueTextRun) const;
00108 
00109 
00110   NS_IMETHOD  GetContentForEvent(nsPresContext* aPresContext,
00111                                  nsEvent* aEvent,
00112                                  nsIContent** aContent);
00113   NS_IMETHOD HandleEvent(nsPresContext* aPresContext,
00114                         nsGUIEvent* aEvent,
00115                         nsEventStatus* aEventStatus);
00116   NS_IMETHOD GetCursor(const nsPoint& aPoint,
00117                        nsIFrame::Cursor& aCursor);
00118   NS_IMETHOD AttributeChanged(nsIContent* aChild,
00119                               PRInt32 aNameSpaceID,
00120                               nsIAtom* aAttribute,
00121                               PRInt32 aModType);
00122 
00123 #ifdef ACCESSIBILITY
00124   NS_IMETHOD GetAccessible(nsIAccessible** aAccessible);
00125 #endif
00126 
00127   virtual nsIAtom* GetType() const;
00128 #ifdef DEBUG
00129   NS_IMETHOD GetFrameName(nsAString& aResult) const;
00130   NS_IMETHOD List(nsPresContext* aPresContext, FILE* out, PRInt32 aIndent) const;
00131 #endif
00132 
00133   NS_IMETHOD GetImageMap(nsPresContext *aPresContext, nsIImageMap **aImageMap);
00134 
00135   NS_IMETHOD GetIntrinsicImageSize(nsSize& aSize);
00136 
00137   static void ReleaseGlobals() {
00138     if (gIconLoad) {
00139       gIconLoad->Shutdown();
00140       NS_RELEASE(gIconLoad);
00141     }
00142     NS_IF_RELEASE(sIOService);
00143   }
00144 
00145 protected:
00146   // nsISupports
00147   NS_IMETHOD_(nsrefcnt) AddRef(void);
00148   NS_IMETHOD_(nsrefcnt) Release(void);
00149 
00150   virtual ~nsImageFrame();
00151 
00152   virtual void GetDesiredSize(nsPresContext* aPresContext,
00153                               const nsHTMLReflowState& aReflowState,
00154                               nsHTMLReflowMetrics& aDesiredSize);
00155 
00156   nsImageMap* GetImageMap(nsPresContext* aPresContext);
00157 
00158   void TriggerLink(nsPresContext* aPresContext,
00159                    nsIURI* aURI,
00160                    const nsString& aTargetSpec,
00161                    PRBool aClick);
00162 
00163   PRBool IsServerImageMap();
00164 
00165   void TranslateEventCoords(const nsPoint& aPoint,
00166                             nsPoint& aResult);
00167 
00168   PRBool GetAnchorHREFAndTarget(nsIURI** aHref, nsString& aTarget);
00169 
00170   void MeasureString(const PRUnichar*     aString,
00171                      PRInt32              aLength,
00172                      nscoord              aMaxWidth,
00173                      PRUint32&            aMaxFit,
00174                      nsIRenderingContext& aContext);
00175 
00176   void DisplayAltText(nsPresContext*      aPresContext,
00177                       nsIRenderingContext& aRenderingContext,
00178                       const nsString&      aAltText,
00179                       const nsRect&        aRect);
00180 
00181   void DisplayAltFeedback(nsPresContext*      aPresContext,
00182                           nsIRenderingContext& aRenderingContext,
00183                           imgIRequest*         aRequest);
00184 
00185   nsRect GetInnerArea() const;
00186 
00187 protected:
00188   friend class nsImageListener;
00189   nsresult OnStartContainer(imgIRequest *aRequest, imgIContainer *aImage);
00190   nsresult OnDataAvailable(imgIRequest *aRequest,
00191                            gfxIImageFrame *aFrame,
00192                            const nsRect * rect);
00193   nsresult OnStopDecode(imgIRequest *aRequest,
00194                         nsresult aStatus,
00195                         const PRUnichar *aStatusArg);
00196   nsresult FrameChanged(imgIContainer *aContainer,
00197                         gfxIImageFrame *aNewframe,
00198                         nsRect *aDirtyRect);
00199 
00200 private:
00201   // random helpers
00202   inline void SpecToURI(const nsAString& aSpec, nsIIOService *aIOService,
00203                         nsIURI **aURI);
00204 
00205   inline void GetLoadGroup(nsPresContext *aPresContext,
00206                            nsILoadGroup **aLoadGroup);
00207   nscoord GetContinuationOffset(nscoord* aWidth = 0) const;
00208   void GetDocumentCharacterSet(nsACString& aCharset) const;
00209 
00219   PRBool RecalculateTransform(imgIContainer* aImage);
00220 
00226   PRBool IsPendingLoad(imgIRequest* aRequest) const;
00227   PRBool IsPendingLoad(imgIContainer* aContainer) const;
00228 
00233   nsRect SourceRectToDest(const nsRect & aRect);
00234 
00241   nsresult HandleLoadError(PRInt16 aImageStatus);
00242   
00243   nsImageMap*         mImageMap;
00244 
00245   nsCOMPtr<imgIDecoderObserver> mListener;
00246 
00247   nsSize mComputedSize;
00248   nsSize mIntrinsicSize;
00249   nsTransform2D mTransform;
00250   
00251   nsMargin            mBorderPadding;
00252 
00253   static nsIIOService* sIOService;
00254 
00255   /* loading / broken image icon support */
00256 
00257   // XXXbz this should be handled by the prescontext, I think; that
00258   // way we would have a single iconload per mozilla session instead
00259   // of one per document...
00260 
00261   // LoadIcons: initiate the loading of the static icons used to show
00262   // loading / broken images
00263   nsresult LoadIcons(nsPresContext *aPresContext);
00264   nsresult LoadIcon(const nsAString& aSpec, nsPresContext *aPresContext,
00265                     imgIRequest **aRequest);
00266   
00267   // HandleIconLoads: See if the request is for an Icon load. If it
00268   // is, handle it and return TRUE otherwise, return FALSE (aCompleted
00269   // is an input arg telling the routine if the request has completed)
00270   PRBool HandleIconLoads(imgIRequest* aRequest, PRBool aCompleted);
00271   void InvalidateIcon();
00272 
00273   class IconLoad : public nsIObserver {
00274     // private class that wraps the data and logic needed for 
00275     // broken image and loading image icons
00276   public:
00277     IconLoad(imgIDecoderObserver* aObserver);
00278 
00279     void Shutdown()
00280     {
00281       // in case the pref service releases us later
00282       if (mLoadingImage) {
00283         mLoadingImage->Cancel(NS_ERROR_FAILURE);
00284         mLoadingImage = nsnull;
00285       }
00286       if (mBrokenImage) {
00287         mBrokenImage->Cancel(NS_ERROR_FAILURE);
00288         mBrokenImage = nsnull;
00289       }
00290     }
00291 
00292     NS_DECL_ISUPPORTS
00293     NS_DECL_NSIOBSERVER
00294 
00295   private:
00296     void GetPrefs();
00297 
00298   public:
00299     nsCOMPtr<imgIRequest> mLoadingImage;
00300     nsCOMPtr<imgIRequest> mBrokenImage;
00301     nsCOMPtr<imgIDecoderObserver> mLoadObserver; // keeps the observer alive
00302     PRUint8          mIconsLoaded;
00303     PRPackedBool     mPrefForceInlineAltText;
00304     PRPackedBool     mPrefShowPlaceholders;
00305   };
00306   static IconLoad* gIconLoad; // singleton pattern: one LoadIcons instance is used
00307 };
00308 
00309 #endif /* nsImageFrame_h___ */