X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/15807266c61a2e820c3a15c0701159c58b003af3..24e1f1ca21c6f2cd15c69bcbbdc79f7848c7fa29:/src/generic/calctrl.cpp diff --git a/src/generic/calctrl.cpp b/src/generic/calctrl.cpp index 5419de38ce..78b6c57f49 100644 --- a/src/generic/calctrl.cpp +++ b/src/generic/calctrl.cpp @@ -36,6 +36,38 @@ #include "wx/calctrl.h" +#define DEBUG_PAINT 0 + +// ---------------------------------------------------------------------------- +// private classes +// ---------------------------------------------------------------------------- + +class wxMonthComboBox : public wxComboBox +{ +public: + wxMonthComboBox(wxCalendarCtrl *cal); + + void OnMonthChange(wxCommandEvent& event) { m_cal->OnMonthChange(event); } + +private: + wxCalendarCtrl *m_cal; + + DECLARE_EVENT_TABLE() +}; + +class wxYearSpinCtrl : public wxSpinCtrl +{ +public: + wxYearSpinCtrl(wxCalendarCtrl *cal); + + void OnYearChange(wxSpinEvent& event) { m_cal->OnYearChange(event); } + +private: + wxCalendarCtrl *m_cal; + + DECLARE_EVENT_TABLE() +}; + // ---------------------------------------------------------------------------- // wxWin macros // ---------------------------------------------------------------------------- @@ -46,9 +78,15 @@ BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl) EVT_CHAR(wxCalendarCtrl::OnChar) EVT_LEFT_DOWN(wxCalendarCtrl::OnClick) + EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick) +END_EVENT_TABLE() + +BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox) + EVT_COMBOBOX(-1, wxMonthComboBox::OnMonthChange) +END_EVENT_TABLE() - EVT_COMBOBOX(-1, wxCalendarCtrl::OnMonthChange) - EVT_SPINCTRL(-1, wxCalendarCtrl::OnYearChange) +BEGIN_EVENT_TABLE(wxYearSpinCtrl, wxSpinCtrl) + EVT_SPINCTRL(-1, wxYearSpinCtrl::OnYearChange) END_EVENT_TABLE() IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl) @@ -57,6 +95,40 @@ IMPLEMENT_DYNAMIC_CLASS(wxCalendarCtrl, wxControl) // implementation // ============================================================================ +// ---------------------------------------------------------------------------- +// wxMonthComboBox and wxYearSpinCtrl +// ---------------------------------------------------------------------------- + +wxMonthComboBox::wxMonthComboBox(wxCalendarCtrl *cal) + : wxComboBox(cal->GetParent(), -1, + wxEmptyString, + wxDefaultPosition, + wxDefaultSize, + 0, NULL, + wxCB_READONLY) +{ + m_cal = cal; + + wxDateTime::Month m; + for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) + { + Append(wxDateTime::GetMonthName(m)); + } + + SetSelection(m_cal->GetDate().GetMonth()); +} + +wxYearSpinCtrl::wxYearSpinCtrl(wxCalendarCtrl *cal) + : wxSpinCtrl(cal->GetParent(), -1, + cal->GetDate().Format(_T("%Y")), + wxDefaultPosition, + wxDefaultSize, + wxSP_ARROW_KEYS, + -4300, 10000, cal->GetDate().GetYear()) +{ + m_cal = cal; +} + // ---------------------------------------------------------------------------- // wxCalendarCtrl // ---------------------------------------------------------------------------- @@ -76,40 +148,20 @@ void wxCalendarCtrl::Init() } } -bool wxCalendarCtrl::Create(wxWindow *parent, - wxWindowID id, +bool wxCalendarCtrl::Create(wxWindow * WXUNUSED(parent), + wxWindowID WXUNUSED(id), const wxDateTime& date, - const wxPoint& pos, + const wxPoint& WXUNUSED(pos), const wxSize& size, long style, - const wxString& name) + const wxString& WXUNUSED(name)) { - m_date = date.IsValid() ? date : wxDateTime::Today(); + SetWindowStyle(style | (wxRAISED_BORDER | wxWANTS_CHARS)); - wxString monthNames[12]; - wxDateTime::Month m; - for ( m = wxDateTime::Jan; m < wxDateTime::Inv_Month; wxNextMonth(m) ) - { - monthNames[m] = wxDateTime::GetMonthName(m); - } - - m_comboMonth = new wxComboBox(parent, -1, - monthNames[m_date.GetMonth()], - wxDefaultPosition, - wxDefaultSize, - WXSIZEOF(monthNames), monthNames, - wxCB_READONLY); - - m_spinYear = new wxSpinCtrl(parent, -1, - m_date.Format(_T("%Y")), - wxDefaultPosition, - wxDefaultSize, - wxSP_ARROW_KEYS, - -4300, 10000, m_date.GetYear()); + m_date = date.IsValid() ? date : wxDateTime::Today(); - // we want to process the events from these controls - m_comboMonth->PushEventHandler(this); - m_spinYear->PushEventHandler(this); + m_comboMonth = new wxMonthComboBox(this); + m_spinYear = new wxYearSpinCtrl(this); wxSize sizeReal; if ( size.x == -1 || size.y == -1 ) @@ -133,6 +185,44 @@ bool wxCalendarCtrl::Create(wxWindow *parent, return TRUE; } +wxCalendarCtrl::~wxCalendarCtrl() +{ +#if 0 + m_comboMonth->PopEventHandler(); + m_spinYear->PopEventHandler(); +#endif // 0 +} + +// ---------------------------------------------------------------------------- +// forward wxWin functions to subcontrols +// ---------------------------------------------------------------------------- + +bool wxCalendarCtrl::Show(bool show) +{ + if ( !wxControl::Show(show) ) + { + return FALSE; + } + + m_comboMonth->Show(show); + m_spinYear->Show(show); + + return TRUE; +} + +bool wxCalendarCtrl::Enable(bool enable) +{ + if ( !wxControl::Enable(enable) ) + { + return FALSE; + } + + m_comboMonth->Enable(enable); + m_spinYear->Enable(enable); + + return TRUE; +} + // ---------------------------------------------------------------------------- // changing date // ---------------------------------------------------------------------------- @@ -207,14 +297,13 @@ wxDateTime wxCalendarCtrl::GetStartDate() const wxDateTime::Tm tm = m_date.GetTm(); wxDateTime date = wxDateTime(1, tm.mon, tm.year); - if ( date.GetWeekDay() != wxDateTime::Sun ) - { - date.SetToPrevWeekDay(wxDateTime::Sun); - // be sure to do it or it might gain 1 hour if DST changed in between - date.ResetTime(); - } - //else: we already have it + // rewind back + date.SetToPrevWeekDay(GetWindowStyle() & wxCAL_MONDAY_FIRST + ? wxDateTime::Mon : wxDateTime::Sun); + + // be sure to do it or it might gain 1 hour if DST changed in between + date.ResetTime(); return date; } @@ -226,7 +315,9 @@ bool wxCalendarCtrl::IsDateShown(const wxDateTime& date) const size_t wxCalendarCtrl::GetWeek(const wxDateTime& date) const { - return date.GetWeekOfMonth(wxDateTime::Sunday_First); + return date.GetWeekOfMonth(GetWindowStyle() & wxCAL_MONDAY_FIRST + ? wxDateTime::Monday_First + : wxDateTime::Sunday_First); } // ---------------------------------------------------------------------------- @@ -270,7 +361,7 @@ void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height) m_comboMonth->Move(x, y); int xDiff = sizeCombo.x + HORZ_MARGIN; - m_spinYear->SetSize(x + xDiff, y, width - xDiff, -1); + m_spinYear->SetSize(x + xDiff, y, width - xDiff, sizeCombo.y); wxSize sizeSpin = m_spinYear->GetSize(); int yDiff = wxMax(sizeSpin.y, sizeCombo.y) + VERT_MARGIN; @@ -278,6 +369,28 @@ void wxCalendarCtrl::DoMoveWindow(int x, int y, int width, int height) wxControl::DoMoveWindow(x, y + yDiff, width, height - yDiff); } +void wxCalendarCtrl::DoGetPosition(int *x, int *y) const +{ + wxControl::DoGetPosition(x, y); + + // our real top corner is not in this position + if ( y ) + { + *y -= m_comboMonth->GetSize().y + VERT_MARGIN; + } +} + +void wxCalendarCtrl::DoGetSize(int *width, int *height) const +{ + wxControl::DoGetSize(width, height); + + // our real height is bigger + if ( height ) + { + *height += m_comboMonth->GetSize().y + VERT_MARGIN; + } +} + void wxCalendarCtrl::RecalcGeometry() { if ( m_widthCol != 0 ) @@ -310,33 +423,43 @@ void wxCalendarCtrl::RecalcGeometry() // drawing // ---------------------------------------------------------------------------- -void wxCalendarCtrl::OnPaint(wxPaintEvent& event) +void wxCalendarCtrl::OnPaint(wxPaintEvent& WXUNUSED(event)) { wxPaintDC dc(this); - wxDateTime::WeekDay wd; - dc.SetFont(m_font); RecalcGeometry(); +#if DEBUG_PAINT printf("--- starting to paint, selection: %s, week %u\n", m_date.Format("%a %d-%m-%Y %H:%M:%S").c_str(), GetWeek(m_date)); +#endif // first draw the week days if ( IsExposed(0, 0, 7*m_widthCol, m_heightRow) ) { +#if DEBUG_PAINT puts("painting the header"); +#endif dc.SetTextForeground(*wxBLUE); dc.SetBrush(wxBrush(*wxLIGHT_GREY, wxSOLID)); dc.SetBackgroundMode(wxTRANSPARENT); dc.SetPen(*wxLIGHT_GREY_PEN); dc.DrawRectangle(0, 0, 7*m_widthCol, m_heightRow); - for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) + + bool startOnMonday = (GetWindowStyle() & wxCAL_MONDAY_FIRST) != 0; + for ( size_t wd = 0; wd < 7; wd++ ) { - dc.DrawText(m_weekdays[wd], wd*m_widthCol + 1, 0); + size_t n; + if ( startOnMonday ) + n = wd == 6 ? 0 : wd + 1; + else + n = wd; + + dc.DrawText(m_weekdays[n], wd*m_widthCol + 1, 0); } } @@ -347,8 +470,10 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& event) wxCoord y = m_heightRow; wxDateTime date = GetStartDate(); +#if DEBUG_PAINT printf("starting calendar from %s\n", date.Format("%a %d-%m-%Y %H:%M:%S").c_str()); +#endif dc.SetBackgroundMode(wxSOLID); for ( size_t nWeek = 1; nWeek <= 6; nWeek++, y += m_heightRow ) @@ -360,13 +485,16 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& event) continue; } - + +#if DEBUG_PAINT printf("painting week %d at y = %d\n", nWeek, y); +#endif - for ( wd = wxDateTime::Sun; wd < wxDateTime::Inv_WeekDay; wxNextWDay(wd) ) + for ( size_t wd = 0; wd < 7; wd++ ) { if ( IsDateShown(date) ) { + // don't use wxDate::Format() which prepends 0s wxString day = wxString::Format(_T("%u"), date.GetDay()); wxCoord width; dc.GetTextExtent(day, &width, (wxCoord *)NULL); @@ -391,8 +519,9 @@ void wxCalendarCtrl::OnPaint(wxPaintEvent& event) date += wxDateSpan::Day(); } } - +#if DEBUG_PAINT puts("+++ finished painting"); +#endif } void wxCalendarCtrl::RefreshDate(const wxDateTime& date) @@ -409,10 +538,12 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date) rect.width = 7*m_widthCol; rect.height = m_heightRow; +#if DEBUG_PAINT printf("*** refreshing week %d at (%d, %d)-(%d, %d)\n", GetWeek(date), rect.x, rect.y, rect.x + rect.width, rect.y + rect.height); +#endif Refresh(TRUE, &rect); } @@ -421,44 +552,91 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date) // mouse handling // ---------------------------------------------------------------------------- -void wxCalendarCtrl::OnClick(wxMouseEvent& event) +void wxCalendarCtrl::OnDClick(wxMouseEvent& event) { - RecalcGeometry(); - - wxDateTime date; - if ( !HitTest(event.GetPosition(), &date) ) + if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY ) { event.Skip(); } else { - ChangeDay(date); + GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED, FALSE); + } +} - GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED); +void wxCalendarCtrl::OnClick(wxMouseEvent& event) +{ + wxDateTime date; + wxDateTime::WeekDay wday; + switch ( HitTest(event.GetPosition(), &date, &wday) ) + { + case wxCAL_HITTEST_DAY: + ChangeDay(date); + + GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED); + break; + + case wxCAL_HITTEST_HEADER: + { + wxCalendarEvent event(this, wxEVT_CALENDAR_WEEKDAY_CLICKED); + event.m_wday = wday; + (void)GetEventHandler()->ProcessEvent(event); + } + break; + + default: + wxFAIL_MSG(_T("unknown hittest code")); + // fall through + + case wxCAL_HITTEST_NOWHERE: + event.Skip(); + break; } } -bool wxCalendarCtrl::HitTest(const wxPoint& pos, wxDateTime *date) +wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos, + wxDateTime *date, + wxDateTime::WeekDay *wd) { RecalcGeometry(); + int wday = pos.x / m_widthCol; + wxCoord y = pos.y; if ( y < m_heightRow ) - return FALSE; + { + if ( wd ) + { + if ( GetWindowStyle() & wxCAL_MONDAY_FIRST ) + { + wday = wday == 6 ? 0 : wday + 1; + } - y -= m_heightRow; - int week = y / m_heightRow, - wday = pos.x / m_widthCol; + *wd = (wxDateTime::WeekDay)wday; + } + return wxCAL_HITTEST_HEADER; + } + + int week = (y - m_heightRow) / m_heightRow; if ( week >= 6 || wday >= 7 ) - return FALSE; + { + return wxCAL_HITTEST_NOWHERE; + } - wxCHECK_MSG( date, FALSE, _T("bad pointer in wxCalendarCtrl::HitTest") ); + wxDateTime dt = GetStartDate() + wxDateSpan::Days(7*week + wday); - *date = GetStartDate(); - *date += wxDateSpan::Days(7*week + wday); + if ( IsDateShown(dt) ) + { + if ( date ) + *date = dt; - return IsDateShown(*date); + return wxCAL_HITTEST_DAY; + } + else + { + return wxCAL_HITTEST_NOWHERE; + } } // ---------------------------------------------------------------------------- @@ -484,7 +662,7 @@ void wxCalendarCtrl::OnYearChange(wxSpinEvent& event) { wxDateTime::Tm tm = m_date.GetTm(); - int year = event.GetInt(); + int year = (int)event.GetInt(); if ( tm.mday > wxDateTime::GetNumberOfDays(tm.mon, year) ) { tm.mday = wxDateTime::GetNumberOfDays(tm.mon, year); @@ -513,20 +691,30 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event) SetDateAndNotify(m_date - wxDateSpan::Year()); break; - case WXK_PAGEDOWN: - SetDateAndNotify(m_date + wxDateSpan::Year()); + case WXK_PRIOR: + SetDateAndNotify(m_date - wxDateSpan::Month()); break; - case WXK_PAGEUP: - SetDateAndNotify(m_date - wxDateSpan::Year()); + case WXK_NEXT: + SetDateAndNotify(m_date + wxDateSpan::Month()); break; case WXK_RIGHT: - SetDateAndNotify(m_date + wxDateSpan::Day()); + if ( event.ControlDown() ) + SetDateAndNotify(wxDateTime(m_date).SetToNextWeekDay( + GetWindowStyle() & wxCAL_MONDAY_FIRST + ? wxDateTime::Sun : wxDateTime::Sat)); + else + SetDateAndNotify(m_date + wxDateSpan::Day()); break; case WXK_LEFT: - SetDateAndNotify(m_date - wxDateSpan::Day()); + if ( event.ControlDown() ) + SetDateAndNotify(wxDateTime(m_date).SetToPrevWeekDay( + GetWindowStyle() & wxCAL_MONDAY_FIRST + ? wxDateTime::Mon : wxDateTime::Sun)); + else + SetDateAndNotify(m_date - wxDateSpan::Day()); break; case WXK_UP: @@ -538,7 +726,14 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event) break; case WXK_HOME: - SetDateAndNotify(wxDateTime::Today()); + if ( event.ControlDown() ) + SetDateAndNotify(wxDateTime::Today()); + else + SetDateAndNotify(wxDateTime(1, m_date.GetMonth(), m_date.GetYear())); + break; + + case WXK_END: + SetDateAndNotify(wxDateTime(m_date).SetToLastMonthDay()); break; default: @@ -550,15 +745,24 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event) // wxCalendarEvent // ---------------------------------------------------------------------------- -void wxCalendarCtrl::GenerateEvent(wxEventType type) +void wxCalendarCtrl::GenerateEvent(wxEventType type, bool selChanged) { // we're called for a change in some particular date field but we always // also generate a generic "changed" event wxCalendarEvent event(this, type); - wxCalendarEvent event2(this, wxEVT_CALENDAR_SEL_CHANGED); - (void)GetEventHandler()->ProcessEvent(event); - (void)GetEventHandler()->ProcessEvent(event2); + + if ( selChanged ) + { + wxCalendarEvent event2(this, wxEVT_CALENDAR_SEL_CHANGED); + + (void)GetEventHandler()->ProcessEvent(event2); + } +} + +void wxCalendarEvent::Init() +{ + m_wday = wxDateTime::Inv_WeekDay; } wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)