Wiley.Symbian.OS.C.plus.plus.for.Mobile.Phones.Aug.2007 (779890), страница 83
Текст из файла (страница 83)
You can set the range into which colors get mapped bymeans of a call to SetFadingParameters(). If you don’t make a callto SetFadingParameters(), then a UI-specific default range is used.A compound control should never set focus to a dimmed or invisiblecontrol.SummaryControls are the principal means of interaction between an applicationand the user. This chapter started by explaining what a control is andwhat its place is in UI development, and presented different aspects ofcontrol development.We showed how to create controls by extending CCoeControl, andthe difference between simple controls and compound controls. Weshowed how to manage the control layout and how controls shouldrespond to key and pointer events.In the section on drawing a control we demonstrated how to customizea control by implementing the Draw() method and showed how to usethe window server to handle complex drawing situations.The topics, along with the examples provided, should help you tounderstand basic UI development and design user interfaces for particularapplications.16DialogsThis chapter explains the basic principles of programming and usingdialogs.
The examples provided in this chapter have deliberately beenkept general. Where applicable, the differences between UIQ and S60are explained.16.1 What Is a Dialog?A dialog is simply a specialized window-owning compound control.The dialog framework manages the behavior and many other aspects ofdialogs, including layout, drawing and user interaction with componentcontrols. A dialog can be waiting or non-waiting, and it can be modelessor modal. A dialog is a pop-up window with a title, one or more buttons,and one or more lines which contain controls that display information orlet the user input information.By default, dialogs are modal. Once a modal dialog is running, the usercan interact only with the dialog until the dialog is dismissed. The useris therefore forced to respond, and cannot interact with the underlyingapplication until they have done so.
A modeless dialog allows the userto interact with other parts of the application’s user interface while it isactive.The component controls that make up a specific dialog are generallydefined in the application’s resource file. The dialog framework codeuses the definitions to construct the appropriate controls and incorporatethem into the dialog. The layout and positioning of the elements ofthe dialog are handled automatically by the dialog framework, but fullydynamic construction of a dialog is possible and we can influence theprocess.458DIALOGSCCoeControlCAknControlCEikBorderedControlMEikDialogPageObserverMAknFadedComponentCEikDialogFigure 16.1 Generic derivation of CEikDialogOn the S60 platform all dialog classes are derived directly or indirectly from CEikDialog, which is derived from CCoeControl, asshown in Figure 16.1.
In UIQ 3, CEikDialog is deprecated in favorof CQikSimpleDialog and CQikViewDialog, both directly derivedfrom CCoeControl, as shown in Figures 16.2 and 16.3.CCoeControlMQikCommandHandlerMCoeControlObserverCQikSimpleDialogFigure 16.2Generic derivation of CQikSimpleDialogCCoeControlCEikBorderedControlMQikContainerMQikComandListenerCQikViewDialogFigure 16.3 Generic derivation of CQikViewDialogSIMPLE DIALOGS459As shown in the above figures, dialogs also inherit from a number ofinterface classes. The purpose of these is discussed below.16.2 Simple DialogsSimple Single-Page DialogCreating a simple dialog requires a few straightforward steps. The first isto create a DIALOG resource in our resource file to define the dialog titleand the set of dialog lines.The second is to create a class derived from CQikSimpleDialog ifwe are developing for UIQ, or from CEikDialog if we are developingfor S60.
The minimum code we have to provide in this class is thecode to initialize the component controls. Another piece of code thatwe may want to implement is the code to save the control valueswhen the dialog is dismissed, if our dialog requests input from theuser.The last step is to launch the dialog. This is done by invoking thedialog’s ExecuteLD() method (implemented in CEikDialog andCQikSimpleDialog).A simple single-page UIQ dialog is presented as an example inFigure 16.4.Figure 16.4 UIQ simple dialogDialog controls are displayed in lines, in a vertical list.
The waycontrols are arranged in a dialog is a key design decision, making manythings easier for both the user and the developer.• Navigation from field to field is easy: use the pointer, or the up anddown navigation keys.• Dialog layout is easy: the only difficulty is establishing the width ofthe controls.460DIALOGS• The width of the dialog limits the width of a control. If a dialog linecontains text longer than the control’s width, the text will be truncatedby replacing the last three displayable characters with an ellipsis (.
. .).• Because layout is handled by the dialog framework, you do nothave to specify the pixel coordinates of each field in the dialogdefinition resource, so there is no need for a GUI-based resourcebuilder.The example dialog has only two controls. Dialogs can have more,but you need to consider what will happen if they overflow the screenvertically. In UIQ, for example, if the controls overflow the screen, thedialog becomes scrollable, with scroll bars automatically added on thedialog’s right hand side by the framework.
However, as a design principle,it is not good style to use scrollable dialogs. If you need more lines, youcan use multi-page dialogs (see below).Do not create monster dialogs that offer a bewildering set of choices tothe user. Keep in mind the dialog’s usability when you have to createhuge dialogs.In UIQ, focus is indicated only in text and numeric fields. For textfields the flashing cursor indicates the location of focus, and for numericfields a highlighted background is used.
In S60, focus is indicated in allcontrols that can receive focus.Standard DialogsBoth UIQ and S60 provide a number of convenient standard dialogs.When using standard dialogs, we do not have to create a derived class;we only have to define the DIALOG resource in our resource file andlaunch the dialog.Figure 16.5 UIQ alert dialogSIMPLE DIALOGS461AlertsAn alert dialog displays the title ‘Information’, one or two lines of text,and a button labeled ‘Continue’ in UIQ (see Figure 16.5) or ’Ok’ in S60(see Figure 16.6).Figure 16.6 S60 alert dialogThe UI environment constructs a ready-made alert dialog invokedwith iEikonEnv>AlertWin(), specifying either one or two stringparameters. An alert dialog is an example of a pre-constructed, or sleepingdialog, which does not need to allocate any resources at the time it isdisplayed.
As a consequence, you can never run out of memory whenusing AlertWin().QueriesA query dialog, as illustrated in Figure 16.7, enables a minimal form ofinteraction.Figure 16.7 UIQ query dialog462DIALOGSA query dialog displays a title, a line of text and two buttons. You canuse it to ask a simple yes/no question, as follows:if(iEikonEnv->QueryWinL(R_EIK_TBUF_CONTACT_DELETE_CONFIRM))User::LeaveIfError(cntModel->DeleteContactL(iContactId));else; //do nothingThe call to iEikonEnv->QueryWinL() specifies a string from aresource file to be used as a question.
The query dialog has a Yes buttonand a No button; iEikonEnv>QueryWinL() returns ETrue if Yes ispressed, otherwise EFalse.Unlike alert dialogs, query dialogs are not sleeping dialogs, so theprocess of constructing and executing a query can leave.Other standard dialogsUIQ and S60 provide several other standard dialogs. UIQ, for example,includes dialogs:• to enter and change a password• to set the current date and time• to set the options for formatting dates and times.Many of the more sophisticated controls include dialogs of their own.EDWINs (text editors), for example, include dialogs for Find, Replace andOptions for replace.16.3Complex DialogsUnlike the previous examples, most dialogs contain more than one lineand some very complex dialogs can contain multiple pages.
However,this does not mean that such dialogs are significantly more complicated to write. If the various lines are truly independent of each other,the only added complexity is the need to initialize the extra components appropriately, and to process the results on completion of thedialog.UIQ and S60 use different classes to handle complex dialogs, CQikSimpleDialog and CQikViewDialog in UIQ and CEikDialog inS60.Complex dialogs introduce issues that did not arise in the earlierexample, such as:COMPLEX DIALOGS463• focus can change from one control to another, and controls may notbe in a fit state to lose or gain focus• controls may be dependent on one another, so that modifying thestate of one control requires the state of one or more other controls tochange.Change of FocusIn UIQ, application code can obtain a pointer to the focused controlusing CQikViewDialog::FocusedControl().
In S60, it can find theID of the currently focused control using CEikDialog::IdOfFocusControl(), and then obtain a pointer to the control itself by callingControl(), which takes a control ID as its parameter.The dialog framework extends the MCoeControlObserver interface.Because of this, whenever focus is about to be lost by a control, the dialogframework will handle an EEventPrepareFocusTransition event.The framework’s default action is to identify the control in the currentlyfocused line and call its PrepareForFocusLossL() function. Thecontrol must either ensure that it is in a fit state to relinquish focus orleave.
If the function leaves, focus will not be removed from the control.The focused control can be changed in a number of ways. A generaltechnique, usable in both UIs, is to call the SetFocus() method ona control in the dialog. In UIQ, a change of focus can be initiated bycalling RequestFocusL(), passing the control which is to receive thefocus. In S60, a change of focus can be initiated by calling TryChangeFocusToL(), passing the ID of the control which is to receive thefocus.In S60, any attempt to transfer focus to a dimmed dialog item resultsin a call to the dialog’s HandleInteractionRefused() function,whose default action is to display an information message.