uifw/AvKon/src/AknQueryValueNumber.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     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 *
       
    16 */
       
    17 
       
    18 // AknQueryValueNumber.cpp
       
    19 //
       
    20 // Copyright (c) 2001 Symbian Ltd.  All rights reserved.
       
    21 //
       
    22 
       
    23 #include "eikenv.h"
       
    24 
       
    25 #include <eikmfne.h>
       
    26 #include <avkon.hrh>
       
    27 #include <avkon.rsg>
       
    28 #include "AknQueryValueNumber.h"
       
    29 #include "AknQueryDialog.h"
       
    30 
       
    31 #include "AknPanic.h"
       
    32 
       
    33 //-------------------------------------------------
       
    34 // class CAknQueryValueNumber
       
    35 //-------------------------------------------------
       
    36 
       
    37 /**
       
    38  * First stage of two stage construction.
       
    39  */
       
    40 EXPORT_C CAknQueryValueNumber* CAknQueryValueNumber::NewL()
       
    41 	{
       
    42 	CAknQueryValueNumber* self = NewLC();
       
    43 	CleanupStack::Pop();
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 /**
       
    48  * First stage of two stage construction.
       
    49  */
       
    50 EXPORT_C CAknQueryValueNumber* CAknQueryValueNumber::NewLC()
       
    51 	{
       
    52 	CAknQueryValueNumber* self = new(ELeave) CAknQueryValueNumber;
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 /**
       
    59  * Destructor.
       
    60  */
       
    61 EXPORT_C CAknQueryValueNumber::~CAknQueryValueNumber()
       
    62 	{
       
    63 	}
       
    64 
       
    65 /**
       
    66  * Set the array.
       
    67  * 
       
    68  * @param aArray pointer to array, ownership is not passed
       
    69  */
       
    70 EXPORT_C void CAknQueryValueNumber::SetArrayL(const CAknQueryValueNumberArray* aArray)
       
    71 	{
       
    72 	iArray = aArray;
       
    73 	}
       
    74 
       
    75 /**
       
    76  * Set the string used for the query caption to be a string other than the default.
       
    77  *
       
    78  * @param aResourceId		resource id of string to use for caption.
       
    79  *
       
    80  */
       
    81 EXPORT_C void CAknQueryValueNumber::SetQueryCaption(TInt aResourceId)
       
    82 	{
       
    83 	iQueryCaptionId = aResourceId;
       
    84 	}
       
    85 
       
    86 /**
       
    87  * Return the current value, which may have been set by the user
       
    88  *
       
    89  * @return The current value
       
    90  *
       
    91  */
       
    92 EXPORT_C TInt CAknQueryValueNumber::Value() const
       
    93 	{
       
    94 	return iNumber;
       
    95 	}
       
    96 
       
    97 /**
       
    98  * Returns the array as a descriptor array
       
    99  * 
       
   100  * @return descriptor array, ownership is not passed
       
   101  */
       
   102 EXPORT_C const MDesCArray* CAknQueryValueNumber::MdcArray() const
       
   103 	{
       
   104 	return iArray;
       
   105 	}
       
   106 
       
   107 /**
       
   108  * Returns the current value as text
       
   109  * 
       
   110  * @return	descriptor representing current value, new 
       
   111  * descriptor is created and left on cleanup stack, ownership passed back to client
       
   112  */
       
   113 EXPORT_C HBufC* CAknQueryValueNumber::CurrentValueTextLC()
       
   114 	{
       
   115 	HBufC* buf = HBufC::NewMaxLC(iArray->FormattedStringSize());
       
   116 	TPtr ptr = buf->Des();
       
   117 	ptr.Format(*(iArray->FormatString()), iNumber);
       
   118     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( ptr );
       
   119 	return buf;
       
   120 	}
       
   121 
       
   122 /**
       
   123  * Returns the index in the array of the current value. 
       
   124  * If there are duplicates, returns the index of the first match.
       
   125  * If there are no matches, returns zero;
       
   126  *
       
   127  * @return index in array of current value
       
   128  */
       
   129 EXPORT_C TInt CAknQueryValueNumber::CurrentValueIndex() const
       
   130 	{
       
   131 	return iCurrentIndex;
       
   132 	}
       
   133 
       
   134 /**
       
   135  * Changes the current value to correspond to a value in the array.
       
   136  *
       
   137  * @param aIndex	index in array of value to set as current
       
   138  */
       
   139 EXPORT_C void CAknQueryValueNumber::SetCurrentValueIndex(const TInt aIndex)
       
   140 	{
       
   141 	iCurrentIndex = aIndex;
       
   142 	const CAknQueryValueNumberArray::NumberArray* numberArray = iArray->Array();
       
   143 	if (IsValidIndex(iCurrentIndex))
       
   144 	    iNumber = (*numberArray)[aIndex];
       
   145 	}
       
   146 
       
   147 /**
       
   148  * Creates a dialog containing a query control. If the value is edited and the Dialog
       
   149  * OK'd, the new value will be set as the current value. Otherwise the current value 
       
   150  * remains unchanged.
       
   151  *
       
   152  * @return	ETrue if current value was altered; EFalse otherwise
       
   153  */
       
   154 EXPORT_C TBool CAknQueryValueNumber::CreateEditorL()
       
   155 	{
       
   156 	TBool result = EFalse;
       
   157 
       
   158 	HBufC* queryString = CEikonEnv::Static()->AllocReadResourceLC(iQueryCaptionId);
       
   159 	CAknNumberQueryDialog* dlg = CAknNumberQueryDialog::NewL(iNumber);
       
   160 	TInt keyPressed = dlg->ExecuteLD(R_AVKON_DIALOG_QUERY_VALUE_NUMBER,*queryString);
       
   161 	CleanupStack::PopAndDestroy(); // queryString
       
   162 	
       
   163 	switch(keyPressed)
       
   164 		{
       
   165 	case EAknSoftkeyOk:
       
   166 		result = ETrue;
       
   167 		if ( iFlags.IsSet( EAutoAppendBitIndex ) )
       
   168 			{
       
   169 			AppendValueIfNewL();
       
   170 			}
       
   171 		CalculateCurrentIndex();
       
   172 		break;
       
   173 	case EAknSoftkeyCancel:
       
   174 		// fall through
       
   175 	case EAknSoftkeyBack:
       
   176 		// fall through
       
   177 	default:
       
   178 		result = EFalse;
       
   179 		break;
       
   180 		}
       
   181 
       
   182 	return result;
       
   183 	}
       
   184 
       
   185 /**
       
   186  * Two stage construction
       
   187  *
       
   188  */
       
   189 void CAknQueryValueNumber::ConstructL()
       
   190 	{
       
   191 	iQueryCaptionId = R_AVKON_TBUF_POPUP_FIELD_DEFAULT_NUMBER_QUERY_PROMPT;
       
   192 	}
       
   193 
       
   194 /**
       
   195  * Constructor.
       
   196  *
       
   197  */
       
   198 CAknQueryValueNumber::CAknQueryValueNumber()
       
   199 	{
       
   200 	}
       
   201 
       
   202 /**
       
   203  * Search for the current value in the list, and set the curent index 
       
   204  * to be the found index. If not found, set current index to 1 after the size of the array.
       
   205  *
       
   206  */
       
   207 void CAknQueryValueNumber::CalculateCurrentIndex()
       
   208 	{
       
   209 	// ensure that if the new value is not in the list, the current index is set to the beginning
       
   210 	TInt index = 0; 
       
   211 	const CAknQueryValueNumberArray::NumberArray* numberArray = iArray->Array();
       
   212 	TInt length = numberArray->Count();
       
   213 	// search through array to find match for current value
       
   214 	TBool match = EFalse;
       
   215 	for(TInt i = 0; i < length; i++)
       
   216 		{
       
   217 		if((*numberArray)[i] == iNumber)
       
   218 			{
       
   219 			index = i;
       
   220 			match = ETrue;
       
   221 			break;
       
   222 			}
       
   223 		}
       
   224 	if(!match)
       
   225 		index = length;
       
   226 	iCurrentIndex = index;
       
   227 	}
       
   228 
       
   229 /**
       
   230  * Append the current value if it is new
       
   231  *
       
   232  *
       
   233  */
       
   234 void CAknQueryValueNumber::AppendValueIfNewL()
       
   235 	{
       
   236 	const CAknQueryValueNumberArray::NumberArray* array = iArray->Array();
       
   237 	CalculateCurrentIndex();
       
   238 	if ( iCurrentIndex == array->Count() ) 
       
   239 		{
       
   240 		CAknQueryValueNumberArray::NumberArray* array = iArray->Array();
       
   241 		array->AppendL( iNumber );
       
   242 		}
       
   243 	}
       
   244 
       
   245 EXPORT_C void CAknQueryValueNumber::Reserved_MAknQueryValue()
       
   246 	{
       
   247 
       
   248 	};
       
   249 
       
   250 
       
   251 //-------------------------------------------------
       
   252 // class CAknQueryValueNumberArray
       
   253 //-------------------------------------------------
       
   254 
       
   255 /**
       
   256  * NewL.
       
   257  *
       
   258  * @param aResourceId	Recource id of a TBUF containing a number format string. Client
       
   259  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_NUMBER_FORMAT
       
   260  *							if desired, or provide its own resource. 
       
   261  */
       
   262 EXPORT_C CAknQueryValueNumberArray* CAknQueryValueNumberArray::NewL(TInt aResourceId)
       
   263 	{
       
   264 	CAknQueryValueNumberArray* self = NewLC(aResourceId);
       
   265 	CleanupStack::Pop();
       
   266 	return self;
       
   267 	}
       
   268 
       
   269 /**
       
   270  * NewLC.
       
   271  *
       
   272  * @param aResourceId	Recource id of a TBUF containing a number format string. Client
       
   273  *							can use R_AVKON_TBUF_POPUP_FIELD_DEFAULT_NUMBER_FORMAT
       
   274  *							if desired, or provide its own resource. 
       
   275  */
       
   276 EXPORT_C CAknQueryValueNumberArray* CAknQueryValueNumberArray::NewLC(TInt aResourceId)
       
   277 	{
       
   278 	CAknQueryValueNumberArray* self = new(ELeave) CAknQueryValueNumberArray;
       
   279 	CleanupStack::PushL(self);
       
   280 	self->ConstructL(aResourceId);
       
   281 	return self;
       
   282 	}
       
   283 
       
   284 /**
       
   285  * Destructor.
       
   286  */
       
   287 EXPORT_C CAknQueryValueNumberArray::~CAknQueryValueNumberArray()
       
   288 	{
       
   289 	delete iLastGeneratedTextValue;
       
   290 	delete iFormatString;
       
   291 	}
       
   292 
       
   293 /**
       
   294  * Set the array of values. 
       
   295  * Note that client can use any implementation of array class, but must pass in a 
       
   296  * TArray generated from it (by calling the Array() method on the array class)
       
   297  *
       
   298  * @param array of values, ownership is not passed
       
   299  */
       
   300 EXPORT_C void CAknQueryValueNumberArray::SetArray(NumberArray& aArray)
       
   301 	{
       
   302 	iArray = &aArray;
       
   303 	}
       
   304 
       
   305 /**
       
   306  * Get the array of values as a TArray. 
       
   307  * Note that client can use any implementation of array class, but the array is 
       
   308  * treated as a TArray.
       
   309  *
       
   310  * @return array of values, ownership is not passed
       
   311  */
       
   312 EXPORT_C CAknQueryValueNumberArray::NumberArray* CAknQueryValueNumberArray::Array() const
       
   313 	{
       
   314 	return iArray;
       
   315 	}
       
   316 
       
   317 /**
       
   318  * Return the TTime format string that is used to generate MDesCArray values
       
   319  *
       
   320  * @return format string
       
   321  */
       
   322 EXPORT_C const HBufC* CAknQueryValueNumberArray::FormatString() const
       
   323 	{
       
   324 	return iFormatString;
       
   325 	}
       
   326 
       
   327 /**
       
   328  * Return the size of a maximal time string formated using the format string 
       
   329  * that was supplied during construction of this instance.
       
   330  * Two versions to eliminate compiler warnings.
       
   331  *
       
   332  * @return length of formatted string.
       
   333  */
       
   334 #ifdef __WINS__
       
   335 EXPORT_C const TInt CAknQueryValueNumberArray::FormattedStringSize() const
       
   336 	{
       
   337 	return iFormattedStringSize;
       
   338 	}
       
   339 #else
       
   340 EXPORT_C TInt CAknQueryValueNumberArray::FormattedStringSize() const
       
   341 	{
       
   342 	return iFormattedStringSize;
       
   343 	}
       
   344 #endif // __WINS__
       
   345 
       
   346 
       
   347 /**
       
   348  * Reports count of contained array
       
   349  *
       
   350  * @return count of contained array
       
   351  */
       
   352 EXPORT_C TInt CAknQueryValueNumberArray::MdcaCount() const
       
   353 	{
       
   354 	return iArray->Count();
       
   355 	}
       
   356 
       
   357 /**
       
   358  * Returns array element, converting value to text. 
       
   359  * <p> WARNING: the returned pointer is only valid until the next time this
       
   360  * method is invoked. 
       
   361  *
       
   362  * @param index of element to return
       
   363  * @return descriptor representing array element, ownership is not passed
       
   364  */
       
   365 EXPORT_C TPtrC CAknQueryValueNumberArray::MdcaPoint(TInt aIndex) const
       
   366 	{
       
   367 	// only storing the last used string saves memory by avoiding keeping an array 
       
   368 	// of descriptors in memory. 
       
   369 	TInt number = (*iArray)[aIndex];
       
   370 	TPtr textPtr = iLastGeneratedTextValue->Des();
       
   371 	TPtr formatPtr = iFormatString->Des();
       
   372 	textPtr.Format(formatPtr, number);
       
   373     AknTextUtils::DisplayTextLanguageSpecificNumberConversion( textPtr );
       
   374     return textPtr;
       
   375 	}
       
   376 
       
   377 /**
       
   378  * Constructor.
       
   379  */
       
   380 CAknQueryValueNumberArray::CAknQueryValueNumberArray()
       
   381 	{
       
   382 
       
   383 	}
       
   384 
       
   385 /**
       
   386  * ConstructL
       
   387  *
       
   388  * @param aResourceId	id of a resource containing a time format string
       
   389  */
       
   390 void CAknQueryValueNumberArray::ConstructL(TInt aResourceId)
       
   391 	{
       
   392 	iFormatString = CEikonEnv::Static()->AllocReadResourceL(aResourceId);
       
   393 	iFormattedStringSize = KSafeSizeOfDescriptorForNumberFormat;
       
   394 	iLastGeneratedTextValue = HBufC::NewL(iFormattedStringSize);
       
   395 	}
       
   396 
       
   397 // End of File