/// <summary> /// Initializes a new instance of the <see cref="MonthCalendarMonth"/> class. /// </summary> /// <param name="enhancedMonthCal"> /// The <see cref="Calendar"/> which hosts the <see cref="MonthCalendarMonth"/> instance. /// </param> /// <param name="date"> /// The date that represents the month and year which is displayed in this instance. /// </param> public MonthCalendarMonth(EnhancedMonthCalendar enhancedMonthCal, DateTime date) { this.Calendar = enhancedMonthCal; this.Date = date; this._location = new Point(0, 0); MonthCalendarDate dt = new MonthCalendarDate(enhancedMonthCal.CultureCalendar, date).FirstOfMonth.GetFirstDayInWeek(enhancedMonthCal.FormatProvider); List<MonthCalendarDay> dayList = new List<MonthCalendarDay>(); List<MonthCalendarWeek> weekList = new List<MonthCalendarWeek>(); int dayAdjust = 0; while (dt.AddDays(dayAdjust).DayOfWeek != enhancedMonthCal.FormatProvider.FirstDayOfWeek) { dayAdjust++; } int d = dayAdjust != 0 ? 8 - dayAdjust : 0; for (int i = dayAdjust; i < 42 + dayAdjust; i++, dt = dt.AddDays(1)) { MonthCalendarDay day = new MonthCalendarDay(this, dt.Date); dayList.Add(day); if (day.Visible) { if (this._firstVisibleDate == DateTime.MinValue) { this._firstVisibleDate = dt.Date; } if (!day.TrailingDate) { this._lastVisibleDate = dt.Date; } } if (i == dayAdjust || ((i - d) % 7) == 0) { DateTime weekEnd = dt.GetEndDateOfWeek(enhancedMonthCal.FormatProvider).Date; int weekNumEnd = DateMethods.GetWeekOfYear(enhancedMonthCal.Culture, enhancedMonthCal.CultureCalendar, weekEnd); weekList.Add(new MonthCalendarWeek(this, weekNumEnd, dt.Date, weekEnd)); } if (dt.Date == enhancedMonthCal.CultureCalendar.MaxSupportedDateTime.Date) { break; } } this.Days = dayList.ToArray(); this.Weeks = weekList.ToArray(); }
/// <summary>Updates the shown months.</summary> public void UpdateMonths() { int x = this.Padding.Left, y = this.Padding.Top, index = 0; int calWidthDim = this.calendarDimensions.Width; int calHeightDim = this.calendarDimensions.Height; List<MonthCalendarMonth> monthList = new List<MonthCalendarMonth>(); MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.viewStart); if (dt.GetEndDateOfWeek(this._formatProvider).Month != dt.Month) { dt = dt.GetEndDateOfWeek(this._formatProvider).FirstOfMonth; } if (this.UseRTL) { x = this._monthWidth * (calWidthDim - 1); for (int i = 0; i < calHeightDim; i++) { for (int j = calWidthDim - 1; j >= 0; j--) { if (dt.Date >= this._maxDate) { break; } monthList.Add(new MonthCalendarMonth(this, dt.Date) { Location = new Point(x, y), Index = index++ }); x -= this._monthWidth; dt = dt.AddMonths(1); } x = this._monthWidth * (calWidthDim - 1); y += this._monthHeight; } } else { for (int i = 0; i < calHeightDim; i++) { for (int j = 0; j < calWidthDim; j++) { if (dt.Date >= this._maxDate) { break; } monthList.Add(new MonthCalendarMonth(this, dt.Date) { Location = new Point(x, y), Index = index++ }); x += this._monthWidth; dt = dt.AddMonths(1); } x = 0; y += this._monthHeight; } } this._lastVisibleDate = monthList[monthList.Count - 1].LastVisibleDate; this.Months = monthList.ToArray(); }
/// <summary> /// Raises the <see cref="System.Windows.Forms.Control.MouseDown"/> event. /// </summary> /// <param name="e"> /// A <see cref="MouseEventArgs"/> that contains the event data. /// </param> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.Focus(); this.Capture = true; // reset the selection range where selection started this._selectionStartRange = null; if (e.Button == MouseButtons.Left) { MonthCalendarHitTest hit = this.HitTest(e.Location); // perform hit test this.currentMoveBounds = hit.Bounds; // set current bounds this.currentHitType = hit.Type; // set current hit type switch (hit.Type) { case MonthCalendarHitType.Day: { // save old selection range SelectionRange oldRange = this.SelectionRange; if (!this.extendSelection || this.daySelectionMode != MonthCalendarSelectionMode.Manual) { // clear all selection ranges this.selectionRanges.Clear(); } switch (this.daySelectionMode) { case MonthCalendarSelectionMode.Day: { this.OnDateClicked(new DateEventArgs(hit.Date)); // only single days are selectable if (this.selectionStart != hit.Date) { this.SelectionStart = hit.Date; this.RaiseDateSelected(); } break; } case MonthCalendarSelectionMode.WorkWeek: { // only single work week is selectable // get first day of week DateTime firstDay = new MonthCalendarDate(this.CultureCalendar, hit.Date).GetFirstDayInWeek(this._formatProvider).Date; // get work days List<DayOfWeek> workDays = DateMethods.GetWorkDays(this.nonWorkDays); // reset selection start and end this.selectionEnd = DateTime.MinValue; this.selectionStart = DateTime.MinValue; // current range SelectionRange currentRange = null; // build selection ranges for work days for (int i = 0; i < 7; i++) { DateTime toAdd = firstDay.AddDays(i); if (workDays.Contains(toAdd.DayOfWeek)) { if (currentRange == null) { currentRange = new SelectionRange(DateTime.MinValue, DateTime.MinValue); } if (currentRange.Start == DateTime.MinValue) { currentRange.Start = toAdd; } else { currentRange.End = toAdd; } } else if (currentRange != null) { this.selectionRanges.Add(currentRange); currentRange = null; } } if (this.selectionRanges.Count >= 1) { // set first selection range this.SelectionRange = this.selectionRanges[0]; this.selectionRanges.RemoveAt(0); // if selection range changed, raise event if (this.SelectionRange != oldRange) { this.RaiseDateSelected(); } } else { this.Refresh(); } break; } case MonthCalendarSelectionMode.FullWeek: { // only a full week is selectable // get selection start and end MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, hit.Date).GetFirstDayInWeek(this._formatProvider); this.selectionStart = dt.Date; this.selectionEnd = dt.GetEndDateOfWeek(this._formatProvider).Date; // if range changed, raise event if (this.SelectionRange != oldRange) { this.RaiseDateSelected(); this.Refresh(); } break; } case MonthCalendarSelectionMode.Month: { // only a full month is selectable MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, hit.Date).FirstOfMonth; // get selection start and end this.selectionStart = dt.Date; this.selectionEnd = dt.AddMonths(1).AddDays(-1).Date; // if range changed, raise event if (this.SelectionRange != oldRange) { this.RaiseDateSelected(); this.Refresh(); } break; } case MonthCalendarSelectionMode.Manual: { if (this.extendSelection) { var range = this.selectionRanges.Find(r => hit.Date >= r.Start && hit.Date <= r.End); if (range != null) { this.selectionRanges.Remove(range); } } // manual mode - selection ends when user is releasing the left mouse button this.selectionStarted = true; this._backupRange = this.SelectionRange; this.selectionEnd = DateTime.MinValue; this.SelectionStart = hit.Date; break; } } break; } case MonthCalendarHitType.Week: { this.selectionRanges.Clear(); if (this.maxSelectionCount > 6 || this.maxSelectionCount == 0) { this._backupRange = this.SelectionRange; this.selectionStarted = true; this.selectionEnd = new MonthCalendarDate(this.CultureCalendar, hit.Date).GetEndDateOfWeek(this._formatProvider).Date; this.SelectionStart = hit.Date; this._selectionStartRange = this.SelectionRange; } break; } case MonthCalendarHitType.MonthName: { this._monthSelected = hit.Date; this.mouseMoveFlags.HeaderDate = hit.Date; this.Invalidate(hit.InvalidateBounds); this.Update(); this.monthMenu.Tag = hit.Date; this.UpdateMonthMenu(this.CultureCalendar.GetYear(hit.Date)); this._showingMenu = true; // show month menu this.monthMenu.Show(this, hit.Bounds.Right, e.Location.Y); break; } case MonthCalendarHitType.MonthYear: { this._yearSelected = hit.Date; this.mouseMoveFlags.HeaderDate = hit.Date; this.Invalidate(hit.InvalidateBounds); this.Update(); this.UpdateYearMenu(this.CultureCalendar.GetYear(hit.Date)); this.yearMenu.Tag = hit.Date; this._showingMenu = true; // show year menu this.yearMenu.Show(this, hit.Bounds.Right, e.Location.Y); break; } case MonthCalendarHitType.Arrow: { // an arrow was pressed // set new start date if (this.SetStartDate(hit.Date)) { // update months this.UpdateMonths(); // raise event this.RaiseDateChanged(); this.mouseMoveFlags.HeaderDate = this._leftArrowRect.Contains(e.Location) ? this.Months[0].Date : this.Months[this.calendarDimensions.Width - 1].Date; this.Refresh(); } break; } case MonthCalendarHitType.Footer: { // footer was pressed this.selectionRanges.Clear(); bool raiseDateChanged = false; SelectionRange range = this.SelectionRange; // determine if date changed event has to be raised if (DateTime.Today < this.Months[0].FirstVisibleDate || DateTime.Today > this._lastVisibleDate) { // set new start date if (this.SetStartDate(DateTime.Today)) { // update months this.UpdateMonths(); raiseDateChanged = true; } else { break; } } // set new selection start and end values this.selectionStart = DateTime.Today; this.selectionEnd = DateTime.Today; this.SetSelectionRange(this.daySelectionMode); this.OnDateClicked(new DateEventArgs(DateTime.Today)); // raise events if necessary if (range != this.SelectionRange) { this.RaiseDateSelected(); } if (raiseDateChanged) { this.RaiseDateChanged(); } this.Refresh(); break; } case MonthCalendarHitType.Header: { // header was pressed this.Invalidate(hit.Bounds); this.Update(); break; } } } }