2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsSimplePageSequence.h"
|
|
|
|
#include "nsPresContext.h"
|
2008-04-07 17:32:48 -07:00
|
|
|
#include "gfxContext.h"
|
2011-04-07 18:04:40 -07:00
|
|
|
#include "nsRenderingContext.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIPrintSettings.h"
|
|
|
|
#include "nsPageFrame.h"
|
|
|
|
#include "nsStyleConsts.h"
|
|
|
|
#include "nsRegion.h"
|
|
|
|
#include "nsCSSFrameConstructor.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDisplayList.h"
|
2011-05-24 23:32:00 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// DateTime Includes
|
|
|
|
#include "nsDateTimeFormatCID.h"
|
|
|
|
|
|
|
|
#define OFFSET_NOT_SET -1
|
|
|
|
|
|
|
|
// Print Options
|
|
|
|
#include "nsIPrintOptions.h"
|
|
|
|
#include "nsGfxCIID.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
2011-05-24 23:32:00 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
static const char sPrintOptionsContractID[] = "@mozilla.org/gfx/printsettings-service;1";
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "prlog.h"
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo * kLayoutPrintingLogMod = PR_NewLogModule("printing-layout");
|
|
|
|
#define PR_PL(_p1) PR_LOG(kLayoutPrintingLogMod, PR_LOG_DEBUG, _p1)
|
|
|
|
#else
|
|
|
|
#define PR_PL(_p1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// This object a shared by all the nsPageFrames
|
|
|
|
// parented to a SimplePageSequenceFrame
|
|
|
|
nsSharedPageData::nsSharedPageData() :
|
|
|
|
mDateTimeStr(nsnull),
|
|
|
|
mHeadFootFont(nsnull),
|
|
|
|
mPageNumFormat(nsnull),
|
|
|
|
mPageNumAndTotalsFormat(nsnull),
|
|
|
|
mDocTitle(nsnull),
|
|
|
|
mDocURL(nsnull),
|
|
|
|
mReflowSize(0,0),
|
|
|
|
mReflowMargin(0,0,0,0),
|
|
|
|
mExtraMargin(0,0,0,0),
|
|
|
|
mEdgePaperMargin(0,0,0,0),
|
|
|
|
mPageContentXMost(0),
|
|
|
|
mPageContentSize(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSharedPageData::~nsSharedPageData()
|
|
|
|
{
|
|
|
|
nsMemory::Free(mDateTimeStr);
|
2011-05-17 07:01:36 -07:00
|
|
|
delete mHeadFootFont;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsMemory::Free(mPageNumFormat);
|
|
|
|
nsMemory::Free(mPageNumAndTotalsFormat);
|
|
|
|
if (mDocTitle) nsMemory::Free(mDocTitle);
|
|
|
|
if (mDocURL) nsMemory::Free(mDocURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame*
|
|
|
|
NS_NewSimplePageSequenceFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
|
|
|
|
{
|
|
|
|
return new (aPresShell) nsSimplePageSequenceFrame(aContext);
|
|
|
|
}
|
|
|
|
|
2009-09-12 09:49:24 -07:00
|
|
|
NS_IMPL_FRAMEARENA_HELPERS(nsSimplePageSequenceFrame)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSimplePageSequenceFrame::nsSimplePageSequenceFrame(nsStyleContext* aContext) :
|
|
|
|
nsContainerFrame(aContext),
|
|
|
|
mTotalPages(-1),
|
|
|
|
mSelectionHeight(-1),
|
|
|
|
mYSelOffset(0)
|
|
|
|
{
|
2010-08-13 02:58:04 -07:00
|
|
|
nscoord halfInch = PresContext()->CSSTwipsToAppUnits(NS_INCHES_TO_TWIPS(0.5));
|
2007-03-22 10:30:00 -07:00
|
|
|
mMargin.SizeTo(halfInch, halfInch, halfInch, halfInch);
|
|
|
|
|
|
|
|
// XXX Unsafe to assume successful allocation
|
|
|
|
mPageData = new nsSharedPageData();
|
2012-01-25 15:52:26 -08:00
|
|
|
mPageData->mHeadFootFont =
|
|
|
|
new nsFont(*PresContext()->GetDefaultFont(kGenericFont_serif,
|
|
|
|
aContext->GetStyleFont()->mLanguage));
|
2010-08-13 02:58:04 -07:00
|
|
|
mPageData->mHeadFootFont->size = nsPresContext::CSSPointsToAppUnits(10);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
mPageData->mPrintOptions = do_GetService(sPrintOptionsContractID, &rv);
|
|
|
|
|
|
|
|
// Doing this here so we only have to go get these formats once
|
2011-10-17 07:59:28 -07:00
|
|
|
SetPageNumberFormat("pagenumber", "%1$d", true);
|
|
|
|
SetPageNumberFormat("pageofpages", "%1$d of %2$d", false);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsSimplePageSequenceFrame::~nsSimplePageSequenceFrame()
|
|
|
|
{
|
2011-05-17 07:01:36 -07:00
|
|
|
delete mPageData;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-01-12 11:20:59 -08:00
|
|
|
NS_QUERYFRAME_HEAD(nsSimplePageSequenceFrame)
|
|
|
|
NS_QUERYFRAME_ENTRY(nsIPageSequenceFrame)
|
|
|
|
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
void
|
|
|
|
nsSimplePageSequenceFrame::SetDesiredSize(nsHTMLReflowMetrics& aDesiredSize,
|
|
|
|
const nsHTMLReflowState& aReflowState,
|
|
|
|
nscoord aWidth,
|
|
|
|
nscoord aHeight)
|
|
|
|
{
|
|
|
|
// Aim to fill the whole size of the document, not only so we
|
|
|
|
// can act as a background in print preview but also handle overflow
|
|
|
|
// in child page frames correctly.
|
|
|
|
// Use availableWidth so we don't cause a needless horizontal scrollbar.
|
|
|
|
aDesiredSize.width = NS_MAX(aReflowState.availableWidth,
|
|
|
|
nscoord(aWidth * PresContext()->GetPrintPreviewScale()));
|
|
|
|
aDesiredSize.height = NS_MAX(aReflowState.ComputedHeight(),
|
|
|
|
nscoord(aHeight * PresContext()->GetPrintPreviewScale()));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::Reflow(nsPresContext* aPresContext,
|
|
|
|
nsHTMLReflowMetrics& aDesiredSize,
|
|
|
|
const nsHTMLReflowState& aReflowState,
|
|
|
|
nsReflowStatus& aStatus)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aPresContext->IsRootPaginatedDocument(),
|
|
|
|
"A Page Sequence is only for real pages");
|
|
|
|
DO_GLOBAL_REFLOW_COUNT("nsSimplePageSequenceFrame");
|
|
|
|
DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
|
|
|
|
NS_FRAME_TRACE_REFLOW_IN("nsSimplePageSequenceFrame::Reflow");
|
|
|
|
|
|
|
|
aStatus = NS_FRAME_COMPLETE; // we're always complete
|
|
|
|
|
2007-04-25 22:33:34 -07:00
|
|
|
// Don't do incremental reflow until we've taught tables how to do
|
|
|
|
// it right in paginated mode.
|
|
|
|
if (!(GetStateBits() & NS_FRAME_FIRST_REFLOW)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Return our desired size
|
2011-06-10 03:07:26 -07:00
|
|
|
SetDesiredSize(aDesiredSize, aReflowState, mSize.width, mSize.height);
|
2010-10-06 21:25:46 -07:00
|
|
|
aDesiredSize.SetOverflowAreasToDesiredBounds();
|
2007-03-22 10:30:00 -07:00
|
|
|
FinishAndStoreOverflow(&aDesiredSize);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if we can get a Print Settings from the Context
|
|
|
|
if (!mPageData->mPrintSettings &&
|
|
|
|
aPresContext->Medium() == nsGkAtoms::print) {
|
|
|
|
mPageData->mPrintSettings = aPresContext->GetPrintSettings();
|
|
|
|
}
|
|
|
|
|
2007-11-20 21:09:58 -08:00
|
|
|
// now get out margins & edges
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mPageData->mPrintSettings) {
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntMargin unwriteableTwips;
|
2008-03-19 13:51:42 -07:00
|
|
|
mPageData->mPrintSettings->GetUnwriteableMarginInTwips(unwriteableTwips);
|
|
|
|
NS_ASSERTION(unwriteableTwips.left >= 0 && unwriteableTwips.top >= 0 &&
|
|
|
|
unwriteableTwips.right >= 0 && unwriteableTwips.bottom >= 0,
|
|
|
|
"Unwriteable twips should be non-negative");
|
|
|
|
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntMargin marginTwips;
|
2007-03-22 10:30:00 -07:00
|
|
|
mPageData->mPrintSettings->GetMarginInTwips(marginTwips);
|
2010-08-13 02:58:04 -07:00
|
|
|
mMargin = aPresContext->CSSTwipsToAppUnits(marginTwips + unwriteableTwips);
|
2008-03-19 13:51:42 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt16 printType;
|
|
|
|
mPageData->mPrintSettings->GetPrintRange(&printType);
|
|
|
|
mPrintRangeType = printType;
|
2007-11-20 21:09:58 -08:00
|
|
|
|
2009-01-14 19:27:09 -08:00
|
|
|
nsIntMargin edgeTwips;
|
2007-11-20 21:09:58 -08:00
|
|
|
mPageData->mPrintSettings->GetEdgeInTwips(edgeTwips);
|
|
|
|
|
2008-01-28 23:38:20 -08:00
|
|
|
// sanity check the values. three inches are sometimes needed
|
2010-08-13 02:59:16 -07:00
|
|
|
PRInt32 inchInTwips = NS_INCHES_TO_INT_TWIPS(3.0);
|
2011-10-28 11:33:28 -07:00
|
|
|
edgeTwips.top = clamped(edgeTwips.top, 0, inchInTwips);
|
|
|
|
edgeTwips.bottom = clamped(edgeTwips.bottom, 0, inchInTwips);
|
|
|
|
edgeTwips.left = clamped(edgeTwips.left, 0, inchInTwips);
|
|
|
|
edgeTwips.right = clamped(edgeTwips.right, 0, inchInTwips);
|
2007-11-20 21:09:58 -08:00
|
|
|
|
|
|
|
mPageData->mEdgePaperMargin =
|
2010-08-13 02:58:04 -07:00
|
|
|
aPresContext->CSSTwipsToAppUnits(edgeTwips + unwriteableTwips);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// *** Special Override ***
|
|
|
|
// If this is a sub-sdoc (meaning it doesn't take the whole page)
|
|
|
|
// and if this Document is in the upper left hand corner
|
|
|
|
// we need to suppress the top margin or it will reflow too small
|
|
|
|
|
|
|
|
nsSize pageSize = aPresContext->GetPageSize();
|
|
|
|
|
|
|
|
mPageData->mReflowSize = pageSize;
|
2008-04-21 18:18:26 -07:00
|
|
|
// If we're printing a selection, we need to reflow with
|
|
|
|
// unconstrained height, to make sure we'll get to the selection
|
|
|
|
// even if it's beyond the first page of content.
|
|
|
|
if (nsIPrintSettings::kRangeSelection == mPrintRangeType) {
|
|
|
|
mPageData->mReflowSize.height = NS_UNCONSTRAINEDSIZE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
mPageData->mReflowMargin = mMargin;
|
|
|
|
|
|
|
|
// Compute the size of each page and the x coordinate that each page will
|
|
|
|
// be placed at
|
2009-09-16 08:01:36 -07:00
|
|
|
nscoord extraThreshold = NS_MAX(pageSize.width, pageSize.height)/10;
|
2011-05-24 23:32:00 -07:00
|
|
|
PRInt32 gapInTwips = Preferences::GetInt("print.print_extra_margin");
|
2009-09-16 08:01:36 -07:00
|
|
|
gapInTwips = NS_MAX(0, gapInTwips);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-13 02:58:04 -07:00
|
|
|
nscoord extraGap = aPresContext->CSSTwipsToAppUnits(gapInTwips);
|
2009-09-16 08:01:36 -07:00
|
|
|
extraGap = NS_MIN(extraGap, extraThreshold); // clamp to 1/10 of the largest dim of the page
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsMargin extraMargin(0,0,0,0);
|
|
|
|
if (aPresContext->IsScreen()) {
|
|
|
|
extraMargin.SizeTo(extraGap, extraGap, extraGap, extraGap);
|
|
|
|
}
|
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
mPageData->mExtraMargin = extraMargin;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
// We use the CSS "margin" property on the -moz-page pseudoelement
|
|
|
|
// to determine the space between each page in print preview.
|
|
|
|
// Keep a running y-offset for each page.
|
|
|
|
nscoord y = 0;
|
|
|
|
nscoord maxXMost = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
nsSize availSize(pageSize.width + extraMargin.LeftRight(),
|
|
|
|
pageSize.height + extraMargin.TopBottom());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Tile the pages vertically
|
|
|
|
nsHTMLReflowMetrics kidSize;
|
|
|
|
for (nsIFrame* kidFrame = mFrames.FirstChild(); nsnull != kidFrame; ) {
|
2008-02-08 18:54:55 -08:00
|
|
|
// Set the shared data into the page frame before reflow
|
|
|
|
nsPageFrame * pf = static_cast<nsPageFrame*>(kidFrame);
|
|
|
|
pf->SetSharedPageData(mPageData);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Reflow the page
|
|
|
|
nsHTMLReflowState kidReflowState(aPresContext, aReflowState, kidFrame,
|
|
|
|
availSize);
|
|
|
|
nsReflowStatus status;
|
|
|
|
|
|
|
|
kidReflowState.SetComputedWidth(kidReflowState.availableWidth);
|
2007-08-02 11:08:05 -07:00
|
|
|
//kidReflowState.SetComputedHeight(kidReflowState.availableHeight);
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_PL(("AV W: %d H: %d\n", kidReflowState.availableWidth, kidReflowState.availableHeight));
|
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
nsMargin pageCSSMargin = kidReflowState.mComputedMargin;
|
|
|
|
y += pageCSSMargin.top;
|
|
|
|
const nscoord x = pageCSSMargin.left;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Place and size the page. If the page is narrower than our
|
|
|
|
// max width then center it horizontally
|
|
|
|
ReflowChild(kidFrame, aPresContext, kidSize, kidReflowState, x, y, 0, status);
|
|
|
|
|
|
|
|
FinishReflowChild(kidFrame, aPresContext, nsnull, kidSize, x, y, 0);
|
|
|
|
y += kidSize.height;
|
2011-06-10 03:07:26 -07:00
|
|
|
y += pageCSSMargin.bottom;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-10 03:07:26 -07:00
|
|
|
maxXMost = NS_MAX(maxXMost, x + kidSize.width + pageCSSMargin.right);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Is the page complete?
|
|
|
|
nsIFrame* kidNextInFlow = kidFrame->GetNextInFlow();
|
|
|
|
|
2007-10-01 22:57:45 -07:00
|
|
|
if (NS_FRAME_IS_FULLY_COMPLETE(status)) {
|
2009-09-18 04:09:35 -07:00
|
|
|
NS_ASSERTION(!kidNextInFlow, "bad child flow list");
|
|
|
|
} else if (!kidNextInFlow) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// The page isn't complete and it doesn't have a next-in-flow, so
|
2009-09-18 04:09:35 -07:00
|
|
|
// create a continuing page.
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIFrame* continuingPage;
|
2009-09-18 04:09:35 -07:00
|
|
|
nsresult rv = aPresContext->PresShell()->FrameConstructor()->
|
|
|
|
CreateContinuingFrame(aPresContext, kidFrame, this, &continuingPage);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add it to our child list
|
2009-09-18 04:09:35 -07:00
|
|
|
mFrames.InsertFrame(nsnull, kidFrame, continuingPage);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next page
|
|
|
|
kidFrame = kidFrame->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Total Page Count
|
|
|
|
nsIFrame* page;
|
|
|
|
PRInt32 pageTot = 0;
|
|
|
|
for (page = mFrames.FirstChild(); page; page = page->GetNextSibling()) {
|
|
|
|
pageTot++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Page Number Info
|
|
|
|
PRInt32 pageNum = 1;
|
|
|
|
for (page = mFrames.FirstChild(); page; page = page->GetNextSibling()) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsPageFrame * pf = static_cast<nsPageFrame*>(page);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (pf != nsnull) {
|
|
|
|
pf->SetPageNumInfo(pageNum, pageTot);
|
|
|
|
}
|
|
|
|
pageNum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create current Date/Time String
|
|
|
|
if (!mDateFormatter)
|
|
|
|
mDateFormatter = do_CreateInstance(NS_DATETIMEFORMAT_CONTRACTID);
|
2008-10-08 20:51:54 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(mDateFormatter, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsAutoString formattedDateString;
|
|
|
|
time_t ltime;
|
|
|
|
time( <ime );
|
|
|
|
if (NS_SUCCEEDED(mDateFormatter->FormatTime(nsnull /* nsILocale* locale */,
|
|
|
|
kDateFormatShort,
|
|
|
|
kTimeFormatNoSeconds,
|
|
|
|
ltime,
|
|
|
|
formattedDateString))) {
|
|
|
|
PRUnichar * uStr = ToNewUnicode(formattedDateString);
|
|
|
|
SetDateTimeStr(uStr); // memory will be freed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return our desired size
|
2011-06-10 03:07:26 -07:00
|
|
|
// Adjust the reflow size by PrintPreviewScale so the scrollbars end up the
|
2007-03-22 10:30:00 -07:00
|
|
|
// correct size
|
2011-06-10 03:07:26 -07:00
|
|
|
SetDesiredSize(aDesiredSize, aReflowState, maxXMost, y);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-10-06 21:25:46 -07:00
|
|
|
aDesiredSize.SetOverflowAreasToDesiredBounds();
|
2007-03-22 10:30:00 -07:00
|
|
|
FinishAndStoreOverflow(&aDesiredSize);
|
|
|
|
|
|
|
|
// cache the size so we can set the desired size
|
|
|
|
// for the other reflows that happen
|
2011-06-10 03:07:26 -07:00
|
|
|
mSize.width = maxXMost;
|
2009-03-08 12:01:02 -07:00
|
|
|
mSize.height = y;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_FRAME_TRACE_REFLOW_OUT("nsSimplePageSequeceFrame::Reflow", aStatus);
|
|
|
|
NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::GetFrameName(nsAString& aResult) const
|
|
|
|
{
|
|
|
|
return MakeFrameName(NS_LITERAL_STRING("SimplePageSequence"), aResult);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//====================================================================
|
|
|
|
//== Asynch Printing
|
|
|
|
//====================================================================
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::GetCurrentPageNum(PRInt32* aPageNum)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPageNum);
|
|
|
|
|
|
|
|
*aPageNum = mPageNum;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::GetNumPages(PRInt32* aNumPages)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aNumPages);
|
|
|
|
|
|
|
|
*aNumPages = mTotalPages;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsSimplePageSequenceFrame::IsDoingPrintRange(bool* aDoing)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aDoing);
|
|
|
|
|
|
|
|
*aDoing = mDoingPageRange;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::GetPrintRange(PRInt32* aFromPage, PRInt32* aToPage)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aFromPage);
|
|
|
|
NS_ENSURE_ARG_POINTER(aToPage);
|
|
|
|
|
|
|
|
*aFromPage = mFromPageNum;
|
|
|
|
*aToPage = mToPageNum;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper Function
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsSimplePageSequenceFrame::SetPageNumberFormat(const char* aPropName, const char* aDefPropVal, bool aPageNumOnly)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Doing this here so we only have to go get these formats once
|
|
|
|
nsXPIDLString pageNumberFormat;
|
|
|
|
// Now go get the Localized Page Formating String
|
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::GetLocalizedString(nsContentUtils::ePRINTING_PROPERTIES,
|
|
|
|
aPropName, pageNumberFormat);
|
|
|
|
if (NS_FAILED(rv)) { // back stop formatting
|
|
|
|
pageNumberFormat.AssignASCII(aDefPropVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the format into a static data member which will own the memory and free it
|
|
|
|
PRUnichar* uStr = ToNewUnicode(pageNumberFormat);
|
|
|
|
if (uStr != nsnull) {
|
|
|
|
SetPageNumberFormat(uStr, aPageNumOnly); // nsPageFrame will own the memory
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::StartPrint(nsPresContext* aPresContext,
|
|
|
|
nsIPrintSettings* aPrintSettings,
|
|
|
|
PRUnichar* aDocTitle,
|
|
|
|
PRUnichar* aDocURL)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aPresContext);
|
|
|
|
NS_ENSURE_ARG_POINTER(aPrintSettings);
|
|
|
|
|
|
|
|
if (!mPageData->mPrintSettings) {
|
|
|
|
mPageData->mPrintSettings = aPrintSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only set them if they are not null
|
|
|
|
if (aDocTitle) mPageData->mDocTitle = aDocTitle;
|
|
|
|
if (aDocURL) mPageData->mDocURL = aDocURL;
|
|
|
|
|
|
|
|
aPrintSettings->GetStartPageRange(&mFromPageNum);
|
|
|
|
aPrintSettings->GetEndPageRange(&mToPageNum);
|
2011-12-06 20:45:06 -08:00
|
|
|
aPrintSettings->GetPageRanges(mPageRanges);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mDoingPageRange = nsIPrintSettings::kRangeSpecifiedPageRange == mPrintRangeType ||
|
|
|
|
nsIPrintSettings::kRangeSelection == mPrintRangeType;
|
|
|
|
|
|
|
|
// If printing a range of pages make sure at least the starting page
|
|
|
|
// number is valid
|
|
|
|
PRInt32 totalPages = mFrames.GetLength();
|
|
|
|
|
|
|
|
if (mDoingPageRange) {
|
|
|
|
if (mFromPageNum > totalPages) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin printing of the document
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Determine if we are rendering only the selection
|
|
|
|
aPresContext->SetIsRenderingOnlySelection(nsIPrintSettings::kRangeSelection == mPrintRangeType);
|
|
|
|
|
|
|
|
|
|
|
|
if (mDoingPageRange) {
|
|
|
|
// XXX because of the hack for making the selection all print on one page
|
|
|
|
// we must make sure that the page is sized correctly before printing.
|
2007-11-20 21:09:58 -08:00
|
|
|
nscoord height = aPresContext->GetPageSize().height;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRInt32 pageNum = 1;
|
|
|
|
nscoord y = 0;//mMargin.top;
|
|
|
|
|
|
|
|
for (nsIFrame* page = mFrames.FirstChild(); page;
|
|
|
|
page = page->GetNextSibling()) {
|
|
|
|
if (pageNum >= mFromPageNum && pageNum <= mToPageNum) {
|
|
|
|
nsRect rect = page->GetRect();
|
|
|
|
rect.y = y;
|
|
|
|
rect.height = height;
|
|
|
|
page->SetRect(rect);
|
|
|
|
y += rect.height + mMargin.top + mMargin.bottom;
|
|
|
|
}
|
|
|
|
pageNum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust total number of pages
|
|
|
|
if (nsIPrintSettings::kRangeSelection != mPrintRangeType) {
|
|
|
|
totalPages = pageNum - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mPageNum = 1;
|
|
|
|
mCurrentPageFrame = mFrames.FirstChild();
|
|
|
|
|
|
|
|
if (mTotalPages == -1) {
|
|
|
|
mTotalPages = totalPages;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::PrintNextPage()
|
|
|
|
{
|
|
|
|
// Print each specified page
|
|
|
|
// pageNum keeps track of the current page and what pages are printing
|
|
|
|
//
|
|
|
|
// printedPageNum keeps track of the current page number to be printed
|
|
|
|
// Note: When print al the pages or a page range the printed page shows the
|
|
|
|
// actual page number, when printing selection it prints the page number starting
|
|
|
|
// with the first page of the selection. For example if the user has a
|
|
|
|
// selection that starts on page 2 and ends on page 3, the page numbers when
|
|
|
|
// print are 1 and then two (which is different than printing a page range, where
|
|
|
|
// the page numbers would have been 2 and then 3)
|
|
|
|
|
|
|
|
if (mCurrentPageFrame == nsnull) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool printEvenPages, printOddPages;
|
2007-03-22 10:30:00 -07:00
|
|
|
mPageData->mPrintSettings->GetPrintOptions(nsIPrintSettings::kPrintEvenPages, &printEvenPages);
|
|
|
|
mPageData->mPrintSettings->GetPrintOptions(nsIPrintSettings::kPrintOddPages, &printOddPages);
|
|
|
|
|
|
|
|
// Begin printing of the document
|
2011-04-16 18:22:44 -07:00
|
|
|
nsDeviceContext *dc = PresContext()->DeviceContext();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// See whether we should print this page
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrintThisPage = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If printing a range of pages check whether the page number is in the
|
|
|
|
// range of pages to print
|
|
|
|
if (mDoingPageRange) {
|
|
|
|
if (mPageNum < mFromPageNum) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrintThisPage = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else if (mPageNum > mToPageNum) {
|
|
|
|
mPageNum++;
|
|
|
|
mCurrentPageFrame = nsnull;
|
|
|
|
return NS_OK;
|
2011-12-06 20:45:06 -08:00
|
|
|
} else {
|
|
|
|
PRInt32 length = mPageRanges.Length();
|
|
|
|
|
|
|
|
// Page ranges are pairs (start, end)
|
|
|
|
if (length && (length % 2 == 0)) {
|
|
|
|
mPrintThisPage = false;
|
|
|
|
|
|
|
|
PRInt32 i;
|
|
|
|
for (i = 0; i < length; i += 2) {
|
|
|
|
if (mPageRanges[i] <= mPageNum && mPageNum <= mPageRanges[i+1]) {
|
|
|
|
mPrintThisPage = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for printing of odd and even pages
|
|
|
|
if (mPageNum & 0x1) {
|
|
|
|
if (!printOddPages) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrintThisPage = false; // don't print odd numbered page
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!printEvenPages) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrintThisPage = false; // don't print even numbered page
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsIPrintSettings::kRangeSelection == mPrintRangeType) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mPrintThisPage = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mPrintThisPage) {
|
|
|
|
// XXX This is temporary fix for printing more than one page of a selection
|
|
|
|
// This does a poor man's "dump" pagination (see Bug 89353)
|
|
|
|
// It has laid out as one long page and now we are just moving or view up/down
|
|
|
|
// one page at a time and printing the contents of what is exposed by the rect.
|
|
|
|
// currently this does not work for IFrames
|
|
|
|
// I will soon improve this to work with IFrames
|
2011-09-28 23:19:26 -07:00
|
|
|
bool continuePrinting = true;
|
2007-11-20 21:09:58 -08:00
|
|
|
nscoord width, height;
|
2007-03-30 14:11:41 -07:00
|
|
|
width = PresContext()->GetPageSize().width;
|
|
|
|
height = PresContext()->GetPageSize().height;
|
2007-03-22 10:30:00 -07:00
|
|
|
height -= mMargin.top + mMargin.bottom;
|
|
|
|
width -= mMargin.left + mMargin.right;
|
|
|
|
nscoord selectionY = height;
|
2011-08-24 13:54:30 -07:00
|
|
|
nsIFrame* conFrame = mCurrentPageFrame->GetFirstPrincipalChild();
|
2007-11-20 21:09:58 -08:00
|
|
|
if (mSelectionHeight >= 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
conFrame->SetPosition(conFrame->GetPosition() + nsPoint(0, -mYSelOffset));
|
2008-04-28 15:55:04 -07:00
|
|
|
nsContainerFrame::PositionChildViews(conFrame);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// cast the frame to be a page frame
|
2007-07-08 00:08:04 -07:00
|
|
|
nsPageFrame * pf = static_cast<nsPageFrame*>(mCurrentPageFrame);
|
2007-03-22 10:30:00 -07:00
|
|
|
pf->SetPageNumInfo(mPageNum, mTotalPages);
|
|
|
|
pf->SetSharedPageData(mPageData);
|
|
|
|
|
|
|
|
PRInt32 printedPageNum = 1;
|
|
|
|
while (continuePrinting) {
|
2007-03-30 14:11:41 -07:00
|
|
|
if (PresContext()->IsRootPaginatedDocument()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_PL(("\n"));
|
|
|
|
PR_PL(("***************** BeginPage *****************\n"));
|
|
|
|
rv = dc->BeginPage();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2009-09-06 17:35:14 -07:00
|
|
|
PR_PL(("SeqFr::PrintNextPage -> %p PageNo: %d", pf, mPageNum));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-04-07 18:04:40 -07:00
|
|
|
nsRefPtr<nsRenderingContext> renderingContext;
|
2010-08-20 12:29:01 -07:00
|
|
|
dc->CreateRenderingContext(*getter_AddRefs(renderingContext));
|
|
|
|
NS_ENSURE_TRUE(renderingContext, NS_ERROR_OUT_OF_MEMORY);
|
2008-04-07 17:32:48 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRect drawingRect(nsPoint(0, 0),
|
|
|
|
mCurrentPageFrame->GetSize());
|
|
|
|
nsRegion drawingRegion(drawingRect);
|
|
|
|
nsLayoutUtils::PaintFrame(renderingContext, mCurrentPageFrame,
|
2009-09-12 15:44:18 -07:00
|
|
|
drawingRegion, NS_RGBA(0,0,0,0),
|
|
|
|
nsLayoutUtils::PAINT_SYNC_DECODE_IMAGES);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-20 21:09:58 -08:00
|
|
|
if (mSelectionHeight >= 0 && selectionY < mSelectionHeight) {
|
2007-03-22 10:30:00 -07:00
|
|
|
selectionY += height;
|
|
|
|
printedPageNum++;
|
|
|
|
pf->SetPageNumInfo(printedPageNum, mTotalPages);
|
|
|
|
conFrame->SetPosition(conFrame->GetPosition() + nsPoint(0, -height));
|
2008-06-24 09:30:55 -07:00
|
|
|
nsContainerFrame::PositionChildViews(conFrame);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PR_PL(("***************** End Page (PrintNextPage) *****************\n"));
|
|
|
|
rv = dc->EndPage();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
2011-10-17 07:59:28 -07:00
|
|
|
continuePrinting = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::DoPageEnd()
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2007-03-30 14:11:41 -07:00
|
|
|
if (PresContext()->IsRootPaginatedDocument() && mPrintThisPage) {
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_PL(("***************** End Page (DoPageEnd) *****************\n"));
|
2007-03-30 14:11:41 -07:00
|
|
|
rv = PresContext()->DeviceContext()->EndPage();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
mPageNum++;
|
|
|
|
|
|
|
|
if (mCurrentPageFrame) {
|
|
|
|
mCurrentPageFrame = mCurrentPageFrame->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-04-07 18:04:40 -07:00
|
|
|
static void PaintPageSequence(nsIFrame* aFrame, nsRenderingContext* aCtx,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsRect& aDirtyRect, nsPoint aPt)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsSimplePageSequenceFrame*>(aFrame)->PaintPageSequence(*aCtx, aDirtyRect, aPt);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void
|
2011-04-07 18:04:40 -07:00
|
|
|
nsSimplePageSequenceFrame::PaintPageSequence(nsRenderingContext& aRenderingContext,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
nsPoint aPt) {
|
|
|
|
nsRect rect = aDirtyRect;
|
2007-03-30 14:11:41 -07:00
|
|
|
float scale = PresContext()->GetPrintPreviewScale();
|
2007-03-22 10:30:00 -07:00
|
|
|
aRenderingContext.PushState();
|
|
|
|
nsPoint framePos = aPt;
|
2011-04-07 18:04:39 -07:00
|
|
|
aRenderingContext.Translate(framePos);
|
2007-03-22 10:30:00 -07:00
|
|
|
rect -= framePos;
|
|
|
|
aRenderingContext.Scale(scale, scale);
|
|
|
|
rect.ScaleRoundOut(1.0f / scale);
|
|
|
|
|
|
|
|
// Now the rect and the rendering coordinates are are relative to this frame.
|
|
|
|
// Loop over the pages and paint them.
|
2011-08-24 13:54:30 -07:00
|
|
|
nsIFrame* child = GetFirstPrincipalChild();
|
2007-03-22 10:30:00 -07:00
|
|
|
while (child) {
|
|
|
|
nsPoint pt = child->GetPosition();
|
|
|
|
// The rendering context has to be translated before each call to PaintFrame
|
|
|
|
aRenderingContext.PushState();
|
2011-04-07 18:04:39 -07:00
|
|
|
aRenderingContext.Translate(pt);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsLayoutUtils::PaintFrame(&aRenderingContext, child,
|
2009-09-12 15:44:18 -07:00
|
|
|
nsRegion(rect - pt), NS_RGBA(0,0,0,0),
|
|
|
|
nsLayoutUtils::PAINT_SYNC_DECODE_IMAGES);
|
2007-03-22 10:30:00 -07:00
|
|
|
aRenderingContext.PopState();
|
|
|
|
child = child->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
aRenderingContext.PopState();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
|
|
|
|
const nsRect& aDirtyRect,
|
|
|
|
const nsDisplayListSet& aLists)
|
|
|
|
{
|
|
|
|
nsresult rv = DisplayBorderBackgroundOutline(aBuilder, aLists);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = aLists.Content()->AppendNewToTop(new (aBuilder)
|
2010-08-13 03:01:13 -07:00
|
|
|
nsDisplayGeneric(aBuilder, this, ::PaintPageSequence, "PageSequence",
|
2010-07-15 14:07:49 -07:00
|
|
|
nsDisplayItem::TYPE_PAGE_SEQUENCE));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
|
|
|
nsSimplePageSequenceFrame::GetType() const
|
|
|
|
{
|
|
|
|
return nsGkAtoms::sequenceFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void
|
2011-09-28 23:19:26 -07:00
|
|
|
nsSimplePageSequenceFrame::SetPageNumberFormat(PRUnichar * aFormatStr, bool aForPageNumOnly)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aFormatStr != nsnull, "Format string cannot be null!");
|
|
|
|
NS_ASSERTION(mPageData != nsnull, "mPageData string cannot be null!");
|
|
|
|
|
|
|
|
if (aForPageNumOnly) {
|
|
|
|
if (mPageData->mPageNumFormat != nsnull) {
|
|
|
|
nsMemory::Free(mPageData->mPageNumFormat);
|
|
|
|
}
|
|
|
|
mPageData->mPageNumFormat = aFormatStr;
|
|
|
|
} else {
|
|
|
|
if (mPageData->mPageNumAndTotalsFormat != nsnull) {
|
|
|
|
nsMemory::Free(mPageData->mPageNumAndTotalsFormat);
|
|
|
|
}
|
|
|
|
mPageData->mPageNumAndTotalsFormat = aFormatStr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
nsSimplePageSequenceFrame::SetDateTimeStr(PRUnichar * aDateTimeStr)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aDateTimeStr != nsnull, "DateTime string cannot be null!");
|
|
|
|
NS_ASSERTION(mPageData != nsnull, "mPageData string cannot be null!");
|
|
|
|
|
|
|
|
if (mPageData->mDateTimeStr != nsnull) {
|
|
|
|
nsMemory::Free(mPageData->mDateTimeStr);
|
|
|
|
}
|
|
|
|
mPageData->mDateTimeStr = aDateTimeStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// For Shrink To Fit
|
|
|
|
//
|
|
|
|
// Return the percentage that the page needs to shrink to
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSimplePageSequenceFrame::GetSTFPercent(float& aSTFPercent)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(mPageData, NS_ERROR_UNEXPECTED);
|
|
|
|
aSTFPercent = 1.0f;
|
|
|
|
if (mPageData && (mPageData->mPageContentXMost > mPageData->mPageContentSize)) {
|
|
|
|
aSTFPercent = float(mPageData->mPageContentSize) / float(mPageData->mPageContentXMost);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|