/* Copyright 2012 Justin LeCheminant This file is part of WindowsFormsCalendar. indowsFormsCalendar is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. indowsFormsCalendar is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with indowsFormsCalendar. If not, see . */ using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsCalendar { /// /// Represents a day present on the control's view. /// public class CalendarDay : CalendarSelectableElement { #region Static private Size overflowSize = new Size(16, 16); private Padding overflowPadding = new Padding(5); #endregion #region Events #endregion #region Fields private List _containedItems; private Calendar _calendar; private DateTime _date; private CalendarDayTop _dayTop; private int _index; private bool _overflowStart; private bool _overflowEnd; private bool _overflowStartSelected; private bool _overlowEndSelected; private CalendarTimeScaleUnit[] _timeUnits; #endregion #region Properties /// /// Gets a list of items contained on the day /// internal List ContainedItems { get { return _containedItems; } } /// /// Gets the DayTop of the day, the place where multi-day and all-day items are placed /// public CalendarDayTop DayTop { get { return _dayTop; } } /// /// Gets the bounds of the body of the day (where time-based CalendarItems are placed) /// public Rectangle BodyBounds { get { return Rectangle.FromLTRB(Bounds.Left, DayTop.Bounds.Bottom, Bounds.Right, Bounds.Bottom); } } /// /// Gets the date this day represents /// public override DateTime Date { get { return _date; } } /// /// Gets the bounds of the header of the day /// public Rectangle HeaderBounds { get { return new Rectangle(Bounds.Left, Bounds.Top, Bounds.Width, Calendar.Renderer.DayHeaderHeight); } } /// /// Gets the index of this day on the calendar /// public int Index { get { return _index; } } /// /// Gets a value indicating if the day is specified on the view (See remarks). /// /// /// A day may not be specified on the view, but still present to make up a square calendar. /// This days should be drawn in a way that indicates it's necessary but unrequested presence. /// public bool SpecifiedOnView { get { return Date.CompareTo(Calendar.ViewStart) >= 0 && Date.CompareTo(Calendar.ViewEnd) <= 0; } } /// /// Gets the time units contained on the day /// public CalendarTimeScaleUnit[] TimeUnits { get { return _timeUnits; } } /// /// /// /// Gets a value indicating if the day contains items not shown through the start of the day /// /// public bool OverflowStart { get { return _overflowStart; } } /// /// Gets the bounds of the indicator /// public virtual Rectangle OverflowStartBounds { get { return new Rectangle(new Point(Bounds.Right - overflowPadding.Right - overflowSize.Width, Bounds.Top + overflowPadding.Top), overflowSize); } } /// /// Gets a value indicating if the indicator is currently selected /// /// /// This value set to true when user hovers the mouse on the area /// public bool OverflowStartSelected { get { return _overflowStartSelected; } } /// /// Gets a value indicating if the day contains items not shown through the end of the day /// public bool OverflowEnd { get { return _overflowEnd; } } /// /// Gets the bounds of the indicator /// public virtual Rectangle OverflowEndBounds { get { return new Rectangle(new Point(Bounds.Right - overflowPadding.Right - overflowSize.Width, Bounds.Bottom - overflowPadding.Bottom - overflowSize.Height), overflowSize); } } /// /// Gets a value indicating if the indicator is currently selected /// /// /// This value set to true when user hovers the mouse on the area /// public bool OverflowEndSelected { get { return _overlowEndSelected; } } #endregion /// /// Initializes a new instance of the class. /// /// The calendar. /// The date. /// The index. internal CalendarDay(Calendar calendar, DateTime date, int index) : base(calendar) { _containedItems = new List(); _calendar = calendar; _dayTop = new CalendarDayTop(this); _date = date; _index = index; UpdateUnits(); } #region Public Methods /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return Date.ToShortDateString(); } #endregion #region Private Methods /// /// Adds an item to the list if not in yet /// /// internal void AddContainedItem(CalendarItem item) { if (!ContainedItems.Contains(item)) { ContainedItems.Add(item); } } /// /// Sets the value of he property /// /// Value of the property internal void SetOverflowEnd(bool overflow) { _overflowEnd = overflow; } /// /// Sets the value of the property /// /// Value to pass to the property internal void SetOverflowEndSelected(bool selected) { _overlowEndSelected= selected; } /// /// Sets the value of he property /// /// Value of the property internal void SetOverflowStart(bool overflow) { _overflowStart = overflow; } /// /// Sets the value of the property /// /// Value to pass to the property internal void SetOverflowStartSelected(bool selected) { _overflowStartSelected = selected; } /// /// Updates the value of property /// internal void UpdateUnits() { int factor = 0; switch (Calendar.TimeScale) { case CalendarTimeScale.SixtyMinutes: factor = 1; break; case CalendarTimeScale.ThirtyMinutes: factor = 2; break; case CalendarTimeScale.FifteenMinutes: factor = 4; break; case CalendarTimeScale.TenMinutes: factor = 6; break; case CalendarTimeScale.SixMinutes: factor = 10; break; case CalendarTimeScale.FiveMinutes: factor = 12; break; default: throw new NotImplementedException("TimeScale not supported"); } _timeUnits = new CalendarTimeScaleUnit[24 * factor]; int hourSum = 0; int minSum = 0; for (int i = 0; i < _timeUnits.Length; i++) { _timeUnits[i] = new CalendarTimeScaleUnit(this, i, hourSum, minSum); minSum += 60 / factor; if (minSum >= 60) { minSum = 0; hourSum++; } } UpdateHighlights(); } /// /// Updates the highlights of the units /// internal void UpdateHighlights() { if (TimeUnits == null) return; for (int i = 0; i < TimeUnits.Length; i++) { TimeUnits[i].SetHighlighted(TimeUnits[i].CheckHighlighted()); } } #endregion } }