BOOL CXTPCalendarWeekViewDay::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (!GetView() || !GetCalendarControl())
	{
		ASSERT(FALSE);
		return FALSE;
	}

	if (GetView()->GetTheme())
	{
		if (XTP_SAFE_GET4(GetView(), GetTheme(), GetWeekViewPart(), GetDayPart(),
			OnLButtonDown(this, nFlags, point), FALSE))
		{
			return TRUE;
		}
	}
	else if (m_Layout.m_rcExpandSign.PtInRect(point))
	{
		if (UserAction_OnExpandDay(xtpCalendarExpandDayButton_WeekView))
			return TRUE;

		XTP_SAFE_CALL1(GetCalendarControl(), QueueDayViewSwitch(GetDayDate()));
		return TRUE;
	}

	return TBase::OnLButtonDown(nFlags, point);
}
void CXTPCalendarViewDay::OnPrePopulateDay()
{
	WPARAM wParam = (WPARAM)(CXTPCalendarViewDay*)this;

	XTP_SAFE_CALL2(GetView_(), GetCalendarControl(), SendNotification(
		XTP_NC_CALENDAR_PREPOPULATE_DAY, wParam, 0));
}
BOOL CXTPCalendarViewDay::UserAction_OnExpandDay(XTPCalendarExpandDayButton eButton)
{
	XTP_CALENDAR_USERACTION_PARAMS uaParams;
	::ZeroMemory(&uaParams, sizeof(uaParams));

	uaParams.m_eAction = xtpCalendarUserAction_OnExpandDay;
	uaParams.m_ExpandDayButton.nButton = eButton;
	uaParams.m_ExpandDayButton.pViewDay = this;

	BOOL bCancel = FALSE;

	if (GetCalendarControl())
		GetCalendarControl()->SendNotification(XTP_NC_CALENDAR_USERACTION, (WPARAM)&uaParams, (LPARAM)&bCancel);

	return bCancel;
}
CString CXTPCalendarViewGroup::GetCaption()
{
	if (!m_pResources)
	{
		return _T("");
	}

	//BOOL bShowAll = TRUE;
	CString strCaption;

	int nResCount = m_pResources->GetCount();
	for (int i = 0; i < nResCount; i++)
	{
		if (m_pResources->GetAt(i) /* && m_pResources->GetAt(i)->GetSchedules()*/)
		{
			if (!strCaption.IsEmpty())
			{
				strCaption += _T(", ");
			}
			strCaption += m_pResources->GetAt(i)->GetName();
		}
	}

	WPARAM wParam = (WPARAM)this;
	LPARAM lParam = (LPARAM)(CString*)&strCaption;
	XTP_SAFE_CALL3(GetViewDay_(), GetView_(), GetCalendarControl(),
		SendNotification(XTP_NC_CALENDAR_GETGROPVIEWCAPTION, wParam, lParam));

	return strCaption;
}
void CXTPCalendarViewGroup::OnPrePopulate(CXTPCalendarEvents* pEvents)
{
	WPARAM wParam = (WPARAM)(CXTPCalendarViewGroup*)this;
	LPARAM lParam = (LPARAM)pEvents;

	XTP_SAFE_CALL3(GetViewDay_(), GetView_(), GetCalendarControl(), SendNotification(
		XTP_NC_CALENDAR_PREPOPULATE, wParam, lParam));
}
CXTPCalendarControl* CXTPCalendarViewDay::GetCalendarControl() const
{
	ASSERT(this);
	if (!this)
	{
		return NULL;
	}
	return XTP_SAFE_GET1(GetView_(), GetCalendarControl(), NULL);
}
CString CXTPCalendarWeekViewDay::GetCaption()
{
	if (!GetView() || !GetCalendarControl())
	{
		ASSERT(FALSE);
		return _T("");
	}

	CString strHeaderFormat = GetView()->GetDayHeaderFormat();

	if (strHeaderFormat == _T("MMMM d"))
		strHeaderFormat = _T("dddd MMMM dd");
	else if (strHeaderFormat == _T("d MMMM"))
		strHeaderFormat = _T("dddd dd MMMM");

	SYSTEMTIME st;
	COleDateTime dtDay = GetDayDate();
	CXTPCalendarUtils::GetAsSystemTime(dtDay, st);
	CString strDate = CXTPCalendarUtils::GetDateFormat(&st, strHeaderFormat);

	//-------------------------------------------------------------
	DWORD dwFlags = GetCalendarControl()->GetAskItemTextFlags();

	if (dwFlags & xtpCalendarItemText_WeekViewDayHeader)
	{
		XTP_CALENDAR_GETITEMTEXT_PARAMS objRequest;
		::ZeroMemory(&objRequest, sizeof(objRequest));

		objRequest.nItem = (int)xtpCalendarItemText_WeekViewDayHeader;
		objRequest.pstrText = &strDate;
		objRequest.pViewDay = this;

		GetCalendarControl()->SendNotificationAlways(XTP_NC_CALENDAR_GETITEMTEXT,
													(WPARAM)&objRequest, 0);
	}
	//-------------------------------------------------------------

	return strDate;
}
void CXTPCalendarWeekView::AdjustFirstDayOfWeek()
{
	if (!GetCalendarControl())
	{
		return;
	}

	int nFDPrev = m_nFirstDayOfWeekIndex;

	//- Adjust First day of Week ------------------------------------------
	m_nFirstDayOfWeekIndex = GetCalendarControl()->GetFirstDayOfWeek();
	//Allow Sunday as 1st day of week
	//if (m_nFirstDayOfWeekIndex == 1)
	//  m_nFirstDayOfWeekIndex = 2;

	int nDShift = m_nFirstDayOfWeekIndex - nFDPrev;
	COleDateTime dtBeginDate_prev = GetBeginDate();
	COleDateTime dtBeginDate = dtBeginDate_prev + COleDateTimeSpan(nDShift);

	SetBeginDate(dtBeginDate);

	dtBeginDate = GetBeginDate();
}
CRect CXTPCalendarWeekViewDay::GetDayEventsRect() const
{
	int nColHeaderHeight = XTP_SAFE_GET1(GetView(), GetRowHeight(), 0);
	CRect rcDayEvents = m_Layout.m_rcDay;
	//rcDayEvents.bottom -= min(2, rcDayEvents.Height());
	rcDayEvents.top += min(nColHeaderHeight + 0, rcDayEvents.Height());
	int nBotSpace = XTP_SAFE_GET5(GetView(), GetCalendarControl(), GetTheme(),
		GetWeekViewPart(), GetDayPart(), GetExpandButtonHeight(), 0);

	rcDayEvents.bottom -= min(nBotSpace + 2, rcDayEvents.Height());


	return rcDayEvents;
}
void CXTPCalendarDayViewTimeScale::AdjustLayout(CDC* pDC)
{
	UNREFERENCED_PARAMETER(pDC);

	ASSERT(pDC);
	ASSERT(m_pDayView);
	if (!m_pDayView || !pDC)
	{
		return;
	}

	CRect rcAllDayEvents = m_pDayView->GetAllDayEventsRectangle();

	m_Layout.m_nXPosBase = m_Layout.m_rcHeader.left;

	m_Layout.m_rcTimeZone = m_Layout.m_rcHeader;
	m_Layout.m_rcTimeZone.top = rcAllDayEvents.bottom;

	//----------------------------------------------------
	int nNowLineFlags = XTP_SAFE_GET3(m_pDayView, GetCalendarControl(), GetCalendarOptions(), nDayView_CurrentTimeMarkVisible, 0);

	m_bDrawNowLineDyn = m_bDrawNowLine;
	if (m_bDrawNowLineDyn)
	{
		if (m_pDayView->IsTodayVisible())
		{
			int nTAMask = xtpCalendarCurrentTimeMarkVisibleForToday | xtpCalendarCurrentTimeMarkVisibleAlways;
			m_bDrawNowLineDyn = m_bDrawNowLineDyn && (nNowLineFlags & nTAMask);
		}
		else
		{
			m_bDrawNowLineDyn = m_bDrawNowLineDyn && (nNowLineFlags & xtpCalendarCurrentTimeMarkVisibleAlways);
		}

		if (pDC->IsPrinting())
		{
			m_bDrawNowLineDyn = m_bDrawNowLineDyn && (nNowLineFlags & xtpCalendarCurrentTimeMarkPrinted);
		}
	}
}
COleDateTime CXTPCalendarTLV_TimeScaleParams::CalcScaleIntervalBegin(
														COleDateTime dtStart)
{
	if (!m_scaleInterval.m_nValue)
	{
		ASSERT(FALSE);
		return dtStart;
	}

	COleDateTime dtBegin = dtStart;

	int nFirstDayOfWeekIndex = 0;
	COleDateTimeSpan spAdd;

	LONGLONG nVal = 0;
	LONGLONG nDiv = 0;
	//int nMod = 0;
	LONGLONG nNewVal = 0;

	LONGLONG nllDays_total = (int)(DATE)dtStart;

	switch (m_scaleInterval.m_eType)
	{
	case xtpDTScale_Sec:
		nVal = dtStart.GetSecond() + nllDays_total * 24 * 60 * 60;
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = nDiv * m_scaleInterval.m_nValue;

		dtBegin = (DATE)(nNewVal / (24 * 60 * 60));
		dtBegin = CXTPCalendarUtils::UpdateTime(dtBegin, (int)(nNewVal % (24 * 60 * 60)),
										(int)(nNewVal % (60 * 60)), (int)(nNewVal % 60));

		/*nVal = dtStart.GetSecond();
		nDiv = nVal / m_scaleInterval.m_nValue;
		//nMod = nVal % m_scaleInterval.m_nValue;
		nNewVal = (nDiv + (nMod ? 1 : 0)) * m_scaleInterval.m_nValue;

		spAdd.SetDateTimeSpan(0, 0, 0, nNewVal);
		dtBegin = CXTPCalendarUtils::UpdateTime(dtBegin, dtStart.GetHour(), dtStart.GetMinute(), 0);
		dtBegin += spAdd;*/

		break;
	case xtpDTScale_Min:
		nVal = dtStart.GetMinute() /*+ (dtStart.GetSecond() ? 1 : 0)*/;
		nDiv = nVal / m_scaleInterval.m_nValue;
		//nMod = nVal % m_scaleInterval.m_nValue;
		//nNewVal = (nDiv +(nMod?1:0)) * m_scaleInterval.m_nValue;
		nNewVal = nDiv * m_scaleInterval.m_nValue;

		spAdd.SetDateTimeSpan(0, 0, (int)nNewVal, 0);
		dtBegin = CXTPCalendarUtils::UpdateTime(dtStart, dtStart.GetHour(), 0, 0);
		dtBegin += spAdd;

		break;

	case xtpDTScale_Hour:
		nVal = dtStart.GetHour();
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;

		spAdd.SetDateTimeSpan(0, (int)nNewVal, 0, 0);
		dtBegin = CXTPCalendarUtils::ResetTime(dtBegin);
		dtBegin += spAdd;

		break;

	case xtpDTScale_Day:
		nVal = dtStart.GetDay();
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;

		//spAdd.SetDateTimeSpan((int)max(0, nNewVal - 1), 0, 0, 0);
		//dtBegin = CXTPCalendarUtils::ResetTime(dtBegin);
		//CXTPCalendarUtils::UpdateMonthDay(dtBegin, 1);
		//dtBegin += spAdd;

		dtBegin = CXTPCalendarUtils::ResetTime(dtBegin);
		CXTPCalendarUtils::UpdateMonthDay(dtBegin, (int)nNewVal);

		break;
	case xtpDTScale_Week:
		// update first day of week
		nFirstDayOfWeekIndex = XTP_SAFE_GET3(m_pTimeScale, GetView(), GetCalendarControl(), GetFirstDayOfWeek(), 2);
		dtBegin = CXTPCalendarUtils::ShiftDateToWeekBegin(dtStart, nFirstDayOfWeekIndex);

		nVal = (int)(DATE)dtStart;
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;

		dtBegin = (DATE)nNewVal;

		break;

	case xtpDTScale_WorkWeek:
		// update first day of week
		nFirstDayOfWeekIndex = XTP_SAFE_GET3(m_pTimeScale, GetView(), GetCalendarControl(), GetFirstDayOfWeek(), 2);
		dtBegin = CXTPCalendarUtils::ShiftDateToWeekBegin(dtStart, nFirstDayOfWeekIndex);

		nVal = (int)(DATE)dtStart;
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;

		dtBegin = (DATE)nNewVal;

		break;
	case xtpDTScale_Month:
		dtBegin = CXTPCalendarUtils::ResetTime(dtStart);

		nVal = dtStart.GetMonth() + (dtStart.GetYear() - 1900) * 12;
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;
		{
			int nNewYear = (int)(nNewVal / 12 + 1900);
			int nNewMonth = (int)(nNewVal % 12);

			dtBegin.SetDate(nNewYear, nNewMonth, 1);
		}

		break;
	case xtpDTScale_Year:
		dtBegin = CXTPCalendarUtils::ResetTime(dtStart);

		nVal = dtStart.GetYear();
		nDiv = nVal / m_scaleInterval.m_nValue;
		nNewVal = (nDiv) * m_scaleInterval.m_nValue;

		dtBegin.SetDate((int)nNewVal, 1, 1);

		break;
	default:
		ASSERT(FALSE);
	}

	return dtBegin;
}