// EIKSCBUT.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#pragma warning( disable : 4710 )	// function not expanded

#include <eikscbut.h>
#include <eiksfont.h>
#include <eikdutil.h>
#include <eikenv.h>

#include <eikcolor.h>

const TInt KEikScrollButtonBorderWidth=1;

EXPORT_C CEikScrollButton::~CEikScrollButton()
	{
	}

EXPORT_C CEikScrollButton::CEikScrollButton(CEikScrollButton::TType aType)
	: iType(aType)
	{
	__DECLARE_NAME(_S("CEikScrollButton"));
	iBorder=TEikBorder(TEikBorder::EDeepRaised);
 	SetBehavior(EEikButtonStaysClear);
	SetReportOnPointerDown();
	SetNonFocusing();
	}

void CEikScrollButton::StateChanged()
	{
	if (!IsPressed())
		iBorder.SetType(TEikBorder::EDeepRaised);
	else 
		iBorder.SetType(TEikBorder::EShallowSunken);
	}

void CEikScrollButton::Draw(const TRect& /*aRect*/) const
	{
    CWindowGc& gc=SystemGc();
	const TRect outerRect=Rect();
	iBorder.Draw(gc,outerRect);
	TRect innerRect=iBorder.InnerRect(outerRect);
	TBool pressed=IsPressed();
	const TRgb brushColor=iEikonEnv->ControlColor((pressed? EEikColorScrollButtonThumbBackgroundPressed : 
															EEikColorScrollButtonThumbBackground),*this);
	gc.SetBrushColor(brushColor);
	gc.Clear(innerRect);
	if (pressed)
		innerRect.iTl+=TPoint(2,2);
	DrawIcon(gc,innerRect);
	}

EXPORT_C CEikScrollButton::TType CEikScrollButton::Type() const
	{
	return iType;
	}

EXPORT_C void CEikScrollButton::SetDimmed(TBool aDimmed)
	{
	CCoeControl::SetDimmed(aDimmed);
	if (aDimmed && IsPressed())
		{
		iButFlags&=(~EDrawClearPressed); // !! knows too much
		SetState(EClear);
		}
	}

void CEikScrollButton::DrawIcon(CWindowGc& aGc,const TRect& aRect) const
// Draw the appropriate icon on the button according to it's type and drag state
	{
	TInt charCode=0;
	switch (iType)
		{
    default:
        break;
	case ENudgeUp:
		charCode=ESymFontScrollUp;
		break;
	case ENudgeDown:
		charCode=ESymFontScrollDown;
		break;
	case ENudgeLeft:
		charCode=ESymFontScrollLeft;
		break;
	case ENudgeRight:
		charCode=ESymFontScrollRight;
		break;
	case EPageUp:
		charCode=ESymFontScrollPageUp;
		break;
	case EPageDown:
		charCode=ESymFontScrollPageDown;
		break;
	case EPageLeft:
		charCode=ESymFontScrollPageLeft;
		break;
	case EPageRight:
		charCode=ESymFontScrollPageRight;
		break;
	case ETop:
		charCode=ESymFontScrollToTop;
		break;
	case EBottom:
		charCode=ESymFontScrollToBottom;
		break;
	case EHome:
		charCode=ESymFontScrollToLeftEnd;
		break;
	case EEnd:
		charCode=ESymFontScrollToRightEnd;
		break;
		}
	TBuf<1> symbol;
	symbol.Append(TChar(charCode));
	const CFont* symFont=iEikonEnv->SymbolFont();
	aGc.UseFont(symFont);
	aGc.SetPenStyle(CGraphicsContext::ESolidPen);
	aGc.SetPenColor(IconColor());
	const TInt extraVerticalSpace=(aRect.Height() - symFont->HeightInPixels());
	const TInt baselineOffset=Max(symFont->AscentInPixels(),extraVerticalSpace/2 + symFont->AscentInPixels());
	aGc.DrawText(symbol,aRect,baselineOffset,CGraphicsContext::ECenter);
	}

TRect CEikScrollButton::IconRect(const TRect& aButtonFaceRect) const
// Given the button face rect, will return the rect in which to draw the icon
	{
	const TInt buttonFaceWidth=aButtonFaceRect.Width();
	const TInt buttonFaceHeight=aButtonFaceRect.Height();
	TRect iconRect(aButtonFaceRect.iTl+TSize((buttonFaceWidth>>2),(buttonFaceHeight>>2)),TSize((buttonFaceWidth>>1),(buttonFaceHeight>>1)));
	// For calculation purposes, ensure iconRect is a multiple of 4 in both dimensions
	switch (iconRect.Width()%4)
		{
	case 1:
		iconRect.iTl.iX++;
		break;
	case 2:
		iconRect.Grow(1,0);
		break;
	case 3:
		iconRect.iBr.iX++;
		}
	switch (iconRect.Height()%4)
		{
	case 1:
		iconRect.iTl.iY++;
		break;
	case 2:
		iconRect.Grow(0,1);
		break;
	case 3:
		iconRect.iBr.iY++;
		}
	return iconRect;
	}

TRgb CEikScrollButton::IconColor() const
	{
	TRgb color=iEikonEnv->ControlColor(EEikColorScrollButtonIcon,*this);
	if (IsDimmed())
		color=iEikonEnv->ControlColor(EEikColorScrollButtonIconDimmed,*this);
	else if (IsPressed())
		color=iEikonEnv->ControlColor(EEikColorScrollButtonIconPressed,*this);
	return color;
	}

