]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrlg.cpp
Added missing wxBitmapComboBox::Insert() implementation
[wxWidgets.git] / src / generic / calctrlg.cpp
index effd171d216f09cc9bb2fa30f8e9ce38d4d09fb9..bd6776ec806b98af2fb80d5aa4ae5a36c92015dc 100644 (file)
 // wxWin macros
 // ----------------------------------------------------------------------------
 
+#ifdef wxHAS_NATIVE_CALENDARCTRL
+
+wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericCalendarCtrl, wxControl,"wx/calctrl.h")
+
+#endif
+
 BEGIN_EVENT_TABLE(wxGenericCalendarCtrl, wxControl)
     EVT_PAINT(wxGenericCalendarCtrl::OnPaint)
 
@@ -58,65 +64,6 @@ BEGIN_EVENT_TABLE(wxGenericCalendarCtrl, wxControl)
     EVT_SYS_COLOUR_CHANGED(wxGenericCalendarCtrl::OnSysColourChanged)
 END_EVENT_TABLE()
 
-#if wxUSE_EXTENDED_RTTI
-WX_DEFINE_FLAGS( wxCalendarCtrlStyle )
-
-wxBEGIN_FLAGS( wxCalendarCtrlStyle )
-    // new style border flags, we put them first to
-    // use them for streaming out
-    wxFLAGS_MEMBER(wxBORDER_SIMPLE)
-    wxFLAGS_MEMBER(wxBORDER_SUNKEN)
-    wxFLAGS_MEMBER(wxBORDER_DOUBLE)
-    wxFLAGS_MEMBER(wxBORDER_RAISED)
-    wxFLAGS_MEMBER(wxBORDER_STATIC)
-    wxFLAGS_MEMBER(wxBORDER_NONE)
-
-    // old style border flags
-    wxFLAGS_MEMBER(wxSIMPLE_BORDER)
-    wxFLAGS_MEMBER(wxSUNKEN_BORDER)
-    wxFLAGS_MEMBER(wxDOUBLE_BORDER)
-    wxFLAGS_MEMBER(wxRAISED_BORDER)
-    wxFLAGS_MEMBER(wxSTATIC_BORDER)
-    wxFLAGS_MEMBER(wxBORDER)
-
-    // standard window styles
-    wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
-    wxFLAGS_MEMBER(wxCLIP_CHILDREN)
-    wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
-    wxFLAGS_MEMBER(wxWANTS_CHARS)
-    wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
-    wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
-    wxFLAGS_MEMBER(wxVSCROLL)
-    wxFLAGS_MEMBER(wxHSCROLL)
-
-    wxFLAGS_MEMBER(wxCAL_SUNDAY_FIRST)
-    wxFLAGS_MEMBER(wxCAL_MONDAY_FIRST)
-    wxFLAGS_MEMBER(wxCAL_SHOW_HOLIDAYS)
-    wxFLAGS_MEMBER(wxCAL_NO_YEAR_CHANGE)
-    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)
-    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
-wxEND_PROPERTIES_TABLE()
-
-wxBEGIN_HANDLERS_TABLE(wxGenericCalendarCtrl)
-wxEND_HANDLERS_TABLE()
-
-wxCONSTRUCTOR_6( wxGenericCalendarCtrl , wxWindow* , Parent , wxWindowID , Id , wxDateTime , Date , wxPoint , Position , wxSize , Size , long , WindowStyle )
-#else
-IMPLEMENT_DYNAMIC_CLASS(wxGenericCalendarCtrl, wxControl)
-#endif
-
 // ============================================================================
 // implementation
 // ============================================================================
@@ -248,11 +195,11 @@ bool wxGenericCalendarCtrl::Create(wxWindow *parent,
     if ( !HasFlag(wxCAL_SEQUENTIAL_MONTH_SELECTION) )
     {
         CreateYearSpinCtrl();
-        m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%Y")),
+        m_staticYear = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(wxT("%Y")),
                                         wxDefaultPosition, wxDefaultSize,
                                         wxALIGN_CENTRE);
         CreateMonthComboBox();
-        m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(_T("%B")),
+        m_staticMonth = new wxStaticText(GetParent(), wxID_ANY, m_date.Format(wxT("%B")),
                                          wxDefaultPosition, wxDefaultSize,
                                          wxALIGN_CENTRE);
     }
@@ -296,7 +243,7 @@ void wxGenericCalendarCtrl::SetWindowStyleFlag(long style)
     // created/shown/hidden accordingly
     wxASSERT_MSG( (style & wxCAL_SEQUENTIAL_MONTH_SELECTION) ==
                     (m_windowStyle & wxCAL_SEQUENTIAL_MONTH_SELECTION),
-                  _T("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
+                  wxT("wxCAL_SEQUENTIAL_MONTH_SELECTION can't be changed after creation") );
 
     wxControl::SetWindowStyleFlag(style);
 }
@@ -335,7 +282,7 @@ void wxGenericCalendarCtrl::CreateMonthComboBox()
 void wxGenericCalendarCtrl::CreateYearSpinCtrl()
 {
     m_spinYear = new wxSpinCtrl(GetParent(), wxID_ANY,
-                                GetDate().Format(_T("%Y")),
+                                GetDate().Format(wxT("%Y")),
                                 wxDefaultPosition,
                                 wxDefaultSize,
                                 wxSP_ARROW_KEYS | wxCLIP_SIBLINGS,
@@ -520,7 +467,7 @@ bool wxGenericCalendarCtrl::SetDate(const wxDateTime& date)
                     if ( AllowYearChange() )
                     {
                         if ( !m_userChangedYear )
-                            m_spinYear->SetValue(m_date.Format(_T("%Y")));
+                            m_spinYear->SetValue(m_date.Format(wxT("%Y")));
                     }
                 }
 
@@ -648,8 +595,7 @@ wxDateTime wxGenericCalendarCtrl::GetStartDate() const
     wxDateTime date = wxDateTime(1, tm.mon, tm.year);
 
     // rewind back
-    date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
-                          ? wxDateTime::Mon : wxDateTime::Sun);
+    date.SetToPrevWeekDay(GetWeekStart());
 
     if ( GetWindowStyle() & wxCAL_SHOW_SURROUNDING_WEEKS )
     {
@@ -682,69 +628,26 @@ bool wxGenericCalendarCtrl::IsDateInRange(const wxDateTime& date) const
         && ( ( m_highdate.IsValid() ) ? ( date <= m_highdate ) : true ) );
 }
 
-bool wxGenericCalendarCtrl::ChangeYear(wxDateTime* target) const
+bool wxGenericCalendarCtrl::AdjustDateToRange(wxDateTime *date) const
 {
-    bool retval = false;
-
-    if ( !(IsDateInRange(*target)) )
-    {
-        if ( target->GetYear() < m_date.GetYear() )
-        {
-            if ( target->GetYear() >= GetLowerDateLimit().GetYear() )
-            {
-                *target = GetLowerDateLimit();
-                retval = true;
-            }
-            else
-            {
-                *target = m_date;
-            }
-        }
-        else
-        {
-            if ( target->GetYear() <= GetUpperDateLimit().GetYear() )
-            {
-                *target = GetUpperDateLimit();
-                retval = true;
-            }
-            else
-            {
-                *target = m_date;
-            }
-        }
-    }
-    else
+    if ( m_lowdate.IsValid() && *date < m_lowdate )
     {
-        retval = true;
+        *date = m_lowdate;
+        return true;
     }
 
-    return retval;
-}
-
-bool wxGenericCalendarCtrl::ChangeMonth(wxDateTime* target) const
-{
-    bool retval = true;
-
-    if ( !(IsDateInRange(*target)) )
+    if ( m_highdate.IsValid() && *date > m_highdate )
     {
-        retval = false;
-
-        if ( target->GetMonth() < m_date.GetMonth() )
-        {
-            *target = GetLowerDateLimit();
-        }
-        else
-        {
-            *target = GetUpperDateLimit();
-        }
+        *date = m_highdate;
+        return true;
     }
 
-    return retval;
+    return false;
 }
 
 size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
 {
-    size_t retval = date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST
+    size_t retval = date.GetWeekOfMonth(HasFlag(wxCAL_MONDAY_FIRST)
                                    ? wxDateTime::Monday_First
                                    : wxDateTime::Sunday_First);
 
@@ -756,8 +659,7 @@ size_t wxGenericCalendarCtrl::GetWeek(const wxDateTime& date) const
         wxDateTime datetest = wxDateTime(1, tm.mon, tm.year);
 
         // rewind back
-        datetest.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST
-                              ? wxDateTime::Mon : wxDateTime::Sun);
+        datetest.SetToPrevWeekDay(GetWeekStart());
 
         if ( datetest.GetDay() == 1 )
         {
@@ -800,7 +702,7 @@ wxSize wxGenericCalendarCtrl::DoGetBestSize() const
                     + VERT_MARGIN;
 #ifdef __WXMAC__
         // the spin control get clipped otherwise
-        width += 10;
+        width += 25;
 #endif
 
         wxCoord w2 = bestSizeCombo.x + HORZ_MARGIN + GetCharWidth()*6;
@@ -1015,7 +917,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         dc.SetPen(wxPen(m_colHeaderBg, 1, wxPENSTYLE_SOLID));
         dc.DrawRectangle(0, y, GetClientSize().x, m_heightRow);
 
-        bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0;
+        bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST);
         for ( int wd = 0; wd < 7; wd++ )
         {
             size_t n;
@@ -1081,7 +983,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
             {
                 // don't use wxDate::Format() which prepends 0s
                 unsigned int day = date.GetDay();
-                wxString dayStr = wxString::Format(_T("%u"), day);
+                wxString dayStr = wxString::Format(wxT("%u"), day);
                 wxCoord width;
                 dc.GetTextExtent(dayStr, &width, NULL);
 
@@ -1176,7 +1078,7 @@ void wxGenericCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
                             break;
 
                         default:
-                            wxFAIL_MSG(_T("unknown border type"));
+                            wxFAIL_MSG(wxT("unknown border type"));
                     }
                 }
 
@@ -1362,7 +1264,7 @@ bool wxGenericCalendarCtrl::GetDateCoord(const wxDateTime& date, int *day, int *
 
     if ( IsDateShown(date) )
     {
-        bool startOnMonday = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) != 0;
+        bool startOnMonday = HasFlag(wxCAL_MONDAY_FIRST);
 
         // Find day
         *day = date.GetWeekDay();
@@ -1500,7 +1402,7 @@ void wxGenericCalendarCtrl::OnClick(wxMouseEvent& event)
             break;
 
         default:
-            wxFAIL_MSG(_T("unknown hittest code"));
+            wxFAIL_MSG(wxT("unknown hittest code"));
             // fall through
 
         case wxCAL_HITTEST_NOWHERE:
@@ -1573,7 +1475,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
                 *date += wxDateSpan::Week() * (( pos.y - m_rowOffset ) / m_heightRow - 1 );
             }
             if ( wd )
-                *wd = ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) ? wxDateTime::Mon : wxDateTime::Sun;
+                *wd = GetWeekStart();
             return wxCAL_HITTEST_WEEK;
         }
         else    // early exit -> the rest of the function checks for clicks on days
@@ -1590,7 +1492,7 @@ wxCalendarHitTestResult wxGenericCalendarCtrl::HitTest(const wxPoint& pos,
         {
             if ( wd )
             {
-                if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
+                if ( HasFlag(wxCAL_MONDAY_FIRST) )
                 {
                     wday = wday == 6 ? 0 : wday + 1;
                 }
@@ -1649,10 +1551,15 @@ void wxGenericCalendarCtrl::OnMonthChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
-    wxDateTime target = wxDateTime(tm.mday, mon, tm.year);
+    wxDateTime dt(tm.mday, mon, tm.year);
+    if ( AdjustDateToRange(&dt) )
+    {
+        // The date must have been changed to ensure it's in valid range,
+        // reflect this in the month choice control.
+        m_comboMonth->SetSelection(dt.GetMonth());
+    }
 
-    ChangeMonth(&target);
-    SetDateAndNotify(target);
+    SetDateAndNotify(dt);
 }
 
 void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
@@ -1671,18 +1578,15 @@ void wxGenericCalendarCtrl::HandleYearChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
-    wxDateTime target = wxDateTime(tm.mday, tm.mon, year);
-
-    if ( ChangeYear(&target) )
+    wxDateTime dt(tm.mday, tm.mon, year);
+    if ( AdjustDateToRange(&dt) )
     {
-        SetDateAndNotify(target);
-    }
-    else
-    {
-        // In this case we don't want to change the date. That would put us
-        // inside the same year but a strange number of months forward/back..
-        m_spinYear->SetValue(target.GetYear());
+        // As above, if the date was changed to keep it in valid range, its
+        // possibly changed year must be shown in the GUI.
+        m_spinYear->SetValue(dt.GetYear());
     }
+
+    SetDateAndNotify(dt);
 }
 
 void wxGenericCalendarCtrl::OnYearChange(wxSpinEvent& event)
@@ -1716,49 +1620,31 @@ void wxGenericCalendarCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
 
 void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
 {
-    wxDateTime target;
     switch ( event.GetKeyCode() )
     {
-        case _T('+'):
+        case wxT('+'):
         case WXK_ADD:
-            target = m_date + wxDateSpan::Year();
-            if ( ChangeYear(&target) )
-            {
-                SetDateAndNotify(target);
-            }
+            SetDateAndNotify(m_date + wxDateSpan::Year());
             break;
 
-        case _T('-'):
+        case wxT('-'):
         case WXK_SUBTRACT:
-            target = m_date - wxDateSpan::Year();
-            if ( ChangeYear(&target) )
-            {
-                SetDateAndNotify(target);
-            }
+            SetDateAndNotify(m_date - wxDateSpan::Year());
             break;
 
         case WXK_PAGEUP:
-            target = m_date - wxDateSpan::Month();
-            ChangeMonth(&target);
-            SetDateAndNotify(target); // always
+            SetDateAndNotify(m_date - wxDateSpan::Month());
             break;
 
         case WXK_PAGEDOWN:
-            target = m_date + wxDateSpan::Month();
-            ChangeMonth(&target);
-            SetDateAndNotify(target); // always
+            SetDateAndNotify(m_date + wxDateSpan::Month());
             break;
 
         case WXK_RIGHT:
             if ( event.ControlDown() )
             {
-                target = wxDateTime(m_date).SetToNextWeekDay(
-                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
-                                 ? wxDateTime::Sun : wxDateTime::Sat);
-                if ( !IsDateInRange(target) )
-                {
-                    target = GetUpperDateLimit();
-                }
+                wxDateTime target = m_date.SetToNextWeekDay(GetWeekEnd());
+                AdjustDateToRange(&target);
                 SetDateAndNotify(target);
             }
             else
@@ -1768,13 +1654,8 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
         case WXK_LEFT:
             if ( event.ControlDown() )
             {
-                target = wxDateTime(m_date).SetToPrevWeekDay(
-                                 GetWindowStyle() & wxCAL_MONDAY_FIRST
-                                 ? wxDateTime::Mon : wxDateTime::Sun);
-                if ( !IsDateInRange(target) )
-                {
-                    target = GetLowerDateLimit();
-                }
+                wxDateTime target = m_date.SetToPrevWeekDay(GetWeekStart());
+                AdjustDateToRange(&target);
                 SetDateAndNotify(target);
             }
             else
@@ -1815,7 +1696,7 @@ void wxGenericCalendarCtrl::OnChar(wxKeyEvent& event)
 
 void wxGenericCalendarCtrl::SetHoliday(size_t day)
 {
-    wxCHECK_RET( day > 0 && day < 32, _T("invalid day in SetHoliday") );
+    wxCHECK_RET( day > 0 && day < 32, wxT("invalid day in SetHoliday") );
 
     wxCalendarDateAttr *attr = GetAttr(day);
     if ( !attr )
@@ -1842,7 +1723,7 @@ void wxGenericCalendarCtrl::ResetHolidayAttrs()
 
 void wxGenericCalendarCtrl::Mark(size_t day, bool mark)
 {
-    wxCHECK_RET( day > 0 && day < 32, _T("invalid day in Mark") );
+    wxCHECK_RET( day > 0 && day < 32, wxT("invalid day in Mark") );
 
     const wxCalendarDateAttr& m = wxCalendarDateAttr::GetMark();
     if (mark) {