Calendar.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: Calendar
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 60%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.Collections;
  15. using System.Globalization;
  16. using System.Text;
  17. using System.Web;
  18. using System.Web.UI;
  19. using System.Drawing;
  20. namespace System.Web.UI.WebControls
  21. {
  22. public class Calendar : WebControl, IPostBackEventHandler
  23. {
  24. //
  25. private TableItemStyle dayHeaderStyle;
  26. private TableItemStyle dayStyle;
  27. private TableItemStyle nextPrevStyle;
  28. private TableItemStyle otherMonthDayStyle;
  29. private SelectedDatesCollection selectedDates;
  30. private ArrayList selectedDatesList;
  31. private TableItemStyle selectedDayStyle;
  32. private TableItemStyle selectorStyle;
  33. private TableItemStyle titleStyle;
  34. private TableItemStyle todayDayStyle;
  35. private TableItemStyle weekendDayStyle;
  36. private static readonly object DayRenderEvent = new object();
  37. private static readonly object SelectionChangedEvent = new object();
  38. private static readonly object VisibleMonthChangedEvent = new object();
  39. private Color defaultTextColor;
  40. private System.Globalization.Calendar globCal;
  41. public Calendar(): base()
  42. {
  43. //TODO: Initialization
  44. }
  45. public int CellPadding
  46. {
  47. get
  48. {
  49. object o = ViewState["CellPadding"];
  50. if(o!=null)
  51. return (int)o;
  52. return 2;
  53. }
  54. set
  55. {
  56. ViewState["CellPadding"] = value;
  57. }
  58. }
  59. public int CellSpacing
  60. {
  61. get
  62. {
  63. object o = ViewState["CellSpacing"];
  64. if(o!=null)
  65. return (int)o;
  66. return 0;
  67. }
  68. set
  69. {
  70. if(value<-1)
  71. throw new ArgumentOutOfRangeException();
  72. ViewState["CellSpacing"] = value;
  73. }
  74. }
  75. public TableItemStyle DayHeaderStyle
  76. {
  77. get
  78. {
  79. if(dayHeaderStyle==null)
  80. dayHeaderStyle = new TableItemStyle();
  81. return dayHeaderStyle;
  82. }
  83. }
  84. public DayNameFormat DayNameFormat
  85. {
  86. get
  87. {
  88. object o = ViewState["DayNameFormat"];
  89. if(o!=null)
  90. return (DayNameFormat)o;
  91. return DayNameFormat.Short;
  92. }
  93. set
  94. {
  95. if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
  96. throw new ArgumentException();
  97. ViewState["DayNameFormat"] = value;
  98. }
  99. }
  100. public TableItemStyle DayStyle
  101. {
  102. get
  103. {
  104. if(dayStyle==null)
  105. dayStyle = new TableItemStyle();
  106. return dayStyle;
  107. }
  108. }
  109. public FirstDayOfWeek FirstDayOfWeek
  110. {
  111. get
  112. {
  113. object o = ViewState["FirstDayOfWeek"];
  114. if(o!=null)
  115. return (FirstDayOfWeek)o;
  116. return FirstDayOfWeek.Default;
  117. }
  118. set
  119. {
  120. if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
  121. throw new ArgumentException();
  122. ViewState["FirstDayOfWeek"] = value;
  123. }
  124. }
  125. public string NextMonthText
  126. {
  127. get
  128. {
  129. object o = ViewState["NextMonthText"];
  130. if(o!=null)
  131. return (string)o;
  132. return "&gt;";
  133. }
  134. set
  135. {
  136. ViewState["NextMonthText"] = value;
  137. }
  138. }
  139. public NextPrevFormat NextPrevFormat
  140. {
  141. get
  142. {
  143. object o = ViewState["NextPrevFormat"];
  144. if(o!=null)
  145. return (NextPrevFormat)o;
  146. return NextPrevFormat.CustomText;
  147. }
  148. set
  149. {
  150. if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
  151. throw new ArgumentException();
  152. ViewState["NextPrevFormat"] = value;
  153. }
  154. }
  155. public TableItemStyle NextPrevStyle
  156. {
  157. get
  158. {
  159. if(nextPrevStyle == null)
  160. nextPrevStyle = new TableItemStyle();
  161. return nextPrevStyle;
  162. }
  163. }
  164. public TableItemStyle OtherMonthDayStyle
  165. {
  166. get
  167. {
  168. if(otherMonthDayStyle == null)
  169. otherMonthDayStyle = new TableItemStyle();
  170. return otherMonthDayStyle;
  171. }
  172. }
  173. public string PrevMonthText
  174. {
  175. get
  176. {
  177. object o = ViewState["PrevMonthText"];
  178. if(o!=null)
  179. return (string)o;
  180. return "&lt;";
  181. }
  182. set
  183. {
  184. ViewState["PrevMonthText"] = value;
  185. }
  186. }
  187. public DateTime SelectedDate
  188. {
  189. // TODO: Am I right here? I got confused with the "Remarks" written in the documentation
  190. /*
  191. * Looks like I have to first do something with SelectionMode,
  192. * then with SelectedDates,
  193. * Update when SelectionChanged is called => Link to the function.
  194. * Pretty confused at this point
  195. */
  196. get
  197. {
  198. object o = ViewState["SelectedDate"];
  199. if(o!=null)
  200. return (DateTime)o;
  201. return DateTime.MinValue;
  202. }
  203. set
  204. {
  205. ViewState["SelectedDate"] = value;
  206. }
  207. }
  208. public SelectedDatesCollection SelectedDates
  209. {
  210. get
  211. {
  212. if(selectedDates==null)
  213. {
  214. if(selectedDatesList == null)
  215. selectedDatesList = new ArrayList();
  216. selectedDates = new SelectedDatesCollection(selectedDatesList);
  217. }
  218. return selectedDates;
  219. }
  220. }
  221. public TableItemStyle SelectedDayStyle
  222. {
  223. get
  224. {
  225. if(selectedDayStyle==null)
  226. selectedDayStyle = new TableItemStyle();
  227. return selectedDayStyle;
  228. }
  229. }
  230. public CalendarSelectionMode SelectionMode
  231. {
  232. get
  233. {
  234. object o = ViewState["SelectionMode"];
  235. if(o!=null)
  236. return (CalendarSelectionMode)o;
  237. return CalendarSelectionMode.Day;
  238. }
  239. set
  240. {
  241. if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
  242. throw new ArgumentException();
  243. ViewState["SelectionMode"] = value;
  244. }
  245. }
  246. public string SelectedMonthText
  247. {
  248. get
  249. {
  250. object o = ViewState["SelectedMonthText"];
  251. if(o!=null)
  252. return (string)o;
  253. return "&gt;&gt;";
  254. }
  255. set
  256. {
  257. ViewState["SelectedMonthText"] = value;
  258. }
  259. }
  260. public TableItemStyle SelectorStyle
  261. {
  262. get
  263. {
  264. if(selectorStyle==null)
  265. selectorStyle = new TableItemStyle();
  266. return selectorStyle;
  267. }
  268. }
  269. public string SelectedWeekText
  270. {
  271. get
  272. {
  273. object o = ViewState["SelectedWeekText"];
  274. if(o!=null)
  275. return (string)o;
  276. return "&gt;";
  277. }
  278. set
  279. {
  280. ViewState["SelectedWeekText"] = value;
  281. }
  282. }
  283. public bool ShowDayHeader
  284. {
  285. get
  286. {
  287. object o = ViewState["ShowDayHeader"];
  288. if(o!=null)
  289. return (bool)o;
  290. return true;
  291. }
  292. set
  293. {
  294. ViewState["ShowDayHeader"] = value;
  295. }
  296. }
  297. public bool ShowGridLines
  298. {
  299. get
  300. {
  301. object o = ViewState["ShowGridLines"];
  302. if(o!=null)
  303. return (bool)o;
  304. return false;
  305. }
  306. set
  307. {
  308. ViewState["ShowGridLines"] = value;
  309. }
  310. }
  311. public bool ShowNextPrevMonth
  312. {
  313. get
  314. {
  315. object o = ViewState["ShowNextPrevMonth"];
  316. if(o!=null)
  317. return (bool)o;
  318. return true;
  319. }
  320. set
  321. {
  322. ViewState["ShowNextPrevMonth"] = value;
  323. }
  324. }
  325. public bool ShowTitle
  326. {
  327. get
  328. {
  329. object o = ViewState["ShowTitle"];
  330. if(o!=null)
  331. return (bool)o;
  332. return true;
  333. }
  334. set
  335. {
  336. ViewState["ShowTitle"] = value;
  337. }
  338. }
  339. public TitleFormat TitleFormat
  340. {
  341. get
  342. {
  343. object o = ViewState["TitleFormat"];
  344. if(o!=null)
  345. return (TitleFormat)o;
  346. return TitleFormat.MonthYear;
  347. }
  348. set
  349. {
  350. if(!System.Enum.IsDefined(typeof(TitleFormat), value))
  351. throw new ArgumentException();
  352. ViewState["TitleFormat"] = value;
  353. }
  354. }
  355. public TableItemStyle TitleStyle
  356. {
  357. get
  358. {
  359. if(titleStyle==null)
  360. titleStyle = new TableItemStyle();
  361. return titleStyle;
  362. }
  363. }
  364. public TableItemStyle TodayDayStyle
  365. {
  366. get
  367. {
  368. if(todayDayStyle==null)
  369. todayDayStyle = new TableItemStyle();
  370. return todayDayStyle;
  371. }
  372. }
  373. public DateTime TodaysDate
  374. {
  375. get
  376. {
  377. object o = ViewState["TodaysDate"];
  378. if(o!=null)
  379. return (DateTime)o;
  380. return DateTime.Today;
  381. }
  382. set
  383. {
  384. ViewState["TodaysDate"] = value;
  385. }
  386. }
  387. public DateTime VisibleDate
  388. {
  389. get
  390. {
  391. object o = ViewState["VisibleDate"];
  392. if(o!=null)
  393. return (DateTime)o;
  394. return DateTime.MinValue;
  395. }
  396. set
  397. {
  398. ViewState["VisibleDate"] = value;
  399. }
  400. }
  401. public TableItemStyle WeekendDayStyle
  402. {
  403. get
  404. {
  405. if(weekendDayStyle == null)
  406. weekendDayStyle = new TableItemStyle();
  407. return weekendDayStyle;
  408. }
  409. }
  410. public event DayRenderEventHandler DayRender
  411. {
  412. add
  413. {
  414. Events.AddHandler(DayRenderEvent, value);
  415. }
  416. remove
  417. {
  418. Events.RemoveHandler(DayRenderEvent, value);
  419. }
  420. }
  421. public event EventHandler SelectionChanged
  422. {
  423. add
  424. {
  425. Events.AddHandler(SelectionChangedEvent, value);
  426. }
  427. remove
  428. {
  429. Events.RemoveHandler(SelectionChangedEvent, value);
  430. }
  431. }
  432. public event MonthChangedEventHandler VisibleMonthChanged
  433. {
  434. add
  435. {
  436. Events.AddHandler(VisibleMonthChangedEvent, value);
  437. }
  438. remove
  439. {
  440. Events.RemoveHandler(VisibleMonthChangedEvent, value);
  441. }
  442. }
  443. protected virtual void OnDayRender(TableCell cell, CalendarDay day)
  444. {
  445. if(Events!=null)
  446. {
  447. DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
  448. if(dreh!=null)
  449. dreh(this, new DayRenderEventArgs(cell, day));
  450. }
  451. }
  452. protected virtual void OnSelectionChanged()
  453. {
  454. if(Events!=null)
  455. {
  456. EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
  457. if(eh!=null)
  458. eh(this, new EventArgs());
  459. }
  460. }
  461. protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
  462. {
  463. if(Events!=null)
  464. {
  465. MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
  466. if(mceh!=null)
  467. mceh(this, new MonthChangedEventArgs(newDate, prevDate));
  468. }
  469. }
  470. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  471. {
  472. //TODO: Implement Me
  473. // Written to keep compile get going
  474. throw new NotImplementedException();
  475. }
  476. protected override void Render(HtmlTextWriter writer)
  477. {
  478. //TODO: Implement me
  479. throw new NotImplementedException();
  480. globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
  481. SetFirstCalendarDay(GetEffectiveVisibleDate());
  482. /*
  483. * ForeColor else defaultTextColor
  484. * Draw a table
  485. * if(ControlStyleCreated())
  486. * then
  487. * {
  488. * ApplyStyle(ControlStyle)
  489. * }
  490. * GridLines?
  491. * RenderBeginTag(writer)
  492. * RenderTitle(writer, visibleDate from GetEffectiveVisibleDate, this.SelectionMode, IsEnabled)
  493. * if(ShowHeader)
  494. * RenderHeader(writer, visibleDate, SelectionMode, IsEnabled,
  495. * RenderAllDays
  496. * RenderEndTag(writer)
  497. */
  498. }
  499. protected override ControlCollection CreateControlCollection()
  500. {
  501. return new EmptyControlCollection(this);
  502. }
  503. protected override void LoadViewState(object savedState)
  504. {
  505. if(savedState!=null)
  506. {
  507. //TODO: Implement me
  508. //object[] states = (object[]) savedState;
  509. //loadViewState of all the states/styles
  510. }
  511. throw new NotImplementedException();
  512. }
  513. protected override object SaveViewState()
  514. {
  515. //TODO: Implement me
  516. // SaveViewState of all the styles
  517. throw new NotImplementedException();
  518. }
  519. protected override void TrackViewState()
  520. {
  521. base.TrackViewState();
  522. if(titleStyle!=null)
  523. {
  524. titleStyle.TrackViewState();
  525. }
  526. if(nextPrevStyle!=null)
  527. {
  528. nextPrevStyle.TrackViewState();
  529. }
  530. if(dayStyle!=null)
  531. {
  532. dayStyle.TrackViewState();
  533. }
  534. if(dayHeaderStyle!=null)
  535. {
  536. dayHeaderStyle.TrackViewState();
  537. }
  538. if(todayDayStyle!=null)
  539. {
  540. todayDayStyle.TrackViewState();
  541. }
  542. if(weekendDayStyle!=null)
  543. {
  544. weekendDayStyle.TrackViewState();
  545. }
  546. if(otherMonthStyle!=null)
  547. {
  548. otherMonthStyle.TrackViewState();
  549. }
  550. if(selectedDayStyle!=null)
  551. {
  552. selectedDayStyle.TrackViewState();
  553. }
  554. if(selectorStyle!=null)
  555. {
  556. selectorStyle.TrackViewState();
  557. }
  558. }
  559. //TODO: Recheck, I am through with all the functions?
  560. private void RenderAllDays(HtmlTextWriter writer, DateTime firstDay, DateTime activeDate, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  561. {
  562. throw new NotImplementedException();
  563. //TODO: Implement me
  564. /*
  565. * "<tr>"
  566. * "</tr>"
  567. */
  568. }
  569. private void RenderHeader(HtmlTextWriter writer, DateTime firstDay, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  570. {
  571. throw new NotImplementedException();
  572. //TODO: Implement Me
  573. /*
  574. * "<tr>"
  575. * "</tr>"
  576. */
  577. }
  578. private void RenderTitle(HtmlTextWriter writer, DateTime visibleDate, CalendarSelectionMode mode, bool isActive)
  579. {
  580. throw new NotImplementedException();
  581. //TODO: Implement me
  582. /*
  583. * Make a row with the following contents: "<tr>"
  584. * Draw a table, with cell having the following properties
  585. * if(mode==CalendarSelectionMode.DayWeek || mode==CalendarSelectionMode.DayWeekMonth)
  586. * then draw a column with colspan=8
  587. * else
  588. * draw with colspan=7
  589. * set gridlines?
  590. * set width
  591. * set cellspacing
  592. * ApplyStyleToTitle(table, cell, style)
  593. * RenderBeginTag(writer)
  594. * RenderBeginTag(writer)
  595. * "<tr>"
  596. * -> The next/previous months things
  597. * GetCalendarText("previousMonth", PrevMonthText, NextPrevStyle.ForeColor, isActive)
  598. * RenderCalendarCell(writer, cell, ^^^)
  599. * ..
  600. * ..
  601. * Then for NextMonthText
  602. * "</tr>"
  603. * "</tr>"
  604. */
  605. }
  606. private void ApplyStyleToTitle(Table table, TableCell cell, TableItemStyle style)
  607. {
  608. throw new NotImplementedException();
  609. //TODO: Implement me
  610. /*
  611. * Font
  612. * Background color
  613. * Foreground color
  614. * Border color
  615. * Border width
  616. * Border style
  617. * Vertical alignment
  618. */
  619. }
  620. private void RenderCalendarCell(HtmlTextWriter writer, TableCell cell, string text)
  621. {
  622. cell.RenderBeginTag(writer);
  623. writer.Write(text);
  624. cell.RenderEndTag(writer);
  625. }
  626. private DateTime SetFirstCalendarDay(DateTime visibleDate)
  627. {
  628. globCal = visibleDate;
  629. throw new NotImplementedException();
  630. //TODO: Implement me
  631. }
  632. private DateTime GetEffectiveVisibleDate()
  633. {
  634. DateTime dt = VisibleDate;
  635. if(dt.Equals(DateTime.MinValue))
  636. {
  637. dt = TodaysDate;
  638. }
  639. return new DateTime(globCal.GetYear(dt), globCal.GetMonth(dt), globCal.GetDayOfMonth(dt), globCal);
  640. }
  641. /*
  642. * Creates text to be displayed, with all attributes if to be
  643. * shown as a hyperlink
  644. */
  645. private string GetCalendarText(string eventArg, string text, Color foreground, bool isLink)
  646. {
  647. if(isLink)
  648. {
  649. StringBuilder dispVal = new StringBuilder();
  650. dispVal.Append("<a href=\"");
  651. dispVal.Append(Page.GetPostBackClientHyperlink(this, eventArg));
  652. dispVal.Append("\" style=\"color: ");
  653. if(foreground.IsEmpty)
  654. {
  655. dispVal.Append(ColorTranslater.ToHtml(defaultTextColor);
  656. } else
  657. {
  658. dispVal.Append(ColorTranslater.ToHtml(foreground);
  659. }
  660. dispVal.Append("\">");
  661. dispVal.Append(text);
  662. dispVal.Append("</a>");
  663. return dispVal.ToString();
  664. }
  665. return text;
  666. }
  667. private string GetHtmlForCell(TableCell cell, bool showLinks)
  668. {
  669. StringWriter sw = new StringWriter();
  670. HtmlTextWriter htw = new HtmlTextWriter(sw);
  671. RenderBeginTag(htw);
  672. if(showLinks)
  673. {
  674. //sw.Write(GetCalendarText(,,true, ForeColor));
  675. //TODO: Implement me
  676. }
  677. throw new NotImplementedException();
  678. }
  679. }
  680. }