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

📄 listboxcontainer.cpp

📁 这是一个学习Symbian的ListBox的好例子,我也是看学个学会的
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <e32def.h>
#include <eikclbd.h>
#include <akntitle.h>
#include <aknlists.h>
#include <akniconarray.h>
#include "ListIcons.mbg"
#include "ListBoxContainer.h"

void ListBoxContainer::ConstructL(const TRect& aRect)
{
	CreateWindowL();

	activateListBoxL(false);	// false: do not redraw

    SetRect(aRect);
	ActivateL();
}

ListBoxContainer::~ListBoxContainer()
{
	delete _listBox;
}

void ListBoxContainer::SizeChanged()
{
	TRect r = Rect();
	if (_listBox)
		_listBox->SetExtent(TPoint(0,0), TSize(r.Width(), r.Height()));
}

TInt ListBoxContainer::CountComponentControls() const
{
	return 1;
}

CCoeControl* ListBoxContainer::ComponentControl(TInt aIndex) const
{
	switch ( aIndex )
	{
		case 0: return _listBox;
		default: return NULL;
	}
}

void ListBoxContainer::Draw(const TRect& aRect) const
{
	CWindowGc& gc = SystemGc();
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.SetBrushColor(KRgbWhite);
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.DrawRect(aRect);
}

void ListBoxContainer::HandleControlEventL(CCoeControl* control, TCoeEvent eventType)
{
}

TKeyResponse ListBoxContainer::OfferKeyEventL(const TKeyEvent& keyEvent, TEventCode type)
{
	if (type != EEventKey )
        return EKeyWasNotConsumed;

	return _listBox->OfferKeyEventL(keyEvent, type);
}

void ListBoxContainer::HandleListBoxEventL(CEikListBox* listBox, TListBoxEvent eventType)
{
    if (!_displayListBoxNotifications)
        return;
	switch (eventType)	
	{   
		case EEventEnterKeyPressed :  iEikonEnv->InfoMsg(_L("EEventEnterKeyPressed")); break;
		case EEventItemClicked:       iEikonEnv->InfoMsg(_L("EEventItemClicked")); break;
		case EEventItemDoubleClicked: iEikonEnv->InfoMsg(_L("EEventItemDoubleClicked")); break;
		case EEventItemActioned:      iEikonEnv->InfoMsg(_L("EEventItemActioned")); break;
		case EEventEditingStarted:    iEikonEnv->InfoMsg(_L("EEventEditingStarted")); break;
		case EEventEditingStopped:    iEikonEnv->InfoMsg(_L("EEventEditingStopped")); break;
	};
}

void ListBoxContainer::activateListBoxL(TBool redraw /*true*/)
{
	createListBoxL(EAvkonListBoxType(_currentListBoxType));
	TBuf<48> title;
	getListBoxTitle(EAvkonListBoxType(_currentListBoxType), title);

	CEikStatusPane* statusPane = iEikonEnv->AppUiFactory()->StatusPane();
    CAknTitlePane* titlePane = static_cast<CAknTitlePane*>(statusPane->ControlL(TUid::Uid(EEikStatusPaneUidTitle)));
	titlePane->SetTextL(title); 

	if (redraw)
	{
		SizeChanged();
		_listBox->ActivateL();
		DrawNow();
	}
}

void ListBoxContainer::cmdActivatePrevListBoxL()
{
	if (_currentListBoxType == eFirst)
		_currentListBoxType = eLast;
	else
		_currentListBoxType--;
	activateListBoxL();
}

void ListBoxContainer::cmdActivateNextListBoxL()
{
	if (_currentListBoxType == eLast)
		_currentListBoxType = eFirst;
	else
		_currentListBoxType++;
	activateListBoxL();
}

void ListBoxContainer::cmdAddL()
{
	if (_listBox == 0)
		return;
	CTextListBoxModel* model = _listBox->Model();
	MDesCArray* textArray = model->ItemTextArray();
	CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);
	addListBoxItemL(EAvkonListBoxType(_currentListBoxType),	listBoxItems);

	_listBox->HandleItemAdditionL(); // Update listbox
	_listBox->SetCurrentItemIndexAndDraw(listBoxItems->Count() - 1);
}

void ListBoxContainer::cmdRemoveL()
{
	if (_listBox == 0)
		return;
	CTextListBoxModel* model = _listBox->Model();
	TInt itemCount = model->NumberOfItems();
	TInt currentItem = _listBox->CurrentItemIndex();
	__ASSERT_DEBUG(currentItem >= 0 && currentItem < itemCount, iEikonEnv->InfoMsg(_L("Invalid Idx")));

	MDesCArray* textArray = model->ItemTextArray();
	CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);
	listBoxItems->Delete(currentItem, 1);	// 1 = how many items to delete
	
	AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(_listBox, currentItem, ETrue);
	_listBox->DrawNow(); // Update listbox
}

void ListBoxContainer::cmdMark(TInt item, TBool mark)
{
	if (_listBox == 0)
		return;
	CTextListBoxModel* model = _listBox->Model();
	TInt itemCount = model->NumberOfItems();
	CListBoxView* listBoxView = _listBox->View();
	if (mark)
	{
		for (TUint i = 0; i < itemCount; i++)
			if (item == -1 || item == i)
				listBoxView->SelectItemL(i);
	}
	else
	{
		for (TUint i = 0; i < itemCount; i++)
			if (item == -1 || item == i)
				listBoxView->DeselectItem(i);
	}
}

TBool ListBoxContainer::isListBoxEmpty() const
{
	if (_listBox == 0)
		return ETrue;
	CTextListBoxModel* model = _listBox->Model();
	return model->NumberOfItems() == 0;
}

TInt ListBoxContainer::getCurrentListBoxItem() const
{
	if (_listBox == 0)
		return -1;
	return _listBox->CurrentItemIndex();
}

CArrayPtr<CGulIcon>* ListBoxContainer::loadIconListL() const
{
	_LIT(KListBoxIconsFilename, "\\system\\apps\\ListBox\\ListIcons.mbm"); 
	// CGulIcon packages two bitmaps: icon image and its mask
	// CAknIconArray inherits from CArrayPtrFlat<CGulIcon>
	CArrayPtr<CGulIcon>* iconList = new (ELeave) CAknIconArray(10);
	CleanupStack::PushL(iconList);

	iconList->AppendL( iEikonEnv->CreateIconL(KListBoxIconsFilename, EMbmListiconsOne13x13, EMbmListiconsOne13x13m) );
	iconList->AppendL( iEikonEnv->CreateIconL(KListBoxIconsFilename, EMbmListiconsTwo13x13, EMbmListiconsTwo13x13m) );
	iconList->AppendL( iEikonEnv->CreateIconL(KListBoxIconsFilename, EMbmListiconsThree13x13, EMbmListiconsThree13x13m) );
	iconList->AppendL( iEikonEnv->CreateIconL(KListBoxIconsFilename, EMbmListiconsAt24x24, EMbmListiconsAt24x24m));
	iconList->AppendL( iEikonEnv->CreateIconL(KListBoxIconsFilename, EMbmListiconsAt30x40, EMbmListiconsAt30x40m));

	CleanupStack::Pop();
	return iconList;
}

void ListBoxContainer::createListBoxL(EAvkonListBoxType listBoxType)
{
	delete _listBox;
	_listBox = 0;

	switch (listBoxType)
	{
		case eSingle:							_listBox = new (ELeave) CAknSingleStyleListBox; break;
		case eSingleNumber:						_listBox = new (ELeave) CAknSingleNumberStyleListBox; break;
		case eSingleHeading:					_listBox = new (ELeave) CAknSingleHeadingStyleListBox; break;
		case eSingleGraphic:					_listBox = new (ELeave) CAknSingleGraphicStyleListBox; break;
		case eSingleGraphicHeading:				_listBox = new (ELeave) CAknSingleGraphicHeadingStyleListBox; break;
		case eSingleNumberHeading:				_listBox = new (ELeave) CAknSingleNumberHeadingStyleListBox; break;
		case eSingleLarge:						_listBox = new (ELeave) CAknSingleLargeStyleListBox; break;
		case eDouble:							_listBox = new (ELeave) CAknDoubleStyleListBox; break;
		case eDouble2:							_listBox = new (ELeave) CAknDoubleStyle2ListBox; break;
		case eDoubleNumber:						_listBox = new (ELeave) CAknDoubleNumberStyleListBox; break;
		case eDoubleTime:						_listBox = new (ELeave) CAknDoubleTimeStyleListBox; break;
		case eDoubleLarge:						_listBox = new (ELeave) CAknDoubleLargeStyleListBox; break;
		case eDoubleGraphic:					_listBox = new (ELeave) CAknDoubleGraphicStyleListBox; break;
		case eFormDoubleGraphic:				_listBox = new (ELeave) CAknFormDoubleGraphicStyleListBox; break;
		case eFormDouble:						_listBox = new (ELeave) CAknFormDoubleStyleListBox; break;
		case eSetting1:					
		case eSetting2:					
		case eSetting3:					
		case eSetting4:							_listBox = new (ELeave) CAknSettingStyleListBox; break;
		case eSettingNumber:					_listBox = new (ELeave) CAknSettingNumberStyleListBox; break;
		// popup
		case eSinglePopupMenu:					_listBox = new (ELeave) CAknSinglePopupMenuStyleListBox; break;
		case eSingleGraphicPopupMenu:			_listBox = new (ELeave) CAknSingleGraphicPopupMenuStyleListBox; break;
		case eSingleGraphicBtPopupMenu:			_listBox = new (ELeave) CAknSingleGraphicBtPopupMenuStyleListBox; break;
		case eSingleHeadingPopupMenu:			_listBox = new (ELeave) CAknSingleHeadingPopupMenuStyleListBox; break;
		case eSingleGraphicHeadingPopupMenu:	_listBox = new (ELeave) CAknSingleGraphicHeadingPopupMenuStyleListBox; break;
		case eDoublePopupMenu:					_listBox = new (ELeave) CAknDoublePopupMenuStyleListBox; break;
		case eDoubleLargeGraphicPopupMenu:		_listBox = new (ELeave) CAknDoubleLargeGraphicPopupMenuStyleListBox; break;
		case eSet1:								
		case eSet2:								_listBox = new (ELeave) CAknSetStyleListBox; break;
		case eFormGraphic1:
		case eFormGraphic2:						_listBox = new (ELeave) CAknFormGraphicStyleListBox; break;
		case eFormGraphicWide1:
		case eFormGraphicWide2:					_listBox = new (ELeave) CAknFormGraphicWideStyleListBox; break;
		default:
			__ASSERT_DEBUG(false, iEikonEnv->InfoMsg(_L("False")));
			return;
	}

	_listBox->ConstructL(this, EAknListBoxMultiselectionList);	// default flags
	_listBox->SetContainerWindowL(*this);
    _listBox->SetListBoxObserver(this); // receiving notifications through HandleListBoxEventL

    _listBox->CreateScrollBarFrameL(ETrue);
    _listBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto);

	// Icon list is the same for all types:
	// Three 13x13 icons named 'One', 'Two' and 'Three' + one 24x24 icon named 'At' + one 30x40 thumbnail icon named 'At'
	// Not all the lists use icons, and not necessarily all icons are used
	if (getListBoxBaseType(EAvkonListBoxType(_currentListBoxType)) == eColumn)
	{
		CEikColumnListBox* columnListBox = static_cast<CEikColumnListBox*>(_listBox);
		columnListBox->ItemDrawer()->ColumnData()->SetIconArray(loadIconListL());
	}
	else if (getListBoxBaseType(EAvkonListBoxType(_currentListBoxType)) == eFormattedCell)
	{
		CEikFormattedCellListBox* formattedCellListBox = static_cast<CEikFormattedCellListBox*>(_listBox);
		formattedCellListBox->ItemDrawer()->FormattedCellData()->SetIconArray(loadIconListL());
		// Drawing position of icon is left.
		formattedCellListBox->ItemDrawer()->FormattedCellData()->SetSubCellAlignmentL(2, CGraphicsContext::ELeft);
	}
	else
		__ASSERT_DEBUG(false, iEikonEnv->InfoMsg(_L("False")));

	// Items could be read also from a resource file (ARRAY structure in .rss file),
	// in cases where items are not modifiable (and perhaps language-dependent)
	// CDesCArrayFlat* listBoxItems = iCoeEnv->ReadDesCArrayResourceL(R_MYAPP_LISTBOX_ITEMS);

	CDesCArray* listBoxItems = generateListBoxItemsL(EAvkonListBoxType(_currentListBoxType));

	CTextListBoxModel* model = _listBox->Model();
    model->SetItemTextArray(listBoxItems);
    model->SetOwnershipType(ELbmOwnsItemArray); // transfers ownership
}

CDesCArray* ListBoxContainer::generateListBoxItemsL(EAvkonListBoxType listBoxType) const
{
	CDesCArray* listBoxItems = new (ELeave) CDesCArrayFlat(10);	// initial num of items

	TUint NUM_OF_LISTBOX_ITEMS = 8;

	for (TUint i = 0; i < NUM_OF_LISTBOX_ITEMS; i++)
		addListBoxItemL(listBoxType, listBoxItems);

	return listBoxItems;
}

void ListBoxContainer::addListBoxItemL(EAvkonListBoxType listBoxType, CDesCArray* listBoxItems) const
{
	if (!listBoxItems)
		return;

	_LIT(KListBoxItemTextFormat,"Item%d");
	_LIT(KListBoxHeadingFormat,"Head%d");
	_LIT(KListBoxLabelFormat,"Label%d%c");	// label1a, label1b...
	_LIT(KListBoxValueFormat,"Value%d");	
	const TUint itemCount = listBoxItems->Count();

	TBuf<16> f;	// format
	getListBoxItemFormat(listBoxType, f);
	TBuf<32> item;	// final item content

	switch (listBoxType)
	{
		case eSingle: // CAknSingleStyleListBox, item text format "\tItemText\t\t"
		{
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, &itemName);
		}
		break;
		case eSingleNumber:	// CAknSingleNumberStyleListBox, "<number>\t<item text>\t\t"
		{
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, itemCount+1, &itemName);
		}
		break;
		case eSingleHeading: // CAknSingleHeadingStyleListBox, "<item heading>\t<item text>\t\t"
		{
			TBuf<16> heading;
			heading.Format(KListBoxHeadingFormat, itemCount + 1);
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, &heading, &itemName);
		}
		break;
		case eSingleGraphic: // CAknSingleGraphicStyleListBox, "<icon idx>\t<item text>\t<icon idx>\t<icon idx>"
		{
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, 0, &itemName, 1, 2);	// 0, 1, 2 = icon indexes in iconlist
		}
		break;
		case eSingleGraphicHeading:		// CAknSingleGraphicHeadingStyleListBox, "<icon idx>\t<item heading>\t<item text>\t<icon idx>\t<icon idx>"
		{
			TBuf<16> heading;
			heading.Format(KListBoxHeadingFormat, itemCount + 1);
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, 0, &heading, &itemName, 1, 2);
		}
		break;
		case eSingleNumberHeading:	// CAknSingleNumberHeadingStyleListBox, "<number>\t<item heading>\t<item text>\t\t"; check if it supports icons!
		{
			TBuf<16> heading;
			heading.Format(KListBoxHeadingFormat, itemCount + 1);
			TBuf<16> itemName;
			itemName.Format(KListBoxItemTextFormat, itemCount + 1);
			item.Format(f, itemCount+1, &heading, &itemName);
		}
		break;
		case eSingleLarge:	// CAknSingleLargeStyleListBox, "\t<item text\t"
		{

⌨️ 快捷键说明

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