chxavtextcontrol.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 289 行

CPP
289
字号
/************************************************************************
 * chxavtextcontrol.cpp
 * --------------------
 *
 * Synopsis:
 * Text control for ui implementation.
 *
 * Target:
 * Symbian OS
 *
 *
 * (c) 1995-2003 RealNetworks, Inc. Patents pending. All rights reserved.
 *
 ************************************************************************/

// Helix includes...
#include "hxstring.h"

// Includes from this project...
#include "hxsym_debug.h"
#include "hxsym_leaveutil.h"
#include "chxavmisc.h"
#include "chxavcleanstring.h"
#include "chxavstringutils.h"
#include "chxavtextcontrol.h"


CHXAvTextControl::CHXAvTextControl(TInt cid)
: CHXAvControl(cid)
, m_pCommand(0)
, m_pbmpBG(0)
, m_pExternalFont(0)
, m_bUseEllipsisForTruncation(true)
{
    SetText(KNullDesC);
}

CHXAvTextControl* 
CHXAvTextControl::NewL(const CCoeControl& container, TInt cid )
{
    CHXAvTextControl* pSelf = new(ELeave) CHXAvTextControl(cid);
    CleanupStack::PushL(pSelf);
    pSelf->ConstructL(container);
    CleanupStack::Pop();
    return pSelf;
}

void 
CHXAvTextControl::ConstructL(const CCoeControl& parent)
{
    SetContainerWindowL(parent);
    EnableDragEvents();
}

CHXAvTextControl::~CHXAvTextControl()
{
    if (m_pFont )
    {
	iEikonEnv->ScreenDevice()->ReleaseFont(m_pFont);
	m_pFont = 0;
    }

    HX_DELETE(m_pbmpBG);
    HX_DELETE(m_pText);
    HX_DELETE(m_pCommand);
}


void 
CHXAvTextControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
    DPRINTF(SYMP_WSEVENTS, ("CHXAvTextControl::HandlePointerEventL (%d)\n", aPointerEvent.iType));

    // only handle events if we are not disabled
    if (!IsDimmed())
    {
	if( aPointerEvent.iType == TPointerEvent::EButton1Down )
	{
	    if( m_pCommand)
	    {
		m_pCommand->Execute();
	    }
	}
    }
}

void CHXAvTextControl::SetCommandL(const CHXAvCommand& commandProto)
{
    HX_DELETE(m_pCommand);
    m_pCommand = commandProto.CloneL();
}

// 
// create a copy of the parent background bitmap
//
// useful if the text control is transparent, and the text
// will change during the lifetime of the control.
//
void 
CHXAvTextControl::SetBGBitmapL(CFbsBitmap* pbmpParentBG)
{
    if( !pbmpParentBG )
    {
	// free current bitmap
	HX_DELETE(m_pbmpBG);
	return;
    }

    CWsScreenDevice* pScreenDevice = iCoeEnv->ScreenDevice();

    // create bg bitmap of same size as this control
    if( m_pbmpBG )
    {
	m_pbmpBG->Reset();
    }
    {
	m_pbmpBG = new (ELeave) CFbsBitmap();
    }
    CleanupStack::PushL(m_pbmpBG);
    m_pbmpBG->Create(Rect().Size(), pScreenDevice->DisplayMode());
		    
    // create device 
    CFbsBitmapDevice* pbmpDevice = CFbsBitmapDevice::NewL(m_pbmpBG);
    CleanupStack::PushL(pbmpDevice);

    // create context from device
    CGraphicsContext* pbmpContext = 0;
    HXSYM_LEAVE_IF_ERR(pbmpDevice->CreateContext(pbmpContext));
    CleanupStack::PushL(pbmpContext);

    // draw from parent bitmap to new bitmap
    TRect rcDest(TPoint(0,0), Rect().Size());
    pbmpContext->DrawBitmap(rcDest, pbmpParentBG, Rect());

    CleanupStack::PopAndDestroy(2);	// gc, dev
    CleanupStack::Pop();			// m_pbmpBG
}


void
CHXAvTextControl::Draw(const TRect& /*rect*/) const
{
    CWindowGc& gc = SystemGc();

    const CFont* pFont = GetFont();
    HX_ASSERT(pFont); // call UpdateFont() after attributes are set

    gc.SetPenColor(m_attr.textColor);
    gc.SetBrushColor(m_attr.bgColor);
    if (m_attr.bIsTransparent)
    {
	// draw background if supplied
	if( m_pbmpBG )
	{
	    gc.BitBlt(Rect().iTl, m_pbmpBG);
	}
	gc.SetBrushStyle(CGraphicsContext::ENullBrush);
    }
    else
    {
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    }

    gc.UseFont(pFont);

    const TDesC& text = GetText();
    if (text.Length() > 0)
    {
	HBufC* pNewText = 0;
	TRAPD(ret, CHXAvStringUtils::GetTruncatedDisplayTextL(text, pFont, Rect().Width(), pNewText, m_bUseEllipsisForTruncation));
	// only safe to draw if function succeeded
	if( ret == KErrNone )
	{
	    const TDesC* pText = pNewText ? pNewText: &text;

	    gc.DrawText(*pText, Rect(), 
		    Rect().Height()/2 + pFont->AscentInPixels()/2, 
		    m_attr.justify);
	    delete pNewText;
	}
    }
    else
    {
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.DrawRect(Rect());
    }

    //gc.DiscardFont();

}

void 
CHXAvTextControl::SetText(const TDesC& text)
{
    HX_DELETE(m_pText);
    m_pText = text.Alloc();
}

void 
CHXAvTextControl::SetAttributes(const CHXAvTextControl::TextControlAttributes& attr)
{
    m_attr = attr;	
}

////////////////////////////////////////////////////
//
// 1) use an externally owned font
// 2) create internal font based on current font attributes
//
void CHXAvTextControl::UpdateFont(const CFont* pExternalFont)
{
    // reset the current font
    if (m_pFont)
    {
	iEikonEnv->ScreenDevice()->ReleaseFont(m_pFont);
	m_pFont = 0;
    }

    if( pExternalFont )
    {
	m_pExternalFont = pExternalFont;
    }
    else
    {
	m_pExternalFont = 0;
	m_pFont = CreateFont();
    }
}
    
void
CHXAvTextControl::SetTextColor(const TRgb& color)
{
    m_attr.textColor = color;
}
    
void
CHXAvTextControl::SetBackgroundColor(const TRgb& color)
{
    m_attr.bgColor = color;
}
    
void
CHXAvTextControl::SetFontName(const CHXString& fontName)
{
    m_attr.fontName = fontName;
}
    
void
CHXAvTextControl::SetFontHeight(int height)
{
    m_attr.fontHeight = height;
}
    
void
CHXAvTextControl::SetFontBold(bool bold)
{
    m_attr.bIsBold = bold;
}  

void 
CHXAvTextControl::SetJustification(CGraphicsContext::TTextAlign justify)
{
    m_attr.justify = justify;
}
    
void
CHXAvTextControl::SetTransparent(bool transparent)
{
    m_attr.bIsTransparent = transparent;
}

CFont*
CHXAvTextControl::CreateFont() const
{
    CHXAvCleanString fontName(m_attr.fontName); //XXXLCM leave
    TFontSpec spec(fontName(), PointsToTwips(m_attr.fontHeight));

    if (m_attr.bIsBold)
    {
	spec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
    }

    CFont* font;
    iEikonEnv->ScreenDevice()->GetNearestFontInTwips(font, spec); 

    return font;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?