os/textandloc/textrendering/word/SRC/WPDOC.CPP
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <s32std.h>
    20 #include <s32stor.h>
    21 #include <s32file.h>
    22 
    23 #include <f32file.h>
    24 
    25 #include <gdi.h>
    26 
    27 #include <txtrich.h>
    28 #include "txtmrtsr.h"
    29 
    30 #include <frmprint.h>
    31 
    32 #include <prnsetup.h>
    33 
    34 #include <basched.h>
    35 
    36 #include <coemain.h>
    37 
    38 #include <apparc.h>
    39 #include <apgdoor.h>
    40 
    41 #include <eikproc.h>
    42 #include <techview/eikpword.h>
    43 #include <techview/eikon.rsg>
    44 #include <eikfutil.h>
    45 #include <techview/eikdialg.h>
    46 #include <eikenv.h>
    47 
    48 #include "WNGMODEL.H"
    49 
    50 #include "WPDOC.H"
    51 #include "WPAPPUI.H"
    52 #include "WPMAIN.H"
    53 #include "WPPANIC.H"
    54 #include "wpresources.h"
    55 #include <word.rsg>
    56 #include "WORD.HRH"
    57 
    58 //#define ALLOC_TESTING
    59 
    60 //
    61 // CWordDocument
    62 //
    63 
    64 const TUid KUidWordAppUiStream={268436035};
    65 
    66 CWordDocument* CWordDocument::NewL(CEikApplication& aApp)
    67 // Return a handle to a new instance of this class.
    68 //
    69 	{
    70 #if defined(ALLOC_TESTING)
    71 	// reserve some space on the cleanup stack
    72 	{for (TInt ii=0;ii<1000;++ii)
    73 		CleanupStack::PushL(&ii);}
    74 	CleanupStack::Pop(1000);
    75 	//
    76 	__UHEAP_MARK;
    77 	{
    78 	CWordDocument* self=NULL;
    79 	for (TInt ii=2;;++ii)
    80 		{
    81 		__UHEAP_SETFAIL(RHeap::EDeterministic,ii);
    82 		__UHEAP_MARK;
    83 		TInt err=KErrNone;
    84 		self=new(ELeave) CWordDocument(aApp);
    85 		if (self)
    86 			{
    87 			TRAP(err,self->ConstructL());
    88 			delete self;
    89 			self=NULL;
    90 			}
    91 		__UHEAP_MARKEND;
    92 		User::Heap().Check();
    93 		if (err==KErrNone)
    94 			break;
    95 		}
    96 	__UHEAP_MARKEND;
    97 	__UHEAP_RESET;
    98 	User::Heap().Check();
    99 	}
   100 #endif
   101 	CWordDocument* self=new(ELeave) CWordDocument(aApp);
   102 	CleanupStack::PushL(self);
   103 	self->ConstructL();
   104 	CleanupStack::Pop();
   105 	return self;
   106 	}
   107 
   108 
   109 void CWordDocument::ConstructL()
   110 // Complete initialisation
   111 //
   112 	{
   113 	CreateModelL();
   114 	ConstructPrintL();
   115 	iUiData=new(ELeave) TWordUiData;
   116 	}
   117 
   118 
   119 CWordDocument::~CWordDocument()
   120 	{
   121 	delete iModel;
   122 	delete iPrint;
   123 	delete iUiData;
   124 	}
   125 
   126 
   127 void CWordDocument::CreateModelL()
   128 	{iModel=CWordModel::NewL(this,this);}
   129 
   130 
   131 void CWordDocument::ConstructPrintL()
   132 // Print initialisation
   133 //
   134 	{
   135 	CCoeEnv* coeEnv=CCoeEnv::Static();
   136 	CWordModel& model=*Model();
   137 	CPrintSetup& printSetup=*model.PrintSetup();
   138 	printSetup.CreatePrinterDeviceL(KUidPrinterDevice,coeEnv->FsSession());
   139 	iPrint=CTextPageRegionPrinter::NewL(model.Text(),printSetup.PrinterDevice());
   140 	//
   141 	TPageMargins margins;									 
   142 	margins.iMargins.iLeft=1440;
   143 	margins.iMargins.iRight=1440;			
   144 	margins.iMargins.iTop=1440;		   
   145 	margins.iMargins.iBottom=1440;
   146 	margins.iHeaderOffset=720;
   147 	margins.iFooterOffset=720;
   148 	TPageSpec pageSpec;
   149 	pageSpec.iPortraitPageSize=TSize(11906,16838);	   // A4 !!	
   150 	printSetup.iPageMarginsInTwips=margins;
   151   	printSetup.PrinterDevice()->SelectPageSpecInTwips(pageSpec);	   // A4 !!
   152 	}
   153 
   154 
   155 void CWordDocument::NewDocumentL()
   156 // Builds a new main (container) document, creating content from a standard file.
   157 // If a leave occurs whilst loading the standard document, current document
   158 // content will not be lost.
   159 //
   160 	{ 
   161 	TFileName templateFileName;
   162 	TWordFilePath::GetNormalTemplateFolderName(templateFileName);
   163 	TFindFile* ff = new(ELeave) TFindFile(Process()->FsSession());
   164 	CleanupStack::PushL(ff);
   165 	TFileName* tempFileName = new(ELeave) TFileName();
   166 	CleanupStack::PushL(tempFileName);
   167 	CCoeEnv* coeEnv = CCoeEnv::Static();
   168 	coeEnv->ReadResource(*tempFileName, R_WORD_NORMAL_TEMPLATE_FILE_NAME);
   169 	templateFileName.Append(*tempFileName);
   170 	// look for a normal template
   171 	templateFileName[0] = 'Y';
   172 	TParsePtrC tParse(templateFileName);
   173 	TInt error = ff->FindByDir(templateFileName,tParse.DriveAndPath());
   174 	if(error)
   175 		{
   176 		// no normal template, search ROM for blank
   177 		TWordFilePath::GetNormalTemplateFolderName(templateFileName);
   178 		coeEnv->ReadResource(*tempFileName, R_WORD_BLANK_TEMPLATE_FILE_NAME);
   179 		templateFileName.Append(*tempFileName);
   180 		templateFileName[0] = 'Z';
   181 		TParsePtr blankParse(templateFileName);
   182 		error = ff->FindByDir(blankParse.NameAndExt(),blankParse.DriveAndPath());
   183 		}
   184 	CleanupStack::PopAndDestroy(); // tempFileName
   185 	if(error!=KErrNone)
   186 		Panic(EDefaultTemplateNotPresent);
   187 	templateFileName = ff->File();
   188 	CleanupStack::PopAndDestroy(); // ff
   189 	DoNewDocumentL(templateFileName);
   190 	if (iAppUi)
   191 		WordAppUi().NotifyNewTextL();  // edwin must know of new text for focus loss etc...
   192 	}
   193 
   194 
   195 CFileStore* CWordDocument::DoNewDocumentL(const TDesC& aStandardDocument,TKeepStoreOpen aKeepStoreOpen/*=EKeepFalse*/)
   196 // Open and restore the specified valid document.
   197 // The store that is opened over the file for restoration may be kept open, or destroyed, depending on
   198 // the value of aKeepStoreOpen.
   199 //
   200 	{
   201 	CFileStore* store=NULL;
   202 	CStreamDictionary* dictionary=CApaProcess::ReadRootStreamLC(Process()->FsSession(),store,aStandardDocument,EFileShareExclusive|EFileRead);  // takes ownership of store
   203 	CleanupStack::PushL(store);
   204 	iNewDocument = ETrue;
   205 	RestoreL(*store,*dictionary);
   206 	iNewDocument = EFalse;
   207 	iModel->Text()->DetachFromStoreL(CPicture::EDetachFull);
   208 	if (aKeepStoreOpen==EKeepTrue)
   209 		CleanupStack::Pop();  // store
   210 	else
   211 		CleanupStack::PopAndDestroy();  // store
   212 	CleanupStack::PopAndDestroy();  // dictionary
   213 	return (aKeepStoreOpen==EKeepTrue)
   214 		? store
   215 		: NULL;
   216 	}
   217 
   218 
   219 void CWordDocument::LocateTemplateL(const TDes& aFullFileName) const
   220 // Verify that the template file selected by the user is still present
   221 //
   222 	{
   223 	TFindFile ff(Process()->FsSession());
   224 	TParsePtrC tParse(aFullFileName);
   225 	User::LeaveIfError(ff.FindByDir(aFullFileName,tParse.DriveAndPath()));
   226 	VerifyDocumentTypeL(aFullFileName);
   227 	}
   228 
   229 void CWordDocument::VerifyDocumentTypeL(const TFileName& aFileName)const
   230 // Verify the document of the specified name is indeed of the
   231 // same type as this document object.
   232 //
   233 	{
   234 	TEntry entry;
   235 	User::LeaveIfError(Process()->FsSession().Entry(aFileName,entry));
   236 	if (entry.MostDerivedUid()!=Application()->AppDllUid())
   237 		User::Leave(KErrCorrupt);
   238 	}
   239 
   240 
   241 const CStreamStore& CWordDocument::StreamStoreL(TInt /*aPos*/)const
   242 // In this case the store does not vary with document position.
   243 //
   244 	{return *iEditStore;}
   245 
   246 
   247 void CWordDocument::DetachFromStoreL(CPicture::TDetach aDegree)
   248 // Propogate the detachFromStore to all picture container components.
   249 //
   250 	{
   251 	iModel->Text()->DetachFromStoreL(aDegree);
   252 	CRichText* text=iModel->PrintSetup()->Header()->Text();
   253 	if (text)
   254 		text->DetachFromStoreL(aDegree);
   255 	text=iModel->PrintSetup()->Footer()->Text();
   256 	if (text)
   257 		text->DetachFromStoreL(aDegree);
   258 	iEditStore=NULL;
   259 	}
   260 
   261 void CWordDocument::StoreUiDataL(CStreamStore& aStore,CStreamDictionary& aStreamDic)const
   262 // Write any Ui data
   263 //
   264 	{
   265 	if (iAppUi)
   266 		WordAppUi().GetUiData(*iUiData);
   267 
   268 	RStoreWriteStream stream;
   269 	TStreamId id=stream.CreateLC(aStore);
   270 	stream <<*iUiData;
   271 	stream.CommitL();
   272 	CleanupStack::PopAndDestroy();  // stream
   273 
   274 	aStreamDic.AssignL(KUidWordAppUiStream,id);
   275 	}
   276 
   277 
   278 void CWordDocument::StoreL(CStreamStore& aStore,CStreamDictionary& aStreamDic)const
   279 // Write this object network.
   280 //
   281 	{
   282 	if (iAppUi)
   283 		CEikonEnv::Static()->BusyMsgL(R_EIK_TBUF_SAVING_FILE,KFileBusyInitialDelayInMicroSeconds);
   284 	StoreUiDataL(aStore,aStreamDic);
   285 	iModel->StoreL(aStore, aStreamDic, 0);
   286 	if (iAppUi)
   287 		CEikonEnv::Static()->BusyMsgCancel();
   288 	}
   289 
   290 
   291 void CWordDocument::RestoreUiDataL(const CStreamStore& aStore,const CStreamDictionary& aStreamDic)
   292 // Restore any ui data for future use
   293 //
   294 	{
   295 	TStreamId id=aStreamDic.At(KUidWordAppUiStream);
   296 	if (id==KNullStreamId)
   297 		iUiData->Reset();
   298 	else
   299 		{
   300 		RStoreReadStream stream;
   301 		stream.OpenLC(aStore,id);
   302 		stream>> *iUiData;
   303 		CleanupStack::PopAndDestroy();  // stream
   304 		}
   305 	}
   306 
   307 
   308 void CWordDocument::RestoreL(const CStreamStore& aStore,const CStreamDictionary& aStreamDic)
   309 // Restore this document.
   310 // All model restoration is done here, to save having a temporary model member data,
   311 // implementing a rollback robust model restore.
   312 //
   313 	{
   314 	CEikonEnv* eikonEnv=CEikonEnv::Static();
   315 	if (iAppUi && !iNewDocument)
   316 		eikonEnv->BusyMsgL(R_EIK_TBUF_OPENING_FILE,KFileBusyInitialDelayInMicroSeconds);
   317 	else if (iAppUi && iNewDocument)
   318 		eikonEnv->BusyMsgL(R_WORD_FILE_CREATE_FILE,KFileBusyInitialDelayInMicroSeconds);
   319 	//
   320 	CWordModel* tempModel=CWordModel::NewL(this,this);
   321 	CleanupStack::PushL(tempModel);
   322 	//
   323 	CStreamStore* oldStore=iEditStore;
   324 	iEditStore=CONST_CAST(CStreamStore*,&aStore);
   325 	//
   326 	tempModel->Text()->SetPictureFactory(&iPictureFactory,this);  // rich text callbacks
   327 	TRAPD(ret,
   328 		tempModel->RestoreL(aStore,aStreamDic,0,this,this,eikonEnv->PictureFactory()));  // print setup field callbacks
   329 	if (ret!=KErrNone)
   330 		{
   331 		iEditStore=oldStore;
   332 		User::Leave(ret);
   333 		}
   334 	delete iModel;
   335 	iModel=tempModel;  // document takes over ownership
   336 	CleanupStack::Pop();  // tempModel
   337 
   338 	SetChanged(EFalse);  // this is a brand new document - it cannot yet have been changed.
   339 
   340 	TRAP(ret,
   341 	RestoreUiDataL(aStore,aStreamDic));
   342 		if (ret!=KErrNone)
   343 			iUiData->Reset();  // do not propogate.  We can handle this.
   344 	//
   345 	if (iAppUi)
   346 		{
   347 		WordAppUi().SetUiData();
   348 //		CEikonEnv::Static()->BusyMsgCancel();
   349 		}
   350 	}
   351 
   352 
   353 TInt CWordDocument::UpdateFieldFileName(TPtr& aValueText)const
   354 // Set aValueText with the current document filename only.
   355 // (No path or extension).
   356 //
   357 	{
   358 	TParsePtrC parser(Process()->MainDocFileName());
   359 	TPtrC pp=parser.NameAndExt();
   360 	if (pp.Length()>aValueText.MaxLength())
   361 		return pp.Length();
   362 	aValueText=pp;
   363 	return 0;
   364 	}
   365 
   366 
   367 TInt CWordDocument::UpdateFieldNumPages()const
   368 // Return the number of pages currently in this document.
   369 // Precondition: pagination of the document has already occured.
   370 //
   371 	{return iModel->PageTable()->Count();}
   372 
   373 
   374 void CWordDocument::DoFileNewL(const TFileName& aNewFilename, const TFileName& aTemplateFileName)
   375 // Create an initialised document instance with the specified name,
   376 // and initialise a new file store to store the document in.
   377 // (A new document is not created, *this* instance is re-used).
   378 //
   379 	{
   380 	//
   381 	// Create the file store for the new document
   382 	LocateTemplateL(aTemplateFileName);
   383 	User::LeaveIfError(EikFileUtils::CopyFile(aTemplateFileName,aNewFilename));
   384 	CCoeEnv* coeEnv=CCoeEnv::Static();
   385 	User::LeaveIfError(coeEnv->FsSession().SetAtt(aNewFilename,KEntryAttNormal,KEntryAttReadOnly));
   386 	// turn read-only file attribute if set, just to be safe.
   387 	//
   388 	CFileStore* store=DoNewDocumentL(aNewFilename,EKeepTrue);
   389 	__ASSERT_DEBUG(store,Panic(ENewFileNullStore));
   390 	//
   391 	SetMainStoreAndMainDocFileName(store,aNewFilename);
   392 	SetChanged(ETrue);
   393 	TUint appFileMode=AppFileMode();
   394 	appFileMode|=EFileWrite;
   395 	SetAppFileMode(appFileMode);
   396 	WordAppUi().SetReadOnly((TBool)!(appFileMode&EFileWrite));
   397 	}
   398 
   399 
   400 void CWordDocument::SetMainStoreAndMainDocFileName(CFileStore* aMainStore,const TDesC& aMainDocFileName)
   401 	{
   402 	CEikProcess& process=REINTERPRET_CAST(CEikProcess&,*Process());
   403 	delete process.MainStore();  // remove the [original] store
   404 	process.SetMainStore(aMainStore);  // set the new file store as the main one.
   405 	process.SetMainDocFileName(aMainDocFileName);
   406 	}
   407 
   408 
   409 void CWordDocument::DoFileOpenL(const TFileName& aFileName,TUint aFileMode)
   410 // Open the Word document specified by aFileName.
   411 //
   412 	{
   413 	VerifyDocumentTypeL(aFileName);
   414 	RFs session=Process()->FsSession();
   415 	GetFileOpenModeL(aFileMode,aFileName,session);
   416 //
   417 	CFileStore* store=NULL;
   418 	CStreamDictionary* dic=CApaProcess::ReadRootStreamLC(session,store,aFileName,EFileShareExclusive|aFileMode);
   419 	CleanupStack::PushL(store);
   420 	RestoreL(*store,*dic);
   421 	CleanupStack::Pop();  // store
   422 	CleanupStack::PopAndDestroy();  // dictionary
   423 	//
   424 	SetMainStoreAndMainDocFileName(store,aFileName);
   425 	SetAppFileMode(aFileMode);
   426 	WordAppUi().SetReadOnly((TBool)!(aFileMode&EFileWrite));
   427 	}
   428 
   429 
   430 void CWordDocument::GetFileOpenModeL(TUint& aFileMode,const TDesC& aFilename,RFs& aSession)
   431 // Attempt to open the specified file in the specified mode.
   432 // If this fails because write access was requested on a read-only file,
   433 // the file is opened read-only.
   434 // Any other error (leave) is propogated.
   435 // aFileMode is set to the mode that the file was successfully opened in.
   436 //
   437 	{
   438 	CFileStore* store=NULL;
   439 	TRAPD(ret,
   440 	store=CFileStore::OpenL(aSession,aFilename,aFileMode));
   441 	if (ret==KErrAccessDenied && aFileMode&EFileWrite)
   442 		{
   443 		ret=KErrNone;
   444 		aFileMode&=~EFileWrite;
   445 		store=CFileStore::OpenL(aSession,aFilename,aFileMode);
   446 		}
   447 	//
   448 	User::LeaveIfError(ret);
   449 	__ASSERT_DEBUG(ret==KErrNone && store,Panic(EFileOpenIntegrityError));
   450 	delete store;
   451 	}
   452 
   453 
   454 void CWordDocument::DoFileSaveToCurrentL()
   455 	{
   456 	SaveL();	  
   457 	}
   458 
   459 
   460 void CWordDocument::DoFileSaveToNewL(const TFileName& aNewFileName)
   461 	{
   462 	((CEikProcess*)Process())->SaveToDirectFileStoreL(this,&aNewFileName); // writes root stream
   463 	SetAppFileMode(EFileWrite);
   464 	}
   465 
   466 void CWordDocument::DoFileSaveToNewNoSwitchL(const TFileName& aNewFileName)
   467 // Save the current file to a new file, but don't open the new file
   468 	{
   469 	// From CEikProcess::SaveToDirectFileStoreL but without the close iMainDoc at the end
   470 	//
   471 	CApaProcess* proc = Process();
   472 	RFs& fsSession=proc->FsSession();
   473 	CDirectFileStore* store;
   474 	store = CDirectFileStore::CreateLC(fsSession,aNewFileName,EFileWrite);
   475 	store->SetTypeL(REINTERPRET_CAST(CEikProcess*,proc)->MainStore()->Type());
   476 	CStreamDictionary* streamDic=CStreamDictionary::NewL();
   477 	CleanupStack::PushL(streamDic);
   478 	proc->MainDocument()->StoreL(*store,*streamDic);
   479 	// write root stream
   480 	proc->WriteRootStreamL(*store,*streamDic,*(this->Application()));
   481 	CleanupStack::PopAndDestroy(); // streamDic
   482 	// close the new store
   483 	store->CommitL();
   484 	CleanupStack::PopAndDestroy(); // store
   485 	}
   486 
   487 void CWordDocument::DoFileRevertL()
   488 	{
   489 	CStreamDictionary* streamDic=CStreamDictionary::NewL();
   490 	CleanupStack::PushL(streamDic);
   491 	CFileStore* mainStore=REINTERPRET_CAST(CEikProcess*,Process())->MainStore();
   492 	RStoreReadStream root;
   493 	root.OpenLC(*mainStore,mainStore->Root());
   494 	root>> *streamDic;
   495 	root.Close();
   496 	CleanupStack::PopAndDestroy(); // root
   497 	RestoreL(*mainStore,*streamDic);
   498 	CleanupStack::PopAndDestroy(); // streamDic
   499 	}
   500 
   501 //
   502 // CWordApplication
   503 //
   504 
   505 TUid CWordApplication::AppDllUid() const
   506 	{
   507 	return KUidWordAppValue;
   508 	}
   509 
   510 CApaDocument* CWordApplication::CreateDocumentL()
   511 	{return CWordDocument::NewL(*this);}
   512 
   513 
   514 CEikAppUi* CWordDocument::CreateAppUiL()
   515 	{
   516 #if defined(ALLOC_TESTING)
   517 	// reserve some space on the cleanup stack
   518 	{for (TInt ii=0;ii<1000;++ii)
   519 		CleanupStack::PushL(&ii);}
   520 	CleanupStack::Pop(1000);
   521 	// construct the appUi before the setfail loop so that cached resources
   522 	// do not cause alloc failure
   523 	CWordAppUi* appUi=NULL;
   524 	appUi=new(ELeave) CWordAppUi;
   525 	appUi->SetDocument(this);
   526 	appUi->ConstructL();
   527 	delete appUi;
   528 	appUi=NULL;
   529 	__UHEAP_MARK;
   530 	{
   531 	for (TInt ii=2;;++ii)
   532 		{
   533 		__UHEAP_SETFAIL(RHeap::EDeterministic,ii);
   534 		__UHEAP_MARK;
   535 		appUi=new(ELeave) CWordAppUi;
   536 		appUi->SetDocument(this);
   537 		TInt err=KErrNone;
   538 		if (appUi)
   539 			{
   540 			TRAP(err,appUi->ConstructL());
   541 			delete appUi;
   542 			appUi=NULL;
   543 			}
   544 		__UHEAP_MARKEND;
   545 		User::Heap().Check();
   546 		if (err==KErrNone)
   547 			break;
   548 		}
   549 	__UHEAP_MARKEND;
   550 	__UHEAP_RESET;
   551 	User::Heap().Check();
   552 	}
   553 #endif
   554     return(new(ELeave) CWordAppUi);
   555 	}