Calendar.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: Calendar
  4. *
  5. * Authors: Gaurav Vaish, Gonzalo Paniagua Javier
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 98%
  10. *
  11. * (C) Gaurav Vaish (2001)
  12. * (c) 2002 Ximian, Inc. (http://www.ximian.com)
  13. */
  14. using System;
  15. using System.IO;
  16. using System.Collections;
  17. using System.Globalization;
  18. using System.Text;
  19. using System.Web;
  20. using System.Web.UI;
  21. using System.Drawing;
  22. using System.ComponentModel;
  23. namespace System.Web.UI.WebControls
  24. {
  25. [DefaultEvent("SelectionChanged")]
  26. [DefaultProperty("SelectedDate")]
  27. //TODO: [Designer("??")]
  28. //[DataBindingHandler("??")]
  29. public class Calendar : WebControl, IPostBackEventHandler
  30. {
  31. //
  32. private TableItemStyle dayHeaderStyle;
  33. private TableItemStyle dayStyle;
  34. private TableItemStyle nextPrevStyle;
  35. private TableItemStyle otherMonthDayStyle;
  36. private SelectedDatesCollection selectedDates;
  37. private ArrayList selectedDatesList;
  38. private TableItemStyle selectedDayStyle;
  39. private TableItemStyle selectorStyle;
  40. private TableItemStyle titleStyle;
  41. private TableItemStyle todayDayStyle;
  42. private TableItemStyle weekendDayStyle;
  43. private static readonly object DayRenderEvent = new object();
  44. private static readonly object SelectionChangedEvent = new object();
  45. private static readonly object VisibleMonthChangedEvent = new object();
  46. private Color defaultTextColor;
  47. private System.Globalization.Calendar globCal;
  48. private DateTimeFormatInfo infoCal = DateTimeFormatInfo.CurrentInfo;
  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.Date;
  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.Date;
  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. if(eventArgument == "nextMonth")
  510. {
  511. VisibleDate = globCal.AddMonths(visDate, 1);
  512. OnVisibleMonthChanged(VisibleDate, visDate);
  513. return;
  514. }
  515. if(eventArgument == "prevMonth")
  516. {
  517. VisibleDate = globCal.AddMonths(visDate, -1);
  518. OnVisibleMonthChanged(VisibleDate, visDate);
  519. return;
  520. }
  521. if(eventArgument == "selectMonth")
  522. {
  523. DateTime oldDate = new DateTime(globCal.GetYear(visDate), globCal.GetMonth(visDate), 1, globCal);
  524. SelectRangeInternal(oldDate, globCal.AddDays(globCal.AddMonths(oldDate, 1), -1), visDate);
  525. return;
  526. }
  527. if(String.Compare(eventArgument, 0, "selectWeek", 0, "selectWeek".Length)==0)
  528. {
  529. int week = -1;
  530. try
  531. {
  532. week = Int32.Parse(eventArgument.Substring("selectWeek".Length));
  533. } catch(Exception e)
  534. {
  535. }
  536. if(week >= 0 && week <= 5)
  537. {
  538. DateTime weekStart = globCal.AddDays(GetFirstCalendarDay(visDate), week * 7);
  539. SelectRangeInternal(weekStart, globCal.AddDays(weekStart, 6), visDate);
  540. }
  541. return;
  542. }
  543. if(String.Compare(eventArgument, 0, "selectDay", 0, "selectDay".Length)==0)
  544. {
  545. int day = -1;
  546. try
  547. {
  548. day = Int32.Parse(eventArgument.Substring("selectDay".Length));
  549. } catch(Exception e)
  550. {
  551. }
  552. if(day >= 0 && day <= 42)
  553. {
  554. DateTime dayStart = globCal.AddDays(GetFirstCalendarDay(visDate), day);
  555. SelectRangeInternal(dayStart, dayStart, visDate);
  556. }
  557. }
  558. }
  559. protected override void Render(HtmlTextWriter writer)
  560. {
  561. //TODO: Implement me
  562. globCal = DateTimeFormatInfo.CurrentInfo.Calendar;
  563. DateTime visDate = GetEffectiveVisibleDate();
  564. DateTime firstDate = GetFirstCalendarDay(visDate);
  565. bool isEnabled;
  566. bool isHtmlTextWriter;
  567. //FIXME: when Control.Site works, reactivate this
  568. //if (Page == null || Site == null) {
  569. // isEnabled = false;
  570. // isHtmlTextWriter = false;
  571. //} else {
  572. isEnabled = Enabled;
  573. isHtmlTextWriter = (writer.GetType() != typeof(HtmlTextWriter));
  574. //}
  575. defaultTextColor = ForeColor;
  576. if(defaultTextColor == Color.Empty)
  577. defaultTextColor = Color.Black;
  578. Table calTable = new Table ();
  579. calTable.ID = ID;
  580. calTable.CopyBaseAttributes(this);
  581. if(ControlStyleCreated)
  582. ApplyStyle(ControlStyle);
  583. calTable.Width = Width;
  584. calTable.Height = Height;
  585. calTable.CellSpacing = CellSpacing;
  586. calTable.CellPadding = CellPadding;
  587. if (ControlStyleCreated &&
  588. ControlStyle.IsSet (WebControls.Style.BORDERWIDTH) &&
  589. BorderWidth != Unit.Empty)
  590. calTable.BorderWidth = BorderWidth;
  591. else
  592. calTable.BorderWidth = Unit.Pixel(1);
  593. if (ShowGridLines)
  594. calTable.GridLines = GridLines.Both;
  595. else
  596. calTable.GridLines = GridLines.None;
  597. calTable.RenderBeginTag (writer);
  598. if (ShowTitle)
  599. RenderTitle (writer, visDate, SelectionMode, isEnabled);
  600. if (ShowDayHeader)
  601. RenderHeader (writer, firstDate, SelectionMode, isEnabled, isHtmlTextWriter);
  602. RenderAllDays (writer, firstDate, visDate, SelectionMode, isEnabled, isHtmlTextWriter);
  603. calTable.RenderEndTag(writer);
  604. }
  605. protected override ControlCollection CreateControlCollection()
  606. {
  607. return new EmptyControlCollection(this);
  608. }
  609. protected override void LoadViewState(object savedState)
  610. {
  611. if (savedState!=null) {
  612. object[] states = (object[]) savedState;
  613. if(states[0] != null)
  614. base.LoadViewState(states[0]);
  615. if(states[1] != null)
  616. DayHeaderStyle.LoadViewState(states[1]);
  617. if(states[2] != null)
  618. DayStyle.LoadViewState(states[2]);
  619. if(states[3] != null)
  620. NextPrevStyle.LoadViewState(states[3]);
  621. if(states[4] != null)
  622. OtherMonthDayStyle.LoadViewState(states[4]);
  623. if(states[5] != null)
  624. SelectedDayStyle.LoadViewState(states[5]);
  625. if(states[6] != null)
  626. SelectorStyle.LoadViewState(states[6]);
  627. if(states[7] != null)
  628. TitleStyle.LoadViewState(states[7]);
  629. if(states[8] != null)
  630. TodayDayStyle.LoadViewState(states[8]);
  631. if(states[9] != null)
  632. WeekendDayStyle.LoadViewState(states[9]);
  633. ArrayList dateList = ViewState ["_CalendarSelectedDates"] as ArrayList;
  634. if (dateList != null)
  635. selectedDates = new SelectedDatesCollection (dateList);
  636. }
  637. }
  638. protected override object SaveViewState()
  639. {
  640. ViewState["_CalendarSelectedDates"] = (SelectedDates.Count > 0 ? selectedDates.GetDateList () : null);
  641. object[] states = new object[11];
  642. states[0] = base.SaveViewState();
  643. states[1] = (dayHeaderStyle == null ? null : dayHeaderStyle.SaveViewState());
  644. states[2] = (dayStyle == null ? null : dayStyle.SaveViewState());
  645. states[3] = (nextPrevStyle == null ? null : nextPrevStyle.SaveViewState());
  646. states[4] = (otherMonthDayStyle == null ? null : otherMonthDayStyle.SaveViewState());
  647. states[5] = (selectedDayStyle == null ? null : selectedDayStyle.SaveViewState());
  648. states[6] = (selectorStyle == null ? null : selectorStyle.SaveViewState());
  649. states[7] = (titleStyle == null ? null : titleStyle.SaveViewState());
  650. states[8] = (todayDayStyle == null ? null : todayDayStyle.SaveViewState());
  651. states[9] = (weekendDayStyle == null ? null : weekendDayStyle.SaveViewState());
  652. for(int i=0; i < states.Length; i++)
  653. {
  654. if(states[i]!=null)
  655. return states;
  656. }
  657. return null;
  658. }
  659. protected override void TrackViewState()
  660. {
  661. base.TrackViewState();
  662. if(titleStyle!=null)
  663. {
  664. titleStyle.TrackViewState();
  665. }
  666. if(nextPrevStyle!=null)
  667. {
  668. nextPrevStyle.TrackViewState();
  669. }
  670. if(dayStyle!=null)
  671. {
  672. dayStyle.TrackViewState();
  673. }
  674. if(dayHeaderStyle!=null)
  675. {
  676. dayHeaderStyle.TrackViewState();
  677. }
  678. if(todayDayStyle!=null)
  679. {
  680. todayDayStyle.TrackViewState();
  681. }
  682. if(weekendDayStyle!=null)
  683. {
  684. weekendDayStyle.TrackViewState();
  685. }
  686. if(otherMonthDayStyle!=null)
  687. {
  688. otherMonthDayStyle.TrackViewState();
  689. }
  690. if(selectedDayStyle!=null)
  691. {
  692. selectedDayStyle.TrackViewState();
  693. }
  694. if(selectorStyle!=null)
  695. {
  696. selectorStyle.TrackViewState();
  697. }
  698. }
  699. private void RenderAllDays (HtmlTextWriter writer,
  700. DateTime firstDay,
  701. DateTime activeDate,
  702. CalendarSelectionMode mode,
  703. bool isActive,
  704. bool isDownLevel)
  705. {
  706. TableItemStyle weeksStyle;
  707. TableCell weeksCell = new TableCell ();
  708. TableItemStyle weekendStyle = WeekendDayStyle;
  709. TableItemStyle otherMonthStyle = OtherMonthDayStyle;
  710. Unit size;
  711. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
  712. mode == CalendarSelectionMode.DayWeekMonth);
  713. if (isWeekMode) {
  714. weeksStyle = new TableItemStyle ();
  715. weeksStyle.Width = Unit.Percentage (12);
  716. weeksStyle.HorizontalAlign = HorizontalAlign.Center;
  717. weeksStyle.CopyFrom (SelectorStyle);
  718. size = Unit.Percentage (12);
  719. } else {
  720. size = Unit.Percentage (14);
  721. }
  722. TableItemStyle [] styles = new TableItemStyle [16];
  723. int definedStyles = MASK_SELECTED;
  724. if (weekendStyle != null && !weekendStyle.IsEmpty)
  725. definedStyles |= MASK_WEEKEND;
  726. if (otherMonthStyle != null && !otherMonthStyle.IsEmpty)
  727. definedStyles |= MASK_OMONTH;
  728. if (todayDayStyle != null && !todayDayStyle.IsEmpty)
  729. definedStyles |= MASK_TODAY;
  730. if (dayStyle != null && !dayStyle.IsEmpty)
  731. definedStyles |= MASK_DAY;
  732. bool isCalendar = GetType () == typeof (Calendar);
  733. int month = globCal.GetMonth (activeDate);
  734. DateTime currentDay = firstDay;
  735. for (int crr = 0; crr < 6; crr++) {
  736. writer.Write ("<tr>");
  737. if (isWeekMode) {
  738. string cellText = GetCalendarLinkText (
  739. "selectWeek" + crr.ToString (),
  740. SelectWeekText,
  741. weeksCell.ForeColor,
  742. isActive);
  743. weeksCell.Text = cellText;
  744. RenderCalendarCell (writer, weeksCell, cellText);
  745. }
  746. for (int weekDay = 0; weekDay < 7; weekDay++) {
  747. string dayString = currentDay.Day.ToString ();
  748. CalendarDay calDay =
  749. new CalendarDay (
  750. currentDay,
  751. weekDay < 2,
  752. currentDay == TodaysDate,
  753. selectedDates.Contains (currentDay),
  754. globCal.GetMonth (currentDay) != month,
  755. dayString
  756. );
  757. int dayStyles = GetMask (calDay) & definedStyles;
  758. TableItemStyle currentDayStyle = styles [dayStyles];
  759. if (currentDayStyle == null) {
  760. currentDayStyle = new TableItemStyle ();
  761. if ((dayStyles & MASK_DAY) != 0)
  762. currentDayStyle.CopyFrom (DayStyle);
  763. if ((dayStyles & MASK_WEEKEND) != 0)
  764. currentDayStyle.CopyFrom (WeekendDayStyle);
  765. if ((dayStyles & MASK_TODAY) != 0)
  766. currentDayStyle.CopyFrom (TodayDayStyle);
  767. if ((dayStyles & MASK_OMONTH) != 0)
  768. currentDayStyle.CopyFrom (OtherMonthDayStyle);
  769. if ((dayStyles & MASK_SELECTED) != 0) {
  770. currentDayStyle.ForeColor = Color.White;
  771. currentDayStyle.BackColor = Color.Silver;
  772. currentDayStyle.CopyFrom (SelectedDayStyle);
  773. }
  774. currentDayStyle.Width = size;
  775. currentDayStyle.HorizontalAlign = HorizontalAlign.Center;
  776. }
  777. TableCell dayCell = new TableCell ();
  778. dayCell.ApplyStyle (currentDayStyle);
  779. calDay.IsSelectable = isActive;
  780. OnDayRender (dayCell, calDay);
  781. if (isActive)
  782. dayCell.Text = GetCalendarLinkText (
  783. "selectDay" + (crr * 7 + weekDay),
  784. dayString,
  785. dayCell.ForeColor,
  786. true);
  787. else
  788. dayCell.Text = dayString;
  789. dayCell.RenderControl (writer);
  790. currentDay = globCal.AddDays (currentDay, 1);
  791. }
  792. writer.Write("</tr>");
  793. }
  794. }
  795. private int GetMask (CalendarDay day)
  796. {
  797. int retVal = MASK_DAY;
  798. if(day.IsSelected)
  799. retVal |= MASK_SELECTED;
  800. if(day.IsToday)
  801. retVal |= MASK_TODAY;
  802. if(day.IsOtherMonth)
  803. retVal |= MASK_OMONTH;
  804. if(day.IsWeekend)
  805. retVal |= MASK_WEEKEND;
  806. return retVal;
  807. }
  808. /// <remarks>
  809. /// Refers to the second line of the calendar, that contains a link
  810. /// to select whole month, and weekdays as defined by DayNameFormat
  811. /// </remarks>
  812. private void RenderHeader (HtmlTextWriter writer,
  813. DateTime firstDay,
  814. CalendarSelectionMode mode,
  815. bool isActive,
  816. bool isDownLevel)
  817. {
  818. writer.Write("<tr>");
  819. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
  820. mode == CalendarSelectionMode.DayWeekMonth);
  821. TableCell headerCell = new TableCell ();
  822. headerCell.HorizontalAlign = HorizontalAlign.Center;
  823. string selMthText = String.Empty;
  824. if (isWeekMode) {
  825. if (mode == CalendarSelectionMode.DayWeekMonth) {
  826. headerCell.ApplyStyle (SelectorStyle);
  827. selMthText = GetCalendarLinkText ("selectMonth",
  828. SelectMonthText,
  829. SelectorStyle.ForeColor,
  830. isActive);
  831. } else {
  832. headerCell.ApplyStyle (DayHeaderStyle);
  833. selMthText = String.Empty;
  834. }
  835. RenderCalendarCell (writer, headerCell, selMthText);
  836. }
  837. TableCell dayHeaderCell = new TableCell ();
  838. dayHeaderCell.HorizontalAlign = HorizontalAlign.Center;
  839. int dayOfWeek = (int) globCal.GetDayOfWeek (firstDay);
  840. DateTimeFormatInfo currDTInfo = DateTimeFormatInfo.CurrentInfo;
  841. for(int currDay = dayOfWeek; currDay < dayOfWeek + 7; currDay++) {
  842. DayOfWeek effDay = (DayOfWeek) Enum.ToObject (typeof (DayOfWeek), currDay % 7);
  843. string currDayContent;
  844. switch(DayNameFormat) {
  845. case DayNameFormat.Full:
  846. currDayContent = currDTInfo.GetDayName (effDay);
  847. break;
  848. case DayNameFormat.FirstLetter:
  849. currDayContent = currDTInfo.GetDayName (effDay).Substring (0,1);
  850. break;
  851. case DayNameFormat.FirstTwoLetters:
  852. currDayContent = currDTInfo.GetDayName (effDay).Substring (0,2);
  853. break;
  854. case DayNameFormat.Short:
  855. goto default;
  856. default:
  857. currDayContent = currDTInfo.GetAbbreviatedDayName (effDay);
  858. break;
  859. }
  860. RenderCalendarCell(writer, dayHeaderCell, currDayContent);
  861. }
  862. writer.Write ("</tr>");
  863. }
  864. private void RenderTitle (HtmlTextWriter writer,
  865. DateTime visibleDate,
  866. CalendarSelectionMode mode,
  867. bool isActive)
  868. {
  869. writer.Write("<tr>");
  870. Table innerTable = new Table ();
  871. TableCell titleCell = new TableCell();
  872. bool isWeekMode = (mode == CalendarSelectionMode.DayWeek ||
  873. mode == CalendarSelectionMode.DayWeekMonth);
  874. titleCell.ColumnSpan = (isWeekMode ? 8 : 7);
  875. titleCell.BackColor = Color.Silver;
  876. innerTable.GridLines = GridLines.None;
  877. innerTable.Width = Unit.Percentage (100);
  878. innerTable.CellSpacing = 0;
  879. ApplyTitleStyle (innerTable, titleCell, TitleStyle);
  880. titleCell.RenderBeginTag (writer);
  881. innerTable.RenderBeginTag (writer);
  882. writer.Write ("<tr>");
  883. string prevContent = String.Empty;
  884. if (ShowNextPrevMonth) {
  885. TableCell prevCell = new TableCell ();
  886. prevCell.Width = Unit.Percentage (15);
  887. prevCell.HorizontalAlign = HorizontalAlign.Left;
  888. if (NextPrevFormat == NextPrevFormat.CustomText) {
  889. prevContent = PrevMonthText;
  890. } else {
  891. int pMthInt = globCal.GetMonth(globCal.AddMonths (visibleDate, -1));
  892. if (NextPrevFormat == NextPrevFormat.FullMonth)
  893. prevContent = infoCal.GetMonthName (pMthInt);
  894. else
  895. prevContent = infoCal.GetAbbreviatedMonthName (pMthInt);
  896. }
  897. prevCell.ApplyStyle (NextPrevStyle);
  898. RenderCalendarCell (writer,
  899. prevCell,
  900. GetCalendarLinkText ("prevMonth",
  901. prevContent,
  902. NextPrevStyle.ForeColor,
  903. isActive)
  904. );
  905. }
  906. TableCell currCell = new TableCell ();
  907. currCell.Width = Unit.Percentage (70);
  908. if (TitleStyle.HorizontalAlign == HorizontalAlign.NotSet)
  909. currCell.HorizontalAlign = HorizontalAlign.Center;
  910. else
  911. currCell.HorizontalAlign = TitleStyle.HorizontalAlign;
  912. currCell.Wrap = TitleStyle.Wrap;
  913. string currMonthContent = String.Empty;
  914. if (TitleFormat == TitleFormat.Month) {
  915. currMonthContent = visibleDate.ToString ("MMMM");
  916. } else {
  917. string cmcFmt = infoCal.YearMonthPattern;
  918. if (cmcFmt.IndexOf (',') >= 0)
  919. cmcFmt = "MMMM yyyy";
  920. currMonthContent = visibleDate.ToString (cmcFmt);
  921. }
  922. RenderCalendarCell (writer, currCell, currMonthContent);
  923. string nextContent = String.Empty;
  924. if (ShowNextPrevMonth) {
  925. TableCell nextCell = new TableCell ();
  926. nextCell.Width = Unit.Percentage(15);
  927. nextCell.HorizontalAlign = HorizontalAlign.Left;
  928. if (NextPrevFormat == NextPrevFormat.CustomText) {
  929. nextContent = NextMonthText;
  930. } else {
  931. int nMthInt = globCal.GetMonth (globCal.AddMonths (visibleDate, 1));
  932. if(NextPrevFormat == NextPrevFormat.FullMonth)
  933. nextContent = infoCal.GetMonthName(nMthInt);
  934. else
  935. nextContent = infoCal.GetAbbreviatedMonthName(nMthInt);
  936. }
  937. nextCell.ApplyStyle(NextPrevStyle);
  938. RenderCalendarCell (writer,
  939. nextCell,
  940. GetCalendarLinkText ("nextMonth",
  941. nextContent,
  942. NextPrevStyle.ForeColor,
  943. isActive)
  944. );
  945. }
  946. writer.Write("</tr>");
  947. innerTable.RenderEndTag(writer);
  948. titleCell.RenderEndTag(writer);
  949. writer.Write("</tr>");
  950. }
  951. private void ApplyTitleStyle(Table table, TableCell cell, TableItemStyle style)
  952. {
  953. if(style.BackColor != Color.Empty)
  954. {
  955. cell.BackColor = style.BackColor;
  956. }
  957. if(style.BorderStyle != BorderStyle.NotSet)
  958. {
  959. cell.BorderStyle = style.BorderStyle;
  960. }
  961. if(style.BorderColor != Color.Empty)
  962. {
  963. cell.BorderColor = style.BorderColor;
  964. }
  965. if(style.BorderWidth != Unit.Empty)
  966. {
  967. cell.BorderWidth = style.BorderWidth;
  968. }
  969. if(style.Height != Unit.Empty)
  970. {
  971. cell.Height = style.Height;
  972. }
  973. if(style.VerticalAlign != VerticalAlign.NotSet)
  974. {
  975. cell.VerticalAlign = style.VerticalAlign;
  976. }
  977. if(style.ForeColor != Color.Empty)
  978. {
  979. table.ForeColor = style.ForeColor;
  980. } else if(ForeColor != Color.Empty)
  981. {
  982. table.ForeColor = ForeColor;
  983. }
  984. table.Font.CopyFrom(style.Font);
  985. table.Font.MergeWith(Font);
  986. }
  987. private void RenderCalendarCell (HtmlTextWriter writer, TableCell cell, string text)
  988. {
  989. cell.RenderBeginTag(writer);
  990. writer.Write(text);
  991. cell.RenderEndTag(writer);
  992. }
  993. private DateTime GetFirstCalendarDay(DateTime visibleDate)
  994. {
  995. int fow = (int) FirstDayOfWeek;
  996. if (fow == 7)
  997. fow = (int) infoCal.FirstDayOfWeek;
  998. int days = (int) globCal.GetDayOfWeek (visibleDate) - fow;
  999. if (days <= 0)
  1000. days += 7;
  1001. return globCal.AddDays (visibleDate, -days);
  1002. }
  1003. private DateTime GetEffectiveVisibleDate()
  1004. {
  1005. DateTime dt = VisibleDate;
  1006. if (VisibleDate == DateTime.MinValue)
  1007. dt = TodaysDate;
  1008. return globCal.AddDays (dt, 1 - globCal.GetDayOfMonth (dt));
  1009. }
  1010. /// <summary>
  1011. /// Creates text to be displayed, with all attributes if to be
  1012. /// shown as a hyperlink
  1013. /// </summary>
  1014. private string GetCalendarLinkText (string eventArg,
  1015. string text,
  1016. Color foreground,
  1017. bool isLink)
  1018. {
  1019. if (isLink) {
  1020. StringBuilder dispVal = new StringBuilder ();
  1021. dispVal.Append ("<a href=\"");
  1022. dispVal.Append (Page.GetPostBackClientHyperlink (this, eventArg));
  1023. dispVal.Append ("\" style=\"color: ");
  1024. if (foreground.IsEmpty) {
  1025. dispVal.Append (ColorTranslator.ToHtml (defaultTextColor));
  1026. } else {
  1027. dispVal.Append (ColorTranslator.ToHtml (foreground));
  1028. }
  1029. dispVal.Append ("\">");
  1030. dispVal.Append (text);
  1031. dispVal.Append ("</a>");
  1032. return dispVal.ToString ();
  1033. }
  1034. return text;
  1035. }
  1036. private string GetHtmlForCell (TableCell cell, bool showLinks)
  1037. {
  1038. StringWriter sw = new StringWriter ();
  1039. HtmlTextWriter htw = new HtmlTextWriter (sw);
  1040. cell.RenderBeginTag (htw);
  1041. if(showLinks) {
  1042. htw.Write (GetCalendarLinkText ("{0}", "{1}", cell.ForeColor, showLinks));
  1043. } else {
  1044. htw.Write ("{0}");
  1045. }
  1046. cell.RenderEndTag (htw);
  1047. return sw.ToString();
  1048. }
  1049. internal void SelectRangeInternal (DateTime fromDate, DateTime toDate, DateTime visibleDate)
  1050. {
  1051. TimeSpan span = toDate - fromDate;
  1052. if (SelectedDates.Count != span.Days + 1 ||
  1053. SelectedDates [0] != fromDate ||
  1054. SelectedDates [SelectedDates.Count - 1] != toDate) {
  1055. SelectedDates.SelectRange (fromDate, toDate);
  1056. OnSelectionChanged ();
  1057. }
  1058. }
  1059. }
  1060. }