Esempio n. 1
0
        /// <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();
        }
Esempio n. 2
0
        /// <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;
        }
Esempio n. 3
0
        /// <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;
                    }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Processes a dialog key.
        /// </summary>
        /// <param name="keyData">
        /// One of the <see cref="Keys"/> value that represents the key to process.
        /// </param>
        /// <returns>
        /// true if the key was processed by the control; otherwise, false.
        /// </returns>
        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (this.daySelectionMode != MonthCalendarSelectionMode.Day)
            {
                return base.ProcessDialogKey(keyData);
            }

            MonthCalendarDate dt = new MonthCalendarDate(this.cultureCalendar, this.selectionStart);
            bool retValue = false;

            if (keyData == Keys.Left)
            {
                this.selectionStart = dt.AddDays(-1).Date;
                retValue = true;
            }
            else if (keyData == Keys.Right)
            {
                this.selectionStart = dt.AddDays(1).Date;
                retValue = true;
            }
            else if (keyData == Keys.Up)
            {
                this.selectionStart = dt.AddDays(-7).Date;
                retValue = true;
            }
            else if (keyData == Keys.Down)
            {
                this.selectionStart = dt.AddDays(7).Date;
                retValue = true;
            }

            if (retValue)
            {
                if (this.selectionStart < this._minDate)
                {
                    this.selectionStart = this._minDate;
                }
                else if (this.selectionStart > this._maxDate)
                {
                    this.selectionStart = this._maxDate;
                }

                this.SetSelectionRange(this.daySelectionMode);
                this.EnsureSeletedDateIsVisible();
                this.RaiseInternalDateSelected();
                this.Invalidate();
                return true;
            }

            return base.ProcessDialogKey(keyData);
        }
        /// <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;
                        }
                }
            }
        }