/// <summary> /// Sets the start date. /// </summary> /// <param name="start"> /// The start date. /// </param> /// <returns> /// true if <paramref name="start"/> is valid; false otherwise. /// </returns> private bool SetStartDate(DateTime start) { if (start < DateTime.MinValue.Date || start > DateTime.MaxValue.Date) { return false; } DayOfWeek firstDayOfWeek = this._formatProvider.FirstDayOfWeek; MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this._maxDate); if (start > this._maxDate) { start = dt.AddMonths(1 - this.Months.Length).FirstOfMonth.Date; } if (start < this._minDate) { start = this._minDate; } dt = new MonthCalendarDate(this.CultureCalendar, start); int length = this.Months != null ? this.Months.Length - 1 : 0; while (dt.Date > this._minDate && dt.Day != 1) { dt = dt.AddDays(-1); } MonthCalendarDate endDate = dt.AddMonths(length); MonthCalendarDate endDateDay = endDate.AddDays(endDate.DaysInMonth - 1 - (endDate.Day - 1)); if (endDate.Date >= this._maxDate || endDateDay.Date >= this._maxDate) { dt = new MonthCalendarDate(this.CultureCalendar, this._maxDate).AddMonths(-length).FirstOfMonth; } this.viewStart = dt.Date; while (dt.Date > this.CultureCalendar.MinSupportedDateTime.Date && dt.DayOfWeek != firstDayOfWeek) { dt = dt.AddDays(-1); } this.realStart = dt.Date; return true; }
/// <summary> /// Sets the selection range for the specified <see cref="MonthCalendarSelectionMode"/>. /// </summary> /// <param name="selMode"> /// The <see cref="MonthCalendarSelectionMode"/> value to set the selection range for. /// </param> private void SetSelectionRange(MonthCalendarSelectionMode selMode) { switch (selMode) { case MonthCalendarSelectionMode.Day: { this.selectionEnd = this.selectionStart; break; } case MonthCalendarSelectionMode.WorkWeek: case MonthCalendarSelectionMode.FullWeek: { MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.selectionStart).GetFirstDayInWeek( this._formatProvider); this.selectionStart = dt.Date; this.selectionEnd = dt.AddDays(6).Date; break; } case MonthCalendarSelectionMode.Month: { MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.selectionStart).FirstOfMonth; this.selectionStart = dt.Date; this.selectionEnd = dt.AddMonths(1).AddDays(-1).Date; break; } } }
/// <summary> /// Handles clicks in the month menu. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="e"> /// The event data. /// </param> private void MonthClick(object sender, EventArgs e) { MonthCalendarDate currentMonthYear = new MonthCalendarDate(this.CultureCalendar, (DateTime)this.monthMenu.Tag); int monthClicked = (int)((ToolStripMenuItem)sender).Tag; if (currentMonthYear.Month != monthClicked) { MonthCalendarDate dt = new MonthCalendarDate( this.CultureCalendar, new DateTime(currentMonthYear.Year, monthClicked, 1, this.CultureCalendar)); DateTime newStartDate = dt.AddMonths(-this.GetIndex(currentMonthYear.Date)).Date; if (this.SetStartDate(newStartDate)) { this.UpdateMonths(); this.RaiseDateChanged(); this.Focus(); this.Refresh(); } } }
/// <summary> /// Gets the new date for the specified scroll direction. /// </summary> /// <param name="up"> /// true for scrolling upwards, false otherwise. /// </param> /// <returns> /// The new start date. /// </returns> private DateTime GetNewScrollDate(bool up) { if ((this._lastVisibleDate == this._maxDate && !up) || (this.Months[0].FirstVisibleDate == this._minDate && up)) { return this.viewStart; } MonthCalendarDate dt = new MonthCalendarDate(this.CultureCalendar, this.viewStart); int monthsToAdd = (this.scrollChange == 0 ? Math.Max((this.calendarDimensions.Width * this.calendarDimensions.Height) - 1, 1) : this.scrollChange) * (up ? -1 : 1); int length = this.Months == null ? Math.Max(1, this.calendarDimensions.Width * this.calendarDimensions.Height) : this.Months.Length; MonthCalendarDate newStartMonthDate = dt.AddMonths(monthsToAdd); MonthCalendarDate lastMonthDate = newStartMonthDate.AddMonths(length - 1); MonthCalendarDate lastMonthEndDate = lastMonthDate.AddDays(lastMonthDate.DaysInMonth - 1 - lastMonthDate.Day); if (newStartMonthDate.Date < this._minDate) { newStartMonthDate = new MonthCalendarDate(this.CultureCalendar, this._minDate); } else if (lastMonthEndDate.Date >= this._maxDate || lastMonthDate.Date >= this._maxDate) { MonthCalendarDate maxdt = new MonthCalendarDate(this.CultureCalendar, this._maxDate).FirstOfMonth; newStartMonthDate = maxdt.AddMonths(1 - length); } return newStartMonthDate.Date; }
/// <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; } } } }