aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/Components/SelectedObjectCollection.cs
blob: e09bb832e638f9b89df82884227b6d3daa0164bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.SharedUI.Components
{
    public class SelectedObjectCollection<T> : ObservableCollection<SelectedObject<T>>
    {
        public event EventHandler SelectionChanged;
        public ObservableCollection<T> Source { get; set; }
        public ObservableCollection<T> SynchedSource { get; set; }

        public SelectedObjectCollection(ObservableCollection<T> source, ObservableCollection<T> synchedSource)
        {
            SynchedSource = synchedSource;
            Source = source;

            foreach (var item in source)
            {
                var selectedItem = new SelectedObject<T>(item, synchedSource.Contains(item));
                this.Add(selectedItem);
                selectedItem.IsSelectedChanged += SelectedItem_IsSelectedChanged;
            }
        }

        private void SelectedItem_IsSelectedChanged(object sender, EventArgs e)
        {
            SelectedObject<T> item = sender as SelectedObject<T>;
            
            if (item.IsSelected)
            {
                if (!SynchedSource.Contains(item.Data))
                {
                    SynchedSource.Add(item.Data);
                }
            }
            else
            {
                SynchedSource.Remove(item.Data);
            }

            SelectionChanged?.Invoke(this, new EventArgs());
        }
    }
}
class="w"> - element.VisualColumn; TextRun run = element.CreateTextRun(textSourceCharacterIndex, this); if (run == null) throw new ArgumentNullException(element.GetType().Name + ".CreateTextRun"); if (run.Length == 0) throw new ArgumentException("The returned TextRun must not have length 0.", element.GetType().Name + ".Length"); if (relativeOffset + run.Length > element.VisualLength) throw new ArgumentException("The returned TextRun is too long.", element.GetType().Name + ".CreateTextRun"); InlineObjectRun inlineRun = run as InlineObjectRun; if (inlineRun != null) { inlineRun.VisualLine = VisualLine; VisualLine.hasInlineObjects = true; TextView.AddInlineObject(inlineRun); } return run; } } if (TextView.Options.ShowEndOfLine && textSourceCharacterIndex == VisualLine.VisualLength) { return CreateTextRunForNewLine(); } return new TextEndOfParagraph(1); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } } TextRun CreateTextRunForNewLine() { string newlineText = ""; DocumentLine lastDocumentLine = VisualLine.LastDocumentLine; if (lastDocumentLine.DelimiterLength == 2) { newlineText = "¶"; } else if (lastDocumentLine.DelimiterLength == 1) { char newlineChar = Document.GetCharAt(lastDocumentLine.Offset + lastDocumentLine.Length); if (newlineChar == '\r') newlineText = "\\r"; else if (newlineChar == '\n') newlineText = "\\n"; else newlineText = "?"; } return new FormattedTextRun(new FormattedTextElement(TextView.cachedElements.GetTextForNonPrintableCharacter(newlineText, this), 0), GlobalTextRunProperties); } public override TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText(int textSourceCharacterIndexLimit) { try { foreach (VisualLineElement element in VisualLine.Elements) { if (textSourceCharacterIndexLimit > element.VisualColumn && textSourceCharacterIndexLimit <= element.VisualColumn + element.VisualLength) { TextSpan<CultureSpecificCharacterBufferRange> span = element.GetPrecedingText(textSourceCharacterIndexLimit, this); if (span == null) break; int relativeOffset = textSourceCharacterIndexLimit - element.VisualColumn; if (span.Length > relativeOffset) throw new ArgumentException("The returned TextSpan is too long.", element.GetType().Name + ".GetPrecedingText"); return span; } } CharacterBufferRange empty = CharacterBufferRange.Empty; return new TextSpan<CultureSpecificCharacterBufferRange>(empty.Length, new CultureSpecificCharacterBufferRange(null, empty)); } catch (Exception ex) { Debug.WriteLine(ex.ToString()); throw; } } public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex(int textSourceCharacterIndex) { throw new NotSupportedException(); } string cachedString; int cachedStringOffset; public StringSegment GetText(int offset, int length) { if (cachedString != null) { if (offset >= cachedStringOffset && offset + length <= cachedStringOffset + cachedString.Length) { return new StringSegment(cachedString, offset - cachedStringOffset, length); } } cachedStringOffset = offset; return new StringSegment(cachedString = this.Document.GetText(offset, length)); } } }