📄 biaocommcontainer.cpp
字号:
/*
* BiaoCommContainer.cpp
*
* Created on: 2009-4-24
* Author: IBM
*/
#include "BiaoCommContainer.h"
#include "BiaofengBaseControl.h"
#include "Biaofeng.hrh"
//#include "Biaofeng.rss"
//////////////////////////////////////////////////////////////////////////////
//
// -----> CSmileyContainer (implementation)
//
//////////////////////////////////////////////////////////////////////////////
CSmileyContainer::CSmileyContainer()
{}
CSmileyContainer::~CSmileyContainer()
{
// Delete all the contained controls
delete iSmiley1;
delete iSmiley2;
}
// Because CSmileyContainer is a compound control, it needs a
// ConstructL() for when it's created outside a dialog, and a
// ConstructFromResourceL() for when it's created inside a dialog.
void CSmileyContainer::ConstructL(const TRect& aRect)
{
TBool isSmiling=ETrue;
// Create the two CSmileys. Their size and position is
// set in CSmileyContainer::SizeChangedL().
iSmiley1 = new(ELeave) CSmiley(isSmiling);
iSmiley1->SetContainerWindowL(*this);
isSmiling=EFalse;
iSmiley2 = new(ELeave) CSmiley(isSmiling);
iSmiley2->SetContainerWindowL(*this);
iSmiley1->SetFocus(ETrue);
// Set the container as the observer of the two CSmileys. This
// is for handling keyboard focus. When an arrow key is pressed
// or the pointer is clicked on one of the CSmileys, an
// EEventRequestFocus event is sent to the container, and the
// container changes the focus if applicable.
iSmiley1->SetObserver(this);
iSmiley2->SetObserver(this);
// Set the bounding rectangle of this control (this will result in
// a call to SizeChangedL(). The component controls must be
// created before calling this, because SizeChangedL() sets their
// sizes.
SetRect(aRect);
}
// This function is used when the CSmileyContainer is created inside a dialog.
void CSmileyContainer::ConstructFromResourceL(TResourceReader& aReader)
{
// Read the smiley mood from the resource file
TBool isSmiling=(TBool)aReader.ReadInt8();
// Read the width of the smiley container from the resource file.
TInt width=aReader.ReadInt16();
// Set the height of the container to be half its width
TSize containerSize (width, width/2);
iSmiley1 = new(ELeave) CSmiley(isSmiling);
iSmiley1->SetContainerWindowL(*this);
iSmiley2 = new(ELeave) CSmiley(isSmiling);
iSmiley2->SetContainerWindowL(*this);
iSmiley1->SetFocus(ETrue);
iSmiley1->SetObserver(this);
iSmiley2->SetObserver(this);
SetSize(containerSize); //##############
ActivateL();
}
// The following two functions have to be implemented for all compound controls.
TInt CSmileyContainer::CountComponentControls() const
{
return 2;
}
CCoeControl* CSmileyContainer::ComponentControl(TInt aIndex) const
{
if (aIndex==0)
return iSmiley1;
else
return iSmiley2;
}
// This function gets called whenever one of the size-setting functions is called.
// As this is a compound control, this function calculates and sets the size and
// position for its components, based on its own size.
void CSmileyContainer::SizeChanged()
{
TInt containerWidth=Size().iWidth;
TInt containerHeight=Size().iHeight;
// Find half of the greater - width or height
TInt length=containerHeight>containerWidth ? containerWidth/4 : containerHeight/4;
TSize smileySize(length,length);
// Do some preliminary calculations so that Draw() is as short
// as possible.
TInt xOffset=smileySize.iWidth/4; // x offset from the center
TInt yOffset=(containerHeight - smileySize.iHeight) / 2;
iSmiley1->SetPosition(Position() +
TPoint(containerWidth/2 - smileySize.iWidth - xOffset, yOffset));
iSmiley2->SetPosition(Position() +
TPoint(containerWidth/2 + xOffset, yOffset));
// Calling SetSizeL() causes the components' SizeChanged() to be called.
iSmiley1->SetSize(smileySize);
iSmiley2->SetSize(smileySize);
}
void CSmileyContainer::Draw(const TRect& aRect) const
{
// Just draw a rectangle round the edge of the control.
CWindowGc& gc=SystemGc();
gc.Clear(aRect);
gc.SetClippingRect(aRect);
gc.DrawRect(Rect());
}
// This function is defined by MCoeControlObserver. It gets called whenever
// a control that this control is observing calls ReportEventL().
// In this example, the CSmileyContainer is the observer for both of the
// CSmileys. CCoeControl::ProcessPointerEventL() calls ReportEvent(),
// sending an event of type EEventRequestFocus, whenever an EButton1Down event
// occurs in the CSmiley that doesn't currently have focus.
void CSmileyContainer::HandleControlEventL(CCoeControl* aControl,
TCoeEvent aEventType)
{
switch (aEventType)
{
case EEventRequestFocus:
{
if (aControl->IsFocused())
return;
SwapFocus(aControl);
}
break;
default:
break;
}
}
// This function is called by the framework whenever a component in a dialog is
// about to lose focus. It checks that the data in ithe component is valid. In
// this example, there's a "rule" that both the CSmileys in the container can't
// be miserable! If they are, the function leaves. The framework issues the message
// we give it, and doesn't move focus away from the CSmileyContainer.
void CSmileyContainer::PrepareForFocusLossL()
{
if (!iSmiley1->IsSmiling() && !iSmiley2->IsSmiling())
{
// CEikonEnv::Static()->LeaveWithInfoMsg(R_EXAMPLE_TEXT_VALIDATE);
// CEikonEnv::Static()->LeaveWithInfoMsg(_L("They_can't_both"));
}
}
// This function gets called whenever the application calls SetFocus().
// It redraws the CSmileyContainer, so that they are updated to show
// which one now has focus.
void CSmileyContainer::FocusChanged(TDrawNow aDrawNow)
{
if (IsFocused())
{
iSmiley1->SetFocus(ETrue, EDrawNow);
}
else
{
if (iSmiley1->IsFocused())
iSmiley1->SetFocus(EFalse, EDrawNow);
else
iSmiley2->SetFocus(EFalse, EDrawNow);
}
if (aDrawNow)
DrawNow();
}
void CSmileyContainer::SwapFocus(CCoeControl* aControl)
{
if (aControl==iSmiley1)
{
iSmiley2->SetFocus(EFalse, EDrawNow);
iSmiley1->SetFocus(ETrue, EDrawNow);
}
else
{
iSmiley1->SetFocus(EFalse, EDrawNow);
iSmiley2->SetFocus(ETrue, EDrawNow);
}
}
TKeyResponse CSmileyContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
// Use the arrow keys to move focus between the two CSmileys.
switch (aKeyEvent.iScanCode)
{
case EStdKeySpace:
if (iSmiley1->IsFocused())
return iSmiley1->OfferKeyEventL(aKeyEvent, aType);
else if (iSmiley2->IsFocused())
return iSmiley2->OfferKeyEventL(aKeyEvent, aType);
break;
case EStdKeyRightArrow:
if (iSmiley1->IsFocused())
SwapFocus(iSmiley2);
return EKeyWasConsumed;
break;
case EStdKeyLeftArrow:
if (iSmiley2->IsFocused())
SwapFocus(iSmiley1);
return EKeyWasConsumed;
break;
//case EStdKeyUpArrow:
//return EKeyWasConsumed;
// break;
default:
break;
}
// If the CSmileyContainer didn't use the key event, it must return EKeyWasNotConsumed,
// so that the key event is passed to other controls on the stack.
return EKeyWasNotConsumed;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -