From navigating the forums of various websites about Symbian, I have found many members asking and querying about Wait Dialogs. I will not lie and not say I was not one of them, for God knows how many times I was almost broke the keyboard trying to do any of them. Finally after many hours of struggling I have figured it out. And I believe it is my duty as a member of the programming world to share this knowledge with all the others. I present in this document one of the methods for creating a Wait Dialog. Basically there are two methods or steps to create a Wait Dialog:
the first is to create an Active Object that will host within it the Wait Dialog and allow for processing to happen in the background while Wait Dialog whatever loop it has to process.
the second type which will be presented in this document is one that doesn’t rely on having an Active Object which makes it a little simpler, but adds some constraint on the type of processing that you would like to do. This will be cleared later in the document. So let us begin with Part I of Wait Dialogs.
The MAknBackgroundProcess class
To begin you will have to get acquainted with an important class that will aid you in the creation of the Wait Dialog: MAknBackgroundProcess class. This class is an abstract class that will execute the process that will take an indefinite time to do. The reason I say an indefinite time is because if you know the amount of time that the process would take, it would be better if you were to use the process dialog for such a case. So back to MAknBackgroundProcess, this class has five virtual functions. A brief description of each of these functions is presented in the SDK. Therefore, I shall not waste time to describe what they do. So let us get our hands dirty.
The CWaitDialog class definition
First you will need to create a class that will inherit the MAknBackgroundProcess Class. You will also need to overload each function of the MAknBackgroundProcess Class. For this document I have created a class called CWaitDialog. I have also added two functions the first is the ConstructL() function for second-phase construction if needed. The second is IssueRequest() function the reason will be explained shortly. Your header file should look as follows:
#include <aknwaitdialog.h>
#include <aknwaitnotewrapper.h>
#include "WaitDialog.h"
#include "File.rsg"
class CWaitDialog: MAknBackgroundProcess
{
public: //Public Functions
void IssueRequest();
void ConstructL();
private: //Private Functions
void DialogDismissedL(TInt dismissed);
TInt CycleError(TInt aError);
void ProcessFinished();
TBool IsProcessDone() const;
void StepL();
private://Private Variables
TInt Counter;
};
The CWaitDialog class implementation
You will then need to implement the five functions that you have inherited from MAknBackgroundProcess Class. The method of implementation depends on the process that you would like to do. For instance, if you can break your loop into smaller sections then every step of the loop will be added in the StepL() function and then check in IsProcessDone() function to insure that the loop has terminated. For example, in the following code I would like to have a Counter that will increase until it reaches the value 10000000. Therefore, I added in the StepL() the formula to increment the value of the variable Counter by one. This will be executed once after which a call to IsProcessDone() is initiated. In this function you check whether you have finished the process that you are executing. For this example I check if the value of the variable Counter has reached 10000000. If "yes" I return ETrue to dismiss the Wait Dialog, else I return EFalse which will cause the function StepL() to be called again and so on and so forth until the value of Counter reaches 10000000. It is because of the style of MAknBackgroundProcess Class that you have to be able to break down your process into smaller sections so that you can disperse your code in the two functions StepL() and IsProcessDone(). The rest of the functions of the class are pretty simple.
I have added the function IssueRequest() to be my entry function into the Wait Dialog. Using the CAknWaitNoteWrapper Class I create an instance of a note wrapper which will basically create my Wait Dialog structure. I then call the function ExecuteL() function of the CAknWaitNoteWrapper Class. The ExecuteL() function of CAknWaitNoteWrapper takes three parameter the first is the id of the resource dialog in the resource file. An example of the resource structure is given below. The second parameter is of type MAknBackgroundProcess therefore I pass the instance of our CWaitDialog class so that the StepL() and the rest of our functions are called. And finally I pass ETrue to make the dialog visible. The code for the following example is presented below.
#include "WaitDialog.h"
#include "coeaui.h"
// Constructor Function of the Class
// (just in case we need second phase construction)
void CWaitDialog::ConstructL()
{
Counter=0;
}
// This function handles one step of the loop.
// In this case it will just increment Counter
void CWaitDialog::StepL()
{
Counter++;
}
// This function is called
// when IsProcessDone returns ETrue
void CWaitDialog::ProcessFinished()
{
}
// This function is in case the StepL function leaves.
// I just return the error then.
TInt CWaitDialog::CycleError(TInt aError)
{
return aError;
}
// This function is called after the dialog is missed.
// I reset the counter in case the user presses cancel
void CWaitDialog::DialogDismissedL(TInt dismissed)
{
//If the cancel button has been pressed.
// Reset the counter.
if (dismissed==-1)
Counter=0;
}
// This function informs the system
// when the dialog is to be dismissed
// or when the processing is done.
TBool CWaitDialog::IsProcessDone() const
{
if (Counter>10000000)
return ETrue;
else
return EFalse
}
// This function is my entry point to the class
// and the one that starts the wait dialog.
void CWaitDialog::IssueRequest()
{
CAknWaitNoteWrapper* dialog = CAknWaitNoteWrapper::NewL();
dialog->ExecuteL(WAITDIALOG,*this,ETrue);
}
Here is the corresponding resource definition:
RESOURCE DIALOG WaitDialog
{
flags = EAknWaitNoteFlags | EEikDialogFlagNotifyEsc;
buttons=R_AVKON_SOFTKEYS_CANCEL;
items =
{
DLG_LINE
{
type = EAknCtNote;
id = EGeneralNote;
control = AVKON_NOTE
{
layout = EWaitLayout;
singular_label=" Please Wait...";
animation = R_QGN_GRAF_WAIT_BAR_ANIM;
};
}
};
}
|