2 // System.Web.UI.WebControls.Calendar.cs
5 // Jordi Mas i Hernandez (jordi@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Globalization;
33 using System.Collections;
34 using System.ComponentModel;
37 namespace System.Web.UI.WebControls {
38 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
39 [DefaultEvent("SelectionChanged")]
40 [DefaultProperty("SelectedDate")]
41 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
43 [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
45 public class Calendar : WebControl, IPostBackEventHandler {
47 private TableItemStyle dayHeaderStyle;
48 private TableItemStyle dayStyle;
49 private TableItemStyle nextPrevStyle;
50 private TableItemStyle otherMonthDayStyle;
51 private TableItemStyle selectedDayStyle;
52 private TableItemStyle titleStyle;
53 private TableItemStyle todayDayStyle;
54 private TableItemStyle selectorStyle;
55 private TableItemStyle weekendDayStyle;
56 private DateTimeFormatInfo dateInfo;
57 private SelectedDatesCollection selectedDatesCollection;
58 private ArrayList dateList;
59 private static DateTime dateZenith = new DateTime (2000, 1,1);
60 private const int daysInAWeek = 7;
61 private static readonly object DayRenderEvent = new object ();
62 private static readonly object SelectionChangedEvent = new object ();
63 private static readonly object VisibleMonthChangedEvent = new object ();
67 dayHeaderStyle = null;
70 selectedDayStyle = null;
73 dateInfo = new DateTimeFormatInfo ();
74 selectedDatesCollection = null;
81 [WebSysDescription ("")]
82 [WebCategoryAttribute ("Appearance")]
83 public virtual string Caption
86 return ViewState.GetString ("Caption", "");
89 ViewState["Caption"] = value;
93 [DefaultValue (TableCaptionAlign.NotSet)]
94 [WebSysDescription ("")]
95 [WebCategoryAttribute ("Accessibility")]
96 public virtual TableCaptionAlign CaptionAlign
99 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
102 ViewState ["CaptionAlign"] = value;
111 [WebSysDescription ("")]
112 [WebCategory ("Layout")]
113 public int CellPadding {
115 return ViewState.GetInt ("CellPadding", 2);
120 throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
122 ViewState ["CellPadding"] = value;
130 [WebSysDescription ("")]
131 [WebCategory ("Layout")]
132 public int CellSpacing {
134 return ViewState.GetInt ("CellSpacing", 0);
139 throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
141 ViewState ["CellSpacing"] = value;
145 [PersistenceMode(PersistenceMode.InnerProperty)]
146 [NotifyParentProperty(true)]
147 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
148 [WebSysDescription ("")]
149 [WebCategory ("Style")]
150 public TableItemStyle DayHeaderStyle {
152 if (dayHeaderStyle == null)
153 dayHeaderStyle = new TableItemStyle ();
155 if (IsTrackingViewState)
156 dayHeaderStyle.TrackViewState ();
158 return dayHeaderStyle;
165 [DefaultValue(DayNameFormat.Short)]
166 [WebSysDescription ("")]
167 [WebCategory ("Appearance")]
168 public DayNameFormat DayNameFormat {
170 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
174 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
175 value != DayNameFormat.Full && value != DayNameFormat.Short) {
176 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values."); }
178 ViewState ["DayNameFormat"] = value;
183 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
184 [NotifyParentProperty(true)]
185 [PersistenceMode(PersistenceMode.InnerProperty)]
186 [WebSysDescription ("")]
187 [WebCategory ("Style")]
188 public TableItemStyle DayStyle {
190 if (dayStyle == null)
191 dayStyle = new TableItemStyle ();
193 if (IsTrackingViewState)
194 dayStyle.TrackViewState ();
203 [DefaultValue(FirstDayOfWeek.Default)]
204 [WebSysDescription ("")]
205 [WebCategory ("Appearance")]
206 public FirstDayOfWeek FirstDayOfWeek {
208 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
212 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
213 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
216 ViewState ["FirstDayOfWeek"] = value;
223 [DefaultValue(">")]
227 [WebSysDescription ("")]
228 [WebCategory ("Appearance")]
229 public string NextMonthText {
231 return ViewState.GetString ("NextMonthText", ">");
235 ViewState ["NextMonthText"] = value;
242 [DefaultValue(NextPrevFormat.CustomText)]
243 [WebSysDescription ("")]
244 [WebCategory ("Appearance")]
245 public NextPrevFormat NextPrevFormat {
247 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
251 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
252 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
255 ViewState ["NextPrevFormat"] = value;
259 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
260 [NotifyParentProperty(true)]
261 [PersistenceMode(PersistenceMode.InnerProperty)]
262 [WebSysDescription ("")]
263 [WebCategory ("Style")]
264 public TableItemStyle NextPrevStyle {
266 if (nextPrevStyle == null)
267 nextPrevStyle = new TableItemStyle ();
269 if (IsTrackingViewState)
270 nextPrevStyle.TrackViewState ();
272 return nextPrevStyle;
277 [NotifyParentProperty(true)]
278 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
279 [PersistenceMode(PersistenceMode.InnerProperty)]
280 [WebSysDescription ("")]
281 [WebCategory ("Style")]
282 public TableItemStyle OtherMonthDayStyle {
284 if (otherMonthDayStyle == null)
285 otherMonthDayStyle = new TableItemStyle ();
287 if (IsTrackingViewState)
288 otherMonthDayStyle.TrackViewState ();
290 return otherMonthDayStyle;
297 [DefaultValue("<")]
301 [WebSysDescription ("")]
302 [WebCategory ("Appearance")]
303 public string PrevMonthText {
305 return ViewState.GetString ("PrevMonthText", "<");
309 ViewState ["PrevMonthText"] = value;
314 [Bindable(true, BindingDirection.TwoWay)]
318 [DefaultValue("1/1/0001 12:00:00 AM")]
319 [WebSysDescription ("")]
320 [WebCategory ("Appearance")]
321 public DateTime SelectedDate {
323 if (SelectedDates.Count > 0)
324 return SelectedDates [0];
326 return DateTime.MinValue;
330 SelectedDates.SelectRange (value, value);
335 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
336 [WebSysDescription ("")]
337 [WebCategory ("Appearance")]
338 public SelectedDatesCollection SelectedDates {
340 if (dateList == null)
341 dateList = new ArrayList ();
343 if (selectedDatesCollection == null)
344 selectedDatesCollection = new SelectedDatesCollection (dateList);
346 return selectedDatesCollection;
351 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
352 [NotifyParentProperty(true)]
353 [PersistenceMode(PersistenceMode.InnerProperty)]
354 [WebSysDescription ("")]
355 [WebCategory ("Style")]
356 public TableItemStyle SelectedDayStyle {
358 if (selectedDayStyle == null)
359 selectedDayStyle = new TableItemStyle ();
361 if (IsTrackingViewState)
362 selectedDayStyle.TrackViewState ();
364 return selectedDayStyle;
371 [DefaultValue(CalendarSelectionMode.Day)]
372 [WebSysDescription ("")]
373 [WebCategory ("Behavior")]
374 public CalendarSelectionMode SelectionMode {
376 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
380 if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
381 value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
382 throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
384 ViewState ["SelectionMode"] = value;
391 [DefaultValue(">>")]
395 [WebSysDescription ("")]
396 [WebCategory ("Appearance")]
397 public string SelectMonthText {
399 return ViewState.GetString ("SelectMonthText", ">>");
403 ViewState ["SelectMonthText"] = value;
407 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
408 [NotifyParentProperty(true)]
409 [PersistenceMode(PersistenceMode.InnerProperty)]
410 [WebSysDescription ("")]
411 [WebCategory ("Style")]
412 public TableItemStyle SelectorStyle {
414 if (selectorStyle == null)
415 selectorStyle = new TableItemStyle ();
417 if (IsTrackingViewState)
418 selectorStyle.TrackViewState ();
420 return selectorStyle;
427 [DefaultValue(">")]
431 [WebSysDescription ("")]
432 [WebCategory ("Appearance")]
433 public string SelectWeekText {
435 return ViewState.GetString ("SelectWeekText", ">");
439 ViewState ["SelectWeekText"] = value;
447 [WebSysDescription ("")]
448 [WebCategory ("Appearance")]
449 public bool ShowDayHeader {
451 return ViewState.GetBool ("ShowDayHeader", true);
455 ViewState ["ShowDayHeader"] = value;
462 [DefaultValue(false)]
463 [WebSysDescription ("")]
464 [WebCategory ("Appearance")]
465 public bool ShowGridLines {
467 return ViewState.GetBool ("ShowGridLines", false);
471 ViewState ["ShowGridLines"] = value;
479 [WebSysDescription ("")]
480 [WebCategory ("Appearance")]
481 public bool ShowNextPrevMonth {
483 return ViewState.GetBool ("ShowNextPrevMonth", true);
487 ViewState ["ShowNextPrevMonth"] = value;
495 [WebSysDescription ("")]
496 [WebCategory ("Appearance")]
497 public bool ShowTitle {
499 return ViewState.GetBool ("ShowTitle", true);
503 ViewState ["ShowTitle"] = value;
510 [DefaultValue(TitleFormat.MonthYear)]
511 [WebSysDescription ("")]
512 [WebCategory ("Appearance")]
513 public TitleFormat TitleFormat {
515 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
519 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
520 throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
523 ViewState ["TitleFormat"] = value;
527 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
528 [NotifyParentProperty(true)]
529 [PersistenceMode(PersistenceMode.InnerProperty)]
530 [WebSysDescription ("")]
531 [WebCategory ("Style")]
532 public TableItemStyle TitleStyle {
534 if (titleStyle == null)
535 titleStyle = new TableItemStyle ();
537 if (IsTrackingViewState)
538 titleStyle.TrackViewState ();
545 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
546 [NotifyParentProperty(true)]
547 [PersistenceMode(PersistenceMode.InnerProperty)]
548 [WebSysDescription ("")]
549 [WebCategory ("Style")]
550 public TableItemStyle TodayDayStyle {
552 if (todayDayStyle == null)
553 todayDayStyle = new TableItemStyle ();
555 if (IsTrackingViewState)
556 todayDayStyle.TrackViewState ();
558 return todayDayStyle;
566 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
567 [WebSysDescription ("")]
568 [WebCategory ("Style")]
569 public DateTime TodaysDate {
571 object obj = ViewState ["TodaysDate"];
574 return (DateTime) obj;
576 return DateTime.Today;
580 ViewState ["TodaysDate"] = value.Date;
585 [DefaultValue (true)]
586 [WebSysDescription ("")]
587 [WebCategoryAttribute ("Accessibility")]
588 public virtual bool UseAccessibleHeader
591 return ViewState.GetBool ("UseAccessibleHeader", true);
594 ViewState ["UseAccessibleHeader"] = value;
600 [DefaultValue("1/1/0001 12:00:00 AM")]
601 [WebSysDescription ("")]
602 [WebCategory ("Style")]
603 public DateTime VisibleDate {
605 object obj = ViewState ["VisibleDate"];
608 return (DateTime) obj;
610 return DateTime.MinValue;
614 ViewState ["VisibleDate"] = value.Date;
618 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
619 [NotifyParentProperty(true)]
620 [PersistenceMode(PersistenceMode.InnerProperty)]
621 [WebSysDescription ("")]
622 [WebCategory ("Style")]
623 public TableItemStyle WeekendDayStyle {
625 if (weekendDayStyle == null)
626 weekendDayStyle = new TableItemStyle ();
628 if (IsTrackingViewState)
629 weekendDayStyle.TrackViewState ();
631 return weekendDayStyle;
636 // Private properties
637 private DateTime DisplayDate {
640 if (VisibleDate == DateTime.MinValue) // If visibledate is still the default value
641 dateTime = TodaysDate;
643 dateTime = VisibleDate;
649 private DayOfWeek DisplayFirstDayOfWeek {
651 if (FirstDayOfWeek != FirstDayOfWeek.Default)
652 return (DayOfWeek) FirstDayOfWeek;
654 return (DayOfWeek) dateInfo.FirstDayOfWeek;
658 protected override ControlCollection CreateControlCollection ()
660 return base.CreateControlCollection ();
663 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
665 if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
672 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
674 RaisePostBackEvent (eventArgument);
677 protected virtual void RaisePostBackEvent (string arg)
679 void IPostBackEventHandler.RaisePostBackEvent (string arg)
685 DateTime dt = dateZenith;
687 if (arg[0] == 'V') { // Goes to Next or Previous month
688 DateTime prev = VisibleDate;
689 int days = Int32.Parse (arg.Substring (1));
690 dt = GetGlobalCalendar().AddDays (dt, days);
692 OnVisibleMonthChanged (VisibleDate, prev);
696 if (arg[0] == 'R') { // Selects a range of dates
697 string num, date, days;
698 num = arg.Substring (1);
699 days = num.Substring (num.Length - 2, 2);
700 date = num.Substring (0, num.Length - 2);
701 dt = GetGlobalCalendar().AddDays (dt, Int32.Parse (date));
702 SelectDates (dt, Int32.Parse (days));
706 // Selects a single day
707 int daysFromZenith = Int32.Parse (arg);
708 dt = GetGlobalCalendar().AddDays (dt, daysFromZenith);
712 protected override void LoadViewState (object savedState)
714 object [] states = (object []) savedState;
716 if (states [0] != null)
717 base.LoadViewState (states [0]);
719 if (states [1] != null)
720 DayHeaderStyle.LoadViewState (states [1]);
722 if (states [2] != null)
723 DayStyle.LoadViewState (states [2]);
725 if (states [3] != null)
726 NextPrevStyle.LoadViewState (states [3]);
728 if (states [4] != null)
729 OtherMonthDayStyle.LoadViewState (states [4]);
731 if (states [5] != null)
732 SelectedDayStyle.LoadViewState (states [5]);
734 if (states [6] != null)
735 TitleStyle.LoadViewState (states [6]);
737 if (states [7] != null)
738 TodayDayStyle.LoadViewState (states [7]);
740 if (states [8] != null)
741 SelectorStyle.LoadViewState (states [8]);
743 if (states [9] != null)
744 WeekendDayStyle.LoadViewState (states [9]);
746 ArrayList array = (ArrayList) ViewState ["SelectedDates"];
749 selectedDatesCollection = new SelectedDatesCollection (dateList);
753 protected virtual void OnDayRender (TableCell cell, CalendarDay day)
755 if (Events != null) {
756 DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
758 eh (this, new DayRenderEventArgs (cell, day));
767 override void OnPreRender (EventArgs e)
769 base.OnPreRender (e);
772 protected virtual void OnSelectionChanged ()
774 if (Events != null) {
775 EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
777 eh (this, EventArgs.Empty);
781 protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
783 if (Events != null) {
784 MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
786 eh (this, new MonthChangedEventArgs (newDate, previousDate));
795 override void Render (HtmlTextWriter writer)
797 Table table = new Table ();
798 table.CellSpacing = CellSpacing;
799 table.CellPadding = CellPadding;
800 table.ControlStyle.CopyFrom (ControlStyle);
803 table.GridLines = GridLines.Both;
805 table.RenderBeginTag (writer);
809 WriteCaption (writer);
816 WriteDayHeader (writer);
820 table.RenderEndTag (writer);
823 protected override object SaveViewState ()
825 object [] states = new object [10];
827 if (dayHeaderStyle != null)
828 states [1] = dayHeaderStyle.SaveViewState ();
830 if (dayStyle != null)
831 states [2] = dayStyle.SaveViewState ();
833 if (nextPrevStyle != null)
834 states [3] = nextPrevStyle.SaveViewState ();
836 if (otherMonthDayStyle != null)
837 states [4] = otherMonthDayStyle.SaveViewState ();
839 if (selectedDayStyle != null)
840 states [5] = selectedDayStyle.SaveViewState ();
842 if (titleStyle != null)
843 states [6] = titleStyle.SaveViewState ();
845 if (todayDayStyle != null)
846 states [7] =todayDayStyle.SaveViewState ();
848 if (selectorStyle != null)
849 states [8] = selectorStyle.SaveViewState ();
851 if (weekendDayStyle != null)
852 states [9] = weekendDayStyle.SaveViewState ();
854 if (SelectedDates.Count > 0) {
855 ViewState ["SelectedDates"] = dateList;
858 states [0] = base.SaveViewState ();
860 for (int i = 0; i < states.Length; i++) {
861 if (states [i] != null) {
869 protected override void TrackViewState ()
871 base.TrackViewState ();
873 if (dayHeaderStyle != null)
874 dayHeaderStyle.TrackViewState ();
876 if (dayStyle != null)
877 dayStyle.TrackViewState ();
879 if (nextPrevStyle != null)
880 nextPrevStyle.TrackViewState ();
882 if (otherMonthDayStyle != null)
883 otherMonthDayStyle.TrackViewState ();
885 if (selectedDayStyle != null)
886 selectedDayStyle.TrackViewState ();
888 if (titleStyle != null)
889 titleStyle.TrackViewState ();
891 if (todayDayStyle != null)
892 todayDayStyle.TrackViewState ();
894 if (selectorStyle != null)
895 selectorStyle.TrackViewState ();
897 if (weekendDayStyle != null)
898 weekendDayStyle.TrackViewState ();
904 private void WriteDayHeader (HtmlTextWriter writer)
908 i = first = (int) (DisplayFirstDayOfWeek);
912 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
914 if (SelectionMode == CalendarSelectionMode.DayWeek) {
915 cell = new TableCell();
916 cell.HorizontalAlign = HorizontalAlign.Center;
917 cell.ApplyStyle (DayHeaderStyle);
920 cell.RenderBeginTag (writer);
921 cell.RenderEndTag (writer);
923 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
924 TableCell selector = new TableCell ();
925 selector.ApplyStyle (SelectorStyle);
926 selector.HorizontalAlign = HorizontalAlign.Center;
928 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
929 int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
931 selector.RenderBeginTag (writer);
932 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, true));
933 selector.RenderEndTag (writer);
938 DayOfWeek dayOfWeek = (DayOfWeek) i;
939 dayName = dateInfo.GetDayName (dayOfWeek);
942 if (UseAccessibleHeader) {
943 writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
944 writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col");
945 cell = new TableHeaderCell();
949 cell = new TableCell();
951 cell.HorizontalAlign = HorizontalAlign.Center;
952 cell.ApplyStyle (DayHeaderStyle);
954 cell.RenderBeginTag (writer);
956 switch (DayNameFormat) {
957 case DayNameFormat.FirstLetter:
958 dayName = dayName.Substring (0, 1);
960 case DayNameFormat.FirstTwoLetters:
961 dayName = dayName.Substring (0, 2);
963 case DayNameFormat.Full:
965 case DayNameFormat.Short:
967 dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
971 writer.Write (dayName);
972 cell.RenderEndTag (writer);
974 if (i >= daysInAWeek - 1) {
984 writer.RenderEndTag ();
987 private void WriteDay (DateTime date, HtmlTextWriter writer)
989 Style style = new Style ();
990 TableCell cell = new TableCell ();
992 CalendarDay day = new CalendarDay (date,
993 IsWeekEnd (date.DayOfWeek),
994 date == TodaysDate, SelectedDates.Contains (date),
995 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
996 date.Day.ToString ());
998 day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
999 cell.HorizontalAlign = HorizontalAlign.Center;
1000 cell.Width = Unit.Percentage (GetCellWidth ());
1002 LiteralControl lit = new LiteralControl (day.DayNumberText);
1003 cell.Controls.Add (lit);
1005 OnDayRender (cell, day);
1007 if (dayStyle != null && !dayStyle.IsEmpty) {
1008 style.CopyFrom (dayStyle);
1011 if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1012 style.CopyFrom (weekendDayStyle);
1015 if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1016 style.CopyFrom (todayDayStyle);
1019 if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1020 style.CopyFrom (otherMonthDayStyle);
1023 if (day.IsSelected) {
1024 style.BackColor = Color.Silver;
1025 style.ForeColor = Color.White;
1026 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1027 style.CopyFrom (selectedDayStyle);
1031 cell.ApplyStyle (style);
1033 lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1034 cell.ForeColor, day.IsSelectable);
1036 cell.RenderControl (writer);
1039 private void WriteDays (HtmlTextWriter writer)
1041 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1043 TableCell selectorCell = null;
1046 // Goes backwards until we find the date of that is begining of the week
1047 for (n = 0; n < daysInAWeek; n++) {
1048 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1051 date = GetGlobalCalendar().AddDays (date, -1);
1053 /* if the start date is the first day of the week, we need to shift backward one more week */
1055 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1057 lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1060 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1062 if (HasWeekSelectors (SelectionMode)) { // Week selector
1063 if (selectorCell == null) {
1064 selectorCell = new TableCell ();
1065 selectorCell.ApplyStyle (SelectorStyle);
1066 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1067 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1070 selectorCell.RenderBeginTag (writer);
1071 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, true));
1072 selectorCell.RenderEndTag (writer);
1075 for (int i = 0; i < daysInAWeek; i++) {
1076 WriteDay (date, writer);
1077 date = GetGlobalCalendar().AddDays (date, 1);
1080 writer.RenderEndTag ();
1081 if (date >= lastDate)
1086 private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1088 string str = string.Empty;
1090 hasLink = (Page != null && hasLink == true) ? true : false;
1094 str += Page.ClientScript.GetPostBackClientHyperlink (this, arg);
1098 if (!foreColor.IsEmpty) {
1101 if (ForeColor.IsEmpty)
1107 str += " style=color:" + ColorTranslator.ToHtml (clr);
1118 private int GetDaysFromZenith (DateTime date)
1120 TimeSpan span = date.Subtract (dateZenith);
1125 void WriteCaption (HtmlTextWriter writer)
1127 if (CaptionAlign != TableCaptionAlign.NotSet)
1128 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
1130 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1131 writer.Write (Caption);
1132 writer.RenderEndTag ();
1136 private void WriteTitle (HtmlTextWriter writer)
1138 TableCell cellNextPrev = null;
1139 TableCell titleCell = new TableCell ();
1140 Table tableTitle = new Table ();
1142 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1144 titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1146 if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1147 titleCell.BackColor = titleStyle.BackColor;
1149 titleCell.BackColor = Color.Silver;
1152 titleCell.RenderBeginTag (writer);
1155 tableTitle.Width = Unit.Percentage (100);
1156 if (titleStyle != null && !titleStyle.IsEmpty) {
1157 tableTitle.ApplyStyle (titleStyle);
1160 tableTitle.RenderBeginTag (writer);
1161 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1163 if (ShowNextPrevMonth) { // Previous Table Data
1164 cellNextPrev = new TableCell ();
1165 cellNextPrev.ApplyStyle (nextPrevStyle);
1166 cellNextPrev.Width = Unit.Percentage (15);
1168 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1169 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1170 cellNextPrev.RenderBeginTag (writer);
1171 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, true));
1172 cellNextPrev.RenderEndTag (writer);
1175 // Current Month Table Data
1178 TableCell cellMonth = new TableCell ();
1179 cellMonth.Width = Unit.Percentage (70);
1180 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1182 cellMonth.RenderBeginTag (writer);
1184 str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1186 if (TitleFormat == TitleFormat.MonthYear)
1187 str += " " + (DisplayDate.Year.ToString ());
1190 cellMonth.RenderEndTag (writer);
1193 if (ShowNextPrevMonth) { // Next Table Data
1194 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1195 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1197 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1198 cellNextPrev.RenderBeginTag (writer);
1199 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, true));
1200 cellNextPrev.RenderEndTag (writer);
1203 writer.RenderEndTag ();
1204 tableTitle.RenderEndTag (writer);
1205 titleCell.RenderEndTag (writer);
1206 writer.RenderEndTag (); //tr
1211 private string GetNextPrevFormatText (DateTime date, bool next)
1214 switch (NextPrevFormat) {
1215 case NextPrevFormat.FullMonth:
1216 text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1218 case NextPrevFormat.ShortMonth:
1219 text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1221 case NextPrevFormat.CustomText:
1224 text = NextMonthText;
1227 text = PrevMonthText;
1235 private bool IsWeekEnd (DayOfWeek day)
1237 return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1240 private void SelectDates (DateTime startDate, int days)
1242 SelectedDates.SelectRange (startDate, GetGlobalCalendar().AddDays (startDate, days - 1));
1243 OnSelectionChanged ();
1246 private double GetCellWidth ()
1248 return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1251 private System.Globalization.Calendar GetGlobalCalendar ()
1253 return DateTimeFormatInfo.CurrentInfo.Calendar;
1256 [WebSysDescription ("")]
1257 [WebCategory ("Action")]
1258 public event DayRenderEventHandler DayRender
1261 Events.AddHandler (DayRenderEvent, value);
1264 Events.RemoveHandler (DayRenderEvent, value);
1268 [WebSysDescription ("")]
1269 [WebCategory ("Action")]
1270 public event EventHandler SelectionChanged
1273 Events.AddHandler (SelectionChangedEvent, value);
1276 Events.RemoveHandler (SelectionChangedEvent, value);
1280 [WebSysDescription ("")]
1281 [WebCategory ("Action")]
1282 public event MonthChangedEventHandler VisibleMonthChanged
1285 Events.AddHandler (VisibleMonthChangedEvent, value);
1288 Events.RemoveHandler (VisibleMonthChangedEvent, value);