Beispiel #1
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;
               }
         }
      }
Beispiel #2
0
      /// <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;
      }
Beispiel #3
0
      /// <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();
            }
         }
      }
Beispiel #4
0
      /// <summary>
      /// Updates the shown months.
      /// </summary>
      public void UpdateMonths()
      {
         int x = 0, y = 0, 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();
      }
Beispiel #5
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;
      }
Beispiel #6
0
      /// <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)
         {
            // perform hit test
            MonthCalendarHitTest hit = this.HitTest(e.Location);

            // set current bounds
            this.currentMoveBounds = hit.Bounds;

            // set current hit type
            this.currentHitType = 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;
                  }
            }
         }
      }