Calendar.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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(): TrackViewState()
  656. {
  657. if(titleStyle!=null)
  658. {
  659. titleStyle.TrackViewState();
  660. }
  661. if(nextPrevStyle!=null)
  662. {
  663. nextPrevStyle.TrackViewState();
  664. }
  665. if(dayStyle!=null)
  666. {
  667. dayStyle.TrackViewState();
  668. }
  669. if(dayHeaderStyle!=null)
  670. {
  671. dayHeaderStyle.TrackViewState();
  672. }
  673. if(todayDayStyle!=null)
  674. {
  675. todayDayStyle.TrackViewState();
  676. }
  677. if(weekendDayStyle!=null)
  678. {
  679. weekendDayStyle.TrackViewState();
  680. }
  681. if(otherMonthDayStyle!=null)
  682. {
  683. otherMonthDayStyle.TrackViewState();
  684. }
  685. if(selectedDayStyle!=null)
  686. {
  687. selectedDayStyle.TrackViewState();
  688. }
  689. if(selectorStyle!=null)
  690. {
  691. selectorStyle.TrackViewState();
  692. }
  693. }
  694. [MonoTODO("Individual_Day_Rendering_Part_Left")]
  695. private void RenderAllDays(HtmlTextWriter writer, DateTime firstDay, DateTime activeDate, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  696. {
  697. TableCell weeksCell;
  698. string weeksCellData;
  699. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  700. if(isWeekMode)
  701. {
  702. weeksCell = new TableCell();
  703. weeksCell.Width = Unit.Percentage(12);
  704. weeksCell.HorizontalAlign = HorizontalAlign.Center;
  705. weeksCell.ApplyStyle(SelectorStyle);
  706. if(!isDownLevel)
  707. weeksCellData = GetHtmlForCell(weeksCell, isActive);
  708. }
  709. bool dayRenderBool = false;
  710. if(GetType() != typeof(Calendar) || Events[DayRenderEvent] != null || !isDownLevel)
  711. dayRenderBool = true;
  712. string[] content = new string[0x01 << 4];
  713. int definedStyles = MASK_SELECTED;
  714. if(weekendStyle != null && !weekendStyle.IsEmpty)
  715. definedStyles |= MASK_WEEKEND;
  716. if(otherMonthStyle != null && !otherMonthStyle.IsEmpty)
  717. definedStyles |= MASK_OMONTH;
  718. if(todayDayStyle != null && todayDayStyle.IsEmpty)
  719. definedStyles |= MASK_TODAY;
  720. if(dayStyle != null && !dayStyle.IsEmpty)
  721. definedStyles |= MASK_DAY;
  722. bool selectDayBool = false;
  723. if(isActive && mode != CalendarSelectionMode.None)
  724. {
  725. selectDayBool = true;
  726. }
  727. for(int crr = 0; crr < 6; crr++)
  728. {
  729. writer.Write("<tr>");
  730. if(isWeekMode)
  731. {
  732. if(isDownLevel)
  733. {
  734. string cellText = GetCalendarLinkText("selectWeek" + crr.ToString(), SelectWeekText, isActive, weeksCell.ForeColor);
  735. weeksCell.Text = cellText;
  736. RenderCalendarCell(writer, weeksCell, cellText);
  737. } else
  738. {
  739. if(isActive)
  740. {
  741. writer.Write(String.Format(weeksCellData, "selectWeek" + crr.ToString(), SelectWeekText));
  742. } else
  743. {
  744. writer.Write(String.Format(weeksCellData, "selectWeek" + crr.ToString()));
  745. }
  746. }
  747. }
  748. for(int crc = 0; crc < 7; crc++)
  749. {
  750. // have to display for each day in the week.
  751. throw new NotImplementedException();
  752. }
  753. }
  754. throw new NotImplementedException();
  755. }
  756. private int GetMask(CalendarDay day)
  757. {
  758. int retVal = MASK_DAY;
  759. if(day.IsSelected)
  760. retVal |= MASK_SELECTED;
  761. if(day.IsToday)
  762. retVal |= MASK_TODAY;
  763. if(day.IsOtherMonth)
  764. retVal |= MASK_OMONTH;
  765. if(day.IsWeekend)
  766. retVal |= MASK_WEEKEND;
  767. return retVal;
  768. }
  769. /// <remarks>
  770. /// Refers to the second line of the calendar, that contains a link
  771. /// to select whole month, and weekdays as defined by DayNameFormat
  772. /// </remarks>
  773. private void RenderHeader(HtmlTextWriter writer, DateTime firstDay, CalendarSelectionMode mode, bool isActive, bool isDownLevel)
  774. {
  775. writer.Write("<tr>");
  776. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  777. TableCell headerCell = new TableCell();
  778. headerCell.HorizontalAlign = HorizontalAlign.Center;
  779. string selMthText = String.Empty;
  780. if(isWeekMode)
  781. {
  782. headerCell.ApplyStyle(SelectorStyle);
  783. selMthText = GetCalendarLinkText("selectMonth", SelectMonthText, isActive, SelectorStyle.ForeColor);
  784. } else
  785. {
  786. headerCell.ApplyStyle(DayHeaderStyle);
  787. }
  788. RenderCalendarCell(writer, headerCell, selMthText);
  789. TableCell dayHeaderCell = new TableCell();
  790. dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
  791. string content = null;
  792. if(!isDownLevel)
  793. {
  794. content = GetHtmlForCell(dayHeaderCell, isActive);
  795. }
  796. int dayOfWeek = (int)globCal.GetDayOfWeek(firstDay);
  797. for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++)
  798. {
  799. int effDay = (currDay % 7);
  800. string currDayContent = String.Empty;
  801. switch(DayNameFormat)
  802. {
  803. DayNameFormat.Full: currDayContent = DateTimeFormatInfo.GetDayName(effDay);
  804. break;
  805. DayNameFormat.FirstLetter: currDayContent = DateTimeFormatInfo.GetDayName(effDay).Substring(0,1);
  806. break;
  807. DayNameFormat.FirstTwoLetters: currDayContent = DateTimeFormatInfo.GetDayName(effDay).Substring(0,2);
  808. break;
  809. DayNameFormat.Short:
  810. default: currDayContent = DateTimeFormatInfo.GetAbbreviatedDayName(effDay);
  811. break;
  812. }
  813. if(isDownLevel)
  814. {
  815. RenderCalendarCell(writer, dayHeaderCell, currDayContent);
  816. } else
  817. {
  818. writer.Write(String.Format(content, currDayContent);
  819. }
  820. }
  821. writer.Write("</tr>");
  822. }
  823. private void RenderTitle(HtmlTextWriter writer, DateTime visibleDate, CalendarSelectionMode mode, bool isActive)
  824. {
  825. writer.Write("<tr>");
  826. Table innerTable = new Table();
  827. TableCell titleCell = new TableCell();
  828. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek || mode == CalendarSelectionMode.DayWeekMonth);
  829. titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
  830. titleCell.BackColor = "Silver";
  831. innerTable.GridLines = GridLine.None;
  832. innerTable.Width = Unit.Percentage(100);
  833. innerTable.CellSpacing = 0;
  834. ApplyTitleStyle(innerTable, titleCell, TitleStyle);
  835. innerTable.RenderBeginTag(writer);
  836. titleCell.RenderBeginTag(writer);
  837. writer.Write("<tr>");
  838. string prevContent = String.Empty;
  839. if(ShowNextPrevMonth)
  840. {
  841. TableCell prevCell = new TableCell();
  842. prevCell.Width = Unit.Percentage(15);
  843. prevCell.HorizontalAlign = HorizontalAlign.Left;
  844. if(NextPrevFormat == NextPrevFormat.CustomText)
  845. {
  846. prevContent = PrevMonthText;
  847. } else
  848. {
  849. int pMthInt = globCal.GetMonth(globCal.AddMonths(visibleDate, -1));
  850. if(NextPrevFormat == NextPrevFormat.FullText)
  851. prevContent = DateTimeFormatInfo.CurrentInfo.GetMonthName(pMthInt);
  852. else
  853. prevContent = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(pMthInt);
  854. }
  855. prevCell.ApplyStyle(NextPrevStyle);
  856. RenderCalendarCell(writer, prevCell, GetCalendarLinkText("prevMonth", prevContent, isActive, NextPrevStyle.ForeColor));
  857. }
  858. TableCell currCell = new TableCell();
  859. currCell.Width = Unit.Percentage(70);
  860. if(TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
  861. currCell.HorizontalAlign = HorizontalAlign.Center;
  862. else
  863. currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
  864. currCell.Wrap = TitleStyle.Wrap;
  865. string currMonthContent = String.Empty;
  866. if(TitleFormat == TitleFormat.Month)
  867. {
  868. currMonthContent = visibleDate.ToString("MMMM");
  869. } else
  870. {
  871. string cmcFmt = DateTimeFormatInfo.CurrentInfo.YearMonthPattern;
  872. if(cmcFmt.IndexOf(',') >= 0)
  873. {
  874. cmcFmt = "MMMM yyyy";
  875. }
  876. currMonthContent = visibleDate.ToString(cmcFmt);
  877. }
  878. string nextContent = String.Empty;
  879. if(ShowNextPrevMonth)
  880. {
  881. TableCell nextCell = new TableCell();
  882. nextCell.Width = Unit.Percentage(15);
  883. nextCell.HorizontalAlign = HorizontalAlign.Left;
  884. if(NextPrevFormat == NextPrevFormat.CustomText)
  885. {
  886. nextContent = PrevMonthText;
  887. } else
  888. {
  889. int nMthInt = globCal.GetMonth(globCal.AddMonths(visibleDate, 1));
  890. if(NextPrevFormat == NextPrevFormat.FullText)
  891. nextContent = DateTimeFormatInfo.CurrentInfo.GetMonthName(nMthInt);
  892. else
  893. nextContent = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(nMthInt);
  894. }
  895. nextCell.ApplyStyle(NextPrevStyle);
  896. RenderCalendarCell(writer, nextCell, GetCalendarLinkText("nextMonth", nextContent, isActive, NextPrevStyle.ForeColor));
  897. }
  898. writer.Write("</tr>");
  899. titleCell.RenderEndTag(writer);
  900. innerTable.RenderEndTag(writer);
  901. writer.Write("</tr>");
  902. }
  903. private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
  904. {
  905. if(style.BackColor != Color.Empty)
  906. {
  907. cell.BackColor = style.BackColor;
  908. }
  909. if(style.BorderStyle != BorderStyle.NotSet)
  910. {
  911. cell.BorderStyle = style.BorderStyle;
  912. }
  913. if(style.BorderColor != Color.Empty)
  914. {
  915. cell.BorderColor = style.BorderColor;
  916. }
  917. if(style.BorderWidth != Unit.Empty)
  918. {
  919. cell.BorderWidth = style.BorderWidth;
  920. }
  921. if(style.Height != Unit.Empty)
  922. {
  923. cell.Height = style.Height;
  924. }
  925. if(style.VerticalAlign != VerticalAlign.NotSet)
  926. {
  927. cell.VerticalAlign = style.VerticalAlign;
  928. }
  929. if(style.ForeColor != Color.Empty)
  930. {
  931. table.ForeColor = style.ForeColor;
  932. } else if(ForeColor != Color.Empty)
  933. {
  934. table.ForeColor = ForeColor;
  935. }
  936. table.Font.CopyFrom(style.Font);
  937. table.Font.MergeWith(Font);
  938. }
  939. private void RenderCalendarCell(HtmlTextWriter writer, TableCell cell, string text)
  940. {
  941. cell.RenderBeginTag(writer);
  942. writer.Write(text);
  943. cell.RenderEndTag(writer);
  944. }
  945. private DateTime GetFirstCalendarDay(DateTime visibleDate)
  946. {
  947. DayOfWeek firstDay = ( FirstDayOfWeek == FirstDayOfWeek.Default ? DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek : FirstDayOfWeek);
  948. //FIXME: is (int)(Enum) correct?
  949. int days = (int)globCal.GetDayOfWeek(visibleDate) - (int)firstDay;
  950. if(days < 0)
  951. {
  952. days += 7;
  953. }
  954. return globCal.AddDays(visibleDate, -days);
  955. }
  956. private DateTime GetEffectiveVisibleDate()
  957. {
  958. DateTime dt = VisibleDate;
  959. if(dt.Equals(DateTime.MinValue))
  960. {
  961. dt = TodaysDate;
  962. }
  963. return new DateTime(globCal.GetYear(dt), globCal.GetMonth(dt), globCal.GetDayOfMonth(dt), globCal);
  964. }
  965. /// <summary>
  966. /// Creates text to be displayed, with all attributes if to be
  967. /// shown as a hyperlink
  968. /// </summary>
  969. private string GetCalendarLinkText(string eventArg, string text, Color foreground, bool isLink)
  970. {
  971. if(isLink)
  972. {
  973. StringBuilder dispVal = new StringBuilder();
  974. dispVal.Append("<a href=\"");
  975. dispVal.Append(Page.GetPostBackClientHyperlink(this, eventArg));
  976. dispVal.Append("\" style=\"color: ");
  977. if(foreground.IsEmpty)
  978. {
  979. dispVal.Append(ColorTranslator.ToHtml(defaultTextColor));
  980. } else
  981. {
  982. dispVal.Append(ColorTranslator.ToHtml(foreground));
  983. }
  984. dispVal.Append("\">");
  985. dispVal.Append(text);
  986. dispVal.Append("</a>");
  987. return dispVal.ToString();
  988. }
  989. return text;
  990. }
  991. private string GetHtmlForCell(TableCell cell, bool showLinks)
  992. {
  993. StringWriter sw = new StringWriter();
  994. HtmlTextWriter htw = new HtmlTextWriter(sw);
  995. cell.RenderBeginTag(htw);
  996. if(showLinks)
  997. {
  998. htw.Write(GetCalendarLinkText("{0}", "{1}", cell.ForeColor, showLinks));
  999. } else
  1000. {
  1001. htw.Write("{0}");
  1002. }
  1003. cell.RenderEndTag(htw);
  1004. return sw.ToString();
  1005. }
  1006. internal DateTime SelectRangeInternal(DateTime fromDate, DateTime toDate, DateTime visibleDate)
  1007. {
  1008. TimeSpan span = fromDate - toDate;
  1009. if(SelectedDates.Count != span.Days || SelectedDates[SelectedDate.Count - 1]!= toDate)
  1010. {
  1011. SelectedDates.SelectRange(fromDate, toDate);
  1012. OnSelectionChanged();
  1013. }
  1014. if(globCal.GetMonth(fromDate) == globCal.GetMonth(fromDate) && globCal.GetMonth(fromDate) != globCal.GetMonth(visibleDate)
  1015. {
  1016. VisibleDate = new DateTime(globCal.GetYear(fromDate), globCal.getMonth(fromDate), 1, globCal);
  1017. OnVisibleMonthChanged(VisibleDate, visibleDate);
  1018. }
  1019. }
  1020. }
  1021. }