/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* ***** 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 * Vladimir Vukicevic * Portions created by the Initial Developer are Copyright (C) 2004 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Vladimir Vukicevic (original author) * * 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 ***** */ #include "nsHTMLParts.h" #include "nsCOMPtr.h" #include "nsIServiceManager.h" #include "nsLayoutAtoms.h" #include "nsHTMLCanvasFrame.h" #include "nsICanvasElement.h" nsresult NS_NewHTMLCanvasFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame) { nsHTMLCanvasFrame* it = new (aPresShell) nsHTMLCanvasFrame; if (nsnull == it) { return NS_ERROR_OUT_OF_MEMORY; } *aNewFrame = it; return NS_OK; } nsHTMLCanvasFrame::nsHTMLCanvasFrame() { } nsHTMLCanvasFrame::~nsHTMLCanvasFrame() { } NS_IMETHODIMP nsHTMLCanvasFrame::Init(nsPresContext* aPresContext, nsIContent* aContent, nsIFrame* aParent, nsStyleContext* aContext, nsIFrame* aPrevInFlow) { nsCOMPtr canvas (do_QueryInterface(aContent)); NS_ENSURE_TRUE(canvas, NS_ERROR_FAILURE); nsresult rv = canvas->GetCanvasImageContainer(getter_AddRefs(mImageContainer)); NS_ENSURE_SUCCESS(rv, rv); if (mImageContainer) { PRInt32 w, h; mImageContainer->GetWidth(&w); mImageContainer->GetHeight(&h); mCanvasSize.SizeTo(w, h); } rv = nsSplittableFrame::Init(aPresContext, aContent, aParent, aContext, aPrevInFlow); return rv; } NS_IMETHODIMP nsHTMLCanvasFrame::Reflow(nsPresContext* aPresContext, nsHTMLReflowMetrics& aMetrics, const nsHTMLReflowState& aReflowState, nsReflowStatus& aStatus) { DO_GLOBAL_REFLOW_COUNT("nsHTMLCanvasFrame", aReflowState.reason); DISPLAY_REFLOW(aPresContext, this, aReflowState, aMetrics, aStatus); NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("enter nsHTMLCanvasFrame::Reflow: availSize=%d,%d", aReflowState.availableWidth, aReflowState.availableHeight)); NS_PRECONDITION(mState & NS_FRAME_IN_REFLOW, "frame is not in reflow"); aStatus = NS_FRAME_COMPLETE; float p2t = GetPresContext()->PixelsToTwips(); aMetrics.width = NSIntPixelsToTwips(mCanvasSize.width, p2t); aMetrics.height = NSIntPixelsToTwips(mCanvasSize.height, p2t); aMetrics.width += aReflowState.mComputedBorderPadding.left + aReflowState.mComputedBorderPadding.right; aMetrics.height += aReflowState.mComputedBorderPadding.top + aReflowState.mComputedBorderPadding.bottom; if (mPrevInFlow) { nscoord y = GetContinuationOffset(&aMetrics.width); aMetrics.height -= y + mBorderPadding.top; aMetrics.height = PR_MAX(0, aMetrics.height); } aMetrics.ascent = aMetrics.height; aMetrics.descent = 0; if (aMetrics.mComputeMEW) { aMetrics.SetMEWToActualWidth(aReflowState.mStylePosition->mWidth.GetUnit()); } if (aMetrics.mFlags & NS_REFLOW_CALC_MAX_WIDTH) { aMetrics.mMaximumWidth = aMetrics.width; } aMetrics.mOverflowArea.SetRect(0, 0, aMetrics.width, aMetrics.height); FinishAndStoreOverflow(&aMetrics); if (mRect.width != aMetrics.width || mRect.height != aMetrics.height) { Invalidate(nsRect(0, 0, mRect.width, mRect.height), PR_FALSE); } NS_FRAME_TRACE(NS_FRAME_TRACE_CALLS, ("exit nsHTMLCanvasFrame::Reflow: size=%d,%d", aMetrics.width, aMetrics.height)); NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aMetrics); return NS_OK; } NS_IMETHODIMP nsHTMLCanvasFrame::Paint(nsPresContext* aPresContext, nsIRenderingContext& aRenderingContext, const nsRect& aDirtyRect, nsFramePaintLayer aWhichLayer, PRUint32 aFlags) { PRBool isVisible; if (NS_SUCCEEDED(IsVisibleForPainting(aPresContext, aRenderingContext, PR_TRUE, &isVisible)) && isVisible && mRect.width && mRect.height) { // If painting is suppressed, we need to stop image painting. PRBool paintingSuppressed = PR_FALSE; aPresContext->PresShell()->IsPaintingSuppressed(&paintingSuppressed); if (paintingSuppressed) { return NS_OK; } if (aWhichLayer == NS_FRAME_PAINT_LAYER_FOREGROUND) { nsPoint dstpt = GetPosition(); float p2t = aPresContext->PixelsToTwips(); nsRect src(0, 0, NSIntPixelsToTwips(mCanvasSize.width, p2t), NSIntPixelsToTwips(mCanvasSize.height, p2t)); nsRect dst(0, 0, src.width, src.height); if (mImageContainer) { aRenderingContext.DrawImage(mImageContainer, src, dst); } else { aRenderingContext.FillRect(dst); } } } return nsFrame::Paint(aPresContext, aRenderingContext, aDirtyRect, aWhichLayer, nsISelectionDisplay::DISPLAY_IMAGES); } NS_IMETHODIMP nsHTMLCanvasFrame::CanContinueTextRun(PRBool& aContinueTextRun) const { // stolen from nsImageFrame.cpp // images really CAN continue text runs, but the textFrame needs to be // educated before we can indicate that it can. For now, we handle the fixing up // of max element widths in nsLineLayout::VerticalAlignFrames, but hopefully // this can be eliminated and the textFrame can be convinced to handle inlines // that take up space in text runs. aContinueTextRun = PR_FALSE; return NS_OK; } NS_IMETHODIMP nsHTMLCanvasFrame::GetContentForEvent(nsPresContext* aPresContext, nsEvent* aEvent, nsIContent** aContent) { NS_ENSURE_ARG_POINTER(aContent); *aContent = GetContent(); NS_IF_ADDREF(*aContent); return NS_OK; } nsIAtom* nsHTMLCanvasFrame::GetType() const { return nsLayoutAtoms::canvasFrame; } // get the offset into the content area of the image where aImg starts if it is a continuation. // from nsImageFrame nscoord nsHTMLCanvasFrame::GetContinuationOffset(nscoord* aWidth) const { nscoord offset = 0; if (aWidth) { *aWidth = 0; } if (mPrevInFlow) { for (nsIFrame* prevInFlow = mPrevInFlow ; prevInFlow; prevInFlow = prevInFlow->GetPrevInFlow()) { nsRect rect = prevInFlow->GetRect(); if (aWidth) { *aWidth = rect.width; } offset += rect.height; } offset -= mBorderPadding.top; offset = PR_MAX(0, offset); } return offset; } #ifdef ACCESSIBILITY NS_IMETHODIMP nsHTMLCanvasFrame::GetAccessible(nsIAccessible** aAccessible) { return NS_ERROR_FAILURE; } #endif #ifdef DEBUG NS_IMETHODIMP nsHTMLCanvasFrame::List(nsPresContext* aPresContext, FILE* out, PRInt32 aIndent) const { IndentBy(out, aIndent); ListTag(out); fputs("\n", out); return NS_OK; } #endif