Calendar.cs 28 KB

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