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

📄 console.cpp

📁 在手机操作系统symbina上使用的一个脚本扩展语言的代码实现,可以参考用于自己的开发
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if (iCurrentPos.iX>iScreenRectInChars.Size().iWidth) // should only be possible if last line wrap
		{
		Cr();
		Lf();
		}
	if (iCurrentPos.iY>iScreenRectInChars.Size().iHeight)
		{
		if (iFlags&EConsoleScrollLock)
			{
			iCurrentPos.iY=iScreenRectInChars.Size().iHeight; // set to start of last row
			Cr();
			}
		else
			Scroll();
		}
	}

void COplConsole::WrapIfNeeded()
	{
	if (iCurrentPos.iX>iScreenRectInChars.Size().iWidth)
		{
		if (iFlags&EConsoleWrapLock)
			iCurrentPos.iX=iScreenRectInChars.Size().iWidth; // set to end of row
		else if (!(iFlags&EConsoleLastLineWrap) && iCurrentPos.iY==iScreenRectInChars.Size().iHeight)
			iCurrentPos.iX=iScreenRectInChars.Size().iWidth+1; // just off the end of the line
		else
			{ // set to start of next row
			Cr();
			Lf();
			}
		}
	}

void COplConsole::Beep()
	{
	CEikonEnv::Beep();
	}

void COplConsole::Scroll()
	{
	TInt scrollHeight=iCharSizeInPixels.iHeight;
	TRect area=CharsToPixels(TRect(TPoint(1,1),iScreenRectInChars.Size()));
	area.iTl.iY+=scrollHeight;
	iWindow.Scroll(TPoint(0,-scrollHeight),area);
	area.iTl.iY=area.iBr.iY-scrollHeight;
	DoClearRect(area);
	iCurrentPos.iY-=1; // done here or in ScrollIfNeeded !?!
	}

void COplConsole::DoScrollClear(const TPoint& aOffset,const TRect& aArea)
	{
	if (iFlags&EConsoleInverse)
		iGc->SetBrushColor(KRgbWhite);
	TInt absDX=Abs(aOffset.iX);
	TInt absDY=Abs(aOffset.iY);
	if (absDX>aArea.Size().iWidth||absDY>aArea.Size().iHeight)
		iGc->Clear(aArea); // no overlap
	else
		{
		TSize size(absDX,aArea.Size().iHeight);
		if (aOffset.iX>0)
			iGc->Clear(TRect(aArea.iTl,size));
		else if (aOffset.iX<0)
			iGc->Clear(TRect(TPoint(aArea.iBr.iX+aOffset.iX,aArea.iTl.iY),size));
		size.SetSize(aArea.Size().iWidth,absDY);
		if (aOffset.iY>0)
			iGc->Clear(TRect(aArea.iTl,size));
		else if (aOffset.iY<0)
			iGc->Clear(TRect(TPoint(aArea.iTl.iX,aArea.iBr.iY+aOffset.iY),size));
		}
	if (iFlags&EConsoleInverse)
		iGc->SetBrushColor(KRgbBlack);
	}

void COplConsole::ScrollRect(const TPoint& aOffsetInChars,const TRect& aRectInChars)
	{
	TInt charWidth=iCharSizeInPixels.iWidth;
	TInt charHeight=iCharSizeInPixels.iHeight;
	TPoint offset(aOffsetInChars.iX*charWidth,aOffsetInChars.iY*charHeight);
	TRect rect=CharsToPixels(aRectInChars);
	iWindow.Scroll(offset,rect);
	DoScrollClear(offset,rect);
	}

void COplConsole::Print(const TDesC& aText)
	{
	TInt remaining=aText.Length();
	const TText* text=aText.Ptr();
	TInt lineLength=iScreenRectInChars.Size().iWidth;
	while (remaining>0)
		{
		TInt len=0;
		TPtrC ptr(text,len);
		ScrollIfNeeded();
		while (remaining>0 && len<lineLength-iCurrentPos.iX+1) // chars left on line including current pos
			{
			if (TChar(*text).IsControl())
				{
				if (len!=0)
					{
					ptr.Set(ptr.Ptr(),len);
					DoPrint(ptr);
					len=0;
					}
				switch (*text)
					{
					case 0x7:
						Beep();
						break;
					case 0x8:
						BackSpace();
						break;
					case 0x9:
						Tab();
						break;
					case 0xa:
						Lf();
						break;
					case 0xc:
						Ff();
						break;
					case 0xd:
						Cr();
						break;
					}
				remaining--;
				text++;
				ptr.Set(text,len);
				continue;
				}
			len++;
			remaining--;
			text++;
			}
		if (len!=0)
			{
			ptr.Set(ptr.Ptr(),len);
			DoPrint(ptr);
			}
		}
	DrawCursorIfOn(iCurrentPos);
	}

void COplConsole::DoPrint(const TDesC& aText)
	{
	ScrollIfNeeded();
	DrawText(aText,iCurrentPos);
	iCurrentPos.iX+=aText.Length();
	WrapIfNeeded();
	}

void COplConsole::DrawText(const TDesC& aText,const TPoint& aPosInChars)
	{
	TRect rect(TPoint(aPosInChars.iX,aPosInChars.iY),TSize(aText.Length(),1));
	iGc->DrawText(aText,CharsToPixels(rect),iFont->AscentInPixels());
	}

void COplConsole::SetCursorToFont()
	{
	iCursor.iType=TTextCursor::ETypeRectangle;
	iCursor.iHeight=iCharSizeInPixels.iHeight;
	iCursor.iWidth=iCharSizeInPixels.iWidth;
	iCursor.iAscent=iFont->AscentInPixels();
	iCursor.iFlags=0;
	iCursor.iColor=KRgbWhite;
	}

void COplConsole::CursorOn()
	{
	iFlags|=EConsoleCursorOn;
	DrawCursorIfOn(iCurrentPos);
	}

void COplConsole::DrawCursorIfOn(const TPoint& aPos)
	{
	if (iFlags&EConsoleCursorOn)
		{
		DoDrawCursor(aPos);
		}
	}

void COplConsole::DoDrawCursor(const TPoint& aPos)
	{
	TInt charWidth=iCharSizeInPixels.iWidth;
	TInt charHeight=iCharSizeInPixels.iHeight;
	TPoint topLeft(iTopLeft.iX+(iScreenRectInChars.iTl.iX-1)*charWidth,iTopLeft.iY+(iScreenRectInChars.iTl.iY-1)*charHeight);
	TPoint pos((topLeft.iX+(aPos.iX-1)*charWidth),(topLeft.iY+(aPos.iY-1)*charHeight+iCursor.iAscent));
	iRootWin->SetTextCursor(iWindow,pos,iCursor);
	}

void COplConsole::HideCursor()
	{
	iRootWin->CancelTextCursor();
	iFlags&=~EConsoleCursorOn;
	}

TInt COplConsole::Edit(CCoeEnv& aCoeEnv,TDes& aDes,TBool aTrap)
	{
	ScrollIfNeeded();
	TBuf<KOplMaxStrLen> buf(aDes);
	TInt maxLength=aDes.MaxLength();
	TInt width=iScreenRectInChars.Size().iWidth;

	TInt end=iCurrentPos.iX+maxLength;
	if (end>width+1)
		end=width+1;
	TPoint endPos(end,iCurrentPos.iY+1);
	ClearRect(TRect(iCurrentPos,endPos));

DrawAtEnd:	
	TPoint pos(iCurrentPos);
	TInt posInString=0;
	TInt availableWidth=width-iCurrentPos.iX+1;
	TInt bufLen=buf.Length();
	if (bufLen>=availableWidth)
		posInString=bufLen-availableWidth+1;
	DrawString(pos,posInString,buf);
	TPoint eol(width+1,pos.iY+1);
	ClearRect(TRect(pos,TSize(1,1)));
	FOREVER
		{
		if (pos.iX>width)
			{
			pos.iX=width;
			ScrollRect(TPoint(-1,0),TRect(TPoint(iCurrentPos.iX+1,iCurrentPos.iY),eol));
			}
		DoDrawCursor(pos);
		TUint key=iEventHandler->DoGetUnmapped(aCoeEnv);
		switch (key)
			{
		case EKeyEnter:
			aDes.Copy(buf);
			if (!iFlags&EConsoleCursorOn)
				HideCursor();
			Cr();
			Lf();
			return KErrNone;
		case EKeyEscape:
			if (buf.Length()>0)
				{
				buf.SetLength(0);
				pos.iX=iCurrentPos.iX;
				posInString=0;
				ClearRect(TRect(pos,eol));
				}
			else if (aTrap)
				{
				if (!iFlags&EConsoleCursorOn)
					HideCursor();
				return KErrCancel;
				}
			break;
		case EKeyLeftArrow:
			if (posInString>0)
				{
				posInString--;
				if (pos.iX==iCurrentPos.iX)
					{
					ScrollRect(TPoint(1,0),TRect(pos,TPoint(eol.iX-1,eol.iY)));
					DrawText(buf.Mid(posInString,1),pos);
					}
				else
					pos.iX--;
				}
			break;
		case EKeyRightArrow:
			if (posInString<buf.Length())
				{
				posInString++;
				if (pos.iX==width)
					{
					ScrollRect(TPoint(-1,0),TRect(TPoint(iCurrentPos.iX+1,iCurrentPos.iY),eol));
					if (posInString<buf.Length())
						DrawText(buf.Mid(posInString,1),pos);
					}
				else
					pos.iX++;
				}
			break;
		case EKeyDelete:
			if (posInString==buf.Length())
				break;
			pos.iX++;
			posInString++;
			// fall through to EKeyBackspace
		case EKeyBackspace:
			if (posInString!=0)
				{
				buf.Delete(--posInString,1);
				ScrollRect(TPoint(-1,0),TRect(pos,eol));
				TInt eolCharPos=eol.iX-pos.iX+posInString;
				if (eolCharPos<buf.Length())
					DrawText(buf.Mid(eolCharPos,1),TPoint(eol.iX-1,eol.iY-1));
				pos.iX--;
				}
			break;
		case EKeyEnd:
			goto DrawAtEnd;
		case EKeyHome:
			{
			pos=iCurrentPos;
			posInString=0;
			TPoint tempPos(iCurrentPos);
			TInt temp=0;
			DrawString(tempPos,temp,buf);
			}
			break;
		default:
			{
			TChar c(key);
			if (c.IsPrint())
				{
				if (posInString==maxLength)
					{
					Beep();
					break;
					}
				TBuf<1> b;
				b.Append(c);
				buf.Insert(posInString++,b);
				ScrollRect(TPoint(1,0),TRect(pos,TPoint(eol.iX-1,eol.iY)));
				DrawText(b,pos);
				pos.iX++;
				}
			}
			}
		}
	}

void COplConsole::DrawString(TPoint& aPos,TInt& aPosInString,const TDesC& aBuf)
	{
	TInt bufLen=aBuf.Length();
	while (aPosInString<bufLen)
		{
		TChar c(aBuf[aPosInString]);
		if (!c.IsPrint())
			{
			switch(c)
				{
				case 0x9:
					break; // 9 gives right arrow
				case 0xa:
				case 0xd:
					c=0x7; // 7 is down arrow
					break;
				default:
					c=0xf; // centered dot
				}
			}
		TBuf<1> b;
		b.Append(c);
		DrawText(b,aPos);
		aPosInString++;
		aPos.iX++;
		}
	}

_LIT(KQuestionMark,"?");

void COplConsole::InputL(CCoeEnv& aCoeEnv,TInt16& aInt,TBool aTrap)
	{
	TBuf<KOplLongLen> buf;
	TInt16 temp;
	TReal64 val;
	FOREVER
		{
		TInt ret=Edit(aCoeEnv,buf,aTrap);
		if (ret!=KErrNone)
			User::Leave(KOplErrEsc);
		// check input
		TLex lex(buf);
		if (lex.Val(val)==KErrNone && lex.Remainder().Length()==0 && Math::Int(temp,val)==KErrNone)
			{
			aInt=temp;
			return;
			}
		else if (aTrap)
			User::Leave(KOplErrGenFail);
		Print(KQuestionMark);
		buf.SetLength(0);
		}
	}

void COplConsole::InputL(CCoeEnv& aCoeEnv,TInt32& aLong,TBool aTrap)
	{
	TBuf<KOplLongLen> buf;
	TInt32 temp;
	TReal64 val;
	FOREVER
		{
		TInt ret=Edit(aCoeEnv,buf,aTrap);
		if (ret!=KErrNone)
			User::Leave(KOplErrEsc);
		// check input
		TLex lex(buf);
		if (lex.Val(val)==KErrNone && lex.Remainder().Length()==0 && Math::Int(temp,val)==KErrNone)
			{
			aLong=temp;
			return;
			}
		else if (aTrap)
			User::Leave(KOplErrGenFail);
		Print(KQuestionMark);
		buf.SetLength(0);
		}
	}

void COplConsole::InputL(CCoeEnv& aCoeEnv,TReal64& aInt,TBool aTrap)
	{
	TBuf<KOplFloatLen> buf;
	TReal64 val;
	FOREVER
		{
		TInt ret=Edit(aCoeEnv,buf,aTrap);
		if (ret!=KErrNone)
			User::Leave(KOplErrEsc);
		// check input
		TLex lex(buf);
		if (lex.Val(val)==KErrNone && lex.Remainder().Length()==0)
			{
			aInt=val;
			return;
			}
		else if (aTrap)
			User::Leave(KOplErrGenFail);
		Print(KQuestionMark);
		buf.SetLength(0);
		}
	}

void COplConsole::SetFlag(TInt aFlag,TBool aSet)
	{
	if (aSet)
		iFlags|=aFlag;
	else
		iFlags&=~aFlag;
	}

void COplConsole::SetScrollLock(TBool aLock)
	{
	SetFlag(EConsoleScrollLock,aLock);
	}

void COplConsole::SetWrapLock(TBool aLock)
	{
	SetFlag(EConsoleWrapLock,aLock);
	}

void COplConsole::SetLastLineWrap(TBool aWrap)
	{
	SetFlag(EConsoleLastLineWrap,aWrap);
	}

void COplConsole::UseNewWindow(RBackedUpWindow& aNewWindow)
	{
	iGc->Deactivate();
	iWindow.Close();
	iWindow=aNewWindow;
	iGc->Activate(iWindow);
#if defined(__SERIES60__)
	TBool standardFont=OplUtil::IsStandardFontUid(iFontUid);//
	iGc->UseFont(iFont);
	iIsFontToBeReleased=(standardFont)?EFalse:ETrue;
#else
	iGc->UseFont(iFont);
#endif
	iGc->SetDrawMode(CGraphicsContext::EDrawModePEN);
	iGc->SetPenStyle(CGraphicsContext::ESolidPen);
	iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
	TBool inverse=iFlags&EConsoleInverse;
	TBool underline=iFlags&EConsoleUnderline;
	iFlags&=~(EConsoleInverse|EConsoleUnderline); // remove these as the gc has been reset
	SetInverseAndUnderline(inverse,underline);
	ResetScreenSize();
	iCurrentPos=TPoint(1,1);
	DrawCursorIfOn(iCurrentPos);
	}

⌨️ 快捷键说明

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