extras/converter/Ui/Src/CCnvMainForm.cpp
changeset 0 3ee3dfdd8d69
child 6 57ec1f1a7fd2
equal deleted inserted replaced
-1:000000000000 0:3ee3dfdd8d69
       
     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:  
       
    15 *      This is the implementation of the class defined in CCnvMainForm.h
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <barsread.h>
       
    22 #include <eikenv.h>
       
    23 #include <eikappui.h>
       
    24 #include <eikfpne.h>
       
    25 #include <avkon.hrh>
       
    26 #include <AknPopupField.h>
       
    27 #include <aknnotewrappers.h>
       
    28 #include <eikmenup.h>
       
    29 #include <s32file.h>
       
    30 #include <ErrorUI.h>
       
    31 #include <featmgr.h>
       
    32 
       
    33 #include <hlplch.h>
       
    34 #include <csxhelp/cnv.hlp.hrh>
       
    35 
       
    36 #include <CCnvConverter.h>
       
    37 #include <TCnvCategory.h>
       
    38 #include <Converter.rsg>
       
    39 #include "Converter.hrh"
       
    40 #include "CCnvApplication.h"
       
    41 #include "CCnvMainForm.h"
       
    42 #include "CCnvRatesForm.h"
       
    43 
       
    44 #include <aknnumed.h> 
       
    45 #include <f32file.h>
       
    46 #include <e32std.h>
       
    47 
       
    48 _LIT( KCnvPlusSign, "+" );
       
    49 _LIT( KCnvMinusSign, "-" );
       
    50 _LIT( KCnvExponentSign, "E" );
       
    51 _LIT( KCnvDotSign, "." );
       
    52 
       
    53 _LIT( KCnvMinusInTheMiddle, "?*-?*" );
       
    54 _LIT( KCnvPlusInTheMiddle, "?*+?*" );
       
    55 _LIT( KCnvExponentInTheMiddle, "?*E?*" );
       
    56 _LIT( KCnvExponentInTheMiddleAndEnd, "?*E?*E?" );
       
    57 
       
    58 	_LIT( KConverterFilename,"UnitConverter.ini");
       
    59 	_LIT(KDelimiter, ":");
       
    60 
       
    61 CCnvMainForm::CCnvMainForm( TBool &aAlreadyDead )
       
    62 : iIsFullyConstructed( EFalse ),
       
    63 iHasNotifiedDiscFull( EFalse ),
       
    64 iLastUsedEditorId( EConverterEditor1 ),
       
    65 iHistoryTypeCategory( -1 ),
       
    66 iHistoryUnit1Category( -1 ),
       
    67 iHistoryUnit2Category( -1 ),
       
    68 iAlreadyDead( aAlreadyDead )
       
    69 	{
       
    70 	}
       
    71 
       
    72 CCnvMainForm::~CCnvMainForm()
       
    73 	{
       
    74 	if( iIsFullyConstructed )
       
    75 		{
       
    76 		SaveState();
       
    77 		}
       
    78 	delete iModel;
       
    79 	delete iConversionTimer;
       
    80 	delete iErrorUI;
       
    81 	iAlreadyDead = ETrue;
       
    82 
       
    83 	//remove the edwin observers
       
    84 	CEikFloatingPointEditor* editor1 =
       
    85 		static_cast< CEikFloatingPointEditor* >( Control( EConverterEditor1 ) );
       
    86 	editor1->RemoveEdwinObserver(this);
       
    87 
       
    88 	CEikFloatingPointEditor* editor2 =
       
    89 		static_cast< CEikFloatingPointEditor* >( Control( EConverterEditor2 ) );	
       
    90 	editor2->RemoveEdwinObserver(this);
       
    91 
       
    92 	}
       
    93 
       
    94 void CCnvMainForm::ConstructL()
       
    95 	{
       
    96 	CAknDialog::ConstructL( R_CNV_MAINFORM_MENUBAR );
       
    97 
       
    98 	iErrorUI = CErrorUI::NewL( *iCoeEnv );
       
    99 
       
   100 	TResourceReader reader;
       
   101 	iEikonEnv->CreateResourceReaderLC( reader, R_CNV_MODEL );
       
   102 	iModel = CCnvConverter::NewL( reader );
       
   103 	CleanupStack::PopAndDestroy(); // reader
       
   104 
       
   105 	iConversionTimer = CPeriodic::NewL( CActive::EPriorityStandard );
       
   106 
       
   107 	iCurrencyCategoryId = iModel->CurrencyCategoryId();
       
   108 	iIsFullyConstructed = ETrue;
       
   109 	isEdwinUpdateText = EFalse;
       
   110 	
       
   111 		TFileName path;
       
   112     	iEikonEnv->FsSession().PrivatePath(path);
       
   113     	iEikonEnv->FsSession().MkDirAll(path);
       
   114   
       
   115 	}
       
   116 
       
   117 void CCnvMainForm::GetHelpContext( TCoeHelpContext& aContext ) const
       
   118 	{
       
   119 	aContext.iMajor = KUidCnv;
       
   120 	aContext.iContext = KCNV_HLP_MAIN_STATE;
       
   121 	}
       
   122 
       
   123 // Handle commands from Options menu
       
   124 void CCnvMainForm::ProcessCommandL( TInt aCommandId )
       
   125 	{
       
   126     // this hides the options menu etc.
       
   127     CAknForm::ProcessCommandL( aCommandId );
       
   128 
       
   129 	switch( aCommandId )
       
   130 		{
       
   131 		case ECmdSetType:
       
   132 			{
       
   133 			CmdSetTypeL();
       
   134 			break;
       
   135 			}
       
   136 
       
   137 		case ECmdSetUnit:		// FLOWTHROUGH
       
   138 		case ECmdSetCurrency:
       
   139 			{
       
   140 			CmdSetUnitL();
       
   141 			break;
       
   142 			}
       
   143 
       
   144 		case ECmdRates:
       
   145 			{
       
   146 			CmdRatesL();
       
   147 			break;
       
   148 			}
       
   149 		case EAknCmdHelp:
       
   150 			{
       
   151 			iEikonEnv->EikAppUi()->HandleCommandL( EAknCmdHelp );
       
   152 			break;
       
   153 			}
       
   154 		case EEikCmdExit:
       
   155 			{
       
   156 			OkToExitL( EAknSoftkeyExit );
       
   157 			break;
       
   158 			}
       
   159 
       
   160 		default:
       
   161 			{
       
   162 			break;
       
   163 			}
       
   164 		}
       
   165 	}
       
   166 
       
   167 // Takes any action required when the current line is changed to aControlId.
       
   168 void CCnvMainForm::LineChangedL( TInt aControlId )
       
   169 	{
       
   170 
       
   171 		CEikButtonGroupContainer& cba = ButtonGroupContainer();
       
   172 		cba.SetCommandSetL(R_CNV_MAINFORM_SOFTKEYS);
       
   173 		
       
   174 	if( ( aControlId == EConverterEditor1 ) ||
       
   175 		( aControlId == EConverterEditor2 ) )
       
   176 		{
       
   177 		CEikFloatingPointEditor* editor =
       
   178 			static_cast< CEikFloatingPointEditor* >( Control( aControlId ) );
       
   179 
       
   180 		// don't clear the selection if there is only '0' in the editor
       
   181 		TBuf<KEditorTextMaxLength> editorText;
       
   182 		editor->GetText(editorText);
       
   183 
       
   184 		SynchronousConversionL();
       
   185 		//        iActiveEditorChanged = EFalse;
       
   186 		}
       
   187 	else if(aControlId == EConverterUnit1Popup || 
       
   188 		aControlId == EConverterUnit2Popup)
       
   189 		{
       
   190 			
       
   191 									
       
   192 		// if focus is moved to unit popup from amount field
       
   193 		// then try to do the conversion.
       
   194 		// IsEditorValueValidL shows invalid value note if value is not valid
       
   195 		TBool editorValueValid = IsEditorValueValidL(iLastUsedEditorId);
       
   196 		if (editorValueValid && AreRatesValidL(ETrue))
       
   197 			{
       
   198 			SynchronousConversionL();
       
   199 			}
       
   200 		}
       
   201 	if(aControlId ==  EConverterUnit1Popup || 
       
   202 		aControlId == EConverterUnit2Popup || 
       
   203 		aControlId == EConverterTypePopup)
       
   204 		{
       
   205 		//Makes the MSK visible
       
   206 	
       
   207 			cba.MakeCommandVisible(EAknSoftkeyChange,ETrue);
       
   208 	
       
   209 				
       
   210 		}
       
   211 		else if(aControlId ==EConverterEditor1 ||
       
   212 				aControlId ==EConverterEditor2) 
       
   213 		{
       
   214 			
       
   215 		
       
   216 		//makes the MSK invisible
       
   217 	
       
   218 		cba.MakeCommandVisible(EAknSoftkeyChange,EFalse);
       
   219 
       
   220 		}
       
   221 	 cba.DrawNow();
       
   222 	}
       
   223 
       
   224 void CCnvMainForm::DynInitMenuPaneL( TInt aResourceId,
       
   225                                     CEikMenuPane* aMenuPane )
       
   226     {
       
   227     // When click on LSK(option), the invalid value should be set to 0.
       
   228     TInt controlId( IdOfFocusControl() );
       
   229     if ( ( controlId == EConverterEditor1 ) || ( controlId == EConverterEditor2 ) )
       
   230     	  {
       
   231         if ( EditorValueCausesHold( controlId ) || !IsVaildNumberEntered( controlId ) )
       
   232             {
       
   233             SetFloatEditorValueL( controlId, 0.0 );
       
   234             }
       
   235         SynchronousConversionL( EFalse );
       
   236     	}
       
   237 
       
   238     TUint currentType( iTypeCategory.CurrentValueIndex() );
       
   239 
       
   240 	if( aResourceId == R_CNV_MAINFORM_MENUPANE )
       
   241 		{
       
   242 		if (!FeatureManager::FeatureSupported(KFeatureIdHelp))
       
   243 			{
       
   244 			aMenuPane->SetItemDimmed(EAknCmdHelp, ETrue);
       
   245 			}
       
   246 
       
   247 		if( currentType == iCurrencyCategoryId )
       
   248 			{
       
   249 			aMenuPane->SetItemDimmed( ECmdRates, EFalse ) ;
       
   250 			}
       
   251 
       
   252 		switch( IdOfFocusControl() )
       
   253 			{
       
   254 			case EConverterUnit1Popup:
       
   255 			case EConverterUnit2Popup:
       
   256 				{
       
   257 				if( currentType == iCurrencyCategoryId )
       
   258 					{
       
   259 					aMenuPane->SetItemDimmed( ECmdSetCurrency, EFalse ) ;
       
   260 					}
       
   261 				else
       
   262 					{
       
   263 					aMenuPane->SetItemDimmed( ECmdSetUnit, EFalse ) ;
       
   264 					}
       
   265 				break;
       
   266 				}
       
   267 			default:
       
   268 				{
       
   269 				break;
       
   270 				}
       
   271 			}
       
   272 		}
       
   273 	}
       
   274 
       
   275 // Handles events to the three pop lists
       
   276 void CCnvMainForm::HandleControlEventL( CCoeControl* /*aControl*/,
       
   277 									   TCoeEvent aEventType )
       
   278 	{
       
   279 	// We are interested only about EEventStateChanged events
       
   280 	if( aEventType != EEventStateChanged )
       
   281 		{
       
   282 		return;
       
   283 		}
       
   284 
       
   285 	TInt controlId( IdOfFocusControl() );
       
   286 
       
   287 	if( !HasReallyChanged( controlId ) )
       
   288 		{
       
   289 		return;
       
   290 		}
       
   291 
       
   292 	switch( controlId )
       
   293 		{
       
   294 		case EConverterTypePopup:
       
   295 			{
       
   296 			HandleCategoryChangedL();
       
   297 			break;
       
   298 			}
       
   299 		case EConverterUnit1Popup:
       
   300 		case EConverterUnit2Popup:
       
   301 			{
       
   302 			HandleUnitChangedL();
       
   303 			break;
       
   304 			}
       
   305 		//These have been moved to OfferKeyEventL
       
   306 		case EConverterEditor1:
       
   307 		case EConverterEditor2:
       
   308 			{
       
   309 			iLastUsedEditorId = controlId;
       
   310 			if( (iActiveEditorChanged)&&
       
   311 				( isEdwinNavigated == EFalse || isEdwinUpdateText )  ) // Is there any navigation (or selection) 
       
   312 				//happening on the editor
       
   313 				{
       
   314 				AsynchronousConversion();
       
   315 				}
       
   316 			isEdwinNavigated = EFalse; //make the boolean false
       
   317 			iActiveEditorChanged = ETrue;
       
   318 			break;
       
   319 			}
       
   320 
       
   321 		default:
       
   322 			{
       
   323 			break;
       
   324 			}
       
   325 		}
       
   326 	}
       
   327 
       
   328 TBool CCnvMainForm::SaveFormData()
       
   329 	{
       
   330 	// Always allow exit (data is saved in the destructor)
       
   331 	return ETrue;
       
   332 	}
       
   333 
       
   334 TBool CCnvMainForm::OkToExitL( TInt aButtonId )
       
   335 	{
       
   336 	TBool status( CAknDialog::OkToExitL( aButtonId ) );
       
   337 	TInt controlId( IdOfFocusControl() );
       
   338 	if( status && ( aButtonId == EAknSoftkeyExit ) )
       
   339 		{
       
   340 		iEikonEnv->EikAppUi()->HandleCommandL( EAknCmdExit );
       
   341 		}
       
   342 	else if(aButtonId == EAknSoftkeyChange)
       
   343 		{
       
   344 		if (controlId == EConverterTypePopup)
       
   345 		{
       
   346 		CmdSetTypeL();
       
   347 		status = EFalse;
       
   348 		}
       
   349 	else if ((controlId == EConverterUnit1Popup) ||(controlId == EConverterUnit2Popup))
       
   350 		{
       
   351 		CmdSetUnitL();
       
   352 		status = EFalse;
       
   353 		}
       
   354 		}
       
   355 	else
       
   356 		{
       
   357 		status = EFalse;
       
   358 		}
       
   359 	return status;
       
   360 	}
       
   361 
       
   362 void CCnvMainForm::PreLayoutDynInitL()
       
   363 	{
       
   364 	// Store pointers to the popup list controls
       
   365 	// (makes further access easier/faster)
       
   366 	iTypePopupCtrl =
       
   367 		static_cast< CAknPopupField* >( Control( EConverterTypePopup ) );
       
   368 	iUnit1PopupCtrl =
       
   369 		static_cast< CAknPopupField* >( Control( EConverterUnit1Popup ) );
       
   370 	iUnit2PopupCtrl =
       
   371 		static_cast< CAknPopupField* >( Control( EConverterUnit2Popup ) );
       
   372 
       
   373 
       
   374 	// Set values for the unit category popup control
       
   375 	iModel->GetCategorylistAccessor( iTypeCategory );
       
   376 	iTypePopupCtrl->SetQueryValueL( &iTypeCategory );
       
   377 
       
   378 	// Load user selections from file (if any)
       
   379 	LoadStateL();
       
   380 
       
   381 	// Set initial values for the popup lists
       
   382 	HandleCategoryChangedL();
       
   383 
       
   384 	InitialiseChangeHistory();
       
   385 
       
   386 	if (iUnit1Category.UnitFactor(iUnit1Category.CurrentValueIndex()) != 0.0 &&
       
   387 		iUnit2Category.UnitFactor(iUnit2Category.CurrentValueIndex()) != 0.0)
       
   388 		{
       
   389 		SynchronousConversionL();
       
   390 		}
       
   391 	
       
   392 	//Add wdwin observers for the two editors so that they can capture the Navigation 
       
   393 	//events and not do asynchronous conversion
       
   394 	CEikFloatingPointEditor* editor1 =
       
   395 		static_cast< CEikFloatingPointEditor* >( Control( EConverterEditor1 ) );	
       
   396 
       
   397 	CEikFloatingPointEditor* editor2 =
       
   398 		static_cast< CEikFloatingPointEditor* >( Control( EConverterEditor2 ) );	
       
   399 
       
   400 	editor1->AddEdwinObserverL(this);
       
   401 	editor2->AddEdwinObserverL(this);
       
   402 
       
   403 
       
   404 	}
       
   405 
       
   406 TBool CCnvMainForm::HasReallyChanged( TInt controlId )
       
   407 	{
       
   408 	TBool changed( EFalse );
       
   409 
       
   410 	switch( controlId )
       
   411 		{
       
   412         case EConverterTypePopup:
       
   413             {
       
   414             if( iTypeCategory.CurrentValueIndex() != iHistoryTypeCategory )
       
   415                 {
       
   416                 changed = ETrue;
       
   417                 iHistoryTypeCategory = iTypeCategory.CurrentValueIndex();
       
   418 
       
   419                 // renew iHistoryUnit1Category and iHistoryUnit2Category
       
   420                 iModel->GetCategoryAccessor( iUnit1Category, iHistoryTypeCategory );
       
   421                 TUint HistoryUnit1Category;
       
   422                 TUint HistoryUnit2Category;
       
   423                 iUnit1Category.GetDefaultUnits( HistoryUnit1Category , HistoryUnit2Category );
       
   424                 iHistoryUnit1Category = HistoryUnit1Category;
       
   425                 iHistoryUnit2Category = HistoryUnit2Category;
       
   426                 }
       
   427             break;
       
   428 			}
       
   429 		case EConverterUnit1Popup:
       
   430 			{
       
   431 			if( iUnit1Category.CurrentValueIndex() != iHistoryUnit1Category )
       
   432 				{
       
   433 				changed = ETrue;
       
   434 				iHistoryUnit1Category = iUnit1Category.CurrentValueIndex();
       
   435 				}
       
   436 			break;
       
   437 			}
       
   438 		case EConverterUnit2Popup:
       
   439 			{
       
   440 			if( iUnit2Category.CurrentValueIndex() != iHistoryUnit2Category )
       
   441 				{
       
   442 				changed = ETrue;
       
   443 				iHistoryUnit2Category = iUnit2Category.CurrentValueIndex();
       
   444 				}
       
   445 			break;
       
   446 			}
       
   447 
       
   448 		case EConverterEditor1:
       
   449 		case EConverterEditor2:
       
   450 			{
       
   451 			changed = ETrue;
       
   452 			break;
       
   453 			}
       
   454 
       
   455 		default:
       
   456 			{
       
   457 			break;
       
   458 			}
       
   459 		}
       
   460 
       
   461 	return changed;
       
   462 	}
       
   463 
       
   464 void CCnvMainForm::InitialiseChangeHistory()
       
   465 	{
       
   466 	iHistoryTypeCategory = iTypeCategory.CurrentValueIndex();
       
   467 	iHistoryUnit1Category = iUnit1Category.CurrentValueIndex();
       
   468 	iHistoryUnit2Category = iUnit2Category.CurrentValueIndex();
       
   469 	}
       
   470 
       
   471 void CCnvMainForm::HandleCategoryChangedL()
       
   472 	{
       
   473 	TInt categoryId = iTypeCategory.CurrentValueIndex();
       
   474 	iModel->GetCategoryAccessor( iUnit1Category, categoryId );
       
   475 	iModel->GetCategoryAccessor( iUnit2Category, categoryId );
       
   476 
       
   477 	TUint srcUnit, dstUnit;
       
   478 	iUnit1Category.GetDefaultUnits( srcUnit, dstUnit );
       
   479 	iUnit1Category.SetCurrentValueIndex( srcUnit );
       
   480 	iUnit2Category.SetCurrentValueIndex( dstUnit );
       
   481 
       
   482 	// Set arrays to popup lists and redraw them
       
   483 	iUnit1PopupCtrl->SetQueryValueL( &iUnit1Category );
       
   484 	iUnit2PopupCtrl->SetQueryValueL( &iUnit2Category );
       
   485 	iUnit1PopupCtrl->DrawNow();
       
   486 	iUnit2PopupCtrl->DrawNow();
       
   487 
       
   488 	// Reset amount fields
       
   489 	TReal zero( 0.0 );
       
   490 	SetFloatingPointEditorValueL( EConverterEditor1, &zero );
       
   491 	SetFloatingPointEditorValueL( EConverterEditor2, &zero );
       
   492 
       
   493     // when category's type is temperature the editor's value can't simply set to zero
       
   494 	AsynchronousConversion();
       
   495     
       
   496 	iLastUsedEditorId = EConverterEditor1;
       
   497 	}
       
   498 
       
   499 void CCnvMainForm::HandleUnitChangedL()
       
   500 	{
       
   501 	iUnit1Category.SetDefaultUnits( iUnit1Category.CurrentValueIndex(),
       
   502 		iUnit2Category.CurrentValueIndex() );
       
   503 	SynchronousConversionL();
       
   504 	}
       
   505 
       
   506 TBool CCnvMainForm::AreRatesValidL(TBool aShowNote)
       
   507 	{
       
   508 	TInt numInvalid( 0 );
       
   509 	if( iUnit1Category.UnitFactor( iUnit1Category.CurrentValueIndex() )
       
   510 		== 0.0 )
       
   511 		{
       
   512 		numInvalid++;
       
   513 		SetFloatEditorValueL( EConverterEditor1, 0.0 );
       
   514 		}
       
   515 	if( iUnit2Category.UnitFactor( iUnit2Category.CurrentValueIndex() )
       
   516 		== 0.0 )
       
   517 		{
       
   518 		numInvalid++;
       
   519 		SetFloatEditorValueL( EConverterEditor2, 0.0 );
       
   520 		}
       
   521 
       
   522 	if(numInvalid)
       
   523 		{
       
   524 		CancelConversion();
       
   525 
       
   526 		if (aShowNote)
       
   527 			{
       
   528 			TBool isPlural( EFalse );
       
   529 			if( numInvalid > 1 )
       
   530 				{
       
   531 				isPlural = ETrue;
       
   532 				}
       
   533 
       
   534 			TInt resourceId(R_CNV_MAINFORM_NORATE );
       
   535 			if( isPlural )
       
   536 				{
       
   537 				resourceId = R_CNV_MAINFORM_NORATE_PLURAL;
       
   538 				}
       
   539 			HBufC* label = iCoeEnv->AllocReadResourceLC( resourceId );
       
   540 			CAknErrorNote* note = new( ELeave ) CAknErrorNote;
       
   541 			note->ExecuteLD( *label );
       
   542 			CleanupStack::PopAndDestroy();	// label;
       
   543 			}
       
   544 		return EFalse;
       
   545 		}
       
   546 	else
       
   547 		{
       
   548 		return ETrue;
       
   549 		}
       
   550 	}
       
   551 
       
   552 void CCnvMainForm::ShowInvalidValueNoteL(TInt aEditorId)
       
   553 	{
       
   554 	CancelConversion();
       
   555 	
       
   556 	SetFloatEditorValueL( aEditorId, 0.0 );
       
   557 	
       
   558 	HBufC* label = iCoeEnv->AllocReadResourceLC(
       
   559 		R_CNV_MAINFORM_INVALIDVALUE );
       
   560 	CAknErrorNote* note = new( ELeave ) CAknErrorNote;
       
   561 	note->ExecuteLD( *label );
       
   562 	CleanupStack::PopAndDestroy();	// label;
       
   563 
       
   564 	SynchronousConversionL();
       
   565 	}
       
   566 
       
   567 TBool CCnvMainForm::IsEditorValueValidL( TUint aEditorId )
       
   568 	{
       
   569 	TReal amount( FloatingPointEditorValue( aEditorId ) );
       
   570 
       
   571 	TReal minimum;
       
   572 	if( aEditorId == EConverterEditor1 )
       
   573 		{
       
   574 		minimum = iUnit1Category.UnitMinimum(
       
   575 			iUnit1Category.CurrentValueIndex() );
       
   576 		}
       
   577 	else
       
   578 		{
       
   579 		minimum = iUnit2Category.UnitMinimum(
       
   580 			iUnit2Category.CurrentValueIndex() );
       
   581 		}
       
   582 
       
   583 	if( amount < minimum )
       
   584 		{
       
   585 		ShowInvalidValueNoteL(aEditorId);
       
   586 		return EFalse;
       
   587 		}
       
   588 	else
       
   589 		{
       
   590 		return ETrue;
       
   591 		}
       
   592 	}
       
   593 
       
   594 TBool CCnvMainForm::EditorValueCausesHold( TUint aEditorId )
       
   595 	{
       
   596 	CEikFloatingPointEditor* editor =
       
   597 		static_cast< CEikFloatingPointEditor* >( Control( aEditorId ) );	
       
   598 
       
   599 	if( editor->TextLength() == 0 )
       
   600 		{
       
   601 		return ETrue;
       
   602 		}
       
   603 
       
   604     TBuf< 12 > content;
       
   605     TLocale locale;
       
   606 
       
   607     // Not all the countries use '.' to express radix point, some use ','.
       
   608     // So use locale.DecimalSeparator() to get local symbol.
       
   609     TBuf<1> decimal;
       
   610     decimal.Append( locale.DecimalSeparator() );
       
   611     editor->Text()->Extract( content, 0, 12 );
       
   612     TInt contentLength( content.Length() );
       
   613     if ( !content.Compare( KCnvPlusSign ) )
       
   614         {
       
   615         return ETrue;
       
   616         }
       
   617     else if ( !content.Compare( KCnvMinusSign ) )
       
   618         {
       
   619         return ETrue;
       
   620         }
       
   621 
       
   622     else if ( !content.Compare( decimal ) )
       
   623         {
       
   624         return ETrue;
       
   625         }
       
   626 	else if( contentLength > 1 && 
       
   627 		content[ contentLength - 1 ] == KCnvExponentSign()[ 0 ] &&
       
   628 		IsDigit( content[ contentLength - 2 ] ) &&
       
   629 		content.Match( KCnvExponentInTheMiddle ) == KErrNotFound &&
       
   630 		content.Match( KCnvMinusInTheMiddle ) == KErrNotFound &&
       
   631 		content.Match( KCnvPlusInTheMiddle ) == KErrNotFound)
       
   632 		{
       
   633 		// If content ends with <digit>E with no minus, plus or exponent
       
   634 		// sign in the middle
       
   635 		return ETrue;
       
   636 		}
       
   637 	else if( contentLength > 2 &&
       
   638 		( content[ contentLength - 1 ] == KCnvMinusSign()[ 0 ] ||
       
   639 		content[ contentLength - 1 ] == KCnvPlusSign()[ 0 ] ) &&
       
   640 		content[ contentLength - 2 ] == KCnvExponentSign()[ 0 ] &&
       
   641 		IsDigit( content[ contentLength - 3 ] ) &&
       
   642 		content.Match( KCnvExponentInTheMiddleAndEnd ) == KErrNotFound &&
       
   643 		content.Match( KCnvMinusInTheMiddle ) == KErrNotFound &&
       
   644 		content.Match( KCnvPlusInTheMiddle ) == KErrNotFound )
       
   645 		{
       
   646 		// If content ends with <digit>E[+|-] with no minus, plus or exponent
       
   647 		// sign in the middle
       
   648 		return ETrue;
       
   649 		}
       
   650 	else if(contentLength > 12 &&
       
   651 		content.Locate(locale.DecimalSeparator()) == contentLength - 1)
       
   652 		{
       
   653 		// content ends with decimal separator and there is no other
       
   654 		// decimal separator
       
   655 		return ETrue;
       
   656 		}
       
   657 
       
   658 
       
   659 	return EFalse;
       
   660 	}
       
   661 
       
   662 void CCnvMainForm::CmdRatesL()
       
   663 	{
       
   664 	TBool wantsRelaunch;
       
   665 	TBool shouldExit( EFalse );
       
   666 	TBool needsSave( EFalse );
       
   667 	do
       
   668 		{
       
   669 		wantsRelaunch = EFalse;
       
   670 		TCnvCategory currencies;
       
   671 		iModel->GetCategoryAccessor( currencies, iCurrencyCategoryId );
       
   672 
       
   673 		CCnvRatesForm* rates = new( ELeave ) CCnvRatesForm( &wantsRelaunch,
       
   674 			&shouldExit, &needsSave, currencies);
       
   675 
       
   676 		CleanupStack::PushL( rates );
       
   677 		rates->ConstructL();
       
   678 		CleanupStack::Pop(); // rates
       
   679 		rates->ExecuteLD( R_CNV_RATESFORM );
       
   680 		}
       
   681 	while( wantsRelaunch );
       
   682 
       
   683 	if( needsSave )
       
   684 		{
       
   685 		SaveState();
       
   686 		}
       
   687 
       
   688 	if( !shouldExit )
       
   689 		{
       
   690 		// Reset the form (there might be changes in unit names..)
       
   691 		HandleCategoryChangedL();
       
   692 		
       
   693 		DrawNow();
       
   694 		}
       
   695 	else
       
   696 		{
       
   697 		TryExitL( EAknSoftkeyExit );
       
   698 		}
       
   699 	}
       
   700 
       
   701 void CCnvMainForm::CmdSetTypeL()
       
   702 	{
       
   703 	TryChangeFocusToL( EConverterTypePopup );
       
   704 	iTypePopupCtrl->ActivateSelectionListL();
       
   705 	}
       
   706 
       
   707 void CCnvMainForm::CmdSetUnitL()
       
   708 	{
       
   709 	switch ( IdOfFocusControl() )
       
   710 		{
       
   711 		case EConverterUnit1Popup:
       
   712 			{
       
   713 			iUnit1PopupCtrl->ActivateSelectionListL();							
       
   714 			break;
       
   715 			}
       
   716 
       
   717 		case EConverterUnit2Popup:
       
   718 			{
       
   719 			iUnit2PopupCtrl->ActivateSelectionListL();
       
   720 			break;
       
   721 			}
       
   722 
       
   723 		default:
       
   724 			{
       
   725 			break;
       
   726 			}
       
   727 		}
       
   728 	}
       
   729 
       
   730 void CCnvMainForm::LoadStateL()
       
   731 	{
       
   732 	RFileReadStream in;
       
   733 	
       
   734 		TFileName filePath;
       
   735 		TBuf<1> tempDes;
       
   736 		TChar driveChar;
       
   737     	User::LeaveIfError(iEikonEnv->FsSession().PrivatePath(filePath));
       
   738     	iEikonEnv->FsSession().DriveToChar(KDefaultDrive, driveChar);
       
   739     	tempDes.Append(driveChar);
       
   740     	filePath.Insert(0,KDelimiter);
       
   741     	filePath.Insert(0,tempDes);
       
   742     	filePath.Append(KConverterFilename);
       
   743   		
       
   744 		TInt err( in.Open( iEikonEnv->FsSession(), filePath,
       
   745 			EFileRead ) );
       
   746 	
       
   747 	if( !err )
       
   748 		{
       
   749 		TRAP( err, iModel->InternalizeL( in ) );
       
   750 		if( !err )
       
   751 			{
       
   752 			TRAP( err, iTypeCategory.SetCurrentValueIndex( in.ReadInt8L() ) );
       
   753 			}
       
   754 		in.Close();
       
   755 		}
       
   756 	}
       
   757 
       
   758 void CCnvMainForm::SaveState()
       
   759 	{
       
   760 	RFileWriteStream out;
       
   761 	
       
   762 		TFileName filePath;
       
   763 		TBuf<1> tempDes;
       
   764 		TChar driveChar;
       
   765     	TRAPD(errd, iEikonEnv->FsSession().PrivatePath(filePath));
       
   766     	iEikonEnv->FsSession().DriveToChar(KDefaultDrive, driveChar);
       
   767     	tempDes.Append(driveChar);
       
   768     	if(!errd)
       
   769     	{
       
   770     		
       
   771     	filePath.Insert(0,KDelimiter);
       
   772     	filePath.Insert(0,tempDes);
       
   773     	filePath.Append(KConverterFilename);
       
   774     	}
       
   775     	
       
   776 		TInt err( out.Replace( iEikonEnv->FsSession(), filePath,
       
   777 			EFileWrite ) );
       
   778 	
       
   779 	if( !err )
       
   780 		{
       
   781 		TRAP( err, iModel->ExternalizeL( out ) );
       
   782 		if( !err )
       
   783 			{
       
   784 			TRAP( err, out.WriteInt8L( iTypeCategory.CurrentValueIndex() ) );
       
   785 			}
       
   786 		out.Close();
       
   787 		}
       
   788 
       
   789 	if( err && !iHasNotifiedDiscFull )
       
   790 		{
       
   791 		TRAP_IGNORE(iErrorUI->ShowGlobalErrorNoteL( err ) );
       
   792 		iHasNotifiedDiscFull = ETrue;
       
   793 		}
       
   794 	}
       
   795 
       
   796 void CCnvMainForm::SynchronousConversionL( TBool aShowRateInvalideNote )
       
   797     {
       
   798     // Don't report an "invalid value" error. Cancel conversion.
       
   799     // if the field is empty (i.e. the user has used delete/backspace) etc.
       
   800     if( EditorValueCausesHold( iLastUsedEditorId ) )
       
   801         {
       
   802         // if one Editor is blank, set the other to 0.
       
   803         CEikFloatingPointEditor* editor =
       
   804         		static_cast< CEikFloatingPointEditor* >( Control( iLastUsedEditorId ) );
       
   805     	if( editor->TextLength() == 0 )
       
   806     		{
       
   807             if( iLastUsedEditorId == EConverterEditor1)
       
   808             	{
       
   809             	SetFloatEditorValueL( EConverterEditor2, 0.0 );
       
   810             	}
       
   811             if( iLastUsedEditorId == EConverterEditor2)
       
   812             	{
       
   813             	SetFloatEditorValueL( EConverterEditor1, 0.0 );
       
   814             	}
       
   815     		}
       
   816         return;
       
   817         }
       
   818 
       
   819     if ( !IsVaildNumberEntered( iLastUsedEditorId ) )
       
   820         {
       
   821         ShowInvalidValueNoteL( iLastUsedEditorId );
       
   822         }
       
   823 
       
   824 
       
   825     // PrepareForFocusLossL leaves if the component is not able to
       
   826     // parse the content. We trap it so that IsEditorValueValidL()
       
   827     // can check and report the situation correctly.
       
   828     TRAP_IGNORE( Control( iLastUsedEditorId )->PrepareForFocusLossL() );
       
   829     TInt CurrentControlId( IdOfFocusControl() );
       
   830     if( CurrentControlId == EConverterUnit1Popup )
       
   831     	{
       
   832     	IsEditorValueValidL( EConverterEditor1 );
       
   833     	}
       
   834     else if (CurrentControlId == EConverterUnit2Popup)
       
   835     	{
       
   836     	IsEditorValueValidL( EConverterEditor2 );
       
   837     	}
       
   838     else if (CurrentControlId == EConverterEditor2 )
       
   839         	{
       
   840         	if ( !IsVaildNumberEntered( EConverterEditor2 ) )
       
   841         		{
       
   842         		iLastUsedEditorId = CurrentControlId;
       
   843         		ShowInvalidValueNoteL( EConverterEditor2 );
       
   844         		}
       
   845         	}
       
   846     else if (CurrentControlId == EConverterEditor1 )
       
   847         	{
       
   848         	if ( !IsVaildNumberEntered( EConverterEditor1 ) )
       
   849         		{
       
   850         		iLastUsedEditorId = CurrentControlId;
       
   851         		ShowInvalidValueNoteL( EConverterEditor1 );
       
   852         		}
       
   853         	}
       
   854     TReal result( 0.0 );
       
   855 
       
   856     // send an argument to notify the AreRatesValidL() display a note dialog or not
       
   857     if (IsEditorValueValidL(iLastUsedEditorId))
       
   858 		{
       
   859 		if (AreRatesValidL(aShowRateInvalideNote))
       
   860 			{
       
   861 			TReal amount(FloatingPointEditorValue(iLastUsedEditorId));
       
   862 			TUint unit1(iUnit1Category.CurrentValueIndex());
       
   863 			TUint unit2(iUnit2Category.CurrentValueIndex());
       
   864 
       
   865 			//        RAPD( ignored, Control( iLastUsedEditorId )->PrepareForFocusLossL() );
       
   866 
       
   867 
       
   868 			// do not reset the touch UI to standby state
       
   869 			// CCoeEnv::Static()->SyncNotifyFocusObserversOfChangeInFocus();
       
   870 
       
   871 
       
   872 			// Do the calculation and change the value for the related edit field      
       
   873 			ValueCalculationL(unit1, unit2, amount);
       
   874 
       
   875 			CEikFloatingPointEditor* editor =
       
   876 					static_cast<CEikFloatingPointEditor*> (Control(
       
   877 							iLastUsedEditorId));
       
   878 			TBuf<KEditorTextMaxLength> editorText;
       
   879 			editor->GetText(editorText);
       
   880 
       
   881 			// Use local value for different area
       
   882 			TBuf<1> localeZero;
       
   883 			localeZero.AppendNum(0);
       
   884 			AknTextUtils::LanguageSpecificNumberConversion(localeZero);
       
   885 			if (editorText.Length() > 1 && editorText[0] == localeZero[0])
       
   886 				{
       
   887 				TCursorSelection sel = editor->Selection();
       
   888 				TInt removedZeroes = 0;
       
   889 				// remove all leading zeroes
       
   890 				while (editorText.Length() > 0 && editorText[0]
       
   891 						== localeZero[0])
       
   892 					{
       
   893 					editorText.Delete(0, 1);
       
   894 					++removedZeroes;
       
   895 					}
       
   896 				// if too many zeroes removed, insert one.
       
   897 				TLocale locale;
       
   898 				if (editorText.Length() == 0 || TChar(editorText[0])
       
   899 						== locale.DecimalSeparator())
       
   900 					{
       
   901 					editorText.Insert(0, localeZero);
       
   902 					--removedZeroes;
       
   903 					}
       
   904 				editor->SetTextL(&editorText);
       
   905 
       
   906 				// adjust the selection accroding to removed zeroes
       
   907 				TInt anchor =
       
   908 						(sel.iAnchorPos - removedZeroes > 0) ? sel.iAnchorPos
       
   909 								- removedZeroes : 0;
       
   910 				TInt cursor =
       
   911 						(sel.iCursorPos - removedZeroes > 0) ? sel.iCursorPos
       
   912 								- removedZeroes : 0;
       
   913 				editor->SetSelectionL(cursor, anchor);
       
   914             
       
   915             if ( !IsVaildNumberEntered( iLastUsedEditorId ) )
       
   916                    {
       
   917                    ShowInvalidValueNoteL( iLastUsedEditorId );
       
   918                    }
       
   919 				// notify the touch window
       
   920 				editor->SetCursorPosL(cursor, ETrue);
       
   921 				editor->DrawNow();
       
   922 				}
       
   923 			}
       
   924 		}
       
   925     }
       
   926 
       
   927 void CCnvMainForm::AsynchronousConversion()
       
   928 	{
       
   929 	CancelConversion();
       
   930 	TCallBack cb( TimerCallback, this );
       
   931 	iConversionTimer->Start( 750000, 0, cb );
       
   932 	}
       
   933 
       
   934 void CCnvMainForm::CancelConversion()
       
   935 	{
       
   936 	iConversionTimer->Cancel();
       
   937 	}
       
   938 
       
   939 TInt CCnvMainForm::TimerCallback( TAny* aMainForm )
       
   940 	{
       
   941 
       
   942 	CCnvMainForm* form = reinterpret_cast< CCnvMainForm* >( aMainForm );
       
   943 
       
   944 	TRAP_IGNORE(form->SynchronousConversionL() );
       
   945 
       
   946 	form->CancelConversion();
       
   947 
       
   948 	return KErrNone;
       
   949 	}
       
   950 
       
   951 
       
   952 
       
   953 // ----------------------------------------------------
       
   954 // CCnvMainForm::OfferKeyEventL
       
   955 // Keyevent process.
       
   956 // ----------------------------------------------------
       
   957 //
       
   958 TKeyResponse CCnvMainForm::OfferKeyEventL(const TKeyEvent& aKeyEvent,
       
   959 										  TEventCode aType)
       
   960 	{
       
   961 
       
   962 	if ( !IsFocused() )
       
   963 	{
       
   964 	   if (aKeyEvent.iCode == EKeyEscape)
       
   965 		   {
       
   966 		   return CAknForm::OfferKeyEventL( aKeyEvent, aType );
       
   967 		   }
       
   968 		return EKeyWasNotConsumed;
       
   969 	}
       
   970 	
       
   971 	TInt controlId( IdOfFocusControl() );
       
   972 
       
   973 	TBool hashKeyPressed = EFalse;
       
   974   
       
   975     // Use decimalChar instead of 46
       
   976     TLocale locale;
       
   977     TChar decimalChar = locale.DecimalSeparator();
       
   978     if ( aKeyEvent.iScanCode == EStdKeyHash || decimalChar == TChar( aKeyEvent.iCode ) )
       
   979         { 
       
   980         hashKeyPressed = ETrue;
       
   981         }
       
   982 		
       
   983 	if( hashKeyPressed )
       
   984 		{
       
   985             // If inputting through VKB or HWR, the event can be done only when aType is equal to EEventKey.
       
   986             // Besides, only when inputting the dot in VKB or HWR, aKeyEvent.iScanCode is equal to decimalChar.
       
   987             TBool VKBdotKeyPressed = EFalse; 
       
   988             if ( ( aKeyEvent.iScanCode == decimalChar ) )
       
   989             {
       
   990                 VKBdotKeyPressed = ETrue;
       
   991             }
       
   992             if ( ( aType == EEventKey) && ( !VKBdotKeyPressed ) )
       
   993 			{
       
   994 				return EKeyWasConsumed;
       
   995 			}
       
   996 			else if( aType == EEventKeyUp)
       
   997 			{
       
   998 				TKeyEvent keyevent( aKeyEvent );
       
   999 				aType = EEventKey;
       
  1000                 keyevent.iCode = decimalChar;
       
  1001 				keyevent.iModifiers = 0;
       
  1002 				keyevent.iScanCode = 0;
       
  1003 				keyevent.iRepeats = 0;
       
  1004 				return  static_cast< CEikFloatingPointEditor* >( Control( controlId ) )
       
  1005 				->OfferKeyEventL(keyevent,aType);
       
  1006 			}
       
  1007 			else
       
  1008 			{//
       
  1009 			}
       
  1010 		}
       
  1011 	
       
  1012 	//Not a key event this case handles the menu and exit keys 
       
  1013 	//when an invalid value is entered in the amount field
       
  1014 	if(aType == EEventKeyDown) 
       
  1015 		{
       
  1016 		if( ( EConverterEditor1 == controlId )||
       
  1017 			( EConverterEditor2 == controlId ) )
       
  1018 			{
       
  1019 
       
  1020 			if( (aKeyEvent.iScanCode == EStdKeyDevice0) ||//menu Key
       
  1021 				(aKeyEvent.iScanCode == EStdKeyDevice1) )//Exit key
       
  1022 				{
       
  1023 				if( EditorValueCausesHold( controlId ) ||
       
  1024 					!IsVaildNumberEntered( controlId ) )
       
  1025 					{
       
  1026 					SetFloatEditorValueL( controlId, 0.0);
       
  1027 					}
       
  1028 				//return EKeyWasNotConsumed;
       
  1029 				}
       
  1030 			}
       
  1031 		}
       
  1032 
       
  1033     if ( aType != EEventKey )
       
  1034         {
       
  1035         // Return EKeyWasNotConsumed if OfferKeyEventL() does not handle any event.	
       
  1036         return EKeyWasNotConsumed;
       
  1037         } 	
       
  1038 
       
  1039 	if( ( EConverterEditor1 == controlId )||
       
  1040 		( EConverterEditor2 == controlId ) )
       
  1041 		{	
       
  1042 		//Just to see that -9E-99 doesnt appear when an invalid number is editor is 
       
  1043 		//entered in the editor		
       
  1044 
       
  1045 		if(aKeyEvent.iScanCode == EStdKeyUpArrow ||
       
  1046 			aKeyEvent.iScanCode == EStdKeyDownArrow )
       
  1047 			{
       
  1048 			CancelConversion();
       
  1049 			if( EditorValueCausesHold( controlId ) )
       
  1050 				{
       
  1051 				SetFloatEditorValueL( controlId, 0.0);
       
  1052 				}
       
  1053 			if(	!IsVaildNumberEntered( controlId ) )
       
  1054 				{
       
  1055 				ShowInvalidValueNoteL( controlId );
       
  1056 				}		
       
  1057 			}
       
  1058 
       
  1059 		iLastUsedEditorId = controlId;
       
  1060 
       
  1061 		//For arrow keys asynchronous conversion not required
       
  1062 		// if (iActiveEditorChanged)
       
  1063 		// {
       
  1064 		/*if( ( aKeyEvent.iScanCode != EStdKeyRightArrow )&&
       
  1065 		( aKeyEvent.iScanCode != EStdKeyLeftArrow )&&
       
  1066 		( aKeyEvent.iScanCode != EStdKeyUpArrow )&&
       
  1067 		( aKeyEvent.iScanCode != EStdKeyDownArrow )&&
       
  1068 		( isEditKey == EFalse ) )
       
  1069 		{
       
  1070 		AsynchronousConversion();			
       
  1071 		}*/
       
  1072 		//}
       
  1073 
       
  1074 		//iActiveEditorChanged = ETrue;
       
  1075 
       
  1076 		}
       
  1077 	return CAknForm::OfferKeyEventL(aKeyEvent, aType);
       
  1078 
       
  1079 	}
       
  1080 
       
  1081 
       
  1082 // ----------------------------------------------------
       
  1083 // CCnvMainForm::IsVaildNumberEntered
       
  1084 // Checks if a valid number is entered in the editor field and returns
       
  1085 // ETrue if a vaild number is entered and
       
  1086 // EFalse if an invalid number is entered
       
  1087 // ----------------------------------------------------
       
  1088 //
       
  1089 
       
  1090 TBool CCnvMainForm::IsVaildNumberEntered( TInt aEditorId )
       
  1091     {
       
  1092     CEikFloatingPointEditor* editor =
       
  1093         static_cast< CEikFloatingPointEditor* >( Control( aEditorId ) );
       
  1094 
       
  1095     if ( editor->TextLength() == 0 )
       
  1096         {
       
  1097         return EFalse;
       
  1098         }	
       
  1099 
       
  1100     TReal real;
       
  1101     CAknNumericEdwin::TValidationStatus validationStatus = editor->GetValueAsReal( real );
       
  1102 
       
  1103     // The enter number is not valid if GetValueAsReal function's return value is not EValueValid 
       
  1104     if ( validationStatus != CAknNumericEdwin::EValueValid /*&& validationStatus != CAknNumericEdwin::EValueTooLarge*/ )
       
  1105         {
       
  1106         return EFalse;
       
  1107         }
       
  1108     return ETrue;
       
  1109     }
       
  1110 
       
  1111 
       
  1112 
       
  1113 // ----------------------------------------------------
       
  1114 // CCnvMainForm::HandleEdwinEventL
       
  1115 // Checks the Navigation events on the editor 
       
  1116 // ----------------------------------------------------
       
  1117 //
       
  1118 
       
  1119 void CCnvMainForm::HandleEdwinEventL(CEikEdwin* /*aEdwin*/,TEdwinEvent aEventType)
       
  1120 	{
       
  1121 	if(EEventNavigation == aEventType)
       
  1122 		{
       
  1123 		isEdwinNavigated = ETrue;
       
  1124 		}
       
  1125 	 if( aEventType == EEventTextUpdate )
       
  1126 			{
       
  1127 			isEdwinUpdateText = ETrue;
       
  1128 			}
       
  1129 	
       
  1130 	}
       
  1131 
       
  1132 // ----------------------------------------------------
       
  1133 // CCnvMainForm::IsDigit
       
  1134 // Converts the digit from any digit mode (devangiri or arabic) 
       
  1135 // to western digit and checks whether it is a valid digit or not.
       
  1136 // ----------------------------------------------------
       
  1137 //
       
  1138 TBool CCnvMainForm::IsDigit(TUint aChar)
       
  1139     {
       
  1140     TBuf<1> digit;
       
  1141     digit.Append(aChar);
       
  1142     
       
  1143     AknTextUtils::ConvertDigitsTo(digit , EDigitTypeWestern);
       
  1144     
       
  1145     TChar ch = digit[0];
       
  1146     return ch.IsDigit();
       
  1147     }
       
  1148     
       
  1149 // ----------------------------------------------------
       
  1150 // CCnvMainForm::PrepareForFocusTransitionL
       
  1151 // The Uikon framework calls this function immediately before focus move.
       
  1152 // ----------------------------------------------------
       
  1153 //    
       
  1154 void CCnvMainForm::PrepareForFocusTransitionL() 
       
  1155     {
       
  1156     TInt controlId( IdOfFocusControl() );
       
  1157     if ( ( EConverterEditor1 == controlId ) || ( EConverterEditor2 == controlId ) )
       
  1158         {
       
  1159 
       
  1160         // record the last use editor id
       
  1161         iLastUsedEditorId = controlId;
       
  1162         CancelConversion();
       
  1163         if ( EditorValueCausesHold( controlId ) )
       
  1164             {
       
  1165             SetFloatEditorValueL( controlId, 0.0 );
       
  1166             }
       
  1167 
       
  1168         // Delete the radix point when there is no number behind it.
       
  1169         else if ( IsVaildNumberEntered( controlId ) )
       
  1170             {
       
  1171             CEikFloatingPointEditor* editor =
       
  1172                 static_cast<CEikFloatingPointEditor*> ( Control( controlId ) ); 
       
  1173             TBuf<KEditorTextMaxLength> editorText;
       
  1174             editor->GetText( editorText );
       
  1175             TLocale locale;
       
  1176             TInt editorLength = editorText.Length();
       
  1177 
       
  1178             // Check if the last character is radix point.
       
  1179             if ( editorText[ editorLength - 1 ] == locale.DecimalSeparator() )
       
  1180                 {
       
  1181                 editorText.Delete( editorLength - 1, 1 );
       
  1182                 editor->SetTextL( &editorText );
       
  1183                 }
       
  1184             }
       
  1185         }
       
  1186     }
       
  1187 
       
  1188 // ----------------------------------------------------
       
  1189 // CCnvMainForm::ValueCalculation
       
  1190 // Do the calculation and change the value for the related edit field
       
  1191 // ----------------------------------------------------
       
  1192 //        
       
  1193 void CCnvMainForm::ValueCalculationL( TUint aUnit1Popup, TUint aUnit2Popup, TReal aMount )
       
  1194     {
       
  1195     TReal result( 0.0 );
       
  1196     // Use "LastUnit1" and "LastUnit2" to insure that the second amount don't change if the last used editor 
       
  1197     // is the second and the category's units do not change.
       
  1198     static TInt LastUnit1 = -1;
       
  1199     static TInt LastUnit2 = -1;
       
  1200     
       
  1201     // According to the UI Spec:
       
  1202     // 1. If the first conversion unit field is changed, a new conversion is calculated to the 
       
  1203     // second amount field of units, or vice versa.
       
  1204     // 2. If the first edit field is changed, a new conversion is calculated to the second 
       
  1205     // edit field, or vice versa.
       
  1206     if ( iLastUsedEditorId == EConverterEditor1 )
       
  1207         {
       
  1208         //the first unit is changed
       
  1209         if ( ( ( IdOfFocusControl() != EConverterEditor1 ) && ( LastUnit1 != aUnit1Popup ) )
       
  1210          || ( IdOfFocusControl() == EConverterTypePopup ) )
       
  1211             {
       
  1212             aMount = FloatingPointEditorValue( EConverterEditor1 );
       
  1213             result = iUnit1Category.Convert( aUnit1Popup, aUnit2Popup, aMount );
       
  1214             SetFloatEditorValueL( EConverterEditor2, result );
       
  1215             }
       
  1216         //the second unit is changed
       
  1217         else if ( ( IdOfFocusControl() != EConverterEditor1 ) && ( LastUnit2 != aUnit2Popup ) )
       
  1218             {
       
  1219             aMount = FloatingPointEditorValue( EConverterEditor2 );
       
  1220             result = iUnit1Category.Convert( aUnit2Popup, aUnit1Popup, aMount );
       
  1221             SetFloatEditorValueL( EConverterEditor1, result );           	
       
  1222             }
       
  1223         //the first amount field is changed
       
  1224         else
       
  1225             {        
       
  1226             result = iUnit1Category.Convert( aUnit1Popup, aUnit2Popup, aMount );
       
  1227             SetFloatEditorValueL( EConverterEditor2, result );
       
  1228             }
       
  1229         }
       
  1230     else
       
  1231         {
       
  1232         //the first unit is changed
       
  1233         if ( ( ( IdOfFocusControl() != EConverterEditor2 ) && ( LastUnit1 != aUnit1Popup ) )
       
  1234            || ( IdOfFocusControl() == EConverterTypePopup ))
       
  1235             {
       
  1236             aMount = FloatingPointEditorValue( EConverterEditor1 );
       
  1237             result = iUnit1Category.Convert( aUnit1Popup, aUnit2Popup, aMount );
       
  1238             SetFloatEditorValueL( EConverterEditor2, result );
       
  1239             }
       
  1240         //the second unit is changed
       
  1241         else if ( ( IdOfFocusControl() != EConverterEditor2 ) && ( LastUnit2 != aUnit2Popup ) )
       
  1242         	{
       
  1243             aMount = FloatingPointEditorValue( EConverterEditor2 );
       
  1244             result = iUnit1Category.Convert( aUnit2Popup, aUnit1Popup, aMount );
       
  1245             SetFloatEditorValueL( EConverterEditor1, result );           	
       
  1246         	}
       
  1247         //the second amount field is changed
       
  1248         else
       
  1249             {
       
  1250             result = iUnit1Category.Convert( aUnit2Popup, aUnit1Popup, aMount );
       
  1251             SetFloatEditorValueL( EConverterEditor1, result );
       
  1252             } 
       
  1253         }
       
  1254 
       
  1255     // record units of the category at this time.
       
  1256     LastUnit1 = aUnit1Popup;
       
  1257     LastUnit2 = aUnit2Popup;	
       
  1258     }
       
  1259 //End of file