examples/SFExamples/OandXViewArch/S60/src/oandxhistview.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 
00031 #include <eiklabel.h> // CEikLabel
00032 #include <barsread.h>  // for TResourceReader
00033 
00034 #include "OandXDefs.h"
00035 #include "OandXAppUi.h"
00036 #include "OandXApplication.h"
00037 #include "OandXController.h"
00038 #include "OandXHistView.h"
00039 #include "OandX.hrh"
00040 #include "OandX.pan"
00041 #include <OandX.rsg>
00042 
00043 // View
00044 
00045 COandXHistoryView* COandXHistoryView::NewLC()
00046         {
00047         COandXHistoryView* self=new(ELeave) COandXHistoryView();
00048         CleanupStack::PushL(self);
00049         self->ConstructL();
00050         return self;
00051         }
00052 
00053 COandXHistoryView::COandXHistoryView()
00054         {
00055         }
00056 
00060 COandXHistoryView::~COandXHistoryView()
00061         {
00062     if (iHistViewStacked)
00063         {
00064         AppUi()->RemoveFromViewStack( *this, iContainer );
00065         }
00066         delete iContainer;
00067         }
00068 
00069 void COandXHistoryView::ConstructL()
00070         {
00071     BaseConstructL( R_OANDX_HISTORY_VIEW );
00072         iContainer = COandXHistViewContainer::NewL(ClientRect())    ;
00073         }
00074         
00075 
00079 TUid COandXHistoryView::Id() const
00080         {
00081     return KUidOandXHistoryView;
00082         }
00083 
00084 /*
00085 Sets the appropriate commands in the menu.
00086 */
00087 void COandXHistoryView::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
00088         {
00089         if (aResourceId == R_OANDX_HISTORY_MENU)
00090                 {
00091                 if (IsDisplayingHistory())
00092                         {
00093                         aMenuPane->DeleteMenuItem(EOandXDisplayHistory);
00094                         }
00095                 else
00096                         {
00097                         aMenuPane->DeleteMenuItem(EOandXDisplayStats);
00098                         }
00099                 }
00100         }
00101 
00102 COandXHistViewContainer* COandXHistoryView::Container()
00103     {
00104     return iContainer;
00105     }
00106 
00107 /*
00108 Handles all commands in the view
00109 Called by the UI framework when a command has been issued.
00110 The command Ids are defined in the .rss file.
00111 */
00112 void COandXHistoryView::HandleCommandL(TInt aCommand)
00113         {
00114     switch ( aCommand )
00115         {
00116 
00117         case EAknSoftkeyBack:
00118             {
00119             AppUi()->HandleCommandL( EEikCmdExit );
00120             break;
00121             }
00122 
00123         default:
00124             {
00125             AppUi()->HandleCommandL( aCommand );
00126             break;
00127             }
00128         }
00129         }
00130 
00131 void COandXHistoryView::HandleViewRectChange()
00132     {
00133     if ( iContainer )
00134         {
00135         iContainer->SetRect(ClientRect());
00136         }
00137     }
00138 
00139 /* 
00140 This function is called by the View Server when the view is activated.
00141 The first time that it is called it constructs the History and Stats controls.
00142 It processes the message to determine which control to show
00143 */
00144 void COandXHistoryView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
00145                                     TUid aCustomMessageId,
00146                                     const TDesC8& /*aCustomMessage*/)
00147         {
00148         __ASSERT_ALWAYS(!iHistViewStacked, Panic(EOandXControlAlreadyStacked));
00149     AppUi()->AddToStackL( *this, iContainer );
00150     iHistViewStacked = ETrue;
00151     
00152         switch ( aCustomMessageId.iUid )
00153                 {               
00154         // switch using  the message ID
00155         case EHistoryViewDisplayHistory:
00156                 ChangeDisplayL( ETrue );
00157                 break;
00158         case EHistoryViewDisplayStats:
00159                 ChangeDisplayL( EFalse );
00160                 break;
00161         default:
00162                 // display as last time.
00163                 ChangeDisplayL( iDisplayingHistory );
00164                 break ;
00165                 }
00166         iActivated = ETrue;
00167         }
00168         
00169 /*
00170 Called by the View Server when this view is deactivated.
00171 Hides the view (and all of its components ).
00172 There is no data to save
00173 */
00174 void COandXHistoryView::DoDeactivate()
00175         {
00176         __ASSERT_ALWAYS(iHistViewStacked, Panic(EOandXControlNotStacked));
00177     AppUi()->RemoveFromViewStack( *this, iContainer );
00178     iHistViewStacked = EFalse;
00179 
00180         iContainer->MakeVisible(EFalse);
00181         iActivated = EFalse;
00182         }
00183 
00184 /*
00185 Utility function that manages the state of the History and Stats controls
00186 */      
00187 void COandXHistoryView::ChangeDisplayL( TBool aDisplayHistory ) 
00188         {
00189         iDisplayingHistory =  aDisplayHistory;
00190         iContainer->SetContentL(aDisplayHistory);
00191         iContainer->MakeVisible(ETrue);
00192         }       
00193 
00194 
00195 // Container
00196 
00197 COandXHistViewContainer* COandXHistViewContainer::NewL(const TRect& aRect)
00198         {
00199         COandXHistViewContainer* self = new (ELeave) COandXHistViewContainer();
00200     CleanupStack::PushL( self );
00201     self->ConstructL(aRect);
00202     CleanupStack::Pop(); // self
00203     return self;
00204         }
00205 
00206 COandXHistViewContainer::~COandXHistViewContainer()
00207         {
00208         delete iTitle;
00209         for (TInt i=0; i< KNumDataLines; i++)
00210                 {
00211                 delete iDataLines[i];
00212                 }
00213         }
00214 
00215 COandXHistViewContainer::COandXHistViewContainer()
00216         {
00217         }
00218 
00219 void COandXHistViewContainer::ConstructL(const TRect& aRect)
00220         {
00221         // Create a window for this application view
00222         CreateWindowL();
00223         
00224         // Create components
00225         TResourceReader reader;
00226         iTitle = new (ELeave) CEikLabel();
00227     iTitle->SetContainerWindowL( *this );
00228         iEikonEnv->CreateResourceReaderLC(reader, R_HISTORY_VIEW_LABEL);
00229         iTitle->ConstructFromResourceL(reader);
00230         CleanupStack::PopAndDestroy(); // reader
00231         
00232         for (TInt i=0; i< KNumDataLines; i++)
00233                 {
00234                 iDataLines[i] = new (ELeave) CEikLabel();
00235             iDataLines[i]->SetContainerWindowL( *this );
00236         iEikonEnv->CreateResourceReaderLC(reader, R_HISTORY_VIEW_LABEL);
00237         iDataLines[i]->ConstructFromResourceL(reader);
00238         CleanupStack::PopAndDestroy(); // reader
00239                 iDataLines[i]->SetLabelAlignment(ELayoutAlignLeft);
00240                 }
00241     
00242         iEikonEnv->ReadResourceL(iNumGamesText, R_HISTORY_VIEW_GAMES_TEXT);
00243         iEikonEnv->ReadResourceL(iNumOWinsText, R_HISTORY_VIEW_OWINS_TEXT);
00244         iEikonEnv->ReadResourceL(iNumXWinsText, R_HISTORY_VIEW_XWINS_TEXT);
00245         iEikonEnv->ReadResourceL(iNumDrawsText, R_HISTORY_VIEW_DRAWN_TEXT);
00246         iEikonEnv->ReadResourceL(iStatOWonText, R_HISTORY_VIEW_O_WINNER_TEXT);
00247         iEikonEnv->ReadResourceL(iStatXWonText, R_HISTORY_VIEW_X_WINNER_TEXT);
00248         iEikonEnv->ReadResourceL(iStatDrawText, R_HISTORY_VIEW_NO_WINNER_TEXT);
00249         iEikonEnv->ReadResourceL(iHistoryTitle, R_HISTORY_VIEW_HISTORY_TITLE);
00250         iEikonEnv->ReadResourceL(iStatsTitle, R_HISTORY_VIEW_STATS_TITLE);      
00251 
00252         // Set the window's size
00253         SetRect(aRect); // needs to be after component creation - see SizeChanged()
00254 
00255         // Activate the window, which makes it ready to be drawn
00256         ActivateL();
00257         }
00258 
00259 void COandXHistViewContainer::SizeChanged()
00260         {
00261         TRect rect = Rect();
00262         
00263         TInt labelHeight = (rect.iBr.iY - rect.iTl.iY)/(KNumDataLines+1);
00264         rect.iBr.iY = labelHeight;
00265         iTitle->SetRect(rect);
00266         for (TInt i=0; i<KNumDataLines; i++)
00267                 {
00268                 rect.iTl.iY += labelHeight;
00269                 rect.iBr.iY += labelHeight;
00270                 iDataLines[i]->SetRect(rect);
00271                 }
00272         }
00273 
00274 void COandXHistViewContainer::Draw(const TRect& /*aRect*/) const
00275         {
00276         CWindowGc& gc = SystemGc();
00277         gc.Clear();
00278         }
00279 
00280 void COandXHistViewContainer::SetContentL( TBool aDisplayHistory )
00281         {
00282         CreateNewItemsL(aDisplayHistory);
00283         DrawNow();
00284         }
00285         
00286 void COandXHistViewContainer::CreateNewItemsL(TBool aDisplayHistory)
00287         {
00288         // Clear all existing data
00289         TBuf<KFormatBufSize> itemName;
00290         iEikonEnv->ReadResourceL(itemName, R_HISTORY_VIEW_NO_DATA_TEXT);
00291         for (TInt i=0; i<KNumDataLines; i++)
00292                 {
00293                 iDataLines[i]->SetTextL(itemName);
00294                 }
00295 
00296         // Insert the data to be displayed
00297         TUint played = Controller().GamesPlayed();
00298         
00299         if (aDisplayHistory)
00300                 {
00301                 // Set the title
00302                 iTitle->SetTextL(iHistoryTitle);
00303                 // Add all available history data
00304                 for (TInt i=0; i<KNumHistoryRecords; i++)
00305                         {
00306                         switch (Controller().GameRecord(i))
00307                                 {
00308                         case ETileNought:
00309                                 itemName.Format(iStatOWonText, played - i);
00310                                 break;
00311                         case ETileCross:
00312                                 itemName.Format(iStatXWonText, played - i);
00313                                 break;
00314                         case ETileDraw:
00315                                 itemName.Format(iStatDrawText, played - i);
00316                                 break;
00317                         default:
00318                                 iEikonEnv->ReadResourceL(itemName, R_HISTORY_VIEW_NO_DATA_TEXT);
00319                                 break;
00320                                 }
00321                         iDataLines[i]->SetTextL(itemName);
00322                         }
00323                 }
00324         else // Displaying statistics
00325                 {
00326                 // Set the title
00327                 iTitle->SetTextL(iStatsTitle);
00328                 
00329                 // Total games played
00330                 itemName.Format(iNumGamesText, played);
00331                 iDataLines[0]->SetTextL(itemName);
00332 
00333                 // Wins by Noughts
00334                 TUint oWins = Controller().WonByO();
00335                 itemName.Format(iNumOWinsText, oWins);
00336                 iDataLines[1]->SetTextL(itemName);
00337 
00338                 // Wins by Crosses
00339                 TUint xWins = Controller().WonByX();
00340                 itemName.Format(iNumXWinsText, xWins);
00341                 iDataLines[2]->SetTextL(itemName);
00342                 
00343                 // Drawn (or abandoned) games
00344                 itemName.Format(iNumDrawsText, played - oWins - xWins);
00345                 iDataLines[3]->SetTextL(itemName);
00346                 }
00347         }
00348 
00349 TInt COandXHistViewContainer::CountComponentControls() const
00350         {
00351         return KNumDataLines+1;
00352         }
00353 
00354 CCoeControl* COandXHistViewContainer::ComponentControl(TInt aIndex) const
00355         {
00356         if (aIndex==0)
00357                 {
00358                 return iTitle;
00359                 }
00360         else
00361                 {
00362                 return iDataLines[aIndex-1];
00363                 }
00364         }

Generated by  doxygen 1.6.2