using System; using System.Drawing; using System.Windows.Forms; using JD.Kernel.Structs; using JD.Sequencers.Controls; using JD.Sequencers.Windowlesses; namespace JD.Sequencers.Tools { /// /// Basic tool that selects controls. /// public class SelectingTool : Tool { #region Variables private bool m_allowMultiselection = true; #endregion #region Constructor /// /// Constructor. /// /// public SelectingTool(WindowlessContainer owner) : base(owner) { } #endregion #region Properties /// /// Whether to allow multi-selection. /// public bool AllowMultiselection { get { return m_allowMultiselection; } set { m_allowMultiselection = value; } } #endregion #region Methods /// /// Overriden. /// /// /// protected internal override bool CanStart(ToolStartingEventArgs e) { return (e.Button == MouseButtons.Left || e.Button == MouseButtons.Right) && base.CanStart(e); } private bool CanSelect(WindowlessControl control) { return Selection.Count == 0 || AllowMultiselection; } protected internal override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); DoSingleSelection(e.Button, e.Location, SelectionType.MouseDown); } protected internal override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); DoSingleSelection(e.Button, e.Location, SelectionType.MouseUp); } [Flags] protected enum SelectionType { MouseDown = 1, MouseUp = 2 } protected void DoSingleSelection(MouseButtons button, Point location, Flags selectionType) { CurrentControl = Container.FindSelectableControl(location); if (CurrentControl != null) { if (IsControlPressed && selectionType[SelectionType.MouseDown]) { if (CurrentControl.Selected) { Selection.Remove(CurrentControl); } else if (CanSelect(CurrentControl)) { AddToSelection(CurrentControl); Selection.PerformMultiSelect(); } } else if (IsShiftPressed && CanSelect(CurrentControl) && selectionType[SelectionType.MouseDown]) { AddToSelection(CurrentControl); } else if (!IsShiftPressed && !IsControlPressed) { if (!CurrentControl.Selected || (selectionType[SelectionType.MouseUp] && button == MouseButtons.Left)) { Selection.Select(CurrentControl); } } } else if (!IsControlPressed && !IsShiftPressed && selectionType[SelectionType.MouseUp]) { Selection.Clear(); } } private void AddToSelection(WindowlessControl control) { if (Selection.Count == 0 || Selection.Primary.CanSelectWith(control)) { Selection.Add(control); } } /// /// Overriden. /// /// protected internal override void OnMouseMove(MouseEventArgs e) { if (e.Location != LastMouseDownEventArgs.Location) { EndTool(); } } #endregion } }