/// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="Colors">The color of the segment.</param>
        /// <param name="Text">The text associated with the segment.</param>
        public ColoredDocumentLineSegment(ColorPair Colors, string Text)
        {
            if (Text == null)
            {
                throw new ArgumentNullException("Text");
            }

            mColor = Colors;
            mText  = Text;
        }
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="Colors">The color of the segment.</param>
		/// <param name="Text">The text associated with the segment.</param>
		public ColoredDocumentLineSegment(ColorPair Colors, string Text)
		{
			if(Text == null)
			{
				throw new ArgumentNullException("Text");
			}

			mColor = Colors;
			mText = Text;
		}
            /// <summary>
            /// Gets an array of colored line segments that can be used for drawing.
            /// </summary>
            /// <remarks>
            /// If the line of text contains any instances of <paramref name="FindText"/> they will be replaced with new line segments that contain <paramref name="InsertionColors"/> for drawing.
            /// </remarks>
            /// <param name="StartIndex">The starting character at which to begin generating segments.</param>
            /// <param name="Length">The number of characters to include in segment generation.</param>
            /// <param name="FindText">Text to search for that requires special coloring.</param>
            /// <param name="InsertionColors">The colors to use when drawing <paramref name="FindText"/>.</param>
            /// <param name="ComparisonType">The type of string comparison that will be conducted when searching for <paramref name="FindText"/>.</param>
            /// <param name="bHasFindText">Set to True if <paramref name="FindText"/> exists within the line segments.</param>
            /// <returns>An array of colored line segments.</returns>
            public ColoredDocumentLineSegment[] GetLineSegmentsWithFindString(int StartIndex, int Length, string FindText, ColorPair InsertionColors, StringComparison ComparisonType, out bool bHasFindText)
            {
                string LineText      = this.ToString();
                int    FindTextIndex = LineText.IndexOf(FindText, ComparisonType);
                int    EndLineIndex  = StartIndex + Length;
                List <ColoredTextRange>           FindTextSegments = new List <ColoredTextRange>(mColorRanges.Count);
                List <ColoredDocumentLineSegment> Segments         = new List <ColoredDocumentLineSegment>(mColorRanges.Count * 2);

                while (FindTextIndex != -1)
                {
                    if (FindTextIndex >= EndLineIndex)
                    {
                        break;
                    }

                    // If any part of the text is past StartIndex it will be visible so add it to the array
                    if (FindTextIndex + FindText.Length > StartIndex)
                    {
                        if (FindTextIndex >= StartIndex)
                        {
                            FindTextSegments.Add(new ColoredTextRange(InsertionColors.ForeColor, InsertionColors.BackColor, FindTextIndex, FindText.Length));
                        }
                        else
                        {
                            FindTextSegments.Add(new ColoredTextRange(InsertionColors.ForeColor, InsertionColors.BackColor, StartIndex, (FindTextIndex + FindText.Length) - StartIndex));
                        }
                    }

                    FindTextIndex = LineText.IndexOf(FindText, FindTextIndex + FindText.Length, ComparisonType);
                }

                bHasFindText = FindTextSegments.Count > 0;

                int  CurrentFindTextSegmentIndex = 0;
                bool bHasOverlap = false;

                foreach (ColoredTextRange TextRange in this.mColorRanges)
                {
                    if (TextRange.StartIndex < StartIndex && TextRange.StartIndex + TextRange.Length <= StartIndex)
                    {
                        continue;
                    }
                    else if (TextRange.StartIndex >= EndLineIndex)
                    {
                        break;
                    }

                    int CurTextRangeStart = Math.Max(StartIndex, TextRange.StartIndex);

                    if (CurrentFindTextSegmentIndex < FindTextSegments.Count)
                    {
                        int CurTextRangeEnd = Math.Min(TextRange.StartIndex + TextRange.Length, EndLineIndex);

                        // check to see if it's an overlapping find segment
                        if (bHasOverlap)
                        {
                            // if it is then it has already been added so reassign the start point
                            CurTextRangeStart = FindTextSegments[CurrentFindTextSegmentIndex].StartIndex + FindTextSegments[CurrentFindTextSegmentIndex].Length;
                            ++CurrentFindTextSegmentIndex;
                            bHasOverlap = false;
                        }

                        while (CurrentFindTextSegmentIndex < FindTextSegments.Count && CurTextRangeStart < EndLineIndex && FindTextSegments[CurrentFindTextSegmentIndex].StartIndex >= CurTextRangeStart &&
                               FindTextSegments[CurrentFindTextSegmentIndex].StartIndex < CurTextRangeEnd)
                        {
                            if (FindTextSegments[CurrentFindTextSegmentIndex].StartIndex > CurTextRangeStart)
                            {
                                Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex - CurTextRangeStart, EndLineIndex - CurTextRangeStart))));
                            }

                            if (FindTextSegments[CurrentFindTextSegmentIndex].StartIndex < EndLineIndex)
                            {
                                Segments.Add(new ColoredDocumentLineSegment(InsertionColors, this.ToString(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex, Math.Min(FindTextSegments[CurrentFindTextSegmentIndex].Length, EndLineIndex - FindTextSegments[CurrentFindTextSegmentIndex].StartIndex))));
                            }

                            CurTextRangeStart = FindTextSegments[CurrentFindTextSegmentIndex].StartIndex + FindTextSegments[CurrentFindTextSegmentIndex].Length;

                            // if the beginning of the next segment is within the current text range move onto the next "find" segment
                            // if not that means the current "find" text segment overlaps into the next regular text segment
                            if (CurTextRangeStart < CurTextRangeEnd)
                            {
                                ++CurrentFindTextSegmentIndex;
                            }
                            else
                            {
                                bHasOverlap = true;
                            }
                        }

                        // if there was text remaining in the current text range after the last segment then add it now
                        if (CurTextRangeStart < CurTextRangeEnd && CurTextRangeStart < EndLineIndex)
                        {
                            Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(CurTextRangeEnd - CurTextRangeStart, EndLineIndex - CurTextRangeStart))));
                        }
                    }
                    else
                    {
                        Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(TextRange.Length, EndLineIndex - CurTextRangeStart))));
                    }
                }

                return(Segments.ToArray());
            }
 /// <summary>
 /// Gets an array of colored line segments that can be used for drawing.
 /// </summary>
 /// <remarks>
 /// If the line of text contains any instances of <paramref name="FindText"/> they will be replaced with new line segments that contain <paramref name="InsertionColors"/> for drawing.
 /// </remarks>
 /// <param name="LineIndex">The index of the line to retrieve the segments for.</param>
 /// <param name="StartIndex">The starting character at which to begin generating segments.</param>
 /// <param name="Length">The number of characters to include in segment generation.</param>
 /// <param name="FindText">Text to search for that requires special coloring.</param>
 /// <param name="InsertionColors">The colors to use when drawing <paramref name="FindText"/>.</param>
 /// <param name="ComparisonType">The type of string comparison that will be conducted when searching for <paramref name="FindText"/>.</param>
 /// <param name="bHasFindText">Set to True if <paramref name="FindText"/> exists within the line segments.</param>
 /// <returns>An array of colored line segments.</returns>
 public ColoredDocumentLineSegment[] GetLineSegmentsWithFindString(int LineIndex, int StartIndex, int Length, string FindText, ColorPair InsertionColors, StringComparison ComparisonType, out bool bHasFindText)
 {
     return(mLines[LineIndex].GetLineSegmentsWithFindString(StartIndex, Length, FindText, InsertionColors, ComparisonType, out bHasFindText));
 }
 /// <summary>
 /// Gets an array of colored line segments that can be used for drawing.
 /// </summary>
 /// <remarks>
 /// If the line of text contains any instances of <paramref name="FindText"/> they will be replaced with new line segments that contain <paramref name="InsertionColors"/> for drawing.
 /// </remarks>
 /// <param name="LineIndex">The index of the line to retrieve the segments for.</param>
 /// <param name="StartIndex">The starting character at which to begin generating segments.</param>
 /// <param name="Length">The number of characters to include in segment generation.</param>
 /// <param name="FindText">Text to search for that requires special coloring.</param>
 /// <param name="InsertionColors">The colors to use when drawing <paramref name="FindText"/>.</param>
 /// <param name="ComparisonType">The type of string comparison that will be conducted when searching for <paramref name="FindText"/>.</param>
 /// <param name="bHasFindText">Set to True if <paramref name="FindText"/> exists within the line segments.</param>
 /// <returns>An array of colored line segments.</returns>
 public ColoredDocumentLineSegment[] GetLineSegmentsWithFindString(int LineIndex, int StartIndex, int Length, string FindText, ColorPair InsertionColors, StringComparison ComparisonType, out bool bHasFindText)
 {
     return mLines[LineIndex].GetLineSegmentsWithFindString(StartIndex, Length, FindText, InsertionColors, ComparisonType, out bHasFindText);
 }
            /// <summary>
            /// Gets an array of colored line segments that can be used for drawing.
            /// </summary>
            /// <remarks>
            /// If the line of text contains any instances of <paramref name="FindText"/> they will be replaced with new line segments that contain <paramref name="InsertionColors"/> for drawing.
            /// </remarks>
            /// <param name="StartIndex">The starting character at which to begin generating segments.</param>
            /// <param name="Length">The number of characters to include in segment generation.</param>
            /// <param name="FindText">Text to search for that requires special coloring.</param>
            /// <param name="InsertionColors">The colors to use when drawing <paramref name="FindText"/>.</param>
            /// <param name="ComparisonType">The type of string comparison that will be conducted when searching for <paramref name="FindText"/>.</param>
            /// <param name="bHasFindText">Set to True if <paramref name="FindText"/> exists within the line segments.</param>
            /// <returns>An array of colored line segments.</returns>
            public ColoredDocumentLineSegment[] GetLineSegmentsWithFindString(int StartIndex, int Length, string FindText, ColorPair InsertionColors, StringComparison ComparisonType, out bool bHasFindText)
            {
                string LineText = this.ToString();
                int FindTextIndex = LineText.IndexOf(FindText, ComparisonType);
                int EndLineIndex = StartIndex + Length;
                List<ColoredTextRange> FindTextSegments = new List<ColoredTextRange>(mColorRanges.Count);
                List<ColoredDocumentLineSegment> Segments = new List<ColoredDocumentLineSegment>(mColorRanges.Count * 2);

                while(FindTextIndex != -1)
                {
                    if(FindTextIndex >= EndLineIndex)
                    {
                        break;
                    }

                    // If any part of the text is past StartIndex it will be visible so add it to the array
                    if(FindTextIndex + FindText.Length > StartIndex)
                    {
                        if(FindTextIndex >= StartIndex)
                        {
                            FindTextSegments.Add(new ColoredTextRange(InsertionColors.ForeColor, InsertionColors.BackColor, FindTextIndex, FindText.Length));
                        }
                        else
                        {
                            FindTextSegments.Add(new ColoredTextRange(InsertionColors.ForeColor, InsertionColors.BackColor, StartIndex, (FindTextIndex + FindText.Length) - StartIndex));
                        }
                    }

                    FindTextIndex = LineText.IndexOf(FindText, FindTextIndex + FindText.Length, ComparisonType);
                }

                bHasFindText = FindTextSegments.Count > 0;

                int CurrentFindTextSegmentIndex = 0;
                bool bHasOverlap = false;

                foreach(ColoredTextRange TextRange in this.mColorRanges)
                {
                    if(TextRange.StartIndex < StartIndex && TextRange.StartIndex + TextRange.Length <= StartIndex)
                    {
                        continue;
                    }
                    else if(TextRange.StartIndex >= EndLineIndex)
                    {
                        break;
                    }

                    int CurTextRangeStart = Math.Max(StartIndex, TextRange.StartIndex);

                    if(CurrentFindTextSegmentIndex < FindTextSegments.Count)
                    {
                        int CurTextRangeEnd = Math.Min(TextRange.StartIndex + TextRange.Length, EndLineIndex);

                        // check to see if it's an overlapping find segment
                        if(bHasOverlap)
                        {
                            // if it is then it has already been added so reassign the start point
                            CurTextRangeStart = FindTextSegments[CurrentFindTextSegmentIndex].StartIndex + FindTextSegments[CurrentFindTextSegmentIndex].Length;
                            ++CurrentFindTextSegmentIndex;
                            bHasOverlap = false;
                        }

                        while(CurrentFindTextSegmentIndex < FindTextSegments.Count && CurTextRangeStart < EndLineIndex && FindTextSegments[CurrentFindTextSegmentIndex].StartIndex >= CurTextRangeStart
                            && FindTextSegments[CurrentFindTextSegmentIndex].StartIndex < CurTextRangeEnd)
                        {
                            if(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex > CurTextRangeStart)
                            {
                                Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex - CurTextRangeStart, EndLineIndex - CurTextRangeStart))));
                            }

                            if(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex < EndLineIndex)
                            {
                                Segments.Add(new ColoredDocumentLineSegment(InsertionColors, this.ToString(FindTextSegments[CurrentFindTextSegmentIndex].StartIndex, Math.Min(FindTextSegments[CurrentFindTextSegmentIndex].Length, EndLineIndex - FindTextSegments[CurrentFindTextSegmentIndex].StartIndex))));
                            }

                            CurTextRangeStart = FindTextSegments[CurrentFindTextSegmentIndex].StartIndex + FindTextSegments[CurrentFindTextSegmentIndex].Length;

                            // if the beginning of the next segment is within the current text range move onto the next "find" segment
                            // if not that means the current "find" text segment overlaps into the next regular text segment
                            if(CurTextRangeStart < CurTextRangeEnd)
                            {
                                ++CurrentFindTextSegmentIndex;
                            }
                            else
                            {
                                bHasOverlap = true;
                            }
                        }

                        // if there was text remaining in the current text range after the last segment then add it now
                        if(CurTextRangeStart < CurTextRangeEnd && CurTextRangeStart < EndLineIndex)
                        {
                            Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(CurTextRangeEnd - CurTextRangeStart, EndLineIndex - CurTextRangeStart))));
                        }
                    }
                    else
                    {
                        Segments.Add(new ColoredDocumentLineSegment(new ColorPair(TextRange.BackColor, TextRange.ForeColor), this.ToString(CurTextRangeStart, Math.Min(TextRange.Length, EndLineIndex - CurTextRangeStart))));
                    }
                }

                return Segments.ToArray();
            }