Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


FontsShell: fonts

Note: This example is designed to work with TechView and there is no guarantee that it will work with other interfaces.

[Top]


Description

FontsShell provides an application shell and two concrete controls.

CGraphicExampleControl is a control class, derived from CCoeControl, which links the app shell to the particular controls.

CHelloControl illustrates the drawing of text using a standard font.

CFontControl illustrates the use of fonts.

[Top]


Example Code

The source code for this example application can be found in the directory:

The files reproduced here are the main files contained in the examples directory. Some extra files may be needed to run the examples, and these will be found in the appropriate examples directory.

examples\Graphics\Fonts

// FontsShell.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#ifndef __FontsShell_H
#define __FontsShell_H

#include <coecntrl.h>
#include <coeccntx.h>

#include <eikappui.h>
#include <eikapp.h>
#include <eikdoc.h>

#include "FontsGraphicsControl.h"

// UID of app

const TUid KUidExampleShellApp={ 0x1000525d };

//
// TExampleShellModel
//

class TExampleShellModel
    {
public:
    TExampleShellModel();
    TBool Differs(const TExampleShellModel* aCompare) const;
public:
    TFileName iLibrary; // active control
    };

//
// class CExampleShellContainer
//

class CExampleShellContainer : public CCoeControl,
        public MCoeControlBrushContext,
        public MGraphicsExampleObserver
    {
public:
    void ConstructL(const TRect& aRect, TExampleShellModel* aModel);
    ~CExampleShellContainer();
    // changing view
    void ResetExampleL(CGraphicExampleControl* aExample);
private: // from CCoeControl
    void Draw(const TRect& /*aRect*/) const;
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
    TInt CountComponentControls() const;
    CCoeControl* ComponentControl(TInt aIndex) const;
private: // from MGraphicsExampleObserver
    void NotifyGraphicExampleFinished();
public: // also from MGraphicsExampleObserver
    void NotifyStatus(const TDesC& aMessage);
private: // new function
    void CreateLabelL();
private: // data
    CGraphicExampleControl* iExampleControl; // example control
    CEikLabel* iLabel; // label for status messages
    // irrelevant
    TExampleShellModel* iModel;
    };

//
// CExampleShellDocument
//

class CExampleShellDocument : public CEikDocument
    {
public:
    CExampleShellDocument(CEikApplication& aApp): CEikDocument(aApp) { }
    TExampleShellModel* Model() { return(&iModel); }
private: // from CEikDocument
    CEikAppUi* CreateAppUiL();
private:
    TExampleShellModel iModel;
    };

//
// CExampleShellAppUi
//

class CExampleShellAppUi : public CEikAppUi
    {
public:
    void ConstructL();
    ~CExampleShellAppUi();
private: // from CEikAppUi
    void HandleCommandL(TInt aCommand);
private: // internal use
//  void PrepareToolbarButtons();
private:
    CExampleShellContainer* iContainer;
    TExampleShellModel* iModel;
    };

//
// CExampleShellApplication
//

class CExampleShellApplication : public CEikApplication
    {
private: // from CApaApplication
    CApaDocument* CreateDocumentL();
    TUid AppDllUid() const;
    };

#endif
// FontsGraphicsControl.h
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#ifndef __FontsGraphicsControl_H
#define __FontsGraphicsControl_H

#include <coecntrl.h>
#include <s32file.h>
#include "CommonGraphicsControlFramework.h"


class CSmileyPicture : public CPicture
    {
public:
    enum TMood { EHappy, ENeutral, ESad }; // various moods
    enum TSizeSpec { ELarge, EMedium, ESmall }; // sizes

    // creating
    CSmileyPicture();
    static CSmileyPicture* NewL(TMood aMood, TSizeSpec aSizeSpec); // from scratch
    static CSmileyPicture* NewL(const CStreamStore& aStore, TStreamId aStreamId); // from stream
    TStreamId StoreL(CStreamStore& aStore) const;
    void Draw(CGraphicsContext& aGc,const TPoint& aTopLeft,const TRect& aClipRect,
                        MGraphicsDeviceMap* aMap)const; 

    void SetMood(TMood aMood); // set mood
    TMood Mood(); // get mood
    void SetSize(TSizeSpec aSizeSpec);    // set size
    TSizeSpec Size(); // get size
    TInt SpecToFactor() const;

    void GetOriginalSizeInTwips(TSize& aSize) const;

    // only needed for cropping and scaling
    void SetScaleFactor(TInt aScaleFactorWidth,TInt aScaleFactorHeight);
    void SetCropInTwips(const TMargins& aMargins);
    TPictureCapability Capability() const;
    void GetCropInTwips(TMargins& aMargins) const;
    TInt ScaleFactorWidth() const;
    TInt ScaleFactorHeight() const;
private:
    // streaming
    void ExternalizeL(RWriteStream& aStream) const;      // externalize state
    void InternalizeL(RReadStream& aStream);          // internalize state
private:
    // member data - size and details of whether happy or sad
    TMood iMood;
    TSizeSpec iSizeSpec;
    };

// sundry derived classes

class CHelloControl : public CGraphicExampleControl
    {
public:
    CHelloControl() { SetMaxPhases(7); };
    void UpdateModelL();
    void Draw(const TRect& aRect) const;
    };

class CPictureControl : public CGraphicExampleControl
    {
public:
    CPictureControl();
    ~CPictureControl() { delete(iPicture); };
    void UpdateModelL();
    void Draw(const TRect& aRect) const;
private:
    enum TDocStatus { EFalse, EHeader, EPicture };
    TStreamId StoreHeaderL(CStreamStore& aStore) const;
    void StoreHeaderComponentsL(CStoreMap& aMap,CStreamStore& aStore) const;
    void RestoreHeaderL(CStreamStore& aStore, TStreamId aId);

    TZoomFactor testZf;
    MGraphicsDeviceMap* testMap;

    TDocStatus iValidDocument; // if false, then Draw() draws gray screen
    CSmileyPicture* iPicture; // if there, then draw
    TPictureHeader iHeader; // if no iPicture, draw outline to specified size
    TPoint iOffset; // offset of picture from top-left
    CFileStore* iStore; // stream store for persistence
    TStreamId iHeaderId;  // root stream of store
    };

class CFontControl : public CGraphicExampleControl
    {
public:
    CFontControl();
    ~CFontControl() { };
    void UpdateModelL();
    void Draw(const TRect& aRect) const;
private:
    MGraphicsDeviceMap* iDeviceMap;
    TZoomFactor iZoomFactor;
    TInt iNumTypefaces;
    TBuf<19> iCurrentFont;
    };

#endif
// Fonts.cpp
//
// Copyright (c) 2000-2005 Symbian Software Ltd.  All rights reserved.

#include "FontsGraphicsControl.h"
#include <coemain.h>

CFontControl::CFontControl() {
    // find the number of typefaces
    iNumTypefaces = iCoeEnv->ScreenDevice()->NumTypefaces();
    // set the number of phases to the number of typefaces
    SetMaxPhases(iNumTypefaces);
}

void CFontControl::UpdateModelL()
    {
    // set up zoom factor & graphics device map for getting fonts and drawing text
    iZoomFactor.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
    iZoomFactor.SetZoomFactor(TZoomFactor::EZoomOneToOne);
    iDeviceMap = &iZoomFactor;

    // find the font to display this phase
    TTypefaceSupport* myTypefaceSupport = new TTypefaceSupport;
    iCoeEnv->ScreenDevice()->TypefaceSupport(*myTypefaceSupport,Phase());
    iCurrentFont = myTypefaceSupport->iTypeface.iName.Des();

    // set up descriptor for commentary area
    _LIT(KFormat,"Typeface index=%d. Name=%S. Num heights=%d. (Num typefaces=%d)");
    TBuf<128> commentaryText;
    commentaryText.Format(KFormat,Phase(),&iCurrentFont,myTypefaceSupport->iNumHeights,iNumTypefaces);
    iGraphObserver->NotifyStatus(commentaryText);
    delete myTypefaceSupport;
    };

void CFontControl::Draw(const TRect& /* aRect */) const
    {
    // setup screen for example
    CWindowGc& gc=SystemGc();
    gc.Clear(); 
    
    // create a centered rectangle of the default size
    TRect screenRect=Rect();

    // set up absolute font-spec and text box for 300 twip font
    TFontSpec fontSpec(iCurrentFont,300);
    // find the nearest font to the specified one
    CFont* screenFont;                                     
    iDeviceMap->GetNearestFontInTwips(screenFont,fontSpec);
    // use it for this graphics context
    gc.UseFont(screenFont);

    // get height of screen box
    TInt screenHeight=screenRect.Height();
    // get font height
    TInt textHeight = screenFont->HeightInPixels();

    // set up the positioning of the character set rows
    TInt exampleMargin = 0;
    TInt currentOffset = ((screenHeight+textHeight)/2) - (TInt)(textHeight*2.4);

    // set up descriptors to hold Unicode characters 32 to 255
    TBuf<50> buffer;
    TInt endPoint[6] = {64,96,138,182,216,255};
    TInt count = 32;
    for(TInt pass=0;pass<=5;pass++) {
        while (count<=endPoint[pass]) {
            buffer.Append((TUint)count);
            count++;
        }
        // draw the row in the current font
        gc.DrawText(buffer,screenRect,currentOffset,CGraphicsContext::ECenter,exampleMargin);   
        currentOffset = currentOffset + (TInt)(textHeight*1.2);
        buffer.Zero();
    }

    // discard and release font
    gc.DiscardFont();
    iDeviceMap->ReleaseFont(screenFont); 
    }
// FontsGraphicsControl.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

#include "FontsGraphicsControl.h"

#include <coemain.h>
#include <coeaui.h>

_LIT(KtxtSwiss,"Swiss");

void CGraphicExampleControl::ConstructL(const TRect& aRect, MGraphicsExampleObserver* aGraphObserver, const CCoeControl& aParent)
    {
    // remember the graphics observer
    iGraphObserver=aGraphObserver;
    // create window
    CreateWindowL(&aParent);
    // construct font for messages
    TFontSpec spec(KtxtSwiss,213);
    iMessageFont=iCoeEnv->CreateScreenFontL(spec);
    // set rectangle to prescription
    SetRect(aRect);
    // go for it
    ActivateL();
    UpdateModelL(); // phase 0
    }

CGraphicExampleControl::~CGraphicExampleControl()
    {
    iCoeEnv->ReleaseScreenFont(iMessageFont);
    }

void CGraphicExampleControl::Quit()
    {
    iGraphObserver->NotifyGraphicExampleFinished();
    }

void CGraphicExampleControl::NextPhaseL()
    {
    if (++iPhase >= iMaxPhases)
        Quit();
    else
        {
        UpdateModelL();
        DrawNow();
        }
    }

void CGraphicExampleControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
    {
    if (aPointerEvent.iType==TPointerEvent::EButton1Down) NextPhaseL();
    }

TKeyResponse CGraphicExampleControl::OfferKeyEventL(
            const TKeyEvent& aKeyEvent,TEventCode aType
            )
    {
    if (aType!=EEventKey) return EKeyWasNotConsumed;
    TInt code=aKeyEvent.iCode;
    switch (code)
        {
        case ' ':
            NextPhaseL();
            break;
        default:
            return EKeyWasNotConsumed;
        }
    return EKeyWasConsumed;
    }
// FontsShell.cpp
//
// Copyright (c) 2000-2005 Symbian Software Ltd.  All rights reserved.

#include <e32keys.h>

#include <coemain.h>

#include <eikenv.h>
#include <eikdef.h>
#include <eikon.hrh>
#include <eiklabel.h>
#include <eikstart.h>

#include <FontsShell.rsg>
#include "FontsShell.hrh"
#include "FontsShell.h"
#include "FontsGraphicsControl.h"

//
// TExampleShellModel
//

TExampleShellModel::TExampleShellModel()
    {
    iLibrary=KNullDesC;
    }

TBool TExampleShellModel::Differs(const TExampleShellModel* aCompare) const
    {
    return((*(TInt32*)this)!=(*(TInt32*)aCompare));
    }

//
// class CExampleShellContainer
//

void CExampleShellContainer::ConstructL(const TRect& aRect, TExampleShellModel* aModel)
    {
    iModel=aModel;
    CreateWindowL();
    Window().SetShadowDisabled(ETrue);
    iContext=this;
     iBrushStyle=CGraphicsContext::ESolidBrush;
    iBrushColor=KRgbWhite;
    SetRect(aRect);
    CreateLabelL();
    ActivateL();
    }

CExampleShellContainer::~CExampleShellContainer()
    {
    delete iExampleControl;
    delete iLabel;
    }
    
TInt CExampleShellContainer::CountComponentControls() const
    {
    return 1 + (iExampleControl ? 1 : 0);
    }

CCoeControl* CExampleShellContainer::ComponentControl(TInt aIndex) const
    {
    switch (aIndex)
        {
    case 0: return iLabel;
    case 1: return iExampleControl;
    default: return 0;
        };
    }

const TInt KLabelHeight=20;

void CExampleShellContainer::CreateLabelL()
    {
    iLabel=new (ELeave) CEikLabel;
    TRect rect=Rect();
    rect.iTl.iY=rect.iBr.iY-KLabelHeight; // make it bottom 20 pixels
    iLabel->SetContainerWindowL(*this);
    iLabel->SetRect(rect);
    iLabel->SetAlignment(EHCenterVCenter); // center text
    iLabel->SetBufferReserveLengthL(200); // nice long buffer
    iLabel->SetFont(iEikonEnv->AnnotationFont());
    iLabel->ActivateL(); // now ready
    }

void CExampleShellContainer::ResetExampleL(CGraphicExampleControl* aExample)
    {
    // get rid of old control
    delete iExampleControl;
    // set up new one
    iExampleControl=aExample;
    // if non-zero, then carry on
    if (!iExampleControl) return;
    TRect rect=Rect(); // get our rect
    rect.iBr.iY-=KLabelHeight; // make way for label
    rect.Shrink(2,2); // shrink it a bit
    iExampleControl->ConstructL(rect,this,*this); // construct, giving rect and observer
    }

_LIT(KTxtFinished,"example finished");
void CExampleShellContainer::NotifyGraphicExampleFinished()
    {
    NotifyStatus(KTxtFinished);
    }

void CExampleShellContainer::NotifyStatus(const TDesC& aMessage)
    {
    iLabel->SetTextL(aMessage);
    if (IsActivated()) iLabel->DrawNow();
    }

TKeyResponse CExampleShellContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    if  (iExampleControl)
         return iExampleControl->OfferKeyEventL(aKeyEvent,aType);
    else
        return EKeyWasNotConsumed;
    }

void CExampleShellContainer::Draw(const TRect& /*aRect*/) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(Rect());
    }

//
// CExampleShellAppUi
//

_LIT(KTxtInitialized,"initialized");
void CExampleShellAppUi::ConstructL()
    {
    BaseConstructL();
    iModel=((CExampleShellDocument*)iDocument)->Model();
    iContainer=new(ELeave) CExampleShellContainer;
    iContainer->ConstructL(ClientRect(),iModel);
    iContainer->NotifyStatus(KTxtInitialized);
    // add container to stack; enables key event handling.
    AddToStackL(iContainer);
    }

void CExampleShellAppUi::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
    case EExampleShellSelectHello:
        iContainer->ResetExampleL(new (ELeave) CHelloControl);
        return;
    case EExampleShellSelectFont:
        iContainer->ResetExampleL(new (ELeave) CFontControl);
        return;
    case EEikCmdExit:
        Exit();
        return;
        }
    }

CExampleShellAppUi::~CExampleShellAppUi()
    {
    RemoveFromStack(iContainer);
    delete iContainer;
    }

//
// CExampleShellDocument
//

CEikAppUi* CExampleShellDocument::CreateAppUiL()
    {
    return(new(ELeave) CExampleShellAppUi);
    }

//
// CExampleShellApplication
//

TUid CExampleShellApplication::AppDllUid() const
    {
    return KUidExampleShellApp;
    }

CApaDocument* CExampleShellApplication::CreateDocumentL()
    {
    return new(ELeave) CExampleShellDocument(*this);
    }

//
// EXPORTed functions
//

EXPORT_C CApaApplication* NewApplication()
    {
    return new CExampleShellApplication;
    }

GLDEF_C TInt E32Main()
    {
    return EikStart::RunApplication(NewApplication);
    }
// Hello.cpp
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.


#include "FontsGraphicsControl.h"

_LIT(KTxtUpdateModelEnglish,"English");
_LIT(KTxtUpdateModelFrench,"French");
_LIT(KTxtUpdateModelGerman,"German");
_LIT(KTxtUpdateModelItalian,"Italian");
_LIT(KTxtUpdateModelTurkish,"Turkish");
_LIT(KTxtUpdateModelJapanese,"Japanese");
_LIT(KTxtUpdateModelShakespearian,"Shakespearian");
_LIT(KTxtUpdateModelOverrun,"overrun!");

void CHelloControl::UpdateModelL()
    {
    switch(Phase())
        {
    case 0:
        iGraphObserver->NotifyStatus(KTxtUpdateModelEnglish);
        break;
    case 1:
        iGraphObserver->NotifyStatus(KTxtUpdateModelFrench);
        break;
    case 2:
        iGraphObserver->NotifyStatus(KTxtUpdateModelGerman);
        break;
    case 3:
        iGraphObserver->NotifyStatus(KTxtUpdateModelItalian);
        break;
    case 4:
        iGraphObserver->NotifyStatus(KTxtUpdateModelTurkish);
        break;
    case 5:
        iGraphObserver->NotifyStatus(KTxtUpdateModelJapanese);
        break;
    case 6:
        iGraphObserver->NotifyStatus(KTxtUpdateModelShakespearian);
        break;
    default:
        iGraphObserver->NotifyStatus(KTxtUpdateModelOverrun);
        break;
        };
    }

//Text literals
_LIT(KTxtDrawCase0,"Hello world!");
_LIT(KTxtDrawCase1,"Bonjour tout le monde!");
_LIT(KTxtDrawCase2,"Hallo aller Welt!");
_LIT(KTxtDrawCase3,"Buongiorno tutto il mondo!");
_LIT(KTxtDrawCase4,"Merhaba Dunyaya!");
_LIT(KTxtDrawCase5,"Ohayo gozaimasu!");
_LIT(KTxtDrawCase6,"Friends, Romans, Countrymen!");

void CHelloControl::Draw(const TRect& /* aRect */) const
    {
    // draw surrounding rectangle
    SystemGc().DrawRect(Rect());
    // calculate rectangle to draw into
    TRect rect=Rect();
    rect.Shrink(1,1);
    // calculate vertical centering
    CFont *font=iMessageFont;
    TInt ascent=
            (rect.Height() - font->HeightInPixels())/2 +
                font->AscentInPixels();
    // draw text in rectangle
    CWindowGc& gc=SystemGc();
     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.UseFont(iMessageFont);
    switch(Phase())
        {
    case 0:
        gc.DrawText(KTxtDrawCase0,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 1:
        gc.DrawText(KTxtDrawCase1,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 2:
        gc.DrawText(KTxtDrawCase2,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 3:
        gc.DrawText(KTxtDrawCase3,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 4:
        gc.DrawText(KTxtDrawCase4,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 5:
        gc.DrawText(KTxtDrawCase5,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    case 6:
        gc.DrawText(KTxtDrawCase6,rect,ascent, CGraphicsContext::ECenter, 0);
        break;
    default:
        break;
        };
     }
// FontsShell.hrh
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

enum TExampleMenuCommands
    {
    EExampleShellSelectHello=300,
    EExampleShellSelectFont
    };
// BLD.INF
// Component description file 
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

PRJ_MMPFILES

FontsShell.mmp
// FontsShell.mmp
//
// Copyright (c) 2000-2005 Symbian Software Ltd.  All rights reserved.

// using relative paths for source and userinclude directories


TARGET        FontsShell.exe
TARGETTYPE    exe
UID           0x100039ce 0x1000525d
VENDORID 0x70000001

SOURCEPATH    .
SOURCE        FontsShell.cpp FontsGraphicsControl.cpp  Fonts.cpp Hello.cpp

START RESOURCE FontsShell.rss
TARGETPATH     \resource\apps
HEADER
END

USERINCLUDE   .
USERINCLUDE   ..\CommonGraphicsExampleFiles
SYSTEMINCLUDE \Epoc32\include
SYSTEMINCLUDE \epoc32\include\techview

LIBRARY       euser.lib  gdi.lib   ws32.lib 
LIBRARY       cone.lib  apparc.lib eikcore.lib  eikcoctl.lib 

START RESOURCE FontsShell_reg.rss
TARGETPATH     \private\10003a3f\apps
END
// FontsShell.rss
//
// Copyright (c) 2000 Symbian Ltd.  All rights reserved.

NAME GRSH


#include <eikon.rh>
#include "FontsShell.hrh"

RESOURCE RSS_SIGNATURE { }

RESOURCE TBUF { buf=""; }

RESOURCE EIK_APP_INFO
    {
    menubar=r_grsh_menubar;
    hotkeys=r_grsh_hotkeys;
    }


RESOURCE HOTKEYS r_grsh_hotkeys
    {
    control=
        {
        HOTKEY { command=EEikCmdExit; key='e'; }
        };
    }

RESOURCE MENU_BAR r_grsh_menubar
    {
    titles=
        {
        MENU_TITLE {menu_pane=menu1; txt="File"; },
    MENU_TITLE {menu_pane=menu2; txt="List Of Programs "; }
    };
    }


RESOURCE MENU_PANE menu1
    {
    items=
        {
        MENU_ITEM { command=EEikCmdExit; txt="Exit"; }
        };
    }

RESOURCE MENU_PANE menu2
    {
    items=
        {
        MENU_ITEM { command=EExampleShellSelectHello; txt="Hello"; },
        MENU_ITEM { command=EExampleShellSelectFont; txt="Font"; }
        
        };
    }
#include <appinfo.rh>

UID2 KUidAppRegistrationResourceFile
UID3 0x1000525d // application UID
RESOURCE APP_REGISTRATION_INFO
    {
    app_file = "FontsShell";
    }

[Top]


Build

The source code for this example application can be found in the directory:

examples\Graphics\Fonts

It may be in the directory in which you installed Symbian OS, or it may be in src\common\developerlibrary\. It includes the two project files needed for building: bld.inf and the .mmp file.

The Symbian OS build process describes how to build this application. For the emulator, an application called FontsShell.exe is created in epoc32\release\winscw\<udeb or urel>\.

[Top]


Usage

  1. Launch the emulator:

    \epoc32\release\winscw\<urel or udeb>\EPOC.EXE.

  2. Click on FONTSSHELL to run the application. If using the TechView emulator, this will be in the Extras menu.

  3. The controls are listed on the menu List of Programs. Press the appropriate menu item to select a specific control.

  4. Step through each phase of the example by pressing the space bar or by tapping on the window drawn by that example.

[Top]


Classes used

By the hello control:

By the fonts control: