void CXTPDatePickerItemMonth::PopulateDays()
{
	if (m_dtMonth.GetYear() <= 100 && m_dtMonth.GetMonth() <= 2)
		m_dtMonth = COleDateTime(100,2,1,0,0,0);

	if (m_dtMonth.GetStatus() != COleDateTime::valid)
	{
		ASSERT(FALSE);
		m_dtMonth = CXTPCalendarUtils::ResetTime(COleDateTime::GetCurrentTime());
	}
	//if (m_dtMonth < COleDateTime(1601,4,1,0,0,0))
	//  m_dtMonth = COleDateTime(1601,4,1,0,0,0);

	// init days from 1st to last
	COleDateTime dtDay(m_dtMonth.GetYear(), m_dtMonth.GetMonth(), 1, 0, 0, 0);
	COleDateTimeSpan spDay(1, 0, 0, 0);

	// adjust first day of the week
	int nOleFirstDayOfWeek = m_pControl->GetFirstDayOfWeek();

	int iDayOfWeek = dtDay.GetDayOfWeek();
	if (iDayOfWeek != -1)
	{
		while (dtDay.GetDayOfWeek() != nOleFirstDayOfWeek && dtDay.GetDayOfWeek() != -1)
		{
			dtDay -= spDay;
		}
	}
	else
	{
		COleDateTime dtFutureDay(dtDay);
		dtFutureDay.SetDate(dtFutureDay.GetYear() + 2000, dtFutureDay.GetMonth(), dtFutureDay.GetDay());
		iDayOfWeek = dtFutureDay.GetDayOfWeek();

		while (dtFutureDay.GetDayOfWeek() != nOleFirstDayOfWeek && dtFutureDay.GetDayOfWeek() != -1)
		{
			dtFutureDay -= spDay;
			dtDay -= spDay;
		}
	}

	//while (dtDay.GetDayOfWeek() != nOleFirstDayOfWeek && dtDay.GetDayOfWeek() != -1)
	//{
	//  dtDay -= spDay;
	//}

	// populate all grid days
	for (int nWeek = 0; nWeek < XTP_MAX_WEEKS; nWeek++)
	{
		for (int nDay = 0; nDay < XTP_WEEK_DAYS; nDay++)
		{
			// create and add new day item
			CXTPDatePickerItemDay* pDay = new CXTPDatePickerItemDay(m_pControl, this, dtDay);
			m_arrDays.Add(pDay);
			// move next day
			dtDay += spDay;
		}
	}
}
void CXTPDatePickerItemMonth::OnMouseMove(UINT /*nFlags*/, CPoint point)
{
	if ((m_pControl->m_mouseMode == CXTPDatePickerControl::mouseDeselecting ||
		m_pControl->m_mouseMode == CXTPDatePickerControl::mouseSelecting) &&
		(m_rcDaysArea.PtInRect(point) || m_rcWeekNumbers.PtInRect(point)))
	{
		// check day item
		CPoint ptWeekBegin(m_rcWeekNumbers.right + 3, point.y);
		CXTPDatePickerItemDay* pDay = HitTest(m_pControl->m_bSelectWeek ? ptWeekBegin : point, FALSE);
		if (pDay)
		{
			// save old clicked dates
			COleDateTime dtOldFirstClicked = m_pControl->m_dtFirstClicked;
			COleDateTime dtOldLastClicked = m_pControl->m_dtLastClicked;

			// calculate new clicked dates
			COleDateTime dtDay(pDay->GetDate());
			if (m_pControl->m_bSelectWeek)
			{
				COleDateTime dtWeekEnd(dtDay + COleDateTimeSpan(6, 0, 0, 0));
				if (dtWeekEnd >= m_pControl->m_dtFirstClicked)
				{
					if (dtDay.GetDayOfWeek() != m_pControl->m_dtFirstClicked.GetDayOfWeek())
						m_pControl->m_dtFirstClicked -= COleDateTimeSpan(6, 0, 0, 0);
					m_pControl->m_dtLastClicked = dtWeekEnd;
				}
				if (dtDay < m_pControl->m_dtFirstClicked)
				{
					if (dtDay.GetDayOfWeek() == m_pControl->m_dtFirstClicked.GetDayOfWeek())
						m_pControl->m_dtFirstClicked += COleDateTimeSpan(6, 0, 0, 0);
					m_pControl->m_dtLastClicked = dtDay;
				}
			}
			else
			{
				m_pControl->m_dtLastClicked = dtDay;
			}

			// update control if needed
			if (((dtOldLastClicked - m_pControl->m_dtLastClicked).GetDays() != 0) ||
				((dtOldFirstClicked - m_pControl->m_dtFirstClicked).GetDays() != 0))
			{
				m_pControl->RedrawControl();
			}
		}
	}
}
void CXTPCalendarController::OnEvent_DatePicker(XTP_NOTIFY_CODE Event, WPARAM wParam, LPARAM /*lParam*/)
{
    wParam;

    switch (Event)
    {
    case XTP_NC_DATEPICKERBEFOREGOMODAL:
        if (m_pDatePickerCtrl && m_pCalendarCtrl)
        {
            OnEvent_CalendarView(XTP_NC_CALENDARVIEWWASCHANGED, 0, 0);
        }
        break;
    case XTP_NC_DATEPICKERBUTTONCLICKED:
        DBG_TRACE_DP_NF(_T("OnEvent: DatePickerButtonClicked - ID = %u.     {CalendarController}. \n"), wParam);
        break;
    case XTP_NC_DATEPICKERMONTHCHANGED:
        m_DayInfoCache.UpActivePriority();
        break;
    case XTP_NC_DATEPICKERSELECTIONCHANGED:
        DBG_TRACE_DP_NF(_T("OnEvent: XTP_NC_DATEPICKERSELECTIONCHANGED.     {CalendarController}. \n"));
        if (m_pDatePickerCtrl && m_pCalendarCtrl && m_pCalendarCtrl->GetActiveView())
        {
            BOOL bFastMode = m_pCalendarCtrl->m_bSwitchToDayViewIfPickedSingleDay;
            BOOL bChanged = FALSE;

            COleDateTime dtFrom;
            COleDateTime dtTo;
            if (m_pDatePickerCtrl->GetSelRange(dtFrom, dtTo))
            {
                int nFirstDayOfWeekIndex = m_pDatePickerCtrl->GetFirstDayOfWeek();
                int nFirstDayOfWeek = dtFrom.GetDayOfWeek();
                BOOL bWeekStart = (nFirstDayOfWeekIndex == nFirstDayOfWeek);

                CXTPCalendarControl::CUpdateContext updateContext(m_pCalendarCtrl);

                int nSelRangeDays = CXTPCalendarUtils::GetTotalDays(dtTo - dtFrom) + 1;
                // count selected days
                int nSelDays = 0;
                COleDateTimeSpan spDay(1, 0, 0, 0);
                for (COleDateTime dtDay = dtFrom; dtDay <= dtTo; dtDay += spDay)
                {
                    if (m_pDatePickerCtrl->IsSelected(dtDay))
                        nSelDays++;
                }
                BOOL bDaysCont = (nSelRangeDays == nSelDays);
//LOGIC TO SELECT VIEW TYPE <<
                // Day view
                int eViewMode = XTP_SAFE_GET2(m_pCalendarCtrl, GetActiveView(), GetViewType(), xtpCalendarDayView);

                if (eViewMode == xtpCalendarTimeLineView)
                {
                    CXTPCalendarTimeLineView* pView = (CXTPCalendarTimeLineView* ) m_pCalendarCtrl->GetActiveView();
                    if (pView)
                    {
                        pView->SetStartViewDate(dtFrom);
                        if (nSelRangeDays == 1)
                            pView->SetTimeScaleID(xtpTSPID_Day);
                        else if (nSelRangeDays <= 5)
                            pView->SetTimeScaleID(xtpTSPID_WorkWeek);
                        else if (nSelRangeDays <= 7)
                            pView->SetTimeScaleID(xtpTSPID_Week);
                        else
                            pView->SetTimeScaleID(xtpTSPID_Month);
                    }

                    return;
                }

                if (nSelRangeDays <= nXTPMaxDayViewDays ||
                        !bDaysCont || !bWeekStart && nSelRangeDays <= 8)
                {
                    m_bAdjustingView = TRUE;

                    // to save WorkWeek view mode

                    //if (eViewMode != xtpCalendarWorkWeekView || nSelDays != 1)
                    if (bFastMode &&
                            ((eViewMode != xtpCalendarWorkWeekView)
                             && (eViewMode != xtpCalendarFullWeekView))
                            || nSelDays != 1)
                        eViewMode = xtpCalendarDayView;

                    if (eViewMode == xtpCalendarMonthView)
                    {
                        CXTPCalendarMonthView* pMonthView = DYNAMIC_DOWNCAST(CXTPCalendarMonthView, m_pCalendarCtrl->GetActiveView());
                        if (pMonthView)
                        {
                            pMonthView->SelectDay(dtFrom);
                            CXTPCalendarControl::CViewChangedContext viewChanged(m_pCalendarCtrl, xtpCalendarViewChangedLock);
                            COleDateTime dtBegin = dtFrom;
                            // Calculate weeks count
                            COleDateTime dtWeekFromBegin = pMonthView->GetGrid()->ShiftDateToCell_00(dtFrom);
                            COleDateTime dtWeekToEnd = pMonthView->GetGrid()->ShiftDateToCell_00(dtTo);
                            dtWeekToEnd += COleDateTimeSpan(7, 0, 0, 0);
                            int nSelectedWeeksRange = CXTPCalendarUtils::GetTotalDays(dtWeekToEnd - dtWeekFromBegin);

                            int nWeeks = nSelectedWeeksRange / 7;
                            nWeeks = max(XTP_CALENDAR_MONTHVIEW_SHOW_WEEKS_MIN, nWeeks);
                            nWeeks = min(XTP_CALENDAR_MONTHVIEW_SHOW_WEEKS_MAX, nWeeks);

                            pMonthView->GetGrid()->SetBeginDate(dtBegin);
                            pMonthView->GetGrid()->SetWeeksCount(nWeeks);

                            m_pCalendarCtrl->Populate();
                        }
                    }
                    else if ((eViewMode == xtpCalendarWorkWeekView)
                             || (eViewMode == xtpCalendarFullWeekView)
                             || (eViewMode == xtpCalendarWeekView))
                    {
                        CXTPCalendarDayView* pDayView = DYNAMIC_DOWNCAST(CXTPCalendarDayView, m_pCalendarCtrl->GetActiveView());
                        if (pDayView)
                        {
                            pDayView->UnselectAllEvents();

                            COleDateTime dtWWStart;
                            m_pCalendarCtrl->GetWorkDayStartTime(dtWWStart);
                            dtWWStart = CXTPCalendarUtils::UpdateDate(dtWWStart, dtFrom);

                            pDayView->SetSelection(dtWWStart, dtWWStart + pDayView->GetCellDuration());
                        }
                        else
                        {
                            CXTPCalendarWeekView* pWeekView = DYNAMIC_DOWNCAST(CXTPCalendarWeekView, m_pCalendarCtrl->GetActiveView());
                            if (pWeekView)
                            {
                                pWeekView->SelectDay(dtFrom);
                                pWeekView->SetBeginDate(dtFrom);
                                m_pCalendarCtrl->Populate();
                            }
                        }
                    }

                    //*** switch calendar view
                    m_pCalendarCtrl->SwitchActiveView((XTPCalendarViewType)eViewMode);
                    //***

                    CXTPCalendarDayView* pDayView = DYNAMIC_DOWNCAST(CXTPCalendarDayView, m_pCalendarCtrl->GetActiveView());

                    // DayView - set visible days
                    if (pDayView && eViewMode == xtpCalendarDayView)
                    {
                        COleDateTime dtSelStart, dtSelEnd;
                        BOOL bAllDay = FALSE;
                        BOOL bSel = pDayView->GetSelection(&dtSelStart, &dtSelEnd, &bAllDay);
                        if (bSel)
                        {
                            COleDateTimeSpan spSel = dtSelEnd - dtSelStart;
                            COleDateTimeSpan spSelMin = pDayView->GetCellDuration();
                            if (spSel < spSelMin)
                                bSel = FALSE;
                        }
                        if (!bSel)
                        {
                            m_pCalendarCtrl->GetWorkDayStartTime(dtSelStart);
                            dtSelEnd = dtSelStart + pDayView->GetCellDuration();
                            bAllDay = FALSE;
                        }
                        dtSelStart = CXTPCalendarUtils::UpdateDate(dtSelStart, dtFrom);
                        dtSelEnd = CXTPCalendarUtils::UpdateDate(dtSelEnd, dtFrom);

                        pDayView->ShowDay(dtFrom);

                        pDayView->SetSelection(dtSelStart, dtSelEnd, bAllDay);

                        // add other selected days to calendar view
                        for (COleDateTime dtDay(dtFrom + spDay); dtDay <= dtTo; dtDay += spDay)
                        {
                            if (m_pDatePickerCtrl->IsSelected(dtDay))
                                pDayView->AddDay(dtDay); // add it to Calendar View
                        }
                        m_pCalendarCtrl->Populate();
                    }
                    m_bAdjustingView = FALSE;

                    bChanged = TRUE;
                }
                // Week view
                else if (nSelRangeDays == 7 && bDaysCont)
                {
                    COleDateTime dtBegin = dtFrom;

                    m_bAdjustingView = TRUE;

                    if (m_pCalendarCtrl->m_bMultiColumnWeekMode)
                        m_pCalendarCtrl->SwitchActiveView(xtpCalendarFullWeekView);
                    else
                        m_pCalendarCtrl->SwitchActiveView(xtpCalendarWeekView);

                    CXTPCalendarWeekView* pWeekView = DYNAMIC_DOWNCAST(CXTPCalendarWeekView, m_pCalendarCtrl->GetActiveView());
                    if (pWeekView)
                    {
                        pWeekView->SetBeginDate(dtBegin);

                        m_pCalendarCtrl->Populate();
                    }
                    else if (m_pCalendarCtrl->m_bMultiColumnWeekMode)
                    {
                        CXTPCalendarDayView* pWeekDayView = DYNAMIC_DOWNCAST(CXTPCalendarDayView, m_pCalendarCtrl->GetActiveView());
                        if (pWeekDayView)
                        {
                            pWeekDayView->ShowDays(dtBegin, dtTo);

                            m_pCalendarCtrl->Populate();
                        }
                    }
                    m_bAdjustingView = FALSE;

                    bChanged = TRUE;
                }
                // Month view
                else
                {
                    ASSERT(nSelRangeDays == nSelDays && nSelRangeDays > 7);

                    m_bAdjustingView = TRUE;

                    m_pCalendarCtrl->SwitchActiveView(xtpCalendarMonthView);
                    CXTPCalendarMonthView* pMonthView = DYNAMIC_DOWNCAST(CXTPCalendarMonthView, m_pCalendarCtrl->GetActiveView());
                    if (pMonthView)
                    {
                        CXTPCalendarControl::CViewChangedContext viewChanged(m_pCalendarCtrl, xtpCalendarViewChangedLock);
                        COleDateTime dtBegin = dtFrom;
                        // Calculate weeks count
                        COleDateTime dtWeekFromBegin = pMonthView->GetGrid()->ShiftDateToCell_00(dtFrom);
                        COleDateTime dtWeekToEnd = pMonthView->GetGrid()->ShiftDateToCell_00(dtTo);
                        dtWeekToEnd += COleDateTimeSpan(7, 0, 0, 0);
                        int nSelectedWeeksRange = CXTPCalendarUtils::GetTotalDays(dtWeekToEnd - dtWeekFromBegin);

                        int nWeeks = nSelectedWeeksRange / 7;
                        nWeeks = max(XTP_CALENDAR_MONTHVIEW_SHOW_WEEKS_MIN, nWeeks);
                        nWeeks = min(XTP_CALENDAR_MONTHVIEW_SHOW_WEEKS_MAX, nWeeks);

                        pMonthView->GetGrid()->SetBeginDate(dtBegin);
                        pMonthView->GetGrid()->SetWeeksCount(nWeeks);

                        m_pCalendarCtrl->Populate();
                    }
                    m_bAdjustingView = FALSE;

                    bChanged = TRUE;
                }
//LOGIC TO SELECT VIEW TYPE <<
                if (bChanged)
                {
                    OnEvent_CalendarView(XTP_NC_CALENDARVIEWWASCHANGED, 0, XTP_CC_DONT_SCROLL_DP);

                    if (m_pDatePickerCtrl->IsModal())
                    {
                        m_pCalendarCtrl->Invalidate(FALSE);
                        m_pCalendarCtrl->RedrawControl();
                    }
                }
            }
        }
        break;
    default:
        ASSERT(FALSE);
    }
}
void CXTPCalendarController::CXTPDayInfoCache::OnRefreshDays(int nDaysCountToRefresh)
{
    ASSERT(m_pOwner);

    if (!XTP_SAFE_GET1(m_pOwner, m_pResourcesNf, NULL))
    {
        if (!m_dwWaitingDataTime)
        {
            m_dwWaitingDataTime = GetTickCount();
            DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - OnRefreshDays, Start Wait for data\n"));
        }

        if (abs((long)(GetTickCount() - m_dwWaitingDataTime)) >= 5000)
        {
            m_dwWaitingDataTime = 0;

            if (m_uTimerID)
            {
                DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - OnRefreshDays, KillTimer (ID = %d) \n"), m_uTimerID);
                DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - OnRefreshDays, end Wait for data\n"));

                KillTimer();

                OnSelfClearOld();
            }
        }
        return;
    }
    else
    {
        if (m_dwWaitingDataTime)
        {
            DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - OnRefreshDays, end Wait for data\n"));
        }
        m_dwWaitingDataTime = 0;
    }

    //***************************************************
    for (int i = 0; i < nDaysCountToRefresh; i++)
    {
        long nDay_min = LONG_MAX;
        UINT uPriority_max = 0;

        POSITION pos = m_mapDaysToRefresh.GetStartPosition();
        while (pos)
        {
            long nDay = 0;
            UINT uPriority = 0;
            m_mapDaysToRefresh.GetNextAssoc(pos, nDay, uPriority);

            if (uPriority == uPriority_max && nDay < nDay_min ||
                    uPriority > uPriority_max)
            {
                uPriority_max = uPriority;
                nDay_min = nDay;
            }
        }

        if (nDay_min < LONG_MAX)
        {
            m_mapDaysToRefresh.RemoveKey(nDay_min);

            COleDateTime dtDay((DATE)nDay_min);
            BOOL bHasEvents = XTP_SAFE_GET1(m_pOwner, _HasEvents(dtDay), FALSE);

            UpdateDayInfo(dtDay, bHasEvents);
        }
    }
    //***************************************************

    if (m_mapDaysToRefresh.GetCount() == 0 && m_uTimerID)
    {
        DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - OnRefreshDays, KillTimer (ID = %d) \n"), m_uTimerID);
        DBG_TRACE_DP_TIMER(_T("XTPCalendarController::DayInfoCache - Days In Cache %d \n"), m_mapDaysInfo.GetCount());

        KillTimer();

        OnSelfClearOld();
    }

    if (abs((long)(GetTickCount() - m_dwLastRedrawTime)) >= 500 || m_uTimerID == 0)
    {
        XTP_SAFE_CALL2(m_pOwner, m_pDatePickerCtrl, RedrawControl());
        m_dwLastRedrawTime = GetTickCount();
    }
}
void CXTPDatePickerItemMonth::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (!m_pControl)
		return;

	BOOL bMultiselect = !!(nFlags & MK_CONTROL) || m_pControl->IsMultiSelectionMode();

	// check left scroll item
	if (m_bShowLeftScroll
		&& !m_rcLeftScroll.IsRectEmpty()
		&& m_rcLeftScroll.PtInRect(point))
	{
		m_pControl->m_mouseMode = CXTPDatePickerControl::mouseScrollingLeft;
		m_pControl->m_nTimerID = (UINT)m_pControl->SetTimer(XTP_DATEPICKER_TIMERID, XTP_DATEPICKER_TIMER_INTERVAL, NULL);
		if (m_pControl->m_bYearsTriangle)
		{
			if (point.x < m_rcLeftScroll.left + 9)
			{
				if (m_dtMonth.GetYear() >= 100 ||
					(m_dtMonth.GetYear() == 100 && m_dtMonth.GetMonth() >= 3))
					m_pControl->ScrollLeft(m_pControl->GetMonthDelta());
			}
			else if (point.x > m_rcLeftScroll.right - 9)
			{
				if (m_dtMonth.GetYear() >= 101 ||
					(m_dtMonth.GetYear() == 101 && m_dtMonth.GetMonth() >= 3))
					m_pControl->ScrollLeft(12 * m_pControl->GetMonthDelta());
			}
		}
		else
		{
			m_pControl->ScrollLeft(m_pControl->GetMonthDelta());
		}
		return;
	}
	// check right scroll item
	if (m_bShowRightScroll
		&& !m_rcRightScroll.IsRectEmpty()
		&& m_rcRightScroll.PtInRect(point))
	{
		m_pControl->m_mouseMode = CXTPDatePickerControl::mouseScrollingRight;
		m_pControl->m_nTimerID = (UINT)m_pControl->SetTimer(XTP_DATEPICKER_TIMERID, XTP_DATEPICKER_TIMER_INTERVAL, NULL);

		if (m_pControl->m_bYearsTriangle)
		{
			if (point.x < m_rcRightScroll.left + 9)
				m_pControl->ScrollRight(12 * m_pControl->GetMonthDelta());
			else if (point.x > m_rcLeftScroll.right - 9)
				m_pControl->ScrollRight(m_pControl->GetMonthDelta());
		}
		else
		{
			m_pControl->ScrollRight(m_pControl->GetMonthDelta());
		}
		return;
	}
	// check header click
	if (!m_rcHeader.IsRectEmpty()
		&& CRect(m_rcHeader.left + 20, m_rcHeader.top, m_rcHeader.right - 20, m_rcHeader.bottom).PtInRect(point))
		//m_rcHeader.PtInRect(point))
	{
		// call month popup list
		m_pControl->ShowListHeader(m_rcHeader, m_dtMonth);
		return;
	}
	// check week numbers click
	m_pControl->m_bSelectWeek = !m_rcWeekNumbers.IsRectEmpty() && m_rcWeekNumbers.PtInRect(point);
	CPoint ptFirstWeekDay(point);
	ptFirstWeekDay.x = m_rcWeekNumbers.right + 3;

	// check day item
	CXTPDatePickerItemDay* pDay = HitTest(m_pControl->m_bSelectWeek ? ptFirstWeekDay : point, FALSE);
	if (pDay)
	{
		m_pControl->ClearFocus();

		BOOL bAllowNoncontinuousSelection = m_pControl->IsAllowNoncontinuousSelection();

		COleDateTime dtDay(pDay->GetDate());
		BOOL bSelected = m_pControl->IsSelected(dtDay);
		if (m_pControl->m_bSelectWeek)
		{
//week is selected only when all week days are selected
			for (int i = 0; i < 7; i++)
			{
				COleDateTime dtDay_i = dtDay + COleDateTimeSpan(i, 0, 0, 0);
				bSelected &= m_pControl->IsSelected(dtDay_i);
			}
		}
		if (bSelected && bMultiselect && bAllowNoncontinuousSelection)
			m_pControl->m_mouseMode = CXTPDatePickerControl::mouseDeselecting;
		else
			m_pControl->m_mouseMode = CXTPDatePickerControl::mouseSelecting;

		if (!bMultiselect || !bAllowNoncontinuousSelection)
			m_pControl->m_pSelectedDays->Clear();

		if (nFlags & MK_SHIFT)
		{
			if (m_pControl->m_dtFirstClicked.GetStatus() == COleDateTime::valid)
			{
				m_pControl->m_dtLastClicked = dtDay;
			}
			else if (m_pControl->m_dtLastClicked.GetStatus() == COleDateTime::valid)
			{
				m_pControl->m_dtFirstClicked = m_pControl->m_dtLastClicked;
				m_pControl->m_dtLastClicked = dtDay;
			}
			else if (m_pControl->GetSelRange(m_pControl->m_dtFirstClicked, m_pControl->m_dtLastClicked))
			{
				if (m_pControl->m_dtFirstClicked < dtDay)
					m_pControl->m_dtLastClicked = dtDay;
				else
					m_pControl->m_dtFirstClicked = dtDay;
			}
			else
			{
				m_pControl->m_dtFirstClicked = dtDay;
				m_pControl->m_dtLastClicked = dtDay;
			}
		}
		else
		{
			m_pControl->m_dtFirstClicked = dtDay;
			m_pControl->m_dtLastClicked = m_pControl->m_bSelectWeek ?
				dtDay + COleDateTimeSpan(6, 0, 0, 0) : dtDay;
		}

		m_pControl->_RedrawControl(FALSE);
	}
}
void CDxDatePickerMonth::OnLButtonDown(UINT nFlags, CPoint point)
{
    if (!m_pControl)
        return;

    // check left scroll item
    if (m_bShowLeftScroll && !m_rcLeftScroll.IsRectEmpty() && m_rcLeftScroll.PtInRect(point))
    {
        m_pControl->m_mouseMode = CDxMonthPicker::mouseScrollingLeft;
        if (m_pControl->m_bYearsTriangle)
        {
            if (point.x < m_rcLeftScroll.left + 9)
            {
                if (m_dtMonth.GetYear() >= 100 || (m_dtMonth.GetYear() == 100 && m_dtMonth.GetMonth() >= 3))
                    m_pControl->ScrollLeft(m_pControl->GetMonthDelta());
            }
            else if (point.x > m_rcLeftScroll.right - 9)
            {
                if (m_dtMonth.GetYear() >= 101 || (m_dtMonth.GetYear() == 101 && m_dtMonth.GetMonth() >= 3))
                    m_pControl->ScrollLeft(12 * m_pControl->GetMonthDelta());
            }
        }
        else
        {
            m_pControl->ScrollLeft(m_pControl->GetMonthDelta());
        }
        return;
    }

    // check right scroll item
    if (m_bShowRightScroll && !m_rcRightScroll.IsRectEmpty() && m_rcRightScroll.PtInRect(point))
    {
        m_pControl->m_mouseMode = CDxMonthPicker::mouseScrollingRight;

        if (m_pControl->m_bYearsTriangle)
        {
            if (point.x < m_rcRightScroll.left + 9)
                m_pControl->ScrollRight(12 * m_pControl->GetMonthDelta());
            else if (point.x > m_rcLeftScroll.right - 9)
                m_pControl->ScrollRight(m_pControl->GetMonthDelta());
        }
        else
        {
            m_pControl->ScrollRight(m_pControl->GetMonthDelta());
        }
        return;
    }

    //  check header click
    if (!m_rcHeader.IsRectEmpty() && CRect(m_rcHeader.left + 20, m_rcHeader.top, m_rcHeader.right - 20, m_rcHeader.bottom).PtInRect(point))
    {
        // call month popup list
        //m_pControl->ShowListHeader(m_rcHeader, m_dtMonth);
        return;
    }

    // check week numbers click
    m_pControl->m_bSelectWeek = !m_rcWeekNumbers.IsRectEmpty() && m_rcWeekNumbers.PtInRect(point);

    CPoint ptFirstWeekDay(point);
    ptFirstWeekDay.x = m_rcWeekNumbers.right + 3;

    //  check day item
    CDxDatePickerDay* pDay = HitTest(m_pControl->m_bSelectWeek ? ptFirstWeekDay : point, FALSE);
    if (pDay)
    {
        COleDateTime dtDay(pDay->GetDate());
        BOOL bSelected = m_pControl->IsSelected(dtDay);
        if (m_pControl->m_bSelectWeek)
        {
            //  week is selected only when all week days are selected
            for (int i = 0; i < 7; i++)
            {
                COleDateTime dtDay_i = dtDay + COleDateTimeSpan(i, 0, 0, 0);
                bSelected &= m_pControl->IsSelected(dtDay_i);
            }
        }

        m_pControl->m_mouseMode = CDxMonthPicker::mouseSelecting;

        m_pControl->m_dtFSelBase = dtDay;

        m_pControl->NotifyInvalidate();
    }
}