📄 resman.cpp
字号:
// resman.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 1034634 $
//
//////////////////////////////////////////////////////////////////////
// //
// TRAP AND CLEAN-UP //
// //
//////////////////////////////////////////////////////////////////////
//
// Objective:
//
// Test the creation and deletion of objects on the heap
// first without the alloc failure activated then
// with alloc failure set to deterministic and frequency at 2.
//
// Use ctrl+alt+shift A to show number of cells allocated.
// Use ctrl+alt+shift P to show dialog for heap falure tool.
// Use ctrl+alt+shift Q to turn off heap failure mode.
//
//////////////////////////////////////////////////////////////////////
// SYSTEM HEADERS
#include <e32keys.h>
#include <coemain.h>
#include <coeutils.h>
#include <eikstart.h>
#include <eikenv.h>
#include <eikcore.rsg>
#include <resman.rsg>
// PROJECT HEADERS
#include "resman.hrh"
#include "resman.h"
#include "resobj.h"
// class CResModel
void CResModel::ConstructL()
{
iMood = EHappy;
iEyesBig = EFalse;
}
// class CResControl
CResControl::CResControl(CResDocument* aDoc)
: iDoc(aDoc)
{
}
void CResControl::ConstructL(const CCoeControl* aParent, const TRect& aRect)
{
// create window - a lodger control
SetContainerWindowL(*aParent);
SetRect(aRect);
// go for it
ActivateL();
}
CResControl::~CResControl()
{
}
void CResControl::Draw(const TRect& /* aRect */) const
{
CWindowGc& gc = SystemGc();
TRect faceRect = Rect();
TInt faceHeight = faceRect.Height();
TInt faceWidth = faceRect.Width();
gc.DrawEllipse(faceRect);
// eyes
TRect leftEyeRect;
TRect rightEyeRect;
TInt eyeY1 = faceRect.iTl.iY + faceHeight / 4;
TInt eyeY2 = eyeY1 + faceHeight / 5;
TInt eyeWidth = faceWidth / 4;
TInt eyeFromMiddle = faceWidth / 10;
TInt faceMiddleX = faceRect.iTl.iX + faceWidth / 2;
leftEyeRect = TRect(TPoint(faceMiddleX - eyeFromMiddle - eyeWidth, eyeY1),
TPoint(faceMiddleX - eyeFromMiddle, eyeY2));
rightEyeRect = TRect(TPoint(faceMiddleX + eyeFromMiddle, eyeY1),
TPoint(faceMiddleX + eyeFromMiddle + eyeWidth, eyeY2));
// optional pupils
if (!iDoc->iModel->iEyesBig)
{
leftEyeRect.Shrink(eyeWidth / 4, eyeWidth / 4);
rightEyeRect.Shrink(eyeWidth / 4, eyeWidth / 4);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.SetBrushColor(KRgbBlack);
gc.DrawEllipse(leftEyeRect);
gc.DrawEllipse(rightEyeRect);
gc.SetBrushStyle(CGraphicsContext::ENullBrush);
}
else
{
gc.DrawEllipse(leftEyeRect);
gc.DrawEllipse(rightEyeRect);
}
// mouth
TRect mouthRect;
TInt mouthY1 = faceRect.iTl.iY + faceHeight / 2;
TInt mouthHalfWidth = faceWidth / 3;
TInt mouthY2 = mouthY1 + faceHeight / 3;
TInt mouthHalfHeight = (mouthY2 - mouthY1) / 2;
mouthRect = TRect(TPoint(faceMiddleX - mouthHalfWidth, mouthY1),
TPoint(faceMiddleX + mouthHalfWidth, mouthY2));
switch (iDoc->iModel->iMood)
{
case CResModel::EHappy:
gc.DrawArc(mouthRect,
TPoint(mouthRect.iTl.iX, mouthRect.iBr.iY - mouthHalfHeight),
TPoint(mouthRect.iBr.iX, mouthRect.iBr.iY - mouthHalfHeight));
break;
case CResModel::ESad:
mouthRect.Move(0, mouthHalfHeight / 2);
gc.DrawArc(mouthRect,
TPoint(mouthRect.iBr.iX, mouthRect.iBr.iY - mouthHalfHeight),
TPoint(mouthRect.iTl.iX, mouthRect.iBr.iY - mouthHalfHeight));
break;
case CResModel::ENeutral:
gc.DrawLine(TPoint(faceMiddleX - mouthHalfWidth, mouthY1 + mouthHalfHeight),
TPoint(faceMiddleX + mouthHalfWidth, mouthY1 + mouthHalfHeight));
break;
default: ;
gc.DrawRect(mouthRect);
}
}
// class CResAppView
void CResAppView::ConstructL(const TRect& aRect, CResDocument* aDoc)
{
iDoc = aDoc;
CreateWindowL();
Window().SetShadowDisabled(ETrue);
iContext = this;
iBrushStyle = CGraphicsContext::ESolidBrush;
iBrushColor = KRgbWhite;
SetRect(aRect);
ConstructViewL();
ActivateL();
}
CResAppView::~CResAppView()
{
delete iControl;
}
TInt CResAppView::CountComponentControls() const
{
return 1;
}
CCoeControl* CResAppView::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0: return iControl;
default: return 0;
};
}
void CResAppView::ConstructViewL()
{
// make control
TSize viewSize(100, 100);
TInt x = (Rect().Width() - viewSize.iWidth) / 2;
TInt y = (Rect().Height() - viewSize.iHeight) / 2;
TRect controlRect(TPoint(x, y), viewSize);
iControl = new(ELeave) CResControl(iDoc);
iControl->ConstructL(this, controlRect);
}
void CResAppView::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.SetPenStyle(CGraphicsContext::ENullPen);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.DrawRect(Rect());
}
// handle commands
// CResAppUi
void CResAppUi::ConstructL()
{
BaseConstructL();
iDoc = ((CResDocument*)iDocument);
iAppView = new(ELeave) CResAppView;
iAppView->ConstructL(ClientRect(),iDoc);
AddToStackL(iAppView); // app view should go onto control stack
}
CResAppUi::~CResAppUi()
{
if (iAppView)
{
RemoveFromStack(iAppView);
delete iAppView;
}
}
void CResAppUi::HandleCommandL(TInt aCommand)
{
iAppView->DrawDeferred(); // Draw face & cell count when I'm done
switch (aCommand)
{
// misc commands
case EResCmd1:
Cmd1PressedL();
break;
case EResCmd2:
Cmd2PressedL();
break;
case EResCmd3:
Cmd3PressedL();
break;
case EResCmd4:
Cmd4PressedL();
break;
// file/app
case EEikCmdExit:
Exit();
break;
}
}
// command handling
void CResAppUi::Cmd1PressedL()
{
// This method, which is called when you click on the "Leak a cell" command button,
// creates two CMySimpleClass objects.
// The first object created is deleted - but the second object is not.
// This method therefore deliberately leaks a heap cell every time it is run.
// Practice using the Uikon debug keys and observe number of allocated heap cells
// increases by 1 every time this method is called.
// Leaking a cell - so indicate unhappiness to the user!
//
iDoc->iModel->iMood = CResModel::ESad;
iDoc->iModel->iEyesBig = EFalse;
// iEikonEnv->InfoMsg(R_RES_COMMAND_1);
// Pointers are maintained by member variables so
// if Out Of Memory occurs, simple objects will be correctly deleted
iLeak = new(ELeave) CMySimpleClass;
//do something that might leave here
delete iLeak;
iLeak = 0; // Important to zero the pointer before re-use, in case next alloc fails!
iLeak = new(ELeave) CMySimpleClass;
}
void CResAppUi::Cmd2PressedL()
{
// Construction OK
iDoc->iModel->iMood = CResModel::EHappy;
iDoc->iModel->iEyesBig = EFalse;
// iEikonEnv->InfoMsg(R_RES_COMMAND_2);
// 2 simple objects on and off the heap
// with C++ type construction
iSimple = new(ELeave) CMySimpleClass;
// do something that might leave here
delete iSimple;
iSimple = 0; // Important to zero the pointer before re-use, in case next alloc fails!
iSimple = new(ELeave) CMySimpleClass;
//do something that might leave here
delete iSimple;
iSimple = 0; //Important to zero the pointer before re-use, in case next alloc fails!
}
void CResAppUi::Cmd3PressedL()
{
// A compound object on and off the heap
// with C++ type construction.
// A second simple object is created on the heap,
// by the compound object.
//
// If OOM occurs during construction of the simple object
// construction of the compound object will fail.
// If this was standard C++ construction, a NULL pointer would
// be returned.
// As we are using the CBase-supplied new operator,
// we will leave at the point of Out Of Memory and control will pass
// to the nearest TRAP macro. (Provided by the WINS CONE environment in this case.)
// First off indicate not perfect construction
iDoc->iModel->iMood = CResModel::ENeutral;
iDoc->iModel->iEyesBig = EFalse;
// iEikonEnv->InfoMsg(R_RES_COMMAND_3);
iCompound = new(ELeave) CMyCompoundClass;
//do something that might leave here
delete iCompound;
iCompound = 0; // Important to zero the pointer before re-use, in case next alloc fails!
}
void CResAppUi::Cmd4PressedL()
{
// Create a compound object (CMyCompoundSymbianOSClass) on the heap and then delete it.
// Using 2-phase construction & the cleanup stack
// if OOM occurs during construction of the simple object,
// construction of the compound object will fail.
// But a safe pointer to the partially constructed
// compound object exists on the cleanup stack
// and it will be safely deleted as the stack is unwound on hitting the
// TRAP macro.
// Because CResAppUi is ultimately derived from CBase,
// i2Phase will be NULL on creation of the CResAppUi object.
// If Out Of Memory occurs while creating our CMyCompoundSymbianOSClass object
// we do not need to worry about whether we can delete i2Phase or not,
// because it is already NULL, and deleting NULL has no effect.
// Indicate perfection
iDoc->iModel->iMood = CResModel::EHappy;
iDoc->iModel->iEyesBig = EFalse;
// iEikonEnv->InfoMsg(R_RES_COMMAND_4);
i2Phase = CMyCompoundSymbianOSClass::NewL();
delete i2Phase;
i2Phase = 0; // Important to zero the pointer before re-use, in case next alloc fails!
}
// file handling
TBool CResAppUi::ProcessCommandParametersL(TApaCommand /* aCommand */,
TFileName& /* aDocumentName */,
const TDesC& /* aTail */)
{
return EFalse; // file not found
}
// CResDocument
CResDocument::CResDocument(CEikApplication& aApp)
: CEikDocument(aApp)
{
}
CResDocument* CResDocument::NewL(CEikApplication& aApp)
{
CResDocument* self = new(ELeave) CResDocument(aApp);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CResDocument::ConstructL()
{
iModel = new(ELeave) CResModel;
iModel->ConstructL();
}
CFileStore* CResDocument::OpenFileL(TBool /* aDoOpen */,
const TDesC& /* aFilename */,
RFs& /* aFs */)
{
Process()->SetMainDocFileName(TPtrC());
CEikonEnv::Static()->UpdateTaskNameL();
return 0; // but we don't want to store it in a file
}
CResDocument::~CResDocument()
{
delete iModel;
}
CEikAppUi* CResDocument::CreateAppUiL()
{
return new(ELeave) CResAppUi;
}
// CResApplication
const TUid KUidResApp = { 0x10005037 };
TUid CResApplication::AppDllUid() const
{
return KUidResApp;
}
CApaDocument* CResApplication::CreateDocumentL()
{
return CResDocument::NewL(*this);
}
// EXPORTed functions
LOCAL_C CApaApplication* NewApplication()
{
return new CResApplication;
}
GLDEF_C TInt E32Main()
{
return EikStart::RunApplication(NewApplication);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -