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

📄 mdictionaryappview1container.cpp

📁 用于symbian手机的词典工具.绝对有用。
💻 CPP
字号:
/*
* MDictionary - dictionary program for Symbian cellular phones
*
* Copyright (C) 2005 Egyeki Gergely
*
* 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 <aknsfld.h>				// for CAknSearchField
#include <AknWaitDialog.h> 			// for the wait dialog

#include "MDictionaryAppUI.h"
#include "MDictionaryAppView1.h"
#include "MDictionaryAppView1Container.h"
#include "DBHandler.h"
#include "MDictionary.hrh"
#include <MDictionary.rsg>
#include "Messages.h"

_LIT(KFormatter, "\t");
#define KNumberOfControls				2

enum TListComponentControls
{
    EListBox = 0,
	EFindBox
};


CMDictionaryAppView1Container::CMDictionaryAppView1Container():iListBox( NULL ), 
									iListBoxRows(NULL), iFindBox( NULL ),
									iSelectedText(NULL), iFilter(NULL)
{
}

// Destructor
CMDictionaryAppView1Container::~CMDictionaryAppView1Container()
{
	delete iFilter;
	iFilter = NULL;
	delete iListBox;
	iListBox = NULL;
	delete iListBoxRows;
	iListBoxRows= NULL;
    delete iFindBox;
	iFindBox = NULL;
	delete iSelectedText;
	iSelectedText = NULL;
}

void CMDictionaryAppView1Container::ConstructL(const TRect& aRect, 
											   CMDictionaryAppView1* aParent)
{
	//Create a window for the view
    CreateWindowL();
	//Set the windows size
    SetRect(aRect);
	iParent = aParent;
	popupShown = EFalse;
	//selected Text
	iSelectedText = NULL;
	iSelectedText = HBufC::NewL(KWordMaxLength);
	//the FindBox text
	iFilter = NULL;
	iFilter = HBufC::NewL(KWordMaxLength);
	TPtr aAddress( iFilter->Des() );
	aAddress = iParent->GetAppUi()->GetDocument()->iDB->GetFLetter1L()->Des();
	//ListBox
	CreateListBoxL();

	//Loading Note
	CAknWaitNoteWrapper* waitNote = CAknWaitNoteWrapper::NewL();
	CleanupStack::PushL( reinterpret_cast<CBase*>(waitNote) );
	iParent->GetAppUi()->GetDocument()->iDB->SetParameters(iFilter,0,0,0);
	waitNote->ExecuteL(
		R_LOADING_NOTE, *iParent->GetAppUi()->GetDocument()->iDB, 
		ETrue, CAknNoteDialog::ENoTone);
	CleanupStack::PopAndDestroy( waitNote );
	//reset iFilter
	aAddress = _L("");
	iStartInd = 0;
	iEndInd = KListMaxLength;

	AddItemsToListBoxL();
	DisplayFindBoxL();
	CreateFilterItemsL();

	//Activate the window, which makes it ready to be drawn
	iListBox->ActivateL();
	iListBox->DrawNow();
	iFindBox->ActivateL();
	iFindBox->DrawNow();
	SizeChanged();
	ActivateL();
}


// ---------------------------------------------------------
// CMDictionaryAppViewContainer::SizeChanged()
// Called by framework when the view size is changed
// (other items were commented in a header).
// ---------------------------------------------------------
//
void CMDictionaryAppView1Container::SizeChanged()
{
   if ( iListBox )
    {
		if ( iFindBox )
        {
			AknFind::HandleFixedFindSizeChanged(this, iListBox, iFindBox);
		}
		else
		{
			iListBox->SetRect( Rect() ); // Sets rectangle of listbox.
		}
	}
}


TInt CMDictionaryAppView1Container::CountComponentControls() const
{
    return KNumberOfControls; // return nbr of controls inside this container
}


void CMDictionaryAppView1Container::Draw(const TRect& aRect) const
{    
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.DrawRect(aRect);
}


// ---------------------------------------------------------
// CMDictionaryAppViewContainer::OfferKeyEventL(...)
// Notify key events
// ---------------------------------------------------------
//
TKeyResponse CMDictionaryAppView1Container::OfferKeyEventL(
    const TKeyEvent& aKeyEvent, TEventCode aType)
{
    if (aType != EEventKey)
    {
        return EKeyWasNotConsumed;
    }

	TInt code = aKeyEvent.iCode;
	TInt currentIndex = iListBox->CurrentItemIndex();
	TInt maxIndex = iParent->GetAppUi()->GetDocument()->iDB->iSelectList->Count() - 1;

	switch (code)
	{
        case EKeyLeftArrow:
        case EKeyRightArrow:
			return EKeyWasNotConsumed;
			break;
        case EKeyDownArrow:
			{
				if( currentIndex == KListMaxLength )
				{
					if( iEndInd < maxIndex)
					{
						iStartInd++;
						iEndInd++;
						AddItemsToListBoxL();
						for(TInt i=0; i<KListMaxLength; i++)
						{ iListBox->OfferKeyEventL( aKeyEvent, aType ); }
					}
					else
					{
						iStartInd = 0;
						iEndInd = KListMaxLength;
						AddItemsToListBoxL();
					}
				}
			}
			break;
        case EKeyUpArrow:
			{
				if( currentIndex == 0 )
				{
					if( iStartInd > 0 )
					{
						iStartInd--;
						iEndInd--;
						AddItemsToListBoxL();
						for(TInt i=0; i<KListMaxLength; i++)
						{ iListBox->OfferKeyEventL( aKeyEvent, aType ); }
					}
					else
					{
						iStartInd = maxIndex - KListMaxLength;
						iEndInd = maxIndex;
						if( iParent->GetAppUi()->GetDocument()->
							iDB->iSelectList->Count() <= KListMaxLength )
						{ iStartInd = 0; }
						AddItemsToListBoxL();
					}
				}
			}
			break;
	}

	if ( iListBox )
	{
		if ( iFindBox )
		{
			//Popup select
			if ( code == EKeyEnter || code == EKeyOK )
			{
				if ( popupShown == EFalse )
				{
					popupSelectedInd = CMessages::ShowPopUpSearchL();
					popupShown = ETrue;
				}
				if ( popupShown != EFalse )
				{
					popupShown = EFalse;
					if ( popupSelectedInd == 0 )
					{  //Search
						iParent->HandleCommandL(EMDictionarySearch1);
						return( EKeyWasConsumed );
					}
					if ( popupSelectedInd == 1 )
					{ //SearchEx
						iParent->HandleCommandL(EMDictionarySearchEx1);
						return( EKeyWasConsumed );
					}
				}
			}
			else
			{
				TBool needRefresh( EFalse );
				TBool flagsOfPopup( EFalse );

				// Offers the key event to find box.
				if ( AknFind::HandleFindOfferKeyEventL( aKeyEvent, aType, this,
                                                iListBox, iFindBox,
                                                flagsOfPopup,
                                                needRefresh ) == EKeyWasConsumed 
				)
				{
					if ( needRefresh )
					{
						AknFind::HandleFixedFindSizeChanged(this, iListBox, iFindBox);
						DrawNow();
					}
					return EKeyWasConsumed;
				}
			}//end popup
        }//end FindBox
		return iListBox->OfferKeyEventL( aKeyEvent, aType );
    }//endListBox
	return EKeyWasNotConsumed;
}





CCoeControl* CMDictionaryAppView1Container::ComponentControl(TInt aIndex) const
{
    switch ( aIndex )
    {
		case EListBox:
		{
			if (iListBox){ return iListBox;}
		}
		break;
		case EFindBox:
		{
			if (iFindBox){ return iFindBox;}
		}
		break;
		default:
            return NULL;
    }
	return NULL;
}


void CMDictionaryAppView1Container::CreateListBoxL()
    {
    iListBox = new (ELeave) CAknSingleNumberStyleListBox();
    iListBox->ConstructL(this);
	iListBox->SetContainerWindowL(*this);
	iListBox->SetRect( Rect() );

	iListBoxRows = new (ELeave) CDesC16ArraySeg( KStoreArrayLength );
	iListBox->Model()->SetItemTextArray( iListBoxRows );
	iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);

    iListBox->CreateScrollBarFrameL( ETrue );
    iListBox->ScrollBarFrame()->SetScrollBarVisibilityL (
                                                 CEikScrollBarFrame::EOff,
                                                 CEikScrollBarFrame::EAuto );
}


void CMDictionaryAppView1Container::DisplayFindBoxL()
{
    CAknFilteredTextListBoxModel* model =
		static_cast<CAknFilteredTextListBoxModel*>( iListBox->Model() );
    iFindBox = CAknSearchField::NewL( *this, CAknSearchField::ESearch,
                                      NULL, KAknExListFindBoxTextLength );

    // Creates CAknListBoxFilteredItems class.
    CleanupStack::PushL(iFindBox);
	iListBox->SetContainerWindowL(*this);
    model->CreateFilterL( iListBox, iFindBox );
    CleanupStack::Pop(iFindBox);
}


void CMDictionaryAppView1Container::CreateFilterItemsL()
{
    CAknFilteredTextListBoxModel* model =
		static_cast<CAknFilteredTextListBoxModel*>( iListBox->Model() );
	iFilterItems = model->Filter();
	iFilterItems->SetObserver(this);
}


void CMDictionaryAppView1Container::AddItemsToListBoxL()
{
    if( iListBox ) // Is listbox created?
    {
		iListBoxRows->Reset();
		TBuf<KWordMaxLength> tmp;
		for(TInt i=iStartInd; i<=iEndInd; i++)
		{
			tmp = KFormatter;
			tmp.Append(iParent->GetAppUi()->GetDocument()->
				iDB->iSelectList->MdcaPoint(i));
			iListBoxRows->AppendL( tmp );
			iListBox->HandleItemAdditionL();
		}
    }
}



//By MCoeControlObserver
void CMDictionaryAppView1Container::HandleControlEventL(CCoeControl* aControl,
														TCoeEvent aEventType)
{	//see TCoeEvent
	TPtr aAddress( iFilter->Des() );
	TBuf<KWordMaxLength> tmp;
	if( aControl == iFindBox )
	{
		if( aEventType == EEventStateChanged )
		{

			iFindBox->GetSearchText(tmp);
			if( (tmp.Length() > iFilter->Length()) || 
				(tmp.Length() < iFilter->Length()) ||
				((tmp.Length() == iFilter->Length())&&(tmp != iFilter->Des()))
			  )
			{
				if( tmp.Length() == 0 )
				{
					aAddress = iParent->GetAppUi()->GetDocument()->iDB->
						GetFLetter1L()->Des();
					iParent->GetAppUi()->GetDocument()->iDB->
						SetParameters(iFilter,0,0,0);					
					iParent->GetAppUi()->GetDocument()->iDB->ListSelectL();
					aAddress = _L("");
				}
				else
				{
					aAddress = tmp;
					iParent->GetAppUi()->GetDocument()->iDB->
						SetParameters(iFilter,0,0,0);					
					iParent->GetAppUi()->GetDocument()->iDB->ListSelectL();
				}
				iStartInd = 0;
				iEndInd = KListMaxLength;
				if( iParent->GetAppUi()->GetDocument()->
					iDB->iSelectList->Count() <= KListMaxLength )
				{
					iEndInd = iParent->GetAppUi()->GetDocument()->iDB->
						iSelectList->Count() - 1;
				}
				AddItemsToListBoxL();
			}

		}
	}
}


HBufC* CMDictionaryAppView1Container::GetWordL()
{
	TPtr aAddress( iSelectedText->Des() );
	TInt ind = iListBox->CurrentItemIndex();
	CAknFilteredTextListBoxModel* model =
		static_cast<CAknFilteredTextListBoxModel*>( iListBox->Model() );

	if (ind >= 0)
	{
		aAddress = model->ItemText(ind).Right( model->ItemText(ind).Length()-1 );
	}
	else { aAddress = _L(""); }
	return iSelectedText;
}

⌨️ 快捷键说明

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