applayerprotocols/telnetengine/TUIEDIT/TUIEDIT.CPP
changeset 0 b16258d2340f
equal deleted inserted replaced
-1:000000000000 0:b16258d2340f
       
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TUIEDIT.H"
       
    17 
       
    18 CLineEdit::CLineEdit()
       
    19 //
       
    20 // Constructor
       
    21 //
       
    22 	{
       
    23 
       
    24 //	iHistory=NULL;
       
    25 //	iConsole=NULL;
       
    26 //	iMaxHistory=0;
       
    27 //	iWidth=0;
       
    28 //	iHeight=0;
       
    29 //	iPos=0;
       
    30 //	iOrigin=0;
       
    31 //	iLine=0;
       
    32 //	iRecall=0;
       
    33 //	iBuf=NULL;
       
    34 //	iPrompt=_L("");
       
    35 	__DECLARE_NAME(_S("CLineEdit"));
       
    36 	}
       
    37 
       
    38 
       
    39 CLineEdit::~CLineEdit()
       
    40 //
       
    41 // Destroy the line editor
       
    42 //
       
    43 	{
       
    44 
       
    45 	TInt count=iHistory->Count();
       
    46 	while (count--)
       
    47 		User::Free((*iHistory)[count]);
       
    48 	delete iHistory;
       
    49 	}
       
    50 
       
    51 EXPORT_C CLineEdit* CLineEdit::NewL(CConsoleBase* aConsole,TInt aMaxHistory)
       
    52 //
       
    53 // Create a new line editor
       
    54 //
       
    55 	{
       
    56 
       
    57 	CLineEdit* pE=new(ELeave) CLineEdit;
       
    58 	pE->iHistory=new(ELeave) CArrayFixFlat<HBufC*>(aMaxHistory+2);
       
    59 	pE->iConsole=aConsole;
       
    60 	pE->iMaxHistory=aMaxHistory;
       
    61 	pE->iWidth=pE->iConsole->ScreenSize().iWidth;
       
    62 	pE->iHeight=pE->iConsole->ScreenSize().iHeight;
       
    63 	return(pE);
       
    64 	}
       
    65 
       
    66 TInt CLineEdit::Lines()
       
    67 //
       
    68 // The number of lines being edited.
       
    69 //
       
    70     {
       
    71 
       
    72     TInt nL=1;
       
    73     if (Buf().Length()>=iWidth-2-iOrigin)
       
    74 		nL+=(Buf().Length()+iOrigin)/(iWidth-2);
       
    75     return(nL);
       
    76     }
       
    77 
       
    78 TPoint CLineEdit::Where()
       
    79 //
       
    80 // Return the real cursor position.
       
    81 //
       
    82     {
       
    83 
       
    84     if (iPos>=(iWidth-2-iOrigin))
       
    85 		return(TPoint((iPos+iOrigin)%(iWidth-2),((iPos+iOrigin)/(iWidth-2))+iLine));
       
    86 	return(TPoint(iPos+iOrigin,iLine));
       
    87     }
       
    88 
       
    89 void CLineEdit::ClearLine()
       
    90 //
       
    91 // Clears the line being edited.
       
    92 //
       
    93     {
       
    94 
       
    95     if (Buf().Length())
       
    96 		{
       
    97 		TInt nL=Lines();
       
    98 		while (nL--)
       
    99 	    	{
       
   100 	    	iConsole->SetPos(nL ? 0 : iOrigin,iLine+nL);
       
   101 	    	iConsole->ClearToEndOfLine();
       
   102 	    	}
       
   103 		Buf().Zero();
       
   104 		iPos=0;
       
   105 		}
       
   106     }
       
   107 
       
   108 void CLineEdit::ClearLast(TInt aCnt)
       
   109 //
       
   110 // Clears the last aCnt characters.
       
   111 //
       
   112     {
       
   113 
       
   114     TInt aPos=iPos;
       
   115     iPos=((TInt)Buf().Length())-aCnt;
       
   116     while (iPos<((TInt)Buf().Length()))
       
   117 		{
       
   118 		TPoint p=Where();
       
   119 		iConsole->SetCursorPosAbs(p);
       
   120 		iConsole->ClearToEndOfLine();
       
   121 		iPos+=(iWidth-p.iX);
       
   122 		}
       
   123     iPos=aPos;
       
   124     }
       
   125 
       
   126 void CLineEdit::Recall()
       
   127 //
       
   128 // Recall a line for editing.
       
   129 //
       
   130     {
       
   131 
       
   132 	if (iRecall!=(-1))
       
   133 		{
       
   134 		ClearLine();
       
   135 		HBufC* pL=(*iHistory)[iRecall];
       
   136 		Buf()=(*pL);
       
   137 		iConsole->Write(Buf());
       
   138 		iPos=Buf().Length();
       
   139 		TInt nL=Lines();
       
   140 		if ((iLine+nL)>(iHeight-2))
       
   141 	    	iLine=iHeight-2-nL;
       
   142 		Buf()=(*pL);
       
   143 		}
       
   144     }
       
   145 
       
   146 TInt CLineEdit::WordLeft()
       
   147 //
       
   148 // Position the cursor to the next word left.
       
   149 //
       
   150     {
       
   151 
       
   152     TInt x=iPos-1;
       
   153     while (x && TChar(Buf()[x]).IsSpace())
       
   154 		x--;
       
   155     while (x && TChar(Buf()[x]).IsGraph())
       
   156 		x--;
       
   157     if (TChar(Buf()[x]).IsSpace())
       
   158 		x++;
       
   159     return(x);
       
   160     }
       
   161 
       
   162 TInt CLineEdit::WordRight()
       
   163 //
       
   164 // Position the cursor to the next word right.
       
   165 //
       
   166     {
       
   167 
       
   168     TInt x=iPos;
       
   169     while (x<(TInt)Buf().Length() && TChar(Buf()[x]).IsGraph())
       
   170 		x++;
       
   171     while (x<(TInt)Buf().Length() && TChar(Buf()[x]).IsSpace())
       
   172 		x++;
       
   173     return(x);
       
   174     }
       
   175 
       
   176 void CLineEdit::Cursor()
       
   177 //
       
   178 // Position the cursor.
       
   179 //
       
   180     {
       
   181 
       
   182     iConsole->SetCursorPosAbs(Where());
       
   183     }
       
   184 
       
   185 void CLineEdit::Refresh()
       
   186 //
       
   187 // Refresh the line.
       
   188 //
       
   189     {
       
   190 
       
   191 	iConsole->SetCursorHeight(ECursorNone);
       
   192     iConsole->SetPos(iOrigin,iLine);
       
   193     iConsole->Write(Buf());
       
   194 	Cursor();
       
   195 	iConsole->SetCursorHeight(iMode==EEditOverWrite ? ECursorNormal : ECursorInsert);
       
   196     }
       
   197 
       
   198 EXPORT_C void CLineEdit::Edit(const TDesC& aPrompt,TDes* aBuf)
       
   199 //
       
   200 // Start the editor or a single key fetch.
       
   201 //
       
   202     {
       
   203 
       
   204 	iBuf=aBuf;
       
   205     iPos=0;
       
   206 	Buf().Zero();
       
   207 	iMode=EEditOverWrite;
       
   208 	iConsole->SetCursorHeight(iMode==EEditOverWrite ? ECursorNormal : ECursorInsert);
       
   209     iConsole->Write(aPrompt);
       
   210 	iOrigin=iConsole->WhereX();
       
   211 	iLine=iConsole->WhereY();
       
   212 	iRecall=(-1);
       
   213     TInt hCount=iHistory->Count();
       
   214     if (hCount>iMaxHistory)
       
   215 		hCount=iMaxHistory;
       
   216 	FOREVER
       
   217 		{
       
   218 		TChar gChar=iConsole->Getch();
       
   219 		switch (gChar)
       
   220 	    	{
       
   221 		case EKeyEscape:
       
   222 	    	ClearLine();
       
   223 			iRecall=(-1);
       
   224 	    	break;
       
   225 		case EKeyHome:
       
   226 	    	iPos=0;
       
   227 	    	Cursor();
       
   228 	    	break;
       
   229 		case EKeyLeftArrow:
       
   230 	    	if (iPos)
       
   231                 {
       
   232                 if(iConsole->KeyModifiers()==EModifierCtrl)
       
   233                     iPos=WordLeft();
       
   234                 else
       
   235     				iPos--;
       
   236                 Cursor();
       
   237                 }
       
   238 	    	break;
       
   239 		case EKeyRightArrow:
       
   240 	    	if (iPos<((TInt)Buf().Length()))
       
   241                 {
       
   242                 if(iConsole->KeyModifiers()==EModifierCtrl)
       
   243                     iPos=WordRight();
       
   244                 else
       
   245     				iPos++;
       
   246                 Cursor();
       
   247                 }
       
   248 	    	break;
       
   249 		case EKeyEnd:
       
   250 	    	iPos=((TInt)Buf().Length());
       
   251 	    	Cursor();
       
   252 	    	break;
       
   253 		case EKeyPageUp:
       
   254 	    	if (hCount==0)
       
   255 				break;
       
   256 	    	iRecall=hCount-1;
       
   257 	    	Recall();
       
   258 	    	break;
       
   259 		case EKeyUpArrow:
       
   260 	    	if (iRecall==(-1))
       
   261 				{
       
   262 				if (hCount==0)
       
   263 		    		break;
       
   264 				iRecall=0;
       
   265 				}
       
   266 	    	else if (iRecall>=(hCount-1))
       
   267 				{
       
   268 				ClearLine();
       
   269 				iRecall=(-1);
       
   270 				break;
       
   271 				}
       
   272 	    	else
       
   273 				iRecall++;
       
   274 	    	Recall();
       
   275 	    	break;
       
   276 		case EKeyDownArrow:
       
   277 	    	if (iRecall==(-1))
       
   278 				{
       
   279 				if (hCount==0)
       
   280 		    		break;
       
   281 				iRecall=hCount-1;
       
   282 				}
       
   283 	    	else if (iRecall==0)
       
   284 				{
       
   285 				ClearLine();
       
   286 				iRecall=(-1);
       
   287 				break;
       
   288 				}
       
   289 	    	else
       
   290 				iRecall--;
       
   291 	    	Recall();
       
   292 	    	break;
       
   293 		case EKeyPageDown:
       
   294 	    	if (hCount==0)
       
   295 				break;
       
   296 	    	iRecall=0;
       
   297 	    	Recall();
       
   298 	    	break;
       
   299 		case EKeyEnter:
       
   300 	    	iConsole->SetCursorHeight(ECursorNone);
       
   301 	    	iConsole->SetPos(0,iLine+Lines()-1); // CR on the last line
       
   302 	    	iConsole->Write(_L("\n")); // Just a line feed
       
   303 //			iConsole->SetCursorHeight(iDefaultMode==EEditOverWrite ? ECursorNormal : ECursorInsert);
       
   304 	    	iRecall=(-1);
       
   305 	    	if (Buf().Length()>2)
       
   306 				{
       
   307 				if (iHistory->Count()==iMaxHistory+1)
       
   308 					{
       
   309 					User::Free((*iHistory)[iMaxHistory]);
       
   310 		    		iHistory->Delete(iMaxHistory);
       
   311 					}
       
   312 				HBufC* pB=Buf().Alloc();
       
   313 				if (pB!=NULL)
       
   314 					{TRAPD(r,iHistory->InsertL(0,pB));}
       
   315 				}
       
   316 	    	return;
       
   317 		case EKeyBackspace:
       
   318 	    	if (iPos)
       
   319 				{
       
   320 				TInt iN=1;
       
   321 				if (iConsole->KeyModifiers()==EModifierCtrl)
       
   322 		    		iN=iPos-WordLeft();
       
   323 				ClearLast(iN);
       
   324 				iPos-=iN;
       
   325 				Buf().Delete(iPos,iN);
       
   326 				Refresh();
       
   327 				}
       
   328 	    	break;
       
   329 		case EKeyDelete:
       
   330 	    	if (iPos<((TInt)Buf().Length()))
       
   331 				{
       
   332 				TInt iN=1;
       
   333 				if (iConsole->KeyModifiers()==EModifierCtrl)
       
   334 		    		iN=WordRight()-iPos;
       
   335 				ClearLast(iN);
       
   336 				Buf().Delete(iPos,iN);
       
   337 				Refresh();
       
   338 				}
       
   339 	    	break;
       
   340 		case EKeyInsert:
       
   341 	    	iMode=(iMode==EEditOverWrite ? EEditInsert : EEditOverWrite);
       
   342 			iConsole->SetCursorHeight(iMode==EEditOverWrite ? ECursorNormal : ECursorInsert);
       
   343 	    	break;
       
   344 		default:
       
   345    	    	if (!gChar.IsPrint())
       
   346 				break;
       
   347 	    	if (iMode==EEditOverWrite && iPos<((TInt)Buf().Length()))
       
   348 				Buf()[iPos++]=(TText)gChar;
       
   349 	    	else if (Buf().Length()<0x100)
       
   350 				{
       
   351 				TInt oL=Lines();
       
   352 				TBuf<0x02> b;
       
   353 				b.Append(gChar);
       
   354 				Buf().Insert(iPos++,b);
       
   355 				TInt nL=Lines();
       
   356 				if (nL!=oL)
       
   357 		    		{
       
   358 		    		iConsole->SetCursorHeight(ECursorNone);
       
   359 		    		iConsole->SetPos(0,iLine+oL-1);
       
   360 		    		iConsole->Write(_L("\n"));
       
   361 		    		iConsole->SetPos(0,iLine);
       
   362 		    		if (iLine+nL>iHeight-2)
       
   363 						iLine=iHeight-2-nL;
       
   364 		    		}
       
   365 				}
       
   366 			else
       
   367 				{
       
   368 				iConsole->Write(_L("\7"));
       
   369 				iConsole->SetPos((iOrigin+iPos)%(iWidth-2),iLine+Lines()-1);
       
   370 				break;
       
   371 				}
       
   372 			Refresh();
       
   373 			}
       
   374 		}
       
   375 	}
       
   376 
       
   377 
       
   378 EXPORT_C void ClientStubOrdinal1() 
       
   379 	{
       
   380 	_LIT(KStubPanic, "tuiedit.dll Stub");
       
   381 	User::Panic(KStubPanic, KErrNotSupported);
       
   382 	}