]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrlg.cpp
fixed wxXmlResource::Load's detection of filenames to be done as early as possible
[wxWidgets.git] / src / generic / calctrlg.cpp
index d59309415be57bb5204dbcd0dc2c59168b8a11a4..effd171d216f09cc9bb2fa30f8e9ce38d4d09fb9 100644 (file)
@@ -96,13 +96,14 @@ wxBEGIN_FLAGS( wxCalendarCtrlStyle )
     wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE)
     wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION)
     wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS)
     wxFLAGS_MEMBER(wxCAL_NO_MONTH_CHANGE)
     wxFLAGS_MEMBER(wxCAL_SEQUENTIAL_MONTH_SELECTION)
     wxFLAGS_MEMBER(wxCAL_SHOW_SURROUNDING_WEEKS)
+    wxFLAGS_MEMBER(wxCAL_SHOW_WEEK_NUMBERS)
 
 wxEND_FLAGS( wxCalendarCtrlStyle )
 
 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl, wxControl,"wx/calctrl.h")
 
 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl)
 
 wxEND_FLAGS( wxCalendarCtrlStyle )
 
 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl, wxControl,"wx/calctrl.h")
 
 wxBEGIN_PROPERTIES_TABLE(wxGenericCalendarCtrl)
-    wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEKDAY_CLICKED , wxCalendarEvent )
+    wxEVENT_RANGE_PROPERTY( Updated , wxEVT_CALENDAR_SEL_CHANGED , wxEVT_CALENDAR_WEEK_CLICKED , wxCalendarEvent )
     wxHIDE_PROPERTY( Children )
     wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
     wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
     wxHIDE_PROPERTY( Children )
     wxPROPERTY( Date,wxDateTime, SetDate , GetDate, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
     wxPROPERTY_FLAGS( WindowStyle , wxCalendarCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
@@ -190,7 +191,8 @@ void wxGenericCalendarCtrl::Init()
     m_userChangedYear = false;
 
     m_widthCol =
     m_userChangedYear = false;
 
     m_widthCol =
-    m_heightRow = 0;
+    m_heightRow =
+    m_calendarWeekWidth = 0;
 
     wxDateTime::WeekDay wd;
     for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
 
     wxDateTime::WeekDay wd;
     for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) )
@@ -347,7 +349,7 @@ void wxGenericCalendarCtrl::CreateYearSpinCtrl()
                         NULL, this);
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
                         NULL, this);
 
     m_spinYear->Connect(m_spinYear->GetId(), wxEVT_COMMAND_SPINCTRL_UPDATED,
-                        wxCommandEventHandler(wxGenericCalendarCtrl::OnYearChange),
+                        wxSpinEventHandler(wxGenericCalendarCtrl::OnYearChange),
                         NULL, this);
 }
 
                         NULL, this);
 }
 
@@ -772,7 +774,7 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 
 // this is a composite control and it must arrange its parts each time its
 // size or position changes: the combobox and spinctrl are along the top of
 
 // this is a composite control and it must arrange its parts each time its
 // size or position changes: the combobox and spinctrl are along the top of
-// the available area and the calendar takes up therest of the space
+// the available area and the calendar takes up the rest of the space
 
 // the static controls are supposed to be always smaller than combo/spin so we
 // always use the latter for size calculations and position the static to take
 
 // the static controls are supposed to be always smaller than combo/spin so we
 // always use the latter for size calculations and position the static to take
@@ -785,10 +787,10 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 wxSize wxGenericCalendarCtrl::DoGetBestSize() const
 {
     // calc the size of the calendar
 wxSize wxGenericCalendarCtrl::DoGetBestSize() const
 {
     // calc the size of the calendar
-    wx_const_cast(wxGenericCalendarCtrl *, this)->RecalcGeometry();
+    const_cast<wxGenericCalendarCtrl *>(this)->RecalcGeometry();
 
 
-    wxCoord width = 7*m_widthCol,
-            height = 7*m_heightRow + m_rowOffset;
+    wxCoord width = 7*m_widthCol + m_calendarWeekWidth,
+            height = 7*m_heightRow + m_rowOffset + VERT_MARGIN;
 
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
 
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
@@ -813,7 +815,7 @@ wxSize wxGenericCalendarCtrl::DoGetBestSize() const
     }
 
     CacheBestSize(best);
     }
 
     CacheBestSize(best);
-    
+
     return best;
 }
 
     return best;
 }
 
@@ -855,7 +857,7 @@ void wxGenericCalendarCtrl::DoGetSize(int *width, int *height) const
 {
 #ifdef __WXMAC__
     wxControl::DoGetSize( width, height );
 {
 #ifdef __WXMAC__
     wxControl::DoGetSize( width, height );
-    
+
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) && m_staticMonth && height )
     {
         wxSize sizeCombo = m_comboMonth->GetEffectiveMinSize();
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) && m_staticMonth && height )
     {
         wxSize sizeCombo = m_comboMonth->GetEffectiveMinSize();
@@ -900,6 +902,9 @@ void wxGenericCalendarCtrl::RecalcGeometry()
         }
     }
 
         }
     }
 
+    m_calendarWeekWidth = HasFlag( wxCAL_SHOW_WEEK_NUMBERS )
+        ? dc.GetTextExtent( wxString::Format( wxT( "%d" ), 42 )).GetWidth() + 4 : 0;
+
     // leave some margins
     m_widthCol += 2;
     m_heightRow += 2;
     // leave some margins
     m_widthCol += 2;
     m_heightRow += 2;
@@ -926,7 +931,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 #endif
 
     wxCoord y = 0;
 #endif
 
     wxCoord y = 0;
-    wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+    wxCoord x0 = m_calendarWeekWidth;
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
@@ -1029,6 +1034,24 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     //dc.SetFont(*wxNORMAL_FONT);
 
     y += m_heightRow;
     //dc.SetFont(*wxNORMAL_FONT);
 
     y += m_heightRow;
+
+    // draw column with calendar week nr
+    if ( HasFlag( wxCAL_SHOW_WEEK_NUMBERS ) && IsExposed( 0, y, m_calendarWeekWidth, m_heightRow * 6 ))
+    {
+        dc.SetBackgroundMode(wxTRANSPARENT);
+        dc.SetBrush(wxBrush(m_colHeaderBg, wxSOLID));
+        dc.SetPen(wxPen(m_colHeaderBg, 1, wxSOLID));
+        dc.DrawRectangle( 0, y, m_calendarWeekWidth, m_heightRow * 6 );
+        wxDateTime date = GetStartDate();
+        for ( size_t i = 0; i < 6; ++i )
+        {
+            const int weekNr = date.GetWeekOfYear();
+            wxString text = wxString::Format( wxT( "%d" ), weekNr );
+            dc.DrawText( text, m_calendarWeekWidth - dc.GetTextExtent( text ).GetWidth() - 2, y + m_heightRow * i );
+            date += wxDateSpan::Week();
+        }
+    }
+
     wxDateTime date = GetStartDate();
 
 #if DEBUG_PAINT
     wxDateTime date = GetStartDate();
 
 #if DEBUG_PAINT
@@ -1060,7 +1083,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
                 unsigned int day = date.GetDay();
                 wxString dayStr = wxString::Format(_T("%u"), day);
                 wxCoord width;
                 unsigned int day = date.GetDay();
                 wxString dayStr = wxString::Format(_T("%u"), day);
                 wxCoord width;
-                dc.GetTextExtent(dayStr, &width, (wxCoord *)NULL);
+                dc.GetTextExtent(dayStr, &width, NULL);
 
                 bool changedColours = false,
                      changedFont = false;
 
                 bool changedColours = false,
                      changedFont = false;
@@ -1211,9 +1234,9 @@ void wxGenericCalendarCtrl::RefreshDate(const wxDateTime& date)
     wxRect rect;
 
     // always refresh the whole row at once because our OnPaint() will draw
     wxRect rect;
 
     // always refresh the whole row at once because our OnPaint() will draw
-    // the whole row anyhow - and this allows the small optimisation in
+    // the whole row anyhow - and this allows the small optimization in
     // OnClick() below to work
     // OnClick() below to work
-    rect.x = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+    rect.x = m_calendarWeekWidth;
 
     rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
 
 
     rect.y = (m_heightRow * GetWeek(date)) + m_rowOffset;
 
@@ -1279,7 +1302,7 @@ void wxGenericCalendarCtrl::HighlightRange(wxPaintDC* pDC, const wxDateTime& fro
             {
                 int numpoints;
                 wxPoint corners[8]; // potentially 8 corners in polygon
             {
                 int numpoints;
                 wxPoint corners[8]; // potentially 8 corners in polygon
-                wxCoord x0 = wxMax( (GetSize().x - m_widthCol*7) /2 , 0 );
+                wxCoord x0 = m_calendarWeekWidth;
 
                 if ( fw == tw )
                 {
 
                 if ( fw == tw )
                 {
@@ -1452,7 +1475,14 @@ void wxGenericCalendarCtrl::OnClick(wxMouseEvent& event)
                 // GenerateAllChangeEvents() here, we know which event to send
                 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
             }
                 // GenerateAllChangeEvents() here, we know which event to send
                 GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
             }
-            break;
+        break;
+
+        case wxCAL_HITTEST_WEEK:
+        {
+            wxCalendarEvent send( this, date, wxEVT_CALENDAR_WEEK_CLICKED );
+            HandleWindowEvent( send );
+        }
+        break;
 
         case wxCAL_HITTEST_HEADER:
             {
 
         case wxCAL_HITTEST_HEADER:
             {
@@ -1490,7 +1520,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
     RecalcGeometry();
 
     // the position where the calendar really begins
     RecalcGeometry();
 
     // the position where the calendar really begins
-    wxCoord x0 = wxMax((GetSize().x - m_widthCol*7)/2, 0);
+    wxCoord x0 = m_calendarWeekWidth;
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
 
     if ( HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
@@ -1531,12 +1561,30 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
 
             return wxCAL_HITTEST_INCMONTH;
         }
 
             return wxCAL_HITTEST_INCMONTH;
         }
+    }
 
 
+    if ( pos.x - x0 < 0 )
+    {
+        if ( pos.x >= 0 && pos.y > m_rowOffset + m_heightRow && pos.y <= m_rowOffset + m_heightRow * 7 )
+        {
+            if ( date )
+            {
+                *date = GetStartDate();
+                *date += wxDateSpan::Week() * (( pos.y - m_rowOffset ) / m_heightRow - 1 );
+            }
+            if ( wd )
+                *wd = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) ? wxDateTime::Mon : wxDateTime::Sun;
+            return wxCAL_HITTEST_WEEK;
+        }
+        else    // early exit -> the rest of the function checks for clicks on days
+            return wxCAL_HITTEST_NOWHERE;
     }
 
     // header: week days
     int wday = (pos.x - x0) / m_widthCol;
     }
 
     // header: week days
     int wday = (pos.x - x0) / m_widthCol;
-    if ( pos.y < (m_heightRow + m_rowOffset) )
+    if ( wday > 6 )
+        return wxCAL_HITTEST_NOWHERE;
+    if ( pos.y < (m_heightRow + m_rowOffset))
     {
         if ( pos.y > m_rowOffset )
         {
     {
         if ( pos.y > m_rowOffset )
         {
@@ -1607,7 +1655,7 @@ void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent& event)
     SetDateAndNotify(target);
 }
 
     SetDateAndNotify(target);
 }
 
-void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent& event)
+void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
 {
     int year = (int)event.GetInt();
     if ( year == INT_MIN )
 {
     int year = (int)event.GetInt();
     if ( year == INT_MIN )
@@ -1637,10 +1685,15 @@ void wxGenericCalendarCtrl::OnYearChange(wxCommandEvent& event)
     }
 }
 
     }
 }
 
+void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent& event)
+{
+    HandleYearChange( event );
+}
+
 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
 {
     SetUserChangedYear();
 void wxGenericCalendarCtrl::OnYearTextChange(wxCommandEvent& event)
 {
     SetUserChangedYear();
-    OnYearChange(event);
+    HandleYearChange(event);
 }
 
 // Responds to colour changes, and passes event on to children.
 }
 
 // Responds to colour changes, and passes event on to children.