fontsandtextdevicefontscontainer.cpp

来自「s60源码」· C++ 代码 · 共 244 行

CPP
244
字号
/**
 * @brief Definition of CFontsAndTextDeviceFontsContainer
 *
 * Copyright (c) EMCC Software Ltd 2003
 * @version 1.0
 */

// INCLUDE FILES

// Class includes
#include "FontsAndTextDeviceFontsContainer.h"

// User includes
#include <eikenv.h>		// iEikonEnv
#include <eiksbfrm.h>	// CEikScrollBarFrame

// CONSTANTS

const TInt KMaxFontName = 20;
const TInt KFontSpecSize = 100;
const TInt KColorBlack = 215;
const TInt KTextLeftMarginFactor = 7;
const TInt KBaseLineIncrementer = 2;

// ================= MEMBER FUNCTIONS =======================

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CFontsAndTextDeviceFontsContainer using the NewLC method, popping
 * the constructed object from the CleanupStack before returning it.
 * 
 * @param aMop The object provider for scroll bars
 * @param aRect The rectangle for this window
 * @return The newly constructed CFontsAndTextDeviceFontsContainer
 */
CFontsAndTextDeviceFontsContainer* CFontsAndTextDeviceFontsContainer::NewL(MObjectProvider& aMop, const TRect& aRect)
	{
	CFontsAndTextDeviceFontsContainer* self = CFontsAndTextDeviceFontsContainer::NewLC(aMop, aRect);
	CleanupStack::Pop(self);
	return self;
	}

/**
 * Symbian OS 2 phase constructor.
 * Constructs the CFontsAndTextDeviceFontsContainer using the constructor and ConstructL 
 * method, leaving the constructed object on the CleanupStack before returning it.
 * 
 * @param aMop The object provider for scroll bars
 * @param aRect The rectangle for this window
 * @return The newly constructed CFontsAndTextDeviceFontsContainer
 */
CFontsAndTextDeviceFontsContainer* CFontsAndTextDeviceFontsContainer::NewLC(MObjectProvider& aMop, const TRect& aRect)
	{
	CFontsAndTextDeviceFontsContainer* self = new (ELeave) CFontsAndTextDeviceFontsContainer();
	CleanupStack::PushL(self);
	self->ConstructL(aMop, aRect);
	return self;
	}

/**
 * Symbian OS 2nd phase constructor.  
 * Creates a Window for the control to draw to.
 * Uses the screen graphics device to obtain the number of system fonts
 * Constructs scroll bar
 *
 * @param aMop The object provider for scroll bars
 * @param aRect The rectangle for this window
 */ 
void CFontsAndTextDeviceFontsContainer::ConstructL(MObjectProvider& aMop, const TRect& aRect)
	{
	// Create the window.
	CreateWindowL(); 	
	SetRect(aRect);
	CCoeControl::SetMopParent(&aMop);

	// Obtain a pointer to the screen device and set up typeface support.
	iDeviceMap = iCoeEnv->ScreenDevice();
	iTypefaceSupport = new TTypefaceSupport();
	iNumTypefaces = iCoeEnv->ScreenDevice()->NumTypefaces();

	// Set up the scroll bar.
	iSBFrame = new (ELeave) CEikScrollBarFrame(this, this);
	iSBFrame->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EOn);
	iSBFrame->SetTypeOfVScrollBar(CEikScrollBarFrame::EArrowHead);
	
	TRect rect = Rect();
	iVertModel.iScrollSpan = iNumTypefaces;
	iVertModel.iThumbSpan = 1;
	iVertModel.iThumbPosition = iCurrentScrollNum;

	TEikScrollBarFrameLayout layout;
	iSBFrame->TileL(&iHorizModel, &iVertModel, rect, rect, layout);
	iSBFrame->SetVFocusPosToThumbPos(iVertModel.iThumbPosition);
	ActivateL();	// Activate the control.
	iSBFrame->DrawScrollBarsNow();
	}

/**
 * Standard C++ constructor
 *
 */
CFontsAndTextDeviceFontsContainer::CFontsAndTextDeviceFontsContainer() :
	iCurrentScrollNum(0)
	{
	}

/**
 * Destructor
 */ 
CFontsAndTextDeviceFontsContainer::~CFontsAndTextDeviceFontsContainer()
	{   
	delete iTypefaceSupport;
	
	if (iSBFrame)
		{
		iSBFrame->SetScrollBarsDimmed(ETrue, ETrue);
		delete iSBFrame;
		}
	}

/**
 *	The number of lodging CCoeControls
 *
 * @return The number of lodging CCoeControls (in this case the scroll bar)
 */
TInt CFontsAndTextDeviceFontsContainer::CountComponentControls() const
	{
	return iSBFrame->CountComponentControls(); // return number of controls inside this container
	}

/**
 *	Returns a particular lodging CCoeControl at index aIndex
 *
 * @param aIndex The index of the lodging control so it can be returned
 * @return The lodging CCoeControl at aIndex
 */
CCoeControl* CFontsAndTextDeviceFontsContainer::ComponentControl(TInt aIndex) const
	{
	return iSBFrame->ComponentControl(aIndex);
	}

/**
 *
 * This draw function loops through all the fonts on the device, using them
 * to draw a text string. This string is the name of the font itself.
 *
 * @param aRect The rectangular area of the control
 *
 */
void CFontsAndTextDeviceFontsContainer::Draw(const TRect& aRect) const
	{
	// Clear the graphics context.
	CWindowGc& gc = SystemGc();
	gc.Clear();    

	// Set the pen colour to black.
	gc.SetPenColor(AKN_LAF_COLOR(KColorBlack));

	// Loop through the fonts and draw text to screen
	TBuf<KMaxFontName> fontName;
	CFont* fontToUse;
	TPoint textPoint(aRect.Width() / KTextLeftMarginFactor, 0);

	for (TInt i = iCurrentScrollNum; i < iNumTypefaces; i++)
		{
		// Get the i-th font on the device.
		iCoeEnv->ScreenDevice()->TypefaceSupport(*iTypefaceSupport, i);

		// Get the font name.
		fontName = iTypefaceSupport->iTypeface.iName.Des();

		// Create font specification.
		TFontSpec fontSpec(fontName, KFontSpecSize);
		iDeviceMap->GetNearestFontInTwips(fontToUse, fontSpec);
		
		// Increment baseline by two times height of font.
		textPoint.iY += (fontToUse->HeightInPixels() * KBaseLineIncrementer);

		// Draw text in font with graphics context.
		gc.UseFont(fontToUse);
		gc.DrawText(fontName, textPoint);
		gc.DiscardFont();
		iDeviceMap->ReleaseFont(fontToUse);
		}		
	}

/**
 * Interface function of MEikScrollbarObserver. 
 * Called after a particular scroll bar event
 *
 * @param aScrollBar The scroll bar
 * @param aEventType The scroll bar event
 *
 */
void CFontsAndTextDeviceFontsContainer::HandleScrollEventL(CEikScrollBar* /*aScrollBar*/, TEikScrollEvent /*aEventType*/)
	{
	}

/**
 * Handles the application response to keypresses.
 * In this case it causes the scroll down effect allowing the user to 
 * see further fonts.
 *
 * @param aKeyEvent The key event
 * @param aType The type of key event from EEventKeyUp, EEventKey or EEventKeyDown
 *
 * @return Shows whether or not a key was pressed as determined by the function
 */
TKeyResponse CFontsAndTextDeviceFontsContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
	{
	if (aType != EEventKey)
		{
		return EKeyWasNotConsumed;
		}

	if ((aKeyEvent.iCode == EKeyDownArrow) || (aKeyEvent.iCode == EKeyUpArrow))
		{
		if (aKeyEvent.iCode == EKeyDownArrow)
			{
			if (iCurrentScrollNum < iNumTypefaces - 1)
				{
				iCurrentScrollNum++;
				}
			}
		else if (aKeyEvent.iCode == EKeyUpArrow)
			{
			if (iCurrentScrollNum > 0)
				{
				iCurrentScrollNum--;
				}
			}

		iVertModel.iThumbPosition = iCurrentScrollNum;
		DrawNow();
		iSBFrame->SetVFocusPosToThumbPos(iVertModel.iThumbPosition);	
		iSBFrame->DrawScrollBarsNow();
		return EKeyWasConsumed;
		}			
		
	return EKeyWasNotConsumed;
	}

// End of File  

⌨️ 快捷键说明

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