lafagnosticuifoundation/uigraphicsutils/gulsrc/GULCOLOR.CPP
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <s32std.h>
       
    17 #include <s32stor.h>
       
    18 #include <s32strm.h>
       
    19 #include <gulcolor.h>
       
    20 #include <gulpanic.h>
       
    21 #include "GULSTD.H"
       
    22 
       
    23 const TUint KEikCustomColorsArrayValue=0x100057c2; // this constant is properly defined in Eikenv.h
       
    24 
       
    25 //
       
    26 // class CColorList
       
    27 //
       
    28 
       
    29 inline CColorList::TAppColorList::TAppColorList(TUid aApp, CColorArray* aColorArray)
       
    30 	:iApp(aApp), iColorArray(aColorArray)
       
    31 	{}
       
    32 
       
    33 const TInt KGulColorGranularity=1;
       
    34 const TInt KGulColListVer=1;
       
    35 
       
    36 EXPORT_C CColorList* CColorList::NewLC()
       
    37 /** Allocates and constructs a new colour list and puts the pointer 
       
    38 to the new colour list object onto the cleanup stack.
       
    39 
       
    40 @return The colour list. */
       
    41 	{ // static
       
    42 	CArrayFix<TRgb>* colors=new(ELeave) CArrayFixFlat<TRgb>(4);
       
    43 	CleanupStack::PushL(colors);
       
    44 	CColorList* self=new(ELeave) CColorList(colors);
       
    45 	CleanupStack::Pop(); // colors;
       
    46 	CleanupStack::PushL(self);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 EXPORT_C CColorList* CColorList::NewL(CArrayFix<TRgb>* aColors)
       
    51 /** Allocates and constructs a new colour list.
       
    52 
       
    53 @param aColors The array of physical colours to store in the list. 
       
    54 The colour list takes ownership.
       
    55 @return The colour list. */
       
    56 	{ // static
       
    57 	__ASSERT_ALWAYS(aColors,Panic(EEgulPanicNullPointer));
       
    58 	CColorList* self=new(ELeave) CColorList(aColors);
       
    59 	return self;
       
    60 	}
       
    61 
       
    62 CColorList::CColorList(CArrayFix<TRgb>* aColors)
       
    63 	: iEikColors(aColors)
       
    64 	{}
       
    65 
       
    66 EXPORT_C CColorList::~CColorList()
       
    67 /** Destructor. */
       
    68 	{
       
    69 	delete iEikColors;
       
    70 	if (iAppColors)
       
    71 		{
       
    72 		const TInt count=iAppColors->Count();
       
    73 		for (TInt ii=0;ii<count;ii++)
       
    74 			delete (*iAppColors)[ii].iColorArray;
       
    75 		delete iAppColors;
       
    76 		}
       
    77 	}
       
    78 
       
    79 EXPORT_C TRgb CColorList::Color(TLogicalColor aColor) const
       
    80 /** Gets the physical colour (TRgb) equivalent of a logical colour.
       
    81 
       
    82 @param aColor Logical colour value.
       
    83 @return The physical colour equivalent of aColor.
       
    84 @panic EGUL 3 The logical color specified is not found in the colour list. */
       
    85 	{
       
    86 	if ((TInt)aColor<0 || (TInt)aColor>=iEikColors->Count())
       
    87 		{
       
    88 		Panic(EEgulPanicLogicalColorNotFound);
       
    89 		}
       
    90 	return (*iEikColors)[aColor];
       
    91 	}
       
    92 
       
    93 /** Gets the number of colors in the list.
       
    94 
       
    95 @return The number of colors in the list
       
    96 */
       
    97 EXPORT_C TInt CColorList::Count() const
       
    98 	{
       
    99 	return iEikColors->Count();
       
   100 	}
       
   101 
       
   102 EXPORT_C TRgb CColorList::Color(TUid aApp, TInt aColor) const
       
   103 /** Gets the physical colour (TRgb) equivalent of a logical colour from 
       
   104 the specified application's colour array.
       
   105 
       
   106 @param aApp The UID of the application.
       
   107 @param aColor A logical colour supported by the application.
       
   108 @return The physical colour equivalent of the logical colour.
       
   109 @panic EGUL 6 The specified application does not have an entry in the colour list. */
       
   110 	{
       
   111 	const TInt pos=Find(aApp);
       
   112 	if (pos==KErrNotFound)
       
   113 		{
       
   114 		Panic(EEgulPanicAppColorArrayNotFound);
       
   115 		}
       
   116 	return (*iAppColors)[pos].iColorArray->Color(aColor);
       
   117 	}
       
   118 
       
   119 EXPORT_C CColorArray* CColorList::ColorArray(TUid aApp) const
       
   120 /** Gets an application's colour array.
       
   121 
       
   122 @param aApp The UID of the application.
       
   123 @return The specified application's colour array. The caller does not take ownership.
       
   124 @panic EGUL 6 The specified application does not have an entry in the colour list. */
       
   125 	{
       
   126 	TInt pos=Find(aApp);
       
   127 	__ASSERT_ALWAYS(pos!=KErrNotFound,Panic(EEgulPanicAppColorArrayNotFound));
       
   128 	return (*iAppColors)[pos].iColorArray;
       
   129 	}
       
   130 
       
   131 EXPORT_C TBool CColorList::ContainsColorArray(TUid aApp) const
       
   132 /** Tests whether the specified application has an entry in the colour list.
       
   133 
       
   134 @param aApp The UID of the application.
       
   135 @return True if a colour array is found for the application, false if not. */
       
   136 	{
       
   137 	return (Find(aApp)!=KErrNotFound);
       
   138 	}
       
   139 
       
   140 EXPORT_C void CColorList::SetColor(TLogicalColor aLogicalColor, TRgb aColor)
       
   141 /** Sets the TRgb colour value that a logical colour maps to.
       
   142 
       
   143 @param aLogicalColor The logical colour that needs to be set.
       
   144 @param aColor The TRgb colour value. */
       
   145 	{
       
   146 	(*iEikColors)[aLogicalColor]=aColor;
       
   147 	}
       
   148 
       
   149 EXPORT_C void CColorList::AddColorArrayL(TUid aApp, CColorArray* aArray)
       
   150 /** Creates a new entry in the colour list with the specified  application UID 
       
   151 and its colour array.
       
   152 
       
   153 @param aApp The UID of the application.
       
   154 @param aArray The application's colour array. The colour list takes ownership. */
       
   155 	{
       
   156 	__ASSERT_DEBUG(aArray,User::Invariant());
       
   157 	if (!iAppColors)
       
   158 		iAppColors=new(ELeave) CArrayFixFlat<TAppColorList>(KGulColorGranularity);
       
   159 	iAppColors->AppendL(TAppColorList(aApp,aArray));
       
   160 	}
       
   161 
       
   162 EXPORT_C void CColorList::DeleteColorArray(TUid aApp)
       
   163 /** Deletes the entry in the colour list for the specified application, and 
       
   164 deletes the application's colour array.
       
   165 
       
   166 @param aApp The UID of the application whose entry is deleted from the colour list.
       
   167 @panic EGUL 6 The specified application does not have an entry in the colour list. */
       
   168 	{
       
   169 	TInt pos=Find(aApp);
       
   170 	__ASSERT_ALWAYS(pos!=KErrNotFound,Panic(EEgulPanicAppColorArrayNotFound));
       
   171 	delete (*iAppColors)[pos].iColorArray;
       
   172 	iAppColors->Delete(pos);
       
   173 	}
       
   174 
       
   175 EXPORT_C void CColorList::InternalizeL(RReadStream& aStream)
       
   176 /** Internalises the colour list from a read stream.
       
   177 
       
   178 @param aStream Stream from which the object should be internalised.*/
       
   179 	{
       
   180 	TCardinality version;
       
   181 	aStream>>version; // ignore version number for now
       
   182 	// read in eik colors
       
   183 	if (iEikColors)
       
   184 		iEikColors->Reset();
       
   185 	else
       
   186 		iEikColors=new(ELeave) CArrayFixFlat<TRgb>(4);
       
   187 	TCardinality card;
       
   188 	aStream>>card;
       
   189 	const TInt count(card);
       
   190 	TRgb rgb;
       
   191 	for (TInt ii=0;ii<count;ii++)
       
   192 		{
       
   193 		aStream>>rgb;
       
   194 		iEikColors->AppendL(rgb);
       
   195 		}
       
   196 	// then the other arrays, one at a time
       
   197 	if (!iAppColors)
       
   198 		iAppColors=new(ELeave) CArrayFixFlat<TAppColorList>(KGulColorGranularity);
       
   199 	else
       
   200 		{
       
   201 		const TInt colCount=iAppColors->Count();
       
   202 		for (TInt jj=0;jj<colCount;jj++)
       
   203 			delete (*iAppColors)[jj].iColorArray;
       
   204 		iAppColors->Reset();
       
   205 		}
       
   206 	aStream>>card;
       
   207 	const TInt arrayCount(card);
       
   208 	for (TInt jj=0;jj<arrayCount;jj++)
       
   209 		{
       
   210 		TUid uid=TUid::Uid(aStream.ReadInt32L());
       
   211 		CColorArray* array=CColorArray::NewLC();
       
   212 		aStream>>*array;
       
   213 		iAppColors->AppendL(TAppColorList(uid,array));
       
   214 		CleanupStack::Pop(); //array
       
   215 		}
       
   216 	}
       
   217 
       
   218 EXPORT_C void CColorList::ExternalizeL(RWriteStream& aStream) const
       
   219 /** Externalises the colour list to a write stream.
       
   220 
       
   221 @param aStream Stream to which the object should be externalised.*/
       
   222 	{
       
   223 	TCardinality version(KGulColListVer);
       
   224 	aStream<<version;
       
   225 
       
   226 	const TInt colCount=iEikColors->Count();
       
   227 	aStream<<TCardinality(colCount);
       
   228 	for (TInt ii=0;ii<colCount;ii++)
       
   229 		{
       
   230 		aStream<<(*iEikColors)[ii];
       
   231 		}
       
   232 
       
   233 	const TInt arrayCount=(iAppColors? iAppColors->Count() : 0);
       
   234 //	aStream<<TCardinality(arrayCount);
       
   235 	TBool wroteDeviceSpecificStream=EFalse;
       
   236 	for (TInt jj=0;jj<arrayCount;jj++)
       
   237 		{
       
   238 		const TAppColorList& colList=(*iAppColors)[jj];
       
   239 		if (colList.iApp.iUid==KEikCustomColorsArrayValue)
       
   240 			{
       
   241 			aStream<<TCardinality(1);
       
   242 			aStream.WriteInt32L(colList.iApp.iUid);
       
   243 			aStream<<*(colList.iColorArray);
       
   244 			wroteDeviceSpecificStream=ETrue;
       
   245 			break;
       
   246 			}
       
   247 		}
       
   248 	if (wroteDeviceSpecificStream==EFalse)
       
   249 		{
       
   250 		aStream<<TCardinality(0);
       
   251 		}
       
   252 	}
       
   253 
       
   254 EXPORT_C void CColorList::MergeL(const CColorList& aList)
       
   255 /** Merges the specified CColorList with target object. 
       
   256 
       
   257 @param aList A reference to the colour list object, set by calling CColorList::AddColorArray() 
       
   258 */
       
   259 	{
       
   260 	//AddColorArray() should have been called
       
   261 	if (!aList.iAppColors)
       
   262 		{
       
   263 		return;
       
   264 		}
       
   265 
       
   266 	const TInt count=aList.iAppColors->Count();
       
   267 	for (TInt ii=0;ii<count;ii++)
       
   268 		{
       
   269 		CArrayFix<TAppColorList>* array=aList.iAppColors;
       
   270 		TAppColorList& appColorList=(*array)[ii];
       
   271 		TUid uid=appColorList.iApp;
       
   272 //		const TInt match=aList.Find(uid);
       
   273 		const TInt match=Find(uid);
       
   274 //		if (match!=KErrNotFound)
       
   275 		if (match==KErrNotFound)
       
   276 			{
       
   277 			CColorArray* copy=CColorArray::NewLC(*(appColorList.iColorArray));
       
   278 			AddColorArrayL(uid,copy);
       
   279 			CleanupStack::Pop(copy);
       
   280 			}
       
   281 		}
       
   282 	}
       
   283 
       
   284 TInt CColorList::Find(TUid aApp) const
       
   285 	{
       
   286 	if (!iAppColors)
       
   287 		return KErrNotFound;
       
   288 	TInt pos;
       
   289 	TKeyArrayFix key(_FOFF(TAppColorList,iApp.iUid),ECmpTInt);
       
   290 	TAppColorList appColor(aApp,NULL);
       
   291 	if (iAppColors->Find(appColor,key,pos)==KErrNone)
       
   292 		return pos;
       
   293 	return KErrNotFound;
       
   294 	}
       
   295 
       
   296 //
       
   297 // class CColorArray
       
   298 //
       
   299 
       
   300 inline CColorArray::TColor::TColor()
       
   301 	: iColor(KRgbWhite), iLogicalColor(0)
       
   302 	{}
       
   303 inline CColorArray::TColor::TColor(TRgb aColor,TInt aLogicalColor)
       
   304 	: iColor(aColor), iLogicalColor(aLogicalColor)
       
   305 	{}
       
   306 
       
   307 void CColorArray::TColor::InternalizeL(RReadStream& aStream)
       
   308 	{
       
   309 	aStream>>iColor;
       
   310 	iLogicalColor=aStream.ReadInt32L();
       
   311 	}
       
   312 
       
   313 void CColorArray::TColor::ExternalizeL(RWriteStream& aStream) const
       
   314 	{
       
   315 	aStream<<iColor;
       
   316 	aStream.WriteInt32L(iLogicalColor);
       
   317 	}
       
   318 
       
   319 EXPORT_C CColorArray* CColorArray::NewL()
       
   320 /** Creates a colour array.
       
   321 
       
   322 @return Pointer to the colour array.*/
       
   323 	{ // static
       
   324 	CColorArray* self=new(ELeave) CColorArray;
       
   325 	return self;
       
   326 	}
       
   327 
       
   328 EXPORT_C CColorArray* CColorArray::NewLC()
       
   329 /** Creates a colour array and leaves it on the cleanup stack.
       
   330 
       
   331 @return Pointer to the colour array. */
       
   332 	{ // static
       
   333 	CColorArray* self=CColorArray::NewL();
       
   334 	CleanupStack::PushL(self);
       
   335 	return self;
       
   336 	}
       
   337 
       
   338 EXPORT_C CColorArray::~CColorArray()
       
   339 /** Destructor. */
       
   340 	{
       
   341 	iColors.Reset();
       
   342 	}
       
   343 
       
   344 EXPORT_C TRgb CColorArray::Color(TInt aLogicalColor) const
       
   345 /** Gets the physical colour (TRgb) which corresponds to a logical colour.
       
   346 
       
   347 @param aLogicalColor The logical colour.
       
   348 @return The TRgb value.
       
   349 @panic EGUL 3 The logical colour specified is not found in the colour array. */
       
   350 	{
       
   351 	TInt index=Find(aLogicalColor);
       
   352 	__ASSERT_ALWAYS(index!=KErrNotFound,Panic(EEgulPanicLogicalColorNotFound));
       
   353 	return iColors[index].iColor;
       
   354 	}
       
   355 
       
   356 EXPORT_C void CColorArray::SetColor(TInt aLogicalColor,TRgb aColor)
       
   357 /** Changes a logical to physical mapping in the colour array. The specified 
       
   358 logical colour must already exist in the colour array.
       
   359 
       
   360 @param aLogicalColor The logical colour.
       
   361 @param aColor The TRgb value.
       
   362 @panic EGUL 3 The logical colour specified is not found in the colour array. */
       
   363 	{
       
   364 	TInt index=Find(aLogicalColor);
       
   365 	__ASSERT_ALWAYS(index!=KErrNotFound,Panic(EEgulPanicLogicalColorNotFound));
       
   366 	iColors[index].iColor=aColor;
       
   367 	}
       
   368 
       
   369 EXPORT_C TBool CColorArray::Contains(TInt aLogicalColor) const
       
   370 /** Tests whether the colour array contains the specified logical colour.
       
   371 
       
   372 @param aLogicalColor The logical colour.
       
   373 @return True if the logical colour is found in the colour array, false if not. */
       
   374 	{
       
   375 	return Find(aLogicalColor)!=KErrNotFound;
       
   376 	}
       
   377 
       
   378 EXPORT_C TInt CColorArray::Count() const
       
   379 /** Gets the number of logical to physical colour mappings in the array.
       
   380 
       
   381 @return The number of logical to physical colour mappings in the array. */
       
   382 	{
       
   383 	return iColors.Count();
       
   384 	}
       
   385 
       
   386 EXPORT_C void CColorArray::Reset()
       
   387 /** Resets the colour array. */
       
   388 	{
       
   389 	iColors.Reset();
       
   390 	}
       
   391 
       
   392 EXPORT_C void CColorArray::AddL(TInt aLogicalColor,TRgb aColor)
       
   393 /** Adds a logical to physical colour mapping to the colour array. 
       
   394 The specified logical colour must not already exist in the colour array.
       
   395 
       
   396 @param aLogicalColor The logical colour.
       
   397 @param aColor The TRgb colour.
       
   398 @panic EGUL 4 The logical colour specified already exists in the array. Debug builds only.*/
       
   399 	{
       
   400 	__ASSERT_DEBUG(Find(aLogicalColor)==KErrNotFound,Panic(EEgulPanicLogicalColorAlreadyExists));
       
   401 	iColors.AppendL(CColorArray::TColor(aColor,aLogicalColor));
       
   402 	}
       
   403 
       
   404 EXPORT_C void CColorArray::Remove(TInt aLogicalColor)
       
   405 /** Removes a logical to physical colour mapping from the array.
       
   406 
       
   407 @param aLogicalColor The logical colour to remove. 
       
   408 @panic EGUL 3 The logical colour specified is not found in the array. */
       
   409 	{
       
   410 	const TInt index=Find(aLogicalColor);
       
   411 	__ASSERT_ALWAYS(index!=KErrNotFound,Panic(EEgulPanicLogicalColorNotFound));
       
   412 	iColors.Delete(index);
       
   413 	}
       
   414 
       
   415 void CColorArray::InternalizeL(RReadStream& aStream)
       
   416 	{
       
   417 	iColors.Reset();
       
   418 	TCardinality card;
       
   419 	aStream>>card;
       
   420 	const TInt count(card);
       
   421 	for (TInt ii=0;ii<count;ii++)
       
   422 		{
       
   423 		TColor color;
       
   424 		aStream>>color;
       
   425 		iColors.AppendL(color);
       
   426 		}
       
   427 	}
       
   428 
       
   429 void CColorArray::ExternalizeL(RWriteStream& aStream) const
       
   430 	{
       
   431 	const TInt count=Count();
       
   432 	aStream<<TCardinality(count);
       
   433 	for (TInt ii=0;ii<count;ii++)
       
   434 		{
       
   435 		aStream<<iColors[ii];
       
   436 		}
       
   437 	}
       
   438 
       
   439 CColorArray::CColorArray()
       
   440 	: iColors(4)
       
   441 	{}
       
   442 
       
   443 TInt CColorArray::Find(TInt aLogicalColor) const
       
   444 	{
       
   445 	TInt pos;
       
   446 	TKeyArrayFix key(sizeof(TRgb),ECmpTInt);
       
   447 	TRgb junk; // never used
       
   448 	CColorArray::TColor color(junk,aLogicalColor);
       
   449 	if (iColors.Find(color,key,pos)==KErrNone)
       
   450 		return pos;
       
   451 	return KErrNotFound;
       
   452 	}
       
   453 
       
   454 CColorArray* CColorArray::NewLC(const CColorArray& aArray)
       
   455 	{ // static
       
   456 	CColorArray* self=CColorArray::NewLC();
       
   457 	const TInt count=aArray.iColors.Count();
       
   458 	for (TInt ii=0;ii<count;ii++)
       
   459 		{
       
   460 		self->iColors.AppendL(aArray.iColors[ii]);
       
   461 		}
       
   462 	return self;
       
   463 	}