www.pudn.com > sockets.rar > socketsappview.cpp


/* Copyright (c) 2004, Nokia. All rights reserved */ 
 
 
// INCLUDE FILES 
#include  
#include  
#include   // for resource reader 
#include  
#include  
#include  
 
#include "SocketsAppView.h" 
#include "Sockets.pan" 
 
// ========================= MEMBER FUNCTIONS ================================== 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::NewL() 
// Two-phased constructor. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppView* CSocketsAppView::NewL( const TRect& aRect ) 
    { 
    CSocketsAppView* self = CSocketsAppView::NewLC( aRect ); 
    CleanupStack::Pop( self ); 
    return self; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::NewLC() 
// Two-phased constructor. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppView* CSocketsAppView::NewLC( const TRect& aRect ) 
    { 
    CSocketsAppView* self = new ( ELeave ) CSocketsAppView; 
    CleanupStack::PushL( self ); 
    self->ConstructL( aRect ); 
    return self; 
    } 
 
// ----------------------------------------------------------------------------- 
// CCSAsyncAppView::ConstructL() 
// Symbian 2nd phase constructor can leave. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::ConstructL( const TRect& aRect ) 
    { 
 
    CreateWindowL(); 
 
    iStatusWindow = new ( ELeave ) CEikLabel; 
    iStatusWindow->SetContainerWindowL( *this ); 
 
    HBufC* uninitialised = StringLoader::LoadLC( R_SOCK_UNINITIALISED ); 
    iStatusWindow->SetTextL( *uninitialised ); 
    CleanupStack::PopAndDestroy(); // uninitialised 
 
    iStatusWindow->SetExtent( TPoint( KLabelPositionX, KLabelPositionY ), 
                              TSize( KLabelWidth, KLabelHeight ) ); 
 
    TResourceReader reader; 
    iCoeEnv->CreateResourceReaderLC( reader, R_OUTPUT_VIEW_RTEXTED ); 
    iOutputWindow = new ( ELeave ) CEikRichTextEditor(); 
 
    iOutputWindow->SetContainerWindowL( *this ); 
    iOutputWindow->ConstructFromResourceL( reader ); 
    CleanupStack::PopAndDestroy();  // Resource reader 
    iOutputWindow->SetFocus( ETrue ); 
 
    iOutputWindow->SetExtent( TPoint( KOutputPositionX, KOutputPositionY ), 
                              TSize( KOutputWidth, KOutputHeight ) ); 
 
    SetRect( aRect ); 
    ActivateL(); 
 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::CSocketsAppView() 
// C++ default constructor can NOT contain any code, that might leave. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppView::CSocketsAppView() 
    { 
    // No implementation required 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::~CSocketsAppView() 
// Destructor. 
// ----------------------------------------------------------------------------- 
// 
CSocketsAppView::~CSocketsAppView() 
    { 
    delete iOutputWindow; 
    iOutputWindow = NULL; 
 
    delete iStatusWindow; 
    iStatusWindow = NULL; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::ClearTextL() 
// Clear contents of editor window. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::ClearTextL() 
    { 
    iOutputWindow->Text()->Reset(); 
    iOutputWindow->HandleTextChangedL(); 
    iOutputWindow->SetCursorPosL( 0, EFalse ); 
    iFontStyle = 0; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::Draw() 
// Draws this CSocketsAppView to the screen. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::Draw( const TRect& aRect ) const 
    { 
    // Clear the screen 
    CWindowGc& gc = SystemGc(); 
    gc.Clear( aRect ); 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::CountComponentControls() 
// Determines number of component controls. 
// ----------------------------------------------------------------------------- 
// 
TInt CSocketsAppView::CountComponentControls() const 
    { 
    return 2; 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::ComponentControl() 
// Gets handle to a component control. 
// ----------------------------------------------------------------------------- 
// 
CCoeControl* CSocketsAppView::ComponentControl( TInt aIndex ) const 
    { 
 
    switch( aIndex ) 
        { 
            case 0: 
                return iStatusWindow; 
            case 1: 
                return iOutputWindow; 
 
            default: 
                User::Panic ( KPanicSockets, ESocketsAppView ); 
                return 0; 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::KeyEventL() 
// Handles a key event. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::KeyEventL( TInt aCode ) 
    { 
    // Add this 'keypress' to the main output window 
    TKeyEvent event; 
    event.iCode = aCode; 
    event.iScanCode = aCode; 
    iOutputWindow->SetFocus( ETrue ); 
    iOutputWindow->OfferKeyEventL( event, EEventKey ); 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::PrintNotify() 
// Displays text on console. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::PrintNotify( const TDesC& aDes, TUint aFontStyle ) 
    { 
    // Display these characters in the main output window, 
    // in the specified style 
    SetFontStyle( aFontStyle ); 
 
    for ( TInt i = 0; i < aDes.Length(); i++ ) 
        { 
        TRAPD( error, KeyEventL( aDes[i] ) ); 
        if( error != KErrNone ) 
            { 
            User::Panic ( KPanicSockets, ESocketsAppView ); 
            } 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::PrintNotify() 
// Displays text on console. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::PrintNotify( const TDesC8& aDes, TUint aFontStyle ) 
    { 
    // Display these characters in the main output window, 
    // in the specified style 
    SetFontStyle( aFontStyle ); 
 
    for ( TInt i = 0; i < aDes.Length(); i++ ) 
        { 
        TRAPD( error, KeyEventL( aDes[i] ) ); 
        if( error != KErrNone ) 
            { 
            User::Panic ( KPanicSockets, ESocketsAppView ); 
            } 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::SetFontStyle() 
// Changes font style for subsequent text. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::SetFontStyle( TUint aFontStyle ) 
    { 
    // Set current main window style to match that requested 
    UpdateFontAttribute( aFontStyle, CEikGlobalTextEditor::EBold ); 
    UpdateFontAttribute( aFontStyle, CEikGlobalTextEditor::EItalic ); 
    UpdateFontAttribute( aFontStyle, CEikGlobalTextEditor::EUnderline ); 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::UpdateFontAttribute() 
// Set or clear an individual font attribute. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::UpdateFontAttribute( TUint aFontStyle, TInt aAttribute ) 
    { 
    // Ensure an individual attribute is on or off as requested 
    if ( ( aFontStyle & aAttribute ) != ( iFontStyle & aAttribute ) ) 
        { 
        // Note: This function 'toggles' styles based on the bits set in 
        //       supplied mask, i.e. supplying the same bit twice toggles 
        //       that style on then off again 
        TRAPD( error, iOutputWindow->BoldItalicUnderlineEventL( aAttribute ) ); 
        if( error != KErrNone ) 
            { 
            User::Panic ( KPanicSockets, ESocketsAppView ); 
            } 
        iFontStyle ^= aAttribute; 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::ErrorNotify() 
// Notifies user of an error. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::ErrorNotify( const TDesC& aErrMessage, TInt aErrCode ) 
    { 
    _LIT( KNewLine, "\n" ); 
 
    TRAPD( error, 
 
    HBufC* errorTitleCode = HBufC::NewLC( 50 ); 
    errorTitleCode->Des().Append( aErrMessage ); 
    errorTitleCode->Des().Append( KNewLine ); 
    errorTitleCode->Des().AppendNum( aErrCode ); 
 
    CAknGlobalNote* globalNote = CAknGlobalNote::NewLC(); 
    globalNote->ShowNoteL( EAknGlobalErrorNote, *errorTitleCode ); 
    CleanupStack::PopAndDestroy( globalNote ); 
    CleanupStack::PopAndDestroy( errorTitleCode ); 
   ) 
 
    if( error != KErrNone ) 
        { 
        User::Panic ( KPanicSockets, ESocketsAppView ); 
        } 
    } 
 
// ----------------------------------------------------------------------------- 
// CSocketsAppView::SetStatus() 
// Changes 'status' display. 
// ----------------------------------------------------------------------------- 
// 
void CSocketsAppView::SetStatus( const TDesC& aStatus ) 
    { 
    // Update contents of status window 
    TRAPD( error, iStatusWindow->SetTextL( aStatus ) ); 
    if( error != KErrNone ) 
        { 
        User::Panic ( KPanicSockets, ESocketsAppView ); 
        } 
 
    iStatusWindow->DrawDeferred(); 
    } 
 
// End of File