// OandXView.cpp
//
// � Symbian Software Ltd 2004 - 2006. All rights reserved.
//
// void SetAvailable(const CCoeControl &aClient, TInt aCommandId, TBool aIsAvailable, TInt aUiConfigMode=KUiConfigMaskAll);
#include <QikCommand.h>
#include <OandX.rsg>
#include <eiklabel.h>
#include <devicekeys.h>
#include "OandX.pan"
#include "OandX.hrh"
#include "OandXAppUi.h"
#include "OandXApplication.h"
#include "OandXAppView.h"
// O and X symbol-drawing control member functions
void COandXSymbolControl::DrawSymbol(CWindowGc& aGc, TRect& aRect, TBool aDrawCross) const
{
TSize size;
size.SetSize(aRect.iBr.iX- aRect.iTl.iX, aRect.iBr.iY - aRect.iTl.iY);
aRect.Shrink(size.iWidth/6,size.iHeight/6); // Shrink by about 15%
aGc.SetPenStyle(CGraphicsContext::ESolidPen);
size.iWidth /= 9; // Pen size set to just over 10% of the shape's size
size.iHeight /= 9;
aGc.SetPenSize(size);
if (aDrawCross)
{
aGc.SetPenColor(KRgbGreen);
aRect.Shrink(size.iWidth/2,size.iHeight/2); // Cosmetic reduction of cross size by half the line width
aGc.DrawLine(aRect.iTl, aRect.iBr);
TInt temp;
temp = aRect.iTl.iX;
aRect.iTl.iX = aRect.iBr.iX;
aRect.iBr.iX = temp;
aGc.DrawLine(aRect.iTl, aRect.iBr);
}
else // draw a circle
{
aGc.SetPenColor(KRgbRed);
aGc.SetBrushStyle(CGraphicsContext::ESolidBrush);
aGc.DrawEllipse(aRect);
}
};
// Tile member functions
COandXTile::COandXTile()
{
}
COandXTile::~COandXTile()
{
}
void COandXTile::ConstructL(COandXView* aOwningView)
{
iAppView =aOwningView;
}
void COandXTile::Draw(const TRect& /*aRect*/) const
{
TInt tileType;
tileType = iAppView->SquareStatus(this);
CWindowGc& gc = SystemGc();
TRect rect = Rect();
if (IsFocused())
{
gc.SetBrushColor(KRgbYellow);
}
gc.Clear(rect);
if (tileType!=ETileBlank)
{
DrawSymbol(gc, rect, tileType==ETileCross);
}
}
void COandXTile::TryHitL()
{
iAppView->TryHitSquareL(this);
}
TKeyResponse COandXTile::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
TKeyResponse keyResponse = EKeyWasNotConsumed;
if (aType!=EEventKey)
{
return keyResponse;
}
switch (aKeyEvent.iCode)
{
case EDeviceKeyAction:
TryHitL();
keyResponse = EKeyWasConsumed;
break;
default:
keyResponse = EKeyWasNotConsumed;
break;
}
return keyResponse;
}
TCoeInputCapabilities COandXTile::InputCapabilities() const
{
return TCoeInputCapabilities::ENavigation;
}
void COandXTile::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
if (aPointerEvent.iType == TPointerEvent::EButton1Down)
{
TryHitL();
}
}
// Status window member functions
COandXStatusWin* COandXStatusWin::NewL(RWindow& aWindow)
{
COandXStatusWin* self=new(ELeave) COandXStatusWin;
CleanupStack::PushL(self);
self->ConstructL(aWindow);
CleanupStack::Pop();
return self;
}
COandXStatusWin::COandXStatusWin()
{
}
COandXStatusWin::~COandXStatusWin()
{
}
void COandXStatusWin::ConstructL(RWindow& aWindow)
{
SetContainerWindowL(aWindow);
MakeVisible(EFalse);
// No call to ActivateL() as the window is activated by its container
}
void COandXStatusWin::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
TRect boxRect = Rect();
gc.Clear(boxRect);
TInt boxHeight = boxRect.iBr.iY - boxRect.iTl.iY;
boxRect.iTl.iX = boxRect.iBr.iX - boxHeight;
DrawSymbol(gc, boxRect, Controller().IsCrossTurn());
}
// View member functions
#define KBorderWidth 10
#define KLineWidth ((KTilesPerRow > KTilesPerCol ? KTilesPerRow : KTilesPerCol) > 4 ? 2 : 4)
/**
Creates and constructs the view.
@param aAppUi Reference to the AppUi
@return Pointer to a COandXView object
*/
COandXView* COandXView::NewL(CQikAppUi& aAppUi)
{
COandXView* self = new(ELeave) COandXView(aAppUi);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}
/**
Constructor for the view.
Passes the application UI reference to the construction of the super class.
KNullViewId should normally be passed as parent view for the applications
default view. The parent view is the logical view that is normally activated
when a go back command is issued. KNullViewId will activate the system
default view.
@param aAppUi Reference to the application UI
*/
COandXView::COandXView(CQikAppUi& aAppUi)
: CQikViewBase(aAppUi, KNullViewId), iCommandManager(CQikCommandManager::Static())
{
}
/**
Destructor for the view
*/
COandXView::~COandXView()
{
if (iStatWinNotAppended) // Once appended, system code takes care of a component's deletion
{
delete iStatusWin;
}
}
void COandXView::ConstructL()
{
// Calls ConstructL that initialises the standard values.
// This should always be called in the concrete view implementations.
BaseConstructL();
iStatusWin = COandXStatusWin::NewL(Window()); // pointer stored in member data solely for convenience
iStatWinNotAppended = ETrue; // Status window not yet owned as a component
}
/**
Inherited from CQikViewBase and called upon by the UI Framework.
It creates the view from a resource and constructs the control's
component tiles.
*/
void COandXView::ViewConstructL()
{
// Loads information about the UI configurations this view supports
// together with definition of each view.
ViewConstructFromResourceL(R_OANDX_UI_CONFIGURATIONS);
InitComponentArrayL();
for (TInt i = 0; i < KNumberOfTiles; i++)
{
COandXTile * tile = new(ELeave) COandXTile;
AddControlLC(tile,i);
tile->ConstructL(this);
tile->SetFocusing(ETrue);
CleanupStack::Pop(tile);
iTileControls[i] = tile; // store here for easy access
tile->MakeVisible(EFalse);
}
// Status window not created here
CCoeControl::Components().AppendLC(iStatusWin,KNumberOfTiles);
CleanupStack::Pop(); // statusWin
iStatWinNotAppended = EFalse; // Status window now owned as a component
SetFocusByIdL(0);
}
/**
Returns the view Id
@return Returns the Uid of the view
*/
TVwsViewId COandXView::ViewId()const
{
return TVwsViewId(KUidOandXApp, KUidOandXView);
}
void COandXView::SizeChanged()
{
// __ASSERT_DEBUG(iTiles[KNumberOfTiles-1], Panic(EOandXNoTiles)); // all component tiles must already exist
TRect rect = Rect();
rect.iTl.iY = rect.iBr.iY - KStatusWinHeight;
iStatusWin->SetRect(rect);
rect = Rect();
rect.iBr.iY -= KStatusWinHeight;
TSize controlSize = rect.Size();
TSize tileSize;
tileSize.iWidth=2*((controlSize.iWidth-2*KBorderWidth-(KTilesPerRow-1)*KLineWidth)/(2*KTilesPerRow));
tileSize.iHeight=2*((controlSize.iHeight-2*KBorderWidth-(KTilesPerCol-1)*KLineWidth)/(2*KTilesPerCol));
iTileSide = tileSize.iWidth < tileSize.iHeight ? tileSize.iWidth :tileSize.iHeight;
TSize boardSize;
boardSize.iWidth = KTilesPerRow*iTileSide + (KTilesPerRow-1)*KLineWidth;
boardSize.iHeight = KTilesPerCol*iTileSide + (KTilesPerCol-1)*KLineWidth;
iBoardRect.iTl.iX = (controlSize.iWidth - boardSize.iWidth)/2;
iBoardRect.iTl.iY = (controlSize.iHeight - boardSize.iHeight)/2;
iBoardRect.iBr.iX = iBoardRect.iTl.iX + boardSize.iWidth;
iBoardRect.iBr.iY = iBoardRect.iTl.iY + boardSize.iHeight;
iBorderRect = iBoardRect;
iBorderRect.Grow(KBorderWidth,KBorderWidth);
for (TInt