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) 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;
\r
54 private static int MASK_WEEKEND = (0x01 << 0);
\r
55 private static int MASK_OMONTH = (0x01 << 1);
\r
56 private static int MASK_TODAY = (0x01 << 2);
\r
57 private static int MASK_SELECTED = (0x01 << 3);
\r
58 private static int MASK_DAY = (0x01 << 4);
\r
59 private static int MASK_UNIQUE = MASK_WEEKEND | MASK_OMONTH | MASK_TODAY | MASK_SELECTED;
\r
61 public Calendar(): base()
\r
63 //TODO: Initialization
\r
67 [DefaultValue (2), Bindable (true), WebCategory ("Layout")]
68 [WebSysDescription ("The border left within the calendar days.")]
\r
69 public int CellPadding
\r
73 object o = ViewState["CellPadding"];
\r
81 throw new ArgumentOutOfRangeException ("value", "CellPadding value has to be -1 for 'not set' or > -1.");
\r
82 ViewState["CellPadding"] = value;
\r
86 [DefaultValue (0), Bindable (true), WebCategory ("Layout")]
87 [WebSysDescription ("The border left between calendar days.")]
\r
88 public int CellSpacing
\r
92 object o = ViewState["CellSpacing"];
\r
100 throw new ArgumentOutOfRangeException ("value", "CellSpacing value has to be -1 for 'not set' or > -1.");
\r
101 ViewState["CellSpacing"] = value;
\r
105 [NotifyParentProperty (true), WebCategory ("Style")]
106 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
107 [PersistenceMode (PersistenceMode.InnerProperty)]
108 [WebSysDescription ("The style applied to the day header.")]
\r
109 public TableItemStyle DayHeaderStyle
\r
113 if(dayHeaderStyle==null)
\r
114 dayHeaderStyle = new TableItemStyle();
\r
115 if(IsTrackingViewState)
\r
116 dayHeaderStyle.TrackViewState();
\r
117 return dayHeaderStyle;
\r
121 [DefaultValue (typeof (DayNameFormat), "Short"), Bindable (true), WebCategory ("Appearance")]
122 [WebSysDescription ("The format for the day name display.")]
\r
123 public DayNameFormat DayNameFormat
\r
127 object o = ViewState["DayNameFormat"];
\r
129 return (DayNameFormat)o;
\r
130 return DayNameFormat.Short;
\r
134 if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
\r
135 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
136 ViewState["DayNameFormat"] = value;
\r
140 [DefaultValue (null)]
141 [NotifyParentProperty (true), WebCategory ("Style")]
142 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
143 [PersistenceMode (PersistenceMode.InnerProperty)]
144 [WebSysDescription ("The style applied to the day entry.")]
\r
145 public TableItemStyle DayStyle
\r
150 dayStyle = new TableItemStyle();
\r
151 if(IsTrackingViewState)
\r
152 dayStyle.TrackViewState();
\r
157 [DefaultValue (typeof (FirstDayOfWeek), "Default"), Bindable (true), WebCategory ("Appearance")]
158 [WebSysDescription ("The day that a week begins with.")]
\r
159 public FirstDayOfWeek FirstDayOfWeek
\r
163 object o = ViewState["FirstDayOfWeek"];
\r
165 return (FirstDayOfWeek)o;
\r
166 return FirstDayOfWeek.Default;
\r
170 if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
\r
171 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
172 ViewState["FirstDayOfWeek"] = value;
\r
176 [DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
177 [WebSysDescription ("The text for selecting the next month.")]
\r
178 public string NextMonthText
\r
182 object o = ViewState["NextMonthText"];
\r
189 ViewState["NextMonthText"] = value;
\r
193 [DefaultValue (typeof (NextPrevFormat), "CustomText"), Bindable (true), WebCategory ("Appearance")]
194 [WebSysDescription ("The format for the month navigation.")]
\r
195 public NextPrevFormat NextPrevFormat
\r
199 object o = ViewState["NextPrevFormat"];
\r
201 return (NextPrevFormat)o;
\r
202 return NextPrevFormat.CustomText;
\r
206 if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
\r
207 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
208 ViewState["NextPrevFormat"] = value;
\r
212 [NotifyParentProperty (true), WebCategory ("Style")]
213 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
214 [PersistenceMode (PersistenceMode.InnerProperty)]
215 [WebSysDescription ("The style applied to the month navigation.")]
\r
216 public TableItemStyle NextPrevStyle
\r
220 if(nextPrevStyle == null)
\r
221 nextPrevStyle = new TableItemStyle();
\r
222 if(IsTrackingViewState)
\r
223 nextPrevStyle.TrackViewState();
\r
224 return nextPrevStyle;
\r
228 [DefaultValue (null)]
229 [NotifyParentProperty (true), WebCategory ("Style")]
230 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
231 [PersistenceMode (PersistenceMode.InnerProperty)]
232 [WebSysDescription ("The style applied to day entries belonging to another month.")]
\r
233 public TableItemStyle OtherMonthDayStyle
\r
237 if(otherMonthDayStyle == null)
\r
238 otherMonthDayStyle = new TableItemStyle();
\r
239 if(IsTrackingViewState)
\r
240 otherMonthDayStyle.TrackViewState();
\r
241 return otherMonthDayStyle;
\r
245 [DefaultValue ("<"), Bindable (true), WebCategory ("Appearance")]
246 [WebSysDescription ("The text for selecting the previous month.")]
\r
247 public string PrevMonthText
\r
251 object o = ViewState["PrevMonthText"];
\r
258 ViewState["PrevMonthText"] = value;
\r
262 [DefaultValue (null), Bindable (true)]
263 [WebSysDescription ("The currently selected date.")]
\r
264 public DateTime SelectedDate
\r
268 if(SelectedDates.Count > 0)
\r
270 return SelectedDates[0];
\r
272 return DateTime.MinValue;
\r
276 if(value == DateTime.MinValue)
\r
278 SelectedDates.Clear();
\r
281 SelectedDates.SelectRange(value, value);
\r
286 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
287 [WebSysDescription ("All currently selected dates.")]
\r
288 public SelectedDatesCollection SelectedDates
\r
292 if(selectedDates==null)
\r
294 if(selectedDatesList == null)
\r
295 selectedDatesList = new ArrayList();
\r
296 selectedDates = new SelectedDatesCollection(selectedDatesList);
\r
298 return selectedDates;
\r
302 [DefaultValue (null)]
303 [NotifyParentProperty (true), WebCategory ("Style")]
304 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
305 [PersistenceMode (PersistenceMode.InnerProperty)]
306 [WebSysDescription ("The style applied to the selected day.")]
\r
307 public TableItemStyle SelectedDayStyle
\r
311 if(selectedDayStyle==null)
\r
312 selectedDayStyle = new TableItemStyle();
\r
313 if(IsTrackingViewState)
\r
314 selectedDayStyle.TrackViewState();
\r
315 return selectedDayStyle;
\r
319 [DefaultValue (typeof (CalendarSelectionMode), "Day"), Bindable (true), WebCategory ("Behavior")]
320 [WebSysDescription ("The mode in which days or other entries are selected.")]
\r
321 public CalendarSelectionMode SelectionMode
\r
325 object o = ViewState["SelectionMode"];
\r
327 return (CalendarSelectionMode)o;
\r
328 return CalendarSelectionMode.Day;
\r
332 if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
\r
333 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
334 ViewState["SelectionMode"] = value;
\r
338 [DefaultValue (">>"), Bindable (true), WebCategory ("Appearance")]
339 [WebSysDescription ("The text that is used for selection of months.")]
\r
340 public string SelectMonthText
\r
344 object o = ViewState["SelectMonthText"];
\r
351 ViewState["SelectMonthText"] = value;
\r
355 [NotifyParentProperty (true), WebCategory ("Style")]
356 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
357 [PersistenceMode (PersistenceMode.InnerProperty)]
358 [WebSysDescription ("The style applied to the selector.")]
\r
359 public TableItemStyle SelectorStyle
\r
363 if(selectorStyle==null)
\r
364 selectorStyle = new TableItemStyle();
\r
365 return selectorStyle;
\r
369 [DefaultValue (">"), Bindable (true), WebCategory ("Appearance")]
370 [WebSysDescription ("The text that is used for selection of weeks.")]
\r
371 public string SelectWeekText
\r
375 object o = ViewState["SelectWeekText"];
\r
382 ViewState["SelectWeekText"] = value;
\r
386 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
387 [WebSysDescription ("Determines if the header for days is shown.")]
\r
388 public bool ShowDayHeader
\r
392 object o = ViewState["ShowDayHeader"];
\r
399 ViewState["ShowDayHeader"] = value;
\r
403 [DefaultValue (false), Bindable (true), WebCategory ("Appearance")]
404 [WebSysDescription ("Determines if gridlines are shown.")]
\r
405 public bool ShowGridLines
\r
409 object o = ViewState["ShowGridLines"];
\r
416 ViewState["ShowGridLines"] = value;
\r
420 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
421 [WebSysDescription ("Determines if month navigation is shown.")]
\r
422 public bool ShowNextPrevMonth
\r
426 object o = ViewState["ShowNextPrevMonth"];
\r
433 ViewState["ShowNextPrevMonth"] = value;
\r
437 [DefaultValue (true), Bindable (true), WebCategory ("Appearance")]
438 [WebSysDescription ("Determines if the title is shown.")]
\r
439 public bool ShowTitle
\r
443 object o = ViewState["ShowTitle"];
\r
450 ViewState["ShowTitle"] = value;
\r
454 [DefaultValue (typeof (TitleFormat), "MonthYear"), Bindable (true), WebCategory ("Appearance")]
455 [WebSysDescription ("The format in which the title is rendered.")]
\r
456 public TitleFormat TitleFormat
\r
460 object o = ViewState["TitleFormat"];
\r
462 return (TitleFormat)o;
\r
463 return TitleFormat.MonthYear;
\r
467 if(!System.Enum.IsDefined(typeof(TitleFormat), value))
\r
468 throw new ArgumentOutOfRangeException ("value", "Only valid enumeration members are allowed");
\r
469 ViewState["TitleFormat"] = value;
\r
473 [NotifyParentProperty (true), WebCategory ("Style")]
474 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
475 [PersistenceMode (PersistenceMode.InnerProperty)]
476 [WebSysDescription ("The style applied to the title.")]
\r
477 public TableItemStyle TitleStyle
\r
481 if(titleStyle==null)
\r
482 titleStyle = new TableItemStyle();
\r
483 if(IsTrackingViewState)
\r
484 titleStyle.TrackViewState();
\r
489 [DefaultValue (null)]
490 [NotifyParentProperty (true), WebCategory ("Style")]
491 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
492 [PersistenceMode (PersistenceMode.InnerProperty)]
493 [WebSysDescription ("The style applied to the today's date display.")]
\r
494 public TableItemStyle TodayDayStyle
\r
498 if(todayDayStyle==null)
\r
499 todayDayStyle = new TableItemStyle();
\r
500 if(IsTrackingViewState)
\r
501 todayDayStyle.TrackViewState();
\r
502 return todayDayStyle;
\r
506 [Bindable (true), Browsable (false)]
507 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
508 [WebSysDescription ("The current date.")]
\r
509 public DateTime TodaysDate
\r
513 object o = ViewState["TodaysDate"];
\r
515 return (DateTime)o;
\r
516 return DateTime.Today;
\r
520 ViewState["TodaysDate"] = value.Date;
\r
524 [DefaultValue (null), Bindable (true)]
525 [WebSysDescription ("The month that is displayed.")]
\r
526 public DateTime VisibleDate
\r
530 object o = ViewState["VisibleDate"];
\r
532 return (DateTime)o;
\r
533 return DateTime.MinValue;
\r
537 ViewState["VisibleDate"] = value.Date;
\r
541 [NotifyParentProperty (true), WebCategory ("Style")]
542 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
543 [PersistenceMode (PersistenceMode.InnerProperty)]
544 [WebSysDescription ("The style applied to weekend days.")]
\r
545 public TableItemStyle WeekendDayStyle
\r
549 if(weekendDayStyle == null)
\r
550 weekendDayStyle = new TableItemStyle();
\r
551 if(IsTrackingViewState)
\r
553 weekendDayStyle.TrackViewState();
\r
555 return weekendDayStyle;
\r
559 [WebCategory ("Action")]
560 [WebSysDescription ("Raised when a day entry is rendered.")]
\r
561 public event DayRenderEventHandler DayRender
\r
565 Events.AddHandler(DayRenderEvent, value);
\r
569 Events.RemoveHandler(DayRenderEvent, value);
\r
573 [WebCategory ("Action")]
574 [WebSysDescription ("Raised when another entry is selected.")]
\r
575 public event EventHandler SelectionChanged
\r
579 Events.AddHandler(SelectionChangedEvent, value);
\r
583 Events.RemoveHandler(SelectionChangedEvent, value);
\r
587 [WebCategory ("Action")]
588 [WebSysDescription ("Raised when a the currently visible month has changed.")]
\r
589 public event MonthChangedEventHandler VisibleMonthChanged
\r
593 Events.AddHandler(VisibleMonthChangedEvent, value);
\r
597 Events.RemoveHandler(VisibleMonthChangedEvent, value);
\r
601 protected virtual void OnDayRender(TableCell cell, CalendarDay day)
\r
605 DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
\r
607 dreh(this, new DayRenderEventArgs(cell, day));
\r
611 protected virtual void OnSelectionChanged()
\r
615 EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
\r
617 eh(this, new EventArgs());
\r
621 protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
\r
625 MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
\r
627 mceh(this, new MonthChangedEventArgs(newDate, prevDate));
\r
632 /// See test6.aspx in Tests directory for verification
\r
634 void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
\r
636 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
637 DateTime visDate = GetEffectiveVisibleDate();
\r
638 if(eventArgument == "nextMonth")
\r
640 VisibleDate = globCal.AddMonths(visDate, 1);
\r
641 OnVisibleMonthChanged(VisibleDate, visDate);
\r
644 if(eventArgument == "prevMonth")
\r
646 VisibleDate = globCal.AddMonths(visDate, -1);
\r
647 OnVisibleMonthChanged(VisibleDate, visDate);
\r
650 if(eventArgument == "selectMonth")
\r
652 DateTime oldDate = new DateTime(globCal.GetYear(visDate), globCal.GetMonth(visDate), 1, globCal);
\r
653 SelectRangeInternal(oldDate, globCal.AddDays(globCal.AddMonths(oldDate, 1), -1), visDate);
\r
656 if(String.Compare(eventArgument, 0, "selectWeek", 0, "selectWeek".Length)==0)
\r
661 week = Int32.Parse(eventArgument.Substring("selectWeek".Length));
\r
662 } catch(Exception e)
\r
665 if(week >= 0 && week <= 5)
\r
667 DateTime weekStart = globCal.AddDays(GetFirstCalendarDay(visDate), week * 7);
\r
668 SelectRangeInternal(weekStart, globCal.AddDays(weekStart, 6), visDate);
\r
672 if(String.Compare(eventArgument, 0, "selectDay", 0, "selectDay".Length)==0)
\r
677 day = Int32.Parse(eventArgument.Substring("selectDay".Length));
\r
678 } catch(Exception e)
\r
681 if(day >= 0 && day <= 42)
\r
683 DateTime dayStart = globCal.AddDays(GetFirstCalendarDay(visDate), day);
\r
684 SelectRangeInternal(dayStart, dayStart, visDate);
\r
689 protected override void Render(HtmlTextWriter writer)
\r
691 //TODO: Implement me
\r
692 globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
\r
693 DateTime visDate = GetEffectiveVisibleDate();
\r
694 DateTime firstDate = GetFirstCalendarDay(visDate);
\r
697 bool isHtmlTextWriter;
\r
698 //FIXME: when Control.Site works, reactivate this
\r
699 //if (Page == null || Site == null) {
\r
700 // isEnabled = false;
\r
701 // isHtmlTextWriter = false;
\r
703 isEnabled = Enabled;
\r
704 isHtmlTextWriter = (writer.GetType() != typeof(HtmlTextWriter));
\r
706 defaultTextColor = ForeColor;
\r
707 if(defaultTextColor == Color.Empty)
\r
708 defaultTextColor = Color.Black;
\r
710 Table calTable = new Table ();
\r
712 calTable.CopyBaseAttributes(this);
\r
713 if(ControlStyleCreated)
\r
714 calTable.ApplyStyle(ControlStyle);
\r
715 calTable.Width = Width;
\r
716 calTable.Height = Height;
\r
717 calTable.CellSpacing = CellSpacing;
\r
718 calTable.CellPadding = CellPadding;
\r
720 if (ControlStyleCreated &&
\r
721 ControlStyle.IsSet (WebControls.Style.BORDERWIDTH) &&
\r
722 BorderWidth != Unit.Empty)
\r
723 calTable.BorderWidth = BorderWidth;
\r
725 calTable.BorderWidth = Unit.Pixel(1);
\r
728 calTable.GridLines = GridLines.Both;
\r
730 calTable.GridLines = GridLines.None;
\r
732 calTable.RenderBeginTag (writer);
\r
735 RenderTitle (writer, visDate, SelectionMode, isEnabled);
\r
738 RenderHeader (writer, firstDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
740 RenderAllDays (writer, firstDate, visDate, SelectionMode, isEnabled, isHtmlTextWriter);
\r
742 calTable.RenderEndTag(writer);
\r
745 protected override ControlCollection CreateControlCollection()
\r
747 return new EmptyControlCollection(this);
\r
750 protected override void LoadViewState(object savedState)
\r
752 if (savedState!=null) {
\r
753 object[] states = (object[]) savedState;
\r
754 if(states[0] != null)
\r
755 base.LoadViewState(states[0]);
\r
756 if(states[1] != null)
\r
757 DayHeaderStyle.LoadViewState(states[1]);
\r
758 if(states[2] != null)
\r
759 DayStyle.LoadViewState(states[2]);
\r
760 if(states[3] != null)
\r
761 NextPrevStyle.LoadViewState(states[3]);
\r
762 if(states[4] != null)
\r
763 OtherMonthDayStyle.LoadViewState(states[4]);
\r
764 if(states[5] != null)
\r
765 SelectedDayStyle.LoadViewState(states[5]);
\r
766 if(states[6] != null)
\r
767 SelectorStyle.LoadViewState(states[6]);
\r
768 if(states[7] != null)
\r
769 TitleStyle.LoadViewState(states[7]);
\r
770 if(states[8] != null)
\r
771 TodayDayStyle.LoadViewState(states[8]);
\r
772 if(states[9] != null)
\r
773 WeekendDayStyle.LoadViewState(states[9]);
\r
775 ArrayList dateList = ViewState ["_CalendarSelectedDates"] as ArrayList;
\r
776 if (dateList != null)
\r
777 selectedDates = new SelectedDatesCollection (dateList);
\r
781 protected override object SaveViewState()
\r
783 if (SelectedDates.Count > 0)
\r
784 ViewState["_CalendarSelectedDates"] = selectedDates.GetDateList ();
786 object[] states = new object [10];
\r
787 states[0] = base.SaveViewState();
\r
788 states[1] = (dayHeaderStyle == null ? null : dayHeaderStyle.SaveViewState());
\r
789 states[2] = (dayStyle == null ? null : dayStyle.SaveViewState());
\r
790 states[3] = (nextPrevStyle == null ? null : nextPrevStyle.SaveViewState());
\r
791 states[4] = (otherMonthDayStyle == null ? null : otherMonthDayStyle.SaveViewState());
\r
792 states[5] = (selectedDayStyle == null ? null : selectedDayStyle.SaveViewState());
\r
793 states[6] = (selectorStyle == null ? null : selectorStyle.SaveViewState());
\r
794 states[7] = (titleStyle == null ? null : titleStyle.SaveViewState());
\r
795 states[8] = (todayDayStyle == null ? null : todayDayStyle.SaveViewState());
\r
796 states[9] = (weekendDayStyle == null ? null : weekendDayStyle.SaveViewState());
\r
797 for(int i=0; i < states.Length; i++)
\r
799 if(states[i]!=null)
\r
805 protected override void TrackViewState()
\r
807 base.TrackViewState();
\r
808 if(titleStyle!=null)
\r
810 titleStyle.TrackViewState();
\r
812 if(nextPrevStyle!=null)
\r
814 nextPrevStyle.TrackViewState();
\r
818 dayStyle.TrackViewState();
\r
820 if(dayHeaderStyle!=null)
\r
822 dayHeaderStyle.TrackViewState();
\r
824 if(todayDayStyle!=null)
\r
826 todayDayStyle.TrackViewState();
\r
828 if(weekendDayStyle!=null)
\r
830 weekendDayStyle.TrackViewState();
\r
832 if(otherMonthDayStyle!=null)
\r
834 otherMonthDayStyle.TrackViewState();
\r
836 if(selectedDayStyle!=null)
\r
838 selectedDayStyle.TrackViewState();
\r
840 if(selectorStyle!=null)
\r
842 selectorStyle.TrackViewState();
\r
846 private void RenderAllDays (HtmlTextWriter writer,
\r
848 DateTime activeDate,
\r
849 CalendarSelectionMode mode,
\r
853 TableItemStyle weeksStyle = null;
854 TableCell weeksCell = new TableCell ();
\r
855 TableItemStyle weekendStyle = WeekendDayStyle;
\r
856 TableItemStyle otherMonthStyle = OtherMonthDayStyle;
\r
858 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
859 mode == CalendarSelectionMode.DayWeekMonth);
\r
862 weeksStyle = new TableItemStyle ();
\r
863 weeksStyle.Width = Unit.Percentage (12);
\r
864 weeksStyle.HorizontalAlign = HorizontalAlign.Center;
\r
865 weeksStyle.CopyFrom (SelectorStyle);
\r
866 size = Unit.Percentage (12);
\r
868 size = Unit.Percentage (14);
\r
871 TableItemStyle [] styles = new TableItemStyle [32];
\r
872 int definedStyles = MASK_SELECTED;
\r
873 if (weekendStyle != null && !weekendStyle.IsEmpty)
\r
874 definedStyles |= MASK_WEEKEND;
\r
875 if (otherMonthStyle != null && !otherMonthStyle.IsEmpty)
\r
876 definedStyles |= MASK_OMONTH;
\r
877 if (todayDayStyle != null && !todayDayStyle.IsEmpty)
\r
878 definedStyles |= MASK_TODAY;
\r
879 if (dayStyle != null && !dayStyle.IsEmpty)
\r
880 definedStyles |= MASK_DAY;
\r
882 bool isCalendar = GetType () == typeof (Calendar);
\r
883 int month = globCal.GetMonth (activeDate);
\r
884 DateTime currentDay = firstDay;
\r
885 for (int crr = 0; crr < 6; crr++) {
\r
886 writer.Write ("<tr>");
\r
888 string cellText = GetCalendarLinkText (
\r
889 "selectWeek" + crr.ToString (),
\r
891 weeksCell.ForeColor,
\r
894 weeksCell.Text = cellText;
\r
895 weeksCell.ApplyStyle (weeksStyle);
896 RenderCalendarCell (writer, weeksCell, cellText);
\r
899 for (int weekDay = 0; weekDay < 7; weekDay++) {
\r
900 string dayString = currentDay.Day.ToString ();
\r
901 DayOfWeek dow = currentDay.DayOfWeek;
902 CalendarDay calDay =
\r
905 dow == DayOfWeek.Sunday ||
906 dow == DayOfWeek.Saturday,
907 currentDay == TodaysDate,
\r
908 selectedDates.Contains (currentDay),
\r
909 globCal.GetMonth (currentDay) != month,
\r
914 int dayStyles = GetMask (calDay) & definedStyles;
\r
915 TableItemStyle currentDayStyle = styles [dayStyles];
\r
916 if (currentDayStyle == null) {
\r
917 currentDayStyle = new TableItemStyle ();
\r
918 if ((dayStyles & MASK_DAY) != 0)
\r
919 currentDayStyle.CopyFrom (DayStyle);
\r
921 if ((dayStyles & MASK_WEEKEND) != 0)
\r
922 currentDayStyle.CopyFrom (WeekendDayStyle);
\r
924 if ((dayStyles & MASK_TODAY) != 0)
\r
925 currentDayStyle.CopyFrom (TodayDayStyle);
\r
927 if ((dayStyles & MASK_OMONTH) != 0)
\r
928 currentDayStyle.CopyFrom (OtherMonthDayStyle);
\r
930 if ((dayStyles & MASK_SELECTED) != 0) {
\r
931 currentDayStyle.ForeColor = Color.White;
\r
932 currentDayStyle.BackColor = Color.Silver;
\r
933 currentDayStyle.CopyFrom (SelectedDayStyle);
\r
936 currentDayStyle.Width = size;
\r
937 currentDayStyle.HorizontalAlign = HorizontalAlign.Center;
\r
940 TableCell dayCell = new TableCell ();
\r
941 dayCell.ApplyStyle (currentDayStyle);
\r
942 calDay.IsSelectable = isActive;
\r
943 OnDayRender (dayCell, calDay);
\r
945 dayCell.Text = GetCalendarLinkText (
\r
946 "selectDay" + (crr * 7 + weekDay),
\r
951 dayCell.Text = dayString;
\r
953 dayCell.RenderControl (writer);
\r
955 currentDay = globCal.AddDays (currentDay, 1);
\r
957 writer.Write("</tr>");
\r
961 private int GetMask (CalendarDay day)
\r
963 int retVal = MASK_DAY;
\r
965 retVal |= MASK_SELECTED;
\r
967 retVal |= MASK_TODAY;
\r
968 if(day.IsOtherMonth)
\r
969 retVal |= MASK_OMONTH;
\r
971 retVal |= MASK_WEEKEND;
\r
976 /// Refers to the second line of the calendar, that contains a link
\r
977 /// to select whole month, and weekdays as defined by DayNameFormat
\r
979 private void RenderHeader (HtmlTextWriter writer,
\r
981 CalendarSelectionMode mode,
\r
985 writer.Write("<tr>");
\r
986 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
987 mode == CalendarSelectionMode.DayWeekMonth);
\r
989 TableCell headerCell = new TableCell ();
\r
990 headerCell.HorizontalAlign = HorizontalAlign.Center;
\r
991 string selMthText = String.Empty;
\r
993 if (mode == CalendarSelectionMode.DayWeekMonth) {
\r
994 headerCell.ApplyStyle (SelectorStyle);
\r
995 selMthText = GetCalendarLinkText ("selectMonth",
\r
997 SelectorStyle.ForeColor,
\r
1000 headerCell.ApplyStyle (DayHeaderStyle);
\r
1001 selMthText = String.Empty;
\r
1003 RenderCalendarCell (writer, headerCell, selMthText);
\r
1006 TableCell dayHeaderCell = new TableCell ();
\r
1007 dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
\r
1008 dayHeaderCell.ApplyStyle (dayHeaderStyle);
1010 int dayOfWeek = (int) globCal.GetDayOfWeek (firstDay);
\r
1011 DateTimeFormatInfo currDTInfo = DateTimeFormatInfo.CurrentInfo;
\r
1012 for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++) {
\r
1013 DayOfWeek effDay = (DayOfWeek) Enum.ToObject (typeof (DayOfWeek), currDay % 7);
\r
1014 string currDayContent;
\r
1015 switch(DayNameFormat) {
\r
1016 case DayNameFormat.Full:
\r
1017 currDayContent = currDTInfo.GetDayName (effDay);
\r
1019 case DayNameFormat.FirstLetter:
\r
1020 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,1);
\r
1022 case DayNameFormat.FirstTwoLetters:
\r
1023 currDayContent = currDTInfo.GetDayName (effDay).Substring (0,2);
\r
1025 case DayNameFormat.Short:
\r
1028 currDayContent = currDTInfo.GetAbbreviatedDayName (effDay);
\r
1032 RenderCalendarCell(writer, dayHeaderCell, currDayContent);
\r
1034 writer.Write ("</tr>");
\r
1037 private void RenderTitle (HtmlTextWriter writer,
\r
1038 DateTime visibleDate,
\r
1039 CalendarSelectionMode mode,
\r
1042 writer.Write("<tr>");
\r
1043 Table innerTable = new Table ();
\r
1044 TableCell titleCell = new TableCell();
\r
1045 bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
\r
1046 mode == CalendarSelectionMode.DayWeekMonth);
\r
1048 titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
\r
1049 titleCell.BackColor = Color.Silver;
\r
1051 innerTable.GridLines = GridLines.None;
\r
1052 innerTable.Width = Unit.Percentage (100);
\r
1053 innerTable.CellSpacing = 0;
\r
1054 ApplyTitleStyle (innerTable, titleCell, TitleStyle);
\r
1056 titleCell.RenderBeginTag (writer);
\r
1057 innerTable.RenderBeginTag (writer);
\r
1059 writer.Write ("<tr>");
\r
1060 string prevContent = String.Empty;
\r
1061 if (ShowNextPrevMonth) {
\r
1062 TableCell prevCell = new TableCell ();
\r
1063 prevCell.Width = Unit.Percentage (15);
\r
1064 prevCell.HorizontalAlign = HorizontalAlign.Left;
\r
1065 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1066 prevContent = PrevMonthText;
\r
1068 int pMthInt = globCal.GetMonth(globCal.AddMonths (visibleDate, -1));
\r
1069 if (NextPrevFormat == NextPrevFormat.FullMonth)
\r
1070 prevContent = infoCal.GetMonthName (pMthInt);
\r
1072 prevContent = infoCal.GetAbbreviatedMonthName (pMthInt);
\r
1074 prevCell.ApplyStyle (NextPrevStyle);
\r
1075 RenderCalendarCell (writer,
\r
1077 GetCalendarLinkText ("prevMonth",
\r
1079 NextPrevStyle.ForeColor,
\r
1084 TableCell currCell = new TableCell ();
\r
1085 currCell.Width = Unit.Percentage (70);
\r
1086 if (TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
\r
1087 currCell.HorizontalAlign = HorizontalAlign.Center;
\r
1089 currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
\r
1091 currCell.Wrap = TitleStyle.Wrap;
\r
1092 string currMonthContent = String.Empty;
\r
1093 if (TitleFormat == TitleFormat.Month) {
\r
1094 currMonthContent = visibleDate.ToString ("MMMM");
\r
1096 string cmcFmt = infoCal.YearMonthPattern;
\r
1097 if (cmcFmt.IndexOf (',') >= 0)
\r
1098 cmcFmt = "MMMM yyyy";
\r
1100 currMonthContent = visibleDate.ToString (cmcFmt);
\r
1103 RenderCalendarCell (writer, currCell, currMonthContent);
\r
1105 string nextContent = String.Empty;
\r
1106 if (ShowNextPrevMonth) {
\r
1107 TableCell nextCell = new TableCell ();
\r
1108 nextCell.Width = Unit.Percentage(15);
\r
1109 nextCell.HorizontalAlign = HorizontalAlign.Right;
\r
1110 if (NextPrevFormat == NextPrevFormat.CustomText) {
\r
1111 nextContent = NextMonthText;
\r
1113 int nMthInt = globCal.GetMonth (globCal.AddMonths (visibleDate, 1));
\r
1114 if(NextPrevFormat == NextPrevFormat.FullMonth)
\r
1115 nextContent = infoCal.GetMonthName(nMthInt);
\r
1117 nextContent = infoCal.GetAbbreviatedMonthName(nMthInt);
\r
1119 nextCell.ApplyStyle(NextPrevStyle);
\r
1120 RenderCalendarCell (writer,
\r
1122 GetCalendarLinkText ("nextMonth",
\r
1124 NextPrevStyle.ForeColor,
\r
1129 writer.Write("</tr>");
\r
1130 innerTable.RenderEndTag(writer);
\r
1131 titleCell.RenderEndTag(writer);
\r
1133 writer.Write("</tr>");
\r
1136 private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
\r
1138 if(style.BackColor != Color.Empty)
\r
1140 cell.BackColor = style.BackColor;
\r
1142 if(style.BorderStyle != BorderStyle.NotSet)
\r
1144 cell.BorderStyle = style.BorderStyle;
\r
1146 if(style.BorderColor != Color.Empty)
\r
1148 cell.BorderColor = style.BorderColor;
\r
1150 if(style.BorderWidth != Unit.Empty)
\r
1152 cell.BorderWidth = style.BorderWidth;
\r
1154 if(style.Height != Unit.Empty)
\r
1156 cell.Height = style.Height;
\r
1158 if(style.VerticalAlign != VerticalAlign.NotSet)
\r
1160 cell.VerticalAlign = style.VerticalAlign;
\r
1163 if(style.ForeColor != Color.Empty)
\r
1165 table.ForeColor = style.ForeColor;
\r
1166 } else if(ForeColor != Color.Empty)
\r
1168 table.ForeColor = ForeColor;
\r
1171 table.Font.CopyFrom(style.Font);
\r
1172 table.Font.MergeWith(Font);
\r
1175 private void RenderCalendarCell (HtmlTextWriter writer, TableCell cell, string text)
\r
1177 cell.RenderBeginTag(writer);
\r
1178 writer.Write(text);
\r
1179 cell.RenderEndTag(writer);
\r
1182 private DateTime GetFirstCalendarDay(DateTime visibleDate)
\r
1184 int fow = (int) FirstDayOfWeek;
\r
1186 fow = (int) infoCal.FirstDayOfWeek;
\r
1188 int days = (int) globCal.GetDayOfWeek (visibleDate) - fow;
\r
1191 return globCal.AddDays (visibleDate, -days);
\r
1194 private DateTime GetEffectiveVisibleDate()
\r
1196 DateTime dt = VisibleDate;
\r
1197 if (VisibleDate == DateTime.MinValue)
\r
1200 return globCal.AddDays (dt, 1 - globCal.GetDayOfMonth (dt));
\r
1204 /// Creates text to be displayed, with all attributes if to be
\r
1205 /// shown as a hyperlink
\r
1207 private string GetCalendarLinkText (string eventArg,
\r
1213 StringBuilder dispVal = new StringBuilder ();
\r
1214 dispVal.Append ("<a href=\"");
\r
1215 dispVal.Append (Page.GetPostBackClientHyperlink (this, eventArg));
\r
1216 dispVal.Append ("\" style=\"color: ");
\r
1217 if (foreground.IsEmpty) {
\r
1218 dispVal.Append (ColorTranslator.ToHtml (defaultTextColor));
\r
1220 dispVal.Append (ColorTranslator.ToHtml (foreground));
\r
1222 dispVal.Append ("\">");
\r
1223 dispVal.Append (text);
\r
1224 dispVal.Append ("</a>");
\r
1225 return dispVal.ToString ();
\r
1230 private string GetHtmlForCell (TableCell cell, bool showLinks)
\r
1232 StringWriter sw = new StringWriter ();
\r
1233 HtmlTextWriter htw = new HtmlTextWriter (sw);
\r
1234 cell.RenderBeginTag (htw);
\r
1236 htw.Write (GetCalendarLinkText ("{0}", "{1}", cell.ForeColor, showLinks));
\r
1238 htw.Write ("{0}");
\r
1240 cell.RenderEndTag (htw);
\r
1241 return sw.ToString();
\r
1244 internal void SelectRangeInternal (DateTime fromDate, DateTime toDate, DateTime visibleDate)
\r
1246 TimeSpan span = toDate - fromDate;
\r
1247 if (SelectedDates.Count != span.Days + 1 ||
\r
1248 SelectedDates [0] != fromDate ||
\r
1249 SelectedDates [SelectedDates.Count - 1] != toDate) {
\r
1250 SelectedDates.SelectRange (fromDate, toDate);
\r
1251 OnSelectionChanged ();
\r