Calendar.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. //
  2. // System.Web.UI.WebControls.Calendar.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez ([email protected])
  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. using System.Globalization;
  32. using System.Collections;
  33. using System.ComponentModel;
  34. using System.Drawing;
  35. namespace System.Web.UI.WebControls {
  36. [DataBindingHandler("System.Web.UI.Design.WebControls.CalendarDataBindingHandler, " + Consts.AssemblySystem_Design)]
  37. [DefaultEvent("SelectionChanged")]
  38. [DefaultProperty("SelectedDate")]
  39. [Designer("System.Web.UI.Design.WebControls.CalendarDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. #if NET_2_0
  41. [ControlValueProperty ("SelectedDate", "1/1/0001 12:00:00 AM")]
  42. #endif
  43. public class Calendar : WebControl, IPostBackEventHandler {
  44. private TableItemStyle dayHeaderStyle;
  45. private TableItemStyle dayStyle;
  46. private TableItemStyle nextPrevStyle;
  47. private TableItemStyle otherMonthDayStyle;
  48. private TableItemStyle selectedDayStyle;
  49. private TableItemStyle titleStyle;
  50. private TableItemStyle todayDayStyle;
  51. private TableItemStyle selectorStyle;
  52. private TableItemStyle weekendDayStyle;
  53. private DateTimeFormatInfo dateInfo;
  54. private SelectedDatesCollection selectedDatesCollection;
  55. private ArrayList dateList;
  56. private static DateTime dateZenith = new DateTime (2000, 1,1);
  57. private const int daysInAWeek = 7;
  58. private static readonly object DayRenderEvent = new object ();
  59. private static readonly object SelectionChangedEvent = new object ();
  60. private static readonly object VisibleMonthChangedEvent = new object ();
  61. public Calendar ()
  62. {
  63. dayHeaderStyle = null;
  64. dayStyle = null;
  65. nextPrevStyle = null;
  66. selectedDayStyle = null;
  67. titleStyle = null;
  68. todayDayStyle = null;
  69. dateInfo = new DateTimeFormatInfo ();
  70. selectedDatesCollection = null;
  71. dateList = null;
  72. }
  73. #if NET_2_0
  74. [Localizable (true)]
  75. [DefaultValue ("")]
  76. [WebSysDescription ("")]
  77. [WebCategoryAttribute ("Appearance")]
  78. public virtual string Caption
  79. {
  80. get {
  81. return ViewState.GetString ("Caption", "");
  82. }
  83. set {
  84. ViewState["Caption"] = value;
  85. }
  86. }
  87. [DefaultValue (TableCaptionAlign.NotSet)]
  88. [WebSysDescription ("")]
  89. [WebCategoryAttribute ("Accessibility")]
  90. public virtual TableCaptionAlign CaptionAlign
  91. {
  92. get {
  93. return (TableCaptionAlign)ViewState.GetInt ("CaptionAlign", (int)TableCaptionAlign.NotSet);
  94. }
  95. set {
  96. ViewState ["CaptionAlign"] = value;
  97. }
  98. }
  99. #endif
  100. #if ONLY_1_1
  101. [Bindable(true)]
  102. #endif
  103. [DefaultValue(2)]
  104. [WebSysDescription ("")]
  105. [WebCategory ("Layout")]
  106. public int CellPadding {
  107. get {
  108. return ViewState.GetInt ("CellPadding", 2);
  109. }
  110. set {
  111. if (value < -1)
  112. throw new ArgumentOutOfRangeException ("The specified cell padding is less than -1.");
  113. ViewState ["CellPadding"] = value;
  114. }
  115. }
  116. #if ONLY_1_1
  117. [Bindable(true)]
  118. #endif
  119. [DefaultValue(0)]
  120. [WebSysDescription ("")]
  121. [WebCategory ("Layout")]
  122. public int CellSpacing {
  123. get {
  124. return ViewState.GetInt ("CellSpacing", 0);
  125. }
  126. set {
  127. if (value < -1)
  128. throw new ArgumentOutOfRangeException ("The specified cell spacing is less than -1");
  129. ViewState ["CellSpacing"] = value;
  130. }
  131. }
  132. [PersistenceMode(PersistenceMode.InnerProperty)]
  133. [NotifyParentProperty(true)]
  134. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  135. [WebSysDescription ("")]
  136. [WebCategory ("Style")]
  137. public TableItemStyle DayHeaderStyle {
  138. get {
  139. if (dayHeaderStyle == null)
  140. dayHeaderStyle = new TableItemStyle ();
  141. if (IsTrackingViewState)
  142. dayHeaderStyle.TrackViewState ();
  143. return dayHeaderStyle;
  144. }
  145. }
  146. #if ONLY_1_1
  147. [Bindable(true)]
  148. #endif
  149. [DefaultValue(DayNameFormat.Short)]
  150. [WebSysDescription ("")]
  151. [WebCategory ("Appearance")]
  152. public DayNameFormat DayNameFormat {
  153. get {
  154. return (DayNameFormat) ViewState.GetInt ("DayNameFormat", (int) DayNameFormat.Short);
  155. }
  156. set {
  157. if (value != DayNameFormat.FirstLetter && value != DayNameFormat.FirstTwoLetters &&
  158. value != DayNameFormat.Full && value != DayNameFormat.Short) {
  159. throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values."); }
  160. ViewState ["DayNameFormat"] = value;
  161. }
  162. }
  163. [DefaultValue(null)]
  164. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  165. [NotifyParentProperty(true)]
  166. [PersistenceMode(PersistenceMode.InnerProperty)]
  167. [WebSysDescription ("")]
  168. [WebCategory ("Style")]
  169. public TableItemStyle DayStyle {
  170. get {
  171. if (dayStyle == null)
  172. dayStyle = new TableItemStyle ();
  173. if (IsTrackingViewState)
  174. dayStyle.TrackViewState ();
  175. return dayStyle;
  176. }
  177. }
  178. #if ONLY_1_1
  179. [Bindable(true)]
  180. #endif
  181. [DefaultValue(FirstDayOfWeek.Default)]
  182. [WebSysDescription ("")]
  183. [WebCategory ("Appearance")]
  184. public FirstDayOfWeek FirstDayOfWeek {
  185. get {
  186. return (FirstDayOfWeek) ViewState.GetInt ("FirstDayOfWeek", (int) FirstDayOfWeek.Default);
  187. }
  188. set {
  189. if (value < FirstDayOfWeek.Sunday || value > FirstDayOfWeek.Default) {
  190. throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
  191. }
  192. ViewState ["FirstDayOfWeek"] = value;
  193. }
  194. }
  195. #if ONLY_1_1
  196. [Bindable(true)]
  197. #endif
  198. [DefaultValue("&gt;")]
  199. #if NET_2_0
  200. [Localizable (true)]
  201. #endif
  202. [WebSysDescription ("")]
  203. [WebCategory ("Appearance")]
  204. public string NextMonthText {
  205. get {
  206. return ViewState.GetString ("NextMonthText", "&gt;");
  207. }
  208. set {
  209. ViewState ["NextMonthText"] = value;
  210. }
  211. }
  212. #if ONLY_1_1
  213. [Bindable(true)]
  214. #endif
  215. [DefaultValue(NextPrevFormat.CustomText)]
  216. [WebSysDescription ("")]
  217. [WebCategory ("Appearance")]
  218. public NextPrevFormat NextPrevFormat {
  219. get {
  220. return (NextPrevFormat) ViewState.GetInt ("NextPrevFormat", (int) NextPrevFormat.CustomText);
  221. }
  222. set {
  223. if (value != NextPrevFormat.CustomText && value != NextPrevFormat.ShortMonth && value != NextPrevFormat.FullMonth) {
  224. throw new ArgumentOutOfRangeException ("The specified day name format is not one of the DayNameFormat values.");
  225. }
  226. ViewState ["NextPrevFormat"] = value;
  227. }
  228. }
  229. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  230. [NotifyParentProperty(true)]
  231. [PersistenceMode(PersistenceMode.InnerProperty)]
  232. [WebSysDescription ("")]
  233. [WebCategory ("Style")]
  234. public TableItemStyle NextPrevStyle {
  235. get {
  236. if (nextPrevStyle == null)
  237. nextPrevStyle = new TableItemStyle ();
  238. if (IsTrackingViewState)
  239. nextPrevStyle.TrackViewState ();
  240. return nextPrevStyle;
  241. }
  242. }
  243. [DefaultValue(null)]
  244. [NotifyParentProperty(true)]
  245. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  246. [PersistenceMode(PersistenceMode.InnerProperty)]
  247. [WebSysDescription ("")]
  248. [WebCategory ("Style")]
  249. public TableItemStyle OtherMonthDayStyle {
  250. get {
  251. if (otherMonthDayStyle == null)
  252. otherMonthDayStyle = new TableItemStyle ();
  253. if (IsTrackingViewState)
  254. otherMonthDayStyle.TrackViewState ();
  255. return otherMonthDayStyle;
  256. }
  257. }
  258. #if ONLY_1_1
  259. [Bindable(true)]
  260. #endif
  261. [DefaultValue("&lt;")]
  262. #if NET_2_0
  263. [Localizable (true)]
  264. #endif
  265. [WebSysDescription ("")]
  266. [WebCategory ("Appearance")]
  267. public string PrevMonthText {
  268. get {
  269. return ViewState.GetString ("PrevMonthText", "&lt;");
  270. }
  271. set {
  272. ViewState ["PrevMonthText"] = value;
  273. }
  274. }
  275. #if NET_2_0
  276. [Bindable(true, BindingDirection.TwoWay)]
  277. #else
  278. [Bindable(true)]
  279. #endif
  280. [DefaultValue("1/1/0001 12:00:00 AM")]
  281. [WebSysDescription ("")]
  282. [WebCategory ("Appearance")]
  283. public DateTime SelectedDate {
  284. get {
  285. if (SelectedDates.Count > 0)
  286. return SelectedDates [0];
  287. return DateTime.MinValue;
  288. }
  289. set {
  290. SelectedDates.SelectRange (value, value);
  291. }
  292. }
  293. [Browsable(false)]
  294. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  295. [WebSysDescription ("")]
  296. [WebCategory ("Appearance")]
  297. public SelectedDatesCollection SelectedDates {
  298. get {
  299. if (dateList == null)
  300. dateList = new ArrayList ();
  301. if (selectedDatesCollection == null)
  302. selectedDatesCollection = new SelectedDatesCollection (dateList);
  303. return selectedDatesCollection;
  304. }
  305. }
  306. [DefaultValue(null)]
  307. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  308. [NotifyParentProperty(true)]
  309. [PersistenceMode(PersistenceMode.InnerProperty)]
  310. [WebSysDescription ("")]
  311. [WebCategory ("Style")]
  312. public TableItemStyle SelectedDayStyle {
  313. get {
  314. if (selectedDayStyle == null)
  315. selectedDayStyle = new TableItemStyle ();
  316. if (IsTrackingViewState)
  317. selectedDayStyle.TrackViewState ();
  318. return selectedDayStyle;
  319. }
  320. }
  321. #if ONLY_1_1
  322. [Bindable(true)]
  323. #endif
  324. [DefaultValue(CalendarSelectionMode.Day)]
  325. [WebSysDescription ("")]
  326. [WebCategory ("Behavior")]
  327. public CalendarSelectionMode SelectionMode {
  328. get {
  329. return (CalendarSelectionMode) ViewState.GetInt ("SelectionMode", (int) CalendarSelectionMode.Day);
  330. }
  331. set {
  332. if (value != CalendarSelectionMode.Day && value != CalendarSelectionMode.DayWeek &&
  333. value != CalendarSelectionMode.DayWeekMonth && value != CalendarSelectionMode.None) {
  334. throw new ArgumentOutOfRangeException ("The specified selection mode is not one of the CalendarSelectionMode values.");
  335. }
  336. ViewState ["SelectionMode"] = value;
  337. }
  338. }
  339. #if ONLY_1_1
  340. [Bindable(true)]
  341. #endif
  342. [DefaultValue("&gt;&gt;")]
  343. #if NET_2_0
  344. [Localizable (true)]
  345. #endif
  346. [WebSysDescription ("")]
  347. [WebCategory ("Appearance")]
  348. public string SelectMonthText {
  349. get {
  350. return ViewState.GetString ("SelectMonthText", "&gt;&gt;");
  351. }
  352. set {
  353. ViewState ["SelectMonthText"] = value;
  354. }
  355. }
  356. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  357. [NotifyParentProperty(true)]
  358. [PersistenceMode(PersistenceMode.InnerProperty)]
  359. [WebSysDescription ("")]
  360. [WebCategory ("Style")]
  361. public TableItemStyle SelectorStyle {
  362. get {
  363. if (selectorStyle == null)
  364. selectorStyle = new TableItemStyle ();
  365. if (IsTrackingViewState)
  366. selectorStyle.TrackViewState ();
  367. return selectorStyle;
  368. }
  369. }
  370. #if ONLY_1_1
  371. [Bindable(true)]
  372. #endif
  373. [DefaultValue("&gt;")]
  374. #if NET_2_0
  375. [Localizable (true)]
  376. #endif
  377. [WebSysDescription ("")]
  378. [WebCategory ("Appearance")]
  379. public string SelectWeekText {
  380. get {
  381. return ViewState.GetString ("SelectWeekText", "&gt;");
  382. }
  383. set {
  384. ViewState ["SelectWeekText"] = value;
  385. }
  386. }
  387. #if ONLY_1_1
  388. [Bindable(true)]
  389. #endif
  390. [DefaultValue(true)]
  391. [WebSysDescription ("")]
  392. [WebCategory ("Appearance")]
  393. public bool ShowDayHeader {
  394. get {
  395. return ViewState.GetBool ("ShowDayHeader", true);
  396. }
  397. set {
  398. ViewState ["ShowDayHeader"] = value;
  399. }
  400. }
  401. #if ONLY_1_1
  402. [Bindable(true)]
  403. #endif
  404. [DefaultValue(false)]
  405. [WebSysDescription ("")]
  406. [WebCategory ("Appearance")]
  407. public bool ShowGridLines {
  408. get {
  409. return ViewState.GetBool ("ShowGridLines", false);
  410. }
  411. set {
  412. ViewState ["ShowGridLines"] = value;
  413. }
  414. }
  415. #if ONLY_1_1
  416. [Bindable(true)]
  417. #endif
  418. [DefaultValue(true)]
  419. [WebSysDescription ("")]
  420. [WebCategory ("Appearance")]
  421. public bool ShowNextPrevMonth {
  422. get {
  423. return ViewState.GetBool ("ShowNextPrevMonth", true);
  424. }
  425. set {
  426. ViewState ["ShowNextPrevMonth"] = value;
  427. }
  428. }
  429. #if ONLY_1_1
  430. [Bindable(true)]
  431. #endif
  432. [DefaultValue(true)]
  433. [WebSysDescription ("")]
  434. [WebCategory ("Appearance")]
  435. public bool ShowTitle {
  436. get {
  437. return ViewState.GetBool ("ShowTitle", true);
  438. }
  439. set {
  440. ViewState ["ShowTitle"] = value;
  441. }
  442. }
  443. #if ONLY_1_1
  444. [Bindable(true)]
  445. #endif
  446. [DefaultValue(TitleFormat.MonthYear)]
  447. [WebSysDescription ("")]
  448. [WebCategory ("Appearance")]
  449. public TitleFormat TitleFormat {
  450. get {
  451. return (TitleFormat) ViewState.GetInt ("TitleFormat", (int) TitleFormat.MonthYear);
  452. }
  453. set {
  454. if (value != TitleFormat.Month && value != TitleFormat.MonthYear) {
  455. throw new ArgumentOutOfRangeException ("The specified title format is not one of the TitleFormat values.");
  456. }
  457. ViewState ["TitleFormat"] = value;
  458. }
  459. }
  460. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  461. [NotifyParentProperty(true)]
  462. [PersistenceMode(PersistenceMode.InnerProperty)]
  463. [WebSysDescription ("")]
  464. [WebCategory ("Style")]
  465. public TableItemStyle TitleStyle {
  466. get {
  467. if (titleStyle == null)
  468. titleStyle = new TableItemStyle ();
  469. if (IsTrackingViewState)
  470. titleStyle.TrackViewState ();
  471. return titleStyle;
  472. }
  473. }
  474. [DefaultValue(null)]
  475. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  476. [NotifyParentProperty(true)]
  477. [PersistenceMode(PersistenceMode.InnerProperty)]
  478. [WebSysDescription ("")]
  479. [WebCategory ("Style")]
  480. public TableItemStyle TodayDayStyle {
  481. get {
  482. if (todayDayStyle == null)
  483. todayDayStyle = new TableItemStyle ();
  484. if (IsTrackingViewState)
  485. todayDayStyle.TrackViewState ();
  486. return todayDayStyle;
  487. }
  488. }
  489. #if ONLY_1_1
  490. [Bindable(true)]
  491. #endif
  492. [Browsable(false)]
  493. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  494. [WebSysDescription ("")]
  495. [WebCategory ("Style")]
  496. public DateTime TodaysDate {
  497. get {
  498. object obj = ViewState ["TodaysDate"];
  499. if (obj != null)
  500. return (DateTime) obj;
  501. return DateTime.Today;
  502. }
  503. set {
  504. ViewState ["TodaysDate"] = value.Date;
  505. }
  506. }
  507. #if NET_2_0
  508. [DefaultValue (true)]
  509. [WebSysDescription ("")]
  510. [WebCategoryAttribute ("Accessibility")]
  511. public virtual bool UseAccessibleHeader
  512. {
  513. get {
  514. return ViewState.GetBool ("UseAccessibleHeader", true);
  515. }
  516. set {
  517. ViewState ["UseAccessibleHeader"] = value;
  518. }
  519. }
  520. #endif
  521. [Bindable(true)]
  522. [DefaultValue("1/1/0001 12:00:00 AM")]
  523. [WebSysDescription ("")]
  524. [WebCategory ("Style")]
  525. public DateTime VisibleDate {
  526. get {
  527. object obj = ViewState ["VisibleDate"];
  528. if (obj != null)
  529. return (DateTime) obj;
  530. return DateTime.MinValue;
  531. }
  532. set {
  533. ViewState ["VisibleDate"] = value.Date;
  534. }
  535. }
  536. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  537. [NotifyParentProperty(true)]
  538. [PersistenceMode(PersistenceMode.InnerProperty)]
  539. [WebSysDescription ("")]
  540. [WebCategory ("Style")]
  541. public TableItemStyle WeekendDayStyle {
  542. get {
  543. if (weekendDayStyle == null)
  544. weekendDayStyle = new TableItemStyle ();
  545. if (IsTrackingViewState)
  546. weekendDayStyle.TrackViewState ();
  547. return weekendDayStyle;
  548. }
  549. }
  550. // Private properties
  551. private DateTime DisplayDate {
  552. get {
  553. DateTime dateTime;
  554. if (VisibleDate == DateTime.MinValue) // If visibledate is still the default value
  555. dateTime = TodaysDate;
  556. else
  557. dateTime = VisibleDate;
  558. return dateTime;
  559. }
  560. }
  561. private DayOfWeek DisplayFirstDayOfWeek {
  562. get {
  563. if (FirstDayOfWeek != FirstDayOfWeek.Default)
  564. return (DayOfWeek) FirstDayOfWeek;
  565. return (DayOfWeek) dateInfo.FirstDayOfWeek;
  566. }
  567. }
  568. protected override ControlCollection CreateControlCollection ()
  569. {
  570. return base.CreateControlCollection ();
  571. }
  572. protected bool HasWeekSelectors (CalendarSelectionMode selectionMode)
  573. {
  574. if (selectionMode == CalendarSelectionMode.DayWeek || selectionMode == CalendarSelectionMode.DayWeekMonth)
  575. return true;
  576. return false;
  577. }
  578. #if NET_2_0
  579. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  580. {
  581. RaisePostBackEvent (eventArgument);
  582. }
  583. protected virtual void RaisePostBackEvent (string arg)
  584. #else
  585. void IPostBackEventHandler.RaisePostBackEvent (string arg)
  586. #endif
  587. {
  588. if (arg.Length < 1)
  589. return;
  590. DateTime dt = dateZenith;
  591. if (arg[0] == 'V') { // Goes to Next or Previous month
  592. DateTime prev = VisibleDate;
  593. int days = Int32.Parse (arg.Substring (1));
  594. dt = GetGlobalCalendar().AddDays (dt, days);
  595. VisibleDate = dt;
  596. OnVisibleMonthChanged (VisibleDate, prev);
  597. return;
  598. }
  599. if (arg[0] == 'R') { // Selects a range of dates
  600. string num, date, days;
  601. num = arg.Substring (1);
  602. days = num.Substring (num.Length - 2, 2);
  603. date = num.Substring (0, num.Length - 2);
  604. dt = GetGlobalCalendar().AddDays (dt, Int32.Parse (date));
  605. SelectDates (dt, Int32.Parse (days));
  606. return;
  607. }
  608. // Selects a single day
  609. int daysFromZenith = Int32.Parse (arg);
  610. dt = GetGlobalCalendar().AddDays (dt, daysFromZenith);
  611. SelectDates (dt, 1);
  612. }
  613. protected override void LoadViewState (object savedState)
  614. {
  615. object [] states = (object []) savedState;
  616. if (states [0] != null)
  617. base.LoadViewState (states [0]);
  618. if (states [1] != null)
  619. DayHeaderStyle.LoadViewState (states [1]);
  620. if (states [2] != null)
  621. DayStyle.LoadViewState (states [2]);
  622. if (states [3] != null)
  623. NextPrevStyle.LoadViewState (states [3]);
  624. if (states [4] != null)
  625. OtherMonthDayStyle.LoadViewState (states [4]);
  626. if (states [5] != null)
  627. SelectedDayStyle.LoadViewState (states [5]);
  628. if (states [6] != null)
  629. TitleStyle.LoadViewState (states [6]);
  630. if (states [7] != null)
  631. TodayDayStyle.LoadViewState (states [7]);
  632. if (states [8] != null)
  633. SelectorStyle.LoadViewState (states [8]);
  634. if (states [9] != null)
  635. WeekendDayStyle.LoadViewState (states [9]);
  636. ArrayList array = (ArrayList) ViewState ["SelectedDates"];
  637. if (array != null) {
  638. dateList = array;
  639. selectedDatesCollection = new SelectedDatesCollection (dateList);
  640. }
  641. }
  642. protected virtual void OnDayRender (TableCell cell, CalendarDay day)
  643. {
  644. if (Events != null) {
  645. DayRenderEventHandler eh = (DayRenderEventHandler) (Events [DayRenderEvent]);
  646. if (eh != null)
  647. eh (this, new DayRenderEventArgs (cell, day));
  648. }
  649. }
  650. #if NET_2_0
  651. protected internal
  652. #else
  653. protected
  654. #endif
  655. override void OnPreRender (EventArgs e)
  656. {
  657. base.OnPreRender (e);
  658. }
  659. protected virtual void OnSelectionChanged ()
  660. {
  661. if (Events != null) {
  662. EventHandler eh = (EventHandler) (Events [SelectionChangedEvent]);
  663. if (eh != null)
  664. eh (this, EventArgs.Empty);
  665. }
  666. }
  667. protected virtual void OnVisibleMonthChanged (DateTime newDate, DateTime previousDate)
  668. {
  669. if (Events != null) {
  670. MonthChangedEventHandler eh = (MonthChangedEventHandler) (Events [VisibleMonthChangedEvent]);
  671. if (eh != null)
  672. eh (this, new MonthChangedEventArgs (newDate, previousDate));
  673. }
  674. }
  675. #if NET_2_0
  676. protected internal
  677. #else
  678. protected
  679. #endif
  680. override void Render (HtmlTextWriter writer)
  681. {
  682. Table table = new Table ();
  683. table.CellSpacing = CellSpacing;
  684. table.CellPadding = CellPadding;
  685. table.ControlStyle.CopyFrom (ControlStyle);
  686. if (ShowGridLines)
  687. table.GridLines = GridLines.Both;
  688. table.RenderBeginTag (writer);
  689. #if NET_2_0
  690. if (Caption != "")
  691. WriteCaption (writer);
  692. #endif
  693. if (ShowTitle)
  694. WriteTitle (writer);
  695. if (ShowDayHeader)
  696. WriteDayHeader (writer);
  697. WriteDays (writer);
  698. table.RenderEndTag (writer);
  699. }
  700. protected override object SaveViewState ()
  701. {
  702. object [] states = new object [10];
  703. if (dayHeaderStyle != null)
  704. states [1] = dayHeaderStyle.SaveViewState ();
  705. if (dayStyle != null)
  706. states [2] = dayStyle.SaveViewState ();
  707. if (nextPrevStyle != null)
  708. states [3] = nextPrevStyle.SaveViewState ();
  709. if (otherMonthDayStyle != null)
  710. states [4] = otherMonthDayStyle.SaveViewState ();
  711. if (selectedDayStyle != null)
  712. states [5] = selectedDayStyle.SaveViewState ();
  713. if (titleStyle != null)
  714. states [6] = titleStyle.SaveViewState ();
  715. if (todayDayStyle != null)
  716. states [7] =todayDayStyle.SaveViewState ();
  717. if (selectorStyle != null)
  718. states [8] = selectorStyle.SaveViewState ();
  719. if (weekendDayStyle != null)
  720. states [9] = weekendDayStyle.SaveViewState ();
  721. if (SelectedDates.Count > 0) {
  722. ViewState ["SelectedDates"] = dateList;
  723. }
  724. states [0] = base.SaveViewState ();
  725. for (int i = 0; i < states.Length; i++) {
  726. if (states [i] != null) {
  727. return states;
  728. }
  729. }
  730. return null;
  731. }
  732. protected override void TrackViewState ()
  733. {
  734. base.TrackViewState ();
  735. if (dayHeaderStyle != null)
  736. dayHeaderStyle.TrackViewState ();
  737. if (dayStyle != null)
  738. dayStyle.TrackViewState ();
  739. if (nextPrevStyle != null)
  740. nextPrevStyle.TrackViewState ();
  741. if (otherMonthDayStyle != null)
  742. otherMonthDayStyle.TrackViewState ();
  743. if (selectedDayStyle != null)
  744. selectedDayStyle.TrackViewState ();
  745. if (titleStyle != null)
  746. titleStyle.TrackViewState ();
  747. if (todayDayStyle != null)
  748. todayDayStyle.TrackViewState ();
  749. if (selectorStyle != null)
  750. selectorStyle.TrackViewState ();
  751. if (weekendDayStyle != null)
  752. weekendDayStyle.TrackViewState ();
  753. }
  754. //
  755. // Private methods
  756. //
  757. private void WriteDayHeader (HtmlTextWriter writer)
  758. {
  759. int i, first;
  760. string dayName;
  761. i = first = (int) (DisplayFirstDayOfWeek);
  762. TableCell cell;
  763. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  764. if (SelectionMode == CalendarSelectionMode.DayWeek) {
  765. cell = new TableCell();
  766. cell.HorizontalAlign = HorizontalAlign.Center;
  767. cell.ApplyStyle (DayHeaderStyle);
  768. // Empty Cell
  769. cell.RenderBeginTag (writer);
  770. cell.RenderEndTag (writer);
  771. } else {
  772. if (SelectionMode == CalendarSelectionMode.DayWeekMonth) {
  773. TableCell selector = new TableCell ();
  774. selector.ApplyStyle (SelectorStyle);
  775. selector.HorizontalAlign = HorizontalAlign.Center;
  776. DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
  777. int days = DateTime.DaysInMonth (DisplayDate.Year, DisplayDate.Month);
  778. selector.RenderBeginTag (writer);
  779. writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + days, SelectMonthText, DayHeaderStyle.ForeColor, true));
  780. selector.RenderEndTag (writer);
  781. }
  782. }
  783. while (true) {
  784. DayOfWeek dayOfWeek = (DayOfWeek) i;
  785. dayName = dateInfo.GetDayName (dayOfWeek);
  786. #if NET_2_0
  787. if (UseAccessibleHeader) {
  788. writer.AddAttribute (HtmlTextWriterAttribute.Abbr, dayName);
  789. writer.AddAttribute (HtmlTextWriterAttribute.Scope, "col");
  790. cell = new TableHeaderCell();
  791. }
  792. else
  793. #endif
  794. cell = new TableCell();
  795. cell.HorizontalAlign = HorizontalAlign.Center;
  796. cell.ApplyStyle (DayHeaderStyle);
  797. cell.RenderBeginTag (writer);
  798. switch (DayNameFormat) {
  799. case DayNameFormat.FirstLetter:
  800. dayName = dayName.Substring (0, 1);
  801. break;
  802. case DayNameFormat.FirstTwoLetters:
  803. dayName = dayName.Substring (0, 2);
  804. break;
  805. case DayNameFormat.Full:
  806. break;
  807. case DayNameFormat.Short:
  808. default:
  809. dayName = dateInfo.GetAbbreviatedDayName (dayOfWeek);
  810. break;
  811. }
  812. writer.Write (dayName);
  813. cell.RenderEndTag (writer);
  814. if (i >= daysInAWeek - 1) {
  815. i = 0;
  816. }
  817. else {
  818. i++;
  819. }
  820. if (i == first)
  821. break;
  822. }
  823. writer.RenderEndTag ();
  824. }
  825. private void WriteDay (DateTime date, HtmlTextWriter writer)
  826. {
  827. Style style = new Style ();
  828. TableCell cell = new TableCell ();
  829. CalendarDay day = new CalendarDay (date,
  830. IsWeekEnd (date.DayOfWeek),
  831. date == TodaysDate, SelectedDates.Contains (date),
  832. GetGlobalCalendar ().GetMonth (DisplayDate) != GetGlobalCalendar ().GetMonth (date),
  833. date.Day.ToString ());
  834. day.IsSelectable = SelectionMode != CalendarSelectionMode.None;
  835. cell.HorizontalAlign = HorizontalAlign.Center;
  836. cell.Width = Unit.Percentage (GetCellWidth ());
  837. LiteralControl lit = new LiteralControl (day.DayNumberText);
  838. cell.Controls.Add (lit);
  839. OnDayRender (cell, day);
  840. if (dayStyle != null && !dayStyle.IsEmpty) {
  841. style.CopyFrom (dayStyle);
  842. }
  843. if (day.IsWeekend && weekendDayStyle != null && !weekendDayStyle.IsEmpty) {
  844. style.CopyFrom (weekendDayStyle);
  845. }
  846. if (day.IsToday && todayDayStyle != null && !todayDayStyle.IsEmpty) {
  847. style.CopyFrom (todayDayStyle);
  848. }
  849. if (day.IsOtherMonth && otherMonthDayStyle != null && !otherMonthDayStyle.IsEmpty) {
  850. style.CopyFrom (otherMonthDayStyle);
  851. }
  852. if (day.IsSelected && selectedDayStyle != null && !selectedDayStyle.IsEmpty) {
  853. style.CopyFrom (selectedDayStyle);
  854. }
  855. cell.ApplyStyle (style);
  856. lit.Text = BuildLink (GetDaysFromZenith (date).ToString (), day.DayNumberText,
  857. cell.ForeColor, day.IsSelectable);
  858. cell.RenderControl (writer);
  859. }
  860. private void WriteDays (HtmlTextWriter writer)
  861. {
  862. DateTime date = new DateTime (DisplayDate.Year, DisplayDate.Month, 1); // first date
  863. DateTime lastDate;
  864. TableCell selectorCell = null;
  865. // Goes backwards until we find the date of that is begining of the week
  866. for (int n = 0; n < daysInAWeek; n++) {
  867. if (date.DayOfWeek == DisplayFirstDayOfWeek)
  868. break;
  869. date = GetGlobalCalendar().AddDays (date, -1);
  870. }
  871. lastDate = GetGlobalCalendar().AddDays (date, 6 * daysInAWeek); // Always six weeks per months
  872. while (true) {
  873. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  874. if (HasWeekSelectors (SelectionMode)) { // Week selector
  875. if (selectorCell == null) {
  876. selectorCell = new TableCell ();
  877. selectorCell.ApplyStyle (SelectorStyle);
  878. selectorCell.HorizontalAlign = HorizontalAlign.Center;
  879. selectorCell.Width = Unit.Percentage (GetCellWidth ());
  880. }
  881. selectorCell.RenderBeginTag (writer);
  882. writer.Write (BuildLink ("R" + GetDaysFromZenith (date) + "07", SelectWeekText, selectorCell.ForeColor, true));
  883. selectorCell.RenderEndTag (writer);
  884. }
  885. for (int i = 0; i < daysInAWeek; i++) {
  886. WriteDay (date, writer);
  887. date = GetGlobalCalendar().AddDays (date, 1);
  888. }
  889. writer.RenderEndTag ();
  890. if (date >= lastDate)
  891. break;
  892. }
  893. }
  894. private string BuildLink (string arg, string text, Color foreColor, bool hasLink)
  895. {
  896. string str = string.Empty;
  897. Color clr;
  898. hasLink = (Page != null && hasLink == true) ? true : false;
  899. if (hasLink) {
  900. str = "<a href=\"";
  901. str += Page.ClientScript.GetPostBackClientHyperlink (this, arg);
  902. str += "\"";
  903. if (!foreColor.IsEmpty) {
  904. clr = foreColor;
  905. } else {
  906. if (ForeColor.IsEmpty)
  907. clr = Color.Black;
  908. else
  909. clr = ForeColor;
  910. }
  911. str += " style=color:" + ColorTranslator.ToHtml (clr);
  912. str += ">";
  913. str += text;
  914. str += "</a>";
  915. }
  916. else
  917. str += text;
  918. return str;
  919. }
  920. private int GetDaysFromZenith (DateTime date)
  921. {
  922. TimeSpan span = date.Subtract (dateZenith);
  923. return span.Days;
  924. }
  925. #if NET_2_0
  926. void WriteCaption (HtmlTextWriter writer)
  927. {
  928. if (CaptionAlign != TableCaptionAlign.NotSet)
  929. writer.AddAttribute (HtmlTextWriterAttribute.Align, CaptionAlign.ToString (CultureInfo.InvariantCulture));
  930. writer.RenderBeginTag (HtmlTextWriterTag.Caption);
  931. writer.Write (Caption);
  932. writer.RenderEndTag ();
  933. }
  934. #endif
  935. private void WriteTitle (HtmlTextWriter writer)
  936. {
  937. TableCell cellNextPrev = null;
  938. TableCell titleCell = new TableCell ();
  939. Table tableTitle = new Table ();
  940. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  941. titleCell.ColumnSpan = HasWeekSelectors (SelectionMode) ? 8 : 7;
  942. if (titleStyle != null && !titleStyle.IsEmpty && !titleStyle.BackColor.IsEmpty) {
  943. titleCell.BackColor = titleStyle.BackColor;
  944. } else {
  945. titleCell.BackColor = Color.Silver;
  946. }
  947. titleCell.RenderBeginTag (writer);
  948. // Table
  949. tableTitle.Width = Unit.Percentage (100);
  950. tableTitle.ControlStyle.CopyFrom (ControlStyle);
  951. if (titleStyle != null && !titleStyle.IsEmpty) {
  952. tableTitle.ApplyStyle (titleStyle);
  953. }
  954. tableTitle.RenderBeginTag (writer);
  955. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  956. if (ShowNextPrevMonth) { // Previous Table Data
  957. cellNextPrev = new TableCell ();
  958. cellNextPrev.ApplyStyle (nextPrevStyle);
  959. cellNextPrev.Width = Unit.Percentage (15);
  960. DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, - 1);
  961. date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
  962. cellNextPrev.RenderBeginTag (writer);
  963. writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, false), cellNextPrev.ForeColor, true));
  964. cellNextPrev.RenderEndTag (writer);
  965. }
  966. // Current Month Table Data
  967. {
  968. string str;
  969. TableCell cellMonth = new TableCell ();
  970. cellMonth.Width = Unit.Percentage (70);
  971. cellMonth.HorizontalAlign = HorizontalAlign.Center;
  972. cellMonth.RenderBeginTag (writer);
  973. str = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (DisplayDate));
  974. if (TitleFormat == TitleFormat.MonthYear)
  975. str += " " + (DisplayDate.Year.ToString ());
  976. writer.Write (str);
  977. cellMonth.RenderEndTag (writer);
  978. }
  979. if (ShowNextPrevMonth) { // Next Table Data
  980. DateTime date = GetGlobalCalendar().AddMonths (DisplayDate, + 1);
  981. date = GetGlobalCalendar().AddDays (date, -DisplayDate.Day + 1);
  982. cellNextPrev.HorizontalAlign = HorizontalAlign.Right;
  983. cellNextPrev.RenderBeginTag (writer);
  984. writer.Write (BuildLink ("V" + GetDaysFromZenith (date), GetNextPrevFormatText (date, true), cellNextPrev.ForeColor, true));
  985. cellNextPrev.RenderEndTag (writer);
  986. }
  987. writer.RenderEndTag ();
  988. tableTitle.RenderEndTag (writer);
  989. titleCell.RenderEndTag (writer);
  990. writer.RenderEndTag (); //tr
  991. }
  992. private string GetNextPrevFormatText (DateTime date, bool next)
  993. {
  994. string text;
  995. switch (NextPrevFormat) {
  996. case NextPrevFormat.FullMonth:
  997. text = dateInfo.GetMonthName (GetGlobalCalendar ().GetMonth (date));
  998. break;
  999. case NextPrevFormat.ShortMonth:
  1000. text = dateInfo.GetAbbreviatedMonthName (GetGlobalCalendar ().GetMonth (date));
  1001. break;
  1002. case NextPrevFormat.CustomText:
  1003. default:
  1004. if (next) {
  1005. text = NextMonthText;
  1006. }
  1007. else {
  1008. text = PrevMonthText;
  1009. }
  1010. break;
  1011. }
  1012. return text;
  1013. }
  1014. private bool IsWeekEnd (DayOfWeek day)
  1015. {
  1016. return (day == DayOfWeek.Saturday || day == DayOfWeek.Sunday);
  1017. }
  1018. private void SelectDates (DateTime startDate, int days)
  1019. {
  1020. SelectedDates.SelectRange (startDate, GetGlobalCalendar().AddDays (startDate, days - 1));
  1021. OnSelectionChanged ();
  1022. }
  1023. private double GetCellWidth ()
  1024. {
  1025. return HasWeekSelectors (SelectionMode) ? 100/8 : 100/7;
  1026. }
  1027. private System.Globalization.Calendar GetGlobalCalendar ()
  1028. {
  1029. return DateTimeFormatInfo.CurrentInfo.Calendar;
  1030. }
  1031. [WebSysDescription ("")]
  1032. [WebCategory ("Action")]
  1033. public event DayRenderEventHandler DayRender
  1034. {
  1035. add {
  1036. Events.AddHandler (DayRenderEvent, value);
  1037. }
  1038. remove {
  1039. Events.RemoveHandler (DayRenderEvent, value);
  1040. }
  1041. }
  1042. [WebSysDescription ("")]
  1043. [WebCategory ("Action")]
  1044. public event EventHandler SelectionChanged
  1045. {
  1046. add {
  1047. Events.AddHandler (SelectionChangedEvent, value);
  1048. }
  1049. remove {
  1050. Events.RemoveHandler (SelectionChangedEvent, value);
  1051. }
  1052. }
  1053. [WebSysDescription ("")]
  1054. [WebCategory ("Action")]
  1055. public event MonthChangedEventHandler VisibleMonthChanged
  1056. {
  1057. add {
  1058. Events.AddHandler (VisibleMonthChangedEvent, value);
  1059. }
  1060. remove {
  1061. Events.RemoveHandler (VisibleMonthChangedEvent, value);
  1062. }
  1063. }
  1064. }
  1065. }