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
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Collections;
\r
38 using System.Globalization;
\r
41 using System.Web.UI;
\r
42 using System.Drawing;
\r
43 using System.ComponentModel;
44 using System.ComponentModel.Design;
\r
46 namespace System.Web.UI.WebControls
\r
49 // [ControlValuePropertyAttribute ("SelectedDate", DateTime.MinValue)]
\r
51 [DefaultEvent("SelectionChanged")]
\r
52 [DefaultProperty("SelectedDate")]
\r
53 [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
\r
54 [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
55 public class Calendar : WebControl, IPostBackEventHandler
\r
58 private TableItemStyle dayHeaderStyle;
\r
59 private TableItemStyle dayStyle;
\r
60 private TableItemStyle nextPrevStyle;
\r
61 private TableItemStyle otherMonthDayStyle;
\r
62 private SelectedDatesCollection selectedDates;
\r
63 private ArrayList selectedDatesList;
\r
64 private TableItemStyle selectedDayStyle;
\r
65 private TableItemStyle selectorStyle;
\r
66 private TableItemStyle titleStyle;
\r
67 private TableItemStyle todayDayStyle;
\r
68 private TableItemStyle weekendDayStyle;
\r
70 private static readonly object DayRenderEvent = new object();
\r
71 private static readonly object SelectionChangedEvent = new object();
\r
72 private static readonly object VisibleMonthChangedEvent = new object();
\r
74 private Color defaultTextColor;
\r
75 private System.Globalization.Calendar globCal;
\r
76 private DateTimeFormatInfo infoCal = DateTimeFormatInfo.CurrentInfo;
78 private static readonly DateTime begin_date = new DateTime (2000,01,01,0,0,0,0);
81 private static int MASK_WEEKEND = (0x01 << 0);
\r
82 private static int MASK_OMONTH = (0x01 << 1);
\r
83 private static int MASK_TODAY = (0x01 << 2);
\r
84 private static int MASK_SELECTED = (0x01 << 3);
\r
85 private static int MASK_DAY = (0x01 << 4);
\r
86 private static int MASK_UNIQUE = MASK_WEEKEND | MASK_OMONTH | MASK_TODAY | MASK_SELECTED;
\r
88 public Calendar(): base()
\r
96 [DefaultValue (2), WebCategory ("Layout")]
97 [WebSysDescription ("The border left within the calendar days.")]
\r
98 public int CellPadding
\r
102 object o = ViewState["CellPadding"];
\r
110 throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
\r
111 ViewState["CellPadding"] = value;
\r
118 [DefaultValue (0), WebCategory ("Layout")]
119 [WebSysDescription ("The border left between calendar days.")]
\r
120 public int CellSpacing
\r
124 object o = ViewState["CellSpacing"];
\r
132 throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
\r
133 ViewState["CellSpacing"] = value;
\r
137 [NotifyParentProperty (true), WebCategory ("Style")]
138 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
139 [PersistenceMode (PersistenceMode.InnerProperty)]
140 [WebSysDescription ("The style applied to the day header.")]
\r
141 public TableItemStyle DayHeaderStyle
\r
145 if(dayHeaderStyle==null)
\r
146 dayHeaderStyle = new TableItemStyle();
\r
147 if(IsTrackingViewState)
\r
148 dayHeaderStyle.TrackViewState();
\r
149 return dayHeaderStyle;
\r
156 [DefaultValue (typeof (DayNameFormat), "Short"), WebCategory ("Appearance")]
157 [WebSysDescription ("The format for the day name display.")]
\r
158 public DayNameFormat DayNameFormat
\r
162 object o = ViewState["DayNameFormat"];
\r
164 return (DayNameFormat)o;
\r
165 return DayNameFormat.Short;
\r
169 if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
\r
170 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
171 ViewState["DayNameFormat"] = value;
\r
175 [DefaultValue (null)]
176 [NotifyParentProperty (true), WebCategory ("Style")]
177 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
178 [PersistenceMode (PersistenceMode.InnerProperty)]
179 [WebSysDescription ("The style applied to the day entry.")]
\r
180 public TableItemStyle DayStyle
\r
185 dayStyle = new TableItemStyle();
\r
186 if(IsTrackingViewState)
\r
187 dayStyle.TrackViewState();
\r
195 [DefaultValue (typeof (FirstDayOfWeek), "Default"), WebCategory ("Appearance")]
196 [WebSysDescription ("The day that a week begins with.")]
\r
197 public FirstDayOfWeek FirstDayOfWeek
\r
201 object o = ViewState["FirstDayOfWeek"];
\r
203 return (FirstDayOfWeek)o;
\r
204 return FirstDayOfWeek.Default;
\r
208 if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
\r
209 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
210 ViewState["FirstDayOfWeek"] = value;
\r
217 [Localizable (true)]
\r
219 [DefaultValue (">"), WebCategory ("Appearance")]
220 [WebSysDescription ("The text for selecting the next month.")]
\r
221 public string NextMonthText
\r
225 object o = ViewState["NextMonthText"];
\r
232 ViewState["NextMonthText"] = value;
\r
239 [DefaultValue (typeof (NextPrevFormat), "CustomText"), WebCategory ("Appearance")]
240 [WebSysDescription ("The format for the month navigation.")]
\r
241 public NextPrevFormat NextPrevFormat
\r
245 object o = ViewState["NextPrevFormat"];
\r
247 return (NextPrevFormat)o;
\r
248 return NextPrevFormat.CustomText;
\r
252 if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
\r
253 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
254 ViewState["NextPrevFormat"] = value;
\r
258 [NotifyParentProperty (true), WebCategory ("Style")]
259 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
260 [PersistenceMode (PersistenceMode.InnerProperty)]
261 [WebSysDescription ("The style applied to the month navigation.")]
\r
262 public TableItemStyle NextPrevStyle
\r
266 if(nextPrevStyle == null)
\r
267 nextPrevStyle = new TableItemStyle();
\r
268 if(IsTrackingViewState)
\r
269 nextPrevStyle.TrackViewState();
\r
270 return nextPrevStyle;
\r
274 [DefaultValue (null)]
275 [NotifyParentProperty (true), WebCategory ("Style")]
276 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
277 [PersistenceMode (PersistenceMode.InnerProperty)]
278 [WebSysDescription ("The style applied to day entries belonging to another month.")]
\r
279 public TableItemStyle OtherMonthDayStyle
\r
283 if(otherMonthDayStyle == null)
\r
284 otherMonthDayStyle = new TableItemStyle();
\r
285 if(IsTrackingViewState)
\r
286 otherMonthDayStyle.TrackViewState();
\r
287 return otherMonthDayStyle;
\r
294 [Localizable (true)]
\r
296 [DefaultValue ("<"), WebCategory ("Appearance")]
297 [WebSysDescription ("The text for selecting the previous month.")]
\r
298 public string PrevMonthText
\r
302 object o = ViewState["PrevMonthText"];
\r
309 ViewState["PrevMonthText"] = value;
\r
313 [DefaultValue (null), Bindable (true)]
314 [WebSysDescription ("The currently selected date.")]
\r
315 public DateTime SelectedDate
\r
319 if(SelectedDates.Count > 0)
\r
321 return SelectedDates[0];
\r
323 return DateTime.MinValue;
\r
327 if(value == DateTime.MinValue)
\r
329 SelectedDates.Clear();
\r
332 SelectedDates.SelectRange(value, value);
\r
337 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
338 [WebSysDescription ("All currently selected dates.")]
\r
339 public SelectedDatesCollection SelectedDates
\r
343 if(selectedDates==null)
\r
345 if(selectedDatesList == null)
\r
346 selectedDatesList = new ArrayList();
\r
347 selectedDates = new SelectedDatesCollection(selectedDatesList);
\r
349 return selectedDates;
\r
353 [DefaultValue (null)]
354 [NotifyParentProperty (true), WebCategory ("Style")]
355 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
356 [PersistenceMode (PersistenceMode.InnerProperty)]
357 [WebSysDescription ("The style applied to the selected day.")]
\r
358 public TableItemStyle SelectedDayStyle
\r
362 if(selectedDayStyle==null)
\r
363 selectedDayStyle = new TableItemStyle();
\r
364 if(IsTrackingViewState)
\r
365 selectedDayStyle.TrackViewState();
\r
366 return selectedDayStyle;
\r
373 [DefaultValue (typeof (CalendarSelectionMode), "Day"), WebCategory ("Behavior")]
374 [WebSysDescription ("The mode in which days or other entries are selected.")]
\r
375 public CalendarSelectionMode SelectionMode
\r
379 object o = ViewState["SelectionMode"];
\r
381 return (CalendarSelectionMode)o;
\r
382 return CalendarSelectionMode.Day;
\r
386 if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
\r
387 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
388 ViewState["SelectionMode"] = value;
\r
395 [Localizable (true)]
\r
397 [DefaultValue (">>"), WebCategory ("Appearance")]
398 [WebSysDescription ("The text that is used for selection of months.")]
\r
399 public string SelectMonthText
\r
403 object o = ViewState["SelectMonthText"];
\r
410 ViewState["SelectMonthText"] = value;
\r
414 [NotifyParentProperty (true), WebCategory ("Style")]
415 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
416 [PersistenceMode (PersistenceMode.InnerProperty)]
417 [WebSysDescription ("The style applied to the selector.")]
\r
418 public TableItemStyle SelectorStyle
\r
422 if(selectorStyle==null)
\r
423 selectorStyle = new TableItemStyle();
\r
424 return selectorStyle;
\r
431 [Localizable (true)]
\r
433 [DefaultValue (">"), WebCategory ("Appearance")]
434 [WebSysDescription ("The text that is used for selection of weeks.")]
\r
435 public string SelectWeekText
\r
439 object o = ViewState["SelectWeekText"];
\r
446 ViewState["SelectWeekText"] = value;
\r
453 [DefaultValue (true), WebCategory ("Appearance")]
454 [WebSysDescription ("Determines if the header for days is shown.")]
\r
455 public bool ShowDayHeader
\r
459 object o = ViewState["ShowDayHeader"];
\r
466 ViewState["ShowDayHeader"] = value;
\r
473 [DefaultValue (false), WebCategory ("Appearance")]
474 [WebSysDescription ("Determines if gridlines are shown.")]
\r
475 public bool ShowGridLines
\r
479 object o = ViewState["ShowGridLines"];
\r
486 ViewState["ShowGridLines"] = value;
\r
493 [DefaultValue (true), WebCategory ("Appearance")]
494 [WebSysDescription ("Determines if month navigation is shown.")]
\r
495 public bool ShowNextPrevMonth
\r
499 object o = ViewState["ShowNextPrevMonth"];
\r
506 ViewState["ShowNextPrevMonth"] = value;
\r
513 [DefaultValue (true), WebCategory ("Appearance")]
514 [WebSysDescription ("Determines if the title is shown.")]
\r
515 public bool ShowTitle
\r
519 object o = ViewState["ShowTitle"];
\r
526 ViewState["ShowTitle"] = value;
\r
533 [DefaultValue (typeof (TitleFormat), "MonthYear"), WebCategory ("Appearance")]
534 [WebSysDescription ("The format in which the title is rendered.")]
\r
535 public TitleFormat TitleFormat
\r
539 object o = ViewState["TitleFormat"];
\r
541 return (TitleFormat)o;
\r
542 return TitleFormat.MonthYear;
\r
546 if(!System.Enum.IsDefined(typeof(TitleFormat), value))
\r
547 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
548 ViewState["TitleFormat"] = value;
\r
552 [NotifyParentProperty (true), WebCategory ("Style")]
553 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
554 [PersistenceMode (PersistenceMode.InnerProperty)]
555 [WebSysDescription ("The style applied to the title.")]
\r
556 public TableItemStyle TitleStyle
\r
560 if(titleStyle==null)
\r
561 titleStyle = new TableItemStyle();
\r
562 if(IsTrackingViewState)
\r
563 titleStyle.TrackViewState();
\r
568 [DefaultValue (null)]
569 [NotifyParentProperty (true), WebCategory ("Style")]
570 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
571 [PersistenceMode (PersistenceMode.InnerProperty)]
572 [WebSysDescription ("The style applied to the today's date display.")]
\r
573 public TableItemStyle TodayDayStyle
\r
577 if(todayDayStyle==null)
\r
578 todayDayStyle = new TableItemStyle();
\r
579 if(IsTrackingViewState)
\r
580 todayDayStyle.TrackViewState();
\r
581 return todayDayStyle;
\r
589 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
590 [WebSysDescription ("The current date.")]
\r
591 public DateTime TodaysDate
\r
595 object o = ViewState["TodaysDate"];
\r
597 return (DateTime)o;
\r
598 return DateTime.Today;
\r
602 ViewState["TodaysDate"] = value.Date;
\r
606 [DefaultValue (null), Bindable (true)]
607 [WebSysDescription ("The month that is displayed.")]
\r
608 public DateTime VisibleDate
\r
612 object o = ViewState["VisibleDate"];
\r
614 return (DateTime)o;
\r
615 return DateTime.MinValue;
\r
619 ViewState["VisibleDate"] = value.Date;
\r
623 [NotifyParentProperty (true), WebCategory ("Style")]
624 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
625 [PersistenceMode (PersistenceMode.InnerProperty)]
626 [WebSysDescription ("The style applied to weekend days.")]
\r
627 public TableItemStyle WeekendDayStyle
\r
631 if(weekendDayStyle == null)
\r
632 weekendDayStyle = new TableItemStyle();
\r
633 if(IsTrackingViewState)
\r
635 weekendDayStyle.TrackViewState();
\r
637 return weekendDayStyle;
\r
641 [WebCategory ("Action")]
642 [WebSysDescription ("Raised when a day entry is rendered.")]
\r
643 public event DayRenderEventHandler DayRender
\r
647 Events.AddHandler(DayRenderEvent, value);
\r
651 Events.RemoveHandler(DayRenderEvent, value);
\r
655 [WebCategory ("Action")]
656 [WebSysDescription ("Raised when another entry is selected.")]
\r
657 public event EventHandler SelectionChanged
\r
661 Events.AddHandler(SelectionChangedEvent, value);
\r
665 Events.RemoveHandler(SelectionChangedEvent, value);
\r
669 [WebCategory ("Action")]
670 [WebSysDescription ("Raised when a the currently visible month has changed.")]
\r
671 public event MonthChangedEventHandler VisibleMonthChanged
\r
675 Events.AddHandler(VisibleMonthChangedEvent, value);
\r
679 Events.RemoveHandler(VisibleMonthChangedEvent, value);
\r
684 [MonoTODO ("Do something with this")]
\r
685 [DefaultValueAttribute ("")]
\r
686 [WebCategoryAttribute ("Appearance")]
\r
687 public string CalendarEntryText {
\r
689 object o = ViewState ["CalendarEntryText"];
\r
690 if (o != null) return (string) o;
\r
694 ViewState ["CalendarEntryText"] = value;
\r
698 [LocalizableAttribute (true)]
\r
699 [DefaultValueAttribute ("")]
\r
700 [WebCategoryAttribute ("Accessibility")]
\r
701 public string Caption {
\r
703 object o = ViewState ["Caption"];
\r
704 if (o != null) return (string) o;
\r
708 ViewState ["Caption"] = value;
\r
712 [WebCategoryAttribute ("Accessibility")]
\r
713 [DefaultValueAttribute (TableCaptionAlign.NotSet)]
\r
714 public TableCaptionAlign CaptionAlign {
\r
716 object o = ViewState ["CaptionAlign"];
\r
717 if (o != null) return (TableCaptionAlign) o;
\r
718 return TableCaptionAlign.NotSet;
\r
721 ViewState ["CaptionAlign"] = value;
\r
725 [DefaultValueAttribute (false)]
\r
726 [WebCategoryAttribute ("Accessibility")]
\r
727 public bool UseAccessibleHeader {
\r
729 object o = ViewState ["UseAccessibleHeader"];
\r
730 if (o != null) return (bool) o;
\r
734 ViewState ["UseAccessibleHeader"] = value;
\r
739 protected virtual void OnDayRender(TableCell cell, CalendarDay day)
\r
743 DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
\r
745 dreh(this, new DayRenderEventArgs(cell, day));
\r
749 protected virtual void OnSelectionChanged()
\r
753 EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
\r
755 eh(this, new EventArgs());
\r
759 protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
\r
763 MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
\r
765 mceh(this, new MonthChangedEventArgs(newDate, prevDate));
\r
770 /// See test6.aspx in Tests directory for verification
\r
773 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
775 RaisePostBackEvent (eventArgument);
\r
778 protected virtual void RaisePostBackEvent (string eventArgument)
\r
780 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
783 // initialize the calendar...TODO: find out why this isn't done in the constructor
784 // if the culture is changed between rendering and postback this will be broken
785 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
787 // TODO: Find out what kind of exceptions to throw when we get bad data
788 if (eventArgument.StartsWith ("V")) {
789 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument.Substring (1)), 0, 0, 0);
790 DateTime new_date = begin_date + mod;
791 VisibleDate = new_date;
792 OnVisibleMonthChanged (VisibleDate, new_date);
793 } else if (eventArgument.StartsWith ("R")) {
794 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument.Substring (1,
795 eventArgument.Length - 3)), 0, 0, 0);
796 DateTime range_begin = begin_date + mod;
797 int sel_days = Int32.Parse (eventArgument.Substring (eventArgument.Length - 2));
798 SelectRangeInternal (range_begin, range_begin.AddDays (sel_days - 1), GetEffectiveVisibleDate ());
800 TimeSpan mod = new TimeSpan (Int32.Parse (eventArgument), 0, 0, 0);
801 DateTime day = begin_date + mod;
802 SelectRangeInternal (day, day, GetEffectiveVisibleDate ());
806 protected override void OnPreRender (EventArgs e)
808 base.OnPreRender (e);
811 protected override void Render(HtmlTextWriter writer)
\r
813 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
814 DateTime visDate = GetEffectiveVisibleDate();
\r
815 DateTime firstDate = GetFirstCalendarDay(visDate);
\r
818 bool isHtmlTextWriter;
\r
819 //FIXME: when Control.Site works, reactivate this
\r
820 //if (Page == null || Site == null) {
\r
821 // isEnabled = false;
\r
822 // isHtmlTextWriter = false;
\r
824 isEnabled = Enabled;
\r
825 isHtmlTextWriter = (writer.GetType() != typeof(HtmlTextWriter));
\r
827 defaultTextColor = ForeColor;
\r
828 if(defaultTextColor == Color.Empty)
\r
829 defaultTextColor = Color.Black;
\r
831 Table calTable = new Table ();
\r
833 calTable.CopyBaseAttributes(this);
\r
834 if(ControlStyleCreated)
\r
835 calTable.ApplyStyle(ControlStyle);
\r
836 calTable.Width = Width;
\r
837 calTable.Height = Height;
\r
838 calTable.CellSpacing = CellSpacing;
\r
839 calTable.CellPadding = CellPadding;
\r
841 if (ControlStyleCreated &&
\r
842 ControlStyle.IsSet (WebControls.Style.BORDERWIDTH) &&
\r
843 BorderWidth != Unit.Empty)
\r
844 calTable.BorderWidth = BorderWidth;
\r
846 calTable.BorderWidth = Unit.Pixel(1);
\r
849 calTable.GridLines = GridLines.Both;
\r
851 calTable.GridLines = GridLines.None;
\r
854 calTable.Caption = Caption;
\r
855 calTable.CaptionAlign = CaptionAlign;
\r
858 calTable.RenderBeginTag (writer);
\r
861 RenderTitle (writer, visDate, SelectionMode, isEnabled);
\r
864 RenderHeader (writer, firstDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
866 RenderAllDays (writer, firstDate, visDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
868 calTable.RenderEndTag(writer);
\r
871 protected override ControlCollection CreateControlCollection()
\r
873 return new EmptyControlCollection(this);
\r
876 protected override void LoadViewState(object savedState)
\r
878 if (savedState!=null) {
\r
879 object[] states = (object[]) savedState;
\r
880 if(states[0] != null)
\r
881 base.LoadViewState(states[0]);
\r
882 if(states[1] != null)
\r
883 DayHeaderStyle.LoadViewState(states[1]);
\r
884 if(states[2] != null)
\r
885 DayStyle.LoadViewState(states[2]);
\r
886 if(states[3] != null)
\r
887 NextPrevStyle.LoadViewState(states[3]);
\r
888 if(states[4] != null)
\r
889 OtherMonthDayStyle.LoadViewState(states[4]);
\r
890 if(states[5] != null)
\r
891 SelectedDayStyle.LoadViewState(states[5]);
\r
892 if(states[6] != null)
\r
893 SelectorStyle.LoadViewState(states[6]);
\r
894 if(states[7] != null)
\r
895 TitleStyle.LoadViewState(states[7]);
\r
896 if(states[8] != null)
\r
897 TodayDayStyle.LoadViewState(states[8]);
\r
898 if(states[9] != null)
\r
899 WeekendDayStyle.LoadViewState(states[9]);
\r
901 ArrayList dateList = ViewState ["_CalendarSelectedDates"] as ArrayList;
\r
902 if (dateList != null)
\r
903 selectedDates = new SelectedDatesCollection (dateList);
\r
907 protected override object SaveViewState()
\r
909 if (SelectedDates.Count > 0)
\r
910 ViewState["_CalendarSelectedDates"] = selectedDates.GetDateList ();
912 object[] states = new object [10];
\r
913 states[0] = base.SaveViewState();
\r
914 states[1] = (dayHeaderStyle == null ? null : dayHeaderStyle.SaveViewState());
\r
915 states[2] = (dayStyle == null ? null : dayStyle.SaveViewState());
\r
916 states[3] = (nextPrevStyle == null ? null : nextPrevStyle.SaveViewState());
\r
917 states[4] = (otherMonthDayStyle == null ? null : otherMonthDayStyle.SaveViewState());
\r
918 states[5] = (selectedDayStyle == null ? null : selectedDayStyle.SaveViewState());
\r
919 states[6] = (selectorStyle == null ? null : selectorStyle.SaveViewState());
\r
920 states[7] = (titleStyle == null ? null : titleStyle.SaveViewState());
\r
921 states[8] = (todayDayStyle == null ? null : todayDayStyle.SaveViewState());
\r
922 states[9] = (weekendDayStyle == null ? null : weekendDayStyle.SaveViewState());
\r
923 for(int i=0; i < states.Length; i++)
\r
925 if(states[i]!=null)
\r
931 protected override void TrackViewState()
\r
933 base.TrackViewState();
\r
934 if(titleStyle!=null)
\r
936 titleStyle.TrackViewState();
\r
938 if(nextPrevStyle!=null)
\r
940 nextPrevStyle.TrackViewState();
\r
944 dayStyle.TrackViewState();
\r
946 if(dayHeaderStyle!=null)
\r
948 dayHeaderStyle.TrackViewState();
\r
950 if(todayDayStyle!=null)
\r
952 todayDayStyle.TrackViewState();
\r
954 if(weekendDayStyle!=null)
\r
956 weekendDayStyle.TrackViewState();
\r
958 if(otherMonthDayStyle!=null)
\r
960 otherMonthDayStyle.TrackViewState();
\r
962 if(selectedDayStyle!=null)
\r
964 selectedDayStyle.TrackViewState();
\r
966 if(selectorStyle!=null)
\r
968 selectorStyle.TrackViewState();
\r
972 private void RenderAllDays (HtmlTextWriter writer,
\r
974 DateTime activeDate,
\r
975 CalendarSelectionMode mode,
\r
979 TableItemStyle weeksStyle = null;
980 TableCell weeksCell = new TableCell ();
\r
981 TableItemStyle weekendStyle = WeekendDayStyle;
\r
982 TableItemStyle otherMonthStyle = OtherMonthDayStyle;
\r
984 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
985 mode == CalendarSelectionMode.DayWeekMonth);
\r
988 weeksStyle = new TableItemStyle ();
\r
989 weeksStyle.Width = Unit.Percentage (12);
\r
990 weeksStyle.HorizontalAlign = HorizontalAlign.Center;
\r
991 weeksStyle.CopyFrom (SelectorStyle);
\r
992 size = Unit.Percentage (12);
\r
994 size = Unit.Percentage (14);
\r
997 TableItemStyle [] styles = new TableItemStyle [32];
\r
998 int definedStyles = MASK_SELECTED;
\r
999 if (weekendStyle != null && !weekendStyle.IsEmpty)
\r
1000 definedStyles |= MASK_WEEKEND;
\r
1001 if (otherMonthStyle != null && !otherMonthStyle.IsEmpty)
\r
1002 definedStyles |= MASK_OMONTH;
\r
1003 if (todayDayStyle != null && !todayDayStyle.IsEmpty)
\r
1004 definedStyles |= MASK_TODAY;
\r
1005 if (dayStyle != null && !dayStyle.IsEmpty)
\r
1006 definedStyles |= MASK_DAY;
\r
1008 int month = globCal.GetMonth (activeDate);
\r
1009 DateTime currentDay = firstDay;
\r
1010 int begin = (int) (firstDay - begin_date).TotalDays;
1011 for (int crr = 0; crr < 6; crr++) {
\r
1012 writer.Write ("<tr>");
\r
1014 int week_offset = begin + crr * 7;
1015 string cellText = GetCalendarLinkText (
\r
1016 "R" + week_offset + "07",
\r
1018 "Select week " + (crr + 1),
\r
1019 weeksCell.ForeColor,
\r
1022 weeksCell.Text = cellText;
\r
1023 weeksCell.ApplyStyle (weeksStyle);
1024 RenderCalendarCell (writer, weeksCell, cellText);
\r
1027 for (int weekDay = 0; weekDay < 7; weekDay++) {
\r
1028 string dayString = currentDay.Day.ToString ();
\r
1029 DayOfWeek dow = currentDay.DayOfWeek;
1030 CalendarDay calDay =
\r
1033 dow == DayOfWeek.Sunday ||
1034 dow == DayOfWeek.Saturday,
1035 currentDay == TodaysDate,
\r
1036 SelectedDates.Contains (currentDay),
\r
1037 globCal.GetMonth (currentDay) != month,
\r
1042 int dayStyles = GetMask (calDay) & definedStyles;
\r
1043 TableItemStyle currentDayStyle = styles [dayStyles];
\r
1044 if (currentDayStyle == null) {
\r
1045 currentDayStyle = new TableItemStyle ();
\r
1046 if ((dayStyles & MASK_DAY) != 0)
\r
1047 currentDayStyle.CopyFrom (DayStyle);
\r
1049 if ((dayStyles & MASK_WEEKEND) != 0)
\r
1050 currentDayStyle.CopyFrom (WeekendDayStyle);
\r
1052 if ((dayStyles & MASK_TODAY) != 0)
\r
1053 currentDayStyle.CopyFrom (TodayDayStyle);
\r
1055 if ((dayStyles & MASK_OMONTH) != 0)
\r
1056 currentDayStyle.CopyFrom (OtherMonthDayStyle);
\r
1058 if ((dayStyles & MASK_SELECTED) != 0) {
\r
1059 currentDayStyle.ForeColor = Color.White;
\r
1060 currentDayStyle.BackColor = Color.Silver;
\r
1061 currentDayStyle.CopyFrom (SelectedDayStyle);
\r
1064 currentDayStyle.Width = size;
\r
1065 currentDayStyle.HorizontalAlign = HorizontalAlign.Center;
\r
1068 TableCell dayCell = new TableCell ();
1069 dayCell.ApplyStyle (currentDayStyle);
1070 dayCell.Controls.Add (new LiteralControl (dayString));
1071 calDay.IsSelectable = isActive;
1072 if (calDay.IsSelectable)
1073 dayCell.Text = GetCalendarLinkText (
1074 (begin + (crr * 7 + weekDay)).ToString (),
1076 currentDay.ToShortDateString (),
1080 OnDayRender (dayCell, calDay);
1081 dayCell.RenderControl (writer);
1083 currentDay = globCal.AddDays (currentDay, 1);
\r
1085 writer.Write("</tr>");
\r
1089 private int GetMask (CalendarDay day)
\r
1091 int retVal = MASK_DAY;
\r
1092 if(day.IsSelected)
\r
1093 retVal |= MASK_SELECTED;
\r
1095 retVal |= MASK_TODAY;
\r
1096 if(day.IsOtherMonth)
\r
1097 retVal |= MASK_OMONTH;
\r
1099 retVal |= MASK_WEEKEND;
\r
1104 /// Refers to the second line of the calendar, that contains a link
\r
1105 /// to select whole month, and weekdays as defined by DayNameFormat
\r
1107 private void RenderHeader (HtmlTextWriter writer,
\r
1108 DateTime firstDay,
\r
1109 CalendarSelectionMode mode,
\r
1113 writer.Write("<tr>");
\r
1114 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
1115 mode == CalendarSelectionMode.DayWeekMonth);
\r
1117 TableCell headerCell = new TableCell ();
\r
1118 headerCell.HorizontalAlign = HorizontalAlign.Center;
\r
1119 string selMthText = String.Empty;
\r
1121 if (mode == CalendarSelectionMode.DayWeekMonth) {
\r
1122 DateTime visDate = GetEffectiveVisibleDate ();
1123 DateTime sel_month = new DateTime (visDate.Year, visDate.Month, 1);
1124 int month_offset = (int) (sel_month - begin_date).TotalDays;
1125 headerCell.ApplyStyle (SelectorStyle);
\r
1126 selMthText = GetCalendarLinkText ("R" + month_offset +
1127 globCal.GetDaysInMonth (sel_month.Year,
1128 sel_month.Month).ToString ("d2"), // maybe there are calendars with less then 10 days in a month
1130 "Select the whole month",
\r
1131 SelectorStyle.ForeColor,
\r
1134 headerCell.ApplyStyle (DayHeaderStyle);
\r
1135 selMthText = String.Empty;
\r
1137 RenderCalendarCell (writer, headerCell, selMthText);
\r
1140 TableCell dayHeaderCell = new TableCell ();
\r
1141 dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
\r
1142 dayHeaderCell.ApplyStyle (dayHeaderStyle);
1144 int dayOfWeek = (int) globCal.GetDayOfWeek (firstDay);
\r
1145 DateTimeFormatInfo currDTInfo = DateTimeFormatInfo.CurrentInfo;
\r
1146 for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++) {
\r
1147 DayOfWeek effDay = (DayOfWeek) Enum.ToObject (typeof (DayOfWeek), currDay % 7);
\r
1148 string currDayContent;
\r
1149 switch(DayNameFormat) {
\r
1150 case DayNameFormat.Full:
\r
1151 currDayContent = currDTInfo.GetDayName (effDay);
\r
1153 case DayNameFormat.FirstLetter:
\r
1154 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,1);
\r
1156 case DayNameFormat.FirstTwoLetters:
\r
1157 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,2);
\r
1159 case DayNameFormat.Short:
\r
1162 currDayContent = currDTInfo.GetAbbreviatedDayName (effDay);
\r
1166 RenderCalendarHeaderCell (writer, dayHeaderCell, currDayContent, currDTInfo.GetDayName (effDay));
\r
1168 writer.Write ("</tr>");
\r
1171 private void RenderTitle (HtmlTextWriter writer,
\r
1172 DateTime visibleDate,
\r
1173 CalendarSelectionMode mode,
\r
1176 writer.Write("<tr>");
\r
1177 Table innerTable = new Table ();
\r
1178 TableCell titleCell = new TableCell();
\r
1179 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
1180 mode == CalendarSelectionMode.DayWeekMonth);
\r
1182 titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
\r
1183 titleCell.BackColor = Color.Silver;
\r
1185 innerTable.GridLines = GridLines.None;
\r
1186 innerTable.Width = Unit.Percentage (100);
\r
1187 innerTable.CellSpacing = 0;
\r
1188 ApplyTitleStyle (innerTable, titleCell, TitleStyle);
\r
1190 titleCell.RenderBeginTag (writer);
\r
1191 innerTable.RenderBeginTag (writer);
\r
1193 writer.Write ("<tr>");
\r
1194 string prevContent = String.Empty;
\r
1195 if (ShowNextPrevMonth) {
\r
1196 TableCell prevCell = new TableCell ();
\r
1197 prevCell.Width = Unit.Percentage (15);
\r
1198 prevCell.HorizontalAlign = HorizontalAlign.Left;
\r
1199 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1200 prevContent = PrevMonthText;
\r
1202 int pMthInt = globCal.GetMonth(globCal.AddMonths (visibleDate, -1));
\r
1203 if (NextPrevFormat == NextPrevFormat.FullMonth)
\r
1204 prevContent = infoCal.GetMonthName (pMthInt);
\r
1206 prevContent = infoCal.GetAbbreviatedMonthName (pMthInt);
\r
1208 DateTime prev_month = visibleDate.AddMonths (-1);
1209 int prev_offset = (int) (new DateTime (prev_month.Year,
1210 prev_month.Month, 1) - begin_date).TotalDays;
1211 prevCell.ApplyStyle (NextPrevStyle);
\r
1212 RenderCalendarCell (writer,
\r
1214 GetCalendarLinkText ("V" + prev_offset,
\r
1216 "Go to previous month",
\r
1217 NextPrevStyle.ForeColor,
\r
1222 TableCell currCell = new TableCell ();
\r
1223 currCell.Width = Unit.Percentage (70);
\r
1224 if (TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
\r
1225 currCell.HorizontalAlign = HorizontalAlign.Center;
\r
1227 currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
\r
1229 currCell.Wrap = TitleStyle.Wrap;
\r
1230 string currMonthContent = String.Empty;
\r
1231 if (TitleFormat == TitleFormat.Month) {
\r
1232 currMonthContent = visibleDate.ToString ("MMMM");
\r
1234 string cmcFmt = infoCal.YearMonthPattern;
\r
1235 if (cmcFmt.IndexOf (',') >= 0)
\r
1236 cmcFmt = "MMMM yyyy";
\r
1238 currMonthContent = visibleDate.ToString (cmcFmt);
\r
1241 RenderCalendarCell (writer, currCell, currMonthContent);
\r
1243 string nextContent = String.Empty;
\r
1244 if (ShowNextPrevMonth) {
\r
1245 TableCell nextCell = new TableCell ();
\r
1246 nextCell.Width = Unit.Percentage(15);
\r
1247 nextCell.HorizontalAlign = HorizontalAlign.Right;
\r
1248 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1249 nextContent = NextMonthText;
\r
1251 int nMthInt = globCal.GetMonth (globCal.AddMonths (visibleDate, 1));
\r
1252 if(NextPrevFormat == NextPrevFormat.FullMonth)
\r
1253 nextContent = infoCal.GetMonthName(nMthInt);
\r
1255 nextContent = infoCal.GetAbbreviatedMonthName(nMthInt);
\r
1257 DateTime next_month = visibleDate.AddMonths (1);
1258 int next_offset = (int) (new DateTime (next_month.Year,
1259 next_month.Month, 1) - begin_date).TotalDays;
1260 nextCell.ApplyStyle(NextPrevStyle);
\r
1261 RenderCalendarCell (writer,
\r
1263 GetCalendarLinkText ("V" + next_offset,
\r
1265 "Go to next month",
\r
1266 NextPrevStyle.ForeColor,
\r
1271 writer.Write("</tr>");
\r
1272 innerTable.RenderEndTag(writer);
\r
1273 titleCell.RenderEndTag(writer);
\r
1275 writer.Write("</tr>");
\r
1278 private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
\r
1280 if(style.BackColor != Color.Empty)
\r
1282 cell.BackColor = style.BackColor;
\r
1284 if(style.BorderStyle != BorderStyle.NotSet)
\r
1286 cell.BorderStyle = style.BorderStyle;
\r
1288 if(style.BorderColor != Color.Empty)
\r
1290 cell.BorderColor = style.BorderColor;
\r
1292 if(style.BorderWidth != Unit.Empty)
\r
1294 cell.BorderWidth = style.BorderWidth;
\r
1296 if(style.Height != Unit.Empty)
\r
1298 cell.Height = style.Height;
\r
1300 if(style.VerticalAlign != VerticalAlign.NotSet)
\r
1302 cell.VerticalAlign = style.VerticalAlign;
\r
1305 if(style.ForeColor != Color.Empty)
\r
1307 table.ForeColor = style.ForeColor;
\r
1308 } else if(ForeColor != Color.Empty)
\r
1310 table.ForeColor = ForeColor;
\r
1313 if (style.CssClass != "") {
1314 cell.CssClass = style.CssClass;
1317 table.Font.CopyFrom(style.Font);
\r
1318 table.Font.MergeWith(Font);
\r
1321 private void RenderCalendarHeaderCell (HtmlTextWriter writer, TableCell cell, string text, string altText)
\r
1324 if (UseAccessibleHeader) {
\r
1325 writer.AddAttribute ("align", "center");
\r
1326 writer.AddAttribute ("abbr", altText);
\r
1327 writer.AddAttribute ("scope", "column");
\r
1328 writer.RenderBeginTag (HtmlTextWriterTag.Th);
\r
1329 writer.Write (text);
\r
1330 writer.RenderEndTag ();
\r
1334 RenderCalendarCell (writer, cell, text);
\r
1337 private void RenderCalendarCell (HtmlTextWriter writer, TableCell cell, string text)
\r
1339 cell.RenderBeginTag(writer);
\r
1340 writer.Write(text);
\r
1341 cell.RenderEndTag(writer);
\r
1344 private DateTime GetFirstCalendarDay(DateTime visibleDate)
\r
1346 int fow = (int) FirstDayOfWeek;
\r
1348 fow = (int) infoCal.FirstDayOfWeek;
\r
1350 int days = (int) globCal.GetDayOfWeek (visibleDate) - fow;
\r
1353 return globCal.AddDays (visibleDate, -days);
\r
1356 private DateTime GetEffectiveVisibleDate()
\r
1358 DateTime dt = VisibleDate;
\r
1359 if (VisibleDate == DateTime.MinValue)
\r
1362 return globCal.AddDays (dt, 1 - globCal.GetDayOfMonth (dt));
\r
1366 /// Creates text to be displayed, with all attributes if to be
\r
1367 /// shown as a hyperlink
\r
1369 private string GetCalendarLinkText (string eventArg,
\r
1376 StringBuilder dispVal = new StringBuilder ();
\r
1377 dispVal.Append ("<a href=\"");
\r
1378 dispVal.Append (Page.GetPostBackClientHyperlink (this, eventArg));
\r
1379 dispVal.Append ("\" style=\"color: ");
\r
1380 if (foreground.IsEmpty) {
\r
1381 dispVal.Append (ColorTranslator.ToHtml (defaultTextColor));
\r
1383 dispVal.Append (ColorTranslator.ToHtml (foreground));
\r
1385 dispVal.Append ("\"");
\r
1387 if (UseAccessibleHeader) {
\r
1388 dispVal.Append (" title=\"");
\r
1389 dispVal.Append (altText);
\r
1390 dispVal.Append ("\"");
\r
1393 dispVal.Append (">");
\r
1394 dispVal.Append (text);
\r
1395 dispVal.Append ("</a>");
\r
1396 return dispVal.ToString ();
\r
1401 private string GetHtmlForCell (TableCell cell, bool showLinks)
\r
1403 StringWriter sw = new StringWriter ();
\r
1404 HtmlTextWriter htw = new HtmlTextWriter (sw);
\r
1405 cell.RenderBeginTag (htw);
\r
1407 htw.Write (GetCalendarLinkText ("{0}", "{1}", "{1}", cell.ForeColor, showLinks));
\r
1409 htw.Write ("{0}");
\r
1411 cell.RenderEndTag (htw);
\r
1412 return sw.ToString();
\r
1415 internal void SelectRangeInternal (DateTime fromDate, DateTime toDate, DateTime visibleDate)
\r
1417 TimeSpan span = toDate - fromDate;
\r
1418 if (SelectedDates.Count != span.Days + 1 ||
\r
1419 SelectedDates [0] != fromDate ||
\r
1420 SelectedDates [SelectedDates.Count - 1] != toDate) {
\r
1421 SelectedDates.SelectRange (fromDate, toDate);
\r
1422 OnSelectionChanged ();
\r
1426 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
1428 return selectionMode == CalendarSelectionMode.DayWeek ||
1429 selectionMode == CalendarSelectionMode.DayWeekMonth;