예제 #1
0
        internal SelectionRangeInfo Split(VisualSelectionRange selectionRange)
        {
            selectionRange.SwapIfUnOrder();

            EditableVisualPointInfo startPoint = selectionRange.StartPoint;
            EditableVisualPointInfo endPoint   = selectionRange.EndPoint;

            if (startPoint.Run == endPoint.Run)
            {
                Run toBeCutTextRun = startPoint.Run;

                CopyRun leftPart   = toBeCutTextRun.LeftCopy(startPoint.RunLocalSelectedIndex);
                CopyRun middlePart = toBeCutTextRun.Copy(startPoint.RunLocalSelectedIndex, endPoint.LineCharIndex - startPoint.LineCharIndex);
                CopyRun rightPart  = toBeCutTextRun.Copy(endPoint.RunLocalSelectedIndex);

                EditableVisualPointInfo newStartRangePointInfo = null;
                EditableVisualPointInfo newEndRangePointInfo   = null;
                TextLineBox             line = this;

                if (startPoint.LineId != _currentLineNumber)
                {
                    line = _textFlowLayer.GetTextLine(startPoint.LineId);
                }

                line.LocalSuspendLineReArrange();

                if (leftPart != null)
                {
                    Run leftRun = line.AddBefore(toBeCutTextRun, leftPart);
                    newStartRangePointInfo = CreateTextPointInfo(
                        startPoint.LineId, startPoint.LineCharIndex, startPoint.X,
                        leftRun,
                        startPoint.TextRunCharOffset, startPoint.TextRunPixelOffset);
                }
                else
                {
                    //no left part,
                    //so we connect to prev text run

                    Run prevTxtRun = startPoint.Run.PrevRun;
                    if (prevTxtRun != null)
                    {
                        newStartRangePointInfo = CreateTextPointInfo(
                            startPoint.LineId, startPoint.LineCharIndex, startPoint.X,
                            prevTxtRun,
                            startPoint.TextRunCharOffset - leftPart.CharacterCount,
                            startPoint.TextRunPixelOffset - prevTxtRun.Width);
                    }
                    else
                    {
                        //no prev run, we are at the begining of the line
                        newStartRangePointInfo = CreateTextPointInfo(
                            startPoint.LineId,
                            startPoint.LineCharIndex,
                            0,
                            null,
                            0, 0);
                    }
                }

                if (rightPart != null)
                {
                    Run rightRun = line.AddAfter(toBeCutTextRun, rightPart);
                    newEndRangePointInfo =
                        CreateTextPointInfo(
                            endPoint.LineId,
                            endPoint.LineCharIndex,
                            endPoint.X,
                            null, ///??
                            startPoint.TextRunCharOffset + middlePart.CharacterCount,
                            startPoint.TextRunPixelOffset + MeasureCopyRunLength(middlePart).Width);
                }
                else
                {
                    Run nextTxtRun = endPoint.Run.NextRun;
                    if (nextTxtRun != null)
                    {
                        newEndRangePointInfo = CreateTextPointInfo(
                            endPoint.LineId,
                            endPoint.LineCharIndex,
                            endPoint.X,
                            null,  ///??
                            endPoint.TextRunPixelOffset + endPoint.Run.CharacterCount,
                            endPoint.TextRunPixelOffset + endPoint.Run.Width);
                    }
                    else
                    {
                        newEndRangePointInfo = CreateTextPointInfo(
                            endPoint.LineId,
                            endPoint.LineCharIndex,
                            endPoint.X,
                            null,
                            endPoint.TextRunCharOffset,
                            endPoint.TextRunPixelOffset);
                    }
                }

                if (middlePart != null)
                {
                    line.AddAfter(toBeCutTextRun, middlePart);
                }
                else
                {
                    throw new NotSupportedException();
                }
                line.Remove(toBeCutTextRun);
                line.LocalResumeLineReArrange();
                return(new SelectionRangeInfo(newStartRangePointInfo, newEndRangePointInfo));
            }
            else
            {
                TextLineBox workingLine = this;
                if (startPoint.LineId != _currentLineNumber)
                {
                    workingLine = _textFlowLayer.GetTextLine(startPoint.LineId);
                }
                EditableVisualPointInfo newStartPoint = workingLine.Split(startPoint);
                workingLine = this;
                if (endPoint.LineId != _currentLineNumber)
                {
                    workingLine = _textFlowLayer.GetTextLine(endPoint.LineId);
                }

                EditableVisualPointInfo newEndPoint = workingLine.Split(endPoint);
                return(new SelectionRangeInfo(newStartPoint, newEndPoint));
            }
        }
예제 #2
0
        internal void Remove(VisualSelectionRange selectionRange)
        {
            EditableVisualPointInfo startPoint = selectionRange.StartPoint;
            EditableVisualPointInfo endPoint   = selectionRange.EndPoint;

            if (startPoint.Run != null)
            {
                if (startPoint.Run == endPoint.Run)
                {
                    Run removedRun = startPoint.Run;
                    Run.InnerRemove(removedRun,
                                    startPoint.RunLocalSelectedIndex,
                                    endPoint.LineCharIndex - startPoint.LineCharIndex, false);
                    if (removedRun.CharacterCount == 0)
                    {
                        if (startPoint.LineId == _currentLineNumber)
                        {
                            this.Remove(removedRun);
                        }
                        else
                        {
                            TextLineBox line = _textFlowLayer.GetTextLine(startPoint.LineId);
                            line.Remove(removedRun);
                        }
                    }
                }
                else
                {
                    GetStartAndStopLine(startPoint, endPoint, out TextLineBox startLine, out TextLineBox stopLine);

                    EditableVisualPointInfo newStartPoint = startLine.Split(startPoint);
                    EditableVisualPointInfo newStopPoint  = stopLine.Split(endPoint);

                    if (startLine == stopLine)
                    {
                        if (newStartPoint.Run != null)
                        {
                            LinkedList <Run> tobeRemoveRuns = new LinkedList <Run>();
                            if (newStartPoint.LineCharIndex == 0)
                            {
                                foreach (Run t in _textFlowLayer.TextRunForward(
                                             newStartPoint.Run,
                                             newStopPoint.Run))
                                {
                                    tobeRemoveRuns.AddLast(t);
                                }
                            }
                            else
                            {
                                foreach (Run t in _textFlowLayer.TextRunForward(
                                             newStartPoint.Run.NextRun,
                                             newStopPoint.Run))
                                {
                                    tobeRemoveRuns.AddLast(t);
                                }
                            }
                            startLine.LocalSuspendLineReArrange();
                            foreach (Run t in tobeRemoveRuns)
                            {
                                startLine.Remove(t);
                            }
                            startLine.LocalResumeLineReArrange();
                        }
                        else
                        {
                            //this may be the blank line
                            startLine.Clear();
#if DEBUG
                            //TODO: review here again
                            //System.Diagnostics.Debug.WriteLine("EditableTextLine_adv1");
#endif
                        }
                    }
                    else
                    {
                        int startLineId = newStartPoint.LineId;
                        int stopLineId  = newStopPoint.LineId;
                        if (newStopPoint.LineCharIndex > 0)
                        {
                            stopLine.RemoveLeft(newStopPoint.Run);
                        }
                        for (int i = stopLineId - 1; i > startLineId; i--)
                        {
                            TextLineBox line = _textFlowLayer.GetTextLine(i);
                            line.Clear();
                            line.JoinWithNextLine();
                        }
                        if (newStartPoint.LineCharIndex == 0)
                        {
                            startLine.RemoveRight(newStartPoint.Run);
                        }
                        else
                        {
                            Run nextRun = (newStartPoint.Run).NextRun;
                            if (nextRun != null)
                            {
                                startLine.RemoveRight(nextRun);
                            }
                        }
                        startLine.JoinWithNextLine();
                    }
                }
            }
            else
            {
                GetStartAndStopLine(startPoint, endPoint, out TextLineBox startLine, out TextLineBox stopLine);
                EditableVisualPointInfo newStartPoint = startLine.Split(startPoint);
                EditableVisualPointInfo newStopPoint  = stopLine.Split(endPoint);
                if (startLine == stopLine)
                {
                    if (newStartPoint.Run != null)
                    {
                        LinkedList <Run> tobeRemoveRuns = new LinkedList <Run>();
                        if (newStartPoint.LineCharIndex == -1)
                        {
                            foreach (Run t in _textFlowLayer.TextRunForward(
                                         newStartPoint.Run,
                                         newStopPoint.Run))
                            {
                                tobeRemoveRuns.AddLast(t);
                            }
                        }
                        else
                        {
                            foreach (Run t in _textFlowLayer.TextRunForward(
                                         newStartPoint.Run.NextRun,
                                         newStopPoint.Run))
                            {
                                tobeRemoveRuns.AddLast(t);
                            }
                        }
                        foreach (Run t in tobeRemoveRuns)
                        {
                            startLine.Remove(t);
                        }
                    }
                    else
                    {
                        throw new NotSupportedException();
                    }
                }
                else
                {
                    int startLineId = newStartPoint.LineId;
                    int stopLineId  = newStopPoint.LineId;
                    if (newStopPoint.LineCharIndex > -1)
                    {
                        stopLine.RemoveLeft(newStopPoint.Run);
                    }
                    for (int i = stopLineId - 1; i > startLineId; i--)
                    {
                        TextLineBox line = _textFlowLayer.GetTextLine(i);
                        line.Clear();
                        line.JoinWithNextLine();
                    }
                    if (newStartPoint.LineCharIndex == -1)
                    {
                        //TODO: review here again
                        //at this point newStartPoint.TextRun should always null
                        if (newStartPoint.Run != null)
                        {
                            startLine.RemoveRight(newStartPoint.Run);
                        }
                    }
                    else
                    {
                        //at this point newStartPoint.TextRun should always null
                        //TODO newStartPoint.TextRun == null???
                        if (newStartPoint.Run != null)
                        {
                            Run nextRun = newStartPoint.Run.NextRun;
                            if (nextRun != null)
                            {
                                startLine.RemoveRight(nextRun);
                            }
                        }
                    }
                    startLine.JoinWithNextLine();
                }
            }
        }