|
|
|
This guide explains the UIQ control Combo Box (CEikComboBox).The Combo Box is a control that allows the end user to either type in a value or to select a value from a list. The Combo Box consists of a Plain Text Editor and a List Box. This control is normally used in a Container Pop-out.
The following functionality can be used by the application developer:
Set the width of the control, expressed in number of characters, used in the Plain Text Editor,
Set the limit of the composition text length in the Plain Text Editor,
Set the maximum number of items that the List Box can contain,
Change the list of items that are shown in the List Box,
Insert the composition text, which is visible in the Plain Text Editor, into the List Box. If the List Box already contains the maximum number of items, the latest items are removed until the List Box contains the maximum number of items, including the newly inserted item,
Change the composition text in the Plain Text Editor,
Toggle between dimmed state and normal state.
Examples of Combo Box control graphics
|
See the API documentation for Combo Box CEikComboBox.
The Combo Box control uses the Plain Text Editor and the List Box. See the How To guide for Plain Text Editor.
See even the API documentation for Plain Text Editor (CEikEdwin) and
the List Box (CEikListBox) controls.
This section explains how the control is constructed, used and destroyed. Source code examples are used and explained to illustrate how the Combo Box control is used.
Use the following #include directive:
#include <eikcmbox.h>
Use the following LIBRARY directive in the project's mmp-file:
LIBRARY eikctl.lib
Resource files can be used to construct the Combo Box. The resource to use is defined by the COMBOBOX structure, defined in Eikon.rh. The structure looks like this:
STRUCT COMBOBOX
{
WORD width;
WORD maxlength;
WORD maxarraysize;
LLINK array_id=0;
}
The values given in the structure definition are default values. The structure contains the following:
Width the width of the text editor in characters,
Maxlength the maximum number of characters that can be inserted into the text editor,
Maxarraysize the maximum number of items in the drop-down list,
array_id the ID of an array resource with the text items to use in the Combo Box.
The Combo Box can have the following flags. They can only be used when calling the function SetArray(MDesC16Array* aArray, TArrayOwnershipType aOwner=EArrayInternalOwner):
Combo Box - Resource flags
|
This section discusses four different ways of constructing controls. The first three ways describe how to construct and add a control to 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 into 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 Combo Box.
The example below describes how to construct a Combo Box using 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 Combo Box 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 controls' ID in a *.hrh file for use both in resource and cpp */
enum TMyViewControls
{
EMyViewScrollableContainer,
EMyViewBuildingBlock,
EMyViewComboBox,
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_combo_box_view_controls
{
items =
{
QIK_CONTROL
{
unique_handle = EMyViewScrollableContainer;
type = EQikCtScrollableContainer;
control = r_my_combo_box_scroll_pane;
},
QIK_CONTROL
{
unique_handle = EMyViewComboBox;
type = EEikCtComboBox;
control = r_my_combo_box;
},
QIK_CONTROL
{
unique_handle = EMyViewBuildingBlock;
type = EQikCtCaptionedTwolineBuildingBlock;
control = r_my_combo_box_building_block;
}
};
}
3) Define the view and its contents in your resource file:
/* The view */
RESOURCE QIK_VIEW r_my_combo_box_view
{
pages = r_my_combo_box_viewpages;
}
/* The view page */
RESOURCE QIK_VIEW_PAGES r_my_combo_box_viewpages
{
pages =
{
QIK_VIEW_PAGE
{
container_unique_handle = EMyViewScrollableContainer;
page_content = r_my_combo_box_view_container_details;
}
};
}
4) Define the resource for the Scrollable Container used in the view:
/* The scrollable container used in the view */
RESOURCE QIK_SCROLLABLE_CONTAINER r_my_combo_box_scroll_pane
{
}
5) Declare the contents and properties for Scrollable Container used in the view:
/* Contents of the Scrollable Container used in the view */
RESOURCE QIK_SCROLLABLE_CONTAINER_SETTINGS r_my_combo_box_view_container_details
{
controls =
{
QIK_CONTAINER_ITEM
{
unique_handle = EMyViewBuildingBlock;
}
};
}
6) Define the control resource struct used in the view:
/* The Combo Box used in the view */
RESOURCE COMBOBOX r_my_combo_box
{
width = 100;
maxlength = 250;
maxarraysize = 5;
array_id = r_my_combo_box_items;
}
7) Define the array resource to be used in the Combo Box:
/* The Combo Box contents */
RESOURCE ARRAY r_my_combo_box_items
{
items=
{
LBUF {txt = "One";},
LBUF {txt = "Two";},
LBUF {txt = "Three";},
LBUF {txt = "Four";}
};
}
8) Define the settings for the Building Block containing the control:
/* Settings for the EQikCtCaptionedTwolineBuildingBlock containing the Combo Box */
RESOURCE QIK_SYSTEM_BUILDING_BLOCK r_my_combo_box_building_block
{
content =
{
QIK_SLOT_CONTENT
{
slot_id = EQikItemSlot1;
caption = "Choose:";
},
QIK_SLOT_CONTENT
{
slot_id = EQikItemSlot2;
unique_handle = EMyViewComboBox;
}
};
}
9) The configurations of the view:
RESOURCE QIK_VIEW_CONFIGURATIONS r_my_combo_box_ui_configurations
{
configurations=
{
QIK_VIEW_CONFIGURATION
{
ui_config_mode=KQikSoftkeyStylePortrait;
view=r_my_combo_box_view;
command_list=r_my_combo_box_commands;
},
QIK_VIEW_CONFIGURATION
{
ui_config_mode=KQikPenStyleTouchPortrait;
view=r_my_combo_box_view;
command_list=r_my_combo_box_commands;
}
};
}
10) The command list for the view:
RESOURCE QIK_COMMAND_LIST r_my_combo_box_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)";
}
};
}
11) The view framework constructs the view described in this example with this code:
void CMySinglePageView::ViewConstructL()
{
ViewConstructFromResourceL(R_MY_COMBO_BOX_UI_CONFIGURATIONS, R_MY_COMBO_BOX_VIEW_CONTROLS);
}
12) The result should look something like this:
The example below describes how to construct a Combo Box 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 Combo Box in a Building Block.
This example uses the resource structures from the previous example. The following code creates the Combo Box:
#include <eikcmbox.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_COMBO_BOX_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 Combo Box) and
// add it to the container
CQikBuildingBlock* block = CQikBuildingBlock::CreateSystemBuildingBlockL(EQikCtCaptionedTwolineBuildingBlock);
container->AddControlLC(block, EMyViewBuildingBlock);
TResourceReader blockReader;
iCoeEnv->CreateResourceReaderLC(blockReader,R_MY_COMBO_BOX_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 control that was createdreally is a class of the 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 Combo Box from the resource R_MY_BUILDING_BLOCK. Adds the Building Block to the container.
The Combo Box 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 the following code.
Since a pointer to the control is declared here, eikcmbox.h needs to be included in the cpp-file and eikctl.lib in the mmp-file.
// Create the Command Box and add it to the container
TResourceReader reader;
iEikonEnv->CreateResourceReaderLC(reader, R_MY_COMBO_BOX);
CEikComboBox* cmbox = new (ELeave) CEikComboBox();
container->AddControlLC(cmbox, EMyViewComboBox);
cmbox->ConstructFromResourceL(reader);
cmbox->SetUniqueHandle(EMyViewComboBox);
CleanupStack::Pop(cmbox);
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.
The example below describes how to construct a Combo Box solely from 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.
The following source code constructs a Combo Box:
#include <eikcmbox.h>
#include <QikScrollableContainer.h>
#include <QikRowLayoutManager.h>
#include <QikGridLayoutManager.h>
#include <QikBuildingBlock.h>
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 Combo Box and add it to the container
CEikComboBox* cmbox = new (ELeave) CEikComboBox();
container->AddControlLC(cmbox, EMyViewComboBox);
TInt widthInChars = 100;
TInt textLimit = 250;
TInt maxArraySize = 5;
cmbox->ConstructL(widthInChars, textLimit, maxArraySize);
CDesCArrayFlat* array = new(ELeave) CDesCArrayFlat(1);
array->AppendL(_L("One"));
array->AppendL(_L("Two"));
array->AppendL(_L("Three"));
array->AppendL(_L("Four"));
cmbox->SetArrayL(array);//EArrayInternalOwner is default so no need to specify
cmbox->SetUniqueHandle(EMyViewComboBox);
cmbox->SetObserver(this);
CleanupStack::Pop(cmbox);
}
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 Combo Box control from C++ code. Uses SetArray() to set the Combo Box Items to its List Box. Sets the view, this, to be an observer
of the Combo Box. 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 .
The Combo Box can be constructed from resource files in dialogs as well. To construct a dialog from resource a valid resource definition of that dialog must be 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 structure see CEikDialog and DIALOG in the API documentation.
1) Declare a dialog resource containing the Combo Box control:
RESOURCE DIALOG r_my_combo_box_dialog
{
title = "ComboBox Test";
flags = EEikDialogFlagWait;
items =
{
DLG_LINE
{
type = EEikCtComboBox;
prompt = "ComboBox";
control = COMBOBOX
{
width = 100;
maxlength = 250;
maxarraysize = 5;
array_id = r_my_combo_box_items;
};
}
};
}
RESOURCE ARRAY r_my_combo_box_items
{
items=
{
LBUF {txt = "One";},
LBUF {txt = "Two";},
LBUF {txt = "Three";},
LBUF {txt = "Four";}
};
}
The resource properties inside the Control Block are the same as the ones described in the previous section.
2) Launch the dialog using the following source code. The dialog resource ID is passed as an argument:
CEikDialog* dlg = new (ELeave) CEikDialog();
dlg->ExecuteLD(R_MY_COMBO_BOX_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 the ExcecuteLD function name.
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 savea 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
cmbox->SetUniqueHandle(EMyViewComboBox);
// Get a pointer to the Combo Box control
CEikComboBox* cmbox = LocateControlByUniqueHandle<CEikComboBox>(EMyViewComboBox);
To get a pointer to the Combo Box's array containing the items,
use the following function, which returns an MDesC16Array pointer.
MDesC16Array* cmboxArray = cmbox->Array();
To insert the text held in the editor into the Combo Box item array, use the followng function:
cmbox->InsertTextIntoArrayL();
In order to be notified when the Combo Box changes state you
must add an Observer to the Combo Box. An Observer is an object of the type
MCoeControlObserver. The observer will then receive a function call to its function
HandleControlEventL(CCoeControl* aControl, TCoeEvent aEventType) when the Combo Box 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 Combo Box:
void CMySinglePageView::ViewConstructL()
{
// Construction code
…
// Adding this object as an observer
cmbox->SetObserver(this);
}
void CMySinglePageView::HandleControlEventL(CCoeControl* aControl, TCoeEvent aEventType)
{
// Call base class to handle focus management
CQikViewBase::HandleControlEventL(aControl, aEventType);
CEikComboBox* cmbox = LocateControlByUniqueHandle<CEikComboBox>(EMyViewComboBox);
if(aControl == cmbox)
{
switch(aEventType)
{
case EEventStateChanged:
// The internal state of the Combo Box was changed,
// for example, due to another item being selected.
break;
case EEventRequestExit:
break;
case EEventRequestCancel:
break;
case EEventRequestFocus:
// The Combo Box received a pointer down event
break;
case EEventPrepareFocusTransition:
// A focus change is about to appear
break;
case EEventInteractionRefused:
// The Combo Box is dimmed and received a
// pointer down event.
break;
default:
break;
}
}
}
The reason for calling the base class's HandleControlEventL function is
that the view base class CQikViewBase handles focus management between controls in the view.
If the control's 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
type, see class MCoeControlObserver in the API documentation.
Destroying the control is just a matter of invoking operator delete on the Combo Box object. The only thing to think about is whether or not the Combo Box owns the item array. If it does, the Combo Box will delete the array, and its objects, in its destructor. If it does not, someone else must take care of it
Subclassing Combo Box is not recommended.