Calendar.cs 31 KB

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