/*!	\function		CategoryListView::SetScroller
 *	\brief			Obtain access to scroll view that is used to scroll *this.
 *	\details		Unsetting the scroll view is performed by submitting NULL.
 *	\param[in]	scrollViewIn	The BScrollView that is used to scroll current view.
 *	\note			Note on ownership of the BScrollView:
 *					This class takes care of updating the scrollbars etc., but
 *					it doesn't own the BScrollView. The creator of CategoryListView
 *					is responsible for deleting the BScrollView when it's not needed.
 */
void	CategoryListView::SetScroller( BScrollView* scrollViewIn )
{
	this->scrollView = scrollViewIn;
	if ( !this->scrollView ) {
		((BScrollBar*)(scrollView->ScrollBar(B_VERTICAL)))->SetSteps(5, 20);
		((BScrollBar*)(scrollView->ScrollBar(B_HORIZONTAL)))->SetSteps(5, 20);
		
		BListView::TargetedByScrollView( scrollViewIn );
		
		FixupScrollbars();
	}
}	// <-- end of function CategoryListView::SetScroller
/*!
 *	\brief			Called when the parent view is resized.
 *	\details		Main usage of this function is to fix the scrollbars.
 *	\param[in]	width	New width of the view.
 *	\param[in]	height	New height of the view.
 *	\note			Note on the scrollbars:
 *					It's assumed that this view is a target of a BScrollView. It's
 *					resized in the way which makes space for the scrollbars. If it
 *					knows about a BScrollView that targets this view, it resizes that
 *					BScrollView accordingly (thus the owner shouldn't do it).
 */
void	CategoryListView::FrameResized( float width, float height )
{
	// Firstly, resize the base class.
	BView::FrameResized( width - B_V_SCROLL_BAR_WIDTH,
						 height - B_H_SCROLL_BAR_HEIGHT );

	// Now resize the BScrollView
	if ( this->scrollView )
	{
		FixupScrollbars();
	}
}	// <-- end of function "CategoryListView::FrameResized"
/*!	\function		CategoryListView::FrameResized
 *	\brief			Called when the parent view is resized.
 *	\details		Main usage of this function is to fix the scrollbars.
 *	\param[in]	width	New width of the view.
 *	\param[in]	height	New height of the view.
 *	\note			Note on the scrollbars:
 *					It's assumed that this view is a target of a BScrollView. It's
 *					resized in the way which makes space for the scrollbars. If it
 *					knows about a BScrollView that targets this view, it resizes that
 *					BScrollView accordingly (thus the owner shouldn't do it).
 */
void	CategoryListView::FrameResized( float width, float height )
{
	// Firstly, resize the base class.
	BView::FrameResized( width,
						 height );

	// Now resize the BScrollView
	if ( this->scrollView )
	{
		(this->scrollView)->ResizeTo( width, height );
		
		FixupScrollbars();
	}
	
}	// <-- end of function "CategoryListView::FrameResized"
/*!	\brief		This function is called when there's a scroller around CategoryListView.
 *	\details	Does nothing special; just adjusts the scrollbars' size to current contents
 *				and enables / disables them if needed.
 * 	\param[in]	scroller	The BScrollView that targets current object.
 *	\note		Note on ownership of the BScrollView:
 *				This class takes care of updating the scrollbars etc., but
 *				it doesn't own the BScrollView. The creator of CategoryListView
 *				is responsible for deleting the BScrollView when it's not needed.
 */
void CategoryListView::TargetedByScrollView( BScrollView* scroller )
{
	BScrollBar* sb = NULL;
	this->scrollView = scroller;
	
	if ( scroller ) {
		sb = scroller->ScrollBar( B_VERTICAL );
		if ( sb )
		{
			sb->SetSteps( 5, 20 );
		}
		
		sb = scroller->ScrollBar( B_HORIZONTAL );
		if ( sb )
		{
			sb->SetSteps( 5, 20 );
		}
		
		BListView::TargetedByScrollView( scroller );
		
		FixupScrollbars();
	}
}	// <-- end of function CategoryListView::TargetedByScrollView
/*!	\brief		Function that adds an item to the list.
 *	\details	If the item is "Default", it will be added first.
 *				If it's an update of an existing item, it will be updated.
 *				If it's a new item, it will be added in alphabetical order.
 */
bool	CategoryListView::AddItem( CategoryListItem *toAdd )
{
	if ( !toAdd ) { return false; }
	
	BString testString("Default"), toAddLabel( toAdd->GetLabel() );
	CategoryListItem *testItem;
	bool toReturn = false;
	int index = 0, limit = this->CountItems();
	
	// Is it the "Default" item?
	if ( toAddLabel == testString )
	{
		// Well, yes, it is.
		testItem = dynamic_cast< CategoryListItem* >( ItemAt( 0 ) );
		
		if ( ( limit == 0 ) || ( testItem && ( testItem->GetLabel() != testString ) ) )
		{
			// Either adding the first item, 
			//	or the first item is not a CategoryListItem,
			//	or it is a CategoryListItem, but not "Default".
			
			toReturn = BListView::AddItem( toAdd, 0 );
			FixupScrollbars();
			return toReturn;
		}
		else if ( !testItem )
		{
			// The first item is not a CategoryListItem.
			toReturn = BListView::AddItem( toAdd, 0 );
			FixupScrollbars();
			return toReturn;
		}
		else 
		{
			// The first item is a "Default" item. Update its color.
			testItem->UpdateColor( toAdd->GetColor() );
			delete toAdd;
			toAdd = NULL;
			InvalidateItem( 0 );
			return true;
		}		
	}
	
	// Well, it's not a "Default" item.
	index = 0;
	while ( index < limit )
	{
		testItem = 	dynamic_cast< CategoryListItem* >( ItemAt( index ) );
		
		if ( ! testItem )
		{
			++index;
			continue;
		}
		
		testString = testItem->GetLabel();
		
		// Jump over "Default" leaving it at the top.
		if ( testString == "Default" )
		{
			++index;
			continue;
		}
		
		// Jump over items that are alphabetically lower.
		if ( testString < toAddLabel )
		{
			++index;
			continue;
		}
		
		// If encountered item with the same label, update its color.
		if ( testString == toAddLabel )
		{
			testItem->UpdateColor( toAdd->GetColor() );
			delete toAdd;
			toAdd = NULL;
			InvalidateItem( index );
			return true;
		}
		
		// Found the place to insert current item
		if ( testString > toAddLabel )
		{
			toReturn = BListView::AddItem( toAdd, index );
			FixupScrollbars();
			return toReturn;
		}
		
		++index;
	}
	
	// If we got here, then all items are alphabetically less.
	toReturn = BListView::AddItem( toAdd );
	FixupScrollbars();
	return toReturn;

}	// <-- end of function CategoryListView::AddItem