fixed #416: Actions list supports item dragging for be able to re order them

This commit is contained in:
Jaex 2014-12-23 19:44:17 +02:00
parent 18c44f005f
commit 218b32c572
5 changed files with 70 additions and 11 deletions

View file

@ -33,6 +33,9 @@ namespace ShareX.HelpersLib
{
public class MyListView : ListView
{
public delegate void ListViewItemMovedEventHandler(object sender, int oldIndex, int newIndex);
public event ListViewItemMovedEventHandler ItemMoved;
private const int WM_PAINT = 0xF;
private const int WM_ERASEBKGND = 0x14;
@ -45,6 +48,10 @@ public class MyListView : ListView
[DefaultValue(false)]
public bool AllowColumnSort { get; set; }
// Note: AllowDrag also need to be true.
[DefaultValue(false)]
public bool AllowItemDrag { get; set; }
private ListViewColumnSorter lvwColumnSorter;
private int lineIndex = -1;
private int lastLineIndex = -1;
@ -136,7 +143,7 @@ protected override void OnItemDrag(ItemDragEventArgs e)
{
base.OnItemDrag(e);
if (AllowDrop && e.Button == MouseButtons.Left)
if (AllowDrop && AllowItemDrag && e.Button == MouseButtons.Left)
{
DoDragDrop(e.Item, DragDropEffects.Move);
}
@ -154,7 +161,15 @@ protected override void OnDragOver(DragEventArgs drgevent)
Point cp = PointToClient(new Point(drgevent.X, drgevent.Y));
dragOverItem = GetItemAt(cp.X, cp.Y);
lineIndex = dragOverItem != null ? dragOverItem.Index : Items.Count;
if (dragOverItem != null)
{
lineIndex = dragOverItem.Index;
}
else
{
lineIndex = Items.Count;
}
if (lineIndex != lastLineIndex)
{
@ -173,15 +188,41 @@ protected override void OnDragDrop(DragEventArgs drgevent)
if (lvi != null && lvi.ListView == this)
{
ListViewItem insertItem = (ListViewItem)lvi.Clone();
Items.Insert(dragOverItem != null ? dragOverItem.Index : Items.Count, insertItem);
Items.Remove(lvi);
int oldIndex = lvi.Index;
int newIndex;
if (dragOverItem != null)
{
newIndex = dragOverItem.Index;
if (newIndex > oldIndex)
{
newIndex--;
}
}
else
{
newIndex = Items.Count - 1;
}
Items.RemoveAt(oldIndex);
Items.Insert(newIndex, lvi);
OnItemMoved(oldIndex, newIndex);
}
lineIndex = lastLineIndex = -1;
Invalidate();
}
protected void OnItemMoved(int oldIndex, int newIndex)
{
if (ItemMoved != null)
{
ItemMoved(this, oldIndex, newIndex);
}
}
protected override void OnDragLeave(EventArgs e)
{
base.OnDragLeave(e);
@ -217,17 +258,17 @@ protected override void OnColumnClick(ColumnClickEventArgs e)
}
}
private void DrawInsertionLine(int x1, int x2, int y)
private void DrawInsertionLine(int left, int right, int y)
{
using (Graphics g = CreateGraphics())
{
g.DrawLine(Pens.LightBlue, x1, y, x2 - 1, y);
g.DrawLine(SystemPens.HotTrack, left, y, right - 1, y);
Point[] leftTriangle = new Point[] { new Point(x1, y - 4), new Point(x1 + 7, y), new Point(x1, y + 4) };
g.FillPolygon(Brushes.LightBlue, leftTriangle);
Point[] leftTriangle = new Point[] { new Point(left, y - 4), new Point(left + 7, y), new Point(left, y + 4) };
g.FillPolygon(SystemBrushes.HotTrack, leftTriangle);
Point[] rightTriangle = new Point[] { new Point(x2, y - 4), new Point(x2 - 8, y), new Point(x2, y + 4) };
g.FillPolygon(Brushes.LightBlue, rightTriangle);
Point[] rightTriangle = new Point[] { new Point(right, y - 4), new Point(right - 8, y), new Point(right, y + 4) };
g.FillPolygon(SystemBrushes.HotTrack, rightTriangle);
}
}
}

View file

@ -439,5 +439,12 @@ public static Version Normalize(this Version version)
{
return new Version(Math.Max(version.Major, 0), Math.Max(version.Minor, 0), Math.Max(version.Build, 0), Math.Max(version.Revision, 0));
}
public static void Move<T>(this List<T> list, int oldIndex, int newIndex)
{
T obj = list[oldIndex];
list.RemoveAt(oldIndex);
list.Insert(newIndex, obj);
}
}
}

View file

@ -576,6 +576,7 @@ private void InitializeComponent()
// lvSecondaryImageUploaders
//
this.lvSecondaryImageUploaders.AllowDrop = true;
this.lvSecondaryImageUploaders.AllowItemDrag = true;
this.lvSecondaryImageUploaders.AutoFillColumn = true;
this.lvSecondaryImageUploaders.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lvSecondaryImageUploaders.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
@ -600,6 +601,7 @@ private void InitializeComponent()
// lvSecondaryFileUploaders
//
this.lvSecondaryFileUploaders.AllowDrop = true;
this.lvSecondaryFileUploaders.AllowItemDrag = true;
this.lvSecondaryFileUploaders.AutoFillColumn = true;
this.lvSecondaryFileUploaders.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lvSecondaryFileUploaders.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
@ -623,6 +625,7 @@ private void InitializeComponent()
// lvSecondaryTextUploaders
//
this.lvSecondaryTextUploaders.AllowDrop = true;
this.lvSecondaryTextUploaders.AllowItemDrag = true;
this.lvSecondaryTextUploaders.AutoFillColumn = true;
this.lvSecondaryTextUploaders.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.lvSecondaryTextUploaders.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {

View file

@ -1138,6 +1138,8 @@ private void InitializeComponent()
//
// lvActions
//
this.lvActions.AllowDrop = true;
this.lvActions.AllowItemDrag = true;
resources.ApplyResources(this.lvActions, "lvActions");
this.lvActions.AutoFillColumn = true;
this.lvActions.CheckBoxes = true;
@ -1151,6 +1153,7 @@ private void InitializeComponent()
this.lvActions.Name = "lvActions";
this.lvActions.UseCompatibleStateImageBehavior = false;
this.lvActions.View = System.Windows.Forms.View.Details;
this.lvActions.ItemMoved += new ShareX.HelpersLib.MyListView.ListViewItemMovedEventHandler(this.lvActions_ItemMoved);
this.lvActions.ItemChecked += new System.Windows.Forms.ItemCheckedEventHandler(this.lvActions_ItemChecked);
//
// chActionsName

View file

@ -927,6 +927,11 @@ private void lvActions_ItemChecked(object sender, ItemCheckedEventArgs e)
fileAction.IsActive = e.Item.Checked;
}
private void lvActions_ItemMoved(object sender, int oldIndex, int newIndex)
{
TaskSettings.ExternalPrograms.Move(oldIndex, newIndex);
}
#endregion Actions
#region Watch folders