DateTimePicker.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2004 Novell, Inc.
  21. //
  22. // Authors:
  23. // John BouAntoun [email protected]
  24. //
  25. // TODO:
  26. // - implement custom formatting of the date time value
  27. // - implement any behaviour associate with UseUpDown (painting, key and mouse)
  28. // - implement key processing and responding
  29. // - fix MonthCalendar Popdown on form move
  30. // - wire in all events from monthcalendar
  31. using System;
  32. using System.Drawing;
  33. using System.Collections;
  34. using System.ComponentModel;
  35. using System.Windows.Forms;
  36. namespace System.Windows.Forms {
  37. [DefaultEvent("ValueChanged")]
  38. [DefaultProperty("Value")]
  39. [Designer("System.Windows.Forms.Design.DateTimePickerDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  40. public class DateTimePicker : Control {
  41. #region Public variables
  42. // this class has to have the specified hour, minute and second, as it says in msdn
  43. public static readonly DateTime MaxDateTime = new DateTime (9998, 12, 31, 23, 59, 59);
  44. public static readonly DateTime MinDateTime = new DateTime (1753, 1, 1);
  45. #endregion // Public variables
  46. #region Local variables
  47. protected static readonly Color DefaultMonthBackColor = ThemeEngine.Current.ColorWindow;
  48. protected static readonly Color DefaultTitleBackColor = ThemeEngine.Current.ColorActiveCaption;
  49. protected static readonly Color DefaultTitleForeColor = ThemeEngine.Current.ColorActiveCaptionText;
  50. protected static readonly Color DefaultTrailingForeColor = Color.Gray;
  51. internal MonthCalendar month_calendar;
  52. bool is_checked;
  53. string custom_format;
  54. LeftRightAlignment drop_down_align;
  55. DateTimePickerFormat format;
  56. DateTime max_date;
  57. DateTime min_date;
  58. bool show_check_box;
  59. bool show_up_down;
  60. string text;
  61. DateTime date_value;
  62. // variables used for drawing and such
  63. internal int up_down_width;
  64. internal bool is_drop_down_visible;
  65. #endregion // Local variables
  66. #region public constructors
  67. // only public constructor
  68. public DateTimePicker () {
  69. // initialise the month calendar
  70. month_calendar = new MonthCalendar (this);
  71. month_calendar.CalendarDimensions = new Size (1, 1);
  72. month_calendar.MaxSelectionCount = 1;
  73. month_calendar.ForeColor = Control.DefaultForeColor;
  74. month_calendar.BackColor = DefaultMonthBackColor;
  75. month_calendar.TitleBackColor = DefaultTitleBackColor;
  76. month_calendar.TitleForeColor = DefaultTitleForeColor;
  77. month_calendar.TrailingForeColor = DefaultTrailingForeColor;
  78. month_calendar.Visible = false;
  79. // initialise other variables
  80. is_checked = false;
  81. custom_format = string.Empty;
  82. drop_down_align = LeftRightAlignment.Left;
  83. format = DateTimePickerFormat.Long;
  84. max_date = MaxDateTime;
  85. min_date = MinDateTime;
  86. show_check_box = false;
  87. show_up_down = false;
  88. date_value = DateTime.Now;
  89. text = FormatValue ();
  90. up_down_width = 10;
  91. is_drop_down_visible = false;
  92. month_calendar.DateSelected += new DateRangeEventHandler (MonthCalendarDateSelectedHandler);
  93. KeyPress += new KeyPressEventHandler (KeyPressHandler);
  94. // LostFocus += new EventHandler (LostFocusHandler);
  95. MouseDown += new MouseEventHandler (MouseDownHandler);
  96. Paint += new PaintEventHandler (PaintHandler);
  97. SetStyle (ControlStyles.UserPaint | ControlStyles.StandardClick, false);
  98. SetStyle (ControlStyles.FixedHeight, true);
  99. }
  100. #endregion
  101. #region public properties
  102. // no reason why this is overridden
  103. [Browsable(false)]
  104. [EditorBrowsable(EditorBrowsableState.Never)]
  105. public Color BackColor {
  106. set {
  107. base.BackColor = value;
  108. }
  109. get {
  110. return base.BackColor;
  111. }
  112. }
  113. // no reason why this is overridden
  114. [Browsable(false)]
  115. [EditorBrowsable(EditorBrowsableState.Never)]
  116. public override Image BackgroundImage {
  117. set {
  118. base.BackgroundImage = value;
  119. }
  120. get {
  121. return base.BackgroundImage;
  122. }
  123. }
  124. [AmbientValue(null)]
  125. [Localizable(true)]
  126. public Font CalendarFont {
  127. set {
  128. month_calendar.Font = value;
  129. }
  130. get {
  131. return month_calendar.Font;
  132. }
  133. }
  134. public Color CalendarForeColor {
  135. set {
  136. month_calendar.ForeColor = value;
  137. }
  138. get {
  139. return month_calendar.ForeColor;
  140. }
  141. }
  142. public Color CalendarMonthBackground {
  143. set {
  144. month_calendar.BackColor = value;
  145. }
  146. get {
  147. return month_calendar.BackColor;
  148. }
  149. }
  150. public Color CalendarTitleBackColor {
  151. set {
  152. month_calendar.TitleBackColor = value;
  153. }
  154. get {
  155. return month_calendar.TitleBackColor;
  156. }
  157. }
  158. public Color CalendarTitleForeColor {
  159. set {
  160. month_calendar.TitleForeColor = value;
  161. }
  162. get {
  163. return month_calendar.TitleForeColor;
  164. }
  165. }
  166. public Color CalendarTrailingForeColor {
  167. set {
  168. month_calendar.TrailingForeColor = value;
  169. }
  170. get {
  171. return month_calendar.TrailingForeColor;
  172. }
  173. }
  174. // when checked the value is grayed out
  175. [Bindable(true)]
  176. [DefaultValue(true)]
  177. public bool Checked {
  178. set {
  179. if (is_checked != value) {
  180. is_checked = value;
  181. // invalidate the value inside this control
  182. this.Invalidate (date_area_rect);
  183. }
  184. }
  185. get {
  186. return is_checked;
  187. }
  188. }
  189. // the custom format string to format this control with
  190. [DefaultValue(null)]
  191. [RefreshProperties(RefreshProperties.Repaint)]
  192. public string CustomFormat {
  193. set {
  194. if (custom_format != value) {
  195. custom_format = value;
  196. if (this.Format == DateTimePickerFormat.Custom) {
  197. // TODO: change the text value of the dtp
  198. }
  199. }
  200. }
  201. get {
  202. return custom_format;
  203. }
  204. }
  205. // which side the drop down is to be aligned on
  206. [DefaultValue(LeftRightAlignment.Left)]
  207. [Localizable(true)]
  208. public LeftRightAlignment DropDownAlign {
  209. set {
  210. if (drop_down_align != value) {
  211. drop_down_align = value;
  212. }
  213. }
  214. get {
  215. return drop_down_align;
  216. }
  217. }
  218. [Browsable(false)]
  219. [EditorBrowsable(EditorBrowsableState.Never)]
  220. public override Color ForeColor {
  221. set {
  222. base.ForeColor = value;
  223. }
  224. get {
  225. return base.ForeColor;
  226. }
  227. }
  228. // the format of the date time picker text, default is long
  229. [RefreshProperties(RefreshProperties.Repaint)]
  230. public DateTimePickerFormat Format {
  231. set {
  232. if (format != value) {
  233. format = value;
  234. this.OnFormatChanged (EventArgs.Empty);
  235. // invalidate the value inside this control
  236. this.Invalidate (date_area_rect);
  237. }
  238. }
  239. get {
  240. return format;
  241. }
  242. }
  243. public DateTime MaxDate {
  244. set {
  245. if (value < min_date) {
  246. throw new ArgumentException ();
  247. }
  248. if (value > MaxDateTime) {
  249. throw new SystemException ();
  250. }
  251. if (max_date != value) {
  252. max_date = value;
  253. // TODO: verify this is correct behaviour when value > max date
  254. if (Value > max_date) {
  255. Value = max_date;
  256. // invalidate the value inside this control
  257. this.Invalidate (date_area_rect);
  258. }
  259. }
  260. }
  261. get {
  262. return max_date;
  263. }
  264. }
  265. public DateTime MinDate {
  266. set {
  267. if (value < min_date) {
  268. throw new ArgumentException ();
  269. }
  270. if (value < MinDateTime) {
  271. throw new SystemException ();
  272. }
  273. if (min_date != value) {
  274. min_date = value;
  275. // TODO: verify this is correct behaviour when value > max date
  276. if (Value < min_date) {
  277. Value = min_date;
  278. // invalidate the value inside this control
  279. this.Invalidate (date_area_rect);
  280. }
  281. }
  282. }
  283. get {
  284. return min_date;
  285. }
  286. }
  287. // the prefered height to draw this control using current font
  288. [Browsable(false)]
  289. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  290. public int PreferredHeight {
  291. get {
  292. return this.Font.Height + 7;
  293. }
  294. }
  295. // whether or not the check box is shown
  296. [DefaultValue(false)]
  297. public bool ShowCheckBox {
  298. set {
  299. if (show_check_box != value) {
  300. show_check_box = value;
  301. // invalidate the value inside this control
  302. this.Invalidate (date_area_rect);
  303. }
  304. }
  305. get {
  306. return show_check_box;
  307. }
  308. }
  309. // if true show the updown control, else popup the monthcalendar
  310. [DefaultValue(false)]
  311. public bool ShowUpDown {
  312. set {
  313. if (show_up_down != value) {
  314. show_up_down = value;
  315. // need to invalidate the whole control
  316. this.Invalidate ();
  317. }
  318. }
  319. get {
  320. return show_up_down;
  321. }
  322. }
  323. [Browsable(false)]
  324. [EditorBrowsable(EditorBrowsableState.Advanced)]
  325. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  326. public string Text {
  327. set {
  328. // TODO: if the format is a custom format we need to do a custom parse here
  329. DateTime parsed_value = DateTime.Parse (value);
  330. if (date_value != parsed_value) {
  331. Value = parsed_value;
  332. }
  333. text = FormatValue ();
  334. }
  335. get {
  336. return text;
  337. }
  338. }
  339. [Bindable(true)]
  340. [RefreshProperties(RefreshProperties.All)]
  341. public DateTime Value {
  342. set {
  343. if (date_value != value) {
  344. date_value = value;
  345. text = FormatValue ();
  346. this.OnValueChanged (EventArgs.Empty);
  347. this.Invalidate (date_area_rect);
  348. }
  349. }
  350. get {
  351. return date_value;
  352. }
  353. }
  354. #endregion // public properties
  355. #region public methods
  356. // just return the text value
  357. public override string ToString () {
  358. return this.Text;
  359. }
  360. #endregion // public methods
  361. #region public events
  362. // raised when the monthcalendar is closed
  363. public event EventHandler CloseUp;
  364. // raised when the monthcalendar is opened
  365. public event EventHandler DropDown;
  366. // raised when the format of the value is changed
  367. public event EventHandler FormatChanged;
  368. // raised when the date Value is changed
  369. public event EventHandler ValueChanged;
  370. #endregion // public events
  371. #region protected properties
  372. // not sure why we're overriding this one
  373. protected override CreateParams CreateParams {
  374. get {
  375. return base.CreateParams;
  376. }
  377. }
  378. // specify the default size for this control
  379. protected override Size DefaultSize {
  380. get {
  381. // todo actually measure this properly
  382. return new Size (200, PreferredHeight);
  383. }
  384. }
  385. #endregion // protected properties
  386. #region protected methods
  387. // not sure why we're overriding this one
  388. protected override AccessibleObject CreateAccessibilityInstance () {
  389. return base.CreateAccessibilityInstance ();
  390. }
  391. // not sure why we're overriding this one
  392. protected override void CreateHandle () {
  393. base.CreateHandle ();
  394. }
  395. // not sure why we're overriding this one
  396. protected override void DestroyHandle () {
  397. base.DestroyHandle ();
  398. }
  399. // not sure why we're overriding this one
  400. protected override void Dispose (bool disposing) {
  401. base.Dispose (disposing);
  402. }
  403. // find out if this key is an input key for us, depends on which date part is focused
  404. protected override bool IsInputKey (Keys keyData) {
  405. // TODO: fix this implementation of IsInputKey
  406. return false;
  407. }
  408. // raises the CloseUp event
  409. protected virtual void OnCloseUp (EventArgs eventargs) {
  410. if (this.CloseUp != null) {
  411. this.CloseUp (this, eventargs);
  412. }
  413. }
  414. // raise the drop down event
  415. protected virtual void OnDropDown (EventArgs eventargs) {
  416. if (this.DropDown != null) {
  417. this.DropDown (this, eventargs);
  418. }
  419. }
  420. // raises the format changed event
  421. protected virtual void OnFormatChanged (EventArgs e) {
  422. if (this.FormatChanged != null) {
  423. this.FormatChanged (this, e);
  424. }
  425. }
  426. // not sure why we're overriding this one
  427. protected override void OnSystemColorsChanged (EventArgs e) {
  428. base.OnSystemColorsChanged (e);
  429. }
  430. // raise the ValueChanged event
  431. protected virtual void OnValueChanged (EventArgs eventargs) {
  432. if (this.ValueChanged != null) {
  433. this.ValueChanged (this, eventargs);
  434. }
  435. }
  436. // overridden to set the bounds of this control properly
  437. protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
  438. // TODO: ensure I implemented the bounds core setting properly.
  439. if ((specified & BoundsSpecified.Height) == BoundsSpecified.Height ||
  440. (specified & BoundsSpecified.Size) == BoundsSpecified.Size) {
  441. base.SetBoundsCore (x, y, width, DefaultSize.Height, specified);
  442. } else {
  443. base.SetBoundsCore (x, y, width, height, specified);
  444. }
  445. // need to set the rectangles for all the support internal rects
  446. // this is done here as a optimisation since this is an array of rects
  447. if ((specified & BoundsSpecified.X) == BoundsSpecified.X ||
  448. (specified & BoundsSpecified.Y) == BoundsSpecified.Y) {
  449. // TODO set up all the datepart rects
  450. }
  451. }
  452. // not sure why we're overriding this
  453. protected override void WndProc (ref Message m) {
  454. base.WndProc (ref m);
  455. }
  456. #endregion // protected methods
  457. #region internal / private properties
  458. // this is the region that the date and the check box is drawn on
  459. internal Rectangle date_area_rect {
  460. get {
  461. Rectangle rect = this.ClientRectangle;
  462. if (ShowUpDown) {
  463. // set the space to the left of the up/down button
  464. if (rect.Width > (up_down_width + 4)) {
  465. rect.Width -= (up_down_width + 4);
  466. } else {
  467. rect.Width = 0;
  468. }
  469. } else {
  470. // set the space to the left of the up/down button
  471. // TODO make this use up down button
  472. if (rect.Width > (SystemInformation.VerticalScrollBarWidth + 4)) {
  473. rect.Width -= SystemInformation.VerticalScrollBarWidth;
  474. } else {
  475. rect.Width = 0;
  476. }
  477. }
  478. rect.Inflate (-2, -2);
  479. return rect;
  480. }
  481. }
  482. // the rectangle for the drop down arrow
  483. internal Rectangle drop_down_arrow_rect {
  484. get {
  485. Rectangle rect = this.ClientRectangle;
  486. rect.X = rect.Right - SystemInformation.VerticalScrollBarWidth - 2;
  487. if (rect.Width > (SystemInformation.VerticalScrollBarWidth + 2)) {
  488. rect.Width = SystemInformation.VerticalScrollBarWidth;
  489. } else {
  490. rect.Width = Math.Max (rect.Width - 2, 0);
  491. }
  492. rect.Inflate (0, -2);
  493. return rect;
  494. }
  495. }
  496. // the part of the date that is currently hilighted
  497. internal Rectangle hilight_date_area {
  498. get {
  499. // TODO: put hilighted part calculation in here
  500. return Rectangle.Empty;
  501. }
  502. }
  503. #endregion
  504. #region internal / private methods
  505. [MonoTODO("Fix Dropdown location when System.Windows.Forms.Screen gets added")]
  506. private Point CalculateDropDownLocation (Rectangle parent_control_rect, Size child_size, bool align_left)
  507. {
  508. // default bottom left
  509. Point location = new Point(parent_control_rect.Left + 5, parent_control_rect.Bottom);
  510. // now adjust the alignment
  511. if (!align_left) {
  512. location.X = parent_control_rect.Right - child_size.Width;
  513. }
  514. Point screen_location = PointToScreen (location);
  515. // TODO: enable this part when screen comes into the classes
  516. /*
  517. Rectangle working_area = Screen.FromControl(this).WorkingArea;
  518. // now adjust if off the right side of the screen
  519. if (screen_location.X < working_area.X) {
  520. screen_location.X = working_area.X;
  521. }
  522. // now adjust if it should be displayed above control
  523. if (screen_location.Y + child_size.Height > working_area.Bottom) {
  524. screen_location.Y -= (parent_control_rect.Height + child_size.Height);
  525. }
  526. */
  527. return screen_location;
  528. }
  529. // actually draw this control
  530. internal void Draw (Rectangle clip_rect, Graphics dc)
  531. {
  532. ThemeEngine.Current.DrawDateTimePicker (dc, clip_rect, this);
  533. }
  534. // drop the calendar down
  535. internal void DropDownMonthCalendar ()
  536. {
  537. // ensure the right date is set for the month_calendar
  538. month_calendar.SetDate (this.date_value);
  539. // get a rectangle that has the dimensions of the text area,
  540. // but the height of the dtp control.
  541. Rectangle align_area = this.date_area_rect;
  542. align_area.Y = this.ClientRectangle.Y;
  543. align_area.Height = this.ClientRectangle.Height;
  544. // establish the month calendar's location
  545. month_calendar.Location = CalculateDropDownLocation (
  546. align_area,
  547. month_calendar.Size,
  548. (this.DropDownAlign == LeftRightAlignment.Left));
  549. month_calendar.Show ();
  550. month_calendar.Focus ();
  551. month_calendar.Capture = true;
  552. // fire any registered events
  553. if (this.DropDown != null) {
  554. this.DropDown (this, EventArgs.Empty);
  555. }
  556. }
  557. // hide the month calendar
  558. internal void HideMonthCalendar ()
  559. {
  560. this.is_drop_down_visible = false;
  561. Invalidate (drop_down_arrow_rect);
  562. month_calendar.Capture = false;
  563. if (month_calendar.Visible) {
  564. month_calendar.Hide ();
  565. }
  566. }
  567. // raised by any key down events
  568. private void KeyPressHandler (object sender, KeyPressEventArgs e) {
  569. switch (e.KeyChar) {
  570. default:
  571. break;
  572. }
  573. e.Handled = true;
  574. }
  575. // // if we lose focus and the drop down is up, then close it
  576. // private void LostFocusHandler (object sender, EventArgs e)
  577. // {
  578. // if (is_drop_down_visible && !month_calendar.Focused) {
  579. // this.HideMonthCalendar ();
  580. // }
  581. // }
  582. // fired when a user clicks on the month calendar to select a date
  583. private void MonthCalendarDateSelectedHandler (object sender, DateRangeEventArgs e)
  584. {
  585. this.Value = e.Start.Date.Add (this.Value.TimeOfDay);
  586. this.HideMonthCalendar ();
  587. this.Focus ();
  588. }
  589. // to check if the mouse has come down on this control
  590. private void MouseDownHandler (object sender, MouseEventArgs e)
  591. {
  592. /* Click On button*/
  593. if (ShowUpDown) {
  594. // TODO: Process clicking for UPDown
  595. } else {
  596. if (is_drop_down_visible == false && drop_down_arrow_rect.Contains (e.X, e.Y)) {
  597. is_drop_down_visible = true;
  598. Invalidate (drop_down_arrow_rect);
  599. DropDownMonthCalendar ();
  600. } else {
  601. // mouse down on this control anywhere else collapses it
  602. if (is_drop_down_visible) {
  603. HideMonthCalendar ();
  604. }
  605. }
  606. }
  607. }
  608. // paint this control now
  609. private void PaintHandler (object sender, PaintEventArgs pe) {
  610. if (Width <= 0 || Height <= 0 || Visible == false)
  611. return;
  612. Draw (pe.ClipRectangle, pe.Graphics);
  613. }
  614. private string FormatValue () {
  615. string ret_value = string.Empty;
  616. switch (format) {
  617. case DateTimePickerFormat.Custom:
  618. // TODO implement custom text formatting
  619. ret_value = date_value.ToString ();
  620. break;
  621. case DateTimePickerFormat.Short:
  622. ret_value = date_value.ToShortDateString ();
  623. break;
  624. case DateTimePickerFormat.Time:
  625. ret_value = date_value.ToLongTimeString ();
  626. break;
  627. default:
  628. ret_value = date_value.ToLongDateString ();
  629. break;
  630. }
  631. return ret_value;
  632. }
  633. #endregion
  634. }
  635. }