#include "stdafx.h"
#include "popuplist.h"

PopupList * PopupList::CurrentPopup = NULL;
HWND PopupList::activator = NULL;

PopupList::PopupList()
{

}

PopupList::~PopupList()
{
	if(CurrentPopup==this)
		CurrentPopup = NULL;
}

int PopupList::Create(HWND parent)
{
	ShowInTaskBar = 0;
	SkinnableWindow::Create(parent);
	list.Create(hWnd);
	subtitle.Create(hWnd);
	titlebar.Create(hWnd);
	FitToContents();
	moveable = 0;
	return 1;
}

void PopupList::OnMouseEnter()
{
}

void PopupList::OnMouseLeave()
{
	
}

void PopupList::OnTimer()
{
	if(!IsChild(hWnd,env.mouse.hWnd) && env.mouse.hWnd!=hWnd && env.mouse.hWnd!=activator){
		TimeToHide--;
		if(TimeToHide==0){
			KillTimer(hWnd,0);
			Hide();
		}
	} else
		TimeToHide = 3;
}

void PopupList::StopTimer()
{
	KillTimer(hWnd,0);
}

void PopupList::StartTimer()
{
	SetTimer(hWnd,0,300,0);
	TimeToHide = 3;
}


void PopupList::OnContentsChanged(WPARAM wParam, LPARAM lParam)
{
	FitToContents();
}

void PopupList::FitToContents()
{
	SetSize(size.Width,list.location.Y+list.size.Height);
}

void PopupList::Show()
{
	if(CurrentPopup && IsWindow(CurrentPopup->hWnd))
		CurrentPopup->Hide();
	CurrentPopup = NULL;
	SkinnableWindow::Show();
	SetWindowPos(hWnd,HWND_TOP,0,0,0,0,SWP_NOACTIVATE|SWP_NOMOVE|SWP_NOSIZE);
	CurrentPopup = this;
	StartTimer();
}

void PopupList::Hide()
{
	SkinnableWindow::Hide();
	if(CurrentPopup==this)
		CurrentPopup = NULL;
}

void PopupList::OnClose()
{
	if(CurrentPopup==this)
		CurrentPopup = NULL;
}

void PopupList::Redraw()
{
	SkinnableWindow::Redraw();
	subtitle.Redraw();
	titlebar.Redraw();
}

void PopupList::SetActivator(HWND h)
{
	activator = h;
}