#include <eiklabel.h>
#include <aknlists.h>
#include "FCAPaletteAppView.h"
#include "FCAPalette.pan"

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

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

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

void CFCAPaletteAppView::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);
    }

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


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

    delete iListBox;
    iListBox = NULL;

    delete iOutputText;
    iOutputText = NULL;
    }

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

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

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

void CFCAPaletteAppView::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();
    iOutputText->Des().Zero();
    }


void CFCAPaletteAppView::PrintLine2L(TBuf<256> textbuf)
    {

    iOutputText->Des().Append(_L("\t"));
    // if mlen is greater than the length of the string
    // then Left returns the whole of the descriptor.
    iOutputText->Des().Append(textbuf);

    iMessageList->AppendL(*iOutputText);
    iListBox->HandleItemAdditionL();
    iOutputText->Des().Zero();

    }




