2 // System.Web.UI.WebControls.Calendar.cs
5 // Gaurav Vaish (gvaish@iitk.ac.in)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (c) Copyright 2002 Ximian, Inc. (http://www.ximian.com)
10 // (C) Gaurav Vaish (2002)
11 // (C) 2003 Andreas Nahr
16 using System.Collections;
\r
17 using System.Globalization;
\r
20 using System.Web.UI;
\r
21 using System.Drawing;
\r
22 using System.ComponentModel;
23 using System.ComponentModel.Design;
\r
25 namespace System.Web.UI.WebControls
\r
27 [DefaultEvent("SelectionChanged")]
\r
28 [DefaultProperty("SelectedDate")]
\r
29 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
30 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
31 public class Calendar : WebControl, IPostBackEventHandler
\r
34 private TableItemStyle dayHeaderStyle;
\r
35 private TableItemStyle dayStyle;
\r
36 private TableItemStyle nextPrevStyle;
\r
37 private TableItemStyle otherMonthDayStyle;
\r
38 private SelectedDatesCollection selectedDates;
\r
39 private ArrayList selectedDatesList;
\r
40 private TableItemStyle selectedDayStyle;
\r
41 private TableItemStyle selectorStyle;
\r
42 private TableItemStyle titleStyle;
\r
43 private TableItemStyle todayDayStyle;
\r
44 private TableItemStyle weekendDayStyle;
\r
46 private static readonly object DayRenderEvent = new object();
\r
47 private static readonly object SelectionChangedEvent = new object();
\r
48 private static readonly object VisibleMonthChangedEvent = new object();
\r
50 private Color defaultTextColor;
\r
51 private System.Globalization.Calendar globCal;
\r
52 private DateTimeFormatInfo infoCal = DateTimeFormatInfo.CurrentInfo;
54 private static readonly DateTime begin_date = new DateTime (2000,01,01,0,0,0,0);
57 private static int MASK_WEEKEND = (0x01 << 0);
\r
58 private static int MASK_OMONTH = (0x01 << 1);
\r
59 private static int MASK_TODAY = (0x01 << 2);
\r
60 private static int MASK_SELECTED = (0x01 << 3);
\r
61 private static int MASK_DAY = (0x01 << 4);
\r
62 private static int MASK_UNIQUE = MASK_WEEKEND | MASK_OMONTH | MASK_TODAY | MASK_SELECTED;
\r
64 public Calendar(): base()
\r
69 [DefaultValue (2), Bindable (true), WebCategory ("Layout")]
70 [WebSysDescription ("The border left within the calendar days.")]
\r
71 public int CellPadding
\r
75 object o = ViewState["CellPadding"];
\r
83 throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
\r
84 ViewState["CellPadding"] = value;
\r
88 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
89 [WebSysDescription ("The border left between calendar days.")]
\r
90 public int CellSpacing
\r
94 object o = ViewState["CellSpacing"];
\r
102 throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
\r
103 ViewState["CellSpacing"] = value;
\r
107 [NotifyParentProperty (true), WebCategory ("Style")]
108 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
109 [PersistenceMode (PersistenceMode.InnerProperty)]
110 [WebSysDescription ("The style applied to the day header.")]
\r
111 public TableItemStyle DayHeaderStyle
\r
115 if(dayHeaderStyle==null)
\r
116 dayHeaderStyle = new TableItemStyle();
\r
117 if(IsTrackingViewState)
\r
118 dayHeaderStyle.TrackViewState();
\r
119 return dayHeaderStyle;
\r
123 [DefaultValue (typeof (DayNameFormat), "Short"), Bindable (true), WebCategory ("Appearance")]
124 [WebSysDescription ("The format for the day name display.")]
\r
125 public DayNameFormat DayNameFormat
\r
129 object o = ViewState["DayNameFormat"];
\r
131 return (DayNameFormat)o;
\r
132 return DayNameFormat.Short;
\r
136 if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
\r
137 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
138 ViewState["DayNameFormat"] = value;
\r
142 [DefaultValue (null)]
143 [NotifyParentProperty (true), WebCategory ("Style")]
144 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
145 [PersistenceMode (PersistenceMode.InnerProperty)]
146 [WebSysDescription ("The style applied to the day entry.")]
\r
147 public TableItemStyle DayStyle
\r
152 dayStyle = new TableItemStyle();
\r
153 if(IsTrackingViewState)
\r
154 dayStyle.TrackViewState();
\r
159 [DefaultValue (typeof (FirstDayOfWeek), "Default"), Bindable (true), WebCategory ("Appearance")]
160 [WebSysDescription ("The day that a week begins with.")]
\r
161 public FirstDayOfWeek FirstDayOfWeek
\r
165 object o = ViewState["FirstDayOfWeek"];
\r
167 return (FirstDayOfWeek)o;
\r
168 return FirstDayOfWeek.Default;
\r
172 if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
\r
173 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
174 ViewState["FirstDayOfWeek"] = value;
\r
178 [DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
179 [WebSysDescription ("The text for selecting the next month.")]
\r
180 public string NextMonthText
\r
184 object o = ViewState["NextMonthText"];
\r
191 ViewState["NextMonthText"] = value;
\r
195 [DefaultValue (typeof (NextPrevFormat), "CustomText"), Bindable (true), WebCategory ("Appearance")]
196 [WebSysDescription ("The format for the month navigation.")]
\r
197 public NextPrevFormat NextPrevFormat
\r
201 object o = ViewState["NextPrevFormat"];
\r
203 return (NextPrevFormat)o;
\r
204 return NextPrevFormat.CustomText;
\r
208 if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
\r
209 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
210 ViewState["NextPrevFormat"] = value;
\r
214 [NotifyParentProperty (true), WebCategory ("Style")]
215 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
216 [PersistenceMode (PersistenceMode.InnerProperty)]
217 [WebSysDescription ("The style applied to the month navigation.")]
\r
218 public TableItemStyle NextPrevStyle
\r
222 if(nextPrevStyle == null)
\r
223 nextPrevStyle = new TableItemStyle();
\r
224 if(IsTrackingViewState)
\r
225 nextPrevStyle.TrackViewState();
\r
226 return nextPrevStyle;
\r
230 [DefaultValue (null)]
231 [NotifyParentProperty (true), WebCategory ("Style")]
232 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
233 [PersistenceMode (PersistenceMode.InnerProperty)]
234 [WebSysDescription ("The style applied to day entries belonging to another month.")]
\r
235 public TableItemStyle OtherMonthDayStyle
\r
239 if(otherMonthDayStyle == null)
\r
240 otherMonthDayStyle = new TableItemStyle();
\r
241 if(IsTrackingViewState)
\r
242 otherMonthDayStyle.TrackViewState();
\r
243 return otherMonthDayStyle;
\r
247 [DefaultValue ("<"), Bindable (true), WebCategory ("Appearance")]
248 [WebSysDescription ("The text for selecting the previous month.")]
\r
249 public string PrevMonthText
\r
253 object o = ViewState["PrevMonthText"];
\r
260 ViewState["PrevMonthText"] = value;
\r
264 [DefaultValue (null), Bindable (true)]
265 [WebSysDescription ("The currently selected date.")]
\r
266 public DateTime SelectedDate
\r
270 if(SelectedDates.Count > 0)
\r
272 return SelectedDates[0];
\r
274 return DateTime.MinValue;
\r
278 if(value == DateTime.MinValue)
\r
280 SelectedDates.Clear();
\r
283 SelectedDates.SelectRange(value, value);
\r
288 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
289 [WebSysDescription ("All currently selected dates.")]
\r
290 public SelectedDatesCollection SelectedDates
\r
294 if(selectedDates==null)
\r
296 if(selectedDatesList == null)
\r
297 selectedDatesList = new ArrayList();
\r
298 selectedDates = new SelectedDatesCollection(selectedDatesList);
\r
300 return selectedDates;
\r
304 [DefaultValue (null)]
305 [NotifyParentProperty (true), WebCategory ("Style")]
306 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
307 [PersistenceMode (PersistenceMode.InnerProperty)]
308 [WebSysDescription ("The style applied to the selected day.")]
\r
309 public TableItemStyle SelectedDayStyle
\r
313 if(selectedDayStyle==null)
\r
314 selectedDayStyle = new TableItemStyle();
\r
315 if(IsTrackingViewState)
\r
316 selectedDayStyle.TrackViewState();
\r
317 return selectedDayStyle;
\r
321 [DefaultValue (typeof (CalendarSelectionMode), "Day"), Bindable (true), WebCategory ("Behavior")]
322 [WebSysDescription ("The mode in which days or other entries are selected.")]
\r
323 public CalendarSelectionMode SelectionMode
\r
327 object o = ViewState["SelectionMode"];
\r
329 return (CalendarSelectionMode)o;
\r
330 return CalendarSelectionMode.Day;
\r
334 if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
\r
335 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
336 ViewState["SelectionMode"] = value;
\r
340 [DefaultValue (">>"), Bindable (true), WebCategory ("Appearance")]
341 [WebSysDescription ("The text that is used for selection of months.")]
\r
342 public string SelectMonthText
\r
346 object o = ViewState["SelectMonthText"];
\r
353 ViewState["SelectMonthText"] = value;
\r
357 [NotifyParentProperty (true), WebCategory ("Style")]
358 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
359 [PersistenceMode (PersistenceMode.InnerProperty)]
360 [WebSysDescription ("The style applied to the selector.")]
\r
361 public TableItemStyle SelectorStyle
\r
365 if(selectorStyle==null)
\r
366 selectorStyle = new TableItemStyle();
\r
367 return selectorStyle;
\r
371 [DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
372 [WebSysDescription ("The text that is used for selection of weeks.")]
\r
373 public string SelectWeekText
\r
377 object o = ViewState["SelectWeekText"];
\r
384 ViewState["SelectWeekText"] = value;
\r
388 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
389 [WebSysDescription ("Determines if the header for days is shown.")]
\r
390 public bool ShowDayHeader
\r
394 object o = ViewState["ShowDayHeader"];
\r
401 ViewState["ShowDayHeader"] = value;
\r
405 [DefaultValue (false), Bindable (true), WebCategory ("Appearance")]
406 [WebSysDescription ("Determines if gridlines are shown.")]
\r
407 public bool ShowGridLines
\r
411 object o = ViewState["ShowGridLines"];
\r
418 ViewState["ShowGridLines"] = value;
\r
422 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
423 [WebSysDescription ("Determines if month navigation is shown.")]
\r
424 public bool ShowNextPrevMonth
\r
428 object o = ViewState["ShowNextPrevMonth"];
\r
435 ViewState["ShowNextPrevMonth"] = value;
\r
439 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
440 [WebSysDescription ("Determines if the title is shown.")]
\r
441 public bool ShowTitle
\r
445 object o = ViewState["ShowTitle"];
\r
452 ViewState["ShowTitle"] = value;
\r
456 [DefaultValue (typeof (TitleFormat), "MonthYear"), Bindable (true), WebCategory ("Appearance")]
457 [WebSysDescription ("The format in which the title is rendered.")]
\r
458 public TitleFormat TitleFormat
\r
462 object o = ViewState["TitleFormat"];
\r
464 return (TitleFormat)o;
\r
465 return TitleFormat.MonthYear;
\r
469 if(!System.Enum.IsDefined(typeof(TitleFormat), value))
\r
470 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
471 ViewState["TitleFormat"] = value;
\r
475 [NotifyParentProperty (true), WebCategory ("Style")]
476 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
477 [PersistenceMode (PersistenceMode.InnerProperty)]
478 [WebSysDescription ("The style applied to the title.")]
\r
479 public TableItemStyle TitleStyle
\r
483 if(titleStyle==null)
\r
484 titleStyle = new TableItemStyle();
\r
485 if(IsTrackingViewState)
\r
486 titleStyle.TrackViewState();
\r
491 [DefaultValue (null)]
492 [NotifyParentProperty (true), WebCategory ("Style")]
493 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
494 [PersistenceMode (PersistenceMode.InnerProperty)]
495 [WebSysDescription ("The style applied to the today's date display.")]
\r
496 public TableItemStyle TodayDayStyle
\r
500 if(todayDayStyle==null)
\r
501 todayDayStyle = new TableItemStyle();
\r
502 if(IsTrackingViewState)
\r
503 todayDayStyle.TrackViewState();
\r
504 return todayDayStyle;
\r
508 [Bindable (true), Browsable (false)]
509 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
510 [WebSysDescription ("The current date.")]
\r
511 public DateTime TodaysDate
\r
515 object o = ViewState["TodaysDate"];
\r
517 return (DateTime)o;
\r
518 return DateTime.Today;
\r
522 ViewState["TodaysDate"] = value.Date;
\r
526 [DefaultValue (null), Bindable (true)]
527 [WebSysDescription ("The month that is displayed.")]
\r
528 public DateTime VisibleDate
\r
532 object o = ViewState["VisibleDate"];
\r
534 return (DateTime)o;
\r
535 return DateTime.MinValue;
\r
539 ViewState["VisibleDate"] = value.Date;
\r
543 [NotifyParentProperty (true), WebCategory ("Style")]
544 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
545 [PersistenceMode (PersistenceMode.InnerProperty)]
546 [WebSysDescription ("The style applied to weekend days.")]
\r
547 public TableItemStyle WeekendDayStyle
\r
551 if(weekendDayStyle == null)
\r
552 weekendDayStyle = new TableItemStyle();
\r
553 if(IsTrackingViewState)
\r
555 weekendDayStyle.TrackViewState();
\r
557 return weekendDayStyle;
\r
561 [WebCategory ("Action")]
562 [WebSysDescription ("Raised when a day entry is rendered.")]
\r
563 public event DayRenderEventHandler DayRender
\r
567 Events.AddHandler(DayRenderEvent, value);
\r
571 Events.RemoveHandler(DayRenderEvent, value);
\r
575 [WebCategory ("Action")]
576 [WebSysDescription ("Raised when another entry is selected.")]
\r
577 public event EventHandler SelectionChanged
\r
581 Events.AddHandler(SelectionChangedEvent, value);
\r
585 Events.RemoveHandler(SelectionChangedEvent, value);
\r
589 [WebCategory ("Action")]
590 [WebSysDescription ("Raised when a the currently visible month has changed.")]
\r
591 public event MonthChangedEventHandler VisibleMonthChanged
\r
595 Events.AddHandler(VisibleMonthChangedEvent, value);
\r
599 Events.RemoveHandler(VisibleMonthChangedEvent, value);
\r
603 protected virtual void OnDayRender(TableCell cell, CalendarDay day)
\r
607 DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
\r
609 dreh(this, new DayRenderEventArgs(cell, day));
\r
613 protected virtual void OnSelectionChanged()
\r
617 EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
\r
619 eh(this, new EventArgs());
\r
623 protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
\r
627 MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
\r
629 mceh(this, new MonthChangedEventArgs(newDate, prevDate));
\r
634 /// See test6.aspx in Tests directory for verification
\r
636 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
638 // initialize the calendar...TODO: find out why this isn't done in the constructor
639 // if the culture is changed between rendering and postback this will be broken
640 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
642 // TODO: Find out what kind of exceptions to throw when we get bad data
643 if (eventArgument.StartsWith ("V")) {
644 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument.Substring (1)), 0, 0, 0);
645 DateTime new_date = begin_date + mod;
646 VisibleDate = new_date;
647 OnVisibleMonthChanged (VisibleDate, new_date);
648 } else if (eventArgument.StartsWith ("R")) {
649 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument.Substring (1,
650 eventArgument.Length - 3)), 0, 0, 0);
651 DateTime range_begin = begin_date + mod;
652 int sel_days = Int32.Parse (eventArgument.Substring (eventArgument.Length - 2));
653 SelectRangeInternal (range_begin, range_begin.AddDays (sel_days - 1), GetEffectiveVisibleDate ());
655 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument), 0, 0, 0);
656 DateTime day = begin_date + mod;
657 SelectRangeInternal (day, day, GetEffectiveVisibleDate ());
662 protected override void OnPreRender (EventArgs e)
666 protected override void Render(HtmlTextWriter writer)
\r
668 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
669 DateTime visDate = GetEffectiveVisibleDate();
\r
670 DateTime firstDate = GetFirstCalendarDay(visDate);
\r
673 bool isHtmlTextWriter;
\r
674 //FIXME: when Control.Site works, reactivate this
\r
675 //if (Page == null || Site == null) {
\r
676 // isEnabled = false;
\r
677 // isHtmlTextWriter = false;
\r
679 isEnabled = Enabled;
\r
680 isHtmlTextWriter = (writer.GetType() != typeof(HtmlTextWriter));
\r
682 defaultTextColor = ForeColor;
\r
683 if(defaultTextColor == Color.Empty)
\r
684 defaultTextColor = Color.Black;
\r
686 Table calTable = new Table ();
\r
688 calTable.CopyBaseAttributes(this);
\r
689 if(ControlStyleCreated)
\r
690 calTable.ApplyStyle(ControlStyle);
\r
691 calTable.Width = Width;
\r
692 calTable.Height = Height;
\r
693 calTable.CellSpacing = CellSpacing;
\r
694 calTable.CellPadding = CellPadding;
\r
696 if (ControlStyleCreated &&
\r
697 ControlStyle.IsSet (WebControls.Style.BORDERWIDTH) &&
\r
698 BorderWidth != Unit.Empty)
\r
699 calTable.BorderWidth = BorderWidth;
\r
701 calTable.BorderWidth = Unit.Pixel(1);
\r
704 calTable.GridLines = GridLines.Both;
\r
706 calTable.GridLines = GridLines.None;
\r
708 calTable.RenderBeginTag (writer);
\r
711 RenderTitle (writer, visDate, SelectionMode, isEnabled);
\r
714 RenderHeader (writer, firstDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
716 RenderAllDays (writer, firstDate, visDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
718 calTable.RenderEndTag(writer);
\r
721 protected override ControlCollection CreateControlCollection()
\r
723 return new EmptyControlCollection(this);
\r
726 protected override void LoadViewState(object savedState)
\r
728 if (savedState!=null) {
\r
729 object[] states = (object[]) savedState;
\r
730 if(states[0] != null)
\r
731 base.LoadViewState(states[0]);
\r
732 if(states[1] != null)
\r
733 DayHeaderStyle.LoadViewState(states[1]);
\r
734 if(states[2] != null)
\r
735 DayStyle.LoadViewState(states[2]);
\r
736 if(states[3] != null)
\r
737 NextPrevStyle.LoadViewState(states[3]);
\r
738 if(states[4] != null)
\r
739 OtherMonthDayStyle.LoadViewState(states[4]);
\r
740 if(states[5] != null)
\r
741 SelectedDayStyle.LoadViewState(states[5]);
\r
742 if(states[6] != null)
\r
743 SelectorStyle.LoadViewState(states[6]);
\r
744 if(states[7] != null)
\r
745 TitleStyle.LoadViewState(states[7]);
\r
746 if(states[8] != null)
\r
747 TodayDayStyle.LoadViewState(states[8]);
\r
748 if(states[9] != null)
\r
749 WeekendDayStyle.LoadViewState(states[9]);
\r
751 ArrayList dateList = ViewState ["_CalendarSelectedDates"] as ArrayList;
\r
752 if (dateList != null)
\r
753 selectedDates = new SelectedDatesCollection (dateList);
\r
757 protected override object SaveViewState()
\r
759 if (SelectedDates.Count > 0)
\r
760 ViewState["_CalendarSelectedDates"] = selectedDates.GetDateList ();
762 object[] states = new object [10];
\r
763 states[0] = base.SaveViewState();
\r
764 states[1] = (dayHeaderStyle == null ? null : dayHeaderStyle.SaveViewState());
\r
765 states[2] = (dayStyle == null ? null : dayStyle.SaveViewState());
\r
766 states[3] = (nextPrevStyle == null ? null : nextPrevStyle.SaveViewState());
\r
767 states[4] = (otherMonthDayStyle == null ? null : otherMonthDayStyle.SaveViewState());
\r
768 states[5] = (selectedDayStyle == null ? null : selectedDayStyle.SaveViewState());
\r
769 states[6] = (selectorStyle == null ? null : selectorStyle.SaveViewState());
\r
770 states[7] = (titleStyle == null ? null : titleStyle.SaveViewState());
\r
771 states[8] = (todayDayStyle == null ? null : todayDayStyle.SaveViewState());
\r
772 states[9] = (weekendDayStyle == null ? null : weekendDayStyle.SaveViewState());
\r
773 for(int i=0; i < states.Length; i++)
\r
775 if(states[i]!=null)
\r
781 protected override void TrackViewState()
\r
783 base.TrackViewState();
\r
784 if(titleStyle!=null)
\r
786 titleStyle.TrackViewState();
\r
788 if(nextPrevStyle!=null)
\r
790 nextPrevStyle.TrackViewState();
\r
794 dayStyle.TrackViewState();
\r
796 if(dayHeaderStyle!=null)
\r
798 dayHeaderStyle.TrackViewState();
\r
800 if(todayDayStyle!=null)
\r
802 todayDayStyle.TrackViewState();
\r
804 if(weekendDayStyle!=null)
\r
806 weekendDayStyle.TrackViewState();
\r
808 if(otherMonthDayStyle!=null)
\r
810 otherMonthDayStyle.TrackViewState();
\r
812 if(selectedDayStyle!=null)
\r
814 selectedDayStyle.TrackViewState();
\r
816 if(selectorStyle!=null)
\r
818 selectorStyle.TrackViewState();
\r
822 private void RenderAllDays (HtmlTextWriter writer,
\r
824 DateTime activeDate,
\r
825 CalendarSelectionMode mode,
\r
829 TableItemStyle weeksStyle = null;
830 TableCell weeksCell = new TableCell ();
\r
831 TableItemStyle weekendStyle = WeekendDayStyle;
\r
832 TableItemStyle otherMonthStyle = OtherMonthDayStyle;
\r
834 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
835 mode == CalendarSelectionMode.DayWeekMonth);
\r
838 weeksStyle = new TableItemStyle ();
\r
839 weeksStyle.Width = Unit.Percentage (12);
\r
840 weeksStyle.HorizontalAlign = HorizontalAlign.Center;
\r
841 weeksStyle.CopyFrom (SelectorStyle);
\r
842 size = Unit.Percentage (12);
\r
844 size = Unit.Percentage (14);
\r
847 TableItemStyle [] styles = new TableItemStyle [32];
\r
848 int definedStyles = MASK_SELECTED;
\r
849 if (weekendStyle != null && !weekendStyle.IsEmpty)
\r
850 definedStyles |= MASK_WEEKEND;
\r
851 if (otherMonthStyle != null && !otherMonthStyle.IsEmpty)
\r
852 definedStyles |= MASK_OMONTH;
\r
853 if (todayDayStyle != null && !todayDayStyle.IsEmpty)
\r
854 definedStyles |= MASK_TODAY;
\r
855 if (dayStyle != null && !dayStyle.IsEmpty)
\r
856 definedStyles |= MASK_DAY;
\r
858 bool isCalendar = GetType () == typeof (Calendar);
\r
859 int month = globCal.GetMonth (activeDate);
\r
860 DateTime currentDay = firstDay;
\r
861 int begin = (int) (firstDay - begin_date).TotalDays;
862 for (int crr = 0; crr < 6; crr++) {
\r
863 writer.Write ("<tr>");
\r
865 int week_offset = begin + crr * 7;
866 string cellText = GetCalendarLinkText (
\r
867 "R" + week_offset + "07",
\r
869 weeksCell.ForeColor,
\r
872 weeksCell.Text = cellText;
\r
873 weeksCell.ApplyStyle (weeksStyle);
874 RenderCalendarCell (writer, weeksCell, cellText);
\r
877 for (int weekDay = 0; weekDay < 7; weekDay++) {
\r
878 string dayString = currentDay.Day.ToString ();
\r
879 DayOfWeek dow = currentDay.DayOfWeek;
880 CalendarDay calDay =
\r
883 dow == DayOfWeek.Sunday ||
884 dow == DayOfWeek.Saturday,
885 currentDay == TodaysDate,
\r
886 SelectedDates.Contains (currentDay),
\r
887 globCal.GetMonth (currentDay) != month,
\r
892 int dayStyles = GetMask (calDay) & definedStyles;
\r
893 TableItemStyle currentDayStyle = styles [dayStyles];
\r
894 if (currentDayStyle == null) {
\r
895 currentDayStyle = new TableItemStyle ();
\r
896 if ((dayStyles & MASK_DAY) != 0)
\r
897 currentDayStyle.CopyFrom (DayStyle);
\r
899 if ((dayStyles & MASK_WEEKEND) != 0)
\r
900 currentDayStyle.CopyFrom (WeekendDayStyle);
\r
902 if ((dayStyles & MASK_TODAY) != 0)
\r
903 currentDayStyle.CopyFrom (TodayDayStyle);
\r
905 if ((dayStyles & MASK_OMONTH) != 0)
\r
906 currentDayStyle.CopyFrom (OtherMonthDayStyle);
\r
908 if ((dayStyles & MASK_SELECTED) != 0) {
\r
909 currentDayStyle.ForeColor = Color.White;
\r
910 currentDayStyle.BackColor = Color.Silver;
\r
911 currentDayStyle.CopyFrom (SelectedDayStyle);
\r
914 currentDayStyle.Width = size;
\r
915 currentDayStyle.HorizontalAlign = HorizontalAlign.Center;
\r
918 TableCell dayCell = new TableCell ();
919 dayCell.ApplyStyle (currentDayStyle);
920 dayCell.Controls.Add (new LiteralControl (dayString));
921 calDay.IsSelectable = isActive;
922 if (calDay.IsSelectable)
923 dayCell.Text = GetCalendarLinkText (
924 (begin + (crr * 7 + weekDay)).ToString (),
929 OnDayRender (dayCell, calDay);
930 dayCell.RenderControl (writer);
932 currentDay = globCal.AddDays (currentDay, 1);
\r
934 writer.Write("</tr>");
\r
938 private int GetMask (CalendarDay day)
\r
940 int retVal = MASK_DAY;
\r
942 retVal |= MASK_SELECTED;
\r
944 retVal |= MASK_TODAY;
\r
945 if(day.IsOtherMonth)
\r
946 retVal |= MASK_OMONTH;
\r
948 retVal |= MASK_WEEKEND;
\r
953 /// Refers to the second line of the calendar, that contains a link
\r
954 /// to select whole month, and weekdays as defined by DayNameFormat
\r
956 private void RenderHeader (HtmlTextWriter writer,
\r
958 CalendarSelectionMode mode,
\r
962 writer.Write("<tr>");
\r
963 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
964 mode == CalendarSelectionMode.DayWeekMonth);
\r
966 TableCell headerCell = new TableCell ();
\r
967 headerCell.HorizontalAlign = HorizontalAlign.Center;
\r
968 string selMthText = String.Empty;
\r
970 if (mode == CalendarSelectionMode.DayWeekMonth) {
\r
971 DateTime visDate = GetEffectiveVisibleDate ();
972 DateTime sel_month = new DateTime (visDate.Year, visDate.Month, 1);
973 int month_offset = (int) (sel_month - begin_date).TotalDays;
974 headerCell.ApplyStyle (SelectorStyle);
\r
975 selMthText = GetCalendarLinkText ("R" + month_offset +
976 globCal.GetDaysInMonth (sel_month.Year,
977 sel_month.Month).ToString ("d2"), // maybe there are calendars with less then 10 days in a month
979 SelectorStyle.ForeColor,
\r
982 headerCell.ApplyStyle (DayHeaderStyle);
\r
983 selMthText = String.Empty;
\r
985 RenderCalendarCell (writer, headerCell, selMthText);
\r
988 TableCell dayHeaderCell = new TableCell ();
\r
989 dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
\r
990 dayHeaderCell.ApplyStyle (dayHeaderStyle);
992 int dayOfWeek = (int) globCal.GetDayOfWeek (firstDay);
\r
993 DateTimeFormatInfo currDTInfo = DateTimeFormatInfo.CurrentInfo;
\r
994 for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++) {
\r
995 DayOfWeek effDay = (DayOfWeek) Enum.ToObject (typeof (DayOfWeek), currDay % 7);
\r
996 string currDayContent;
\r
997 switch(DayNameFormat) {
\r
998 case DayNameFormat.Full:
\r
999 currDayContent = currDTInfo.GetDayName (effDay);
\r
1001 case DayNameFormat.FirstLetter:
\r
1002 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,1);
\r
1004 case DayNameFormat.FirstTwoLetters:
\r
1005 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,2);
\r
1007 case DayNameFormat.Short:
\r
1010 currDayContent = currDTInfo.GetAbbreviatedDayName (effDay);
\r
1014 RenderCalendarCell(writer, dayHeaderCell, currDayContent);
\r
1016 writer.Write ("</tr>");
\r
1019 private void RenderTitle (HtmlTextWriter writer,
\r
1020 DateTime visibleDate,
\r
1021 CalendarSelectionMode mode,
\r
1024 writer.Write("<tr>");
\r
1025 Table innerTable = new Table ();
\r
1026 TableCell titleCell = new TableCell();
\r
1027 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
1028 mode == CalendarSelectionMode.DayWeekMonth);
\r
1030 titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
\r
1031 titleCell.BackColor = Color.Silver;
\r
1033 innerTable.GridLines = GridLines.None;
\r
1034 innerTable.Width = Unit.Percentage (100);
\r
1035 innerTable.CellSpacing = 0;
\r
1036 ApplyTitleStyle (innerTable, titleCell, TitleStyle);
\r
1038 titleCell.RenderBeginTag (writer);
\r
1039 innerTable.RenderBeginTag (writer);
\r
1041 writer.Write ("<tr>");
\r
1042 string prevContent = String.Empty;
\r
1043 if (ShowNextPrevMonth) {
\r
1044 TableCell prevCell = new TableCell ();
\r
1045 prevCell.Width = Unit.Percentage (15);
\r
1046 prevCell.HorizontalAlign = HorizontalAlign.Left;
\r
1047 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1048 prevContent = PrevMonthText;
\r
1050 int pMthInt = globCal.GetMonth(globCal.AddMonths (visibleDate, -1));
\r
1051 if (NextPrevFormat == NextPrevFormat.FullMonth)
\r
1052 prevContent = infoCal.GetMonthName (pMthInt);
\r
1054 prevContent = infoCal.GetAbbreviatedMonthName (pMthInt);
\r
1056 DateTime prev_month = visibleDate.AddMonths (-1);
1057 int prev_offset = (int) (new DateTime (prev_month.Year,
1058 prev_month.Month, 1) - begin_date).TotalDays;
1059 prevCell.ApplyStyle (NextPrevStyle);
\r
1060 RenderCalendarCell (writer,
\r
1062 GetCalendarLinkText ("V" + prev_offset,
\r
1064 NextPrevStyle.ForeColor,
\r
1069 TableCell currCell = new TableCell ();
\r
1070 currCell.Width = Unit.Percentage (70);
\r
1071 if (TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
\r
1072 currCell.HorizontalAlign = HorizontalAlign.Center;
\r
1074 currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
\r
1076 currCell.Wrap = TitleStyle.Wrap;
\r
1077 string currMonthContent = String.Empty;
\r
1078 if (TitleFormat == TitleFormat.Month) {
\r
1079 currMonthContent = visibleDate.ToString ("MMMM");
\r
1081 string cmcFmt = infoCal.YearMonthPattern;
\r
1082 if (cmcFmt.IndexOf (',') >= 0)
\r
1083 cmcFmt = "MMMM yyyy";
\r
1085 currMonthContent = visibleDate.ToString (cmcFmt);
\r
1088 RenderCalendarCell (writer, currCell, currMonthContent);
\r
1090 string nextContent = String.Empty;
\r
1091 if (ShowNextPrevMonth) {
\r
1092 TableCell nextCell = new TableCell ();
\r
1093 nextCell.Width = Unit.Percentage(15);
\r
1094 nextCell.HorizontalAlign = HorizontalAlign.Right;
\r
1095 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1096 nextContent = NextMonthText;
\r
1098 int nMthInt = globCal.GetMonth (globCal.AddMonths (visibleDate, 1));
\r
1099 if(NextPrevFormat == NextPrevFormat.FullMonth)
\r
1100 nextContent = infoCal.GetMonthName(nMthInt);
\r
1102 nextContent = infoCal.GetAbbreviatedMonthName(nMthInt);
\r
1104 DateTime next_month = visibleDate.AddMonths (1);
1105 int next_offset = (int) (new DateTime (next_month.Year,
1106 next_month.Month, 1) - begin_date).TotalDays;
1107 nextCell.ApplyStyle(NextPrevStyle);
\r
1108 RenderCalendarCell (writer,
\r
1110 GetCalendarLinkText ("V" + next_offset,
\r
1112 NextPrevStyle.ForeColor,
\r
1117 writer.Write("</tr>");
\r
1118 innerTable.RenderEndTag(writer);
\r
1119 titleCell.RenderEndTag(writer);
\r
1121 writer.Write("</tr>");
\r
1124 private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
\r
1126 if(style.BackColor != Color.Empty)
\r
1128 cell.BackColor = style.BackColor;
\r
1130 if(style.BorderStyle != BorderStyle.NotSet)
\r
1132 cell.BorderStyle = style.BorderStyle;
\r
1134 if(style.BorderColor != Color.Empty)
\r
1136 cell.BorderColor = style.BorderColor;
\r
1138 if(style.BorderWidth != Unit.Empty)
\r
1140 cell.BorderWidth = style.BorderWidth;
\r
1142 if(style.Height != Unit.Empty)
\r
1144 cell.Height = style.Height;
\r
1146 if(style.VerticalAlign != VerticalAlign.NotSet)
\r
1148 cell.VerticalAlign = style.VerticalAlign;
\r
1151 if(style.ForeColor != Color.Empty)
\r
1153 table.ForeColor = style.ForeColor;
\r
1154 } else if(ForeColor != Color.Empty)
\r
1156 table.ForeColor = ForeColor;
\r
1159 if (style.CssClass != "") {
1160 cell.CssClass = style.CssClass;
1163 table.Font.CopyFrom(style.Font);
\r
1164 table.Font.MergeWith(Font);
\r
1167 private void RenderCalendarCell (HtmlTextWriter writer, TableCell cell, string text)
\r
1169 cell.RenderBeginTag(writer);
\r
1170 writer.Write(text);
\r
1171 cell.RenderEndTag(writer);
\r
1174 private DateTime GetFirstCalendarDay(DateTime visibleDate)
\r
1176 int fow = (int) FirstDayOfWeek;
\r
1178 fow = (int) infoCal.FirstDayOfWeek;
\r
1180 int days = (int) globCal.GetDayOfWeek (visibleDate) - fow;
\r
1183 return globCal.AddDays (visibleDate, -days);
\r
1186 private DateTime GetEffectiveVisibleDate()
\r
1188 DateTime dt = VisibleDate;
\r
1189 if (VisibleDate == DateTime.MinValue)
\r
1192 return globCal.AddDays (dt, 1 - globCal.GetDayOfMonth (dt));
\r
1196 /// Creates text to be displayed, with all attributes if to be
\r
1197 /// shown as a hyperlink
\r
1199 private string GetCalendarLinkText (string eventArg,
\r
1205 StringBuilder dispVal = new StringBuilder ();
\r
1206 dispVal.Append ("<a href=\"");
\r
1207 dispVal.Append (Page.GetPostBackClientHyperlink (this, eventArg));
\r
1208 dispVal.Append ("\" style=\"color: ");
\r
1209 if (foreground.IsEmpty) {
\r
1210 dispVal.Append (ColorTranslator.ToHtml (defaultTextColor));
\r
1212 dispVal.Append (ColorTranslator.ToHtml (foreground));
\r
1214 dispVal.Append ("\">");
\r
1215 dispVal.Append (text);
\r
1216 dispVal.Append ("</a>");
\r
1217 return dispVal.ToString ();
\r
1222 private string GetHtmlForCell (TableCell cell, bool showLinks)
\r
1224 StringWriter sw = new StringWriter ();
\r
1225 HtmlTextWriter htw = new HtmlTextWriter (sw);
\r
1226 cell.RenderBeginTag (htw);
\r
1228 htw.Write (GetCalendarLinkText ("{0}", "{1}", cell.ForeColor, showLinks));
\r
1230 htw.Write ("{0}");
\r
1232 cell.RenderEndTag (htw);
\r
1233 return sw.ToString();
\r
1236 internal void SelectRangeInternal (DateTime fromDate, DateTime toDate, DateTime visibleDate)
\r
1238 TimeSpan span = toDate - fromDate;
\r
1239 if (SelectedDates.Count != span.Days + 1 ||
\r
1240 SelectedDates [0] != fromDate ||
\r
1241 SelectedDates [SelectedDates.Count - 1] != toDate) {
\r
1242 SelectedDates.SelectRange (fromDate, toDate);
\r
1243 OnSelectionChanged ();
\r
1248 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)