×
Namespaces

Variants
Actions

Анимированный фон в редакторе текста CEikEdwin

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

Статья
Автор: JasFas0la (10 Jul 2009)
Последнее редактирование: hamishwillee (10 Nov 2011)

Данная статья содержит информацию о том, как реализовать анимированный фон в редакторе текста CEikEdwin. В частности, информация из данной статьи может пригодиться для реализации CEikEdwin с произвольным содержимым фоновой картинки. Так же продемонстрирована возможность отображения полупрозрачного шрифта в редакторе.

Реализация

Ниже приведены 2 файла .h и .cpp контейнера, содержащего обьект редактора текста CEikEdwin с вышеуказанными возможностями

// "EdwinBgAppView.h"
#ifndef __EDWINBGAPPVIEW_H__
#define __EDWINBGAPPVIEW_H__
 
// INCLUDES
#include <coecntrl.h>
#include "eikedwin.h"
 
#define DESTROY(a) if(a){delete a;a=0;}
void SetEdwinParams( CEikEdwin* aEdwin, const CFont* aFont, TRgb aRgb );
class CFbsBitmap;
class CFbsBitGc;
class CFbsBitmapDevice;
class CEikEdwin;
// CLASS DECLARATION
class CEdwinBgAppView
: public CCoeControl
, public MFormCustomDraw // for edwin custom background
{
public: // New methods
 
/**
* NewL.
* Two-phased constructor.
* Create a CEdwinBgAppView object, which will draw itself to aRect.
* @param aRect The rectangle this view will be drawn to.
* @return a pointer to the created instance of CEdwinBgAppView.
*/

static CEdwinBgAppView* NewL( const TRect& aRect );
 
/**
* NewLC.
* Two-phased constructor.
* Create a CEdwinBgAppView object, which will draw itself
* to aRect.
* @param aRect Rectangle this view will be drawn to.
* @return A pointer to the created instance of CEdwinBgAppView.
*/

static CEdwinBgAppView* NewLC( const TRect& aRect );
 
/**
* ~CEdwinBgAppView
* Virtual Destructor.
*/

virtual ~CEdwinBgAppView();
 
public: // Functions from base classes
 
/**
* From CCoeControl, Draw
* Draw this CEdwinBgAppView to the screen.
* @param aRect the rectangle of this view that needs updating
*/

void Draw( const TRect& aRect ) const;
 
/**
* From CoeControl, SizeChanged.
* Called by framework when the view size is changed.
*/

virtual void SizeChanged();
 
// Component control access
virtual TInt CountComponentControls() const;
virtual CCoeControl* ComponentControl(TInt aIndex) const;
virtual TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType );
 
private: // Constructors
 
/**
* ConstructL
* 2nd phase constructor.
* Perform the second phase construction of a
* CEdwinBgAppView object.
* @param aRect The rectangle this view will be drawn to.
*/

void ConstructL(const TRect& aRect);
 
/**
* CEdwinBgAppView.
* C++ default constructor.
*/

CEdwinBgAppView();
 
public: // from MFormCustomDraw
virtual void DrawBackground(const TParam& aParam,const TRgb& aBackground,TRect& aDrawn) const;
virtual void DrawLineGraphics(const TParam& aParam,const TLineInfo& aLineInfo) const;
virtual void DrawText(const TParam& aParam,const TLineInfo& aLineInfo,const TCharFormat& aFormat,
const TDesC& aText,const TPoint& aTextOrigin,TInt aExtraPixels) const;
void ResetFont();
 
private:
CFbsBitmap* iOffScreenBitmap;
CFbsBitGc* iOffScreenGc;
CFbsBitmapDevice* iOffScreenBitmapDevice;
 
CFbsBitmap* iGradientBitmap1;
CFbsBitmap* iGradientBitmap2;
 
CEikEdwin* iEdwin;
CFont* iFont;
TRgb iEdwinColor;
TDisplayMode dmode;
TInt colorCounter;
 
CPeriodic* timer;
void StartTimer();
void StopTimer();
static TInt stPeriod( TAny* aPtr );
void Period();
TInt m_xOffset;
 
 
};
 
#endif // __EDWINBGAPPVIEW_H__
// "EdwinBgAppView.cpp"
// INCLUDE FILES
#include <coemain.h>
#include "EdwinBgAppView.h"
#include "GULUTIL.H"
#include <aknutils.h>
 
// ============================ MEMBER FUNCTIONS ===============================
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CEdwinBgAppView* CEdwinBgAppView::NewL( const TRect& aRect )
{
CEdwinBgAppView* self = CEdwinBgAppView::NewLC( aRect );
CleanupStack::Pop( self );
return self;
}
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CEdwinBgAppView* CEdwinBgAppView::NewLC( const TRect& aRect )
{
CEdwinBgAppView* self = new ( ELeave ) CEdwinBgAppView;
CleanupStack::PushL( self );
self->ConstructL( aRect );
return self;
}
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
TInt CEdwinBgAppView::CountComponentControls() const
{
return 1;
}
 
CCoeControl* CEdwinBgAppView::ComponentControl(TInt aIndex) const
{
switch( aIndex ) {
case 0: return iEdwin;
}
return 0;
}
 
void CEdwinBgAppView::ConstructL( const TRect& aRect )
{
timer = CPeriodic::NewL( CActive::EPriorityStandard );
colorCounter = 0;
iOffScreenBitmap = 0;
iOffScreenGc = 0;
iOffScreenBitmapDevice = 0;
iGradientBitmap1 = 0;
iGradientBitmap2 = 0;
 
CreateWindowL();
 
iEdwin = new (ELeave) CEikEdwin();
iEdwin->SetContainerWindowL(*this);
iEdwin->ConstructL(0,0,250,2);
TMargins8 margins;
iEdwin->SetBorderViewMargins( margins );
iEdwin->SetBorder( 0 );
iEdwin->SetAdjacent( 0 );
 
// Set the windows size
SetRect( aRect );
 
// Activate the window, which makes it ready to be drawn
ActivateL();
 
iEdwin->TextLayout()->SetCustomDraw( this );
iEdwin->SetFocus( ETrue );
StartTimer();
}
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::CEdwinBgAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CEdwinBgAppView::CEdwinBgAppView()
{
// No implementation required
}
 
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::~CEdwinBgAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CEdwinBgAppView::~CEdwinBgAppView()
{
StopTimer();
DESTROY(timer);
DESTROY(iOffScreenBitmap);
DESTROY(iOffScreenGc);
DESTROY(iOffScreenBitmapDevice);
DESTROY(iGradientBitmap1);
DESTROY(iGradientBitmap2);
DESTROY(iEdwin);
}
 
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CEdwinBgAppView::Draw( const TRect& aRect ) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
TRect rect( aRect );
rect.Move( -m_xOffset, 0 );
iOffScreenGc->DrawBitmap( rect, iGradientBitmap1 );
rect.Move( aRect.Width(), 0 );
iOffScreenGc->DrawBitmap( rect, iGradientBitmap2 );
rect.Move( aRect.Width(), 0 );
iOffScreenGc->DrawBitmap( rect, iGradientBitmap1 );
 
rect = iEdwin->Rect();
RRegion region( aRect );
region.SubRect( rect );
gc.SetClippingRegion( region );
region.Close();
gc.BitBlt( TPoint( 0, 0 ), iOffScreenBitmap );
gc.CancelClippingRegion();
}
 
// -----------------------------------------------------------------------------
// CEdwinBgAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CEdwinBgAppView::SizeChanged()
{
ResetFont();
m_xOffset = 0;
dmode = CCoeEnv::Static()->ScreenDevice()->DisplayMode();
DESTROY(iOffScreenBitmap);
DESTROY(iOffScreenGc);
DESTROY(iOffScreenBitmapDevice);
 
TSize size( Size() );
TRect rect( size );
 
iOffScreenBitmap = new (ELeave) CFbsBitmap();
iOffScreenBitmap->Create( size, dmode );
iOffScreenBitmapDevice = CFbsBitmapDevice::NewL(iOffScreenBitmap);
iOffScreenBitmapDevice->CreateContext(iOffScreenGc);
 
DESTROY( iGradientBitmap1 );
DESTROY( iGradientBitmap2 );
iGradientBitmap1 = new (ELeave) CFbsBitmap();
iGradientBitmap2 = new (ELeave) CFbsBitmap();
ColorUtils::CreateGradientBitmapL( *iGradientBitmap1, CCoeEnv::Static()->WsSession(), size.iWidth, ColorUtils::EBitmapOrientationHorizontal, TRgb( 255, 200, 200 ), TRgb( 255, 128, 64 ) );
ColorUtils::CreateGradientBitmapL( *iGradientBitmap2, CCoeEnv::Static()->WsSession(), size.iWidth, ColorUtils::EBitmapOrientationHorizontal, TRgb( 255, 128, 64 ), TRgb( 255, 200, 200 ) );
 
rect.Shrink( TSize( 15, 15 ) );
iEdwin->SetRect( rect );
iEdwinColor = TRgb( 0, 0, 255 );
SetEdwinParams( iEdwin, iFont, iEdwinColor );
 
DrawDeferred();
}
 
void CEdwinBgAppView::ResetFont()
{
if( iFont ) {
CCoeEnv::Static()->ScreenDevice()->ReleaseFont( iFont );
iFont = 0;
}
 
iFont = (CFont*) AknLayoutUtils::FontFromId( EAknLogicalFontPrimarySmallFont );
CWsScreenDevice* sd = CCoeEnv::Static()->ScreenDevice();
TFontSpec iFontSpec = iFont->FontSpecInTwips();
 
TInt nHeight = sd->VerticalTwipsToPixels( iFontSpec.iHeight );
iFontSpec.iFontStyle.SetBitmapType(EAntiAliasedGlyphBitmap);
iFontSpec.iFontStyle.SetStrokeWeight( EStrokeWeightBold );
iFontSpec.iHeight = nHeight;
iFont = 0;
sd->GetNearestFontInPixels( (CFont*&) iFont, iFontSpec );
 
 
}
 
TKeyResponse CEdwinBgAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType )
{
return iEdwin->OfferKeyEventL( aKeyEvent, aType );
}
 
void CEdwinBgAppView::StartTimer()
{
if( !timer->IsActive() ) {
timer->Start( 10000, 10000, TCallBack( stPeriod, this ) );
}
}
 
void CEdwinBgAppView::StopTimer()
{
if( timer->IsActive() ) {
timer->Cancel();
}
}
 
TInt CEdwinBgAppView::stPeriod( TAny* aPtr )
{
CEdwinBgAppView* p = static_cast<CEdwinBgAppView*>(aPtr);
p->Period();
return ETrue;
}
 
void CEdwinBgAppView::Period()
{
TInt nMaxOffset = Size().iWidth * 2;
m_xOffset += 3;
if( m_xOffset > nMaxOffset ) m_xOffset -= nMaxOffset;
 
 
colorCounter += 2;
jmp_back:
if( colorCounter < 255 ) {
iEdwinColor.SetAlpha( 255 - colorCounter );
}
else if( colorCounter < 510 ) {
iEdwinColor.SetAlpha( colorCounter - 255 );
}
else if( colorCounter >= 510 ) {
colorCounter -= 510;
goto jmp_back;
}
DrawDeferred();
iEdwin->DrawDeferred();
}
 
 
void CEdwinBgAppView::DrawBackground(const TParam& aParam,const TRgb& aBackground,TRect& aDrawn) const
{
const TRect& iRect = aParam.iDrawRect;
aParam.iGc.DrawBitmap(iRect, iOffScreenBitmap, iRect);
aParam.iGc.SetClippingRect( TRect( 0, 0, 0, 0 ) );
}
 
void CEdwinBgAppView::DrawLineGraphics(const TParam& aParam,const TLineInfo& aLineInfo) const
{
const TRect& iRect = aParam.iDrawRect;
aParam.iGc.DrawBitmap(iRect, iOffScreenBitmap, iRect);
}
 
void CEdwinBgAppView::DrawText(const TParam& aParam,const TLineInfo& aLineInfo,const TCharFormat& aFormat,
const TDesC& aText,const TPoint& aTextOrigin,TInt aExtraPixels) const
{
aParam.iGc.SetPenColor( iEdwinColor );
MFormCustomDraw::DrawText(aParam, aLineInfo, aFormat, aText, aTextOrigin, aExtraPixels);
}
 
void SetEdwinParams( CEikEdwin* aEdwin, const CFont* aFont, TRgb aColor )
{
TCharFormat charFormat;
TCharFormatMask charFormatMask;
 
CCharFormatLayer* iCharFormat;
charFormat.iFontSpec = aFont->FontSpecInTwips();
CWsScreenDevice* iScreenDevice = CCoeEnv::Static()->ScreenDevice();
charFormat.iFontSpec.iHeight = iScreenDevice->VerticalPixelsToTwips( aFont->HeightInPixels() );
charFormat.iFontPresentation.iTextColor = aColor;
charFormat.iFontSpec.iFontStyle.SetPosture( EPostureItalic );
 
charFormat.iFontPresentation.iPictureAlignment = TFontPresentation::EAlignCentered;
charFormatMask.SetAll();
iCharFormat = CCharFormatLayer::NewL(charFormat, charFormatMask);
aEdwin->SetCharFormatLayer(iCharFormat);
}
 
// End of File

Пример создания и уничтожения контейнера в AppUi классе приложения

void CEdwinBgAppUi::ConstructL()
{
// Initialise app UI with standard value.
BaseConstructL();
 
// Create view object
/* CEdwinBgAppView* */ iAppView = CEdwinBgAppView::NewL( ClientRect() );
AddToStackL(iAppView);
}
 
CEdwinBgAppUi::~CEdwinBgAppUi()
{
if ( iAppView )
{
RemoveFromStack(iAppView);
delete iAppView;
iAppView = NULL;
}
}


Необходимые библиотеки

  • print.lib
  • egul.lib
  • uiklaf.lib
  • form.lib
  • etext.lib
  • eikctl.lib
  • eikcoctl.lib
  • fbscli.lib
  • bitgdi.lib
  • gdi.lib
  • euser.lib
  • apparc.lib
  • cone.lib
  • eikcore.lib
  • avkon.lib
  • commonengine.lib
  • efsrv.lib
  • estor.lib
  • hlplch.lib
  • bafl.lib
  • charconv.lib
This page was last modified on 10 November 2011, at 06:03.
73 page views in the last 30 days.
×