examples/ForumNokia/DescriptorExample/src/DescriptorExAppUi.cpp

00001 /*
00002  * Copyright � 2008 Nokia Corporation.
00003  */
00004 
00005 #include "DescriptorExAppUi.h"
00006 #include "DescriptorExContainer.h"
00007 #include <DescriptorEx.rsg>
00008 #include "descriptorex.hrh"
00009 #include <aknutils.h>
00010 
00011 #include <avkon.hrh>
00012 #include "DescriptorExamples.h"
00013 
00014 // -----------------------------------------------------------------------------
00015 // defines a type that is pointer to a example method in CDescriptorExamples
00016 // -----------------------------------------------------------------------------
00017 typedef void (CDescriptorExamples::*TExampleMethodPtr)();
00018 
00019 // -----------------------------------------------------------------------------
00020 // defines an array of pointers pointing to example methods
00021 // -----------------------------------------------------------------------------
00022 static const TExampleMethodPtr ExampleMethods[] =
00023     {
00024     &CDescriptorExamples::ToStack,
00025     &CDescriptorExamples::ToHeapL,
00026     &CDescriptorExamples::Literals,
00027 
00028     &CDescriptorExamples::NonModifyingMethods,
00029     &CDescriptorExamples::ModifyingMethodsL,
00030     &CDescriptorExamples::CharacterConversionsL,
00031     &CDescriptorExamples::LexicalAnalysis,
00032 
00033     &CDescriptorExamples::CircularBuffersL,
00034     &CDescriptorExamples::FlatDynamicBuffersL,
00035     &CDescriptorExamples::SegmentedDynamicBuffersL,
00036     &CDescriptorExamples::PackageBuffers,
00037     &CDescriptorExamples::RBufDemonstrations
00038     };
00039 static const TInt KNumberOfExampleMethods = sizeof(ExampleMethods) / 
00040                                             sizeof(TExampleMethodPtr);
00041 
00042 // -----------------------------------------------------------------------------
00043 // Executes requested method. If aNumber indexes out of bounds of method pointer
00044 // array, nothing is done.
00045 // -----------------------------------------------------------------------------
00046 LOCAL_C void ExecuteExampleMethodL(TInt aNumber, CDescriptorExamples &aExample)
00047     {
00048     if( aNumber >= 0 && aNumber < KNumberOfExampleMethods )
00049         {
00050         TExampleMethodPtr mPtr = ExampleMethods[aNumber];
00051         (aExample.*mPtr)(); // call example method through method pointer
00052         }
00053     }
00054 
00055 // -----------------------------------------------------------------------------
00056 // Second phase constructor of class CDescriptorExAppUi.
00057 // -----------------------------------------------------------------------------
00058 void CDescriptorExAppUi::ConstructL()
00059     {
00060     BaseConstructL(EAknEnableSkin);
00061 
00062     iAppContainer = new (ELeave) CDescriptorExContainer;
00063     iAppContainer->SetMopParent(this);
00064     iAppContainer->ConstructL( ClientRect() );
00065     AddToStackL( iAppContainer );
00066     iBuffer = HBufC::NewL(4096);
00067     iExamples = new (ELeave) CDescriptorExamples( this );
00068     }
00069 
00070 // -----------------------------------------------------------------------------
00071 // This destructor frees reserved resources
00072 // -----------------------------------------------------------------------------
00073 CDescriptorExAppUi::~CDescriptorExAppUi()
00074     {
00075     delete iExamples;
00076     iExamples = NULL;
00077     delete iBuffer;
00078     iBuffer = NULL;
00079     if (iAppContainer)
00080         {
00081         RemoveFromStack( iAppContainer );
00082         delete iAppContainer;
00083         iAppContainer = NULL;
00084         }
00085    }
00086 
00087 // -----------------------------------------------------------------------------
00088 // Run all example methods and write results to the file.
00089 // -----------------------------------------------------------------------------
00090 void CDescriptorExAppUi::RunAllL()
00091     {
00092     _LIT( KFileName, "c:\\Data\\DescriptorEx.log.txt" );
00093 
00094     CFileForwarder *fileWriter = new (ELeave) CFileForwarder();
00095     CleanupStack::PushL( fileWriter );
00096     fileWriter->ConstructL(KFileName);
00097 
00098     CDescriptorExamples *example = new (ELeave) CDescriptorExamples(fileWriter);
00099     CleanupStack::PushL( example );
00100 
00101     for( TInt i=0; i < KNumberOfExampleMethods; i++ )
00102         {
00103         ExecuteExampleMethodL(i, *example);
00104         }
00105     GetViewBuffer().AppendFormat( _L(
00106         "Results of example methods written to file\n\"%S\"\n"), &KFileName );
00107     UpdateView();
00108 
00109     CleanupStack::PopAndDestroy(2); // examples, fileWriter
00110     }
00111 
00112 // -----------------------------------------------------------------------------
00113 // Handles commands from framework. Expecting to receive only menu commands and
00114 // selecting back from right menu (EAknSoftkeyBack).
00115 // -----------------------------------------------------------------------------
00116 void CDescriptorExAppUi::HandleCommandL(TInt aCommand)
00117     {
00118     if( aCommand - TDescriptorExCommandFirstID < KNumberOfExampleMethods )
00119         {
00120         // reset internal buffer before example
00121         iBuffer->Des().SetLength(0);
00122 
00123         // call one of the example methods. Since commands start from
00124         // TDescriptorExCommandFirstID, it has to be decreased from aCommand
00125         // to get correct example method number (since indexing to array
00126         // starts from zero).
00127         ExecuteExampleMethodL( aCommand - TDescriptorExCommandFirstID,
00128                               *iExamples );
00129         }
00130 
00131     switch ( aCommand )
00132         {
00133         case EAknSoftkeyExit:
00134         case EEikCmdExit:
00135             {
00136             Exit();
00137             break;
00138             }
00139         case ECmd_RunAll:
00140             {
00141             //The RBuf demonstration (RBufDemonstrations) consists of code 
00142             //examples in manipulating RBuf objects and doesn't write any 
00143             //results to the file so it isn't executed here.
00144             //iExamples->RBufDemonstrations();
00145             RunAllL();
00146             break;
00147             }
00148         default:
00149             {
00150             break;      
00151             }
00152         };
00153     }
00154 
00155 // -----------------------------------------------------------------------------
00156 // Handle key events
00157 // -----------------------------------------------------------------------------
00158 TKeyResponse CDescriptorExAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,
00159                                                  TEventCode aType)
00160 {
00161     // forward key event to container
00162     return iAppContainer->OfferKeyEventL( aKeyEvent, aType );
00163 }
00164 
00165 // -----------------------------------------------------------------------------
00166 // Return modifiable descriptor pointer to our internal buffer so that examples
00167 // can write their results to it.
00168 // -----------------------------------------------------------------------------
00169 TPtr CDescriptorExAppUi::GetViewBuffer()
00170 {
00171     return iBuffer->Des();
00172 }
00173 
00174 // -----------------------------------------------------------------------------
00175 // Update our edvin control with data in our internal buffer. If unicode
00176 // build, replace any ascii '\n' character with unicode variant since edwin
00177 // understands only unicode paragraphs when unicode build.
00178 // -----------------------------------------------------------------------------
00179 void CDescriptorExAppUi::UpdateView()
00180     {
00181     // replace any '\n' characters from buffer with UCS paragraph separator if
00182     // unicode build.
00183 #ifdef _UNICODE
00184     TText16* ptr=(TText16*)iBuffer->Ptr();
00185     TText16* endPtr=ptr + iBuffer->Length();
00186     for(;ptr < endPtr; ptr++ )
00187     {
00188         if( *ptr == '\n' )
00189         {
00190             *ptr = 0x2029; // UCS_PARASEP = 0x2029
00191         }
00192     }
00193 #endif
00194     // MResultViewer declares this method as non leaving (no suffix L). If
00195     // text editor can't set the text, we ignore the error.
00196     TRAPD(ignored, iAppContainer->SetTextL(*iBuffer));
00197     }
00198 
00199 void CDescriptorExAppUi::HandleResourceChangeL(TInt aType)
00200     {
00201     CAknAppUi::HandleResourceChangeL(aType);
00202 
00203     if ( aType == KEikDynamicLayoutVariantSwitch )
00204         {
00205         TRect rect;
00206         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00207         iAppContainer->SetRect(rect);
00208         }
00209     }

Generated by  doxygen 1.6.2