public void MouseMove(System.Windows.Forms.MouseEventArgs e) { if (e == null) { throw new ArgumentNullException("e"); } Appointment selection = m_dayView.SelectedAppointment; UpdateCursor(e, selection); if ((selection == null) || selection.Locked || (m_mode == Mode.None)) { return; } Rectangle viewrect = m_dayView.GetTrueRectangle(); Rectangle fdrect = m_dayView.GetFullDayApptsRectangle(); if ((e.Button == System.Windows.Forms.MouseButtons.Left) && (viewrect.Contains(e.Location) || fdrect.Contains(e.Location))) { // Get time at mouse position bool longAppt = IsResizingLongAppt(); bool ptInLongAptRect = fdrect.Contains(e.Location); DateTime dateAtCursor = m_dayView.GetDateTimeAt(e.X, e.Y, longAppt); switch (m_mode) { case Mode.Move: if (!selection.IsLongAppt() && !ptInLongAptRect) { if (m_length == TimeSpan.Zero) { m_startDate = selection.StartDate; m_length = selection.Length; } else { DateTime startDate = dateAtCursor.Add(m_delta); DateTime endDate = startDate.Add(m_length); if (startDate.Date < dateAtCursor.Date) { // User has dragged off the top startDate = dateAtCursor.Date; endDate = startDate.Add(m_length); } else if (endDate > dateAtCursor.Date.AddDays(1)) { // User has dragged off the bottom startDate = endDate.Date.Subtract(m_length); endDate = (startDate + m_length); } // Handle horizontal drag DateTime date = m_dayView.GetDateAt(e.X, false); DateTime datePrev = m_dayView.GetDateAt(m_lastMouseMove.X, false); if (date != datePrev) { startDate = date.Date.Add(startDate.TimeOfDay); endDate = startDate.Add(m_length); } // Check for a change if (startDate != selection.StartDate) { selection.StartDate = startDate; selection.EndDate = endDate; m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } } } else if (selection.IsLongAppt() && ptInLongAptRect) { dateAtCursor = dateAtCursor.Add(m_delta); int hoursDiff = dateAtCursor.Subtract(selection.StartDate).Hours; TimeSpan apptLen = selection.Length; if (hoursDiff != 0) { System.DateTime newStart = selection.StartDate.AddHours(hoursDiff); if (newStart != selection.StartDate) { selection.StartDate = newStart; selection.EndDate = (newStart + apptLen); m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } } } break; case Mode.ResizeBottom: if (!ptInLongAptRect) { // Note: the current algorithm tends to 'floor' the time // which makes selecting all the way to midnight tricky. // We solve it by adding half the slot height to the // mouse position dateAtCursor = m_dayView.GetDateTimeAt(e.X, e.Y + m_dayView.SlotHeight, longAppt); if (dateAtCursor == dateAtCursor.Date) { dateAtCursor = dateAtCursor.AddDays(1).AddSeconds(-1); } if (dateAtCursor > selection.StartDate) { if (SameDay(selection.EndDate, dateAtCursor)) { selection.EndDate = dateAtCursor; m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } } } break; case Mode.ResizeTop: if (!ptInLongAptRect && (dateAtCursor < selection.EndDate)) { if (selection.StartDate.Day == dateAtCursor.Day) { selection.StartDate = dateAtCursor; m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } } break; case Mode.ResizeLeft: if (ptInLongAptRect && (dateAtCursor.Date < selection.EndDate.AddHours(-1))) { selection.StartDate = dateAtCursor; m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } break; case Mode.ResizeRight: if (ptInLongAptRect && (dateAtCursor >= selection.StartDate.AddHours(1))) { selection.EndDate = dateAtCursor; m_dayView.Invalidate(); m_dayView.RaiseAppointmentMove(new MoveAppointmentEventArgs(selection, m_mode, false)); } break; } m_lastMouseMove = e.Location; } }
virtual protected bool MoveAppointment(System.Windows.Forms.MouseEventArgs e) { Debug.Assert(CanModifyAppointment(e, true)); if (m_mode != Mode.Move) { return(false); } Rectangle shortApptsRect = m_dayView.GetTrueRectangle(); Rectangle longApptsRect = m_dayView.GetFullDayApptsRectangle(); if (!shortApptsRect.Contains(e.Location) && !longApptsRect.Contains(e.Location)) { return(false); } Appointment selection = m_dayView.SelectedAppointment; bool longAppt = IsEditingLongAppt; bool ptInLongApptsRect = longApptsRect.Contains(e.Location); bool shortAppt = !longAppt; bool ptInShortApptsRect = !ptInLongApptsRect; // Get date/time at mouse position DateTime dateAtCursor = m_dayView.GetDateTimeAt(e.X, e.Y, longAppt); if (shortAppt && ptInShortApptsRect) { if (m_length == TimeSpan.Zero) { m_startDate = selection.StartDate; m_length = selection.Length; } else { DateTime startDate = dateAtCursor.Add(m_delta); DateTime endDate = startDate.Add(m_length); if (startDate.Date < dateAtCursor.Date) { // User has dragged off the top startDate = dateAtCursor.Date; endDate = startDate.Add(m_length); } else if (endDate > dateAtCursor.Date.AddDays(1)) { // User has dragged off the bottom startDate = endDate.Date.Subtract(m_length); endDate = (startDate + m_length); } // Handle horizontal drag DateTime date = m_dayView.GetDateAt(e.X, false); DateTime datePrev = m_dayView.GetDateAt(m_lastMouseMove.X, false); if (date != datePrev) { startDate = date.Date.Add(startDate.TimeOfDay); endDate = startDate.Add(m_length); } // Check for a change if (startDate != selection.StartDate) { selection.StartDate = startDate; selection.EndDate = endDate; return(true); } } } else if (longAppt && ptInLongApptsRect) { dateAtCursor = dateAtCursor.Add(m_delta); int hoursDiff = dateAtCursor.Subtract(selection.StartDate).Hours; TimeSpan apptLen = selection.Length; if (hoursDiff != 0) { System.DateTime newStart = selection.StartDate.AddHours(hoursDiff); if (newStart != selection.StartDate) { selection.StartDate = newStart; selection.EndDate = (newStart + apptLen); return(true); } } } // all else return(false); }