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;
37 using System.Threading;
40 namespace System.Web.UI.WebControls {
42 [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
46 [DefaultEvent("SelectionChanged")]
47 [DefaultProperty("SelectedDate")]
48 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
50 [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
51 [SupportsEventValidation]
53 public class Calendar : WebControl, IPostBackEventHandler {
55 private TableItemStyle dayHeaderStyle;
56 private TableItemStyle dayStyle;
57 private TableItemStyle nextPrevStyle;
58 private TableItemStyle otherMonthDayStyle;
59 private TableItemStyle selectedDayStyle;
60 private TableItemStyle titleStyle;
61 private TableItemStyle todayDayStyle;
62 private TableItemStyle selectorStyle;
63 private TableItemStyle weekendDayStyle;
64 private DateTimeFormatInfo dateInfo;
65 private SelectedDatesCollection selectedDatesCollection;
66 private ArrayList dateList;
67 private DateTime today = DateTime.Today;
68 private static DateTime dateZenith = new DateTime (2000, 1,1);
69 private const int daysInAWeek = 7;
70 private static readonly object DayRenderEvent = new object ();
71 private static readonly object SelectionChangedEvent = new object ();
72 private static readonly object VisibleMonthChangedEvent = new object ();
76 dateInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
82 [WebSysDescription ("")]
83 [WebCategoryAttribute ("Appearance")]
84 public virtual string Caption
87 return ViewState.GetString ("Caption", "");
90 ViewState["Caption"] = value;
94 [DefaultValue (TableCaptionAlign.NotSet)]
95 [WebSysDescription ("")]
96 [WebCategoryAttribute ("Accessibility")]
97 public virtual TableCaptionAlign CaptionAlign
100 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
103 ViewState ["CaptionAlign"] = value;
112 [WebSysDescription ("")]
113 [WebCategory ("Layout")]
114 public int CellPadding {
116 return ViewState.GetInt ("CellPadding", 2);
121 throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
123 ViewState ["CellPadding"] = value;
131 [WebSysDescription ("")]
132 [WebCategory ("Layout")]
133 public int CellSpacing {
135 return ViewState.GetInt ("CellSpacing", 0);
140 throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
142 ViewState ["CellSpacing"] = value;
146 [PersistenceMode(PersistenceMode.InnerProperty)]
147 [NotifyParentProperty(true)]
148 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
149 [WebSysDescription ("")]
150 [WebCategory ("Style")]
151 public TableItemStyle DayHeaderStyle {
153 if (dayHeaderStyle == null) {
154 dayHeaderStyle = new TableItemStyle ();
155 if (IsTrackingViewState)
156 dayHeaderStyle.TrackViewState ();
159 return dayHeaderStyle;
166 [DefaultValue(DayNameFormat.Short)]
167 [WebSysDescription ("")]
168 [WebCategory ("Appearance")]
169 public DayNameFormat DayNameFormat {
171 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
175 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
176 value != DayNameFormat.Full && value != DayNameFormat.Short && value != DayNameFormat.Shortest) {
177 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
180 ViewState ["DayNameFormat"] = value;
185 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
186 [NotifyParentProperty(true)]
187 [PersistenceMode(PersistenceMode.InnerProperty)]
188 [WebSysDescription ("")]
189 [WebCategory ("Style")]
190 public TableItemStyle DayStyle {
192 if (dayStyle == null) {
193 dayStyle = new TableItemStyle ();
194 if (IsTrackingViewState)
195 dayStyle.TrackViewState ();
205 [DefaultValue(FirstDayOfWeek.Default)]
206 [WebSysDescription ("")]
207 [WebCategory ("Appearance")]
208 public FirstDayOfWeek FirstDayOfWeek {
210 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
214 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
215 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
218 ViewState ["FirstDayOfWeek"] = value;
225 [DefaultValue(">")]
229 [WebSysDescription ("")]
230 [WebCategory ("Appearance")]
231 public string NextMonthText {
233 return ViewState.GetString ("NextMonthText", ">");
237 ViewState ["NextMonthText"] = value;
244 [DefaultValue(NextPrevFormat.CustomText)]
245 [WebSysDescription ("")]
246 [WebCategory ("Appearance")]
247 public NextPrevFormat NextPrevFormat {
249 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
253 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
254 throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
257 ViewState ["NextPrevFormat"] = value;
261 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
262 [NotifyParentProperty(true)]
263 [PersistenceMode(PersistenceMode.InnerProperty)]
264 [WebSysDescription ("")]
265 [WebCategory ("Style")]
266 public TableItemStyle NextPrevStyle {
268 if (nextPrevStyle == null) {
269 nextPrevStyle = new TableItemStyle ();
270 if (IsTrackingViewState)
271 nextPrevStyle.TrackViewState ();
274 return nextPrevStyle;
279 [NotifyParentProperty(true)]
280 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
281 [PersistenceMode(PersistenceMode.InnerProperty)]
282 [WebSysDescription ("")]
283 [WebCategory ("Style")]
284 public TableItemStyle OtherMonthDayStyle {
286 if (otherMonthDayStyle == null) {
287 otherMonthDayStyle = new TableItemStyle ();
288 if (IsTrackingViewState)
289 otherMonthDayStyle.TrackViewState ();
292 return otherMonthDayStyle;
299 [DefaultValue("<")]
303 [WebSysDescription ("")]
304 [WebCategory ("Appearance")]
305 public string PrevMonthText {
307 return ViewState.GetString ("PrevMonthText", "<");
311 ViewState ["PrevMonthText"] = value;
316 [Bindable(true, BindingDirection.TwoWay)]
320 [DefaultValue("1/1/0001 12:00:00 AM")]
321 [WebSysDescription ("")]
322 [WebCategory ("Appearance")]
323 public DateTime SelectedDate {
325 if (SelectedDates.Count > 0)
326 return SelectedDates [0];
328 return DateTime.MinValue;
332 SelectedDates.SelectRange (value, value);
337 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
338 [WebSysDescription ("")]
339 [WebCategory ("Appearance")]
340 public SelectedDatesCollection SelectedDates {
342 if (dateList == null)
343 dateList = new ArrayList ();
345 if (selectedDatesCollection == null)
346 selectedDatesCollection = new SelectedDatesCollection (dateList);
348 return selectedDatesCollection;
353 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
354 [NotifyParentProperty(true)]
355 [PersistenceMode(PersistenceMode.InnerProperty)]
356 [WebSysDescription ("")]
357 [WebCategory ("Style")]
358 public TableItemStyle SelectedDayStyle {
360 if (selectedDayStyle == null) {
361 selectedDayStyle = new TableItemStyle ();
362 if (IsTrackingViewState)
363 selectedDayStyle.TrackViewState ();
366 return selectedDayStyle;
373 [DefaultValue(CalendarSelectionMode.Day)]
374 [WebSysDescription ("")]
375 [WebCategory ("Behavior")]
376 public CalendarSelectionMode SelectionMode {
378 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
382 if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
383 value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
384 throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
386 ViewState ["SelectionMode"] = value;
393 [DefaultValue(">>")]
397 [WebSysDescription ("")]
398 [WebCategory ("Appearance")]
399 public string SelectMonthText {
401 return ViewState.GetString ("SelectMonthText", ">>");
405 ViewState ["SelectMonthText"] = value;
409 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
410 [NotifyParentProperty(true)]
411 [PersistenceMode(PersistenceMode.InnerProperty)]
412 [WebSysDescription ("")]
413 [WebCategory ("Style")]
414 public TableItemStyle SelectorStyle {
416 if (selectorStyle == null) {
417 selectorStyle = new TableItemStyle ();
418 if (IsTrackingViewState)
419 selectorStyle.TrackViewState ();
422 return selectorStyle;
429 [DefaultValue(">")]
433 [WebSysDescription ("")]
434 [WebCategory ("Appearance")]
435 public string SelectWeekText {
437 return ViewState.GetString ("SelectWeekText", ">");
441 ViewState ["SelectWeekText"] = value;
449 [WebSysDescription ("")]
450 [WebCategory ("Appearance")]
451 public bool ShowDayHeader {
453 return ViewState.GetBool ("ShowDayHeader", true);
457 ViewState ["ShowDayHeader"] = value;
464 [DefaultValue(false)]
465 [WebSysDescription ("")]
466 [WebCategory ("Appearance")]
467 public bool ShowGridLines {
469 return ViewState.GetBool ("ShowGridLines", false);
473 ViewState ["ShowGridLines"] = value;
481 [WebSysDescription ("")]
482 [WebCategory ("Appearance")]
483 public bool ShowNextPrevMonth {
485 return ViewState.GetBool ("ShowNextPrevMonth", true);
489 ViewState ["ShowNextPrevMonth"] = value;
497 [WebSysDescription ("")]
498 [WebCategory ("Appearance")]
499 public bool ShowTitle {
501 return ViewState.GetBool ("ShowTitle", true);
505 ViewState ["ShowTitle"] = value;
512 [DefaultValue(TitleFormat.MonthYear)]
513 [WebSysDescription ("")]
514 [WebCategory ("Appearance")]
515 public TitleFormat TitleFormat {
517 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
521 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
522 throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
525 ViewState ["TitleFormat"] = value;
529 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
530 [NotifyParentProperty(true)]
531 [PersistenceMode(PersistenceMode.InnerProperty)]
532 [WebSysDescription ("")]
533 [WebCategory ("Style")]
534 public TableItemStyle TitleStyle {
536 if (titleStyle == null) {
537 titleStyle = new TableItemStyle ();
538 if (IsTrackingViewState)
539 titleStyle.TrackViewState ();
547 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
548 [NotifyParentProperty(true)]
549 [PersistenceMode(PersistenceMode.InnerProperty)]
550 [WebSysDescription ("")]
551 [WebCategory ("Style")]
552 public TableItemStyle TodayDayStyle {
554 if (todayDayStyle == null) {
555 todayDayStyle = new TableItemStyle ();
556 if (IsTrackingViewState)
557 todayDayStyle.TrackViewState ();
560 return todayDayStyle;
568 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
569 [WebSysDescription ("")]
570 [WebCategory ("Style")]
571 public DateTime TodaysDate {
573 object obj = ViewState ["TodaysDate"];
576 return (DateTime) obj;
582 ViewState ["TodaysDate"] = value.Date;
587 [DefaultValue (true)]
588 [WebSysDescription ("")]
589 [WebCategoryAttribute ("Accessibility")]
590 public virtual bool UseAccessibleHeader
593 return ViewState.GetBool ("UseAccessibleHeader", true);
596 ViewState ["UseAccessibleHeader"] = value;
602 [DefaultValue("1/1/0001 12:00:00 AM")]
603 [WebSysDescription ("")]
604 [WebCategory ("Style")]
605 public DateTime VisibleDate {
607 object obj = ViewState ["VisibleDate"];
610 return (DateTime) obj;
612 return DateTime.MinValue;
616 ViewState ["VisibleDate"] = value.Date;
620 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
621 [NotifyParentProperty(true)]
622 [PersistenceMode(PersistenceMode.InnerProperty)]
623 [WebSysDescription ("")]
624 [WebCategory ("Style")]
625 public TableItemStyle WeekendDayStyle {
627 if (weekendDayStyle == null) {
628 weekendDayStyle = new TableItemStyle ();
629 if (IsTrackingViewState)
630 weekendDayStyle.TrackViewState ();
633 return weekendDayStyle;
638 // Private properties
639 private DateTime DisplayDate {
641 DateTime dateTime = VisibleDate;
642 if (dateTime == DateTime.MinValue) // If visibledate is still the default value
643 dateTime = TodaysDate;
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 if (arg[0] == 'V') { // Goes to Next or Previous month
686 DateTime prev = VisibleDate;
687 int days = Int32.Parse (arg.Substring (1));
688 DateTime dt = GetGlobalCalendar().AddDays (dateZenith, days);
690 OnVisibleMonthChanged (VisibleDate, prev);
694 if (arg[0] == 'R') { // Selects a range of dates
695 string num, date, days;
696 num = arg.Substring (1);
697 days = num.Substring (num.Length - 2, 2);
698 date = num.Substring (0, num.Length - 2);
699 DateTime d = GetGlobalCalendar().AddDays (dateZenith, Int32.Parse (date));
700 SelectedDates.SelectRange (d, d.AddDays (Int32.Parse (days)));
701 OnSelectionChanged ();
705 // Selects a single day
706 int daysFromZenith = Int32.Parse (arg);
707 DateTime day = GetGlobalCalendar().AddDays (dateZenith, daysFromZenith);
708 SelectedDates.SelectRange (day, day);
709 OnSelectionChanged ();
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 DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
759 eh (this, new DayRenderEventArgs (cell, day, Page.ClientScript.GetPostBackClientHyperlink (this, GetDaysFromZenith (day.Date).ToString (), true)));
762 eh (this, new DayRenderEventArgs (cell, day));
770 override void OnPreRender (EventArgs e)
772 base.OnPreRender (e);
775 protected virtual void OnSelectionChanged ()
777 EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
779 eh (this, EventArgs.Empty);
782 protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
784 MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
786 eh (this, new MonthChangedEventArgs (newDate, previousDate));
794 override void Render (HtmlTextWriter writer)
796 TableStyle ts = new TableStyle ();
797 ts.CellSpacing = CellSpacing;
798 ts.CellPadding = CellPadding;
800 if (ControlStyleCreated)
801 ts.CopyFrom (ControlStyle);
803 ts.GridLines = GridLines.Both;
804 ts.AddAttributesToRender (writer);
805 writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
806 writer.RenderBeginTag (HtmlTextWriterTag.Table);
810 WriteCaption (writer);
817 WriteDayHeader (writer);
821 writer.RenderEndTag ();
824 protected override object SaveViewState ()
826 object [] states = new object [10];
828 if (dayHeaderStyle != null)
829 states [1] = dayHeaderStyle.SaveViewState ();
831 if (dayStyle != null)
832 states [2] = dayStyle.SaveViewState ();
834 if (nextPrevStyle != null)
835 states [3] = nextPrevStyle.SaveViewState ();
837 if (otherMonthDayStyle != null)
838 states [4] = otherMonthDayStyle.SaveViewState ();
840 if (selectedDayStyle != null)
841 states [5] = selectedDayStyle.SaveViewState ();
843 if (titleStyle != null)
844 states [6] = titleStyle.SaveViewState ();
846 if (todayDayStyle != null)
847 states [7] =todayDayStyle.SaveViewState ();
849 if (selectorStyle != null)
850 states [8] = selectorStyle.SaveViewState ();
852 if (weekendDayStyle != null)
853 states [9] = weekendDayStyle.SaveViewState ();
855 if (SelectedDates.Count > 0) {
856 ViewState ["SelectedDates"] = dateList;
859 states [0] = base.SaveViewState ();
861 for (int i = 0; i < states.Length; i++) {
862 if (states [i] != null) {
870 protected override void TrackViewState ()
872 base.TrackViewState ();
874 if (dayHeaderStyle != null)
875 dayHeaderStyle.TrackViewState ();
877 if (dayStyle != null)
878 dayStyle.TrackViewState ();
880 if (nextPrevStyle != null)
881 nextPrevStyle.TrackViewState ();
883 if (otherMonthDayStyle != null)
884 otherMonthDayStyle.TrackViewState ();
886 if (selectedDayStyle != null)
887 selectedDayStyle.TrackViewState ();
889 if (titleStyle != null)
890 titleStyle.TrackViewState ();
892 if (todayDayStyle != null)
893 todayDayStyle.TrackViewState ();
895 if (selectorStyle != null)
896 selectorStyle.TrackViewState ();
898 if (weekendDayStyle != null)
899 weekendDayStyle.TrackViewState ();
905 private void WriteDayHeader (HtmlTextWriter writer)
909 i = first = (int) (DisplayFirstDayOfWeek);
913 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
915 if (SelectionMode == CalendarSelectionMode.DayWeek) {
916 cell = new TableCell();
917 cell.HorizontalAlign = HorizontalAlign.Center;
918 cell.ApplyStyle (DayHeaderStyle);
921 cell.RenderBeginTag (writer);
922 cell.RenderEndTag (writer);
924 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
925 TableCell selector = new TableCell ();
926 selector.ApplyStyle (SelectorStyle);
927 selector.HorizontalAlign = HorizontalAlign.Center;
929 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
930 int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
932 selector.RenderBeginTag (writer);
933 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, Enabled));
934 selector.RenderEndTag (writer);
939 DayOfWeek dayOfWeek = (DayOfWeek) i;
940 dayName = dateInfo.GetDayName (dayOfWeek);
943 if (UseAccessibleHeader) {
944 writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
945 writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col", false);
946 cell = new TableHeaderCell();
950 cell = new TableCell();
952 cell.HorizontalAlign = HorizontalAlign.Center;
953 cell.ApplyStyle (DayHeaderStyle);
955 cell.RenderBeginTag (writer);
957 switch (DayNameFormat) {
958 case DayNameFormat.FirstLetter:
959 dayName = dayName.Substring (0, 1);
961 case DayNameFormat.FirstTwoLetters:
962 dayName = dayName.Substring (0, 2);
965 case DayNameFormat.Shortest:
966 dayName = dateInfo.GetShortestDayName (dayOfWeek);
969 case DayNameFormat.Full:
971 case DayNameFormat.Short:
973 dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
977 writer.Write (dayName);
978 cell.RenderEndTag (writer);
980 if (i >= daysInAWeek - 1) {
990 writer.RenderEndTag ();
993 private void WriteDay (DateTime date, HtmlTextWriter writer)
995 TableItemStyle style = new TableItemStyle ();
996 TableCell cell = new TableCell ();
998 CalendarDay day = new CalendarDay (date,
999 IsWeekEnd (date.DayOfWeek),
1000 date == TodaysDate, SelectedDates.Contains (date),
1001 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
1002 date.Day.ToString ());
1004 day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
1005 cell.HorizontalAlign = HorizontalAlign.Center;
1006 cell.Width = Unit.Percentage (GetCellWidth ());
1008 LiteralControl lit = new LiteralControl (day.DayNumberText);
1009 cell.Controls.Add (lit);
1011 OnDayRender (cell, day);
1013 if (dayStyle != null && !dayStyle.IsEmpty) {
1014 style.CopyFrom (dayStyle);
1017 if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1018 style.CopyFrom (weekendDayStyle);
1021 if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1022 style.CopyFrom (todayDayStyle);
1025 if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1026 style.CopyFrom (otherMonthDayStyle);
1029 if (day.IsSelected && Enabled) {
1030 style.BackColor = Color.Silver;
1031 style.ForeColor = Color.White;
1032 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1033 style.CopyFrom (selectedDayStyle);
1037 cell.ApplyStyle (style);
1039 lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1040 cell.ForeColor, day.IsSelectable && Enabled);
1042 cell.RenderControl (writer);
1045 private void WriteDays (HtmlTextWriter writer)
1047 DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1049 TableCell selectorCell = null;
1052 // Goes backwards until we find the date of that is begining of the week
1053 for (n = 0; n < daysInAWeek; n++) {
1054 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1057 date = GetGlobalCalendar().AddDays (date, -1);
1059 /* if the start date is the first day of the week, we need to shift backward one more week */
1061 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1063 lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1066 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1068 if (HasWeekSelectors (SelectionMode)) { // Week selector
1069 if (selectorCell == null) {
1070 selectorCell = new TableCell ();
1071 selectorCell.ApplyStyle (SelectorStyle);
1072 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1073 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1076 selectorCell.RenderBeginTag (writer);
1077 writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, Enabled));
1078 selectorCell.RenderEndTag (writer);
1081 for (int i = 0; i < daysInAWeek; i++) {
1082 WriteDay (date, writer);
1083 date = GetGlobalCalendar().AddDays (date, 1);
1086 writer.RenderEndTag ();
1087 if (date >= lastDate)
1092 private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1094 StringBuilder str = new StringBuilder ();
1096 hasLink = (Page != null && hasLink == true) ? true : false;
1099 str.Append ("<a href=\"");
1101 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg, true));
1103 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg));
1108 if (!foreColor.IsEmpty) {
1111 if (ForeColor.IsEmpty)
1117 str.Append (" style=\"color:" + ColorTranslator.ToHtml (clr));
1120 str.Append ("</a>");
1125 return str.ToString ();
1128 private int GetDaysFromZenith (DateTime date)
1130 TimeSpan span = date.Subtract (dateZenith);
1135 void WriteCaption (HtmlTextWriter writer)
1137 if (CaptionAlign != TableCaptionAlign.NotSet)
1138 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
1140 writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1141 writer.Write (Caption);
1142 writer.RenderEndTag ();
1146 private void WriteTitle (HtmlTextWriter writer)
1148 TableCell cellNextPrev = null;
1149 TableCell titleCell = new TableCell ();
1150 Table tableTitle = new Table ();
1152 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1154 titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1156 if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1157 titleCell.BackColor = titleStyle.BackColor;
1159 titleCell.BackColor = Color.Silver;
1162 titleCell.RenderBeginTag (writer);
1165 tableTitle.Width = Unit.Percentage (100);
1166 if (titleStyle != null && !titleStyle.IsEmpty) {
1167 tableTitle.ApplyStyle (titleStyle);
1170 tableTitle.RenderBeginTag (writer);
1171 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1173 if (ShowNextPrevMonth) { // Previous Table Data
1174 cellNextPrev = new TableCell ();
1175 cellNextPrev.ApplyStyle (nextPrevStyle);
1176 cellNextPrev.Width = Unit.Percentage (15);
1178 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1179 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1180 cellNextPrev.RenderBeginTag (writer);
1181 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, Enabled));
1182 cellNextPrev.RenderEndTag (writer);
1185 // Current Month Table Data
1188 TableCell cellMonth = new TableCell ();
1189 cellMonth.Width = Unit.Percentage (70);
1190 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1192 cellMonth.RenderBeginTag (writer);
1194 if (TitleFormat == TitleFormat.MonthYear)
1195 str = DisplayDate.ToString (dateInfo.YearMonthPattern, dateInfo);
1197 str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1200 cellMonth.RenderEndTag (writer);
1203 if (ShowNextPrevMonth) { // Next Table Data
1204 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1205 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1207 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1208 cellNextPrev.RenderBeginTag (writer);
1209 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, Enabled));
1210 cellNextPrev.RenderEndTag (writer);
1213 writer.RenderEndTag ();
1214 tableTitle.RenderEndTag (writer);
1215 titleCell.RenderEndTag (writer);
1216 writer.RenderEndTag (); //tr
1221 private string GetNextPrevFormatText (DateTime date, bool next)
1224 switch (NextPrevFormat) {
1225 case NextPrevFormat.FullMonth:
1226 text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1228 case NextPrevFormat.ShortMonth:
1229 text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1231 case NextPrevFormat.CustomText:
1233 text = ((next) ? NextMonthText : PrevMonthText);
1240 private bool IsWeekEnd (DayOfWeek day)
1242 return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1245 private double GetCellWidth ()
1247 return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1250 private System.Globalization.Calendar GetGlobalCalendar ()
1252 return DateTimeFormatInfo.CurrentInfo.Calendar;
1255 [WebSysDescription ("")]
1256 [WebCategory ("Action")]
1257 public event DayRenderEventHandler DayRender {
1259 Events.AddHandler (DayRenderEvent, value);
1262 Events.RemoveHandler (DayRenderEvent, value);
1266 [WebSysDescription ("")]
1267 [WebCategory ("Action")]
1268 public event EventHandler SelectionChanged {
1270 Events.AddHandler (SelectionChangedEvent, value);
1273 Events.RemoveHandler (SelectionChangedEvent, value);
1277 [WebSysDescription ("")]
1278 [WebCategory ("Action")]
1279 public event MonthChangedEventHandler VisibleMonthChanged {
1281 Events.AddHandler (VisibleMonthChangedEvent, value);
1284 Events.RemoveHandler (VisibleMonthChangedEvent, value);