UIQ Technology
Symbian OS Library

UIQ 3.1 SDK        UIQ developer portal

[Index] [Spacer] [Previous] [Next]



UIQ Controls - Label


1. Introduction

This guide explains the UIQ control Label (CEikLabel). Label is used to display text that is not intended to be edited. The control supports bidirectional text; if the application language is a right-to-left script the text will be reversed (do not apply to left-to-right texts) and aligned to the right.

The following functionality can be used by the application developer:

By default, the following configuration applies:

Examples of Labels

Label with default settings

Label with background

Label with dimmed text


1.1 Further Reference

See the API documentation for Label CEikLabel.

Label is used in the Labeled Check Box. See the How To guide for Labeled Check Box.

See even the API documentation for Labeled Check Box CEikLabeledCheckBox.

[Top]


2. Architecture

Label inherits from CEikAlignedControl.

High-level architecture of Label

High-level architecture of Label

[Top]


3. Using the control

This chapter explains how the control is constructed, used and destroyed. Source code examples are used and explained to illustrate how the Label control is used.


3.1 Includes and identifications

Use the following #include directive:

#include <eiklabel.h>

Use the following LIBRARY directive in the project's mmp-file:

LIBRARY eikcoctl.lib

Use the following control identifier when specifying the control in resource data files. It is used by the framework when constructing the control from resource data:

EEikCtLabel


3.2 Resource Structure

Resource files can be used to construct the Label. The resource to use is defined by the LABEL structure, which is defined in Uikon.rh. The structure looks like this:

STRUCT LABEL
    {
    WORD flags = 0;
    WORD horiz_align=EEikLabelAlignHCenter;
    WORD vert_align=EEikLabelAlignVTop;
    WORD standard_font=EEikLabelFontNormal;
    WORD reserve_length=0;
    LTEXT txt;
    }

The values given in the structure definition are default values. The structure contains the following:

Label can have the following flags:

EikLabelUnderline

Text is drawn with underlining.

EikLabelStrikeThrough

Text is drawn with strikethrough.

EikLabelOverrideAutoWrapping

Switches off auto wrapping.

EikLabelAutoWrappingOn

Used together with the flag above to switch on the auto wrapping.


3.3 Construction

This section discusses four different ways of constructing controls. The first three ways describe how to construct and add a control into the view of an application. The view framework is used in all three cases but in three different ways. The fourth way describes how to construct and launch a dialog from an application. The dialog framework constructs the control and adds it to the dialog.

A common way to construct controls is to specify them in the resource files and let the framework construct them from there. Specifying the controls in resource files is the preferred way of constructing controls since it allows for easier modifications compared to creating them entirely from source code.

This section covers different ways of constructing a Label.

3.3.1 Construction with View Framework Using Data from a Resource File

The example below describes how to construct a Label together with the view framework.

The reason the example seems to be rather complex is because it demonstrates how to construct a complete view containing a Scrollable Container and a Layout Manager. It also encapsulates the Label in a Building Block. The view supports both pen and softkey styles; support of both styles in a view is optional.

1) Declare an enumeration for the controls to be used in the view in a *.hrh file. Hrh files are files to be included both in resource files (*.rss) and C++ files:

/* Declare the control ID in *.hrh file for use both in resource and cpp */
enum TMyViewControls
    {
    EMyViewScrollableContainer,
    EMyViewBuildingBlock,
    EMyViewLabel,
    EMyViewNumberOfControls
    };

2) Declare the controls to be used in the view in your resource (*.rss) file:

/* Declare the set of controls to be used in the view */
RESOURCE QIK_CONTROL_COLLECTION r_my_label_view_controls
    {
    items =
        {
        QIK_CONTROL
            {
            unique_handle = EMyViewScrollableContainer;
            type = EQikCtScrollableContainer;
            control = r_my_label_scroll_pane;
            },
        QIK_CONTROL
            {
            unique_handle = EMyViewLabel;
            type = EEikCtLabel;
            control = r_my_label;
            },
        QIK_CONTROL
            {
            unique_handle = EMyViewBuildingBlock;
            type = EQikCtCaptionedTwolineBuildingBlock;
            control = r_my_label_building_block;
            }
        };
    }

3) Define the view and its contents in your resource file:

/* The view */
RESOURCE QIK_VIEW r_my_label_view
    {
    pages = r_my_label_viewpages;
    }
                
/* The view page */
RESOURCE QIK_VIEW_PAGES r_my_label_viewpages
    {
    pages =
        {
        QIK_VIEW_PAGE
            {
            container_unique_handle = EMyViewScrollableContainer;
            page_content = r_my_label_view_container_details;
            }
        };
    }

4) Define resource for the Scrollable Container used in the view:

/* The scrollable container used in the view */
RESOURCE QIK_SCROLLABLE_CONTAINER r_my_label_scroll_pane
    {
    }

5) Declare contents and properties for the Scrollable Container used in the view:

/* Contents of the scrollable container used in the view */
RESOURCE QIK_SCROLLABLE_CONTAINER_SETTINGS r_my_label_view_container_details
    {
    controls =
        {
        QIK_CONTAINER_ITEM
            {
            unique_handle = EMyViewBuildingBlock;
            }
        };
    }

6) Define the control resource struct used in the view:

/* The Label used in the view */
RESOURCE LABEL r_my_label
    {
    flags=EikLabelUnderline;
    horiz_align=EEikLabelAlignHCenter;
    txt = "Label";
    }

7) Define the settings for the Building Block containing the control:

/* Settings for the EQikCtCaptionedTwolineBuildingBlock containing the Label */
RESOURCE QIK_SYSTEM_BUILDING_BLOCK r_my_label_building_block
    {
    content =
        {
        QIK_SLOT_CONTENT
            {
            slot_id = EQikItemSlot1;
            caption = "Choose:";
            },
        QIK_SLOT_CONTENT
            {
            slot_id = EQikItemSlot2;
            unique_handle = EMyViewLabel;
            }
        };
    }

8) The configurations of the view:

RESOURCE QIK_VIEW_CONFIGURATIONS r_my_label_ui_configurations
    {
    configurations=
        {
        QIK_VIEW_CONFIGURATION
            {
            ui_config_mode = KQikSoftkeyStylePortrait;
            view = r_my_label_view;
            command_list = r_my_label_commands;
            },
        QIK_VIEW_CONFIGURATION
            {
            ui_config_mode = KQikPenStyleTouchPortrait;
            view = r_my_label_view;
            command_list = r_my_label_commands;
            }
        };
    }

9) The command list for the view:

RESOURCE QIK_COMMAND_LIST r_my_label_commands
    {
    items =
        {
        // This command shall only be visible in debug mode because it is only
        // used to find memory leaks during development of the application.
        QIK_COMMAND
            {
            id = EEikCmdExit;
            type = EQikCommandTypeScreen;
            // Indicate that this command will only be visible in debug
            stateFlags = EQikCmdFlagDebugOnly;
            text = "Close (debug)";
            }
        };
    }

10) The view framework constructs the view described in this example with this code:

void CMySinglePageView::ViewConstructL()
    {
    ViewConstructFromResourceL(R_MY_LABEL_UI_CONFIGURATIONS, R_MY_LABEL_VIEW_CONTROLS);
    }

11) The result should look something like this:

Label created from resource

Label created from resource

3.3.2 Construction with Your Own C++ Code Using Data from a Resource File

The example below describes how to construct a Label from resource with your own C++ code.

The reason the example seems to be rather complex is because it demonstrates how to construct a complete view containing a Scrollable Container and a Layout Manager. It also encapsulates the Label in a Building Block.

This example uses the resource structs from the previous example. The following code creates the Label:

#include <eiklabel.h>
#include <QikRowLayoutManager.h>
#include <QikGridLayoutManager.h>
#include <QikBuildingBlock.h>
                
void CMySinglePageView::ViewConstructL()
    {
    // Give a layout manager to the view
    CQikGridLayoutManager* gl = CQikGridLayoutManager::NewLC();
    SetLayoutManagerL(gl);
    CleanupStack::Pop(gl);
    
    // Create a container and give it to the view
    ControlProvider()->ControlInfos().AddFromResourceL(R_MY_LABEL_VIEW_CONTROLS);
    CQikContainerBase* container = static_cast<CQikContainerBase*>(ControlProvider()->ControlConstructIfNeededL(EMyViewScrollableContainer, *this));
    ASSERT(container);
    Controls().AppendLC(container);
    CleanupStack::Pop(container);
    
    // Create a layout manager to be used inside the container
    CQikRowLayoutManager* rowlayout = CQikRowLayoutManager::NewLC();
    container->SetLayoutManagerL(rowlayout);
    CleanupStack::Pop(rowlayout);
    
    // Create the building block (containing a Label) and
    // add it to the container
    CQikBuildingBlock* block = CQikBuildingBlock::CreateSystemBuildingBlockL(EQikCtCaptionedTwolineBuildingBlock);
    container->AddControlLC(block, EMyViewBuildingBlock);
    TResourceReader blockReader;
    iCoeEnv->CreateResourceReaderLC(blockReader, R_MY_LABEL_BUILDING_BLOCK);
    block->ConstructFromResourceL(blockReader, *ControlProvider());
    CleanupStack::PopAndDestroy(); //blockReader
    CleanupStack::Pop(block);
    }

What the code does

1) Initializes the Command Manager with an empty Command List. The controls placed in the view add their commands to the Command List when they receive focus.

2) Creates a Layout Manager for the view. The Grid Layout Manager fills the view with its only control in this example, the Scrollable Container.

3) Loads the control collection R_MY_VIEW_CONTROLS into the Control Provider. Then the Control Provider is asked to create the Scrollable Container.

4) Uses the MopGetObjectNoChaining function to determine that the created control really is a class of type CQikContainerBase before it is added to the view.

5) Creates a Layout Manager to control the layout inside the Container. Adds the Layout Manager to the Container.

6) Constructs the Building Block containing the Label from resource R_MY_BUILDING_BLOCK. Adds the Building Block to the container.

The Label can also be created without a Building Block. In that case, replace the last section in the code above, from the "Create building block..." comment, with this code example:

Since a pointer to the control is declared here, eiklabel.h needs to be included in the cpp-file and eikcoctl.lib in the mmp-file.

// Create the Label and add it to the container
TResourceReader reader;
iEikonEnv->CreateResourceReaderLC(reader, R_MY_LABEL);
CEikLabel* label = new (ELeave) CEikLabel();
container->AddControlLC(label, EMyViewLabel);
label->ConstructFromResourceL(reader);   
label->SetUniqueHandle(EMyViewLabel);
CleanupStack::Pop(label);
CleanupStack::PopAndDestroy(); //reader

Use AddControlLC to add controls to a Scrollable Container. Add the controls as soon as they are created. Do not push them onto the Cleanup Stack before they are added. Do not pop them from the Cleanup Stack until they are fully constructed. A TCleanupItem created in AddControlLC will make sure that the control is both cleaned up and removed from the Components Array if a leave occurs before the control is fully constructed.

3.3.3 Construction Solely from C++ Code

The example below describes how to construct a Label solely from C++ code.

The reason the example seems to be rather complex is because it demonstrates how to construct a complete view with a Scrollable Container and a Layout Manager.

The following source code will construct a Label:

#include <eiklabel.h>
#include <QikScrollableContainer.h>
#include <QikRowLayoutManager.h>
#include <QikGridLayoutManager.h>
#include <QikBuildingBlock.h>
_LIT(KLabelText, "Label");
            
void CMySinglePageView::ViewConstructL()
    {
    // Give a layout manager to the view
    CQikGridLayoutManager* gridlayout = CQikGridLayoutManager::NewLC();
    SetLayoutManagerL(gridlayout);
    CleanupStack::Pop(gridlayout);
    
    // Create a container and add it to the view
    CQikScrollableContainer* container = new (ELeave) CQikScrollableContainer();
    Controls().AppendLC(container);
    container->ConstructL(EFalse);
    CleanupStack::Pop(container);
    
    // Create a layout manager to be used inside the container
    CQikRowLayoutManager* rowlayout = CQikRowLayoutManager::NewLC();
    container->SetLayoutManagerL(rowlayout);
    CleanupStack::Pop(rowlayout);
    
    // Create the Label and add it to the container
    CEikLabel* label = new (ELeave) CEikLabel();
    container->AddControlLC(label, EMyViewLabel);
    label->ConstructL();
    label->SetUniqueHandle(EMyViewLabel);
    label->SetObserver(this);
    label->SetTextL(KLabelText);
    CleanupStack::Pop(label);
    }

What the code does

1) Initializes the Command Manager with an empty Command List. The controls placed in the view add their Commands to the Command List when they receive focus.

2) Creates a Layout Manager for the view. The Grid Layout Manager fills the view with its only control in this example, the Scrollable Container.

3) Instantiates a container and adds it to the view.

4) Creates a Layout Manager and adds it to the container.

5) Creates the Label control from C++ code. Sets the view, this, to be an observer of the Label. The view's base class CQikViewBase handles focus changes in its method HandleControlEventL. For more details see the section below on how to be notified with Control Events .

3.3.4 Construction with the Dialog Framework Using Data from a Resource File

Label can be constructed from resource files in dialogs as well. To construct a dialog from resource, a valid resource definition of that dialog must exist in one of the project’s resource files.

An example of a dialog resource containing the control is given below. For more information about the dialog class and its resource struct see CEikLabel and DIALOG in the API documentation.

1) Declare a dialog resource containing the Label control:

RESOURCE DIALOG r_label_dialog
    {
    title="Label Test";
    flags=EEikDialogFlagWait;
    items=
        {
        DLG_LINE
            {
            type=EEikCtLabel;
            control=LABEL
                {
                txt="Dialog label";
                };
            }
        };
    }

The resource properties inside the Control Block are the same as the ones described in the previous section.

2) Launch the dialog from following source code. The dialog resource ID is passed as an argument:

CEikDialog* dlg = new (ELeave) CEikDialog();
dlg->ExecuteLD(R_LABEL_DIALOG);

The function returns immediately if EEikDialogFlagWait has not been specified in the dialog resource. If EEikDialogFlagWait is specified, it returns when the dialog exits. The dialog framework will in both situations delete the dialog appropriately as indicated by the D suffix of theExcecuteLD function name.


3.4 Using the Label

This section covers the most common functions used for interacting with the control.

When constructing the control with resource data, no reference to the control is available in the view class. When constructing the control with code, the preferred way might be to not save a reference to the control. In both these cases, the LocateControlByUniqueHandle function is used to get a pointer to the control by supplying the control's unique handle. When constructing the view and the control from code, you must explicitly set this unique handle by calling the method SetUniqueHandle. See the code examples below.

Note that the function returns NULL if the control could not be found. Always check the pointer before using it!

// Set the unique handle
label->SetUniqueHandle(EMyViewLabel);
                            
// Get a pointer to the Label control
CEikLabel* label = LocateControlByUniqueHandle<CEikLabel>(EMyViewLabel);

3.4.1 How to be Notified with Control Events

In order to be notified when the Label changes state, you must add an observer to the Label. An observer is an object of the type MCoeControlObserver. The observer receives a function call to its function HandleControlEventL(CCoeControl* aControl, TCoeEvent aEventType) when the Label changes state.

The view base class, CQikViewBase, implements the MCoeControlObserver. The HandleControlEventL function must be overloaded in the view class because the view inherits from CQikViewBase.

The following source code example shows how to add an object as an observer and how to receive events from the Label:

void CMySinglePageView::ViewConstructL()
    {
    // Construction code
    …
    // Adding this object as an observer
    label>SetObserver(this);
    }

void CMySinglePageView::HandleControlEventL(CCoeControl* aControl, TCoeEvent aEventType)
    {
    // Call base class to get focus navigation right
    CQikViewBase::HandleControlEventL(aControl, aEventType);
                    
    CEikLabel* label = LocateControlByUniqueHandle<CEikLabel>(EMyViewLabel);
    if(aControl == label)
        {
        switch(aEventType)
            {
        case EEventStateChanged:
            // The internal state of the Label was changed,
            // for example, due to another item being selected. 
            break;
                                
            case EEventRequestExit:
                break;
                                
            case EEventRequestCancel:
                break;
                                
            case EEventRequestFocus:
                // The Label received a pointer down event
                break;
                                
            case EEventPrepareFocusTransition:
                // A focus change is about to appear
                break;
                            
            case EEventInteractionRefused:
                // The Label is dimmed and received a
                // pointer down event.
                break;
                            
            default:
                break;
            }
        }
    }

The reason for calling the base class's HandleControlEventL function is that CQikViewBase implements focus navigation between controls in the view. If the Label observer is not a class which derives from CQikViewBase, focus management must be resolved by the observer itself. If a control requests focus and does not get it from the observer, it will generate a panic in some cases if the observer does not leave.

For more details on the TCoeEvent types, see class MCoeControlObserver in see the API documentation.

3.4.2 How to Set a Text to Label

An example of how to add or change the text of the Label control:

_LIT(myText,"a Label text");  
                    
// Get a pointer to the Label control
CEikLabel* label = LocateControlByUniqueHandle<CEikLabel>(EMyViewLabel);
                    
label->SetTextL(myText);

3.4.3 How to Get a Text from Label

An example of how to extract text from a Label control:

// Get a pointer to the Label control
CEikLabel* label = LocateControlByUniqueHandle<CEikLabel>(EMyViewLabel);
                    
TPtrC extractedText = label->Text();


3.5 Destruction

Destroying the control is just a matter of invoking operator delete on the Label object.

[Top]


4. Subclassing

Subclassing Label is not recommended.