1. wxDateTimeHolidayAuthority class for calculating the holidays added

2. wxCalendarCtrl adjustments and some new features


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5182 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2000-01-02 23:17:55 +00:00
parent c88275cb7d
commit 4f6aed9c6a
11 changed files with 817 additions and 60 deletions

View File

@@ -9,9 +9,18 @@
// Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////////
/*
TODO
1. implement multiple selections for date ranges
2. background bitmap for the calendar?
*/
#ifndef _WX_CALCTRL_H
#define _WX_CALCTRL_H
#include "wx/datetime.h"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
@@ -24,17 +33,85 @@ enum wxCalendarHitTestResult
wxCAL_HITTEST_DAY // on a day in the calendar
};
// border types for a date
enum wxCalendarDateBorder
{
wxCAL_BORDER_NONE, // no border (default)
wxCAL_BORDER_SQUARE, // a rectangular border
wxCAL_BORDER_ROUND // a round border
};
// ----------------------------------------------------------------------------
// wxCalendarCtrl
// wxCalendarDateAttr: custom attributes for a calendar date
// ----------------------------------------------------------------------------
// so far we only have a generic version, so keep it simple
#include "wx/generic/calctrl.h"
class WXDLLEXPORT wxCalendarDateAttr
{
public:
// ctors
wxCalendarDateAttr() { Init(); }
wxCalendarDateAttr(const wxColour& colText,
const wxColour& colBack = wxNullColour,
const wxColour& colBorder = wxNullColour,
const wxFont& font = wxNullFont,
wxCalendarDateBorder border = wxCAL_BORDER_NONE)
: m_colText(colText), m_colBack(colBack),
m_colBorder(colBorder), m_font(font)
{
Init(border);
}
wxCalendarDateAttr(wxCalendarDateBorder border,
const wxColour& colBorder = wxNullColour)
: m_colBorder(colBorder)
{
Init(border);
}
// setters
void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
void SetBorderColour(const wxColour& col) { m_colBorder = col; }
void SetFont(const wxFont& font) { m_font = font; }
void SetBorder(wxCalendarDateBorder border) { m_border = border; }
void SetHoliday(bool holiday) { m_holiday = holiday; }
// accessors
bool HasTextColour() const { return m_colText.Ok(); }
bool HasBackgroundColour() const { return m_colBack.Ok(); }
bool HasBorderColour() const { return m_colBorder.Ok(); }
bool HasFont() const { return m_font.Ok(); }
bool HasBorder() const { return m_border != wxCAL_BORDER_NONE; }
bool IsHoliday() const { return m_holiday; }
const wxColour& GetTextColour() const { return m_colText; }
const wxColour& GetBackgroundColour() const { return m_colBack; }
const wxColour& GetBorderColour() const { return m_colBorder; }
const wxFont& GetFont() const { return m_font; }
wxCalendarDateBorder GetBorder() const { return m_border; }
protected:
void Init(wxCalendarDateBorder border = wxCAL_BORDER_NONE)
{
m_border = border;
m_holiday = FALSE;
}
private:
wxColour m_colText,
m_colBack,
m_colBorder;
wxFont m_font;
wxCalendarDateBorder m_border;
bool m_holiday;
};
// ----------------------------------------------------------------------------
// wxCalendarCtrl events
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxCalendarCtrl;
class WXDLLEXPORT wxCalendarEvent : public wxCommandEvent
{
friend class wxCalendarCtrl;
@@ -53,6 +130,17 @@ private:
wxDateTime::WeekDay m_wday;
};
// ----------------------------------------------------------------------------
// wxCalendarCtrl
// ----------------------------------------------------------------------------
// so far we only have a generic version, so keep it simple
#include "wx/generic/calctrl.h"
// ----------------------------------------------------------------------------
// calendar events macros
// ----------------------------------------------------------------------------
#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 },

View File

@@ -38,9 +38,10 @@ class WXDLLEXPORT wxDateSpan;
* TODO Well, everything :-)
*
* + 1. Time zones with minutes (make TimeZone a class)
* 2. getdate() function like under Solaris
* ? 2. getdate() function like under Solaris
* + 3. text conversion for wxDateSpan
* 4. pluggable modules for the workdays calculations
* + 4. pluggable modules for the workdays calculations
* 5. wxDateTimeHolidayAuthority for Easter and other christian feasts
*/
/*
@@ -618,17 +619,21 @@ public:
// default assignment operator is ok
// calendar calculations (functions which set the date only leave the time
// unchanged, e.g. don't explictly zero it)
// unchanged, e.g. don't explictly zero it): SetXXX() functions modify the
// object itself, GetXXX() ones return a new object.
// ------------------------------------------------------------------------
// set to the given week day in the same week as this one
wxDateTime& SetToWeekDayInSameWeek(WeekDay weekday);
inline wxDateTime GetWeekDayInSameWeek(WeekDay weekday) const;
// set to the next week day following this one
wxDateTime& SetToNextWeekDay(WeekDay weekday);
inline wxDateTime GetNextWeekDay(WeekDay weekday) const;
// set to the previous week day before this one
wxDateTime& SetToPrevWeekDay(WeekDay weekday);
inline wxDateTime GetPrevWeekDay(WeekDay weekday) const;
// set to Nth occurence of given weekday in the given month of the
// given year (time is set to 0), return TRUE on success and FALSE on
@@ -638,24 +643,35 @@ public:
int n = 1,
Month month = Inv_Month,
int year = Inv_Year);
inline wxDateTime GetWeekDay(WeekDay weekday,
int n = 1,
Month month = Inv_Month,
int year = Inv_Year) const;
// sets to the last weekday in the given month, year
inline bool SetToLastWeekDay(WeekDay weekday,
Month month = Inv_Month,
int year = Inv_Year);
inline wxDateTime GetLastWeekDay(WeekDay weekday,
Month month = Inv_Month,
int year = Inv_Year);
// sets the date to the given day of the given week in the year,
// returns TRUE on success and FALSE if given date doesn't exist (e.g.
// numWeek is > 53)
bool SetToTheWeek(wxDateTime_t numWeek, WeekDay weekday = Mon);
inline wxDateTime GetWeek(wxDateTime_t numWeek, WeekDay weekday = Mon) const;
// sets the date to the last day of the given (or current) month or the
// given (or current) year
wxDateTime& SetToLastMonthDay(Month month = Inv_Month,
int year = Inv_Year);
inline wxDateTime GetLastMonthDay(Month month = Inv_Month,
int year = Inv_Year) const;
// sets to the given year day (1..365 or 366)
wxDateTime& SetToYearDay(wxDateTime_t yday);
inline wxDateTime GetYearDay(wxDateTime_t yday) const;
// The definitions below were taken verbatim from
//
@@ -784,13 +800,12 @@ public:
// is this date a work day? This depends on a country, of course,
// because the holidays are different in different countries
bool IsWorkDay(Country country = Country_Default,
const TimeZone& tz = Local) const;
bool IsWorkDay(Country country = Country_Default) const;
// is this date later than Gregorian calendar introduction for the
// given country (see enum GregorianAdoption)?
//
// NB: this function shouldn't be considered as absolute authoiruty in
// NB: this function shouldn't be considered as absolute authority in
// the matter. Besides, for some countries the exact date of
// adoption of the Gregorian calendar is simply unknown.
bool IsGregorianDate(GregorianAdoption country = Gr_Standard) const;
@@ -891,9 +906,12 @@ public:
wxString FormatDate() const { return Format(_T("%x")); }
// preferred time representation for the current locale
wxString FormatTime() const { return Format(_T("%X")); }
// return the string representing the date in ISO 8601 format
// returns the string representing the date in ISO 8601 format
// (YYYY-MM-DD)
wxString FormatISODate() const { return Format(_T("%Y-%m-%d")); }
// returns the string representing the time in ISO 8601 format
// (HH:MM:SS)
wxString FormatISOTime() const { return Format(_T("%H:%M:%S")); }
// implementation
// ------------------------------------------------------------------------
@@ -1098,7 +1116,12 @@ private:
// one month to Feb, 15 - we want to get Mar, 15, of course).
//
// When adding a month to the date, all lesser components (days, hours, ...)
// won't be changed.
// won't be changed unless the resulting date would be invalid: for example,
// Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
//
// Because of this feature, adding and substracting back again the same
// wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
// month will be Jan 28, not Jan 31!
//
// wxDateSpan can be either positive or negative. They may be
// multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
@@ -1210,10 +1233,75 @@ private:
m_days;
};
WXDLLEXPORT_DATA(extern wxDateSpan) wxYear;
WXDLLEXPORT_DATA(extern wxDateSpan) wxMonth;
WXDLLEXPORT_DATA(extern wxDateSpan) wxWeek;
WXDLLEXPORT_DATA(extern wxDateSpan) wxDay;
// ----------------------------------------------------------------------------
// wxDateTimeArray: array of dates.
// ----------------------------------------------------------------------------
#include "wx/dynarray.h"
WX_DECLARE_OBJARRAY(wxDateTime, wxDateTimeArray);
// ----------------------------------------------------------------------------
// wxDateTimeHolidayAuthority: an object of this class will decide whether a
// given date is a holiday and is used by all functions working with "work
// days".
//
// NB: the base class is an ABC, derived classes must implement the pure
// virtual methods to work with the holidays they correspond to.
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxDateTimeHolidayAuthority;
WX_DEFINE_ARRAY(wxDateTimeHolidayAuthority *, wxHolidayAuthoritiesArray);
class WXDLLEXPORT wxDateTimeHolidayAuthority
{
public:
// returns TRUE if the given date is a holiday
static bool IsHoliday(const wxDateTime& dt);
// fills the provided array with all holidays in the given range, returns
// the number of them
static size_t GetHolidaysInRange(const wxDateTime& dtStart,
const wxDateTime& dtEnd,
wxDateTimeArray& holidays);
// clear the list of holiday authorities
static void ClearAllAuthorities();
// add a new holiday authority (the pointer will be deleted by
// wxDateTimeHolidayAuthority)
static void AddAuthority(wxDateTimeHolidayAuthority *auth);
protected:
// this function is called to determine whether a given day is a holiday
virtual bool DoIsHoliday(const wxDateTime& dt) const = 0;
// this function should fill the array with all holidays between the two
// given dates - it is implemented in the base class, but in a very
// inefficient way (it just iterates over all days and uses IsHoliday() for
// each of them), so it must be overridden in the derived class where the
// base class version may be explicitly used if needed
//
// returns the number of holidays in the given range and fills holidays
// array
virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
const wxDateTime& dtEnd,
wxDateTimeArray& holidays) const = 0;
private:
// all holiday authorities
static wxHolidayAuthoritiesArray ms_authorities;
};
// the holidays for this class are all Saturdays and Sundays
class WXDLLEXPORT wxDateTimeWorkDays : public wxDateTimeHolidayAuthority
{
protected:
virtual bool DoIsHoliday(const wxDateTime& dt) const;
virtual size_t DoGetHolidaysInRange(const wxDateTime& dtStart,
const wxDateTime& dtEnd,
wxDateTimeArray& holidays) const;
};
// ============================================================================
// inline functions implementation

View File

@@ -16,8 +16,21 @@
#error "This file is only included by wx/datetime.h, don't include it manually!"
#endif
// ----------------------------------------------------------------------------
// private macros
// ----------------------------------------------------------------------------
#define MILLISECONDS_PER_DAY 86400000l
// some broken compilers (HP-UX CC) refuse to compile the "normal" version, but
// using a temp variable always might prevent other compilers from optimising
// it away - hence use of this ugly macro
#ifndef __HPUX__
#define MODIFY_AND_RETURN(op) return wxDateTime(*this).op
#else
#define MODIFY_AND_RETURN(op) wxDateTime dt(*this); dt.op; return dt
#endif
// ----------------------------------------------------------------------------
// wxDateTime construction
// ----------------------------------------------------------------------------
@@ -138,6 +151,57 @@ bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
return SetToWeekDay(weekday, -1, month, year);
}
wxDateTime wxDateTime::GetWeekDayInSameWeek(WeekDay weekday) const
{
MODIFY_AND_RETURN( SetToWeekDayInSameWeek(weekday) );
}
wxDateTime wxDateTime::GetNextWeekDay(WeekDay weekday) const
{
MODIFY_AND_RETURN( SetToNextWeekDay(weekday) );
}
wxDateTime wxDateTime::GetPrevWeekDay(WeekDay weekday) const
{
MODIFY_AND_RETURN( SetToPrevWeekDay(weekday) );
}
wxDateTime wxDateTime::GetWeekDay(WeekDay weekday,
int n,
Month month,
int year) const
{
wxDateTime dt(*this);
return dt.SetToWeekDay(weekday, n, month, year) ? dt : wxInvalidDateTime;
}
wxDateTime wxDateTime::GetLastWeekDay(WeekDay weekday,
Month month,
int year)
{
wxDateTime dt(*this);
return dt.SetToLastWeekDay(weekday, month, year) ? dt : wxInvalidDateTime;
}
wxDateTime wxDateTime::GetWeek(wxDateTime_t numWeek, WeekDay weekday) const
{
wxDateTime dt(*this);
return dt.SetToTheWeek(numWeek, weekday) ? dt : wxInvalidDateTime;
}
wxDateTime wxDateTime::GetLastMonthDay(Month month, int year) const
{
MODIFY_AND_RETURN( SetToLastMonthDay(month, year) );
}
wxDateTime wxDateTime::GetYearDay(wxDateTime_t yday) const
{
MODIFY_AND_RETURN( SetToYearDay(yday) );
}
// ----------------------------------------------------------------------------
// wxDateTime comparison
// ----------------------------------------------------------------------------
@@ -287,8 +351,7 @@ wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz,
bool noDST) const
{
wxDateTime dt(*this);
return dt.MakeTimezone(tz, noDST);
MODIFY_AND_RETURN( MakeTimezone(tz, noDST) );
}
// ----------------------------------------------------------------------------
@@ -437,3 +500,5 @@ wxDateSpan& wxDateSpan::Neg()
}
#undef MILLISECONDS_PER_DAY
#undef MODIFY_AND_RETURN

View File

@@ -448,6 +448,16 @@ WX_DEFINE_EXPORTED_ARRAY(void *, wxArrayPtrVoid);
// convinience macros
// -----------------------------------------------------------------------------
// append all element of one array to another one
#define WX_APPEND_ARRAY(array, other) \
{ \
size_t count = other.Count(); \
for ( size_t n = 0; n < count; n++ ) \
{ \
array.Add(other[n]); \
} \
}
// delete all array elements
//
// NB: the class declaration of the array elements must be visible from the

View File

@@ -18,9 +18,9 @@
#include "wx/control.h" // the base class
#include "wx/datetime.h" // for m_date
#include "wx/combobox.h" // for m_comboMonth
#include "wx/spinctrl.h" // for m_spinYear
#include "wx/spinctrl.h" // for wxSpinEvent
class WXDLLEXPORT wxComboBox;
#define wxCalendarNameStr _T("CalendarCtrl")
@@ -41,7 +41,7 @@ public:
const wxDateTime& date = wxDefaultDateTime,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
long style = wxCAL_SHOW_HOLIDAYS,
const wxString& name = wxCalendarNameStr)
: wxControl(parent, id, pos, size,
style | wxWANTS_CHARS, wxDefaultValidator, name)
@@ -56,15 +56,81 @@ public:
const wxDateTime& date = wxDefaultDateTime,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
long style = wxCAL_SHOW_HOLIDAYS,
const wxString& name = wxCalendarNameStr);
virtual ~wxCalendarCtrl();
// set/get the current date
// ------------------------
void SetDate(const wxDateTime& date);
const wxDateTime& GetDate() const { return m_date; }
// customization
// -------------
// header colours are used for painting the weekdays at the top
void SetHeaderColours(const wxColour& colFg, const wxColour& colBg)
{
m_colHeaderFg = colFg;
m_colHeaderBg = colBg;
}
const wxColour& GetHeaderColourFg() const { return m_colHeaderFg; }
const wxColour& GetHeaderColourBg() const { return m_colHeaderBg; }
// highlight colour is used for the currently selected date
void SetHighlightColours(const wxColour& colFg, const wxColour& colBg)
{
m_colHighlightFg = colFg;
m_colHighlightBg = colBg;
}
const wxColour& GetHighlightColourFg() const { return m_colHighlightFg; }
const wxColour& GetHighlightColourBg() const { return m_colHighlightBg; }
// holiday colour is used for the holidays (if style & wxCAL_SHOW_HOLIDAYS)
void SetHolidayColours(const wxColour& colFg, const wxColour& colBg)
{
m_colHolidayFg = colFg;
m_colHolidayBg = colBg;
}
const wxColour& GetHolidayColourFg() const { return m_colHolidayFg; }
const wxColour& GetHolidayColourBg() const { return m_colHolidayBg; }
// this function should be called instead of directly changing the
// wxCAL_SHOW_HOLIDAYS bit in the control style after the control creation
// (this won't work)
void EnableHolidayDisplay(bool display = TRUE);
// an item without custom attributes is drawn with the default colours and
// font and without border, setting custom attributes allows to modify this
//
// the day parameter should be in 1..31 range, for days 29, 30, 31 the
// corresponding attribute is just unused if there is no such day in the
// current month
wxCalendarDateAttr *GetAttr(size_t day) const
{
wxCHECK_MSG( day > 0 && day < 32, NULL, _T("invalid day") );
return m_attrs[day - 1];
}
void SetAttr(size_t day, wxCalendarDateAttr *attr)
{
wxCHECK_RET( day > 0 && day < 32, _T("invalid day") );
delete m_attrs[day - 1];
m_attrs[day - 1] = attr;
}
void SetHoliday(size_t day);
void ResetAttr(size_t day) { SetAttr(day, (wxCalendarDateAttr *)NULL); }
// returns one of wxCAL_HITTEST_XXX constants and fills either date or wd
// with the corresponding value (none for NOWHERE, the date for DAY and wd
// for HEADER)
@@ -90,6 +156,7 @@ private:
void OnChar(wxKeyEvent& event);
void OnMonthChange(wxCommandEvent& event);
void OnYearChange(wxSpinEvent& event);
void OnCalMonthChange(wxCalendarEvent& event);
// override some base class virtuals
virtual wxSize DoGetBestSize() const;
@@ -119,16 +186,43 @@ private:
// change the date inside the same month/year
void ChangeDay(const wxDateTime& date);
// generate the given calendar event and a "selection changed" one if
// selChanged is TRUE
void GenerateEvent(wxEventType type, bool selChanged = TRUE);
// set the attributes for the holidays if needed
void SetHolidayAttrs();
// reset all holidays
void ResetHolidayAttrs();
// generate the given calendar event(s)
void GenerateEvent(wxEventType type)
{
wxCalendarEvent event(this, type);
(void)GetEventHandler()->ProcessEvent(event);
}
void GenerateEvents(wxEventType type1, wxEventType type2)
{
GenerateEvent(type1);
GenerateEvent(type2);
}
// the subcontrols
wxComboBox *m_comboMonth;
wxSpinCtrl *m_spinYear;
// the current selection
wxDateTime m_date;
// default attributes
wxColour m_colHighlightFg,
m_colHighlightBg,
m_colHolidayFg,
m_colHolidayBg,
m_colHeaderFg,
m_colHeaderBg;
// the attributes for each of the month days
wxCalendarDateAttr *m_attrs[31];
// the width and height of one column/row in the calendar
wxCoord m_widthCol,
m_heightRow;