fmradio/fmradio/src/fmradiofrequencynumber.cpp
changeset 0 f3d95d9c00ab
equal deleted inserted replaced
-1:000000000000 0:f3d95d9c00ab
       
     1 /*
       
     2 * Copyright (c) 2000 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: Frequency number handling for manual tuning dialog
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include	<aknappui.h>
       
    22 #include	<aknsoundsystem.h>
       
    23 #include	<AknTextDecorationMetrics.h>
       
    24 #include	<numberconversion.h>
       
    25 #include	<EIKCTL.rsg>
       
    26 
       
    27 #include    "fmradiofrequencynumber.h"
       
    28 #include 	"debug.h"
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 
       
    32 // C++ default constructor can NOT contain any code, that
       
    33 // might leave.
       
    34 //
       
    35 CFMRadioFrequencyNumber::CFMRadioFrequencyNumber(TInt aMinimumValue, TInt aMaximumValue, TUint32 aFlags)
       
    36 	: iMinimumValue(aMinimumValue), iMaximumValue(aMaximumValue), iFlags(aFlags)
       
    37 	{
       
    38 	// Get maximum number of digits.
       
    39 	TBuf<16> maxText;
       
    40 	maxText.AppendNum(static_cast<TInt64>(aMaximumValue));
       
    41 	iMaxDigits = maxText.Length();
       
    42 	}
       
    43 
       
    44 // EPOC default constructor can leave.
       
    45 void CFMRadioFrequencyNumber::ConstructL(const CFont& aFont, TInt aInitialValue)
       
    46     {
       
    47     FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::ConstructL") ) );
       
    48 	// Init text field containing the value.
       
    49 	iText = HBufC::NewL(iMaxDigits);
       
    50 	SetValue(aInitialValue, aFont);
       
    51     }
       
    52 
       
    53 // Two-phased constructor.
       
    54 CFMRadioFrequencyNumber* CFMRadioFrequencyNumber::NewL(const CFont& aFont, TInt aMinimumValue, TInt aMaximumValue, TInt aInitialValue, TUint32 aFlags)
       
    55     {
       
    56     FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::NewL") ) );
       
    57     CFMRadioFrequencyNumber* self = new (ELeave) CFMRadioFrequencyNumber(aMinimumValue, aMaximumValue, aFlags);
       
    58     CleanupStack::PushL(self);
       
    59     self->ConstructL(aFont, aInitialValue);
       
    60     CleanupStack::Pop();
       
    61     return self;
       
    62     }
       
    63 
       
    64     
       
    65 // Destructor
       
    66 CFMRadioFrequencyNumber::~CFMRadioFrequencyNumber()
       
    67     {
       
    68     delete iText;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------
       
    72 // CFMRadioFrequencyNumber::MaximumWidthInPixels
       
    73 // TInt addedWith is calculated same way than in TInt CEikMfneField::AdditionalWidthForHighlights, 
       
    74 // which is public but not exported method.
       
    75 // (other items were commented in a header).
       
    76 // ---------------------------------------------------------
       
    77 //
       
    78 TInt CFMRadioFrequencyNumber::MaximumWidthInPixels(const CFont& aFont, TBool /*aShrinkToMinimumSize*/)
       
    79 	{
       
    80 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::MaximumWidthInPixels") ) );
       
    81 	TInt addedWidth = 0;
       
    82     if ( IsEditable() )
       
    83         {
       
    84         TInt rightHighlight = 0;
       
    85         TAknTextDecorationMetrics metrics( &aFont );
       
    86         // Width is increased only by leftHighlight.  This is a compromise in that glyphs already have 
       
    87         // spacing within them to achieve character spacing. This spacing is generally (for numbers) on the right. 
       
    88         metrics.GetLeftAndRightMargins( addedWidth, rightHighlight ); // rightHighlight is not used
       
    89         }
       
    90 
       
    91 	return (iMaxDigits * TFindWidthOfWidestDigitType(iDigitType).MaximumWidthInPixels(aFont)) + addedWidth;
       
    92 	}
       
    93 
       
    94 
       
    95 // ---------------------------------------------------------
       
    96 // CFMRadioFrequencyNumber::InputCapabilities
       
    97 // ?implementation_description
       
    98 // (other items were commented in a header).
       
    99 // ---------------------------------------------------------
       
   100 //
       
   101 TCoeInputCapabilities CFMRadioFrequencyNumber::InputCapabilities() const
       
   102 	{
       
   103 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::InputCapabilities") ) );
       
   104 	return TCoeInputCapabilities::EWesternNumericIntegerPositive;
       
   105 	}
       
   106 
       
   107 // ---------------------------------------------------------
       
   108 // CFMRadioFrequencyNumber::Text
       
   109 // ?implementation_description
       
   110 // (other items were commented in a header).
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 const TDesC& CFMRadioFrequencyNumber::Text() const
       
   114 	{
       
   115 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::Text") ) );
       
   116 
       
   117 	if (iFlags & EIsUninitialised)
       
   118 		{
       
   119 		return KNullDesC();
       
   120 		}
       
   121 
       
   122 	return *iText;
       
   123 	}
       
   124 
       
   125 // ---------------------------------------------------------
       
   126 // CFMRadioFrequencyNumber::SetValue
       
   127 // ?implementation_description
       
   128 // (other items were commented in a header).
       
   129 // ---------------------------------------------------------
       
   130 //
       
   131 void CFMRadioFrequencyNumber::SetValue(TInt aValue, const CFont& aFont)
       
   132 	{
       
   133 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::SetValue") ) );
       
   134 	iFlags &= ~EIsUninitialised;
       
   135 	TPtr text = iText->Des();
       
   136 	text.SetLength(0);
       
   137 	TInt firstUnsetCharacter = text.Length();
       
   138 	text.SetLength(firstUnsetCharacter + iMaxDigits);
       
   139 	TInt lastUnsetCharacter = text.Length() - 1;
       
   140 
       
   141 	if (aValue == 0)
       
   142 		{
       
   143 		text[lastUnsetCharacter--] = TText(iDigitType);
       
   144 		}
       
   145 	else
       
   146 		{
       
   147 		for (; aValue; aValue/=10)
       
   148 			{	
       
   149 			text[lastUnsetCharacter--] = (TText)(TText(iDigitType) + (aValue%10));
       
   150 			}
       
   151 		}
       
   152 	
       
   153 	if (lastUnsetCharacter >= firstUnsetCharacter)
       
   154 		{
       
   155 		if (iFlags & EFillWithLeadingZeros)
       
   156 			{
       
   157 			while (lastUnsetCharacter >= firstUnsetCharacter)
       
   158 				{
       
   159 				text[lastUnsetCharacter--] = TText(iDigitType);
       
   160 				}
       
   161 			}
       
   162 		else
       
   163 			{
       
   164 			text.Delete(firstUnsetCharacter, (lastUnsetCharacter + 1) - firstUnsetCharacter);
       
   165 			}
       
   166 		}
       
   167 
       
   168 	if (iFlags & EPreserveOldWidthBeforeEditing)
       
   169 		{
       
   170 		iMinimumWidthInPixels = aFont.TextWidthInPixels(Text());
       
   171 		}
       
   172 	}
       
   173 
       
   174 // ---------------------------------------------------------
       
   175 // CFMRadioFrequencyNumber::Value
       
   176 // ?implementation_description
       
   177 // (other items were commented in a header).
       
   178 // ---------------------------------------------------------
       
   179 //
       
   180 TInt CFMRadioFrequencyNumber::Value() const
       
   181 	{
       
   182 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::Value") ) );
       
   183     if (iText->Length() == 0)
       
   184         {
       
   185         CAknKeySoundSystem* soundPlayer = (static_cast<CAknAppUi*>(CEikonEnv::Static()->AppUi()))->KeySounds();
       
   186 		if (soundPlayer)
       
   187 			{
       
   188 			soundPlayer->PlaySound(EAvkonSIDWarningTone); 
       
   189 			}
       
   190 		CBaActiveScheduler::LeaveNoAlert();
       
   191         }
       
   192 
       
   193     return ValueFromText();
       
   194 	}
       
   195 
       
   196 // ---------------------------------------------------------
       
   197 // CFMRadioFrequencyNumber::IsValid
       
   198 // ?implementation_description
       
   199 // (other items were commented in a header).
       
   200 // ---------------------------------------------------------
       
   201 //
       
   202 TBool CFMRadioFrequencyNumber::IsValid() const
       
   203 	{
       
   204 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::IsValid") ) );
       
   205 	return (iText->Length() != 0);
       
   206 	}
       
   207 
       
   208 // ---------------------------------------------------------
       
   209 // CFMRadioFrequencyNumber::IsEditable
       
   210 // ?implementation_description
       
   211 // (other items were commented in a header).
       
   212 // ---------------------------------------------------------
       
   213 //
       
   214 TBool CFMRadioFrequencyNumber::IsEditable() const
       
   215 	{
       
   216 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::IsEditable") ) );
       
   217 	return ETrue;
       
   218 	}
       
   219 
       
   220 // ---------------------------------------------------------
       
   221 // CFMRadioFrequencyNumber::HighlightType
       
   222 // ?implementation_description
       
   223 // (other items were commented in a header).
       
   224 // ---------------------------------------------------------
       
   225 //
       
   226 CEikMfneField::THighlightType CFMRadioFrequencyNumber::HighlightType() const
       
   227 	{
       
   228 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::HighlightType") ) );
       
   229 	return (iFlags & EIsBeingEditedWithCursor) ? ECursor : EInverseVideo;
       
   230 	}
       
   231 
       
   232 // ---------------------------------------------------------
       
   233 // CFMRadioFrequencyNumber::HandleKey
       
   234 // ?implementation_description
       
   235 // (other items were commented in a header).
       
   236 // ---------------------------------------------------------
       
   237 //
       
   238 void CFMRadioFrequencyNumber::HandleKey(const CFont& /*aFont*/, const TKeyEvent& aKeyEvent, TBool /*aInterpretLeftAndRightAsEarEvents*/, TBool& aDataAltered, TInt& aHighlightIncrement)
       
   239 	{
       
   240 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::HandleKey") ) );
       
   241 	TChar ch = aKeyEvent.iCode;
       
   242 	TPtr text = iText->Des();
       
   243 	TInt textLength = text.Length();
       
   244 	
       
   245 	switch (ch)
       
   246 		{
       
   247 		case EKeyLeftArrow:
       
   248 		case EKeyRightArrow:
       
   249 			{
       
   250 			HandleLeftOrRightArrow(ch, aDataAltered, aHighlightIncrement);	
       
   251 			break;
       
   252 			}
       
   253 		case EKeyBackspace:
       
   254 			{
       
   255 			if (textLength)
       
   256 				{
       
   257 				iFlags |= EIsBeingEditedWithCursor;
       
   258 				text.SetLength(textLength-1);
       
   259 				aDataAltered = ETrue;
       
   260 				iFlags &= ~EIsUninitialised;
       
   261 				}
       
   262 			break;
       
   263 			}
       
   264 		default:
       
   265 			{
       
   266 			// Number key:
       
   267 			if (ch.GetNumericValue() != KErrNotFound)
       
   268 				{
       
   269 				if (~iFlags & EIsBeingEditedWithCursor)
       
   270 					{
       
   271 					// If only one number field, do not change to cursor mode.
       
   272 					if (iMaxDigits > 1)
       
   273 						{
       
   274 						iFlags |=  EIsBeingEditedWithCursor;
       
   275 						}
       
   276 					text.SetLength(0);
       
   277 					}
       
   278 
       
   279 				TBuf<1> normCh;
       
   280 				normCh.Append(ch);
       
   281 				NumberConversion::ConvertDigits(normCh, iDigitType);
       
   282 				text.Append(normCh[0]);
       
   283 				aDataAltered = ETrue;
       
   284 				iFlags &= ~EIsUninitialised;
       
   285 				if (iText->Length() >= iMaxDigits)
       
   286 					{
       
   287 					aHighlightIncrement = 1;
       
   288 					}
       
   289 				}
       
   290 			break;
       
   291 			}
       
   292 		}
       
   293 	}
       
   294 
       
   295 // ---------------------------------------------------------
       
   296 // CFMRadioFrequencyNumber::HandleDeHighlight
       
   297 // ?implementation_description
       
   298 // (other items were commented in a header).
       
   299 // ---------------------------------------------------------
       
   300 //
       
   301 void CFMRadioFrequencyNumber::HandleDeHighlight(const CFont& aFont, CEikonEnv& aEikonEnv, TBool& aDataAltered, TBool& aError)
       
   302 	{
       
   303 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::HandleDeHighlight") ) );
       
   304 	iFlags &= ~EIsBeingEditedWithCursor;
       
   305 
       
   306 	if (iText->Length() == 0)
       
   307 		{
       
   308 		SetValue(iMaximumValue, aFont);
       
   309 		aDataAltered = ETrue;
       
   310 		aError = ETrue;
       
   311 		aEikonEnv.InfoMsg(R_EIK_TBUF_NO_NUMBER_ENTERED);
       
   312 		return;
       
   313 		}
       
   314 
       
   315 	TInt value = ValueFromText();
       
   316 	TBuf<128> oldText = *iText;
       
   317 	TBool unInit = iFlags & EIsUninitialised;
       
   318 	SetValue(value, aFont);
       
   319 
       
   320 	if (oldText != *iText)
       
   321 		{
       
   322 		aDataAltered = ETrue;
       
   323 		}
       
   324 	else
       
   325 		{
       
   326 		if (unInit)
       
   327 			{
       
   328 			iFlags |= EIsUninitialised;
       
   329 			}
       
   330 		else
       
   331 			{
       
   332 			iFlags &= ~EIsUninitialised;
       
   333 			}
       
   334 		}
       
   335 	}
       
   336 
       
   337 // ---------------------------------------------------------
       
   338 // CFMRadioFrequencyNumber::ValueFromText
       
   339 // ?implementation_description
       
   340 // (other items were commented in a header).
       
   341 // ---------------------------------------------------------
       
   342 //
       
   343 TInt CFMRadioFrequencyNumber::ValueFromText() const
       
   344 	{
       
   345 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::ValueFromText") ) );
       
   346 	if (!iText->Length())
       
   347 		{
       
   348 		return iMinimumValue;
       
   349 		}
       
   350 
       
   351 	TInt i = 0;
       
   352 	TInt valueFromText = 0;
       
   353 	TInt textLength = iText->Length();
       
   354 
       
   355 	for (; i<textLength; ++i)
       
   356 		{
       
   357 		TText digit = (*iText)[i];
       
   358 		valueFromText = (valueFromText * 10) + (TInt)(digit - TText(iDigitType));
       
   359 		}
       
   360 
       
   361 	return valueFromText;
       
   362 	}
       
   363 
       
   364 // ---------------------------------------------------------
       
   365 // CFMRadioFrequencyNumber::SetDigitType
       
   366 // ?implementation_description
       
   367 // (other items were commented in a header).
       
   368 // ---------------------------------------------------------
       
   369 //
       
   370 void CFMRadioFrequencyNumber::SetDigitType(TDigitType aDigitType, const CFont& aFont)
       
   371 	{
       
   372 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::SetDigitType") ) );
       
   373 	TInt value = Value();
       
   374 	iDigitType = aDigitType;
       
   375 	SetValue(value, aFont);
       
   376 	}
       
   377 
       
   378 // ---------------------------------------------------------
       
   379 // CFMRadioFrequencyNumber::DigitType
       
   380 // ?implementation_description
       
   381 // (other items were commented in a header).
       
   382 // ---------------------------------------------------------
       
   383 //
       
   384 TDigitType CFMRadioFrequencyNumber::DigitType() const
       
   385 	{
       
   386 	FTRACE( FPrint( _L(" *** FM Radio -- CFMRadioFrequencyNumber::DigitType") ) );
       
   387 	return iDigitType;
       
   388 	}
       
   389 
       
   390 //  End of File