/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- * vim: set ts=2 sw=2 et tw=78: * ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is mozilla.org code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Steve Clark * HÃ¥kan Waara * Dan Rosen * Daniel Glazman * Mats Palmgren * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** * * This Original Code has been modified by IBM Corporation. * Modifications made by IBM described herein are * Copyright (c) International Business Machines * Corporation, 2000 * * Modifications to Mozilla code or documentation * identified per MPL Section 3.3 * * Date Modified by Description of modification * 05/03/2000 IBM Corp. Observer events for reflow states */ /* a presentation of a document, part 2 */ #include "nsIPresShell.h" #include "nsPresContext.h" #include "nsIContent.h" #include "mozilla/dom/Element.h" #include "nsIDocument.h" #include "nsIDOMXULDocument.h" #include "nsStubDocumentObserver.h" #include "nsStyleSet.h" #include "nsCSSStyleSheet.h" // XXX for UA sheet loading hack, can this go away please? #include "nsIDOMCSSStyleSheet.h" // for Pref-related rule management (bugs 22963,20760,31816) #include "nsINameSpaceManager.h" // for Pref-related rule management (bugs 22963,20760,31816) #include "nsIServiceManager.h" #include "nsFrame.h" #include "nsIViewManager.h" #include "nsCRT.h" #include "nsCRTGlue.h" #include "prlog.h" #include "prmem.h" #include "prprf.h" #include "prinrval.h" #include "nsTArray.h" #include "nsCOMArray.h" #include "nsHashtable.h" #include "nsIViewObserver.h" #include "nsContainerFrame.h" #include "nsIDeviceContext.h" #include "nsEventStateManager.h" #include "nsDOMEvent.h" #include "nsGUIEvent.h" #include "nsHTMLParts.h" #include "nsContentUtils.h" #include "nsISelection.h" #include "nsISelectionController.h" #include "nsISelectionPrivate.h" #include "nsLayoutCID.h" #include "nsGkAtoms.h" #include "nsIDOMRange.h" #include "nsIDOMDocument.h" #include "nsIDOMNode.h" #include "nsIDOM3Node.h" #include "nsIDOMNodeList.h" #include "nsIDOMElement.h" #include "nsRange.h" #include "nsCSSPseudoElements.h" #include "nsCOMPtr.h" #include "nsAutoPtr.h" #include "nsReadableUtils.h" #include "nsUnicharUtils.h" #include "nsWeakReference.h" #include "nsIPageSequenceFrame.h" #include "nsCaret.h" #include "nsIDOMHTMLDocument.h" #include "nsIXPointer.h" #include "nsIDOMXMLDocument.h" #include "nsIParser.h" #include "nsParserCIID.h" #include "nsFrameSelection.h" #include "nsIDOMNSHTMLInputElement.h" //optimization for ::DoXXX commands #include "nsIDOMNSHTMLTextAreaElement.h" #include "nsViewsCID.h" #include "nsPresArena.h" #include "nsFrameManager.h" #include "nsXPCOM.h" #include "nsISupportsPrimitives.h" #include "nsILayoutHistoryState.h" #include "nsILineIterator.h" // for ScrollContentIntoView #include "nsWeakPtr.h" #include "pldhash.h" #include "nsIObserverService.h" #include "nsIObserver.h" #include "nsIDocShell.h" // for reflow observation #include "nsIBaseWindow.h" #include "nsLayoutErrors.h" #include "nsLayoutUtils.h" #include "nsCSSRendering.h" // for |#ifdef DEBUG| code #include "prenv.h" #include "nsIAttribute.h" #include "nsIGlobalHistory2.h" #include "nsDisplayList.h" #include "nsIRegion.h" #include "nsRegion.h" #ifdef MOZ_REFLOW_PERF #include "nsIRenderingContext.h" #include "nsIFontMetrics.h" #endif #include "nsIReflowCallback.h" #include "nsPIDOMWindow.h" #include "nsFocusManager.h" #include "nsIPluginInstance.h" #include "nsIObjectFrame.h" #include "nsIObjectLoadingContent.h" #include "nsNetUtil.h" #include "nsEventDispatcher.h" #include "nsThreadUtils.h" #include "nsStyleSheetService.h" #include "gfxImageSurface.h" #include "gfxContext.h" #ifdef MOZ_MEDIA #include "nsHTMLMediaElement.h" #endif #ifdef MOZ_SMIL #include "nsSMILAnimationController.h" #endif // Drag & Drop, Clipboard #include "nsWidgetsCID.h" #include "nsIClipboard.h" #include "nsIClipboardHelper.h" #include "nsIDocShellTreeItem.h" #include "nsIURI.h" #include "nsIScrollableFrame.h" #include "prtime.h" #include "prlong.h" #include "nsIDragService.h" #include "nsCopySupport.h" #include "nsIDOMHTMLAnchorElement.h" #include "nsIDOMHTMLAreaElement.h" #include "nsIDOMHTMLLinkElement.h" #include "nsITimer.h" #ifdef ACCESSIBILITY #include "nsIAccessibilityService.h" #include "nsAccessible.h" #endif // For style data reconstruction #include "nsStyleChangeList.h" #include "nsCSSFrameConstructor.h" #ifdef MOZ_XUL #include "nsMenuFrame.h" #include "nsTreeBodyFrame.h" #include "nsIBoxObject.h" #include "nsITreeBoxObject.h" #include "nsMenuPopupFrame.h" #include "nsITreeColumns.h" #include "nsIDOMXULMultSelectCntrlEl.h" #include "nsIDOMXULSelectCntrlItemEl.h" #include "nsIDOMXULMenuListElement.h" #endif #include "nsPlaceholderFrame.h" #include "nsCanvasFrame.h" // Content viewer interfaces #include "nsIContentViewer.h" #include "imgIEncoder.h" #include "gfxPlatform.h" #include "mozilla/FunctionTimer.h" #ifdef NS_FUNCTION_TIMER #define NS_TIME_FUNCTION_DECLARE_DOCURL \ nsCAutoString docURL__("N/A"); \ nsIURI *uri__ = mDocument->GetDocumentURI(); \ if (uri__) uri__->GetSpec(docURL__); #define NS_TIME_FUNCTION_WITH_DOCURL \ NS_TIME_FUNCTION_DECLARE_DOCURL \ NS_TIME_FUNCTION_MIN_FMT(1.0, \ "%s (line %d) (document: %s)", MOZ_FUNCTION_NAME, \ __LINE__, docURL__.get()) #else #define NS_TIME_FUNCTION_WITH_DOCURL do{} while(0) #endif #include "nsContentCID.h" static NS_DEFINE_IID(kRangeCID, NS_RANGE_CID); /* for NS_MEMORY_REPORTER_IMPLEMENT */ #include "nsIMemoryReporter.h" using namespace mozilla; using namespace mozilla::layers; using namespace mozilla::dom; PRBool nsIPresShell::gIsAccessibilityActive = PR_FALSE; CapturingContentInfo nsIPresShell::gCaptureInfo; // convert a color value to a string, in the CSS format #RRGGBB // * - initially created for bugs 31816, 20760, 22963 static void ColorToString(nscolor aColor, nsAutoString &aString); // Class ID's static NS_DEFINE_CID(kFrameSelectionCID, NS_FRAMESELECTION_CID); // RangePaintInfo is used to paint ranges to offscreen buffers struct RangePaintInfo { nsCOMPtr mRange; nsDisplayListBuilder mBuilder; nsDisplayList mList; // offset of builder's reference frame to the root frame nsPoint mRootOffset; RangePaintInfo(nsIRange* aRange, nsIFrame* aFrame) : mRange(aRange), mBuilder(aFrame, PR_FALSE, PR_FALSE) { MOZ_COUNT_CTOR(RangePaintInfo); } ~RangePaintInfo() { mList.DeleteAll(); MOZ_COUNT_DTOR(RangePaintInfo); } }; #undef NOISY // ---------------------------------------------------------------------- #ifdef NS_DEBUG // Set the environment variable GECKO_VERIFY_REFLOW_FLAGS to one or // more of the following flags (comma separated) for handy debug // output. static PRUint32 gVerifyReflowFlags; struct VerifyReflowFlags { const char* name; PRUint32 bit; }; static const VerifyReflowFlags gFlags[] = { { "verify", VERIFY_REFLOW_ON }, { "reflow", VERIFY_REFLOW_NOISY }, { "all", VERIFY_REFLOW_ALL }, { "list-commands", VERIFY_REFLOW_DUMP_COMMANDS }, { "noisy-commands", VERIFY_REFLOW_NOISY_RC }, { "really-noisy-commands", VERIFY_REFLOW_REALLY_NOISY_RC }, { "resize", VERIFY_REFLOW_DURING_RESIZE_REFLOW }, }; #define NUM_VERIFY_REFLOW_FLAGS (sizeof(gFlags) / sizeof(gFlags[0])) static void ShowVerifyReflowFlags() { printf("Here are the available GECKO_VERIFY_REFLOW_FLAGS:\n"); const VerifyReflowFlags* flag = gFlags; const VerifyReflowFlags* limit = gFlags + NUM_VERIFY_REFLOW_FLAGS; while (flag < limit) { printf(" %s\n", flag->name); ++flag; } printf("Note: GECKO_VERIFY_REFLOW_FLAGS is a comma separated list of flag\n"); printf("names (no whitespace)\n"); } #endif //======================================================================== //======================================================================== //======================================================================== #ifdef MOZ_REFLOW_PERF class ReflowCountMgr; static const char kGrandTotalsStr[] = "Grand Totals"; // Counting Class class ReflowCounter { public: ReflowCounter(ReflowCountMgr * aMgr = nsnull); ~ReflowCounter(); void ClearTotals(); void DisplayTotals(const char * aStr); void DisplayDiffTotals(const char * aStr); void DisplayHTMLTotals(const char * aStr); void Add() { mTotal++; } void Add(PRUint32 aTotal) { mTotal += aTotal; } void CalcDiffInTotals(); void SetTotalsCache(); void SetMgr(ReflowCountMgr * aMgr) { mMgr = aMgr; } PRUint32 GetTotal() { return mTotal; } protected: void DisplayTotals(PRUint32 aTotal, const char * aTitle); void DisplayHTMLTotals(PRUint32 aTotal, const char * aTitle); PRUint32 mTotal; PRUint32 mCacheTotal; ReflowCountMgr * mMgr; // weak reference (don't delete) }; // Counting Class class IndiReflowCounter { public: IndiReflowCounter(ReflowCountMgr * aMgr = nsnull) : mFrame(nsnull), mCount(0), mMgr(aMgr), mCounter(aMgr), mHasBeenOutput(PR_FALSE) {} virtual ~IndiReflowCounter() {} nsAutoString mName; nsIFrame * mFrame; // weak reference (don't delete) PRInt32 mCount; ReflowCountMgr * mMgr; // weak reference (don't delete) ReflowCounter mCounter; PRBool mHasBeenOutput; }; //-------------------- // Manager Class //-------------------- class ReflowCountMgr { public: ReflowCountMgr(); virtual ~ReflowCountMgr(); void ClearTotals(); void ClearGrandTotals(); void DisplayTotals(const char * aStr); void DisplayHTMLTotals(const char * aStr); void DisplayDiffsInTotals(const char * aStr); void Add(const char * aName, nsIFrame * aFrame); ReflowCounter * LookUp(const char * aName); void PaintCount(const char * aName, nsIRenderingContext* aRenderingContext, nsPresContext* aPresContext, nsIFrame * aFrame, PRUint32 aColor); FILE * GetOutFile() { return mFD; } PLHashTable * GetIndiFrameHT() { return mIndiFrameCounts; } void SetPresContext(nsPresContext * aPresContext) { mPresContext = aPresContext; } // weak reference void SetPresShell(nsIPresShell* aPresShell) { mPresShell= aPresShell; } // weak reference void SetDumpFrameCounts(PRBool aVal) { mDumpFrameCounts = aVal; } void SetDumpFrameByFrameCounts(PRBool aVal) { mDumpFrameByFrameCounts = aVal; } void SetPaintFrameCounts(PRBool aVal) { mPaintFrameByFrameCounts = aVal; } PRBool IsPaintingFrameCounts() { return mPaintFrameByFrameCounts; } protected: void DisplayTotals(PRUint32 aTotal, PRUint32 * aDupArray, char * aTitle); void DisplayHTMLTotals(PRUint32 aTotal, PRUint32 * aDupArray, char * aTitle); static PRIntn RemoveItems(PLHashEntry *he, PRIntn i, void *arg); static PRIntn RemoveIndiItems(PLHashEntry *he, PRIntn i, void *arg); void CleanUp(); // stdout Output Methods static PRIntn DoSingleTotal(PLHashEntry *he, PRIntn i, void *arg); static PRIntn DoSingleIndi(PLHashEntry *he, PRIntn i, void *arg); void DoGrandTotals(); void DoIndiTotalsTree(); // HTML Output Methods static PRIntn DoSingleHTMLTotal(PLHashEntry *he, PRIntn i, void *arg); void DoGrandHTMLTotals(); // Zero Out the Totals static PRIntn DoClearTotals(PLHashEntry *he, PRIntn i, void *arg); // Displays the Diff Totals static PRIntn DoDisplayDiffTotals(PLHashEntry *he, PRIntn i, void *arg); PLHashTable * mCounts; PLHashTable * mIndiFrameCounts; FILE * mFD; PRBool mDumpFrameCounts; PRBool mDumpFrameByFrameCounts; PRBool mPaintFrameByFrameCounts; PRBool mCycledOnce; // Root Frame for Individual Tracking nsPresContext * mPresContext; nsIPresShell* mPresShell; // ReflowCountMgr gReflowCountMgr; }; #endif //======================================================================== // comment out to hide caret #define SHOW_CARET // The upper bound on the amount of time to spend reflowing, in // microseconds. When this bound is exceeded and reflow commands are // still queued up, a reflow event is posted. The idea is for reflow // to not hog the processor beyond the time specifed in // gMaxRCProcessingTime. This data member is initialized from the // layout.reflow.timeslice pref. #define NS_MAX_REFLOW_TIME 1000000 static PRInt32 gMaxRCProcessingTime = -1; #define MARK_INCREMENT 50 #define BLOCK_INCREMENT 4044 /* a bit under 4096, for malloc overhead */ /**A block of memory that the stack will * chop up and hand out */ struct StackBlock { // a block of memory. Note that this must be first so that it will // be aligned. char mBlock[BLOCK_INCREMENT]; // another block of memory that would only be created // if our stack overflowed. Yes we have the ability // to grow on a stack overflow StackBlock* mNext; StackBlock() : mNext(nsnull) { } ~StackBlock() { } }; /* we hold an array of marks. A push pushes a mark on the stack * a pop pops it off. */ struct StackMark { // the block of memory we are currently handing out chunks of StackBlock* mBlock; // our current position in the memory size_t mPos; }; /* A stack arena allows a stack based interface to a block of memory. * It should be used when you need to allocate some temporary memory that * you will immediately return. */ class StackArena { public: StackArena(); ~StackArena(); nsresult Init() { return mBlocks ? NS_OK : NS_ERROR_OUT_OF_MEMORY; } // Memory management functions void* Allocate(size_t aSize); void Push(); void Pop(); PRUint32 Size() { PRUint32 result = 0; StackBlock *block = mBlocks; while (block) { result += sizeof(StackBlock); block = block->mNext; } return result; } private: // our current position in memory size_t mPos; // a list of memory block. Usually there is only one // but if we overrun our stack size we can get more memory. StackBlock* mBlocks; // the current block of memory we are passing our chucks of StackBlock* mCurBlock; // our stack of mark where push has been called StackMark* mMarks; // the current top of the mark list PRUint32 mStackTop; // the size of the mark array PRUint32 mMarkLength; }; StackArena::StackArena() { mMarkLength = 0; mMarks = nsnull; // allocate our stack memory mBlocks = new StackBlock(); mCurBlock = mBlocks; mStackTop = 0; mPos = 0; } StackArena::~StackArena() { // free up our data delete[] mMarks; while(mBlocks) { StackBlock* toDelete = mBlocks; mBlocks = mBlocks->mNext; delete toDelete; } } void StackArena::Push() { // Resize the mark array if we overrun it. Failure to allocate the // mark array is not fatal; we just won't free to that mark. This // allows callers not to worry about error checking. if (mStackTop >= mMarkLength) { PRUint32 newLength = mStackTop + MARK_INCREMENT; StackMark* newMarks = new StackMark[newLength]; if (newMarks) { if (mMarkLength) memcpy(newMarks, mMarks, sizeof(StackMark)*mMarkLength); // Fill in any marks that we couldn't allocate during a prior call // to Push(). for (; mMarkLength < mStackTop; ++mMarkLength) { NS_NOTREACHED("should only hit this on out-of-memory"); newMarks[mMarkLength].mBlock = mCurBlock; newMarks[mMarkLength].mPos = mPos; } delete [] mMarks; mMarks = newMarks; mMarkLength = newLength; } } // set a mark at the top (if we can) NS_ASSERTION(mStackTop < mMarkLength, "out of memory"); if (mStackTop < mMarkLength) { mMarks[mStackTop].mBlock = mCurBlock; mMarks[mStackTop].mPos = mPos; } mStackTop++; } void* StackArena::Allocate(size_t aSize) { NS_ASSERTION(mStackTop > 0, "Allocate called without Push"); // make sure we are aligned. Beard said 8 was safer then 4. // Round size to multiple of 8 aSize = PR_ROUNDUP(aSize, 8); // if the size makes the stack overflow. Grab another block for the stack if (mPos + aSize >= BLOCK_INCREMENT) { NS_ASSERTION(aSize <= BLOCK_INCREMENT,"Requested memory is greater that our block size!!"); if (mCurBlock->mNext == nsnull) mCurBlock->mNext = new StackBlock(); mCurBlock = mCurBlock->mNext; mPos = 0; } // return the chunk they need. void *result = mCurBlock->mBlock + mPos; mPos += aSize; return result; } void StackArena::Pop() { // pop off the mark NS_ASSERTION(mStackTop > 0, "unmatched pop"); mStackTop--; if (mStackTop >= mMarkLength) { // We couldn't allocate the marks array at the time of the push, so // we don't know where we're freeing to. NS_NOTREACHED("out of memory"); if (mStackTop == 0) { // But we do know if we've completely pushed the stack. mCurBlock = mBlocks; mPos = 0; } return; } #ifdef DEBUG // Mark the "freed" memory with 0xdd to help with debugging of memory // allocation problems. { StackBlock *block = mMarks[mStackTop].mBlock, *block_end = mCurBlock; size_t pos = mMarks[mStackTop].mPos; for (; block != block_end; block = block->mNext, pos = 0) { memset(block->mBlock + pos, 0xdd, sizeof(block->mBlock) - pos); } memset(block->mBlock + pos, 0xdd, mPos - pos); } #endif mCurBlock = mMarks[mStackTop].mBlock; mPos = mMarks[mStackTop].mPos; } struct nsCallbackEventRequest { nsIReflowCallback* callback; nsCallbackEventRequest* next; }; // ---------------------------------------------------------------------------- #define ASSERT_REFLOW_SCHEDULED_STATE() \ NS_ASSERTION(mReflowScheduled == \ GetPresContext()->RefreshDriver()-> \ IsRefreshObserver(this, Flush_Layout), "Unexpected state") class nsPresShellEventCB; class nsAutoCauseReflowNotifier; class PresShell : public nsIPresShell, public nsIViewObserver, public nsStubDocumentObserver, public nsISelectionController, public nsIObserver, public nsSupportsWeakReference, public nsARefreshObserver { public: PresShell(); NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW // nsISupports NS_DECL_ISUPPORTS // nsIPresShell virtual NS_HIDDEN_(nsresult) Init(nsIDocument* aDocument, nsPresContext* aPresContext, nsIViewManager* aViewManager, nsStyleSet* aStyleSet, nsCompatibility aCompatMode); virtual NS_HIDDEN_(void) Destroy(); virtual NS_HIDDEN_(void*) AllocateFrame(nsQueryFrame::FrameIID aCode, size_t aSize); virtual NS_HIDDEN_(void) FreeFrame(nsQueryFrame::FrameIID aCode, void* aChunk); virtual NS_HIDDEN_(void*) AllocateMisc(size_t aSize); virtual NS_HIDDEN_(void) FreeMisc(size_t aSize, void* aChunk); // Dynamic stack memory allocation virtual NS_HIDDEN_(void) PushStackMemory(); virtual NS_HIDDEN_(void) PopStackMemory(); virtual NS_HIDDEN_(void*) AllocateStackMemory(size_t aSize); virtual NS_HIDDEN_(nsresult) SetPreferenceStyleRules(PRBool aForceReflow); NS_IMETHOD GetSelection(SelectionType aType, nsISelection** aSelection); virtual nsISelection* GetCurrentSelection(SelectionType aType); NS_IMETHOD SetDisplaySelection(PRInt16 aToggle); NS_IMETHOD GetDisplaySelection(PRInt16 *aToggle); NS_IMETHOD ScrollSelectionIntoView(SelectionType aType, SelectionRegion aRegion, PRBool aIsSynchronous); NS_IMETHOD RepaintSelection(SelectionType aType); virtual NS_HIDDEN_(void) BeginObservingDocument(); virtual NS_HIDDEN_(void) EndObservingDocument(); virtual NS_HIDDEN_(nsresult) InitialReflow(nscoord aWidth, nscoord aHeight); virtual NS_HIDDEN_(nsresult) ResizeReflow(nscoord aWidth, nscoord aHeight); virtual NS_HIDDEN_(void) StyleChangeReflow(); virtual NS_HIDDEN_(nsIPageSequenceFrame*) GetPageSequenceFrame() const; virtual NS_HIDDEN_(nsIFrame*) GetRealPrimaryFrameFor(nsIContent* aContent) const; virtual NS_HIDDEN_(nsIFrame*) GetPlaceholderFrameFor(nsIFrame* aFrame) const; virtual NS_HIDDEN_(void) FrameNeedsReflow(nsIFrame *aFrame, IntrinsicDirty aIntrinsicDirty, nsFrameState aBitToAdd); virtual NS_HIDDEN_(void) FrameNeedsToContinueReflow(nsIFrame *aFrame); virtual NS_HIDDEN_(void) CancelAllPendingReflows(); virtual NS_HIDDEN_(PRBool) IsSafeToFlush() const; virtual NS_HIDDEN_(void) FlushPendingNotifications(mozFlushType aType); /** * Recreates the frames for a node */ virtual NS_HIDDEN_(nsresult) RecreateFramesFor(nsIContent* aContent); /** * Post a callback that should be handled after reflow has finished. */ virtual NS_HIDDEN_(nsresult) PostReflowCallback(nsIReflowCallback* aCallback); virtual NS_HIDDEN_(void) CancelReflowCallback(nsIReflowCallback* aCallback); virtual NS_HIDDEN_(void) ClearFrameRefs(nsIFrame* aFrame); virtual NS_HIDDEN_(nsresult) CreateRenderingContext(nsIFrame *aFrame, nsIRenderingContext** aContext); virtual NS_HIDDEN_(nsresult) GoToAnchor(const nsAString& aAnchorName, PRBool aScroll); virtual NS_HIDDEN_(nsresult) ScrollToAnchor(); virtual NS_HIDDEN_(nsresult) ScrollContentIntoView(nsIContent* aContent, PRIntn aVPercent, PRIntn aHPercent); virtual PRBool ScrollFrameRectIntoView(nsIFrame* aFrame, const nsRect& aRect, PRIntn aVPercent, PRIntn aHPercent, PRUint32 aFlags); virtual nsRectVisibility GetRectVisibility(nsIFrame *aFrame, const nsRect &aRect, nscoord aMinTwips) const; virtual NS_HIDDEN_(void) SetIgnoreFrameDestruction(PRBool aIgnore); virtual NS_HIDDEN_(void) NotifyDestroyingFrame(nsIFrame* aFrame); virtual NS_HIDDEN_(nsresult) GetLinkLocation(nsIDOMNode* aNode, nsAString& aLocationString) const; virtual NS_HIDDEN_(nsresult) CaptureHistoryState(nsILayoutHistoryState** aLayoutHistoryState, PRBool aLeavingPage); virtual NS_HIDDEN_(void) UnsuppressPainting(); virtual nsresult GetAgentStyleSheets(nsCOMArray& aSheets); virtual nsresult SetAgentStyleSheets(const nsCOMArray& aSheets); virtual nsresult AddOverrideStyleSheet(nsIStyleSheet *aSheet); virtual nsresult RemoveOverrideStyleSheet(nsIStyleSheet *aSheet); virtual NS_HIDDEN_(nsresult) HandleEventWithTarget(nsEvent* aEvent, nsIFrame* aFrame, nsIContent* aContent, nsEventStatus* aStatus); virtual NS_HIDDEN_(nsIFrame*) GetEventTargetFrame(); virtual NS_HIDDEN_(already_AddRefed) GetEventTargetContent(nsEvent* aEvent); virtual nsresult ReconstructFrames(void); virtual void Freeze(); virtual void Thaw(); virtual void FireOrClearDelayedEvents(PRBool aFireEvents); virtual nsIFrame* GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt); virtual NS_HIDDEN_(nsresult) RenderDocument(const nsRect& aRect, PRUint32 aFlags, nscolor aBackgroundColor, gfxContext* aThebesContext); virtual already_AddRefed RenderNode(nsIDOMNode* aNode, nsIntRegion* aRegion, nsIntPoint& aPoint, nsIntRect* aScreenRect); virtual already_AddRefed RenderSelection(nsISelection* aSelection, nsIntPoint& aPoint, nsIntRect* aScreenRect); virtual already_AddRefed GetRootWindow(); //nsIViewObserver interface NS_IMETHOD Paint(nsIView* aDisplayRoot, nsIView* aViewToPaint, nsIWidget* aWidget, const nsRegion& aDirtyRegion, const nsIntRegion& aIntDirtyRegion, PRBool aPaintDefaultBackground, PRBool aWillSendDidPaint); NS_IMETHOD HandleEvent(nsIView* aView, nsGUIEvent* aEvent, nsEventStatus* aEventStatus); virtual NS_HIDDEN_(nsresult) HandleDOMEventWithTarget(nsIContent* aTargetContent, nsEvent* aEvent, nsEventStatus* aStatus); virtual NS_HIDDEN_(nsresult) HandleDOMEventWithTarget(nsIContent* aTargetContent, nsIDOMEvent* aEvent, nsEventStatus* aStatus); NS_IMETHOD ResizeReflow(nsIView *aView, nscoord aWidth, nscoord aHeight); NS_IMETHOD_(PRBool) IsVisible(); NS_IMETHOD_(void) WillPaint(PRBool aWillSendDidPaint); NS_IMETHOD_(void) DidPaint(); NS_IMETHOD_(void) DispatchSynthMouseMove(nsGUIEvent *aEvent, PRBool aFlushOnHoverChange); NS_IMETHOD_(void) ClearMouseCapture(nsIView* aView); // caret handling virtual NS_HIDDEN_(already_AddRefed) GetCaret() const; virtual NS_HIDDEN_(void) MaybeInvalidateCaretPosition(); NS_IMETHOD SetCaretEnabled(PRBool aInEnable); NS_IMETHOD SetCaretReadOnly(PRBool aReadOnly); NS_IMETHOD GetCaretEnabled(PRBool *aOutEnabled); NS_IMETHOD SetCaretVisibilityDuringSelection(PRBool aVisibility); NS_IMETHOD GetCaretVisible(PRBool *_retval); virtual void SetCaret(nsCaret *aNewCaret); virtual void RestoreCaret(); NS_IMETHOD SetSelectionFlags(PRInt16 aInEnable); NS_IMETHOD GetSelectionFlags(PRInt16 *aOutEnable); // nsISelectionController NS_IMETHOD CharacterMove(PRBool aForward, PRBool aExtend); NS_IMETHOD CharacterExtendForDelete(); NS_IMETHOD CharacterExtendForBackspace(); NS_IMETHOD WordMove(PRBool aForward, PRBool aExtend); NS_IMETHOD WordExtendForDelete(PRBool aForward); NS_IMETHOD LineMove(PRBool aForward, PRBool aExtend); NS_IMETHOD IntraLineMove(PRBool aForward, PRBool aExtend); NS_IMETHOD PageMove(PRBool aForward, PRBool aExtend); NS_IMETHOD ScrollPage(PRBool aForward); NS_IMETHOD ScrollLine(PRBool aForward); NS_IMETHOD ScrollHorizontal(PRBool aLeft); NS_IMETHOD CompleteScroll(PRBool aForward); NS_IMETHOD CompleteMove(PRBool aForward, PRBool aExtend); NS_IMETHOD SelectAll(); NS_IMETHOD CheckVisibility(nsIDOMNode *node, PRInt16 startOffset, PRInt16 EndOffset, PRBool *_retval); // nsIDocumentObserver virtual void BeginUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType); virtual void EndUpdate(nsIDocument* aDocument, nsUpdateType aUpdateType); virtual void BeginLoad(nsIDocument* aDocument); virtual void EndLoad(nsIDocument* aDocument); virtual void ContentStatesChanged(nsIDocument* aDocument, nsIContent* aContent1, nsIContent* aContent2, PRInt32 aStateMask); virtual void DocumentStatesChanged(nsIDocument* aDocument, PRInt32 aStateMask); virtual void StyleSheetAdded(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, PRBool aDocumentSheet); virtual void StyleSheetRemoved(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, PRBool aDocumentSheet); virtual void StyleSheetApplicableStateChanged(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, PRBool aApplicable); virtual void StyleRuleChanged(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aOldStyleRule, nsIStyleRule* aNewStyleRule); virtual void StyleRuleAdded(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRule); virtual void StyleRuleRemoved(nsIDocument* aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRule); // nsIMutationObserver NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTEWILLCHANGE NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED NS_DECL_NSIOBSERVER // nsARefreshObserver virtual void WillRefresh(mozilla::TimeStamp aTime); #ifdef MOZ_REFLOW_PERF virtual NS_HIDDEN_(void) DumpReflows(); virtual NS_HIDDEN_(void) CountReflows(const char * aName, nsIFrame * aFrame); virtual NS_HIDDEN_(void) PaintCount(const char * aName, nsIRenderingContext* aRenderingContext, nsPresContext* aPresContext, nsIFrame * aFrame, PRUint32 aColor); virtual NS_HIDDEN_(void) SetPaintFrameCount(PRBool aOn); virtual PRBool IsPaintingFrameCounts(); #endif #ifdef DEBUG virtual void ListStyleContexts(nsIFrame *aRootFrame, FILE *out, PRInt32 aIndent = 0); virtual void ListStyleSheets(FILE *out, PRInt32 aIndent = 0); virtual void VerifyStyleTree(); #endif #ifdef PR_LOGGING static PRLogModuleInfo* gLog; #endif virtual NS_HIDDEN_(void) DisableNonTestMouseEvents(PRBool aDisable); virtual void UpdateCanvasBackground(); virtual nsresult AddCanvasBackgroundColorItem(nsDisplayListBuilder& aBuilder, nsDisplayList& aList, nsIFrame* aFrame, const nsRect& aBounds, nscolor aBackstopColor, PRBool aForceDraw); protected: virtual ~PresShell(); void HandlePostedReflowCallbacks(PRBool aInterruptible); void CancelPostedReflowCallbacks(); void UnsuppressAndInvalidate(); void WillCauseReflow() { nsContentUtils::AddScriptBlocker(); ++mChangeNestCount; } nsresult DidCauseReflow(); friend class nsAutoCauseReflowNotifier; void WillDoReflow(); void DidDoReflow(PRBool aInterruptible); // ProcessReflowCommands returns whether we processed all our dirty roots // without interruptions. PRBool ProcessReflowCommands(PRBool aInterruptible); // MaybeScheduleReflow checks if posting a reflow is needed, then checks if // the last reflow was interrupted. In the interrupted case ScheduleReflow is // called off a timer, otherwise it is called directly. void MaybeScheduleReflow(); // Actually schedules a reflow. This should only be called by // MaybeScheduleReflow and the reflow timer ScheduleReflowOffTimer // sets up. void ScheduleReflow(); // DoReflow returns whether the reflow finished without interruption PRBool DoReflow(nsIFrame* aFrame, PRBool aInterruptible); #ifdef DEBUG void DoVerifyReflow(); void VerifyHasDirtyRootAncestor(nsIFrame* aFrame); #endif // Helper for ScrollContentIntoView void DoScrollContentIntoView(nsIContent* aContent, PRIntn aVPercent, PRIntn aHPercent); friend class nsPresShellEventCB; PRBool mCaretEnabled; #ifdef NS_DEBUG nsresult CloneStyleSet(nsStyleSet* aSet, nsStyleSet** aResult); PRBool VerifyIncrementalReflow(); PRBool mInVerifyReflow; void ShowEventTargetDebug(); #endif /** * methods that manage rules that are used to implement the associated preferences * - initially created for bugs 31816, 20760, 22963 */ nsresult ClearPreferenceStyleRules(void); nsresult CreatePreferenceStyleSheet(void); nsresult SetPrefLinkRules(void); nsresult SetPrefFocusRules(void); nsresult SetPrefNoScriptRule(); nsresult SetPrefNoFramesRule(void); // methods for painting a range to an offscreen buffer // given a display list, clip the items within the list to // the range nsRect ClipListToRange(nsDisplayListBuilder *aBuilder, nsDisplayList* aList, nsIRange* aRange); // create a RangePaintInfo for the range aRange containing the // display list needed to paint the range to a surface RangePaintInfo* CreateRangePaintInfo(nsIDOMRange* aRange, nsRect& aSurfaceRect, PRBool aForPrimarySelection); /* * Paint the items to a new surface and return it. * * aSelection - selection being painted, if any * aRegion - clip region, if any * aArea - area that the surface occupies, relative to the root frame * aPoint - reference point, typically the mouse position * aScreenRect - [out] set to the area of the screen the painted area should * be displayed at */ already_AddRefed PaintRangePaintInfo(nsTArray >* aItems, nsISelection* aSelection, nsIntRegion* aRegion, nsRect aArea, nsIntPoint& aPoint, nsIntRect* aScreenRect); /** * Methods to handle changes to user and UA sheet lists that we get * notified about. */ void AddUserSheet(nsISupports* aSheet); void AddAgentSheet(nsISupports* aSheet); void RemoveSheet(nsStyleSet::sheetType aType, nsISupports* aSheet); // Hide a view if it is a popup void HideViewIfPopup(nsIView* aView); // Utility method to restore the root scrollframe state void RestoreRootScrollPosition(); void MaybeReleaseCapturingContent() { nsCOMPtr frameSelection = FrameSelection(); if (frameSelection) { frameSelection->SetMouseDownState(PR_FALSE); } if (gCaptureInfo.mContent && gCaptureInfo.mContent->GetOwnerDoc() == mDocument) { SetCapturingContent(nsnull, 0); } } nsRefPtr mPrefStyleSheet; // mStyleSet owns it but we // maintain a ref, may be null #ifdef DEBUG PRUint32 mUpdateCount; #endif // reflow roots that need to be reflowed, as both a queue and a hashtable nsTArray mDirtyRoots; PRPackedBool mDocumentLoading; PRPackedBool mIgnoreFrameDestruction; PRPackedBool mHaveShutDown; // This is used to protect ourselves from triggering reflow while in the // middle of frame construction and the like... it really shouldn't be // needed, one hopes, but it is for now. PRUint32 mChangeNestCount; nsIFrame* mCurrentEventFrame; nsCOMPtr mCurrentEventContent; nsTArray mCurrentEventFrameStack; nsCOMArray mCurrentEventContentStack; nsCOMPtr mLastAnchorScrolledTo; nscoord mLastAnchorScrollPositionY; nsRefPtr mCaret; nsRefPtr mOriginalCaret; nsPresArena mFrameArena; StackArena mStackArena; nsCOMPtr mDragService; #ifdef DEBUG // The reflow root under which we're currently reflowing. Null when // not in reflow. nsIFrame* mCurrentReflowRoot; #endif // Set of frames that we should mark with NS_FRAME_HAS_DIRTY_CHILDREN after // we finish reflowing mCurrentReflowRoot. nsTHashtable< nsPtrHashKey > mFramesToDirty; // Information needed to properly handle scrolling content into view if the // pre-scroll reflow flush can be interrupted. mContentToScrollTo is // non-null between the initial scroll attempt and the first time we finish // processing all our dirty roots. mContentScrollVPosition and // mContentScrollHPosition are only used when it's non-null. nsCOMPtr mContentToScrollTo; PRIntn mContentScrollVPosition; PRIntn mContentScrollHPosition; class nsDelayedEvent { public: virtual ~nsDelayedEvent() {}; virtual void Dispatch(PresShell* aShell) {} }; class nsDelayedInputEvent : public nsDelayedEvent { public: virtual void Dispatch(PresShell* aShell) { if (mEvent && mEvent->widget) { nsCOMPtr w = mEvent->widget; nsEventStatus status; w->DispatchEvent(mEvent, status); } } protected: void Init(nsInputEvent* aEvent) { mEvent->time = aEvent->time; mEvent->refPoint = aEvent->refPoint; mEvent->isShift = aEvent->isShift; mEvent->isControl = aEvent->isControl; mEvent->isAlt = aEvent->isAlt; mEvent->isMeta = aEvent->isMeta; } nsDelayedInputEvent() : nsDelayedEvent(), mEvent(nsnull) {} nsInputEvent* mEvent; }; class nsDelayedMouseEvent : public nsDelayedInputEvent { public: nsDelayedMouseEvent(nsMouseEvent* aEvent) : nsDelayedInputEvent() { mEvent = new nsMouseEvent(NS_IS_TRUSTED_EVENT(aEvent), aEvent->message, aEvent->widget, aEvent->reason, aEvent->context); if (mEvent) { Init(aEvent); static_cast(mEvent)->clickCount = aEvent->clickCount; } } virtual ~nsDelayedMouseEvent() { delete static_cast(mEvent); } }; class nsDelayedKeyEvent : public nsDelayedInputEvent { public: nsDelayedKeyEvent(nsKeyEvent* aEvent) : nsDelayedInputEvent() { mEvent = new nsKeyEvent(NS_IS_TRUSTED_EVENT(aEvent), aEvent->message, aEvent->widget); if (mEvent) { Init(aEvent); static_cast(mEvent)->keyCode = aEvent->keyCode; static_cast(mEvent)->charCode = aEvent->charCode; static_cast(mEvent)->alternativeCharCodes = aEvent->alternativeCharCodes; static_cast(mEvent)->isChar = aEvent->isChar; } } virtual ~nsDelayedKeyEvent() { delete static_cast(mEvent); } }; PRPackedBool mNoDelayedMouseEvents; PRPackedBool mNoDelayedKeyEvents; nsTArray > mDelayedEvents; nsCallbackEventRequest* mFirstCallbackEventRequest; nsCallbackEventRequest* mLastCallbackEventRequest; PRPackedBool mSuppressInterruptibleReflows; PRPackedBool mIsDocumentGone; // We've been disconnected from the document. // We will refuse to paint the document until either // (a) our timer fires or (b) all frames are constructed. PRPackedBool mShouldUnsuppressPainting; // Indicates that it is safe to unlock painting once all pending // reflows have been processed. PRPackedBool mReflowScheduled; // If true, we have a reflow scheduled. // Guaranteed to be false if // mReflowContinueTimer is non-null. nsCOMPtr mPaintSuppressionTimer; // This timer controls painting suppression. Until it fires // or all frames are constructed, we won't paint anything but // our background and scrollbars. #define PAINTLOCK_EVENT_DELAY 250 // 250ms. This is actually // pref-controlled, but we use this // value if we fail to get the pref // for any reason. static void sPaintSuppressionCallback(nsITimer* aTimer, void* aPresShell); // A callback for the timer. // At least on Win32 and Mac after interupting a reflow we need to post // the resume reflow event off a timer to avoid event starvation because // posted messages are processed before other messages when the modal // moving/sizing loop is running, see bug 491700 for details. nsCOMPtr mReflowContinueTimer; static void sReflowContinueCallback(nsITimer* aTimer, void* aPresShell); PRBool ScheduleReflowOffTimer(); #ifdef MOZ_REFLOW_PERF ReflowCountMgr * mReflowCountMgr; #endif static PRBool sDisableNonTestMouseEvents; // false if a check should be done for key/ime events that should be // retargeted to the currently focused presshell static PRBool sDontRetargetEvents; private: PRBool InZombieDocument(nsIContent *aContent); already_AddRefed GetParentPresShell(); nsresult RetargetEventToParent(nsGUIEvent* aEvent, nsEventStatus* aEventStatus); //helper funcs for event handling protected: //protected because nsPresShellEventCB needs this. nsIFrame* GetCurrentEventFrame(); private: void PushCurrentEventInfo(nsIFrame* aFrame, nsIContent* aContent); void PopCurrentEventInfo(); nsresult HandleEventInternal(nsEvent* aEvent, nsIView* aView, nsEventStatus *aStatus); nsresult HandlePositionedEvent(nsIView* aView, nsIFrame* aTargetFrame, nsGUIEvent* aEvent, nsEventStatus* aEventStatus); // This returns the focused DOM window under our top level window. // I.e., when we are deactive, this returns the *last* focused DOM window. already_AddRefed GetFocusedDOMWindowInOurWindow(); /* * This and the next two helper methods are used to target and position the * context menu when the keyboard shortcut is used to open it. * * If another menu is open, the context menu is opened relative to the * active menuitem within the menu, or the menu itself if no item is active. * Otherwise, if the caret is visible, the menu is opened near the caret. * Otherwise, if a selectable list such as a listbox is focused, the * current item within the menu is opened relative to this item. * Otherwise, the context menu is opened at the topleft corner of the * view. * * Returns true if the context menu event should fire and false if it should * not. */ PRBool AdjustContextMenuKeyEvent(nsMouseEvent* aEvent); // PRBool PrepareToUseCaretPosition(nsIWidget* aEventWidget, nsIntPoint& aTargetPt); // Get the selected item and coordinates in device pixels relative to root // document's root view for element, first ensuring the element is onscreen void GetCurrentItemAndPositionForElement(nsIDOMElement *aCurrentEl, nsIContent **aTargetToUse, nsIntPoint& aTargetPt, nsIWidget *aRootWidget); void FireResizeEvent(); static void AsyncResizeEventCallback(nsITimer* aTimer, void* aPresShell); nsRevocableEventPtr > mResizeEvent; nsCOMPtr mAsyncResizeEventTimer; PRPackedBool mAsyncResizeTimerIsActive; PRPackedBool mInResize; private: /* * Computes the backstop color for the view: transparent if in a transparent * widget, otherwise the PresContext default background color. This color is * only visible if the contents of the view as a whole are translucent. */ nscolor ComputeBackstopColor(nsIView* aDisplayRoot); #ifdef DEBUG // Ensure that every allocation from the PresArena is eventually freed. PRUint32 mPresArenaAllocCount; #endif public: PRUint32 EstimateMemoryUsed() { PRUint32 result = 0; result += sizeof(PresShell); result += mStackArena.Size(); result += mFrameArena.Size(); return result; } static PLDHashOperator LiveShellSizeEnumerator(PresShellPtrKey *aEntry, void *userArg) { PresShell *aShell = static_cast(aEntry->GetKey()); PRUint32 *val = (PRUint32*)userArg; *val += aShell->EstimateMemoryUsed(); *val += aShell->mPresContext->EstimateMemoryUsed(); return PL_DHASH_NEXT; } static PLDHashOperator LiveShellBidiSizeEnumerator(PresShellPtrKey *aEntry, void *userArg) { PresShell *aShell = static_cast(aEntry->GetKey()); PRUint32 *val = (PRUint32*)userArg; *val += aShell->mPresContext->GetBidiMemoryUsed(); return PL_DHASH_NEXT; } static PRUint32 EstimateShellsMemory(nsTHashtable::Enumerator aEnumerator) { PRUint32 result = 0; sLiveShells->EnumerateEntries(aEnumerator, &result); return result; } static PRInt64 SizeOfLayoutMemoryReporter(void *) { return EstimateShellsMemory(LiveShellSizeEnumerator); } static PRInt64 SizeOfBidiMemoryReporter(void *) { return EstimateShellsMemory(LiveShellBidiSizeEnumerator); } }; class nsAutoCauseReflowNotifier { public: nsAutoCauseReflowNotifier(PresShell* aShell) : mShell(aShell) { mShell->WillCauseReflow(); } ~nsAutoCauseReflowNotifier() { // This check should not be needed. Currently the only place that seem // to need it is the code that deals with bug 337586. if (!mShell->mHaveShutDown) { mShell->DidCauseReflow(); } else { nsContentUtils::RemoveScriptBlocker(); } } PresShell* mShell; }; class NS_STACK_CLASS nsPresShellEventCB : public nsDispatchingCallback { public: nsPresShellEventCB(PresShell* aPresShell) : mPresShell(aPresShell) {} virtual void HandleEvent(nsEventChainPostVisitor& aVisitor) { if (aVisitor.mPresContext && aVisitor.mEvent->eventStructType != NS_EVENT) { nsIFrame* frame = mPresShell->GetCurrentEventFrame(); if (frame) { frame->HandleEvent(aVisitor.mPresContext, (nsGUIEvent*) aVisitor.mEvent, &aVisitor.mEventStatus); } } } nsRefPtr mPresShell; }; PRBool PresShell::sDisableNonTestMouseEvents = PR_FALSE; PRBool PresShell::sDontRetargetEvents = PR_FALSE; #ifdef PR_LOGGING PRLogModuleInfo* PresShell::gLog; #endif #ifdef NS_DEBUG static void VerifyStyleTree(nsPresContext* aPresContext, nsFrameManager* aFrameManager) { if (nsFrame::GetVerifyStyleTreeEnable()) { nsIFrame* rootFrame = aFrameManager->GetRootFrame(); aFrameManager->DebugVerifyStyleTree(rootFrame); } } #define VERIFY_STYLE_TREE ::VerifyStyleTree(mPresContext, FrameManager()) #else #define VERIFY_STYLE_TREE #endif static PRBool gVerifyReflowEnabled; PRBool nsIPresShell::GetVerifyReflowEnable() { #ifdef NS_DEBUG static PRBool firstTime = PR_TRUE; if (firstTime) { firstTime = PR_FALSE; char* flags = PR_GetEnv("GECKO_VERIFY_REFLOW_FLAGS"); if (flags) { PRBool error = PR_FALSE; for (;;) { char* comma = PL_strchr(flags, ','); if (comma) *comma = '\0'; PRBool found = PR_FALSE; const VerifyReflowFlags* flag = gFlags; const VerifyReflowFlags* limit = gFlags + NUM_VERIFY_REFLOW_FLAGS; while (flag < limit) { if (PL_strcasecmp(flag->name, flags) == 0) { gVerifyReflowFlags |= flag->bit; found = PR_TRUE; break; } ++flag; } if (! found) error = PR_TRUE; if (! comma) break; *comma = ','; flags = comma + 1; } if (error) ShowVerifyReflowFlags(); } if (VERIFY_REFLOW_ON & gVerifyReflowFlags) { gVerifyReflowEnabled = PR_TRUE; printf("Note: verifyreflow is enabled"); if (VERIFY_REFLOW_NOISY & gVerifyReflowFlags) { printf(" (noisy)"); } if (VERIFY_REFLOW_ALL & gVerifyReflowFlags) { printf(" (all)"); } if (VERIFY_REFLOW_DUMP_COMMANDS & gVerifyReflowFlags) { printf(" (show reflow commands)"); } if (VERIFY_REFLOW_NOISY_RC & gVerifyReflowFlags) { printf(" (noisy reflow commands)"); if (VERIFY_REFLOW_REALLY_NOISY_RC & gVerifyReflowFlags) { printf(" (REALLY noisy reflow commands)"); } } printf("\n"); } } #endif return gVerifyReflowEnabled; } void nsIPresShell::SetVerifyReflowEnable(PRBool aEnabled) { gVerifyReflowEnabled = aEnabled; } /* virtual */ void nsIPresShell::AddWeakFrameExternal(nsWeakFrame* aWeakFrame) { AddWeakFrameInternal(aWeakFrame); } void nsIPresShell::AddWeakFrameInternal(nsWeakFrame* aWeakFrame) { if (aWeakFrame->GetFrame()) { aWeakFrame->GetFrame()->AddStateBits(NS_FRAME_EXTERNAL_REFERENCE); } aWeakFrame->SetPreviousWeakFrame(mWeakFrames); mWeakFrames = aWeakFrame; } /* virtual */ void nsIPresShell::RemoveWeakFrameExternal(nsWeakFrame* aWeakFrame) { RemoveWeakFrameInternal(aWeakFrame); } void nsIPresShell::RemoveWeakFrameInternal(nsWeakFrame* aWeakFrame) { if (mWeakFrames == aWeakFrame) { mWeakFrames = aWeakFrame->GetPreviousWeakFrame(); return; } nsWeakFrame* nextWeak = mWeakFrames; while (nextWeak && nextWeak->GetPreviousWeakFrame() != aWeakFrame) { nextWeak = nextWeak->GetPreviousWeakFrame(); } if (nextWeak) { nextWeak->SetPreviousWeakFrame(aWeakFrame->GetPreviousWeakFrame()); } } already_AddRefed nsIPresShell::FrameSelection() { NS_IF_ADDREF(mSelection); return mSelection; } //---------------------------------------------------------------------- nsresult NS_NewPresShell(nsIPresShell** aInstancePtrResult) { NS_PRECONDITION(nsnull != aInstancePtrResult, "null ptr"); if (!aInstancePtrResult) return NS_ERROR_NULL_POINTER; *aInstancePtrResult = new PresShell(); if (!*aInstancePtrResult) return NS_ERROR_OUT_OF_MEMORY; NS_ADDREF(*aInstancePtrResult); return NS_OK; } nsTHashtable *nsIPresShell::sLiveShells = 0; NS_MEMORY_REPORTER_IMPLEMENT(LayoutPresShell, "layout/all", "Memory in use by layout PresShell, PresContext, and other related areas.", PresShell::SizeOfLayoutMemoryReporter, nsnull) NS_MEMORY_REPORTER_IMPLEMENT(LayoutBidi, "layout/bidi", "Memory in use by layout Bidi processor.", PresShell::SizeOfBidiMemoryReporter, nsnull) PresShell::PresShell() { mSelection = nsnull; #ifdef MOZ_REFLOW_PERF mReflowCountMgr = new ReflowCountMgr(); mReflowCountMgr->SetPresContext(mPresContext); mReflowCountMgr->SetPresShell(this); #endif #ifdef PR_LOGGING if (! gLog) gLog = PR_NewLogModule("PresShell"); #endif mSelectionFlags = nsISelectionDisplay::DISPLAY_TEXT | nsISelectionDisplay::DISPLAY_IMAGES; mIsThemeSupportDisabled = PR_FALSE; #ifdef DEBUG mPresArenaAllocCount = 0; #endif static bool registeredReporter = false; if (!registeredReporter) { NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(LayoutPresShell)); NS_RegisterMemoryReporter(new NS_MEMORY_REPORTER_NAME(LayoutBidi)); registeredReporter = true; } new (this) nsFrameManager(); sLiveShells->PutEntry(this); } NS_IMPL_ISUPPORTS8(PresShell, nsIPresShell, nsIDocumentObserver, nsIViewObserver, nsISelectionController, nsISelectionDisplay, nsIObserver, nsISupportsWeakReference, nsIMutationObserver) PresShell::~PresShell() { sLiveShells->RemoveEntry(this); if (!mHaveShutDown) { NS_NOTREACHED("Someone did not call nsIPresShell::destroy"); Destroy(); } NS_ASSERTION(mCurrentEventContentStack.Count() == 0, "Huh, event content left on the stack in pres shell dtor!"); NS_ASSERTION(mFirstCallbackEventRequest == nsnull && mLastCallbackEventRequest == nsnull, "post-reflow queues not empty. This means we're leaking"); #ifdef DEBUG NS_ASSERTION(mPresArenaAllocCount == 0, "Some pres arena objects were not freed"); #endif delete mStyleSet; NS_IF_RELEASE(mFrameConstructor); mCurrentEventContent = nsnull; NS_IF_RELEASE(mPresContext); NS_IF_RELEASE(mDocument); NS_IF_RELEASE(mSelection); } /** * Initialize the presentation shell. Create view manager and style * manager. */ nsresult PresShell::Init(nsIDocument* aDocument, nsPresContext* aPresContext, nsIViewManager* aViewManager, nsStyleSet* aStyleSet, nsCompatibility aCompatMode) { NS_TIME_FUNCTION_MIN(1.0); NS_PRECONDITION(nsnull != aDocument, "null ptr"); NS_PRECONDITION(nsnull != aPresContext, "null ptr"); NS_PRECONDITION(nsnull != aViewManager, "null ptr"); nsresult result; if ((nsnull == aDocument) || (nsnull == aPresContext) || (nsnull == aViewManager)) { return NS_ERROR_NULL_POINTER; } if (mDocument) { NS_WARNING("PresShell double init'ed"); return NS_ERROR_ALREADY_INITIALIZED; } result = mStackArena.Init(); NS_ENSURE_SUCCESS(result, result); if (!mFramesToDirty.Init()) { return NS_ERROR_OUT_OF_MEMORY; } mDocument = aDocument; NS_ADDREF(mDocument); mViewManager = aViewManager; // Create our frame constructor. mFrameConstructor = new nsCSSFrameConstructor(mDocument, this); NS_ENSURE_TRUE(mFrameConstructor, NS_ERROR_OUT_OF_MEMORY); NS_ADDREF(mFrameConstructor); // The document viewer owns both view manager and pres shell. mViewManager->SetViewObserver(this); // Bind the context to the presentation shell. mPresContext = aPresContext; NS_ADDREF(mPresContext); aPresContext->SetShell(this); // Now we can initialize the style set. result = aStyleSet->Init(aPresContext); NS_ENSURE_SUCCESS(result, result); // From this point on, any time we return an error we need to make // sure to null out mStyleSet first, since an error return from this // method will cause the caller to delete the style set, so we don't // want to delete it in our destructor. mStyleSet = aStyleSet; // Notify our prescontext that it now has a compatibility mode. Note that // this MUST happen after we set up our style set but before we create any // frames. mPresContext->CompatibilityModeChanged(); // setup the preference style rules (no forced reflow), and do it // before creating any frames. SetPreferenceStyleRules(PR_FALSE); result = CallCreateInstance(kFrameSelectionCID, &mSelection); if (NS_FAILED(result)) { mStyleSet = nsnull; return result; } // Create and initialize the frame manager result = FrameManager()->Init(this, mStyleSet); if (NS_FAILED(result)) { NS_WARNING("Frame manager initialization failed"); mStyleSet = nsnull; return result; } mSelection->Init(this, nsnull); // Important: this has to happen after the selection has been set up #ifdef SHOW_CARET // make the caret nsresult err = NS_NewCaret(getter_AddRefs(mCaret)); if (NS_SUCCEEDED(err)) { mCaret->Init(this); mOriginalCaret = mCaret; } //SetCaretEnabled(PR_TRUE); // make it show in browser windows #endif //set up selection to be displayed in document // Don't enable selection for print media nsPresContext::nsPresContextType type = aPresContext->Type(); if (type != nsPresContext::eContext_PrintPreview && type != nsPresContext::eContext_Print) SetDisplaySelection(nsISelectionController::SELECTION_DISABLED); if (gMaxRCProcessingTime == -1) { gMaxRCProcessingTime = nsContentUtils::GetIntPref("layout.reflow.timeslice", NS_MAX_REFLOW_TIME); } { nsCOMPtr os = mozilla::services::GetObserverService(); if (os) { os->AddObserver(this, "agent-sheet-added", PR_FALSE); os->AddObserver(this, "user-sheet-added", PR_FALSE); os->AddObserver(this, "agent-sheet-removed", PR_FALSE); os->AddObserver(this, "user-sheet-removed", PR_FALSE); #ifdef MOZ_XUL os->AddObserver(this, "chrome-flush-skin-caches", PR_FALSE); #endif #ifdef ACCESSIBILITY os->AddObserver(this, "a11y-init-or-shutdown", PR_FALSE); #endif } } // cache the drag service so we can check it during reflows mDragService = do_GetService("@mozilla.org/widget/dragservice;1"); #ifdef MOZ_REFLOW_PERF if (mReflowCountMgr) { PRBool paintFrameCounts = nsContentUtils::GetBoolPref("layout.reflow.showframecounts"); PRBool dumpFrameCounts = nsContentUtils::GetBoolPref("layout.reflow.dumpframecounts"); PRBool dumpFrameByFrameCounts = nsContentUtils::GetBoolPref("layout.reflow.dumpframebyframecounts"); mReflowCountMgr->SetDumpFrameCounts(dumpFrameCounts); mReflowCountMgr->SetDumpFrameByFrameCounts(dumpFrameByFrameCounts); mReflowCountMgr->SetPaintFrameCounts(paintFrameCounts); } #endif #ifdef MOZ_SMIL if (mDocument->HasAnimationController()) { nsSMILAnimationController* animCtrl = mDocument->GetAnimationController(); if (!animCtrl->IsPaused()) { animCtrl->StartSampling(GetPresContext()->RefreshDriver()); } } #endif // MOZ_SMIL return NS_OK; } void PresShell::Destroy() { NS_TIME_FUNCTION_MIN(1.0); NS_ASSERTION(!nsContentUtils::IsSafeToRunScript(), "destroy called on presshell while scripts not blocked"); #ifdef MOZ_REFLOW_PERF DumpReflows(); if (mReflowCountMgr) { delete mReflowCountMgr; mReflowCountMgr = nsnull; } #endif if (mHaveShutDown) return; #ifdef ACCESSIBILITY if (gIsAccessibilityActive) { nsCOMPtr accService = do_GetService("@mozilla.org/accessibilityService;1"); if (accService) { accService->PresShellDestroyed(this); } } #endif // ACCESSIBILITY MaybeReleaseCapturingContent(); mContentToScrollTo = nsnull; if (mPresContext) { // We need to notify the destroying the nsPresContext to ESM for // suppressing to use from ESM. mPresContext->EventStateManager()->NotifyDestroyPresContext(mPresContext); } { nsCOMPtr os = mozilla::services::GetObserverService(); if (os) { os->RemoveObserver(this, "agent-sheet-added"); os->RemoveObserver(this, "user-sheet-added"); os->RemoveObserver(this, "agent-sheet-removed"); os->RemoveObserver(this, "user-sheet-removed"); #ifdef MOZ_XUL os->RemoveObserver(this, "chrome-flush-skin-caches"); #endif #ifdef ACCESSIBILITY os->RemoveObserver(this, "a11y-init-or-shutdown"); #endif } } // If our paint suppression timer is still active, kill it. if (mPaintSuppressionTimer) { mPaintSuppressionTimer->Cancel(); mPaintSuppressionTimer = nsnull; } // Same for our reflow continuation timer if (mReflowContinueTimer) { mReflowContinueTimer->Cancel(); mReflowContinueTimer = nsnull; } if (mCaret) { mCaret->Terminate(); mCaret = nsnull; } if (mSelection) { mSelection->DisconnectFromPresShell(); } // release our pref style sheet, if we have one still ClearPreferenceStyleRules(); mIsDestroying = PR_TRUE; // We can't release all the event content in // mCurrentEventContentStack here since there might be code on the // stack that will release the event content too. Double release // bad! // The frames will be torn down, so remove them from the current // event frame stack (since they'd be dangling references if we'd // leave them in) and null out the mCurrentEventFrame pointer as // well. mCurrentEventFrame = nsnull; PRInt32 i, count = mCurrentEventFrameStack.Length(); for (i = 0; i < count; i++) { mCurrentEventFrameStack[i] = nsnull; } mFramesToDirty.Clear(); if (mViewManager) { // Clear the view manager's weak pointer back to |this| in case it // was leaked. mViewManager->SetViewObserver(nsnull); mViewManager = nsnull; } mStyleSet->BeginShutdown(mPresContext); nsRefreshDriver* rd = GetPresContext()->RefreshDriver(); // This shell must be removed from the document before the frame // hierarchy is torn down to avoid finding deleted frames through // this presshell while the frames are being torn down if (mDocument) { NS_ASSERTION(mDocument->GetShell() == this, "Wrong shell?"); mDocument->DeleteShell(); #ifdef MOZ_SMIL if (mDocument->HasAnimationController()) { mDocument->GetAnimationController()->StopSampling(rd); } #endif // MOZ_SMIL } // Revoke any pending events. We need to do this and cancel pending reflows // before we destroy the frame manager, since apparently frame destruction // sometimes spins the event queue when plug-ins are involved(!). rd->RemoveRefreshObserver(this, Flush_Layout); mResizeEvent.Revoke(); if (mAsyncResizeTimerIsActive) { mAsyncResizeEventTimer->Cancel(); mAsyncResizeTimerIsActive = PR_FALSE; } CancelAllPendingReflows(); CancelPostedReflowCallbacks(); // Destroy the frame manager. This will destroy the frame hierarchy mFrameConstructor->WillDestroyFrameTree(); FrameManager()->Destroy(); // Destroy all frame properties (whose destruction was suppressed // while destroying the frame tree, but which might contain more // frames within the properties. if (mPresContext) { // Clear out the prescontext's property table -- since our frame tree is // now dead, we shouldn't be looking up any more properties in that table. // We want to do this before we call SetShell() on the prescontext, so // property destructors can usefully call GetPresShell() on the // prescontext. mPresContext->PropertyTable()->DeleteAll(); } NS_WARN_IF_FALSE(!mWeakFrames, "Weak frames alive after destroying FrameManager"); while (mWeakFrames) { mWeakFrames->Clear(this); } // Let the style set do its cleanup. mStyleSet->Shutdown(mPresContext); if (mPresContext) { // We hold a reference to the pres context, and it holds a weak link back // to us. To avoid the pres context having a dangling reference, set its // pres shell to NULL mPresContext->SetShell(nsnull); // Clear the link handler (weak reference) as well mPresContext->SetLinkHandler(nsnull); } mHaveShutDown = PR_TRUE; } // Dynamic stack memory allocation /* virtual */ void PresShell::PushStackMemory() { mStackArena.Push(); } /* virtual */ void PresShell::PopStackMemory() { mStackArena.Pop(); } /* virtual */ void* PresShell::AllocateStackMemory(size_t aSize) { return mStackArena.Allocate(aSize); } void PresShell::FreeFrame(nsQueryFrame::FrameIID aCode, void* aPtr) { #ifdef DEBUG mPresArenaAllocCount--; #endif if (PRESARENA_MUST_FREE_DURING_DESTROY || !mIsDestroying) mFrameArena.FreeByCode(aCode, aPtr); } void* PresShell::AllocateFrame(nsQueryFrame::FrameIID aCode, size_t aSize) { #ifdef DEBUG mPresArenaAllocCount++; #endif void* result = mFrameArena.AllocateByCode(aCode, aSize); if (result) { memset(result, 0, aSize); } return result; } void PresShell::FreeMisc(size_t aSize, void* aPtr) { #ifdef DEBUG mPresArenaAllocCount--; #endif if (PRESARENA_MUST_FREE_DURING_DESTROY || !mIsDestroying) mFrameArena.FreeBySize(aSize, aPtr); } void* PresShell::AllocateMisc(size_t aSize) { #ifdef DEBUG mPresArenaAllocCount++; #endif return mFrameArena.AllocateBySize(aSize); } void nsIPresShell::SetAuthorStyleDisabled(PRBool aStyleDisabled) { if (aStyleDisabled != mStyleSet->GetAuthorStyleDisabled()) { mStyleSet->SetAuthorStyleDisabled(aStyleDisabled); ReconstructStyleData(); } } PRBool nsIPresShell::GetAuthorStyleDisabled() const { return mStyleSet->GetAuthorStyleDisabled(); } nsresult PresShell::SetPreferenceStyleRules(PRBool aForceReflow) { NS_TIME_FUNCTION_MIN(1.0); if (!mDocument) { return NS_ERROR_NULL_POINTER; } nsPIDOMWindow *window = mDocument->GetWindow(); // If the document doesn't have a window there's no need to notify // its presshell about changes to preferences since the document is // in a state where it doesn't matter any more (see // DocumentViewerImpl::Close()). if (!window) { return NS_ERROR_NULL_POINTER; } NS_PRECONDITION(mPresContext, "presContext cannot be null"); if (mPresContext) { // first, make sure this is not a chrome shell if (nsContentUtils::IsInChromeDocshell(mDocument)) { return NS_OK; } #ifdef DEBUG_attinasi printf("Setting Preference Style Rules:\n"); #endif // if here, we need to create rules for the prefs // - this includes the background-color, the text-color, // the link color, the visited link color and the link-underlining // first clear any exising rules nsresult result = ClearPreferenceStyleRules(); // now the link rules (must come after the color rules, or links will not be correct color!) // XXX - when there is both an override and agent pref stylesheet this won't matter, // as the color rules will be overrides and the links rules will be agent if (NS_SUCCEEDED(result)) { result = SetPrefLinkRules(); } if (NS_SUCCEEDED(result)) { result = SetPrefFocusRules(); } if (NS_SUCCEEDED(result)) { result = SetPrefNoScriptRule(); } if (NS_SUCCEEDED(result)) { result = SetPrefNoFramesRule(); } #ifdef DEBUG_attinasi printf( "Preference Style Rules set: error=%ld\n", (long)result); #endif // Note that this method never needs to force any calculation; the caller // will recalculate style if needed return result; } return NS_ERROR_NULL_POINTER; } nsresult PresShell::ClearPreferenceStyleRules(void) { nsresult result = NS_OK; if (mPrefStyleSheet) { NS_ASSERTION(mStyleSet, "null styleset entirely unexpected!"); if (mStyleSet) { // remove the sheet from the styleset: // - note that we have to check for success by comparing the count before and after... #ifdef NS_DEBUG PRInt32 numBefore = mStyleSet->SheetCount(nsStyleSet::eUserSheet); NS_ASSERTION(numBefore > 0, "no user stylesheets in styleset, but we have one!"); #endif mStyleSet->RemoveStyleSheet(nsStyleSet::eUserSheet, mPrefStyleSheet); #ifdef DEBUG_attinasi NS_ASSERTION((numBefore - 1) == mStyleSet->GetNumberOfUserStyleSheets(), "Pref stylesheet was not removed"); printf("PrefStyleSheet removed\n"); #endif // clear the sheet pointer: it is strictly historical now mPrefStyleSheet = nsnull; } } return result; } nsresult PresShell::CreatePreferenceStyleSheet(void) { NS_TIME_FUNCTION_MIN(1.0); NS_ASSERTION(!mPrefStyleSheet, "prefStyleSheet already exists"); nsresult result = NS_NewCSSStyleSheet(getter_AddRefs(mPrefStyleSheet)); if (NS_SUCCEEDED(result)) { NS_ASSERTION(mPrefStyleSheet, "null but no error"); nsCOMPtr uri; result = NS_NewURI(getter_AddRefs(uri), "about:PreferenceStyleSheet", nsnull); if (NS_SUCCEEDED(result)) { NS_ASSERTION(uri, "null but no error"); mPrefStyleSheet->SetURIs(uri, uri, uri); mPrefStyleSheet->SetComplete(); PRUint32 index; result = mPrefStyleSheet->InsertRuleInternal(NS_LITERAL_STRING("@namespace url(http://www.w3.org/1999/xhtml);"), 0, &index); if (NS_SUCCEEDED(result)) { mStyleSet->AppendStyleSheet(nsStyleSet::eUserSheet, mPrefStyleSheet); } } } #ifdef DEBUG_attinasi printf("CreatePrefStyleSheet completed: error=%ld\n",(long)result); #endif if (NS_FAILED(result)) { mPrefStyleSheet = nsnull; } return result; } // XXX We want these after the @namespace rule. Does order matter // for these rules, or can we call nsICSSStyleRule::StyleRuleCount() // and just "append"? static PRUint32 sInsertPrefSheetRulesAt = 1; nsresult PresShell::SetPrefNoScriptRule() { NS_TIME_FUNCTION_MIN(1.0); nsresult rv = NS_OK; // also handle the case where print is done from print preview // see bug #342439 for more details nsIDocument* doc = mDocument; if (mPresContext->Type() == nsPresContext::eContext_PrintPreview || mPresContext->Type() == nsPresContext::eContext_Print) { while (doc->GetOriginalDocument()) { doc = doc->GetOriginalDocument(); } } PRBool scriptEnabled = doc->IsScriptEnabled(); if (scriptEnabled) { if (!mPrefStyleSheet) { rv = CreatePreferenceStyleSheet(); NS_ENSURE_SUCCESS(rv, rv); } PRUint32 index = 0; mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("noscript{display:none!important}"), sInsertPrefSheetRulesAt, &index); } return rv; } nsresult PresShell::SetPrefNoFramesRule(void) { NS_TIME_FUNCTION_MIN(1.0); NS_ASSERTION(mPresContext,"null prescontext not allowed"); if (!mPresContext) { return NS_ERROR_FAILURE; } nsresult rv = NS_OK; if (!mPrefStyleSheet) { rv = CreatePreferenceStyleSheet(); NS_ENSURE_SUCCESS(rv, rv); } NS_ASSERTION(mPrefStyleSheet, "prefstylesheet should not be null"); PRBool allowSubframes = PR_TRUE; nsCOMPtr container = mPresContext->GetContainer(); nsCOMPtr docShell(do_QueryInterface(container)); if (docShell) { docShell->GetAllowSubframes(&allowSubframes); } if (!allowSubframes) { PRUint32 index = 0; rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("noframes{display:block}"), sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(rv, rv); rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("frame, frameset, iframe {display:none!important}"), sInsertPrefSheetRulesAt, &index); } return rv; } nsresult PresShell::SetPrefLinkRules(void) { NS_TIME_FUNCTION_MIN(1.0); NS_ASSERTION(mPresContext,"null prescontext not allowed"); if (!mPresContext) { return NS_ERROR_FAILURE; } nsresult rv = NS_OK; if (!mPrefStyleSheet) { rv = CreatePreferenceStyleSheet(); NS_ENSURE_SUCCESS(rv, rv); } NS_ASSERTION(mPrefStyleSheet, "prefstylesheet should not be null"); // support default link colors: // this means the link colors need to be overridable, // which they are if we put them in the agent stylesheet, // though if using an override sheet this will cause authors grief still // In the agent stylesheet, they are !important when we are ignoring document colors nscolor linkColor(mPresContext->DefaultLinkColor()); nscolor activeColor(mPresContext->DefaultActiveLinkColor()); nscolor visitedColor(mPresContext->DefaultVisitedLinkColor()); NS_NAMED_LITERAL_STRING(ruleClose, "}"); PRUint32 index = 0; nsAutoString strColor; // insert a rule to color links: '*|*:link {color: #RRGGBB [!important];}' ColorToString(linkColor, strColor); rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("*|*:link{color:") + strColor + ruleClose, sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(rv, rv); // - visited links: '*|*:visited {color: #RRGGBB [!important];}' ColorToString(visitedColor, strColor); rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("*|*:visited{color:") + strColor + ruleClose, sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(rv, rv); // - active links: '*|*:-moz-any-link:active {color: #RRGGBB [!important];}' ColorToString(activeColor, strColor); rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("*|*:-moz-any-link:active{color:") + strColor + ruleClose, sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(rv, rv); PRBool underlineLinks = mPresContext->GetCachedBoolPref(kPresContext_UnderlineLinks); if (underlineLinks) { // create a rule to make underlining happen // '*|*:-moz-any-link {text-decoration:[underline|none];}' // no need for important, we want these to be overridable // NOTE: these must go in the agent stylesheet or they cannot be // overridden by authors rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("*|*:-moz-any-link{text-decoration:underline}"), sInsertPrefSheetRulesAt, &index); } else { rv = mPrefStyleSheet-> InsertRuleInternal(NS_LITERAL_STRING("*|*:-moz-any-link{text-decoration:none}"), sInsertPrefSheetRulesAt, &index); } return rv; } nsresult PresShell::SetPrefFocusRules(void) { NS_TIME_FUNCTION_MIN(1.0); NS_ASSERTION(mPresContext,"null prescontext not allowed"); nsresult result = NS_OK; if (!mPresContext) result = NS_ERROR_FAILURE; if (NS_SUCCEEDED(result) && !mPrefStyleSheet) result = CreatePreferenceStyleSheet(); if (NS_SUCCEEDED(result)) { NS_ASSERTION(mPrefStyleSheet, "prefstylesheet should not be null"); if (mPresContext->GetUseFocusColors()) { nscolor focusBackground(mPresContext->FocusBackgroundColor()); nscolor focusText(mPresContext->FocusTextColor()); // insert a rule to make focus the preferred color PRUint32 index = 0; nsAutoString strRule, strColor; /////////////////////////////////////////////////////////////// // - focus: '*:focus ColorToString(focusText,strColor); strRule.AppendLiteral("*:focus,*:focus>font {color: "); strRule.Append(strColor); strRule.AppendLiteral(" !important; background-color: "); ColorToString(focusBackground,strColor); strRule.Append(strColor); strRule.AppendLiteral(" !important; } "); // insert the rules result = mPrefStyleSheet-> InsertRuleInternal(strRule, sInsertPrefSheetRulesAt, &index); } PRUint8 focusRingWidth = mPresContext->FocusRingWidth(); PRBool focusRingOnAnything = mPresContext->GetFocusRingOnAnything(); PRUint8 focusRingStyle = mPresContext->GetFocusRingStyle(); if ((NS_SUCCEEDED(result) && focusRingWidth != 1 && focusRingWidth <= 4 ) || focusRingOnAnything) { PRUint32 index = 0; nsAutoString strRule; if (!focusRingOnAnything) strRule.AppendLiteral("*|*:link:focus, *|*:visited"); // If we only want focus rings on the normal things like links strRule.AppendLiteral(":focus {outline: "); // For example 3px dotted WindowText (maximum 4) strRule.AppendInt(focusRingWidth); if (focusRingStyle == 0) // solid strRule.AppendLiteral("px solid -moz-mac-focusring !important; -moz-outline-radius: 3px; outline-offset: 1px; } "); else // dotted strRule.AppendLiteral("px dotted WindowText !important; } "); // insert the rules result = mPrefStyleSheet-> InsertRuleInternal(strRule, sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(result, result); if (focusRingWidth != 1) { // If the focus ring width is different from the default, fix buttons with rings strRule.AssignLiteral("button::-moz-focus-inner, input[type=\"reset\"]::-moz-focus-inner,"); strRule.AppendLiteral("input[type=\"button\"]::-moz-focus-inner, "); strRule.AppendLiteral("input[type=\"submit\"]::-moz-focus-inner { padding: 1px 2px 1px 2px; border: "); strRule.AppendInt(focusRingWidth); if (focusRingStyle == 0) // solid strRule.AppendLiteral("px solid transparent !important; } "); else strRule.AppendLiteral("px dotted transparent !important; } "); result = mPrefStyleSheet-> InsertRuleInternal(strRule, sInsertPrefSheetRulesAt, &index); NS_ENSURE_SUCCESS(result, result); strRule.AssignLiteral("button:focus::-moz-focus-inner, input[type=\"reset\"]:focus::-moz-focus-inner,"); strRule.AppendLiteral("input[type=\"button\"]:focus::-moz-focus-inner, input[type=\"submit\"]:focus::-moz-focus-inner {"); strRule.AppendLiteral("border-color: ButtonText !important; }"); result = mPrefStyleSheet-> InsertRuleInternal(strRule, sInsertPrefSheetRulesAt, &index); } } } return result; } void PresShell::AddUserSheet(nsISupports* aSheet) { // Make sure this does what DocumentViewerImpl::CreateStyleSet does wrt // ordering. We want this new sheet to come after all the existing stylesheet // service sheets, but before other user sheets; see nsIStyleSheetService.idl // for the ordering. Just remove and readd all the nsStyleSheetService // sheets. nsCOMPtr dummy = do_GetService(NS_STYLESHEETSERVICE_CONTRACTID); mStyleSet->BeginUpdate(); nsStyleSheetService *sheetService = nsStyleSheetService::gInstance; nsCOMArray & userSheets = *sheetService->UserStyleSheets(); PRInt32 i; // Iterate forwards when removing so the searches for RemoveStyleSheet are as // short as possible. for (i = 0; i < userSheets.Count(); ++i) { mStyleSet->RemoveStyleSheet(nsStyleSet::eUserSheet, userSheets[i]); } // Now iterate backwards, so that the order of userSheets will be the same as // the order of sheets from it in the style set. for (i = userSheets.Count() - 1; i >= 0; --i) { mStyleSet->PrependStyleSheet(nsStyleSet::eUserSheet, userSheets[i]); } mStyleSet->EndUpdate(); ReconstructStyleData(); } void PresShell::AddAgentSheet(nsISupports* aSheet) { // Make sure this does what DocumentViewerImpl::CreateStyleSet does // wrt ordering. nsCOMPtr sheet = do_QueryInterface(aSheet); if (!sheet) { return; } mStyleSet->AppendStyleSheet(nsStyleSet::eAgentSheet, sheet); ReconstructStyleData(); } void PresShell::RemoveSheet(nsStyleSet::sheetType aType, nsISupports* aSheet) { nsCOMPtr sheet = do_QueryInterface(aSheet); if (!sheet) { return; } mStyleSet->RemoveStyleSheet(aType, sheet); ReconstructStyleData(); } NS_IMETHODIMP PresShell::SetDisplaySelection(PRInt16 aToggle) { mSelection->SetDisplaySelection(aToggle); return NS_OK; } NS_IMETHODIMP PresShell::GetDisplaySelection(PRInt16 *aToggle) { *aToggle = mSelection->GetDisplaySelection(); return NS_OK; } NS_IMETHODIMP PresShell::GetSelection(SelectionType aType, nsISelection **aSelection) { if (!aSelection || !mSelection) return NS_ERROR_NULL_POINTER; *aSelection = mSelection->GetSelection(aType); if (!(*aSelection)) return NS_ERROR_INVALID_ARG; NS_ADDREF(*aSelection); return NS_OK; } nsISelection* PresShell::GetCurrentSelection(SelectionType aType) { if (!mSelection) return nsnull; return mSelection->GetSelection(aType); } NS_IMETHODIMP PresShell::ScrollSelectionIntoView(SelectionType aType, SelectionRegion aRegion, PRBool aIsSynchronous) { if (!mSelection) return NS_ERROR_NULL_POINTER; return mSelection->ScrollSelectionIntoView(aType, aRegion, aIsSynchronous); } NS_IMETHODIMP PresShell::RepaintSelection(SelectionType aType) { if (!mSelection) return NS_ERROR_NULL_POINTER; return mSelection->RepaintSelection(aType); } // Make shell be a document observer void PresShell::BeginObservingDocument() { if (mDocument && !mIsDestroying) { mDocument->AddObserver(this); if (mIsDocumentGone) { NS_WARNING("Adding a presshell that was disconnected from the document " "as a document observer? Sounds wrong..."); mIsDocumentGone = PR_FALSE; } } } // Make shell stop being a document observer void PresShell::EndObservingDocument() { // XXXbz do we need to tell the frame constructor that the document // is gone, perhaps? Except for printing it's NOT gone, sometimes. mIsDocumentGone = PR_TRUE; if (mDocument) { mDocument->RemoveObserver(this); } } #ifdef DEBUG_kipp char* nsPresShell_ReflowStackPointerTop; #endif nsresult PresShell::InitialReflow(nscoord aWidth, nscoord aHeight) { if (mIsDestroying) { return NS_OK; } if (!mDocument) { // Nothing to do return NS_OK; } NS_TIME_FUNCTION_WITH_DOCURL; NS_ASSERTION(!mDidInitialReflow, "Why are we being called?"); nsCOMPtr kungFuDeathGrip(this); mDidInitialReflow = PR_TRUE; #ifdef NS_DEBUG if (VERIFY_REFLOW_NOISY_RC & gVerifyReflowFlags) { if (mDocument) { nsIURI *uri = mDocument->GetDocumentURI(); if (uri) { nsCAutoString url; uri->GetSpec(url); printf("*** PresShell::InitialReflow (this=%p, url='%s')\n", (void*)this, url.get()); } } } #endif if (mCaret) mCaret->EraseCaret(); // XXX Do a full invalidate at the beginning so that invalidates along // the way don't have region accumulation issues? mPresContext->SetVisibleArea(nsRect(0, 0, aWidth, aHeight)); // Get the root frame from the frame manager // XXXbz it would be nice to move this somewhere else... like frame manager // Init(), say. But we need to make sure our views are all set up by the // time we do this! nsIFrame* rootFrame = FrameManager()->GetRootFrame(); NS_ASSERTION(!rootFrame, "How did that happen, exactly?"); if (!rootFrame) { nsAutoScriptBlocker scriptBlocker; mFrameConstructor->BeginUpdate(); mFrameConstructor->ConstructRootFrame(&rootFrame); FrameManager()->SetRootFrame(rootFrame); mFrameConstructor->EndUpdate(); } NS_ENSURE_STATE(!mHaveShutDown); if (!rootFrame) { return NS_ERROR_OUT_OF_MEMORY; } Element *root = mDocument->GetRootElement(); if (root) { { nsAutoCauseReflowNotifier reflowNotifier(this); mFrameConstructor->BeginUpdate(); // Have the style sheet processor construct frame for the root // content object down mFrameConstructor->ContentInserted(nsnull, root, nsnull, PR_FALSE); VERIFY_STYLE_TREE; // Something in mFrameConstructor->ContentInserted may have caused // Destroy() to get called, bug 337586. NS_ENSURE_STATE(!mHaveShutDown); mFrameConstructor->EndUpdate(); } // nsAutoScriptBlocker going out of scope may have killed us too NS_ENSURE_STATE(!mHaveShutDown); // Run the XBL binding constructors for any new frames we've constructed mDocument->BindingManager()->ProcessAttachedQueue(); NS_TIME_FUNCTION_MARK("XBL binding constructors fired"); // Constructors may have killed us too NS_ENSURE_STATE(!mHaveShutDown); // Now flush out pending restyles before we actually reflow, in // case XBL constructors changed styles somewhere. { nsAutoScriptBlocker scriptBlocker; mFrameConstructor->CreateNeededFrames(); mFrameConstructor->ProcessPendingRestyles(); } // And that might have run _more_ XBL constructors NS_ENSURE_STATE(!mHaveShutDown); } NS_ASSERTION(rootFrame, "How did that happen?"); // Note: Because the frame just got created, it has the NS_FRAME_IS_DIRTY // bit set. Unset it so that FrameNeedsReflow() will work right. NS_ASSERTION(!mDirtyRoots.Contains(rootFrame), "Why is the root in mDirtyRoots already?"); rootFrame->RemoveStateBits(NS_FRAME_IS_DIRTY | NS_FRAME_HAS_DIRTY_CHILDREN); FrameNeedsReflow(rootFrame, eResize, NS_FRAME_IS_DIRTY); NS_ASSERTION(mDirtyRoots.Contains(rootFrame), "Should be in mDirtyRoots now"); NS_ASSERTION(mReflowScheduled, "Why no reflow scheduled?"); // Restore our root scroll position now if we're getting here after EndLoad // got called, since this is our one chance to do it. Note that we need not // have reflowed for this to work; when the scrollframe is finally reflowed // it'll puick up the position we store in it here. if (!mDocumentLoading) { RestoreRootScrollPosition(); } // For printing, we just immediately unsuppress. if (!mPresContext->IsPaginated()) { // Kick off a one-shot timer based off our pref value. When this timer // fires, if painting is still locked down, then we will go ahead and // trigger a full invalidate and allow painting to proceed normally. mPaintingSuppressed = PR_TRUE; mPaintSuppressionTimer = do_CreateInstance("@mozilla.org/timer;1"); if (!mPaintSuppressionTimer) // Uh-oh. We must be out of memory. No point in keeping painting locked down. mPaintingSuppressed = PR_FALSE; else { // Initialize the timer. // Default to PAINTLOCK_EVENT_DELAY if we can't get the pref value. PRInt32 delay = nsContentUtils::GetIntPref("nglayout.initialpaint.delay", PAINTLOCK_EVENT_DELAY); mPaintSuppressionTimer->InitWithFuncCallback(sPaintSuppressionCallback, this, delay, nsITimer::TYPE_ONE_SHOT); } } return NS_OK; //XXX this needs to be real. MMP } void PresShell::sPaintSuppressionCallback(nsITimer *aTimer, void* aPresShell) { nsRefPtr self = static_cast(aPresShell); if (self) self->UnsuppressPainting(); } void PresShell::AsyncResizeEventCallback(nsITimer* aTimer, void* aPresShell) { static_cast(aPresShell)->FireResizeEvent(); } nsresult PresShell::ResizeReflow(nscoord aWidth, nscoord aHeight) { NS_PRECONDITION(!mIsReflowing, "Shouldn't be in reflow here!"); NS_PRECONDITION(aWidth != NS_UNCONSTRAINEDSIZE, "shouldn't use unconstrained widths anymore"); // If we don't have a root frame yet, that means we haven't had our initial // reflow... If that's the case, and aWidth or aHeight is unconstrained, // ignore them altogether. nsIFrame* rootFrame = FrameManager()->GetRootFrame(); if (!rootFrame && aHeight == NS_UNCONSTRAINEDSIZE) { // We can't do the work needed for SizeToContent without a root // frame, and we want to return before setting the visible area. return NS_ERROR_NOT_AVAILABLE; } mPresContext->SetVisibleArea(nsRect(0, 0, aWidth, aHeight)); // There isn't anything useful we can do if the initial reflow hasn't happened if (!rootFrame) return NS_OK; NS_ASSERTION(mViewManager, "Must have view manager"); nsCOMPtr viewManagerDeathGrip = mViewManager; // Take this ref after viewManager so it'll make sure to go away first nsCOMPtr kungFuDeathGrip(this); if (!GetPresContext()->SupressingResizeReflow()) { nsIViewManager::UpdateViewBatch batch(mViewManager); // Have to make sure that the content notifications are flushed before we // start messing with the frame model; otherwise we can get content doubling. mDocument->FlushPendingNotifications(Flush_ContentAndNotify); // Make sure style is up to date { nsAutoScriptBlocker scriptBlocker; mFrameConstructor->CreateNeededFrames(); mFrameConstructor->ProcessPendingRestyles(); } if (!mIsDestroying) { // XXX Do a full invalidate at the beginning so that invalidates along // the way don't have region accumulation issues? { nsAutoCauseReflowNotifier crNotifier(this); WillDoReflow(); // Kick off a top-down reflow AUTO_LAYOUT_PHASE_ENTRY_POINT(GetPresContext(), Reflow); mDirtyRoots.RemoveElement(rootFrame); DoReflow(rootFrame, PR_TRUE); } DidDoReflow(PR_TRUE); } batch.EndUpdateViewBatch(NS_VMREFRESH_NO_SYNC); } if (aHeight == NS_UNCONSTRAINEDSIZE) { mPresContext->SetVisibleArea( nsRect(0, 0, aWidth, rootFrame->GetRect().height)); } if (!mIsDestroying && !mResizeEvent.IsPending() && !mAsyncResizeTimerIsActive) { if (mInResize) { if (!mAsyncResizeEventTimer) { mAsyncResizeEventTimer = do_CreateInstance("@mozilla.org/timer;1"); } if (mAsyncResizeEventTimer) { mAsyncResizeTimerIsActive = PR_TRUE; mAsyncResizeEventTimer->InitWithFuncCallback(AsyncResizeEventCallback, this, 15, nsITimer::TYPE_ONE_SHOT); } } else { nsRefPtr > resizeEvent = NS_NewRunnableMethod(this, &PresShell::FireResizeEvent); if (NS_SUCCEEDED(NS_DispatchToCurrentThread(resizeEvent))) { mResizeEvent = resizeEvent; } } } return NS_OK; //XXX this needs to be real. MMP } void PresShell::FireResizeEvent() { if (mAsyncResizeTimerIsActive) { mAsyncResizeTimerIsActive = PR_FALSE; mAsyncResizeEventTimer->Cancel(); } mResizeEvent.Revoke(); if (mIsDocumentGone) return; //Send resize event from here. nsEvent event(PR_TRUE, NS_RESIZE_EVENT); nsEventStatus status = nsEventStatus_eIgnore; nsPIDOMWindow *window = mDocument->GetWindow(); if (window) { nsCOMPtr kungFuDeathGrip(this); mInResize = PR_TRUE; nsEventDispatcher::Dispatch(window, mPresContext, &event, nsnull, &status); mInResize = PR_FALSE; } } void PresShell::SetIgnoreFrameDestruction(PRBool aIgnore) { mIgnoreFrameDestruction = aIgnore; } void PresShell::NotifyDestroyingFrame(nsIFrame* aFrame) { NS_TIME_FUNCTION_MIN(1.0); mPresContext->ForgetUpdatePluginGeometryFrame(aFrame); if (!mIgnoreFrameDestruction) { mPresContext->StopImagesFor(aFrame); mFrameConstructor->NotifyDestroyingFrame(aFrame); for (PRInt32 idx = mDirtyRoots.Length(); idx; ) { --idx; if (mDirtyRoots[idx] == aFrame) { mDirtyRoots.RemoveElementAt(idx); } } // Notify the frame manager FrameManager()->NotifyDestroyingFrame(aFrame); // Remove frame properties mPresContext->NotifyDestroyingFrame(aFrame); if (aFrame == mCurrentEventFrame) { mCurrentEventContent = aFrame->GetContent(); mCurrentEventFrame = nsnull; } #ifdef NS_DEBUG if (aFrame == mDrawEventTargetFrame) { mDrawEventTargetFrame = nsnull; } #endif for (unsigned int i=0; i < mCurrentEventFrameStack.Length(); i++) { if (aFrame == mCurrentEventFrameStack.ElementAt(i)) { //One of our stack frames was deleted. Get its content so that when we //pop it we can still get its new frame from its content nsIContent *currentEventContent = aFrame->GetContent(); mCurrentEventContentStack.ReplaceObjectAt(currentEventContent, i); mCurrentEventFrameStack[i] = nsnull; } } mFramesToDirty.RemoveEntry(aFrame); } } already_AddRefed PresShell::GetCaret() const { nsCaret* caret = mCaret; NS_IF_ADDREF(caret); return caret; } void PresShell::MaybeInvalidateCaretPosition() { if (mCaret) { mCaret->InvalidateOutsideCaret(); } } void PresShell::SetCaret(nsCaret *aNewCaret) { mCaret = aNewCaret; } void PresShell::RestoreCaret() { mCaret = mOriginalCaret; } NS_IMETHODIMP PresShell::SetCaretEnabled(PRBool aInEnable) { PRBool oldEnabled = mCaretEnabled; mCaretEnabled = aInEnable; if (mCaret && (mCaretEnabled != oldEnabled)) { /* Don't change the caret's selection here! This was an evil side-effect of SetCaretEnabled() nsCOMPtr domSel; if (NS_SUCCEEDED(GetSelection(nsISelectionController::SELECTION_NORMAL, getter_AddRefs(domSel))) && domSel) mCaret->SetCaretDOMSelection(domSel); */ mCaret->SetCaretVisible(mCaretEnabled); } return NS_OK; } NS_IMETHODIMP PresShell::SetCaretReadOnly(PRBool aReadOnly) { if (mCaret) mCaret->SetCaretReadOnly(aReadOnly); return NS_OK; } NS_IMETHODIMP PresShell::GetCaretEnabled(PRBool *aOutEnabled) { NS_ENSURE_ARG_POINTER(aOutEnabled); *aOutEnabled = mCaretEnabled; return NS_OK; } NS_IMETHODIMP PresShell::SetCaretVisibilityDuringSelection(PRBool aVisibility) { if (mCaret) mCaret->SetVisibilityDuringSelection(aVisibility); return NS_OK; } NS_IMETHODIMP PresShell::GetCaretVisible(PRBool *aOutIsVisible) { *aOutIsVisible = PR_FALSE; if (mCaret) { nsresult rv = mCaret->GetCaretVisible(aOutIsVisible); NS_ENSURE_SUCCESS(rv,rv); } return NS_OK; } NS_IMETHODIMP PresShell::SetSelectionFlags(PRInt16 aInEnable) { mSelectionFlags = aInEnable; return NS_OK; } NS_IMETHODIMP PresShell::GetSelectionFlags(PRInt16 *aOutEnable) { if (!aOutEnable) return NS_ERROR_INVALID_ARG; *aOutEnable = mSelectionFlags; return NS_OK; } //implementation of nsISelectionController NS_IMETHODIMP PresShell::CharacterMove(PRBool aForward, PRBool aExtend) { return mSelection->CharacterMove(aForward, aExtend); } NS_IMETHODIMP PresShell::CharacterExtendForDelete() { return mSelection->CharacterExtendForDelete(); } NS_IMETHODIMP PresShell::CharacterExtendForBackspace() { return mSelection->CharacterExtendForBackspace(); } NS_IMETHODIMP PresShell::WordMove(PRBool aForward, PRBool aExtend) { return mSelection->WordMove(aForward, aExtend); } NS_IMETHODIMP PresShell::WordExtendForDelete(PRBool aForward) { return mSelection->WordExtendForDelete(aForward); } NS_IMETHODIMP PresShell::LineMove(PRBool aForward, PRBool aExtend) { nsresult result = mSelection->LineMove(aForward, aExtend); // if we can't go down/up any more we must then move caret completely to // end/beginning respectively. if (NS_FAILED(result)) result = CompleteMove(aForward,aExtend); return result; } NS_IMETHODIMP PresShell::IntraLineMove(PRBool aForward, PRBool aExtend) { return mSelection->IntraLineMove(aForward, aExtend); } NS_IMETHODIMP PresShell::PageMove(PRBool aForward, PRBool aExtend) { nsIScrollableFrame *scrollableFrame = GetFrameToScrollAsScrollable(nsIPresShell::eVertical); if (!scrollableFrame) return NS_OK; mSelection->CommonPageMove(aForward, aExtend, scrollableFrame); // After ScrollSelectionIntoView(), the pending notifications might be // flushed and PresShell/PresContext/Frames may be dead. See bug 418470. return ScrollSelectionIntoView(nsISelectionController::SELECTION_NORMAL, nsISelectionController::SELECTION_FOCUS_REGION, PR_TRUE); } NS_IMETHODIMP PresShell::ScrollPage(PRBool aForward) { nsIScrollableFrame* scrollFrame = GetFrameToScrollAsScrollable(nsIPresShell::eVertical); if (scrollFrame) { scrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1), nsIScrollableFrame::PAGES, nsIScrollableFrame::SMOOTH); } return NS_OK; } NS_IMETHODIMP PresShell::ScrollLine(PRBool aForward) { nsIScrollableFrame* scrollFrame = GetFrameToScrollAsScrollable(nsIPresShell::eVertical); if (scrollFrame) { PRInt32 lineCount = 1; #ifdef MOZ_WIDGET_COCOA // Emulate the Mac IE behavior of scrolling a minimum of 2 lines // rather than 1. This vastly improves scrolling speed. lineCount = 2; #endif scrollFrame->ScrollBy(nsIntPoint(0, aForward ? lineCount : -lineCount), nsIScrollableFrame::LINES, nsIScrollableFrame::SMOOTH); //NEW FOR LINES // force the update to happen now, otherwise multiple scrolls can // occur before the update is processed. (bug #7354) // I'd use Composite here, but it doesn't always work. // vm->Composite(); nsIViewManager* viewManager = GetViewManager(); if (viewManager) { viewManager->ForceUpdate(); } } return NS_OK; } NS_IMETHODIMP PresShell::ScrollHorizontal(PRBool aLeft) { nsIScrollableFrame* scrollFrame = GetFrameToScrollAsScrollable(nsIPresShell::eHorizontal); if (scrollFrame) { scrollFrame->ScrollBy(nsIntPoint(aLeft ? -1 : 1, 0), nsIScrollableFrame::LINES, nsIScrollableFrame::SMOOTH); //NEW FOR LINES // force the update to happen now, otherwise multiple scrolls can // occur before the update is processed. (bug #7354) // I'd use Composite here, but it doesn't always work. // vm->Composite(); nsIViewManager* viewManager = GetViewManager(); if (viewManager) { viewManager->ForceUpdate(); } } return NS_OK; } NS_IMETHODIMP PresShell::CompleteScroll(PRBool aForward) { nsIScrollableFrame* scrollFrame = GetFrameToScrollAsScrollable(nsIPresShell::eVertical); if (scrollFrame) { scrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1), nsIScrollableFrame::WHOLE, nsIScrollableFrame::INSTANT); } return NS_OK; } NS_IMETHODIMP PresShell::CompleteMove(PRBool aForward, PRBool aExtend) { // Beware! This may flush notifications via synchronous // ScrollSelectionIntoView. nsIContent* limiter = mSelection->GetAncestorLimiter(); nsIFrame* frame = limiter ? limiter->GetPrimaryFrame() : FrameConstructor()->GetRootElementFrame(); if (!frame) return NS_ERROR_FAILURE; nsPeekOffsetStruct pos = frame->GetExtremeCaretPosition(!aForward); mSelection->HandleClick(pos.mResultContent, pos.mContentOffset, pos.mContentOffset, aExtend, PR_FALSE, aForward); if (limiter) { // HandleClick resets ancestorLimiter, so set it again. mSelection->SetAncestorLimiter(limiter); } // After ScrollSelectionIntoView(), the pending notifications might be // flushed and PresShell/PresContext/Frames may be dead. See bug 418470. return ScrollSelectionIntoView(nsISelectionController::SELECTION_NORMAL, nsISelectionController::SELECTION_FOCUS_REGION, PR_TRUE); } NS_IMETHODIMP PresShell::SelectAll() { return mSelection->SelectAll(); } NS_IMETHODIMP PresShell::CheckVisibility(nsIDOMNode *node, PRInt16 startOffset, PRInt16 EndOffset, PRBool *_retval) { if (!node || startOffset>EndOffset || !_retval || startOffset<0 || EndOffset<0) return NS_ERROR_INVALID_ARG; *_retval = PR_FALSE; //initialize return parameter nsCOMPtr content(do_QueryInterface(node)); if (!content) return NS_ERROR_FAILURE; nsIFrame *frame = content->GetPrimaryFrame(); if (!frame) //no frame to look at so it must not be visible return NS_OK; //start process now to go through all frames to find startOffset. then check chars after that to see //if anything until EndOffset is visible. PRBool finished = PR_FALSE; frame->CheckVisibility(mPresContext,startOffset,EndOffset,PR_TRUE,&finished, _retval); return NS_OK;//dont worry about other return val } //end implementations nsISelectionController void PresShell::StyleChangeReflow() { nsIFrame* rootFrame = FrameManager()->GetRootFrame(); // At the moment at least, we don't have a root frame before the initial // reflow; it's safe to just ignore the request in that case if (!rootFrame) return; FrameNeedsReflow(rootFrame, eStyleChange, NS_FRAME_IS_DIRTY); } nsIFrame* nsIPresShell::GetRootFrameExternal() const { return FrameManager()->GetRootFrame(); } nsIFrame* nsIPresShell::GetRootScrollFrame() const { nsIFrame* rootFrame = FrameManager()->GetRootFrame(); // Ensure root frame is a viewport frame if (!rootFrame || nsGkAtoms::viewportFrame != rootFrame->GetType()) return nsnull; nsIFrame* theFrame = rootFrame->GetFirstChild(nsnull); if (!theFrame || nsGkAtoms::scrollFrame != theFrame->GetType()) return nsnull; return theFrame; } nsIScrollableFrame* nsIPresShell::GetRootScrollFrameAsScrollable() const { nsIFrame* frame = GetRootScrollFrame(); if (!frame) return nsnull; nsIScrollableFrame* scrollableFrame = do_QueryFrame(frame); NS_ASSERTION(scrollableFrame, "All scroll frames must implement nsIScrollableFrame"); return scrollableFrame; } nsIScrollableFrame* nsIPresShell::GetRootScrollFrameAsScrollableExternal() const { return GetRootScrollFrameAsScrollable(); } nsIPageSequenceFrame* PresShell::GetPageSequenceFrame() const { nsIFrame* frame = mFrameConstructor->GetPageSequenceFrame(); return do_QueryFrame(frame); } nsIFrame* PresShell::GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt) { return nsLayoutUtils::GetFrameForPoint(aFrame, aPt); } void PresShell::BeginUpdate(nsIDocument *aDocument, nsUpdateType aUpdateType) { #ifdef DEBUG mUpdateCount++; #endif mFrameConstructor->BeginUpdate(); if (aUpdateType & UPDATE_STYLE) mStyleSet->BeginUpdate(); } void PresShell::EndUpdate(nsIDocument *aDocument, nsUpdateType aUpdateType) { #ifdef DEBUG NS_PRECONDITION(0 != mUpdateCount, "too many EndUpdate's"); --mUpdateCount; #endif if (aUpdateType & UPDATE_STYLE) { mStyleSet->EndUpdate(); if (mStylesHaveChanged) ReconstructStyleData(); } mFrameConstructor->EndUpdate(); } void PresShell::RestoreRootScrollPosition() { // Restore frame state for the root scroll frame nsCOMPtr historyState = mDocument->GetLayoutHistoryState(); // Make sure we don't reenter reflow via the sync paint that happens while // we're scrolling to our restored position. Entering reflow for the // scrollable frame will cause it to reenter ScrollToRestoredPosition(), and // it'll get all confused. nsAutoScriptBlocker scriptBlocker; ++mChangeNestCount; if (historyState) { nsIFrame* scrollFrame = GetRootScrollFrame(); if (scrollFrame) { nsIScrollableFrame* scrollableFrame = do_QueryFrame(scrollFrame); if (scrollableFrame) { FrameManager()->RestoreFrameStateFor(scrollFrame, historyState, nsIStatefulFrame::eDocumentScrollState); scrollableFrame->ScrollToRestoredPosition(); } } } --mChangeNestCount; } void PresShell::BeginLoad(nsIDocument *aDocument) { mDocumentLoading = PR_TRUE; } void PresShell::EndLoad(nsIDocument *aDocument) { NS_PRECONDITION(aDocument == mDocument, "Wrong document"); RestoreRootScrollPosition(); mDocumentLoading = PR_FALSE; } #ifdef DEBUG void PresShell::VerifyHasDirtyRootAncestor(nsIFrame* aFrame) { // XXXbz due to bug 372769, can't actually assert anything here... return; // XXXbz shouldn't need this part; remove it once FrameNeedsReflow // handles the root frame correctly. if (!aFrame->GetParent()) { return; } // Make sure that there is a reflow root ancestor of |aFrame| that's // in mDirtyRoots already. while (aFrame && (aFrame->GetStateBits() & NS_FRAME_HAS_DIRTY_CHILDREN)) { if (((aFrame->GetStateBits() & NS_FRAME_REFLOW_ROOT) || !aFrame->GetParent()) && mDirtyRoots.Contains(aFrame)) { return; } aFrame = aFrame->GetParent(); } NS_NOTREACHED("Frame has dirty bits set but isn't scheduled to be " "reflowed?"); } #endif void PresShell::FrameNeedsReflow(nsIFrame *aFrame, IntrinsicDirty aIntrinsicDirty, nsFrameState aBitToAdd) { #ifdef NS_FUNCTION_TIMER NS_TIME_FUNCTION_DECLARE_DOCURL; nsCAutoString frameType__("N/A"); nsIAtom *atomType__ = aFrame ? aFrame->GetType() : nsnull; if (atomType__) atomType__->ToUTF8String(frameType__); NS_TIME_FUNCTION_MIN_FMT(1.0, "%s (line %d) (document: %s, frame type: %s)", MOZ_FUNCTION_NAME, __LINE__, docURL__.get(), frameType__.get()); #endif NS_PRECONDITION(aBitToAdd == NS_FRAME_IS_DIRTY || aBitToAdd == NS_FRAME_HAS_DIRTY_CHILDREN, "Unexpected bits being added"); NS_PRECONDITION(aIntrinsicDirty != eStyleChange || aBitToAdd == NS_FRAME_IS_DIRTY, "bits don't correspond to style change reason"); NS_ASSERTION(!mIsReflowing, "can't mark frame dirty during reflow"); // If we've not yet done the initial reflow, then don't bother // enqueuing a reflow command yet. if (! mDidInitialReflow) return; // If we're already destroying, don't bother with this either. if (mIsDestroying) return; #ifdef DEBUG //printf("gShellCounter: %d\n", gShellCounter++); if (mInVerifyReflow) return; if (VERIFY_REFLOW_NOISY_RC & gVerifyReflowFlags) { printf("\nPresShell@%p: frame %p needs reflow\n", (void*)this, (void*)aFrame); if (VERIFY_REFLOW_REALLY_NOISY_RC & gVerifyReflowFlags) { printf("Current content model:\n"); Element *rootElement = mDocument->GetRootElement(); if (rootElement) { rootElement->List(stdout, 0); } } } #endif nsAutoTArray subtrees; subtrees.AppendElement(aFrame); do { nsIFrame *subtreeRoot = subtrees.ElementAt(subtrees.Length() - 1); subtrees.RemoveElementAt(subtrees.Length() - 1); // Grab |wasDirty| now so we can go ahead and update the bits on // subtreeRoot. PRBool wasDirty = NS_SUBTREE_DIRTY(subtreeRoot); subtreeRoot->AddStateBits(aBitToAdd); // Now if subtreeRoot is a reflow root we can cut off this reflow at it if // the bit being added is NS_FRAME_HAS_DIRTY_CHILDREN. PRBool targetFrameDirty = (aBitToAdd == NS_FRAME_IS_DIRTY); #define FRAME_IS_REFLOW_ROOT(_f) \ ((_f->GetStateBits() & NS_FRAME_REFLOW_ROOT) && \ (_f != subtreeRoot || !targetFrameDirty)) // Mark the intrinsic widths as dirty on the frame, all of its ancestors, // and all of its descendants, if needed: if (aIntrinsicDirty != eResize) { // Mark argument and all ancestors dirty. (Unless we hit a reflow // root that should contain the reflow. That root could be // subtreeRoot itself if it's not dirty, or it could be some // ancestor of subtreeRoot.) for (nsIFrame *a = subtreeRoot; a && !FRAME_IS_REFLOW_ROOT(a); a = a->GetParent()) a->MarkIntrinsicWidthsDirty(); } if (aIntrinsicDirty == eStyleChange) { // Mark all descendants dirty (using an nsTArray stack rather than // recursion). nsAutoTArray stack; stack.AppendElement(subtreeRoot); do { nsIFrame *f = stack.ElementAt(stack.Length() - 1); stack.RemoveElementAt(stack.Length() - 1); if (f->GetType() == nsGkAtoms::placeholderFrame) { nsIFrame *oof = nsPlaceholderFrame::GetRealFrameForPlaceholder(f); if (!nsLayoutUtils::IsProperAncestorFrame(subtreeRoot, oof)) { // We have another distinct subtree we need to mark. subtrees.AppendElement(oof); } } PRInt32 childListIndex = 0; nsIAtom *childListName; do { childListName = f->GetAdditionalChildListName(childListIndex++); for (nsIFrame *kid = f->GetFirstChild(childListName); kid; kid = kid->GetNextSibling()) { kid->MarkIntrinsicWidthsDirty(); stack.AppendElement(kid); } } while (childListName); } while (stack.Length() != 0); } // Set NS_FRAME_HAS_DIRTY_CHILDREN bits (via nsIFrame::ChildIsDirty) // up the tree until we reach either a frame that's already dirty or // a reflow root. nsIFrame *f = subtreeRoot; for (;;) { if (FRAME_IS_REFLOW_ROOT(f) || !f->GetParent()) { // we've hit a reflow root or the root frame if (!wasDirty) { mDirtyRoots.AppendElement(f); } #ifdef DEBUG else { VerifyHasDirtyRootAncestor(f); } #endif break; } nsIFrame *child = f; f = f->GetParent(); wasDirty = NS_SUBTREE_DIRTY(f); f->ChildIsDirty(child); NS_ASSERTION(f->GetStateBits() & NS_FRAME_HAS_DIRTY_CHILDREN, "ChildIsDirty didn't do its job"); if (wasDirty) { // This frame was already marked dirty. #ifdef DEBUG VerifyHasDirtyRootAncestor(f); #endif break; } } } while (subtrees.Length() != 0); MaybeScheduleReflow(); } void PresShell::FrameNeedsToContinueReflow(nsIFrame *aFrame) { NS_ASSERTION(mIsReflowing, "Must be in reflow when marking path dirty."); NS_PRECONDITION(mCurrentReflowRoot, "Must have a current reflow root here"); NS_ASSERTION(aFrame == mCurrentReflowRoot || nsLayoutUtils::IsProperAncestorFrame(mCurrentReflowRoot, aFrame), "Frame passed in is not the descendant of mCurrentReflowRoot"); NS_ASSERTION(aFrame->GetStateBits() & NS_FRAME_IN_REFLOW, "Frame passed in not in reflow?"); mFramesToDirty.PutEntry(aFrame); } nsIScrollableFrame* nsIPresShell::GetFrameToScrollAsScrollable( nsIPresShell::ScrollDirection aDirection) { nsIScrollableFrame* scrollFrame = nsnull; nsCOMPtr focusedContent; nsIFocusManager* fm = nsFocusManager::GetFocusManager(); if (fm && mDocument) { nsCOMPtr window = do_QueryInterface(mDocument->GetWindow()); nsCOMPtr focusedElement; fm->GetFocusedElementForWindow(window, PR_FALSE, nsnull, getter_AddRefs(focusedElement)); focusedContent = do_QueryInterface(focusedElement); } if (!focusedContent && mSelection) { nsISelection* domSelection = mSelection-> GetSelection(nsISelectionController::SELECTION_NORMAL); if (domSelection) { nsCOMPtr focusedNode; domSelection->GetFocusNode(getter_AddRefs(focusedNode)); focusedContent = do_QueryInterface(focusedNode); } } if (focusedContent) { nsIFrame* startFrame = focusedContent->GetPrimaryFrame(); if (startFrame) { scrollFrame = startFrame->GetScrollTargetFrame(); if (scrollFrame) { startFrame = scrollFrame->GetScrolledFrame(); } if (aDirection == nsIPresShell::eEither) { scrollFrame = nsLayoutUtils::GetNearestScrollableFrame(startFrame); } else { scrollFrame = nsLayoutUtils::GetNearestScrollableFrameForDirection(startFrame, aDirection == eVertical ? nsLayoutUtils::eVertical : nsLayoutUtils::eHorizontal); } } } if (!scrollFrame) { scrollFrame = GetRootScrollFrameAsScrollable(); } return scrollFrame; } void PresShell::CancelAllPendingReflows() { mDirtyRoots.Clear(); if (mReflowScheduled) { GetPresContext()->RefreshDriver()->RemoveRefreshObserver(this, Flush_Layout); mReflowScheduled = PR_FALSE; } ASSERT_REFLOW_SCHEDULED_STATE(); } #ifdef ACCESSIBILITY void nsIPresShell::InvalidateAccessibleSubtree(nsIContent *aContent) { if (gIsAccessibilityActive) { nsCOMPtr accService = do_GetService("@mozilla.org/accessibilityService;1"); if (accService) { accService->InvalidateSubtreeFor(this, aContent, nsIAccessibilityService::FRAME_SIGNIFICANT_CHANGE); } } } #endif nsresult PresShell::RecreateFramesFor(nsIContent* aContent) { NS_TIME_FUNCTION_MIN(1.0); NS_ENSURE_TRUE(mPresContext, NS_ERROR_FAILURE); if (!mDidInitialReflow) { // Nothing to do here. In fact, if we proceed and aContent is the // root we will crash. return NS_OK; } // Don't call RecreateFramesForContent since that is not exported and we want // to keep the number of entrypoints down. NS_ASSERTION(mViewManager, "Should have view manager"); nsIViewManager::UpdateViewBatch batch(mViewManager); // Have to make sure that the content notifications are flushed before we // start messing with the frame model; otherwise we can get content doubling. mDocument->FlushPendingNotifications(Flush_ContentAndNotify); nsAutoScriptBlocker scriptBlocker; nsStyleChangeList changeList; changeList.AppendChange(nsnull, aContent, nsChangeHint_ReconstructFrame); // Mark ourselves as not safe to flush while we're doing frame construction. ++mChangeNestCount; nsresult rv = mFrameConstructor->ProcessRestyledFrames(changeList); --mChangeNestCount; batch.EndUpdateViewBatch(NS_VMREFRESH_NO_SYNC); #ifdef ACCESSIBILITY InvalidateAccessibleSubtree(aContent); #endif return rv; } void nsIPresShell::PostRecreateFramesFor(Element* aElement) { FrameConstructor()->PostRestyleEvent(aElement, nsRestyleHint(0), nsChangeHint_ReconstructFrame); } void nsIPresShell::RestyleForAnimation(Element* aElement, nsRestyleHint aHint) { FrameConstructor()->PostAnimationRestyleEvent(aElement, aHint, NS_STYLE_HINT_NONE); } void PresShell::ClearFrameRefs(nsIFrame* aFrame) { mPresContext->EventStateManager()->ClearFrameRefs(aFrame); nsWeakFrame* weakFrame = mWeakFrames; while (weakFrame) { nsWeakFrame* prev = weakFrame->GetPreviousWeakFrame(); if (weakFrame->GetFrame() == aFrame) { // This removes weakFrame from mWeakFrames. weakFrame->Clear(this); } weakFrame = prev; } } nsresult PresShell::CreateRenderingContext(nsIFrame *aFrame, nsIRenderingContext** aResult) { NS_TIME_FUNCTION_MIN(1.0); NS_PRECONDITION(nsnull != aResult, "null ptr"); if (nsnull == aResult) { return NS_ERROR_NULL_POINTER; } nsIWidget* widget = nsnull; nsPoint offset(0,0); if (mPresContext->IsScreen()) { // Get the widget to create the rendering context for and calculate // the offset from the frame to it. nsPoint viewOffset; nsIView* view = aFrame->GetClosestView(&viewOffset); nsPoint widgetOffset; widget = view->GetNearestWidget(&widgetOffset); offset = viewOffset + widgetOffset; } else { nsIFrame* pageFrame = nsLayoutUtils::GetPageFrame(aFrame); // This might not always come up with a frame, i.e. during reflow; // that's fine, because the translation doesn't matter during reflow. if (pageFrame) offset = aFrame->GetOffsetTo(pageFrame); } nsresult rv; nsIRenderingContext* result = nsnull; nsIDeviceContext *deviceContext = mPresContext->DeviceContext(); if (widget) { rv = deviceContext->CreateRenderingContext(widget, result); } else { rv = deviceContext->CreateRenderingContext(result); } *aResult = result; if (NS_SUCCEEDED(rv)) { result->Translate(offset.x, offset.y); } return rv; } nsresult PresShell::GoToAnchor(const nsAString& aAnchorName, PRBool aScroll) { if (!mDocument) { return NS_ERROR_FAILURE; } // Hold a reference to the ESM in case event dispatch tears us down. nsCOMPtr esm = mPresContext->EventStateManager(); if (aAnchorName.IsEmpty()) { NS_ASSERTION(!aScroll, "can't scroll to empty anchor name"); esm->SetContentState(nsnull, NS_EVENT_STATE_URLTARGET); return NS_OK; } nsCOMPtr htmlDoc = do_QueryInterface(mDocument); nsresult rv = NS_OK; nsCOMPtr content; // Search for an element with a matching "id" attribute if (mDocument) { content = mDocument->GetElementById(aAnchorName); } // Search for an anchor element with a matching "name" attribute if (!content && htmlDoc) { nsCOMPtr list; // Find a matching list of named nodes rv = htmlDoc->GetElementsByName(aAnchorName, getter_AddRefs(list)); if (NS_SUCCEEDED(rv) && list) { PRUint32 i; // Loop through the named nodes looking for the first anchor for (i = 0; PR_TRUE; i++) { nsCOMPtr node; rv = list->Item(i, getter_AddRefs(node)); if (!node) { // End of list break; } // Ensure it's an anchor element content = do_QueryInterface(node); if (content) { if (content->Tag() == nsGkAtoms::a && content->IsHTML()) { break; } content = nsnull; } } } } // Search for anchor in the HTML namespace with a matching name if (!content && !htmlDoc) { nsCOMPtr doc = do_QueryInterface(mDocument); nsCOMPtr list; NS_NAMED_LITERAL_STRING(nameSpace, "http://www.w3.org/1999/xhtml"); // Get the list of anchor elements rv = doc->GetElementsByTagNameNS(nameSpace, NS_LITERAL_STRING("a"), getter_AddRefs(list)); if (NS_SUCCEEDED(rv) && list) { PRUint32 i; // Loop through the named nodes looking for the first anchor for (i = 0; PR_TRUE; i++) { nsCOMPtr node; rv = list->Item(i, getter_AddRefs(node)); if (!node) { // End of list break; } // Compare the name attribute nsCOMPtr element = do_QueryInterface(node); nsAutoString value; if (element && NS_SUCCEEDED(element->GetAttribute(NS_LITERAL_STRING("name"), value))) { if (value.Equals(aAnchorName)) { content = do_QueryInterface(element); break; } } } } } nsCOMPtr jumpToRange; nsCOMPtr xpointerResult; if (!content) { nsCOMPtr xmldoc = do_QueryInterface(mDocument); if (xmldoc) { // Try XPointer xmldoc->EvaluateXPointer(aAnchorName, getter_AddRefs(xpointerResult)); if (xpointerResult) { xpointerResult->Item(0, getter_AddRefs(jumpToRange)); if (!jumpToRange) { // We know it was an XPointer, so there is no point in // trying any other pointer types, let's just return // an error. return NS_ERROR_FAILURE; } } // Finally try FIXptr if (!jumpToRange) { xmldoc->EvaluateFIXptr(aAnchorName,getter_AddRefs(jumpToRange)); } if (jumpToRange) { nsCOMPtr node; jumpToRange->GetStartContainer(getter_AddRefs(node)); if (node) { PRUint16 nodeType; node->GetNodeType(&nodeType); PRInt32 offset = -1; jumpToRange->GetStartOffset(&offset); switch (nodeType) { case nsIDOMNode::ATTRIBUTE_NODE: { // XXX Assuming jumping to the ownerElement is the sanest action. nsCOMPtr attr = do_QueryInterface(node); content = attr->GetContent(); break; } case nsIDOMNode::DOCUMENT_NODE: { if (offset >= 0) { nsCOMPtr document = do_QueryInterface(node); content = document->GetChildAt(offset); } break; } case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: case nsIDOMNode::ELEMENT_NODE: case nsIDOMNode::ENTITY_REFERENCE_NODE: { if (offset >= 0) { nsCOMPtr parent = do_QueryInterface(node); content = parent->GetChildAt(offset); } break; } case nsIDOMNode::CDATA_SECTION_NODE: case nsIDOMNode::COMMENT_NODE: case nsIDOMNode::TEXT_NODE: case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: { // XXX This should scroll to a specific position in the text. content = do_QueryInterface(node); break; } } } } } } esm->SetContentState(content, NS_EVENT_STATE_URLTARGET); #ifdef ACCESSIBILITY nsIContent *anchorTarget = content; #endif if (content) { if (aScroll) { rv = ScrollContentIntoView(content, NS_PRESSHELL_SCROLL_TOP, NS_PRESSHELL_SCROLL_ANYWHERE); NS_ENSURE_SUCCESS(rv, rv); nsIScrollableFrame* rootScroll = GetRootScrollFrameAsScrollable(); if (rootScroll) { mLastAnchorScrolledTo = content; mLastAnchorScrollPositionY = rootScroll->GetScrollPosition().y; } } // Should we select the target? This action is controlled by a // preference: the default is to not select. PRBool selectAnchor = nsContentUtils::GetBoolPref("layout.selectanchor"); // Even if select anchor pref is false, we must still move the // caret there. That way tabbing will start from the new // location if (!jumpToRange) { jumpToRange = do_CreateInstance(kRangeCID); if (jumpToRange) { while (content && content->GetChildCount() > 0) { content = content->GetChildAt(0); } nsCOMPtr node(do_QueryInterface(content)); NS_ASSERTION(node, "No nsIDOMNode for descendant of anchor"); jumpToRange->SelectNodeContents(node); } } if (jumpToRange) { // Select the anchor nsISelection* sel = mSelection-> GetSelection(nsISelectionController::SELECTION_NORMAL); if (sel) { sel->RemoveAllRanges(); sel->AddRange(jumpToRange); if (!selectAnchor) { // Use a caret (collapsed selection) at the start of the anchor sel->CollapseToStart(); } } if (selectAnchor && xpointerResult) { // Select the rest (if any) of the ranges in XPointerResult PRUint32 count, i; xpointerResult->GetLength(&count); for (i = 1; i < count; i++) { // jumpToRange is i = 0 nsCOMPtr range; xpointerResult->Item(i, getter_AddRefs(range)); sel->AddRange(range); } } // Selection is at anchor. // Now focus the document itself if focus is on an element within it. nsPIDOMWindow *win = mDocument->GetWindow(); nsIFocusManager* fm = nsFocusManager::GetFocusManager(); if (fm && win) { nsCOMPtr focusedWindow; fm->GetFocusedWindow(getter_AddRefs(focusedWindow)); if (SameCOMIdentity(win, focusedWindow)) fm->ClearFocus(focusedWindow); } } } else { rv = NS_ERROR_FAILURE; //changed to NS_OK in quirks mode if ScrollTo is called // Scroll to the top/left if the anchor can not be // found and it is labelled top (quirks mode only). @see bug 80784 if ((NS_LossyConvertUTF16toASCII(aAnchorName).LowerCaseEqualsLiteral("top")) && (mPresContext->CompatibilityMode() == eCompatibility_NavQuirks)) { rv = NS_OK; nsIScrollableFrame* sf = GetRootScrollFrameAsScrollable(); // Check |aScroll| after setting |rv| so we set |rv| to the same // thing whether or not |aScroll| is true. if (aScroll && sf) { // Scroll to the top of the page sf->ScrollTo(nsPoint(0, 0), nsIScrollableFrame::INSTANT); } } } #ifdef ACCESSIBILITY if (anchorTarget && gIsAccessibilityActive) { nsCOMPtr accService = do_GetService("@mozilla.org/accessibilityService;1"); if (accService) accService->NotifyOfAnchorJumpTo(anchorTarget); } #endif return rv; } nsresult PresShell::ScrollToAnchor() { if (!mLastAnchorScrolledTo) return NS_OK; NS_ASSERTION(mDidInitialReflow, "should have done initial reflow by now"); nsIScrollableFrame* rootScroll = GetRootScrollFrameAsScrollable(); if (!rootScroll || mLastAnchorScrollPositionY != rootScroll->GetScrollPosition().y) return NS_OK; nsresult rv = ScrollContentIntoView(mLastAnchorScrolledTo, NS_PRESSHELL_SCROLL_TOP, NS_PRESSHELL_SCROLL_ANYWHERE); mLastAnchorScrolledTo = nsnull; return rv; } /* * Helper (per-continuation) for ScrollContentIntoView. * * @param aContainerFrame [in] the frame which aRect is relative to * @param aFrame [in] Frame whose bounds should be unioned * @param aUseWholeLineHeightForInlines [in] if true, then for inline frames * we should include the top of the line in the added rectangle * @param aRect [inout] rect into which its bounds should be unioned * @param aHaveRect [inout] whether aRect contains data yet */ static void AccumulateFrameBounds(nsIFrame* aContainerFrame, nsIFrame* aFrame, PRBool aUseWholeLineHeightForInlines, nsRect& aRect, PRBool& aHaveRect) { nsRect frameBounds = aFrame->GetRect() + aFrame->GetParent()->GetOffsetTo(aContainerFrame); // If this is an inline frame and either the bounds height is 0 (quirks // layout model) or aUseWholeLineHeightForInlines is set, we need to // change the top of the bounds to include the whole line. if (frameBounds.height == 0 || aUseWholeLineHeightForInlines) { nsIAtom* frameType = NULL; nsIFrame *prevFrame = aFrame; nsIFrame *f = aFrame; while (f && (frameType = f->GetType()) == nsGkAtoms::inlineFrame) { prevFrame = f; f = prevFrame->GetParent(); } if (f != aFrame && f && frameType == nsGkAtoms::blockFrame) { // find the line containing aFrame and increase the top of |offset|. nsAutoLineIterator lines = f->GetLineIterator(); if (lines) { PRInt32 index = lines->FindLineContaining(prevFrame); if (index >= 0) { nsIFrame *trash1; PRInt32 trash2; nsRect lineBounds; PRUint32 trash3; if (NS_SUCCEEDED(lines->GetLine(index, &trash1, &trash2, lineBounds, &trash3))) { lineBounds += f->GetOffsetTo(aContainerFrame); if (lineBounds.y < frameBounds.y) { frameBounds.height = frameBounds.YMost() - lineBounds.y; frameBounds.y = lineBounds.y; } } } } } } if (aHaveRect) { // We can't use nsRect::UnionRect since it drops empty rects on // the floor, and we need to include them. (Thus we need // aHaveRect to know when to drop the initial value on the floor.) aRect.UnionRectIncludeEmpty(aRect, frameBounds); } else { aHaveRect = PR_TRUE; aRect = frameBounds; } } /** * This function takes a scrollable frame, a rect in the coordinate system * of the scrolled frame, and a desired percentage-based scroll * position and attempts to scroll the rect to that position in the * scrollport. * * This needs to work even if aRect has a width or height of zero. */ static void ScrollToShowRect(nsIScrollableFrame* aScrollFrame, const nsRect& aRect, PRIntn aVPercent, PRIntn aHPercent, PRUint32 aFlags) { nsPoint scrollPt = aScrollFrame->GetScrollPosition(); nsRect visibleRect(scrollPt, aScrollFrame->GetScrollPortRect().Size()); nsSize lineSize = aScrollFrame->GetLineScrollAmount(); nsPresContext::ScrollbarStyles ss = aScrollFrame->GetScrollbarStyles(); if ((aFlags & nsIPresShell::SCROLL_OVERFLOW_HIDDEN) || ss.mVertical != NS_STYLE_OVERFLOW_HIDDEN) { // See how the rect should be positioned vertically if (NS_PRESSHELL_SCROLL_ANYWHERE == aVPercent || (NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE == aVPercent && aRect.height < lineSize.height)) { // The caller doesn't care where the frame is positioned vertically, // so long as it's fully visible if (aRect.y < visibleRect.y) { // Scroll up so the frame's top edge is visible scrollPt.y = aRect.y; } else if (aRect.YMost() > visibleRect.YMost()) { // Scroll down so the frame's bottom edge is visible. Make sure the // frame's top edge is still visible scrollPt.y += aRect.YMost() - visibleRect.YMost(); if (scrollPt.y > aRect.y) { scrollPt.y = aRect.y; } } } else if (NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE == aVPercent) { // Scroll only if no part of the frame is visible in this view if (aRect.YMost() - lineSize.height < visibleRect.y) { // Scroll up so the frame's top edge is visible scrollPt.y = aRect.y; } else if (aRect.y + lineSize.height > visibleRect.YMost()) { // Scroll down so the frame's bottom edge is visible. Make sure the // frame's top edge is still visible scrollPt.y += aRect.YMost() - visibleRect.YMost(); if (scrollPt.y > aRect.y) { scrollPt.y = aRect.y; } } } else { // Align the frame edge according to the specified percentage nscoord frameAlignY = NSToCoordRound(aRect.y + aRect.height * (aVPercent / 100.0f)); scrollPt.y = NSToCoordRound(frameAlignY - visibleRect.height * (aVPercent / 100.0f)); } } if ((aFlags & nsIPresShell::SCROLL_OVERFLOW_HIDDEN) || ss.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN) { // See how the frame should be positioned horizontally if (NS_PRESSHELL_SCROLL_ANYWHERE == aHPercent || (NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE == aHPercent && aRect.width < lineSize.width)) { // The caller doesn't care where the frame is positioned horizontally, // so long as it's fully visible if (aRect.x < visibleRect.x) { // Scroll left so the frame's left edge is visible scrollPt.x = aRect.x; } else if (aRect.XMost() > visibleRect.XMost()) { // Scroll right so the frame's right edge is visible. Make sure the // frame's left edge is still visible scrollPt.x += aRect.XMost() - visibleRect.XMost(); if (scrollPt.x > aRect.x) { scrollPt.x = aRect.x; } } } else if (NS_PRESSHELL_SCROLL_IF_NOT_VISIBLE == aHPercent) { // Scroll only if no part of the frame is visible in this view if (aRect.XMost() - lineSize.width < visibleRect.x) { // Scroll left so the frame's left edge is visible scrollPt.x = aRect.x; } else if (aRect.x + lineSize.width > visibleRect.XMost()) { // Scroll right so the frame's right edge is visible. Make sure the // frame's left edge is still visible scrollPt.x += aRect.XMost() - visibleRect.XMost(); if (scrollPt.x > aRect.x) { scrollPt.x = aRect.x; } } } else { // Align the frame edge according to the specified percentage nscoord frameAlignX = NSToCoordRound(aRect.x + (aRect.width) * (aHPercent / 100.0f)); scrollPt.x = NSToCoordRound(frameAlignX - visibleRect.width * (aHPercent / 100.0f)); } } aScrollFrame->ScrollTo(scrollPt, nsIScrollableFrame::INSTANT); } nsresult PresShell::ScrollContentIntoView(nsIContent* aContent, PRIntn aVPercent, PRIntn aHPercent) { nsCOMPtr content = aContent; // Keep content alive while flushing. NS_ENSURE_TRUE(content, NS_ERROR_NULL_POINTER); nsCOMPtr currentDoc = content->GetCurrentDoc(); NS_ENSURE_STATE(currentDoc); NS_ASSERTION(mDidInitialReflow, "should have done initial reflow by now"); mContentToScrollTo = aContent; mContentScrollVPosition = aVPercent; mContentScrollHPosition = aHPercent; // Flush layout and attempt to scroll in the process. currentDoc->FlushPendingNotifications(Flush_InterruptibleLayout); // If mContentToScrollTo is non-null, that means we interrupted the reflow // (or suppressed it altogether because we're suppressing interruptible // flushes right now) and won't necessarily get the position correct, but do // a best-effort scroll here. The other option would be to do this inside // FlushPendingNotifications, but I'm not sure the repeated scrolling that // could trigger if reflows keep getting interrupted would be more desirable // than a single best-effort scroll followed by one final scroll on the first // completed reflow. if (mContentToScrollTo) { DoScrollContentIntoView(content, aVPercent, aHPercent); } return NS_OK; } void PresShell::DoScrollContentIntoView(nsIContent* aContent, PRIntn aVPercent, PRIntn aHPercent) { NS_ASSERTION(mDidInitialReflow, "should have done initial reflow by now"); nsIFrame* frame = aContent->GetPrimaryFrame(); if (!frame) { mContentToScrollTo = nsnull; return; } if (frame->GetStateBits() & NS_FRAME_FIRST_REFLOW) { // The reflow flush before this scroll got interrupted, and this frame's // coords and size are all zero, and it has no content showing anyway. // Don't bother scrolling to it. We'll try again when we finish up layout. return; } nsIFrame* container = nsLayoutUtils::GetClosestFrameOfType(frame, nsGkAtoms::scrollFrame); if (!container) { // nothing can be scrolled return; } // This is a two-step process. // Step 1: Find the bounds of the rect we want to scroll into view. For // example, for an inline frame we may want to scroll in the whole // line, or we may want to scroll multiple lines into view. // Step 2: Walk container frame and its ancestors and scroll them // appropriately. // frameBounds is relative to container. We're assuming // that scrollframes don't split so every continuation of frame will // be a descendant of container. (Things would still mostly work // even if that assumption was false.) nsRect frameBounds; PRBool haveRect = PR_FALSE; PRBool useWholeLineHeightForInlines = aVPercent != NS_PRESSHELL_SCROLL_ANYWHERE; do { AccumulateFrameBounds(container, frame, useWholeLineHeightForInlines, frameBounds, haveRect); } while ((frame = frame->GetNextContinuation())); ScrollFrameRectIntoView(container, frameBounds, aVPercent, aHPercent, SCROLL_OVERFLOW_HIDDEN); } PRBool PresShell::ScrollFrameRectIntoView(nsIFrame* aFrame, const nsRect& aRect, PRIntn aVPercent, PRIntn aHPercent, PRUint32 aFlags) { PRBool didScroll = PR_FALSE; // This function needs to work even if rect has a width or height of 0. nsRect rect = aRect; nsIFrame* container = aFrame; // Walk up the frame hierarchy scrolling the rect into view and // keeping rect relative to container do { nsIScrollableFrame* sf = do_QueryFrame(container); if (sf) { nsPoint oldPosition = sf->GetScrollPosition(); ScrollToShowRect(sf, rect - sf->GetScrolledFrame()->GetPosition(), aVPercent, aHPercent, aFlags); nsPoint newPosition = sf->GetScrollPosition(); // If the scroll position increased, that means our content moved up, // so our rect's offset should decrease rect += oldPosition - newPosition; if (oldPosition != newPosition) { didScroll = PR_TRUE; } // only scroll one container when this flag is set if (aFlags & SCROLL_FIRST_ANCESTOR_ONLY) { break; } nsRect scrollPort = sf->GetScrollPortRect(); if (rect.XMost() < scrollPort.x || rect.x > scrollPort.XMost() || rect.YMost() < scrollPort.y || rect.y > scrollPort.YMost()) { // We tried to show the rectangle, but none of it is visible, // not even an edge. // Stop trying to scroll ancestors into view. break; } // Restrict rect to the area that is actually visible through // the scrollport. We don't want to try to scroll some clipped-out // part of 'rect' into view in some ancestor. rect.IntersectRect(rect, sf->GetScrollPortRect()); } rect += container->GetPosition(); nsIFrame* parent = container->GetParent(); if (!parent) { nsPoint extraOffset(0,0); parent = nsLayoutUtils::GetCrossDocParentFrame(container, &extraOffset); if (parent) { PRInt32 APD = container->PresContext()->AppUnitsPerDevPixel(); PRInt32 parentAPD = parent->PresContext()->AppUnitsPerDevPixel(); rect = rect.ConvertAppUnitsRoundOut(APD, parentAPD); rect += extraOffset; } } container = parent; } while (container); return didScroll; } nsRectVisibility PresShell::GetRectVisibility(nsIFrame* aFrame, const nsRect &aRect, nscoord aMinTwips) const { NS_ASSERTION(aFrame->PresContext() == GetPresContext(), "prescontext mismatch?"); nsIFrame* rootFrame = FrameManager()->GetRootFrame(); NS_ASSERTION(rootFrame, "How can someone have a frame for this presshell when there's no root?"); nsIScrollableFrame* sf = GetRootScrollFrameAsScrollable(); nsRect scrollPortRect; if (sf) { scrollPortRect = sf->GetScrollPortRect(); nsIFrame* f = do_QueryFrame(sf); scrollPortRect += f->GetOffsetTo(rootFrame); } else { scrollPortRect = nsRect(nsPoint(0,0), rootFrame->GetSize()); } nsRect r = aRect + aFrame->GetOffsetTo(rootFrame); // If aRect is entirely visible then we don't need to ensure that // at least aMinTwips of it is visible if (scrollPortRect.Contains(r)) return nsRectVisibility_kVisible; nsRect insetRect = scrollPortRect; insetRect.Deflate(aMinTwips, aMinTwips); if (r.YMost() <= insetRect.y) return nsRectVisibility_kAboveViewport; if (r.y >= insetRect.YMost()) return nsRectVisibility_kBelowViewport; if (r.XMost() <= insetRect.x) return nsRectVisibility_kLeftOfViewport; if (r.x >= insetRect.XMost()) return nsRectVisibility_kRightOfViewport; return nsRectVisibility_kVisible; } // GetLinkLocation: copy link location to clipboard nsresult PresShell::GetLinkLocation(nsIDOMNode* aNode, nsAString& aLocationString) const { #ifdef DEBUG_dr printf("dr :: PresShell::GetLinkLocation\n"); #endif NS_ENSURE_ARG_POINTER(aNode); nsresult rv; nsAutoString anchorText; static char strippedChars[] = {'\t','\r','\n'}; // are we an anchor? nsCOMPtr anchor(do_QueryInterface(aNode)); nsCOMPtr area; nsCOMPtr link; nsAutoString xlinkType; if (anchor) { rv = anchor->GetHref(anchorText); NS_ENSURE_SUCCESS(rv, rv); } else { // area? area = do_QueryInterface(aNode); if (area) { rv = area->GetHref(anchorText); NS_ENSURE_SUCCESS(rv, rv); } else { // link? link = do_QueryInterface(aNode); if (link) { rv = link->GetHref(anchorText); NS_ENSURE_SUCCESS(rv, rv); } else { // Xlink? nsCOMPtr element(do_QueryInterface(aNode)); if (element) { NS_NAMED_LITERAL_STRING(xlinkNS,"http://www.w3.org/1999/xlink"); element->GetAttributeNS(xlinkNS,NS_LITERAL_STRING("type"),xlinkType); if (xlinkType.EqualsLiteral("simple")) { element->GetAttributeNS(xlinkNS,NS_LITERAL_STRING("href"),anchorText); if (!anchorText.IsEmpty()) { // Resolve the full URI using baseURI property nsAutoString base; nsCOMPtr node(do_QueryInterface(aNode,&rv)); NS_ENSURE_SUCCESS(rv, rv); node->GetBaseURI(base); nsCOMPtr ios(do_GetService("@mozilla.org/network/io-service;1", &rv)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr baseURI; rv = ios->NewURI(NS_ConvertUTF16toUTF8(base),nsnull,nsnull,getter_AddRefs(baseURI)); NS_ENSURE_SUCCESS(rv, rv); nsCAutoString spec; rv = baseURI->Resolve(NS_ConvertUTF16toUTF8(anchorText),spec); NS_ENSURE_SUCCESS(rv, rv); CopyUTF8toUTF16(spec, anchorText); } } } } } } if (anchor || area || link || xlinkType.EqualsLiteral("simple")) { //Remove all the '\t', '\r' and '\n' from 'anchorText' anchorText.StripChars(strippedChars); aLocationString = anchorText; return NS_OK; } // if no link, fail. return NS_ERROR_FAILURE; } NS_IMETHODIMP_(void) PresShell::DispatchSynthMouseMove(nsGUIEvent *aEvent, PRBool aFlushOnHoverChange) { PRUint32 hoverGenerationBefore = mFrameConstructor->GetHoverGeneration(); nsEventStatus status; nsIView* targetView = nsIView::GetViewFor(aEvent->widget); targetView->GetViewManager()->DispatchEvent(aEvent, targetView, &status); if (aFlushOnHoverChange && hoverGenerationBefore != mFrameConstructor->GetHoverGeneration()) { // Flush so that the resulting reflow happens now so that our caller // can suppress any synthesized mouse moves caused by that reflow. FlushPendingNotifications(Flush_Layout); } } NS_IMETHODIMP_(void) PresShell::ClearMouseCapture(nsIView* aView) { if (gCaptureInfo.mContent) { if (aView) { // if a view was specified, ensure that the captured content is within // this view. nsIFrame* frame = gCaptureInfo.mContent->GetPrimaryFrame(); if (frame) { nsIView* view = frame->GetClosestView(); // if there is no view, capturing won't be handled any more, so // just release the capture. if (view) { do { if (view == aView) { NS_RELEASE(gCaptureInfo.mContent); // the view containing the captured content likely disappeared so // disable capture for now. gCaptureInfo.mAllowed = PR_FALSE; break; } view = view->GetParent(); } while (view); // return if the view wasn't found return; } } } NS_RELEASE(gCaptureInfo.mContent); } // disable mouse capture until the next mousedown as a dialog has opened // or a drag has started. Otherwise, someone could start capture during // the modal dialog or drag. gCaptureInfo.mAllowed = PR_FALSE; } nsresult PresShell::CaptureHistoryState(nsILayoutHistoryState** aState, PRBool aLeavingPage) { NS_TIME_FUNCTION_MIN(1.0); nsresult rv = NS_OK; NS_PRECONDITION(nsnull != aState, "null state pointer"); // We actually have to mess with the docshell here, since we want to // store the state back in it. // XXXbz this isn't really right, since this is being called in the // content viewer's Hide() method... by that point the docshell's // state could be wrong. We should sort out a better ownership // model for the layout history state. nsCOMPtr container = mPresContext->GetContainer(); if (!container) return NS_ERROR_FAILURE; nsCOMPtr docShell(do_QueryInterface(container)); if (!docShell) return NS_ERROR_FAILURE; nsCOMPtr historyState; docShell->GetLayoutHistoryState(getter_AddRefs(historyState)); if (!historyState) { // Create the document state object rv = NS_NewLayoutHistoryState(getter_AddRefs(historyState)); if (NS_FAILED(rv)) { *aState = nsnull; return rv; } docShell->SetLayoutHistoryState(historyState); } *aState = historyState; NS_IF_ADDREF(*aState); // Capture frame state for the entire frame hierarchy nsIFrame* rootFrame = FrameManager()->GetRootFrame(); if (!rootFrame) return NS_OK; // Capture frame state for the root scroll frame // Don't capture state when first creating doc element hierarchy // As the scroll position is 0 and this will cause us to lose // our previously saved place! if (aLeavingPage) { nsIFrame* scrollFrame = GetRootScrollFrame(); if (scrollFrame) { FrameManager()->CaptureFrameStateFor(scrollFrame, historyState, nsIStatefulFrame::eDocumentScrollState); } } FrameManager()->CaptureFrameState(rootFrame, historyState); return NS_OK; } void PresShell::UnsuppressAndInvalidate() { if (!mPresContext->EnsureVisible() || mHaveShutDown) { // No point; we're about to be torn down anyway. return; } mPaintingSuppressed = PR_FALSE; nsIFrame* rootFrame = FrameManager()->GetRootFrame(); if (rootFrame) { // let's assume that outline on a root frame is not supported nsRect rect(nsPoint(0, 0), rootFrame->GetSize()); rootFrame->Invalidate(rect); if (mCaretEnabled && mCaret) { mCaret->CheckCaretDrawingState(); } nsRootPresContext* rootPC = mPresContext->GetRootPresContext(); if (rootPC) { rootPC->RequestUpdatePluginGeometry(rootFrame); } } // now that painting is unsuppressed, focus may be set on the document nsPIDOMWindow *win = mDocument->GetWindow(); if (win) win->SetReadyForFocus(); if (!mHaveShutDown && mViewManager) mViewManager->SynthesizeMouseMove(PR_FALSE); } void PresShell::UnsuppressPainting() { if (mPaintSuppressionTimer) { mPaintSuppressionTimer->Cancel(); mPaintSuppressionTimer = nsnull; } if (mIsDocumentGone || !mPaintingSuppressed) return; // If we have reflows pending, just wait until we process // the reflows and get all the frames where we want them // before actually unlocking the painting. Otherwise // go ahead and unlock now. if (mDirtyRoots.Length() > 0) mShouldUnsuppressPainting = PR_TRUE; else UnsuppressAndInvalidate(); } // Post a request to handle an arbitrary callback after reflow has finished. nsresult PresShell::PostReflowCallback(nsIReflowCallback* aCallback) { void* result = AllocateMisc(sizeof(nsCallbackEventRequest)); if (NS_UNLIKELY(!result)) { return NS_ERROR_OUT_OF_MEMORY; } nsCallbackEventRequest* request = (nsCallbackEventRequest*)result; request->callback = aCallback; request->next = nsnull; if (mLastCallbackEventRequest) { mLastCallbackEventRequest = mLastCallbackEventRequest->next = request; } else { mFirstCallbackEventRequest = request; mLastCallbackEventRequest = request; } return NS_OK; } void PresShell::CancelReflowCallback(nsIReflowCallback* aCallback) { nsCallbackEventRequest* before = nsnull; nsCallbackEventRequest* node = mFirstCallbackEventRequest; while(node) { nsIReflowCallback* callback = node->callback; if (callback == aCallback) { nsCallbackEventRequest* toFree = node; if (node == mFirstCallbackEventRequest) { node = node->next; mFirstCallbackEventRequest = node; NS_ASSERTION(before == nsnull, "impossible"); } else { node = node->next; before->next = node; } if (toFree == mLastCallbackEventRequest) { mLastCallbackEventRequest = before; } FreeMisc(sizeof(nsCallbackEventRequest), toFree); } else { before = node; node = node->next; } } } void PresShell::CancelPostedReflowCallbacks() { while (mFirstCallbackEventRequest) { nsCallbackEventRequest* node = mFirstCallbackEventRequest; mFirstCallbackEventRequest = node->next; if (!mFirstCallbackEventRequest) { mLastCallbackEventRequest = nsnull; } nsIReflowCallback* callback = node->callback; FreeMisc(sizeof(nsCallbackEventRequest), node); if (callback) { callback->ReflowCallbackCanceled(); } } } void PresShell::HandlePostedReflowCallbacks(PRBool aInterruptible) { PRBool shouldFlush = PR_FALSE; while (mFirstCallbackEventRequest) { nsCallbackEventRequest* node = mFirstCallbackEventRequest; mFirstCallbackEventRequest = node->next; if (!mFirstCallbackEventRequest) { mLastCallbackEventRequest = nsnull; } nsIReflowCallback* callback = node->callback; FreeMisc(sizeof(nsCallbackEventRequest), node); if (callback) { if (callback->ReflowFinished()) { shouldFlush = PR_TRUE; } } } mozFlushType flushType = aInterruptible ? Flush_InterruptibleLayout : Flush_Layout; if (shouldFlush) FlushPendingNotifications(flushType); } PRBool PresShell::IsSafeToFlush() const { // Not safe if we are reflowing or in the middle of frame construction PRBool isSafeToFlush = !mIsReflowing && !mChangeNestCount; if (isSafeToFlush) { // Not safe if we are painting nsIViewManager* viewManager = GetViewManager(); if (viewManager) { PRBool isPainting = PR_FALSE; viewManager->IsPainting(isPainting); if (isPainting) { isSafeToFlush = PR_FALSE; } } } return isSafeToFlush; } void PresShell::FlushPendingNotifications(mozFlushType aType) { #ifdef NS_FUNCTION_TIMER NS_TIME_FUNCTION_DECLARE_DOCURL; static const char *flushTypeNames[] = { "Flush_Content", "Flush_ContentAndNotify", "Flush_Styles", "Flush_InterruptibleLayout", "Flush_Layout", "Flush_Display" }; NS_TIME_FUNCTION_MIN_FMT(1.0, "%s (line %d) (document: %s, type: %s)", MOZ_FUNCTION_NAME, __LINE__, docURL__.get(), flushTypeNames[aType - 1]); #endif NS_ASSERTION(aType >= Flush_Frames, "Why did we get called?"); PRBool isSafeToFlush = IsSafeToFlush(); isSafeToFlush = isSafeToFlush && nsContentUtils::IsSafeToRunScript(); NS_ASSERTION(!isSafeToFlush || mViewManager, "Must have view manager"); // Make sure the view manager stays alive while batching view updates. nsCOMPtr viewManagerDeathGrip = mViewManager; if (isSafeToFlush && mViewManager) { // Processing pending notifications can kill us, and some callers only // hold weak refs when calling FlushPendingNotifications(). :( nsCOMPtr kungFuDeathGrip(this); if (mResizeEvent.IsPending()) { FireResizeEvent(); if (mIsDestroying) { return; } } // Style reresolves not in conjunction with reflows can't cause // painting or geometry changes, so don't bother with view update // batching if we only have style reresolve nsIViewManager::UpdateViewBatch batch(mViewManager); // We need to make sure external resource documents are flushed too (for // example, svg filters that reference a filter in an external document // need the frames in the external document to be constructed for the // filter to work). We only need external resources to be flushed when the // main document is flushing >= Flush_Frames, so we flush external // resources here instead of nsDocument::FlushPendingNotifications. mDocument->FlushExternalResources(aType); // Force flushing of any pending content notifications that might have // queued up while our event was pending. That will ensure that we don't // construct frames for content right now that's still waiting to be // notified on, mDocument->FlushPendingNotifications(Flush_ContentAndNotify); // Process pending restyles, since any flush of the presshell wants // up-to-date style data. if (!mIsDestroying) { mPresContext->FlushPendingMediaFeatureValuesChanged(); // Flush any pending update of the user font set, since that could // cause style changes (for updating ex/ch units, and to cause a // reflow). mPresContext->FlushUserFontSet(); #ifdef MOZ_SMIL // Flush any requested SMIL samples. if (mDocument->HasAnimationController()) { mDocument->GetAnimationController()->FlushResampleRequests(); } #endif // MOZ_SMIL nsAutoScriptBlocker scriptBlocker; mFrameConstructor->CreateNeededFrames(); mFrameConstructor->ProcessPendingRestyles(); } // Process whatever XBL constructors those restyles queued up. This // ensures that onload doesn't fire too early and that we won't do extra // reflows after those constructors run. if (!mIsDestroying) { mDocument->BindingManager()->ProcessAttachedQueue(); } // Now those constructors might have posted restyle events. At the same // time, we still need up-to-date style data. In particular, reflow // depends on style being completely up to date. If it's not, then style // context reparenting, which can happen during reflow, might suddenly pick // up the new rules and we'll end up with frames whose style doesn't match // the frame type. if (!mIsDestroying) { nsAutoScriptBlocker scriptBlocker; mFrameConstructor->CreateNeededFrames(); mFrameConstructor->ProcessPendingRestyles(); } // There might be more pending constructors now, but we're not going to // worry about them. They can't be triggered during reflow, so we should // be good. if (aType >= (mSuppressInterruptibleReflows ? Flush_Layout : Flush_InterruptibleLayout) && !mIsDestroying) { mFrameConstructor->RecalcQuotesAndCounters(); mViewManager->FlushDelayedResize(); if (ProcessReflowCommands(aType < Flush_Layout) && mContentToScrollTo) { // We didn't get interrupted. Go ahead and scroll to our content DoScrollContentIntoView(mContentToScrollTo, mContentScrollVPosition, mContentScrollHPosition); mContentToScrollTo = nsnull; } } if (aType >= Flush_Layout) { // Flush plugin geometry. Don't flush plugin geometry for // interruptible layouts, since WillPaint does an interruptible // layout. nsRootPresContext* rootPresContext = mPresContext->GetRootPresContext(); if (rootPresContext) { rootPresContext->UpdatePluginGeometry(); } } PRUint32 updateFlags = NS_VMREFRESH_NO_SYNC; if (aType >= Flush_Display) { // Flushing paints, so perform the invalidates and drawing // immediately updateFlags = NS_VMREFRESH_IMMEDIATE; } else if (aType < Flush_InterruptibleLayout) { // Not flushing reflows, so do deferred invalidates. This will keep us // from possibly flushing out reflows due to invalidates being processed // at the end of this view batch. updateFlags = NS_VMREFRESH_DEFERRED; } batch.EndUpdateViewBatch(updateFlags); } } void PresShell::CharacterDataChanged(nsIDocument *aDocument, nsIContent* aContent, CharacterDataChangeInfo* aInfo) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected CharacterDataChanged"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); nsAutoCauseReflowNotifier crNotifier(this); if (mCaret) { // Invalidate the caret's current location before we call into the frame // constructor. It is important to do this now, and not wait until the // resulting reflow, because this call causes continuation frames of the // text frame the caret is in to forget what part of the content they // refer to, making it hard for them to return the correct continuation // frame to the caret. mCaret->InvalidateOutsideCaret(); } // Call this here so it only happens for real content mutations and // not cases when the frame constructor calls its own methods to force // frame reconstruction. nsIContent *container = aContent->GetParent(); PRUint32 selectorFlags = container ? (container->GetFlags() & NODE_ALL_SELECTOR_FLAGS) : 0; if (selectorFlags != 0 && !aContent->IsRootOfAnonymousSubtree()) { Element* element = container->AsElement(); if (aInfo->mAppend && !aContent->GetNextSibling()) mFrameConstructor->RestyleForAppend(element, aContent); else mFrameConstructor->RestyleForInsertOrChange(element, aContent); } mFrameConstructor->CharacterDataChanged(aContent, aInfo); VERIFY_STYLE_TREE; } void PresShell::ContentStatesChanged(nsIDocument* aDocument, nsIContent* aContent1, nsIContent* aContent2, PRInt32 aStateMask) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected ContentStatesChanged"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); if (mDidInitialReflow) { nsAutoCauseReflowNotifier crNotifier(this); mFrameConstructor->ContentStatesChanged(aContent1, aContent2, aStateMask); VERIFY_STYLE_TREE; } } void PresShell::DocumentStatesChanged(nsIDocument* aDocument, PRInt32 aStateMask) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected DocumentStatesChanged"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); if (mDidInitialReflow && mStyleSet->HasDocumentStateDependentStyle(mPresContext, mDocument->GetRootElement(), aStateMask)) { mFrameConstructor->PostRestyleEvent(mDocument->GetRootElement(), eRestyle_Subtree, NS_STYLE_HINT_NONE); VERIFY_STYLE_TREE; } if (aStateMask & NS_DOCUMENT_STATE_WINDOW_INACTIVE) { nsIFrame* root = FrameManager()->GetRootFrame(); if (root) { // It's a display root. So, invalidate the layer contents of // everything we can find. We need to do this because the contents // of controls etc can depend on whether the window is active, // and when a window becomes (in)active it just gets repainted // and we don't specifically invalidate each affected control. nsIWidget* widget = root->GetNearestWidget(); if (widget) { LayerManager* layerManager = widget->GetLayerManager(); if (layerManager) { FrameLayerBuilder::InvalidateAllThebesLayerContents(layerManager); } } } } } void PresShell::AttributeWillChange(nsIDocument* aDocument, nsIContent* aContent, PRInt32 aNameSpaceID, nsIAtom* aAttribute, PRInt32 aModType) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected AttributeChanged"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); // XXXwaterson it might be more elegant to wait until after the // initial reflow to begin observing the document. That would // squelch any other inappropriate notifications as well. if (mDidInitialReflow) { nsAutoCauseReflowNotifier crNotifier(this); mFrameConstructor->AttributeWillChange(aContent, aNameSpaceID, aAttribute, aModType); VERIFY_STYLE_TREE; } } void PresShell::AttributeChanged(nsIDocument* aDocument, nsIContent* aContent, PRInt32 aNameSpaceID, nsIAtom* aAttribute, PRInt32 aModType) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected AttributeChanged"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); // XXXwaterson it might be more elegant to wait until after the // initial reflow to begin observing the document. That would // squelch any other inappropriate notifications as well. if (mDidInitialReflow) { nsAutoCauseReflowNotifier crNotifier(this); mFrameConstructor->AttributeChanged(aContent, aNameSpaceID, aAttribute, aModType); VERIFY_STYLE_TREE; } } void PresShell::ContentAppended(nsIDocument *aDocument, nsIContent* aContainer, nsIContent* aFirstNewContent, PRInt32 aNewIndexInContainer) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected ContentAppended"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); NS_PRECONDITION(aContainer, "must have container"); if (!mDidInitialReflow) { return; } nsAutoCauseReflowNotifier crNotifier(this); // Call this here so it only happens for real content mutations and // not cases when the frame constructor calls its own methods to force // frame reconstruction. mFrameConstructor->RestyleForAppend(aContainer->AsElement(), aFirstNewContent); mFrameConstructor->ContentAppended(aContainer, aFirstNewContent, PR_TRUE); VERIFY_STYLE_TREE; } void PresShell::ContentInserted(nsIDocument* aDocument, nsIContent* aContainer, nsIContent* aChild, PRInt32 aIndexInContainer) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected ContentInserted"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); if (!mDidInitialReflow) { return; } nsAutoCauseReflowNotifier crNotifier(this); // Call this here so it only happens for real content mutations and // not cases when the frame constructor calls its own methods to force // frame reconstruction. if (aContainer) mFrameConstructor->RestyleForInsertOrChange(aContainer->AsElement(), aChild); mFrameConstructor->ContentInserted(aContainer, aChild, nsnull, PR_TRUE); VERIFY_STYLE_TREE; } void PresShell::ContentRemoved(nsIDocument *aDocument, nsIContent* aContainer, nsIContent* aChild, PRInt32 aIndexInContainer, nsIContent* aPreviousSibling) { NS_PRECONDITION(!mIsDocumentGone, "Unexpected ContentRemoved"); NS_PRECONDITION(aDocument == mDocument, "Unexpected aDocument"); // Make sure that the caret doesn't leave a turd where the child used to be. if (mCaret) { mCaret->InvalidateOutsideCaret(); } // Notify the ESM that the content has been removed, so that // it can clean up any state related to the content. mPresContext->EventStateManager()->ContentRemoved(aDocument, aChild); nsAutoCauseReflowNotifier crNotifier(this); // Call this here so it only happens for real content mutations and // not cases when the frame constructor calls its own methods to force // frame reconstruction. nsIContent* oldNextSibling; if (aContainer) { oldNextSibling = aContainer->GetChildAt(aIndexInContainer); } else { oldNextSibling = nsnull; } if (aContainer) mFrameConstructor->RestyleForRemove(aContainer->AsElement(), aChild, oldNextSibling); PRBool didReconstruct; mFrameConstructor->ContentRemoved(aContainer, aChild, oldNextSibling, nsCSSFrameConstructor::REMOVE_CONTENT, &didReconstruct); VERIFY_STYLE_TREE; } nsresult PresShell::ReconstructFrames(void) { nsCOMPtr kungFuDeathGrip(this); // Have to make sure that the content notifications are flushed before we // start messing with the frame model; otherwise we can get content doubling. mDocument->FlushPendingNotifications(Flush_ContentAndNotify); nsAutoCauseReflowNotifier crNotifier(this); mFrameConstructor->BeginUpdate(); nsresult rv = mFrameConstructor->ReconstructDocElementHierarchy(); VERIFY_STYLE_TREE; mFrameConstructor->EndUpdate(); return rv; } void nsIPresShell::ReconstructStyleDataInternal() { mStylesHaveChanged = PR_FALSE; if (mIsDestroying) { // We don't want to mess with restyles at this point return; } if (mPresContext) { mPresContext->RebuildUserFontSet(); } Element* root = mDocument->GetRootElement(); if (!mDidInitialReflow) { // Nothing to do here, since we have no frames yet return; } if (!root) { // No content to restyle return; } mFrameConstructor->PostRestyleEvent(root, eRestyle_Subtree, NS_STYLE_HINT_NONE); #ifdef ACCESSIBILITY InvalidateAccessibleSubtree(nsnull); #endif } void nsIPresShell::ReconstructStyleDataExternal() { ReconstructStyleDataInternal(); } void PresShell::StyleSheetAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, PRBool aDocumentSheet) { // We only care when enabled sheets are added NS_PRECONDITION(aStyleSheet, "Must have a style sheet!"); if (aStyleSheet->IsApplicable() && aStyleSheet->HasRules()) { mStylesHaveChanged = PR_TRUE; } } void PresShell::StyleSheetRemoved(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, PRBool aDocumentSheet) { // We only care when enabled sheets are removed NS_PRECONDITION(aStyleSheet, "Must have a style sheet!"); if (aStyleSheet->IsApplicable() && aStyleSheet->HasRules()) { mStylesHaveChanged = PR_TRUE; } } void PresShell::StyleSheetApplicableStateChanged(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, PRBool aApplicable) { if (aStyleSheet->HasRules()) { mStylesHaveChanged = PR_TRUE; } } void PresShell::StyleRuleChanged(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aOldStyleRule, nsIStyleRule* aNewStyleRule) { mStylesHaveChanged = PR_TRUE; } void PresShell::StyleRuleAdded(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRule) { mStylesHaveChanged = PR_TRUE; } void PresShell::StyleRuleRemoved(nsIDocument *aDocument, nsIStyleSheet* aStyleSheet, nsIStyleRule* aStyleRule) { mStylesHaveChanged = PR_TRUE; } nsIFrame* PresShell::GetRealPrimaryFrameFor(nsIContent* aContent) const { if (aContent->GetDocument() != GetDocument()) { return nsnull; } nsIFrame *primaryFrame = aContent->GetPrimaryFrame(); if (!primaryFrame) return nsnull; return nsPlaceholderFrame::GetRealFrameFor(primaryFrame); } nsIFrame* PresShell::GetPlaceholderFrameFor(nsIFrame* aFrame) const { return FrameManager()->GetPlaceholderFrameFor(aFrame); } nsresult PresShell::RenderDocument(const nsRect& aRect, PRUint32 aFlags, nscolor aBackgroundColor, gfxContext* aThebesContext) { NS_TIME_FUNCTION_WITH_DOCURL; NS_ENSURE_TRUE(!(aFlags & RENDER_IS_UNTRUSTED), NS_ERROR_NOT_IMPLEMENTED); // Set up the rectangle as the path in aThebesContext gfxRect r(0, 0, nsPresContext::AppUnitsToFloatCSSPixels(aRect.width), nsPresContext::AppUnitsToFloatCSSPixels(aRect.height)); aThebesContext->NewPath(); #ifdef MOZ_GFX_OPTIMIZE_MOBILE aThebesContext->Rectangle(r, PR_TRUE); #else aThebesContext->Rectangle(r); #endif nsIFrame* rootFrame = FrameManager()->GetRootFrame(); if (!rootFrame) { // Nothing to paint, just fill the rect aThebesContext->SetColor(gfxRGBA(aBackgroundColor)); aThebesContext->Fill(); return NS_OK; } gfxContextAutoSaveRestore save(aThebesContext); gfxContext::GraphicsOperator oldOperator = aThebesContext->CurrentOperator(); if (oldOperator == gfxContext::OPERATOR_OVER) { // Clip to the destination rectangle before we push the group, // to limit the size of the temporary surface aThebesContext->Clip(); } // we want the window to be composited as a single image using // whatever operator was set; set OPERATOR_OVER here, which is // either already the case, or overrides the operator in a group. // the original operator will be present when we PopGroup. // we can avoid using a temporary surface if we're using OPERATOR_OVER // and our background color has no alpha (so we'll be compositing on top // of a fully opaque solid color region) PRBool needsGroup = NS_GET_A(aBackgroundColor) < 0xff || oldOperator != gfxContext::OPERATOR_OVER; if (needsGroup) { aThebesContext->PushGroup(NS_GET_A(aBackgroundColor) == 0xff ? gfxASurface::CONTENT_COLOR : gfxASurface::CONTENT_COLOR_ALPHA); aThebesContext->Save(); if (oldOperator != gfxContext::OPERATOR_OVER) { // Clip now while we paint to the temporary surface. For // non-source-bounded operators (e.g., SOURCE), we need to do clip // here after we've pushed the group, so that eventually popping // the group and painting it will be able to clear the entire // destination surface. aThebesContext->Clip(); aThebesContext->SetOperator(gfxContext::OPERATOR_OVER); } } aThebesContext->Translate(gfxPoint(-nsPresContext::AppUnitsToFloatCSSPixels(aRect.x), -nsPresContext::AppUnitsToFloatCSSPixels(aRect.y))); nsIDeviceContext* devCtx = mPresContext->DeviceContext(); gfxFloat scale = gfxFloat(devCtx->AppUnitsPerDevPixel())/nsPresContext::AppUnitsPerCSSPixel(); aThebesContext->Scale(scale, scale); // Since canvas APIs use floats to set up their matrices, we may have // some slight inaccuracy here. Adjust matrix components that are // integers up to the accuracy of floats to be those integers. aThebesContext->NudgeCurrentMatrixToIntegers(); nsCOMPtr rc; devCtx->CreateRenderingContextInstance(*getter_AddRefs(rc)); rc->Init(devCtx, aThebesContext); PRUint32 flags = nsLayoutUtils::PAINT_SYNC_DECODE_IMAGES | nsLayoutUtils::PAINT_IGNORE_SUPPRESSION; if (aFlags & RENDER_USE_WIDGET_LAYERS) { flags |= nsLayoutUtils::PAINT_WIDGET_LAYERS; } if (!(aFlags & RENDER_CARET)) { flags |= nsLayoutUtils::PAINT_HIDE_CARET; } if (aFlags & RENDER_IGNORE_VIEWPORT_SCROLLING) { flags |= nsLayoutUtils::PAINT_IGNORE_VIEWPORT_SCROLLING; } nsLayoutUtils::PaintFrame(rc, rootFrame, nsRegion(aRect), aBackgroundColor, flags); // if we had to use a group, paint it to the destination now if (needsGroup) { aThebesContext->Restore(); aThebesContext->PopGroupToSource(); aThebesContext->Paint(); } return NS_OK; } /* * Clip the display list aList to a range. Returns the clipped * rectangle surrounding the range. */ nsRect PresShell::ClipListToRange(nsDisplayListBuilder *aBuilder, nsDisplayList* aList, nsIRange* aRange) { NS_TIME_FUNCTION_WITH_DOCURL; // iterate though the display items and add up the bounding boxes of each. // This will allow the total area of the frames within the range to be // determined. To do this, remove an item from the bottom of the list, check // whether it should be part of the range, and if so, append it to the top // of the temporary list tmpList. If the item is a text frame at the end of // the selection range, wrap it in an nsDisplayClip to clip the display to // the portion of the text frame that is part of the selection. Then, append // the wrapper to the top of the list. Otherwise, just delete the item and // don't append it. nsRect surfaceRect; nsDisplayList tmpList; nsDisplayItem* i; while ((i = aList->RemoveBottom())) { // itemToInsert indiciates the item that should be inserted into the // temporary list. If null, no item should be inserted. nsDisplayItem* itemToInsert = nsnull; nsIFrame* frame = i->GetUnderlyingFrame(); if (frame) { nsIContent* content = frame->GetContent(); if (content) { PRBool atStart = (content == aRange->GetStartParent()); PRBool atEnd = (content == aRange->GetEndParent()); if ((atStart || atEnd) && frame->GetType() == nsGkAtoms::textFrame) { PRInt32 frameStartOffset, frameEndOffset; frame->GetOffsets(frameStartOffset, frameEndOffset); PRInt32 hilightStart = atStart ? NS_MAX(aRange->StartOffset(), frameStartOffset) : frameStartOffset; PRInt32 hilightEnd = atEnd ? NS_MIN(aRange->EndOffset(), frameEndOffset) : frameEndOffset; if (hilightStart < hilightEnd) { // determine the location of the start and end edges of the range. nsPoint startPoint, endPoint; frame->GetPointFromOffset(hilightStart, &startPoint); frame->GetPointFromOffset(hilightEnd, &endPoint); // the clip rectangle is determined by taking the the start and // end points of the range, offset from the reference frame. // Because of rtl, the end point may be to the left of the // start point, so x is set to the lowest value nsRect textRect(aBuilder->ToReferenceFrame(frame), frame->GetSize()); nscoord x = NS_MIN(startPoint.x, endPoint.x); textRect.x += x; textRect.width = NS_MAX(startPoint.x, endPoint.x) - x; surfaceRect.UnionRect(surfaceRect, textRect); // wrap the item in an nsDisplayClip so that it can be clipped to // the selection. If the allocation fails, fall through and delete // the item below. itemToInsert = new (aBuilder)nsDisplayClip(frame, frame, i, textRect); } } // Don't try to descend into subdocuments. // If this ever changes we'd need to add handling for subdocuments with // different zoom levels. else if (content->GetCurrentDoc() == aRange->GetStartParent()->GetCurrentDoc()) { // if the node is within the range, append it to the temporary list PRBool before, after; nsresult rv = nsRange::CompareNodeToRange(content, aRange, &before, &after); if (NS_SUCCEEDED(rv) && !before && !after) { itemToInsert = i; surfaceRect.UnionRect(surfaceRect, i->GetBounds(aBuilder)); } } } } // insert the item into the list if necessary. If the item has a child // list, insert that as well nsDisplayList* sublist = i->GetList(); if (itemToInsert || sublist) { tmpList.AppendToTop(itemToInsert ? itemToInsert : i); // if the item is a list, iterate over it as well if (sublist) surfaceRect.UnionRect(surfaceRect, ClipListToRange(aBuilder, sublist, aRange)); } else { // otherwise, just delete the item and don't readd it to the list i->~nsDisplayItem(); } } // now add all the items back onto the original list again aList->AppendToTop(&tmpList); return surfaceRect; } #ifdef DEBUG #include static PRBool gDumpRangePaintList = PR_FALSE; #endif RangePaintInfo* PresShell::CreateRangePaintInfo(nsIDOMRange* aRange, nsRect& aSurfaceRect, PRBool aForPrimarySelection) { NS_TIME_FUNCTION_WITH_DOCURL; RangePaintInfo* info = nsnull; nsCOMPtr range = do_QueryInterface(aRange); if (!range) return nsnull; nsIFrame* ancestorFrame; nsIFrame* rootFrame = GetRootFrame(); // If the start or end of the range is the document, just use the root // frame, otherwise get the common ancestor of the two endpoints of the // range. nsINode* startParent = range->GetStartParent(); nsINode* endParent = range->GetEndParent(); nsIDocument* doc = startParent->GetCurrentDoc(); if (startParent == doc || endParent == doc) { ancestorFrame = rootFrame; } else { nsINode* ancestor = nsContentUtils::GetCommonAncestor(startParent, endParent); NS_ASSERTION(!ancestor || ancestor->IsNodeOfType(nsINode::eCONTENT), "common ancestor is not content"); if (!ancestor || !ancestor->IsNodeOfType(nsINode::eCONTENT)) return nsnull; nsIContent* ancestorContent = static_cast(ancestor); ancestorFrame = ancestorContent->GetPrimaryFrame(); // use the nearest ancestor frame that includes all continuations as the // root for building the display list while (ancestorFrame && nsLayoutUtils::GetNextContinuationOrSpecialSibling(ancestorFrame)) ancestorFrame = ancestorFrame->GetParent(); } if (!ancestorFrame) return nsnull; info = new RangePaintInfo(range, ancestorFrame); if (!info) return nsnull; nsRect ancestorRect = ancestorFrame->GetOverflowRect(); // get a display list containing the range if (aForPrimarySelection) { info->mBuilder.SetSelectedFramesOnly(); } info->mBuilder.EnterPresShell(ancestorFrame, ancestorRect); ancestorFrame->BuildDisplayListForStackingContext(&info->mBuilder, ancestorRect, &info->mList); info->mBuilder.LeavePresShell(ancestorFrame, ancestorRect); #ifdef DEBUG if (gDumpRangePaintList) { fprintf(stderr, "CreateRangePaintInfo --- before ClipListToRange:\n"); nsFrame::PrintDisplayList(&(info->mBuilder), info->mList); } #endif nsRect rangeRect = ClipListToRange(&info->mBuilder, &info->mList, range); #ifdef DEBUG if (gDumpRangePaintList) { fprintf(stderr, "CreateRangePaintInfo --- after ClipListToRange:\n"); nsFrame::PrintDisplayList(&(info->mBuilder), info->mList); } #endif // determine the offset of the reference frame for the display list // to the root frame. This will allow the coordinates used when painting // to all be offset from the same point info->mRootOffset = ancestorFrame->GetOffsetTo(rootFrame); rangeRect.MoveBy(info->mRootOffset); aSurfaceRect.UnionRect(aSurfaceRect, rangeRect); return info; } already_AddRefed PresShell::PaintRangePaintInfo(nsTArray >* aItems, nsISelection* aSelection, nsIntRegion* aRegion, nsRect aArea, nsIntPoint& aPoint, nsIntRect* aScreenRect) { NS_TIME_FUNCTION_WITH_DOCURL; nsPresContext* pc = GetPresContext(); if (!pc || aArea.width == 0 || aArea.height == 0) return nsnull; nsIDeviceContext* deviceContext = pc->DeviceContext(); // use the rectangle to create the surface nsIntRect pixelArea = aArea.ToOutsidePixels(pc->AppUnitsPerDevPixel()); // if the area of the image is larger than the maximum area, scale it down float scale = 0.0; nsIntRect rootScreenRect = GetRootFrame()->GetScreenRect(); // if the image is larger in one or both directions than half the size of // the available screen area, scale the image down to that size. nsRect maxSize; deviceContext->GetClientRect(maxSize); nscoord maxWidth = pc->AppUnitsToDevPixels(maxSize.width >> 1); nscoord maxHeight = pc->AppUnitsToDevPixels(maxSize.height >> 1); PRBool resize = (pixelArea.width > maxWidth || pixelArea.height > maxHeight); if (resize) { scale = 1.0; // divide the maximum size by the image size in both directions. Whichever // direction produces the smallest result determines how much should be // scaled. if (pixelArea.width > maxWidth) scale = NS_MIN(scale, float(maxWidth) / pixelArea.width); if (pixelArea.height > maxHeight) scale = NS_MIN(scale, float(maxHeight) / pixelArea.height); pixelArea.width = NSToIntFloor(float(pixelArea.width) * scale); pixelArea.height = NSToIntFloor(float(pixelArea.height) * scale); // adjust the screen position based on the rescaled size nscoord left = rootScreenRect.x + pixelArea.x; nscoord top = rootScreenRect.y + pixelArea.y; aScreenRect->x = NSToIntFloor(aPoint.x - float(aPoint.x - left) * scale); aScreenRect->y = NSToIntFloor(aPoint.y - float(aPoint.y - top) * scale); } else { // move aScreenRect to the position of the surface in screen coordinates aScreenRect->MoveTo(rootScreenRect.x + pixelArea.x, rootScreenRect.y + pixelArea.y); } aScreenRect->width = pixelArea.width; aScreenRect->height = pixelArea.height; gfxImageSurface* surface = new gfxImageSurface(gfxIntSize(pixelArea.width, pixelArea.height), gfxImageSurface::ImageFormatARGB32); if (!surface || surface->CairoStatus()) { delete surface; return nsnull; } // clear the image gfxContext context(surface); context.SetOperator(gfxContext::OPERATOR_CLEAR); context.Rectangle(gfxRect(0, 0, pixelArea.width, pixelArea.height)); context.Fill(); nsCOMPtr rc; deviceContext->CreateRenderingContextInstance(*getter_AddRefs(rc)); rc->Init(deviceContext, surface); if (aRegion) { // Convert aRegion from CSS pixels to dev pixels nsIntRegion region = aRegion->ToAppUnits(nsPresContext::AppUnitsPerCSSPixel()) .ToOutsidePixels(pc->AppUnitsPerDevPixel()); rc->SetClipRegion(region, nsClipCombine_kReplace); } if (resize) rc->Scale(scale, scale); // translate so that points are relative to the surface area rc->Translate(-aArea.x, -aArea.y); // temporarily hide the selection so that text is drawn normally. If a // selection is being rendered, use that, otherwise use the presshell's // selection. nsCOMPtr frameSelection; if (aSelection) { nsCOMPtr selpriv = do_QueryInterface(aSelection); selpriv->GetFrameSelection(getter_AddRefs(frameSelection)); } else { frameSelection = FrameSelection(); } PRInt16 oldDisplaySelection = frameSelection->GetDisplaySelection(); frameSelection->SetDisplaySelection(nsISelectionController::SELECTION_HIDDEN); // next, paint each range in the selection PRInt32 count = aItems->Length(); for (PRInt32 i = 0; i < count; i++) { RangePaintInfo* rangeInfo = (*aItems)[i]; // the display lists paint relative to the offset from the reference // frame, so translate the rendering context nsIRenderingContext::AutoPushTranslation translate(rc, rangeInfo->mRootOffset.x, rangeInfo->mRootOffset.y); aArea.MoveBy(-rangeInfo->mRootOffset.x, -rangeInfo->mRootOffset.y); nsRegion visible(aArea); rangeInfo->mList.ComputeVisibility(&rangeInfo->mBuilder, &visible, nsnull); rangeInfo->mList.PaintRoot(&rangeInfo->mBuilder, rc, nsDisplayList::PAINT_DEFAULT); aArea.MoveBy(rangeInfo->mRootOffset.x, rangeInfo->mRootOffset.y); } // restore the old selection display state frameSelection->SetDisplaySelection(oldDisplaySelection); NS_ADDREF(surface); return surface; } already_AddRefed PresShell::RenderNode(nsIDOMNode* aNode, nsIntRegion* aRegion, nsIntPoint& aPoint, nsIntRect* aScreenRect) { // area will hold the size of the surface needed to draw the node, measured // from the root frame. nsRect area; nsTArray > rangeItems; // nothing to draw if the node isn't in a document nsCOMPtr node = do_QueryInterface(aNode); if (!node->IsInDoc()) return nsnull; nsCOMPtr range; NS_NewRange(getter_AddRefs(range)); if (NS_FAILED(range->SelectNode(aNode))) return nsnull; RangePaintInfo* info = CreateRangePaintInfo(range, area, PR_FALSE); if (info && !rangeItems.AppendElement(info)) { delete info; return nsnull; } if (aRegion) { // combine the area with the supplied region nsIntRect rrectPixels = aRegion->GetBounds(); nsRect rrect = rrectPixels.ToAppUnits(nsPresContext::AppUnitsPerCSSPixel()); area.IntersectRect(area, rrect); nsPresContext* pc = GetPresContext(); if (!pc) return nsnull; // move the region so that it is offset from the topleft corner of the surface aRegion->MoveBy(-pc->AppUnitsToDevPixels(area.x), -pc->AppUnitsToDevPixels(area.y)); } return PaintRangePaintInfo(&rangeItems, nsnull, aRegion, area, aPoint, aScreenRect); } already_AddRefed PresShell::RenderSelection(nsISelection* aSelection, nsIntPoint& aPoint, nsIntRect* aScreenRect) { // area will hold the size of the surface needed to draw the selection, // measured from the root frame. nsRect area; nsTArray > rangeItems; // iterate over each range and collect them into the rangeItems array. // This is done so that the size of selection can be determined so as // to allocate a surface area PRInt32 numRanges; aSelection->GetRangeCount(&numRanges); NS_ASSERTION(numRanges > 0, "RenderSelection called with no selection"); for (PRInt32 r = 0; r < numRanges; r++) { nsCOMPtr range; aSelection->GetRangeAt(r, getter_AddRefs(range)); RangePaintInfo* info = CreateRangePaintInfo(range, area, PR_TRUE); if (info && !rangeItems.AppendElement(info)) { delete info; return nsnull; } } return PaintRangePaintInfo(&rangeItems, aSelection, nsnull, area, aPoint, aScreenRect); } static PRBool AddCanvasBackgroundColor(const nsDisplayList& aList, nsIFrame* aCanvasFrame, nscolor aColor) { for (nsDisplayItem* i = aList.GetBottom(); i; i = i->GetAbove()) { if (i->GetUnderlyingFrame() == aCanvasFrame && i->GetType() == nsDisplayItem::TYPE_CANVAS_BACKGROUND) { nsDisplayCanvasBackground* bg = static_cast(i); bg->SetExtraBackgroundColor(aColor); return PR_TRUE; } nsDisplayList* sublist = i->GetList(); if (sublist && AddCanvasBackgroundColor(*sublist, aCanvasFrame, aColor)) return PR_TRUE; } return PR_FALSE; } nsresult PresShell::AddCanvasBackgroundColorItem(nsDisplayListBuilder& aBuilder, nsDisplayList& aList, nsIFrame* aFrame, const nsRect& aBounds, nscolor aBackstopColor, PRBool aForceDraw) { // We don't want to add an item for the canvas background color if the frame // (sub)tree we are painting doesn't include any canvas frames. There isn't // an easy way to check this directly, but if we check if the root of the // (sub)tree we are painting is a canvas frame that should cover us in all // cases (it will usually be a viewport frame when we have a canvas frame in // the (sub)tree). if (!aForceDraw && !nsCSSRendering::IsCanvasFrame(aFrame)) return NS_OK; nscolor bgcolor = NS_ComposeColors(aBackstopColor, mCanvasBackgroundColor); // To make layers work better, we want to avoid having a big non-scrolled // color background behind a scrolled transparent background. Instead, // we'll try to move the color background into the scrolled content // by making nsDisplayCanvasBackground paint it. if (!aFrame->GetParent()) { nsIScrollableFrame* sf = aFrame->PresContext()->PresShell()->GetRootScrollFrameAsScrollable(); if (sf) { nsCanvasFrame* canvasFrame = do_QueryFrame(sf->GetScrolledFrame()); if (canvasFrame && canvasFrame->IsVisibleForPainting(&aBuilder)) { if (AddCanvasBackgroundColor(aList, canvasFrame, bgcolor)) return NS_OK; } } } return aList.AppendNewToBottom( new (&aBuilder) nsDisplaySolidColor(aFrame, aBounds, bgcolor)); } void PresShell::UpdateCanvasBackground() { // If we have a frame tree and it has style information that // specifies the background color of the canvas, update our local // cache of that color. nsIFrame* rootFrame = FrameConstructor()->GetRootElementStyleFrame(); if (rootFrame) { nsStyleContext* bgStyle = nsCSSRendering::FindRootFrameBackground(rootFrame); // XXX We should really be passing the canvasframe, not the root element // style frame but we don't have access to the canvasframe here. It isn't // a problem because only a few frames can return something other than true // and none of them would be a canvas frame or root element style frame. mCanvasBackgroundColor = nsCSSRendering::DetermineBackgroundColor(GetPresContext(), bgStyle, rootFrame); } // If the root element of the document (ie html) has style 'display: none' // then the document's background color does not get drawn; cache the // color we actually draw. if (!FrameConstructor()->GetRootElementFrame()) { mCanvasBackgroundColor = mPresContext->DefaultBackgroundColor(); } } nscolor PresShell::ComputeBackstopColor(nsIView* aDisplayRoot) { nsIWidget* widget = aDisplayRoot->GetWidget(); if (widget && widget->GetTransparencyMode() != eTransparencyOpaque) { // Within a transparent widget, so the backstop color must be // totally transparent. return NS_RGBA(0,0,0,0); } // Within an opaque widget (or no widget at all), so the backstop // color must be totally opaque. The user's default background // as reported by the prescontext is guaranteed to be opaque. return GetPresContext()->DefaultBackgroundColor(); } struct PaintParams { nsIFrame* mFrame; nsPoint mOffsetToWidget; const nsRegion* mDirtyRegion; nscolor mBackgroundColor; }; static void DrawThebesLayer(ThebesLayer* aLayer, gfxContext* aContext, const nsIntRegion& aRegionToDraw, const nsIntRegion& aRegionToInvalidate, void* aCallbackData) { PaintParams* params = static_cast(aCallbackData); nsIFrame* frame = params->mFrame; if (frame) { // We're drawing into a child window. nsIDeviceContext* devCtx = frame->PresContext()->DeviceContext(); nsCOMPtr rc; nsresult rv = devCtx->CreateRenderingContextInstance(*getter_AddRefs(rc)); if (NS_SUCCEEDED(rv)) { rc->Init(devCtx, aContext); nsIRenderingContext::AutoPushTranslation push(rc, params->mOffsetToWidget.x, params->mOffsetToWidget.y); nsLayoutUtils::PaintFrame(rc, frame, *params->mDirtyRegion, params->mBackgroundColor, nsLayoutUtils::PAINT_WIDGET_LAYERS); } } else { aContext->NewPath(); aContext->SetColor(gfxRGBA(params->mBackgroundColor)); nsIntRect dirtyRect = aRegionToDraw.GetBounds(); aContext->Rectangle( gfxRect(dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height)); aContext->Fill(); } } NS_IMETHODIMP PresShell::Paint(nsIView* aDisplayRoot, nsIView* aViewToPaint, nsIWidget* aWidgetToPaint, const nsRegion& aDirtyRegion, const nsIntRegion& aIntDirtyRegion, PRBool aPaintDefaultBackground, PRBool aWillSendDidPaint) { #ifdef NS_FUNCTION_TIMER NS_TIME_FUNCTION_DECLARE_DOCURL; const nsRect& bounds__ = aDirtyRegion.GetBounds(); NS_TIME_FUNCTION_MIN_FMT(1.0, "%s (line %d) (document: %s, dirty rect: (<%f, %f>, <%f, %f>)", MOZ_FUNCTION_NAME, __LINE__, docURL__.get(), NSCoordToFloat(bounds__.x), NSCoordToFloat(bounds__.y), NSCoordToFloat(bounds__.XMost()), NSCoordToFloat(bounds__.YMost())); #endif nsPresContext* presContext = GetPresContext(); AUTO_LAYOUT_PHASE_ENTRY_POINT(presContext, Paint); NS_ASSERTION(!mIsDestroying, "painting a destroyed PresShell"); NS_ASSERTION(aDisplayRoot, "null view"); NS_ASSERTION(aViewToPaint, "null view"); NS_ASSERTION(aWidgetToPaint, "Can't paint without a widget"); nscolor bgcolor = ComputeBackstopColor(aDisplayRoot); nsIFrame* frame = aPaintDefaultBackground ? nsnull : static_cast(aDisplayRoot->GetClientData()); if (frame && aViewToPaint == aDisplayRoot) { // Defer invalidates that are triggered during painting, and discard // invalidates of areas that are already being repainted. // The layer system can trigger invalidates during painting // (see FrameLayerBuilder). frame->BeginDeferringInvalidatesForDisplayRoot(aDirtyRegion); // We can paint directly into the widget using its layer manager. // When we get rid of child widgets, this will be the only path we // need. (aPaintDefaultBackground will never be needed since the // chrome can always paint a default background.) nsLayoutUtils::PaintFrame(nsnull, frame, aDirtyRegion, bgcolor, nsLayoutUtils::PAINT_WIDGET_LAYERS); frame->EndDeferringInvalidatesForDisplayRoot(); return NS_OK; } if (frame) { // Defer invalidates that are triggered during painting, and discard // invalidates of areas that are already being repainted. frame->BeginDeferringInvalidatesForDisplayRoot(aDirtyRegion); } LayerManager* layerManager = aWidgetToPaint->GetLayerManager(); NS_ASSERTION(layerManager, "Must be in paint event"); layerManager->BeginTransaction(); nsRefPtr root = layerManager->CreateThebesLayer(); if (root) { root->SetVisibleRegion(aIntDirtyRegion); layerManager->SetRoot(root); } if (!frame) { bgcolor = NS_ComposeColors(bgcolor, mCanvasBackgroundColor); } PaintParams params = { frame, aDisplayRoot->GetOffsetToWidget(aWidgetToPaint), &aDirtyRegion, bgcolor }; layerManager->EndTransaction(DrawThebesLayer, ¶ms); if (frame) { frame->EndDeferringInvalidatesForDisplayRoot(); } return NS_OK; } // static void nsIPresShell::SetCapturingContent(nsIContent* aContent, PRUint8 aFlags) { NS_IF_RELEASE(gCaptureInfo.mContent); // only set capturing content if allowed or the CAPTURE_IGNOREALLOWED flag // is used if ((aFlags & CAPTURE_IGNOREALLOWED) || gCaptureInfo.mAllowed) { if (aContent) { NS_ADDREF(gCaptureInfo.mContent = aContent); } gCaptureInfo.mRetargetToElement = (aFlags & CAPTURE_RETARGETTOELEMENT) != 0; gCaptureInfo.mPreventDrag = (aFlags & CAPTURE_PREVENTDRAG) != 0; } } nsIFrame* PresShell::GetCurrentEventFrame() { if (NS_UNLIKELY(mIsDestroying)) { return nsnull; } if (!mCurrentEventFrame && mCurrentEventContent) { // Make sure the content still has a document reference. If not, // then we assume it is no longer in the content tree and the // frame shouldn't get an event, nor should we even assume its // safe to try and find the frame. if (mCurrentEventContent->GetDocument()) { mCurrentEventFrame = mCurrentEventContent->GetPrimaryFrame(); } } return mCurrentEventFrame; } nsIFrame* PresShell::GetEventTargetFrame() { return GetCurrentEventFrame(); } already_AddRefed PresShell::GetEventTargetContent(nsEvent* aEvent) { nsIContent* content = nsnull; if (mCurrentEventContent) { content = mCurrentEventContent; NS_IF_ADDREF(content); } else { nsIFrame* currentEventFrame = GetCurrentEventFrame(); if (currentEventFrame) { currentEventFrame->GetContentForEvent(mPresContext, aEvent, &content); } else { content = nsnull; } } return content; } void PresShell::PushCurrentEventInfo(nsIFrame* aFrame, nsIContent* aContent) { if (mCurrentEventFrame || mCurrentEventContent) { mCurrentEventFrameStack.InsertElementAt(0, mCurrentEventFrame); mCurrentEventContentStack.InsertObjectAt(mCurrentEventContent, 0); } mCurrentEventFrame = aFrame; mCurrentEventContent = aContent; } void PresShell::PopCurrentEventInfo() { mCurrentEventFrame = nsnull; mCurrentEventContent = nsnull; if (0 != mCurrentEventFrameStack.Length()) { mCurrentEventFrame = mCurrentEventFrameStack.ElementAt(0); mCurrentEventFrameStack.RemoveElementAt(0); mCurrentEventContent = mCurrentEventContentStack.ObjectAt(0); mCurrentEventContentStack.RemoveObjectAt(0); } } PRBool PresShell::InZombieDocument(nsIContent *aContent) { // If a content node points to a null document, or the document is not // attached to a window, then it is possibly in a zombie document, // about to be replaced by a newly loading document. // Such documents cannot handle DOM events. // It might actually be in a node not attached to any document, // in which case there is not parent presshell to retarget it to. nsIDocument *doc = aContent->GetDocument(); return !doc || !doc->GetWindow(); } already_AddRefed PresShell::GetRootWindow() { nsCOMPtr window = do_QueryInterface(mDocument->GetWindow()); if (window) { nsCOMPtr rootWindow = window->GetPrivateRoot(); NS_ASSERTION(rootWindow, "nsPIDOMWindow::GetPrivateRoot() returns NULL"); return rootWindow.forget(); } // If we don't have DOM window, we're zombie, we should find the root window // with our parent shell. nsCOMPtr parent = GetParentPresShell(); NS_ENSURE_TRUE(parent, nsnull); return parent->GetRootWindow(); } already_AddRefed PresShell::GetParentPresShell() { NS_ENSURE_TRUE(mPresContext, nsnull); nsCOMPtr container = mPresContext->GetContainer(); if (!container) { container = do_QueryReferent(mForwardingContainer); } // Now, find the parent pres shell and send the event there nsCOMPtr treeItem = do_QueryInterface(container); // Might have gone away, or never been around to start with NS_ENSURE_TRUE(treeItem, nsnull); nsCOMPtr parentTreeItem; treeItem->GetParent(getter_AddRefs(parentTreeItem)); nsCOMPtr parentDocShell = do_QueryInterface(parentTreeItem); NS_ENSURE_TRUE(parentDocShell && treeItem != parentTreeItem, nsnull); nsIPresShell* parentPresShell = nsnull; parentDocShell->GetPresShell(&parentPresShell); return parentPresShell; } nsresult PresShell::RetargetEventToParent(nsGUIEvent* aEvent, nsEventStatus* aEventStatus) { // Send this events straight up to the parent pres shell. // We do this for keystroke events in zombie documents or if either a frame // or a root content is not present. // That way at least the UI key bindings can work. nsCOMPtr kungFuDeathGrip(this); nsCOMPtr parentPresShell = GetParentPresShell(); NS_ENSURE_TRUE(parentPresShell, NS_ERROR_FAILURE); nsCOMPtr parentViewObserver = do_QueryInterface(parentPresShell); if (!parentViewObserver) { return NS_ERROR_FAILURE; } // Fake the event as though it'ss from the parent pres shell's root view. nsIView *parentRootView; parentPresShell->GetViewManager()->GetRootView(parentRootView); sDontRetargetEvents = PR_TRUE; nsresult rv = parentViewObserver->HandleEvent(parentRootView, aEvent, aEventStatus); sDontRetargetEvents = PR_FALSE; return rv; } void PresShell::DisableNonTestMouseEvents(PRBool aDisable) { sDisableNonTestMouseEvents = aDisable; } already_AddRefed PresShell::GetFocusedDOMWindowInOurWindow() { nsCOMPtr rootWindow = GetRootWindow(); NS_ENSURE_TRUE(rootWindow, nsnull); nsPIDOMWindow* focusedWindow; nsFocusManager::GetFocusedDescendant(rootWindow, PR_TRUE, &focusedWindow); return focusedWindow; } NS_IMETHODIMP PresShell::HandleEvent(nsIView *aView, nsGUIEvent* aEvent, nsEventStatus* aEventStatus) { NS_ASSERTION(aView, "null view"); if (mIsDestroying || !nsContentUtils::IsSafeToRunScript() || (sDisableNonTestMouseEvents && NS_IS_MOUSE_EVENT(aEvent) && !(aEvent->flags & NS_EVENT_FLAG_SYNTHETIC_TEST_EVENT))) { return NS_OK; } NS_TIME_FUNCTION_MIN(1.0); #ifdef ACCESSIBILITY if (aEvent->eventStructType == NS_ACCESSIBLE_EVENT) { // Accessibility events come through OS requests and not from scripts, // so it is safe to handle here return HandleEventInternal(aEvent, aView, aEventStatus); } #endif nsIContent* capturingContent = NS_IS_MOUSE_EVENT(aEvent) ? GetCapturingContent() : nsnull; nsCOMPtr retargetEventDoc; if (!sDontRetargetEvents) { // key and IME related events should not cross top level window boundary. // Basically, such input events should be fired only on focused widget. // However, some IMEs might need to clean up composition after focused // window is deactivated. And also some tests on MozMill want to test key // handling on deactivated window because MozMill window can be activated // during tests. So, there is no merit the events should be redirected to // active window. So, the events should be handled on the last focused // content in the last focused DOM window in same top level window. // Note, if no DOM window has been focused yet, we can discard the events. if (NS_IsEventTargetedAtFocusedWindow(aEvent)) { nsCOMPtr window = GetFocusedDOMWindowInOurWindow(); // No DOM window in same top level window has not been focused yet, // discard the events. if (!window) { return NS_OK; } retargetEventDoc = do_QueryInterface(window->GetExtantDocument()); if (!retargetEventDoc) return NS_OK; } else if (capturingContent) { // if the mouse is being captured then retarget the mouse event at the // document that is being captured. retargetEventDoc = capturingContent->GetCurrentDoc(); } if (retargetEventDoc) { nsIPresShell* presShell = retargetEventDoc->GetShell(); if (!presShell) return NS_OK; if (presShell != this) { nsCOMPtr viewObserver = do_QueryInterface(presShell); if (!viewObserver) return NS_ERROR_FAILURE; nsIView *view; presShell->GetViewManager()->GetRootView(view); sDontRetargetEvents = PR_TRUE; nsresult rv = viewObserver->HandleEvent(view, aEvent, aEventStatus); sDontRetargetEvents = PR_FALSE; return rv; } } } // Check for a theme change up front, since the frame type is irrelevant if (aEvent->message == NS_THEMECHANGED && mPresContext) { mPresContext->ThemeChanged(); return NS_OK; } if (aEvent->message == NS_UISTATECHANGED && mDocument) { nsPIDOMWindow* win = mDocument->GetWindow(); if (win) { nsUIStateChangeEvent* event = (nsUIStateChangeEvent*)aEvent; win->SetKeyboardIndicators(event->showAccelerators, event->showFocusRings); } return NS_OK; } // Check for a system color change up front, since the frame type is // irrelevant if ((aEvent->message == NS_SYSCOLORCHANGED) && mPresContext) { nsIViewManager* vm = GetViewManager(); if (vm) { // Only dispatch system color change when the message originates from // from the root views widget. This is necessary to prevent us from // dispatching the SysColorChanged notification for each child window // which may be redundant. nsIView *view; vm->GetRootView(view); if (view == aView) { *aEventStatus = nsEventStatus_eConsumeDoDefault; mPresContext->SysColorChanged(); return NS_OK; } } return NS_OK; } if (aEvent->eventStructType == NS_KEY_EVENT && mDocument && mDocument->EventHandlingSuppressed()) { if (aEvent->message == NS_KEY_DOWN) { mNoDelayedKeyEvents = PR_TRUE; } else if (!mNoDelayedKeyEvents) { nsDelayedEvent* event = new nsDelayedKeyEvent(static_cast(aEvent)); if (event && !mDelayedEvents.AppendElement(event)) { delete event; } } return NS_OK; } nsIFrame* frame = static_cast(aView->GetClientData()); PRBool dispatchUsingCoordinates = NS_IsEventUsingCoordinates(aEvent); // if this event has no frame, we need to retarget it at a parent // view that has a frame. if (!frame && (dispatchUsingCoordinates || NS_IS_KEY_EVENT(aEvent) || NS_IS_IME_RELATED_EVENT(aEvent) || NS_IS_NON_RETARGETED_PLUGIN_EVENT(aEvent) || aEvent->message == NS_PLUGIN_ACTIVATE)) { nsIView* targetView = aView; while (targetView && !targetView->GetClientData()) { targetView = targetView->GetParent(); } if (targetView) { aView = targetView; frame = static_cast(aView->GetClientData()); } } if (dispatchUsingCoordinates) { NS_WARN_IF_FALSE(frame, "Nothing to handle this event!"); if (!frame) return NS_OK; nsPresContext* framePresContext = frame->PresContext(); nsPresContext* rootPresContext = framePresContext->GetRootPresContext(); NS_ASSERTION(rootPresContext == mPresContext->GetRootPresContext(), "How did we end up outside the connected prescontext/viewmanager hierarchy?"); // If we aren't starting our event dispatch from the root frame of the root prescontext, // then someone must be capturing the mouse. In that case we don't want to search the popup // list. if (framePresContext == rootPresContext && frame == FrameManager()->GetRootFrame()) { nsIFrame* popupFrame = nsLayoutUtils::GetPopupFrameForEventCoordinates(aEvent); // If the popupFrame is an ancestor of the 'frame', the frame should // handle the event, otherwise, the popup should handle it. if (popupFrame && !nsContentUtils::ContentIsCrossDocDescendantOf( framePresContext->GetPresShell()->GetDocument(), popupFrame->GetContent())) { frame = popupFrame; } } PRBool captureRetarget = PR_FALSE; if (capturingContent) { // If a capture is active, determine if the docshell is visible. If not, // clear the capture and target the mouse event normally instead. This // would occur if the mouse button is held down while a tab change occurs. // If the docshell is visible, look for a scrolling container. PRBool vis; nsCOMPtr supports = mPresContext->GetContainer(); nsCOMPtr baseWin(do_QueryInterface(supports)); if (baseWin && NS_SUCCEEDED(baseWin->GetVisibility(&vis)) && vis) { captureRetarget = gCaptureInfo.mRetargetToElement; if (!captureRetarget) { // A check was already done above to ensure that capturingContent is // in this presshell. NS_ASSERTION(capturingContent->GetCurrentDoc() == GetDocument(), "Unexpected document"); nsIFrame* captureFrame = capturingContent->GetPrimaryFrame(); if (captureFrame) { if (capturingContent->Tag() == nsGkAtoms::select && capturingContent->IsHTML()) { // a dropdown