Example #1
0
        public bool IsInputable(int position)
        {
            if (position == 0 || position == ProfileText.Length)
            {
                return(true);
            }
            var pos = ProfileTextPostionList.FindAtPosition(position);

            if (pos.Position.Offset == position)
            {
                return(true);
            }
            if (pos.Position.Offset + pos.Position.Length == position)
            {
                return(true);
            }
            if (pos.Fragment.FragmentType == FragmentType.Text)
            {
                return(true);
            }
            return(false);
        }
Example #2
0
        public void GetFragmentsAt(out Fragment before, out Fragment after, int position)
        {
            var pos = ProfileTextPostionList.FindAtPosition(position);

            if (pos == null)
            {
                before = Profile;
                after  = null;
                return;
            }

            if (pos.Position.Offset + pos.Position.Length == position)
            {
                before = pos.Fragment;
                after  = null;
                return;
            }

            if (pos.Position.Offset != position && pos.Position.Offset + pos.Position.Length != position)
            {
                before = pos.Fragment;
                after  = pos.Fragment;
                return;
            }

            after = pos.Fragment;
            var fragments = ((FragmentBody)after.Parent).FragmentList;
            var i         = fragments.IndexOf(after);

            if (i == 0 && after.ParentFragment.FragmentType == FragmentType.TextBlock)
            {
                fragments = ((FragmentBody)after.ParentFragment.Parent).FragmentList;
                i         = fragments.IndexOf(after.ParentFragment);
            }
            before = i == 0 ? null : fragments[i - 1];
        }
Example #3
0
        public FragmentSelection GetSelection(int start, int end)
        {
            Contract.Requires(IsSelectable(start, end, false));
            var      fragmentSelection = new FragmentSelection(start, end);
            Fragment beforeStart, afterStart, beforeEnd, afterEnd;

            GetFragmentsAt(out beforeStart, out afterStart, start);
            GetFragmentsAt(out beforeEnd, out afterEnd, end);
            if (beforeStart != afterStart && beforeEnd != afterEnd)
            {
                if (afterStart == beforeEnd)
                {
                    fragmentSelection.Fragments.Add(afterStart);
                }
                else if (afterStart.ParentFragment == beforeEnd.ParentFragment)
                {
                    var fragments = ((FragmentBody)afterStart.Parent).FragmentList;
                    CopySelectionFragments(fragments, fragmentSelection.Fragments, fragments.IndexOf(afterStart),
                                           fragments.IndexOf(beforeEnd));
                }
                else
                {
                    var prefixText = afterStart.ParentFragment is TextBlock;
                    var suffixText = beforeEnd.ParentFragment is TextBlock;
                    var fragments  = prefixText
                        ? ((FragmentBody)afterStart.ParentFragment.Parent).FragmentList
                        : ((FragmentBody)afterStart.Parent).FragmentList;
                    int i;
                    if (prefixText)
                    {
                        i = fragments.IndexOf(afterStart.ParentFragment) + 1;
                        var textFragments = ((FragmentBody)afterStart.Parent).FragmentList;
                        CopySelectionFragments(textFragments, fragmentSelection.Fragments,
                                               textFragments.IndexOf(afterStart), textFragments.Count - 1);
                    }
                    else
                    {
                        i = fragments.IndexOf(afterStart);
                    }
                    var j = suffixText ? fragments.IndexOf(beforeEnd.ParentFragment) - 1 : fragments.IndexOf(beforeEnd);
                    CopySelectionFragments(fragments, fragmentSelection.Fragments, i, j);
                    if (suffixText)
                    {
                        var textFragments = ((FragmentBody)beforeEnd.Parent).FragmentList;
                        CopySelectionFragments(textFragments, fragmentSelection.Fragments, 0,
                                               textFragments.IndexOf(beforeEnd));
                    }
                }
            }
            else if (beforeStart == afterStart && beforeEnd != afterEnd)
            {
                Contract.Assert(beforeStart is Text);
                var pos = ProfileTextPostionList.GetFragmentPosition(afterStart);
                fragmentSelection.SetPrefix((Text)afterStart, start - pos.Position.Offset);
                if (afterStart != beforeEnd)
                {
                    var fragments = ((FragmentBody)afterStart.Parent).FragmentList;
                    var n         = fragments.Count;
                    var i         = fragments.IndexOf(afterStart);
                    CopySelectionFragments(fragments, fragmentSelection.Fragments, i + 1, n - 1);
                    fragmentSelection.Fragments.Add(beforeEnd);
                }
            }
            else if (beforeStart != afterStart && beforeEnd == afterEnd)
            {
                Contract.Assert(afterEnd is Text);
                var pos = ProfileTextPostionList.GetFragmentPosition(beforeEnd);
                fragmentSelection.SetSuffix((Text)beforeEnd, end - pos.Position.Offset);
                if (beforeEnd != afterStart)
                {
                    fragmentSelection.Fragments.Add(afterStart);
                }
            }
            else if (beforeStart == afterStart && beforeEnd == afterEnd)
            {
                Contract.Assert(afterEnd is Text);
                var pos = ProfileTextPostionList.GetFragmentPosition(beforeEnd);
                fragmentSelection.SetInfix((Text)beforeEnd, start - pos.Position.Offset, end - pos.Position.Offset);
            }

            fragmentSelection.ProfileText = ProfileText.Substring(start, end - start);
            return(fragmentSelection);
        }