S60 3rd Edition SDK for Symbian OS
Example Applications Guide

ChatContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : ChatContainer.cpp
00004 *  Part of     : Chat
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2005-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include "ChatContainer.h"
00018 #include "Chat.pan"
00019 #include "Chat.hrh"
00020 #include "ChatProtocolConstants.h"
00021 
00022 // ============================ MEMBER FUNCTIONS ==============================
00023 
00024 // ----------------------------------------------------------------------------
00025 // CChatContainer::NewL()
00026 // Two-phased constructor.
00027 // ----------------------------------------------------------------------------
00028 //
00029 CChatContainer* CChatContainer::NewL( const TRect& aRect )
00030     {
00031     CChatContainer* self = CChatContainer::NewLC( aRect );
00032     CleanupStack::Pop( self );
00033     return self;
00034     }
00035 
00036 // ----------------------------------------------------------------------------
00037 // CChatContainer::NewLC()
00038 // Two-phased constructor.
00039 // ----------------------------------------------------------------------------
00040 //
00041 CChatContainer* CChatContainer::NewLC( const TRect& aRect )
00042     {
00043     CChatContainer* self = new ( ELeave ) CChatContainer;
00044     CleanupStack::PushL( self );
00045     self->ConstructL( aRect );
00046     return self;
00047     }
00048 
00049 // ----------------------------------------------------------------------------
00050 // CChatContainer::ConstructL()
00051 // Symbian 2nd phase constructor can leave.
00052 // ----------------------------------------------------------------------------
00053 //
00054 void CChatContainer::ConstructL( const TRect& aRect )
00055     {
00056     // Create a window for this application view
00057     CreateWindowL();
00058 
00059     // Set the windows size
00060     SetRect( aRect );
00061 
00062     // Create a control to display a list of messages
00063     iListBox = new ( ELeave ) CAknSingleNumberStyleListBox;
00064     iListBox->SetContainerWindowL( *this );
00065     iListBox->ConstructL( this, 0 );
00066 
00067     iListBox->SetRect( aRect.Size() );
00068 
00069     iListBox->ActivateL();
00070     iListBox->CreateScrollBarFrameL( ETrue );
00071     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
00072         CEikScrollBarFrame::EOn, 
00073         CEikScrollBarFrame::EAuto );
00074 
00075     // Create an array to hold the messages
00076     iMessageList = new ( ELeave ) CDesCArrayFlat( KMessageListLen );
00077 
00078     // Give it to the control
00079     CTextListBoxModel* model = iListBox->Model();
00080     model->SetItemTextArray( iMessageList );
00081 
00082     // transfer ownership of iMessageList
00083     model->SetOwnershipType( ELbmOwnsItemArray );
00084 
00085     // Activate the window, which makes it ready to be drawn
00086     ActivateL();
00087     }
00088 
00089 // ----------------------------------------------------------------------------
00090 // CChatContainer::CChatContainer()
00091 // Constructor.
00092 // ----------------------------------------------------------------------------
00093 //
00094 CChatContainer::CChatContainer()
00095     {
00096     // no implementation required
00097     }
00098 
00099 // ----------------------------------------------------------------------------
00100 // CChatContainer::~CChatContainer()
00101 // Destructor.
00102 // ----------------------------------------------------------------------------
00103 //
00104 CChatContainer::~CChatContainer()
00105     {
00106     // iMessageList not deleted as it is owned by iListBox->Model()
00107     delete iListBox;
00108     iListBox = NULL;
00109     }
00110 
00111 // ----------------------------------------------------------------------------
00112 // CChatContainer::ClearMessageListL()
00113 // Clears all the entries in the list view.
00114 // ----------------------------------------------------------------------------
00115 //
00116 void CChatContainer::ClearMessageListL()
00117     {
00118     iMessageList->Reset();
00119 
00120     iListBox->HandleItemRemovalL();
00121     iListBox->Reset();
00122       
00123     // tell the control about the change
00124     iListBox->HandleItemAdditionL();
00125     }
00126 
00127 // ----------------------------------------------------------------------------
00128 // CChatContainer::CountComponentControls()
00129 // Returns the specified component control.
00130 // ----------------------------------------------------------------------------
00131 //
00132 TInt CChatContainer::CountComponentControls() const
00133     {
00134     return 1; // Only have one Component
00135     }
00136 
00137 // ----------------------------------------------------------------------------
00138 // CChatContainer::ComponentControl()
00139 // Gets the specified component of a compound control.
00140 // ----------------------------------------------------------------------------
00141 //
00142 CCoeControl* CChatContainer::ComponentControl( TInt aIndex ) const
00143     {
00144     __ASSERT_ALWAYS( aIndex == 0, Panic( EChatInvalidControlIndex ) );
00145     return iListBox;    //  Return the component
00146     }
00147 
00148 // ----------------------------------------------------------------------------
00149 // CChatContainer::OfferKeyEventL()
00150 // Offer the key event to the list box.
00151 // ----------------------------------------------------------------------------
00152 //
00153 TKeyResponse CChatContainer
00154 ::OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
00155     {
00156     return iListBox->OfferKeyEventL( aKeyEvent, aType );
00157     }
00158 
00159 // ----------------------------------------------------------------------------
00160 // CChatContainer::LogL()
00161 // Add an entry to the log.
00162 // ----------------------------------------------------------------------------
00163 //
00164 void CChatContainer::LogL( const TDesC& aText )
00165     {
00166     LogL( aText, KNullDesC );
00167     }
00168 
00169 // ----------------------------------------------------------------------------
00170 // CChatContainer::LogL()
00171 // A number to append onto the entry.
00172 // ----------------------------------------------------------------------------
00173 //
00174 void CChatContainer::LogL( const TDesC& aText, TInt aNumber )
00175     {
00176     TBuf<KMessageHeaderLen> numberString;
00177     HBufC* buffer = HBufC::NewLC( KMessageHeaderLen + aText.Length() +
00178         numberString.Length() );
00179     numberString.Num( aNumber );
00180     buffer->Des().Append( aText );
00181     buffer->Des().Append( ' ' );
00182     buffer->Des().Append( numberString );
00183     LogL( buffer->Des(), KNullDesC );
00184     CleanupStack::PopAndDestroy( buffer );
00185     }
00186 
00187 // ----------------------------------------------------------------------------
00188 // CChatContainer::LogL()
00189 // Extra text to append onto the entry.
00190 // ----------------------------------------------------------------------------
00191 //
00192 void CChatContainer::LogL( const TDesC& aText,
00193     const TDesC& aExtraText )
00194     {
00195     HBufC* buffer = HBufC::NewLC( KMessageHeaderLen + aText.Length() +
00196         aExtraText.Length() );
00197 
00198     buffer->Des().Append( aExtraText );
00199     buffer->Des().Append( '\t' );
00200     buffer->Des().Append( aText );
00201     
00202     // add the message to the list
00203     iMessageList->AppendL( *buffer );
00204     CleanupStack::PopAndDestroy( buffer );
00205 
00206     // tell the control about the change
00207     iListBox->HandleItemAdditionL();
00208     }
00209 
00210 // ----------------------------------------------------------------------------
00211 // CChatContainer::ContainsEntries()
00212 // Does the view contain any log entries.
00213 // ----------------------------------------------------------------------------
00214 //
00215 TBool CChatContainer::ContainsEntries()
00216     {
00217     return iListBox->Model()->NumberOfItems() != 0;
00218     }
00219 
00220 
00221 // ----------------------------------------------------------------------------
00222 // CChatContainer::ShowDataQueryL()
00223 // Display of Data Query.
00224 // ----------------------------------------------------------------------------
00225 //
00226 void CChatContainer::ShowDataQueryL(
00227     const TInt aQueryResourceId,
00228     const TInt aTextResourceId,
00229     const TInt aPromptResoureId,
00230     const TInt aMaxLength,
00231     const TBool aDisplayFlag,
00232     TDes& aText )
00233     {
00234     
00235     iDisplayDialog = ETrue;
00236 
00237     TBuf<KChatTextBufLength> textData;
00238     
00239     if ( aTextResourceId )
00240         {
00241         iCoeEnv->ReadResourceL( textData, aTextResourceId );
00242         }
00243     
00244     CAknTextQueryDialog* dlg = CAknTextQueryDialog::NewL(
00245         textData, CAknQueryDialog::ENoTone );
00246     CleanupStack::PushL( dlg );
00247          
00248     if ( aPromptResoureId )
00249         {
00250         TBuf<KChatTextBufLength> prompt;
00251         iCoeEnv->ReadResourceL( prompt, aTextResourceId );
00252         dlg->SetPromptL( prompt );
00253         }
00254 
00255     if ( aMaxLength )
00256         {
00257         dlg->SetMaxLength( aMaxLength );
00258         }
00259         
00260           CleanupStack::Pop( dlg );
00261           TBool answer( dlg->ExecuteLD( aQueryResourceId ) );
00262     
00263     if ( aDisplayFlag && answer )
00264         {
00265         HBufC* buffer = HBufC::NewLC( KMessageHeaderLen + 
00266                                   textData.Length() );
00267 
00268         buffer->Des().Append( '<' );
00269         buffer->Des().Append( '\t' );
00270         buffer->Des().Append( textData );
00271         
00272         // add the message to the list
00273         iMessageList->AppendL( *buffer );
00274         CleanupStack::PopAndDestroy( buffer );
00275       
00276         // tell the control about the change
00277         iListBox->HandleItemAdditionL();
00278         }
00279 
00280     iDisplayDialog = EFalse;
00281   
00282     // get message
00283     aText = textData;
00284     
00285     DrawNow();
00286     }
00287  
00288 
00289 // -----------------------------------------------------------------------------
00290 // CChatContainer::SizeChanged()
00291 // Called by framework when the view size is changed.
00292 // -----------------------------------------------------------------------------
00293 //
00294 void CChatContainer::SizeChanged()
00295     { 
00296     if ( iListBox ) 
00297         { 
00298         iListBox-> SetRect( iAvkonAppUi->ApplicationRect() ); 
00299         } 
00300     }
00301     
00302 // End of File  

© Nokia 2006

Back to top