2005-04-18 10:33:23 +04:00
|
|
|
/* -*- 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 <vladimir@pobox.com>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2004
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com> (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 ***** */
|
|
|
|
|
2006-03-29 22:29:03 +04:00
|
|
|
/* rendering object for the HTML <canvas> element */
|
|
|
|
|
2005-04-18 10:33:23 +04:00
|
|
|
#include "nsHTMLParts.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2005-04-18 10:33:23 +04:00
|
|
|
|
|
|
|
#include "nsHTMLCanvasFrame.h"
|
|
|
|
#include "nsICanvasElement.h"
|
2006-01-26 05:29:17 +03:00
|
|
|
#include "nsDisplayList.h"
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2006-04-19 01:23:15 +04:00
|
|
|
#include "nsTransform2D.h"
|
|
|
|
|
2005-11-01 23:40:54 +03:00
|
|
|
nsIFrame*
|
2006-03-27 01:30:36 +04:00
|
|
|
NS_NewHTMLCanvasFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2006-03-27 01:30:36 +04:00
|
|
|
return new (aPresShell) nsHTMLCanvasFrame(aContext);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLCanvasFrame::~nsHTMLCanvasFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
nsSize
|
|
|
|
nsHTMLCanvasFrame::GetCanvasSize()
|
|
|
|
{
|
|
|
|
PRUint32 w, h;
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsICanvasElement> canvas(do_QueryInterface(GetContent()));
|
|
|
|
if (canvas) {
|
|
|
|
rv = canvas->GetSize(&w, &h);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_NOTREACHED("couldn't get canvas size");
|
|
|
|
h = w = 1;
|
|
|
|
}
|
|
|
|
|
2007-02-07 10:46:44 +03:00
|
|
|
return nsSize(w, h);
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ nscoord
|
|
|
|
nsHTMLCanvasFrame::GetMinWidth(nsIRenderingContext *aRenderingContext)
|
|
|
|
{
|
|
|
|
// XXX The caller doesn't account for constraints of the height,
|
|
|
|
// min-height, and max-height properties.
|
2007-02-07 10:46:44 +03:00
|
|
|
nscoord result = nsPresContext::CSSPixelsToAppUnits(GetCanvasSize().width);
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
DISPLAY_MIN_WIDTH(this, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ nscoord
|
|
|
|
nsHTMLCanvasFrame::GetPrefWidth(nsIRenderingContext *aRenderingContext)
|
|
|
|
{
|
|
|
|
// XXX The caller doesn't account for constraints of the height,
|
|
|
|
// min-height, and max-height properties.
|
2007-02-07 10:46:44 +03:00
|
|
|
nscoord result = nsPresContext::CSSPixelsToAppUnits(GetCanvasSize().width);
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
DISPLAY_PREF_WIDTH(this, result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* virtual */ nsSize
|
|
|
|
nsHTMLCanvasFrame::ComputeSize(nsIRenderingContext *aRenderingContext,
|
|
|
|
nsSize aCBSize, nscoord aAvailableWidth,
|
|
|
|
nsSize aMargin, nsSize aBorder, nsSize aPadding,
|
|
|
|
PRBool aShrinkWrap)
|
|
|
|
{
|
2007-02-07 10:46:44 +03:00
|
|
|
nsSize size = GetCanvasSize();
|
|
|
|
nsSize canvasSize(nsPresContext::CSSPixelsToAppUnits(size.width),
|
|
|
|
nsPresContext::CSSPixelsToAppUnits(size.height));
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
|
|
|
|
return nsLayoutUtils::ComputeSizeWithIntrinsicDimensions(
|
2007-01-17 05:41:47 +03:00
|
|
|
aRenderingContext, this, canvasSize,
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
aCBSize, aBorder, aPadding);
|
|
|
|
}
|
2005-04-18 10:33:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCanvasFrame::Reflow(nsPresContext* aPresContext,
|
|
|
|
nsHTMLReflowMetrics& aMetrics,
|
|
|
|
const nsHTMLReflowState& aReflowState,
|
|
|
|
nsReflowStatus& aStatus)
|
|
|
|
{
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
DO_GLOBAL_REFLOW_COUNT("nsHTMLCanvasFrame");
|
2005-04-18 10:33:23 +04:00
|
|
|
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;
|
|
|
|
|
2007-01-26 03:05:12 +03:00
|
|
|
aMetrics.width = aReflowState.ComputedWidth();
|
Bug 300030: Move intrinsic width computation out of nsIFrame::Reflow and into its own methods on nsIFrame. Replace reflow reasons, types, and commands with dirty bits/notifications. Thanks to bzbarsky for almost all of the HTML form controls (mozilla/layout/forms) changes, and many others for help testing and patching. For detailed commit logs, see REFLOW_YYYYMMDD_BRANCH, where YYYYMMDD is one of 20061031, 20060830, 20060603, 20060302, 20060119, 20051011, 20050804, 20050429, 20050315, 20050111, and 20041213.
2006-12-08 08:38:33 +03:00
|
|
|
aMetrics.height = aReflowState.mComputedHeight;
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
// stash this away so we can compute our inner area later
|
|
|
|
mBorderPadding = aReflowState.mComputedBorderPadding;
|
|
|
|
|
|
|
|
aMetrics.width += mBorderPadding.left + mBorderPadding.right;
|
|
|
|
aMetrics.height += mBorderPadding.top + mBorderPadding.bottom;
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2006-02-22 00:33:47 +03:00
|
|
|
if (GetPrevInFlow()) {
|
2005-04-18 10:33:23 +04:00
|
|
|
nscoord y = GetContinuationOffset(&aMetrics.width);
|
|
|
|
aMetrics.height -= y + mBorderPadding.top;
|
|
|
|
aMetrics.height = PR_MAX(0, aMetrics.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2005-05-07 07:45:06 +04:00
|
|
|
// FIXME taken from nsImageFrame, but then had splittable frame stuff
|
|
|
|
// removed. That needs to be fixed.
|
|
|
|
nsRect
|
|
|
|
nsHTMLCanvasFrame::GetInnerArea() const
|
|
|
|
{
|
|
|
|
nsRect r;
|
|
|
|
r.x = mBorderPadding.left;
|
|
|
|
r.y = mBorderPadding.top;
|
|
|
|
r.width = mRect.width - mBorderPadding.left - mBorderPadding.right;
|
|
|
|
r.height = mRect.height - mBorderPadding.top - mBorderPadding.bottom;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
void
|
|
|
|
nsHTMLCanvasFrame::PaintCanvas(nsIRenderingContext& aRenderingContext,
|
|
|
|
const nsRect& aDirtyRect, nsPoint aPt)
|
|
|
|
{
|
|
|
|
nsRect inner = GetInnerArea() + aPt;
|
|
|
|
|
2006-04-19 01:23:15 +04:00
|
|
|
nsCOMPtr<nsICanvasElement> canvas(do_QueryInterface(GetContent()));
|
|
|
|
if (!canvas)
|
|
|
|
return;
|
|
|
|
|
2007-01-17 05:41:47 +03:00
|
|
|
nsSize canvasSize = GetCanvasSize();
|
2007-02-07 10:46:44 +03:00
|
|
|
nsSize sizeAppUnits(GetPresContext()->DevPixelsToAppUnits(canvasSize.width),
|
|
|
|
GetPresContext()->DevPixelsToAppUnits(canvasSize.height));
|
2007-01-17 05:41:47 +03:00
|
|
|
|
2006-04-19 01:23:15 +04:00
|
|
|
// XXXvlad clip to aDirtyRect!
|
|
|
|
|
2007-02-07 10:46:44 +03:00
|
|
|
if (inner.Size() != sizeAppUnits)
|
2006-04-19 01:23:15 +04:00
|
|
|
{
|
2007-02-07 10:46:44 +03:00
|
|
|
float sx = inner.width / (float) sizeAppUnits.width;
|
|
|
|
float sy = inner.height / (float) sizeAppUnits.height;
|
2006-04-19 01:23:15 +04:00
|
|
|
|
|
|
|
aRenderingContext.PushState();
|
|
|
|
aRenderingContext.Translate(inner.x, inner.y);
|
|
|
|
aRenderingContext.Scale(sx, sy);
|
|
|
|
|
|
|
|
canvas->RenderContexts(&aRenderingContext);
|
|
|
|
|
|
|
|
aRenderingContext.PopState();
|
|
|
|
} else {
|
|
|
|
//nsIRenderingContext::AutoPushTranslation(&aRenderingContext, px, py);
|
|
|
|
|
|
|
|
aRenderingContext.PushState();
|
|
|
|
aRenderingContext.Translate(inner.x, inner.y);
|
|
|
|
|
|
|
|
canvas->RenderContexts(&aRenderingContext);
|
|
|
|
|
|
|
|
aRenderingContext.PopState();
|
|
|
|
}
|
2006-01-26 05:29:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PaintCanvas(nsIFrame* aFrame, nsIRenderingContext* aCtx,
|
|
|
|
const nsRect& aDirtyRect, nsPoint aPt)
|
|
|
|
{
|
|
|
|
NS_STATIC_CAST(nsHTMLCanvasFrame*, aFrame)->PaintCanvas(*aCtx, aDirtyRect, aPt);
|
|
|
|
}
|
|
|
|
|
2005-04-18 10:33:23 +04:00
|
|
|
NS_IMETHODIMP
|
2006-01-26 05:29:17 +03:00
|
|
|
nsHTMLCanvasFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
2006-01-26 05:29:17 +03:00
|
|
|
if (!IsVisibleForPainting(aBuilder))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = DisplayBorderBackgroundOutline(aBuilder, aLists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2006-04-19 01:23:15 +04:00
|
|
|
rv = aLists.Content()->AppendNewToTop(new (aBuilder)
|
|
|
|
nsDisplayGeneric(this, ::PaintCanvas, "Canvas"));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2005-04-18 10:33:23 +04:00
|
|
|
|
2006-01-26 05:29:17 +03:00
|
|
|
return DisplaySelectionOverlay(aBuilder, aLists,
|
|
|
|
nsISelectionDisplay::DISPLAY_IMAGES);
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2006-12-26 20:47:52 +03:00
|
|
|
return nsGkAtoms::HTMLCanvasFrame;
|
2005-04-18 10:33:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2006-02-22 00:33:47 +03:00
|
|
|
if (GetPrevInFlow()) {
|
|
|
|
for (nsIFrame* prevInFlow = GetPrevInFlow() ; prevInFlow; prevInFlow = prevInFlow->GetPrevInFlow()) {
|
2005-04-18 10:33:23 +04:00
|
|
|
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
|
2005-05-07 07:45:06 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLCanvasFrame::GetFrameName(nsAString& aResult) const
|
|
|
|
{
|
|
|
|
return MakeFrameName(NS_LITERAL_STRING("HTMLCanvas"), aResult);
|
|
|
|
}
|
|
|
|
|
2005-04-18 10:33:23 +04:00
|
|
|
NS_IMETHODIMP
|
2005-09-07 01:34:50 +04:00
|
|
|
nsHTMLCanvasFrame::List(FILE* out, PRInt32 aIndent) const
|
2005-04-18 10:33:23 +04:00
|
|
|
{
|
|
|
|
IndentBy(out, aIndent);
|
|
|
|
ListTag(out);
|
|
|
|
fputs("\n", out);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|