Moved drag scrolling to DarkScrollBase.

This commit is contained in:
Robin 2015-12-07 16:20:11 +00:00
parent 22b9bb90cc
commit a29da0ffd9
2 changed files with 199 additions and 112 deletions

View File

@ -27,10 +27,14 @@ namespace DarkUI.Controls
private Point _offsetMousePosition;
private int _maxDragChange = 0;
private Timer _dragTimer;
#endregion
#region Property Region
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Rectangle Viewport
@ -67,6 +71,23 @@ namespace DarkUI.Controls
get { return _offsetMousePosition; }
}
[Category("Behavior")]
[Description("Determines the maximum scroll change when dragging.")]
[DefaultValue(0)]
public int MaxDragChange
{
get { return _maxDragChange; }
set
{
_maxDragChange = value;
Invalidate();
}
}
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public bool IsDragging { get; private set; }
#endregion
#region Constructor Region
@ -87,97 +108,10 @@ namespace DarkUI.Controls
_vScrollBar.MouseDown += delegate { Select(); };
_hScrollBar.MouseDown += delegate { Select(); };
}
#endregion
#region Event Handler Region
protected override void OnCreateControl()
{
base.OnCreateControl();
UpdateScrollBars();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
UpdateScrollBars();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
_offsetMousePosition = new Point(e.X + Viewport.Left, e.Y + Viewport.Top);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Right)
Select();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
var horizontal = false;
if (_hScrollBar.Visible && ModifierKeys == Keys.Control)
horizontal = true;
if (_hScrollBar.Visible && !_vScrollBar.Visible)
horizontal = true;
if (!horizontal)
{
if (e.Delta > 0)
_vScrollBar.ScrollByPhysical(3);
else if (e.Delta < 0)
_vScrollBar.ScrollByPhysical(-3);
}
else
{
if (e.Delta > 0)
_hScrollBar.ScrollByPhysical(3);
else if (e.Delta < 0)
_hScrollBar.ScrollByPhysical(-3);
}
}
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
base.OnPreviewKeyDown(e);
// Allows arrow keys to trigger OnKeyPress
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
e.IsInputKey = true;
break;
}
_dragTimer = new Timer();
_dragTimer.Interval = 1;
_dragTimer.Tick += DragTimer_Tick;
}
#endregion
@ -287,6 +221,18 @@ namespace DarkUI.Controls
_hScrollBar.Value = value;
}
protected virtual void StartDrag()
{
IsDragging = true;
_dragTimer.Start();
}
protected virtual void StopDrag()
{
IsDragging = false;
_dragTimer.Stop();
}
public Point PointToView(Point point)
{
return new Point(point.X - Viewport.Left, point.Y - Viewport.Top);
@ -298,5 +244,151 @@ namespace DarkUI.Controls
}
#endregion
#region Event Handler Region
protected override void OnCreateControl()
{
base.OnCreateControl();
UpdateScrollBars();
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
Invalidate();
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
UpdateScrollBars();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
_offsetMousePosition = new Point(e.X + Viewport.Left, e.Y + Viewport.Top);
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Right)
Select();
}
protected override void OnMouseWheel(MouseEventArgs e)
{
base.OnMouseWheel(e);
var horizontal = false;
if (_hScrollBar.Visible && ModifierKeys == Keys.Control)
horizontal = true;
if (_hScrollBar.Visible && !_vScrollBar.Visible)
horizontal = true;
if (!horizontal)
{
if (e.Delta > 0)
_vScrollBar.ScrollByPhysical(3);
else if (e.Delta < 0)
_vScrollBar.ScrollByPhysical(-3);
}
else
{
if (e.Delta > 0)
_hScrollBar.ScrollByPhysical(3);
else if (e.Delta < 0)
_hScrollBar.ScrollByPhysical(-3);
}
}
protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
{
base.OnPreviewKeyDown(e);
// Allows arrow keys to trigger OnKeyPress
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
e.IsInputKey = true;
break;
}
}
private void DragTimer_Tick(object sender, EventArgs e)
{
var pos = PointToClient(MousePosition);
if (_vScrollBar.Visible)
{
// Scroll up
if (pos.Y < ClientRectangle.Top)
{
var difference = (pos.Y - ClientRectangle.Top) * -1;
if (MaxDragChange > 0 && difference > MaxDragChange)
difference = MaxDragChange;
_vScrollBar.Value = _vScrollBar.Value - difference;
}
// Scroll down
if (pos.Y > ClientRectangle.Bottom)
{
var difference = pos.Y - ClientRectangle.Bottom;
if (MaxDragChange > 0 && difference > MaxDragChange)
difference = MaxDragChange;
_vScrollBar.Value = _vScrollBar.Value + difference;
}
}
if (_hScrollBar.Visible)
{
// Scroll left
if (pos.X < ClientRectangle.Left)
{
var difference = (pos.X - ClientRectangle.Left) * -1;
if (MaxDragChange > 0 && difference > MaxDragChange)
difference = MaxDragChange;
_hScrollBar.Value = _hScrollBar.Value - difference;
}
// Scroll right
if (pos.X > ClientRectangle.Right)
{
var difference = pos.X - ClientRectangle.Right;
if (MaxDragChange > 0 && difference > MaxDragChange)
difference = MaxDragChange;
_hScrollBar.Value = _hScrollBar.Value + difference;
}
}
}
#endregion
}
}

View File

@ -47,12 +47,10 @@ namespace DarkUI.Controls
private Bitmap _nodeOpenHover;
private Bitmap _nodeOpenHoverSelected;
private bool _isDragging;
private DarkTreeNode _provisionalNode;
private DarkTreeNode _dropNode;
private bool _provisionalDragging;
private List<DarkTreeNode> _dragNodes;
private Timer _dragTimer;
private Point _dragPos;
#endregion
@ -103,6 +101,7 @@ namespace DarkUI.Controls
set
{
_itemHeight = value;
MaxDragChange = _itemHeight;
UpdateNodes();
}
}
@ -153,9 +152,7 @@ namespace DarkUI.Controls
_selectedNodes = new ObservableCollection<DarkTreeNode>();
_selectedNodes.CollectionChanged += SelectedNodes_CollectionChanged;
_dragTimer = new Timer();
_dragTimer.Interval = 1;
_dragTimer.Tick += DragTimer_Tick;
MaxDragChange = _itemHeight;
LoadIcons();
}
@ -284,7 +281,7 @@ namespace DarkUI.Controls
}
}
if (_isDragging)
if (IsDragging)
{
if (_dropNode != null)
{
@ -299,7 +296,7 @@ namespace DarkUI.Controls
CheckHover();
if (_isDragging)
if (IsDragging)
{
HandleDrag();
}
@ -327,7 +324,7 @@ namespace DarkUI.Controls
protected override void OnMouseUp(MouseEventArgs e)
{
if (_isDragging)
if (IsDragging)
{
HandleDrop();
}
@ -374,7 +371,7 @@ namespace DarkUI.Controls
{
base.OnKeyDown(e);
if (_isDragging)
if (IsDragging)
return;
if (Nodes.Count == 0)
@ -469,7 +466,7 @@ namespace DarkUI.Controls
private void DragTimer_Tick(object sender, EventArgs e)
{
if (!_isDragging)
if (!IsDragging)
{
StopDrag();
return;
@ -566,7 +563,7 @@ namespace DarkUI.Controls
private void UpdateNodes()
{
if (_isDragging)
if (IsDragging)
return;
if (Nodes.Count == 0)
@ -680,7 +677,7 @@ namespace DarkUI.Controls
{
if (!ClientRectangle.Contains(PointToClient(MousePosition)))
{
if (_isDragging)
if (IsDragging)
{
if (_dropNode != null)
{
@ -708,7 +705,7 @@ namespace DarkUI.Controls
private void CheckNodeHover(DarkTreeNode node, Point location)
{
if (_isDragging)
if (IsDragging)
{
var rect = GetNodeFullRowArea(node);
if (rect.Contains(OffsetMousePosition))
@ -1019,7 +1016,7 @@ namespace DarkUI.Controls
#region Drag & Drop Region
private void StartDrag()
protected override void StartDrag()
{
if (!AllowMoveNodes)
{
@ -1043,11 +1040,10 @@ namespace DarkUI.Controls
}
_provisionalDragging = false;
_isDragging = true;
_dragTimer.Start();
Cursor = Cursors.SizeAll;
base.StartDrag();
}
private void HandleDrag()
@ -1127,17 +1123,16 @@ namespace DarkUI.Controls
UpdateNodes();
}
private void StopDrag()
protected override void StopDrag()
{
_isDragging = false;
_dragNodes = null;
_dropNode = null;
_dragTimer.Stop();
Cursor = Cursors.Default;
Invalidate();
base.StopDrag();
}
protected virtual bool ForceDropToParent(DarkTreeNode node)
@ -1214,7 +1209,7 @@ namespace DarkUI.Controls
if (SelectedNodes.Count > 0 && SelectedNodes.Contains(node))
bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection;
if (_isDragging && _dropNode == node)
if (IsDragging && _dropNode == node)
bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection;
using (var b = new SolidBrush(bgColor))