⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printer.cpp

📁 在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己的开发
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// PRINTER.CPP
//
// Copyright (c) 1997-2001 Symbian Ltd. All rights reserved.

#include "Printer.h"

//////////////////////////////////////////////////////////////////////////////
//																			//
//		The class CPRINTEROpx, which is in the format required by opx		//
//		will access COplPrintEngine which will do all the actual work		//
//																			//
//////////////////////////////////////////////////////////////////////////////
COplPrintEngine::~COplPrintEngine() 
	{
	delete iPrintSetup;
	delete iPrint;
	delete iParaFormatLayer;
	delete iCharFormatLayer;
	delete iMyRichText;
	delete iCharsPerPage;
	delete iParaFormat;
	}

COplPrintEngine* COplPrintEngine::NewL(OplAPI& aOplAPI)
	{
	COplPrintEngine* This=new(ELeave) COplPrintEngine();
	CleanupStack::PushL(This);
	This->ConstructL(aOplAPI);
	CleanupStack::Pop();
	return This;
	}

void COplPrintEngine::ConstructL(OplAPI& aOplAPI) 
	{
	iInitialised = EFalse;
	iDocumentChanged =ETrue;
	iPaginateDialogCalled = EFalse;
	iOplAPI = &aOplAPI;
	iFormatChanged = EFalse;
	_LIT(KArialName,"Arial"); // !! This probably shouldn't be hard-coded
	iFontName = KArialName;
	iFontHeight = 178;

	iParaFormat = CParaFormat::NewL();
	DefaultFormatting();

	iParaFormatLayer = CParaFormatLayer::NewL();
	iCharFormatLayer = CCharFormatLayer::NewL(iCharFormat,iCharFormatMask);
	iMyRichText=CRichText::NewL(iParaFormatLayer,iCharFormatLayer);
	// iRichText is the current doc, can change in SendRichTextToPrinter
	// Keep a reference to the original though, so we can delete it
	iRichText = iMyRichText;
	iPrintSetup = aOplAPI.EikonEnv().NewDefaultPrintSetupL();

	iPrintSetup->Header()->SetFileNameInfo(*this); 
	iPrintSetup->Header()->SetNumPagesInfo(*this); 
	iPrintSetup->Footer()->SetFileNameInfo(*this); 
	iPrintSetup->Footer()->SetNumPagesInfo(*this); 
	
	iPrint= CTextPageRegionPrinter::NewL(iRichText,iPrintSetup->PrinterDevice());
	iCharsPerPage=new(ELeave) CArrayFixFlat<TInt>(5);
	iPrint->SetPageList(iCharsPerPage);
	iPrintParameters.iFirstPage=0;
	iPrintParameters.iNumCopies=1;
	iPrint->SetFirstPageOfDoc(0);
	iPrint->SetPageMarginsInTwips(iPrintSetup->iPageMarginsInTwips.iMargins);
	iPrint->SetPageSpecInTwips(iPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
	iRichText->InsertL(0,KNullDesC);
	}

TInt COplPrintEngine::UpdateFieldFileName(TPtr& aValueText) const
// Set aValueText with the current document filename only. // (No path or extension).
	{
	TParse parser;

	User::LeaveIfError(parser.Set(iOplAPI->EikonEnv().Process()->MainDocFileName(),NULL,NULL));
	if (parser.Name().Length()>aValueText.MaxLength())
		return parser.Name().Length();
	aValueText=parser.Name();
	return 0;
	}

TInt COplPrintEngine::UpdateFieldNumPages(void)const
	{
	return (iCharsPerPage->Count());
	}

void COplPrintEngine::RunPageSetupDialogL() 
	{		
	TBool cancel=EFalse;
	if (iDocumentChanged)
		cancel=RunPaginationDialogL();
	if (cancel)
		return;
#if !defined(__UIQ__)
	if (CEikPageSetupDialog::RunDlgLD(iPrintSetup))	
		{
		iPrint->SetPageMarginsInTwips(iPrintSetup->iPageMarginsInTwips.iMargins);
		iPrint->SetPageSpecInTwips(iPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
		iDocumentChanged=ETrue;
		}
#endif
	}

void COplPrintEngine::RunPrintPreviewDialogL() 
	{
	TBool cancel=EFalse;
	if (iDocumentChanged)
		cancel=RunPaginationDialogL();
	if (cancel)
		return;
	
	iPrint->SetPrintPreview(ETrue);
#if !defined(__UIQ__)
	TInt totalNumPages=	iCharsPerPage->Count(); // hard coded this before
	if (CEikPrintPreviewDialog::RunDlgLD(*iPrintSetup,*iPrint,totalNumPages,this,4))
		{
		iPrint->SetPrintPreview(EFalse);
		CEikPrintProgressDialog::RunDlgLD(iPrintSetup,iPrint,iPrintParameters);
		}
#endif
	iPrintSetup->FreeModelList();
	}

/*
  This method is required by MEikEdwinOberver
*/
TBool COplPrintEngine::RunPrintRangeDialogL(CPrintSetup* aPrintSetup, TInt& aNumPagesInDoc)
	{
	TUid olduid = aPrintSetup->PrinterDevice()->Model().iUid;
	TUid uid = olduid;
	iPrintParameters.iFirstPage=0;
	iPrintParameters.iLastPage=aNumPagesInDoc-1;
#if !defined(__UIQ__)
	if (CEikPrintRangeDialog::RunDlgLD(iPrintParameters,aPrintSetup,uid))
		{
		if (olduid!=uid)	
			{
			aPrintSetup->CreatePrinterDeviceL(uid,iOplAPI->EikonEnv().FsSession());
			iPrint->SetPrinterDevice(aPrintSetup->PrinterDevice());
			iDocumentChanged = ETrue;
			if (RunPaginationDialogL())
				return ETrue;
			}
		}
#endif
	aNumPagesInDoc = iCharsPerPage->Count();
	iPrintSetup->FreeModelList();
	return EFalse; //no cancellation
	}

//
// From CEikGlobalTextEditor
//
TBool COplPrintEngine::RunPaginationDialogL()
	{
	iPaginateDialogCalled = ETrue;
	TBool cancel=EFalse;
#if !defined(__UIQ__)
	CTextPaginator* paginator=CTextPaginator::NewL(iPrintSetup->PrinterDevice(),iCharsPerPage,0); // Priority 0
	CleanupStack::PushL(paginator);
	paginator->SetPageMarginsInTwips(iPrintSetup->iPageMarginsInTwips.iMargins);
	paginator->SetPageSpecInTwips(iPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
	paginator->SetDocumentL(iRichText); // takes an MLayDoc
	if (!CEikPaginateDialog::RunDlgLD(paginator))
		cancel=ETrue;
#endif
	CleanupStack::PopAndDestroy(); //paginator;
	iDocumentChanged =EFalse; // Prevent repagination
	iPrintParameters.iFirstPage = 0;
	iPrintParameters.iLastPage = iCharsPerPage->Count()-1;
	return cancel;
	}

void COplPrintEngine::RunPrintDialogL()
	{
	TBool cancel=EFalse;
	if (iDocumentChanged)
		cancel=RunPaginationDialogL();
	if (cancel)
		return;
	TUid olduid=iPrintSetup->PrinterDevice()->Model().iUid; 	
	TUid uid=olduid;
#pragma message ("*** the following two lines produce an alloc heaven on UIKON 308. It's demonstrable with the test code")	
#if !defined(__UIQ__)
	CEikDialog* rangedialog=new(ELeave) CEikPrintRangeDialog(iPrintParameters,iPrintSetup,uid,ETrue);
	if (rangedialog->ExecuteLD(R_EIK_DIALOG_PRINT_RANGE_SETUP))
		{
		if (olduid!=uid)
			{
			iPrintSetup->CreatePrinterDeviceL(uid,iOplAPI->EikonEnv().FsSession());
			iPrint->SetPrinterDevice(iPrintSetup->PrinterDevice());
			iDocumentChanged=ETrue;
			}
		}
	else
#endif
		return;
	if (iDocumentChanged)
		cancel=RunPaginationDialogL();
	if (cancel)
		return;
	//progress
	iPrint->SetPrintPreview(EFalse);
#if !defined(__UIQ__)
 	CEikPrintProgressDialog::RunDlgLD(iPrintSetup,iPrint,iPrintParameters);
#endif
	iPrintSetup->FreeModelList();
	}

TBool COplPrintEngine::PageSetupChangedL(CPrintSetup* aPrintSetup,TInt& aNumPagesInDoc)
	{
	iPrint->SetPageMarginsInTwips(aPrintSetup->iPageMarginsInTwips.iMargins);
	iPrint->SetPageSpecInTwips(aPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
	iDocumentChanged=ETrue;
	if (RunPaginationDialogL())
		return ETrue;
	aNumPagesInDoc=iCharsPerPage->Count();
	return EFalse;
	}

void COplPrintEngine::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
	{
	if (aEventType==EEventStateChanged)
		iDocumentChanged=ETrue;
	if (iPaginateDialogCalled && aEventType==EEventRequestExit)
		{ // !! possibly better - the dialog should define its own observer class
		delete aControl;
		iDocumentChanged=EFalse;
		iPaginateDialogCalled=EFalse;
		}
	}

void COplPrintEngine::ResetPrinting()
	// Remove all content and formatting
	{
	iRichText->Reset();
	iFormatChanged = ETrue;
	iDocumentChanged = ETrue;
	DefaultFormatting();
	iRichText->InsertL(0,KNullDesC);
	}

void COplPrintEngine::DefaultFormatting()
	// Reset the formatting
	{
	TCharFormat defaultCharFormat(iFontName,iFontHeight);
	iCharFormat = defaultCharFormat;
	iCharFormatMask.ClearAll();
	iCharFormatMask.SetAttrib(EAttFontTypeface);
	iCharFormatMask.SetAttrib(EAttFontHeight);
	_LIT(KScreenDeviceName,"scdv.dll");
	CFbsScreenDevice *device = CFbsScreenDevice::NewL(KScreenDeviceName,DefaultDisplayMode());
	iCharFormat.iFontSpec.iTypeface.SetAttributes(FontUtils::TypefaceAttributes(*device,iFontName));
	delete device;
	}

TDisplayMode COplPrintEngine::DefaultDisplayMode() const
	{
	TInt numGrays=0;
	TInt numColors=0;
	TDisplayMode defaultMode=iOplAPI->WsSession().GetDefModeMaxNumColors(numColors,numGrays);
	if (defaultMode != EColor16M)
		{
		return defaultMode;
		}
	else
		{
		return EColor4K;
		}
	}

void COplPrintEngine::InsertStringL(TDesC& aString, const TInt aPos, TTextInsertPosition aTIP)
	// Insert a string at the given position if aTIP == EInsert. Append otherwise
	{
	TInt pos;
	pos = SetInsertPositon(aPos, aTIP);
	iDocumentChanged=ETrue;
	if(iFormatChanged)
		{
		ResetFormatL(pos);
		}
	iRichText->InsertL(pos, aString);
	
	}

void COplPrintEngine::SetFontNameL(TDesC& aName)
	{
	iDocumentChanged=ETrue;
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontTypeface);
	_LIT(KScreenDeviceName,"scdv.dll");
	CFbsScreenDevice *device = CFbsScreenDevice::NewL(KScreenDeviceName,DefaultDisplayMode());
	iCharFormat.iFontSpec.iTypeface.SetAttributes(FontUtils::TypefaceAttributes(*device,aName));
	iCharFormat.iFontSpec.iTypeface.iName = aName;
	delete device;
	}

void COplPrintEngine::SetFontHeight(const TInt16 aHeight)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontHeight);
	iCharFormat.iFontSpec.iHeight = aHeight;
	}

void COplPrintEngine::SetFontPosition(const TInt16 aPosition)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontPrintPos);
	iCharFormat.iFontSpec.iFontStyle.SetPrintPosition((TFontPrintPosition)aPosition);
	}

void COplPrintEngine::SetFontWeight(const TInt16 aWeight)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontStrokeWeight);
	iCharFormat.iFontSpec.iFontStyle.SetStrokeWeight((TFontStrokeWeight)aWeight);
	}

void COplPrintEngine::SetFontPosture(const TInt16 aPosture)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontPosture);
	iCharFormat.iFontSpec.iFontStyle.SetPosture((TFontPosture)aPosture);
	}

void COplPrintEngine::SetFontStrikethrough(const TInt16 aStrikethrough)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontStrikethrough);
	iCharFormat.iFontPresentation.iStrikethrough = (TFontStrikethrough)aStrikethrough;
	}

void COplPrintEngine::SetFontUnderline(const TInt16 aUnderline)
	{
	iFormatChanged = ETrue;
	iCharFormatMask.SetAttrib(EAttFontUnderline);
	iCharFormat.iFontPresentation.iUnderline = (TFontUnderline)aUnderline;
	}

void COplPrintEngine::ResetFormatL(const TInt aPos)
	// If the format has been changed since insert was last called
	// the this member is called to enter the new format at the insert position.
	{
	iRichText->CancelInsertCharFormat();
	if (RemoveUnnessaryCharSettings(&iCharFormat,&iCharFormatMask,aPos))
		iRichText->SetInsertCharFormatL(iCharFormat, iCharFormatMask, aPos);
	iFormatChanged = EFalse;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -