#include <eiklabel.h>
#include <aknlists.h>
#include "FCABackupAppView.h"
#include "FCABackup.pan"

const TInt KMaxLineLength = 40;
const TInt KGranularityOfArray = 10;

// Standard construction sequence
CFCABackupAppView* CFCABackupAppView::NewL(const TRect& aRect)
    {
    CFCABackupAppView* self = CFCABackupAppView::NewLC(aRect);
    CleanupStack::Pop(self);
    return self;
    }

CFCABackupAppView* CFCABackupAppView::NewLC(const TRect& aRect)
    {
    CFCABackupAppView* self = new (ELeave) CFCABackupAppView;
    CleanupStack::PushL(self);
    self->ConstructL(aRect);
    return self;
    }

void CFCABackupAppView::ConstructL(const TRect& aRect)
    {
    iOutputText = HBufC::NewL(KMaxLineLength);

    CreateWindowL();
    SetRect(aRect);

    iMsgIndex = 0;
    
    iListBox = new (ELeave) CAknSingleStyleListBox;
    iListBox->SetContainerWindowL(*this);
    iListBox->ConstructL(this, 0);
    
    iListBox->SetRect(aRect.Size());

    iListBox->ActivateL();
    iListBox->CreateScrollBarFrameL(ETrue);
    iListBox->ScrollBarFrame()
            ->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto);
    iListBox->SetDimmed(ETrue);

    iMessageList = new (ELeave) CDesCArrayFlat(KGranularityOfArray);

    CTextListBoxModel* model = iListBox->Model();
    model->SetItemTextArray(iMessageList);
    model->SetOwnershipType(ELbmOwnsItemArray); // transfer ownership of iMessageList

    ActivateL();
    MakeVisible(ETrue);
    }

CFCABackupAppView::CFCABackupAppView()
    {
    // Add any construction code that can not leave here
    }


CFCABackupAppView::~CFCABackupAppView()
    {
    //delete iMessageList;   //  Owned by iListBox->Model()

    delete iListBox;
    iListBox = NULL;

    delete iOutputText;
    iOutputText = NULL;
    }

TInt CFCABackupAppView::CountComponentControls() const
    {
    return 1; // Only have one Component
    }

CCoeControl* CFCABackupAppView::ComponentControl(TInt aIndex) const
    {
    __ASSERT_DEBUG(aIndex == 0, Panic(EFCABackupInvalidComponentIndex));
    return iListBox;
    }

TKeyResponse CFCABackupAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
    {
    return iListBox->OfferKeyEventL(aKeyEvent, aType);
    }

void CFCABackupAppView::PrintLineL(const TDesC& aText)
    {
    iOutputText->Des().Append(_L("\t"));
    TInt spaceRemaining = KMaxLineLength - iOutputText->Des().Length();
    // if mlen is greater than the length of the string
    // then Left returns the whole of the descriptor.
    iOutputText->Des().Append(aText.Left(spaceRemaining));
    iMessageList->AppendL(*iOutputText);
    iListBox->HandleItemAdditionL();
    iListBox->SetCurrentItemIndexAndDraw(iListBox->BottomItemIndex());
//SetCurrentItemIndexAndDraw/ScrollToMakeItemVisible
    iOutputText->Des().Zero();
    }
