1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
23 // Peter Bartok pbartok@novell.com
29 // There's still plenty of things missing, I've got most of it planned, just hadn't had
30 // the time to write it all yet.
31 // Stuff missing (in no particular order):
32 // - Align text after RecalculateLine
33 // - Implement tag types for hotlinks, images, etc.
34 // - Implement CaretPgUp/PgDown
37 // selection_start.pos and selection_end.pos are 0-based
38 // selection_start.pos = first selected char
39 // selection_end.pos = first NOT-selected char
41 // FormatText methods are 1-based (as are all tags, LineTag.Start is 1 for
42 // the first character on a line; the reason is that 0 is the position
43 // *before* the first character on a line
49 using System
.Collections
;
51 using System
.Drawing
.Text
;
54 namespace System
.Windows
.Forms
{
55 internal enum LineColor
{
60 internal enum CaretSelection
{
61 Position
, // Selection=Caret
62 Word
, // Selection=Word under caret
63 Line
// Selection=Line under caret
66 internal class FontDefinition
{
69 internal FontStyle add_style
;
70 internal FontStyle remove_style
;
72 internal Font font_obj
;
74 internal FontDefinition() {
81 internal enum CaretDirection
{
82 CharForward
, // Move a char to the right
83 CharBack
, // Move a char to the left
84 LineUp
, // Move a line up
85 LineDown
, // Move a line down
86 Home
, // Move to the beginning of the line
87 End
, // Move to the end of the line
88 PgUp
, // Move one page up
89 PgDn
, // Move one page down
90 CtrlPgUp
, // Move caret to the first visible char in the viewport
91 CtrlPgDn
, // Move caret to the last visible char in the viewport
92 CtrlHome
, // Move to the beginning of the document
93 CtrlEnd
, // Move to the end of the document
94 WordBack
, // Move to the beginning of the previous word (or beginning of line)
95 WordForward
, // Move to the beginning of the next word (or end of line)
96 SelectionStart
, // Move to the beginning of the current selection
97 SelectionEnd
, // Move to the end of the current selection
98 CharForwardNoWrap
, // Move a char forward, but don't wrap onto the next line
99 CharBackNoWrap
// Move a char backward, but don't wrap onto the previous line
102 // Being cloneable should allow for nice line and document copies...
103 internal class Line
: ICloneable
, IComparable
{
104 #region Local Variables
105 // Stuff that matters for our line
106 internal StringBuilder text
; // Characters for the line
107 internal float[] widths
; // Width of each character; always one larger than text.Length
108 internal int space
; // Number of elements in text and widths
109 internal int line_no
; // Line number
110 internal LineTag tags
; // Tags describing the text
111 internal int Y
; // Baseline
112 internal int height
; // Height of the line (height of tallest tag)
113 internal int ascent
; // Ascent of the line (ascent of the tallest tag)
114 internal HorizontalAlignment alignment
; // Alignment of the line
115 internal int align_shift
; // Pixel shift caused by the alignment
116 internal bool soft_break
; // Tag is 'broken soft' and continuation from previous line
117 internal int indent
; // Left indent for the first line
118 internal int hanging_indent
; // Hanging indent (left indent for all but the first line)
119 internal int right_indent
; // Right indent for all lines
120 internal bool carriage_return
;
123 // Stuff that's important for the tree
124 internal Line parent
; // Our parent line
125 internal Line left
; // Line with smaller line number
126 internal Line right
; // Line with higher line number
127 internal LineColor color
; // We're doing a black/red tree. this is the node color
128 internal int DEFAULT_TEXT_LEN
; //
129 internal static StringFormat string_format
; // For calculating widths/heights
130 internal bool recalc
; // Line changed
131 #endregion // Local Variables
135 color
= LineColor
.Red
;
142 alignment
= HorizontalAlignment
.Left
;
144 if (string_format
== null) {
145 string_format
= new StringFormat(StringFormat
.GenericTypographic
);
146 string_format
.Trimming
= StringTrimming
.None
;
147 string_format
.FormatFlags
= StringFormatFlags
.MeasureTrailingSpaces
;
151 internal Line(int LineNo
, string Text
, Font font
, Brush color
) : this() {
152 space
= Text
.Length
> DEFAULT_TEXT_LEN
? Text
.Length
+1 : DEFAULT_TEXT_LEN
;
154 text
= new StringBuilder(Text
, space
);
157 widths
= new float[space
+ 1];
158 tags
= new LineTag(this, 1, text
.Length
);
163 internal Line(int LineNo
, string Text
, HorizontalAlignment align
, Font font
, Brush color
) : this() {
164 space
= Text
.Length
> DEFAULT_TEXT_LEN
? Text
.Length
+1 : DEFAULT_TEXT_LEN
;
166 text
= new StringBuilder(Text
, space
);
170 widths
= new float[space
+ 1];
171 tags
= new LineTag(this, 1, text
.Length
);
176 internal Line(int LineNo
, string Text
, LineTag tag
) : this() {
177 space
= Text
.Length
> DEFAULT_TEXT_LEN
? Text
.Length
+1 : DEFAULT_TEXT_LEN
;
179 text
= new StringBuilder(Text
, space
);
182 widths
= new float[space
+ 1];
186 #endregion // Constructors
188 #region Internal Properties
189 internal int Indent
{
200 internal int HangingIndent
{
202 return hanging_indent
;
206 hanging_indent
= value;
211 internal int RightIndent
{
217 right_indent
= value;
223 internal int Height
{
233 internal int LineNo
{
243 internal string Text
{
245 return text
.ToString();
249 text
= new StringBuilder(value, value.Length
> DEFAULT_TEXT_LEN
? value.Length
: DEFAULT_TEXT_LEN
);
253 internal HorizontalAlignment Alignment
{
259 if (alignment
!= value) {
266 internal StringBuilder Text
{
276 #endregion // Internal Properties
278 #region Internal Methods
279 // Make sure we always have enoughs space in text and widths
280 internal void Grow(int minimum
) {
284 length
= text
.Length
;
286 if ((length
+ minimum
) > space
) {
287 // We need to grow; double the size
289 if ((length
+ minimum
) > (space
* 2)) {
290 new_widths
= new float[length
+ minimum
* 2 + 1];
291 space
= length
+ minimum
* 2;
293 new_widths
= new float[space
* 2 + 1];
296 widths
.CopyTo(new_widths
, 0);
302 internal void Streamline(int lines
) {
309 // Catch what the loop below wont; eliminate 0 length
310 // tags, but only if there are other tags after us
311 while ((current
.length
== 0) && (next
!= null)) {
313 tags
.previous
= null;
322 while (next
!= null) {
323 // Take out 0 length tags unless it's the last tag in the document
324 if (next
.length
== 0) {
325 if ((next
.next
!= null) || (line_no
!= lines
)) {
326 current
.next
= next
.next
;
327 if (current
.next
!= null) {
328 current
.next
.previous
= current
;
334 if (current
.Combine(next
)) {
339 current
= current
.next
;
344 /// <summary> Find the tag on a line based on the character position, pos is 0-based</summary>
345 internal LineTag
FindTag(int pos
) {
354 if (pos
>= text
.Length
) {
355 pos
= text
.Length
- 1;
358 while (tag
!= null) {
359 if (((tag
.start
- 1) <= pos
) && (pos
< (tag
.start
+ tag
.length
- 1))) {
360 return LineTag
.GetFinalTag (tag
);
368 /// Recalculate a single line using the same char for every character in the line
371 internal bool RecalculatePasswordLine(Graphics g
, Document doc
) {
380 len
= this.text
.Length
;
390 w
= g
.MeasureString(doc
.password_char
, tags
.font
, 10000, string_format
).Width
;
392 if (this.height
!= (int)tag
.font
.Height
) {
398 this.height
= (int)tag
.font
.Height
;
399 tag
.height
= this.height
;
401 XplatUI
.GetFontMetrics(g
, tag
.font
, out tag
.ascent
, out descent
);
402 this.ascent
= tag
.ascent
;
407 widths
[pos
] = widths
[pos
-1] + w
;
414 /// Go through all tags on a line and recalculate all size-related values;
415 /// returns true if lineheight changed
417 internal bool RecalculateLine(Graphics g
, Document doc
) {
431 len
= this.text
.Length
;
433 prev_height
= this.height
; // For drawing optimization calculations
434 this.height
= 0; // Reset line height
435 this.ascent
= 0; // Reset the ascent for the line
439 if (this.soft_break
) {
440 widths
[0] = hanging_indent
;
453 size
= g
.MeasureString(this.text
.ToString(pos
, 1), tag
.font
, 10000, string_format
);
455 while (tag
.length
== 0) { // We should always have tags after a tag.length==0 unless len==0
458 if (tag
.previous
!= null) {
459 tag
.X
= tag
.previous
.X
;
461 tag
.X
= (int)widths
[pos
];
470 if (Char
.IsWhiteSpace(text
[pos
])) {
472 wrap_width
= tag
.width
+ w
;
476 if ((wrap_pos
> 0) && (wrap_pos
!= len
) && (widths
[pos
] + w
) + 5 > (doc
.viewport_width
- this.right_indent
)) {
478 tag
.width
= wrap_width
;
479 doc
.Split(this, tag
, pos
, this.soft_break
);
480 this.soft_break
= true;
481 len
= this.text
.Length
;
484 } else if (pos
> 1 && (widths
[pos
] + w
) > (doc
.viewport_width
- this.right_indent
)) {
485 // No suitable wrap position was found so break right in the middle of a word
486 tag
.width
= tag
.width
+ w
;
487 doc
.Split(this, tag
, pos
, this.soft_break
);
488 this.soft_break
= true;
489 len
= this.text
.Length
;
495 // Contract all soft lines that follow back into our line
501 widths
[pos
] = widths
[pos
-1] + w
;
504 line
= doc
.GetLine(this.line_no
+ 1);
505 if ((line
!= null) && soft_break
) {
506 // Pull the two lines together
507 doc
.Combine(this.line_no
, this.line_no
+ 1);
508 len
= this.text
.Length
;
514 if (pos
== (tag
.start
-1 + tag
.length
)) {
515 // We just found the end of our current tag
516 tag
.height
= (int)tag
.font
.Height
;
518 // Check if we're the tallest on the line (so far)
519 if (tag
.height
> this.height
) {
520 this.height
= tag
.height
; // Yep; make sure the line knows
523 if (tag
.ascent
== 0) {
526 XplatUI
.GetFontMetrics(g
, tag
.font
, out tag
.ascent
, out descent
);
529 if (tag
.ascent
> this.ascent
) {
532 // We have a tag that has a taller ascent than the line;
536 t
.shift
= tag
.ascent
- t
.ascent
;
541 this.ascent
= tag
.ascent
;
543 tag
.shift
= this.ascent
- tag
.ascent
;
546 // Update our horizontal starting pixel position
547 if (tag
.previous
== null) {
548 tag
.X
= (int)widths
[0];
550 tag
.X
= tag
.previous
.X
+ (int)tag
.previous
.width
;
558 wrap_width
= tag
.width
;
563 if (this.height
== 0) {
564 this.height
= tags
.font
.Height
;
565 tag
.height
= this.height
;
568 if (prev_height
!= this.height
) {
573 #endregion // Internal Methods
575 #region Administrative
576 public int CompareTo(object obj
) {
581 if (! (obj
is Line
)) {
582 throw new ArgumentException("Object is not of type Line", "obj");
585 if (line_no
< ((Line
)obj
).line_no
) {
587 } else if (line_no
> ((Line
)obj
).line_no
) {
594 public object Clone() {
602 clone
.left
= (Line
)left
.Clone();
606 clone
.left
= (Line
)left
.Clone();
612 internal object CloneLine() {
622 public override bool Equals(object obj
) {
627 if (!(obj
is Line
)) {
635 if (line_no
== ((Line
)obj
).line_no
) {
642 public override int GetHashCode() {
643 return base.GetHashCode ();
646 public override string ToString() {
647 return "Line " + line_no
;
650 #endregion // Administrative
653 internal class Document
: ICloneable
, IEnumerable
{
655 // FIXME - go through code and check for places where
656 // we do explicit comparisons instead of using the compare overloads
657 internal struct Marker
{
659 internal LineTag tag
;
663 public static bool operator<(Marker lhs
, Marker rhs
) {
664 if (lhs
.line
.line_no
< rhs
.line
.line_no
) {
668 if (lhs
.line
.line_no
== rhs
.line
.line_no
) {
669 if (lhs
.pos
< rhs
.pos
) {
676 public static bool operator>(Marker lhs
, Marker rhs
) {
677 if (lhs
.line
.line_no
> rhs
.line
.line_no
) {
681 if (lhs
.line
.line_no
== rhs
.line
.line_no
) {
682 if (lhs
.pos
> rhs
.pos
) {
689 public static bool operator==(Marker lhs
, Marker rhs
) {
690 if ((lhs
.line
.line_no
== rhs
.line
.line_no
) && (lhs
.pos
== rhs
.pos
)) {
696 public static bool operator!=(Marker lhs
, Marker rhs
) {
697 if ((lhs
.line
.line_no
!= rhs
.line
.line_no
) || (lhs
.pos
!= rhs
.pos
)) {
703 public void Combine(Line move_to_line
, int move_to_line_length
) {
705 pos
+= move_to_line_length
;
706 tag
= LineTag
.FindTag(line
, pos
);
709 // This is for future use, right now Document.Split does it by hand, with some added shortcut logic
710 public void Split(Line move_to_line
, int split_at
) {
713 tag
= LineTag
.FindTag(line
, pos
);
716 public override bool Equals(object obj
) {
717 return this==(Marker
)obj
;
720 public override int GetHashCode() {
721 return base.GetHashCode ();
724 public override string ToString() {
725 return "Marker Line " + line
+ ", Position " + pos
;
729 #endregion Structures
731 #region Local Variables
732 private Line document
;
734 private Line sentinel
;
735 private int document_id
;
736 private Random random
= new Random();
737 internal string password_char
;
738 private StringBuilder password_cache
;
739 private bool calc_pass
;
740 private int char_count
;
742 private bool no_recalc
;
743 private bool recalc_pending
;
744 private int recalc_start
;
745 private int recalc_end
;
746 private bool recalc_optimize
;
748 internal bool multiline
;
751 internal UndoClass undo
;
753 internal Marker caret
;
754 internal Marker selection_start
;
755 internal Marker selection_end
;
756 internal bool selection_visible
;
757 internal Marker selection_anchor
;
758 internal Marker selection_prev
;
759 internal bool selection_end_anchor
;
761 internal int viewport_x
;
762 internal int viewport_y
; // The visible area of the document
763 internal int viewport_width
;
764 internal int viewport_height
;
766 internal int document_x
; // Width of the document
767 internal int document_y
; // Height of the document
769 internal Rectangle invalid
;
771 internal int crlf_size
; // 1 or 2, depending on whether we use \r\n or just \n
773 internal TextBoxBase owner
; // Who's owning us?
774 static internal int caret_width
= 1;
775 static internal int caret_shift
= 1;
776 #endregion // Local Variables
779 internal Document(TextBoxBase owner
) {
788 recalc_pending
= false;
790 // Tree related stuff
791 sentinel
= new Line();
792 sentinel
.color
= LineColor
.Black
;
796 // We always have a blank line
797 owner
.HandleCreated
+= new EventHandler(owner_HandleCreated
);
798 owner
.VisibleChanged
+= new EventHandler(owner_VisibleChanged
);
800 Add(1, "", owner
.Font
, ThemeEngine
.Current
.ResPool
.GetSolidBrush(owner
.ForeColor
));
801 Line l
= GetLine (1);
804 undo
= new UndoClass(this);
806 selection_visible
= false;
807 selection_start
.line
= this.document
;
808 selection_start
.pos
= 0;
809 selection_start
.tag
= selection_start
.line
.tags
;
810 selection_end
.line
= this.document
;
811 selection_end
.pos
= 0;
812 selection_end
.tag
= selection_end
.line
.tags
;
813 selection_anchor
.line
= this.document
;
814 selection_anchor
.pos
= 0;
815 selection_anchor
.tag
= selection_anchor
.line
.tags
;
816 caret
.line
= this.document
;
818 caret
.tag
= caret
.line
.tags
;
825 // Default selection is empty
827 document_id
= random
.Next();
831 #region Internal Properties
848 internal Line CaretLine
{
854 internal int CaretPosition
{
860 internal Point Caret
{
862 return new Point((int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
, caret
.line
.Y
);
866 internal LineTag CaretTag
{
876 internal int CRLFSize
{
886 internal string PasswordChar
{
888 return password_char
;
892 password_char
= value;
893 if ((password_char
.Length
!= 0) && (password_char
[0] != '\0')) {
898 password_cache
= new StringBuilder(1024);
899 for (int i
= 0; i
< 1024; i
++) {
900 password_cache
.Append(ch
);
904 password_cache
= null;
909 internal int ViewPortX
{
919 internal int Length
{
921 return char_count
+ lines
- 1; // Add \n for each line but the last
925 private int CharCount
{
933 if (LengthChanged
!= null) {
934 LengthChanged(this, EventArgs
.Empty
);
939 ///<summary>Setting NoRecalc to true will prevent the document from being recalculated.
940 ///This ensures that coordinates of added text are predictable after adding the text even with wrapped view</summary>
941 internal bool NoRecalc
{
948 if (!no_recalc
&& recalc_pending
) {
949 RecalculateDocument(owner
.CreateGraphicsInternal(), recalc_start
, recalc_end
, recalc_optimize
);
950 recalc_pending
= false;
955 internal int ViewPortY
{
965 internal int ViewPortWidth
{
967 return viewport_width
;
971 viewport_width
= value;
975 internal int ViewPortHeight
{
977 return viewport_height
;
981 viewport_height
= value;
988 return this.document_x
;
992 internal int Height
{
994 return this.document_y
;
998 internal bool SelectionVisible
{
1000 return selection_visible
;
1004 internal bool Wrap
{
1014 #endregion // Internal Properties
1016 #region Private Methods
1018 internal int DumpTree(Line line
, bool with_tags
) {
1023 Console
.Write("Line {0} [# {1}], Y: {2}, soft: {3}, Text: '{4}'",
1024 line
.line_no
, line
.GetHashCode(), line
.Y
, line
.soft_break
,
1025 line
.text
!= null ? line
.text
.ToString() : "undefined");
1027 if (line
.left
== sentinel
) {
1028 Console
.Write(", left = sentinel");
1029 } else if (line
.left
== null) {
1030 Console
.Write(", left = NULL");
1033 if (line
.right
== sentinel
) {
1034 Console
.Write(", right = sentinel");
1035 } else if (line
.right
== null) {
1036 Console
.Write(", right = NULL");
1039 Console
.WriteLine("");
1049 Console
.Write(" Tags: ");
1050 while (tag
!= null) {
1051 Console
.Write("{0} <{1}>-<{2}> ", count
++, tag
.start
, tag
.length
);
1052 length
+= tag
.length
;
1054 if (tag
.line
!= line
) {
1055 Console
.Write("BAD line link");
1056 throw new Exception("Bad line link in tree");
1060 Console
.Write(", ");
1063 if (length
> line
.text
.Length
) {
1064 throw new Exception(String
.Format("Length of tags more than length of text on line (expected {0} calculated {1})", line
.text
.Length
, length
));
1065 } else if (length
< line
.text
.Length
) {
1066 throw new Exception(String
.Format("Length of tags less than length of text on line (expected {0} calculated {1})", line
.text
.Length
, length
));
1068 Console
.WriteLine("");
1070 if (line
.left
!= null) {
1071 if (line
.left
!= sentinel
) {
1072 total
+= DumpTree(line
.left
, with_tags
);
1075 if (line
!= sentinel
) {
1076 throw new Exception("Left should not be NULL");
1080 if (line
.right
!= null) {
1081 if (line
.right
!= sentinel
) {
1082 total
+= DumpTree(line
.right
, with_tags
);
1085 if (line
!= sentinel
) {
1086 throw new Exception("Right should not be NULL");
1090 for (int i
= 1; i
<= this.lines
; i
++) {
1091 if (GetLine(i
) == null) {
1092 throw new Exception(String
.Format("Hole in line order, missing {0}", i
));
1096 if (line
== this.Root
) {
1097 if (total
< this.lines
) {
1098 throw new Exception(String
.Format("Not enough nodes in tree, found {0}, expected {1}", total
, this.lines
));
1099 } else if (total
> this.lines
) {
1100 throw new Exception(String
.Format("Too many nodes in tree, found {0}, expected {1}", total
, this.lines
));
1107 private void SetSelectionVisible (bool value)
1109 selection_visible
= value;
1111 // cursor and selection are enemies, we can't have both in the same room at the same time
1112 if (owner
.IsHandleCreated
)
1113 XplatUI
.CaretVisible (owner
.Handle
, !selection_visible
);
1116 private void DecrementLines(int line_no
) {
1120 while (current
<= lines
) {
1121 GetLine(current
).line_no
--;
1127 private void IncrementLines(int line_no
) {
1130 current
= this.lines
;
1131 while (current
>= line_no
) {
1132 GetLine(current
).line_no
++;
1138 private void RebalanceAfterAdd(Line line1
) {
1141 while ((line1
!= document
) && (line1
.parent
.color
== LineColor
.Red
)) {
1142 if (line1
.parent
== line1
.parent
.parent
.left
) {
1143 line2
= line1
.parent
.parent
.right
;
1145 if ((line2
!= null) && (line2
.color
== LineColor
.Red
)) {
1146 line1
.parent
.color
= LineColor
.Black
;
1147 line2
.color
= LineColor
.Black
;
1148 line1
.parent
.parent
.color
= LineColor
.Red
;
1149 line1
= line1
.parent
.parent
;
1151 if (line1
== line1
.parent
.right
) {
1152 line1
= line1
.parent
;
1156 line1
.parent
.color
= LineColor
.Black
;
1157 line1
.parent
.parent
.color
= LineColor
.Red
;
1159 RotateRight(line1
.parent
.parent
);
1162 line2
= line1
.parent
.parent
.left
;
1164 if ((line2
!= null) && (line2
.color
== LineColor
.Red
)) {
1165 line1
.parent
.color
= LineColor
.Black
;
1166 line2
.color
= LineColor
.Black
;
1167 line1
.parent
.parent
.color
= LineColor
.Red
;
1168 line1
= line1
.parent
.parent
;
1170 if (line1
== line1
.parent
.left
) {
1171 line1
= line1
.parent
;
1175 line1
.parent
.color
= LineColor
.Black
;
1176 line1
.parent
.parent
.color
= LineColor
.Red
;
1177 RotateLeft(line1
.parent
.parent
);
1181 document
.color
= LineColor
.Black
;
1184 private void RebalanceAfterDelete(Line line1
) {
1187 while ((line1
!= document
) && (line1
.color
== LineColor
.Black
)) {
1188 if (line1
== line1
.parent
.left
) {
1189 line2
= line1
.parent
.right
;
1190 if (line2
.color
== LineColor
.Red
) {
1191 line2
.color
= LineColor
.Black
;
1192 line1
.parent
.color
= LineColor
.Red
;
1193 RotateLeft(line1
.parent
);
1194 line2
= line1
.parent
.right
;
1196 if ((line2
.left
.color
== LineColor
.Black
) && (line2
.right
.color
== LineColor
.Black
)) {
1197 line2
.color
= LineColor
.Red
;
1198 line1
= line1
.parent
;
1200 if (line2
.right
.color
== LineColor
.Black
) {
1201 line2
.left
.color
= LineColor
.Black
;
1202 line2
.color
= LineColor
.Red
;
1204 line2
= line1
.parent
.right
;
1206 line2
.color
= line1
.parent
.color
;
1207 line1
.parent
.color
= LineColor
.Black
;
1208 line2
.right
.color
= LineColor
.Black
;
1209 RotateLeft(line1
.parent
);
1213 line2
= line1
.parent
.left
;
1214 if (line2
.color
== LineColor
.Red
) {
1215 line2
.color
= LineColor
.Black
;
1216 line1
.parent
.color
= LineColor
.Red
;
1217 RotateRight(line1
.parent
);
1218 line2
= line1
.parent
.left
;
1220 if ((line2
.right
.color
== LineColor
.Black
) && (line2
.left
.color
== LineColor
.Black
)) {
1221 line2
.color
= LineColor
.Red
;
1222 line1
= line1
.parent
;
1224 if (line2
.left
.color
== LineColor
.Black
) {
1225 line2
.right
.color
= LineColor
.Black
;
1226 line2
.color
= LineColor
.Red
;
1228 line2
= line1
.parent
.left
;
1230 line2
.color
= line1
.parent
.color
;
1231 line1
.parent
.color
= LineColor
.Black
;
1232 line2
.left
.color
= LineColor
.Black
;
1233 RotateRight(line1
.parent
);
1238 line1
.color
= LineColor
.Black
;
1241 private void RotateLeft(Line line1
) {
1242 Line line2
= line1
.right
;
1244 line1
.right
= line2
.left
;
1246 if (line2
.left
!= sentinel
) {
1247 line2
.left
.parent
= line1
;
1250 if (line2
!= sentinel
) {
1251 line2
.parent
= line1
.parent
;
1254 if (line1
.parent
!= null) {
1255 if (line1
== line1
.parent
.left
) {
1256 line1
.parent
.left
= line2
;
1258 line1
.parent
.right
= line2
;
1265 if (line1
!= sentinel
) {
1266 line1
.parent
= line2
;
1270 private void RotateRight(Line line1
) {
1271 Line line2
= line1
.left
;
1273 line1
.left
= line2
.right
;
1275 if (line2
.right
!= sentinel
) {
1276 line2
.right
.parent
= line1
;
1279 if (line2
!= sentinel
) {
1280 line2
.parent
= line1
.parent
;
1283 if (line1
.parent
!= null) {
1284 if (line1
== line1
.parent
.right
) {
1285 line1
.parent
.right
= line2
;
1287 line1
.parent
.left
= line2
;
1293 line2
.right
= line1
;
1294 if (line1
!= sentinel
) {
1295 line1
.parent
= line2
;
1300 internal void UpdateView(Line line
, int pos
) {
1301 if (!owner
.IsHandleCreated
) {
1306 recalc_start
= line
.line_no
;
1307 recalc_end
= line
.line_no
;
1308 recalc_optimize
= true;
1309 recalc_pending
= true;
1313 // Optimize invalidation based on Line alignment
1314 if (RecalculateDocument(owner
.CreateGraphicsInternal(), line
.line_no
, line
.line_no
, true)) {
1315 // Lineheight changed, invalidate the rest of the document
1316 if ((line
.Y
- viewport_y
) >=0 ) {
1317 // We formatted something that's in view, only draw parts of the screen
1318 owner
.Invalidate(new Rectangle(0, line
.Y
- viewport_y
, viewport_width
, owner
.Height
- line
.Y
- viewport_y
));
1320 // The tag was above the visible area, draw everything
1324 switch(line
.alignment
) {
1325 case HorizontalAlignment
.Left
: {
1326 owner
.Invalidate(new Rectangle((int)line
.widths
[pos
] - viewport_x
- 1, line
.Y
- viewport_y
, viewport_width
, line
.height
+ 1));
1330 case HorizontalAlignment
.Center
: {
1331 owner
.Invalidate(new Rectangle(0, line
.Y
- viewport_y
, viewport_width
, line
.height
+ 1));
1335 case HorizontalAlignment
.Right
: {
1336 owner
.Invalidate(new Rectangle(0, line
.Y
- viewport_y
, (int)line
.widths
[pos
+ 1] - viewport_x
+ line
.align_shift
, line
.height
+ 1));
1344 // Update display from line, down line_count lines; pos is unused, but required for the signature
1345 internal void UpdateView(Line line
, int line_count
, int pos
) {
1346 if (!owner
.IsHandleCreated
) {
1351 recalc_start
= line
.line_no
;
1352 recalc_end
= line
.line_no
+ line_count
- 1;
1353 recalc_optimize
= true;
1354 recalc_pending
= true;
1358 if (RecalculateDocument(owner
.CreateGraphicsInternal(), line
.line_no
, line
.line_no
+ line_count
- 1, true)) {
1359 // Lineheight changed, invalidate the rest of the document
1360 if ((line
.Y
- viewport_y
) >=0 ) {
1361 // We formatted something that's in view, only draw parts of the screen
1362 //blah Console.WriteLine("TextControl.cs(981) Invalidate called in UpdateView(line, line_count, pos)");
1363 owner
.Invalidate(new Rectangle(0, line
.Y
- viewport_y
, viewport_width
, owner
.Height
- line
.Y
- viewport_y
));
1365 // The tag was above the visible area, draw everything
1366 //blah Console.WriteLine("TextControl.cs(985) Invalidate called in UpdateView(line, line_count, pos)");
1372 end_line
= GetLine(line
.line_no
+ line_count
-1);
1373 if (end_line
== null) {
1377 //blah Console.WriteLine("TextControl.cs(996) Invalidate called in UpdateView(line, line_count, pos)");
1378 owner
.Invalidate(new Rectangle(0 - viewport_x
, line
.Y
- viewport_y
, (int)line
.widths
[line
.text
.Length
], end_line
.Y
+ end_line
.height
));
1381 #endregion // Private Methods
1383 #region Internal Methods
1384 // Clear the document and reset state
1385 internal void Empty() {
1387 document
= sentinel
;
1390 // We always have a blank line
1391 Add(1, "", owner
.Font
, ThemeEngine
.Current
.ResPool
.GetSolidBrush(owner
.ForeColor
));
1392 Line l
= GetLine (1);
1393 l
.soft_break
= true;
1395 this.RecalculateDocument(owner
.CreateGraphicsInternal());
1396 PositionCaret(0, 0);
1398 SetSelectionVisible (false);
1400 selection_start
.line
= this.document
;
1401 selection_start
.pos
= 0;
1402 selection_start
.tag
= selection_start
.line
.tags
;
1403 selection_end
.line
= this.document
;
1404 selection_end
.pos
= 0;
1405 selection_end
.tag
= selection_end
.line
.tags
;
1414 if (owner
.IsHandleCreated
)
1415 owner
.Invalidate ();
1418 internal void PositionCaret(Line line
, int pos
) {
1419 if (owner
.IsHandleCreated
) {
1420 undo
.RecordCursor();
1423 caret
.tag
= line
.FindTag(pos
);
1426 caret
.height
= caret
.tag
.height
;
1428 if (owner
.IsHandleCreated
) {
1429 if (owner
.Focused
) {
1430 XplatUI
.SetCaretPos(owner
.Handle
, (int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
- viewport_x
, caret
.line
.Y
+ caret
.tag
.shift
- viewport_y
+ caret_shift
);
1433 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1439 internal void PositionCaret(int x
, int y
) {
1440 if (!owner
.IsHandleCreated
) {
1444 undo
.RecordCursor();
1446 caret
.tag
= FindCursor(x
, y
, out caret
.pos
);
1447 caret
.line
= caret
.tag
.line
;
1448 caret
.height
= caret
.tag
.height
;
1450 if (owner
.Focused
) {
1451 XplatUI
.SetCaretPos(owner
.Handle
, (int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
- viewport_x
, caret
.line
.Y
+ caret
.tag
.shift
- viewport_y
+ caret_shift
);
1454 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1457 internal void CaretHasFocus() {
1458 if ((caret
.tag
!= null) && owner
.IsHandleCreated
) {
1459 XplatUI
.CreateCaret(owner
.Handle
, caret_width
, caret
.height
);
1460 XplatUI
.SetCaretPos(owner
.Handle
, (int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
- viewport_x
, caret
.line
.Y
+ caret
.tag
.shift
- viewport_y
+ caret_shift
);
1466 internal void CaretLostFocus() {
1467 if (!owner
.IsHandleCreated
) {
1470 XplatUI
.DestroyCaret(owner
.Handle
);
1473 internal void AlignCaret() {
1474 if (!owner
.IsHandleCreated
) {
1478 undo
.RecordCursor();
1480 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1481 caret
.height
= caret
.tag
.height
;
1483 if (owner
.Focused
) {
1484 XplatUI
.CreateCaret(owner
.Handle
, caret_width
, caret
.height
);
1485 XplatUI
.SetCaretPos(owner
.Handle
, (int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
- viewport_x
, caret
.line
.Y
+ caret
.tag
.shift
- viewport_y
+ caret_shift
);
1489 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1492 internal void UpdateCaret() {
1493 if (!owner
.IsHandleCreated
|| caret
.tag
== null) {
1497 undo
.RecordCursor();
1499 if (caret
.tag
.height
!= caret
.height
) {
1500 caret
.height
= caret
.tag
.height
;
1501 if (owner
.Focused
) {
1502 XplatUI
.CreateCaret(owner
.Handle
, caret_width
, caret
.height
);
1506 XplatUI
.SetCaretPos(owner
.Handle
, (int)caret
.tag
.line
.widths
[caret
.pos
] + caret
.line
.align_shift
- viewport_x
, caret
.line
.Y
+ caret
.tag
.shift
- viewport_y
+ caret_shift
);
1510 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1513 internal void DisplayCaret() {
1514 if (!owner
.IsHandleCreated
) {
1518 if (owner
.Focused
&& !selection_visible
) {
1519 XplatUI
.CaretVisible(owner
.Handle
, true);
1523 internal void HideCaret() {
1524 if (!owner
.IsHandleCreated
) {
1528 if (owner
.Focused
) {
1529 XplatUI
.CaretVisible(owner
.Handle
, false);
1533 internal void MoveCaret(CaretDirection direction
) {
1534 // FIXME should we use IsWordSeparator to detect whitespace, instead
1535 // of looking for actual spaces in the Word move cases?
1537 bool nowrap
= false;
1539 case CaretDirection
.CharForwardNoWrap
:
1541 goto case CaretDirection
.CharForward
;
1542 case CaretDirection
.CharForward
: {
1544 if (caret
.pos
> caret
.line
.text
.Length
) {
1545 if (multiline
&& !nowrap
) {
1546 // Go into next line
1547 if (caret
.line
.line_no
< this.lines
) {
1548 caret
.line
= GetLine(caret
.line
.line_no
+1);
1550 caret
.tag
= caret
.line
.tags
;
1555 // Single line; we stay where we are
1559 if ((caret
.tag
.start
- 1 + caret
.tag
.length
) < caret
.pos
) {
1560 caret
.tag
= caret
.tag
.next
;
1567 case CaretDirection
.CharBackNoWrap
:
1569 goto case CaretDirection
.CharBack
;
1570 case CaretDirection
.CharBack
: {
1571 if (caret
.pos
> 0) {
1572 // caret.pos--; // folded into the if below
1573 if (--caret
.pos
> 0) {
1574 if (caret
.tag
.start
> caret
.pos
) {
1575 caret
.tag
= caret
.tag
.previous
;
1579 if (caret
.line
.line_no
> 1 && !nowrap
) {
1580 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1581 caret
.pos
= caret
.line
.text
.Length
;
1582 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1589 case CaretDirection
.WordForward
: {
1592 len
= caret
.line
.text
.Length
;
1593 if (caret
.pos
< len
) {
1594 while ((caret
.pos
< len
) && (caret
.line
.text
[caret
.pos
] != ' ')) {
1597 if (caret
.pos
< len
) {
1598 // Skip any whitespace
1599 while ((caret
.pos
< len
) && (caret
.line
.text
[caret
.pos
] == ' ')) {
1603 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1605 if (caret
.line
.line_no
< this.lines
) {
1606 caret
.line
= GetLine(caret
.line
.line_no
+ 1);
1608 caret
.tag
= caret
.line
.tags
;
1615 case CaretDirection
.WordBack
: {
1616 if (caret
.pos
> 0) {
1619 while ((caret
.pos
> 0) && (caret
.line
.text
[caret
.pos
] == ' ')) {
1623 while ((caret
.pos
> 0) && (caret
.line
.text
[caret
.pos
] != ' ')) {
1627 if (caret
.line
.text
.ToString(caret
.pos
, 1) == " ") {
1628 if (caret
.pos
!= 0) {
1631 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1632 caret
.pos
= caret
.line
.text
.Length
;
1635 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1637 if (caret
.line
.line_no
> 1) {
1638 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1639 caret
.pos
= caret
.line
.text
.Length
;
1640 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1647 case CaretDirection
.LineUp
: {
1648 if (caret
.line
.line_no
> 1) {
1651 pixel
= (int)caret
.line
.widths
[caret
.pos
];
1652 PositionCaret(pixel
, GetLine(caret
.line
.line_no
- 1).Y
);
1659 case CaretDirection
.LineDown
: {
1660 if (caret
.line
.line_no
< lines
) {
1663 pixel
= (int)caret
.line
.widths
[caret
.pos
];
1664 PositionCaret(pixel
, GetLine(caret
.line
.line_no
+ 1).Y
);
1671 case CaretDirection
.Home
: {
1672 if (caret
.pos
> 0) {
1674 caret
.tag
= caret
.line
.tags
;
1680 case CaretDirection
.End
: {
1681 if (caret
.pos
< caret
.line
.text
.Length
) {
1682 caret
.pos
= caret
.line
.text
.Length
;
1683 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1689 case CaretDirection
.PgUp
: {
1691 int new_y
, y_offset
;
1693 if (viewport_y
== 0) {
1695 // This should probably be handled elsewhere
1696 if (!(owner
is RichTextBox
)) {
1697 // Page down doesn't do anything in a regular TextBox
1698 // if the bottom of the document
1699 // is already visible, the page and the caret stay still
1703 // We're just placing the caret at the end of the document, no scrolling needed
1704 owner
.vscroll
.Value
= 0;
1705 Line line
= GetLine (1);
1706 PositionCaret (line
, 0);
1709 y_offset
= caret
.line
.Y
- viewport_y
;
1710 new_y
= caret
.line
.Y
- viewport_height
;
1712 owner
.vscroll
.Value
= Math
.Max (new_y
, 0);
1713 PositionCaret ((int)caret
.line
.widths
[caret
.pos
], y_offset
+ viewport_y
);
1717 case CaretDirection
.PgDn
: {
1718 int new_y
, y_offset
;
1720 if ((viewport_y
+ viewport_height
) > document_y
) {
1722 // This should probably be handled elsewhere
1723 if (!(owner
is RichTextBox
)) {
1724 // Page up doesn't do anything in a regular TextBox
1725 // if the bottom of the document
1726 // is already visible, the page and the caret stay still
1730 // We're just placing the caret at the end of the document, no scrolling needed
1731 owner
.vscroll
.Value
= owner
.vscroll
.Maximum
- viewport_height
+ 1;
1732 Line line
= GetLine (lines
);
1733 PositionCaret (line
, line
.Text
.Length
);
1736 y_offset
= caret
.line
.Y
- viewport_y
;
1737 new_y
= caret
.line
.Y
+ viewport_height
;
1739 owner
.vscroll
.Value
= Math
.Min (new_y
, owner
.vscroll
.Maximum
- viewport_height
+ 1);
1740 PositionCaret ((int)caret
.line
.widths
[caret
.pos
], y_offset
+ viewport_y
);
1745 case CaretDirection
.CtrlPgUp
: {
1746 PositionCaret(0, viewport_y
);
1751 case CaretDirection
.CtrlPgDn
: {
1756 tag
= FindTag(0, viewport_y
+ viewport_height
, out index
, false);
1757 if (tag
.line
.line_no
> 1) {
1758 line
= GetLine(tag
.line
.line_no
- 1);
1762 PositionCaret(line
, line
.Text
.Length
);
1767 case CaretDirection
.CtrlHome
: {
1768 caret
.line
= GetLine(1);
1770 caret
.tag
= caret
.line
.tags
;
1776 case CaretDirection
.CtrlEnd
: {
1777 caret
.line
= GetLine(lines
);
1778 caret
.pos
= caret
.line
.text
.Length
;
1779 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1785 case CaretDirection
.SelectionStart
: {
1786 caret
.line
= selection_start
.line
;
1787 caret
.pos
= selection_start
.pos
;
1788 caret
.tag
= selection_start
.tag
;
1794 case CaretDirection
.SelectionEnd
: {
1795 caret
.line
= selection_end
.line
;
1796 caret
.pos
= selection_end
.pos
;
1797 caret
.tag
= selection_end
.tag
;
1805 // Draw the document
1806 internal void Draw(Graphics g
, Rectangle clip
) {
1807 Line line
; // Current line being drawn
1808 LineTag tag
; // Current tag being drawn
1809 int start
; // First line to draw
1810 int end
; // Last line to draw
1811 StringBuilder text
; // String representing the current line
1817 // First, figure out from what line to what line we need to draw
1818 start
= GetLineByPixel(clip
.Top
+ viewport_y
, false).line_no
;
1819 end
= GetLineByPixel(clip
.Bottom
+ viewport_y
, false).line_no
;
1820 //Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
1822 // Now draw our elements; try to only draw those that are visible
1826 DateTime n
= DateTime
.Now
;
1827 Console
.WriteLine("Started drawing: {0}s {1}ms", n
.Second
, n
.Millisecond
);
1830 disabled
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorGrayText
);
1831 hilight
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorHighlight
);
1832 hilight_text
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorHighlightText
);
1834 while (line_no
<= end
) {
1835 line
= GetLine(line_no
);
1837 if (owner
.backcolor_set
|| (owner
.Enabled
&& !owner
.read_only
)) {
1838 g
.FillRectangle(ThemeEngine
.Current
.ResPool
.GetSolidBrush(owner
.BackColor
), new Rectangle(clip
.Left
, line
.Y
- viewport_y
, clip
.Width
, line
.Y
- viewport_y
+ line
.Height
));
1840 g
.FillRectangle(ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorControl
), new Rectangle(clip
.Left
, line
.Y
- viewport_y
, clip
.Width
, line
.Y
- viewport_y
+ line
.Height
));
1849 // This fails if there's a password > 1024 chars...
1850 text
= this.password_cache
;
1852 while (tag
!= null) {
1853 if (tag
.length
== 0) {
1858 if (((tag
.X
+ tag
.width
) > (clip
.Left
- viewport_x
)) || (tag
.X
< (clip
.Right
- viewport_x
))) {
1859 // Check for selection
1860 if ((!selection_visible
) || (!owner
.ShowSelection
) || (line_no
< selection_start
.line
.line_no
) || (line_no
> selection_end
.line
.line_no
)) {
1861 // regular drawing, no selection to deal with
1862 //g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
1863 if (owner
.is_enabled
) {
1864 g
.DrawString(text
.ToString(tag
.start
-1, tag
.length
), tag
.font
, tag
.color
, tag
.X
+ line
.align_shift
- viewport_x
, line
.Y
+ tag
.shift
- viewport_y
, StringFormat
.GenericTypographic
);
1869 a
= ((SolidBrush
)tag
.color
).Color
;
1870 b
= ThemeEngine
.Current
.ColorWindowText
;
1872 if ((a
.R
== b
.R
) && (a
.G
== b
.G
) && (a
.B
== b
.B
)) {
1873 g
.DrawString(text
.ToString(tag
.start
-1, tag
.length
), tag
.font
, disabled
, tag
.X
+ line
.align_shift
- viewport_x
, line
.Y
+ tag
.shift
- viewport_y
, StringFormat
.GenericTypographic
);
1875 g
.DrawString(text
.ToString(tag
.start
-1, tag
.length
), tag
.font
, tag
.color
, tag
.X
+ line
.align_shift
- viewport_x
, line
.Y
+ tag
.shift
- viewport_y
, StringFormat
.GenericTypographic
);
1879 // we might have to draw our selection
1880 if ((line_no
!= selection_start
.line
.line_no
) && (line_no
!= selection_end
.line
.line_no
)) {
1881 // Special case, whole line is selected, draw this tag selected
1884 tag
.X
+ line
.align_shift
- viewport_x
, // X
1885 line
.Y
+ tag
.shift
- viewport_y
, // Y
1886 line
.widths
[tag
.start
+ tag
.length
- 1], // width
1887 tag
.height
// Height
1891 //s.Substring(tag.start-1, tag.length), // String
1892 text
.ToString(tag
.start
-1, tag
.length
), // String
1894 hilight_text
, // Brush
1895 tag
.X
+ line
.align_shift
- viewport_x
, // X
1896 line
.Y
+ tag
.shift
- viewport_y
, // Y
1897 StringFormat
.GenericTypographic
);
1905 // One or more, but not all tags on the line are selected
1906 if ((selection_start
.tag
== tag
) && (selection_end
.tag
== tag
)) {
1907 // Single tag selected, draw "normalSELECTEDnormal"
1909 // First, the regular part
1911 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1912 text
.ToString(tag
.start
- 1, selection_start
.pos
- tag
.start
+ 1), // String
1915 tag
.X
+ line
.align_shift
- viewport_x
, // X
1916 line
.Y
+ tag
.shift
- viewport_y
, // Y
1917 StringFormat
.GenericTypographic
);
1919 // Now the highlight
1922 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1923 line
.Y
+ tag
.shift
- viewport_y
, // Y
1924 line
.widths
[selection_end
.pos
] - line
.widths
[selection_start
.pos
], // Width
1925 tag
.height
); // Height
1928 //s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), // String
1929 text
.ToString(selection_start
.pos
, selection_end
.pos
- selection_start
.pos
), // String
1931 hilight_text
, // Brush
1932 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1933 line
.Y
+ tag
.shift
- viewport_y
, // Y
1934 StringFormat
.GenericTypographic
);
1936 // And back to the regular
1938 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1939 text
.ToString(selection_end
.pos
, tag
.start
+ tag
.length
- selection_end
.pos
- 1), // String
1942 line
.widths
[selection_end
.pos
] + line
.align_shift
- viewport_x
, // X
1943 line
.Y
+ tag
.shift
- viewport_y
, // Y
1944 StringFormat
.GenericTypographic
);
1946 } else if (selection_start
.tag
== tag
) {
1949 // The highlighted part
1952 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
,
1953 line
.Y
+ tag
.shift
- viewport_y
,
1954 line
.widths
[tag
.start
+ tag
.length
- 1] - line
.widths
[selection_start
.pos
],
1958 //s.Substring(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
1959 text
.ToString(selection_start
.pos
, tag
.start
+ tag
.length
- selection_start
.pos
- 1), // String
1961 hilight_text
, // Brush
1962 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1963 line
.Y
+ tag
.shift
- viewport_y
, // Y
1964 StringFormat
.GenericTypographic
);
1968 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1969 text
.ToString(tag
.start
- 1, selection_start
.pos
- tag
.start
+ 1), // String
1972 tag
.X
+ line
.align_shift
- viewport_x
, // X
1973 line
.Y
+ tag
.shift
- viewport_y
, // Y
1974 StringFormat
.GenericTypographic
);
1975 } else if (selection_end
.tag
== tag
) {
1978 // The highlighted part
1981 tag
.X
+ line
.align_shift
- viewport_x
,
1982 line
.Y
+ tag
.shift
- viewport_y
,
1983 line
.widths
[selection_end
.pos
] - line
.widths
[tag
.start
- 1],
1987 //s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), // String
1988 text
.ToString(tag
.start
- 1, selection_end
.pos
- tag
.start
+ 1), // String
1990 hilight_text
, // Brush
1991 tag
.X
+ line
.align_shift
- viewport_x
, // X
1992 line
.Y
+ tag
.shift
- viewport_y
, // Y
1993 StringFormat
.GenericTypographic
);
1997 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1998 text
.ToString(selection_end
.pos
, tag
.start
+ tag
.length
- selection_end
.pos
- 1), // String
2001 line
.widths
[selection_end
.pos
] + line
.align_shift
- viewport_x
, // X
2002 line
.Y
+ tag
.shift
- viewport_y
, // Y
2003 StringFormat
.GenericTypographic
);
2005 // no partially selected tags here, simple checks...
2006 if (selection_start
.line
== line
) {
2010 begin
= tag
.start
- 1;
2011 stop
= tag
.start
+ tag
.length
- 1;
2012 if (selection_end
.line
== line
) {
2013 if ((begin
>= selection_start
.pos
) && (stop
< selection_end
.pos
)) {
2017 if (stop
> selection_start
.pos
) {
2021 } else if (selection_end
.line
== line
) {
2022 if ((tag
.start
- 1) < selection_end
.pos
) {
2032 tag
.X
+ line
.align_shift
- viewport_x
,
2033 line
.Y
+ tag
.shift
- viewport_y
,
2034 line
.widths
[tag
.start
+ tag
.length
- 1] - line
.widths
[tag
.start
- 1],
2038 //s.Substring(tag.start-1, tag.length), // String
2039 text
.ToString(tag
.start
-1, tag
.length
), // String
2041 hilight_text
, // Brush
2042 tag
.X
+ line
.align_shift
- viewport_x
, // X
2043 line
.Y
+ tag
.shift
- viewport_y
, // Y
2044 StringFormat
.GenericTypographic
);
2047 //s.Substring(tag.start-1, tag.length), // String
2048 text
.ToString(tag
.start
-1, tag
.length
), // String
2051 tag
.X
+ line
.align_shift
- viewport_x
, // X
2052 line
.Y
+ tag
.shift
- viewport_y
, // Y
2053 StringFormat
.GenericTypographic
);
2068 Console
.WriteLine("Finished drawing: {0}s {1}ms", n
.Second
, n
.Millisecond
);
2073 private void InsertLineString (Line line
, int pos
, string s
)
2075 bool carriage_return
= false;
2077 if (s
.EndsWith ("\r")) {
2078 s
= s
.Substring (0, s
.Length
- 1);
2079 carriage_return
= true;
2082 InsertString (line
, pos
, s
);
2084 if (carriage_return
) {
2085 Line l
= GetLine (line
.line_no
);
2086 l
.carriage_return
= true;
2090 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
2091 internal void Insert(Line line
, int pos
, bool update_caret
, string s
) {
2096 LineTag tag
= LineTag
.FindTag (line
, pos
);
2099 undo
.BeginCompoundAction ();
2101 base_line
= line
.line_no
;
2102 old_line_count
= lines
;
2104 break_index
= s
.IndexOf ('\n');
2106 // Bump the text at insertion point a line down if we're inserting more than one line
2107 if (break_index
> -1) {
2109 line
.soft_break
= false;
2110 // Remainder of start line is now in base_line + 1
2113 if (break_index
== -1)
2114 break_index
= s
.Length
;
2116 InsertLineString (line
, pos
, s
.Substring (0, break_index
));
2119 while (break_index
< s
.Length
) {
2121 int next_break
= s
.IndexOf ('\n', break_index
);
2122 int adjusted_next_break
;
2123 bool carriage_return
= false;
2125 if (next_break
== -1) {
2126 next_break
= s
.Length
;
2130 adjusted_next_break
= next_break
;
2131 if (s
[next_break
- 1] == '\r') {
2132 adjusted_next_break
--;
2133 carriage_return
= true;
2136 string line_text
= s
.Substring (break_index
, adjusted_next_break
- break_index
);
2137 Add (base_line
+ count
, line_text
, line
.alignment
, tag
.font
, tag
.color
);
2139 if (carriage_return
) {
2140 Line last
= GetLine (base_line
+ count
);
2141 last
.carriage_return
= true;
2144 last
.soft_break
= true;
2146 Line last
= GetLine (base_line
+ count
);
2147 last
.soft_break
= true;
2151 break_index
= next_break
+ 1;
2156 UpdateView(line
, lines
- old_line_count
+ 1, pos
);
2159 // Move caret to the end of the inserted text
2160 Line l
= GetLine (line
.line_no
+ lines
- old_line_count
);
2161 PositionCaret(l
, l
.text
.Length
);
2165 undo
.EndCompoundAction ();
2168 // Inserts a character at the given position
2169 internal void InsertString(Line line
, int pos
, string s
) {
2170 InsertString(line
.FindTag(pos
), pos
, s
);
2173 // Inserts a string at the given position
2174 internal void InsertString(LineTag tag
, int pos
, string s
) {
2183 line
.text
.Insert(pos
, s
);
2186 // TODO: sometimes getting a null tag here when pasting ???
2188 while (tag
!= null) {
2195 UpdateView(line
, pos
);
2198 // Inserts a string at the caret position
2199 internal void InsertStringAtCaret(string s
, bool move_caret
) {
2207 caret
.line
.text
.Insert(caret
.pos
, s
);
2208 caret
.tag
.length
+= len
;
2210 if (caret
.tag
.next
!= null) {
2211 tag
= caret
.tag
.next
;
2212 while (tag
!= null) {
2217 caret
.line
.Grow(len
);
2218 caret
.line
.recalc
= true;
2220 UpdateView(caret
.line
, caret
.pos
);
2229 // Inserts a character at the given position
2230 internal void InsertChar(Line line
, int pos
, char ch
) {
2231 InsertChar(line
.FindTag(pos
), pos
, ch
);
2234 // Inserts a character at the given position
2235 internal void InsertChar(LineTag tag
, int pos
, char ch
) {
2241 line
.text
.Insert(pos
, ch
);
2245 while (tag
!= null) {
2252 UpdateView(line
, pos
);
2255 // Inserts a character at the current caret position
2256 internal void InsertCharAtCaret(char ch
, bool move_caret
) {
2261 caret
.line
.text
.Insert(caret
.pos
, ch
);
2264 if (caret
.tag
.next
!= null) {
2265 tag
= caret
.tag
.next
;
2266 while (tag
!= null) {
2272 caret
.line
.recalc
= true;
2274 UpdateView(caret
.line
, caret
.pos
);
2278 SetSelectionToCaret(true);
2282 internal void DeleteMultiline (Line start_line
, int pos
, int length
)
2284 Marker start
= new Marker ();
2285 Marker end
= new Marker ();
2286 int start_index
= LineTagToCharIndex (start_line
, pos
);
2288 start
.line
= start_line
;
2290 start
.tag
= LineTag
.FindTag (start_line
, pos
);
2292 CharIndexToLineTag (start_index
+ length
, out end
.line
,
2293 out end
.tag
, out end
.pos
);
2295 if (start
.line
== end
.line
) {
2296 DeleteChars (start
.tag
, pos
, end
.pos
- pos
);
2299 // Delete first and last lines
2300 DeleteChars (start
.tag
, start
.pos
, start
.line
.text
.Length
- start
.pos
);
2301 DeleteChars (end
.line
.tags
, 0, end
.pos
);
2303 int current
= start
.line
.line_no
+ 1;
2304 if (current
< end
.line
.line_no
) {
2305 for (int i
= end
.line
.line_no
- 1; i
>= current
; i
--) {
2310 // BIG FAT WARNING - selection_end.line might be stale due
2311 // to the above Delete() call. DONT USE IT before hitting the end of this method!
2313 // Join start and end
2314 Combine (start
.line
.line_no
, current
);
2319 // Deletes n characters at the given position; it will not delete past line limits
2321 internal void DeleteChars(LineTag tag
, int pos
, int count
) {
2330 if (pos
== line
.text
.Length
) {
2334 line
.text
.Remove(pos
, count
);
2336 // Make sure the tag points to the right spot
2337 while ((tag
!= null) && (tag
.start
+ tag
.length
- 1) <= pos
) {
2345 // Check if we're crossing tag boundaries
2346 if ((pos
+ count
) > (tag
.start
+ tag
.length
- 1)) {
2349 // We have to delete cross tag boundaries
2353 left
-= tag
.start
+ tag
.length
- pos
- 1;
2354 tag
.length
-= tag
.start
+ tag
.length
- pos
- 1;
2357 while ((tag
!= null) && (left
> 0)) {
2358 tag
.start
-= count
- left
;
2359 if (tag
.length
> left
) {
2370 // We got off easy, same tag
2372 tag
.length
-= count
;
2374 if (tag
.length
== 0) {
2379 // Delete empty orphaned tags at the end
2381 while (walk
!= null && walk
.next
!= null && walk
.next
.length
== 0) {
2383 walk
.next
= walk
.next
.next
;
2384 if (walk
.next
!= null)
2385 walk
.next
.previous
= t
;
2389 // Adjust the start point of any tags following
2392 while (tag
!= null) {
2400 line
.Streamline(lines
);
2403 UpdateView(line
, pos
);
2406 // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
2407 internal void DeleteChar(LineTag tag
, int pos
, bool forward
) {
2416 if ((pos
== 0 && forward
== false) || (pos
== line
.text
.Length
&& forward
== true)) {
2422 line
.text
.Remove(pos
, 1);
2424 while ((tag
!= null) && (tag
.start
+ tag
.length
- 1) <= pos
) {
2434 if (tag
.length
== 0) {
2439 line
.text
.Remove(pos
, 1);
2440 if (pos
>= (tag
.start
- 1)) {
2442 if (tag
.length
== 0) {
2445 } else if (tag
.previous
!= null) {
2446 tag
.previous
.length
--;
2447 if (tag
.previous
.length
== 0) {
2453 // Delete empty orphaned tags at the end
2455 while (walk
!= null && walk
.next
!= null && walk
.next
.length
== 0) {
2457 walk
.next
= walk
.next
.next
;
2458 if (walk
.next
!= null)
2459 walk
.next
.previous
= t
;
2464 while (tag
!= null) {
2470 line
.Streamline(lines
);
2473 UpdateView(line
, pos
);
2476 // Combine two lines
2477 internal void Combine(int FirstLine
, int SecondLine
) {
2478 Combine(GetLine(FirstLine
), GetLine(SecondLine
));
2481 internal void Combine(Line first
, Line second
) {
2485 // Combine the two tag chains into one
2488 // Maintain the line ending style
2489 first
.soft_break
= second
.soft_break
;
2491 while (last
.next
!= null) {
2495 last
.next
= second
.tags
;
2496 last
.next
.previous
= last
;
2498 shift
= last
.start
+ last
.length
- 1;
2500 // Fix up references within the chain
2502 while (last
!= null) {
2504 last
.start
+= shift
;
2508 // Combine both lines' strings
2509 first
.text
.Insert(first
.text
.Length
, second
.text
.ToString());
2510 first
.Grow(first
.text
.Length
);
2512 // Remove the reference to our (now combined) tags from the doomed line
2516 DecrementLines(first
.line_no
+ 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
2519 first
.recalc
= true;
2520 first
.height
= 0; // This forces RecalcDocument/UpdateView to redraw from this line on
2521 first
.Streamline(lines
);
2523 // Update Caret, Selection, etc
2524 if (caret
.line
== second
) {
2525 caret
.Combine(first
, shift
);
2527 if (selection_anchor
.line
== second
) {
2528 selection_anchor
.Combine(first
, shift
);
2530 if (selection_start
.line
== second
) {
2531 selection_start
.Combine(first
, shift
);
2533 if (selection_end
.line
== second
) {
2534 selection_end
.Combine(first
, shift
);
2541 check_first
= GetLine(first
.line_no
);
2542 check_second
= GetLine(check_first
.line_no
+ 1);
2544 Console
.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first
.Y
, check_second
.Y
);
2547 this.Delete(second
);
2550 check_first
= GetLine(first
.line_no
);
2551 check_second
= GetLine(check_first
.line_no
+ 1);
2553 Console
.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first
.Y
, check_second
.Y
);
2557 // Split the line at the position into two
2558 internal void Split(int LineNo
, int pos
) {
2562 line
= GetLine(LineNo
);
2563 tag
= LineTag
.FindTag(line
, pos
);
2564 Split(line
, tag
, pos
, false);
2567 internal void Split(Line line
, int pos
) {
2570 tag
= LineTag
.FindTag(line
, pos
);
2571 Split(line
, tag
, pos
, false);
2574 ///<summary>Split line at given tag and position into two lines</summary>
2575 ///<param name="soft">True if the split should be marked as 'soft', indicating that it can be contracted
2576 ///if more space becomes available on previous line</param>
2577 internal void Split(Line line
, LineTag tag
, int pos
, bool soft
) {
2581 bool move_sel_start
;
2585 move_sel_start
= false;
2586 move_sel_end
= false;
2588 // Adjust selection and cursors
2589 if (soft
&& (caret
.line
== line
) && (caret
.pos
>= pos
)) {
2592 if (selection_start
.line
== line
&& selection_start
.pos
> pos
) {
2593 move_sel_start
= true;
2596 if (selection_end
.line
== line
&& selection_end
.pos
> pos
) {
2597 move_sel_end
= true;
2600 // cover the easy case first
2601 if (pos
== line
.text
.Length
) {
2602 Add(line
.line_no
+ 1, "", line
.alignment
, tag
.font
, tag
.color
);
2604 new_line
= GetLine(line
.line_no
+ 1);
2606 line
.carriage_return
= false;
2607 new_line
.carriage_return
= line
.carriage_return
;
2611 caret
.line
= new_line
;
2612 caret
.line
.soft_break
= true;
2613 caret
.tag
= new_line
.tags
;
2616 new_line
.soft_break
= true;
2620 if (move_sel_start
) {
2621 selection_start
.line
= new_line
;
2622 selection_start
.pos
= 0;
2623 selection_start
.tag
= new_line
.tags
;
2627 selection_end
.line
= new_line
;
2628 selection_end
.pos
= 0;
2629 selection_end
.tag
= new_line
.tags
;
2634 // We need to move the rest of the text into the new line
2635 Add(line
.line_no
+ 1, line
.text
.ToString(pos
, line
.text
.Length
- pos
), line
.alignment
, tag
.font
, tag
.color
);
2637 // Now transfer our tags from this line to the next
2638 new_line
= GetLine(line
.line_no
+ 1);
2640 line
.carriage_return
= false;
2641 new_line
.carriage_return
= line
.carriage_return
;
2644 new_line
.recalc
= true;
2646 if ((tag
.start
- 1) == pos
) {
2649 // We can simply break the chain and move the tag into the next line
2650 if (tag
== line
.tags
) {
2651 new_tag
= new LineTag(line
, 1, 0);
2652 new_tag
.font
= tag
.font
;
2653 new_tag
.color
= tag
.color
;
2654 line
.tags
= new_tag
;
2657 if (tag
.previous
!= null) {
2658 tag
.previous
.next
= null;
2660 new_line
.tags
= tag
;
2661 tag
.previous
= null;
2662 tag
.line
= new_line
;
2664 // Walk the list and correct the start location of the tags we just bumped into the next line
2665 shift
= tag
.start
- 1;
2668 while (new_tag
!= null) {
2669 new_tag
.start
-= shift
;
2670 new_tag
.line
= new_line
;
2671 new_tag
= new_tag
.next
;
2676 new_tag
= new LineTag(new_line
, 1, tag
.start
- 1 + tag
.length
- pos
);
2677 new_tag
.next
= tag
.next
;
2678 new_tag
.font
= tag
.font
;
2679 new_tag
.color
= tag
.color
;
2680 new_line
.tags
= new_tag
;
2681 if (new_tag
.next
!= null) {
2682 new_tag
.next
.previous
= new_tag
;
2685 tag
.length
= pos
- tag
.start
+ 1;
2688 new_tag
= new_tag
.next
;
2689 while (new_tag
!= null) {
2690 new_tag
.start
-= shift
;
2691 new_tag
.line
= new_line
;
2692 new_tag
= new_tag
.next
;
2699 caret
.line
= new_line
;
2700 caret
.pos
= caret
.pos
- pos
;
2701 caret
.tag
= caret
.line
.FindTag(caret
.pos
);
2703 new_line
.soft_break
= true;
2706 if (move_sel_start
) {
2707 selection_start
.line
= new_line
;
2708 selection_start
.pos
= selection_start
.pos
- pos
;
2709 selection_start
.tag
= new_line
.FindTag(selection_start
.pos
);
2713 selection_end
.line
= new_line
;
2714 selection_end
.pos
= selection_end
.pos
- pos
;
2715 selection_end
.tag
= new_line
.FindTag(selection_end
.pos
);
2718 CharCount
-= line
.text
.Length
- pos
;
2719 line
.text
.Remove(pos
, line
.text
.Length
- pos
);
2722 // Adds a line of text, with given font.
2723 // Bumps any line at that line number that already exists down
2724 internal void Add(int LineNo
, string Text
, Font font
, Brush color
) {
2725 Add(LineNo
, Text
, HorizontalAlignment
.Left
, font
, color
);
2728 internal void Add(int LineNo
, string Text
, HorizontalAlignment align
, Font font
, Brush color
) {
2733 CharCount
+= Text
.Length
;
2735 if (LineNo
<1 || Text
== null) {
2737 throw new ArgumentNullException("LineNo", "Line numbers must be positive");
2739 throw new ArgumentNullException("Text", "Cannot insert NULL line");
2743 add = new Line(LineNo
, Text
, align
, font
, color
);
2746 while (line
!= sentinel
) {
2748 line_no
= line
.line_no
;
2750 if (LineNo
> line_no
) {
2752 } else if (LineNo
< line_no
) {
2755 // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
2756 IncrementLines(line
.line_no
);
2761 add.left
= sentinel
;
2762 add.right
= sentinel
;
2764 if (add.parent
!= null) {
2765 if (LineNo
> add.parent
.line_no
) {
2766 add.parent
.right
= add;
2768 add.parent
.left
= add;
2775 RebalanceAfterAdd(add);
2780 internal virtual void Clear() {
2783 document
= sentinel
;
2786 public virtual object Clone() {
2789 clone
= new Document(null);
2791 clone
.lines
= this.lines
;
2792 clone
.document
= (Line
)document
.Clone();
2797 internal void Delete(int LineNo
) {
2804 line
= GetLine(LineNo
);
2806 CharCount
-= line
.text
.Length
;
2808 DecrementLines(LineNo
+ 1);
2812 internal void Delete(Line line1
) {
2813 Line line2
;// = new Line();
2816 if ((line1
.left
== sentinel
) || (line1
.right
== sentinel
)) {
2819 line3
= line1
.right
;
2820 while (line3
.left
!= sentinel
) {
2825 if (line3
.left
!= sentinel
) {
2828 line2
= line3
.right
;
2831 line2
.parent
= line3
.parent
;
2832 if (line3
.parent
!= null) {
2833 if(line3
== line3
.parent
.left
) {
2834 line3
.parent
.left
= line2
;
2836 line3
.parent
.right
= line2
;
2842 if (line3
!= line1
) {
2845 if (selection_start
.line
== line3
) {
2846 selection_start
.line
= line1
;
2849 if (selection_end
.line
== line3
) {
2850 selection_end
.line
= line1
;
2853 if (selection_anchor
.line
== line3
) {
2854 selection_anchor
.line
= line1
;
2857 if (caret
.line
== line3
) {
2862 line1
.alignment
= line3
.alignment
;
2863 line1
.ascent
= line3
.ascent
;
2864 line1
.hanging_indent
= line3
.hanging_indent
;
2865 line1
.height
= line3
.height
;
2866 line1
.indent
= line3
.indent
;
2867 line1
.line_no
= line3
.line_no
;
2868 line1
.recalc
= line3
.recalc
;
2869 line1
.right_indent
= line3
.right_indent
;
2870 line1
.soft_break
= line3
.soft_break
;
2871 line1
.space
= line3
.space
;
2872 line1
.tags
= line3
.tags
;
2873 line1
.text
= line3
.text
;
2874 line1
.widths
= line3
.widths
;
2878 while (tag
!= null) {
2884 if (line3
.color
== LineColor
.Black
)
2885 RebalanceAfterDelete(line2
);
2890 // Invalidate a section of the document to trigger redraw
2891 internal void Invalidate(Line start
, int start_pos
, Line end
, int end_pos
) {
2897 if ((start
== end
) && (start_pos
== end_pos
)) {
2901 if (end_pos
== -1) {
2902 end_pos
= end
.text
.Length
;
2905 // figure out what's before what so the logic below is straightforward
2906 if (start
.line_no
< end
.line_no
) {
2912 } else if (start
.line_no
> end
.line_no
) {
2919 if (start_pos
< end_pos
) {
2934 Console
.WriteLine("Invaliding from {0}:{1} to {2}:{3}", l1
.line_no
, p1
, l2
.line_no
, p2
);
2939 (int)l1
.widths
[p1
] + l1
.align_shift
- viewport_x
,
2941 (int)l2
.widths
[p2
] - (int)l1
.widths
[p1
] + 1,
2949 Console
.WriteLine("Invaliding from {0}:{1} to {2}:{3} Start => x={4}, y={5}, {6}x{7}", l1
.line_no
, p1
, l2
.line_no
, p2
, (int)l1
.widths
[p1
] + l1
.align_shift
- viewport_x
, l1
.Y
- viewport_y
, viewport_width
, l1
.height
);
2952 // Three invalidates:
2953 // First line from start
2954 owner
.Invalidate(new Rectangle((int)l1
.widths
[p1
] + l1
.align_shift
- viewport_x
, l1
.Y
- viewport_y
, viewport_width
, l1
.height
));
2957 if ((l1
.line_no
+ 1) < l2
.line_no
) {
2960 y
= GetLine(l1
.line_no
+ 1).Y
;
2961 owner
.Invalidate(new Rectangle(0, y
- viewport_y
, viewport_width
, GetLine(l2
.line_no
).Y
- y
- viewport_y
));
2964 Console
.WriteLine("Invaliding from {0}:{1} to {2}:{3} Middle => x={4}, y={5}, {6}x{7}", l1
.line_no
, p1
, l2
.line_no
, p2
, 0, y
- viewport_y
, viewport_width
, GetLine(l2
.line_no
).Y
- y
- viewport_y
);
2969 owner
.Invalidate(new Rectangle((int)l2
.widths
[0] + l2
.align_shift
- viewport_x
, l2
.Y
- viewport_y
, (int)l2
.widths
[p2
] + 1, l2
.height
));
2971 Console
.WriteLine("Invaliding from {0}:{1} to {2}:{3} End => x={4}, y={5}, {6}x{7}", l1
.line_no
, p1
, l2
.line_no
, p2
, (int)l2
.widths
[0] + l2
.align_shift
- viewport_x
, l2
.Y
- viewport_y
, (int)l2
.widths
[p2
] + 1, l2
.height
);
2975 /// <summary>Select text around caret</summary>
2976 internal void ExpandSelection(CaretSelection mode
, bool to_caret
) {
2978 // We're expanding the selection to the caret position
2980 case CaretSelection
.Line
: {
2981 // Invalidate the selection delta
2982 if (caret
> selection_prev
) {
2983 Invalidate(selection_prev
.line
, 0, caret
.line
, caret
.line
.text
.Length
);
2985 Invalidate(selection_prev
.line
, selection_prev
.line
.text
.Length
, caret
.line
, 0);
2988 if (caret
.line
.line_no
<= selection_anchor
.line
.line_no
) {
2989 selection_start
.line
= caret
.line
;
2990 selection_start
.tag
= caret
.line
.tags
;
2991 selection_start
.pos
= 0;
2993 selection_end
.line
= selection_anchor
.line
;
2994 selection_end
.tag
= selection_anchor
.tag
;
2995 selection_end
.pos
= selection_anchor
.pos
;
2997 selection_end_anchor
= true;
2999 selection_start
.line
= selection_anchor
.line
;
3000 selection_start
.pos
= selection_anchor
.height
;
3001 selection_start
.tag
= selection_anchor
.line
.FindTag(selection_anchor
.height
);
3003 selection_end
.line
= caret
.line
;
3004 selection_end
.tag
= caret
.line
.tags
;
3005 selection_end
.pos
= caret
.line
.text
.Length
;
3007 selection_end_anchor
= false;
3009 selection_prev
.line
= caret
.line
;
3010 selection_prev
.tag
= caret
.tag
;
3011 selection_prev
.pos
= caret
.pos
;
3016 case CaretSelection
.Word
: {
3020 start_pos
= FindWordSeparator(caret
.line
, caret
.pos
, false);
3021 end_pos
= FindWordSeparator(caret
.line
, caret
.pos
, true);
3024 // Invalidate the selection delta
3025 if (caret
> selection_prev
) {
3026 Invalidate(selection_prev
.line
, selection_prev
.pos
, caret
.line
, end_pos
);
3028 Invalidate(selection_prev
.line
, selection_prev
.pos
, caret
.line
, start_pos
);
3030 if (caret
< selection_anchor
) {
3031 selection_start
.line
= caret
.line
;
3032 selection_start
.tag
= caret
.line
.FindTag(start_pos
);
3033 selection_start
.pos
= start_pos
;
3035 selection_end
.line
= selection_anchor
.line
;
3036 selection_end
.tag
= selection_anchor
.tag
;
3037 selection_end
.pos
= selection_anchor
.pos
;
3039 selection_prev
.line
= caret
.line
;
3040 selection_prev
.tag
= caret
.tag
;
3041 selection_prev
.pos
= start_pos
;
3043 selection_end_anchor
= true;
3045 selection_start
.line
= selection_anchor
.line
;
3046 selection_start
.pos
= selection_anchor
.height
;
3047 selection_start
.tag
= selection_anchor
.line
.FindTag(selection_anchor
.height
);
3049 selection_end
.line
= caret
.line
;
3050 selection_end
.tag
= caret
.line
.FindTag(end_pos
);
3051 selection_end
.pos
= end_pos
;
3053 selection_prev
.line
= caret
.line
;
3054 selection_prev
.tag
= caret
.tag
;
3055 selection_prev
.pos
= end_pos
;
3057 selection_end_anchor
= false;
3062 case CaretSelection
.Position
: {
3063 SetSelectionToCaret(false);
3068 // We're setting the selection 'around' the caret position
3070 case CaretSelection
.Line
: {
3071 this.Invalidate(caret
.line
, 0, caret
.line
, caret
.line
.text
.Length
);
3073 selection_start
.line
= caret
.line
;
3074 selection_start
.tag
= caret
.line
.tags
;
3075 selection_start
.pos
= 0;
3077 selection_end
.line
= caret
.line
;
3078 selection_end
.pos
= caret
.line
.text
.Length
;
3079 selection_end
.tag
= caret
.line
.FindTag(selection_end
.pos
);
3081 selection_anchor
.line
= selection_end
.line
;
3082 selection_anchor
.tag
= selection_end
.tag
;
3083 selection_anchor
.pos
= selection_end
.pos
;
3084 selection_anchor
.height
= 0;
3086 selection_prev
.line
= caret
.line
;
3087 selection_prev
.tag
= caret
.tag
;
3088 selection_prev
.pos
= caret
.pos
;
3090 this.selection_end_anchor
= true;
3095 case CaretSelection
.Word
: {
3099 start_pos
= FindWordSeparator(caret
.line
, caret
.pos
, false);
3100 end_pos
= FindWordSeparator(caret
.line
, caret
.pos
, true);
3102 this.Invalidate(selection_start
.line
, start_pos
, caret
.line
, end_pos
);
3104 selection_start
.line
= caret
.line
;
3105 selection_start
.tag
= caret
.line
.FindTag(start_pos
);
3106 selection_start
.pos
= start_pos
;
3108 selection_end
.line
= caret
.line
;
3109 selection_end
.tag
= caret
.line
.FindTag(end_pos
);
3110 selection_end
.pos
= end_pos
;
3112 selection_anchor
.line
= selection_end
.line
;
3113 selection_anchor
.tag
= selection_end
.tag
;
3114 selection_anchor
.pos
= selection_end
.pos
;
3115 selection_anchor
.height
= start_pos
;
3117 selection_prev
.line
= caret
.line
;
3118 selection_prev
.tag
= caret
.tag
;
3119 selection_prev
.pos
= caret
.pos
;
3121 this.selection_end_anchor
= true;
3128 SetSelectionVisible (!(selection_start
== selection_end
));
3131 internal void SetSelectionToCaret(bool start
) {
3133 // Invalidate old selection; selection is being reset to empty
3134 this.Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3136 selection_start
.line
= caret
.line
;
3137 selection_start
.tag
= caret
.tag
;
3138 selection_start
.pos
= caret
.pos
;
3140 // start always also selects end
3141 selection_end
.line
= caret
.line
;
3142 selection_end
.tag
= caret
.tag
;
3143 selection_end
.pos
= caret
.pos
;
3145 selection_anchor
.line
= caret
.line
;
3146 selection_anchor
.tag
= caret
.tag
;
3147 selection_anchor
.pos
= caret
.pos
;
3149 // Invalidate from previous end to caret (aka new end)
3150 if (selection_end_anchor
) {
3151 if (selection_start
!= caret
) {
3152 this.Invalidate(selection_start
.line
, selection_start
.pos
, caret
.line
, caret
.pos
);
3155 if (selection_end
!= caret
) {
3156 this.Invalidate(selection_end
.line
, selection_end
.pos
, caret
.line
, caret
.pos
);
3160 if (caret
< selection_anchor
) {
3161 selection_start
.line
= caret
.line
;
3162 selection_start
.tag
= caret
.tag
;
3163 selection_start
.pos
= caret
.pos
;
3165 selection_end
.line
= selection_anchor
.line
;
3166 selection_end
.tag
= selection_anchor
.tag
;
3167 selection_end
.pos
= selection_anchor
.pos
;
3169 selection_end_anchor
= true;
3171 selection_start
.line
= selection_anchor
.line
;
3172 selection_start
.tag
= selection_anchor
.tag
;
3173 selection_start
.pos
= selection_anchor
.pos
;
3175 selection_end
.line
= caret
.line
;
3176 selection_end
.tag
= caret
.tag
;
3177 selection_end
.pos
= caret
.pos
;
3179 selection_end_anchor
= false;
3183 SetSelectionVisible (!(selection_start
== selection_end
));
3186 internal void SetSelection(Line start
, int start_pos
, Line end
, int end_pos
) {
3187 if (selection_visible
) {
3188 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3191 if ((end
.line_no
< start
.line_no
) || ((end
== start
) && (end_pos
<= start_pos
))) {
3192 selection_start
.line
= end
;
3193 selection_start
.tag
= LineTag
.FindTag(end
, end_pos
);
3194 selection_start
.pos
= end_pos
;
3196 selection_end
.line
= start
;
3197 selection_end
.tag
= LineTag
.FindTag(start
, start_pos
);
3198 selection_end
.pos
= start_pos
;
3200 selection_end_anchor
= true;
3202 selection_start
.line
= start
;
3203 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3204 selection_start
.pos
= start_pos
;
3206 selection_end
.line
= end
;
3207 selection_end
.tag
= LineTag
.FindTag(end
, end_pos
);
3208 selection_end
.pos
= end_pos
;
3210 selection_end_anchor
= false;
3213 selection_anchor
.line
= start
;
3214 selection_anchor
.tag
= selection_start
.tag
;
3215 selection_anchor
.pos
= start_pos
;
3217 if (((start
== end
) && (start_pos
== end_pos
)) || start
== null || end
== null) {
3218 SetSelectionVisible (false);
3220 SetSelectionVisible (true);
3221 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3225 internal void SetSelectionStart(Line start
, int start_pos
) {
3226 // Invalidate from the previous to the new start pos
3227 Invalidate(selection_start
.line
, selection_start
.pos
, start
, start_pos
);
3229 selection_start
.line
= start
;
3230 selection_start
.pos
= start_pos
;
3231 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3233 selection_anchor
.line
= start
;
3234 selection_anchor
.pos
= start_pos
;
3235 selection_anchor
.tag
= selection_start
.tag
;
3237 selection_end_anchor
= false;
3240 if ((selection_end
.line
!= selection_start
.line
) || (selection_end
.pos
!= selection_start
.pos
)) {
3241 SetSelectionVisible (true);
3243 SetSelectionVisible (false);
3246 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3249 internal void SetSelectionStart(int character_index
) {
3254 if (character_index
< 0) {
3258 CharIndexToLineTag(character_index
, out line
, out tag
, out pos
);
3259 SetSelectionStart(line
, pos
);
3262 internal void SetSelectionEnd(Line end
, int end_pos
) {
3264 if (end
== selection_end
.line
&& end_pos
== selection_start
.pos
) {
3265 selection_anchor
.line
= selection_start
.line
;
3266 selection_anchor
.tag
= selection_start
.tag
;
3267 selection_anchor
.pos
= selection_start
.pos
;
3269 selection_end
.line
= selection_start
.line
;
3270 selection_end
.tag
= selection_start
.tag
;
3271 selection_end
.pos
= selection_start
.pos
;
3273 selection_end_anchor
= false;
3274 } else if ((end
.line_no
< selection_anchor
.line
.line_no
) || ((end
== selection_anchor
.line
) && (end_pos
<= selection_anchor
.pos
))) {
3275 selection_start
.line
= end
;
3276 selection_start
.tag
= LineTag
.FindTag(end
, end_pos
);
3277 selection_start
.pos
= end_pos
;
3279 selection_end
.line
= selection_anchor
.line
;
3280 selection_end
.tag
= selection_anchor
.tag
;
3281 selection_end
.pos
= selection_anchor
.pos
;
3283 selection_end_anchor
= true;
3285 selection_start
.line
= selection_anchor
.line
;
3286 selection_start
.tag
= selection_anchor
.tag
;
3287 selection_start
.pos
= selection_anchor
.pos
;
3289 selection_end
.line
= end
;
3290 selection_end
.tag
= LineTag
.FindTag(end
, end_pos
);
3291 selection_end
.pos
= end_pos
;
3293 selection_end_anchor
= false;
3296 if ((selection_end
.line
!= selection_start
.line
) || (selection_end
.pos
!= selection_start
.pos
)) {
3297 SetSelectionVisible (true);
3298 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3300 SetSelectionVisible (false);
3301 // ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
3305 internal void SetSelectionEnd(int character_index
) {
3310 if (character_index
< 0) {
3314 CharIndexToLineTag(character_index
, out line
, out tag
, out pos
);
3315 SetSelectionEnd(line
, pos
);
3318 internal void SetSelection(Line start
, int start_pos
) {
3319 if (selection_visible
) {
3320 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3323 selection_start
.line
= start
;
3324 selection_start
.pos
= start_pos
;
3325 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3327 selection_end
.line
= start
;
3328 selection_end
.tag
= selection_start
.tag
;
3329 selection_end
.pos
= start_pos
;
3331 selection_anchor
.line
= start
;
3332 selection_anchor
.tag
= selection_start
.tag
;
3333 selection_anchor
.pos
= start_pos
;
3335 selection_end_anchor
= false;
3336 SetSelectionVisible (false);
3339 internal void InvalidateSelectionArea() {
3340 Invalidate (selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3343 // Return the current selection, as string
3344 internal string GetSelection() {
3345 // We return String.Empty if there is no selection
3346 if ((selection_start
.pos
== selection_end
.pos
) && (selection_start
.line
== selection_end
.line
)) {
3347 return string.Empty
;
3350 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3351 return selection_start
.line
.text
.ToString(selection_start
.pos
, selection_end
.pos
- selection_start
.pos
);
3358 sb
= new StringBuilder();
3359 start
= selection_start
.line
.line_no
;
3360 end
= selection_end
.line
.line_no
;
3362 sb
.Append(selection_start
.line
.text
.ToString(selection_start
.pos
, selection_start
.line
.text
.Length
- selection_start
.pos
) + Environment
.NewLine
);
3364 if ((start
+ 1) < end
) {
3365 for (i
= start
+ 1; i
< end
; i
++) {
3366 sb
.Append(GetLine(i
).text
.ToString() + Environment
.NewLine
);
3370 sb
.Append(selection_end
.line
.text
.ToString(0, selection_end
.pos
));
3372 return sb
.ToString();
3376 internal void ReplaceSelection(string s
, bool select_new
) {
3379 undo
.BeginCompoundAction ();
3381 int selection_start_pos
= LineTagToCharIndex (selection_start
.line
, selection_start
.pos
);
3382 // First, delete any selected text
3383 if ((selection_start
.pos
!= selection_end
.pos
) || (selection_start
.line
!= selection_end
.line
)) {
3384 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3385 undo
.RecordDeleteChars(selection_start
.line
, selection_start
.pos
+ 1, selection_end
.pos
- selection_start
.pos
);
3387 DeleteChars(selection_start
.tag
, selection_start
.pos
, selection_end
.pos
- selection_start
.pos
);
3389 // The tag might have been removed, we need to recalc it
3390 selection_start
.tag
= selection_start
.line
.FindTag(selection_start
.pos
);
3395 start
= selection_start
.line
.line_no
;
3396 end
= selection_end
.line
.line_no
;
3398 undo
.RecordDelete(selection_start
.line
, selection_start
.pos
+ 1, selection_end
.line
, selection_end
.pos
);
3400 // Delete first line
3401 DeleteChars(selection_start
.tag
, selection_start
.pos
, selection_start
.line
.text
.Length
- selection_start
.pos
);
3404 DeleteChars(selection_end
.line
.tags
, 0, selection_end
.pos
);
3408 for (i
= end
- 1; i
>= start
; i
--) {
3413 // BIG FAT WARNING - selection_end.line might be stale due
3414 // to the above Delete() call. DONT USE IT before hitting the end of this method!
3416 // Join start and end
3417 Combine(selection_start
.line
.line_no
, start
);
3421 Insert(selection_start
.line
, selection_start
.pos
, true, s
);
3422 undo
.RecordInsertString (selection_start
.line
, selection_start
.pos
, s
);
3425 CharIndexToLineTag(selection_start_pos
+ s
.Length
, out selection_start
.line
,
3426 out selection_start
.tag
, out selection_start
.pos
);
3428 selection_end
.line
= selection_start
.line
;
3429 selection_end
.pos
= selection_start
.pos
;
3430 selection_end
.tag
= selection_start
.tag
;
3431 selection_anchor
.line
= selection_start
.line
;
3432 selection_anchor
.pos
= selection_start
.pos
;
3433 selection_anchor
.tag
= selection_start
.tag
;
3435 SetSelectionVisible (false);
3437 CharIndexToLineTag(selection_start_pos
, out selection_start
.line
,
3438 out selection_start
.tag
, out selection_start
.pos
);
3440 CharIndexToLineTag(selection_start_pos
+ s
.Length
, out selection_end
.line
,
3441 out selection_end
.tag
, out selection_end
.pos
);
3443 selection_anchor
.line
= selection_start
.line
;
3444 selection_anchor
.pos
= selection_start
.pos
;
3445 selection_anchor
.tag
= selection_start
.tag
;
3447 SetSelectionVisible (true);
3450 undo
.EndCompoundAction ();
3453 internal void CharIndexToLineTag(int index
, out Line line_out
, out LineTag tag_out
, out int pos
) {
3462 for (i
= 1; i
<= lines
; i
++) {
3466 chars
+= line
.text
.Length
+ crlf_size
;
3468 if (index
<= chars
) {
3469 // we found the line
3472 while (tag
!= null) {
3473 if (index
< (start
+ tag
.start
+ tag
.length
)) {
3475 tag_out
= LineTag
.GetFinalTag (tag
);
3476 pos
= index
- start
;
3479 if (tag
.next
== null) {
3482 next_line
= GetLine(line
.line_no
+ 1);
3484 if (next_line
!= null) {
3485 line_out
= next_line
;
3486 tag_out
= LineTag
.GetFinalTag (next_line
.tags
);
3491 tag_out
= LineTag
.GetFinalTag (tag
);
3492 pos
= line_out
.text
.Length
;
3501 line_out
= GetLine(lines
);
3502 tag
= line_out
.tags
;
3503 while (tag
.next
!= null) {
3507 pos
= line_out
.text
.Length
;
3510 internal int LineTagToCharIndex(Line line
, int pos
) {
3514 // Count first and last line
3517 // Count the lines in the middle
3519 for (i
= 1; i
< line
.line_no
; i
++) {
3520 length
+= GetLine(i
).text
.Length
+ crlf_size
;
3528 internal int SelectionLength() {
3529 if ((selection_start
.pos
== selection_end
.pos
) && (selection_start
.line
== selection_end
.line
)) {
3533 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3534 return selection_end
.pos
- selection_start
.pos
;
3541 // Count first and last line
3542 length
= selection_start
.line
.text
.Length
- selection_start
.pos
+ selection_end
.pos
+ crlf_size
;
3544 // Count the lines in the middle
3545 start
= selection_start
.line
.line_no
+ 1;
3546 end
= selection_end
.line
.line_no
;
3549 for (i
= start
; i
< end
; i
++) {
3550 length
+= GetLine(i
).text
.Length
+ crlf_size
;
3561 /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
3562 internal Line
GetLine(int LineNo
) {
3563 Line line
= document
;
3565 while (line
!= sentinel
) {
3566 if (LineNo
== line
.line_no
) {
3568 } else if (LineNo
< line
.line_no
) {
3578 /// <summary>Retrieve the previous tag; walks line boundaries</summary>
3579 internal LineTag
PreviousTag(LineTag tag
) {
3582 if (tag
.previous
!= null) {
3583 return tag
.previous
;
3587 if (tag
.line
.line_no
== 1) {
3591 l
= GetLine(tag
.line
.line_no
- 1);
3596 while (t
.next
!= null) {
3605 /// <summary>Retrieve the next tag; walks line boundaries</summary>
3606 internal LineTag
NextTag(LineTag tag
) {
3609 if (tag
.next
!= null) {
3614 l
= GetLine(tag
.line
.line_no
+ 1);
3622 internal Line
ParagraphStart(Line line
) {
3623 while (line
.soft_break
) {
3624 line
= GetLine(line
.line_no
- 1);
3629 internal Line
ParagraphEnd(Line line
) {
3632 while (line
.soft_break
) {
3633 l
= GetLine(line
.line_no
+ 1);
3634 if ((l
== null) || (!l
.soft_break
)) {
3642 /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
3643 internal Line
GetLineByPixel(int y
, bool exact
) {
3644 Line line
= document
;
3647 while (line
!= sentinel
) {
3649 if ((y
>= line
.Y
) && (y
< (line
.Y
+line
.height
))) {
3651 } else if (y
< line
.Y
) {
3664 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3665 internal LineTag
FindTag(int x
, int y
, out int index
, bool exact
) {
3669 line
= GetLineByPixel(y
, exact
);
3676 // Alignment adjustment
3677 x
+= line
.align_shift
;
3680 if (x
>= tag
.X
&& x
< (tag
.X
+tag
.width
)) {
3683 end
= tag
.start
+ tag
.length
- 1;
3685 for (int pos
= tag
.start
; pos
< end
; pos
++) {
3686 if (x
< line
.widths
[pos
]) {
3688 return LineTag
.GetFinalTag (tag
);
3692 return LineTag
.GetFinalTag (tag
);
3694 if (tag
.next
!= null) {
3702 index
= line
.text
.Length
;
3703 return LineTag
.GetFinalTag (tag
);
3708 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3709 internal LineTag
FindCursor(int x
, int y
, out int index
) {
3713 line
= GetLineByPixel(y
, false);
3716 // Adjust for alignment
3717 x
-= line
.align_shift
;
3720 if (x
>= tag
.X
&& x
< (tag
.X
+tag
.width
)) {
3723 end
= tag
.start
+ tag
.length
- 1;
3725 for (int pos
= tag
.start
-1; pos
< end
; pos
++) {
3726 // When clicking on a character, we position the cursor to whatever edge
3727 // of the character the click was closer
3728 if (x
< (line
.widths
[pos
] + ((line
.widths
[pos
+1]-line
.widths
[pos
])/2))) {
3736 if (tag
.next
!= null) {
3739 index
= line
.text
.Length
;
3745 /// <summary>Format area of document in specified font and color</summary>
3746 /// <param name="start_pos">1-based start position on start_line</param>
3747 /// <param name="end_pos">1-based end position on end_line </param>
3748 internal void FormatText(Line start_line
, int start_pos
, Line end_line
, int end_pos
, Font font
, Brush color
) {
3751 // First, format the first line
3752 if (start_line
!= end_line
) {
3754 LineTag
.FormatText(start_line
, start_pos
, start_line
.text
.Length
- start_pos
+ 1, font
, color
);
3757 LineTag
.FormatText(end_line
, 1, end_pos
, font
, color
);
3759 // Now all the lines inbetween
3760 for (int i
= start_line
.line_no
+ 1; i
< end_line
.line_no
; i
++) {
3762 LineTag
.FormatText(l
, 1, l
.text
.Length
, font
, color
);
3765 // Special case, single line
3766 LineTag
.FormatText(start_line
, start_pos
, end_pos
- start_pos
, font
, color
);
3770 /// <summary>Re-format areas of the document in specified font and color</summary>
3771 /// <param name="start_pos">1-based start position on start_line</param>
3772 /// <param name="end_pos">1-based end position on end_line </param>
3773 /// <param name="font">Font specifying attributes</param>
3774 /// <param name="color">Color (or NULL) to apply</param>
3775 /// <param name="apply">Attributes from font and color to apply</param>
3776 internal void FormatText(Line start_line
, int start_pos
, Line end_line
, int end_pos
, FontDefinition attributes
) {
3779 // First, format the first line
3780 if (start_line
!= end_line
) {
3782 LineTag
.FormatText(start_line
, start_pos
, start_line
.text
.Length
- start_pos
+ 1, attributes
);
3785 LineTag
.FormatText(end_line
, 1, end_pos
- 1, attributes
);
3787 // Now all the lines inbetween
3788 for (int i
= start_line
.line_no
+ 1; i
< end_line
.line_no
; i
++) {
3790 LineTag
.FormatText(l
, 1, l
.text
.Length
, attributes
);
3793 // Special case, single line
3794 LineTag
.FormatText(start_line
, start_pos
, end_pos
- start_pos
, attributes
);
3798 internal void RecalculateAlignments() {
3804 while (line_no
<= lines
) {
3805 line
= GetLine(line_no
);
3808 switch (line
.alignment
) {
3809 case HorizontalAlignment
.Left
:
3810 line
.align_shift
= 0;
3812 case HorizontalAlignment
.Center
:
3813 line
.align_shift
= (viewport_width
- (int)line
.widths
[line
.text
.Length
]) / 2;
3815 case HorizontalAlignment
.Right
:
3816 line
.align_shift
= viewport_width
- (int)line
.widths
[line
.text
.Length
];
3826 /// <summary>Calculate formatting for the whole document</summary>
3827 internal bool RecalculateDocument(Graphics g
) {
3828 return RecalculateDocument(g
, 1, this.lines
, false);
3831 /// <summary>Calculate formatting starting at a certain line</summary>
3832 internal bool RecalculateDocument(Graphics g
, int start
) {
3833 return RecalculateDocument(g
, start
, this.lines
, false);
3836 /// <summary>Calculate formatting within two given line numbers</summary>
3837 internal bool RecalculateDocument(Graphics g
, int start
, int end
) {
3838 return RecalculateDocument(g
, start
, end
, false);
3841 /// <summary>With optimize on, returns true if line heights changed</summary>
3842 internal bool RecalculateDocument(Graphics g
, int start
, int end
, bool optimize
) {
3851 recalc_pending
= true;
3852 recalc_start
= start
;
3854 recalc_optimize
= optimize
;
3858 Y
= GetLine(start
).Y
;
3863 changed
= true; // We always return true if we run non-optimized
3868 while (line_no
<= (end
+ this.lines
- shift
)) {
3869 line
= GetLine(line_no
++);
3874 line
.RecalculateLine(g
, this);
3876 if (line
.recalc
&& line
.RecalculateLine(g
, this)) {
3878 // If the height changed, all subsequent lines change
3885 line
.RecalculatePasswordLine(g
, this);
3887 if (line
.recalc
&& line
.RecalculatePasswordLine(g
, this)) {
3889 // If the height changed, all subsequent lines change
3896 if (line
.widths
[line
.text
.Length
] > new_width
) {
3897 new_width
= (int)line
.widths
[line
.text
.Length
];
3900 // Calculate alignment
3901 if (line
.alignment
!= HorizontalAlignment
.Left
) {
3902 if (line
.alignment
== HorizontalAlignment
.Center
) {
3903 line
.align_shift
= (viewport_width
- (int)line
.widths
[line
.text
.Length
]) / 2;
3905 line
.align_shift
= viewport_width
- (int)line
.widths
[line
.text
.Length
] - 1;
3911 if (line_no
> lines
) {
3916 if (document_x
!= new_width
) {
3917 document_x
= new_width
;
3918 if (WidthChanged
!= null) {
3919 WidthChanged(this, null);
3923 RecalculateAlignments();
3925 line
= GetLine(lines
);
3927 if (document_y
!= line
.Y
+ line
.height
) {
3928 document_y
= line
.Y
+ line
.height
;
3929 if (HeightChanged
!= null) {
3930 HeightChanged(this, null);
3937 internal int Size() {
3941 private void owner_HandleCreated(object sender
, EventArgs e
) {
3942 RecalculateDocument(owner
.CreateGraphicsInternal());
3946 private void owner_VisibleChanged(object sender
, EventArgs e
) {
3947 if (owner
.Visible
) {
3948 RecalculateDocument(owner
.CreateGraphicsInternal());
3952 internal static bool IsWordSeparator(char ch
) {
3966 internal int FindWordSeparator(Line line
, int pos
, bool forward
) {
3969 len
= line
.text
.Length
;
3972 for (int i
= pos
+ 1; i
< len
; i
++) {
3973 if (IsWordSeparator(line
.Text
[i
])) {
3979 for (int i
= pos
- 1; i
> 0; i
--) {
3980 if (IsWordSeparator(line
.Text
[i
- 1])) {
3988 /* Search document for text */
3989 internal bool FindChars(char[] chars
, Marker start
, Marker end
, out Marker result
) {
3995 // Search for occurence of any char in the chars array
3996 result
= new Marker();
3999 line_no
= start
.line
.line_no
;
4001 while (line_no
<= end
.line
.line_no
) {
4002 line_len
= line
.text
.Length
;
4003 while (pos
< line_len
) {
4004 for (int i
= 0; i
< chars
.Length
; i
++) {
4005 if (line
.text
[pos
] == chars
[i
]) {
4007 if ((line
.line_no
== end
.line
.line_no
) && (pos
>= end
.pos
)) {
4021 line
= GetLine(line_no
);
4027 // This version does not build one big string for searching, instead it handles
4028 // line-boundaries, which is faster and less memory intensive
4029 // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
4030 // search stuff and change it to accept and return positions instead of Markers (which would match
4031 // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
4032 internal bool Find(string search
, Marker start
, Marker end
, out Marker result
, RichTextBoxFinds options
) {
4034 string search_string
;
4046 result
= new Marker();
4047 word_option
= ((options
& RichTextBoxFinds
.WholeWord
) != 0);
4048 ignore_case
= ((options
& RichTextBoxFinds
.MatchCase
) == 0);
4049 reverse
= ((options
& RichTextBoxFinds
.Reverse
) != 0);
4052 line_no
= start
.line
.line_no
;
4056 // Prep our search string, lowercasing it if we do case-independent matching
4059 sb
= new StringBuilder(search
);
4060 for (int i
= 0; i
< sb
.Length
; i
++) {
4061 sb
[i
] = Char
.ToLower(sb
[i
]);
4063 search_string
= sb
.ToString();
4065 search_string
= search
;
4068 // We need to check if the character before our start position is a wordbreak
4071 if ((pos
== 0) || (IsWordSeparator(line
.text
[pos
- 1]))) {
4078 if (IsWordSeparator(line
.text
[pos
- 1])) {
4084 // Need to check the end of the previous line
4087 prev_line
= GetLine(line_no
- 1);
4088 if (prev_line
.soft_break
) {
4089 if (IsWordSeparator(prev_line
.text
[prev_line
.text
.Length
- 1])) {
4103 // To avoid duplication of this loop with reverse logic, we search
4104 // through the document, remembering the last match and when returning
4105 // report that last remembered match
4107 last
= new Marker();
4108 last
.height
= -1; // Abused - we use it to track change
4110 while (line_no
<= end
.line
.line_no
) {
4111 if (line_no
!= end
.line
.line_no
) {
4112 line_len
= line
.text
.Length
;
4117 while (pos
< line_len
) {
4118 if (word_option
&& (current
== search_string
.Length
)) {
4119 if (IsWordSeparator(line
.text
[pos
])) {
4132 c
= Char
.ToLower(line
.text
[pos
]);
4137 if (c
== search_string
[current
]) {
4142 if (!word_option
|| (word_option
&& (word
|| (current
> 0)))) {
4146 if (!word_option
&& (current
== search_string
.Length
)) {
4163 if (IsWordSeparator(c
)) {
4171 // Mark that we just saw a word boundary
4172 if (!line
.soft_break
) {
4176 if (current
== search_string
.Length
) {
4192 line
= GetLine(line_no
);
4196 if (last
.height
!= -1) {
4206 // if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
4218 internal void GetMarker(out Marker mark
, bool start
) {
4219 mark
= new Marker();
4222 mark
.line
= GetLine(1);
4223 mark
.tag
= mark
.line
.tags
;
4226 mark
.line
= GetLine(lines
);
4227 mark
.tag
= mark
.line
.tags
;
4228 while (mark
.tag
.next
!= null) {
4229 mark
.tag
= mark
.tag
.next
;
4231 mark
.pos
= mark
.line
.text
.Length
;
4234 #endregion // Internal Methods
4237 internal event EventHandler CaretMoved
;
4238 internal event EventHandler WidthChanged
;
4239 internal event EventHandler HeightChanged
;
4240 internal event EventHandler LengthChanged
;
4241 #endregion // Events
4243 #region Administrative
4244 public IEnumerator
GetEnumerator() {
4249 public override bool Equals(object obj
) {
4254 if (!(obj
is Document
)) {
4262 if (ToString().Equals(((Document
)obj
).ToString())) {
4269 public override int GetHashCode() {
4273 public override string ToString() {
4274 return "document " + this.document_id
;
4276 #endregion // Administrative
4279 internal class LineTag
{
4280 #region Local Variables;
4281 // Payload; formatting
4282 internal Font font
; // System.Drawing.Font object for this tag
4283 internal Brush color
; // System.Drawing.Brush object
4286 internal int start
; // start, in chars; index into Line.text
4287 internal int length
; // length, in chars
4288 internal bool r_to_l
; // Which way is the font
4291 internal int height
; // Height in pixels of the text this tag describes
4292 internal int X
; // X location of the text this tag describes
4293 internal float width
; // Width in pixels of the text this tag describes
4294 internal int ascent
; // Ascent of the font for this tag
4295 internal int shift
; // Shift down for this tag, to stay on baseline
4298 internal Line line
; // The line we're on
4299 internal LineTag next
; // Next tag on the same line
4300 internal LineTag previous
; // Previous tag on the same line
4303 #region Constructors
4304 internal LineTag(Line line
, int start
, int length
) {
4307 this.length
= length
;
4311 #endregion // Constructors
4313 #region Internal Methods
4314 ///<summary>Break a tag into two with identical attributes; pos is 1-based; returns tag starting at >pos< or null if end-of-line</summary>
4315 internal LineTag
Break(int pos
) {
4319 if (pos
== this.start
) {
4321 } else if (pos
>= (start
+ length
)) {
4325 new_tag
= new LineTag(line
, pos
, start
+ length
- pos
);
4326 new_tag
.color
= color
;
4327 new_tag
.font
= font
;
4328 this.length
-= new_tag
.length
;
4329 new_tag
.next
= this.next
;
4330 this.next
= new_tag
;
4331 new_tag
.previous
= this;
4332 if (new_tag
.next
!= null) {
4333 new_tag
.next
.previous
= new_tag
;
4339 ///<summary>Create new font and brush from existing font and given new attributes. Returns true if fontheight changes</summary>
4340 internal static bool GenerateTextFormat(Font font_from
, Brush color_from
, FontDefinition attributes
, out Font new_font
, out Brush new_color
) {
4346 if (attributes
.font_obj
== null) {
4347 size
= font_from
.SizeInPoints
;
4348 unit
= font_from
.Unit
;
4349 face
= font_from
.Name
;
4350 style
= font_from
.Style
;
4352 if (attributes
.face
!= null) {
4353 face
= attributes
.face
;
4356 if (attributes
.size
!= 0) {
4357 size
= attributes
.size
;
4360 style
|= attributes
.add_style
;
4361 style
&= ~attributes
.remove_style
;
4364 new_font
= new Font(face
, size
, style
, unit
);
4366 new_font
= attributes
.font_obj
;
4369 // Create 'new' color brush
4370 if (attributes
.color
!= Color
.Empty
) {
4371 new_color
= new SolidBrush(attributes
.color
);
4373 new_color
= color_from
;
4376 if (new_font
.Height
== font_from
.Height
) {
4382 /// <summary>Applies 'font' and 'brush' to characters starting at 'start' for 'length' chars;
4383 /// Removes any previous tags overlapping the same area;
4384 /// returns true if lineheight has changed</summary>
4385 /// <param name="start">1-based character position on line</param>
4386 internal static bool FormatText(Line line
, int start
, int length
, Font font
, Brush color
) {
4391 bool retval
= false; // Assume line-height doesn't change
4394 if (font
.Height
!= line
.height
) {
4397 line
.recalc
= true; // This forces recalculation of the line in RecalculateDocument
4399 // A little sanity, not sure if it's needed, might be able to remove for speed
4400 if (length
> line
.text
.Length
) {
4401 length
= line
.text
.Length
;
4405 end
= start
+ length
;
4407 // Common special case
4408 if ((start
== 1) && (length
== tag
.length
)) {
4415 //Console.WriteLine("Finding tag for {0} {1}", line, start);
4416 start_tag
= FindTag(line
, start
);
4417 end_tag
= FindTag (line
, end
);
4419 if (start_tag
== null) { // FIXME - is there a better way to handle this, or do we even need it?
4420 throw new Exception(String
.Format("Could not find start_tag in document at line {0} position {1}", line
.line_no
, start
));
4423 tag
= new LineTag(line
, start
, length
);
4430 //Console.WriteLine("Start tag: '{0}'", start_tag!=null ? start_tag.ToString() : "NULL");
4431 if (start_tag
.start
== start
) {
4432 tag
.next
= start_tag
;
4433 tag
.previous
= start_tag
.previous
;
4434 if (start_tag
.previous
!= null) {
4435 start_tag
.previous
.next
= tag
;
4437 start_tag
.previous
= tag
;
4441 if (end_tag
!= null) {
4442 // Shorten up the end tag
4443 end_tag
.previous
= tag
;
4444 end_tag
.length
= end
- start_tag
.start
+ start_tag
.length
;
4445 end_tag
.start
= end
;
4451 while (tag
!= end_tag
) {
4452 if ((tag
.start
+ tag
.length
) <= end
) {
4454 tag
.previous
.next
= tag
.next
;
4455 if (tag
.next
!= null) {
4456 tag
.next
.previous
= tag
.previous
;
4466 /// <summary>Applies font attributes specified to characters starting at 'start' for 'length' chars;
4467 /// Breaks tags at start and end point, keeping middle tags with altered attributes.
4468 /// Returns true if lineheight has changed</summary>
4469 /// <param name="start">1-based character position on line</param>
4470 internal static bool FormatText(Line line
, int start
, int length
, FontDefinition attributes
) {
4474 bool retval
= false; // Assume line-height doesn't change
4476 line
.recalc
= true; // This forces recalculation of the line in RecalculateDocument
4478 // A little sanity, not sure if it's needed, might be able to remove for speed
4479 if (length
> line
.text
.Length
) {
4480 length
= line
.text
.Length
;
4485 // Common special case
4486 if ((start
== 1) && (length
== tag
.length
)) {
4488 GenerateTextFormat(tag
.font
, tag
.color
, attributes
, out tag
.font
, out tag
.color
);
4492 start_tag
= FindTag(line
, start
);
4494 if (start_tag
== null) {
4496 // We are 'starting' after all valid tags; create a new tag with the right attributes
4497 start_tag
= FindTag(line
, line
.text
.Length
- 1);
4498 start_tag
.next
= new LineTag(line
, line
.text
.Length
+ 1, 0);
4499 start_tag
.next
.font
= start_tag
.font
;
4500 start_tag
.next
.color
= start_tag
.color
;
4501 start_tag
.next
.previous
= start_tag
;
4502 start_tag
= start_tag
.next
;
4504 throw new Exception(String
.Format("Could not find start_tag in document at line {0} position {1}", line
.line_no
, start
));
4507 start_tag
= start_tag
.Break(start
);
4510 end_tag
= FindTag(line
, start
+ length
);
4511 if (end_tag
!= null) {
4512 end_tag
= end_tag
.Break(start
+ length
);
4515 // start_tag or end_tag might be null; we're cool with that
4516 // we now walk from start_tag to end_tag, applying new attributes
4518 while ((tag
!= null) && tag
!= end_tag
) {
4519 if (LineTag
.GenerateTextFormat(tag
.font
, tag
.color
, attributes
, out tag
.font
, out tag
.color
)) {
4528 /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
4529 internal static LineTag
FindTag(Line line
, int pos
) {
4530 LineTag tag
= line
.tags
;
4532 // Beginning of line is a bit special
4534 // Not sure if we should get the final tag here
4538 while (tag
!= null) {
4539 if ((tag
.start
<= pos
) && (pos
< (tag
.start
+tag
.length
))) {
4540 return GetFinalTag (tag
);
4549 // There can be multiple tags at the same position, we want to make
4550 // sure we are using the very last tag at the given position
4551 internal static LineTag
GetFinalTag (LineTag tag
)
4555 while (res
.next
!= null && res
.next
.length
== 0)
4560 /// <summary>Combines 'this' tag with 'other' tag</summary>
4561 internal bool Combine(LineTag other
) {
4562 if (!this.Equals(other
)) {
4566 this.width
+= other
.width
;
4567 this.length
+= other
.length
;
4568 this.next
= other
.next
;
4569 if (this.next
!= null) {
4570 this.next
.previous
= this;
4577 /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
4578 internal bool Remove() {
4579 if ((this.start
== 1) && (this.next
== null)) {
4580 // We cannot remove the only tag
4583 if (this.start
!= 1) {
4584 this.previous
.length
+= this.length
;
4585 this.previous
.width
= -1;
4586 this.previous
.next
= this.next
;
4587 this.next
.previous
= this.previous
;
4589 this.next
.start
= 1;
4590 this.next
.length
+= this.length
;
4591 this.next
.width
= -1;
4592 this.line
.tags
= this.next
;
4593 this.next
.previous
= null;
4599 /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
4600 public override bool Equals(object obj
) {
4607 if (!(obj
is LineTag
)) {
4615 other
= (LineTag
)obj
;
4617 if (this.font
.Equals(other
.font
) && this.color
.Equals(other
.color
)) { // FIXME add checking for things like link or type later
4624 public override int GetHashCode() {
4625 return base.GetHashCode ();
4628 public override string ToString() {
4630 return "Tag starts at index " + this.start
+ "length " + this.length
+ " text: " + this.line
.Text
.Substring(this.start
-1, this.length
) + "Font " + this.font
.ToString();
4631 return "Zero Lengthed tag at index " + this.start
;
4634 #endregion // Internal Methods
4637 internal class UndoClass
{
4638 internal enum ActionType
{
4649 internal class Action
{
4650 internal ActionType type
;
4651 internal int line_no
;
4653 internal object data
;
4656 #region Local Variables
4657 private Document document
;
4658 private Stack undo_actions
;
4659 private Stack redo_actions
;
4660 private int caret_line
;
4661 private int caret_pos
;
4662 #endregion // Local Variables
4664 #region Constructors
4665 internal UndoClass(Document doc
) {
4667 undo_actions
= new Stack(50);
4668 redo_actions
= new Stack(50);
4670 #endregion // Constructors
4673 [MonoTODO("Change this to be configurable")]
4674 internal int UndoLevels
{
4676 return undo_actions
.Count
;
4680 [MonoTODO("Change this to be configurable")]
4681 internal int RedoLevels
{
4683 return redo_actions
.Count
;
4687 [MonoTODO("Come up with good naming and localization")]
4688 internal string UndoName
{
4692 action
= (Action
)undo_actions
.Peek();
4693 switch(action
.type
) {
4694 case ActionType
.InsertChar
: {
4695 Locale
.GetText("Insert character");
4699 case ActionType
.DeleteChar
: {
4700 Locale
.GetText("Delete character");
4704 case ActionType
.InsertString
: {
4705 Locale
.GetText("Insert string");
4709 case ActionType
.DeleteChars
: {
4710 Locale
.GetText("Delete string");
4714 case ActionType
.CursorMove
: {
4715 Locale
.GetText("Cursor move");
4723 internal string RedoName() {
4726 #endregion // Properties
4728 #region Internal Methods
4729 internal void Clear() {
4730 undo_actions
.Clear();
4731 redo_actions
.Clear();
4734 internal void Undo() {
4736 int compound_stack
= 0;
4738 if (undo_actions
.Count
== 0) {
4745 action
= (Action
)undo_actions
.Pop();
4747 // Put onto redo stack
4748 redo_actions
.Push(action
);
4751 switch(action
.type
) {
4752 case ActionType
.CompoundEnd
:
4756 case ActionType
.CompoundBegin
:
4760 case ActionType
.InsertString
:
4761 document
.DeleteMultiline (document
.GetLine (action
.line_no
),
4762 action
.pos
, ((string) action
.data
).Length
+ 1);
4765 case ActionType
.InsertChar
: {
4766 // FIXME - implement me
4770 case ActionType
.DeleteChars
: {
4771 this.Insert(document
.GetLine(action
.line_no
), action
.pos
, (Line
)action
.data
);
4772 Undo(); // Grab the cursor location
4776 case ActionType
.CursorMove
: {
4777 document
.caret
.line
= document
.GetLine(action
.line_no
);
4778 if (document
.caret
.line
== null) {
4783 document
.caret
.tag
= document
.caret
.line
.FindTag(action
.pos
);
4784 document
.caret
.pos
= action
.pos
;
4785 document
.caret
.height
= document
.caret
.tag
.height
;
4787 if (document
.owner
.IsHandleCreated
) {
4788 XplatUI
.DestroyCaret(document
.owner
.Handle
);
4789 XplatUI
.CreateCaret(document
.owner
.Handle
, 2, document
.caret
.height
);
4790 XplatUI
.SetCaretPos(document
.owner
.Handle
, (int)document
.caret
.tag
.line
.widths
[document
.caret
.pos
] + document
.caret
.line
.align_shift
- document
.viewport_x
, document
.caret
.line
.Y
+ document
.caret
.tag
.shift
- document
.viewport_y
+ Document
.caret_shift
);
4792 document
.DisplayCaret ();
4795 // FIXME - enable call
4796 //if (document.CaretMoved != null) document.CaretMoved(this, EventArgs.Empty);
4800 } while (compound_stack
> 0);
4803 internal void Redo() {
4804 if (redo_actions
.Count
== 0) {
4808 #endregion // Internal Methods
4810 #region Private Methods
4812 public void BeginCompoundAction ()
4814 Action cb
= new Action ();
4815 cb
.type
= ActionType
.CompoundBegin
;
4817 undo_actions
.Push (cb
);
4820 public void EndCompoundAction ()
4822 Action ce
= new Action ();
4823 ce
.type
= ActionType
.CompoundEnd
;
4825 undo_actions
.Push (ce
);
4829 public void RecordDeleteChars(Line line
, int pos
, int length
) {
4830 RecordDelete(line
, pos
, line
, pos
+ length
- 1);
4833 // start_pos, end_pos = 1 based
4834 public void RecordDelete(Line start_line
, int start_pos
, Line end_line
, int end_pos
) {
4838 l
= Duplicate(start_line
, start_pos
, end_line
, end_pos
);
4841 a
.type
= ActionType
.DeleteChars
;
4843 a
.line_no
= start_line
.line_no
;
4844 a
.pos
= start_pos
- 1;
4846 // Record the cursor position before, since the actions will occur in reverse order
4848 undo_actions
.Push(a
);
4851 public void RecordInsertString (Line line
, int pos
, string str
)
4853 Action a
= new Action ();
4855 a
.type
= ActionType
.InsertString
;
4857 a
.line_no
= line
.line_no
;
4860 undo_actions
.Push (a
);
4863 public void RecordCursor() {
4864 if (document
.caret
.line
== null) {
4868 RecordCursor(document
.caret
.line
, document
.caret
.pos
);
4871 public void RecordCursor(Line line
, int pos
) {
4874 if ((line
.line_no
== caret_line
) && (pos
== caret_pos
)) {
4878 caret_line
= line
.line_no
;
4882 a
.type
= ActionType
.CursorMove
;
4883 a
.line_no
= line
.line_no
;
4886 undo_actions
.Push(a
);
4889 // start_pos = 1-based
4890 // end_pos = 1-based
4891 public Line
Duplicate(Line start_line
, int start_pos
, Line end_line
, int end_pos
) {
4896 LineTag current_tag
;
4905 for (int i
= start_line
.line_no
; i
<= end_line
.line_no
; i
++) {
4906 current
= document
.GetLine(i
);
4908 if (start_line
.line_no
== i
) {
4914 if (end_line
.line_no
== i
) {
4917 end
= current
.text
.Length
;
4921 line
.text
= new StringBuilder(current
.text
.ToString(start
- 1, end
- start
+ 1));
4923 // Copy tags from start to start+length onto new line
4924 current_tag
= current
.FindTag(start
- 1);
4925 while ((current_tag
!= null) && (current_tag
.start
< end
)) {
4926 if ((current_tag
.start
<= start
) && (start
< (current_tag
.start
+ current_tag
.length
))) {
4927 // start tag is within this tag
4930 tag_start
= current_tag
.start
;
4933 if (end
< (current_tag
.start
+ current_tag
.length
)) {
4934 tag_length
= end
- tag_start
+ 1;
4936 tag_length
= current_tag
.start
+ current_tag
.length
- tag_start
;
4938 tag
= new LineTag(line
, tag_start
- start
+ 1, tag_length
);
4939 tag
.color
= current_tag
.color
;
4940 tag
.font
= current_tag
.font
;
4942 current_tag
= current_tag
.next
;
4944 // Add the new tag to the line
4945 if (line
.tags
== null) {
4951 while (tail
.next
!= null) {
4955 tag
.previous
= tail
;
4959 if ((i
+ 1) <= end_line
.line_no
) {
4960 line
.soft_break
= current
.soft_break
;
4962 // Chain them (we use right/left as next/previous)
4963 line
.right
= new Line();
4964 line
.right
.left
= line
;
4972 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
4973 internal void Insert(Line line
, int pos
, Line insert
) {
4980 // Handle special case first
4981 if (insert
.right
== null) {
4983 // Single line insert
4984 document
.Split(line
, pos
);
4986 if (insert
.tags
== null) {
4987 return; // Blank line
4990 //Insert our tags at the end
4993 while (tag
.next
!= null) {
4997 offset
= tag
.start
+ tag
.length
- 1;
4999 tag
.next
= insert
.tags
;
5000 line
.text
.Insert(offset
, insert
.text
.ToString());
5002 // Adjust start locations
5004 while (tag
!= null) {
5005 tag
.start
+= offset
;
5009 // Put it back together
5010 document
.Combine(line
.line_no
, line
.line_no
+ 1);
5011 document
.UpdateView(line
, pos
);
5018 while (current
!= null) {
5019 if (current
== insert
) {
5020 // Inserting the first line we split the line (and make space)
5021 document
.Split(line
, pos
);
5022 //Insert our tags at the end of the line
5026 while (tag
.next
!= null) {
5029 offset
= tag
.start
+ tag
.length
- 1;
5030 tag
.next
= current
.tags
;
5031 tag
.next
.previous
= tag
;
5037 line
.tags
= current
.tags
;
5038 line
.tags
.previous
= null;
5042 document
.Split(line
.line_no
, 0);
5044 line
.tags
= current
.tags
;
5045 line
.tags
.previous
= null;
5048 // Adjust start locations and line pointers
5049 while (tag
!= null) {
5050 tag
.start
+= offset
;
5055 line
.text
.Insert(offset
, current
.text
.ToString());
5056 line
.Grow(line
.text
.Length
);
5059 line
= document
.GetLine(line
.line_no
+ 1);
5061 // FIXME? Test undo of line-boundaries
5062 if ((current
.right
== null) && (current
.tags
.length
!= 0)) {
5063 document
.Combine(line
.line_no
- 1, line
.line_no
);
5065 current
= current
.right
;
5070 // Recalculate our document
5071 document
.UpdateView(first
, lines
, pos
);
5074 #endregion // Private Methods