]> git.saurik.com Git - wxWidgets.git/blobdiff - src/generic/calctrl.cpp
fixed false alert in wxRegKey::DeleteSelf
[wxWidgets.git] / src / generic / calctrl.cpp
index 73f74042c9e044a6dfe7ca77f1e09818c5350950..f2ecd265a11c072bfe3d3141844fad8ee39021db 100644 (file)
     #include "wx/settings.h"
     #include "wx/brush.h"
     #include "wx/combobox.h"
+    #include "wx/stattext.h"
 #endif //WX_PRECOMP
 
+// Can only use wxSpinEvent if this is enabled
+#if wxUSE_SPINBTN
+
 #include "wx/calctrl.h"
 
 #define DEBUG_PAINT 0
@@ -80,9 +84,6 @@ BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
 
     EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
     EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
-
-    EVT_CALENDAR_MONTH(-1, wxCalendarCtrl::OnCalMonthChange)
-    EVT_CALENDAR_YEAR(-1, wxCalendarCtrl::OnCalMonthChange)
 END_EVENT_TABLE()
 
 BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
@@ -94,6 +95,7 @@ BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl)
 END_EVENT_TABLE()
 
 IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl)
+IMPLEMENT_DYNAMIC_CLASS(wxCalendarEvent, wxCommandEvent)
 
 // ============================================================================
 // implementation
@@ -120,6 +122,7 @@ wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal)
     }
 
     SetSelection(m_cal->GetDate().GetMonth());
+    SetSize(-1, -1, -1, -1, wxSIZE_AUTO_WIDTH|wxSIZE_AUTO_HEIGHT);
 }
 
 wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal)
@@ -175,12 +178,22 @@ bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent),
                             long style,
                             const wxString& WXUNUSED(name))
 {
-    SetWindowStyle(style | (wxRAISED_BORDER | wxWANTS_CHARS));
+    // needed to get the arrow keys normally used for the dialog navigation
+    SetWindowStyle(style | wxWANTS_CHARS);
 
     m_date = date.IsValid() ? date : wxDateTime::Today();
 
-    m_comboMonth = new wxMonthComboBox(this);
     m_spinYear = new wxYearSpinCtrl(this);
+    m_staticYear = new wxStaticText(GetParent(), -1, m_date.Format(_T("%Y")),
+                                    wxDefaultPosition, wxDefaultSize,
+                                    wxALIGN_CENTRE);
+
+    m_comboMonth = new wxMonthComboBox(this);
+    m_staticMonth = new wxStaticText(GetParent(), -1, m_date.Format(_T("%B")),
+                                     wxDefaultPosition, wxDefaultSize,
+                                     wxALIGN_CENTRE);
+
+    ShowCurrentControls();
 
     wxSize sizeReal;
     if ( size.x == -1 || size.y == -1 )
@@ -225,8 +238,8 @@ bool wxCalendarCtrl::Show(bool show)
         return FALSE;
     }
 
-    m_comboMonth->Show(show);
-    m_spinYear->Show(show);
+    GetMonthControl()->Show(show);
+    GetYearControl()->Show(show);
 
     return TRUE;
 }
@@ -238,32 +251,118 @@ bool wxCalendarCtrl::Enable(bool enable)
         return FALSE;
     }
 
-    m_comboMonth->Enable(enable);
-    m_spinYear->Enable(enable);
+    GetMonthControl()->Enable(enable);
+    GetYearControl()->Enable(enable);
 
     return TRUE;
 }
 
+// ----------------------------------------------------------------------------
+// enable/disable month/year controls
+// ----------------------------------------------------------------------------
+
+void wxCalendarCtrl::ShowCurrentControls()
+{
+    if ( AllowMonthChange() )
+    {
+        m_comboMonth->Show();
+        m_staticMonth->Hide();
+
+        if ( AllowYearChange() )
+        {
+            m_spinYear->Show();
+            m_staticYear->Hide();
+
+            // skip the rest
+            return;
+        }
+    }
+    else
+    {
+        m_comboMonth->Hide();
+        m_staticMonth->Show();
+    }
+
+    // year change not allowed here
+    m_spinYear->Hide();
+    m_staticYear->Show();
+}
+
+wxControl *wxCalendarCtrl::GetMonthControl() const
+{
+    return AllowMonthChange() ? (wxControl *)m_comboMonth : (wxControl *)m_staticMonth;
+}
+
+wxControl *wxCalendarCtrl::GetYearControl() const
+{
+    return AllowYearChange() ? (wxControl *)m_spinYear : (wxControl *)m_staticYear;
+}
+
+void wxCalendarCtrl::EnableYearChange(bool enable)
+{
+    if ( enable != AllowYearChange() )
+    {
+        long style = GetWindowStyle();
+        if ( enable )
+            style &= ~wxCAL_NO_YEAR_CHANGE;
+        else
+            style |= wxCAL_NO_YEAR_CHANGE;
+        SetWindowStyle(style);
+
+        ShowCurrentControls();
+    }
+}
+
+void wxCalendarCtrl::EnableMonthChange(bool enable)
+{
+    if ( enable != AllowMonthChange() )
+    {
+        long style = GetWindowStyle();
+        if ( enable )
+            style &= ~wxCAL_NO_MONTH_CHANGE;
+        else
+            style |= wxCAL_NO_MONTH_CHANGE;
+        SetWindowStyle(style);
+
+        ShowCurrentControls();
+    }
+}
+
 // ----------------------------------------------------------------------------
 // changing date
 // ----------------------------------------------------------------------------
 
 void wxCalendarCtrl::SetDate(const wxDateTime& date)
 {
-    if ( m_date.GetMonth() == date.GetMonth() &&
-         m_date.GetYear() == date.GetYear() )
+    bool sameMonth = m_date.GetMonth() == date.GetMonth(),
+         sameYear = m_date.GetYear() == date.GetYear();
+
+    if ( sameMonth && sameYear )
     {
         // just change the day
         ChangeDay(date);
     }
     else
     {
+        if ( !AllowMonthChange() || (!AllowYearChange() && !sameYear) )
+        {
+            // forbidden
+            return;
+        }
+
         // change everything
         m_date = date;
 
         // update the controls
         m_comboMonth->SetSelection(m_date.GetMonth());
-        m_spinYear->SetValue(m_date.Format(_T("%Y")));
+
+        if ( AllowYearChange() )
+        {
+            m_spinYear->SetValue(m_date.Format(_T("%Y")));
+        }
+
+        // as the month changed, holidays did too
+        SetHolidayAttrs();
 
         // update the calendar
         Refresh();
@@ -346,6 +445,10 @@ size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const
 // 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 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 same space
+
 // the constants used for the layout
 #define VERT_MARGIN     5           // distance between combo and calendar
 #define HORZ_MARGIN    15           //                            spin
@@ -363,6 +466,12 @@ wxSize wxCalendarCtrl::DoGetBestSize() const
 
     height += VERT_MARGIN + wxMax(sizeCombo.y, sizeSpin.y);
 
+    if ( GetWindowStyle() & (wxRAISED_BORDER | wxSUNKEN_BORDER) )
+    {
+        // the border would clip the last line otherwise
+        height += 4;
+    }
+
     return wxSize(width, height);
 }
 
@@ -376,10 +485,15 @@ void wxCalendarCtrl::DoSetSize(int x, int y,
 void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height)
 {
     wxSize sizeCombo = m_comboMonth->GetSize();
+    wxSize sizeStatic = m_staticMonth->GetSize();
+
+    int dy = (sizeCombo.y - sizeStatic.y) / 2;
     m_comboMonth->Move(x, y);
+    m_staticMonth->SetSize(x, y + dy, sizeCombo.x, sizeStatic.y);
 
     int xDiff = sizeCombo.x + HORZ_MARGIN;
     m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y);
+    m_staticYear->SetSize(x + xDiff, y + dy, width - xDiff, sizeStatic.y);
 
     wxSize sizeSpin = m_spinYear->GetSize();
     int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN;
@@ -394,7 +508,7 @@ void wxCalendarCtrl::DoGetPosition(int *x, int *y) const
     // our real top corner is not in this position
     if ( y )
     {
-        *y -= m_comboMonth->GetSize().y + VERT_MARGIN;
+        *y -= GetMonthControl()->GetSize().y + VERT_MARGIN;
     }
 }
 
@@ -405,7 +519,7 @@ void wxCalendarCtrl::DoGetSize(int *width, int *height) const
     // our real height is bigger
     if ( height )
     {
-        *height += m_comboMonth->GetSize().y + VERT_MARGIN;
+        *height += GetMonthControl()->GetSize().y + VERT_MARGIN;
     }
 }
 
@@ -450,7 +564,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     RecalcGeometry();
 
 #if DEBUG_PAINT
-    printf("--- starting to paint, selection: %s, week %u\n",
+    wxLogDebug("--- starting to paint, selection: %s, week %u\n",
            m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(),
            GetWeek(m_date));
 #endif
@@ -459,7 +573,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
     if ( IsExposed(0, 0, 7*m_widthCol, m_heightRow) )
     {
 #if DEBUG_PAINT
-        puts("painting the header");
+        wxLogDebug("painting the header");
 #endif
 
         dc.SetBackgroundMode(wxTRANSPARENT);
@@ -489,7 +603,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
 
     wxDateTime date = GetStartDate();
 #if DEBUG_PAINT
-    printf("starting calendar from %s\n",
+    wxLogDebug("starting calendar from %s\n",
             date.Format("%a %d-%m-%Y %H:%M:%S").c_str());
 #endif
 
@@ -505,7 +619,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         }
 
 #if DEBUG_PAINT
-        printf("painting week %d at y = %d\n", nWeek, y);
+        wxLogDebug("painting week %d at y = %d\n", nWeek, y);
 #endif
 
         for ( size_t wd = 0; wd < 7; wd++ )
@@ -618,7 +732,7 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
         }
     }
 #if DEBUG_PAINT
-    puts("+++ finished painting");
+    wxLogDebug("+++ finished painting");
 #endif
 }
 
@@ -636,8 +750,17 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
     rect.width = 7*m_widthCol;
     rect.height = m_heightRow;
 
+#ifdef __WXMSW__
+    // VZ: for some reason, the selected date seems to occupy more space under
+    //     MSW - this is probably some bug in the font size calculations, but I
+    //     don't know where exactly. This fix is ugly and leads to more
+    //     refreshes than really needed, but without it the selected days
+    //     leaves even more ugly underscores on screen.
+    rect.Inflate(0, 1);
+#endif // MSW
+
 #if DEBUG_PAINT
-    printf("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
+    wxLogDebug("*** refreshing week %d at (%d, %d)-(%d, %d)\n",
            GetWeek(date),
            rect.x, rect.y,
            rect.x + rect.width, rect.y + rect.height);
@@ -752,9 +875,7 @@ void wxCalendarCtrl::OnMonthChange(wxCommandEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(mon, tm.year);
     }
 
-    SetDate(wxDateTime(tm.mday, mon, tm.year));
-
-    GenerateEvents(wxEVT_CALENDAR_MONTH_CHANGED, wxEVT_CALENDAR_SEL_CHANGED);
+    SetDateAndNotify(wxDateTime(tm.mday, mon, tm.year));
 }
 
 void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
@@ -767,9 +888,7 @@ void wxCalendarCtrl::OnYearChange(wxSpinEvent& event)
         tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year);
     }
 
-    SetDate(wxDateTime(tm.mday, tm.mon, year));
-
-    GenerateEvents(wxEVT_CALENDAR_YEAR_CHANGED, wxEVT_CALENDAR_SEL_CHANGED);
+    SetDateAndNotify(wxDateTime(tm.mday, tm.mon, year));
 }
 
 // ----------------------------------------------------------------------------
@@ -848,13 +967,6 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
 // holidays handling
 // ----------------------------------------------------------------------------
 
-void wxCalendarCtrl::OnCalMonthChange(wxCalendarEvent& event)
-{
-    SetHolidayAttrs();
-
-    event.Skip();
-}
-
 void wxCalendarCtrl::EnableHolidayDisplay(bool display)
 {
     long style = GetWindowStyle();
@@ -935,3 +1047,6 @@ wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
 {
     m_date = cal->GetDate();
 }
+
+#endif // wxUSE_SPINBTN
+