#include "StdAfx.h"
#include "listitem.h"

ListItem::ListItem(void)
{
	type = ListItemType;
	prev = NULL;
	next = NULL;
	userdata = NULL;
	usertype = 0;
}

ListItem::~ListItem(void)
{
}

void ListItem::Set(Appearance& x){
	fontname = x.fontname;
	fontsize = x.fontsize;
	textlocation = x.textlocation;
	normal = x.normal;
	selected = x.selected;
	mouseover = x.mouseover;
	pressed = x.pressed;
	size.Height = x.size.Height;
}

bool ListItem::Selected()
{
	return state==AppearanceState::selected;
}

void ListItem::Draw(Graphics& graphics)
{
	try {
		// Create a Rect object.
		Rect fillRect(Point(0,0),size);
		
		if(current->bgcolor.GetAlpha()>0) {
			// Create a SolidBrush object.
			SolidBrush bgbrush(current->bgcolor);

			// Fill the rectangle.
			graphics.FillRectangle(&bgbrush, fillRect);
		} 

		if(current->image){
			DrawTransparentImage(&graphics,
				current->image,
				current->imagex,
				current->imagey,
				current->image->GetWidth(),
				current->image->GetHeight());
		}

		if(current->borderwidth>0){
			// Create a Pen object.
			Pen borderpen(current->bordercolor, current->borderwidth);

			// Draw the rectangle that bounds the image.
			fillRect.Width--;
			fillRect.Height--;
			graphics.DrawRectangle(&borderpen, fillRect);
		}

		if(current->drawtext){
			// draw the text
			FontFamily   fontFamily(fontname.data());
			Font         font(&fontFamily, fontsize, FontStyleRegular, UnitPoint);
			SolidBrush brush(current->textcolor);
			RectF        rectF(textlocation,SizeF(size.Width-textlocation.X-1,size.Height-textlocation.Y-1));
			StringFormat stringFormat;
			// left-justify each line of text.
			stringFormat.SetAlignment(StringAlignmentNear);
			stringFormat.SetLineAlignment(StringAlignmentNear);
			graphics.DrawString(title.data(), -1, &font, rectF, &stringFormat, &brush);
		}

		std::list<Appearance *>::iterator i;
		for(i=visuals.begin();i!=visuals.end();i++){
			(*i)->Draw(graphics);
		}

	} catch(...) {
		print(L"Error drawing list item.\n");
		return;
	}
}

void ListItem::OnMouseEnter()
{
	SkinnableButton::OnMouseEnter();
	SafeSendMessage(hWndParent,ID_POPUPSTART,(WPARAM)this,0);
	if(hWndSink)
		SafeSendMessage(hWndSink,ID_POPUPSTART,(WPARAM)this,0);
}

void ListItem::OnMouseLeave()
{
	SkinnableButton::OnMouseLeave();
	SafeSendMessage(hWndParent,ID_POPUPEND,(WPARAM)this,0);
	if(hWndSink)
		SafeSendMessage(hWndSink,ID_POPUPEND,(WPARAM)this,0);
}

void ListItem::OnKeyDown(WPARAM wParam, LPARAM lParam)
{
	switch (wParam) 
	{ 

	case VK_UP: 

		// Process the UP ARROW key. 
		if(prev)
			SetFocus(prev->hWnd);
		break; 

	case VK_DOWN: 

		// Process the DOWN ARROW key. 
		if(next)
			SetFocus(next->hWnd);

		break; 

	case VK_HOME: 

		// Process the HOME key. 
		
		break; 

	case VK_END: 

		// Process the END key. 

		break; 

	case VK_DELETE: 

		// Process the DEL key. 
		SafeSendMessage(hWndParent,ID_LIST_DELETE,(WPARAM)0,(LPARAM)this);
		if(hWndSink)
			SafeSendMessage(hWndSink,ID_LIST_DELETE,(WPARAM)0,(LPARAM)this);
		break; 


		// Process other non-character keystrokes. 

	default: 
		SkinnableWindow::OnKeyDown(wParam,lParam);
		break; 
	}
}

void ListItem::AdjustHeightToText()
{
	SetSize(size.Width,MeasureTextHeight(fontname.data(),fontsize,
			title.data(),size.Width-textlocation.X-1));
}