texted.cpp

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

CPP
1,978
字号
	}

void CTxtedAppUi::ReplaceWithLineDelimiter(TDes& aBuffer, const TDesC& aTextToReplace, const TDesC& aReplaceWith) const
	{
	TInt location=KErrNotFound;
	while ( (location = aBuffer.Find(aTextToReplace)) != KErrNotFound ) 
		aBuffer.Replace(location, aTextToReplace.Length(), aReplaceWith);
	}

void CTxtedAppUi::ExportTextL()
	{
	TFileName fileName=CurrentFilePath();
	fileName.Append(KTextExtension);
#if defined(__UIQ__)
	CEikFileSaveAsDialog* saveAsDlg = new (ELeave) CEikFileSaveAsDialog(&fileName);
	if(saveAsDlg->ExecuteLD(R_EIK_DIALOG_FILE_SAVEAS))
#else
	if (CCknSaveAsFileDialog::RunDlgLD(fileName,R_TXED_EXPORT_DIALOG_TITLE,CCknFileListDialogBase::EShowAllDrives,KNullUid))
#endif
		{
		if (iExportFormatOption==EExportFormatUnixASCII || iExportFormatOption==EExportFormatDosASCII)
			{
			iEikonEnv->BusyMsgL(R_TXED_EXPORTING,500000); // after 0.5 seconds
			TRAPD(err,DoAsciiExportL(fileName));
			iEikonEnv->BusyMsgCancel();
			User::LeaveIfError(err);
			}
		else
			iGlobalEd->Text()->ExportAsTextL(fileName,CPlainText::EOrganiseByParagraph,KExportLineWrap);
		iEikonEnv->InfoMsg(R_EIK_TBUF_SAVED);
		}
	}

void CTxtedAppUi::DoAsciiExportL(const TFileName& aFileName)
	{
	HBufC* wideBuf=iGlobalEd->GetTextInHBufL();
	CleanupStack::PushL(wideBuf);
	// If we need to export as MS-DOS ASCII, parse the text for CEditableText::EParagraphDelimiter
	// and total the occurrences so we can increase the buffer size, allowing us to insert
	// the 2-character ASCII line delimeter. Once we have the new buffer size, re-allocate
	// it and then re-parse, replacing any Unicode line delimeters
	// (CEditableText::EParagraphDelimiter) with KCarriageReturnLineFeed.
	if (iExportFormatOption==EExportFormatDosASCII)
		{
		TPtr widePtr(wideBuf->Des());
		const TInt oldSize=widePtr.Length();
		TInt bufferSize=oldSize;
		TBuf<1> unicodeLineDelimeter;
		unicodeLineDelimeter.Append(CEditableText::EParagraphDelimiter);
		TInt location=0;
		TPtrC substring(widePtr);
		while ( (location = substring.Find(unicodeLineDelimeter)) != KErrNotFound ) 
			{
			substring.Set(widePtr.Mid(location+1,substring.Length()-location-1));
			bufferSize++;
			}
		// Only re-allocate if the new buffer size is actually different
		if (bufferSize!=oldSize)
			{
			wideBuf=wideBuf->ReAllocL(bufferSize);
			CleanupStack::Pop(); // wideBuf - in case we realloacted to a different start cell
			CleanupStack::PushL(wideBuf);
			}

		widePtr.Set(wideBuf->Des());
		ReplaceWithLineDelimiter(widePtr, unicodeLineDelimeter, KCarriageReturnLineFeed);
		}
	//
	// Now do the Unicode to Narrow conversion and output to file
	//
	ConeUtils::EnsurePathExistsL(aFileName);
	HBufC8* narrowBuf=COplRuntimeCharConv::ConvertFromUnicodeToNarrowLC(*wideBuf);
	RFile file;
	User::LeaveIfError(file.Replace(iEikonEnv->FsSession(), aFileName, EFileStreamText | EFileWrite));
	CleanupClosePushL(file);
	User::LeaveIfError(file.Write(*narrowBuf));
	User::LeaveIfError(file.Flush());
	CleanupStack::PopAndDestroy(3); // file (calls Close() for us), narrowBuf,wideBuf
	}

void CTxtedAppUi::DeleteL()
	{
	TBuf<KTextEdResourceLen> title;
	TBuf<KTextEdResourceLen> buttonText;
	iCoeEnv->ReadResource(title,R_TXED_DELETE_CURRENT_FILE);
	iCoeEnv->ReadResource(buttonText,R_TXED_BTEXT_DELETE_FILE);
#if defined(__UIQ__)
	if(!iEikonEnv->QueryWinL(title,KNullDesC))
#else
	if (!CCknConfirmationDialog::RunDlgLD(title,KNullDesC,NULL,&buttonText))
#endif
		return;
	delete iEikonEnv->Process()->MainStore();
	iEikonEnv->Process()->SetMainStore(NULL);
	User::LeaveIfError(CEikonEnv::Static()->FsSession().Delete(iEikonEnv->Process()->MainDocFileName()));
	Exit();
	}

void CTxtedAppUi::AddToHomeL()
	{
#if defined(USE_ADDTODESK)
	CLinkDocument* link=CLinkDocument::NewL();
	CleanupStack::PushL(link);
	link->SetDocumentL(iEikonEnv->Process()->MainDocFileName());
	LinkUtils::CreateLinkDocumentL(*link);
	CleanupStack::PopAndDestroy(); // link
#endif
	}

void CTxtedAppUi::DoLogL()
	{
	_LIT(KLogViewerAppFileName, "\\System\\Apps\\Logview\\Logview.app");
	TFileName dll;
	Dll::FileName(dll);
	TParse parse;
	parse.Set(KLogViewerAppFileName,&dll,NULL);
	CApaCommandLine* cmdLine=CApaCommandLine::NewLC();
	cmdLine->SetCommandL(EApaCommandRun);
	cmdLine->SetLibraryNameL(parse.FullName());
	EikDll::StartAppL(*cmdLine);
	CleanupStack::PopAndDestroy(cmdLine);
	}

void CTxtedAppUi::SetTitleBarFileNameL(const TDesC& aFileName)
	{
	TFileName fileName(aFileName);
	if (fileName.Length()==0)
		fileName=iEikonEnv->EikAppUi()->Application()->AppCaption();
	User::LeaveIfError(EikFileUtils::Parse(fileName));
	TParsePtrC parse(aFileName);
	if (parse.NamePresent())
		fileName=parse.NameAndExt();
	else
		fileName=TPtrC();
#if !defined(__UIQ__)
	iTitleBar->SetTextL(fileName, CCknAppTitle::EMainTitle);
	iTitleBar->DrawNow();
#endif
	}

//
/////////////////////OPL stuff//////////////////////////////
//
TInt CTxtedAppUi::OpenSource(TDes& aFileName,MTextSource*& aTextSource)
	{
	if (aFileName==iDocument->Process()->MainDocFileName()) // currently open file
		{
		aTextSource=new CTextSource(STATIC_CAST(CTextEdDocument*,iDocument),NULL);
		return (aTextSource)?KErrNone:KErrNoMemory;
		}
	TRAPD(err,DoOpenSourceL(aFileName,aTextSource));
	return err;
	}

void CTxtedAppUi::DoOpenSourceL(TDes& aFileName,MTextSource*& aTextSource)
	{
	RFs& fsSession=iEikonEnv->FsSession();
	TParse parse;
	TFileName mainDoc=iDocument->Process()->MainDocFileName();
	User::LeaveIfError(parse.Set(aFileName,&mainDoc,NULL));
	if (!parse.NameOrExtPresent())
		User::Leave(KErrBadName);
	TEntry entry;
	if (fsSession.Entry(parse.FullName(),entry))
		{
		parse.Set(aFileName,NULL,NULL);
		TFindFile find(fsSession);
		if (!parse.DrivePresent() && !parse.PathPresent() && find.FindByDir(aFileName,KDefaultOplIncludePath)==KErrNone)
			aFileName=find.File();
		else
			User::Leave(KErrNotFound);
		}
	else
		aFileName=parse.FullName();

	CApaProcess* process=iDocument->Process();
	CFileStore* store;
	CStreamDictionary* dict;
	CApaDocument* doc=process->OpenNewDocumentL(store,dict,aFileName,EFileShareReadersOnly|EFileRead);
	delete dict;
	delete store; // close the store
	TAny* cell=User::Alloc(sizeof(CTextSource));
	if (cell)
		aTextSource=new(cell) CTextSource(STATIC_CAST(CTextEdDocument*,doc),process);
	else
		{
		process->DestroyDocument(doc);
		User::LeaveNoMemory();
		}
	}

void CTxtedAppUi::LoadTranslatorL()
	{
	User::LeaveIfError(iTranslatorDll.Load(KDefaultTranslatorName));
	if ((iTranslatorDll.Type())[1]!=KUidOplTranslatorProvider)
		User::Leave(KErrNotSupported);
	iNewTranFuncL=(TNewOplTranslatorL)iTranslatorDll.Lookup(1);
	if (iNewTranFuncL==NULL)
		User::Leave(KErrBadLibraryEntryPoint);
	}

COplTranslatorBase* CTxtedAppUi::NewTranslatorLC()
	{
	COplTranslatorBase* translator=(*iNewTranFuncL)();
	CleanupStack::PushL(translator);
	translator->SetTarget(EOplTargetER1);
	translator->SetAppCB(this);
	return translator;
	}

void CTxtedAppUi::DoTranslateL(CTextTranslator::TDebugFlag aDebugFlag)
	{
	RFs& fsSession=iEikonEnv->FsSession();
	TPtrC fileName=iDocument->Process()->MainDocFileName();
	fsSession.SetSessionPath(fileName);
	TFileName output(fileName);
	TInt len=output.Length();
	if (len>4 && output.Right(4).CompareF(KOplExtension)==KErrNone)
		output.SetLength(len-4); // remove .OPL extension if present
	else if (len+4>output.MaxLength())
		User::Leave(KErrBadName);
	output.Append(KOpoExtension);
	TSourceTranslateError anErr;
	COplTranslatorBase* translator=NewTranslatorLC();
	translator->StartTranslateL(*this,fileName,output,aDebugFlag,anErr);
	// Save default output name
	// This name is changed elsewhere if the program is an app
	delete iTranFileName;
	iTranFileName=NULL;
	iTranFileName=output.AllocL();

	TInt r=KErrCancel; // Need to initialise r to avoid compiler warning

	CEikDialog* dialog=new(ELeave) CTextEdTranslateDialog(translator,r);
	dialog->ExecuteLD(R_TXED_DIALOG_TRANSLATE);
	CleanupStack::PopAndDestroy(); // the translator

	if (r!=KErrCancel)
		{
		if (r!=KErrGeneral || anErr.Error()!=EErrSuccess)
			{
			GetTranslateErrorText(iLastErrorBuf,anErr.Error());
			if (anErr.Source().CompareF(fileName)==0 || OpenIncludeFileWithErrorL(anErr.Source()))
				{
				TInt pos=anErr.Position();
				if (pos<=iGlobalEd->TextLength())
					{
					TRAPD(ignore,iGlobalEd->SetCursorPosL(pos,EFalse));
					}
				}
			iEikonEnv->InfoMsg(iLastErrorBuf);
			}
		else
			{
#if defined(__UIQ__)
			if(iEikonEnv->QueryWinL(R_TXED_TRANSLATE_COMPLETE,R_TXED_TRANS_RUN_QUESTION))
#else
			if (CCknConfirmationDialog::RunDlgLD(R_TXED_TRANSLATE_COMPLETE,
												 R_TXED_TRANS_RUN_QUESTION,
												 NULL,
												 R_TXED_BTEXT_RUN_TRANSLATED))
#endif
				DoRunL(*iTranFileName);
			}
		}
	}

TBool CTxtedAppUi::OpenIncludeFileWithErrorL(const TDesC& aFilename)
	{
	TBuf<KTextEdResourceLen> errInInclude;
	TBuf<KTextEdResourceLen> resErrorText;
	TBuf<KTextEdResourceLen> resOpenText;
	iCoeEnv->ReadResource(resErrorText,R_TXED_ERROR_IN_INCLUDE_FILE);
	iCoeEnv->ReadResource(resOpenText,R_TXED_OPEN_THE_FILE);
	_LIT(KInclude,"INCLUDE"); // Constant text, even for non-English localizations.
	errInInclude.Format(resErrorText,&KInclude);
	TBuf<KTextEdResourceLen> buttonText;
	iCoeEnv->ReadResource(buttonText, R_TXED_BTEXT_INCLUDE_OPEN_FILE);
#if defined(__UIQ__)
	if(iEikonEnv->QueryWinL(errInInclude,resOpenText))
#else
	if (CCknConfirmationDialog::RunDlgLD(errInInclude,resOpenText,NULL,&buttonText))
#endif
		{
		OpenFileL(aFilename);
		return ETrue;
		}
	return EFalse;
	}

void CTxtedAppUi::DoLocateRuntimeErrorL(CTextTranslator::TDebugFlag aDebugFlag,
										TSourceTranslateError& anErr,
										TInt aProcLine)
	{
	TInt qCodeOffset=anErr.Position();
	RFs& fsSession=iEikonEnv->FsSession();
	TPtrC fileName=iDocument->Process()->MainDocFileName();
	fsSession.SetSessionPath(fileName);
	TFileName output;
	RFile tempFile;
#if defined(__S80_DP2_0__)
	User::LeaveIfError(tempFile.Temp(fsSession,iDocument->Process()->TempFilePath(),output,EFileWrite));
#else
	User::LeaveIfError(tempFile.Temp(fsSession,Apfile::TempPath(),output,EFileWrite));
#endif
	tempFile.Close();
	COplTranslatorBase* translator=NewTranslatorLC();
	translator->LocateErrorL(*this,fileName,output,aDebugFlag,anErr,aProcLine);
	TInt ret=KErrCancel;
	CEikDialog* dialog=new(ELeave) CTextEdTranslateDialog(translator,ret,R_TXED_LOCATE_ERROR_DIALOG_TITLE);
	dialog->ExecuteLD(R_TXED_DIALOG_TRANSLATE);
	CleanupStack::PopAndDestroy(); // the translator

	if (ret!=KErrCancel)
		{
		if (ret==KErrGeneral && anErr.Error()==EErrFound)
			{
			TInt pos=anErr.Position();
			if (pos<=iGlobalEd->TextLength())
				{
				if (qCodeOffset<0)
					{
					TUint scanMask=CPlainText::EScanBackwards|CPlainText::EScanStayIfBoundary|CPlainText::EScanToUnitStart;
					iGlobalEd->GlobalText()->ScanParas(pos,scanMask); // guaranteed to find para start
					}
				TRAPD(ignore,iGlobalEd->SetCursorPosL(pos,EFalse));
				}
			}
		// else
		// error not found - Info message?
		}
	fsSession.Delete(output); // ignore any error
	}

void CTxtedAppUi::OplTargetIsAppL(TFileName& aName)
	{
	TParse parse;
	User::LeaveIfError(parse.SetNoWild(KDefaultAppPath,&aName,NULL));
	User::LeaveIfError(parse.AddDir(parse.Name()));
	aName=parse.FullName();
	// save the output name
	delete iTranFileName;
	iTranFileName=NULL;
	iTranFileName=aName.AllocL();
	}

void CTxtedAppUi::HandleRuntimeErrorL()
	{
	iEikonEnv->SetBusy(EFalse);
	if (iDocument->AppFileMode()&EFileWrite)
		iGlobalEd->SetReadOnly(EFalse);
	TRuntimeResBuf res=iRuntimeResultPckg();
	if (res.iError)
		{
		iLastErrorBuf=res.iErrMsg;
		TSourceTranslateError tranErr;
		tranErr.SetPosition(res.iOffset);
		if (res.iSrcFile.CompareF(iDocument->Process()->MainDocFileName())==0)
			{
			TRAPD(err,DoLocateRuntimeErrorL(res.iDebugFlag,tranErr,res.iProcLineNumber));	// ignore error
			iEikonEnv->InfoMsg(iLastErrorBuf);
			}
		}
	}

void CTxtedAppUi::DoRunFileL()
	{
	TFileName fileName;
	if (iTranFileName)
		fileName=*iTranFileName;
	else
		{
		fileName=iDocument->Process()->MainDocFileName();
		TInt len=fileName.Length();
		if (len>4 && fileName.Right(4).CompareF(KOplExtension)==KErrNone)
			fileName.SetLength(len-4); // remove .OPL extension if present
		if (fileName.Length()+4<=fileName.MaxLength())
			fileName.Append(KOpoExtension);
		}
#if defined(__UIQ__)
#else
	if (CCknOpenFileDialog::RunSourceDlgLD(fileName,R_TXED_RUN_PROGRAM,R_TXED_RUN_SHOW_PROGRAMS,CCknFileListDialogBase::EShowAllDrives,KUidOplInterpreter,&KAppExtension))
#endif
		{
		DoRunL(fileName);
		}
	}

#define KDelim 32

void CTxtedAppUi::DoRunL(const TDesC& aFileName)
	{
	const TChar delim(KDelim);
	const TC

⌨️ 快捷键说明

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