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;
36 using System.Security.Permissions;
38 namespace System.Web.UI.WebControls {
40 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
44 [DefaultEvent("SelectionChanged")]
45 [DefaultProperty("SelectedDate")]
46 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
48 [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
49 [SupportsEventValidation]
51 public class Calendar : WebControl, IPostBackEventHandler {
53 private TableItemStyle dayHeaderStyle;
54 private TableItemStyle dayStyle;
55 private TableItemStyle nextPrevStyle;
56 private TableItemStyle otherMonthDayStyle;
57 private TableItemStyle selectedDayStyle;
58 private TableItemStyle titleStyle;
59 private TableItemStyle todayDayStyle;
60 private TableItemStyle selectorStyle;
61 private TableItemStyle weekendDayStyle;
62 private DateTimeFormatInfo dateInfo;
63 private SelectedDatesCollection selectedDatesCollection;
64 private ArrayList dateList;
65 private static DateTime dateZenith = new DateTime (2000, 1,1);
66 private const int daysInAWeek = 7;
67 private static readonly object DayRenderEvent = new object ();
68 private static readonly object SelectionChangedEvent = new object ();
69 private static readonly object VisibleMonthChangedEvent = new object ();
73 dateInfo = new DateTimeFormatInfo ();
79 [WebSysDescription ("")]
80 [WebCategoryAttribute ("Appearance")]
81 public virtual string Caption
84 return ViewState.GetString ("Caption", "");
87 ViewState["Caption"] = value;
91 [DefaultValue (TableCaptionAlign.NotSet)]
92 [WebSysDescription ("")]
93 [WebCategoryAttribute ("Accessibility")]
94 public virtual TableCaptionAlign CaptionAlign
97 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
100 ViewState ["CaptionAlign"] = value;
109 [WebSysDescription ("")]
110 [WebCategory ("Layout")]
111 public int CellPadding {
113 return ViewState.GetInt ("CellPadding", 2);
118 throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
120 ViewState ["CellPadding"] = value;
128 [WebSysDescription ("")]
129 [WebCategory ("Layout")]
130 public int CellSpacing {
132 return ViewState.GetInt ("CellSpacing", 0);
137 throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
139 ViewState ["CellSpacing"] = value;
143 [PersistenceMode(PersistenceMode.InnerProperty)]
144 [NotifyParentProperty(true)]
145 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
146 [WebSysDescription ("")]
147 [WebCategory ("Style")]
148 public TableItemStyle DayHeaderStyle {
150 if (dayHeaderStyle == null) {
151 dayHeaderStyle = new TableItemStyle ();
152 if (IsTrackingViewState)
153 dayHeaderStyle.TrackViewState ();
156 return dayHeaderStyle;
163 [DefaultValue(DayNameFormat.Short)]
164 [WebSysDescription ("")]
165 [WebCategory ("Appearance")]
166 public DayNameFormat DayNameFormat {
168 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
172 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
173 value != DayNameFormat.Full && value != DayNameFormat.Short) {
174 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
177 ViewState ["DayNameFormat"] = value;
182 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
183 [NotifyParentProperty(true)]
184 [PersistenceMode(PersistenceMode.InnerProperty)]
185 [WebSysDescription ("")]
186 [WebCategory ("Style")]
187 public TableItemStyle DayStyle {
189 if (dayStyle == null) {
190 dayStyle = new TableItemStyle ();
191 if (IsTrackingViewState)
192 dayStyle.TrackViewState ();
202 [DefaultValue(FirstDayOfWeek.Default)]
203 [WebSysDescription ("")]
204 [WebCategory ("Appearance")]
205 public FirstDayOfWeek FirstDayOfWeek {
207 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
211 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
212 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
215 ViewState ["FirstDayOfWeek"] = value;
222 [DefaultValue(">")]
226 [WebSysDescription ("")]
227 [WebCategory ("Appearance")]
228 public string NextMonthText {
230 return ViewState.GetString ("NextMonthText", ">");
234 ViewState ["NextMonthText"] = value;
241 [DefaultValue(NextPrevFormat.CustomText)]
242 [WebSysDescription ("")]
243 [WebCategory ("Appearance")]
244 public NextPrevFormat NextPrevFormat {
246 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
250 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
251 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
254 ViewState ["NextPrevFormat"] = value;
258 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
259 [NotifyParentProperty(true)]
260 [PersistenceMode(PersistenceMode.InnerProperty)]
261 [WebSysDescription ("")]
262 [WebCategory ("Style")]
263 public TableItemStyle NextPrevStyle {
265 if (nextPrevStyle == null) {
266 nextPrevStyle = new TableItemStyle ();
267 if (IsTrackingViewState)
268 nextPrevStyle.TrackViewState ();
271 return nextPrevStyle;
276 [NotifyParentProperty(true)]
277 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
278 [PersistenceMode(PersistenceMode.InnerProperty)]
279 [WebSysDescription ("")]
280 [WebCategory ("Style")]
281 public TableItemStyle OtherMonthDayStyle {
283 if (otherMonthDayStyle == null) {
284 otherMonthDayStyle = new TableItemStyle ();
285 if (IsTrackingViewState)
286 otherMonthDayStyle.TrackViewState ();
289 return otherMonthDayStyle;
296 [DefaultValue("<")]
300 [WebSysDescription ("")]
301 [WebCategory ("Appearance")]
302 public string PrevMonthText {
304 return ViewState.GetString ("PrevMonthText", "<");
308 ViewState ["PrevMonthText"] = value;
313 [Bindable(true, BindingDirection.TwoWay)]
317 [DefaultValue("1/1/0001 12:00:00 AM")]
318 [WebSysDescription ("")]
319 [WebCategory ("Appearance")]
320 public DateTime SelectedDate {
322 if (SelectedDates.Count > 0)
323 return SelectedDates [0];
325 return DateTime.MinValue;
329 SelectedDates.SelectRange (value, value);
334 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
335 [WebSysDescription ("")]
336 [WebCategory ("Appearance")]
337 public SelectedDatesCollection SelectedDates {
339 if (dateList == null)
340 dateList = new ArrayList ();
342 if (selectedDatesCollection == null)
343 selectedDatesCollection = new SelectedDatesCollection (dateList);
345 return selectedDatesCollection;
350 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
351 [NotifyParentProperty(true)]
352 [PersistenceMode(PersistenceMode.InnerProperty)]
353 [WebSysDescription ("")]
354 [WebCategory ("Style")]
355 public TableItemStyle SelectedDayStyle {
357 if (selectedDayStyle == null) {
358 selectedDayStyle = new TableItemStyle ();
359 if (IsTrackingViewState)
360 selectedDayStyle.TrackViewState ();
363 return selectedDayStyle;
370 [DefaultValue(CalendarSelectionMode.Day)]
371 [WebSysDescription ("")]
372 [WebCategory ("Behavior")]
373 public CalendarSelectionMode SelectionMode {
375 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
379 if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
380 value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
381 throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
383 ViewState ["SelectionMode"] = value;
390 [DefaultValue(">>")]
394 [WebSysDescription ("")]
395 [WebCategory ("Appearance")]
396 public string SelectMonthText {
398 return ViewState.GetString ("SelectMonthText", ">>");
402 ViewState ["SelectMonthText"] = value;
406 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
407 [NotifyParentProperty(true)]
408 [PersistenceMode(PersistenceMode.InnerProperty)]
409 [WebSysDescription ("")]
410 [WebCategory ("Style")]
411 public TableItemStyle SelectorStyle {
413 if (selectorStyle == null) {
414 selectorStyle = new TableItemStyle ();
415 if (IsTrackingViewState)
416 selectorStyle.TrackViewState ();
419 return selectorStyle;
426 [DefaultValue(">")]
430 [WebSysDescription ("")]
431 [WebCategory ("Appearance")]
432 public string SelectWeekText {
434 return ViewState.GetString ("SelectWeekText", ">");
438 ViewState ["SelectWeekText"] = value;
446 [WebSysDescription ("")]
447 [WebCategory ("Appearance")]
448 public bool ShowDayHeader {
450 return ViewState.GetBool ("ShowDayHeader", true);
454 ViewState ["ShowDayHeader"] = value;
461 [DefaultValue(false)]
462 [WebSysDescription ("")]
463 [WebCategory ("Appearance")]
464 public bool ShowGridLines {
466 return ViewState.GetBool ("ShowGridLines", false);
470 ViewState ["ShowGridLines"] = value;
478 [WebSysDescription ("")]
479 [WebCategory ("Appearance")]
480 public bool ShowNextPrevMonth {
482 return ViewState.GetBool ("ShowNextPrevMonth", true);
486 ViewState ["ShowNextPrevMonth"] = value;
494 [WebSysDescription ("")]
495 [WebCategory ("Appearance")]
496 public bool ShowTitle {
498 return ViewState.GetBool ("ShowTitle", true);
502 ViewState ["ShowTitle"] = value;
509 [DefaultValue(TitleFormat.MonthYear)]
510 [WebSysDescription ("")]
511 [WebCategory ("Appearance")]
512 public TitleFormat TitleFormat {
514 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
518 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
519 throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
522 ViewState ["TitleFormat"] = value;
526 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
527 [NotifyParentProperty(true)]
528 [PersistenceMode(PersistenceMode.InnerProperty)]
529 [WebSysDescription ("")]
530 [WebCategory ("Style")]
531 public TableItemStyle TitleStyle {
533 if (titleStyle == null) {
534 titleStyle = new TableItemStyle ();
535 if (IsTrackingViewState)
536 titleStyle.TrackViewState ();
544 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
545 [NotifyParentProperty(true)]
546 [PersistenceMode(PersistenceMode.InnerProperty)]
547 [WebSysDescription ("")]
548 [WebCategory ("Style")]
549 public TableItemStyle TodayDayStyle {
551 if (todayDayStyle == null) {
552 todayDayStyle = new TableItemStyle ();
553 if (IsTrackingViewState)
554 todayDayStyle.TrackViewState ();
557 return todayDayStyle;
565 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
566 [WebSysDescription ("")]
567 [WebCategory ("Style")]
568 public DateTime TodaysDate {
570 object obj = ViewState ["TodaysDate"];
573 return (DateTime) obj;
575 return DateTime.Today;
579 ViewState ["TodaysDate"] = value.Date;
584 [DefaultValue (true)]
585 [WebSysDescription ("")]
586 [WebCategoryAttribute ("Accessibility")]
587 public virtual bool UseAccessibleHeader
590 return ViewState.GetBool ("UseAccessibleHeader", true);
593 ViewState ["UseAccessibleHeader"] = value;
599 [DefaultValue("1/1/0001 12:00:00 AM")]
600 [WebSysDescription ("")]
601 [WebCategory ("Style")]
602 public DateTime VisibleDate {
604 object obj = ViewState ["VisibleDate"];
607 return (DateTime) obj;
609 return DateTime.MinValue;
613 ViewState ["VisibleDate"] = value.Date;
617 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
618 [NotifyParentProperty(true)]
619 [PersistenceMode(PersistenceMode.InnerProperty)]
620 [WebSysDescription ("")]
621 [WebCategory ("Style")]
622 public TableItemStyle WeekendDayStyle {
624 if (weekendDayStyle == null) {
625 weekendDayStyle = new TableItemStyle ();
626 if (IsTrackingViewState)
627 weekendDayStyle.TrackViewState ();
630 return weekendDayStyle;
635 // Private properties
636 private DateTime DisplayDate {
638 DateTime dateTime = VisibleDate;
639 if (dateTime == DateTime.MinValue) // If visibledate is still the default value
640 dateTime = TodaysDate;
646 private DayOfWeek DisplayFirstDayOfWeek {
648 if (FirstDayOfWeek != FirstDayOfWeek.Default)
649 return (DayOfWeek) FirstDayOfWeek;
651 return (DayOfWeek) dateInfo.FirstDayOfWeek;
655 protected override ControlCollection CreateControlCollection ()
657 return base.CreateControlCollection ();
660 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
662 if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
669 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
671 RaisePostBackEvent (eventArgument);
674 protected virtual void RaisePostBackEvent (string arg)
676 void IPostBackEventHandler.RaisePostBackEvent (string arg)
682 if (arg[0] == 'V') { // Goes to Next or Previous month
683 DateTime prev = VisibleDate;
684 int days = Int32.Parse (arg.Substring (1));
685 DateTime dt = GetGlobalCalendar().AddDays (dateZenith, days);
687 OnVisibleMonthChanged (VisibleDate, prev);
691 if (arg[0] == 'R') { // Selects a range of dates
692 string num, date, days;
693 num = arg.Substring (1);
694 days = num.Substring (num.Length - 2, 2);
695 date = num.Substring (0, num.Length - 2);
696 DateTime d = GetGlobalCalendar().AddDays (dateZenith, Int32.Parse (date));
697 SelectedDates.SelectRange (d, d.AddDays (Int32.Parse (days)));
698 OnSelectionChanged ();
702 // Selects a single day
703 int daysFromZenith = Int32.Parse (arg);
704 DateTime day = GetGlobalCalendar().AddDays (dateZenith, daysFromZenith);
705 SelectedDates.SelectRange (day, day);
706 OnSelectionChanged ();
709 protected override void LoadViewState (object savedState)
711 object [] states = (object []) savedState;
713 if (states [0] != null)
714 base.LoadViewState (states [0]);
716 if (states [1] != null)
717 DayHeaderStyle.LoadViewState (states [1]);
719 if (states [2] != null)
720 DayStyle.LoadViewState (states [2]);
722 if (states [3] != null)
723 NextPrevStyle.LoadViewState (states [3]);
725 if (states [4] != null)
726 OtherMonthDayStyle.LoadViewState (states [4]);
728 if (states [5] != null)
729 SelectedDayStyle.LoadViewState (states [5]);
731 if (states [6] != null)
732 TitleStyle.LoadViewState (states [6]);
734 if (states [7] != null)
735 TodayDayStyle.LoadViewState (states [7]);
737 if (states [8] != null)
738 SelectorStyle.LoadViewState (states [8]);
740 if (states [9] != null)
741 WeekendDayStyle.LoadViewState (states [9]);
743 ArrayList array = (ArrayList) ViewState ["SelectedDates"];
746 selectedDatesCollection = new SelectedDatesCollection (dateList);
750 protected virtual void OnDayRender (TableCell cell, CalendarDay day)
752 DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
754 eh (this, new DayRenderEventArgs (cell, day));
762 override void OnPreRender (EventArgs e)
764 base.OnPreRender (e);
767 protected virtual void OnSelectionChanged ()
769 EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
771 eh (this, EventArgs.Empty);
774 protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
776 MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
778 eh (this, new MonthChangedEventArgs (newDate, previousDate));
786 override void Render (HtmlTextWriter writer)
788 Table table = new Table ();
789 table.CellSpacing = CellSpacing;
790 table.CellPadding = CellPadding;
791 if (ControlStyleCreated)
792 table.ControlStyle.CopyFrom (ControlStyle);
793 table.CopyBaseAttributes (this);
797 table.GridLines = GridLines.Both;
799 table.RenderBeginTag (writer);
803 WriteCaption (writer);
810 WriteDayHeader (writer);
814 table.RenderEndTag (writer);
817 protected override object SaveViewState ()
819 object [] states = new object [10];
821 if (dayHeaderStyle != null)
822 states [1] = dayHeaderStyle.SaveViewState ();
824 if (dayStyle != null)
825 states [2] = dayStyle.SaveViewState ();
827 if (nextPrevStyle != null)
828 states [3] = nextPrevStyle.SaveViewState ();
830 if (otherMonthDayStyle != null)
831 states [4] = otherMonthDayStyle.SaveViewState ();
833 if (selectedDayStyle != null)
834 states [5] = selectedDayStyle.SaveViewState ();
836 if (titleStyle != null)
837 states [6] = titleStyle.SaveViewState ();
839 if (todayDayStyle != null)
840 states [7] =todayDayStyle.SaveViewState ();
842 if (selectorStyle != null)
843 states [8] = selectorStyle.SaveViewState ();
845 if (weekendDayStyle != null)
846 states [9] = weekendDayStyle.SaveViewState ();
848 if (SelectedDates.Count > 0) {
849 ViewState ["SelectedDates"] = dateList;
852 states [0] = base.SaveViewState ();
854 for (int i = 0; i < states.Length; i++) {
855 if (states [i] != null) {
863 protected override void TrackViewState ()
865 base.TrackViewState ();
867 if (dayHeaderStyle != null)
868 dayHeaderStyle.TrackViewState ();
870 if (dayStyle != null)
871 dayStyle.TrackViewState ();
873 if (nextPrevStyle != null)
874 nextPrevStyle.TrackViewState ();
876 if (otherMonthDayStyle != null)
877 otherMonthDayStyle.TrackViewState ();
879 if (selectedDayStyle != null)
880 selectedDayStyle.TrackViewState ();
882 if (titleStyle != null)
883 titleStyle.TrackViewState ();
885 if (todayDayStyle != null)
886 todayDayStyle.TrackViewState ();
888 if (selectorStyle != null)
889 selectorStyle.TrackViewState ();
891 if (weekendDayStyle != null)
892 weekendDayStyle.TrackViewState ();
898 private void WriteDayHeader (HtmlTextWriter writer)
902 i = first = (int) (DisplayFirstDayOfWeek);
906 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
908 if (SelectionMode == CalendarSelectionMode.DayWeek) {
909 cell = new TableCell();
910 cell.HorizontalAlign = HorizontalAlign.Center;
911 cell.ApplyStyle (DayHeaderStyle);
914 cell.RenderBeginTag (writer);
915 cell.RenderEndTag (writer);
917 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
918 TableCell selector = new TableCell ();
919 selector.ApplyStyle (SelectorStyle);
920 selector.HorizontalAlign = HorizontalAlign.Center;
922 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
923 int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
925 selector.RenderBeginTag (writer);
926 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, true));
927 selector.RenderEndTag (writer);
932 DayOfWeek dayOfWeek = (DayOfWeek) i;
933 dayName = dateInfo.GetDayName (dayOfWeek);
936 if (UseAccessibleHeader) {
937 writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
938 writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col");
939 cell = new TableHeaderCell();
943 cell = new TableCell();
945 cell.HorizontalAlign = HorizontalAlign.Center;
946 cell.ApplyStyle (DayHeaderStyle);
948 cell.RenderBeginTag (writer);
950 switch (DayNameFormat) {
951 case DayNameFormat.FirstLetter:
952 dayName = dayName.Substring (0, 1);
954 case DayNameFormat.FirstTwoLetters:
955 dayName = dayName.Substring (0, 2);
957 case DayNameFormat.Full:
959 case DayNameFormat.Short:
961 dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
965 writer.Write (dayName);
966 cell.RenderEndTag (writer);
968 if (i >= daysInAWeek - 1) {
978 writer.RenderEndTag ();
981 private void WriteDay (DateTime date, HtmlTextWriter writer)
983 Style style = new Style ();
984 TableCell cell = new TableCell ();
986 CalendarDay day = new CalendarDay (date,
987 IsWeekEnd (date.DayOfWeek),
988 date == TodaysDate, SelectedDates.Contains (date),
989 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
990 date.Day.ToString ());
992 day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
993 cell.HorizontalAlign = HorizontalAlign.Center;
994 cell.Width = Unit.Percentage (GetCellWidth ());
996 LiteralControl lit = new LiteralControl (day.DayNumberText);
997 cell.Controls.Add (lit);
999 OnDayRender (cell, day);
1001 if (dayStyle != null && !dayStyle.IsEmpty) {
1002 style.CopyFrom (dayStyle);
1005 if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1006 style.CopyFrom (weekendDayStyle);
1009 if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1010 style.CopyFrom (todayDayStyle);
1013 if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1014 style.CopyFrom (otherMonthDayStyle);
1017 if (day.IsSelected) {
1018 style.BackColor = Color.Silver;
1019 style.ForeColor = Color.White;
1020 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1021 style.CopyFrom (selectedDayStyle);
1025 cell.ApplyStyle (style);
1027 lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1028 cell.ForeColor, day.IsSelectable);
1030 cell.RenderControl (writer);
1033 private void WriteDays (HtmlTextWriter writer)
1035 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1037 TableCell selectorCell = null;
1040 // Goes backwards until we find the date of that is begining of the week
1041 for (n = 0; n < daysInAWeek; n++) {
1042 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1045 date = GetGlobalCalendar().AddDays (date, -1);
1047 /* if the start date is the first day of the week, we need to shift backward one more week */
1049 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1051 lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1054 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1056 if (HasWeekSelectors (SelectionMode)) { // Week selector
1057 if (selectorCell == null) {
1058 selectorCell = new TableCell ();
1059 selectorCell.ApplyStyle (SelectorStyle);
1060 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1061 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1064 selectorCell.RenderBeginTag (writer);
1065 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, true));
1066 selectorCell.RenderEndTag (writer);
1069 for (int i = 0; i < daysInAWeek; i++) {
1070 WriteDay (date, writer);
1071 date = GetGlobalCalendar().AddDays (date, 1);
1074 writer.RenderEndTag ();
1075 if (date >= lastDate)
1080 private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1082 string str = string.Empty;
1084 hasLink = (Page != null && hasLink == true) ? true : false;
1088 str += Page.ClientScript.GetPostBackClientHyperlink (this, arg);
1092 if (!foreColor.IsEmpty) {
1095 if (ForeColor.IsEmpty)
1101 str += " style=color:" + ColorTranslator.ToHtml (clr);
1112 private int GetDaysFromZenith (DateTime date)
1114 TimeSpan span = date.Subtract (dateZenith);
1119 void WriteCaption (HtmlTextWriter writer)
1121 if (CaptionAlign != TableCaptionAlign.NotSet)
1122 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
1124 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1125 writer.Write (Caption);
1126 writer.RenderEndTag ();
1130 private void WriteTitle (HtmlTextWriter writer)
1132 TableCell cellNextPrev = null;
1133 TableCell titleCell = new TableCell ();
1134 Table tableTitle = new Table ();
1136 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1138 titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1140 if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1141 titleCell.BackColor = titleStyle.BackColor;
1143 titleCell.BackColor = Color.Silver;
1146 titleCell.RenderBeginTag (writer);
1149 tableTitle.Width = Unit.Percentage (100);
1150 if (titleStyle != null && !titleStyle.IsEmpty) {
1151 tableTitle.ApplyStyle (titleStyle);
1154 tableTitle.RenderBeginTag (writer);
1155 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1157 if (ShowNextPrevMonth) { // Previous Table Data
1158 cellNextPrev = new TableCell ();
1159 cellNextPrev.ApplyStyle (nextPrevStyle);
1160 cellNextPrev.Width = Unit.Percentage (15);
1162 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1163 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1164 cellNextPrev.RenderBeginTag (writer);
1165 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, true));
1166 cellNextPrev.RenderEndTag (writer);
1169 // Current Month Table Data
1172 TableCell cellMonth = new TableCell ();
1173 cellMonth.Width = Unit.Percentage (70);
1174 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1176 cellMonth.RenderBeginTag (writer);
1178 str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1180 if (TitleFormat == TitleFormat.MonthYear)
1181 str += " " + (DisplayDate.Year.ToString ());
1184 cellMonth.RenderEndTag (writer);
1187 if (ShowNextPrevMonth) { // Next Table Data
1188 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1189 date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
1191 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1192 cellNextPrev.RenderBeginTag (writer);
1193 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, true));
1194 cellNextPrev.RenderEndTag (writer);
1197 writer.RenderEndTag ();
1198 tableTitle.RenderEndTag (writer);
1199 titleCell.RenderEndTag (writer);
1200 writer.RenderEndTag (); //tr
1205 private string GetNextPrevFormatText (DateTime date, bool next)
1208 switch (NextPrevFormat) {
1209 case NextPrevFormat.FullMonth:
1210 text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1212 case NextPrevFormat.ShortMonth:
1213 text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1215 case NextPrevFormat.CustomText:
1217 text = ((next) ? NextMonthText : PrevMonthText);
1224 private bool IsWeekEnd (DayOfWeek day)
1226 return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1229 private double GetCellWidth ()
1231 return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1234 private System.Globalization.Calendar GetGlobalCalendar ()
1236 return DateTimeFormatInfo.CurrentInfo.Calendar;
1239 [WebSysDescription ("")]
1240 [WebCategory ("Action")]
1241 public event DayRenderEventHandler DayRender {
1243 Events.AddHandler (DayRenderEvent, value);
1246 Events.RemoveHandler (DayRenderEvent, value);
1250 [WebSysDescription ("")]
1251 [WebCategory ("Action")]
1252 public event EventHandler SelectionChanged {
1254 Events.AddHandler (SelectionChangedEvent, value);
1257 Events.RemoveHandler (SelectionChangedEvent, value);
1261 [WebSysDescription ("")]
1262 [WebCategory ("Action")]
1263 public event MonthChangedEventHandler VisibleMonthChanged {
1265 Events.AddHandler (VisibleMonthChangedEvent, value);
1268 Events.RemoveHandler (VisibleMonthChangedEvent, value);