uifw/eikctl/src/aknuniteditor.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2006-2007 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:  Unit MFNE
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <barsread.h>
       
    20 #include <eikmfne.pan>
       
    21 #include <e32math.h>
       
    22 #include <eikon.hrh>
       
    23 #include <eikctl.rsg>
       
    24 #include <StringLoader.h>
       
    25 #include <AknUtils.h>
       
    26 
       
    27 #include <AknTasHook.h>
       
    28 #include "aknmfnefloat.h"
       
    29 #include "aknmfneseparator.h"
       
    30 #include "aknuniteditor.h"
       
    31 
       
    32 const TInt KAknNumberOfFields( 3 );
       
    33 const TInt KAknMaxAllowedFractionalDigits( 8 );
       
    34 const TInt KAknResourceTable[] =
       
    35         {
       
    36         0,
       
    37         R_AVKON_UNIT_EDITOR_METER,
       
    38         R_AVKON_UNIT_EDITOR_KILOMETER,
       
    39         R_AVKON_UNIT_EDITOR_FOOT,
       
    40         R_AVKON_UNIT_EDITOR_MILE,
       
    41         R_AVKON_UNIT_EDITOR_METR_SPEED,
       
    42         R_AVKON_UNIT_EDITOR_IMPER_SPEED
       
    43         };
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // A utility function to copy from source to destination with a length
       
    47 // check. If source is larger than destination, the function returns the
       
    48 // length of the source. This value can then be passed to client (where
       
    49 // source length is not necessarily known) for allocating a buffer.
       
    50 // ---------------------------------------------------------------------------
       
    51 static TInt CopyDescOrNull( TDes& aDestination, const TDesC& aSource )
       
    52     {
       
    53     TInt sourceLength( aSource.Length() );
       
    54     
       
    55     if ( aDestination.MaxLength() >= sourceLength )
       
    56         {
       
    57         aDestination.Copy( aSource );
       
    58         sourceLength = 0;
       
    59         }
       
    60     
       
    61     return sourceLength;
       
    62     }
       
    63 
       
    64 // ======== MEMBER FUNCTIONS ========
       
    65 
       
    66 CAknUnitEditor::CAknUnitEditor() : iUnitLimit( sizeof( KAknResourceTable ) / sizeof( TInt ) )
       
    67     {
       
    68     }
       
    69     
       
    70 
       
    71 EXPORT_C CAknUnitEditor* CAknUnitEditor::NewL()
       
    72     {
       
    73     CAknUnitEditor* self = CAknUnitEditor::NewLC();
       
    74     CleanupStack::Pop( self );
       
    75     return self;
       
    76     }
       
    77 
       
    78 
       
    79 EXPORT_C CAknUnitEditor* CAknUnitEditor::NewLC()
       
    80     {
       
    81     CAknUnitEditor* self = new( ELeave ) CAknUnitEditor;
       
    82     CleanupStack::PushL( self );
       
    83     AKNTASHOOK_ADDL( self, "CAknUnitEditor" );
       
    84     return self;
       
    85     }
       
    86     
       
    87     
       
    88 EXPORT_C CAknUnitEditor::~CAknUnitEditor()
       
    89     {
       
    90     AKNTASHOOK_REMOVE();
       
    91     }
       
    92 
       
    93 
       
    94 EXPORT_C void CAknUnitEditor::ConstructL(
       
    95         TReal aMinimumValue, TReal aMaximumValue,
       
    96         TReal aInitialValue, 
       
    97         TInt aMaxFractionalDigits, TInt aUnit, TUint aFlags )
       
    98     {
       
    99     // No other construction so this function could be called again
       
   100     if ( !SupportsUnit( aUnit ) )
       
   101         {
       
   102         User::Leave( KErrNotSupported );
       
   103         }
       
   104     
       
   105     CreateFieldArrayL( KAknNumberOfFields );
       
   106 
       
   107     iFloatField = CAknMfneFloat::NewL( aMaxFractionalDigits );
       
   108     
       
   109     iFloatField->SetLimits( aMinimumValue, aMaximumValue );
       
   110     
       
   111     _LIT( KSpace, " " );
       
   112     iSeparatorField = CAknMfneSeparator::NewL( KSpace );
       
   113     
       
   114     iUnitField = CAknMfneSeparator::NewL( KNullDesC );
       
   115     
       
   116     // This is for the lifetime of the editor.    
       
   117     if ( AknLayoutUtils::LayoutMirrored() )
       
   118         {
       
   119         AddField( iUnitField );      // ownership transferred
       
   120         AddField( iSeparatorField ); // ownership transferred
       
   121         AddField( iFloatField );     // ownership transferred
       
   122         }
       
   123     else
       
   124         {   
       
   125         AddField( iFloatField );     // ownership transferred
       
   126         AddField( iSeparatorField ); // ownership transferred
       
   127         AddField( iUnitField );      // ownership transferred
       
   128         }
       
   129         
       
   130     SetValue( aInitialValue );
       
   131     SetUnitL( aUnit );
       
   132 
       
   133     SetFlags( aFlags );
       
   134     }
       
   135 
       
   136 
       
   137 EXPORT_C void CAknUnitEditor::ConstructFromResourceL( TResourceReader& aResourceReader )
       
   138     {
       
   139     TReal minimumValue       = aResourceReader.ReadReal64();
       
   140     TReal maximumValue       = aResourceReader.ReadReal64();
       
   141     TReal initialValue       = aResourceReader.ReadReal64();
       
   142     TInt maxFractionalDigits = aResourceReader.ReadInt8();
       
   143     TInt unit                = aResourceReader.ReadInt32();
       
   144     TUint flags              = aResourceReader.ReadInt32();
       
   145         
       
   146     ConstructL( minimumValue, maximumValue, initialValue, maxFractionalDigits, unit, flags );
       
   147     }
       
   148 
       
   149 
       
   150 EXPORT_C TBool CAknUnitEditor::SetValue( TReal aValue )
       
   151     {
       
   152     TBool ret = iFloatField->SetValue( aValue );
       
   153     iUnitField->MakeVisible( UnitFieldVisibility() );
       
   154     DrawDeferred();
       
   155     if ( !IsFocused() )
       
   156         {
       
   157         TRAP_IGNORE( ReportEventL( MCoeControlObserver::EEventStateChanged ) );
       
   158         }
       
   159     return ret;
       
   160     }
       
   161     
       
   162     
       
   163 EXPORT_C TReal CAknUnitEditor::Value() const
       
   164     {
       
   165     return iFloatField->Value();
       
   166     }
       
   167     
       
   168 EXPORT_C TBool CAknUnitEditor::SupportsUnit( TInt aUnit ) const
       
   169     {
       
   170     return ( aUnit >= 0 && aUnit < iUnitLimit );
       
   171     }
       
   172 
       
   173 EXPORT_C void CAknUnitEditor::SetUnitL( const TDesC& aUnit )
       
   174     {
       
   175     iUnitField->SetTextL( aUnit );
       
   176     iUnitType = EAknUnitEditorCustomUnit;
       
   177     DrawDeferred();
       
   178     }
       
   179     
       
   180 
       
   181 EXPORT_C void CAknUnitEditor::SetUnitL( TInt aUnit )
       
   182     {
       
   183     TInt resourceId( EAknUnitEditorCustomUnit );
       
   184         
       
   185     if ( !SupportsUnit( aUnit ) )
       
   186         {
       
   187         User::Leave( KErrNotSupported );
       
   188         }
       
   189         
       
   190     resourceId = KAknResourceTable[aUnit];
       
   191     
       
   192     if ( resourceId )
       
   193         {
       
   194         HBufC* unitString = StringLoader::LoadL( resourceId );
       
   195         CleanupStack::PushL( unitString );
       
   196         iUnitField->SetTextL( *unitString );
       
   197         CleanupStack::PopAndDestroy( unitString ); 
       
   198         }
       
   199     else
       
   200         {
       
   201         iUnitField->SetTextL( KNullDesC );
       
   202         }
       
   203 
       
   204     // Either a leave has occurred or this is a safe match
       
   205     iUnitType = aUnit;
       
   206     
       
   207     DrawDeferred();
       
   208     }
       
   209     
       
   210 
       
   211 EXPORT_C TInt CAknUnitEditor::GetUnit( TDes& aDes ) const
       
   212     {
       
   213     return CopyDescOrNull( aDes, iUnitField->Text() );
       
   214     }
       
   215     
       
   216     
       
   217 EXPORT_C TInt CAknUnitEditor::Unit() const
       
   218     {
       
   219     return iUnitType;
       
   220     }
       
   221 
       
   222 
       
   223 EXPORT_C void CAknUnitEditor::SetMaxFractionalDigits(
       
   224     TInt aMaxFractionalDigits )
       
   225     {
       
   226     TBool validValue = ( aMaxFractionalDigits >= 0 &&
       
   227          aMaxFractionalDigits <= KAknMaxAllowedFractionalDigits );
       
   228     
       
   229     if ( validValue )
       
   230         {
       
   231         iFloatField->SetMaxFractionalDigits( aMaxFractionalDigits );
       
   232         DrawDeferred();
       
   233         }
       
   234     }
       
   235     
       
   236 
       
   237 EXPORT_C TInt CAknUnitEditor::MaxFractionalDigits() const
       
   238     {
       
   239     return iFloatField->MaxFractionalDigits();
       
   240     }
       
   241     
       
   242 
       
   243 EXPORT_C void CAknUnitEditor::SetMinimumAndMaximum( TReal aMinimumValue, TReal aMaximumValue )
       
   244     {
       
   245     iFloatField->SetLimits( aMinimumValue, aMaximumValue );
       
   246     DrawDeferred();
       
   247     }
       
   248     
       
   249 
       
   250 EXPORT_C void CAknUnitEditor::GetMinimumAndMaximum( TReal& aMinimumValue, TReal& aMaximumValue ) const
       
   251     {
       
   252     iFloatField->GetLimits( aMinimumValue, aMaximumValue );
       
   253     }
       
   254    
       
   255 
       
   256 EXPORT_C void CAknUnitEditor::SetFlags( TUint aFlags )
       
   257     {
       
   258     iFlags = aFlags;
       
   259     
       
   260     if ( iFlags & EAknUnitEditorAllowUninitialized )
       
   261         {
       
   262         iFloatField->SetFlags( CAknMfneFloat::EAllowNaN );
       
   263         }
       
   264 
       
   265     DrawDeferred();
       
   266     }
       
   267 
       
   268 
       
   269 EXPORT_C TUint CAknUnitEditor::Flags() const
       
   270     {
       
   271     return iFlags;
       
   272     }
       
   273     
       
   274 
       
   275 EXPORT_C void CAknUnitEditor::PrepareForFocusLossL()
       
   276     {
       
   277     CEikMfne::PrepareForFocusLossL();
       
   278     }
       
   279 
       
   280 
       
   281 void CAknUnitEditor::FocusChanged( TDrawNow aDrawNow )
       
   282     {
       
   283     // Due to a quirk in dialog page captioned control creation
       
   284     // this function could get called even if the editor is
       
   285     // not fully constructed.
       
   286     if ( iUnitField )
       
   287         {
       
   288         iUnitField->MakeVisible( UnitFieldVisibility() );    
       
   289         }
       
   290         
       
   291     CEikMfne::FocusChanged( aDrawNow );
       
   292     }
       
   293 
       
   294 
       
   295 TBool CAknUnitEditor::UnitFieldVisibility() const
       
   296     {
       
   297     return ( !Math::IsNaN( Value() ) || IsFocused() );
       
   298     }
       
   299 
       
   300 
       
   301 // End of file