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
&& !owner
.show_caret_w_selection
)
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
);
1465 if (owner
.IsHandleCreated
&& selection_visible
) {
1466 InvalidateSelectionArea ();
1470 internal void CaretLostFocus() {
1471 if (!owner
.IsHandleCreated
) {
1474 XplatUI
.DestroyCaret(owner
.Handle
);
1477 internal void AlignCaret() {
1478 if (!owner
.IsHandleCreated
) {
1482 undo
.RecordCursor();
1484 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1485 caret
.height
= caret
.tag
.height
;
1487 if (owner
.Focused
) {
1488 XplatUI
.CreateCaret(owner
.Handle
, caret_width
, caret
.height
);
1489 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
);
1493 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1496 internal void UpdateCaret() {
1497 if (!owner
.IsHandleCreated
|| caret
.tag
== null) {
1501 undo
.RecordCursor();
1503 if (caret
.tag
.height
!= caret
.height
) {
1504 caret
.height
= caret
.tag
.height
;
1505 if (owner
.Focused
) {
1506 XplatUI
.CreateCaret(owner
.Handle
, caret_width
, caret
.height
);
1510 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
);
1514 if (CaretMoved
!= null) CaretMoved(this, EventArgs
.Empty
);
1517 internal void DisplayCaret() {
1518 if (!owner
.IsHandleCreated
) {
1522 if (owner
.Focused
&& (!selection_visible
|| owner
.show_caret_w_selection
)) {
1523 XplatUI
.CaretVisible(owner
.Handle
, true);
1527 internal void HideCaret() {
1528 if (!owner
.IsHandleCreated
) {
1532 if (owner
.Focused
) {
1533 XplatUI
.CaretVisible(owner
.Handle
, false);
1537 internal void MoveCaret(CaretDirection direction
) {
1538 // FIXME should we use IsWordSeparator to detect whitespace, instead
1539 // of looking for actual spaces in the Word move cases?
1541 bool nowrap
= false;
1543 case CaretDirection
.CharForwardNoWrap
:
1545 goto case CaretDirection
.CharForward
;
1546 case CaretDirection
.CharForward
: {
1548 if (caret
.pos
> caret
.line
.text
.Length
) {
1549 if (multiline
&& !nowrap
) {
1550 // Go into next line
1551 if (caret
.line
.line_no
< this.lines
) {
1552 caret
.line
= GetLine(caret
.line
.line_no
+1);
1554 caret
.tag
= caret
.line
.tags
;
1559 // Single line; we stay where we are
1563 if ((caret
.tag
.start
- 1 + caret
.tag
.length
) < caret
.pos
) {
1564 caret
.tag
= caret
.tag
.next
;
1571 case CaretDirection
.CharBackNoWrap
:
1573 goto case CaretDirection
.CharBack
;
1574 case CaretDirection
.CharBack
: {
1575 if (caret
.pos
> 0) {
1576 // caret.pos--; // folded into the if below
1577 if (--caret
.pos
> 0) {
1578 if (caret
.tag
.start
> caret
.pos
) {
1579 caret
.tag
= caret
.tag
.previous
;
1583 if (caret
.line
.line_no
> 1 && !nowrap
) {
1584 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1585 caret
.pos
= caret
.line
.text
.Length
;
1586 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1593 case CaretDirection
.WordForward
: {
1596 len
= caret
.line
.text
.Length
;
1597 if (caret
.pos
< len
) {
1598 while ((caret
.pos
< len
) && (caret
.line
.text
[caret
.pos
] != ' ')) {
1601 if (caret
.pos
< len
) {
1602 // Skip any whitespace
1603 while ((caret
.pos
< len
) && (caret
.line
.text
[caret
.pos
] == ' ')) {
1607 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1609 if (caret
.line
.line_no
< this.lines
) {
1610 caret
.line
= GetLine(caret
.line
.line_no
+ 1);
1612 caret
.tag
= caret
.line
.tags
;
1619 case CaretDirection
.WordBack
: {
1620 if (caret
.pos
> 0) {
1623 while ((caret
.pos
> 0) && (caret
.line
.text
[caret
.pos
] == ' ')) {
1627 while ((caret
.pos
> 0) && (caret
.line
.text
[caret
.pos
] != ' ')) {
1631 if (caret
.line
.text
.ToString(caret
.pos
, 1) == " ") {
1632 if (caret
.pos
!= 0) {
1635 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1636 caret
.pos
= caret
.line
.text
.Length
;
1639 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1641 if (caret
.line
.line_no
> 1) {
1642 caret
.line
= GetLine(caret
.line
.line_no
- 1);
1643 caret
.pos
= caret
.line
.text
.Length
;
1644 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1651 case CaretDirection
.LineUp
: {
1652 if (caret
.line
.line_no
> 1) {
1655 pixel
= (int)caret
.line
.widths
[caret
.pos
];
1656 PositionCaret(pixel
, GetLine(caret
.line
.line_no
- 1).Y
);
1663 case CaretDirection
.LineDown
: {
1664 if (caret
.line
.line_no
< lines
) {
1667 pixel
= (int)caret
.line
.widths
[caret
.pos
];
1668 PositionCaret(pixel
, GetLine(caret
.line
.line_no
+ 1).Y
);
1675 case CaretDirection
.Home
: {
1676 if (caret
.pos
> 0) {
1678 caret
.tag
= caret
.line
.tags
;
1684 case CaretDirection
.End
: {
1685 if (caret
.pos
< caret
.line
.text
.Length
) {
1686 caret
.pos
= caret
.line
.text
.Length
;
1687 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1693 case CaretDirection
.PgUp
: {
1695 int new_y
, y_offset
;
1697 if (viewport_y
== 0) {
1699 // This should probably be handled elsewhere
1700 if (!(owner
is RichTextBox
)) {
1701 // Page down doesn't do anything in a regular TextBox
1702 // if the bottom of the document
1703 // is already visible, the page and the caret stay still
1707 // We're just placing the caret at the end of the document, no scrolling needed
1708 owner
.vscroll
.Value
= 0;
1709 Line line
= GetLine (1);
1710 PositionCaret (line
, 0);
1713 y_offset
= caret
.line
.Y
- viewport_y
;
1714 new_y
= caret
.line
.Y
- viewport_height
;
1716 owner
.vscroll
.Value
= Math
.Max (new_y
, 0);
1717 PositionCaret ((int)caret
.line
.widths
[caret
.pos
], y_offset
+ viewport_y
);
1721 case CaretDirection
.PgDn
: {
1722 int new_y
, y_offset
;
1724 if ((viewport_y
+ viewport_height
) > document_y
) {
1726 // This should probably be handled elsewhere
1727 if (!(owner
is RichTextBox
)) {
1728 // Page up doesn't do anything in a regular TextBox
1729 // if the bottom of the document
1730 // is already visible, the page and the caret stay still
1734 // We're just placing the caret at the end of the document, no scrolling needed
1735 owner
.vscroll
.Value
= owner
.vscroll
.Maximum
- viewport_height
+ 1;
1736 Line line
= GetLine (lines
);
1737 PositionCaret (line
, line
.Text
.Length
);
1740 y_offset
= caret
.line
.Y
- viewport_y
;
1741 new_y
= caret
.line
.Y
+ viewport_height
;
1743 owner
.vscroll
.Value
= Math
.Min (new_y
, owner
.vscroll
.Maximum
- viewport_height
+ 1);
1744 PositionCaret ((int)caret
.line
.widths
[caret
.pos
], y_offset
+ viewport_y
);
1749 case CaretDirection
.CtrlPgUp
: {
1750 PositionCaret(0, viewport_y
);
1755 case CaretDirection
.CtrlPgDn
: {
1760 tag
= FindTag(0, viewport_y
+ viewport_height
, out index
, false);
1761 if (tag
.line
.line_no
> 1) {
1762 line
= GetLine(tag
.line
.line_no
- 1);
1766 PositionCaret(line
, line
.Text
.Length
);
1771 case CaretDirection
.CtrlHome
: {
1772 caret
.line
= GetLine(1);
1774 caret
.tag
= caret
.line
.tags
;
1780 case CaretDirection
.CtrlEnd
: {
1781 caret
.line
= GetLine(lines
);
1782 caret
.pos
= caret
.line
.text
.Length
;
1783 caret
.tag
= LineTag
.FindTag(caret
.line
, caret
.pos
);
1789 case CaretDirection
.SelectionStart
: {
1790 caret
.line
= selection_start
.line
;
1791 caret
.pos
= selection_start
.pos
;
1792 caret
.tag
= selection_start
.tag
;
1798 case CaretDirection
.SelectionEnd
: {
1799 caret
.line
= selection_end
.line
;
1800 caret
.pos
= selection_end
.pos
;
1801 caret
.tag
= selection_end
.tag
;
1809 // Draw the document
1810 internal void Draw(Graphics g
, Rectangle clip
) {
1811 Line line
; // Current line being drawn
1812 LineTag tag
; // Current tag being drawn
1813 int start
; // First line to draw
1814 int end
; // Last line to draw
1815 StringBuilder text
; // String representing the current line
1821 // First, figure out from what line to what line we need to draw
1822 start
= GetLineByPixel(clip
.Top
+ viewport_y
, false).line_no
;
1823 end
= GetLineByPixel(clip
.Bottom
+ viewport_y
, false).line_no
;
1824 //Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
1826 // Now draw our elements; try to only draw those that are visible
1830 DateTime n
= DateTime
.Now
;
1831 Console
.WriteLine("Started drawing: {0}s {1}ms", n
.Second
, n
.Millisecond
);
1834 disabled
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorGrayText
);
1835 hilight
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorHighlight
);
1836 hilight_text
= ThemeEngine
.Current
.ResPool
.GetSolidBrush(ThemeEngine
.Current
.ColorHighlightText
);
1838 while (line_no
<= end
) {
1839 line
= GetLine(line_no
);
1841 if (owner
.backcolor_set
|| (owner
.Enabled
&& !owner
.read_only
)) {
1842 g
.FillRectangle(ThemeEngine
.Current
.ResPool
.GetSolidBrush(owner
.BackColor
), new Rectangle(clip
.Left
, line
.Y
- viewport_y
, clip
.Width
, line
.Y
- viewport_y
+ line
.Height
));
1844 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
));
1853 // This fails if there's a password > 1024 chars...
1854 text
= this.password_cache
;
1856 while (tag
!= null) {
1857 if (tag
.length
== 0) {
1862 if (((tag
.X
+ tag
.width
) > (clip
.Left
- viewport_x
)) || (tag
.X
< (clip
.Right
- viewport_x
))) {
1863 // Check for selection
1864 if ((!selection_visible
) || (!owner
.ShowSelection
) || (line_no
< selection_start
.line
.line_no
) || (line_no
> selection_end
.line
.line_no
)) {
1865 // regular drawing, no selection to deal with
1866 //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);
1867 if (owner
.is_enabled
) {
1868 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
);
1873 a
= ((SolidBrush
)tag
.color
).Color
;
1874 b
= ThemeEngine
.Current
.ColorWindowText
;
1876 if ((a
.R
== b
.R
) && (a
.G
== b
.G
) && (a
.B
== b
.B
)) {
1877 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
);
1879 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
);
1883 // we might have to draw our selection
1884 if ((line_no
!= selection_start
.line
.line_no
) && (line_no
!= selection_end
.line
.line_no
)) {
1885 // Special case, whole line is selected, draw this tag selected
1888 tag
.X
+ line
.align_shift
- viewport_x
, // X
1889 line
.Y
+ tag
.shift
- viewport_y
, // Y
1890 line
.widths
[tag
.start
+ tag
.length
- 1], // width
1891 tag
.height
// Height
1895 //s.Substring(tag.start-1, tag.length), // String
1896 text
.ToString(tag
.start
-1, tag
.length
), // String
1898 hilight_text
, // Brush
1899 tag
.X
+ line
.align_shift
- viewport_x
, // X
1900 line
.Y
+ tag
.shift
- viewport_y
, // Y
1901 StringFormat
.GenericTypographic
);
1909 // One or more, but not all tags on the line are selected
1910 if ((selection_start
.tag
== tag
) && (selection_end
.tag
== tag
)) {
1911 // Single tag selected, draw "normalSELECTEDnormal"
1913 // First, the regular part
1915 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1916 text
.ToString(tag
.start
- 1, selection_start
.pos
- tag
.start
+ 1), // String
1919 tag
.X
+ line
.align_shift
- viewport_x
, // X
1920 line
.Y
+ tag
.shift
- viewport_y
, // Y
1921 StringFormat
.GenericTypographic
);
1923 // Now the highlight
1926 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1927 line
.Y
+ tag
.shift
- viewport_y
, // Y
1928 line
.widths
[selection_end
.pos
] - line
.widths
[selection_start
.pos
], // Width
1929 tag
.height
); // Height
1932 //s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), // String
1933 text
.ToString(selection_start
.pos
, selection_end
.pos
- selection_start
.pos
), // String
1935 hilight_text
, // Brush
1936 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1937 line
.Y
+ tag
.shift
- viewport_y
, // Y
1938 StringFormat
.GenericTypographic
);
1940 // And back to the regular
1942 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1943 text
.ToString(selection_end
.pos
, tag
.start
+ tag
.length
- selection_end
.pos
- 1), // String
1946 line
.widths
[selection_end
.pos
] + line
.align_shift
- viewport_x
, // X
1947 line
.Y
+ tag
.shift
- viewport_y
, // Y
1948 StringFormat
.GenericTypographic
);
1950 } else if (selection_start
.tag
== tag
) {
1953 // The highlighted part
1956 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
,
1957 line
.Y
+ tag
.shift
- viewport_y
,
1958 line
.widths
[tag
.start
+ tag
.length
- 1] - line
.widths
[selection_start
.pos
],
1962 //s.Substring(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
1963 text
.ToString(selection_start
.pos
, tag
.start
+ tag
.length
- selection_start
.pos
- 1), // String
1965 hilight_text
, // Brush
1966 line
.widths
[selection_start
.pos
] + line
.align_shift
- viewport_x
, // X
1967 line
.Y
+ tag
.shift
- viewport_y
, // Y
1968 StringFormat
.GenericTypographic
);
1972 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1973 text
.ToString(tag
.start
- 1, selection_start
.pos
- tag
.start
+ 1), // String
1976 tag
.X
+ line
.align_shift
- viewport_x
, // X
1977 line
.Y
+ tag
.shift
- viewport_y
, // Y
1978 StringFormat
.GenericTypographic
);
1979 } else if (selection_end
.tag
== tag
) {
1982 // The highlighted part
1985 tag
.X
+ line
.align_shift
- viewport_x
,
1986 line
.Y
+ tag
.shift
- viewport_y
,
1987 line
.widths
[selection_end
.pos
] - line
.widths
[tag
.start
- 1],
1991 //s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), // String
1992 text
.ToString(tag
.start
- 1, selection_end
.pos
- tag
.start
+ 1), // String
1994 hilight_text
, // Brush
1995 tag
.X
+ line
.align_shift
- viewport_x
, // X
1996 line
.Y
+ tag
.shift
- viewport_y
, // Y
1997 StringFormat
.GenericTypographic
);
2001 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
2002 text
.ToString(selection_end
.pos
, tag
.start
+ tag
.length
- selection_end
.pos
- 1), // String
2005 line
.widths
[selection_end
.pos
] + line
.align_shift
- viewport_x
, // X
2006 line
.Y
+ tag
.shift
- viewport_y
, // Y
2007 StringFormat
.GenericTypographic
);
2009 // no partially selected tags here, simple checks...
2010 if (selection_start
.line
== line
) {
2014 begin
= tag
.start
- 1;
2015 stop
= tag
.start
+ tag
.length
- 1;
2016 if (selection_end
.line
== line
) {
2017 if ((begin
>= selection_start
.pos
) && (stop
< selection_end
.pos
)) {
2021 if (stop
> selection_start
.pos
) {
2025 } else if (selection_end
.line
== line
) {
2026 if ((tag
.start
- 1) < selection_end
.pos
) {
2036 tag
.X
+ line
.align_shift
- viewport_x
,
2037 line
.Y
+ tag
.shift
- viewport_y
,
2038 line
.widths
[tag
.start
+ tag
.length
- 1] - line
.widths
[tag
.start
- 1],
2042 //s.Substring(tag.start-1, tag.length), // String
2043 text
.ToString(tag
.start
-1, tag
.length
), // String
2045 hilight_text
, // Brush
2046 tag
.X
+ line
.align_shift
- viewport_x
, // X
2047 line
.Y
+ tag
.shift
- viewport_y
, // Y
2048 StringFormat
.GenericTypographic
);
2051 //s.Substring(tag.start-1, tag.length), // String
2052 text
.ToString(tag
.start
-1, tag
.length
), // String
2055 tag
.X
+ line
.align_shift
- viewport_x
, // X
2056 line
.Y
+ tag
.shift
- viewport_y
, // Y
2057 StringFormat
.GenericTypographic
);
2072 Console
.WriteLine("Finished drawing: {0}s {1}ms", n
.Second
, n
.Millisecond
);
2077 private void InsertLineString (Line line
, int pos
, string s
)
2079 bool carriage_return
= false;
2081 if (s
.EndsWith ("\r")) {
2082 s
= s
.Substring (0, s
.Length
- 1);
2083 carriage_return
= true;
2086 InsertString (line
, pos
, s
);
2088 if (carriage_return
) {
2089 Line l
= GetLine (line
.line_no
);
2090 l
.carriage_return
= true;
2094 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
2095 internal void Insert(Line line
, int pos
, bool update_caret
, string s
) {
2100 LineTag tag
= LineTag
.FindTag (line
, pos
);
2103 undo
.BeginCompoundAction ();
2105 base_line
= line
.line_no
;
2106 old_line_count
= lines
;
2108 break_index
= s
.IndexOf ('\n');
2110 // Bump the text at insertion point a line down if we're inserting more than one line
2111 if (break_index
> -1) {
2113 line
.soft_break
= false;
2114 // Remainder of start line is now in base_line + 1
2117 if (break_index
== -1)
2118 break_index
= s
.Length
;
2120 InsertLineString (line
, pos
, s
.Substring (0, break_index
));
2123 while (break_index
< s
.Length
) {
2125 int next_break
= s
.IndexOf ('\n', break_index
);
2126 int adjusted_next_break
;
2127 bool carriage_return
= false;
2129 if (next_break
== -1) {
2130 next_break
= s
.Length
;
2134 adjusted_next_break
= next_break
;
2135 if (s
[next_break
- 1] == '\r') {
2136 adjusted_next_break
--;
2137 carriage_return
= true;
2140 string line_text
= s
.Substring (break_index
, adjusted_next_break
- break_index
);
2141 Add (base_line
+ count
, line_text
, line
.alignment
, tag
.font
, tag
.color
);
2143 if (carriage_return
) {
2144 Line last
= GetLine (base_line
+ count
);
2145 last
.carriage_return
= true;
2148 last
.soft_break
= true;
2150 Line last
= GetLine (base_line
+ count
);
2151 last
.soft_break
= true;
2155 break_index
= next_break
+ 1;
2160 UpdateView(line
, lines
- old_line_count
+ 1, pos
);
2163 // Move caret to the end of the inserted text
2164 Line l
= GetLine (line
.line_no
+ lines
- old_line_count
);
2165 PositionCaret(l
, l
.text
.Length
);
2169 undo
.EndCompoundAction ();
2172 // Inserts a character at the given position
2173 internal void InsertString(Line line
, int pos
, string s
) {
2174 InsertString(line
.FindTag(pos
), pos
, s
);
2177 // Inserts a string at the given position
2178 internal void InsertString(LineTag tag
, int pos
, string s
) {
2187 line
.text
.Insert(pos
, s
);
2190 // TODO: sometimes getting a null tag here when pasting ???
2192 while (tag
!= null) {
2199 UpdateView(line
, pos
);
2202 // Inserts a string at the caret position
2203 internal void InsertStringAtCaret(string s
, bool move_caret
) {
2211 caret
.line
.text
.Insert(caret
.pos
, s
);
2212 caret
.tag
.length
+= len
;
2214 if (caret
.tag
.next
!= null) {
2215 tag
= caret
.tag
.next
;
2216 while (tag
!= null) {
2221 caret
.line
.Grow(len
);
2222 caret
.line
.recalc
= true;
2224 UpdateView(caret
.line
, caret
.pos
);
2233 // Inserts a character at the given position
2234 internal void InsertChar(Line line
, int pos
, char ch
) {
2235 InsertChar(line
.FindTag(pos
), pos
, ch
);
2238 // Inserts a character at the given position
2239 internal void InsertChar(LineTag tag
, int pos
, char ch
) {
2245 line
.text
.Insert(pos
, ch
);
2249 while (tag
!= null) {
2256 UpdateView(line
, pos
);
2259 // Inserts a character at the current caret position
2260 internal void InsertCharAtCaret(char ch
, bool move_caret
) {
2265 caret
.line
.text
.Insert(caret
.pos
, ch
);
2268 if (caret
.tag
.next
!= null) {
2269 tag
= caret
.tag
.next
;
2270 while (tag
!= null) {
2276 caret
.line
.recalc
= true;
2278 UpdateView(caret
.line
, caret
.pos
);
2282 SetSelectionToCaret(true);
2286 internal void DeleteMultiline (Line start_line
, int pos
, int length
)
2288 Marker start
= new Marker ();
2289 Marker end
= new Marker ();
2290 int start_index
= LineTagToCharIndex (start_line
, pos
);
2292 start
.line
= start_line
;
2294 start
.tag
= LineTag
.FindTag (start_line
, pos
);
2296 CharIndexToLineTag (start_index
+ length
, out end
.line
,
2297 out end
.tag
, out end
.pos
);
2299 if (start
.line
== end
.line
) {
2300 DeleteChars (start
.tag
, pos
, end
.pos
- pos
);
2303 // Delete first and last lines
2304 DeleteChars (start
.tag
, start
.pos
, start
.line
.text
.Length
- start
.pos
);
2305 DeleteChars (end
.line
.tags
, 0, end
.pos
);
2307 int current
= start
.line
.line_no
+ 1;
2308 if (current
< end
.line
.line_no
) {
2309 for (int i
= end
.line
.line_no
- 1; i
>= current
; i
--) {
2314 // BIG FAT WARNING - selection_end.line might be stale due
2315 // to the above Delete() call. DONT USE IT before hitting the end of this method!
2317 // Join start and end
2318 Combine (start
.line
.line_no
, current
);
2323 // Deletes n characters at the given position; it will not delete past line limits
2325 internal void DeleteChars(LineTag tag
, int pos
, int count
) {
2334 if (pos
== line
.text
.Length
) {
2338 line
.text
.Remove(pos
, count
);
2340 // Make sure the tag points to the right spot
2341 while ((tag
!= null) && (tag
.start
+ tag
.length
- 1) <= pos
) {
2349 // Check if we're crossing tag boundaries
2350 if ((pos
+ count
) > (tag
.start
+ tag
.length
- 1)) {
2353 // We have to delete cross tag boundaries
2357 left
-= tag
.start
+ tag
.length
- pos
- 1;
2358 tag
.length
-= tag
.start
+ tag
.length
- pos
- 1;
2361 while ((tag
!= null) && (left
> 0)) {
2362 tag
.start
-= count
- left
;
2363 if (tag
.length
> left
) {
2374 // We got off easy, same tag
2376 tag
.length
-= count
;
2378 if (tag
.length
== 0) {
2383 // Delete empty orphaned tags at the end
2385 while (walk
!= null && walk
.next
!= null && walk
.next
.length
== 0) {
2387 walk
.next
= walk
.next
.next
;
2388 if (walk
.next
!= null)
2389 walk
.next
.previous
= t
;
2393 // Adjust the start point of any tags following
2396 while (tag
!= null) {
2404 line
.Streamline(lines
);
2407 UpdateView(line
, pos
);
2410 // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
2411 internal void DeleteChar(LineTag tag
, int pos
, bool forward
) {
2420 if ((pos
== 0 && forward
== false) || (pos
== line
.text
.Length
&& forward
== true)) {
2426 line
.text
.Remove(pos
, 1);
2428 while ((tag
!= null) && (tag
.start
+ tag
.length
- 1) <= pos
) {
2438 if (tag
.length
== 0) {
2443 line
.text
.Remove(pos
, 1);
2444 if (pos
>= (tag
.start
- 1)) {
2446 if (tag
.length
== 0) {
2449 } else if (tag
.previous
!= null) {
2450 tag
.previous
.length
--;
2451 if (tag
.previous
.length
== 0) {
2457 // Delete empty orphaned tags at the end
2459 while (walk
!= null && walk
.next
!= null && walk
.next
.length
== 0) {
2461 walk
.next
= walk
.next
.next
;
2462 if (walk
.next
!= null)
2463 walk
.next
.previous
= t
;
2468 while (tag
!= null) {
2474 line
.Streamline(lines
);
2477 UpdateView(line
, pos
);
2480 // Combine two lines
2481 internal void Combine(int FirstLine
, int SecondLine
) {
2482 Combine(GetLine(FirstLine
), GetLine(SecondLine
));
2485 internal void Combine(Line first
, Line second
) {
2489 // Combine the two tag chains into one
2492 // Maintain the line ending style
2493 first
.soft_break
= second
.soft_break
;
2495 while (last
.next
!= null) {
2499 last
.next
= second
.tags
;
2500 last
.next
.previous
= last
;
2502 shift
= last
.start
+ last
.length
- 1;
2504 // Fix up references within the chain
2506 while (last
!= null) {
2508 last
.start
+= shift
;
2512 // Combine both lines' strings
2513 first
.text
.Insert(first
.text
.Length
, second
.text
.ToString());
2514 first
.Grow(first
.text
.Length
);
2516 // Remove the reference to our (now combined) tags from the doomed line
2520 DecrementLines(first
.line_no
+ 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
2523 first
.recalc
= true;
2524 first
.height
= 0; // This forces RecalcDocument/UpdateView to redraw from this line on
2525 first
.Streamline(lines
);
2527 // Update Caret, Selection, etc
2528 if (caret
.line
== second
) {
2529 caret
.Combine(first
, shift
);
2531 if (selection_anchor
.line
== second
) {
2532 selection_anchor
.Combine(first
, shift
);
2534 if (selection_start
.line
== second
) {
2535 selection_start
.Combine(first
, shift
);
2537 if (selection_end
.line
== second
) {
2538 selection_end
.Combine(first
, shift
);
2545 check_first
= GetLine(first
.line_no
);
2546 check_second
= GetLine(check_first
.line_no
+ 1);
2548 Console
.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first
.Y
, check_second
.Y
);
2551 this.Delete(second
);
2554 check_first
= GetLine(first
.line_no
);
2555 check_second
= GetLine(check_first
.line_no
+ 1);
2557 Console
.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first
.Y
, check_second
.Y
);
2561 // Split the line at the position into two
2562 internal void Split(int LineNo
, int pos
) {
2566 line
= GetLine(LineNo
);
2567 tag
= LineTag
.FindTag(line
, pos
);
2568 Split(line
, tag
, pos
, false);
2571 internal void Split(Line line
, int pos
) {
2574 tag
= LineTag
.FindTag(line
, pos
);
2575 Split(line
, tag
, pos
, false);
2578 ///<summary>Split line at given tag and position into two lines</summary>
2579 ///<param name="soft">True if the split should be marked as 'soft', indicating that it can be contracted
2580 ///if more space becomes available on previous line</param>
2581 internal void Split(Line line
, LineTag tag
, int pos
, bool soft
) {
2585 bool move_sel_start
;
2589 move_sel_start
= false;
2590 move_sel_end
= false;
2592 // Adjust selection and cursors
2593 if (soft
&& (caret
.line
== line
) && (caret
.pos
>= pos
)) {
2596 if (selection_start
.line
== line
&& selection_start
.pos
> pos
) {
2597 move_sel_start
= true;
2600 if (selection_end
.line
== line
&& selection_end
.pos
> pos
) {
2601 move_sel_end
= true;
2604 // cover the easy case first
2605 if (pos
== line
.text
.Length
) {
2606 Add(line
.line_no
+ 1, "", line
.alignment
, tag
.font
, tag
.color
);
2608 new_line
= GetLine(line
.line_no
+ 1);
2610 line
.carriage_return
= false;
2611 new_line
.carriage_return
= line
.carriage_return
;
2615 caret
.line
= new_line
;
2616 caret
.line
.soft_break
= true;
2617 caret
.tag
= new_line
.tags
;
2620 new_line
.soft_break
= true;
2624 if (move_sel_start
) {
2625 selection_start
.line
= new_line
;
2626 selection_start
.pos
= 0;
2627 selection_start
.tag
= new_line
.tags
;
2631 selection_end
.line
= new_line
;
2632 selection_end
.pos
= 0;
2633 selection_end
.tag
= new_line
.tags
;
2638 // We need to move the rest of the text into the new line
2639 Add(line
.line_no
+ 1, line
.text
.ToString(pos
, line
.text
.Length
- pos
), line
.alignment
, tag
.font
, tag
.color
);
2641 // Now transfer our tags from this line to the next
2642 new_line
= GetLine(line
.line_no
+ 1);
2644 line
.carriage_return
= false;
2645 new_line
.carriage_return
= line
.carriage_return
;
2648 new_line
.recalc
= true;
2650 if ((tag
.start
- 1) == pos
) {
2653 // We can simply break the chain and move the tag into the next line
2654 if (tag
== line
.tags
) {
2655 new_tag
= new LineTag(line
, 1, 0);
2656 new_tag
.font
= tag
.font
;
2657 new_tag
.color
= tag
.color
;
2658 line
.tags
= new_tag
;
2661 if (tag
.previous
!= null) {
2662 tag
.previous
.next
= null;
2664 new_line
.tags
= tag
;
2665 tag
.previous
= null;
2666 tag
.line
= new_line
;
2668 // Walk the list and correct the start location of the tags we just bumped into the next line
2669 shift
= tag
.start
- 1;
2672 while (new_tag
!= null) {
2673 new_tag
.start
-= shift
;
2674 new_tag
.line
= new_line
;
2675 new_tag
= new_tag
.next
;
2680 new_tag
= new LineTag(new_line
, 1, tag
.start
- 1 + tag
.length
- pos
);
2681 new_tag
.next
= tag
.next
;
2682 new_tag
.font
= tag
.font
;
2683 new_tag
.color
= tag
.color
;
2684 new_line
.tags
= new_tag
;
2685 if (new_tag
.next
!= null) {
2686 new_tag
.next
.previous
= new_tag
;
2689 tag
.length
= pos
- tag
.start
+ 1;
2692 new_tag
= new_tag
.next
;
2693 while (new_tag
!= null) {
2694 new_tag
.start
-= shift
;
2695 new_tag
.line
= new_line
;
2696 new_tag
= new_tag
.next
;
2703 caret
.line
= new_line
;
2704 caret
.pos
= caret
.pos
- pos
;
2705 caret
.tag
= caret
.line
.FindTag(caret
.pos
);
2707 new_line
.soft_break
= true;
2710 if (move_sel_start
) {
2711 selection_start
.line
= new_line
;
2712 selection_start
.pos
= selection_start
.pos
- pos
;
2713 selection_start
.tag
= new_line
.FindTag(selection_start
.pos
);
2717 selection_end
.line
= new_line
;
2718 selection_end
.pos
= selection_end
.pos
- pos
;
2719 selection_end
.tag
= new_line
.FindTag(selection_end
.pos
);
2722 CharCount
-= line
.text
.Length
- pos
;
2723 line
.text
.Remove(pos
, line
.text
.Length
- pos
);
2726 // Adds a line of text, with given font.
2727 // Bumps any line at that line number that already exists down
2728 internal void Add(int LineNo
, string Text
, Font font
, Brush color
) {
2729 Add(LineNo
, Text
, HorizontalAlignment
.Left
, font
, color
);
2732 internal void Add(int LineNo
, string Text
, HorizontalAlignment align
, Font font
, Brush color
) {
2737 CharCount
+= Text
.Length
;
2739 if (LineNo
<1 || Text
== null) {
2741 throw new ArgumentNullException("LineNo", "Line numbers must be positive");
2743 throw new ArgumentNullException("Text", "Cannot insert NULL line");
2747 add = new Line(LineNo
, Text
, align
, font
, color
);
2750 while (line
!= sentinel
) {
2752 line_no
= line
.line_no
;
2754 if (LineNo
> line_no
) {
2756 } else if (LineNo
< line_no
) {
2759 // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
2760 IncrementLines(line
.line_no
);
2765 add.left
= sentinel
;
2766 add.right
= sentinel
;
2768 if (add.parent
!= null) {
2769 if (LineNo
> add.parent
.line_no
) {
2770 add.parent
.right
= add;
2772 add.parent
.left
= add;
2779 RebalanceAfterAdd(add);
2784 internal virtual void Clear() {
2787 document
= sentinel
;
2790 public virtual object Clone() {
2793 clone
= new Document(null);
2795 clone
.lines
= this.lines
;
2796 clone
.document
= (Line
)document
.Clone();
2801 internal void Delete(int LineNo
) {
2808 line
= GetLine(LineNo
);
2810 CharCount
-= line
.text
.Length
;
2812 DecrementLines(LineNo
+ 1);
2816 internal void Delete(Line line1
) {
2817 Line line2
;// = new Line();
2820 if ((line1
.left
== sentinel
) || (line1
.right
== sentinel
)) {
2823 line3
= line1
.right
;
2824 while (line3
.left
!= sentinel
) {
2829 if (line3
.left
!= sentinel
) {
2832 line2
= line3
.right
;
2835 line2
.parent
= line3
.parent
;
2836 if (line3
.parent
!= null) {
2837 if(line3
== line3
.parent
.left
) {
2838 line3
.parent
.left
= line2
;
2840 line3
.parent
.right
= line2
;
2846 if (line3
!= line1
) {
2849 if (selection_start
.line
== line3
) {
2850 selection_start
.line
= line1
;
2853 if (selection_end
.line
== line3
) {
2854 selection_end
.line
= line1
;
2857 if (selection_anchor
.line
== line3
) {
2858 selection_anchor
.line
= line1
;
2861 if (caret
.line
== line3
) {
2866 line1
.alignment
= line3
.alignment
;
2867 line1
.ascent
= line3
.ascent
;
2868 line1
.hanging_indent
= line3
.hanging_indent
;
2869 line1
.height
= line3
.height
;
2870 line1
.indent
= line3
.indent
;
2871 line1
.line_no
= line3
.line_no
;
2872 line1
.recalc
= line3
.recalc
;
2873 line1
.right_indent
= line3
.right_indent
;
2874 line1
.soft_break
= line3
.soft_break
;
2875 line1
.space
= line3
.space
;
2876 line1
.tags
= line3
.tags
;
2877 line1
.text
= line3
.text
;
2878 line1
.widths
= line3
.widths
;
2882 while (tag
!= null) {
2888 if (line3
.color
== LineColor
.Black
)
2889 RebalanceAfterDelete(line2
);
2894 // Invalidate a section of the document to trigger redraw
2895 internal void Invalidate(Line start
, int start_pos
, Line end
, int end_pos
) {
2901 if ((start
== end
) && (start_pos
== end_pos
)) {
2905 if (end_pos
== -1) {
2906 end_pos
= end
.text
.Length
;
2909 // figure out what's before what so the logic below is straightforward
2910 if (start
.line_no
< end
.line_no
) {
2916 } else if (start
.line_no
> end
.line_no
) {
2923 if (start_pos
< end_pos
) {
2938 Console
.WriteLine("Invaliding from {0}:{1} to {2}:{3}", l1
.line_no
, p1
, l2
.line_no
, p2
);
2943 (int)l1
.widths
[p1
] + l1
.align_shift
- viewport_x
,
2945 (int)l2
.widths
[p2
] - (int)l1
.widths
[p1
] + 1,
2953 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
);
2956 // Three invalidates:
2957 // First line from start
2958 owner
.Invalidate(new Rectangle((int)l1
.widths
[p1
] + l1
.align_shift
- viewport_x
, l1
.Y
- viewport_y
, viewport_width
, l1
.height
));
2961 if ((l1
.line_no
+ 1) < l2
.line_no
) {
2964 y
= GetLine(l1
.line_no
+ 1).Y
;
2965 owner
.Invalidate(new Rectangle(0, y
- viewport_y
, viewport_width
, GetLine(l2
.line_no
).Y
- y
- viewport_y
));
2968 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
);
2973 owner
.Invalidate(new Rectangle((int)l2
.widths
[0] + l2
.align_shift
- viewport_x
, l2
.Y
- viewport_y
, (int)l2
.widths
[p2
] + 1, l2
.height
));
2975 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
);
2979 /// <summary>Select text around caret</summary>
2980 internal void ExpandSelection(CaretSelection mode
, bool to_caret
) {
2982 // We're expanding the selection to the caret position
2984 case CaretSelection
.Line
: {
2985 // Invalidate the selection delta
2986 if (caret
> selection_prev
) {
2987 Invalidate(selection_prev
.line
, 0, caret
.line
, caret
.line
.text
.Length
);
2989 Invalidate(selection_prev
.line
, selection_prev
.line
.text
.Length
, caret
.line
, 0);
2992 if (caret
.line
.line_no
<= selection_anchor
.line
.line_no
) {
2993 selection_start
.line
= caret
.line
;
2994 selection_start
.tag
= caret
.line
.tags
;
2995 selection_start
.pos
= 0;
2997 selection_end
.line
= selection_anchor
.line
;
2998 selection_end
.tag
= selection_anchor
.tag
;
2999 selection_end
.pos
= selection_anchor
.pos
;
3001 selection_end_anchor
= true;
3003 selection_start
.line
= selection_anchor
.line
;
3004 selection_start
.pos
= selection_anchor
.height
;
3005 selection_start
.tag
= selection_anchor
.line
.FindTag(selection_anchor
.height
);
3007 selection_end
.line
= caret
.line
;
3008 selection_end
.tag
= caret
.line
.tags
;
3009 selection_end
.pos
= caret
.line
.text
.Length
;
3011 selection_end_anchor
= false;
3013 selection_prev
.line
= caret
.line
;
3014 selection_prev
.tag
= caret
.tag
;
3015 selection_prev
.pos
= caret
.pos
;
3020 case CaretSelection
.Word
: {
3024 start_pos
= FindWordSeparator(caret
.line
, caret
.pos
, false);
3025 end_pos
= FindWordSeparator(caret
.line
, caret
.pos
, true);
3028 // Invalidate the selection delta
3029 if (caret
> selection_prev
) {
3030 Invalidate(selection_prev
.line
, selection_prev
.pos
, caret
.line
, end_pos
);
3032 Invalidate(selection_prev
.line
, selection_prev
.pos
, caret
.line
, start_pos
);
3034 if (caret
< selection_anchor
) {
3035 selection_start
.line
= caret
.line
;
3036 selection_start
.tag
= caret
.line
.FindTag(start_pos
);
3037 selection_start
.pos
= start_pos
;
3039 selection_end
.line
= selection_anchor
.line
;
3040 selection_end
.tag
= selection_anchor
.tag
;
3041 selection_end
.pos
= selection_anchor
.pos
;
3043 selection_prev
.line
= caret
.line
;
3044 selection_prev
.tag
= caret
.tag
;
3045 selection_prev
.pos
= start_pos
;
3047 selection_end_anchor
= true;
3049 selection_start
.line
= selection_anchor
.line
;
3050 selection_start
.pos
= selection_anchor
.height
;
3051 selection_start
.tag
= selection_anchor
.line
.FindTag(selection_anchor
.height
);
3053 selection_end
.line
= caret
.line
;
3054 selection_end
.tag
= caret
.line
.FindTag(end_pos
);
3055 selection_end
.pos
= end_pos
;
3057 selection_prev
.line
= caret
.line
;
3058 selection_prev
.tag
= caret
.tag
;
3059 selection_prev
.pos
= end_pos
;
3061 selection_end_anchor
= false;
3066 case CaretSelection
.Position
: {
3067 SetSelectionToCaret(false);
3072 // We're setting the selection 'around' the caret position
3074 case CaretSelection
.Line
: {
3075 this.Invalidate(caret
.line
, 0, caret
.line
, caret
.line
.text
.Length
);
3077 selection_start
.line
= caret
.line
;
3078 selection_start
.tag
= caret
.line
.tags
;
3079 selection_start
.pos
= 0;
3081 selection_end
.line
= caret
.line
;
3082 selection_end
.pos
= caret
.line
.text
.Length
;
3083 selection_end
.tag
= caret
.line
.FindTag(selection_end
.pos
);
3085 selection_anchor
.line
= selection_end
.line
;
3086 selection_anchor
.tag
= selection_end
.tag
;
3087 selection_anchor
.pos
= selection_end
.pos
;
3088 selection_anchor
.height
= 0;
3090 selection_prev
.line
= caret
.line
;
3091 selection_prev
.tag
= caret
.tag
;
3092 selection_prev
.pos
= caret
.pos
;
3094 this.selection_end_anchor
= true;
3099 case CaretSelection
.Word
: {
3103 start_pos
= FindWordSeparator(caret
.line
, caret
.pos
, false);
3104 end_pos
= FindWordSeparator(caret
.line
, caret
.pos
, true);
3106 this.Invalidate(selection_start
.line
, start_pos
, caret
.line
, end_pos
);
3108 selection_start
.line
= caret
.line
;
3109 selection_start
.tag
= caret
.line
.FindTag(start_pos
);
3110 selection_start
.pos
= start_pos
;
3112 selection_end
.line
= caret
.line
;
3113 selection_end
.tag
= caret
.line
.FindTag(end_pos
);
3114 selection_end
.pos
= end_pos
;
3116 selection_anchor
.line
= selection_end
.line
;
3117 selection_anchor
.tag
= selection_end
.tag
;
3118 selection_anchor
.pos
= selection_end
.pos
;
3119 selection_anchor
.height
= start_pos
;
3121 selection_prev
.line
= caret
.line
;
3122 selection_prev
.tag
= caret
.tag
;
3123 selection_prev
.pos
= caret
.pos
;
3125 this.selection_end_anchor
= true;
3132 SetSelectionVisible (!(selection_start
== selection_end
));
3135 internal void SetSelectionToCaret(bool start
) {
3137 // Invalidate old selection; selection is being reset to empty
3138 this.Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3140 selection_start
.line
= caret
.line
;
3141 selection_start
.tag
= caret
.tag
;
3142 selection_start
.pos
= caret
.pos
;
3144 // start always also selects end
3145 selection_end
.line
= caret
.line
;
3146 selection_end
.tag
= caret
.tag
;
3147 selection_end
.pos
= caret
.pos
;
3149 selection_anchor
.line
= caret
.line
;
3150 selection_anchor
.tag
= caret
.tag
;
3151 selection_anchor
.pos
= caret
.pos
;
3153 // Invalidate from previous end to caret (aka new end)
3154 if (selection_end_anchor
) {
3155 if (selection_start
!= caret
) {
3156 this.Invalidate(selection_start
.line
, selection_start
.pos
, caret
.line
, caret
.pos
);
3159 if (selection_end
!= caret
) {
3160 this.Invalidate(selection_end
.line
, selection_end
.pos
, caret
.line
, caret
.pos
);
3164 if (caret
< selection_anchor
) {
3165 selection_start
.line
= caret
.line
;
3166 selection_start
.tag
= caret
.tag
;
3167 selection_start
.pos
= caret
.pos
;
3169 selection_end
.line
= selection_anchor
.line
;
3170 selection_end
.tag
= selection_anchor
.tag
;
3171 selection_end
.pos
= selection_anchor
.pos
;
3173 selection_end_anchor
= true;
3175 selection_start
.line
= selection_anchor
.line
;
3176 selection_start
.tag
= selection_anchor
.tag
;
3177 selection_start
.pos
= selection_anchor
.pos
;
3179 selection_end
.line
= caret
.line
;
3180 selection_end
.tag
= caret
.tag
;
3181 selection_end
.pos
= caret
.pos
;
3183 selection_end_anchor
= false;
3187 SetSelectionVisible (!(selection_start
== selection_end
));
3190 internal void SetSelection(Line start
, int start_pos
, Line end
, int end_pos
) {
3191 if (selection_visible
) {
3192 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3195 if ((end
.line_no
< start
.line_no
) || ((end
== start
) && (end_pos
<= start_pos
))) {
3196 selection_start
.line
= end
;
3197 selection_start
.tag
= LineTag
.FindTag(end
, end_pos
);
3198 selection_start
.pos
= end_pos
;
3200 selection_end
.line
= start
;
3201 selection_end
.tag
= LineTag
.FindTag(start
, start_pos
);
3202 selection_end
.pos
= start_pos
;
3204 selection_end_anchor
= true;
3206 selection_start
.line
= start
;
3207 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3208 selection_start
.pos
= start_pos
;
3210 selection_end
.line
= end
;
3211 selection_end
.tag
= LineTag
.FindTag(end
, end_pos
);
3212 selection_end
.pos
= end_pos
;
3214 selection_end_anchor
= false;
3217 selection_anchor
.line
= start
;
3218 selection_anchor
.tag
= selection_start
.tag
;
3219 selection_anchor
.pos
= start_pos
;
3221 if (((start
== end
) && (start_pos
== end_pos
)) || start
== null || end
== null) {
3222 SetSelectionVisible (false);
3224 SetSelectionVisible (true);
3225 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3229 internal void SetSelectionStart(Line start
, int start_pos
) {
3230 // Invalidate from the previous to the new start pos
3231 Invalidate(selection_start
.line
, selection_start
.pos
, start
, start_pos
);
3233 selection_start
.line
= start
;
3234 selection_start
.pos
= start_pos
;
3235 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3237 selection_anchor
.line
= start
;
3238 selection_anchor
.pos
= start_pos
;
3239 selection_anchor
.tag
= selection_start
.tag
;
3241 selection_end_anchor
= false;
3244 if ((selection_end
.line
!= selection_start
.line
) || (selection_end
.pos
!= selection_start
.pos
)) {
3245 SetSelectionVisible (true);
3247 SetSelectionVisible (false);
3250 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3253 internal void SetSelectionStart(int character_index
) {
3258 if (character_index
< 0) {
3262 CharIndexToLineTag(character_index
, out line
, out tag
, out pos
);
3263 SetSelectionStart(line
, pos
);
3266 internal void SetSelectionEnd(Line end
, int end_pos
) {
3268 if (end
== selection_end
.line
&& end_pos
== selection_start
.pos
) {
3269 selection_anchor
.line
= selection_start
.line
;
3270 selection_anchor
.tag
= selection_start
.tag
;
3271 selection_anchor
.pos
= selection_start
.pos
;
3273 selection_end
.line
= selection_start
.line
;
3274 selection_end
.tag
= selection_start
.tag
;
3275 selection_end
.pos
= selection_start
.pos
;
3277 selection_end_anchor
= false;
3278 } else if ((end
.line_no
< selection_anchor
.line
.line_no
) || ((end
== selection_anchor
.line
) && (end_pos
<= selection_anchor
.pos
))) {
3279 selection_start
.line
= end
;
3280 selection_start
.tag
= LineTag
.FindTag(end
, end_pos
);
3281 selection_start
.pos
= end_pos
;
3283 selection_end
.line
= selection_anchor
.line
;
3284 selection_end
.tag
= selection_anchor
.tag
;
3285 selection_end
.pos
= selection_anchor
.pos
;
3287 selection_end_anchor
= true;
3289 selection_start
.line
= selection_anchor
.line
;
3290 selection_start
.tag
= selection_anchor
.tag
;
3291 selection_start
.pos
= selection_anchor
.pos
;
3293 selection_end
.line
= end
;
3294 selection_end
.tag
= LineTag
.FindTag(end
, end_pos
);
3295 selection_end
.pos
= end_pos
;
3297 selection_end_anchor
= false;
3300 if ((selection_end
.line
!= selection_start
.line
) || (selection_end
.pos
!= selection_start
.pos
)) {
3301 SetSelectionVisible (true);
3302 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3304 SetSelectionVisible (false);
3305 // ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
3309 internal void SetSelectionEnd(int character_index
) {
3314 if (character_index
< 0) {
3318 CharIndexToLineTag(character_index
, out line
, out tag
, out pos
);
3319 SetSelectionEnd(line
, pos
);
3322 internal void SetSelection(Line start
, int start_pos
) {
3323 if (selection_visible
) {
3324 Invalidate(selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3327 selection_start
.line
= start
;
3328 selection_start
.pos
= start_pos
;
3329 selection_start
.tag
= LineTag
.FindTag(start
, start_pos
);
3331 selection_end
.line
= start
;
3332 selection_end
.tag
= selection_start
.tag
;
3333 selection_end
.pos
= start_pos
;
3335 selection_anchor
.line
= start
;
3336 selection_anchor
.tag
= selection_start
.tag
;
3337 selection_anchor
.pos
= start_pos
;
3339 selection_end_anchor
= false;
3340 SetSelectionVisible (false);
3343 internal void InvalidateSelectionArea() {
3344 Invalidate (selection_start
.line
, selection_start
.pos
, selection_end
.line
, selection_end
.pos
);
3347 // Return the current selection, as string
3348 internal string GetSelection() {
3349 // We return String.Empty if there is no selection
3350 if ((selection_start
.pos
== selection_end
.pos
) && (selection_start
.line
== selection_end
.line
)) {
3351 return string.Empty
;
3354 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3355 return selection_start
.line
.text
.ToString(selection_start
.pos
, selection_end
.pos
- selection_start
.pos
);
3362 sb
= new StringBuilder();
3363 start
= selection_start
.line
.line_no
;
3364 end
= selection_end
.line
.line_no
;
3366 sb
.Append(selection_start
.line
.text
.ToString(selection_start
.pos
, selection_start
.line
.text
.Length
- selection_start
.pos
) + Environment
.NewLine
);
3368 if ((start
+ 1) < end
) {
3369 for (i
= start
+ 1; i
< end
; i
++) {
3370 sb
.Append(GetLine(i
).text
.ToString() + Environment
.NewLine
);
3374 sb
.Append(selection_end
.line
.text
.ToString(0, selection_end
.pos
));
3376 return sb
.ToString();
3380 internal void ReplaceSelection(string s
, bool select_new
) {
3383 undo
.BeginCompoundAction ();
3385 int selection_start_pos
= LineTagToCharIndex (selection_start
.line
, selection_start
.pos
);
3386 // First, delete any selected text
3387 if ((selection_start
.pos
!= selection_end
.pos
) || (selection_start
.line
!= selection_end
.line
)) {
3388 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3389 undo
.RecordDeleteChars(selection_start
.line
, selection_start
.pos
+ 1, selection_end
.pos
- selection_start
.pos
);
3391 DeleteChars(selection_start
.tag
, selection_start
.pos
, selection_end
.pos
- selection_start
.pos
);
3393 // The tag might have been removed, we need to recalc it
3394 selection_start
.tag
= selection_start
.line
.FindTag(selection_start
.pos
);
3399 start
= selection_start
.line
.line_no
;
3400 end
= selection_end
.line
.line_no
;
3402 undo
.RecordDelete(selection_start
.line
, selection_start
.pos
+ 1, selection_end
.line
, selection_end
.pos
);
3404 // Delete first line
3405 DeleteChars(selection_start
.tag
, selection_start
.pos
, selection_start
.line
.text
.Length
- selection_start
.pos
);
3408 DeleteChars(selection_end
.line
.tags
, 0, selection_end
.pos
);
3412 for (i
= end
- 1; i
>= start
; i
--) {
3417 // BIG FAT WARNING - selection_end.line might be stale due
3418 // to the above Delete() call. DONT USE IT before hitting the end of this method!
3420 // Join start and end
3421 Combine(selection_start
.line
.line_no
, start
);
3425 Insert(selection_start
.line
, selection_start
.pos
, true, s
);
3426 undo
.RecordInsertString (selection_start
.line
, selection_start
.pos
, s
);
3429 CharIndexToLineTag(selection_start_pos
+ s
.Length
, out selection_start
.line
,
3430 out selection_start
.tag
, out selection_start
.pos
);
3432 selection_end
.line
= selection_start
.line
;
3433 selection_end
.pos
= selection_start
.pos
;
3434 selection_end
.tag
= selection_start
.tag
;
3435 selection_anchor
.line
= selection_start
.line
;
3436 selection_anchor
.pos
= selection_start
.pos
;
3437 selection_anchor
.tag
= selection_start
.tag
;
3439 SetSelectionVisible (false);
3441 CharIndexToLineTag(selection_start_pos
, out selection_start
.line
,
3442 out selection_start
.tag
, out selection_start
.pos
);
3444 CharIndexToLineTag(selection_start_pos
+ s
.Length
, out selection_end
.line
,
3445 out selection_end
.tag
, out selection_end
.pos
);
3447 selection_anchor
.line
= selection_start
.line
;
3448 selection_anchor
.pos
= selection_start
.pos
;
3449 selection_anchor
.tag
= selection_start
.tag
;
3451 SetSelectionVisible (true);
3454 undo
.EndCompoundAction ();
3457 internal void CharIndexToLineTag(int index
, out Line line_out
, out LineTag tag_out
, out int pos
) {
3466 for (i
= 1; i
<= lines
; i
++) {
3470 chars
+= line
.text
.Length
+ crlf_size
;
3472 if (index
<= chars
) {
3473 // we found the line
3476 while (tag
!= null) {
3477 if (index
< (start
+ tag
.start
+ tag
.length
)) {
3479 tag_out
= LineTag
.GetFinalTag (tag
);
3480 pos
= index
- start
;
3483 if (tag
.next
== null) {
3486 next_line
= GetLine(line
.line_no
+ 1);
3488 if (next_line
!= null) {
3489 line_out
= next_line
;
3490 tag_out
= LineTag
.GetFinalTag (next_line
.tags
);
3495 tag_out
= LineTag
.GetFinalTag (tag
);
3496 pos
= line_out
.text
.Length
;
3505 line_out
= GetLine(lines
);
3506 tag
= line_out
.tags
;
3507 while (tag
.next
!= null) {
3511 pos
= line_out
.text
.Length
;
3514 internal int LineTagToCharIndex(Line line
, int pos
) {
3518 // Count first and last line
3521 // Count the lines in the middle
3523 for (i
= 1; i
< line
.line_no
; i
++) {
3524 length
+= GetLine(i
).text
.Length
+ crlf_size
;
3532 internal int SelectionLength() {
3533 if ((selection_start
.pos
== selection_end
.pos
) && (selection_start
.line
== selection_end
.line
)) {
3537 if (!multiline
|| (selection_start
.line
== selection_end
.line
)) {
3538 return selection_end
.pos
- selection_start
.pos
;
3545 // Count first and last line
3546 length
= selection_start
.line
.text
.Length
- selection_start
.pos
+ selection_end
.pos
+ crlf_size
;
3548 // Count the lines in the middle
3549 start
= selection_start
.line
.line_no
+ 1;
3550 end
= selection_end
.line
.line_no
;
3553 for (i
= start
; i
< end
; i
++) {
3554 length
+= GetLine(i
).text
.Length
+ crlf_size
;
3565 /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
3566 internal Line
GetLine(int LineNo
) {
3567 Line line
= document
;
3569 while (line
!= sentinel
) {
3570 if (LineNo
== line
.line_no
) {
3572 } else if (LineNo
< line
.line_no
) {
3582 /// <summary>Retrieve the previous tag; walks line boundaries</summary>
3583 internal LineTag
PreviousTag(LineTag tag
) {
3586 if (tag
.previous
!= null) {
3587 return tag
.previous
;
3591 if (tag
.line
.line_no
== 1) {
3595 l
= GetLine(tag
.line
.line_no
- 1);
3600 while (t
.next
!= null) {
3609 /// <summary>Retrieve the next tag; walks line boundaries</summary>
3610 internal LineTag
NextTag(LineTag tag
) {
3613 if (tag
.next
!= null) {
3618 l
= GetLine(tag
.line
.line_no
+ 1);
3626 internal Line
ParagraphStart(Line line
) {
3627 while (line
.soft_break
) {
3628 line
= GetLine(line
.line_no
- 1);
3633 internal Line
ParagraphEnd(Line line
) {
3636 while (line
.soft_break
) {
3637 l
= GetLine(line
.line_no
+ 1);
3638 if ((l
== null) || (!l
.soft_break
)) {
3646 /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
3647 internal Line
GetLineByPixel(int y
, bool exact
) {
3648 Line line
= document
;
3651 while (line
!= sentinel
) {
3653 if ((y
>= line
.Y
) && (y
< (line
.Y
+line
.height
))) {
3655 } else if (y
< line
.Y
) {
3668 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3669 internal LineTag
FindTag(int x
, int y
, out int index
, bool exact
) {
3673 line
= GetLineByPixel(y
, exact
);
3680 // Alignment adjustment
3681 x
+= line
.align_shift
;
3684 if (x
>= tag
.X
&& x
< (tag
.X
+tag
.width
)) {
3687 end
= tag
.start
+ tag
.length
- 1;
3689 for (int pos
= tag
.start
; pos
< end
; pos
++) {
3690 if (x
< line
.widths
[pos
]) {
3692 return LineTag
.GetFinalTag (tag
);
3696 return LineTag
.GetFinalTag (tag
);
3698 if (tag
.next
!= null) {
3706 index
= line
.text
.Length
;
3707 return LineTag
.GetFinalTag (tag
);
3712 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3713 internal LineTag
FindCursor(int x
, int y
, out int index
) {
3717 line
= GetLineByPixel(y
, false);
3720 // Adjust for alignment
3721 x
-= line
.align_shift
;
3724 if (x
>= tag
.X
&& x
< (tag
.X
+tag
.width
)) {
3727 end
= tag
.start
+ tag
.length
- 1;
3729 for (int pos
= tag
.start
-1; pos
< end
; pos
++) {
3730 // When clicking on a character, we position the cursor to whatever edge
3731 // of the character the click was closer
3732 if (x
< (line
.widths
[pos
] + ((line
.widths
[pos
+1]-line
.widths
[pos
])/2))) {
3740 if (tag
.next
!= null) {
3743 index
= line
.text
.Length
;
3749 /// <summary>Format area of document in specified font and color</summary>
3750 /// <param name="start_pos">1-based start position on start_line</param>
3751 /// <param name="end_pos">1-based end position on end_line </param>
3752 internal void FormatText(Line start_line
, int start_pos
, Line end_line
, int end_pos
, Font font
, Brush color
) {
3755 // First, format the first line
3756 if (start_line
!= end_line
) {
3758 LineTag
.FormatText(start_line
, start_pos
, start_line
.text
.Length
- start_pos
+ 1, font
, color
);
3761 LineTag
.FormatText(end_line
, 1, end_pos
, font
, color
);
3763 // Now all the lines inbetween
3764 for (int i
= start_line
.line_no
+ 1; i
< end_line
.line_no
; i
++) {
3766 LineTag
.FormatText(l
, 1, l
.text
.Length
, font
, color
);
3769 // Special case, single line
3770 LineTag
.FormatText(start_line
, start_pos
, end_pos
- start_pos
, font
, color
);
3774 /// <summary>Re-format areas of the document in specified font and color</summary>
3775 /// <param name="start_pos">1-based start position on start_line</param>
3776 /// <param name="end_pos">1-based end position on end_line </param>
3777 /// <param name="font">Font specifying attributes</param>
3778 /// <param name="color">Color (or NULL) to apply</param>
3779 /// <param name="apply">Attributes from font and color to apply</param>
3780 internal void FormatText(Line start_line
, int start_pos
, Line end_line
, int end_pos
, FontDefinition attributes
) {
3783 // First, format the first line
3784 if (start_line
!= end_line
) {
3786 LineTag
.FormatText(start_line
, start_pos
, start_line
.text
.Length
- start_pos
+ 1, attributes
);
3789 LineTag
.FormatText(end_line
, 1, end_pos
- 1, attributes
);
3791 // Now all the lines inbetween
3792 for (int i
= start_line
.line_no
+ 1; i
< end_line
.line_no
; i
++) {
3794 LineTag
.FormatText(l
, 1, l
.text
.Length
, attributes
);
3797 // Special case, single line
3798 LineTag
.FormatText(start_line
, start_pos
, end_pos
- start_pos
, attributes
);
3802 internal void RecalculateAlignments() {
3808 while (line_no
<= lines
) {
3809 line
= GetLine(line_no
);
3812 switch (line
.alignment
) {
3813 case HorizontalAlignment
.Left
:
3814 line
.align_shift
= 0;
3816 case HorizontalAlignment
.Center
:
3817 line
.align_shift
= (viewport_width
- (int)line
.widths
[line
.text
.Length
]) / 2;
3819 case HorizontalAlignment
.Right
:
3820 line
.align_shift
= viewport_width
- (int)line
.widths
[line
.text
.Length
];
3830 /// <summary>Calculate formatting for the whole document</summary>
3831 internal bool RecalculateDocument(Graphics g
) {
3832 return RecalculateDocument(g
, 1, this.lines
, false);
3835 /// <summary>Calculate formatting starting at a certain line</summary>
3836 internal bool RecalculateDocument(Graphics g
, int start
) {
3837 return RecalculateDocument(g
, start
, this.lines
, false);
3840 /// <summary>Calculate formatting within two given line numbers</summary>
3841 internal bool RecalculateDocument(Graphics g
, int start
, int end
) {
3842 return RecalculateDocument(g
, start
, end
, false);
3845 /// <summary>With optimize on, returns true if line heights changed</summary>
3846 internal bool RecalculateDocument(Graphics g
, int start
, int end
, bool optimize
) {
3855 recalc_pending
= true;
3856 recalc_start
= start
;
3858 recalc_optimize
= optimize
;
3862 Y
= GetLine(start
).Y
;
3867 changed
= true; // We always return true if we run non-optimized
3872 while (line_no
<= (end
+ this.lines
- shift
)) {
3873 line
= GetLine(line_no
++);
3878 line
.RecalculateLine(g
, this);
3880 if (line
.recalc
&& line
.RecalculateLine(g
, this)) {
3882 // If the height changed, all subsequent lines change
3889 line
.RecalculatePasswordLine(g
, this);
3891 if (line
.recalc
&& line
.RecalculatePasswordLine(g
, this)) {
3893 // If the height changed, all subsequent lines change
3900 if (line
.widths
[line
.text
.Length
] > new_width
) {
3901 new_width
= (int)line
.widths
[line
.text
.Length
];
3904 // Calculate alignment
3905 if (line
.alignment
!= HorizontalAlignment
.Left
) {
3906 if (line
.alignment
== HorizontalAlignment
.Center
) {
3907 line
.align_shift
= (viewport_width
- (int)line
.widths
[line
.text
.Length
]) / 2;
3909 line
.align_shift
= viewport_width
- (int)line
.widths
[line
.text
.Length
] - 1;
3915 if (line_no
> lines
) {
3920 if (document_x
!= new_width
) {
3921 document_x
= new_width
;
3922 if (WidthChanged
!= null) {
3923 WidthChanged(this, null);
3927 RecalculateAlignments();
3929 line
= GetLine(lines
);
3931 if (document_y
!= line
.Y
+ line
.height
) {
3932 document_y
= line
.Y
+ line
.height
;
3933 if (HeightChanged
!= null) {
3934 HeightChanged(this, null);
3941 internal int Size() {
3945 private void owner_HandleCreated(object sender
, EventArgs e
) {
3946 RecalculateDocument(owner
.CreateGraphicsInternal());
3950 private void owner_VisibleChanged(object sender
, EventArgs e
) {
3951 if (owner
.Visible
) {
3952 RecalculateDocument(owner
.CreateGraphicsInternal());
3956 internal static bool IsWordSeparator(char ch
) {
3970 internal int FindWordSeparator(Line line
, int pos
, bool forward
) {
3973 len
= line
.text
.Length
;
3976 for (int i
= pos
+ 1; i
< len
; i
++) {
3977 if (IsWordSeparator(line
.Text
[i
])) {
3983 for (int i
= pos
- 1; i
> 0; i
--) {
3984 if (IsWordSeparator(line
.Text
[i
- 1])) {
3992 /* Search document for text */
3993 internal bool FindChars(char[] chars
, Marker start
, Marker end
, out Marker result
) {
3999 // Search for occurence of any char in the chars array
4000 result
= new Marker();
4003 line_no
= start
.line
.line_no
;
4005 while (line_no
<= end
.line
.line_no
) {
4006 line_len
= line
.text
.Length
;
4007 while (pos
< line_len
) {
4008 for (int i
= 0; i
< chars
.Length
; i
++) {
4009 if (line
.text
[pos
] == chars
[i
]) {
4011 if ((line
.line_no
== end
.line
.line_no
) && (pos
>= end
.pos
)) {
4025 line
= GetLine(line_no
);
4031 // This version does not build one big string for searching, instead it handles
4032 // line-boundaries, which is faster and less memory intensive
4033 // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
4034 // search stuff and change it to accept and return positions instead of Markers (which would match
4035 // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
4036 internal bool Find(string search
, Marker start
, Marker end
, out Marker result
, RichTextBoxFinds options
) {
4038 string search_string
;
4050 result
= new Marker();
4051 word_option
= ((options
& RichTextBoxFinds
.WholeWord
) != 0);
4052 ignore_case
= ((options
& RichTextBoxFinds
.MatchCase
) == 0);
4053 reverse
= ((options
& RichTextBoxFinds
.Reverse
) != 0);
4056 line_no
= start
.line
.line_no
;
4060 // Prep our search string, lowercasing it if we do case-independent matching
4063 sb
= new StringBuilder(search
);
4064 for (int i
= 0; i
< sb
.Length
; i
++) {
4065 sb
[i
] = Char
.ToLower(sb
[i
]);
4067 search_string
= sb
.ToString();
4069 search_string
= search
;
4072 // We need to check if the character before our start position is a wordbreak
4075 if ((pos
== 0) || (IsWordSeparator(line
.text
[pos
- 1]))) {
4082 if (IsWordSeparator(line
.text
[pos
- 1])) {
4088 // Need to check the end of the previous line
4091 prev_line
= GetLine(line_no
- 1);
4092 if (prev_line
.soft_break
) {
4093 if (IsWordSeparator(prev_line
.text
[prev_line
.text
.Length
- 1])) {
4107 // To avoid duplication of this loop with reverse logic, we search
4108 // through the document, remembering the last match and when returning
4109 // report that last remembered match
4111 last
= new Marker();
4112 last
.height
= -1; // Abused - we use it to track change
4114 while (line_no
<= end
.line
.line_no
) {
4115 if (line_no
!= end
.line
.line_no
) {
4116 line_len
= line
.text
.Length
;
4121 while (pos
< line_len
) {
4122 if (word_option
&& (current
== search_string
.Length
)) {
4123 if (IsWordSeparator(line
.text
[pos
])) {
4136 c
= Char
.ToLower(line
.text
[pos
]);
4141 if (c
== search_string
[current
]) {
4146 if (!word_option
|| (word_option
&& (word
|| (current
> 0)))) {
4150 if (!word_option
&& (current
== search_string
.Length
)) {
4167 if (IsWordSeparator(c
)) {
4175 // Mark that we just saw a word boundary
4176 if (!line
.soft_break
) {
4180 if (current
== search_string
.Length
) {
4196 line
= GetLine(line_no
);
4200 if (last
.height
!= -1) {
4210 // if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
4222 internal void GetMarker(out Marker mark
, bool start
) {
4223 mark
= new Marker();
4226 mark
.line
= GetLine(1);
4227 mark
.tag
= mark
.line
.tags
;
4230 mark
.line
= GetLine(lines
);
4231 mark
.tag
= mark
.line
.tags
;
4232 while (mark
.tag
.next
!= null) {
4233 mark
.tag
= mark
.tag
.next
;
4235 mark
.pos
= mark
.line
.text
.Length
;
4238 #endregion // Internal Methods
4241 internal event EventHandler CaretMoved
;
4242 internal event EventHandler WidthChanged
;
4243 internal event EventHandler HeightChanged
;
4244 internal event EventHandler LengthChanged
;
4245 #endregion // Events
4247 #region Administrative
4248 public IEnumerator
GetEnumerator() {
4253 public override bool Equals(object obj
) {
4258 if (!(obj
is Document
)) {
4266 if (ToString().Equals(((Document
)obj
).ToString())) {
4273 public override int GetHashCode() {
4277 public override string ToString() {
4278 return "document " + this.document_id
;
4280 #endregion // Administrative
4283 internal class LineTag
{
4284 #region Local Variables;
4285 // Payload; formatting
4286 internal Font font
; // System.Drawing.Font object for this tag
4287 internal Brush color
; // System.Drawing.Brush object
4290 internal int start
; // start, in chars; index into Line.text
4291 internal int length
; // length, in chars
4292 internal bool r_to_l
; // Which way is the font
4295 internal int height
; // Height in pixels of the text this tag describes
4296 internal int X
; // X location of the text this tag describes
4297 internal float width
; // Width in pixels of the text this tag describes
4298 internal int ascent
; // Ascent of the font for this tag
4299 internal int shift
; // Shift down for this tag, to stay on baseline
4302 internal Line line
; // The line we're on
4303 internal LineTag next
; // Next tag on the same line
4304 internal LineTag previous
; // Previous tag on the same line
4307 #region Constructors
4308 internal LineTag(Line line
, int start
, int length
) {
4311 this.length
= length
;
4315 #endregion // Constructors
4317 #region Internal Methods
4318 ///<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>
4319 internal LineTag
Break(int pos
) {
4323 if (pos
== this.start
) {
4325 } else if (pos
>= (start
+ length
)) {
4329 new_tag
= new LineTag(line
, pos
, start
+ length
- pos
);
4330 new_tag
.color
= color
;
4331 new_tag
.font
= font
;
4332 this.length
-= new_tag
.length
;
4333 new_tag
.next
= this.next
;
4334 this.next
= new_tag
;
4335 new_tag
.previous
= this;
4336 if (new_tag
.next
!= null) {
4337 new_tag
.next
.previous
= new_tag
;
4343 ///<summary>Create new font and brush from existing font and given new attributes. Returns true if fontheight changes</summary>
4344 internal static bool GenerateTextFormat(Font font_from
, Brush color_from
, FontDefinition attributes
, out Font new_font
, out Brush new_color
) {
4350 if (attributes
.font_obj
== null) {
4351 size
= font_from
.SizeInPoints
;
4352 unit
= font_from
.Unit
;
4353 face
= font_from
.Name
;
4354 style
= font_from
.Style
;
4356 if (attributes
.face
!= null) {
4357 face
= attributes
.face
;
4360 if (attributes
.size
!= 0) {
4361 size
= attributes
.size
;
4364 style
|= attributes
.add_style
;
4365 style
&= ~attributes
.remove_style
;
4368 new_font
= new Font(face
, size
, style
, unit
);
4370 new_font
= attributes
.font_obj
;
4373 // Create 'new' color brush
4374 if (attributes
.color
!= Color
.Empty
) {
4375 new_color
= new SolidBrush(attributes
.color
);
4377 new_color
= color_from
;
4380 if (new_font
.Height
== font_from
.Height
) {
4386 /// <summary>Applies 'font' and 'brush' to characters starting at 'start' for 'length' chars;
4387 /// Removes any previous tags overlapping the same area;
4388 /// returns true if lineheight has changed</summary>
4389 /// <param name="start">1-based character position on line</param>
4390 internal static bool FormatText(Line line
, int start
, int length
, Font font
, Brush color
) {
4395 bool retval
= false; // Assume line-height doesn't change
4398 if (font
.Height
!= line
.height
) {
4401 line
.recalc
= true; // This forces recalculation of the line in RecalculateDocument
4403 // A little sanity, not sure if it's needed, might be able to remove for speed
4404 if (length
> line
.text
.Length
) {
4405 length
= line
.text
.Length
;
4409 end
= start
+ length
;
4411 // Common special case
4412 if ((start
== 1) && (length
== tag
.length
)) {
4419 //Console.WriteLine("Finding tag for {0} {1}", line, start);
4420 start_tag
= FindTag(line
, start
);
4421 end_tag
= FindTag (line
, end
);
4423 if (start_tag
== null) { // FIXME - is there a better way to handle this, or do we even need it?
4424 throw new Exception(String
.Format("Could not find start_tag in document at line {0} position {1}", line
.line_no
, start
));
4427 tag
= new LineTag(line
, start
, length
);
4434 //Console.WriteLine("Start tag: '{0}'", start_tag!=null ? start_tag.ToString() : "NULL");
4435 if (start_tag
.start
== start
) {
4436 tag
.next
= start_tag
;
4437 tag
.previous
= start_tag
.previous
;
4438 if (start_tag
.previous
!= null) {
4439 start_tag
.previous
.next
= tag
;
4441 start_tag
.previous
= tag
;
4445 if (end_tag
!= null) {
4446 // Shorten up the end tag
4447 end_tag
.previous
= tag
;
4448 end_tag
.length
= end
- start_tag
.start
+ start_tag
.length
;
4449 end_tag
.start
= end
;
4455 while (tag
!= end_tag
) {
4456 if ((tag
.start
+ tag
.length
) <= end
) {
4458 tag
.previous
.next
= tag
.next
;
4459 if (tag
.next
!= null) {
4460 tag
.next
.previous
= tag
.previous
;
4470 /// <summary>Applies font attributes specified to characters starting at 'start' for 'length' chars;
4471 /// Breaks tags at start and end point, keeping middle tags with altered attributes.
4472 /// Returns true if lineheight has changed</summary>
4473 /// <param name="start">1-based character position on line</param>
4474 internal static bool FormatText(Line line
, int start
, int length
, FontDefinition attributes
) {
4478 bool retval
= false; // Assume line-height doesn't change
4480 line
.recalc
= true; // This forces recalculation of the line in RecalculateDocument
4482 // A little sanity, not sure if it's needed, might be able to remove for speed
4483 if (length
> line
.text
.Length
) {
4484 length
= line
.text
.Length
;
4489 // Common special case
4490 if ((start
== 1) && (length
== tag
.length
)) {
4492 GenerateTextFormat(tag
.font
, tag
.color
, attributes
, out tag
.font
, out tag
.color
);
4496 start_tag
= FindTag(line
, start
);
4498 if (start_tag
== null) {
4500 // We are 'starting' after all valid tags; create a new tag with the right attributes
4501 start_tag
= FindTag(line
, line
.text
.Length
- 1);
4502 start_tag
.next
= new LineTag(line
, line
.text
.Length
+ 1, 0);
4503 start_tag
.next
.font
= start_tag
.font
;
4504 start_tag
.next
.color
= start_tag
.color
;
4505 start_tag
.next
.previous
= start_tag
;
4506 start_tag
= start_tag
.next
;
4508 throw new Exception(String
.Format("Could not find start_tag in document at line {0} position {1}", line
.line_no
, start
));
4511 start_tag
= start_tag
.Break(start
);
4514 end_tag
= FindTag(line
, start
+ length
);
4515 if (end_tag
!= null) {
4516 end_tag
= end_tag
.Break(start
+ length
);
4519 // start_tag or end_tag might be null; we're cool with that
4520 // we now walk from start_tag to end_tag, applying new attributes
4522 while ((tag
!= null) && tag
!= end_tag
) {
4523 if (LineTag
.GenerateTextFormat(tag
.font
, tag
.color
, attributes
, out tag
.font
, out tag
.color
)) {
4532 /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
4533 internal static LineTag
FindTag(Line line
, int pos
) {
4534 LineTag tag
= line
.tags
;
4536 // Beginning of line is a bit special
4538 // Not sure if we should get the final tag here
4542 while (tag
!= null) {
4543 if ((tag
.start
<= pos
) && (pos
< (tag
.start
+tag
.length
))) {
4544 return GetFinalTag (tag
);
4553 // There can be multiple tags at the same position, we want to make
4554 // sure we are using the very last tag at the given position
4555 internal static LineTag
GetFinalTag (LineTag tag
)
4559 while (res
.next
!= null && res
.next
.length
== 0)
4564 /// <summary>Combines 'this' tag with 'other' tag</summary>
4565 internal bool Combine(LineTag other
) {
4566 if (!this.Equals(other
)) {
4570 this.width
+= other
.width
;
4571 this.length
+= other
.length
;
4572 this.next
= other
.next
;
4573 if (this.next
!= null) {
4574 this.next
.previous
= this;
4581 /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
4582 internal bool Remove() {
4583 if ((this.start
== 1) && (this.next
== null)) {
4584 // We cannot remove the only tag
4587 if (this.start
!= 1) {
4588 this.previous
.length
+= this.length
;
4589 this.previous
.width
= -1;
4590 this.previous
.next
= this.next
;
4591 this.next
.previous
= this.previous
;
4593 this.next
.start
= 1;
4594 this.next
.length
+= this.length
;
4595 this.next
.width
= -1;
4596 this.line
.tags
= this.next
;
4597 this.next
.previous
= null;
4603 /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
4604 public override bool Equals(object obj
) {
4611 if (!(obj
is LineTag
)) {
4619 other
= (LineTag
)obj
;
4621 if (this.font
.Equals(other
.font
) && this.color
.Equals(other
.color
)) { // FIXME add checking for things like link or type later
4628 public override int GetHashCode() {
4629 return base.GetHashCode ();
4632 public override string ToString() {
4634 return "Tag starts at index " + this.start
+ "length " + this.length
+ " text: " + this.line
.Text
.Substring(this.start
-1, this.length
) + "Font " + this.font
.ToString();
4635 return "Zero Lengthed tag at index " + this.start
;
4638 #endregion // Internal Methods
4641 internal class UndoClass
{
4642 internal enum ActionType
{
4653 internal class Action
{
4654 internal ActionType type
;
4655 internal int line_no
;
4657 internal object data
;
4660 #region Local Variables
4661 private Document document
;
4662 private Stack undo_actions
;
4663 private Stack redo_actions
;
4665 private int undo_levels
;
4666 private int redo_levels
;
4667 private int caret_line
;
4668 private int caret_pos
;
4669 #endregion // Local Variables
4671 #region Constructors
4672 internal UndoClass(Document doc
) {
4674 undo_actions
= new Stack(50);
4675 redo_actions
= new Stack(50);
4677 #endregion // Constructors
4680 internal int UndoLevels
{
4686 internal int RedoLevels
{
4692 internal string UndoName
{
4696 action
= (Action
)undo_actions
.Peek();
4698 if (action
.type
== ActionType
.CompoundEnd
)
4699 return (string) action
.data
;
4701 switch(action
.type
) {
4702 case ActionType
.InsertChar
: {
4703 Locale
.GetText("Insert character");
4707 case ActionType
.DeleteChar
: {
4708 Locale
.GetText("Delete character");
4712 case ActionType
.InsertString
: {
4713 Locale
.GetText("Insert string");
4717 case ActionType
.DeleteChars
: {
4718 Locale
.GetText("Delete string");
4722 case ActionType
.CursorMove
: {
4723 Locale
.GetText("Cursor move");
4731 internal string RedoName() {
4734 #endregion // Properties
4736 #region Internal Methods
4737 internal void Clear() {
4738 undo_actions
.Clear();
4739 redo_actions
.Clear();
4744 internal void Undo() {
4746 int compound_stack
= 0;
4748 if (undo_actions
.Count
== 0) {
4755 action
= (Action
)undo_actions
.Pop();
4757 // Put onto redo stack
4758 redo_actions
.Push(action
);
4761 switch(action
.type
) {
4762 case ActionType
.CompoundEnd
:
4766 case ActionType
.CompoundBegin
:
4772 case ActionType
.InsertString
:
4773 document
.DeleteMultiline (document
.GetLine (action
.line_no
),
4774 action
.pos
, ((string) action
.data
).Length
+ 1);
4777 case ActionType
.InsertChar
: {
4778 // FIXME - implement me
4782 case ActionType
.DeleteChars
: {
4783 this.Insert(document
.GetLine(action
.line_no
), action
.pos
, (Line
)action
.data
);
4784 Undo(); // Grab the cursor location
4788 case ActionType
.CursorMove
: {
4789 document
.caret
.line
= document
.GetLine(action
.line_no
);
4790 if (document
.caret
.line
== null) {
4795 document
.caret
.tag
= document
.caret
.line
.FindTag(action
.pos
);
4796 document
.caret
.pos
= action
.pos
;
4797 document
.caret
.height
= document
.caret
.tag
.height
;
4799 if (document
.owner
.IsHandleCreated
) {
4800 XplatUI
.DestroyCaret(document
.owner
.Handle
);
4801 XplatUI
.CreateCaret(document
.owner
.Handle
, 2, document
.caret
.height
);
4802 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
);
4804 document
.DisplayCaret ();
4807 // FIXME - enable call
4808 //if (document.CaretMoved != null) document.CaretMoved(this, EventArgs.Empty);
4812 } while (compound_stack
> 0);
4815 internal void Redo() {
4816 if (redo_actions
.Count
== 0) {
4820 #endregion // Internal Methods
4822 #region Private Methods
4824 public void BeginCompoundAction ()
4826 Action cb
= new Action ();
4827 cb
.type
= ActionType
.CompoundBegin
;
4829 undo_actions
.Push (cb
);
4832 public void EndCompoundAction ()
4834 Action ce
= new Action ();
4835 ce
.type
= ActionType
.CompoundEnd
;
4837 undo_actions
.Push (ce
);
4842 public void RecordDeleteChars(Line line
, int pos
, int length
) {
4843 RecordDelete(line
, pos
, line
, pos
+ length
- 1);
4846 // start_pos, end_pos = 1 based
4847 public void RecordDelete(Line start_line
, int start_pos
, Line end_line
, int end_pos
) {
4851 l
= Duplicate(start_line
, start_pos
, end_line
, end_pos
);
4854 a
.type
= ActionType
.DeleteChars
;
4856 a
.line_no
= start_line
.line_no
;
4857 a
.pos
= start_pos
- 1;
4859 // Record the cursor position before, since the actions will occur in reverse order
4861 undo_actions
.Push(a
);
4864 public void RecordInsertString (Line line
, int pos
, string str
)
4866 Action a
= new Action ();
4868 a
.type
= ActionType
.InsertString
;
4870 a
.line_no
= line
.line_no
;
4873 undo_actions
.Push (a
);
4876 public void RecordCursor() {
4877 if (document
.caret
.line
== null) {
4881 RecordCursor(document
.caret
.line
, document
.caret
.pos
);
4884 public void RecordCursor(Line line
, int pos
) {
4887 if ((line
.line_no
== caret_line
) && (pos
== caret_pos
)) {
4891 caret_line
= line
.line_no
;
4895 a
.type
= ActionType
.CursorMove
;
4896 a
.line_no
= line
.line_no
;
4899 undo_actions
.Push(a
);
4902 // start_pos = 1-based
4903 // end_pos = 1-based
4904 public Line
Duplicate(Line start_line
, int start_pos
, Line end_line
, int end_pos
) {
4909 LineTag current_tag
;
4918 for (int i
= start_line
.line_no
; i
<= end_line
.line_no
; i
++) {
4919 current
= document
.GetLine(i
);
4921 if (start_line
.line_no
== i
) {
4927 if (end_line
.line_no
== i
) {
4930 end
= current
.text
.Length
;
4934 line
.text
= new StringBuilder(current
.text
.ToString(start
- 1, end
- start
+ 1));
4936 // Copy tags from start to start+length onto new line
4937 current_tag
= current
.FindTag(start
- 1);
4938 while ((current_tag
!= null) && (current_tag
.start
< end
)) {
4939 if ((current_tag
.start
<= start
) && (start
< (current_tag
.start
+ current_tag
.length
))) {
4940 // start tag is within this tag
4943 tag_start
= current_tag
.start
;
4946 if (end
< (current_tag
.start
+ current_tag
.length
)) {
4947 tag_length
= end
- tag_start
+ 1;
4949 tag_length
= current_tag
.start
+ current_tag
.length
- tag_start
;
4951 tag
= new LineTag(line
, tag_start
- start
+ 1, tag_length
);
4952 tag
.color
= current_tag
.color
;
4953 tag
.font
= current_tag
.font
;
4955 current_tag
= current_tag
.next
;
4957 // Add the new tag to the line
4958 if (line
.tags
== null) {
4964 while (tail
.next
!= null) {
4968 tag
.previous
= tail
;
4972 if ((i
+ 1) <= end_line
.line_no
) {
4973 line
.soft_break
= current
.soft_break
;
4975 // Chain them (we use right/left as next/previous)
4976 line
.right
= new Line();
4977 line
.right
.left
= line
;
4985 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
4986 internal void Insert(Line line
, int pos
, Line insert
) {
4993 // Handle special case first
4994 if (insert
.right
== null) {
4996 // Single line insert
4997 document
.Split(line
, pos
);
4999 if (insert
.tags
== null) {
5000 return; // Blank line
5003 //Insert our tags at the end
5006 while (tag
.next
!= null) {
5010 offset
= tag
.start
+ tag
.length
- 1;
5012 tag
.next
= insert
.tags
;
5013 line
.text
.Insert(offset
, insert
.text
.ToString());
5015 // Adjust start locations
5017 while (tag
!= null) {
5018 tag
.start
+= offset
;
5022 // Put it back together
5023 document
.Combine(line
.line_no
, line
.line_no
+ 1);
5024 document
.UpdateView(line
, pos
);
5031 while (current
!= null) {
5032 if (current
== insert
) {
5033 // Inserting the first line we split the line (and make space)
5034 document
.Split(line
, pos
);
5035 //Insert our tags at the end of the line
5039 while (tag
.next
!= null) {
5042 offset
= tag
.start
+ tag
.length
- 1;
5043 tag
.next
= current
.tags
;
5044 tag
.next
.previous
= tag
;
5050 line
.tags
= current
.tags
;
5051 line
.tags
.previous
= null;
5055 document
.Split(line
.line_no
, 0);
5057 line
.tags
= current
.tags
;
5058 line
.tags
.previous
= null;
5061 // Adjust start locations and line pointers
5062 while (tag
!= null) {
5063 tag
.start
+= offset
;
5068 line
.text
.Insert(offset
, current
.text
.ToString());
5069 line
.Grow(line
.text
.Length
);
5072 line
= document
.GetLine(line
.line_no
+ 1);
5074 // FIXME? Test undo of line-boundaries
5075 if ((current
.right
== null) && (current
.tags
.length
!= 0)) {
5076 document
.Combine(line
.line_no
- 1, line
.line_no
);
5078 current
= current
.right
;
5083 // Recalculate our document
5084 document
.UpdateView(first
, lines
, pos
);
5087 #endregion // Private Methods