added support for several new events in wxCalendarCtrl: clicking on week
days in the header, double clicking git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5153 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -12,6 +12,22 @@
|
|||||||
#ifndef _WX_CALCTRL_H
|
#ifndef _WX_CALCTRL_H
|
||||||
#define _WX_CALCTRL_H
|
#define _WX_CALCTRL_H
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// constants
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// return values for the HitTest() method
|
||||||
|
enum wxCalendarHitTestResult
|
||||||
|
{
|
||||||
|
wxCAL_HITTEST_NOWHERE, // outside of anything
|
||||||
|
wxCAL_HITTEST_HEADER, // on the header (weekdays)
|
||||||
|
wxCAL_HITTEST_DAY // on a day in the calendar
|
||||||
|
};
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// wxCalendarCtrl
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// so far we only have a generic version, so keep it simple
|
// so far we only have a generic version, so keep it simple
|
||||||
#include "wx/generic/calctrl.h"
|
#include "wx/generic/calctrl.h"
|
||||||
|
|
||||||
@@ -21,19 +37,27 @@
|
|||||||
|
|
||||||
class WXDLLEXPORT wxCalendarEvent : public wxCommandEvent
|
class WXDLLEXPORT wxCalendarEvent : public wxCommandEvent
|
||||||
{
|
{
|
||||||
|
friend class wxCalendarCtrl;
|
||||||
public:
|
public:
|
||||||
wxCalendarEvent() { }
|
wxCalendarEvent() { Init(); }
|
||||||
wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type);
|
wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type);
|
||||||
|
|
||||||
const wxDateTime& GetDate() const { return m_date; }
|
const wxDateTime& GetDate() const { return m_date; }
|
||||||
|
wxDateTime::WeekDay GetWeekDay() const { return m_wday; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void Init();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
wxDateTime m_date;
|
wxDateTime m_date;
|
||||||
|
wxDateTime::WeekDay m_wday;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define EVT_CALENDAR(id, fn) { wxEVT_CALENDAR_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
#define EVT_CALENDAR(id, fn) { wxEVT_CALENDAR_DOUBLECLICKED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
|
#define EVT_CALENDAR_SEL_CHANGED(id, fn) { wxEVT_CALENDAR_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
#define EVT_CALENDAR_DAY(id, fn) { wxEVT_CALENDAR_DAY_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
#define EVT_CALENDAR_DAY(id, fn) { wxEVT_CALENDAR_DAY_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
#define EVT_CALENDAR_MONTH(id, fn) { wxEVT_CALENDAR_MONTH_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
#define EVT_CALENDAR_MONTH(id, fn) { wxEVT_CALENDAR_MONTH_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
#define EVT_CALENDAR_YEAR(id, fn) { wxEVT_CALENDAR_YEAR_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
#define EVT_CALENDAR_YEAR(id, fn) { wxEVT_CALENDAR_YEAR_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
|
#define EVT_CALENDAR_WEEKDAY_CLICKED(id, fn) { wxEVT_CALENDAR_WEEKDAY_CLICKED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxCommandEventFunction) & fn, (wxObject *) NULL },
|
||||||
|
|
||||||
#endif // _WX_CALCTRL_H
|
#endif // _WX_CALCTRL_H
|
||||||
|
@@ -255,8 +255,10 @@ const wxEventType wxEVT_CALENDAR_SEL_CHANGED = wxEVT_FIRST + 950;
|
|||||||
const wxEventType wxEVT_CALENDAR_DAY_CHANGED = wxEVT_FIRST + 951;
|
const wxEventType wxEVT_CALENDAR_DAY_CHANGED = wxEVT_FIRST + 951;
|
||||||
const wxEventType wxEVT_CALENDAR_MONTH_CHANGED = wxEVT_FIRST + 952;
|
const wxEventType wxEVT_CALENDAR_MONTH_CHANGED = wxEVT_FIRST + 952;
|
||||||
const wxEventType wxEVT_CALENDAR_YEAR_CHANGED = wxEVT_FIRST + 953;
|
const wxEventType wxEVT_CALENDAR_YEAR_CHANGED = wxEVT_FIRST + 953;
|
||||||
|
const wxEventType wxEVT_CALENDAR_DOUBLECLICKED = wxEVT_FIRST + 954;
|
||||||
|
const wxEventType wxEVT_CALENDAR_WEEKDAY_CLICKED = wxEVT_FIRST + 955;
|
||||||
|
|
||||||
const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
|
const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
|
||||||
|
|
||||||
/* Compatibility */
|
/* Compatibility */
|
||||||
|
|
||||||
|
@@ -30,6 +30,9 @@
|
|||||||
|
|
||||||
class WXDLLEXPORT wxCalendarCtrl : public wxControl
|
class WXDLLEXPORT wxCalendarCtrl : public wxControl
|
||||||
{
|
{
|
||||||
|
friend class wxMonthComboBox;
|
||||||
|
friend class wxYearSpinCtrl;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// construction
|
// construction
|
||||||
wxCalendarCtrl() { Init(); }
|
wxCalendarCtrl() { Init(); }
|
||||||
@@ -62,9 +65,12 @@ public:
|
|||||||
void SetDate(const wxDateTime& date);
|
void SetDate(const wxDateTime& date);
|
||||||
const wxDateTime& GetDate() const { return m_date; }
|
const wxDateTime& GetDate() const { return m_date; }
|
||||||
|
|
||||||
// returns TRUE if the given point is on a day and fills date with its
|
// returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
|
||||||
// value
|
// with the corresponding value (none for NOWHERE, the date for DAY and wd
|
||||||
bool HitTest(const wxPoint& pos, wxDateTime *date);
|
// for HEADER)
|
||||||
|
wxCalendarHitTestResult HitTest(const wxPoint& pos,
|
||||||
|
wxDateTime *date = NULL,
|
||||||
|
wxDateTime::WeekDay *wd = NULL);
|
||||||
|
|
||||||
// implementation only from now on
|
// implementation only from now on
|
||||||
// -------------------------------
|
// -------------------------------
|
||||||
@@ -73,17 +79,18 @@ public:
|
|||||||
virtual bool Enable(bool enable = TRUE);
|
virtual bool Enable(bool enable = TRUE);
|
||||||
virtual bool Show(bool show = TRUE);
|
virtual bool Show(bool show = TRUE);
|
||||||
|
|
||||||
// event handlers
|
|
||||||
void OnPaint(wxPaintEvent& event);
|
|
||||||
void OnClick(wxMouseEvent& event);
|
|
||||||
void OnChar(wxKeyEvent& event);
|
|
||||||
void OnMonthChange(wxCommandEvent& event);
|
|
||||||
void OnYearChange(wxSpinEvent& event);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// common part of all ctors
|
// common part of all ctors
|
||||||
void Init();
|
void Init();
|
||||||
|
|
||||||
|
// event handlers
|
||||||
|
void OnPaint(wxPaintEvent& event);
|
||||||
|
void OnClick(wxMouseEvent& event);
|
||||||
|
void OnDClick(wxMouseEvent& event);
|
||||||
|
void OnChar(wxKeyEvent& event);
|
||||||
|
void OnMonthChange(wxCommandEvent& event);
|
||||||
|
void OnYearChange(wxSpinEvent& event);
|
||||||
|
|
||||||
// override some base class virtuals
|
// override some base class virtuals
|
||||||
virtual wxSize DoGetBestSize() const;
|
virtual wxSize DoGetBestSize() const;
|
||||||
virtual void DoGetPosition(int *x, int *y) const;
|
virtual void DoGetPosition(int *x, int *y) const;
|
||||||
@@ -112,8 +119,9 @@ private:
|
|||||||
// change the date inside the same month/year
|
// change the date inside the same month/year
|
||||||
void ChangeDay(const wxDateTime& date);
|
void ChangeDay(const wxDateTime& date);
|
||||||
|
|
||||||
// generate a calendar event
|
// generate the given calendar event and a "selection changed" one if
|
||||||
void GenerateEvent(wxEventType type);
|
// selChanged is TRUE
|
||||||
|
void GenerateEvent(wxEventType type, bool selChanged = TRUE);
|
||||||
|
|
||||||
// the subcontrols
|
// the subcontrols
|
||||||
wxComboBox *m_comboMonth;
|
wxComboBox *m_comboMonth;
|
||||||
|
@@ -113,6 +113,8 @@ public:
|
|||||||
void OnEnableAll(wxCommandEvent& event);
|
void OnEnableAll(wxCommandEvent& event);
|
||||||
void OnChangeColour(wxCommandEvent& event);
|
void OnChangeColour(wxCommandEvent& event);
|
||||||
|
|
||||||
|
void OnCalendar(wxCalendarEvent& event);
|
||||||
|
void OnCalendarWeekDayClick(wxCalendarEvent& event);
|
||||||
void OnCalendarChange(wxCalendarEvent& event);
|
void OnCalendarChange(wxCalendarEvent& event);
|
||||||
|
|
||||||
wxListBox *m_listbox,
|
wxListBox *m_listbox,
|
||||||
@@ -380,7 +382,11 @@ EVT_SPINCTRL (ID_SPINCTRL, MyPanel::OnSpinCtrl)
|
|||||||
#endif // wxUSE_SPINCTRL
|
#endif // wxUSE_SPINCTRL
|
||||||
EVT_BUTTON (ID_BUTTON_LABEL, MyPanel::OnUpdateLabel)
|
EVT_BUTTON (ID_BUTTON_LABEL, MyPanel::OnUpdateLabel)
|
||||||
EVT_CHECKBOX (ID_CHANGE_COLOUR, MyPanel::OnChangeColour)
|
EVT_CHECKBOX (ID_CHANGE_COLOUR, MyPanel::OnChangeColour)
|
||||||
EVT_CALENDAR (ID_CALENDAR, MyPanel::OnCalendarChange)
|
|
||||||
|
EVT_CALENDAR (ID_CALENDAR, MyPanel::OnCalendar)
|
||||||
|
EVT_CALENDAR_SEL_CHANGED(ID_CALENDAR, MyPanel::OnCalendarChange)
|
||||||
|
EVT_CALENDAR_WEEKDAY_CLICKED(ID_CALENDAR, MyPanel::OnCalendarWeekDayClick)
|
||||||
|
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
|
MyPanel::MyPanel( wxFrame *frame, int x, int y, int w, int h )
|
||||||
@@ -758,6 +764,12 @@ void MyPanel::OnPageChanged( wxNotebookEvent &event )
|
|||||||
*m_text << "Notebook selection is " << event.GetSelection() << "\n";
|
*m_text << "Notebook selection is " << event.GetSelection() << "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MyPanel::OnCalendar(wxCalendarEvent& event)
|
||||||
|
{
|
||||||
|
*m_text << "Selected " << event.GetDate().FormatISODate() <<
|
||||||
|
" from calendar\n";
|
||||||
|
}
|
||||||
|
|
||||||
void MyPanel::OnCalendarChange(wxCalendarEvent& event)
|
void MyPanel::OnCalendarChange(wxCalendarEvent& event)
|
||||||
{
|
{
|
||||||
wxString s;
|
wxString s;
|
||||||
@@ -766,6 +778,13 @@ void MyPanel::OnCalendarChange(wxCalendarEvent& event)
|
|||||||
m_date->SetLabel(s);
|
m_date->SetLabel(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MyPanel::OnCalendarWeekDayClick(wxCalendarEvent& event)
|
||||||
|
{
|
||||||
|
*m_text << "Clicked on "
|
||||||
|
<< wxDateTime::GetWeekDayName(event.GetWeekDay())
|
||||||
|
<< "\n";
|
||||||
|
}
|
||||||
|
|
||||||
void MyPanel::OnChangeColour(wxCommandEvent& WXUNUSED(event))
|
void MyPanel::OnChangeColour(wxCommandEvent& WXUNUSED(event))
|
||||||
{
|
{
|
||||||
static wxColour s_colOld;
|
static wxColour s_colOld;
|
||||||
|
@@ -78,6 +78,7 @@ BEGIN_EVENT_TABLE(wxCalendarCtrl, wxControl)
|
|||||||
EVT_CHAR(wxCalendarCtrl::OnChar)
|
EVT_CHAR(wxCalendarCtrl::OnChar)
|
||||||
|
|
||||||
EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
|
EVT_LEFT_DOWN(wxCalendarCtrl::OnClick)
|
||||||
|
EVT_LEFT_DCLICK(wxCalendarCtrl::OnDClick)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
|
BEGIN_EVENT_TABLE(wxMonthComboBox, wxComboBox)
|
||||||
@@ -551,44 +552,91 @@ void wxCalendarCtrl::RefreshDate(const wxDateTime& date)
|
|||||||
// mouse handling
|
// mouse handling
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void wxCalendarCtrl::OnClick(wxMouseEvent& event)
|
void wxCalendarCtrl::OnDClick(wxMouseEvent& event)
|
||||||
{
|
{
|
||||||
RecalcGeometry();
|
if ( HitTest(event.GetPosition()) != wxCAL_HITTEST_DAY )
|
||||||
|
|
||||||
wxDateTime date;
|
|
||||||
if ( !HitTest(event.GetPosition(), &date) )
|
|
||||||
{
|
{
|
||||||
event.Skip();
|
event.Skip();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ChangeDay(date);
|
GenerateEvent(wxEVT_CALENDAR_DOUBLECLICKED, FALSE);
|
||||||
|
|
||||||
GenerateEvent(wxEVT_CALENDAR_DAY_CHANGED);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxCalendarCtrl::HitTest(const wxPoint& pos, wxDateTime *date)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wxCalendarHitTestResult wxCalendarCtrl::HitTest(const wxPoint& pos,
|
||||||
|
wxDateTime *date,
|
||||||
|
wxDateTime::WeekDay *wd)
|
||||||
{
|
{
|
||||||
RecalcGeometry();
|
RecalcGeometry();
|
||||||
|
|
||||||
|
int wday = pos.x / m_widthCol;
|
||||||
|
|
||||||
wxCoord y = pos.y;
|
wxCoord y = pos.y;
|
||||||
if ( y < m_heightRow )
|
if ( y < m_heightRow )
|
||||||
return FALSE;
|
{
|
||||||
|
if ( wd )
|
||||||
|
{
|
||||||
|
if ( GetWindowStyle() & wxCAL_MONDAY_FIRST )
|
||||||
|
{
|
||||||
|
wday = wday == 6 ? 0 : wday + 1;
|
||||||
|
}
|
||||||
|
|
||||||
y -= m_heightRow;
|
*wd = (wxDateTime::WeekDay)wday;
|
||||||
int week = y / m_heightRow,
|
}
|
||||||
wday = pos.x / m_widthCol;
|
|
||||||
|
|
||||||
|
return wxCAL_HITTEST_HEADER;
|
||||||
|
}
|
||||||
|
|
||||||
|
int week = (y - m_heightRow) / m_heightRow;
|
||||||
if ( week >= 6 || wday >= 7 )
|
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();
|
if ( IsDateShown(dt) )
|
||||||
*date += wxDateSpan::Days(7*week + wday);
|
{
|
||||||
|
if ( date )
|
||||||
|
*date = dt;
|
||||||
|
|
||||||
return IsDateShown(*date);
|
return wxCAL_HITTEST_DAY;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return wxCAL_HITTEST_NOWHERE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -697,15 +745,24 @@ void wxCalendarCtrl::OnChar(wxKeyEvent& event)
|
|||||||
// wxCalendarEvent
|
// 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
|
// we're called for a change in some particular date field but we always
|
||||||
// also generate a generic "changed" event
|
// also generate a generic "changed" event
|
||||||
wxCalendarEvent event(this, type);
|
wxCalendarEvent event(this, type);
|
||||||
wxCalendarEvent event2(this, wxEVT_CALENDAR_SEL_CHANGED);
|
|
||||||
|
|
||||||
(void)GetEventHandler()->ProcessEvent(event);
|
(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)
|
wxCalendarEvent::wxCalendarEvent(wxCalendarCtrl *cal, wxEventType type)
|
||||||
|
Reference in New Issue
Block a user