2008-03-13 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / Calendar.cs
1 //
2 // System.Web.UI.WebControls.Calendar.cs
3 //
4 // Authors:
5 //    Jordi Mas i Hernandez (jordi@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //
31
32 using System.Globalization;
33 using System.Collections;
34 using System.ComponentModel;
35 using System.Drawing;
36 using System.Security.Permissions;
37 using System.Threading;
38 using System.Text;
39
40 namespace System.Web.UI.WebControls {
41         // CAS
42         [AspNetHostingPermissionAttribute (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         [AspNetHostingPermissionAttribute (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         // attributes
45         [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
46         [DefaultEvent("SelectionChanged")]
47         [DefaultProperty("SelectedDate")]
48         [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
49 #if NET_2_0
50         [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
51         [SupportsEventValidation]
52 #endif          
53         public class Calendar : WebControl, IPostBackEventHandler {
54
55                 private TableItemStyle dayHeaderStyle;
56                 private TableItemStyle dayStyle;
57                 private TableItemStyle nextPrevStyle;
58                 private TableItemStyle otherMonthDayStyle;
59                 private TableItemStyle selectedDayStyle;
60                 private TableItemStyle titleStyle;
61                 private TableItemStyle todayDayStyle;
62                 private TableItemStyle selectorStyle;
63                 private TableItemStyle weekendDayStyle;
64                 private DateTimeFormatInfo dateInfo;
65                 private SelectedDatesCollection selectedDatesCollection;
66                 private ArrayList dateList;
67                 private DateTime today = DateTime.Today;
68                 private static DateTime dateZenith  = new DateTime (2000, 1,1);
69                 private const int daysInAWeek = 7;
70                 private static readonly object DayRenderEvent = new object ();
71                 private static readonly object SelectionChangedEvent = new object ();
72                 private static readonly object VisibleMonthChangedEvent = new object ();
73
74                 public Calendar ()
75                 {
76                         dateInfo = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
77                 }
78
79 #if NET_2_0
80                 [Localizable (true)]
81                 [DefaultValue ("")]
82                 [WebSysDescription ("")]
83                 [WebCategoryAttribute ("Appearance")]
84                 public virtual string Caption 
85                 {
86                         get {
87                                 return ViewState.GetString ("Caption", "");
88                         }
89                         set {
90                                 ViewState["Caption"] = value;
91                         }
92                 }
93
94                 [DefaultValue (TableCaptionAlign.NotSet)]
95                 [WebSysDescription ("")]
96                 [WebCategoryAttribute ("Accessibility")]
97                 public virtual TableCaptionAlign CaptionAlign 
98                 {
99                         get {
100                                 return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
101                         }
102                         set {
103                                 ViewState ["CaptionAlign"] = value;
104                         }
105                 }
106 #endif
107
108 #if ONLY_1_1
109                 [Bindable(true)]
110 #endif          
111                 [DefaultValue(2)]
112                 [WebSysDescription ("")]
113                 [WebCategory ("Layout")]
114                 public int CellPadding {
115                         get {
116                                 return ViewState.GetInt ("CellPadding", 2);
117                         }
118
119                         set {
120                                 if (value < -1)
121                                         throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
122
123                                 ViewState ["CellPadding"] = value;
124                         }
125                 }
126
127 #if ONLY_1_1
128                 [Bindable(true)]
129 #endif          
130                 [DefaultValue(0)]
131                 [WebSysDescription ("")]
132                 [WebCategory ("Layout")]
133                 public int CellSpacing {
134                         get {
135                                 return ViewState.GetInt ("CellSpacing", 0);
136                         }
137
138                         set {
139                                 if (value < -1)
140                                         throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
141
142                                 ViewState ["CellSpacing"] = value;
143                         }
144                 }
145
146                 [PersistenceMode(PersistenceMode.InnerProperty)]
147                 [NotifyParentProperty(true)]
148                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
149                 [WebSysDescription ("")]
150                 [WebCategory ("Style")]
151                 public TableItemStyle DayHeaderStyle {
152                         get {
153                                 if (dayHeaderStyle == null) {
154                                         dayHeaderStyle = new TableItemStyle ();
155                                         if (IsTrackingViewState)
156                                                 dayHeaderStyle.TrackViewState ();
157                                 }
158
159                                 return dayHeaderStyle;
160                         }
161                 }
162
163 #if ONLY_1_1
164                 [Bindable(true)]
165 #endif          
166                 [DefaultValue(DayNameFormat.Short)]
167                 [WebSysDescription ("")]
168                 [WebCategory ("Appearance")]
169                 public DayNameFormat DayNameFormat {
170                         get {
171                                 return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
172                         }
173
174                         set {
175                                 if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
176                                         value != DayNameFormat.Full && value != DayNameFormat.Short && value != DayNameFormat.Shortest) {
177                                         throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
178                                 }
179
180                                 ViewState ["DayNameFormat"] = value;
181                         }
182                 }
183
184                 [DefaultValue(null)]
185                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
186                 [NotifyParentProperty(true)]
187                 [PersistenceMode(PersistenceMode.InnerProperty)]
188                 [WebSysDescription ("")]
189                 [WebCategory ("Style")]
190                 public TableItemStyle DayStyle {
191                         get {
192                                 if (dayStyle == null) {
193                                         dayStyle = new TableItemStyle ();
194                                         if (IsTrackingViewState)
195                                                 dayStyle.TrackViewState ();
196                                 }
197
198                                 return dayStyle;
199                         }
200                 }
201
202 #if ONLY_1_1
203                 [Bindable(true)]
204 #endif          
205                 [DefaultValue(FirstDayOfWeek.Default)]
206                 [WebSysDescription ("")]
207                 [WebCategory ("Appearance")]
208                 public FirstDayOfWeek FirstDayOfWeek {
209                         get {
210                                 return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
211                         }
212
213                         set {
214                                 if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
215                                         throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
216                                 }
217
218                                 ViewState ["FirstDayOfWeek"] = value;
219                         }
220                 }
221
222 #if ONLY_1_1
223                 [Bindable(true)]
224 #endif          
225                 [DefaultValue("&gt;")]
226 #if NET_2_0
227                 [Localizable (true)]
228 #endif
229                 [WebSysDescription ("")]
230                 [WebCategory ("Appearance")]
231                 public string NextMonthText {
232                         get {
233                                 return ViewState.GetString ("NextMonthText", "&gt;");
234                         }
235
236                         set {
237                                 ViewState ["NextMonthText"] = value;
238                         }
239                 }
240
241 #if ONLY_1_1
242                 [Bindable(true)]
243 #endif          
244                 [DefaultValue(NextPrevFormat.CustomText)]
245                 [WebSysDescription ("")]
246                 [WebCategory ("Appearance")]
247                 public NextPrevFormat NextPrevFormat {
248                         get {
249                                 return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
250                         }
251
252                         set {
253                                 if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
254                                         throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
255                                 }
256
257                                 ViewState ["NextPrevFormat"] = value;
258                         }
259                 }
260
261                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
262                 [NotifyParentProperty(true)]
263                 [PersistenceMode(PersistenceMode.InnerProperty)]
264                 [WebSysDescription ("")]
265                 [WebCategory ("Style")]
266                 public TableItemStyle NextPrevStyle {
267                         get {
268                                 if (nextPrevStyle == null) {
269                                         nextPrevStyle = new TableItemStyle ();
270                                         if (IsTrackingViewState)
271                                                 nextPrevStyle.TrackViewState ();
272                                 }
273
274                                 return nextPrevStyle;
275                         }
276                 }
277
278                 [DefaultValue(null)]
279                 [NotifyParentProperty(true)]
280                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
281                 [PersistenceMode(PersistenceMode.InnerProperty)]
282                 [WebSysDescription ("")]
283                 [WebCategory ("Style")]
284                 public TableItemStyle OtherMonthDayStyle {
285                         get {
286                                 if (otherMonthDayStyle == null) {
287                                         otherMonthDayStyle = new TableItemStyle ();
288                                         if (IsTrackingViewState)
289                                                 otherMonthDayStyle.TrackViewState ();
290                                 }
291
292                                 return otherMonthDayStyle;
293                         }
294                 }
295
296 #if ONLY_1_1
297                 [Bindable(true)]
298 #endif          
299                 [DefaultValue("&lt;")]
300 #if NET_2_0
301                 [Localizable (true)]
302 #endif
303                 [WebSysDescription ("")]
304                 [WebCategory ("Appearance")]
305                 public string PrevMonthText {
306                         get {
307                                 return ViewState.GetString ("PrevMonthText", "&lt;");
308                         }
309
310                         set {
311                                 ViewState ["PrevMonthText"] = value;
312                         }
313                 }
314
315 #if NET_2_0
316                 [Bindable(true, BindingDirection.TwoWay)]
317 #else
318                 [Bindable(true)]
319 #endif          
320                 [DefaultValue("1/1/0001 12:00:00 AM")]
321                 [WebSysDescription ("")]
322                 [WebCategory ("Appearance")]
323                 public DateTime SelectedDate {
324                         get {
325                                 if (SelectedDates.Count > 0)
326                                         return SelectedDates [0];
327
328                                 return DateTime.MinValue;
329                         }
330
331                         set {
332                                 SelectedDates.SelectRange (value, value);
333                         }
334                 }
335
336                 [Browsable(false)]
337                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
338                 [WebSysDescription ("")]
339                 [WebCategory ("Appearance")]
340                 public SelectedDatesCollection SelectedDates {
341                         get {
342                                 if (dateList == null)
343                                         dateList = new ArrayList ();
344
345                                 if (selectedDatesCollection == null)
346                                         selectedDatesCollection = new SelectedDatesCollection (dateList);
347
348                                 return selectedDatesCollection;
349                         }
350                 }
351
352                 [DefaultValue(null)]
353                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
354                 [NotifyParentProperty(true)]
355                 [PersistenceMode(PersistenceMode.InnerProperty)]
356                 [WebSysDescription ("")]
357                 [WebCategory ("Style")]
358                 public TableItemStyle SelectedDayStyle {
359                         get {
360                                 if (selectedDayStyle == null) {
361                                         selectedDayStyle = new TableItemStyle ();
362                                         if (IsTrackingViewState)
363                                                 selectedDayStyle.TrackViewState ();
364                                 }
365
366                                 return selectedDayStyle;
367                         }
368                 }
369
370 #if ONLY_1_1
371                 [Bindable(true)]
372 #endif          
373                 [DefaultValue(CalendarSelectionMode.Day)]
374                 [WebSysDescription ("")]
375                 [WebCategory ("Behavior")]
376                 public CalendarSelectionMode SelectionMode {
377                         get {
378                                 return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
379                         }
380
381                         set {
382                                 if (value != CalendarSelectionMode.Day  && value != CalendarSelectionMode.DayWeek &&
383                                         value != CalendarSelectionMode.DayWeekMonth  && value != CalendarSelectionMode.None) {
384                                         throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
385                                 }
386                                 ViewState ["SelectionMode"] = value;
387                         }
388                 }
389
390 #if ONLY_1_1
391                 [Bindable(true)]
392 #endif          
393                 [DefaultValue("&gt;&gt;")]
394 #if NET_2_0
395                 [Localizable (true)]
396 #endif
397                 [WebSysDescription ("")]
398                 [WebCategory ("Appearance")]
399                 public string SelectMonthText {
400                         get {
401                                 return ViewState.GetString ("SelectMonthText", "&gt;&gt;");
402                         }
403
404                         set {
405                                 ViewState ["SelectMonthText"] = value;
406                         }
407                 }
408
409                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
410                 [NotifyParentProperty(true)]
411                 [PersistenceMode(PersistenceMode.InnerProperty)]
412                 [WebSysDescription ("")]
413                 [WebCategory ("Style")]
414                 public TableItemStyle SelectorStyle {
415                         get {
416                                 if (selectorStyle == null) {
417                                          selectorStyle = new TableItemStyle ();
418                                         if (IsTrackingViewState)
419                                                 selectorStyle.TrackViewState ();
420                                 }
421
422                                 return selectorStyle;
423                         }
424                 }
425
426 #if ONLY_1_1
427                 [Bindable(true)]
428 #endif          
429                 [DefaultValue("&gt;")]
430 #if NET_2_0             
431                 [Localizable (true)]
432 #endif          
433                 [WebSysDescription ("")]
434                 [WebCategory ("Appearance")]
435                 public string SelectWeekText {
436                         get {
437                                 return ViewState.GetString ("SelectWeekText", "&gt;");
438                         }
439
440                         set {
441                                 ViewState ["SelectWeekText"] = value;
442                         }
443                 }
444
445 #if ONLY_1_1
446                 [Bindable(true)]
447 #endif          
448                 [DefaultValue(true)]
449                 [WebSysDescription ("")]
450                 [WebCategory ("Appearance")]
451                 public bool ShowDayHeader {
452                         get {
453                                 return ViewState.GetBool ("ShowDayHeader", true);
454                         }
455
456                         set {
457                                 ViewState ["ShowDayHeader"] = value;
458                         }
459                 }
460
461 #if ONLY_1_1
462                 [Bindable(true)]
463 #endif          
464                 [DefaultValue(false)]
465                 [WebSysDescription ("")]
466                 [WebCategory ("Appearance")]
467                 public bool ShowGridLines {
468                         get {
469                                 return ViewState.GetBool ("ShowGridLines", false);
470                         }
471
472                         set {
473                                 ViewState ["ShowGridLines"] = value;
474                         }
475                 }
476
477 #if ONLY_1_1
478                 [Bindable(true)]
479 #endif          
480                 [DefaultValue(true)]
481                 [WebSysDescription ("")]
482                 [WebCategory ("Appearance")]
483                 public bool ShowNextPrevMonth {
484                         get {
485                                 return ViewState.GetBool ("ShowNextPrevMonth", true);
486                         }
487
488                         set {
489                                 ViewState ["ShowNextPrevMonth"] = value;
490                         }
491                 }
492
493 #if ONLY_1_1
494                 [Bindable(true)]
495 #endif          
496                 [DefaultValue(true)]
497                 [WebSysDescription ("")]
498                 [WebCategory ("Appearance")]
499                 public bool ShowTitle {
500                         get {
501                                 return ViewState.GetBool ("ShowTitle", true);
502                         }
503
504                         set {
505                                 ViewState ["ShowTitle"] = value;
506                         }
507                 }
508
509 #if ONLY_1_1
510                 [Bindable(true)]
511 #endif          
512                 [DefaultValue(TitleFormat.MonthYear)]
513                 [WebSysDescription ("")]
514                 [WebCategory ("Appearance")]
515                 public TitleFormat TitleFormat {
516                         get {
517                                 return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
518                         }
519
520                         set {
521                                 if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
522                                         throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
523                                 }
524
525                                 ViewState ["TitleFormat"] = value;
526                         }
527                 }
528
529                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
530                 [NotifyParentProperty(true)]
531                 [PersistenceMode(PersistenceMode.InnerProperty)]
532                 [WebSysDescription ("")]
533                 [WebCategory ("Style")]
534                 public TableItemStyle TitleStyle {
535                         get {
536                                 if (titleStyle == null) {
537                                         titleStyle = new TableItemStyle ();
538                                         if (IsTrackingViewState)
539                                                 titleStyle.TrackViewState ();
540                                 }
541
542                                 return titleStyle;
543                         }
544                 }
545
546                 [DefaultValue(null)]
547                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
548                 [NotifyParentProperty(true)]
549                 [PersistenceMode(PersistenceMode.InnerProperty)]
550                 [WebSysDescription ("")]
551                 [WebCategory ("Style")]
552                 public TableItemStyle TodayDayStyle {
553                         get {
554                                 if (todayDayStyle == null) {
555                                         todayDayStyle = new TableItemStyle ();
556                                         if (IsTrackingViewState)
557                                                 todayDayStyle.TrackViewState ();
558                                 }
559
560                                 return todayDayStyle;
561                         }
562                 }
563
564 #if ONLY_1_1
565                 [Bindable(true)]
566 #endif          
567                 [Browsable(false)]
568                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
569                 [WebSysDescription ("")]
570                 [WebCategory ("Style")]
571                 public DateTime TodaysDate {
572                         get {
573                                 object obj = ViewState ["TodaysDate"];
574
575                                 if (obj != null)
576                                         return (DateTime) obj;
577
578                                 return today;
579                         }
580
581                         set {
582                                 ViewState ["TodaysDate"] = value.Date;
583                         }
584                 }
585
586 #if NET_2_0
587                 [DefaultValue (true)]
588                 [WebSysDescription ("")]
589                 [WebCategoryAttribute ("Accessibility")]
590                 public virtual bool UseAccessibleHeader 
591                 {
592                         get {
593                                 return ViewState.GetBool ("UseAccessibleHeader", true);
594                         }
595                         set {
596                                 ViewState ["UseAccessibleHeader"] = value;
597                         }
598                 }
599 #endif          
600
601                 [Bindable(true)]
602                 [DefaultValue("1/1/0001 12:00:00 AM")]
603                 [WebSysDescription ("")]
604                 [WebCategory ("Style")]
605                 public DateTime VisibleDate {
606                         get {
607                                 object obj = ViewState ["VisibleDate"];
608
609                                 if (obj != null)
610                                         return (DateTime) obj;
611
612                                 return DateTime.MinValue;
613                         }
614
615                         set {
616                                 ViewState ["VisibleDate"] = value.Date;
617                         }
618                 }
619
620                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
621                 [NotifyParentProperty(true)]
622                 [PersistenceMode(PersistenceMode.InnerProperty)]
623                 [WebSysDescription ("")]
624                 [WebCategory ("Style")]
625                 public TableItemStyle WeekendDayStyle {
626                         get {
627                                 if (weekendDayStyle == null) {
628                                         weekendDayStyle = new TableItemStyle ();
629                                         if (IsTrackingViewState)
630                                                 weekendDayStyle.TrackViewState ();
631                                 }
632
633                                 return weekendDayStyle;
634                         }
635                 }
636
637
638                 // Private properties
639                 private DateTime DisplayDate {
640                         get {
641                                 DateTime dateTime = VisibleDate;
642                                 if (dateTime == DateTime.MinValue) // If visibledate is still the default value
643                                         dateTime = TodaysDate;
644
645                                 return dateTime;
646                         }
647                 }
648
649                 private DayOfWeek DisplayFirstDayOfWeek {
650                         get {
651                                 if (FirstDayOfWeek != FirstDayOfWeek.Default)
652                                         return (DayOfWeek)  FirstDayOfWeek;
653
654                                 return (DayOfWeek) dateInfo.FirstDayOfWeek;
655                         }
656                 }
657
658                 protected override ControlCollection CreateControlCollection ()
659                 {
660                         return base.CreateControlCollection ();
661                 }
662
663                 protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
664                 {
665                         if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
666                                 return true;
667
668                         return false;
669                 }
670                 
671 #if NET_2_0
672                 void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
673                 {
674                         RaisePostBackEvent (eventArgument);
675                 }
676
677                 protected virtual void RaisePostBackEvent (string arg)
678 #else
679                 void IPostBackEventHandler.RaisePostBackEvent (string arg)
680 #endif
681                 {
682                         if (arg.Length < 1)
683                                 return;
684
685                         if (arg[0] == 'V') { // Goes to Next or Previous month
686                                 DateTime prev = VisibleDate;
687                                 int days = Int32.Parse (arg.Substring (1));
688                                 DateTime dt = GetGlobalCalendar().AddDays (dateZenith, days);
689                                 VisibleDate = dt;
690                                 OnVisibleMonthChanged (VisibleDate, prev);
691                                 return;
692                         }
693
694                         if (arg[0] == 'R') { // Selects a range of dates
695                                 string num, date, days;
696                                 num = arg.Substring (1);
697                                 days = num.Substring (num.Length - 2, 2);
698                                 date = num.Substring (0, num.Length - 2);
699                                 DateTime d = GetGlobalCalendar().AddDays (dateZenith, Int32.Parse (date));
700                                 SelectedDates.SelectRange (d, d.AddDays (Int32.Parse (days)));
701                                 OnSelectionChanged ();
702                                 return;
703                         }
704
705                         // Selects a single day
706                         int daysFromZenith = Int32.Parse (arg);
707                         DateTime day = GetGlobalCalendar().AddDays (dateZenith, daysFromZenith);
708                         SelectedDates.SelectRange (day, day);
709                         OnSelectionChanged ();
710                 }
711
712                 protected override void LoadViewState (object savedState)
713                 {
714                         object [] states = (object []) savedState;
715
716                         if (states [0] != null)
717                                  base.LoadViewState (states [0]);
718
719                         if (states [1] != null)
720                                 DayHeaderStyle.LoadViewState (states [1]);
721
722                         if (states [2] != null)
723                                 DayStyle.LoadViewState (states [2]);
724
725                         if (states [3] != null)
726                                 NextPrevStyle.LoadViewState (states [3]);
727
728                         if (states [4] != null)
729                                 OtherMonthDayStyle.LoadViewState (states [4]);
730
731                         if (states [5] != null)
732                                 SelectedDayStyle.LoadViewState (states [5]);
733
734                         if (states [6] != null)
735                                 TitleStyle.LoadViewState (states [6]);
736
737                         if (states [7] != null)
738                                 TodayDayStyle.LoadViewState (states [7]);
739
740                         if (states [8] != null)
741                                 SelectorStyle.LoadViewState (states [8]);
742
743                         if (states [9] != null)
744                                 WeekendDayStyle.LoadViewState (states [9]);
745
746                         ArrayList array = (ArrayList) ViewState ["SelectedDates"];
747                         if (array != null) {
748                                 dateList = array;
749                                 selectedDatesCollection = new SelectedDatesCollection (dateList);
750                         }
751                 }
752
753                 protected virtual void OnDayRender (TableCell cell, CalendarDay day)
754                 {
755                         DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
756                         if (eh != null)
757 #if NET_2_0
758                                 if (Page != null)
759                                         eh (this, new DayRenderEventArgs (cell, day, Page.ClientScript.GetPostBackClientHyperlink (this, GetDaysFromZenith (day.Date).ToString (), true)));
760                                 else
761 #endif
762                                 eh (this, new DayRenderEventArgs (cell, day));
763                 }
764
765 #if NET_2_0
766                 protected internal
767 #else           
768                 protected
769 #endif          
770                 override void OnPreRender (EventArgs e)
771                 {
772                         base.OnPreRender (e);
773                 }
774
775                 protected virtual void OnSelectionChanged ()
776                 {
777                         EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
778                         if (eh != null)
779                                 eh (this, EventArgs.Empty);
780                 }
781
782                 protected virtual void OnVisibleMonthChanged (DateTime newDate,  DateTime previousDate)
783                 {
784                         MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
785                         if (eh != null)
786                                 eh (this, new MonthChangedEventArgs (newDate, previousDate));
787                 }
788
789 #if NET_2_0
790                 protected internal
791 #else           
792                 protected
793 #endif          
794                 override void Render (HtmlTextWriter writer)
795                 {
796                         TableStyle ts = new TableStyle ();
797                         ts.CellSpacing = CellSpacing;
798                         ts.CellPadding = CellPadding;
799                         ts.BorderWidth = 1;
800                         if (ControlStyleCreated)
801                                 ts.CopyFrom (ControlStyle);
802                         if (ShowGridLines)
803                                 ts.GridLines = GridLines.Both;
804                         ts.AddAttributesToRender (writer);
805                         writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID);
806                         writer.RenderBeginTag (HtmlTextWriterTag.Table);
807
808 #if NET_2_0
809                         if (Caption != "")
810                                 WriteCaption (writer);
811 #endif
812
813                         if (ShowTitle)
814                                 WriteTitle (writer);
815
816                         if (ShowDayHeader)
817                                 WriteDayHeader (writer);
818
819                         WriteDays (writer);
820
821                         writer.RenderEndTag ();
822                 }
823
824                 protected override object SaveViewState ()
825                 {
826                         object [] states = new object [10];
827
828                         if (dayHeaderStyle != null)
829                                 states [1] = dayHeaderStyle.SaveViewState ();
830
831                         if (dayStyle != null)
832                                 states [2] = dayStyle.SaveViewState ();
833
834                         if (nextPrevStyle != null)
835                                 states [3] = nextPrevStyle.SaveViewState ();
836
837                         if (otherMonthDayStyle != null)
838                                 states [4] = otherMonthDayStyle.SaveViewState ();
839
840                         if (selectedDayStyle != null)
841                                 states [5] = selectedDayStyle.SaveViewState ();
842
843                         if (titleStyle != null)
844                                 states [6] = titleStyle.SaveViewState ();
845
846                         if (todayDayStyle != null)
847                                 states [7] =todayDayStyle.SaveViewState ();
848
849                         if (selectorStyle != null)
850                                 states [8] = selectorStyle.SaveViewState ();
851
852                         if (weekendDayStyle != null)
853                                 states [9] = weekendDayStyle.SaveViewState ();
854
855                         if (SelectedDates.Count > 0) {
856                                 ViewState ["SelectedDates"] = dateList;
857                         }
858
859                         states [0] = base.SaveViewState ();
860
861                         for (int i = 0; i < states.Length; i++) {
862                                 if (states [i] != null) {
863                                         return states;
864                                 }
865                         }
866
867                         return null;
868                 }
869
870                 protected override void TrackViewState ()
871                 {
872                         base.TrackViewState ();
873
874                         if (dayHeaderStyle != null)
875                                 dayHeaderStyle.TrackViewState ();
876
877                         if (dayStyle != null)
878                                 dayStyle.TrackViewState ();
879
880                         if (nextPrevStyle != null)
881                                 nextPrevStyle.TrackViewState ();
882
883                         if (otherMonthDayStyle != null)
884                                 otherMonthDayStyle.TrackViewState ();
885
886                         if (selectedDayStyle != null)
887                                 selectedDayStyle.TrackViewState ();
888
889                         if (titleStyle != null)
890                                 titleStyle.TrackViewState ();
891
892                         if (todayDayStyle  != null)
893                                 todayDayStyle.TrackViewState ();
894
895                         if (selectorStyle != null)
896                                 selectorStyle.TrackViewState ();
897
898                         if (weekendDayStyle != null)
899                                 weekendDayStyle.TrackViewState ();
900                 }
901
902                 //
903                 // Private methods
904                 //
905                 private void WriteDayHeader (HtmlTextWriter writer)
906                 {
907                         int i, first;
908                         string dayName;
909                         i = first = (int) (DisplayFirstDayOfWeek);
910                         TableCell cell;
911
912
913                         writer.RenderBeginTag (HtmlTextWriterTag.Tr);
914
915                         if (SelectionMode == CalendarSelectionMode.DayWeek) {
916                                 cell = new TableCell();
917                                 cell.HorizontalAlign = HorizontalAlign.Center;
918                                 cell.ApplyStyle (DayHeaderStyle);
919
920                                 // Empty Cell
921                                 cell.RenderBeginTag (writer);
922                                 cell.RenderEndTag (writer);
923                         } else {
924                                 if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
925                                         TableCell selector = new TableCell ();
926                                         selector.ApplyStyle (SelectorStyle);
927                                         selector.HorizontalAlign = HorizontalAlign.Center;
928
929                                         DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
930                                         int days =  DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
931
932                                         selector.RenderBeginTag (writer);
933                                         writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, Enabled));
934                                         selector.RenderEndTag (writer);
935                                 }
936                         }
937
938                         while (true) {
939                                 DayOfWeek dayOfWeek = (DayOfWeek) i;
940                                 dayName = dateInfo.GetDayName (dayOfWeek);
941
942 #if NET_2_0
943                                 if (UseAccessibleHeader) {
944                                         writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
945                                         writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col", false);
946                                         cell = new TableHeaderCell();
947                                 }
948                                 else
949 #endif
950                                         cell = new TableCell();
951
952                                 cell.HorizontalAlign = HorizontalAlign.Center;
953                                 cell.ApplyStyle (DayHeaderStyle);
954
955                                 cell.RenderBeginTag (writer);
956
957                                 switch (DayNameFormat) {
958                                 case DayNameFormat.FirstLetter:
959                                         dayName = dayName.Substring (0, 1);
960                                         break;
961                                 case DayNameFormat.FirstTwoLetters:
962                                         dayName = dayName.Substring (0, 2);
963                                         break;
964 #if NET_2_0
965                                 case DayNameFormat.Shortest:
966                                         dayName = dateInfo.GetShortestDayName (dayOfWeek);
967                                         break;
968 #endif
969                                 case DayNameFormat.Full:
970                                         break;
971                                 case DayNameFormat.Short:
972                                 default:
973                                         dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
974                                         break;
975                                 }
976
977                                 writer.Write (dayName);
978                                 cell.RenderEndTag (writer);
979
980                                 if (i >= daysInAWeek - 1) {
981                                         i = 0;
982                                 }
983                                 else {
984                                         i++;
985                                 }
986                                 if (i == first)
987                                         break;
988                         }
989
990                         writer.RenderEndTag ();
991                 }
992
993                 private void WriteDay (DateTime date, HtmlTextWriter writer)
994                 {                       
995                         TableItemStyle style = new TableItemStyle ();
996                         TableCell cell = new TableCell ();
997
998                         CalendarDay day = new CalendarDay (date,
999                                 IsWeekEnd (date.DayOfWeek),
1000                                 date == TodaysDate, SelectedDates.Contains (date),
1001                                 GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
1002                                 date.Day.ToString ());
1003
1004                         day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
1005                         cell.HorizontalAlign = HorizontalAlign.Center;
1006                         cell.Width = Unit.Percentage (GetCellWidth ());
1007
1008                         LiteralControl lit = new LiteralControl (day.DayNumberText);
1009                         cell.Controls.Add (lit);
1010
1011                         OnDayRender (cell, day);
1012                                         
1013                         if (dayStyle != null && !dayStyle.IsEmpty) {
1014                                 style.CopyFrom (dayStyle);
1015                         }
1016
1017                         if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
1018                                 style.CopyFrom (weekendDayStyle);
1019                         }
1020
1021                         if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
1022                                 style.CopyFrom (todayDayStyle);
1023                         }
1024
1025                         if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
1026                                 style.CopyFrom (otherMonthDayStyle);
1027                         }
1028
1029                         if (day.IsSelected && Enabled) {
1030                                 style.BackColor = Color.Silver;
1031                                 style.ForeColor = Color.White;
1032                                 if (selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
1033                                         style.CopyFrom (selectedDayStyle);
1034                                 }
1035                         }
1036
1037                         cell.ApplyStyle (style);
1038
1039                         lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
1040                                               cell.ForeColor, day.IsSelectable && Enabled);
1041
1042                         cell.RenderControl (writer);
1043                 }
1044
1045                 private void WriteDays (HtmlTextWriter writer)
1046                 {
1047                         DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
1048                         DateTime lastDate;
1049                         TableCell selectorCell = null;
1050                         int n;
1051
1052                         // Goes backwards until we find the date of that is begining of the week
1053                         for (n = 0; n < daysInAWeek; n++) {
1054                                 if (date.DayOfWeek == DisplayFirstDayOfWeek)
1055                                         break;
1056
1057                                 date = GetGlobalCalendar().AddDays (date, -1);
1058                         }
1059                         /* if the start date is the first day of the week, we need to shift backward one more week */
1060                         if (n == 0)
1061                                 date = GetGlobalCalendar().AddDays (date, -1 * daysInAWeek);
1062
1063                         lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
1064
1065                         while (true) {
1066                                 writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1067
1068                                 if (HasWeekSelectors (SelectionMode)) { // Week selector
1069                                         if (selectorCell == null) {
1070                                                 selectorCell = new TableCell ();
1071                                                 selectorCell.ApplyStyle (SelectorStyle);
1072                                                 selectorCell.HorizontalAlign = HorizontalAlign.Center;
1073                                                 selectorCell.Width = Unit.Percentage (GetCellWidth ());
1074                                         }
1075
1076                                         selectorCell.RenderBeginTag (writer);
1077                                         writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, Enabled));
1078                                         selectorCell.RenderEndTag (writer);
1079                                 }
1080
1081                                 for (int i = 0; i < daysInAWeek; i++) {
1082                                         WriteDay (date, writer);
1083                                         date = GetGlobalCalendar().AddDays (date, 1);
1084                                 }
1085
1086                                 writer.RenderEndTag ();
1087                                 if (date >= lastDate)
1088                                         break;
1089                         }
1090                 }
1091
1092                 private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
1093                 {
1094                         StringBuilder str = new StringBuilder ();
1095                         Color clr;
1096                         hasLink = (Page != null && hasLink == true) ? true : false;
1097
1098                         if (hasLink) {
1099                                 str.Append ("<a href=\"");
1100 #if NET_2_0
1101                                 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg, true));
1102 #else
1103                                 str.Append (Page.ClientScript.GetPostBackClientHyperlink (this, arg));
1104 #endif                  
1105                                 str.Append ('\"');
1106                         
1107
1108                                 if (!foreColor.IsEmpty) {
1109                                         clr = foreColor;
1110                                 } else {
1111                                         if (ForeColor.IsEmpty)
1112                                                 clr = Color.Black;
1113                                         else
1114                                                 clr = ForeColor;
1115                                 }
1116
1117                                 str.Append (" style=\"color:" + ColorTranslator.ToHtml (clr));
1118                                 str.Append ("\">");
1119                                 str.Append (text);
1120                                 str.Append ("</a>");
1121                         }
1122                         else 
1123                                 str.Append (text);
1124
1125                         return str.ToString ();
1126                 }
1127
1128                 private int GetDaysFromZenith (DateTime date)
1129                 {
1130                         TimeSpan span =  date.Subtract (dateZenith);
1131                         return span.Days;
1132                 }
1133
1134 #if NET_2_0
1135                 void WriteCaption (HtmlTextWriter writer)
1136                 {
1137                         if (CaptionAlign != TableCaptionAlign.NotSet)
1138                                 writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
1139
1140                         writer.RenderBeginTag (HtmlTextWriterTag.Caption);
1141                         writer.Write (Caption);
1142                         writer.RenderEndTag ();
1143                 }
1144 #endif
1145
1146                 private void WriteTitle (HtmlTextWriter writer)
1147                 {
1148                         TableCell cellNextPrev = null;
1149                         TableCell titleCell = new TableCell ();
1150                         Table tableTitle = new Table ();
1151
1152                         writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1153
1154                         titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
1155
1156                         if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
1157                                 titleCell.BackColor = titleStyle.BackColor;
1158                         } else {
1159                                 titleCell.BackColor = Color.Silver;
1160                         }
1161
1162                         titleCell.RenderBeginTag (writer);
1163
1164                         // Table
1165                         tableTitle.Width =  Unit.Percentage (100);
1166                         if (titleStyle != null && !titleStyle.IsEmpty) {
1167                                 tableTitle.ApplyStyle (titleStyle);
1168                         }
1169
1170                         tableTitle.RenderBeginTag (writer);
1171                         writer.RenderBeginTag (HtmlTextWriterTag.Tr);
1172
1173                         if (ShowNextPrevMonth) { // Previous Table Data
1174                                 cellNextPrev = new TableCell ();
1175                                 cellNextPrev.ApplyStyle (nextPrevStyle);
1176                                 cellNextPrev.Width = Unit.Percentage (15);
1177
1178                                 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
1179                                 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1180                                 cellNextPrev.RenderBeginTag (writer);
1181                                 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, Enabled));
1182                                 cellNextPrev.RenderEndTag (writer);
1183                         }
1184
1185                         // Current Month Table Data
1186                         {
1187                                 string str;
1188                                 TableCell cellMonth = new TableCell ();
1189                                 cellMonth.Width = Unit.Percentage (70);
1190                                 cellMonth.HorizontalAlign = HorizontalAlign.Center;
1191
1192                                 cellMonth.RenderBeginTag (writer);
1193
1194                                 if (TitleFormat == TitleFormat.MonthYear)
1195                                         str = DisplayDate.ToString (dateInfo.YearMonthPattern, dateInfo);
1196                                 else
1197                                         str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
1198
1199                                 writer.Write (str);
1200                                 cellMonth.RenderEndTag (writer);
1201                         }
1202
1203                         if (ShowNextPrevMonth) { // Next Table Data
1204                                 DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
1205                                 date = GetGlobalCalendar ().AddDays (date, -date.Day + 1);
1206
1207                                 cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
1208                                 cellNextPrev.RenderBeginTag (writer);
1209                                 writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, Enabled));
1210                                 cellNextPrev.RenderEndTag (writer);
1211                         }
1212
1213                         writer.RenderEndTag ();
1214                         tableTitle.RenderEndTag (writer);
1215                         titleCell.RenderEndTag (writer);
1216                         writer.RenderEndTag (); //tr
1217
1218                 }
1219
1220
1221                 private string GetNextPrevFormatText (DateTime date, bool next)
1222                 {
1223                         string text;
1224                         switch (NextPrevFormat) {
1225                                 case NextPrevFormat.FullMonth:
1226                                         text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
1227                                         break;
1228                                 case NextPrevFormat.ShortMonth:
1229                                         text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
1230                                         break;
1231                                 case NextPrevFormat.CustomText:
1232                                 default:
1233                                         text = ((next) ? NextMonthText : PrevMonthText);
1234                                         break;
1235                         }
1236
1237                         return text;
1238                 }
1239
1240                 private bool IsWeekEnd (DayOfWeek day)
1241                 {
1242                         return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
1243                 }
1244
1245                 private double GetCellWidth ()
1246                 {
1247                         return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
1248                 }
1249
1250                 private System.Globalization.Calendar GetGlobalCalendar ()
1251                 {
1252                         return DateTimeFormatInfo.CurrentInfo.Calendar;
1253                 }
1254
1255                 [WebSysDescription ("")]
1256                 [WebCategory ("Action")]
1257                 public event DayRenderEventHandler DayRender {
1258                         add {
1259                                 Events.AddHandler (DayRenderEvent, value);
1260                         }
1261                         remove {
1262                                 Events.RemoveHandler (DayRenderEvent, value);
1263                         }
1264                 }
1265
1266                 [WebSysDescription ("")]
1267                 [WebCategory ("Action")]
1268                 public event EventHandler SelectionChanged {
1269                         add {
1270                                 Events.AddHandler (SelectionChangedEvent, value);
1271                         }
1272                         remove {
1273                                 Events.RemoveHandler (SelectionChangedEvent, value);
1274                         }
1275                 }
1276
1277                 [WebSysDescription ("")]
1278                 [WebCategory ("Action")]
1279                 public event MonthChangedEventHandler VisibleMonthChanged {
1280                         add {
1281                                 Events.AddHandler (VisibleMonthChangedEvent, value);
1282                         }
1283                         remove {
1284                                 Events.RemoveHandler (VisibleMonthChangedEvent, value);
1285                         }
1286                 }
1287         }
1288 }
1289