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 Point _offsetMousePosition;
private int _maxDragChange = 0;
private Timer _dragTimer;
#endregion #endregion
#region Property Region #region Property Region
[Browsable(false)] [Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Rectangle Viewport public Rectangle Viewport
@ -67,6 +71,23 @@ namespace DarkUI.Controls
get { return _offsetMousePosition; } 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 #endregion
#region Constructor Region #region Constructor Region
@ -87,97 +108,10 @@ namespace DarkUI.Controls
_vScrollBar.MouseDown += delegate { Select(); }; _vScrollBar.MouseDown += delegate { Select(); };
_hScrollBar.MouseDown += delegate { Select(); }; _hScrollBar.MouseDown += delegate { Select(); };
}
#endregion _dragTimer = new Timer();
_dragTimer.Interval = 1;
#region Event Handler Region _dragTimer.Tick += DragTimer_Tick;
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;
}
} }
#endregion #endregion
@ -287,6 +221,18 @@ namespace DarkUI.Controls
_hScrollBar.Value = value; _hScrollBar.Value = value;
} }
protected virtual void StartDrag()
{
IsDragging = true;
_dragTimer.Start();
}
protected virtual void StopDrag()
{
IsDragging = false;
_dragTimer.Stop();
}
public Point PointToView(Point point) public Point PointToView(Point point)
{ {
return new Point(point.X - Viewport.Left, point.Y - Viewport.Top); return new Point(point.X - Viewport.Left, point.Y - Viewport.Top);
@ -298,5 +244,151 @@ namespace DarkUI.Controls
} }
#endregion #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 _nodeOpenHover;
private Bitmap _nodeOpenHoverSelected; private Bitmap _nodeOpenHoverSelected;
private bool _isDragging;
private DarkTreeNode _provisionalNode; private DarkTreeNode _provisionalNode;
private DarkTreeNode _dropNode; private DarkTreeNode _dropNode;
private bool _provisionalDragging; private bool _provisionalDragging;
private List<DarkTreeNode> _dragNodes; private List<DarkTreeNode> _dragNodes;
private Timer _dragTimer;
private Point _dragPos; private Point _dragPos;
#endregion #endregion
@ -103,6 +101,7 @@ namespace DarkUI.Controls
set set
{ {
_itemHeight = value; _itemHeight = value;
MaxDragChange = _itemHeight;
UpdateNodes(); UpdateNodes();
} }
} }
@ -153,9 +152,7 @@ namespace DarkUI.Controls
_selectedNodes = new ObservableCollection<DarkTreeNode>(); _selectedNodes = new ObservableCollection<DarkTreeNode>();
_selectedNodes.CollectionChanged += SelectedNodes_CollectionChanged; _selectedNodes.CollectionChanged += SelectedNodes_CollectionChanged;
_dragTimer = new Timer(); MaxDragChange = _itemHeight;
_dragTimer.Interval = 1;
_dragTimer.Tick += DragTimer_Tick;
LoadIcons(); LoadIcons();
} }
@ -284,7 +281,7 @@ namespace DarkUI.Controls
} }
} }
if (_isDragging) if (IsDragging)
{ {
if (_dropNode != null) if (_dropNode != null)
{ {
@ -299,7 +296,7 @@ namespace DarkUI.Controls
CheckHover(); CheckHover();
if (_isDragging) if (IsDragging)
{ {
HandleDrag(); HandleDrag();
} }
@ -327,7 +324,7 @@ namespace DarkUI.Controls
protected override void OnMouseUp(MouseEventArgs e) protected override void OnMouseUp(MouseEventArgs e)
{ {
if (_isDragging) if (IsDragging)
{ {
HandleDrop(); HandleDrop();
} }
@ -374,7 +371,7 @@ namespace DarkUI.Controls
{ {
base.OnKeyDown(e); base.OnKeyDown(e);
if (_isDragging) if (IsDragging)
return; return;
if (Nodes.Count == 0) if (Nodes.Count == 0)
@ -469,7 +466,7 @@ namespace DarkUI.Controls
private void DragTimer_Tick(object sender, EventArgs e) private void DragTimer_Tick(object sender, EventArgs e)
{ {
if (!_isDragging) if (!IsDragging)
{ {
StopDrag(); StopDrag();
return; return;
@ -566,7 +563,7 @@ namespace DarkUI.Controls
private void UpdateNodes() private void UpdateNodes()
{ {
if (_isDragging) if (IsDragging)
return; return;
if (Nodes.Count == 0) if (Nodes.Count == 0)
@ -680,7 +677,7 @@ namespace DarkUI.Controls
{ {
if (!ClientRectangle.Contains(PointToClient(MousePosition))) if (!ClientRectangle.Contains(PointToClient(MousePosition)))
{ {
if (_isDragging) if (IsDragging)
{ {
if (_dropNode != null) if (_dropNode != null)
{ {
@ -708,7 +705,7 @@ namespace DarkUI.Controls
private void CheckNodeHover(DarkTreeNode node, Point location) private void CheckNodeHover(DarkTreeNode node, Point location)
{ {
if (_isDragging) if (IsDragging)
{ {
var rect = GetNodeFullRowArea(node); var rect = GetNodeFullRowArea(node);
if (rect.Contains(OffsetMousePosition)) if (rect.Contains(OffsetMousePosition))
@ -1019,7 +1016,7 @@ namespace DarkUI.Controls
#region Drag & Drop Region #region Drag & Drop Region
private void StartDrag() protected override void StartDrag()
{ {
if (!AllowMoveNodes) if (!AllowMoveNodes)
{ {
@ -1043,11 +1040,10 @@ namespace DarkUI.Controls
} }
_provisionalDragging = false; _provisionalDragging = false;
_isDragging = true;
_dragTimer.Start();
Cursor = Cursors.SizeAll; Cursor = Cursors.SizeAll;
base.StartDrag();
} }
private void HandleDrag() private void HandleDrag()
@ -1127,17 +1123,16 @@ namespace DarkUI.Controls
UpdateNodes(); UpdateNodes();
} }
private void StopDrag() protected override void StopDrag()
{ {
_isDragging = false;
_dragNodes = null; _dragNodes = null;
_dropNode = null; _dropNode = null;
_dragTimer.Stop();
Cursor = Cursors.Default; Cursor = Cursors.Default;
Invalidate(); Invalidate();
base.StopDrag();
} }
protected virtual bool ForceDropToParent(DarkTreeNode node) protected virtual bool ForceDropToParent(DarkTreeNode node)
@ -1214,7 +1209,7 @@ namespace DarkUI.Controls
if (SelectedNodes.Count > 0 && SelectedNodes.Contains(node)) if (SelectedNodes.Count > 0 && SelectedNodes.Contains(node))
bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection; bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection;
if (_isDragging && _dropNode == node) if (IsDragging && _dropNode == node)
bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection; bgColor = Focused ? Colors.BlueSelection : Colors.GreySelection;
using (var b = new SolidBrush(bgColor)) using (var b = new SolidBrush(bgColor))