⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 choicelistexcontainer.cpp

📁 symbian touch ChoiceList 示例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**
 * Copyright (c) 2007 Nokia Corporation.
 * This material, including documentation and any related
 * computer programs, is protected by
 * copyright controlled by Nokia Corporation.
 */

#include <coemain.h>
#include <badesca.h> 
#include <eiklabel.h> 
#include <aknbutton.h> 
#include <aknchoicelist.h>
#include <barsread.h>
#include <choicelistex.rsg>
#include <aknsdrawutils.h>
#include <aknsbasicbackgroundcontrolcontext.h>

#include "ChoiceListExContainer.h"


// ============================== CONSTANTS ==================================

const TInt KArrayGranularity ( 1 );
const TRect KListRect( TPoint( 120, 200 ), TPoint( 400, 250 ) );
const TRect KLabelRect( TPoint( 20, 50 ), TPoint( 450, 100 ) );
                                      
// Texts for choice list items
_LIT( KListItem, "Item index %d" );
_LIT( KListItemNew, "New Item" );

// Message texts
_LIT( KAppName," Choicelist example" );
_LIT( KNoList," No choice list created" );
_LIT( KNoItems, "No items on list");
_LIT( KCreate, " Choice list created" );
_LIT( KSelect, "Selected item index %d" );
_LIT( KAdd, "Added item index %d" );
_LIT( KInsert, "Inserted item to index %d" );
_LIT( KShow, "Choice list opened" );
_LIT( KRemove, "Removed item index %d" );
_LIT( KButtonTxt, "Choice list" );
_LIT( KButtonHelpTxt, "Button help text" );
_LIT( KFlag," List position changed" );

// =========================== MEMBER FUNCTIONS ==============================


// ---------------------------------------------------------------------------
// C++ default Constructor
// ---------------------------------------------------------------------------
//
CChoiceListExContainer::CChoiceListExContainer()
    {
    }

// ---------------------------------------------------------------------------
// Destructor
// ---------------------------------------------------------------------------
//
CChoiceListExContainer::~CChoiceListExContainer()
    {

    delete iLabel;
    delete iBgContext;

    if ( iChoiceList )
        {
        delete iChoiceList;
        }
    }

// ---------------------------------------------------------------------------
// Symbian 2nd Constructor
// ---------------------------------------------------------------------------
//
void CChoiceListExContainer::ConstructL( const TRect& aRect )
    {
    CreateWindowL();
    SetRect( aRect );

    // Label for showing messages    
    iLabel = new ( ELeave ) CEikLabel;
    iLabel->SetLabelAlignment( ELayoutAlignCenter );
    iLabel->SetContainerWindowL( *this );
    iLabel->SetTextL( KAppName );
    iLabel->SetRect( KLabelRect );
    
    // Background context for skinned background
    iBgContext = CAknsBasicBackgroundControlContext::NewL( 
        KAknsIIDSkinBmpMainPaneUsual, TRect( 0, 0, 0, 0 ), ETrue );
    iBgContext->SetRect( Rect() );
    iBgContext->SetParentPos( PositionRelativeToScreen() );

    SizeChanged();
    ActivateL();
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::Draw
// ---------------------------------------------------------------------------
//
void CChoiceListExContainer::Draw( const TRect& aRect ) const
    {
    // Draw background
    CWindowGc& gc = SystemGc();
    MAknsSkinInstance* skin = AknsUtils::SkinInstance();
    if ( !AknsDrawUtils::Background( skin, iBgContext, gc, aRect ) )
        {
        SystemGc().Clear( aRect );
        }
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::CountComponentControls
// ---------------------------------------------------------------------------
//
TInt CChoiceListExContainer::CountComponentControls() const
    {
    TInt count ( 1 ); // iLabel

    if ( iChoiceList )    
        {
        ++count;
        }
        
    return count;
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::ComponentControl
// ---------------------------------------------------------------------------
//
CCoeControl* CChoiceListExContainer::ComponentControl( TInt aIndex ) const
    {
    if ( aIndex == 0 )
        {
        return iLabel;
        }
    else if ( aIndex == 1 )
        {
        return iChoiceList;
        }
        
    return NULL;
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::OfferKeyEventL
// Handles key events by passing them to choice list
// ---------------------------------------------------------------------------
//
TKeyResponse CChoiceListExContainer::OfferKeyEventL( 
    const TKeyEvent& aKeyEvent,TEventCode aType )
    {
    if ( iChoiceList )
        {
        return iChoiceList->OfferKeyEventL( aKeyEvent, aType );    
        }
    return EKeyWasNotConsumed;
    }
    
// ---------------------------------------------------------------------------
// CChoiceListExContainer::HandleControlEventL
// Handles events from the Choice List
// ---------------------------------------------------------------------------
//
void CChoiceListExContainer::HandleControlEventL( CCoeControl* aControl,
                                                  TCoeEvent aEventType )
    {
    if ( aControl == iChoiceList )
        {
        TInt selection;
        switch ( aEventType )
            {
            case EEventStateChanged:
                selection = iChoiceList->SelectedIndex();
                if ( iSelection != selection )
                    {
                    // selected item changed
                    iSelection = selection;
                    TBuf<32> buf;
                    buf.Format( KSelect, selection );
                    iLabel->SetTextL( buf );
                    iLabel->DrawDeferred();
                    }
                break;
                    
            default:
                break;
            }
        }
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::CreateDefaultChoiceListL
// Deletes previous Choice list if exists and constructs a new one with an
// array of items.
// ---------------------------------------------------------------------------
//
void CChoiceListExContainer::CreateDefaultChoiceListL()
    {
    if ( iChoiceList )
        {
        delete iChoiceList;
        iChoiceList = NULL;
        }
   
    // Create array of choice list items        
    CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat ( KArrayGranularity );
    CleanupStack::PushL( array );
    
    for ( TInt k = 0; k < 10; k++ )
        {
        TBuf<32> buf;
        buf.Format( KListItem, k );
        array->AppendL( buf );
        }

    iArraySize = array->Count();
    iSelection = 0;
    
    // Create choice list with array
    iChoiceList = CAknChoiceList::NewL( this, array );
    
    CleanupStack::Pop( array );
    
    iChoiceList->SetObserver( this );
    iChoiceList->SetRect( KListRect );

    iLabel->SetTextL( KCreate );
    }

// ---------------------------------------------------------------------------
// CChoiceListExContainer::CreateButtonChoiceListL
// Deletes previous Choice list if exists and constructs a new one with an
// button and item array.
// ---------------------------------------------------------------------------
//
void CChoiceListExContainer::CreateButtonChoiceListL()
    {
    if ( iChoiceList )
        {
        delete iChoiceList;
        iChoiceList = NULL;
        }
           
    // Create array of choice list items        
    CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat ( KArrayGranularity );
    
    CleanupStack::PushL( array );

⌨️ 快捷键说明

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