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

📄 bookstorelistboxview.cpp

📁 Symbian s60 3RD 数据库开发实例
💻 CPP
字号:
/*
 * ============================================================================
 *  Name     : BookstoreListboxView from BookstoreListboxView.cpp
 *  Part of  : Bookstore
 *  Created  : 8.12.2003 by Forum Nokia
 *  Version  : 1.0
 *  Copyright: Nokia Corporation
 * ============================================================================
 */

#include <eikenv.h>
#include <avkon.hrh>
#include <gdi.h>
#include <eiktxlbx.h>  // CEikTextListBox
#include <eiklabel.h>  // CEikLabel
#include <eiktxlbm.h>  // CTextListBoxModel
#include "BookstoreListboxView.h"


// ---------------------------------------------------------------------------
// CBookstoreListboxView::NewL()
//
// Create instance of this view.
// ---------------------------------------------------------------------------
//
CBookstoreListboxView* CBookstoreListboxView::NewL(const TRect& aRect)
    {
    CBookstoreListboxView* self = new (ELeave) CBookstoreListboxView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    CleanupStack::Pop(self);
    return self;
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::ConstructL()
//
// Perform the second phase construction. Construct the contained controls.
// ---------------------------------------------------------------------------
//
void CBookstoreListboxView::ConstructL(const TRect& aRect)
    {
    // Create a window for this application view
    CreateWindowL();

    iLabel = new (ELeave) CEikLabel();
    iLabel->SetTextL(_L("Listbox view"));
    iLabel->SetContainerWindowL( *this );

    iListBox = new(ELeave)CEikTextListBox();
    iListBox->ConstructL( this);
    iListBox->SetContainerWindowL( *this );

    // Set the windows size
    SetRect(aRect);
    ActivateL();
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::SetCaptionL()
//
// Set caption for this view. Updates the label.
// ---------------------------------------------------------------------------
//
void CBookstoreListboxView::SetCaptionL(const TDesC& aNewCaption)
    {
    iLabel->SetTextL(aNewCaption);
    SizeChanged();
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::SetListItems()
//
// Sets the listbox array and updates the view to reflect new array items.
// The listbox takes ownership of the given aNewItems array.
// ---------------------------------------------------------------------------
//
void CBookstoreListboxView::SetListItems(CDesCArrayFlat* aNewItems)
    {
    CTextListBoxModel* model = iListBox->Model();
    model->SetItemTextArray(aNewItems);
    // Set ListBox model responsible for deleting the listItems array
    model->SetOwnershipType( ELbmOwnsItemArray );
    iListBox->HandleItemAdditionL();
    if(aNewItems->Count()>0)
        {
        // Select the first item, if there is one.
        iListBox->SetCurrentItemIndexAndDraw(0);
        }
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::CBookstoreListboxView()
//
// Constructor
// ---------------------------------------------------------------------------
//
CBookstoreListboxView::CBookstoreListboxView()
    {
    // No implementation required
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::~CBookstoreListboxView()
//
// Desctructor. Delete contained controls
// ---------------------------------------------------------------------------
//
CBookstoreListboxView::~CBookstoreListboxView()
    {
    delete iLabel;
    delete iListBox;
    }


// ---------------------------------------------------------------------------
// CBookstoreAppUi::Draw()
//
// Draw the view background. This is called by the framework, when necessary.
//
// Note: The framework will draw the contained controls (label and the listbox)
// by using CountComponentControls() and ComponentControl() methods.
// ---------------------------------------------------------------------------
//
void CBookstoreListboxView::Draw(const TRect& /*aRect*/) const
    {
    //Clear the screen
    CWindowGc& gc = SystemGc();
    gc.Clear(Rect());
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::SizeChanged()
//
// Specify locations and sizes of the label and listbox. Called by
// the framework when the view size is changed
// ---------------------------------------------------------------------------
//
void CBookstoreListboxView::SizeChanged()
    {
        TRect containerRect = Rect(); // Rect of the container

        TSize labelSize = iLabel->MinimumSize();

        // Show the label in the upper part of the screen and in whole
        // screen width
        iLabel->SetExtent( TPoint(5,2),
                           TSize(containerRect.Width(), labelSize.iHeight) );

        // Fill the rest (bottom) of the screen with listbox
        iListBox->SetExtent( TPoint(5,labelSize.iHeight + 5 ),
            TSize( containerRect.Width(),
                   containerRect.Height()-labelSize.iHeight ) );
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::CountComponentControls()
//
// Return number of contained controls.
// ---------------------------------------------------------------------------
//
TInt CBookstoreListboxView::CountComponentControls() const
    {
        return 2; // Label and listbox
    }


// ---------------------------------------------------------
// CBookstoreListboxView::ComponentControl()
//
// Return owned controls to framework so it will draw them.
// ---------------------------------------------------------
//
CCoeControl* CBookstoreListboxView::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
            return iLabel;
        case 1:
            return iListBox;
        default:
            return NULL;
        }
    }

// ---------------------------------------------------------------------------
// CBookstoreListboxView::OfferKeyEventL()
//
// Handle key events. Let the listbox handle the key events.
//
// AppUi must add this control to control stack to ensure key events are
// received and this method is called.
// ---------------------------------------------------------------------------
//
TKeyResponse CBookstoreListboxView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
    TEventCode aType)
    {
        return iListBox->OfferKeyEventL( aKeyEvent, aType );
    }


// ---------------------------------------------------------------------------
// CBookstoreListboxView::GetSelectedItemL()
//
// Get name of the selected item in the listbox.
// ---------------------------------------------------------------------------
//
TInt CBookstoreListboxView::GetSelectedItemL(TDes& aResult) const
    {
    CTextListBoxModel* model = iListBox->Model();
    if(model->NumberOfItems()==0)
        {
        return KErrNotFound;
        }

    aResult.Copy(model->ItemText(iListBox->CurrentItemIndex()));
    return KErrNone;
    }

⌨️ 快捷键说明

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