ShareX/ShareX.HelpersLib/Controls/MyListView.cs

383 lines
11 KiB
C#
Raw Normal View History

2013-11-03 23:53:49 +13:00
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
2024-01-03 12:57:14 +13:00
Copyright (c) 2007-2024 ShareX Team
2013-11-03 23:53:49 +13:00
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using System;
2013-11-03 23:53:49 +13:00
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
2013-11-03 23:53:49 +13:00
using System.Windows.Forms;
2014-12-11 09:25:20 +13:00
namespace ShareX.HelpersLib
2013-11-03 23:53:49 +13:00
{
public class MyListView : ListView
{
public delegate void ListViewItemMovedEventHandler(object sender, int oldIndex, int newIndex);
2020-07-09 20:50:40 +12:00
public event ListViewItemMovedEventHandler ItemMoving;
public event ListViewItemMovedEventHandler ItemMoved;
2013-11-03 23:53:49 +13:00
[DefaultValue(false)]
public bool AutoFillColumn { get; set; }
[DefaultValue(-1)]
public int AutoFillColumnIndex { get; set; }
2014-05-24 17:07:03 +12:00
[DefaultValue(false)]
public bool AllowColumnSort { get; set; }
// Note: AllowDrag also need to be true.
[DefaultValue(false)]
public bool AllowItemDrag { get; set; }
2022-07-14 09:07:16 +12:00
[DefaultValue(true)]
2022-07-16 01:42:32 +12:00
public bool AllowSelectAll { get; set; }
2022-07-14 09:07:16 +12:00
[DefaultValue(false)]
public bool DisableDeselect { get; set; }
2016-11-22 13:48:26 +13:00
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int SelectedIndex
{
get
{
if (SelectedIndices.Count > 0)
{
return SelectedIndices[0];
}
return -1;
}
set
{
UnselectAll();
if (value > -1)
{
2020-07-09 00:32:43 +12:00
ListViewItem lvi = Items[value];
lvi.EnsureVisible();
lvi.Selected = true;
}
}
}
2014-05-24 17:07:03 +12:00
private ListViewColumnSorter lvwColumnSorter;
2014-04-14 04:40:32 +12:00
private int lineIndex = -1;
private int lastLineIndex = -1;
2014-10-19 10:48:47 +13:00
private ListViewItem dragOverItem;
2013-11-03 23:53:49 +13:00
public MyListView()
{
SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.EnableNotifyMessage, true);
2014-04-14 04:40:32 +12:00
AutoFillColumn = false;
AutoFillColumnIndex = -1;
2014-05-24 17:07:03 +12:00
AllowColumnSort = false;
2022-07-16 01:42:32 +12:00
AllowSelectAll = true;
2013-11-03 23:53:49 +13:00
FullRowSelect = true;
View = View.Details;
2014-05-24 17:07:03 +12:00
lvwColumnSorter = new ListViewColumnSorter();
ListViewItemSorter = lvwColumnSorter;
2013-11-03 23:53:49 +13:00
}
public void FillColumn(int index)
{
if (View == View.Details && Columns.Count > 0 && index >= -1 && index < Columns.Count)
{
if (index == -1)
{
index = Columns.Count - 1;
}
int width = 0;
for (int i = 0; i < Columns.Count; i++)
{
if (i != index) width += Columns[i].Width;
}
int columnWidth = ClientSize.Width - width;
if (columnWidth > 0 && Columns[index].Width != columnWidth)
{
Columns[index].Width = columnWidth;
}
}
}
public void FillLastColumn()
{
FillColumn(-1);
}
public void Select(int index)
{
if (Items.Count > 0 && index > -1 && index < Items.Count)
{
SelectedIndex = index;
}
}
public void SelectLast()
{
if (Items.Count > 0)
{
SelectedIndex = Items.Count - 1;
}
}
public void SelectSingle(ListViewItem lvi)
{
UnselectAll();
if (lvi != null)
{
lvi.Selected = true;
}
}
2022-07-14 09:07:16 +12:00
public void SelectAll()
{
if (AllowSelectAll && MultiSelect)
{
foreach (ListViewItem lvi in Items)
{
lvi.Selected = true;
}
}
}
public void UnselectAll()
{
2020-07-08 23:14:24 +12:00
if (MultiSelect)
{
2020-07-08 23:14:24 +12:00
SelectedItems.Clear();
}
}
2020-07-09 01:54:29 +12:00
public void EnsureSelectedVisible()
{
if (SelectedItems.Count > 0)
{
SelectedItems[0].EnsureVisible();
}
}
2013-11-03 23:53:49 +13:00
protected override void OnKeyDown(KeyEventArgs e)
{
2022-07-14 09:07:16 +12:00
if (e.KeyData == (Keys.Control | Keys.A))
2013-11-03 23:53:49 +13:00
{
2022-07-14 09:07:16 +12:00
SelectAll();
2013-11-03 23:53:49 +13:00
}
base.OnKeyDown(e);
}
[DebuggerStepThrough]
protected override void WndProc(ref Message m)
{
if (AutoFillColumn && m.Msg == (int)WindowsMessages.PAINT && !DesignMode)
{
FillColumn(AutoFillColumnIndex);
}
if (m.Msg == (int)WindowsMessages.ERASEBKGND)
{
return;
}
if (DisableDeselect && m.Msg >= (int)WindowsMessages.LBUTTONDOWN && m.Msg <= (int)WindowsMessages.MBUTTONDBLCLK)
{
Point pos = new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16);
ListViewHitTestInfo hit = HitTest(pos);
switch (hit.Location)
{
case ListViewHitTestLocations.AboveClientArea:
case ListViewHitTestLocations.BelowClientArea:
case ListViewHitTestLocations.LeftOfClientArea:
case ListViewHitTestLocations.RightOfClientArea:
case ListViewHitTestLocations.None:
return;
}
}
base.WndProc(ref m);
if (m.Msg == (int)WindowsMessages.PAINT && lineIndex >= 0)
{
2014-04-14 04:40:32 +12:00
Rectangle rc = Items[lineIndex < Items.Count ? lineIndex : lineIndex - 1].GetBounds(ItemBoundsPortion.Entire);
DrawInsertionLine(rc.Left, rc.Right, lineIndex < Items.Count ? rc.Top : rc.Bottom);
}
}
protected override void OnItemDrag(ItemDragEventArgs e)
{
2014-04-14 04:40:32 +12:00
base.OnItemDrag(e);
if (AllowDrop && AllowItemDrag && e.Button == MouseButtons.Left)
{
2014-04-14 05:15:33 +12:00
DoDragDrop(e.Item, DragDropEffects.Move);
}
}
protected override void OnDragOver(DragEventArgs drgevent)
{
base.OnDragOver(drgevent);
2014-04-14 04:40:32 +12:00
2021-06-10 10:14:01 +12:00
if (drgevent.Data.GetData(typeof(ListViewItem)) is ListViewItem lvi && lvi.ListView == this)
2014-04-14 04:40:32 +12:00
{
2014-04-14 05:15:33 +12:00
drgevent.Effect = DragDropEffects.Move;
Point cp = PointToClient(new Point(drgevent.X, drgevent.Y));
dragOverItem = GetItemAt(cp.X, cp.Y);
if (dragOverItem != null)
{
lineIndex = dragOverItem.Index;
}
else
{
lineIndex = Items.Count;
}
2014-04-14 04:40:32 +12:00
2014-04-14 05:15:33 +12:00
if (lineIndex != lastLineIndex)
{
Invalidate();
}
lastLineIndex = lineIndex;
}
}
protected override void OnDragDrop(DragEventArgs drgevent)
{
base.OnDragDrop(drgevent);
2021-06-10 10:14:01 +12:00
if (drgevent.Data.GetData(typeof(ListViewItem)) is ListViewItem lvi && lvi.ListView == this && lvi != dragOverItem)
{
int oldIndex = lvi.Index;
int newIndex;
if (dragOverItem != null)
{
newIndex = dragOverItem.Index;
if (newIndex > oldIndex)
{
newIndex--;
}
}
else
{
newIndex = Items.Count - 1;
}
2020-07-09 20:50:40 +12:00
OnItemMoving(oldIndex, newIndex);
Items.RemoveAt(oldIndex);
Items.Insert(newIndex, lvi);
OnItemMoved(oldIndex, newIndex);
}
2014-04-14 04:40:32 +12:00
lineIndex = lastLineIndex = -1;
Invalidate();
}
2020-07-09 20:50:40 +12:00
protected void OnItemMoving(int oldIndex, int newIndex)
{
ItemMoving?.Invoke(this, oldIndex, newIndex);
}
protected void OnItemMoved(int oldIndex, int newIndex)
{
2020-07-09 20:50:40 +12:00
ItemMoved?.Invoke(this, oldIndex, newIndex);
}
2014-04-14 04:40:32 +12:00
protected override void OnDragLeave(EventArgs e)
{
2014-04-14 04:40:32 +12:00
base.OnDragLeave(e);
lineIndex = lastLineIndex = -1;
Invalidate();
}
2014-05-24 17:07:03 +12:00
protected override void OnColumnClick(ColumnClickEventArgs e)
{
base.OnColumnClick(e);
if (AllowColumnSort)
{
if (e.Column == lvwColumnSorter.SortColumn)
{
if (lvwColumnSorter.Order == SortOrder.Ascending)
{
lvwColumnSorter.Order = SortOrder.Descending;
}
else
{
lvwColumnSorter.Order = SortOrder.Ascending;
}
}
else
{
lvwColumnSorter.SortColumn = e.Column;
lvwColumnSorter.Order = SortOrder.Ascending;
}
2020-07-08 23:14:24 +12:00
// If the column is tagged as a DateTime, then sort by date
lvwColumnSorter.SortByDate = Columns[e.Column].Tag is DateTime;
Cursor.Current = Cursors.WaitCursor;
2014-05-24 17:07:03 +12:00
Sort();
Cursor.Current = Cursors.Default;
2014-05-24 17:07:03 +12:00
}
}
private void DrawInsertionLine(int left, int right, int y)
2014-04-14 04:40:32 +12:00
{
using (Graphics g = CreateGraphics())
{
g.DrawLine(SystemPens.HotTrack, left, y, right - 1, y);
2014-04-14 04:40:32 +12:00
Point[] leftTriangle = new Point[] { new Point(left, y - 4), new Point(left + 7, y), new Point(left, y + 4) };
g.FillPolygon(SystemBrushes.HotTrack, leftTriangle);
2014-04-14 04:40:32 +12:00
Point[] rightTriangle = new Point[] { new Point(right, y - 4), new Point(right - 8, y), new Point(right, y + 4) };
g.FillPolygon(SystemBrushes.HotTrack, rightTriangle);
}
}
protected override void ScaleControl(SizeF factor, BoundsSpecified specified)
{
base.ScaleControl(factor, specified);
2020-07-08 23:14:24 +12:00
foreach (ColumnHeader column in Columns)
{
column.Width = (int)Math.Round(column.Width * factor.Width);
}
}
2013-11-03 23:53:49 +13:00
}
}