Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (779887), страница 72
Текст из файла (страница 72)
Here is the UIQ commandhandler in the view class:void CSimpleExAppView::HandleCommandL(CQikCommand& aCommand){switch(aCommand.Id()){case ESimpleExCommand:{_LIT(KMessage,"Start Selected!");iEikonEnv->AlertWin(KMessage);break;}default:CQikViewBase::HandleCommandL(aCommand);}}Now let’s look at the view class for S60. Its declaration is below:class CSimpleExAppView : public CCoeControl{public:static CSimpleExAppView* NewL(const TRect& aRect);static CSimpleExAppView* CSimpleExAppView::NewLC(const TRect& aRect);private:void Draw(const TRect&) const;void ConstructL(const TRect& aRect);};Since we just have a single view, I chose to inherit the S60 viewclass directly from CCoeControl – we do not need the added supportof CAknView. Here is the implementation of the S60 view class: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;RESOURCE FILES377CleanupStack::PushL(self);self->ConstructL(aRect);return self;}void CSimpleExAppView::ConstructL(const TRect& aRect){CreateWindowL();SetRect(aRect);ActivateL();}void CSimpleExAppView::Draw(const TRect&) const{CWindowGc& gc = SystemGc();gc.Clear();const CFont*font = iEikonEnv->TitleFont();gc.UseFont(font);TRect drawRect = Rect();TIntbaselineOffset=(drawRect.Height() - font->HeightInPixels())/2;_LIT(KScreenText,"Simple Example");gc.DrawText(KScreenText,drawRect,baselineOffset,CGraphicsContext::ECenter, 0);gc.DiscardFont();}The difference from UIQ here is that, since we have directly inheritedfrom CCoeControl, in ConstructL() we have to create the viewwindow, size it, and activate it.
Furthermore, our command handler isnot here in the view like in UIQ, and is instead in the UI class.12.4 Resource FilesThe application resource file defines a significant part of how yourapplication will appear and function. The resource file is a text file whosename ends in .rss, and is compiled into a binary form by the SDK’sresource compiler. This compiled version of the resource file is loadedonto the phone along with the application executable and is accessedduring application execution.12.4.1 Resource File FormatA resource file consists of data constructs that begin with an uppercasekeyword.
There are only a few keywords used in resource files. The mainones are:•NAMENAME defines a name, of between one and four uppercase characters,used by the resource compiler to generate a 20-bit number thatit prefixes to resource identifiers to ensure they are distinguishablefrom the identifiers of other resources used by the application. Note378GUI APPLICATION PROGRAMMINGthat this means the name need not be globally unique, it just needsto be different from system resource file names – so avoid startingthe name with EIK, or using component names like CONE.
If yourapplication uses multiple resource files, define a unique name foreach.In SimpleEx, the name is defined in the resource file as NAMESIMP•CHARACTER SETCHARACTER SET specifies if your resource file is to use either codepage 1250 or the UTF-8 character set. If CHARACTER SET is notspecified, it defaults to code page 1250.
To specify that your resourcefile is in UTF-8 format, add CHARACTER SET UTF8 to your resourcefile.•STRUCTThe STRUCT keyword is used to define a data structure that consistsof a sequence of items, with each item being specified by its nameand its data type.You won’t often need to define your own STRUCT’s, since there is awide variety of existing ones, for use by all the different GUI elements, anddefined in the system’s various resource header files (e.g., eikon.rh,uikon.rh). However, it is helpful to see what a STRUCT looks like, inorder to better understand the resource file format.•RESOURCEThe RESOURCE keyword is used to create an instance of a datastructure.•ENUMThe ENUM keyword defines an enumeration with the same syntax asin C/C++.
This is used for constants such as control identifiers andevent codes.The following is a simple STRUCT definition:STRUCT MYDATA{WORD value=0;LTEXT main_text;LTEXT text_items[];}RESOURCE FILES379WORD and LTEXT are built-in data types that represent a 16-bit wordand a Unicode text string (with leading length byte), respectively. Othercommon data types are:BYTELONGBUFLLINKSTRUCT8-bit signed value4-byte valueUnicode string with no leading length byteLink to a resource that contains a resourceidentifierUse a STRUCT within a STRUCTArrays can also be defined by appending [] to the attribute name. In theMYDATA structure, text items is defined as an array of text strings.Attributes can also be assigned default values within the STRUCTdefinition.
In the example above, value is assigned a default of 0. So,if the programmer does not assign an explicit value to the attributein a RESOURCE definition, it is automatically assigned the defaultvalue.You use the RESOURCE keyword to create an instance of a STRUCT,as illustrated below for MYDATA:RESOURCE MYDATA r_mydata_res{value=3;main_text="some text string";text_items={"text item1", "some other item","other item"};}This resource creates an instance of the MYDATA structure with aresource identifier of r mydata res (which actually represents a 32-bitinteger).
You access the resource from within program code by using thisidentifier in upper case: R MYDATA RES.It’s worth pointing out that you can include STRUCT members withinSTRUCT’s since this is commonly used for predefined resources. I illustratethis below, using an additional STRUCT called WIDGETDATA:STRUCT WIDGETDATA{LTEXT widget_caption;STRUCT main_data;}main data is specified as a STRUCT but does not indicate thestructure type.
It’s up to the programmer to know what type of structure380GUI APPLICATION PROGRAMMINGto use. In this case, WIDGETDATA expects it to be of our MYDATA type.It’s initialized in the following way:RESOURCE WIDGETDATA r_my_widget{widget_caption="Widget Name";main_data=MYDATA {3,"main data", { "item1", "item2"} };}12.4.2 SimpleEx’s Resource FileArmed with the knowledge in the above section, let’s look at the resourcesfile again (originally presented in Chapter 2 (see section 2.3.4)), startingwith UIQ and then S60.UIQ SimpleEx resourceFollowing is the SimpleEx resource file for UIQ 3:NAME SIMP#include <eikon.rh>#include <qikon.rh>#include <QikCommand.rh>#include "SimpleEx.hrh"RESOURCE RSS_SIGNATURE{}RESOURCE TBUF r_default_document_name{buf="";}RESOURCE EIK_APP_INFO{}RESOURCE QIK_VIEW_CONFIGURATIONS r_simpleex_configurations{configurations ={QIK_VIEW_CONFIGURATION{ui_config_mode = KQikPenStyleTouchPortrait;command_list = r_simpleex_commands;view = r_simpleex_layout;},QIK_VIEW_CONFIGURATION{ui_config_mode = KQikSoftkeyStyleSmallPortrait;command_list = r_simpleex_commands;view = r_simpleex_layout;}};}RESOURCE QIK_VIEW r_simpleex_layout{RESOURCE FILES381pages = r_simpleex_layout_pages;}// Defines the pages of a view.// Only one page for this example.RESOURCE QIK_VIEW_PAGES r_simpleex_layout_pages{pages ={QIK_VIEW_PAGE{page_id = ESimpleExViewPage;}};}RESOURCE QIK_COMMAND_LIST r_simpleex_commands{items ={QIK_COMMAND{id = ESimpleExCommand;type = EQikCommandTypeScreen;text = "Start";}};}The RSS SIGNATURE resource is used to validate the file and mustappear, exactly as shown above, as the first resource in every applicationresource file.The next resource:RESOURCE TBUF r_default_document_name{buf="";}defines the file name of your application’s default document.
A documentis not used in SimpleEx, so it is blank.Note that TBUF is defined as:STRUCT TBUF{BUF buf; // non-zero terminated string}In UIQ, EIK APP INFO should be defined empty – the defaults areappropriate.The QIK VIEW CONFIGURATIONS resource specifies what displayand user configurations are supported by the application. I introducedthese modes in section 12.1.1.
The resource attribute configurationsis an array of QIK VIEW CONFIGURATION structures that define the382GUI APPLICATION PROGRAMMINGsupported configurations along with the commands and view pagesassociated with that mode. ui config mode is set to the supportedconfiguration. The table below lists the major configurations that exist(their support varies per smartphone):KQikPenStyleTouchPortraitPen interaction style, portrait displayKQikPenStyleTouchLandscapePen interaction style, landscapedisplayKQikSoftkeyStylePortraitSoftkey interaction style, portraitdisplayKQikSoftkeyStyleLandscapeSoftkey interaction style, landscapedisplayKQikSoftkeyStyleSmallPortaitSoftkey interaction style, portrait,small screen sizeKQikSoftkeyStyleSmallLandscapeSoftkey interaction style, landscapedisplay, small screen sizeKQikSoftkeyStyleTouchPortraitSoftkey interaction style withtouchscreen, portrait displayKQikSoftkeyStyleTouchLandscapeSoftkey interaction style withtouchscreen, landscape displayThe command attribute is set to point to the user commands thattranslate to softkeys and menu items (it depends on mode, how these areactually presented).
As a side note, a command list can be referenced inother places besides configurations, like in views or view pages (althoughI will not cover these in this chapter).In our case we define only two modes – KQikPenStyleTouchPortrait and KQikSoftKeyStyleSmallPortrait. Both of thesepoint to the same QIK COMMAND LIST resource that defines our‘Start’ command (command list = r simpleex commands). Bothalso point to the same view (view = r simpleex layout).S60 SimpleEx resourceBelow is the S60 3rd Edition SimpleEx resource file:NAME SIMP#include <eikon.rh>#include <avkon.rh>#include <avkon.rsg>#include "SimpleEx.hrh"RESOURCE RSS_SIGNATURERESOURCE FILES383{}RESOURCE TBUF r_default_document_name{buf=" ";}RESOURCE EIK_APP_INFO{menubar = r_SimpleEx_menubar;cba = R_AVKON_SOFTKEYS_OPTIONS_EXIT;}RESOURCE MENU_BAR r_SimpleEx_menubar{titles ={MENU_TITLE{menu_pane = r_SimpleEx_menu;}};}RESOURCE MENU_PANE r_SimpleEx_menu{items ={MENU_ITEM{command = ESimpleExCommand;txt = "Start";}};}The first two resources, RSS SIGNATURE and the TBUF r defaultdocument name, are the same as UIQ.Instead of having an empty EIK APP INFO resource, however, S60defines a control button array (CBA) resource and a menu resource:RESOURCE EIK_APP_INFO{menubar = r_SimpleEx_menubar;cba = R_AVKON_SOFTKEYS_OPTIONS_EXIT;}The line cba = R AVKON SOFTKEYS OPTIONS EXIT causes the leftsoftkey on the application to read Options and display the menu, andthe right softkey to send the exit command to the application.