Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (779887), страница 12
Текст из файла (страница 12)
It contains the application classimplementation. These methods are called by the GUI framework when50SYMBIAN OS QUICK STARTstarting the application. It defines and returns the application’s UIDand creates and returns the application document object. Following isSimpleEx App.cpp:/*=================================================================File: simpleEx_app.cppThis file contains the application class for SimpleEx.==================================================================*/#include "SimpleEx.h"CApaDocument* CSimpleExApplication::CreateDocumentL(){return new(ELeave) CSimpleExDocument(*this);}TUid CSimpleExApplication::AppDllUid() const{return KUidSimpleExApp;}SimpleEx Doc.cppThis file is also common between S60 and UIQ.
The file contains the application document object. Although this example has no actual persistentdata, the example still overrides the CreateAppUiL() method of thedocument class since the framework calls this method to create and passa pointer to the application UI class. Following is SimpleEx Doc.cpp:#include "SimpleEx.h"CEikAppUi* CSimpleExDocument::CreateAppUiL(){return new (ELeave) CSimpleExAppUi;}SimpleEx UI.cppThis file contains the example application’s UI class. In the S60 example,this class contains the HandleCommandL() method where the mainaction of the application occurs. All user events (except alphanumeric keyboard input) come through the UI class method HandleCommandL().When the menu item ‘Start’ is selected, the GUI framework invokesthe HandleCommandL() method, passing it the command ESimpleExCommand (the command specified in the menu resource in the SimpleExresource file).
HandleCommandL() responds to this command by popping up an alert window with the message ‘Start Selected!’.I used the iEikonEnv->AlertWin() function for the pop-up sincethis is a core GUI method available to all platforms. This looks quitegood on UIQ, although fairly plain on the other platforms. In practice,you should follow the UI guidelines for the software platform you areusing, which are available for download from their developer websites.SIMPLE EXAMPLE APPLICATION51This may involve using platform-specific classes for controls or messagedisplays instead of core Uikon controls.As an example, in S60 you can use the S60-specific UI control classCAknInformationNote to pop up a message.
This would look betterfor the S60 UI than the generic Symbian OS application frameworkalternative.The UI class ConstructL() method creates the application view.ConstructL() is called by the framework after getting a pointer to theUI object from the document. You will see later how it is common forSymbian C++ objects to be constructed in two steps: instantiating theC++ class then invoking its ConstructL() method.Note further that, in the HandleCommandL() method, commandEAknSoftkeyExit handles the S60 Exit softkey that is put up at application start, as specified by the R AVKON SOFTKEYS OPTIONS EXIToption in the S60 resource file.
Following is SimpleEx UI.cpp filefor S60:/*=================================================================File: simpleEx_ui.cppThis file contains the application UI class for S60 SimpleEx.==================================================================*/#include "SimpleEx.h"void CSimpleExAppUi::ConstructL(){BaseConstructL(CAknEnableSkin);iAppView = CSimpleExAppView::NewL(ClientRect());}CSimpleExAppUi::~CSimpleExAppUi(){delete iAppView;}void CSimpleExAppUi::HandleCommandL(TInt aCommand){switch(aCommand){case EEikCmdExit:case EAknSoftkeyExit:Exit();break;case ESimpleExCommand:{_LIT(KMessage,"Start Selected!");iEikonEnv->AlertWin(KMessage);break;}}}The UIQ 3 version of SimpleEx appui.cpp is:/*=================================================================File: simpleEx_ui.cppThis file contains the application UI class for UIQ SimpleEx.52SYMBIAN OS QUICK START#include "SimpleEx.h"void CSimpleExAppUi::ConstructL(){BaseConstructL();iAppView = CSimpleExAppView::NewLC(*this);AddViewL(*iAppView);CleanupStack::Pop(iAppView);}In the UIQ version of the UI class, I implemented only one method:the secondary constructor, ConstructL().
This function simply createsthe application view and registers it via AddViewL(). As we will seeshortly, in UIQ the HandleCommandL() function is handled in the viewclass instead of the UI class like in the S60 version. The view mechanismshows its full benefits when using multiple views, but we use it here onlyfor a single view.SimpleEx View.cppThis file contains the application view class. The ConstructL() methodof the view class is called by the UI framework after the view class isinstantiated, and it’s this method that creates the main application windowand activates it for display.Draw() is a method called by the framework for every control inorder to draw it to the screen. The application view is a control, and,for this example, I implement the Draw() function to output the text‘Simple Example’ in the center of the window.
The drawing is performedby opening a graphics context (GC), getting a font, and calling thecontext’s DrawText() function. Cleanup is performed on the font uponcompletion.Following is the view class for S60:/*=================================================================File: simpleEx_view.cppThis file contains the application view class for SimpleEx.==================================================================*/#include "eikenv.h"#include <coemain.h>#include "SimpleEx.h"CSimpleExAppView* CSimpleExAppView::NewL(const TRect& aRect){CSimpleExAppView* self = CSimpleExAppView::NewLC(aRect);CleanupStack::Pop(self);return self;}CSimpleExAppView* CSimpleExAppView::NewLC(const TRect& aRect){CSimpleExAppView* self = new (ELeave) CSimpleExAppView;CleanupStack::PushL(self);self->ConstructL(aRect);return self;SIMPLE EXAMPLE APPLICATION53}void CSimpleExAppView::ConstructL(const TRect& aRect){CreateWindowL();SetRect(aRect);ActivateL();}void CSimpleExAppView::Draw(const TRect& ) const{CWindowGc& gc = SystemGc();TRect drawRect = Rect();gc.Clear();const CFont* font = iEikonEnv->TitleFont();gc.UseFont(font);TInt baselineOffset=(drawRect.Height() - font->HeightInPixels())/2;gc.DrawText(_L("Simple Example"),drawRect,baselineOffset,CGraphicsContext::ECenter, 0);gc.DiscardFont();}The following shows the view class for UIQ:/*=================================================================File: simpleEx_view.cppThis file contains the application view class for UIQ SimpleEx.==================================================================*/#include "eikenv.h"#include <coemain.h>#include "SimpleEx.h"#include "SimpleEx.rsg"#include <QikCommand.h>CSimpleExAppView* CSimpleExAppView::NewL(CQikAppUi& aAppUi){CSimpleExAppView* self = CSimpleExAppView::NewLC(aAppUi);CleanupStack::Pop(self);return self;}CSimpleExAppView* CSimpleExAppView::NewLC(CQikAppUi& aAppUi){CSimpleExAppView* self = new (ELeave) CSimpleExAppView(aAppUi);CleanupStack::PushL(self);self->ConstructL();return self;}CSimpleExAppView::CSimpleExAppView(CQikAppUi& aAppUi): CQikViewBase(aAppUi, KNullViewId){}void CSimpleExAppView::ConstructL(){BaseConstructL();}void CSimpleExAppView::ViewConstructL(){// Loads information about the UI configurations this view supports54SYMBIAN OS QUICK START// together with definition of each view.ViewConstructFromResourceL(R_SIMPLEEX_CONFIGURATIONS);}TVwsViewId CSimpleExAppView::ViewId()const{return TVwsViewId(KUidSimpleExApp, KUidSimpleExView);}void CSimpleExAppView::Draw(const TRect& ) const{CWindowGc& gc = SystemGc();TRect drawRect = Rect();gc.Clear();const CFont* font = iEikonEnv->TitleFont();gc.UseFont(font);TInt baselineOffset=(drawRect.Height() - font->HeightInPixels())/2;gc.DrawText(_L("Simple Example"),drawRect,baselineOffset,CGraphicsContext::ECenter, 0);gc.DiscardFont();}void CSimpleExAppView::HandleCommandL(CQikCommand& aCommand){switch(aCommand.Id()){case ESimpleExCommand:{_LIT(KMessage,"Start Selected!");iEikonEnv->AlertWin(KMessage);break;}default:CQikViewBase::HandleCommandL(aCommand);}}As you can see, the UIQ view class is more complex than the S60 one.The UIQ version of the view class has the method ViewConstructL(),which in turns calls the function ViewConstructFromResourceL(RSIMPLEEX CONFIGURATIONS).
Remember that R SIMPLEEXCONFIGURATIONS is the name of the QIK VIEW CONFIGURATIONSresource defined in the UIQ resource file, which points to the commandsassociated with the different display modes of the phone (in our case,the same command set). The UIQ view class has the HandleCommandL() function in it that handles these commands. ViewId() returnsthe UID of the application along with a view ID that uniquely identifiesthis view (via the line: TVwsViewId(KUidSimpleExApp, KUidSimpleExView)). In SimpleEx.h, this unique view ID (as specified inKUidSimpleExView) is 1.At this point you should have all the source files in the src directory.2.3.6Project Build FilesNow, let’s create the project build files: SimpleEx.mmp and bld.inf.SIMPLE EXAMPLE APPLICATION55Creating the SimpleEx.mmp project definition fileThis section shows the project definition files for the example for S60 andUIQ (they are very similar except for the platform-specific library shownin bold in the listings).
Use the file corresponding to your platform. Namethe file SimpleEx.mmp and place it in the group directory.The S60 project file is as follows:TARGETSimpleEx.exeTARGETTYPEexeUID0x100039CE 0xE000027FSOURCEPATH..\srcSOURCESimpleEx.cppSOURCESimpleEx_app.cppSOURCESimpleEx_view.cppSOURCESimpleEx_ui.cppSOURCESimpleEx_doc.cppSOURCEPATH..\groupSTART RESOURCESimpleEx_reg.rssTARGETPATH\private\10003a3f\appsENDSTART RESOURCESimpleEx.rssHEADERTARGETPATH resource/appsENDSYSTEMINCLUDE\epoc32\includeUSERINCLUDE..\includeLIBRARYeuser.lib apparc.lib cone.lib eikcore.libLIBRARYavkon.lib gdi.libThe UIQ project file is as follows:TARGETSimpleEx.exeTARGETTYPEexeUID0x100039CE 0xE000027FSOURCEPATH..\srcSOURCESimpleEx.cppSOURCESimpleEx_app.cppSOURCESimpleEx_view.cppSOURCESimpleEx_ui.cppSOURCESimpleEx_doc.cppSOURCEPATH..\groupSTART RESOURCESimpleEx_reg.rssTARGETPATH\private\10003a3f\appsENDSTART RESOURCESimpleEx.rssHEADERTARGETPATH resource/appsENDSYSTEMINCLUDE\epoc32\includeUSERINCLUDE..\includeLIBRARYeuser.lib apparc.lib cone.lib eikcore.libLIBRARYqikcore.lib gdi.lib56SYMBIAN OS QUICK STARTThe project definition file is a Symbian OS-specific text file that endsin .mmp and defines how to build the application.