Calendar.cs 28 KB

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