internal void Cell_MouseLeave(object sender, PointerEventArgs e) { if (_isMouseLeftButtonDown) { CalendarDayButton b = (CalendarDayButton)sender; // The button is in Pressed state. Change the state to normal. if (e.Device.Captured == b) { e.Device.Capture(null); } // null check is added for unit tests if (_downEventArg != null) { var arg = new PointerReleasedEventArgs() { Device = _downEventArg.Device, MouseButton = _downEventArg.MouseButton, Handled = _downEventArg.Handled, InputModifiers = _downEventArg.InputModifiers, Route = _downEventArg.Route, Source = _downEventArg.Source }; b.SendMouseLeftButtonUp(arg); } _lastCalendarDayButton = b; } }
internal void Cell_MouseLeftButtonUp(object sender, PointerReleasedEventArgs e) { if (Owner != null) { CalendarDayButton b = sender as CalendarDayButton; if (b != null && !b.IsBlackout) { Owner.OnDayButtonMouseUp(e); } _isMouseLeftButtonDown = false; if (b != null && b.DataContext != null) { if (Owner.SelectionMode == CalendarSelectionMode.None || Owner.SelectionMode == CalendarSelectionMode.SingleDate) { Owner.OnDayClick((DateTime)b.DataContext); return; } if (Owner.HoverStart.HasValue) { switch (Owner.SelectionMode) { case CalendarSelectionMode.SingleRange: { // Update SelectedDates foreach (DateTime item in Owner.SelectedDates) { Owner.RemovedItems.Add(item); } Owner.SelectedDates.ClearInternal(); AddSelection(b); return; } case CalendarSelectionMode.MultipleRange: { // add the selection (either single day or // SingleRange day) AddSelection(b); return; } } } else { // If the day is Disabled but a trailing day we should // be able to switch months if (b.IsInactive && b.IsBlackout) { Owner.OnDayClick((DateTime)b.DataContext); } } } } }
internal void Cell_MouseLeave(object sender, PointerEventArgs e) { if (_isMouseLeftButtonDown) { CalendarDayButton b = (CalendarDayButton)sender; // The button is in Pressed state. Change the state to normal. if (e.Device.Captured == b) { e.Device.Capture(null); } _lastCalendarDayButton = b; } }
private void AddSelection(CalendarDayButton b) { if (Owner != null) { Owner.HoverEndIndex = b.Index; Owner.HoverEnd = (DateTime)b.DataContext; if (Owner.HoverEnd != null && Owner.HoverStart != null) { // this is selection with Mouse, we do not guarantee the // range does not include BlackOutDates. AddRange method // will throw away the BlackOutDates based on the // SelectionMode Owner.IsMouseSelection = true; Owner.SelectedDates.AddRange(Owner.HoverStart.Value, Owner.HoverEnd.Value); Owner.OnDayClick((DateTime)b.DataContext); } } }
internal void Cell_MouseEnter(object sender, PointerEventArgs e) { if (Owner != null) { CalendarDayButton b = sender as CalendarDayButton; if (_isMouseLeftButtonDown && b != null && b.IsEnabled && !b.IsBlackout) { // Update the states of all buttons to be selected starting // from HoverStart to b switch (Owner.SelectionMode) { case CalendarSelectionMode.SingleDate: { DateTime selectedDate = (DateTime)b.DataContext; Owner.DatePickerDisplayDateFlag = true; if (Owner.SelectedDates.Count == 0) { Owner.SelectedDates.Add(selectedDate); } else { Owner.SelectedDates[0] = selectedDate; } return; } case CalendarSelectionMode.SingleRange: case CalendarSelectionMode.MultipleRange: { Debug.Assert(b.DataContext != null, "The DataContext should not be null!"); Owner.UnHighlightDays(); Owner.HoverEndIndex = b.Index; Owner.HoverEnd = (DateTime)b.DataContext; // Update the States of the buttons Owner.HighlightDays(); return; } } } } }
private void Cell_Click(object?sender, RoutedEventArgs e) { if (Owner != null) { if (_isControlPressed && Owner.SelectionMode == CalendarSelectionMode.MultipleRange) { CalendarDayButton b = (CalendarDayButton)sender !; if (b.IsSelected) { Owner.HoverStart = null; _isMouseLeftButtonDown = false; b.IsSelected = false; if (b.DataContext != null) { Owner.SelectedDates.Remove((DateTime)b.DataContext); } } } } _isControlPressed = false; }
private void Cell_Click(object sender, RoutedEventArgs e) { if (Owner != null) { if (_isControlPressed && Owner.SelectionMode == CalendarSelectionMode.MultipleRange) { CalendarDayButton b = sender as CalendarDayButton; Debug.Assert(b != null, "The sender should be a non-null CalendarDayButton!"); if (b.IsSelected) { Owner.HoverStart = null; _isMouseLeftButtonDown = false; b.IsSelected = false; if (b.DataContext != null) { Owner.SelectedDates.Remove((DateTime)b.DataContext); } } } } _isControlPressed = false; }
internal void Cell_MouseLeftButtonDown(object sender, PointerPressedEventArgs e) { if (Owner != null) { if (!Owner.HasFocusInternal) { Owner.Focus(); } bool ctrl, shift; CalendarExtensions.GetMetaKeyState(e.KeyModifiers, out ctrl, out shift); CalendarDayButton b = sender as CalendarDayButton; if (b != null) { _isControlPressed = ctrl; if (b.IsEnabled && !b.IsBlackout) { DateTime selectedDate = (DateTime)b.DataContext; Contract.Requires <ArgumentNullException>(selectedDate != null); _isMouseLeftButtonDown = true; // null check is added for unit tests if (e != null) { _downEventArg = e; } switch (Owner.SelectionMode) { case CalendarSelectionMode.None: { return; } case CalendarSelectionMode.SingleDate: { Owner.DatePickerDisplayDateFlag = true; if (Owner.SelectedDates.Count == 0) { Owner.SelectedDates.Add(selectedDate); } else { Owner.SelectedDates[0] = selectedDate; } return; } case CalendarSelectionMode.SingleRange: { // Set the start or end of the selection // range if (shift) { Owner.UnHighlightDays(); Owner.HoverEnd = selectedDate; Owner.HoverEndIndex = b.Index; Owner.HighlightDays(); } else { Owner.UnHighlightDays(); Owner.HoverStart = selectedDate; Owner.HoverStartIndex = b.Index; } return; } case CalendarSelectionMode.MultipleRange: { if (shift) { if (!ctrl) { // clear the list, set the states to // default foreach (DateTime item in Owner.SelectedDates) { Owner.RemovedItems.Add(item); } Owner.SelectedDates.ClearInternal(); } Owner.HoverEnd = selectedDate; Owner.HoverEndIndex = b.Index; Owner.HighlightDays(); } else { if (!ctrl) { // clear the list, set the states to // default foreach (DateTime item in Owner.SelectedDates) { Owner.RemovedItems.Add(item); } Owner.SelectedDates.ClearInternal(); Owner.UnHighlightDays(); } Owner.HoverStart = selectedDate; Owner.HoverStartIndex = b.Index; } return; } } } else { // If a click occurs on a BlackOutDay we set the // HoverStart to be null Owner.HoverStart = null; } } else { _isControlPressed = false; } } }
private void SetCalendarDayButtons(DateTime firstDayOfMonth) { int lastMonthToDisplay = PreviousMonthDays(firstDayOfMonth); DateTime dateToAdd; if (DateTimeHelper.CompareYearMonth(firstDayOfMonth, DateTime.MinValue) > 0) { // DisplayDate is not equal to DateTime.MinValue we can subtract // days from the DisplayDate dateToAdd = _calendar.AddDays(firstDayOfMonth, -lastMonthToDisplay); } else { dateToAdd = firstDayOfMonth; } if (Owner != null && Owner.HoverEnd != null && Owner.HoverStart != null) { Owner.HoverEndIndex = null; Owner.HoverStartIndex = null; } int count = Calendar.RowsPerMonth * Calendar.ColumnsPerMonth; for (int childIndex = Calendar.ColumnsPerMonth; childIndex < count; childIndex++) { CalendarDayButton childButton = MonthView.Children[childIndex] as CalendarDayButton; Contract.Requires <ArgumentNullException>(childButton != null); childButton.Index = childIndex; SetButtonState(childButton, dateToAdd); // Update the indexes of hoverStart and hoverEnd if (Owner != null && Owner.HoverEnd != null && Owner.HoverStart != null) { if (DateTimeHelper.CompareDays(dateToAdd, Owner.HoverEnd.Value) == 0) { Owner.HoverEndIndex = childIndex; } if (DateTimeHelper.CompareDays(dateToAdd, Owner.HoverStart.Value) == 0) { Owner.HoverStartIndex = childIndex; } } //childButton.Focusable = false; childButton.Content = dateToAdd.Day.ToString(DateTimeHelper.GetCurrentDateFormat()); childButton.DataContext = dateToAdd; if (DateTime.Compare((DateTime)DateTimeHelper.DiscardTime(DateTime.MaxValue), dateToAdd) > 0) { // Since we are sure DisplayDate is not equal to // DateTime.MaxValue, it is safe to use AddDays dateToAdd = _calendar.AddDays(dateToAdd, 1); } else { // DisplayDate is equal to the DateTime.MaxValue, so there // are no trailing days childIndex++; for (int i = childIndex; i < count; i++) { childButton = MonthView.Children[i] as CalendarDayButton; Contract.Requires <ArgumentNullException>(childButton != null); // button needs a content to occupy the necessary space // for the content presenter childButton.Content = i.ToString(DateTimeHelper.GetCurrentDateFormat()); childButton.IsEnabled = false; childButton.Opacity = 0; } return; } } // If the HoverStart or HoverEndInternal could not be found on the // DisplayMonth set the values of the HoverStartIndex or // HoverEndIndex to be the first or last day indexes on the current // month if (Owner != null && Owner.HoverStart.HasValue && Owner.HoverEndInternal.HasValue) { if (!Owner.HoverEndIndex.HasValue) { if (DateTimeHelper.CompareDays(Owner.HoverEndInternal.Value, Owner.HoverStart.Value) > 0) { Owner.HoverEndIndex = Calendar.ColumnsPerMonth * Calendar.RowsPerMonth - 1; } else { Owner.HoverEndIndex = Calendar.ColumnsPerMonth; } } if (!Owner.HoverStartIndex.HasValue) { if (DateTimeHelper.CompareDays(Owner.HoverEndInternal.Value, Owner.HoverStart.Value) > 0) { Owner.HoverStartIndex = Calendar.ColumnsPerMonth; } else { Owner.HoverStartIndex = Calendar.ColumnsPerMonth * Calendar.RowsPerMonth - 1; } } } }
private void SetButtonState(CalendarDayButton childButton, DateTime dateToAdd) { if (Owner != null) { childButton.Opacity = 1; // If the day is outside the DisplayDateStart/End boundary, do // not show it if (DateTimeHelper.CompareDays(dateToAdd, Owner.DisplayDateRangeStart) < 0 || DateTimeHelper.CompareDays(dateToAdd, Owner.DisplayDateRangeEnd) > 0) { childButton.IsEnabled = false; childButton.IsToday = false; childButton.IsSelected = false; childButton.Opacity = 0; } else { // SET IF THE DAY IS SELECTABLE OR NOT if (Owner.BlackoutDates.Contains(dateToAdd)) { childButton.IsBlackout = true; } else { childButton.IsBlackout = false; } childButton.IsEnabled = true; // SET IF THE DAY IS INACTIVE OR NOT: set if the day is a // trailing day or not childButton.IsInactive = (DateTimeHelper.CompareYearMonth(dateToAdd, Owner.DisplayDateInternal) != 0); // SET IF THE DAY IS TODAY OR NOT childButton.IsToday = (Owner.IsTodayHighlighted && dateToAdd == DateTime.Today); // SET IF THE DAY IS SELECTED OR NOT childButton.IsSelected = false; foreach (DateTime item in Owner.SelectedDates) { // Since we should be comparing the Date values not // DateTime values, we can't use // Owner.SelectedDates.Contains(dateToAdd) directly childButton.IsSelected |= (DateTimeHelper.CompareDays(dateToAdd, item) == 0); } // SET THE FOCUS ELEMENT if (Owner.LastSelectedDate != null) { if (DateTimeHelper.CompareDays(Owner.LastSelectedDate.Value, dateToAdd) == 0) { if (Owner.FocusButton != null) { Owner.FocusButton.IsCurrent = false; } Owner.FocusButton = childButton; if (Owner.HasFocusInternal) { Owner.FocusButton.IsCurrent = true; } } else { childButton.IsCurrent = false; } } } } }
private void PopulateGrids() { if (MonthView != null) { var childCount = Calendar.RowsPerMonth + Calendar.RowsPerMonth * Calendar.ColumnsPerMonth; var children = new List <IControl>(childCount); for (int i = 0; i < Calendar.RowsPerMonth; i++) { if (_dayTitleTemplate != null) { var cell = _dayTitleTemplate.Build(); cell.DataContext = string.Empty; cell.SetValue(Grid.RowProperty, 0); cell.SetValue(Grid.ColumnProperty, i); children.Add(cell); } } for (int i = 1; i < Calendar.RowsPerMonth; i++) { for (int j = 0; j < Calendar.ColumnsPerMonth; j++) { CalendarDayButton cell = new CalendarDayButton(); if (Owner != null) { cell.Owner = Owner; } cell.SetValue(Grid.RowProperty, i); cell.SetValue(Grid.ColumnProperty, j); cell.CalendarDayButtonMouseDown += Cell_MouseLeftButtonDown; cell.CalendarDayButtonMouseUp += Cell_MouseLeftButtonUp; cell.PointerEnter += Cell_MouseEnter; cell.PointerLeave += Cell_MouseLeave; cell.Click += Cell_Click; children.Add(cell); } } MonthView.Children.AddRange(children); } if (YearView != null) { var childCount = Calendar.RowsPerYear * Calendar.ColumnsPerYear; var children = new List <IControl>(childCount); CalendarButton month; for (int i = 0; i < Calendar.RowsPerYear; i++) { for (int j = 0; j < Calendar.ColumnsPerYear; j++) { month = new CalendarButton(); if (Owner != null) { month.Owner = Owner; } month.SetValue(Grid.RowProperty, i); month.SetValue(Grid.ColumnProperty, j); month.CalendarLeftMouseButtonDown += Month_CalendarButtonMouseDown; month.CalendarLeftMouseButtonUp += Month_CalendarButtonMouseUp; month.PointerEnter += Month_MouseEnter; month.PointerLeave += Month_MouseLeave; children.Add(month); } } YearView.Children.AddRange(children); } }
private void PopulateGrids() { if (MonthView != null) { var childCount = Calendar.RowsPerMonth + Calendar.RowsPerMonth * Calendar.ColumnsPerMonth; using var children = new PooledList <IControl>(childCount); for (int i = 0; i < Calendar.RowsPerMonth; i++) { if (_dayTitleTemplate != null) { var cell = (Control)_dayTitleTemplate.Build(); cell.DataContext = string.Empty; cell.SetValue(Grid.RowProperty, 0); cell.SetValue(Grid.ColumnProperty, i); children.Add(cell); } } EventHandler <PointerPressedEventArgs> cellMouseLeftButtonDown = Cell_MouseLeftButtonDown; EventHandler <PointerReleasedEventArgs> cellMouseLeftButtonUp = Cell_MouseLeftButtonUp; EventHandler <PointerEventArgs> cellMouseEnter = Cell_MouseEnter; EventHandler <RoutedEventArgs> cellClick = Cell_Click; for (int i = 1; i < Calendar.RowsPerMonth; i++) { for (int j = 0; j < Calendar.ColumnsPerMonth; j++) { var cell = new CalendarDayButton(); if (Owner != null) { cell.Owner = Owner; } cell.SetValue(Grid.RowProperty, i); cell.SetValue(Grid.ColumnProperty, j); cell.CalendarDayButtonMouseDown += cellMouseLeftButtonDown; cell.CalendarDayButtonMouseUp += cellMouseLeftButtonUp; cell.PointerEnter += cellMouseEnter; cell.Click += cellClick; children.Add(cell); } } MonthView.Children.AddRange(children); } if (YearView != null) { var childCount = Calendar.RowsPerYear * Calendar.ColumnsPerYear; var children = new List <IControl>(childCount); EventHandler <PointerPressedEventArgs> monthCalendarButtonMouseDown = Month_CalendarButtonMouseDown; EventHandler <PointerReleasedEventArgs> monthCalendarButtonMouseUp = Month_CalendarButtonMouseUp; EventHandler <PointerEventArgs> monthMouseEnter = Month_MouseEnter; for (int i = 0; i < Calendar.RowsPerYear; i++) { for (int j = 0; j < Calendar.ColumnsPerYear; j++) { var month = new CalendarButton(); if (Owner != null) { month.Owner = Owner; } month.SetValue(Grid.RowProperty, i); month.SetValue(Grid.ColumnProperty, j); month.CalendarLeftMouseButtonDown += monthCalendarButtonMouseDown; month.CalendarLeftMouseButtonUp += monthCalendarButtonMouseUp; month.PointerEnter += monthMouseEnter; children.Add(month); } } YearView.Children.AddRange(children); } }