Calendar.cs 32 KB

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