Using button states

The states can also be added to button dynamically. To add button states as required, use the method AddStateL() in the class CAknButton.

// Add state with button text and help text 
HBufC* stateText = StringLoader::LoadLC( R_MYAPP_BUTTON_TEXT );    
HBufC* stateHelpText = StringLoader::LoadLC( R_MYAPP_BUTTON_TEXT ); 
button->AddStateL( 0, 0, 0, 0, *stateText, *stateHelpText, 0 ); 
CleanupStack::PopAndDestroy( 2 ); // stateText, stateHelpText 

// Add state with icon 
… 
#include <avkon.mbg> 
… 
TPtrC8 pathPtr( ( TUint8* ) AVKON_BITMAP_FILE ); 
HBufC *filePath = HBufC::NewLC( pathPtr.Length() ); 
filePath->Des().Copy( pathPtr ); 
button->AddStateL( *filePath, 
                   EMbmAvkonQgn_indi_volume_arrow_down, 
                   EMbmAvkonQgn_indi_volume_arrow_down_mask, 
                   0, 0, 
                   EMbmAvkonQgn_indi_volume_arrow_down, 
                   EMbmAvkonQgn_indi_volume_arrow_down_mask, 
                   0, 0, 
                   KNullDesC, KNullDesC, 0, 
                   KAknsIIDQgnIndiVolumeArrowDown, 
                   KAknsIIDQgnIndiVolumeArrowDownInactive, 
                   KAknsIIDQgnIndiVolumeArrowDownSelected, 
                   KAknsIIDDefault ); 
CleanupStack::PopAndDestroy( filePath ); 

The following sample code defines three different kinds of buttons with button states in a resource file.

#include <avkon.mbg> 
#include <aknsconstants.hrh> 

… 

RESOURCE AVKON_BUTTON r_myapp_command_button 
    { 
    flags  = 0; 
    states = 
        { 
        AVKON_BUTTON_STATE 
            { 
            bmpfile = AVKON_BITMAP_FILE; 
            bmpid = EMbmAvkonQgn_indi_volume_arrow_down; 
            bmpmask = EMbmAvkonQgn_indi_volume_arrow_down_mask; 
            press_bmpid = EMbmAvkonQgn_indi_volume_arrow_down; 
            press_bmpmask = EMbmAvkonQgn_indi_volume_arrow_down_mask; 
            helptxt = "Volume down"; 
            // Skinning support for state icon 
            extension = r_myapp_command_button_extension; 
            } 
        }; 
    } 

RESOURCE AVKON_BUTTON_STATE_EXTENSION r_myapp_command_button_extension 
    { 
    bmbSkinIdMajor = EAknsMajorGeneric; 
    bmbSkinIdMinor = EAknsMinorGenericQgnIndiVolumeArrowDown; 
    } 

RESOURCE AVKON_BUTTON r_myapp_onoff_button 
    { 
    flags  = 0; 
    states = 
        { 
        AVKON_BUTTON_STATE 
            { 
            txt = "Off"; 
            helptxt = "Turn on"; 
            }, 
            AVKON_BUTTON_STATE 
            { 
            flags = KAknButtonStateHasLatchedFrame; 
            txt = "On"; 
            helptxt = "Turn off"; 
            } 
        }; 
    } 

RESOURCE AVKON_BUTTON r_myapp_mode_button 
    { 
    flags  = 0; 
    states = 
        { 
        AVKON_BUTTON_STATE 
            { 
            txt = "Mode 1"; 
            helptxt = "Switch to 2"; 
            }, 
        AVKON_BUTTON_STATE 
            { 
            txt = "Mode 2"; 
            helptxt = "Switch to 3"; 
            }, 
        AVKON_BUTTON_STATE 
            { 
            txt = "Mode 3"; 
            helptxt = "Switch to 1"; 
            } 
        }; 
    } 

To set current state for the button, use the method SetCurrentState() in the class CAknButton. To get a pointer to the current state of the button, use State() method. The index of the current state can be acquired with the method CAknButton::StateIndex().

if ( button->StateIndex() == 0 ) 
{ 
      button->SetCurrentState( 1, ETrue ); 
}