// EIKLBI.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <eiklbi.h>
#include <eiklbm.h>
#include <badesca.h>
#include <eikpanic.h>
#include <w32std.h>		// Is this really needed?
#include <gdi.h>
#include <eikdutil.h>
#include <eiksfont.h>
#include <eikenv.h>
#include <eikcolor.h>
// class CListItemDrawer

EXPORT_C CListItemDrawer::CListItemDrawer()
    {
	iMarkColumnWidth = 10;
	iMarkGutter = 2;
	iMarkColor = KDefaultLbxMarkColor;
	CEikonEnv* eikEnv=CEikonEnv::Static();
	iBackColor = eikEnv->Color(EEikColorControlBackground); //KDefaultLbxBackColor;
//	iHighlightedBackColor = KDefaultLbxHighlightedBackColor;    
 
	iHighlightedBackColor = eikEnv->Color(EEikColorControlHighlightBackground);
	}

EXPORT_C CListItemDrawer::~CListItemDrawer()
    {
    }

EXPORT_C void CListItemDrawer::ResetGc() const
	{
	iGc->SetPenStyle(CGraphicsContext::ESolidPen);
	iGc->SetBrushStyle(CGraphicsContext::ENullBrush);
	iGc->SetBrushColor(iBackColor);
	}

EXPORT_C void CListItemDrawer::SetGc(CWindowGc* aGc)
	{
	// assert that aGc is not null
	iGc = aGc;
	}

EXPORT_C void CListItemDrawer::SetMarkColumnWidth(TInt aWidthInPixels)
	{
	iMarkColumnWidth = aWidthInPixels;
	}

EXPORT_C void CListItemDrawer::SetMarkGutter(TInt aGapInPixels)
	{
	iMarkGutter = aGapInPixels;
	}

EXPORT_C TInt CListItemDrawer::MarkColumn() const
	{
	return iMarkColumnWidth;
	}

EXPORT_C TInt CListItemDrawer::MarkGutter() const
	{
	return iMarkGutter;
	}

EXPORT_C void CListItemDrawer::SetItemCellSize(const TSize& aSizeInPixels)
	{
	iItemCellSize = aSizeInPixels;
	}

EXPORT_C void CListItemDrawer::SetViewRect(const TRect& aRect)
	{
	iViewRect = aRect;
	}

EXPORT_C CWindowGc* CListItemDrawer::Gc() const
	{
	return(iGc);
	}

EXPORT_C TRect CListItemDrawer::MatcherCursorRect(const TDesC& /*aMatchableText*/, TInt /*aCharPos*/, TInt /*aItemCellYPos*/) const
	{
	// derived classes that deal with text and want to support incremental matching in
	// listboxes need to redefine this function
	return TRect();
	}

EXPORT_C TInt CListItemDrawer::MatcherCursorAscent() const
	{
	// derived classes that deal with text and want to support incremental matching in
	// listboxes need to redefine this function
	return 0;
	}

EXPORT_C void CListItemDrawer::DrawItem(TInt aItemIndex, TPoint aItemRectPos, TBool aSelected, TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool aViewIsDimmed) const
	{
	DrawItemMark(aSelected, aViewIsDimmed, aItemRectPos);
	TRect actualItemRect(aItemRectPos, iItemCellSize);
	if (iDrawMark)
		actualItemRect.iTl.iX += (iMarkColumnWidth + iMarkGutter);
	DrawActualItem(aItemIndex, actualItemRect, aItemIsCurrent, aViewIsEmphasized, aViewIsDimmed);
	}

EXPORT_C void CListItemDrawer::DrawItemMark(TBool aSelected, TBool aViewIsDimmed, const TPoint& aItemRectPos) const
	{
	if (!iDrawMark)
		return;

	CEikonEnv* eikEnv=CEikonEnv::Static();
	TRgb tickColor = aViewIsDimmed? eikEnv->Color(EEikColorControlDimmedText): eikEnv->Color(EEikColorControlBackground);
	if (aSelected)
		tickColor = aViewIsDimmed? eikEnv->Color(EEikColorControlDimmedText) : eikEnv->Color(EEikColorControlText);
	TRect tickRect(aItemRectPos, TSize(iMarkColumnWidth, iItemCellSize.iHeight));
	TBuf<1> buf;
	buf.Append(TChar(ESymFontTick));
	iGc->UseFont(iSymbolFont);
	iGc->SetPenColor(tickColor);
	iGc->SetPenStyle(CGraphicsContext::ESolidPen);
	iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
	iGc->SetBrushColor(iBackColor);
	iGc->DrawText(buf,tickRect,tickRect.Height()-1,CGraphicsContext::ECenter);

/*
	TRgb tickColor = aSelected ? iMarkColor : iBackColor;
	ClearRect(tickRect);
	EikDrawUtils::DrawCheck(*iGc, tickRect, tickColor);
*/

	// blank out the gap between the mark and the start of the actual item
	TPoint blankRectPos(tickRect.iBr.iX, tickRect.iTl.iY);
	TSize blankRectSize(iMarkGutter, iItemCellSize.iHeight);
	TRect blankRect(blankRectPos, blankRectSize);
	ClearRect(blankRect);
	ResetGc();
	}

EXPORT_C void CListItemDrawer::ClearRect(const TRect& aRect) const
	{
	if (iGc)
		{
		iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
		iGc->SetBrushColor(iBackColor);
		iGc->SetPenStyle(CGraphicsContext::ENullPen);
		iGc->DrawRect(aRect);
		ResetGc();
		}
	}

EXPORT_C TInt CListItemDrawer::ItemWidthInPixels(TInt /*aItemIndex*/) const
	{
	return(0);
	}

EXPORT_C void CListItemDrawer::SetDrawMark(TBool aDrawMark)
	{
	iDrawMark = aDrawMark;
	}

EXPORT_C TSize CListItemDrawer::MinimumCellSize() const
	{
	return TSize(0, 0);
	}

EXPORT_C void CListItemDrawer::SetSymbolFont(const CFont* aFont)
	{
	iSymbolFont = aFont;
	}

EXPORT_C void CListItemDrawer::SetVerticalInterItemGap(TInt aGapInPixels)
	{
	iVerticalInterItemGap = aGapInPixels;
	}

EXPORT_C TInt CListItemDrawer::VerticalInterItemGap() const
	{
	return iVerticalInterItemGap;
	}

EXPORT_C TRgb CListItemDrawer::MarkColor() const
	{
	return iMarkColor;
	}


// class CTextListItemDrawer

EXPORT_C CTextListItemDrawer::CTextListItemDrawer()
	{
	}

EXPORT_C CTextListItemDrawer::~CTextListItemDrawer()
	{
	}

EXPORT_C CTextListItemDrawer::CTextListItemDrawer(MTextListBoxModel* aTextListBoxModel,const CFont* aFont)
	: iModel(aTextListBoxModel),
	iFont(aFont)
	{
//	iTextColor = KDefaultLbxTextColor;
//	iHighlightedTextColor = KDefaultLbxHighlightedTextColor;
	CEikonEnv* eikEnv=CEikonEnv::Static();
	iTextColor = eikEnv->Color(EEikColorControlText);
	iHighlightedTextColor = eikEnv->Color(EEikColorControlHighlightText);
	}

EXPORT_C void CTextListItemDrawer::ResetGc() const
	{
	CListItemDrawer::ResetGc();
	iGc->SetPenColor(iTextColor);
	}

EXPORT_C void CTextListItemDrawer::SetGc(CWindowGc* aGc)
	{
	iGc = aGc;
	iGc->UseFont(iFont);
	}

EXPORT_C TRect CTextListItemDrawer::MatcherCursorRect(const TDesC& aMatchableText, TInt aCharPos, TInt aItemCellYPos) const
	{
	TPoint cursorPos;
	TSize cursorSize;
	TPtrC stringBeforeCursor = aMatchableText.Left(aCharPos);
	TInt cursorWidth = 0;
	if (aCharPos < aMatchableText.Length())
		{
		TPtrC charAtCursor = aMatchableText.Mid(aCharPos,1);
		cursorWidth = iFont->TextWidthInPixels(charAtCursor);
		}
	else
		cursorWidth = iFont->MaxNormalCharWidthInPixels();
	cursorPos.iX = iFont->TextWidthInPixels(stringBeforeCursor);
	if(iDrawMark)
		cursorPos.iX += iMarkColumnWidth + iMarkGutter;
	cursorPos.iY = iFont->AscentInPixels() + aItemCellYPos;
	cursorSize.iWidth = cursorWidth;
    cursorSize.iHeight = iFont->HeightInPixels();
	TRect cursorRect(cursorPos, cursorSize);
	return cursorRect;
	}

EXPORT_C TInt CTextListItemDrawer::MatcherCursorAscent() const
	{
	// derived classes that deal with text and want to support incremental matching in
	// listboxes need to redefine this function
	return iFont->AscentInPixels() - (VerticalInterItemGap()/2);
	}

EXPORT_C void CTextListItemDrawer::SetCellWidthInChars(TInt aNumOfCharsToDisplayInCell)
	{
	iNumOfCharsInCell = aNumOfCharsToDisplayInCell;
	}

EXPORT_C TSize CTextListItemDrawer::MinimumCellSize() const
	{
	TInt width = iNumOfCharsInCell * iFont->MaxNormalCharWidthInPixels();
	TInt height = iFont->HeightInPixels() + VerticalInterItemGap();   
	return TSize(width, height);
	}

EXPORT_C void CTextListItemDrawer::DrawActualItem(TInt aItemIndex, const TRect& aActualItemRect, TBool aItemIsCurrent, TBool aViewIsEmphasized, TBool /*aViewIsDimmed*/) const
	{
	DrawItemText(aItemIndex,aActualItemRect,aItemIsCurrent,aViewIsEmphasized); 
	iGc->CancelClippingRect();
	}

EXPORT_C TInt CTextListItemDrawer::ItemWidthInPixels(TInt aItemIndex) const
	{
	TInt itemWidth = 0;
	if (iDrawMark)
		itemWidth += (iMarkColumnWidth + iMarkGutter);
	TPtrC des = iModel->ItemText(aItemIndex);
	if (des.Length())
		itemWidth += iFont->TextWidthInPixels(des);
	return itemWidth;
	}

EXPORT_C void CTextListItemDrawer::DoDrawItemText(const TDesC& aDes, const TRect& aItemTextRect, TBool aItemIsCurrent, TBool aViewIsEmphasized) const
	{
	TRgb penColor=iTextColor;
	TRgb brushColor=iBackColor;
	if (aItemIsCurrent && aViewIsEmphasized)
		{
		penColor=iHighlightedTextColor;
		brushColor=iHighlightedBackColor;
		}
	iGc->SetPenColor(penColor);
	iGc->SetBrushStyle(CGraphicsContext::ESolidBrush);
	iGc->SetBrushColor(brushColor);
	const TInt extraVerticalSpace=aItemTextRect.Height()-iFont->HeightInPixels();
	const TInt baseLineOffset=extraVerticalSpace/2 + iFont->AscentInPixels();
	iGc->DrawText(aDes,aItemTextRect,baseLineOffset);
	if (aItemIsCurrent && !aViewIsEmphasized)
		{
		iGc->SetBrushStyle(CGraphicsContext::ENullBrush);
		iGc->SetPenColor(KDefaultLbxHighlightRectColor);
		iGc->DrawRect(aItemTextRect);
		}
	ResetGc();
	}

EXPORT_C void CTextListItemDrawer::DrawItemText(TInt aItemIndex, const TRect& aItemTextRect, TBool aItemIsCurrent, TBool aViewIsEmphasized) const
	{
	iGc->UseFont(iFont);
	TPtrC des = iModel->ItemText(aItemIndex);
	DoDrawItemText(des, aItemTextRect, aItemIsCurrent, aViewIsEmphasized);
	}

