examples/ForumNokia/DescriptorExample/src/DescriptorExAppUi.cpp

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

Generated by  doxygen 1.6.2