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

📄 oggcontrols.cpp

📁 OggPlay for Symbian 是symbian上的一个媒体播放程序的源码。它支持ogg,wav等等多媒体格式。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*
 *  Copyright (c) 2003 L. H. Wilden.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <OggOs.h>
#include "OggLog.h"
#include "OggControls.h"
#include "OggMsgEnv.h"

#include <OggPlay.rsg>

#include <eikenv.h>
#include <flogger.h>

#include "int_fft.c"

const TInt KLengthFFT = 512;

#if defined(SERIES60)
const TInt KListboxcycles=4;
#else
const TInt KListboxcycles=5;
#endif

TInt GetTextWidth(const TDesC& aText, CFont* aFont, TInt w)
{
  TInt pos = aFont->TextCount(aText, w);
  while (pos<aText.Length()) {
    w+= 16;
    pos = aFont->TextCount(aText, w);
  }
  return w;
}

 // TOggParser
TOggParser::TOggParser(RFs& aFs, const TFileName& aFileName, TInt aScaleFactor)
: iScaleFactor(aScaleFactor)
{
  iLine= 1;
  iDebug= ETrue;
  iBuffer = NULL;

  RFile file;
  TInt err = file.Open(aFs, aFileName, EFileShareReadersOnly);
  if (err == KErrNone)
	{
	TInt size;
	err = file.Size(size);
	if (err == KErrNone)
		{
		iBuffer = HBufC8::New(size);
		if (iBuffer)
			{
			TPtr8 bufDes(iBuffer->Des());
			err = file.Read(bufDes);

			iBufferPtr = iBuffer->Ptr();
			}
		else
			err = KErrNoMemory;
		}

	file.Close();
	}

  iState = (err == KErrNone) ? ESuccess : EFileNotFound;
}

TOggParser::~TOggParser()
{
  delete iBuffer;
}

TBool
TOggParser::ReadHeader()
{
  ReadToken();
  if (iToken!=_L("OggPlay")) {
    iState= ENoOggSkin;
    return EFalse;
  }
  ReadToken(iVersion);
  if (iVersion!=1) {
    iState= EUnknownVersion;
    return EFalse;
  }
  ReadToken();
  if (iToken!=KBeginToken) {
    iState= EBeginExpected;
    return EFalse;
  }
  return ETrue;
}

TBool
TOggParser::ReadToken()
{
  // Read next token. Skip any white space at the beginning.
  // Read possible EOL characters if any.
  iToken.SetLength(0);

  TInt c;
  TInt bufferLength = iBuffer->Length();
  const TUint8* bufferBase = iBuffer->Ptr();
  TBool start(ETrue), stop(EFalse), ignoreWS(EFalse);
  do {
	c = -1;
	if ((iBufferPtr - bufferBase) == bufferLength)
		break;

    c = *iBufferPtr;
	iBufferPtr++;

    // Skip initial white space (32 = Space, 9 = Tab)
    if (start && ((c==32) || (c==9)))
		continue;

	// Check for opening "
	if (start && (c==34))
	{
		ignoreWS = ETrue;
		start = EFalse;
		continue;
	}

	// Interpret the character 
    start = EFalse;
    stop = (c==13) || (c==10) || (c==12);
	if (!stop)
	{
		// Check for closing " or white space
		if (ignoreWS)
			stop = (c==34);
		else
			stop = (c==32) || (c == 9);
	}

    if (!stop)
		iToken.Append((unsigned char) c);
  } while (!stop);

  TBool eol = c != 32;
  if (c == 13)
	{
	// Skip the next character
	if ((iBufferPtr - bufferBase) != bufferLength)
		iBufferPtr++;
	}

  if (eol)
	  iLine++;

  return iToken.Length()>0;
}

TBool
TOggParser::ReadToken(TInt& aValue)
{
  TBool success= ReadToken();
  if (success) {
    TLex parse(iToken);
    success= parse.Val(aValue)==KErrNone;
    if (!success) iState= EIntegerExpected;
  }
  return success;
}

void ScaleBitmapL(CFbsBitmap* aDst, CFbsBitmap* aSrc, TInt aScaleFactor)
{
  TSize origSize = aSrc->SizeInPixels();
  TDisplayMode origDM = aSrc->DisplayMode();
  TSize dblSize = TSize(aScaleFactor*origSize.iWidth, aScaleFactor*origSize.iHeight);
  User::LeaveIfError(aDst->Create(dblSize, origDM));

  CFbsBitmapDevice* device = CFbsBitmapDevice::NewL(aDst);
  CleanupStack::PushL(device);

  CFbsBitGc *gc;
  User::LeaveIfError(device->CreateContext(gc));
  CleanupStack::PushL(gc);

  gc->DrawBitmap(TRect(dblSize), aSrc, TRect(origSize));
  CleanupStack::PopAndDestroy(2, device);
}

CGulIcon*
TOggParser::ReadIcon(const TFileName& aBitmapFile)
{
  TInt idxBitmap,idxMask;
  if (!ReadToken(idxBitmap)) return NULL;
  if (!ReadToken(idxMask)) return NULL;

  CGulIcon* result = NULL;
  if (iScaleFactor == 1)
	  result = CEikonEnv::Static()->CreateIconL(aBitmapFile, idxBitmap, idxMask);
  else
  {
	CFbsBitmap* bitmap = new(ELeave) CFbsBitmap;
	CleanupStack::PushL(bitmap);
	User::LeaveIfError(bitmap->Load(aBitmapFile, idxBitmap));

	CFbsBitmap* mask = new(ELeave) CFbsBitmap;
	CleanupStack::PushL(mask);
	User::LeaveIfError(mask->Load(aBitmapFile, idxMask));

	CFbsBitmap* scaledBitmap = new(ELeave) CFbsBitmap;
	CleanupStack::PushL(scaledBitmap);
	ScaleBitmapL(scaledBitmap, bitmap, iScaleFactor);

	CFbsBitmap* scaledMask = new(ELeave) CFbsBitmap;  
	CleanupStack::PushL(scaledMask);
	ScaleBitmapL(scaledMask, mask, iScaleFactor);

	result= CGulIcon::NewL(scaledBitmap, scaledMask);
	CleanupStack::Pop(2, scaledBitmap);
	CleanupStack::PopAndDestroy(2, bitmap);
  }

  if (!result) iState= EBitmapNotFound;
  return result;
}

TBool
TOggParser::ReadColor(TRgb& aColor)
{
  TInt red(0), green(0), blue(0);
  TBool success= ReadToken(red) && ReadToken(green) && ReadToken(blue);
  if (success) {
    aColor.SetRed(red);
    aColor.SetGreen(green);
    aColor.SetBlue(blue);
  }
  return success;
}

CFont*
TOggParser::ReadFont()
{
  if (!ReadToken()) return 0;
  if (iToken.Length()>32) return 0;
  TBuf<32> name(iToken);
  TInt size(12);
  if (!ReadToken(size)) return 0;
  TInt style(0);
  if (!ReadToken(style)) return 0;
  CFont* result = NULL;
  TFontSpec fs(name,size);
  if (style==1) fs.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
  CCoeEnv::Static()->ScreenDevice()->GetNearestFontInPixels(result,fs);

  return result;
}

void
TOggParser::ReportError()
{
  if (iState==ESuccess) return;
  TBuf<128> buf;
  buf.Append(_L("Line "));
  buf.AppendNum(iLine);
  buf.Append(_L(": "));
  switch (iState) {
  case EFileNotFound: buf.Append(_L("File not found.")); break;
  case ENoOggSkin: buf.Append(_L("This is not an Ogg skin file!")); break;
  case EUnknownVersion: buf.Append(_L("Unknown skin file version.")); break;
  case EFlipOpenExpected: buf.Append(_L("FlipOpen statement expected.")); break;
  case ESyntaxError: buf.Append(_L("Syntax error.")); break;
  case EBeginExpected: buf.Append(_L("{ expected.")); break;
  case EEndExpected: buf.Append(_L("} expected.")); break;
  case EBitmapNotFound: buf.Append(_L("Bitmap not found.")); break;
  case EIntegerExpected: buf.Append(_L("Integer number expected.")); break;
  case EOutOfRange: buf.Append(_L("Number is out of the valid range.")); break;
  default: buf.Append(_L("Unknown error.")); break;
  }
  buf.Append(_L("Last token: <"));
  buf.Append(iToken);
  buf.Append(_L(">"));

  //CCoeEnv::Static()->InfoWinL(_L("Error reading skin file"),buf);
  User::InfoPrint(buf);
  //OGGLOG.Write(buf);
}

void TOggParser::Debug(const TDesC& txt, TInt level)
{
  if (level==0 && !iDebug) return;
  TBuf<256> buf;
  buf.Append(_L("Debug ("));
  buf.AppendNum(iLine);
  buf.Append(_L("): "));
  buf.Append(txt);
  buf.Append(_L(" Token:"));
  buf.Append(iToken); 
  //TRACEF(COggLog::VA(_L("%S"), &buf ));
  //OGGLOG.WriteFormat(buf);

//  User::InfoPrint(buf);
//  User::After(TTimeIntervalMicroSeconds32(1000000));
}


// COggControl
COggControl::COggControl()
: iRedraw(ETrue), iVisible(ETrue)
{
  //TRACE(COggLog::VA(_L("COggControl::COggControl() 0x%X"), this ));
}

COggControl::COggControl(TBool aHighFrequency)
: iRedraw(ETrue), iVisible(ETrue), iHighFrequency(aHighFrequency)
{
  //TRACE(COggLog::VA(_L("COggControl::COggControl(TBool aHighFrequency) 0x%X"), this ));
}

COggControl::~COggControl()
{
  if (iFocusIcon) { delete iFocusIcon; iFocusIcon= 0; }
}
  
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 isVisible)
{
  if (isVisible!=iVisible) {
    iVisible= isVisible;
    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)
{
  if (iObserver && !iDimmed) iObserver->OggPointerEvent(this, p);
}

void
COggControl::ControlEvent(TInt anEventType, TInt aValue)
{
  if (iObserver && !iDimmed) iObserver->OggControlEvent(this, anEventType, aValue);
}


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

void
COggControl::SetFocusIcon(CGulIcon* anIcon)
{
  _LIT(KS,"COggButton::SetFocusIcon");
  RDebug::Print(KS);
  //OGGLOG.Write(KS);
  iAcceptsFocus=ETrue;
  if (iFocusIcon) 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* anIcon)
{
  TSize s(anIcon->Bitmap()->SizeInPixels());
  TPoint p(ix+iw/2-s.iWidth/2,iy+ih/2-s.iHeight/2);
  aBitmapContext.BitBltMasked(p,anIcon->Bitmap(), TRect(TPoint(0,0),s), anIcon->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))
	{
	  ax *= p.iScaleFactor ; ay *= p.iScaleFactor;
	  aw *= p.iScaleFactor ; ah *= p.iScaleFactor;
      SetPosition(ax, ay, aw, ah);
      return ETrue;
    }
    else
      return EFalse;
  } else if (p.iToken==_L("FocusIcon")) {
    p.Debug(_L("Setting focused icon."));
    CGulIcon* aIcon=p.ReadIcon(iBitmapFile);
    //TSize aSize=aIcon->Bitmap()->SizeInPixels();
    //if(aSize.iHeight>ih) OGGLOG.WriteFormat(_L("Info: Bitmap height (%d) > control height (%d)"),aSize.iHeight,ih);
    //if(aSize.iWidth>iw) OGGLOG.WriteFormat(_L("Info: Bitmap width (%d)> control width (%d)"),aSize.iWidth,iw);

    SetFocusIcon(aIcon);
    //__ASSERT_ALWAYS(iObserver,OGGPANIC(_L("No observer set while trying to add control to focus list"),14));
    iObserver->AddControlToFocusList(this);
  }
  return ETrue;
}

TBool COggControl::HighFrequency()
{
	return iHighFrequency;
}


/***********************************************************
 *
 * COggText
 *
 ***********************************************************/
COggText::COggText() :
  COggControl(),
  iFontColor(0,0,0),
  iNeedsScrolling(EFalse),
  iHasScrolled(EFalse),
  iScrollDelay(30),
  iScrollStep(2)
{
}

COggText::~COggText()
{
  delete iText;

  if (iFont && iOwnedByControl) 
      CCoeEnv::Static()->ScreenDevice()->ReleaseFont(iFont);
}

void 
COggText::SetFont(CFont* aFont, TBool ownedByControl)
{
  iFont= aFont;
  iRedraw= ETrue;
  iOwnedByControl = ownedByControl;

  iFontHeight = iFont->HeightInPixels();
  iFontAscent = iFont->AscentInPixels();

#if defined(SERIES60)
  if (iFont->TypeUid() == KCFbsFontUid)
  {
	  CFbsFont* fbsFont = (CFbsFont *) iFont;
	  if (fbsFont->IsOpenFont())
	  {
	    TOpenFontMetrics fontMetrics;
		if (fbsFont->GetFontMetrics(fontMetrics))
		{
		  iFontHeight = fontMetrics.MaxHeight() + fontMetrics.MaxDepth() + 1;
		  iFontAscent = fontMetrics.MaxHeight();
		}
	  }
  }
#endif

  iLinePadding = (ih - iFontHeight) / 2;
  if (iLinePadding<0)
	  iLinePadding = 0;
}

void
COggText::SetFontColor(TRgb aColor)
{
  iFontColor= aColor;
  iRedraw= ETrue;
}

void
COggText::SetScrollStyle(TInt aScrollStyle) {
  iScrollStyle=aScrollStyle;
}

void
COggText::SetScrollDelay(TInt aScrollDelay) {
  iScrollDelay=aScrollDelay;
}

void
COggText::SetScrollStep(TInt aScrollStep) {
  iScrollStep=aScrollStep;
}

void
COggText::SetText(const TDesC& aText)
{
  if (iText) 
    {
    delete iText;
    iText = NULL;
    }

  iText = HBufC::NewL(aText.Length());
  iText->Des().Copy(aText);

  iTextWidth= GetTextWidth(aText, iFont, iw);
  iNeedsScrolling= iTextWidth>iw;

  iCycle= 0;
  iDrawOffset= 0;
  iHasScrolled= EFalse;
  iRedraw= ETrue;
}

void
COggText::ScrollNow()
{
  iHasScrolled= EFalse;
  iCycle= iScrollDelay;
}

void COggText::Cycle()
{
  if (!iNeedsScrolling) return;
  switch(iScrollStyle)
  {
  case EOnce:
    CycleOnce();
    break;

  case EEndless:
    CycleOff();
    break;

  case ETilBorder:
    CycleBorder();
    break;

  case EBackAndForth:
    CycleBackAndForth();
    break;

  default:
    CycleOff();
    break;
  }
}

void COggText::CycleOnce()
{
  if (iHasScrolled) return;
  CycleOff();
}

void COggText::CycleOff()
{

⌨️ 快捷键说明

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