using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; namespace BizHawk.Client.EmuHawk { /// /// A performant VirtualListView implementation that doesn't rely on native Win32 API calls /// (and in fact does not inherit the ListView class at all) /// It is an enhanced version of the work done with GDI+ rendering in InputRoll.cs /// ------------------- /// *** API Related *** /// ------------------- /// public partial class PlatformAgnosticVirtualListView { private Cell _draggingCell; #region Methods /// /// Parent form calls this to add columns /// /// /// /// /// public void AddColumn(string columnName, string columnText, int columnWidth, ListColumn.InputType columnType = ListColumn.InputType.Boolean) { if (AllColumns[columnName] == null) { var column = new ListColumn { Name = columnName, Text = columnText, Width = columnWidth, Type = columnType }; AllColumns.Add(column); } } /// /// Sets the state of the passed row index /// /// /// public void SelectItem(int index, bool val) { if (_columns.VisibleColumns.Any()) { if (val) { SelectCell(new Cell { RowIndex = index, Column = _columns[0] }); } else { IEnumerable items = _selectedItems.Where(cell => cell.RowIndex == index); _selectedItems.RemoveWhere(items.Contains); } } } public void SelectAll() { var oldFullRowVal = FullRowSelect; FullRowSelect = true; for (int i = 0; i < ItemCount; i++) { SelectItem(i, true); } FullRowSelect = oldFullRowVal; } public void DeselectAll() { _selectedItems.Clear(); } public void TruncateSelection(int index) { _selectedItems.RemoveWhere(cell => cell.RowIndex > index); } public bool IsVisible(int index) { return (index >= FirstVisibleRow) && (index <= LastFullyVisibleRow); } public bool IsPartiallyVisible(int index) { return index >= FirstVisibleRow && index <= LastVisibleRow; } public void DragCurrentCell() { _draggingCell = CurrentCell; } public void ReleaseCurrentCell() { if (_draggingCell != null) { var draggedCell = _draggingCell; _draggingCell = null; if (CurrentCell != draggedCell) { CellDropped?.Invoke(this, new CellEventArgs(draggedCell, CurrentCell)); } } } /// /// Scrolls to the given index, according to the scroll settings. /// public void ScrollToIndex(int index) { if (ScrollMethod == "near") { MakeIndexVisible(index); } if (!IsVisible(index) || AlwaysScroll) { if (ScrollMethod == "top") { FirstVisibleRow = index; } else if (ScrollMethod == "bottom") { LastVisibleRow = index; } else if (ScrollMethod == "center") { FirstVisibleRow = Math.Max(index - (VisibleRows / 2), 0); } } } /// /// Scrolls so that the given index is visible, if it isn't already; doesn't use scroll settings. /// public void MakeIndexVisible(int index) { if (!IsVisible(index)) { if (FirstVisibleRow > index) { FirstVisibleRow = index; } else { LastVisibleRow = index; } } } /// /// Compatibility method from VirtualListView /// /// public void ensureVisible() { if (_selectedItems.Count != 0) MakeIndexVisible(_selectedItems.Last().RowIndex.Value); } public void ClearSelectedRows() { _selectedItems.Clear(); } #endregion } }