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

📄 richtexteditorcontainer.cpp

📁 在syambian s60 3rd上开发的超级计算器 可实现大浮点数的计算
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	iRtEd->RichText()->InsertL(iRtEd->RichText()->
				DocumentLength(), CEditableText::EParagraphDelimiter);
	SetRightTab();
	
	switch( CurOp )
	{
	case EOpAdd:
		SetColor( KRgbDarkCyan );
		iRtEd->RichText()->InsertL(iRtEd->RichText()->DocumentLength(), KAdd);
		break;
	case EOpSub:
		SetColor( KRgbDarkCyan );
		iRtEd->RichText()->InsertL(iRtEd->RichText()->DocumentLength(), KSub);
		break;	
	case EOpMult:
		SetColor( KRgbDarkCyan );
		iRtEd->RichText()->InsertL(iRtEd->RichText()->DocumentLength(), KMult);
		break;
	case EOpDiv:
		SetColor( KRgbDarkCyan );
		iRtEd->RichText()->InsertL(iRtEd->RichText()->DocumentLength(), KDiv);
		break;
	}
	
	
	InsertTab();
	iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
	SetFont(EAknLogicalFontPrimarySmallFont);	  
    //恢复下一段落格式
    SetColor( KRgbBlack );
    iPosStart = iRtEd->RichText()->DocumentLength();
    iIsComplete = EFalse;
    iIsOperand1 = EFalse;
}


void CRTEContainer::AboutL()
	{
	TInt pos = 0;
	CParaFormat* paraFormat=CParaFormat::NewLC();
	TParaFormatMask paraFormatMask;
		
	iIsAbout = ETrue;
	iRtEd->SetCursorPosL(0,EFalse);
	SetHorizontalAlignment(CParaFormat::ECenterAlign);
	//设置段落格式掩码和格式
	
	_LIT( KAbout, "\fCalculator v0.1:\f\f- Author -  Xining\f- E-mail -  xin@nlp.nju.edu.cn\f- Date - May 2008");
	TBuf<200> Demobuf;
	iRtEd->RichText()->DeleteL( 1, iRtEd->RichText()->DocumentLength() - 1 );
	iRtEd->HandleTextChangedL();
	iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
	iRtEd->RichText()->InsertL( iRtEd->Text()->DocumentLength(), KAbout);
	iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
	SetHorizontalAlignment(CParaFormat::ECenterAlign);
	CleanupStack::Pop();	
}
	
void CRTEContainer::SetupL()
{
	iRtEd->EnableCcpuSupportL(ETrue);
	iRtEd->SetCursorPosL(0, EFalse);
	iRtEd->UpdateScrollBarsL();
	SizeChanged();
}

// ---------------------------------------------------------
// CRTEContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CRTEContainer::ConstructL(const TRect& aRect)
    {
	CreateWindowL();
	iRtEd = new (ELeave) CEikRichTextEditor;
	iStatusLine = new (ELeave) CEikLabel;
	iStatusLine->SetAlignment(EHCenterVCenter);

	Prepare();

	iRtEd->ConstructL(this,0,0,0);
	iRtEd->SetFocus(ETrue);
	iRtEd->RichText()->Reset();
    iRtEd->SetAknEditorNumericKeymap( EAknEditorConverterNumberModeKeymap  );
    iRtEd->SetAknEditorCurrentInputMode ( EAknEditorNumericInputMode );
    iRtEd->SetAknEditorAllowedInputModes( EAknEditorNumericInputMode );
	SetRect(aRect);

	iRtEd->SetBackgroundColorL(KRgbYellow); 
	iRtEd->TextView()->SetBackgroundColor(KRgbYellow);

	ActivateL();
	SetupL();
	SetColor(KRgbBlack);
	#ifdef __SERIES60_3X__
	SetFont(EAknLogicalFontPrimarySmallFont);
    #else
	SetFont(CalcOperBold21());
    #endif
	
    SetVerticalAlignment(CParaFormat::EBottomAlign);
    SetHorizontalAlignment(CParaFormat::ELeftAlign);
	SetRightTab();
	iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
	iRtEd->RichText()->InsertL(iRtEd->RichText()->DocumentLength(), KTab);		
	iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
	SetBKColor( KRgbYellow );
	iIsAbout = EFalse;
	iIsComplete = EFalse;
    iIsOperand1 = ETrue;
    iIsMinor1 = iIsMinor2 = EFalse;
    iCanDecimal = ETrue;
    iPosEnd = iPosStart = 1;
}

CRTEContainer::~CRTEContainer()
{
	if(iRtEd)
		delete iRtEd;
	
	if(iStatusLine)
		delete iStatusLine;
}

void CRTEContainer::SizeChanged()
    {
	TRect rect = Rect();
	TInt StatusLineHeight = iStatusLine->Font()->HeightInPixels()+6;
	iStatusLine->SetExtent(TPoint(0, 0),TSize(rect.Width(), StatusLineHeight));
	
	#ifdef __SERIES60_3X__
	TRect ScrollBarRect = iRtEd->ScrollBarFrame()->VerticalScrollBar()->Rect();
	//In 3rd edition CEikRichTextEditor draw the view for the whole rect and
	//the scrollbar doesn't show. That is a reason why CEikRichTextEditor Width() is
	//rect.Width()-ScrollBarRect.Width()
	iRtEd->SetExtent(TPoint(0,StatusLineHeight), 
		TSize(rect.Width()-ScrollBarRect.Width(), rect.Height()-StatusLineHeight));
	#else
    iRtEd->SetExtent(TPoint(0,StatusLineHeight), 
    	TSize(rect.Width(), rect.Height()-StatusLineHeight));
    #endif
    }

// ---------------------------------------------------------
// CRTEContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CRTEContainer::CountComponentControls() const
    {
    return 2; // return number of controls inside this container
    }

// ---------------------------------------------------------
// CRTEContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CRTEContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
			return iRtEd;
		case 1:
			return iStatusLine;
        default:
            return NULL;
        }
    }

// ---------------------------------------------------------
// CRTEContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CRTEContainer::Draw(const TRect& aRect) const
    {
	CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor( KRgbYellow );
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);
    }

// ---------------------------------------------------------
// OfferKeyEventL() 
// Distribute the key event to the Editor
// ---------------------------------------------------------
//


TBool CRTEContainer::IsMinor()
{
    UpdateText();
    if( iPosStart >= iRtEd->RichText()->DocumentLength()) return EFalse;
    else if( iCurrentBuf[ iPosStart ] == 45)
		return ETrue;
	else return EFalse;
}

TKeyResponse CRTEContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
	TBuf<32> buf, buftest;
	if (iRtEd->IsFocused())
	{
/*	CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
TBuf<50> temp;		
if(aType == EEventKey) {temp.Num( aKeyEvent.iCode );
//
	note->ExecuteLD(temp);}*/
		switch( aKeyEvent.iScanCode )
			{
			case 48 :
			case 49 :
			case 50 :
			case 51 :
			case 52 :
			case 53 :				
			case 54 :
			case 55 :
			case 56 :
			case 57 : 
				if( iIsComplete || iIsAbout ) 
				{
					iRtEd->SetCursorPosL( 0, false );
					iRtEd->RichText()->DeleteL( 1, iRtEd->RichText()->DocumentLength() - 1 );
					iRtEd->HandleTextChangedL();
					iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
					iPosStart = 1;
					iIsOperand1 = ETrue;
					iIsComplete = EFalse;
					iIsAbout = EFalse;
					return EKeyWasNotConsumed;
				}
				else break;        
			case 14 :	//屏蔽左方向键
			case 15 :   //屏蔽右方向键
				return EKeyWasNotConsumed;
			case 167:
				//iRtEd->SetCursorPosL( 0, false );
				if( aType == EEventKeyDown )
				{	
				iRtEd->RichText()->InsertL( iRtEd->RichText()->DocumentLength(), KPoint );
				iRtEd->HandleTextChangedL();
				iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
				}
				break;
			case 16 :   //上方向键
			case 17 :   //下方向键   
				if( iIsAbout )
				{
					iRtEd->SetCursorPosL( 0, false );
					iRtEd->RichText()->DeleteL( 1, iRtEd->RichText()->DocumentLength() - 1 );
					iRtEd->HandleTextChangedL();
					iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
					iPosStart = 1;
					iIsOperand1 = ETrue;
					iIsComplete = EFalse;
					iIsAbout = EFalse;
				}
				if( aType == EEventKeyDown )
				{		
					if( !IsMinor())
					{
						if( iIsComplete )
						{
							iRtEd->SetCursorPosL( 0, false );
							iRtEd->RichText()->DeleteL( 1, iRtEd->RichText()->DocumentLength() - 1 );
							iRtEd->HandleTextChangedL();
							iRtEd->RichText()->InsertL( 1, iResult );
							iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false );
							iPosStart = 1;
						}
						iRtEd->RichText()->InsertL(iPosStart, KSub);
						if( !iIsOperand1 ) 
						{
							SetColor( KRgbBlack, iPosStart, 1 );
							#ifdef __SERIES60_3X__
							SetFont( EAknLogicalFontPrimarySmallFont, iPosStart, 1 );
							#else
							SetFont( LatinPlain12(), iPosStart, 1 );
							#endif
						}
						iRtEd->HandleTextChangedL();
						iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
					}
					else
					{

						iRtEd->SetCursorPosL(0, false);
			        	iRtEd->RichText()->DeleteL( iPosStart , 1 );
			        	iRtEd->HandleTextChangedL();						
						iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
					}
				}
    	        return EKeyWasNotConsumed;
		
			case 1:  
					if( iIsAbout ) return EKeyWasNotConsumed;
					UpdateText();
			        if( iRtEd->RichText()->DocumentLength() == 1)
			        	return EKeyWasNotConsumed;
			        else if( aType == EEventKey && iIsComplete )
			        {
			          	iRtEd->SetCursorPosL( 0, false );
			        	iRtEd->RichText()->DeleteL( iPosEnd ,iRtEd->RichText()->DocumentLength() - iPosEnd );
			        	iRtEd->HandleTextChangedL();
			          	iRtEd->SetCursorPosL( iRtEd->RichText()->DocumentLength(), false);
			          	SetFont(EAknLogicalFontPrimarySmallFont);
			          	iIsComplete = EFalse;
			          	iIsOperand1 = EFalse;
			          	return EKeyWasNotConsumed;
			        }
					else if( aType == EEventKey && iCurrentBuf[iRtEd->RichText()->DocumentLength()-1] == 9)
					{
					     iRtEd->OfferKeyEventL(aKeyEvent, aType);
						 iRtEd->OfferKeyEventL(aKeyEvent, aType);
						 iRtEd->OfferKeyEventL(aKeyEvent, aType);
						 SetColor( KRgbBlack );
						 iPosStart = 1;
						 iIsOperand1 = ETrue;
						 iRtEd->SetCursorPosL(iRtEd->RichText()->DocumentLength(), false);
						 SetFont(EAknLogicalFontPrimarySmallFont);	
					 	 return EKeyWasNotConsumed;
					 }
					else if( aType == EEventKey )
						return iRtEd->OfferKeyEventL(aKeyEvent, aType);
					else return EKeyWasNotConsumed;
			default :break;
			}//end switch
		return iRtEd->OfferKeyEventL(aKeyEvent, aType);	
	}//end if
	else
	{
		return EKeyWasNotConsumed;
	}
}

void CRTEContainer::SetHorizontalAlignment(CParaFormat::TAlignment aAlignment)
{
	CParaFormat     paraFormat;
	TParaFormatMask paraFormatMask;

	paraFormatMask.SetAttrib(EAttAlignment);    // set the mask
	paraFormat.iHorizontalAlignment = aAlignment;

	iRtEd->ApplyParaFormatL(&paraFormat, paraFormatMask);
}

void CRTEContainer::SetVerticalAlignment(CParaFormat::TAlignment aAlignment)
{
	CParaFormat     paraFormat;
	TParaFormatMask paraFormatMask;

	paraFormatMask.SetAttrib(EAttAlignment);    // set the mask
	paraFormat.iVerticalAlignment = aAlignment;

	iRtEd->ApplyParaFormatL(&paraFormat, paraFormatMask);
}


void CRTEContainer::SetSelectionL(TInt aCursorPos,TInt aAnchorPos)
{
	iRtEd->SetSelectionL(aCursorPos,aAnchorPos);
}

void CRTEContainer::SetRightTab()
{
	CParaFormat paraFormat;
    TParaFormatMask paraFormatMask;
    
    tabstop.iType = TTabStop::ERightTab;
    tabstop.iTwipsPosition = 1950;
    
    paraFormatMask.SetAttrib(EAttTabStop);  
    paraFormat.StoreTabL( tabstop );
    iRtEd->ApplyParaFormatL(&paraFormat,paraFormatMask);
}

void CRTEContainer::SetCharacterBIUAttribL(CEikGlobalTextEditor::TFontStyleFlags aFontFlags)
{
	iRtEd->BoldItalicUnderlineEventL(aFontFlags);
	//Echo(R_TYPE_DIALOG_TBUF_INDICATOR_CHARACTER_FORMAT);
}
	
#ifdef __SERIES60_3X__
// ----------------------------------------------------
// SetFont(TInt aFontId)
// Change the text's font in the editor 
// to the one specified as input parameter for the function 
// ----------------------------------------------------
void CRTEContainer::SetFont(TInt aFontId)
{	
	const CFont* logicalFont = AknLayoutUtils::FontFromId(aFontId);
	TFontSpec fontspec = logicalFont->FontSpecInTwips();
    TCharFormat charFormat(fontspec.iTypeface.iName, fontspec.iHeight);
    TCharFormatMask charFormatMask;
    charFormatMask.SetAttrib(EAttFontTypeface);
    charFormatMask.SetAttrib(EAttFontHeight);
	iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
	Echo(R_TYPE_DIALOG_TBUF_INDICATOR_FONT);
}
#else

void CRTEContainer::SetFont(const CFont *aFont)
{	

    TFontSpec fontspec = aFont->FontSpecInTwips();
    TCharFormat charFormat( fontspec.iTypeface.iName, fontspec.iHeight );
    TCharFormatMask charFormatMask;
    charFormatMask.SetAttrib(EAttFontTypeface);
    charFormatMask.SetAttrib(EAttFontHeight);
	iRtEd->ApplyCharFormatL(charFormat, charFormatMask);
}
#endif


#ifdef __SERIES60_3X__
void CRTEContainer::SetFont(TInt aFontId, TInt aPos, TInt aLength)
{	
	// Get a logical font to base my font on:
	const CFont* logicalFont = AknLayoutUtils::FontFromId(aFontId);
	// Extract font information
	TFontSpec fontspec = logicalFont->FontSpecInTwips();
    TCharFormat charFormat(fontspec.iTypeface.iName, fontspec.iHeight);
    TCharFormatMask charFormatMask;
    charFormatMask.SetAttrib(EAttFontTypeface);
    charFormatMask.SetAttrib(EAttFontHeight);
    iRtEd->RichText()->ApplyCharFormatL(charFormat, charFormatMask, aPos, aLength);
	Echo(R_TYPE_DIALOG_TBUF_INDICATOR_FONT);
}
#else

void CRTEContainer::SetFont(const CFont *aFont, TInt aPos, TInt aLength)
{	
    TFontSpec fontspec = aFont->FontSpecInTwips();
    TCharFormat charFormat( fontspec.iTypeface.iName, fontspec.iHeight );
    TCharFormatMask charFormatMask;
    charFormatMask.SetAttrib(EAttFontTypeface);
    charFormatMask.SetAttrib(EAttFontHeight);
	iRtEd->RichText()->ApplyCharFormatL(charFormat, charFormatMask, aPos, aLength);
}
#endif

	

void CRTEContainer::SetColor(TRgb aColor)
{
	TCharFormat charFormat;
	TCharFormatMask charFormatMask;

	charFormat.iFontPresentation.iTextColor = aColor;
	charFormatMask.SetAttrib(EAttColor);
	iRtEd->ApplyCharFormatL(charFormat, charFormatMask );
}

void CRTEContainer::SetColor(TRgb aColor, TInt aPos, TInt aLength )
{
	TCharFormat charFormat;
	TCharFormatMask charFormatMask;

	charFormat.iFontPresentation.iTextColor = aColor;
	charFormatMask.SetAttrib(EAttColor);
	iRtEd->RichText()->ApplyCharFormatL(charFormat, charFormatMask, aPos, aLength );
}

void CRTEContainer::Echo(TInt aMessage)
{
	TBuf<32> buf;
	CEikonEnv::Static()->ReadResource(buf, aMessage);
	iStatusLine->SetTextL(buf);
}

⌨️ 快捷键说明

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