texted.cpp

来自「在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己」· C++ 代码 · 共 1,978 行 · 第 1/5 页

CPP
1,978
字号
	paraFormatLayer->SenseEffectiveL(paraFormat);
	if(tabStop!=paraFormat->iDefaultTabWidthInTwips)
		{
		TParaFormatMask attribChangedMask;
		attribChangedMask.SetAttrib(EAttDefaultTabWidth);
		paraFormat->iDefaultTabWidthInTwips=tabStop;
		paraFormatLayer->SetL(paraFormat,attribChangedMask);
		iGlobalEd->ApplyParaFormatL(paraFormat,attribChangedMask);
		}
	iTabWidthInChars=aTabWidthInChars;
	CleanupStack::PopAndDestroy();
	}

void CTxtedAppUi::SetPreferencesL()
	{
	CEikDialog* dialog=new(ELeave) CTextEdSetPreferencesDialog(&iIsMonoSpaced,&iNonPrintingCharVisibility,iExportFormatOption);
	if (dialog->ExecuteLD(R_TXED_DIALOG_SETPREFERENCES))
		{
		iGlobalEd->TextLayout()->SetNonPrintingCharsVisibility(iNonPrintingCharVisibility);
		iGlobalEd->NotifyNewFormatL();
		StorePrefs();
		}
	}

#if defined(USE_PRINTING)
void CTxtedAppUi::RunPageSetupDialogL()
	{
	if (CEikPageSetupDialog::RunDlgLD(iPrintSetup))
		PrintSetupChanged(iPrintSetup);
	}

TBool CTxtedAppUi::RunPaginationDialogL(CPrintSetup& aPrintSetup, TCknPrintParameters& aPrintParameters)
	{
	TBool paginationCancelled = EFalse;
	if (iPaginateDocumentChanged)
		{
		paginationCancelled = iGlobalEd->RunPaginateDialogL(&aPrintSetup,iPageTable);
		if (!paginationCancelled)
			{
			aPrintParameters.iParams.iFirstPage=0;
			aPrintParameters.iParams.iLastPage=iPageTable->Count()-1;
			iGlobalEd->GlobalText()->SetPageTable(iPageTable);
			iPaginateDocumentChanged=EFalse;
			}
		}
	return paginationCancelled;
	}

TBool CTxtedAppUi::RunPaginationDialogL()
	{
	return RunPaginationDialogL(*iPrintSetup,iPrintParameters);
	}

TBool CTxtedAppUi::RunPrintPreviewDialogL(CPrintSetup& aPrintSetup, MPageRegionPrinter& aPrinter)
	{
	TInt pageCount=iPageTable->Count();
	return CEikPrintPreviewDialog::RunDlgLD(aPrintSetup,
												aPrinter,
												pageCount,
												this,
												KPrintPreviewBandsPerPage);
	}

void CTxtedAppUi::RunPreviewDialogL()
	{
	iPrint->SetPrintPreview(ETrue);
	if (RunPrintPreviewDialogL(*iPrintSetup,*iPrint))
		RunPrintDialogL();
	iGlobalEd->DrawDeferred();
	}

TBool CTxtedAppUi::PageSetupChangedL(CPrintSetup* aPrintSetup,TInt& aNumPagesInDoc)
	// Return ETrue if pagination is cancelled, otherwise return EFalse.
	{
	iPrint->SetPageMarginsInTwips(aPrintSetup->iPageMarginsInTwips.iMargins);
	iPrint->SetPageSpecInTwips(aPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
	iPaginateDocumentChanged=ETrue; // need to paginate if page spec changed
	if (RunPaginationDialogL()) // returns ETrue if pagination cancelled
		return ETrue;
	aNumPagesInDoc=iPageTable->Count();
	return EFalse;
	}

TBool CTxtedAppUi::RunPrintRangeDialogL(CPrintSetup* aPrintSetup,TInt& aNumPagesInDoc)
	// Return ETrue if pagination is cancelled, otherwise return EFalse.
	{
	aPrintSetup->CreatePrinterDeviceL(aPrintSetup->PrinterDevice()->Model().iUid,iCoeEnv->FsSession());
	return PageSetupChangedL(aPrintSetup,aNumPagesInDoc);
	}

void CTxtedAppUi::RunPrintDialogL()
	{
	TUid oldUid = iPrintSetup->PrinterDevice()->Model().iUid;
	TUid newUid = oldUid;
	TBool printRequested = CCknPrintDialog::RunDlgLD(*iPrint,this,iPrintParameters,*iPrintSetup,newUid);
	if (newUid != oldUid)
		{
		iPrintSetup->CreatePrinterDeviceL(newUid, iEikonEnv->FsSession());
		iPrint->SetPrinterDevice(iPrintSetup->PrinterDevice());
		}
	if (printRequested)
		DoPrintL();
	}

void CTxtedAppUi::DoPrintL()
	// Run the print progress dialog
	{
	iPrint->SetPrintPreview(EFalse);
	CEikPrintProgressDialog::RunDlgLD(iPrintSetup,iPrint,iPrintParameters.iParams);
	iPrintSetup->FreeModelList();
	}

void CTxtedAppUi::PrintSetupChanged(CPrintSetup* aPrintSetup)
	{
	iPrint->SetPrinterDevice(aPrintSetup->PrinterDevice());
	iPrint->SetPageMarginsInTwips(aPrintSetup->iPageMarginsInTwips.iMargins);
	iPrint->SetPageSpecInTwips(aPrintSetup->PrinterDevice()->CurrentPageSpecInTwips());
	STATIC_CAST(CTextEdDocument*,iDocument)->SetDocChanged(ETrue);
	iPaginateDocumentChanged=ETrue; // need to paginate if page spec changed
	}
#endif

TInt CTxtedAppUi::UpdateFieldFileName(TPtr& aValueText) const
	{
	TPtrC fileName=iEikonEnv->Process()->MainDocFileName();
	if (fileName.Length()>aValueText.MaxLength())
		aValueText=fileName.Left(aValueText.MaxLength());
	else
		aValueText=fileName;
	return KErrNone;
	}

TInt CTxtedAppUi::UpdateFieldNumPages() const
	{
#if defined(USE_PRINTING)
	return iPageTable->Count();
#else
	return 0;
#endif
	}

void CTxtedAppUi::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
	{
	if (aControl==iGlobalEd&&aEventType==EEventStateChanged)
		{
#if defined(USE_PRINTING)
		iPaginateDocumentChanged=ETrue;
#endif
		STATIC_CAST(CTextEdDocument*,iDocument)->SetDocChanged(ETrue);
		}
	}

//////////////////
// File dialogs //
//////////////////
void CTxtedAppUi::CmdOpenFileL()
	{
	TFileName fileName=iEikonEnv->Process()->MainDocFolder();
	TParsePtrC parser(fileName);
	fileName = parser.DriveAndPath();
#if defined(__UIQ__)
	CEikFileOpenDialog* fileDialog=new (ELeave)CEikFileOpenDialog(&fileName);
	if(fileDialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
#else
	if (CCknOpenFileDialog::RunDlgLD(fileName,KUidTextEdApp,CCknFileListDialogBase::EShowAllDrives,NULL))
#endif
		OpenFileL(fileName);
	}

void CTxtedAppUi::OpenFileL(const TDesC& aFileName)
	{
	((CTextEdDocument*)iDocument)->DoOpenFileL(aFileName); // Saves current file if changed
	iEikonEnv->UpdateTaskNameL();
	ResetViewL();
	}

void CTxtedAppUi::CmdNewFileL()
	{ 
	TFileName fileName;
	TFileName defaultFileName;
	iEikonEnv->ReadResource(defaultFileName,R_TXED_DEFAULT_FILE_NAME);
	CEikProcess* process=iEikonEnv->Process();
	TFileName tempFileName=process->MainDocFileName(); 
	TParsePtrC parser(tempFileName);
	fileName = parser.DriveAndPath();
	TRAPD(ret,ConeUtils::EnsurePathExistsL(fileName));
	if (ret!=KErrNone)
		{ // If the path is e.g. read-only default to C:
		fileName=KOplDefaultFilePath;
		ConeUtils::EnsurePathExistsL(fileName);
		}
	fileName.Append(defaultFileName);
	User::LeaveIfError(CEikApplication::GenerateFileName(iEikonEnv->Process()->FsSession(),fileName));
#if defined(__UIQ__)
	CEikFileSaveAsDialog* saveAsDlg = new (ELeave) CEikFileSaveAsDialog(&fileName);
	if(saveAsDlg->ExecuteLD(R_EIK_DIALOG_FILE_SAVEAS))
#else
	if (CCknSaveAsFileDialog::RunDlgLD(fileName,R_EIK_TBUF_CREATE_NEW_FILE,CCknFileListDialogBase::EShowAllDrives,KUidTextEdApp))
#endif
		CreateFileL(fileName);
	}

void CTxtedAppUi::CreateFileL(const TDesC& aFileName)
	{
	ConeUtils::EnsurePathExistsL(aFileName);
	((CTextEdDocument*)iDocument)->DoNewFileL(aFileName); // Saves current file if changed
	iEikonEnv->UpdateTaskNameL();
	ResetViewL();
	}

void CTxtedAppUi::CmdFileSaveL()
	{
	((CTextEdDocument*)iDocument)->SaveL();
	iEikonEnv->InfoMsg(R_EIK_TBUF_SAVED);
	}

void CTxtedAppUi::CmdFileSaveAsL()
	{
	CEikProcess* process=iEikonEnv->Process();
	TFileName fileName=process->MainDocFileName(); 
#if defined(__UIQ__)
	CEikFileSaveAsDialog* saveAsDlg = new (ELeave) CEikFileSaveAsDialog(&fileName);
	if(saveAsDlg->ExecuteLD(R_EIK_DIALOG_FILE_SAVEAS))
#else
	if (CCknSaveAsFileDialog::RunDlgLD(fileName,CCknFileListDialogBase::EShowAllDrives,KUidTextEdApp))
#endif
		{
		ConeUtils::EnsurePathExistsL(fileName);
		if (fileName.CompareF(process->MainDocFileName())==0)
			{
			CmdFileSaveL();
			iEikonEnv->InfoMsg(R_EIK_TBUF_SAVED);
			return;
			}
		((CTextEdDocument*)iDocument)->DoSaveToNewFileL(fileName);
		iEikonEnv->UpdateTaskNameL();
		iGlobalEd->SetReadOnly(EFalse); // Must be able to write as we've just saved it
		SetTitleBarFileNameL(fileName);
		iEikonEnv->InfoMsg(R_EIK_TBUF_SAVED);
		}
	}

void CTxtedAppUi::CmdFileRevertL()
	{
	if (!iDocument->HasChanged())
		{
		iEikonEnv->InfoMsg(R_TXED_FILE_UNCHANGED);
		return;
		}
#if defined(__UIQ__)
	if(iEikonEnv->QueryWinL(R_TXED_REVERT_TO_SAVED_TITLE,R_TXED_REVERT_TO_SAVED_TEXT))
#else

	if (CCknConfirmationDialog::RunDlgLD(R_TXED_REVERT_TO_SAVED_TITLE,
										 R_TXED_REVERT_TO_SAVED_TEXT,
										 NULL,
										 R_TXED_BTEXT_REVERT_TO_SAVED))
#endif
		{
		CPrintSetup* pPS=iEikonEnv->NewDefaultPrintSetupL();
		CleanupStack::PushL(pPS);
		CTextEdApplication* app=STATIC_CAST(CTextEdApplication*,iDocument->Application());
		CGlobalText* pGT=CGlobalText::NewL(app->iParaFormat,app->iCharFormat);
		CleanupStack::Pop();
		STATIC_CAST(CTextEdDocument*,iDocument)->DoFileRevertL(pGT,pPS);
		iGlobalEd->ResetWindow();
		TRAPD(err,ResetViewL());
		if (err)
			iEikonEnv->HandleErrorWithContext(err,R_EIK_TBUF_ERROR_WHILE_REDRAWING);
		}
	}

TFileName CTxtedAppUi::CurrentFilePath() const
	{
	TParsePtrC parser(iEikonEnv->Process()->MainDocFileName());
	return parser.DriveAndPath();
	}

void CTxtedAppUi::ImportTextL()
	{ // Import a text file.
	TFileName fileName=CurrentFilePath();
	TInt err=KErrNone;
	// Call the import dialog. There's a nasty hack going on here too - use
	// KUidOplTranslatorProvider to prevent the import dialog also
	// listing TextEd files for import as it does if you just use KNullUid
#if defined(__UIQ__)
	CEikFileOpenDialog* fileDialog=new (ELeave)CEikFileOpenDialog(&fileName);
	if(fileDialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
#else
	if (CCknOpenFileDialog::RunImportDlgLD(fileName,R_TXED_IMPORT_DIALOG_SHOW_TEXT,CCknFileListDialogBase::EShowAllDrives,KUidOplTranslatorProvider,&KTextExtension))
#endif
		{
		RFs fs=iEikonEnv->FsSession();
		// First check this is not a standard Symbian file with UIDs. If it is, report
		// the error and return
		CFileStore* store=NULL;
		TRAP(err,store=CDirectFileStore::OpenL(fs,fileName,EFileStream|EFileRead|EFileShareReadersOnly));
		if (err==KErrNone) // If it was a direct store we tried to open
			{
			CleanupStack::PushL(store);
			const TUidType fileUidType=store->Type();
			if (fileUidType[0]==KDirectFileStoreLayoutUid || fileUidType[1]==KUidAppDllDoc)
				err=KErrNotSupported;
			CleanupStack::PopAndDestroy(); // store
			}
		// Show an error and abort for KErrNotSupported or KErrInUse errors. For all other
		// errors try to import as text. For example KErrEof is sometimes raised for
		// empty/small files (e.g. single EOLN test) and KErrCorrupt can be raised for much
		// larger files.
		if (err==KErrInUse)
			{
			iEikonEnv->InfoMsg(R_TXED_FILE_IN_USE);
			return;
			}
		else if (err==KErrNotSupported)
			{
			iEikonEnv->InfoMsg(R_TXED_CANNOT_IMPORT_FILE);
			return;
			}
		// Next, see if the file is Unicode or ASCII text. If it's Unicode it *probably* has
		// 0xFFFE or 0xFEFF at the start. So open the file and check for this - if it's found
		// then we can use the normal CEikEdwin import method. Otherwise we have to do a
		// 'messy' import from ASCII.
		RFile file;
		User::LeaveIfError(file.Open(fs, fileName, EFileStreamText | EFileShareAny));
		CleanupClosePushL(file);
		TInt size=0;
		User::LeaveIfError(file.Size(size));
		if (size==0)
			{
			iEikonEnv->InfoMsg(R_TXED_NOTHING_TO_IMPORT);
			CleanupStack::PopAndDestroy(); // file (calls Close() automatically)
			return;
			}
		// Read the first two bytes
		HBufC8* tempBuf=HBufC8::NewLC(2);
		TPtr8 ptr(tempBuf->Des());
		User::LeaveIfError(file.Read(ptr,2));
		if ((ptr[0]==0xFF && ptr[1]==0xFE) || (ptr[0]==0xFE && ptr[1]==0xFF)) // Unicode
			{
			CleanupStack::PopAndDestroy(2); // tempBuf,file (calls Close() automatically)
			iGlobalEd->InsertFromTextFileL(fileName,CPlainText::EOrganiseByParagraph); 
			}
		else // Assume ASCII
			{
			CleanupStack::PopAndDestroy(); // tempBuf
			TInt seekPos=0;
			User::LeaveIfError(file.Seek(ESeekStart,seekPos)); // reset file reading to beginning
			iEikonEnv->BusyMsgL(R_TXED_IMPORTING,500000); // after 0.5 seconds
			TRAP(err,DoAsciiImportL(file,size));
			CleanupStack::PopAndDestroy(); // file (calls Close() automatically)
			iEikonEnv->BusyMsgCancel();
			User::LeaveIfError(err);
			}
		}
	}

void CTxtedAppUi::DoAsciiImportL(const RFile& aFile, const TInt aSize)
	{
	iGlobalEd->CancelFepTransaction();
	const TInt oldLength=iGlobalEd->TextLength();
	const TInt oldCursorPos=iGlobalEd->CursorPos();

	HBufC8* narrowBuf=HBufC8::NewLC(aSize);
	TPtr8 ptr(narrowBuf->Des());
	User::LeaveIfError(aFile.Read(ptr));
	HBufC* wideBuf=COplRuntimeCharConv::ConvertFromNarrowToUnicodeLC(*narrowBuf);

	// Now replace ASCII EOL characters with the Unicode one. We need to replace
	// KCarriageReturnLineFeed *before* KLineFeed since KCarriageReturnLineFeed
	// also contains a KLineFeed character. This allows us to cope with both DOS
	// and Unix format text files
	TPtr ptrW(wideBuf->Des());
	TBuf<1> unicodeLineDelimeter;
	unicodeLineDelimeter.Append(CEditableText::EParagraphDelimiter); // Works better than ELineBreak
	ReplaceWithLineDelimiter(ptrW, KCarriageReturnLineFeed, unicodeLineDelimeter);
	ReplaceWithLineDelimiter(ptrW, KLineFeed, unicodeLineDelimeter);

	// With all the conversions done, insert the text and then mirror the behaviour of
	// InsertFromTextFileL()'s so that the user sees consistent display results with
	// a Unicode import
	iGlobalEd->Text()->InsertL(oldCursorPos,*wideBuf);
	const TInt newLength=iGlobalEd->TextLength();
	const TInt newCursorPos=oldCursorPos+newLength-oldLength;
	CTextView* textView=iGlobalEd->TextView();
	textView->SetPendingSelection(TCursorSelection(newCursorPos,newCursorPos));
	if (newLength>KFullFormattingUpperThreshold &&
		oldLength<=KFullFormattingUpperThreshold)
		iGlobalEd->SetAmountToFormatL();
	else
		textView->HandleInsertDeleteL(TCursorSelection(newCursorPos,oldCursorPos),0,ETrue);
	iGlobalEd->DrawContents();
	iGlobalEd->UpdateScrollBarsL();
	iGlobalEd->ReportEventL(MCoeControlObserver::EEventStateChanged);

	CleanupStack::PopAndDestroy(2); // wideBuf,narrowBuf

⌨️ 快捷键说明

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