⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 conbase.cpp

📁 a position control(using in musicplayer) of symbian
💻 CPP
字号:
#include "conbase.h"
#include <EIKENV.H>
COggCanvas::COggCanvas()
: iControls(10),iSExit(EFalse)
	{
	}

COggCanvas::~COggCanvas()
	{
	DestroyBitmap();

	delete iBackground;
	}

void COggCanvas::LoadBackgroundBitmapL(const TDesC& aFileName, TInt iIdx)
	{
	delete iBackground;
	iBackground = NULL;

	iBackground = new(ELeave) CFbsBitmap;
	TInt err= iBackground->Load(aFileName,iIdx);
	if (err != KErrNone)
		{

		delete iBackground;
		iBackground = NULL;

		User::Leave(err);
		}

	DestroyBitmap();
	CreateBitmapL(iBackground->SizeInPixels());

	SetSize(iBackground->SizeInPixels());
	EnableDragEvents();
	DrawControl();
	}
void COggCanvas::Refresh()
	{
	TBool redrawRequired = EFalse;
	TInt i;
	for (i = 0 ; i<iControls.Count() ; i++) 
		{
		if (iControls[i]->iRedraw)
			{
			redrawRequired = ETrue;
			break;
			}
		}

	if (redrawRequired)
		{
		DrawControl(*iBitmapContext, *iBitmapDevice);

		Window().Invalidate();
		ActivateGc();
		Window().BeginRedraw();
		CWindowGc& gc=SystemGc();

#if defined(SERIES90)
		// Series 90 has a problem redrawing parts of the screen, so redraw everything
		TRect rect = TRect(iBitmap->SizeInPixels());
		gc.BitBlt(rect.iTl, iBitmap, rect);
#else
		for (i = 0 ; i<iControls.Count() ; i++)
			{
			if (iControls[i]->iRedraw)
				{
				TRect rect = iControls[i]->Rect();
				gc.BitBlt(rect.iTl, iBitmap, rect);

				iControls[i]->iRedraw = EFalse;
				}
			}
#endif

		Window().EndRedraw();
		DeactivateGc();
		}
	}

void COggCanvas::Invalidate()
	{
	DrawNow();
	}
void COggCanvas::IsExit()
	{
	iSExit=ETrue;
	}
void COggCanvas::NoExit()
	{
	iSExit=EFalse;
	}
COggControl* COggCanvas::GetControl(TInt i)
	{
	return iControls[i];
	}

void COggCanvas::AddControl(COggControl* c)
	{
	iControls.AppendL(c);
	}

void COggCanvas::ClearControls()
	{
	iControls.ResetAndDestroy();
	}

void COggCanvas::DrawControl()
	{
    iBitmapContext->SetClippingRect(Rect());
    iBitmapContext->BitBlt(TPoint(0,0),iBackground);

    // DrawControl will draw relative to its Position().
    // when drawing to the bitmap gc, Position() should be (0,0)
    TPoint	position = iPosition;
    iPosition = TPoint(0,0);
	TInt i;
	TInt iMax = iControls.Count();
    for (i = 0 ; i < iMax ; i++)
		iControls[i]->iRedraw = ETrue;

	DrawControl(*iBitmapContext, *iBitmapDevice);

	for (i = 0 ; i < iMax ; i++)
		iControls[i]->iRedraw = EFalse;

	iPosition = position;
	}

void COggCanvas::DestroyBitmap()
	{
	delete iBitmapContext;
	iBitmapContext = NULL;

	delete iBitmapDevice;
	iBitmapDevice = NULL;

	delete iBitmap;
	iBitmap = NULL;

	delete iOverlayMaskContext;
	iOverlayMaskContext = NULL;

	delete iOverlayMaskDevice;
	iOverlayMaskDevice = NULL;

	delete iOverlayMaskBitmap;
	iOverlayMaskBitmap = NULL;
	}

void COggCanvas::CreateBitmapL(const TSize& aSize)
	{
	iBitmap = new(ELeave) CFbsBitmap;
	iBitmap->Create(aSize, iEikonEnv->ScreenDevice()->DisplayMode());
  
	iBitmapDevice = CFbsBitmapDevice::NewL(iBitmap);
	User::LeaveIfError(iBitmapDevice->CreateContext(iBitmapContext));

	iOverlayMaskBitmap = new(ELeave) CFbsBitmap;
	iOverlayMaskBitmap->Create(aSize, EGray2);

	iOverlayMaskDevice = CFbsBitmapDevice::NewL(iOverlayMaskBitmap);
	User::LeaveIfError(iOverlayMaskDevice->CreateContext(iOverlayMaskContext));
	
	iOverlayMaskContext->SetBrushColor(KRgbWhite);
	iOverlayMaskContext->Clear();
	}

void COggCanvas::Draw(const TRect& aRect) const
	{
	CWindowGc& gc=SystemGc();
	gc.BitBlt(aRect.iTl, iBitmap, TRect(aRect.iTl - Position(), aRect.Size()));
	}

void COggCanvas::DrawControl(CBitmapContext& aBitmapContext, CBitmapDevice& /*aBitmapDevice*/) const
	{
	// Clear backgrounds
	TInt i, iMax = iControls.Count();
	for (i = 0 ; i<iMax ; i++)
		{
		COggControl* c= iControls[i];
		if (c->iRedraw)
			{
			aBitmapContext.SetClippingRect(Rect());

			// If the control has an overlay mask we clear using the mask
			if (c->iOverlayMask)
				{
				//Set up the mask
				iOverlayMaskContext->BitBlt(TPoint(c->ix, c->iy), c->iOverlayMask, TRect(TPoint(0, 0), TSize(c->iw,c->ih)));

				// Do a mask clear
				aBitmapContext.BitBltMasked(TPoint(c->ix, c->iy), iBackground, TRect(TPoint(c->ix,c->iy), TSize(c->iw,c->ih)), iOverlayMaskBitmap, ETrue);

				// Clear the mask (ready for next time)
				iOverlayMaskContext->Clear(TRect(TPoint(c->ix, c->iy), TSize(c->iw,c->ih)));
				}
			else
				{
				// Clear the entire rectangle
				aBitmapContext.BitBlt(TPoint(c->ix, c->iy), iBackground, TRect(TPoint(c->ix,c->iy), TSize(c->iw,c->ih)));
				}
			}
		}

	// Redraw controls
	for (i = 0 ; i<iMax ; i++)
		{
		COggControl* c= iControls[i];
		if (c->iRedraw && c->IsVisible())
			{
			aBitmapContext.SetClippingRect(c->Rect());
			c->Draw(aBitmapContext);
			}
		}
	}

TKeyResponse COggCanvas::OfferKeyEventL(const TKeyEvent& /*aKeyEvent*/, TEventCode /*aType*/)
	{
	return EKeyWasNotConsumed;
	}

void COggCanvas::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
  if (iGrabbed) iGrabbed->PointerEvent(aPointerEvent);

  if (!iGrabbed && aPointerEvent.iType==TPointerEvent::EButton1Down) {
    for (int i=0; i<iControls.Count(); i++) {
      COggControl* c= iControls[i];
      if (c->IsVisible() && !c->IsDimmed() && c->Rect().Contains(aPointerEvent.iPosition)) {
	iGrabbed= c;
	c->PointerEvent(aPointerEvent);
	return;
      }
    }
  }

  if (aPointerEvent.iType==TPointerEvent::EButton1Up) iGrabbed= 0;
}

void COggCanvas::CycleHighFrequencyControls()
	{
	for (TInt i = 0 ; i<iControls.Count() ; i++)
		{
		COggControl* control = iControls[i];
		if (control->HighFrequency())
			control->Cycle();
		}
	}

void COggCanvas::CycleLowFrequencyControls()
	{
	for (TInt i = 0 ; i<iControls.Count() ; i++)
		{
		COggControl* control = iControls[i];
		if (!control->HighFrequency())
			control->Cycle();
		}
	}

COggControl::COggControl()
: iRedraw(ETrue), iVisible(ETrue)
	{
	}

COggControl::COggControl(TBool aHighFrequency)
: iRedraw(ETrue), iVisible(ETrue), iHighFrequency(aHighFrequency)
	{
	}

COggControl::~COggControl()
	{
	delete iFocusIcon;
	delete iOverlayMask;
	}

void COggControl::Cycle()
	{
	}

void COggControl::SetPosition(TInt ax, TInt ay, TInt aw, TInt ah)
	{
	ix = ax;
	iy = ay;
	iw = aw;
	ih = ah;
	
	iRedraw = ETrue;
	}

void COggControl::Redraw(TBool doRedraw)
	{
	iRedraw = doRedraw;
	}

void COggControl::MakeVisible(TBool aVisible)
	{
	if (aVisible != iVisible)
		{
		iVisible = aVisible;
		iRedraw = ETrue;
		}
	}

TBool COggControl::IsVisible()
	{
	return iVisible;
	}

void COggControl::SetDimmed(TBool aDimmed)
	{
	if (aDimmed != iDimmed)
		{
		iDimmed = aDimmed;
		iRedraw = ETrue;
		}
	}

void COggControl::SetFocus(TBool aFocus)
	{
	//__ASSERT_DEBUG(iAcceptsFocus,OGGLOG.Write(_L("Assert: OggControl::SetFocus - asked to set focus, but should never accept it")));
	if (aFocus != iFocus)
		{
		iFocus = aFocus;
		iRedraw = ETrue;
		}
	}

TBool COggControl::Focus()
	{
	return iFocus;
	}


TBool COggControl::IsDimmed()
	{
	return iDimmed;
	}

TRect COggControl::Rect()
	{
	return TRect(TPoint(ix,iy), TSize(iw,ih));
	}

TSize COggControl::Size()
	{
	return TSize(iw,ih);
	}
/*
void COggControl::SetObserver(MOggControlObserver* obs)
	{
	iObserver = obs;
	}*/

void COggControl::PointerEvent(const TPointerEvent& p)
	{
	}

void COggControl::ControlEvent(TInt anEventType, TInt aValue)
	{
	}

void COggControl::SetBitmapFile(const TDesC& aFileName)
	{
	iBitmapFile = aFileName;
	}

void COggControl::SetFocusIcon(CGulIcon* anIcon)
	{
	iAcceptsFocus = ETrue;

	delete iFocusIcon;
	iFocusIcon= anIcon;

	iRedraw = ETrue;
	}

void COggControl::DrawFocus(CBitmapContext& aBitmapContext)
	{
	if (iFocus)
		{
		// __ASSERT_DEBUG(iAcceptsFocus,OGGPANIC(_L("Assert - Control has Focus but should never accept it !?"),3815));
		// __ASSERT_DEBUG(iVisible,OGGPANIC(_L("Assert - Control is not visible, but has focus"),3816));
		DrawCenteredIcon(aBitmapContext,iFocusIcon);
		}
	}

void COggControl::DrawCenteredIcon(CBitmapContext& aBitmapContext, CGulIcon* aIcon)
	{
	TSize s(aIcon->Bitmap()->SizeInPixels());
	TPoint p(ix+iw/2 - s.iWidth/2, iy+ih/2 - s.iHeight/2);
	aBitmapContext.BitBltMasked(p, aIcon->Bitmap(), TRect(TPoint(0, 0), s), aIcon->Mask(), ETrue);
	}
/*
TBool COggControl::Read(TOggParser& p)
	{
	p.ReadToken();
	if (p.iToken != KBeginToken)
		{
		p.iState = TOggParser::EBeginExpected;
		return EFalse;
		}

	while (p.ReadToken() && (p.iToken != KEndToken) && (p.iState == TOggParser::ESuccess))
		ReadArguments(p); 

	if ((p.iState == TOggParser::ESuccess) && (p.iToken != KEndToken)) 
		p.iState = TOggParser::EEndExpected;

	return (p.iState == TOggParser::ESuccess);
	}

TBool COggControl::ReadArguments(TOggParser& p)
	{
	if (p.iToken == _L("Position"))
		{
		p.Debug(_L("Setting position."));
		TInt ax, ay, aw, ah;
		if (p.ReadToken(ax) && p.ReadToken(ay) && p.ReadToken(aw) && p.ReadToken(ah))
			{
			SetPosition(ax, ay, aw, ah);
			return ETrue;
			}
		else
			return EFalse;
		}
	else if (p.iToken == _L("FocusIcon"))
		{
		p.Debug(_L("Setting focused icon."));
		SetFocusIcon(p.ReadIcon(iBitmapFile));

		iObserver->AddControlToFocusList(this);
		}
	else if (p.iToken == _L("OverlayMask"))
		iOverlayMask = p.ReadBitmap(iBitmapFile);

	return ETrue;
	}
*/
TBool COggControl::HighFrequency()
	{
	return iHighFrequency;
	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -