📄 columnlistview.cpp
字号:
/*****************************************************************************
COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2005.
The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
The use of the software is subject to the terms of the end-user license
agreement which accompanies or is included with the software. The software is
provided "as is" and Sony Ericsson specifically disclaim any warranty or
condition whatsoever regarding merchantability or fitness for a specific
purpose, title or non-infringement. No warranty of any kind is made in
relation to the condition, suitability, availability, accuracy, reliability,
merchantability and/or non-infringement of the software provided herein.
*****************************************************************************/
// ColumnListViews.cpp
//
//
#include "ListViews.h"
#include "ListViews_Application.h" // KUidListViews
#include "ColumnListView.h"
#include <eiktxlbm.h>
#include <eikclbd.h>
#include <quartzkeys.h> // EQuartzKeyXxx
#include <eikmenub.h> // CEikMenuBar
#include <w32std.h> // RWindow
#include <gulicon.h> // CGulIcon
CColumnListView* CColumnListView::NewL(const TRect& aRect)
{
CColumnListView * self = new(ELeave) CColumnListView;
CleanupStack::PushL(self);
self->ConstructL(aRect);
CleanupStack::Pop();
return self;
}
CColumnListView::~CColumnListView()
{
delete iTextListEmpty;
if(iListBox)
delete iListBox;
}
void CColumnListView::ConstructL(const TRect& aRect)
{
iListBox = 0;
iDisplayList = EFalse;
iViewId.iViewUid = TUid::Uid(EListViewsCmdColumnListView);
iViewId.iAppUid = KUidListViews;
CreateWindowL();
SetRect(aRect);
// initiate iTextListEmpty - Fetch the text from the resource file.
iTextListEmpty = iEikonEnv->AllocReadResourceL(R_LISTVIEWS_LIST_EMPTY);
CreateList(EListViewsCmdColumnOpt1);
// Get the array of text items held by the list box's model
CDesCArray* array = ((CDesCArray *)iListBox->Model()->ItemTextArray());
// populate it with text items - contacts names
PopulateList(array, EListViewsCmdColumnOpt1);
// Handle the addition of items to the list box model.
iListBox->HandleItemAdditionL(); // This also redraws the list box.
// Set the item at index 0
iListBox->SetCurrentItemIndex(0);
// Set the list box control with a focus - this gives the blue highlighting
// effect shown in the pictures above.
iListBox->SetFocus(ETrue);
// iListBox->ActivateL();
}
TKeyResponse CColumnListView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (aType == EEventKey)
{
if (aKeyEvent.iCode == EQuartzKeyTwoWayDown)
{
iListBox->View()->MoveCursorL( CListBoxView::ECursorNextItem,
CListBoxView::ENoSelection);
iListBox->UpdateScrollBarsL();
return EKeyWasConsumed;
}
else if (aKeyEvent.iCode == EQuartzKeyTwoWayUp)
{
iListBox->View()->MoveCursorL( CListBoxView::ECursorPreviousItem,
CListBoxView::ENoSelection );
iListBox->UpdateScrollBarsL();
return EKeyWasConsumed;
}
}
return iListBox->OfferKeyEventL(aKeyEvent, aType);
}
void CColumnListView::HandlePointerEventL(const TPointerEvent& aPointerEvent)
{
if (aPointerEvent.iType == TPointerEvent::EButton1Down)
{
TPoint penXY = aPointerEvent.iPosition;
int yMod = (int)( ((penXY.iY-6) / iListBox->ItemHeight()) +
iListBox->View()->TopItemIndex() );
if (yMod < 0)
yMod = 0;
if ((yMod <= iListBox->View()->BottomItemIndex()) && (yMod < 236))
{
iListBox->View()->VerticalMoveToItemL(yMod,
CListBoxView::EDisjointSelection);
iListBox->UpdateScrollBarsL();
}
}
}
void CColumnListView::HandleCommandL(TInt aCommand)
{
delete iListBox;
CreateList(aCommand);
// Get the array of text items held by the list box's model
CDesCArray* array = ((CDesCArray *)iListBox->Model()->ItemTextArray());
// populate it with text items - contacts names
PopulateList(array, aCommand);
// Handle the addition of items to the list box model.
iListBox->HandleItemAdditionL(); // This also redraws the list box.
if( aCommand == EListViewsCmdColumnOpt5 ||
aCommand == EListViewsCmdColumnOpt6 ||
aCommand == EListViewsCmdColumnOpt7 )
LoadGraphicsL();
// Set the item at index 0
iListBox->SetCurrentItemIndex(0);
// Set the list box control with a focus - this gives the blue highlighting
// effect shown in the pictures above.
iListBox->SetFocus(ETrue);
iListBox->ActivateL();
iListBox->DrawNow();
}
TVwsViewId CColumnListView::ViewId() const
{
return iViewId;
}
void CColumnListView::Draw(const TRect& /*aRect*/) const
{
// Window graphics context
CWindowGc& gc = SystemGc();
// Start with a clear screen
gc.Clear();
TRect rect = Rect();
const CFont* font = iEikonEnv->TitleFont();
gc.UseFont(font);
TInt baseline = rect.Height()/2 - font->AscentInPixels()/2;
gc.DrawText(*iTextListEmpty, rect, baseline, CGraphicsContext::ECenter);
gc.DiscardFont();
}
TInt CColumnListView::CountComponentControls() const
{
if(iListBox && iDisplayList)
return 1;
else
return 0;
}
CCoeControl* CColumnListView::ComponentControl(TInt aIndex) const
{
switch (aIndex)
{
case 0:
return iListBox;
default:
return 0;
}
}
void CColumnListView::ViewActivatedL( const TVwsViewId& /*aPrevViewId*/,
TUid /*aCustomMessageId*/,
const TDesC8& /*aCustomMessage*/ )
{
ActivateL();
MEikAppUiFactory* factory = iEikonEnv->AppUiFactory();
factory->MenuBar()->ChangeMenuBarL(0,
R_LISTVIEWS_MENUBAR_COLUMN_LISTBOX,
EFalse);
MakeVisible(ETrue);
}
void CColumnListView::ViewDeactivated()
{
MakeVisible(EFalse);
}
void CColumnListView::CreateList(TInt aFlag)
{
// Construct a list box
iListBox = new (ELeave) CEikColumnListBox;
// flag can be different
switch(aFlag)
{
case EListViewsCmdColumnOpt1 :
case EListViewsCmdColumnOpt3 :
case EListViewsCmdColumnOpt5 :
case EListViewsCmdColumnOpt6 :
iListBox->ConstructL(this, 0);
break;
case EListViewsCmdColumnOpt2 :
case EListViewsCmdColumnOpt4 :
case EListViewsCmdColumnOpt7 :
iListBox->ConstructL(this, CEikListBox::EMultipleSelection);
break;
}
// set this view as the parent of the control
iListBox->SetContainerWindowL( *this );
// set look and behaviour:
CColumnListBoxData* listBoxColumns =
((CColumnListBoxItemDrawer*)iListBox->ItemDrawer())->ColumnData();
// - set content, columns of text
switch(aFlag)
{
case EListViewsCmdColumnOpt1 :
case EListViewsCmdColumnOpt2 :
listBoxColumns->SetColumnWidthPixelL(0, 200);
listBoxColumns->SetColumnAlignmentL(0, CGraphicsContext::ELeft);
break;
case EListViewsCmdColumnOpt3 :
case EListViewsCmdColumnOpt4 :
// 1st column
listBoxColumns->SetColumnWidthPixelL(0, 70);
listBoxColumns->SetColumnAlignmentL(0, CGraphicsContext::ELeft);
// 2nd column
listBoxColumns->SetColumnWidthPixelL(1, 200);
listBoxColumns->SetColumnAlignmentL(1, CGraphicsContext::ELeft);
break;
case EListViewsCmdColumnOpt5 :
// 1st column
listBoxColumns->SetGraphicsColumnL(0, ETrue);
listBoxColumns->SetColumnWidthPixelL(0, int(Size().iWidth * 0.15));
listBoxColumns->SetColumnAlignmentL(0, CGraphicsContext::ELeft);
// 2nd column
listBoxColumns->SetColumnWidthPixelL(1, int(Size().iWidth * 0.70));
listBoxColumns->SetColumnAlignmentL(1, CGraphicsContext::ELeft);
break;
case EListViewsCmdColumnOpt6 :
case EListViewsCmdColumnOpt7 :
// 1st column
listBoxColumns->SetGraphicsColumnL(0, ETrue);
listBoxColumns->SetColumnWidthPixelL(0, int(Size().iWidth * 0.15));
listBoxColumns->SetColumnAlignmentL(0, CGraphicsContext::ELeft);
// 2nd column
listBoxColumns->SetGraphicsColumnL(1, ETrue);
listBoxColumns->SetColumnWidthPixelL(1, int(Size().iWidth * 0.15));
listBoxColumns->SetColumnAlignmentL(1, CGraphicsContext::ELeft);
// 3nd column
listBoxColumns->SetColumnWidthPixelL(2, int(Size().iWidth * 0.70));
listBoxColumns->SetColumnAlignmentL(2, CGraphicsContext::ELeft);
break;
}
// - set scrolling capabilities
iListBox->CreateScrollBarFrameL();
iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EAuto,
CEikScrollBarFrame::EAuto);
// - set listbox bordes
iListBox->SetBorder(TGulBorder::EDeepRaisedWithOutline);
// - set position
TPoint p;
TSize sz;
p.iX = 0;
p.iY = 0;
sz.iWidth = 208;
sz.iHeight = 240; // max 255
iListBox->SetExtent(p, sz);
// - set text color
iListBox->View()->ItemDrawer()->SetTextColor(KRgbDarkMagenta);
// set font
iListBox->ItemDrawer()->SetFont(iEikonEnv->AnnotationFont());
}
// this function fills list with all contact names, sorted alphabetically
void CColumnListView::PopulateList(CDesCArray *aArray, TInt aFlag)
{
//item.Format(_L("\t%d %S"), 0, &KItemName);
// put names into array
switch(aFlag)
{
case EListViewsCmdColumnOpt1 :
case EListViewsCmdColumnOpt2 :
{
aArray->AppendL(_L("Jenny Annie"));
aArray->AppendL(_L("Chris Stoll"));
aArray->AppendL(_L("David Mine"));
aArray->AppendL(_L("Johnny Longname"));
aArray->AppendL(_L("Mike Henry"));
aArray->AppendL(_L("Susan"));
aArray->AppendL(_L("Gooch"));
aArray->AppendL(_L("Steve"));
aArray->AppendL(_L("Paul Hasverylongnameman"));
aArray->AppendL(_L("Cindy Crawford"));
aArray->AppendL(_L("Greg O'Hara"));
aArray->AppendL(_L("Erik Rednose"));
aArray->AppendL(_L("Kenny"));
aArray->AppendL(_L("Kyle"));
aArray->AppendL(_L("Bennet Mill"));
aArray->AppendL(_L("Justin"));
aArray->AppendL(_L("Ridiculously long test name used to see if my code works."));
} break;
case EListViewsCmdColumnOpt3 :
case EListViewsCmdColumnOpt4 :
{
aArray->AppendL(_L("Hi\tJenny Annie"));
aArray->AppendL(_L("Welcome\tChris Stoll"));
aArray->AppendL(_L("in\tDavid Mine"));
aArray->AppendL(_L("column\tJohnny Longname"));
aArray->AppendL(_L("list\tMike Henry"));
aArray->AppendL(_L("box\tSusan"));
aArray->AppendL(_L("These\tGooch"));
aArray->AppendL(_L("one\tSteve"));
aArray->AppendL(_L("have\tPaul Hasverylongnameman"));
aArray->AppendL(_L("two\tCindy Crawford"));
aArray->AppendL(_L("columns\tGreg O'Hara"));
aArray->AppendL(_L("\tErik Rednose"));
aArray->AppendL(_L("as\tKenny"));
aArray->AppendL(_L("you\tKyle"));
aArray->AppendL(_L("see\tBennet Mill"));
aArray->AppendL(_L("\tJustin"));
aArray->AppendL(_L("\tRidiculously long test name used to see if my code works."));
} break;
case EListViewsCmdColumnOpt5 :
{
aArray->AppendL(_L("0\tJenny Annie"));
aArray->AppendL(_L("1\tChris Stoll"));
aArray->AppendL(_L("0\tDavid Mine"));
aArray->AppendL(_L("1\tJohnny Longname"));
aArray->AppendL(_L("1\tMike Henry"));
aArray->AppendL(_L("0\tSusan"));
aArray->AppendL(_L("0\tGooch"));
aArray->AppendL(_L("1\tSteve"));
aArray->AppendL(_L("1\tPaul Hasverylongnameman"));
aArray->AppendL(_L("0\tCindy Crawford"));
aArray->AppendL(_L("0\tGreg O'Hara"));
aArray->AppendL(_L("1\tErik Rednose"));
aArray->AppendL(_L("1\tKenny"));
aArray->AppendL(_L("1\tKyle"));
aArray->AppendL(_L("0\tBennet Mill"));
aArray->AppendL(_L("0\tJustin"));
aArray->AppendL(_L("0\tRidiculously long test name used to see if my code works."));
} break;
case EListViewsCmdColumnOpt6 :
case EListViewsCmdColumnOpt7 :
{
aArray->AppendL(_L("0\t0\tJenny Annie"));
aArray->AppendL(_L("1\t0\tChris Stoll"));
aArray->AppendL(_L("0\t1\tDavid Mine"));
aArray->AppendL(_L("1\t0\tJohnny Longname"));
aArray->AppendL(_L("1\t0\tMike Henry"));
aArray->AppendL(_L("0\t1\tSusan"));
aArray->AppendL(_L("0\t0\tGooch"));
aArray->AppendL(_L("1\t1\tSteve"));
aArray->AppendL(_L("1\t0\tPaul Hasverylongnameman"));
aArray->AppendL(_L("0\t1\tCindy Crawford"));
aArray->AppendL(_L("0\t0\tGreg O'Hara"));
aArray->AppendL(_L("1\t1\tErik Rednose"));
aArray->AppendL(_L("1\t0\tKenny"));
aArray->AppendL(_L("1\t1\tKyle"));
aArray->AppendL(_L("0\t0\tBennet Mill"));
aArray->AppendL(_L("0\t1\tJustin"));
aArray->AppendL(_L("0\t0\tRidiculously long test name used to see if my code works."));
} break;
}
iDisplayList = ETrue;
}
void CColumnListView::LoadGraphicsL()
{
TBuf< 256 > pathAppMbm;
iCoeEnv->ReadResource( pathAppMbm, R_LISTVIEWS_PATH_ICONS_MBM );
CArrayPtr<CGulIcon>* icons = new (ELeave) CArrayPtrFlat<CGulIcon>(2);
CleanupStack::PushL( icons );
// here, _L("*") could be used instead of pathAppMbm
icons->AppendL(iEikonEnv->CreateIconL(pathAppMbm, 1, 0));
icons->AppendL(iEikonEnv->CreateIconL(pathAppMbm, 2, 0));
// Set icon array.
iListBox->ItemDrawer()->ColumnData()->SetIconArray( icons );
CleanupStack::Pop(); // icons array
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -