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