protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); //Cursor = Cursors.Hand; var mouseMovePosition = new Point(e.X, e.Y); var mouseMoveScrollPosition = mouseMovePosition - new Size(AutoScrollPosition); var row = RowCoord.Where(d => (isNumberInRange(mouseMoveScrollPosition.Y, d.FirstCoord, d.SecondCoord))).SingleOrDefault(); var moveRow = row != null ? row.RowIndex : -1; var column = ColumnCoord.Where(d => (isNumberInRange(mouseMoveScrollPosition.X, d.FirstCoord, d.SecondCoord))).SingleOrDefault(); WeekPlannerItem hittedItem = null; if (listRows.Count > 0 && moveRow != -1 && listRows.Count > moveRow) { var plannerRow = listRows.ElementAt(moveRow); if (plannerRow.LeftMarginBackColor != Color.White) { plannerRow.LeftMarginOldBackColor = plannerRow.LeftMarginBackColor; } hittedItem = GetItemAt(mouseMoveScrollPosition, plannerRow.Items); if (outHittedItem != hittedItem && hittedItem != null) { outHittedItem = hittedItem; } //to detect cursor movement above left margin column if (mouseMoveScrollPosition.X < LeftMargin) { //to detect cursor movement during changing rows if (outRow != moveRow) { plannerRow.LeftMarginBackColor = Color.White; //sets row color when cursor moves above the row if (plannerRow.IsCollapsible) { //plannerRow.LeftMarginBackColor = Color.White; Cursor = Cursors.Hand; } else { Cursor = Cursors.Default; } //finds row which has left cursor and sets back color if (outRow != -1) { var oRow = listRows.ElementAt(outRow); oRow.LeftMarginBackColor = oRow.LeftMarginOldBackColor; if (oRow.IsCollapsible) { //oRow.LeftMarginBackColor = oRow.LeftMarginOldBackColor; } } outRow = moveRow; } } //to detect when cursor leaves left margin column else { if (plannerRow.IsCollapsible) { //plannerRow.LeftMarginBackColor = plannerRow.LeftMarginOldBackColor; } plannerRow.LeftMarginBackColor = plannerRow.LeftMarginOldBackColor; outRow = -1; } } // to detect cursor movement below left margin column if (moveRow == -1 && outRow != -1) { var r = listRows.ElementAt(outRow); if (r.IsCollapsible) { outRow = -1; //r.LeftMarginBackColor = r.LeftMarginOldBackColor; Cursor = Cursors.Default; } r.LeftMarginBackColor = r.LeftMarginOldBackColor; } switch (State) { case PlannerState.Idle: Cursor should = Cursors.Default; if (hittedItem != null) { if ((hittedItem.ResizeEndDateZone(mouseMoveScrollPosition) || hittedItem.ResizeStartDateZone(mouseMoveScrollPosition))) { should = Cursors.SizeWE; } planner.OnItemMouseHover(new WeekPlannerItemEventArgs(hittedItem)); } if (!Cursor.Equals(should) && mouseMoveScrollPosition.X > LeftMargin) { Cursor = should; } if (hittedItem == null && outHittedItem != null) { planner.OnItemMouseLeave(new WeekPlannerItemEventArgs(outHittedItem)); outHittedItem = null; } break; case PlannerState.DraggingItem: if (column != null) { moveDate = column.DateValue; } itemOnState.StartDate = moveDate.AddDays(durationStart.Days); itemOnState.EndDate = moveDate.AddDays(durationEnd.Days); if (rowNumer != moveRow && moveRow != -1 && IsAllowedDraggingBetweenRows) { try { var oldRowNumber = rowNumer; rowNumer = moveRow; var newRow = Rows.ElementAt(rowNumer); var oldRow = Rows.ElementAt(oldRowNumber); var newItem = new WeekPlannerItem(this); newItem = itemOnState; var addedItem = newRow.Items.Add(newItem); oldRow.Items.Remove(itemOnState); _selectedItem = addedItem; itemOnState = addedItem; } catch (Exception) { Debug.WriteLine("it did it again"); } } itemOnStateChanged = true; break; case PlannerState.ResizingItem: if (column != null) { moveDate = column.DateValue; } if (itemOnState.IsResizingStartDate && moveDate <= itemOnState.EndDate.Date) { itemOnState.StartDate = moveDate.AddDays(durationStart.Days); } else if (itemOnState.IsResizingEndDate && moveDate >= itemOnState.StartDate.Date) { itemOnState.EndDate = moveDate.AddDays(durationEnd.Days); } itemOnStateChanged = true; break; case PlannerState.EditingItemText: break; } }
protected override void OnMouseDown(MouseEventArgs e) { mouseClickPosition = new Point(e.X, e.Y); mouseClickScrollPosition = mouseClickPosition - new Size(AutoScrollPosition); if (!Focused) { Focus(); } var row = RowCoord.Where(d => (isNumberInRange(mouseClickScrollPosition.Y, d.FirstCoord, d.SecondCoord))).SingleOrDefault(); _selectedRow = row != null ? row.RowIndex : -1; var column = ColumnCoord.Where(d => (isNumberInRange(mouseClickScrollPosition.X, d.FirstCoord, d.SecondCoord))).SingleOrDefault(); if (column != null) { _selectedColumn = column.Column; _selectedCellDate = CurrentDate.AddDays(SelectedColumn); } if (listRows.Count > 0 && _selectedRow != -1) { var plannerRow = listRows.ElementAt(_selectedRow); _selectedItem = GetItemAt(mouseClickScrollPosition, plannerRow.Items); } switch (State) { case PlannerState.Idle: if (_selectedItem != null) { itemOnState = _selectedItem; itemOnStateChanged = false; durationStart = itemOnState.StartDate.Date.Subtract(column.DateValue.Date); durationEnd = itemOnState.EndDate.Date.Subtract(column.DateValue.Date); if (itemOnState.ResizeStartDateZone(mouseClickScrollPosition)) { SetState(PlannerState.ResizingItem); itemOnState.SetIsResizingStartDate(true); } else if (itemOnState.ResizeEndDateZone(mouseClickScrollPosition)) { SetState(PlannerState.ResizingItem); itemOnState.SetIsResizingEndDate(true); } else { rowNumer = _selectedRow; SetState(PlannerState.DraggingItem); } } break; case PlannerState.DraggingItem: break; case PlannerState.ResizingItem: break; case PlannerState.EditingItemText: break; } if (row != null && column != null && listRows.Count > 0) { var rowClicked = listRows.ElementAt(_selectedRow); planner.OnRowClick(new RowEventArgs(rowClicked)); Invalidate(); } if (mouseClickScrollPosition.X < LeftMargin) { try { var rowClicked = listRows.ElementAt(_selectedRow); if (e.Button == MouseButtons.Left) { rowClicked.IsExpanded = !rowClicked.IsExpanded; } planner.OnRowLeftColumnClick(new RowClickEventArgs(rowClicked, e.Button), listRows.IndexOf(rowClicked)); rowClicked.LeftMarginBackColor = rowClicked.LeftMarginOldBackColor; } catch (System.ArgumentOutOfRangeException) { Debug.WriteLine("It happened"); } } base.OnMouseDown(e); }