//
// System.Web.UI.WebControls.Calendar.cs
//
// Authors:
// Gaurav Vaish (gvaish@iitk.ac.in)
// Gonzalo Paniagua Javier (gonzalo@ximian.com)
// Andreas Nahr (ClassDevelopment@A-SoftTech.com)
//
// (c) 2002 Ximian, Inc. (http://www.ximian.com)
// (C) Gaurav Vaish (2002)
// (C) 2003 Andreas Nahr
//
using System;
using System.IO;
using System.Collections;
using System.Globalization;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Drawing;
using System.ComponentModel;
using System.ComponentModel.Design;
namespace System.Web.UI.WebControls
{
[DefaultEvent("SelectionChanged")]
[DefaultProperty("SelectedDate")]
[Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
[DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
public class Calendar : WebControl, IPostBackEventHandler
{
//
private TableItemStyle dayHeaderStyle;
private TableItemStyle dayStyle;
private TableItemStyle nextPrevStyle;
private TableItemStyle otherMonthDayStyle;
private SelectedDatesCollection selectedDates;
private ArrayList selectedDatesList;
private TableItemStyle selectedDayStyle;
private TableItemStyle selectorStyle;
private TableItemStyle titleStyle;
private TableItemStyle todayDayStyle;
private TableItemStyle weekendDayStyle;
private static readonly object DayRenderEvent = new object();
private static readonly object SelectionChangedEvent = new object();
private static readonly object VisibleMonthChangedEvent = new object();
private Color defaultTextColor;
private System.Globalization.Calendar globCal;
private DateTimeFormatInfo infoCal = DateTimeFormatInfo.CurrentInfo;
private static int MASK_WEEKEND = (0x01 << 0);
private static int MASK_OMONTH = (0x01 << 1);
private static int MASK_TODAY = (0x01 << 2);
private static int MASK_SELECTED = (0x01 << 3);
private static int MASK_DAY = (0x01 << 4);
private static int MASK_UNIQUE = MASK_WEEKEND | MASK_OMONTH | MASK_TODAY | MASK_SELECTED;
public Calendar(): base()
{
//TODO: Initialization
}
[DefaultValue (2), Bindable (true), WebCategory ("Layout")]
[WebSysDescription ("The border left within the calendar days.")]
public int CellPadding
{
get
{
object o = ViewState["CellPadding"];
if(o!=null)
return (int)o;
return 2;
}
set
{
if (value < -1)
throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
ViewState["CellPadding"] = value;
}
}
[DefaultValue (0), Bindable (true), WebCategory ("Layout")]
[WebSysDescription ("The border left between calendar days.")]
public int CellSpacing
{
get
{
object o = ViewState["CellSpacing"];
if(o!=null)
return (int)o;
return 0;
}
set
{
if (value < -1)
throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
ViewState["CellSpacing"] = value;
}
}
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the day header.")]
public TableItemStyle DayHeaderStyle
{
get
{
if(dayHeaderStyle==null)
dayHeaderStyle = new TableItemStyle();
if(IsTrackingViewState)
dayHeaderStyle.TrackViewState();
return dayHeaderStyle;
}
}
[DefaultValue (typeof (DayNameFormat), "Short"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The format for the day name display.")]
public DayNameFormat DayNameFormat
{
get
{
object o = ViewState["DayNameFormat"];
if(o!=null)
return (DayNameFormat)o;
return DayNameFormat.Short;
}
set
{
if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
ViewState["DayNameFormat"] = value;
}
}
[DefaultValue (null)]
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the day entry.")]
public TableItemStyle DayStyle
{
get
{
if(dayStyle==null)
dayStyle = new TableItemStyle();
if(IsTrackingViewState)
dayStyle.TrackViewState();
return dayStyle;
}
}
[DefaultValue (typeof (FirstDayOfWeek), "Default"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The day that a week begins with.")]
public FirstDayOfWeek FirstDayOfWeek
{
get
{
object o = ViewState["FirstDayOfWeek"];
if(o!=null)
return (FirstDayOfWeek)o;
return FirstDayOfWeek.Default;
}
set
{
if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
ViewState["FirstDayOfWeek"] = value;
}
}
[DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The text for selecting the next month.")]
public string NextMonthText
{
get
{
object o = ViewState["NextMonthText"];
if(o!=null)
return (string)o;
return ">";
}
set
{
ViewState["NextMonthText"] = value;
}
}
[DefaultValue (typeof (NextPrevFormat), "CustomText"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The format for the month navigation.")]
public NextPrevFormat NextPrevFormat
{
get
{
object o = ViewState["NextPrevFormat"];
if(o!=null)
return (NextPrevFormat)o;
return NextPrevFormat.CustomText;
}
set
{
if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
ViewState["NextPrevFormat"] = value;
}
}
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the month navigation.")]
public TableItemStyle NextPrevStyle
{
get
{
if(nextPrevStyle == null)
nextPrevStyle = new TableItemStyle();
if(IsTrackingViewState)
nextPrevStyle.TrackViewState();
return nextPrevStyle;
}
}
[DefaultValue (null)]
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to day entries belonging to another month.")]
public TableItemStyle OtherMonthDayStyle
{
get
{
if(otherMonthDayStyle == null)
otherMonthDayStyle = new TableItemStyle();
if(IsTrackingViewState)
otherMonthDayStyle.TrackViewState();
return otherMonthDayStyle;
}
}
[DefaultValue ("<"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The text for selecting the previous month.")]
public string PrevMonthText
{
get
{
object o = ViewState["PrevMonthText"];
if(o!=null)
return (string)o;
return "<";
}
set
{
ViewState["PrevMonthText"] = value;
}
}
[DefaultValue (null), Bindable (true)]
[WebSysDescription ("The currently selected date.")]
public DateTime SelectedDate
{
get
{
if(SelectedDates.Count > 0)
{
return SelectedDates[0];
}
return DateTime.MinValue;
}
set
{
if(value == DateTime.MinValue)
{
SelectedDates.Clear();
} else
{
SelectedDates.SelectRange(value, value);
}
}
}
[Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("All currently selected dates.")]
public SelectedDatesCollection SelectedDates
{
get
{
if(selectedDates==null)
{
if(selectedDatesList == null)
selectedDatesList = new ArrayList();
selectedDates = new SelectedDatesCollection(selectedDatesList);
}
return selectedDates;
}
}
[DefaultValue (null)]
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the selected day.")]
public TableItemStyle SelectedDayStyle
{
get
{
if(selectedDayStyle==null)
selectedDayStyle = new TableItemStyle();
if(IsTrackingViewState)
selectedDayStyle.TrackViewState();
return selectedDayStyle;
}
}
[DefaultValue (typeof (CalendarSelectionMode), "Day"), Bindable (true), WebCategory ("Behavior")]
[WebSysDescription ("The mode in which days or other entries are selected.")]
public CalendarSelectionMode SelectionMode
{
get
{
object o = ViewState["SelectionMode"];
if(o!=null)
return (CalendarSelectionMode)o;
return CalendarSelectionMode.Day;
}
set
{
if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
ViewState["SelectionMode"] = value;
}
}
[DefaultValue (">>"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The text that is used for selection of months.")]
public string SelectMonthText
{
get
{
object o = ViewState["SelectMonthText"];
if(o!=null)
return (string)o;
return ">>";
}
set
{
ViewState["SelectMonthText"] = value;
}
}
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the selector.")]
public TableItemStyle SelectorStyle
{
get
{
if(selectorStyle==null)
selectorStyle = new TableItemStyle();
return selectorStyle;
}
}
[DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The text that is used for selection of weeks.")]
public string SelectWeekText
{
get
{
object o = ViewState["SelectWeekText"];
if(o!=null)
return (string)o;
return ">";
}
set
{
ViewState["SelectWeekText"] = value;
}
}
[DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("Determines if the header for days is shown.")]
public bool ShowDayHeader
{
get
{
object o = ViewState["ShowDayHeader"];
if(o!=null)
return (bool)o;
return true;
}
set
{
ViewState["ShowDayHeader"] = value;
}
}
[DefaultValue (false), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("Determines if gridlines are shown.")]
public bool ShowGridLines
{
get
{
object o = ViewState["ShowGridLines"];
if(o!=null)
return (bool)o;
return false;
}
set
{
ViewState["ShowGridLines"] = value;
}
}
[DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("Determines if month navigation is shown.")]
public bool ShowNextPrevMonth
{
get
{
object o = ViewState["ShowNextPrevMonth"];
if(o!=null)
return (bool)o;
return true;
}
set
{
ViewState["ShowNextPrevMonth"] = value;
}
}
[DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("Determines if the title is shown.")]
public bool ShowTitle
{
get
{
object o = ViewState["ShowTitle"];
if(o!=null)
return (bool)o;
return true;
}
set
{
ViewState["ShowTitle"] = value;
}
}
[DefaultValue (typeof (TitleFormat), "MonthYear"), Bindable (true), WebCategory ("Appearance")]
[WebSysDescription ("The format in which the title is rendered.")]
public TitleFormat TitleFormat
{
get
{
object o = ViewState["TitleFormat"];
if(o!=null)
return (TitleFormat)o;
return TitleFormat.MonthYear;
}
set
{
if(!System.Enum.IsDefined(typeof(TitleFormat), value))
throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
ViewState["TitleFormat"] = value;
}
}
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the title.")]
public TableItemStyle TitleStyle
{
get
{
if(titleStyle==null)
titleStyle = new TableItemStyle();
if(IsTrackingViewState)
titleStyle.TrackViewState();
return titleStyle;
}
}
[DefaultValue (null)]
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to the today's date display.")]
public TableItemStyle TodayDayStyle
{
get
{
if(todayDayStyle==null)
todayDayStyle = new TableItemStyle();
if(IsTrackingViewState)
todayDayStyle.TrackViewState();
return todayDayStyle;
}
}
[Bindable (true), Browsable (false)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
[WebSysDescription ("The current date.")]
public DateTime TodaysDate
{
get
{
object o = ViewState["TodaysDate"];
if(o!=null)
return (DateTime)o;
return DateTime.Today;
}
set
{
ViewState["TodaysDate"] = value.Date;
}
}
[DefaultValue (null), Bindable (true)]
[WebSysDescription ("The month that is displayed.")]
public DateTime VisibleDate
{
get
{
object o = ViewState["VisibleDate"];
if(o!=null)
return (DateTime)o;
return DateTime.MinValue;
}
set
{
ViewState["VisibleDate"] = value.Date;
}
}
[NotifyParentProperty (true), WebCategory ("Style")]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
[PersistenceMode (PersistenceMode.InnerProperty)]
[WebSysDescription ("The style applied to weekend days.")]
public TableItemStyle WeekendDayStyle
{
get
{
if(weekendDayStyle == null)
weekendDayStyle = new TableItemStyle();
if(IsTrackingViewState)
{
weekendDayStyle.TrackViewState();
}
return weekendDayStyle;
}
}
[WebCategory ("Action")]
[WebSysDescription ("Raised when a day entry is rendered.")]
public event DayRenderEventHandler DayRender
{
add
{
Events.AddHandler(DayRenderEvent, value);
}
remove
{
Events.RemoveHandler(DayRenderEvent, value);
}
}
[WebCategory ("Action")]
[WebSysDescription ("Raised when another entry is selected.")]
public event EventHandler SelectionChanged
{
add
{
Events.AddHandler(SelectionChangedEvent, value);
}
remove
{
Events.RemoveHandler(SelectionChangedEvent, value);
}
}
[WebCategory ("Action")]
[WebSysDescription ("Raised when a the currently visible month has changed.")]
public event MonthChangedEventHandler VisibleMonthChanged
{
add
{
Events.AddHandler(VisibleMonthChangedEvent, value);
}
remove
{
Events.RemoveHandler(VisibleMonthChangedEvent, value);
}
}
protected virtual void OnDayRender(TableCell cell, CalendarDay day)
{
if(Events!=null)
{
DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
if(dreh!=null)
dreh(this, new DayRenderEventArgs(cell, day));
}
}
protected virtual void OnSelectionChanged()
{
if(Events!=null)
{
EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
if(eh!=null)
eh(this, new EventArgs());
}
}
protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
{
if(Events!=null)
{
MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
if(mceh!=null)
mceh(this, new MonthChangedEventArgs(newDate, prevDate));
}
}
///