extras/calcsoft/src/CalcEditline.cpp
changeset 13 83b3f7c09925
parent 2 c4c2ac0facfd
equal deleted inserted replaced
2:c4c2ac0facfd 13:83b3f7c09925
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0""
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Helper class used by CalcDocument,CCalcContainer etc., 
       
    15 *                TCalcEditLine. 
       
    16 *                The numerical value and the character sequence of 
       
    17 *                the real number are held. Addition of a numerical character 
       
    18 *                sequence and which clear operation are performed.
       
    19 *
       
    20 */
       
    21 
       
    22 
       
    23 
       
    24 // INCLUDE FILES  
       
    25 #include    "CalcDoc.h"
       
    26 #include    "CalcEditline.h"
       
    27 #include	"CalcEnv.h"
       
    28 
       
    29 
       
    30 //  LOCAL CONSTANTS AND MACROS  
       
    31 
       
    32 
       
    33 // ================= MEMBER FUNCTIONS =======================
       
    34 
       
    35 
       
    36 // C++ default constructor can NOT contain any code, that
       
    37 // might leave.
       
    38 //
       
    39 TCalcEditLine::TCalcEditLine():
       
    40                      iNumber(0), 
       
    41                      iOperator(ECalcOperatorNone)
       
    42     {
       
    43     }
       
    44 
       
    45 TCalcEditLine::~TCalcEditLine()
       
    46     {
       
    47     }
       
    48 
       
    49 // Copy constructor
       
    50 TCalcEditLine::TCalcEditLine
       
    51            (const TCalcEditLine& aEditLine) 
       
    52     {
       
    53     *this = aEditLine;
       
    54     }
       
    55 
       
    56 // Assignment operator
       
    57 const TCalcEditLine& TCalcEditLine::operator=
       
    58                        (const TCalcEditLine& aEditLine) 
       
    59     {
       
    60     iNumber = aEditLine.iNumber;
       
    61     iOperator = aEditLine.iOperator;
       
    62     return  *this;
       
    63     }
       
    64 
       
    65 
       
    66 // ---------------------------------------------------------
       
    67 // TCalcEditLine::AllClear
       
    68 // This function exists because long-press clear-key event is executed. 
       
    69 // (other items were commented in a header).
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 void TCalcEditLine::AllClear()
       
    73     {
       
    74     iNumber.Zero();
       
    75     iOperator = ECalcOperatorNone;
       
    76     }
       
    77 
       
    78 // ---------------------------------------------------------
       
    79 // TCalcEditLine::ClearL
       
    80 // This function exists because short-press clear-key event is executed. 
       
    81 // (other items were commented in a header).
       
    82 // ---------------------------------------------------------
       
    83 //
       
    84 void TCalcEditLine::ClearL()
       
    85     {
       
    86     TInt length(iNumber.Length());
       
    87     if (length > 1)
       
    88         {
       
    89         CCalcAppEnv* env = CCalcAppEnv::Static();
       
    90         TChar minusIndicator(env->MinusIndicator());
       
    91         if (iNumber[0] == (TUint) minusIndicator && // Casted for THUMB build
       
    92             length == 2)
       
    93             {
       
    94             SetNumber(0);
       
    95             }
       
    96         else
       
    97             {
       
    98             iNumber.Delete(length - 1, 1);
       
    99             length--;
       
   100             if (length == 2 && iNumber[0] == (TUint) minusIndicator &&
       
   101                                              // Casted for THUMB build
       
   102                 NumberL() == 0.0)
       
   103                 {
       
   104                 SetNumber(0);
       
   105                 }
       
   106             }
       
   107         }
       
   108     else
       
   109         {
       
   110         SetNumber(0);
       
   111         }
       
   112     }
       
   113 
       
   114 
       
   115 // ---------------------------------------------------------
       
   116 // TCalcEditLine::ClearOperand
       
   117 // Set number of editor "0".  
       
   118 // (other items were commented in a header).
       
   119 // ---------------------------------------------------------
       
   120 //
       
   121 void TCalcEditLine::ClearOperand()
       
   122     {
       
   123     SetNumber(0);
       
   124     }
       
   125 
       
   126 // ---------------------------------------------------------
       
   127 // TCalcEditLine::NumberL
       
   128 // Convert from TBuf<> to number.    
       
   129 // (other items were commented in a header).
       
   130 // ---------------------------------------------------------
       
   131 //
       
   132 TReal64 TCalcEditLine::NumberL() const
       
   133     {
       
   134     TLex  lex(iNumber);
       
   135     TReal64  data;
       
   136     TInt errorCode(lex.Val(data));
       
   137     // Converting is failed. 
       
   138     if (errorCode)
       
   139         {
       
   140         User::Leave(errorCode);
       
   141         }
       
   142     return  data;
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------
       
   146 // TCalcEditLine::SetNumber
       
   147 // Convert from number to TBuf<>.    
       
   148 // (other items were commented in a header).
       
   149 // ---------------------------------------------------------
       
   150 //
       
   151 void TCalcEditLine::SetNumber
       
   152                 (TReal64 aNumber)
       
   153     {
       
   154     // Set data format. 
       
   155     TRealFormat realFormat( KCalcMaxEditNumberWidth, KCalcMaxDigits );
       
   156     if ( aNumber >= 0 )
       
   157         {
       
   158         realFormat.iWidth--;
       
   159         }
       
   160     realFormat.iType = KRealFormatNoExponent;
       
   161     realFormat.iTriLen = 0;  //  Delimit character is nothing
       
   162     CCalcAppEnv* env = CCalcAppEnv::Static();
       
   163     TChar separator(env->DecimalSeparator());
       
   164     realFormat.iPoint = separator;
       
   165 
       
   166     TInt code = iNumber.Num(aNumber, realFormat);
       
   167     
       
   168     if(KErrOverflow == code || KErrUnderflow == code)
       
   169 	    {
       
   170 	    // Set data format 
       
   171 	    TRealFormat realFormat(KCalcMaxNumberWidth);  
       
   172 	    if ( aNumber >= 0 )
       
   173 	        {
       
   174 	        realFormat.iWidth--;
       
   175 	        }
       
   176 	    realFormat.iType = KRealFormatExponent;
       
   177 	    realFormat.iPlaces = 3;
       
   178 	    realFormat.iTriLen = 0;  //  Delimit character is nothing
       
   179 	    CCalcAppEnv* env = CCalcAppEnv::Static();
       
   180 	    TChar separator(env->DecimalSeparator());
       
   181 	    realFormat.iPoint = separator;
       
   182         code = iNumber.Num(aNumber, realFormat);
       
   183 	    }
       
   184 	   
       
   185 	  switch (code)
       
   186         {
       
   187         case KErrOverflow:
       
   188             {
       
   189             TRAP_IGNORE( User::Leave( KErrOverflow ) );  
       
   190             break;
       
   191             }
       
   192         case KErrUnderflow:
       
   193             {
       
   194             iNumber.Num(0,0);
       
   195             break;
       
   196             }
       
   197         default:
       
   198             {
       
   199             break;
       
   200             }
       
   201         }
       
   202     }
       
   203 
       
   204 // ---------------------------------------------------------
       
   205 // TCalcEditLine::AppendL
       
   206 // This function exists to handle numeric or "#" key event.    
       
   207 // (other items were commented in a header).
       
   208 // ---------------------------------------------------------
       
   209 //
       
   210 void TCalcEditLine::AppendL
       
   211                (TChar aInput) // Input type is digit or decimal point only  
       
   212     {
       
   213     //  Check whether editline is not full
       
   214     if ( iNumber.Length() < KCalcMaxNumberWidth )
       
   215         {
       
   216         if ( aInput.IsDigit() )
       
   217             {
       
   218             if ( NumberDigits() < KCalcMaxDigits )
       
   219                 {
       
   220                 if (CheckZeroL())  
       
   221                     {
       
   222                     iNumber.Zero();
       
   223                     }
       
   224                 iNumber.Append(aInput);
       
   225                 }
       
   226             else
       
   227                 {
       
   228                 // If editor space is full, error code returned.
       
   229                 User::Leave(KCalcErrEditorSpaceFull); 
       
   230                 }
       
   231             }
       
   232         // If decimal point do not exist, decimal point is added 
       
   233         // But editor has 8 dights, decimal point is not added.
       
   234         else 
       
   235             {
       
   236             CCalcAppEnv* env = CCalcAppEnv::Static();
       
   237             TChar separator(env->DecimalSeparator());
       
   238             if ( iNumber.Length() )
       
   239                 {
       
   240                 if ( iNumber.Locate(separator) == KErrNotFound )
       
   241                     {
       
   242                     if (NumberDigits() < KCalcMaxDigits)
       
   243                         {
       
   244                         iNumber.Append(separator);
       
   245                         }
       
   246                     }
       
   247                 }
       
   248             else
       
   249                 {
       
   250                 SetNumber(0);  
       
   251                 iNumber.Append(separator);
       
   252                 }
       
   253             }
       
   254         }
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------
       
   258 // TCalcEditLine::AppendNumberString
       
   259 // This function appends a character after the number.
       
   260 // Used for example in percent calculations.
       
   261 // (other items were commented in a header).
       
   262 // ---------------------------------------------------------
       
   263 //
       
   264 void TCalcEditLine::AppendNumberStringL
       
   265 				(TChar aChar)
       
   266 	{
       
   267 	//  This kind of a paranoid check as when this function
       
   268 	//  is called the amount of digits can never be more
       
   269 	//  than 8. Because the iNumber is going
       
   270 	//  to the output sheet we can display 10 characters
       
   271 	//  8 digits + operator + aChar
       
   272     if ( NumberDigits() < KCalcMaxDigits + 1 )
       
   273         { 
       
   274 			iNumber.Append(aChar);
       
   275         }
       
   276     else
       
   277         {
       
   278 			// If editor space is full, error code returned.
       
   279             User::Leave(KCalcErrEditorSpaceFull); 
       
   280          }
       
   281 	}
       
   282 
       
   283 // ---------------------------------------------------------
       
   284 // TCalcEditLine::ChangeSignL
       
   285 // This function exists to handle "Change Sign" command
       
   286 // (other items were commented in a header).
       
   287 // ---------------------------------------------------------
       
   288 //
       
   289 void TCalcEditLine::ChangeSignL()
       
   290     {
       
   291     TInt length(iNumber.Length()); 
       
   292 
       
   293     // If the number is zero or empty, this command is ignored
       
   294     if ((length > 0) && (NumberL() != 0.0))
       
   295         {
       
   296         CCalcAppEnv* env = CCalcAppEnv::Static();
       
   297         TChar minusIndicator(env->MinusIndicator());
       
   298         // If the number is less than zero, '-' is deleted.
       
   299         if ( iNumber[0] == (TUint) minusIndicator ) // Casted for THUMB build
       
   300             {
       
   301             iNumber.Delete(0, 1);
       
   302             }
       
   303         // Case number is more than zero.
       
   304         else
       
   305             {
       
   306             TBuf<1> buf;
       
   307             buf.Append(minusIndicator);
       
   308             iNumber.Insert(0, buf);
       
   309             }
       
   310         }
       
   311     }
       
   312 
       
   313 // ---------------------------------------------------------
       
   314 // TCalcEditLine::TrimZeroL
       
   315 // If number of editor is "iii.ddd000...", set number "iii.ddd".
       
   316 // (other items were commented in a header).
       
   317 // ---------------------------------------------------------
       
   318 //
       
   319 void TCalcEditLine::TrimZeroL() 
       
   320     {
       
   321     TReal64 realValue(NumberL());
       
   322     SetNumber(realValue);
       
   323     }
       
   324 
       
   325 
       
   326 // ---------------------------------------------------------
       
   327 // TCalcEditLine::CheckZero
       
   328 // For checking that editline is cleared. 
       
   329 // (other items were commented in a header).
       
   330 // ---------------------------------------------------------
       
   331 //
       
   332 TBool TCalcEditLine::CheckZeroL() const
       
   333     {
       
   334     return ((iNumber.Length() == 1) && (NumberL() == 0.0));
       
   335     }
       
   336 
       
   337 // ---------------------------------------------------------
       
   338 // TCalcEditLine::ChangeDecimal
       
   339 // Changing decimal separator.
       
   340 // (other items were commented in a header).
       
   341 // ---------------------------------------------------------
       
   342 //
       
   343 void TCalcEditLine::ChangeDecimal(TChar aOld, TChar aNew)
       
   344     {
       
   345     TInt pos(iNumber.Locate(aOld));
       
   346 
       
   347     if (pos != KErrNotFound)
       
   348         {
       
   349         iNumber[pos] = (TUint16) aNew;
       
   350         }
       
   351     }
       
   352         
       
   353 
       
   354 // ---------------------------------------------------------
       
   355 // TCalcEditLine::NumberDigits
       
   356 // To count of digits in a line
       
   357 // (other items were commented in a header).
       
   358 // ---------------------------------------------------------
       
   359 //
       
   360 TInt TCalcEditLine::NumberDigits() const
       
   361     {
       
   362     TInt len(iNumber.Length());
       
   363     TInt num(0);
       
   364     for (TInt i(0); i < len; ++i)
       
   365         {
       
   366         TChar onechar(iNumber[i]);
       
   367         if (onechar.IsDigit())
       
   368             {
       
   369             ++num;
       
   370             }
       
   371         }
       
   372     return num;
       
   373     }
       
   374 
       
   375 
       
   376 //  End of File