+ Refresh(true, &rect);
+}
+
+void wxCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fromdate, const wxDateTime& todate, wxPen* pPen, wxBrush* pBrush)
+{
+ // Highlights the given range using pen and brush
+ // Does nothing if todate < fromdate
+
+
+#if DEBUG_PAINT
+ wxLogDebug("+++ HighlightRange: (%s) - (%s) +++", fromdate.Format("%d %m %Y"), todate.Format("%d %m %Y"));
+#endif
+
+ if ( todate >= fromdate )
+ {
+ // do stuff
+ // date-coordinates
+ int fd, fw;
+ int td, tw;
+
+ // implicit: both dates must be currently shown - checked by GetDateCoord
+ if ( GetDateCoord(fromdate, &fd, &fw) && GetDateCoord(todate, &td, &tw) )
+ {
+#if DEBUG_PAINT
+ wxLogDebug("Highlight range: (%i, %i) - (%i, %i)", fd, fw, td, tw);
+#endif
+ if ( ( (tw - fw) == 1 ) && ( td < fd ) )
+ {
+ // special case: interval 7 days or less not in same week
+ // split in two separate intervals
+ wxDateTime tfd = fromdate + wxDateSpan::Days(7-fd);
+ wxDateTime ftd = tfd + wxDateSpan::Day();
+#if DEBUG_PAINT
+ wxLogDebug("Highlight: Separate segments");
+#endif
+ // draw separately
+ HighlightRange(pDC, fromdate, tfd, pPen, pBrush);
+ HighlightRange(pDC, ftd, todate, pPen, pBrush);
+ }
+ else
+ {
+ int numpoints;
+ wxPoint corners[8]; // potentially 8 corners in polygon
+
+ if ( fw == tw )
+ {
+ // simple case: same week
+ numpoints = 4;
+ corners[0] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset);
+ corners[1] = wxPoint((fd - 1) * m_widthCol, ((fw + 1 ) * m_heightRow) + m_rowOffset);
+ corners[2] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset);
+ corners[3] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset);
+ }
+ else
+ {
+ int cidx = 0;
+ // "complex" polygon
+ corners[cidx] = wxPoint((fd - 1) * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
+
+ if ( fd > 1 )
+ {
+ corners[cidx] = wxPoint((fd - 1) * m_widthCol, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
+ corners[cidx] = wxPoint(0, ((fw + 1) * m_heightRow) + m_rowOffset); cidx++;
+ }
+
+ corners[cidx] = wxPoint(0, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
+ corners[cidx] = wxPoint(td * m_widthCol, ((tw + 1) * m_heightRow) + m_rowOffset); cidx++;
+
+ if ( td < 7 )
+ {
+ corners[cidx] = wxPoint(td * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
+ corners[cidx] = wxPoint(7 * m_widthCol, (tw * m_heightRow) + m_rowOffset); cidx++;
+ }
+
+ corners[cidx] = wxPoint(7 * m_widthCol, (fw * m_heightRow) + m_rowOffset); cidx++;
+
+ numpoints = cidx;
+ }
+
+ // draw the polygon
+ pDC->SetBrush(*pBrush);
+ pDC->SetPen(*pPen);
+ pDC->DrawPolygon(numpoints, corners);
+ }
+ }
+ }
+ // else do nothing
+#if DEBUG_PAINT
+ wxLogDebug("--- HighlightRange ---");
+#endif
+}
+
+bool wxCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *week) const
+{
+ bool retval = true;
+
+#if DEBUG_PAINT
+ wxLogDebug("+++ GetDateCoord: (%s) +++", date.Format("%d %m %Y"));
+#endif
+
+ if ( IsDateShown(date) )
+ {
+ bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
+
+ // Find day
+ *day = date.GetWeekDay();
+
+ if ( *day == 0 ) // sunday
+ {
+ *day = ( startOnMonday ) ? 7 : 1;
+ }
+ else
+ {
+ *day += ( startOnMonday ) ? 0 : 1;
+ }
+
+ int targetmonth = date.GetMonth() + (12 * date.GetYear());
+ int thismonth = m_date.GetMonth() + (12 * m_date.GetYear());
+
+ // Find week
+ if ( targetmonth == thismonth )
+ {
+ *week = GetWeek(date);
+ }
+ else
+ {
+ if ( targetmonth < thismonth )
+ {
+ *week = 1; // trivial
+ }
+ else // targetmonth > thismonth
+ {
+ wxDateTime ldcm;
+ int lastweek;
+ int lastday;
+
+ // get the datecoord of the last day in the month currently shown
+#if DEBUG_PAINT
+ wxLogDebug(" +++ LDOM +++");
+#endif
+ GetDateCoord(ldcm.SetToLastMonthDay(m_date.GetMonth(), m_date.GetYear()), &lastday, &lastweek);
+#if DEBUG_PAINT
+ wxLogDebug(" --- LDOM ---");
+#endif
+
+ wxTimeSpan span = date - ldcm;
+
+ int daysfromlast = span.GetDays();
+#if DEBUG_PAINT
+ wxLogDebug("daysfromlast: %i", daysfromlast);
+#endif
+ if ( daysfromlast + lastday > 7 ) // past week boundary
+ {
+ int wholeweeks = (daysfromlast / 7);
+ *week = wholeweeks + lastweek;
+ if ( (daysfromlast - (7 * wholeweeks) + lastday) > 7 )
+ {
+ *week += 1;
+ }
+ }
+ else
+ {
+ *week = lastweek;
+ }
+ }
+ }
+ }
+ else
+ {
+ *day = -1;
+ *week = -1;
+ retval = false;
+ }
+
+#if DEBUG_PAINT
+ wxLogDebug("--- GetDateCoord: (%s) = (%i, %i) ---", date.Format("%d %m %Y"), *day, *week);
+#endif
+
+ return retval;