2
0

Calendar.cs 28 KB

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