Calendar.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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: 98%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. */
  13. using System;
  14. using System.IO;
  15. using System.Collections;
  16. using System.Globalization;
  17. using System.Text;
  18. using System.Web;
  19. using System.Web.UI;
  20. using System.Drawing;
  21. using System.ComponentModel;
  22. namespace System.Web.UI.WebControls
  23. {
  24. [DefaultEvent("SelectionChanged")]
  25. [DefaultProperty("SelectedDate")]
  26. //TODO: [Designer("??")]
  27. //[DataBindingHandler("??")]
  28. public class Calendar : WebControl, IPostBackEventHandler
  29. {
  30. //
  31. private TableItemStyle dayHeaderStyle;
  32. private TableItemStyle dayStyle;
  33. private TableItemStyle nextPrevStyle;
  34. private TableItemStyle otherMonthDayStyle;
  35. private SelectedDatesCollection selectedDates;
  36. private ArrayList selectedDatesList;
  37. private TableItemStyle selectedDayStyle;
  38. private TableItemStyle selectorStyle;
  39. private TableItemStyle titleStyle;
  40. private TableItemStyle todayDayStyle;
  41. private TableItemStyle weekendDayStyle;
  42. private static readonly object DayRenderEvent = new object();
  43. private static readonly object SelectionChangedEvent = new object();
  44. private static readonly object VisibleMonthChangedEvent = new object();
  45. private Color defaultTextColor;
  46. private System.Globalization.Calendar globCal;
  47. private static int MASK_WEEKEND = (0x01 << 0);
  48. private static int MASK_OMONTH = (0x01 << 1);
  49. private static int MASK_TODAY = (0x01 << 2);
  50. private static int MASK_SELECTED = (0x01 << 3);
  51. private static int MASK_DAY = (0x01 << 4);
  52. private static int MASK_UNIQUE = MASK_WEEKEND | MASK_OMONTH | MASK_TODAY | MASK_SELECTED;
  53. public Calendar(): base()
  54. {
  55. //TODO: Initialization
  56. }
  57. public int CellPadding
  58. {
  59. get
  60. {
  61. object o = ViewState["CellPadding"];
  62. if(o!=null)
  63. return (int)o;
  64. return 2;
  65. }
  66. set
  67. {
  68. ViewState["CellPadding"] = value;
  69. }
  70. }
  71. public int CellSpacing
  72. {
  73. get
  74. {
  75. object o = ViewState["CellSpacing"];
  76. if(o!=null)
  77. return (int)o;
  78. return 0;
  79. }
  80. set
  81. {
  82. if(value<-1)
  83. throw new ArgumentOutOfRangeException();
  84. ViewState["CellSpacing"] = value;
  85. }
  86. }
  87. public TableItemStyle DayHeaderStyle
  88. {
  89. get
  90. {
  91. if(dayHeaderStyle==null)
  92. dayHeaderStyle = new TableItemStyle();
  93. if(IsTrackingViewState)
  94. dayHeaderStyle.TrackViewState();
  95. return dayHeaderStyle;
  96. }
  97. }
  98. public DayNameFormat DayNameFormat
  99. {
  100. get
  101. {
  102. object o = ViewState["DayNameFormat"];
  103. if(o!=null)
  104. return (DayNameFormat)o;
  105. return DayNameFormat.Short;
  106. }
  107. set
  108. {
  109. if(!System.Enum.IsDefined(typeof(DayNameFormat),value))
  110. throw new ArgumentException();
  111. ViewState["DayNameFormat"] = value;
  112. }
  113. }
  114. public TableItemStyle DayStyle
  115. {
  116. get
  117. {
  118. if(dayStyle==null)
  119. dayStyle = new TableItemStyle();
  120. if(IsTrackingViewState)
  121. dayStyle.TrackViewState();
  122. return dayStyle;
  123. }
  124. }
  125. public FirstDayOfWeek FirstDayOfWeek
  126. {
  127. get
  128. {
  129. object o = ViewState["FirstDayOfWeek"];
  130. if(o!=null)
  131. return (FirstDayOfWeek)o;
  132. return FirstDayOfWeek.Default;
  133. }
  134. set
  135. {
  136. if(!System.Enum.IsDefined(typeof(FirstDayOfWeek), value))
  137. throw new ArgumentException();
  138. ViewState["FirstDayOfWeek"] = value;
  139. }
  140. }
  141. public string NextMonthText
  142. {
  143. get
  144. {
  145. object o = ViewState["NextMonthText"];
  146. if(o!=null)
  147. return (string)o;
  148. return "&gt;";
  149. }
  150. set
  151. {
  152. ViewState["NextMonthText"] = value;
  153. }
  154. }
  155. public NextPrevFormat NextPrevFormat
  156. {
  157. get
  158. {
  159. object o = ViewState["NextPrevFormat"];
  160. if(o!=null)
  161. return (NextPrevFormat)o;
  162. return NextPrevFormat.CustomText;
  163. }
  164. set
  165. {
  166. if(!System.Enum.IsDefined(typeof(NextPrevFormat), value))
  167. throw new ArgumentException();
  168. ViewState["NextPrevFormat"] = value;
  169. }
  170. }
  171. public TableItemStyle NextPrevStyle
  172. {
  173. get
  174. {
  175. if(nextPrevStyle == null)
  176. nextPrevStyle = new TableItemStyle();
  177. if(IsTrackingViewState)
  178. nextPrevStyle.TrackViewState();
  179. return nextPrevStyle;
  180. }
  181. }
  182. public TableItemStyle OtherMonthDayStyle
  183. {
  184. get
  185. {
  186. if(otherMonthDayStyle == null)
  187. otherMonthDayStyle = new TableItemStyle();
  188. if(IsTrackingViewState)
  189. otherMonthDayStyle.TrackViewState();
  190. return otherMonthDayStyle;
  191. }
  192. }
  193. public string PrevMonthText
  194. {
  195. get
  196. {
  197. object o = ViewState["PrevMonthText"];
  198. if(o!=null)
  199. return (string)o;
  200. return "&lt;";
  201. }
  202. set
  203. {
  204. ViewState["PrevMonthText"] = value;
  205. }
  206. }
  207. public DateTime SelectedDate
  208. {
  209. get
  210. {
  211. if(SelectedDates.Count > 0)
  212. {
  213. return SelectedDates[0];
  214. }
  215. return DateTime.MinValue;
  216. }
  217. set
  218. {
  219. if(value == DateTime.MinValue)
  220. {
  221. SelectedDates.Clear();
  222. } else
  223. {
  224. SelectedDates.SelectRange(value, value);
  225. }
  226. }
  227. }
  228. public SelectedDatesCollection SelectedDates
  229. {
  230. get
  231. {
  232. if(selectedDates==null)
  233. {
  234. if(selectedDatesList == null)
  235. selectedDatesList = new ArrayList();
  236. selectedDates = new SelectedDatesCollection(selectedDatesList);
  237. }
  238. return selectedDates;
  239. }
  240. }
  241. public TableItemStyle SelectedDayStyle
  242. {
  243. get
  244. {
  245. if(selectedDayStyle==null)
  246. selectedDayStyle = new TableItemStyle();
  247. if(IsTrackingViewState)
  248. selectedDayStyle.TrackViewState();
  249. return selectedDayStyle;
  250. }
  251. }
  252. public CalendarSelectionMode SelectionMode
  253. {
  254. get
  255. {
  256. object o = ViewState["SelectionMode"];
  257. if(o!=null)
  258. return (CalendarSelectionMode)o;
  259. return CalendarSelectionMode.Day;
  260. }
  261. set
  262. {
  263. if(!System.Enum.IsDefined(typeof(CalendarSelectionMode), value))
  264. throw new ArgumentException();
  265. ViewState["SelectionMode"] = value;
  266. }
  267. }
  268. public string SelectMonthText
  269. {
  270. get
  271. {
  272. object o = ViewState["SelectMonthText"];
  273. if(o!=null)
  274. return (string)o;
  275. return "&gt;&gt;";
  276. }
  277. set
  278. {
  279. ViewState["SelectMonthText"] = value;
  280. }
  281. }
  282. public TableItemStyle SelectorStyle
  283. {
  284. get
  285. {
  286. if(selectorStyle==null)
  287. selectorStyle = new TableItemStyle();
  288. return selectorStyle;
  289. }
  290. }
  291. public string SelectWeekText
  292. {
  293. get
  294. {
  295. object o = ViewState["SelectWeekText"];
  296. if(o!=null)
  297. return (string)o;
  298. return "&gt;";
  299. }
  300. set
  301. {
  302. ViewState["SelectWeekText"] = value;
  303. }
  304. }
  305. public bool ShowDayHeader
  306. {
  307. get
  308. {
  309. object o = ViewState["ShowDayHeader"];
  310. if(o!=null)
  311. return (bool)o;
  312. return true;
  313. }
  314. set
  315. {
  316. ViewState["ShowDayHeader"] = value;
  317. }
  318. }
  319. public bool ShowGridLines
  320. {
  321. get
  322. {
  323. object o = ViewState["ShowGridLines"];
  324. if(o!=null)
  325. return (bool)o;
  326. return false;
  327. }
  328. set
  329. {
  330. ViewState["ShowGridLines"] = value;
  331. }
  332. }
  333. public bool ShowNextPrevMonth
  334. {
  335. get
  336. {
  337. object o = ViewState["ShowNextPrevMonth"];
  338. if(o!=null)
  339. return (bool)o;
  340. return true;
  341. }
  342. set
  343. {
  344. ViewState["ShowNextPrevMonth"] = value;
  345. }
  346. }
  347. public bool ShowTitle
  348. {
  349. get
  350. {
  351. object o = ViewState["ShowTitle"];
  352. if(o!=null)
  353. return (bool)o;
  354. return true;
  355. }
  356. set
  357. {
  358. ViewState["ShowTitle"] = value;
  359. }
  360. }
  361. public TitleFormat TitleFormat
  362. {
  363. get
  364. {
  365. object o = ViewState["TitleFormat"];
  366. if(o!=null)
  367. return (TitleFormat)o;
  368. return TitleFormat.MonthYear;
  369. }
  370. set
  371. {
  372. if(!System.Enum.IsDefined(typeof(TitleFormat), value))
  373. throw new ArgumentException();
  374. ViewState["TitleFormat"] = value;
  375. }
  376. }
  377. public TableItemStyle TitleStyle
  378. {
  379. get
  380. {
  381. if(titleStyle==null)
  382. titleStyle = new TableItemStyle();
  383. if(IsTrackingViewState)
  384. titleStyle.TrackViewState();
  385. return titleStyle;
  386. }
  387. }
  388. public TableItemStyle TodayDayStyle
  389. {
  390. get
  391. {
  392. if(todayDayStyle==null)
  393. todayDayStyle = new TableItemStyle();
  394. if(IsTrackingViewState)
  395. todayDayStyle.TrackViewState();
  396. return todayDayStyle;
  397. }
  398. }
  399. public DateTime TodaysDate
  400. {
  401. get
  402. {
  403. object o = ViewState["TodaysDate"];
  404. if(o!=null)
  405. return (DateTime)o;
  406. return DateTime.Today;
  407. }
  408. set
  409. {
  410. ViewState["TodaysDate"] = value;
  411. }
  412. }
  413. public DateTime VisibleDate
  414. {
  415. get
  416. {
  417. object o = ViewState["VisibleDate"];
  418. if(o!=null)
  419. return (DateTime)o;
  420. return DateTime.MinValue;
  421. }
  422. set
  423. {
  424. ViewState["VisibleDate"] = value;
  425. }
  426. }
  427. public TableItemStyle WeekendDayStyle
  428. {
  429. get
  430. {
  431. if(weekendDayStyle == null)
  432. weekendDayStyle = new TableItemStyle();
  433. if(IsTrackingViewState)
  434. {
  435. weekendDayStyle.TrackViewState();
  436. }
  437. return weekendDayStyle;
  438. }
  439. }
  440. public event DayRenderEventHandler DayRender
  441. {
  442. add
  443. {
  444. Events.AddHandler(DayRenderEvent, value);
  445. }
  446. remove
  447. {
  448. Events.RemoveHandler(DayRenderEvent, value);
  449. }
  450. }
  451. public event EventHandler SelectionChanged
  452. {
  453. add
  454. {
  455. Events.AddHandler(SelectionChangedEvent, value);
  456. }
  457. remove
  458. {
  459. Events.RemoveHandler(SelectionChangedEvent, value);
  460. }
  461. }
  462. public event MonthChangedEventHandler VisibleMonthChanged
  463. {
  464. add
  465. {
  466. Events.AddHandler(VisibleMonthChangedEvent, value);
  467. }
  468. remove
  469. {
  470. Events.RemoveHandler(VisibleMonthChangedEvent, value);
  471. }
  472. }
  473. protected virtual void OnDayRender(TableCell cell, CalendarDay day)
  474. {
  475. if(Events!=null)
  476. {
  477. DayRenderEventHandler dreh = (DayRenderEventHandler)(Events[DayRenderEvent]);
  478. if(dreh!=null)
  479. dreh(this, new DayRenderEventArgs(cell, day));
  480. }
  481. }
  482. protected virtual void OnSelectionChanged()
  483. {
  484. if(Events!=null)
  485. {
  486. EventHandler eh = (EventHandler)(Events[SelectionChangedEvent]);
  487. if(eh!=null)
  488. eh(this, new EventArgs());
  489. }
  490. }
  491. protected virtual void OnVisibleMonthChanged(DateTime newDate, DateTime prevDate)
  492. {
  493. if(Events!=null)
  494. {
  495. MonthChangedEventHandler mceh = (MonthChangedEventHandler)(Events[VisibleMonthChangedEvent]);
  496. if(mceh!=null)
  497. mceh(this, new MonthChangedEventArgs(newDate, prevDate));
  498. }
  499. }
  500. /// <remarks>
  501. /// See test6.aspx in Tests directory for verification
  502. /// </remarks>
  503. void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  504. {
  505. globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
  506. DateTime visDate = GetEffectiveVisibleDate();
  507. //FIXME: Should it be String.Compare(eventArgument, "nextMonth", false);
  508. if(eventArgument == "nextMonth")
  509. {
  510. VisibleDate = globCal.AddMonths(visDate, 1);
  511. OnVisibleMonthChanged(VisibleDate, visDate);
  512. return;
  513. }
  514. if(eventArgument == "prevMonth")
  515. {
  516. VisibleDate = globCal.AddMonths(visDate, -1);
  517. OnVisibleMonthChanged(VisibleDate, visDate);
  518. return;
  519. }
  520. if(eventArgument == "selectMonth")
  521. {
  522. DateTime oldDate = new DateTime(globCal.GetYear(visDate), globCal.GetMonth(visDate), 1, globCal);
  523. SelectRangeInternal(oldDate, globCal.AddDays(globCal.AddMonths(oldDate, 1), -1), visDate);
  524. return;
  525. }
  526. if(String.Compare(eventArgument, 0, "selectWeek", 0, "selectWeek".Length)==0)
  527. {
  528. int week = -1;
  529. try
  530. {
  531. week = Int32.Parse(eventArgument.Substring("selectWeek".Length));
  532. } catch(Exception e)
  533. {
  534. }
  535. if(week >= 0 && week <= 5)
  536. {
  537. DateTime weekStart = globCal.AddDays(GetFirstCalendarDay(visDate), week * 7);
  538. SelectRangeInternal(weekStart, globCal.AddDays(weekStart, 6), visDate);
  539. }
  540. return;
  541. }
  542. if(String.Compare(eventArgument, 0, "selectDay", 0, "selectDay".Length)==0)
  543. {
  544. int day = -1;
  545. try
  546. {
  547. day = Int32.Parse(eventArgument.Substring("selectDay".Length));
  548. } catch(Exception e)
  549. {
  550. }
  551. if(day >= 0 && day <= 42)
  552. {
  553. DateTime dayStart = globCal.AddDays(GetFirstCalendarDay(visDate), day);
  554. SelectRangeInternal(dayStart, dayStart, visDate);
  555. }
  556. }
  557. }
  558. protected override void Render(HtmlTextWriter writer)
  559. {
  560. //TODO: Implement me
  561. globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
  562. DateTime visDate = GetEffectiveVisibleDate();
  563. DateTime firstDate = GetFirstCalendarDay(visDate);
  564. bool isEnabled = false;
  565. bool isHtmlTextWriter = false;
  566. if(Page == null || Site == null)
  567. {
  568. isEnabled = false;
  569. isHtmlTextWriter = false;
  570. } else
  571. {
  572. isEnabled = Enabled;
  573. isHtmlTextWriter = (writer.GetType() != typeof(HtmlTextWriter));
  574. }
  575. defaultTextColor = ForeColor;
  576. if(defaultTextColor == Color.Empty)
  577. defaultTextColor = Color.Black;
  578. Table calTable = new Table();
  579. calTable.ID = ID;
  580. calTable.CopyBaseAttributes(this);
  581. if(ControlStyleCreated)
  582. ApplyStyle(ControlStyle);
  583. calTable.Width = Width;
  584. calTable.Height = Height;
  585. calTable.CellSpacing = CellSpacing;
  586. calTable.CellPadding = CellPadding;
  587. if(ControlStyleCreated && ControlStyle.IsSet(WebControls.Style.BORDERWIDTH) && BorderWidth != Unit.Empty)
  588. {
  589. calTable.BorderWidth = BorderWidth;
  590. } else
  591. {
  592. calTable.BorderWidth = Unit.Pixel(1);
  593. }
  594. if(ShowGridLines)
  595. calTable.GridLines = GridLines.Both;
  596. else
  597. calTable.GridLines = GridLines.None;
  598. calTable.RenderBeginTag(writer);
  599. if(ShowTitle)
  600. RenderTitle(writer, visDate, SelectionMode, isEnabled);
  601. if(ShowDayHeader)
  602. RenderHeader(writer, firstDate, SelectionMode, isEnabled, isHtmlTextWriter);
  603. RenderAllDays(writer, firstDate, visDate, SelectionMode, isEnabled, isHtmlTextWriter);
  604. calTable.RenderEndTag(writer);
  605. }
  606. protected override ControlCollection CreateControlCollection()
  607. {
  608. return new EmptyControlCollection(this);
  609. }
  610. protected override void LoadViewState(object savedState)
  611. {
  612. if(savedState!=null)
  613. {
  614. if(ViewState["_CalendarSelectedDates"] != null)
  615. selectedDates = (SelectedDatesCollection)ViewState["_CalendarSelectedDates"];
  616. object[] states = (object[]) savedState;
  617. if(states[0] != null)
  618. base.LoadViewState(states[0]);
  619. if(states[1] != null)
  620. DayHeaderStyle.LoadViewState(states[1]);
  621. if(states[2] != null)
  622. DayStyle.LoadViewState(states[2]);
  623. if(states[3] != null)
  624. NextPrevStyle.LoadViewState(states[3]);
  625. if(states[4] != null)
  626. OtherMonthDayStyle.LoadViewState(states[4]);
  627. if(states[5] != null)
  628. SelectedDayStyle.LoadViewState(states[5]);
  629. if(states[6] != null)
  630. SelectorStyle.LoadViewState(states[6]);
  631. if(states[7] != null)
  632. TitleStyle.LoadViewState(states[7]);
  633. if(states[8] != null)
  634. TodayDayStyle.LoadViewState(states[8]);
  635. if(states[9] != null)
  636. WeekendDayStyle.LoadViewState(states[9]);
  637. }
  638. }
  639. protected override object SaveViewState()
  640. {
  641. ViewState["_CalendarSelectedDates"] = (SelectedDates.Count > 0 ? selectedDates : null);
  642. object[] states = new object[11];
  643. states[0] = base.SaveViewState();
  644. states[1] = (dayHeaderStyle == null ? null : dayHeaderStyle.SaveViewState());
  645. states[2] = (dayStyle == null ? null : dayStyle.SaveViewState());
  646. states[3] = (nextPrevStyle == null ? null : nextPrevStyle.SaveViewState());
  647. states[4] = (otherMonthDayStyle == null ? null : otherMonthDayStyle.SaveViewState());
  648. states[5] = (selectedDayStyle == null ? null : selectedDayStyle.SaveViewState());
  649. states[6] = (selectorStyle == null ? null : selectorStyle.SaveViewState());
  650. states[7] = (titleStyle == null ? null : titleStyle.SaveViewState());
  651. states[8] = (todayDayStyle == null ? null : todayDayStyle.SaveViewState());
  652. states[9] = (weekendDayStyle == null ? null : weekendDayStyle.SaveViewState());
  653. for(int i=0; i < states.Length; i++)
  654. {
  655. if(states[i]!=null)
  656. return states;
  657. }
  658. return null;
  659. }
  660. protected override void TrackViewState()
  661. {
  662. TrackViewState();
  663. if(titleStyle!=null)
  664. {
  665. titleStyle.TrackViewState();
  666. }
  667. if(nextPrevStyle!=null)
  668. {
  669. nextPrevStyle.TrackViewState();
  670. }
  671. if(dayStyle!=null)
  672. {
  673. dayStyle.TrackViewState();
  674. }
  675. if(dayHeaderStyle!=null)
  676. {
  677. dayHeaderStyle.TrackViewState();
  678. }
  679. if(todayDayStyle!=null)
  680. {
  681. todayDayStyle.TrackViewState();
  682. }
  683. if(weekendDayStyle!=null)
  684. {
  685. weekendDayStyle.TrackViewState();
  686. }
  687. if(otherMonthDayStyle!=null)
  688. {
  689. otherMonthDayStyle.TrackViewState();
  690. }
  691. if(selectedDayStyle!=null)
  692. {
  693. selectedDayStyle.TrackViewState();
  694. }
  695. if(selectorStyle!=null)
  696. {
  697. selectorStyle.TrackViewState();
  698. }
  699. }
  700. [MonoTODO("RenderAllDays")]
  701. private void RenderAllDays(HtmlTextWriter writer, DateTime firstDay, DateTime activeDate, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  702. {
  703. /*
  704. TableItemStyle weeksStyle;
  705. Unit size;
  706. string weeksCellData;
  707. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  708. if(isWeekMode)
  709. {
  710. weeksStyle = new TableItemStyle();
  711. weeksStyle.Width = Unit.Percentage(12);
  712. weeksStyle.HorizontalAlign = HorizontalAlign.Center;
  713. weeksStyle.CopyFrom(SelectorStyle);
  714. size = Unit.Percentage(14);
  715. if(!isDownLevel)
  716. weeksCellData = GetHtmlForCell(weeksCell, isActive);
  717. }
  718. bool dayRenderBool = false;
  719. if(GetType() != typeof(Calendar) || Events[DayRenderEvent] != null || !isDownLevel)
  720. dayRenderBool = true;
  721. string[] content = new string[0x01 << 4];
  722. int definedStyles = MASK_SELECTED;
  723. if(weekendStyle != null && !weekendStyle.IsEmpty)
  724. definedStyles |= MASK_WEEKEND;
  725. if(otherMonthStyle != null && !otherMonthStyle.IsEmpty)
  726. definedStyles |= MASK_OMONTH;
  727. if(todayDayStyle != null && todayDayStyle.IsEmpty)
  728. definedStyles |= MASK_TODAY;
  729. if(dayStyle != null && !dayStyle.IsEmpty)
  730. definedStyles |= MASK_DAY;
  731. bool selectDayBool = false;
  732. if(isActive && mode != CalendarSelectionMode.None)
  733. {
  734. selectDayBool = true;
  735. }
  736. for(int crr = 0; crr < 6; crr++)
  737. {
  738. writer.Write("<tr>");
  739. if(isWeekMode)
  740. {
  741. if(isDownLevel)
  742. {
  743. string cellText = GetCalendarLinkText("selectWeek" + crr.ToString(), SelectWeekText, isActive, weeksCell.ForeColor);
  744. weeksCell.Text = cellText;
  745. RenderCalendarCell(writer, weeksCell, cellText);
  746. } else
  747. {
  748. if(isActive)
  749. {
  750. writer.Write(String.Format(weeksCellData, "selectWeek" + crr.ToString(), SelectWeekText));
  751. } else
  752. {
  753. writer.Write(String.Format(weeksCellData, "selectWeek" + crr.ToString()));
  754. }
  755. }
  756. }
  757. for(int crc = 0; crc < 7; crc++)
  758. {
  759. // have to display for each day in the week.
  760. throw new NotImplementedException();
  761. }
  762. }
  763. */
  764. throw new NotImplementedException();
  765. }
  766. private int GetMask(CalendarDay day)
  767. {
  768. int retVal = MASK_DAY;
  769. if(day.IsSelected)
  770. retVal |= MASK_SELECTED;
  771. if(day.IsToday)
  772. retVal |= MASK_TODAY;
  773. if(day.IsOtherMonth)
  774. retVal |= MASK_OMONTH;
  775. if(day.IsWeekend)
  776. retVal |= MASK_WEEKEND;
  777. return retVal;
  778. }
  779. /// <remarks>
  780. /// Refers to the second line of the calendar, that contains a link
  781. /// to select whole month, and weekdays as defined by DayNameFormat
  782. /// </remarks>
  783. private void RenderHeader(HtmlTextWriter writer, DateTime firstDay, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  784. {
  785. writer.Write("<tr>");
  786. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  787. TableCell headerCell = new TableCell();
  788. headerCell.HorizontalAlign = HorizontalAlign.Center;
  789. string selMthText = String.Empty;
  790. if(isWeekMode)
  791. {
  792. headerCell.ApplyStyle(SelectorStyle);
  793. selMthText = GetCalendarLinkText("selectMonth", SelectMonthText, SelectorStyle.ForeColor, isActive);
  794. } else
  795. {
  796. headerCell.ApplyStyle(DayHeaderStyle);
  797. }
  798. RenderCalendarCell(writer, headerCell, selMthText);
  799. TableCell dayHeaderCell = new TableCell();
  800. dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
  801. string content = null;
  802. if(!isDownLevel)
  803. {
  804. content = GetHtmlForCell(dayHeaderCell, isActive);
  805. }
  806. int dayOfWeek = (int)globCal.GetDayOfWeek(firstDay);
  807. DateTimeFormatInfo currDTInfo = DateTimeFormatInfo.CurrentInfo;
  808. for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++)
  809. {
  810. DayOfWeek effDay = (DayOfWeek) Enum.ToObject(typeof(DayOfWeek),currDay % 7);
  811. string currDayContent = String.Empty;
  812. switch(DayNameFormat)
  813. {
  814. case DayNameFormat.Full: currDayContent = currDTInfo.GetDayName(effDay);
  815. break;
  816. case DayNameFormat.FirstLetter: currDayContent = currDTInfo.GetDayName(effDay).Substring(0,1);
  817. break;
  818. case DayNameFormat.FirstTwoLetters: currDayContent = currDTInfo.GetDayName(effDay).Substring(0,2);
  819. break;
  820. case DayNameFormat.Short: goto default;
  821. default: currDayContent = currDTInfo.GetAbbreviatedDayName(effDay);
  822. break;
  823. }
  824. if(isDownLevel)
  825. {
  826. RenderCalendarCell(writer, dayHeaderCell, currDayContent);
  827. } else
  828. {
  829. writer.Write(String.Format(content, currDayContent));
  830. }
  831. }
  832. writer.Write("</tr>");
  833. }
  834. private void RenderTitle(HtmlTextWriter writer, DateTime visibleDate, CalendarSelectionMode mode, bool isActive)
  835. {
  836. writer.Write("<tr>");
  837. Table innerTable = new Table();
  838. TableCell titleCell = new TableCell();
  839. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  840. titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
  841. titleCell.BackColor = Color.Silver;
  842. innerTable.GridLines = GridLines.None;
  843. innerTable.Width = Unit.Percentage(100);
  844. innerTable.CellSpacing = 0;
  845. ApplyTitleStyle(innerTable, titleCell, TitleStyle);
  846. innerTable.RenderBeginTag(writer);
  847. titleCell.RenderBeginTag(writer);
  848. writer.Write("<tr>");
  849. string prevContent = String.Empty;
  850. if(ShowNextPrevMonth)
  851. {
  852. TableCell prevCell = new TableCell();
  853. prevCell.Width = Unit.Percentage(15);
  854. prevCell.HorizontalAlign = HorizontalAlign.Left;
  855. if(NextPrevFormat == NextPrevFormat.CustomText)
  856. {
  857. prevContent = PrevMonthText;
  858. } else
  859. {
  860. int pMthInt = globCal.GetMonth(globCal.AddMonths(visibleDate, -1));
  861. if(NextPrevFormat == NextPrevFormat.FullMonth)
  862. prevContent = DateTimeFormatInfo.CurrentInfo.GetMonthName(pMthInt);
  863. else
  864. prevContent = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(pMthInt);
  865. }
  866. prevCell.ApplyStyle(NextPrevStyle);
  867. RenderCalendarCell(writer, prevCell, GetCalendarLinkText("prevMonth", prevContent, NextPrevStyle.ForeColor, isActive));
  868. }
  869. TableCell currCell = new TableCell();
  870. currCell.Width = Unit.Percentage(70);
  871. if(TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
  872. currCell.HorizontalAlign = HorizontalAlign.Center;
  873. else
  874. currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
  875. currCell.Wrap = TitleStyle.Wrap;
  876. string currMonthContent = String.Empty;
  877. if(TitleFormat == TitleFormat.Month)
  878. {
  879. currMonthContent = visibleDate.ToString("MMMM");
  880. } else
  881. {
  882. string cmcFmt = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
  883. if(cmcFmt.IndexOf(',') >= 0)
  884. {
  885. cmcFmt = "MMMM yyyy";
  886. }
  887. currMonthContent = visibleDate.ToString(cmcFmt);
  888. }
  889. string nextContent = String.Empty;
  890. if(ShowNextPrevMonth)
  891. {
  892. TableCell nextCell = new TableCell();
  893. nextCell.Width = Unit.Percentage(15);
  894. nextCell.HorizontalAlign = HorizontalAlign.Left;
  895. if(NextPrevFormat == NextPrevFormat.CustomText)
  896. {
  897. nextContent = PrevMonthText;
  898. } else
  899. {
  900. int nMthInt = globCal.GetMonth(globCal.AddMonths(visibleDate, 1));
  901. if(NextPrevFormat == NextPrevFormat.FullMonth)
  902. nextContent = DateTimeFormatInfo.CurrentInfo.GetMonthName(nMthInt);
  903. else
  904. nextContent = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(nMthInt);
  905. }
  906. nextCell.ApplyStyle(NextPrevStyle);
  907. RenderCalendarCell(writer, nextCell, GetCalendarLinkText("nextMonth", nextContent, NextPrevStyle.ForeColor, isActive));
  908. }
  909. writer.Write("</tr>");
  910. titleCell.RenderEndTag(writer);
  911. innerTable.RenderEndTag(writer);
  912. writer.Write("</tr>");
  913. }
  914. private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
  915. {
  916. if(style.BackColor != Color.Empty)
  917. {
  918. cell.BackColor = style.BackColor;
  919. }
  920. if(style.BorderStyle != BorderStyle.NotSet)
  921. {
  922. cell.BorderStyle = style.BorderStyle;
  923. }
  924. if(style.BorderColor != Color.Empty)
  925. {
  926. cell.BorderColor = style.BorderColor;
  927. }
  928. if(style.BorderWidth != Unit.Empty)
  929. {
  930. cell.BorderWidth = style.BorderWidth;
  931. }
  932. if(style.Height != Unit.Empty)
  933. {
  934. cell.Height = style.Height;
  935. }
  936. if(style.VerticalAlign != VerticalAlign.NotSet)
  937. {
  938. cell.VerticalAlign = style.VerticalAlign;
  939. }
  940. if(style.ForeColor != Color.Empty)
  941. {
  942. table.ForeColor = style.ForeColor;
  943. } else if(ForeColor != Color.Empty)
  944. {
  945. table.ForeColor = ForeColor;
  946. }
  947. table.Font.CopyFrom(style.Font);
  948. table.Font.MergeWith(Font);
  949. }
  950. private void RenderCalendarCell(HtmlTextWriter writer, TableCell cell, string text)
  951. {
  952. cell.RenderBeginTag(writer);
  953. writer.Write(text);
  954. cell.RenderEndTag(writer);
  955. }
  956. private DateTime GetFirstCalendarDay(DateTime visibleDate)
  957. {
  958. DayOfWeek firstDay = DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek;
  959. if(FirstDayOfWeek != FirstDayOfWeek.Default)
  960. {
  961. firstDay = (DayOfWeek) Enum.ToObject(typeof(DayOfWeek), (int)FirstDayOfWeek);
  962. }
  963. //FIXME: is (int)(Enum) correct?
  964. int days = (int)globCal.GetDayOfWeek(visibleDate) - (int)firstDay;
  965. if(days < 0)
  966. {
  967. days += 7;
  968. }
  969. return globCal.AddDays(visibleDate, -days);
  970. }
  971. private DateTime GetEffectiveVisibleDate()
  972. {
  973. DateTime dt = VisibleDate;
  974. if(dt.Equals(DateTime.MinValue))
  975. {
  976. dt = TodaysDate;
  977. }
  978. return new DateTime(globCal.GetYear(dt), globCal.GetMonth(dt), globCal.GetDayOfMonth(dt), globCal);
  979. }
  980. /// <summary>
  981. /// Creates text to be displayed, with all attributes if to be
  982. /// shown as a hyperlink
  983. /// </summary>
  984. private string GetCalendarLinkText(string eventArg, string text, Color foreground, bool isLink)
  985. {
  986. if(isLink)
  987. {
  988. StringBuilder dispVal = new StringBuilder();
  989. dispVal.Append("<a href=\"");
  990. dispVal.Append(Page.GetPostBackClientHyperlink(this, eventArg));
  991. dispVal.Append("\" style=\"color: ");
  992. if(foreground.IsEmpty)
  993. {
  994. dispVal.Append(ColorTranslator.ToHtml(defaultTextColor));
  995. } else
  996. {
  997. dispVal.Append(ColorTranslator.ToHtml(foreground));
  998. }
  999. dispVal.Append("\">");
  1000. dispVal.Append(text);
  1001. dispVal.Append("</a>");
  1002. return dispVal.ToString();
  1003. }
  1004. return text;
  1005. }
  1006. private string GetHtmlForCell(TableCell cell, bool showLinks)
  1007. {
  1008. StringWriter sw = new StringWriter();
  1009. HtmlTextWriter htw = new HtmlTextWriter(sw);
  1010. cell.RenderBeginTag(htw);
  1011. if(showLinks)
  1012. {
  1013. htw.Write(GetCalendarLinkText("{0}", "{1}", cell.ForeColor, showLinks));
  1014. } else
  1015. {
  1016. htw.Write("{0}");
  1017. }
  1018. cell.RenderEndTag(htw);
  1019. return sw.ToString();
  1020. }
  1021. internal void SelectRangeInternal(DateTime fromDate, DateTime toDate, DateTime visibleDate)
  1022. {
  1023. TimeSpan span = fromDate - toDate;
  1024. if(SelectedDates.Count != span.Days || SelectedDates[SelectedDates.Count - 1]!= toDate)
  1025. {
  1026. SelectedDates.SelectRange(fromDate, toDate);
  1027. OnSelectionChanged();
  1028. }
  1029. if(globCal.GetMonth(fromDate) == globCal.GetMonth(fromDate) && globCal.GetMonth(fromDate) != globCal.GetMonth(visibleDate))
  1030. {
  1031. VisibleDate = new DateTime(globCal.GetYear(fromDate), globCal.GetMonth(fromDate), 1, globCal);
  1032. OnVisibleMonthChanged(VisibleDate, visibleDate);
  1033. }
  1034. }
  1035. }
  1036. }