Custom Controls in Dialogs

In addition to the stock controls that are available, you may need to include a control of your own. To do so, the steps that you need to follow are:

• First, write the control and test it outside a dialog to make sure it works the way you want.

• If necessary, in a .rh file, define a resource file struct associated with your control, specifying the member names and types for the resource initialization data.

• Choose a value for the control type that isn't used by any other control and add its definition to a .hrh file - a value of 0x10 0 0 or more will certainly be safe.

• In the resource file for the dialog, include a DLG_LINE struct that specifies the new control type value and includes the control's resource struct, if it has one.

• If your control has a resource struct, implement the control's Con-structFromResourceL() function to read in initialization data from the struct in the application's resource file.

• Implement the dialog's CreateCustomControlL() function to test for the relevant control type, and construct and return an SEikCon-trollnfo struct appropriate for your control.

The custom dialog's resource struct, in a .rh file, will normally provide default values for its members, as in the following example:

STRUCT MYCUSTOMCONTROL {

WORD width = 100;

The control type needs to be defined in a .hrh file since it needs to be #included in both the resource file and one or more C++ source files. It is typically defined as an enumeration:

enum

EMyCustomControl = 0x1000 }

enum

EMyControlld }

We've also shown the enumeration that defines the control's ID, needed for any control within a dialog.

The dialog resource is for a Series 60 dialog, so we've omitted a specification of the dialog's title and used one of the standard Avkon button combinations. We've also chosen to replace the default value for the control's width.

RESOURCE DIALOG r_mycustomcontrol_dialog {

buttons=R_AVKON_SOFTKEYS_OK_CANCEL;

DLG_LINE {

type=EMyCustomControl; id=EMyControlId;

control=MYCUSTOMCONTROL {

The custom control's ConstructFromResourceL() function is similar to a normal control's ConstructL() function, except that the data is read from the resource file. A control will need both a Construct-FromResourceL() and a ConstructL() if it is to be used both inside and outside a dialog. On entry to ConstructFromResourceL(), the passed resource reader is positioned to the start of the control's data and the function should consume all available data for the control.

void CMyCustomControl::ConstructFromResourceL(TResourceReader& aReader) {

// Read the width and height from the resource file. TInt width = aReader.ReadInt16(); TInt height = aReader.ReadInt16(); TSize controlSize (width, height);

SetSize(controlSize);

The default implementation of ConstructFromResourceL() is empty, so you don't need to implement it if your custom control has no associated resource struct.

You must, however, implement the dialog's CreateCustomCon-trolL() function in each dialog that contains one or more custom controls, and it must be capable of creating every custom control that appears in the dialog. The function is called from the system's control factory whenever it is asked to construct a control of an unknown type. It is unusual in that it returns an SEikControlInfo structure, defined in eikfctry.h as:

struct SEikControlInfo {

CCoeControl* iControl; Tint iTrailerTextId;

In most circumstances it is sufficient to set the iControl element to point to the newly constructed control and set the remaining elements to zero, as in the following example.

SEikControlInfo CMyCustomControlDialog::CreateCustomControlL(TInt aControlType) {

SEikControlInfo controlInfo; controlInfo.iControl = NULL; controlInfo.iTrailerTextId = 0; controlInfo.iFlags = 0;

switch (aControlType) {

case EMyCustomControl:

controlInfo.iControl = new(ELeave) CMyCustomControl; break; default:

return controlInfo; }

0 0

Post a comment

  • Receive news updates via email from this site