2019-05-02 22:45:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
2019-05-11 17:16:54 +00:00
|
|
|
|
using System.Collections;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
using System.Collections.Generic;
|
2019-05-11 17:16:54 +00:00
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Collections.Specialized;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
using System.Diagnostics;
|
2019-05-11 17:16:54 +00:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Linq;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
using System.Text;
|
2019-05-11 17:16:54 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
namespace SourceGenWPF {
|
|
|
|
|
/// <summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
/// List of items formatted for display.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This is intended to be useful as an ItemSource for a WPF ListView. We need to implement
|
|
|
|
|
/// plain IList to cause ListView to perform data virtualization, and the property/collection
|
|
|
|
|
/// changed events so the view will pick up our changes.
|
|
|
|
|
///
|
|
|
|
|
/// The ItemsControl.ItemsSource property wants an IEnumerable (which IList implements).
|
|
|
|
|
/// According to various articles, if the object implements IList, and the UI element
|
|
|
|
|
/// is providing UI virtualization, you will also get data virtualization. This behavior
|
|
|
|
|
/// doesn't seem to be documented anywhere, but the consensus is that it's expected to work.
|
|
|
|
|
///
|
|
|
|
|
/// Implementing generic IList doesn't seem necessary for XAML, but is useful for other
|
|
|
|
|
/// customers of the data (e.g. the assembler source generator).
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public class DisplayList : IList<DisplayList.FormattedParts>, IList,
|
|
|
|
|
INotifyCollectionChanged, INotifyPropertyChanged {
|
|
|
|
|
|
|
|
|
|
// TODO: check VirtualizingStackPanel.VirtualizationMode == recycling (page 259)
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
/// List of formatted parts. The idea is that the list is initially populated with
|
|
|
|
|
/// null references, and FormattedParts objects are generated on demand.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private List<FormattedParts> mList;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
/// Constructs an empty collection, with the default initial capacity.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public DisplayList() {
|
|
|
|
|
mList = new List<FormattedParts>();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public DisplayList(int count) {
|
|
|
|
|
mList = new List<FormattedParts>(count);
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
mList.Add(null);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#region Property / Collection Changed
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
// See ObservableCollection class, e.g.
|
|
|
|
|
// https://github.com/Microsoft/referencesource/blob/master/System/compmod/system/collections/objectmodel/observablecollection.cs
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private const string CountString = "Count";
|
|
|
|
|
private const string IndexerName = "Item[]";
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#if false
|
|
|
|
|
protected override void ClearItems() {
|
|
|
|
|
base.ClearItems();
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionReset();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
protected override void RemoveItem(int index) {
|
|
|
|
|
FormattedParts removedItem = this[index];
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
base.RemoveItem(index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removedItem, index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
protected override void InsertItem(int index, FormattedParts item) {
|
|
|
|
|
base.InsertItem(index, item);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
protected override void SetItem(int index, FormattedParts item) {
|
|
|
|
|
FormattedParts originalItem = this[index];
|
|
|
|
|
base.SetItem(index, item);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Replace, originalItem, item, index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#endif
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
|
|
|
|
|
PropertyChanged?.Invoke(this, e);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private void OnPropertyChanged(string propertyName) {
|
|
|
|
|
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
|
|
|
|
|
CollectionChanged?.Invoke(this, e);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private void OnCollectionChanged(NotifyCollectionChangedAction action,
|
|
|
|
|
object item, int index) {
|
|
|
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index));
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private void OnCollectionChanged(NotifyCollectionChangedAction action,
|
|
|
|
|
object item, int index, int oldIndex) {
|
|
|
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, item, index, oldIndex));
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private void OnCollectionChanged(NotifyCollectionChangedAction action,
|
|
|
|
|
object oldItem, object newItem, int index) {
|
|
|
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newItem, oldItem, index));
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
private void OnCollectionReset() {
|
|
|
|
|
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#endregion Property / Collection Changed
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#region IList / IList<T>
|
|
|
|
|
public int Count => ((IList<FormattedParts>)mList).Count;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public bool IsReadOnly => ((IList<FormattedParts>)mList).IsReadOnly;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public bool IsFixedSize => ((IList)mList).IsFixedSize;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public object SyncRoot => ((IList)mList).SyncRoot;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public bool IsSynchronized => ((IList)mList).IsSynchronized;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void Add(FormattedParts item) {
|
|
|
|
|
((IList<FormattedParts>)mList).Add(item);
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionReset();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public int Add(object value) {
|
|
|
|
|
int posn = ((IList)mList).Add(value);
|
|
|
|
|
if (posn >= 0) {
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Add, value, posn);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
return posn;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void Clear() {
|
|
|
|
|
((IList<FormattedParts>)mList).Clear();
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionReset();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public bool Contains(FormattedParts item) {
|
|
|
|
|
return ((IList<FormattedParts>)mList).Contains(item);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
bool IList.Contains(object value) {
|
|
|
|
|
return Contains((FormattedParts)value);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void CopyTo(FormattedParts[] array, int arrayIndex) {
|
|
|
|
|
((IList<FormattedParts>)mList).CopyTo(array, arrayIndex);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void CopyTo(Array array, int index) {
|
|
|
|
|
((IList)mList).CopyTo(array, index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public IEnumerator<FormattedParts> GetEnumerator() {
|
|
|
|
|
// Use the indexer, rather than mList's enumerator, to get on-demand string gen.
|
|
|
|
|
for (int i = 0; i < Count; i++) {
|
|
|
|
|
yield return this[i];
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() {
|
|
|
|
|
return GetEnumerator();
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public int IndexOf(FormattedParts item) {
|
|
|
|
|
return ((IList<FormattedParts>)mList).IndexOf(item);
|
|
|
|
|
}
|
|
|
|
|
int IList.IndexOf(object value) {
|
|
|
|
|
return IndexOf((FormattedParts)value);
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void Insert(int index, FormattedParts item) {
|
|
|
|
|
((IList<FormattedParts>)mList).Insert(index, item);
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Add, item, index);
|
|
|
|
|
}
|
|
|
|
|
void IList.Insert(int index, object value) {
|
|
|
|
|
Insert(index, (FormattedParts)value);
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public void RemoveAt(int index) {
|
|
|
|
|
FormattedParts removed = mList[index];
|
|
|
|
|
((IList<FormattedParts>)mList).RemoveAt(index);
|
|
|
|
|
OnPropertyChanged(CountString);
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Remove, removed, index);
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public bool Remove(FormattedParts item) {
|
|
|
|
|
// NotifyCollectionChangedAction.Remove wants an index. We can find the index
|
|
|
|
|
// of the first matching item and then do a RemoveAt, but this call just isn't
|
|
|
|
|
// all that interesting for us, so it's easier to ignore it.
|
|
|
|
|
//return ((IList<FormattedParts>)mList).Remove(item);
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
void IList.Remove(object value) {
|
|
|
|
|
//Remove((FormattedParts)value);
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
object IList.this[int index] {
|
|
|
|
|
// forward to generic impl
|
|
|
|
|
get { return this[index]; }
|
|
|
|
|
set { this[index] = (FormattedParts)value; }
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
// For IList<T>.
|
|
|
|
|
public FormattedParts this[int index] {
|
|
|
|
|
get {
|
|
|
|
|
FormattedParts parts = mList[index];
|
|
|
|
|
if (parts == null) {
|
|
|
|
|
parts = mList[index] = GetEntry(index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
return parts;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
set {
|
|
|
|
|
FormattedParts orig = mList[index];
|
|
|
|
|
mList[index] = value;
|
|
|
|
|
OnPropertyChanged(IndexerName);
|
|
|
|
|
OnCollectionChanged(NotifyCollectionChangedAction.Replace, orig, value, index);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
#endregion IList / IList<T>
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the Nth element.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private FormattedParts GetEntry(int index) {
|
|
|
|
|
Debug.WriteLine("GEN " + index);
|
2019-05-12 00:36:50 +00:00
|
|
|
|
if ((index % 10) != 0) {
|
|
|
|
|
return FormattedParts.Create("off" + index, "addr" + index, "12 34",
|
|
|
|
|
"vncidmx", "", "yup:", "LDA", "$1234", "a & b");
|
|
|
|
|
} else {
|
|
|
|
|
return FormattedParts.Create("offN This is a long comment line");
|
|
|
|
|
}
|
2019-05-11 17:16:54 +00:00
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public class FormattedParts {
|
|
|
|
|
public string Offset { get; private set; }
|
|
|
|
|
public string Addr { get; private set; }
|
|
|
|
|
public string Bytes { get; private set; }
|
|
|
|
|
public string Flags { get; private set; }
|
|
|
|
|
public string Attr { get; private set; }
|
|
|
|
|
public string Label { get; private set; }
|
|
|
|
|
public string Opcode { get; private set; }
|
|
|
|
|
public string Operand { get; private set; }
|
|
|
|
|
public string Comment { get; private set; }
|
2019-05-12 00:36:50 +00:00
|
|
|
|
public bool SingleLine { get; private set; }
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
// Construct with factory methods.
|
|
|
|
|
private FormattedParts() { }
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
public static FormattedParts Create(string offset, string addr, string bytes,
|
|
|
|
|
string flags, string attr, string label, string opcode, string operand,
|
|
|
|
|
string comment) {
|
|
|
|
|
FormattedParts parts = new FormattedParts();
|
|
|
|
|
parts.Offset = offset;
|
|
|
|
|
parts.Addr = addr;
|
|
|
|
|
parts.Bytes = bytes;
|
|
|
|
|
parts.Flags = flags;
|
|
|
|
|
parts.Attr = attr;
|
|
|
|
|
parts.Label = label;
|
|
|
|
|
parts.Opcode = opcode;
|
|
|
|
|
parts.Operand = operand;
|
|
|
|
|
parts.Comment = comment;
|
2019-05-12 00:36:50 +00:00
|
|
|
|
parts.SingleLine = false;
|
|
|
|
|
|
|
|
|
|
return parts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static FormattedParts Create(string longComment) {
|
|
|
|
|
FormattedParts parts = new FormattedParts();
|
|
|
|
|
parts.Comment = longComment;
|
|
|
|
|
parts.SingleLine = true;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
2019-05-11 17:16:54 +00:00
|
|
|
|
return parts;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|