Symbian
Symbian Developer Library

SYMBIAN OS V9.4

Feedback

[Index] [Previous] [Next]


ControlFramework: A UI application with a toolbar

Found in examples\Appframework\UIControls\ControlFramework\.

[Top]


Example code

ControlFrameworkApplication.h

// ControlFrameworkApplication.h
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#ifndef __CONTROLFRAMEWORKAPPLICATION_H
#define __CONTROLFRAMEWORKAPPLICATION_H

#include <eikapp.h>

//Application class
class CControlFrameworkApplication : public CEikApplication
    {
public:
    static CApaApplication* NewApplication();
    ~CControlFrameworkApplication();
    
private:
    CControlFrameworkApplication();
    
    // from CApaApplication
    TUid AppDllUid() const;
    CApaDocument* CreateDocumentL();
    };


#endif // __CONTROLFRAMEWORKAPPLICATION_H

ControlFrameworkDocument.h

// ControlFrameworkDocument.h
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#ifndef __CONTROLFRAMEWORKDOCUMENT_H
#define __CONTROLFRAMEWORKDOCUMENT_H

#include <eikdoc.h>

class CEikAppUi;
class CEikApplication;

//Document class
class CControlFrameworkDocument : public CEikDocument
    {
public:
    static CControlFrameworkDocument* NewL(CEikApplication& aApp);
    ~CControlFrameworkDocument();
    
private:
    CControlFrameworkDocument(CEikApplication& aApp);
    
    // from CEikDocument
    CEikAppUi* CreateAppUiL();
    };


#endif // __CONTROLFRAMEWORKDOCUMENT_H

ControlFrameworkAppUi.h

// ControlFrameworkAppUi.h
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#ifndef  __CONTROLFRAMEWORKAPPUI_H
#define  __CONTROLFRAMEWORKAPPUI_H

#include <eikappui.h>

class CControlFrameworkView;

//Application UI class
class CControlFrameworkAppUi : public CEikAppUi
    {
public:
    CControlFrameworkAppUi();
    ~CControlFrameworkAppUi();
    void AddViewL(CControlFrameworkView* aView);
    enum TViews
        {
        EExampleView
        };
        
    // from CEikAppUi
    void ConstructL();
            
private:
    void HandleCommandL(TInt);

private:
    RPointerArray<CControlFrameworkView> iAppViews;
    };


#endif // __CONTROLFRAMEWORKAPPUI_H

ControlFrameworkView.h

// ControlFrameworkView.h
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#ifndef __CONTROLFRAMEWORKVIEW_H
#define __CONTROLFRAMEWORKVIEW_H

#include <coecntrl.h>
#include <coeview.h>
#include <Babitflags.h> 

class CControlFrameworkAppUi;

//View class
class CControlFrameworkView : public CCoeControl, public MCoeView
    {
public:
    static CControlFrameworkView* NewLC(CControlFrameworkAppUi& iAppUi);
    ~CControlFrameworkView();
    
    TBool IsBold() const;
    TBool IsItalic() const;
    TBool IsUnderline() const;
    TBool IsStrikethrough() const;
    void ToggleBold();
    void ToggleItalic();
    void ToggleUnderline();
    void ToggleStrikethrough();

    // from MCoeView
    TVwsViewId ViewId() const;
    
protected:
    CControlFrameworkView(CControlFrameworkAppUi& iAppUi);
    void DrawBorder(TRect& aRect) const;
    void DrawMessage(const TRect& aRect) const;

    // from MCoeView
    void ViewConstructL();
    void ViewActivatedL(const TVwsViewId &aPrevViewId, TUid aCustomMessageId, const TDesC8 &aCustomMessage);
    void ViewDeactivated();
    
    // from CCoeControl
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
    void HandlePointerEventL(const TPointerEvent& aPointerEvent);
    void Draw( const TRect& /*aRect*/ ) const;
    
private:
    CControlFrameworkAppUi& iAppUi;
    TBidiText* iBidiText;

    RRunInfoArray iRunInfoArray;
    TCoeFont iFont;
    
    enum TCommandBools
        {
        EBold,
        EItalic,
        EUnderline,
        EStrikethrough
        };
    TBitFlags iFontFlags;
    };



#endif // __CONTROLFRAMEWORKVIEW_H

ControlFrameworkGlobals.h

// ControlFrameworkGlobals.h
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//
// File is called 'globals' because other applications may need to import this file 
// to use the view IDs defined here.

#ifndef __CONTROLFRAMEWORKGLOBALS_H
#define __CONTROLFRAMEWORKGLOBALS_H

const TUid KUidControlFrameworkAppUid = { 0xE80000A5 }; 
const TUid KUidControlFrameworkView =   { 0x00000001 };

#endif // __CONTROLFRAMEWORKGLOBALS_H

ControlFramework.hrh

// ControlFramework.hrh
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#define ECmdToggleBoldFont  20
#define ECmdToggleItalicFont  21
#define ECmdToggleUnderline  22
#define ECmdToggleStrikethrough  23

ControlFrameworkApplication.cpp

// ControlFrameworkApplication.cpp
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#include <eikstart.h> 

#include "ControlFrameworkApplication.h"
#include "ControlFrameworkGlobals.h"
#include "ControlFrameworkDocument.h"


//Standard DLL entry point function.
TInt E32Main()
    {
    return EikStart::RunApplication( CControlFrameworkApplication::NewApplication );
    }

CControlFrameworkApplication::CControlFrameworkApplication()
    {
    }


CControlFrameworkApplication::~CControlFrameworkApplication()
    {
    }

TUid CControlFrameworkApplication::AppDllUid() const
    {
    return KUidControlFrameworkAppUid;
    }


//Creates and returns an instance of the CApaApplication-derived class.
CApaApplication* CControlFrameworkApplication::NewApplication()
    {
    return new CControlFrameworkApplication();
    }

CApaDocument* CControlFrameworkApplication::CreateDocumentL()
    {
    return CControlFrameworkDocument::NewL(*this);
    }

ControlFrameworkDocument.cpp

// ControlFrameworkDocument.cpp
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#include <eikapp.h>

#include "ControlFrameworkDocument.h"
#include "ControlFrameworkAppUi.h"


CControlFrameworkDocument::CControlFrameworkDocument(CEikApplication& aApp) : CEikDocument(aApp)
    {
    }

CControlFrameworkDocument::~CControlFrameworkDocument()
    {
    }

CControlFrameworkDocument* CControlFrameworkDocument::NewL(CEikApplication& aApp)
    {
    return new(ELeave) CControlFrameworkDocument(aApp);
    }

//Called by the UI framework to construct the application UI class. 
//Note that the app UI's ConstructL() is called by the UI framework.
CEikAppUi* CControlFrameworkDocument::CreateAppUiL()
    {
    return new(ELeave) CControlFrameworkAppUi();

ControlFrameworkAppUi.cpp

// ControlFrameworkAppUi.cpp
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#include "ControlFrameworkAppUi.h"
#include "ControlFrameworkView.h"
#include "ControlFramework.hrh"
#include "uikon.hrh"


CControlFrameworkAppUi::CControlFrameworkAppUi()
    {
    }

CControlFrameworkAppUi::~CControlFrameworkAppUi()
    {
    // Deregister the view(s) from the view server, remove the view(s) from the control stack
    // and delete them
    TInt view = iAppViews.Count();
    while (view--)
        {
        CControlFrameworkView* v = iAppViews[view];
        RemoveFromStack(v);
        DeregisterView(*v);
        }
    iAppViews.ResetAndDestroy();
    }

// Second phase constructor of the application UI.
// It creates and owns a single view. The view is not fully constructed 
// (via ViewConstructL()) until the first time the view is activated,
// to improve application startup time.
void CControlFrameworkAppUi::ConstructL()
    {
    BaseConstructL(CEikAppUi::EStandardApp);
    
     // Create the view
    CControlFrameworkView* view = CControlFrameworkView::NewLC(*this);
    AddViewL(view);
    CleanupStack::Pop(view);
    }


// Called by the UI framework when a command has been issued 
// by one of the toolbar buttons.
void CControlFrameworkAppUi::HandleCommandL(TInt aCommandId)
    {
    CControlFrameworkView& view = *iAppViews[EExampleView];
    switch (aCommandId)
        {
        // command IDs are defined in the hrh file.
        case ECmdToggleBoldFont:
            view.ToggleBold();
            break;
        
        case ECmdToggleItalicFont: 
            view.ToggleItalic();
            break;
        
        case ECmdToggleUnderline: 
            view.ToggleUnderline();
            break;
            
        case ECmdToggleStrikethrough: 
            view.ToggleStrikethrough();
            break;
                
        default:                
        case EEikCmdExit:
            Exit();
        }
    }


static void CleanupRemoveFromStack(TAny* aView)
    { 
    static_cast<CEikAppUi*>(CCoeEnv::Static()->AppUi())->RemoveFromStack(static_cast<CControlFrameworkView*>(aView)); 
    }

static void CleanupDeregisterView(TAny* aView) 
    { 
    static_cast<CEikAppUi*>(CCoeEnv::Static()->AppUi())->DeregisterView(*static_cast<CControlFrameworkView*>(aView)); 
    }

// Adds the view to the app UI's control stack and registers it with the view server. 
// Takes ownership of the view.
void CControlFrameworkAppUi::AddViewL(CControlFrameworkView* aView)
    {
    ASSERT(aView);
    ASSERT(KErrNotFound == iAppViews.Find(aView));
        
    RegisterViewL(*aView); // Call RegisterViewL before AddToStackL to avoid panic in destructor if AddToStackL leaves 
    CleanupStack::PushL(TCleanupItem(CleanupDeregisterView,aView)); 
    
    AddToStackL(*aView, aView); 
    CleanupStack::PushL(TCleanupItem(CleanupRemoveFromStack, aView)); 
    
    iAppViews.AppendL(aView); 
    CleanupStack::Pop(2); 
    }

ControlFrameworkAppView.cpp

// ControlFrameworkAppView.cpp
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

#include <eikenv.h>
#include <ControlFramework.rsg>

#include "ControlFrameworkView.h"
#include "ControlFrameworkAppUi.h"
#include "ControlFrameworkGlobals.h"

// Maximum number of lines that the text can be split into 
const TInt KMaxLines = 2;
// Maximum number of characters that the text object can hold
const TInt KMaxStringLength = 64;
_LIT(KPointerEventMessage, "Pointer event %d at (%d,%d)");
_LIT(KKeyEventMessage, "Key 0x%x, modifier 0x%x");

CControlFrameworkView::CControlFrameworkView(CControlFrameworkAppUi& aAppUi) : iAppUi(aAppUi), iFont(TCoeFont::ELarge, TCoeFont::EPlain)
    {
    }

CControlFrameworkView* CControlFrameworkView::NewLC(CControlFrameworkAppUi& aAppUi)
    {
    CControlFrameworkView* view = new(ELeave) CControlFrameworkView(aAppUi);
    CleanupStack::PushL(view);
    return view;
    }

CControlFrameworkView::~CControlFrameworkView()
    {
    iRunInfoArray.Close();
    delete iBidiText;
    }

// Called by the UI framework the first time the view is activated.
// As much as possible of the code normally found in ConstructL() has 
// been moved here to improve application startup time.
void CControlFrameworkView::ViewConstructL()
    {
    // The control is window-owning.
    CreateWindowL();

    iBidiText = TBidiText::NewL(KMaxStringLength, KMaxLines);
    iRunInfoArray.OpenL();
    HBufC* message = iEikonEnv->AllocReadResourceLC(R_WELCOME_TEXT);
    iBidiText->SetText(*message, iRunInfoArray); // TDirectionality determined by resource
    CleanupStack::PopAndDestroy(message);
    
    // Set extent of the view. ClientRect calculates to ScreenSize minus Toolbar, Menubar, Statusbar etc.
    SetRect(iAppUi.ClientRect());
    
    // The control is ready to draw, so notify the UI framework.
    ActivateL();
    }   


// Uniquely identifies the view 
TVwsViewId CControlFrameworkView::ViewId() const
    {
    return TVwsViewId(KUidControlFrameworkAppUid, KUidControlFrameworkView);
    }


void CControlFrameworkView::ViewActivatedL(const TVwsViewId& /*aPrevViewId*/, TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
    {
    }

void CControlFrameworkView::ViewDeactivated()
    {
    }

// Receives pointer events and prepares to write a description of 
// the pointer event to the screen.
void CControlFrameworkView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
    {
    TBuf<KMaxStringLength> text;
    text.Format(KPointerEventMessage, aPointerEvent.iType,aPointerEvent.iPosition.iX,aPointerEvent.iPosition.iY);
    iBidiText->SetText(text, TBidiText::ELeftToRight, iRunInfoArray);
      Window().Invalidate();
    }

// Draws the border and the message text
void CControlFrameworkView::Draw(const TRect& aRect) const
    {
    TRect rect = aRect;
    DrawBorder(rect);
    DrawMessage(rect);
    }

// Draws a 5 pixel wide border around the window 
void CControlFrameworkView::DrawBorder(TRect& aRect) const
    {
    CWindowGc& gc=SystemGc();
    gc.SetPenSize(TSize(5,5));
    gc.DrawRect(aRect);
    aRect.Shrink(5,5);
    }

// Displays the message stored in iBidiText centered vertically and horizontally.
void CControlFrameworkView::DrawMessage(const TRect& aRect) const
    {
    CWindowGc& gc=SystemGc();

    if (IsStrikethrough())
        gc.SetStrikethroughStyle(EStrikethroughOn);
    
    if (IsUnderline())
        gc.SetUnderlineStyle(EUnderlineOn);

    XCoeTextDrawer textDrawer(TextDrawer());
    textDrawer->SetAlignment(EHCenterVCenter);
    const CFont& font = ScreenFont(iFont);
    iBidiText->WrapText((aRect.iBr.iX - aRect.iTl.iX) , font, NULL, KMaxLines);
    textDrawer.DrawText(gc, *iBidiText, aRect, font);
 }

/*
Moves the window and prints key event information to the screen.
The following key combinations cause the window to move: 
1. Shift + KeyLeft arrow
2. Shift + KeyRight arrow
3. Shift + KeyDown arrow
4. Shift + KeyUp arrow
*/
TKeyResponse CControlFrameworkView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    if (aType == EEventKey)
        {
        TInt modifiers=aKeyEvent.iModifiers;
        TInt code=aKeyEvent.iCode;

        // Write a description of the key event to the screen
        TBuf<KMaxStringLength> text;
        text.Format(KKeyEventMessage, code,modifiers);
        iBidiText->SetText(text, TBidiText::ELeftToRight, iRunInfoArray);
        Window().Invalidate();
        
        if (modifiers&EModifierShift)
            {
            TPoint pos=Position();
            switch (code)
                {
                case EKeyLeftArrow:
                    pos.iX--;
                    break;
                case EKeyRightArrow:
                    pos.iX++;
                    break;
                case EKeyUpArrow:
                    pos.iY--;
                    break;
                case EKeyDownArrow:
                    pos.iY++;
                    break;
                default:
                    break;
                }
            if (pos != Position())
                {
                SetPosition(pos);
                return(EKeyWasConsumed);
                }
            }
        }
    return(EKeyWasNotConsumed);
    }

TBool CControlFrameworkView::IsStrikethrough() const
    {
    return iFontFlags.IsSet(EStrikethrough);
    }

void CControlFrameworkView::ToggleStrikethrough()
    {
    if (IsStrikethrough())
        iFontFlags.Clear(EStrikethrough);
    else 
        iFontFlags.Set(EStrikethrough);
    
    Window().Invalidate();
    }     
    
TBool CControlFrameworkView::IsUnderline() const
    { 
    return iFontFlags.IsSet(EUnderline);
    }

void CControlFrameworkView::ToggleUnderline()
    {
    if (IsUnderline())
        iFontFlags.Clear(EUnderline);
    else 
        iFontFlags.Set(EUnderline);
    
    Window().Invalidate();
    } 
    
TBool CControlFrameworkView::IsBold() const
    {
    return iFontFlags.IsSet(EBold);
    }

void CControlFrameworkView::ToggleBold()
    {
    if (IsBold())
        {
        iFontFlags.Clear(EBold);
        iFontFlags.Clear(EItalic);
        iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EPlain);
        }
    else 
        {
        iFontFlags.Set(EBold);
        iFontFlags.Clear(EItalic);
        iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EBold);
        }
    
    Window().Invalidate();
    }     
    
TBool CControlFrameworkView::IsItalic() const
    { 
    return iFontFlags.IsSet(EItalic);
    }
    

void CControlFrameworkView::ToggleItalic()
    {
    if (IsItalic())
        {
        iFontFlags.Clear(EBold);
        iFontFlags.Clear(EItalic);
        iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EPlain);
        }
    else 
        {   
        iFontFlags.Set(EItalic);
        iFontFlags.Clear(EBold);
        iFont = TCoeFont(iFont.LogicalSize(), TCoeFont::EItalic);
        }
        
    Window().Invalidate();
    }  
// BLD.INF
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

PRJ_MMPFILES

ControlFramework.mmp
// ControlFramework.mmp
//
// Copyright (c) 2006 Symbian Software Ltd.  All rights reserved.
//

TARGET      ControlFramework.exe
TARGETTYPE  exe

UID     0x100039CE 0xE80000A5
VENDORID    0x70000001

SOURCEPATH  ..\src
SOURCE      ControlFrameworkApplication.cpp
SOURCE      ControlFrameworkDocument.cpp
SOURCE      ControlFrameworkAppUi.cpp
SOURCE      ControlFrameworkView.cpp

USERINCLUDE ..\inc
SYSTEMINCLUDE   \epoc32\include
SYSTEMINCLUDE   \epoc32\include\techview

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

// Resource file
SOURCEPATH  ..\group
START RESOURCE ControlFramework.rss
HEADER
TARGETPATH  \resource\apps
END

// Registration file
SOURCEPATH  .
START RESOURCE ControlFramework_reg.rss
TARGETPATH  \private\10003a3f\apps
END

// Localization file
SOURCEPATH  .
START RESOURCE ControlFramework_loc.rss
TARGETPATH  \resource\apps
END

START bitmap   ControlFramework_icon.mbm
HEADER
TARGETPATH  \resource\apps
SOURCEPATH  ..\image
SOURCE      c8 ControlFramework_24.BMP ControlFramework_2M.BMP
SOURCE      c8 ControlFramework_32.BMP ControlFramework_3M.BMP
SOURCE      c8 ControlFramework_48.BMP ControlFramework_4M.BMP
END 

[Top]


Description

This example program creates an application that uses a single view. It implements the standard classes required by any application using UI controls. That is, an application, a document, an application UI and a view.

A view is a control (derived from CCoeControl) that displays the application's data on the screen and handles user input. The pointer and key input is handled by printing to the screen a description of the key event, or the x,y coordinates of the pointer event.

The application and document are implemented minimally. The application UI handles commands by overriding the CEikAppUi::HandleCommandL() method. In this example, commands are generated by the toolbar buttons, and are handled by changing the formatting of the text. The text that is initially displayed is read from a resource file.

[Top]


Class summary

Related APIs

[Top]


Build

The Symbian OS build process describes how to build an application.

The ControlFramework example builds an executable called ControlFramework.exe in the standard location (\epoc32\release\winscw\<build_variant> for CodeWarrior). Either launch the executable itself, or launch the emulator and then select the ControlFramework application from the Emulator's extras bar.

[Top]


See also

Using UI Control Framework (CONE)