Calendar.cs 28 KB

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