Wiley.Symbian.OS.C.plus.plus.for.Mobile.Phones.Aug.2007 (779890), страница 73
Текст из файла (страница 73)
The first is the application’s globally unique,Symbian-allocated UID, for example:const TUid KAppUid = { 0x0257696A } ;// Symbian allocated UIDThe second is a number, also in the form of a TUid, that needs to beunique only within the application itself, such as:const TUid KViewNumber ={ 0x00000001 } ;// View numberThe two values are combined into a TVwsViewId instance as follows:const TVwsViewId KViewId { KAppUid, KViewNumber } ;// Unique view IDThis value must be returned by the view’s ViewId() function. It isused both to register the view and to activate it. When a view is activated,it is passed the view ID of the previously active view, which is useful ifthe application needs to return from the current view to the one that waspreviously visible.398VIEWS AND THE VIEW ARCHITECTUREConstructionThe construction of a view can be divided between the view’sConstructL() function and the MCoeView interface function, ViewConstructL(). Views supply a default ViewConstructL() functionthat does nothing, so you are free to choose whether or not to provideyour own implementation.The framework calls a view’s ViewConstructL() immediatelybefore the view is activated for the first time, so putting the bulk ofthe construction into ViewConstructL() can considerably improvean application’s start-up response time, especially if the application hasmany views.
However, you should avoid putting large amounts of timeconsuming code into ViewConstructL(), since this may cause theview server to time out.Activation and DeactivationWhen a view is activated, the view server calls the view’s ViewActivatedL() function, passing any custom message ID and messagedata that was specified in the call to the application UI’s ActivateViewL(). In S60, CAknView implements ViewActivatedL() which,in turn, calls another pure virtual function, DoActivateL().
Depending on the UI, your derived view class must implement one of these twofunctions.What your implementation does depends on your design. Typically,you process any passed message ID and data and make the view and itscontents visible. You might also need to do some additional constructionand layout.The UIQ view server maintains only one system-wide active view sowhen a view is activated the view server calls ViewDeactivated()on any previously active view, in any application. The S60 view servermaintains one active view for each running application, so the deactivation message is only sent when the new view is in the same application.As with ViewActivatedL(), S60’s CAknView implements ViewDeactivated() and provides DoDeactivateL().
Again depending onthe UI, your derived view class must implement one function or the other.On deactivation, a view typically makes itself invisible. Any otheractions depend on the nature and design of the application.14.3Introduction to the Example ApplicationNow that the basic principles have been described, the remainder of thischapter illustrates the use of views by extending the Noughts and Crossesexample that was first introduced in Chapter 12. This extension adds aINTRODUCTION TO THE EXAMPLE APPLICATIONCOandXAppUi1399CQikViewBase111COandXViewFigure 14.5COandXHistoryViewMultiple view ownershipsecond view, as illustrated in Figure 14.5, to display information aboutpast games.You can choose to display either a game history, which lists the resultsof the last six games, or the game statistics, which include the total numberof games played and the number of wins for each player.
Figure 14.6shows the statistics displayed on a UIQ phone.As you can see from this illustration, the view has its own menu ofcommands, which is the case for most applications with multiple views.Figure 14.6Viewing the game statistics400VIEWS AND THE VIEW ARCHITECTUREThe data for the additional view, together with the code that generatesit, is implemented within the game’s COandXController class. Theextension to this class is not described here, as it is not directly relevantto the workings of views; if you are interested in the implementation,you can look at the example code that is available for downloading fromdeveloper.symbian.com/main/academy/press.One word of warning – the additional data is saved in the application’sINI file.
This means that the INI file is larger than it was for Chapter 12’sversion of the game. If you run this version after having run the earlierone, you must either first delete the old file (in the emulator it is locatedin epoc32\winscw\c\Private\e04e4143) or accept that the view’sdata is incorrect until you first select the Reset history menu item.The History ViewIn order to keep the code simple, and to reduce the code differencesbetween the S60 and UIQ versions, we’ve chosen to implement the newview as a collection of label controls. Figure 14.7 shows the class diagramfor the UIQ version of the view.CQikViewBaseCOandXHistoryViewCEikLabel1Figure 14.7nHistory view for UIQThe relevant data members of the view class are defined in oandxhistview.h as follows:const TInt KNumDataLines = KNumHistoryRecords+1;class COandXHistoryView : public CQikViewBase{...private:...CEikLabel* iTitle;TFixedArray<CEikLabel*, KNumDataLines> iDataLines;...};INTRODUCTION TO THE EXAMPLE APPLICATION401The value of KNumHistoryRecords is defined in oandxdefs.h as:static const TInt KNumHistoryRecords = 6;The component labels are logically divided into one title line and anarray of seven data lines, the last of which is not used for displaying data.In the UIQ version, the view’s structure and layout can be specified inthe application’s resource script, oandx.rss, by a QIK_VIEW_CONFIGURATIONS item.RESOURCE QIK_VIEW_CONFIGURATIONS r_history_view_ui_configurations{configurations ={QIK_VIEW_CONFIGURATION{ui_config_mode = KQikPenStyleTouchPortrait;view = r_history_view_layout;command_list = r_history_view_commands;},QIK_VIEW_CONFIGURATION{ui_config_mode = KQikSoftkeyStylePortrait;view = r_history_view_layout;command_list = r_history_view_commands;}};}The corresponding QIK_VIEW and QIK_VIEW_PAGES items are asfollows:RESOURCE QIK_VIEW r_history_view_layout{pages = r_history_view_layout_pages;}RESOURCE QIK_VIEW_PAGES r_history_view_layout_pages{pages ={QIK_VIEW_PAGE{page_id = EOandXHistoryViewPage;page_content = r_history_view_page_control;}};}The content and layout of the view’s control are specified by theQIK_CONTAINER_SETTINGS item.402VIEWS AND THE VIEW ARCHITECTURERESOURCE QIK_CONTAINER_SETTINGS r_history_view_page_control{layout_manager_type = EQikRowLayoutManager;layout_manager = r_history_view_row_layout_default;controls ={QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewLabelCtrl;type = EEikCtLabel;control = r_history_view_title_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData1Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData2Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData3Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData4Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData5Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData6Ctrl;type = EEikCtLabel;control = r_history_view_data_label;},QIK_CONTAINER_ITEM_CI_LI{unique_handle = EHistoryViewData7Ctrl;type = EEikCtLabel;control = r_history_view_data_label;// The following makes the Label fill the entire remaining// application space.layout_data = r_history_view_row_layout_data_fill;}};}INTRODUCTION TO THE EXAMPLE APPLICATION403Again, this is just an array of labels, the first of which is used for a title.RESOURCE LABEL r_history_view_title_label{horiz_align=EEikLabelAlignHCenter;standard_font=EEikLabelFontLegend;txt = ""; // Text supplied dynamically}The remaining labels are used to display data.RESOURCE LABEL r_history_view_data_label{horiz_align=EEikLabelAlignHLeft;txt = ""; // Text supplied dynamically}The control uses a default layout, which arranges the labels vertically,one above the other, each with its natural, default height.
The last, unusedlabel is set to occupy all the remaining space in the screen area allocatedto the control.The S60 version of the History view, whose class diagram is shown inFigure 14.8, uses the same collection of controls, but the implementationis slightly different.CAknViewCCoeControlCOandXHistoryViewCOandXHistViewContainer11Figure 14.8CEikLabel1nHistory view for S60The view itself, defined in the S60 version of oandxhistview.h,owns a single container control, COandXHistViewContainer, whichis implemented as a normal compound control.class COandXHistoryView : public CAknView{...private:COandXHistViewContainer* iContainer;...};404VIEWS AND THE VIEW ARCHITECTUREconst TInt KNumDataLines = KNumHistoryRecords+1;class COandXHistViewContainer : public CCoeControl{...private:...void ConstructL(const TRect& aRect);void Draw(const TRect& aRect) const;void SizeChanged();TInt CountComponentControls() const;CCoeControl* ComponentControl(TInt aIndex) const;...private:CEikLabel* iTitle;TFixedArray<CEikLabel*, KNumDataLines> iDataLines;...};The container, in turn, owns the same collection of labels as in theUIQ version, but this time they are created in code from a single LABELresource.void COandXHistViewContainer::ConstructL(const TRect& aRect){// Create a window for this application viewCreateWindowL();// Create componentsTResourceReader reader;iTitle = new (ELeave) CEikLabel();iTitle->SetContainerWindowL(*this);iEikonEnv->CreateResourceReaderLC(reader, R_HISTORY_VIEW_LABEL);iTitle->ConstructFromResourceL(reader);CleanupStack::PopAndDestroy(); // readerfor (TInt i=0; i< KNumDataLines; i++){iDataLines[i] = new (ELeave) CEikLabel();iDataLines[i]->SetContainerWindowL(*this);iEikonEnv->CreateResourceReaderLC(reader, R_HISTORY_VIEW_LABEL);iDataLines[i]->ConstructFromResourceL(reader);CleanupStack::PopAndDestroy(); // readeriDataLines[i]->SetLabelAlignment(ELayoutAlignLeft);}...// Set the window’s sizeSetRect(aRect); // needs to be after component creation - see// SizeChanged()// Activate the window, which makes it ready to be drawnActivateL();}INTRODUCTION TO THE EXAMPLE APPLICATION405The control’s SizeChanged() function performs the layout of thelabels as follows:void COandXHistViewContainer::SizeChanged(){TRect rect = Rect();TInt labelHeight = (rect.iBr.iY - rect.iTl.iY)/(KNumDataLines+1);rect.iBr.iY = labelHeight;iTitle->SetRect(rect);for (TInt i=0; i<KNumDataLines; i++){rect.iTl.iY += labelHeight;rect.iBr.iY += labelHeight;iDataLines[i]->SetRect(rect);}}In this case, for simplicity, the available vertical space is dividedequally between all the labels, including the last, unused one.The control’s ComponentControl() and CountComponentControls() functions are defined as follows:CCoeControl* COandXHistViewContainer::ComponentControl(TInt aIndex) const{if (aIndex==0){return iTitle;}else{return iDataLines[aIndex-1];}}TInt COandXHistViewContainer::CountComponentControls() const{return KNumDataLines+1;}Since the labels fill the entire area of the control, the Draw() functionhas nothing to do other than to clear the entire rectangle.void COandXHistViewContainer::Draw(const TRect& /*aRect*/) const{CWindowGc& gc = SystemGc();gc.Clear();}406VIEWS AND THE VIEW ARCHITECTUREAdditional S60 ConsiderationsUnlike the corresponding UIQ version, the S60 version that was describedin Chapter 12 did not use a view for its display of the game.