Scroller API: Using Scroller API

Creating a scrollbar

A typical scrollbar creation (based on the system's DefaultScrollBarType()) looks like this:

iSBFrame = new ( ELeave ) CEikScrollBarFrame( this, NULL );
CAknAppUiBase* appUi = iAvkonAppUi;

if( AknLayoutUtils::DefaultScrollBarType( appUi ) ==
    CEikScrollBarFrame::EDoubleSpan )
    {
    // window-owning scrollbar, non-remote, vertical, non-horizontal
    iSBFrame->CreateDoubleSpanScrollBarsL( ETrue, EFalse, ETrue, EFalse );
    iSBFrame->SetTypeOfVScrollBar( CEikScrollBarFrame::EDoubleSpan );
    }
else
    {
    iSBFrame->SetTypeOfVScrollBar( CEikScrollBarFrame::EArrowHead );
    }
iSBFrame->SetScrollBarVisibilityL(
    CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto );

Updating ArrowHead scrollbar attributes

Updating the scrollbar is done via the TEikScrollBarModel object. For ArrowHead scrollbar the model attributes are public, they can be set with direct access.

Assuming a list–based control, we can use the number of elements as scrollspan, the actual element's index as thumbposition, and a hard coded value as window size.

// setting ArrowHead attributes via public members of 
// TEikScrollBarModel iArrowAttributes
iArrowAttributes.iThumbPosition = ActualElement();
iArrowAttributes.iThumbSpan = 10;
iArrowAttributes.iScrollSpan = ElementCount();

// updating model using generic scrolbar handle
iSBFrame->GetScrollBarHandle( 
    CEikScrollBar::EVertical )->SetModel( &ArrowAttributes );

Updating DoubleSpan scrollbar attributes

DoubleSpan's model (TAknDoubleSpanScrollBarModel) does not have public attributes. Setting the field size and position are not mandatory, 0 value means a simple span-typed scrollbar; if they are set, a real double span scrollbar is used.

SetModel() function generates a redraw event. More update step can also be performed without drawing, for example setting up the background state; a direct draw updates the scrollbar in this case.

// setting DoubleSpan attributes via public setter functions of
// TAknDoubleSpanScrollBarModel iDoubleSpanAttributes
iDoubleSpanAttributes.SetScrollSpan( iElements->Count() );
iDoubleSpanAttributes.SetFocusPosition( iActualElement );
iDoubleSpanAttributes.SetWindowSize( WindowHeight() / KElementHeight );
iDoubleSpanAttributes.SetFieldSize( KElementHeight*2 );
iDoubleSpanAttributes.SetFieldPosition( 10 );

// updating model using specific vertical handle
iSBFrame->VerticalScrollBar()->SetModel( &iDoubleSpanAttributes );

_OR_

// updating model using direct draw request
iSBFrame->Tile( &iDoubleSpanAttributes );
iSBFrame->DrawScrollBarsNow();

Observing scrollbar events

Standard S60 callback mechanism is used for scrollbar events. Various events such as thumb dragging, button pressed or page changed are generated. The MEikScrollBarObserver class defines them and the callback function.

// defining observer class, CSbObserver
#include <eiksbobs.h>

class CSbObserver : public CBase, public MEikScrollBarObserver
    {
    public:
        CSbObserver();
    public:
        void HandleScrollEventL( 
            CEikScrollBar* aScrollBar, 
            TEikScrollEvent aEventType );
    };
// create and assign observer class
iScrollObserver = new (ELeave) CSbObserver;
iSBFrame->SetScrollBarFrameObserver( iScrollObserver );

Scrollbar thumb data are already modified by the time this callback function is called. The client may update its internal state according to the change:

// the callback function
CSbObserver::HandleScrollEventL( CEikScrollBar* aScrollBar, TEikScrollEvent aEventType )
    {
    switch( aEventType )
        {
        case EEikScrollHome :
            SetActualElment( 0 );
            break;
        case EEikScrollEnd :
            SetActualElement( LastElement() );
            break;
        default:
            SetActualElement( aScrollBar->ThumbPosition() );
            break;
        }
    }

Error handling

Scroller Service API uses standard Symbian OS error reporting mechanism. Leaves and system wide error codes as function return values are used if the error is recoverable. A client application can handle these errors similarly as a normal Symbian platform application.

Limitations of the API

There is no way to define custom images for arrows, thumb, shaft.


Copyright © Nokia Corporation 2001-2008
Back to top