uifw/EikStd/coctlsrc/eiklbd.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 1998 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 
       
    19 
       
    20 #include <eiklbx.h>     // to call static function ListBoxLaf()
       
    21 #include <eiklbd.h>
       
    22 #include <gdi.h>
       
    23 #include <coemain.h>
       
    24 #include <eikenv.h>
       
    25 #include <eiklbi.h>
       
    26 #include "laflbd.h"
       
    27 
       
    28 #include "akntrace.h"
       
    29 
       
    30 class MAknsControlContext;
       
    31 //
       
    32 // class CListBoxDataExtension
       
    33 // (private class -- put all interface to CListBoxData instead)
       
    34 // 
       
    35 
       
    36 NONSHARABLE_CLASS(CListBoxDataExtension) : public CBase
       
    37 	{
       
    38 public:
       
    39 	void ConstructL();
       
    40 	~CListBoxDataExtension();
       
    41 public:
       
    42 	MAknsControlContext* iSkinControlContext;
       
    43 	TBool iSkinEnabled;
       
    44 	};
       
    45 
       
    46 void CListBoxDataExtension::ConstructL()
       
    47 	{
       
    48 	}
       
    49 CListBoxDataExtension::~CListBoxDataExtension()
       
    50 	{
       
    51 	}
       
    52 
       
    53 
       
    54 ///
       
    55 /// class TListBoxBoundValues
       
    56 ///
       
    57 
       
    58 EXPORT_C TListFontBoundValues::TListFontBoundValues()
       
    59 	: iMaxNormalCharWidthInPixels(0), iHeightInPixels(0)
       
    60 	{
       
    61 	}
       
    62 
       
    63 ///
       
    64 /// class CListBoxData
       
    65 ///
       
    66 
       
    67 EXPORT_C CListBoxData::CListBoxData()
       
    68 	: iAlign(CGraphicsContext::ELeft)
       
    69     {}
       
    70 
       
    71 EXPORT_C CListBoxData::~CListBoxData()
       
    72     {
       
    73 	delete iSearchString;
       
    74 	delete iLBDExtension;
       
    75     }
       
    76 
       
    77 EXPORT_C MAknsControlContext *CListBoxData::SkinBackgroundControlContext() const
       
    78 	{
       
    79 	if (iLBDExtension && iLBDExtension->iSkinEnabled)
       
    80 		return iLBDExtension->iSkinControlContext;
       
    81 	return NULL;
       
    82 	}
       
    83 
       
    84 EXPORT_C void CListBoxData::SetSkinBackgroundControlContextL(MAknsControlContext *aContext)
       
    85 	{
       
    86 	if (iLBDExtension)
       
    87 		iLBDExtension->iSkinControlContext = aContext;
       
    88 	}
       
    89 
       
    90 EXPORT_C void CListBoxData::SetSkinEnabledL(TBool aEnabled)
       
    91 	{
       
    92 	if (iLBDExtension)
       
    93 		iLBDExtension->iSkinEnabled = aEnabled;
       
    94 	}
       
    95 
       
    96 EXPORT_C void CListBoxData::ConstructL(const CFont* /* aBaseFont */)
       
    97     {
       
    98 	iLBDExtension = new(ELeave)CListBoxDataExtension;
       
    99 	iLBDExtension->ConstructL();
       
   100     }
       
   101 
       
   102 EXPORT_C CFont* CListBoxData::Font(const TListItemProperties& aItemProperties) const
       
   103     {
       
   104     CArrayPtr<CFont>* fonts=NULL;
       
   105     if ((aItemProperties.IsBold()) && (aItemProperties.IsItalics()))
       
   106         {
       
   107         fonts=iBoldItalicFont.iFonts;
       
   108         }
       
   109     else if (aItemProperties.IsBold())
       
   110         {
       
   111         fonts=iBoldFont.iFonts;
       
   112         }
       
   113     else if (aItemProperties.IsItalics())
       
   114         {
       
   115         fonts=iItalicFont.iFonts;
       
   116         }
       
   117     else 
       
   118 		fonts=iNormalFont.iFonts;
       
   119     if (!fonts || fonts->Count()==0)
       
   120 		return NULL;
       
   121     return fonts->At(0);
       
   122     }
       
   123 
       
   124 EXPORT_C
       
   125 void CListBoxData::SetupGc( const TListItemProperties& aItemProperties,
       
   126                             CWindowGc& aGc) const
       
   127     {
       
   128     aGc.SetUnderlineStyle(aItemProperties.IsUnderlined() ? EUnderlineOn : EUnderlineOff);
       
   129     aGc.SetPenColor(aItemProperties.Color());
       
   130     CFont *font=Font(aItemProperties);
       
   131     if(font)
       
   132         {
       
   133         aGc.UseFont(font);
       
   134         }
       
   135     }
       
   136 
       
   137 EXPORT_C const TListFontBoundValues& CListBoxData::FontBoundValues() const
       
   138     {
       
   139     return iFontBoundValues;
       
   140     }
       
   141 
       
   142 EXPORT_C TInt CListBoxData::SetFontHeight(TInt aFontHeightInTwips)
       
   143     {
       
   144     TListFontBoundValues newValues;
       
   145     iFontBoundValues=newValues;
       
   146 	TInt retVal = KErrNone;
       
   147     retVal = RegenerateFonts(iNormalFont, aFontHeightInTwips);
       
   148 	if(retVal!=KErrNone)
       
   149 		return retVal;
       
   150     retVal = RegenerateFonts(iBoldFont, aFontHeightInTwips);
       
   151 	if(retVal!=KErrNone)
       
   152 		return retVal;
       
   153     retVal = RegenerateFonts(iItalicFont, aFontHeightInTwips);
       
   154 	if(retVal!=KErrNone)
       
   155 		return retVal;
       
   156     retVal = RegenerateFonts(iBoldItalicFont, aFontHeightInTwips);
       
   157 	return retVal;
       
   158     }
       
   159 
       
   160 EXPORT_C TInt CListBoxData::RegenerateFonts(CFontsWithStyle& aFonts, TInt aFontHeightInTwips)
       
   161     {
       
   162 	TInt retVal=KErrNone;
       
   163     if (aFonts.iFonts)
       
   164         {
       
   165 		CGraphicsDevice* screenDevice=CCoeEnv::Static()->ScreenDevice();
       
   166         for (TInt ii=0 ; ii < aFonts.iFonts->Count(); ii++)
       
   167             {
       
   168             CFont* prevFont=aFonts.iFonts->At(ii);
       
   169             TFontSpec fontSpec=prevFont->FontSpecInTwips();
       
   170             fontSpec.iFontStyle=aFonts.FontStyle();
       
   171             fontSpec.iHeight=aFontHeightInTwips;
       
   172             CFont* font;
       
   173             retVal=screenDevice->GetNearestFontInTwips(font,fontSpec);
       
   174 			if(retVal!=KErrNone)
       
   175 				break;
       
   176 			screenDevice->ReleaseFont(prevFont);
       
   177             aFonts.iFonts->At(ii)=font;
       
   178             UpdateFontBoundValues(*font);
       
   179             }
       
   180         }
       
   181 	return retVal;
       
   182     }
       
   183 
       
   184 EXPORT_C void CListBoxData::ConstructFontL(const CFont* aBaseFont, TFontStyle aFontStyle, CFontsWithStyle& aFonts)
       
   185     {
       
   186     // Notice! CColumnListBoxData::AddActualFontL function assumes, that
       
   187     // this function can leave only in the creation of an array or
       
   188     // in appending an item into array.If this fact changes. AddActualFontL
       
   189     // has to be rewritten.
       
   190     if (!aFonts.iFonts)
       
   191         {
       
   192         aFonts.iFonts=new (ELeave) CArrayPtrFlat<CFont> (1);
       
   193         }
       
   194     CFont* font;
       
   195     TFontSpec fontSpec=aBaseFont->FontSpecInTwips();
       
   196     fontSpec.iFontStyle=aFontStyle;
       
   197     User::LeaveIfError(CCoeEnv::Static()->ScreenDevice()->GetNearestFontInTwips(font,fontSpec));
       
   198     UpdateFontBoundValues(*font);
       
   199     aFonts.iFonts->AppendL(font);
       
   200     aFonts.SetFontStyle(aFontStyle);
       
   201     }
       
   202 
       
   203 EXPORT_C void CListBoxData::UpdateFontBoundValues(const CFont& aFont)
       
   204     {
       
   205     if (iFontBoundValues.iMaxNormalCharWidthInPixels<aFont.MaxNormalCharWidthInPixels())
       
   206         {
       
   207         iFontBoundValues.iMaxNormalCharWidthInPixels=aFont.MaxNormalCharWidthInPixels();
       
   208         }
       
   209     if (iFontBoundValues.iHeightInPixels<aFont.HeightInPixels())
       
   210         {
       
   211         iFontBoundValues.iHeightInPixels=aFont.HeightInPixels();
       
   212         }
       
   213     }
       
   214 
       
   215 EXPORT_C void CListBoxData::Reserved_1()
       
   216     {
       
   217     }
       
   218 
       
   219 EXPORT_C void CListBoxData::Reserved_2()
       
   220     {
       
   221     }
       
   222 
       
   223 //
       
   224 // Class CListBoxData::CFontsWithStyle
       
   225 //
       
   226 
       
   227 CListBoxData::CFontsWithStyle::CFontsWithStyle()
       
   228                 : iFonts (NULL)
       
   229     {
       
   230     }
       
   231 
       
   232 CListBoxData::CFontsWithStyle::~CFontsWithStyle()
       
   233     {
       
   234 	ReleaseFonts();
       
   235 	delete iFonts;
       
   236     }
       
   237 
       
   238 EXPORT_C void CListBoxData::CFontsWithStyle::ReleaseFonts()
       
   239     {
       
   240     if (iFonts)
       
   241         {
       
   242         for (TInt ii=0 ; ii < iFonts->Count(); ii++)
       
   243             {
       
   244             CCoeEnv::Static()->ScreenDevice()->ReleaseFont(iFonts->At(ii));
       
   245             }
       
   246         }
       
   247     }
       
   248 
       
   249 EXPORT_C TFontStyle CListBoxData::CFontsWithStyle::FontStyle()
       
   250 	{
       
   251 	return iFontStyle;
       
   252 	}
       
   253 
       
   254 
       
   255 EXPORT_C void CListBoxData::CFontsWithStyle::SetFontStyle(TFontStyle aFontStyle)
       
   256 	{
       
   257 	iFontStyle=aFontStyle;
       
   258 	}
       
   259 
       
   260 /**
       
   261  * Set search string for search result viewing.
       
   262  */
       
   263 EXPORT_C void CListBoxData::SetSearchStringL(const TDesC* aSearchString)
       
   264 	{
       
   265 	_AKNTRACE_FUNC_ENTER;
       
   266 	if (aSearchString==NULL)
       
   267 		{
       
   268 		delete iSearchString;
       
   269 		iSearchString = NULL;
       
   270 		_AKNTRACE_FUNC_EXIT;
       
   271 		return;
       
   272 		}
       
   273 	TInt length = aSearchString->Length();
       
   274 	if (iSearchString)
       
   275 		{
       
   276 		if (length>iSearchString->Length())
       
   277 			{
       
   278 			iSearchString = iSearchString->ReAllocL(length);
       
   279 			}
       
   280 		}
       
   281 	else
       
   282 		{
       
   283 		iSearchString = HBufC::NewL(length);
       
   284 		}
       
   285 	*iSearchString = *aSearchString;
       
   286 	_AKNTRACE_FUNC_EXIT;
       
   287 	}
       
   288 
       
   289 /**
       
   290  * Returns ETrue if a search string with a non-zero length has been set for this list box.
       
   291  */
       
   292 EXPORT_C TBool CListBoxData::IsSearchString() const
       
   293 	{
       
   294 	return (iSearchString && iSearchString->Length());
       
   295 	}
       
   296 
       
   297 
       
   298 /**
       
   299  * Font Draws list item text so that search string is drawed as bold underlined.
       
   300  */
       
   301 EXPORT_C void CListBoxData::DrawItem(CWindowGc& aGc, const TRect& aItemTextRect,const TDesC& aItemText,
       
   302 								const CFont& aItemFont,const TInt aBaseLineOffset,const CGraphicsContext::TTextAlign aAlign) const
       
   303 	{
       
   304 	_AKNTRACE_FUNC_ENTER;
       
   305 	const TInt strPos=aItemText.FindF(*iSearchString);
       
   306 	const TInt strLen = iSearchString->Length();
       
   307 
       
   308 	if (strPos==KErrNotFound)	// if there's no search string treat as normal
       
   309 		{
       
   310 		/*Call to the laf code which is responsible for drawing a normal text string in a rectangle*/
       
   311 		LafTextListItemDrawer::DrawNormalText(aItemText,aItemFont,aGc,aItemTextRect,aBaseLineOffset,aAlign);
       
   312 		}
       
   313 	else 
       
   314 		{
       
   315 		const CFont& boldFont=iBoldFont.iFonts?*(iBoldFont.iFonts->At(0)):aItemFont;
       
   316 		/*Call to the laf code which is responsible for drawing a search text string in a rectangle*/
       
   317 		LafListBoxData::DrawSearchText(aItemTextRect,aGc,aItemFont,boldFont,
       
   318 										aItemText,strPos,strLen,aBaseLineOffset,aAlign);
       
   319 		}
       
   320 	_AKNTRACE_FUNC_EXIT;
       
   321 	}