// TCONE1.CPP
//
// Copyright (c) 1997-1999 Symbian Ltd.  All rights reserved.
//

#include <coemain.h>
#include <coecntrl.h>
#include <coeauis.h>
#include <e32keys.h>
#include <basched.h>

//
// class CTestControl
//

class CTestControl : public CCoeControl
    {
public:
    void ConstructL();
    ~CTestControl();
private: // framework
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
    void HandlePointerEventL(const TPointerEvent& aPointerEvent);
    void Draw(const TRect& aRect) const;
	void FocusChanged(TDrawNow aDrawNow);
private: // new functions
    void CreateFontL();
    void DrawBorder() const;
    void DrawMessage() const;
    void DrawMessageNow() const;
private:
    TBuf<40> iMessage;
    CFont* iFont;
    };

void CTestControl::ConstructL()
    {
    CreateWindowL();
    CreateFontL();
    EnableDragEvents();
    SetExtentL(TPoint(20,20),TSize(600,200));
    ActivateL();
    }

void CTestControl::CreateFontL()
    {
    TFontSpec spec(_L("Arial"),240);
    iFont=iCoeEnv->CreateScreenFontL(spec);
    }

CTestControl::~CTestControl()
    {
    iCoeEnv->ReleaseScreenFont(iFont);
    }

void CTestControl::FocusChanged(TDrawNow aDrawNow)
	{
    if (aDrawNow)
        {
        ActivateGc();
        DrawBorder();
        DeactivateGc();
        }
    }

void CTestControl::DrawBorder() const
    {
    CWindowGc& gc=SystemGc();
    TRect rect=Rect();
    gc.DrawRect(rect);
    if (!IsFocused())
        gc.SetPenColor(KRgbWhite);
    rect.Shrink(1,1);
    gc.DrawRect(rect);
    rect.Shrink(1,1);
    gc.DrawRect(rect);
	}

void CTestControl::Draw(const TRect& /*aRect*/) const
    {
	DrawBorder();
    DrawMessage();
    }

void CTestControl::DrawMessage() const
    {
    TRect rect=Rect();
    rect.Shrink(3,3);
    CWindowGc& gc=SystemGc();
    gc.UseFont(iFont);
    TInt ascent=(rect.iBr.iY-rect.iTl.iY-iFont->HeightInPixels())/2 + iFont->AscentInPixels();
    gc.SetPenColor(KRgbBlack);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawText(iMessage,rect,ascent,CGraphicsContext::ECenter);
    }

void CTestControl::DrawMessageNow() const
    {
	ActivateGc();
	DrawMessage();
	DeactivateGc();
    }

void CTestControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
    {
    TInt modifiers=aPointerEvent.iModifiers;
	if (modifiers&EModifierDoubleClick)
    	iMessage.Format(_L("Double click event at (%d,%d)"),aPointerEvent.iPosition.iX,aPointerEvent.iPosition.iY);
	else
    	iMessage.Format(_L("Pointer event %d at (%d,%d)"),aPointerEvent.iType,aPointerEvent.iPosition.iX,aPointerEvent.iPosition.iY);
    DrawMessageNow();
    }

TKeyResponse CTestControl::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
    {
    if (aType!=EEventKey)
	    return(EKeyWasConsumed);
    TInt modifiers=aKeyEvent.iModifiers;
    TInt code=aKeyEvent.iCode;
    iMessage.Format(_L("Key 0x%x, modifier 0x%x"),code,modifiers);
    DrawMessageNow();
    if (code==CTRL('e'))
        {
    	iCoeEnv->Flush(200000);
        CBaActiveScheduler::Exit();
        }
	if ((modifiers&EAllStdModifiers)==(EModifierShift|EModifierCtrl))
        {
        TPoint pos=Position();
        switch (code)
            {
        case EKeyLeftArrow:
            pos.iX--;
            break;
        case EKeyRightArrow:
            pos.iX++;
            break;
        case EKeyUpArrow:
            pos.iY--;
            break;
        case EKeyDownArrow:
            pos.iY++;
            break;
        default:
            goto exit;
            }
        SetPosition(pos);
        }
exit:
    return(EKeyWasConsumed);
    }

//
// Main
//

void ConstructAppL(CCoeEnv* aCoe)
    { // runs inside a TRAP harness
    aCoe->ConstructL();
    CCoeAppUiSimple* appUi=new(ELeave) CCoeAppUiSimple;
    aCoe->SetAppUi(appUi);
	aCoe->WsSession().SetDoubleClick(1000000,4);
    CTestControl* control=new(ELeave) CTestControl;
    appUi->SetRootControl(control);
    control->ConstructL();
    }

#if defined(__WINS__)
EXPORT_C TInt EntryPoint(TAny*)
#else
GLDEF_C TInt E32Main()
#endif
    {
    CCoeEnv* coe=new CCoeEnv;
    TRAPD(err,ConstructAppL(coe));
    if (!err)
        coe->ExecuteD();
    return(err);
    }

#if defined(__WINS__)
GLDEF_C TInt E32Dll(TDllReason)
	{
	return(KErrNone);
	}
#endif
