#include "StdAfx.h"
#include "DateBox.h"
#include "commctrl.h"

DateBox::DateBox()
{
	type = DateBoxType;
	CalendarIsVisible = 0;
}

DateBox::~DateBox()
{

}

static HWND CreateDatePicker(HWND parent)
{
	INITCOMMONCONTROLSEX icex;

	icex.dwSize = sizeof(icex);
	icex.dwICC = ICC_DATE_CLASSES;

	InitCommonControlsEx(&icex);

	HWND hwndDP = CreateWindowEx(0,
							MONTHCAL_CLASS,
							L"DateTime",
							WS_CHILD,
							20,50,220,20,
							parent,
							NULL,
							env.hInstance,
							NULL);

	// Get the size required to show an entire month.
	RECT rc;
	MonthCal_GetMinReqRect(hwndDP, &rc);

	// Resize the control now that the size values have been obtained.
	SetWindowPos(hwndDP, NULL, 0, 0, 
			rc.right, rc.bottom,
			SWP_NOZORDER|SWP_NOACTIVATE);

	// Set colors for aesthetics.
	MonthCal_SetColor(hwndDP, MCSC_BACKGROUND, RGB(175,175,175));
	MonthCal_SetColor(hwndDP, MCSC_MONTHBK, RGB(248,245,225));
	
	return hwndDP;
}

void DateBox::Load(const char * b)
{
	_bstr_t base = b;
	SkinnableWindow::Load(b);
	text.Load(base+"::text");
	button.Load(base+"::button");
	_tcscpy(DateFormat,env.GetWideString(b,"dateformat",L"%m/%d/%y %I:%M %p"));
}

int DateBox::Create(HWND parent)
{
	SkinnableWindow::Create(parent);
	text.Create(hWnd);
	button.Create(hWnd);
	hDP = CreateDatePicker(parent);
	SetWindowRgn(hWnd,CollapsedRgn(),TRUE);
	date = COleDateTime::GetCurrentTime();
	text.SetText(date.Format(DateFormat));
	return 1;
}

void DateBox::OnButton(HWND h)
{
	if(h==button.hWnd){
		if(CalendarIsVisible){
			ShowWindow(hDP,SW_HIDE);
			//SetWindowRgn(hWnd,CollapsedRgn(),TRUE);
		} else {
			POINT pt;
			pt.x = location.X+text.location.X;
			pt.y = location.Y+text.location.Y+text.size.Height;
			SetWindowPos(hDP,NULL,pt.x,pt.y,0,0,SWP_NOSIZE|SWP_NOZORDER);
			ShowWindow(hDP,SW_SHOW);
			BringWindowToTop(hDP);
			//SetWindowRgn(hWnd,ExpandedRgn(),TRUE);
		}
		CalendarIsVisible = !CalendarIsVisible;
	}
}


HRGN DateBox::CollapsedRgn()
{
	HRGN rgn1 = CreateRectRgn(text.location.X,
		text.location.Y,
		text.location.X+text.size.Width,
		text.location.Y+text.size.Height);
	HRGN rgn2 = CreateRectRgn(button.location.X,
		button.location.Y,
		button.location.X+button.size.Width,
		button.location.Y+button.size.Height);

	CombineRgn(rgn1,rgn1,rgn2,RGN_OR);
	DeleteObject(rgn2);
	return rgn1;
}

HRGN DateBox::ExpandedRgn()
{
	HRGN rgn1 = CreateRectRgn(text.location.X,
		text.location.Y,
		text.location.X+text.size.Width,
		text.location.Y+text.size.Height);
	HRGN rgn2 = CreateRectRgn(button.location.X,
		button.location.Y,
		button.location.X+button.size.Width,
		button.location.Y+button.size.Height);

	RECT rc;
	GetClientRect(hDP,&rc);
	HRGN rgn3 = CreateRectRgn(text.location.X,
		text.location.Y+text.size.Height,
		text.location.X+rc.right,
		text.location.Y+text.size.Height+rc.bottom);

	CombineRgn(rgn1,rgn1,rgn2,RGN_OR);
	CombineRgn(rgn1,rgn1,rgn3,RGN_OR);
	DeleteObject(rgn2);
	DeleteObject(rgn3);
	return rgn1;
}

void DateBox::OnNotify(int idCtrl, NMHDR * pnmh)
{
	if(pnmh->hwndFrom!=hDP) return;

	switch(pnmh->code){
		case MCN_SELECT:
			OnButton(button.hWnd);
		case MCN_SELCHANGE:
			// update the text box with the new date
			SYSTEMTIME st;
			memset(&st,0,sizeof(SYSTEMTIME));
			MonthCal_GetCurSel(hDP,&st);
			date.SetDate(st.wYear,st.wMonth,st.wDay);
			text.SetText(date.Format(DateFormat));
			break;
	}
}

void DateBox::OnContentsChanged(WPARAM wParam, LPARAM lParam)
{
	std::wstring buffer;
	text.GetText(buffer);
	date.ParseDateTime(buffer.data(),VAR_DATEVALUEONLY);
	SYSTEMTIME st;
	date.GetAsSystemTime(st);
	MonthCal_SetCurSel(hDP,&st);
}

void DateBox::SetValue(COleDateTime & d)
{
	date.SetDate(d.GetYear(),d.GetMonth(),d.GetDay());
	text.SetText(date.Format(DateFormat));
	SYSTEMTIME st;
	date.GetAsSystemTime(st);
	MonthCal_SetCurSel(hDP,&st);
}

void DateBox::Hide()
{
	if(CalendarIsVisible){
		ShowWindow(hDP,SW_HIDE);
		//SetWindowRgn(hWnd,CollapsedRgn(),TRUE);
		CalendarIsVisible = 0;
	}
	SkinnableWindow::Hide();
}

void DateBox::OnMouseCaptureChanged()
{
	if(CalendarIsVisible){
		ShowWindow(hDP,SW_HIDE);
		SetWindowRgn(hWnd,CollapsedRgn(),TRUE);
		CalendarIsVisible = 0;
	} 
}
