* TextControl.cs: We need to invalidate the textbox when we
[mono-project.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextControl.cs
blob0b8ec420ce5313b9497a1f491d7773062d927a7f
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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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)
22 // Authors:
23 // Peter Bartok pbartok@novell.com
27 // NOT COMPLETE
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
36 // NOTE:
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
46 #undef Debug
48 using System;
49 using System.Collections;
50 using System.Drawing;
51 using System.Drawing.Text;
52 using System.Text;
54 namespace System.Windows.Forms {
55 internal enum LineColor {
56 Red = 0,
57 Black = 1
60 internal enum CaretSelection {
61 Position, // Selection=Caret
62 Word, // Selection=Word under caret
63 Line // Selection=Line under caret
66 internal class FontDefinition {
67 internal String face;
68 internal int size;
69 internal FontStyle add_style;
70 internal FontStyle remove_style;
71 internal Color color;
72 internal Font font_obj;
74 internal FontDefinition() {
75 face = null;
76 size = 0;
77 color = Color.Empty;
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
133 #region Constructors
134 internal Line() {
135 color = LineColor.Red;
136 left = null;
137 right = null;
138 parent = null;
139 text = null;
140 recalc = true;
141 soft_break = false;
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);
155 line_no = LineNo;
157 widths = new float[space + 1];
158 tags = new LineTag(this, 1, text.Length);
159 tags.font = font;
160 tags.color = color;
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);
167 line_no = LineNo;
168 alignment = align;
170 widths = new float[space + 1];
171 tags = new LineTag(this, 1, text.Length);
172 tags.font = font;
173 tags.color = color;
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);
180 line_no = LineNo;
182 widths = new float[space + 1];
183 tags = tag;
186 #endregion // Constructors
188 #region Internal Properties
189 internal int Indent {
190 get {
191 return indent;
194 set {
195 indent = value;
196 recalc = true;
200 internal int HangingIndent {
201 get {
202 return hanging_indent;
205 set {
206 hanging_indent = value;
207 recalc = true;
211 internal int RightIndent {
212 get {
213 return right_indent;
216 set {
217 right_indent = value;
218 recalc = true;
223 internal int Height {
224 get {
225 return height;
228 set {
229 height = value;
233 internal int LineNo {
234 get {
235 return line_no;
238 set {
239 line_no = value;
243 internal string Text {
244 get {
245 return text.ToString();
248 set {
249 text = new StringBuilder(value, value.Length > DEFAULT_TEXT_LEN ? value.Length : DEFAULT_TEXT_LEN);
253 internal HorizontalAlignment Alignment {
254 get {
255 return alignment;
258 set {
259 if (alignment != value) {
260 alignment = value;
261 recalc = true;
265 #if no
266 internal StringBuilder Text {
267 get {
268 return text;
271 set {
272 text = value;
275 #endif
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) {
281 int length;
282 float[] new_widths;
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;
292 } else {
293 new_widths = new float[space * 2 + 1];
294 space *= 2;
296 widths.CopyTo(new_widths, 0);
298 widths = new_widths;
302 internal void Streamline(int lines) {
303 LineTag current;
304 LineTag next;
306 current = this.tags;
307 next = current.next;
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)) {
312 tags = next;
313 tags.previous = null;
314 current = next;
315 next = current.next;
318 if (next == null) {
319 return;
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;
330 next = current.next;
331 continue;
334 if (current.Combine(next)) {
335 next = current.next;
336 continue;
339 current = current.next;
340 next = 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) {
346 LineTag tag;
348 if (pos == 0) {
349 return tags;
352 tag = this.tags;
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);
362 tag = tag.next;
364 return null;
367 /// <summary>
368 /// Recalculate a single line using the same char for every character in the line
369 /// </summary>
371 internal bool RecalculatePasswordLine(Graphics g, Document doc) {
372 LineTag tag;
373 int pos;
374 int len;
375 float w;
376 bool ret;
377 int descent;
379 pos = 0;
380 len = this.text.Length;
381 tag = this.tags;
382 ascent = 0;
383 tag.shift = 0;
384 tag.width = 0;
386 this.recalc = false;
387 widths[0] = indent;
388 tag.X = indent;
390 w = g.MeasureString(doc.password_char, tags.font, 10000, string_format).Width;
392 if (this.height != (int)tag.font.Height) {
393 ret = true;
394 } else {
395 ret = false;
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;
404 while (pos < len) {
405 tag.width += w;
406 pos++;
407 widths[pos] = widths[pos-1] + w;
410 return ret;
413 /// <summary>
414 /// Go through all tags on a line and recalculate all size-related values;
415 /// returns true if lineheight changed
416 /// </summary>
417 internal bool RecalculateLine(Graphics g, Document doc) {
418 LineTag tag;
419 int pos;
420 int len;
421 SizeF size;
422 float w;
423 int prev_height;
424 bool retval;
425 bool wrapped;
426 Line line;
427 int wrap_pos;
428 float wrap_width;
430 pos = 0;
431 len = this.text.Length;
432 tag = this.tags;
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
436 tag.shift = 0;
437 tag.width = 0;
439 if (this.soft_break) {
440 widths[0] = hanging_indent;
441 } else {
442 widths[0] = indent;
445 this.recalc = false;
446 retval = false;
447 wrapped = false;
449 wrap_pos = 0;
450 wrap_width = 0;
452 while (pos < len) {
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
456 tag.width = 0;
457 tag.ascent = 0;
458 if (tag.previous != null) {
459 tag.X = tag.previous.X;
460 } else {
461 tag.X = (int)widths[pos];
463 tag = tag.next;
464 tag.width = 0;
465 tag.shift = 0;
468 w = size.Width;
470 if (Char.IsWhiteSpace(text[pos])) {
471 wrap_pos = pos + 1;
472 wrap_width = tag.width + w;
475 if (doc.wrap) {
476 if ((wrap_pos > 0) && (wrap_pos != len) && (widths[pos] + w) + 5 > (doc.viewport_width - this.right_indent)) {
477 pos = wrap_pos;
478 tag.width = wrap_width;
479 doc.Split(this, tag, pos, this.soft_break);
480 this.soft_break = true;
481 len = this.text.Length;
482 retval = true;
483 wrapped = true;
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;
490 retval = true;
491 wrapped = true;
495 // Contract all soft lines that follow back into our line
496 if (!wrapped) {
497 tag.width += w;
499 pos++;
501 widths[pos] = widths[pos-1] + w;
503 if (pos == len) {
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;
509 retval = true;
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) {
524 int descent;
526 XplatUI.GetFontMetrics(g, tag.font, out tag.ascent, out descent);
529 if (tag.ascent > this.ascent) {
530 LineTag t;
532 // We have a tag that has a taller ascent than the line;
534 t = tags;
535 while (t != tag) {
536 t.shift = tag.ascent - t.ascent;
537 t = t.next;
540 // Save on our line
541 this.ascent = tag.ascent;
542 } else {
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];
549 } else {
550 tag.X = tag.previous.X + (int)tag.previous.width;
553 tag = tag.next;
554 if (tag != null) {
555 tag.width = 0;
556 tag.shift = 0;
557 wrap_pos = pos;
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) {
569 retval = true;
571 return retval;
573 #endregion // Internal Methods
575 #region Administrative
576 public int CompareTo(object obj) {
577 if (obj == null) {
578 return 1;
581 if (! (obj is Line)) {
582 throw new ArgumentException("Object is not of type Line", "obj");
585 if (line_no < ((Line)obj).line_no) {
586 return -1;
587 } else if (line_no > ((Line)obj).line_no) {
588 return 1;
589 } else {
590 return 0;
594 public object Clone() {
595 Line clone;
597 clone = new Line();
599 clone.text = text;
601 if (left != null) {
602 clone.left = (Line)left.Clone();
605 if (left != null) {
606 clone.left = (Line)left.Clone();
609 return clone;
612 internal object CloneLine() {
613 Line clone;
615 clone = new Line();
617 clone.text = text;
619 return clone;
622 public override bool Equals(object obj) {
623 if (obj == null) {
624 return false;
627 if (!(obj is Line)) {
628 return false;
631 if (obj == this) {
632 return true;
635 if (line_no == ((Line)obj).line_no) {
636 return true;
639 return false;
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 {
654 #region Structures
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 {
658 internal Line line;
659 internal LineTag tag;
660 internal int pos;
661 internal int height;
663 public static bool operator<(Marker lhs, Marker rhs) {
664 if (lhs.line.line_no < rhs.line.line_no) {
665 return true;
668 if (lhs.line.line_no == rhs.line.line_no) {
669 if (lhs.pos < rhs.pos) {
670 return true;
673 return false;
676 public static bool operator>(Marker lhs, Marker rhs) {
677 if (lhs.line.line_no > rhs.line.line_no) {
678 return true;
681 if (lhs.line.line_no == rhs.line.line_no) {
682 if (lhs.pos > rhs.pos) {
683 return true;
686 return false;
689 public static bool operator==(Marker lhs, Marker rhs) {
690 if ((lhs.line.line_no == rhs.line.line_no) && (lhs.pos == rhs.pos)) {
691 return true;
693 return false;
696 public static bool operator!=(Marker lhs, Marker rhs) {
697 if ((lhs.line.line_no != rhs.line.line_no) || (lhs.pos != rhs.pos)) {
698 return true;
700 return false;
703 public void Combine(Line move_to_line, int move_to_line_length) {
704 line = move_to_line;
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) {
711 line = move_to_line;
712 pos -= 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;
733 private int lines;
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;
749 internal bool wrap;
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
778 #region Constructors
779 internal Document(TextBoxBase owner) {
780 lines = 0;
782 this.owner = owner;
784 multiline = true;
785 password_char = "";
786 calc_pass = false;
787 no_recalc = false;
788 recalc_pending = false;
790 // Tree related stuff
791 sentinel = new Line();
792 sentinel.color = LineColor.Black;
794 document = sentinel;
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);
802 l.soft_break = true;
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;
817 caret.pos = 0;
818 caret.tag = caret.line.tags;
820 viewport_x = 0;
821 viewport_y = 0;
823 crlf_size = 2;
825 // Default selection is empty
827 document_id = random.Next();
829 #endregion
831 #region Internal Properties
832 internal Line Root {
833 get {
834 return document;
837 set {
838 document = value;
842 internal int Lines {
843 get {
844 return lines;
848 internal Line CaretLine {
849 get {
850 return caret.line;
854 internal int CaretPosition {
855 get {
856 return caret.pos;
860 internal Point Caret {
861 get {
862 return new Point((int)caret.tag.line.widths[caret.pos] + caret.line.align_shift, caret.line.Y);
866 internal LineTag CaretTag {
867 get {
868 return caret.tag;
871 set {
872 caret.tag = value;
876 internal int CRLFSize {
877 get {
878 return crlf_size;
881 set {
882 crlf_size = value;
886 internal string PasswordChar {
887 get {
888 return password_char;
891 set {
892 password_char = value;
893 if ((password_char.Length != 0) && (password_char[0] != '\0')) {
894 char ch;
896 calc_pass = true;
897 ch = value[0];
898 password_cache = new StringBuilder(1024);
899 for (int i = 0; i < 1024; i++) {
900 password_cache.Append(ch);
902 } else {
903 calc_pass = false;
904 password_cache = null;
909 internal int ViewPortX {
910 get {
911 return viewport_x;
914 set {
915 viewport_x = value;
919 internal int Length {
920 get {
921 return char_count + lines - 1; // Add \n for each line but the last
925 private int CharCount {
926 get {
927 return char_count;
930 set {
931 char_count = value;
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 {
942 get {
943 return no_recalc;
946 set {
947 no_recalc = value;
948 if (!no_recalc && recalc_pending) {
949 RecalculateDocument(owner.CreateGraphicsInternal(), recalc_start, recalc_end, recalc_optimize);
950 recalc_pending = false;
955 internal int ViewPortY {
956 get {
957 return viewport_y;
960 set {
961 viewport_y = value;
965 internal int ViewPortWidth {
966 get {
967 return viewport_width;
970 set {
971 viewport_width = value;
975 internal int ViewPortHeight {
976 get {
977 return viewport_height;
980 set {
981 viewport_height = value;
986 internal int Width {
987 get {
988 return this.document_x;
992 internal int Height {
993 get {
994 return this.document_y;
998 internal bool SelectionVisible {
999 get {
1000 return selection_visible;
1004 internal bool Wrap {
1005 get {
1006 return wrap;
1009 set {
1010 wrap = value;
1014 #endregion // Internal Properties
1016 #region Private Methods
1017 // For debugging
1018 internal int DumpTree(Line line, bool with_tags) {
1019 int total;
1021 total = 1;
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("");
1041 if (with_tags) {
1042 LineTag tag;
1043 int count;
1044 int length;
1046 tag = line.tags;
1047 count = 1;
1048 length = 0;
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");
1058 tag = tag.next;
1059 if (tag != null) {
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);
1074 } else {
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);
1084 } else {
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));
1104 return total;
1107 private void SetSelectionVisible (bool value)
1109 selection_visible = value;
1111 // cursor and selection are enemies, we can't have both in the same room at the same time
1112 if (owner.IsHandleCreated)
1113 XplatUI.CaretVisible (owner.Handle, !selection_visible);
1116 private void DecrementLines(int line_no) {
1117 int current;
1119 current = line_no;
1120 while (current <= lines) {
1121 GetLine(current).line_no--;
1122 current++;
1124 return;
1127 private void IncrementLines(int line_no) {
1128 int current;
1130 current = this.lines;
1131 while (current >= line_no) {
1132 GetLine(current).line_no++;
1133 current--;
1135 return;
1138 private void RebalanceAfterAdd(Line line1) {
1139 Line line2;
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;
1150 } else {
1151 if (line1 == line1.parent.right) {
1152 line1 = line1.parent;
1153 RotateLeft(line1);
1156 line1.parent.color = LineColor.Black;
1157 line1.parent.parent.color = LineColor.Red;
1159 RotateRight(line1.parent.parent);
1161 } else {
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;
1169 } else {
1170 if (line1 == line1.parent.left) {
1171 line1 = line1.parent;
1172 RotateRight(line1);
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) {
1185 Line line2;
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;
1199 } else {
1200 if (line2.right.color == LineColor.Black) {
1201 line2.left.color = LineColor.Black;
1202 line2.color = LineColor.Red;
1203 RotateRight(line2);
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);
1210 line1 = document;
1212 } else {
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;
1223 } else {
1224 if (line2.left.color == LineColor.Black) {
1225 line2.right.color = LineColor.Black;
1226 line2.color = LineColor.Red;
1227 RotateLeft(line2);
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);
1234 line1 = document;
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;
1257 } else {
1258 line1.parent.right = line2;
1260 } else {
1261 document = line2;
1264 line2.left = line1;
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;
1286 } else {
1287 line1.parent.left = line2;
1289 } else {
1290 document = 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) {
1302 return;
1305 if (no_recalc) {
1306 recalc_start = line.line_no;
1307 recalc_end = line.line_no;
1308 recalc_optimize = true;
1309 recalc_pending = true;
1310 return;
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));
1319 } else {
1320 // The tag was above the visible area, draw everything
1321 owner.Invalidate();
1323 } else {
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));
1327 break;
1330 case HorizontalAlignment.Center: {
1331 owner.Invalidate(new Rectangle(0, line.Y - viewport_y, viewport_width, line.height + 1));
1332 break;
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));
1337 break;
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) {
1347 return;
1350 if (no_recalc) {
1351 recalc_start = line.line_no;
1352 recalc_end = line.line_no + line_count - 1;
1353 recalc_optimize = true;
1354 recalc_pending = true;
1355 return;
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));
1364 } else {
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)");
1367 owner.Invalidate();
1369 } else {
1370 Line end_line;
1372 end_line = GetLine(line.line_no + line_count -1);
1373 if (end_line == null) {
1374 end_line = line;
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;
1388 lines = 0;
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;
1406 char_count = 0;
1408 viewport_x = 0;
1409 viewport_y = 0;
1411 document_x = 0;
1412 document_y = 0;
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);
1424 caret.line = line;
1425 caret.pos = 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) {
1441 return;
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);
1462 DisplayCaret ();
1466 internal void CaretLostFocus() {
1467 if (!owner.IsHandleCreated) {
1468 return;
1470 XplatUI.DestroyCaret(owner.Handle);
1473 internal void AlignCaret() {
1474 if (!owner.IsHandleCreated) {
1475 return;
1478 undo.RecordCursor();
1480 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1481 caret.height = caret.tag.height;
1483 if (owner.Focused) {
1484 XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
1485 XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
1486 DisplayCaret ();
1489 if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1492 internal void UpdateCaret() {
1493 if (!owner.IsHandleCreated || caret.tag == null) {
1494 return;
1497 undo.RecordCursor();
1499 if (caret.tag.height != caret.height) {
1500 caret.height = caret.tag.height;
1501 if (owner.Focused) {
1502 XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
1506 XplatUI.SetCaretPos(owner.Handle, (int)caret.tag.line.widths[caret.pos] + caret.line.align_shift - viewport_x, caret.line.Y + caret.tag.shift - viewport_y + caret_shift);
1508 DisplayCaret ();
1510 if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1513 internal void DisplayCaret() {
1514 if (!owner.IsHandleCreated) {
1515 return;
1518 if (owner.Focused && !selection_visible) {
1519 XplatUI.CaretVisible(owner.Handle, true);
1523 internal void HideCaret() {
1524 if (!owner.IsHandleCreated) {
1525 return;
1528 if (owner.Focused) {
1529 XplatUI.CaretVisible(owner.Handle, false);
1533 internal void MoveCaret(CaretDirection direction) {
1534 // FIXME should we use IsWordSeparator to detect whitespace, instead
1535 // of looking for actual spaces in the Word move cases?
1537 bool nowrap = false;
1538 switch(direction) {
1539 case CaretDirection.CharForwardNoWrap:
1540 nowrap = true;
1541 goto case CaretDirection.CharForward;
1542 case CaretDirection.CharForward: {
1543 caret.pos++;
1544 if (caret.pos > caret.line.text.Length) {
1545 if (multiline && !nowrap) {
1546 // Go into next line
1547 if (caret.line.line_no < this.lines) {
1548 caret.line = GetLine(caret.line.line_no+1);
1549 caret.pos = 0;
1550 caret.tag = caret.line.tags;
1551 } else {
1552 caret.pos--;
1554 } else {
1555 // Single line; we stay where we are
1556 caret.pos--;
1558 } else {
1559 if ((caret.tag.start - 1 + caret.tag.length) < caret.pos) {
1560 caret.tag = caret.tag.next;
1563 UpdateCaret();
1564 return;
1567 case CaretDirection.CharBackNoWrap:
1568 nowrap = true;
1569 goto case CaretDirection.CharBack;
1570 case CaretDirection.CharBack: {
1571 if (caret.pos > 0) {
1572 // caret.pos--; // folded into the if below
1573 if (--caret.pos > 0) {
1574 if (caret.tag.start > caret.pos) {
1575 caret.tag = caret.tag.previous;
1578 } else {
1579 if (caret.line.line_no > 1 && !nowrap) {
1580 caret.line = GetLine(caret.line.line_no - 1);
1581 caret.pos = caret.line.text.Length;
1582 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1585 UpdateCaret();
1586 return;
1589 case CaretDirection.WordForward: {
1590 int len;
1592 len = caret.line.text.Length;
1593 if (caret.pos < len) {
1594 while ((caret.pos < len) && (caret.line.text[caret.pos] != ' ')) {
1595 caret.pos++;
1597 if (caret.pos < len) {
1598 // Skip any whitespace
1599 while ((caret.pos < len) && (caret.line.text[caret.pos] == ' ')) {
1600 caret.pos++;
1603 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1604 } else {
1605 if (caret.line.line_no < this.lines) {
1606 caret.line = GetLine(caret.line.line_no + 1);
1607 caret.pos = 0;
1608 caret.tag = caret.line.tags;
1611 UpdateCaret();
1612 return;
1615 case CaretDirection.WordBack: {
1616 if (caret.pos > 0) {
1617 caret.pos--;
1619 while ((caret.pos > 0) && (caret.line.text[caret.pos] == ' ')) {
1620 caret.pos--;
1623 while ((caret.pos > 0) && (caret.line.text[caret.pos] != ' ')) {
1624 caret.pos--;
1627 if (caret.line.text.ToString(caret.pos, 1) == " ") {
1628 if (caret.pos != 0) {
1629 caret.pos++;
1630 } else {
1631 caret.line = GetLine(caret.line.line_no - 1);
1632 caret.pos = caret.line.text.Length;
1635 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1636 } else {
1637 if (caret.line.line_no > 1) {
1638 caret.line = GetLine(caret.line.line_no - 1);
1639 caret.pos = caret.line.text.Length;
1640 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1643 UpdateCaret();
1644 return;
1647 case CaretDirection.LineUp: {
1648 if (caret.line.line_no > 1) {
1649 int pixel;
1651 pixel = (int)caret.line.widths[caret.pos];
1652 PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
1654 DisplayCaret ();
1656 return;
1659 case CaretDirection.LineDown: {
1660 if (caret.line.line_no < lines) {
1661 int pixel;
1663 pixel = (int)caret.line.widths[caret.pos];
1664 PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
1666 DisplayCaret ();
1668 return;
1671 case CaretDirection.Home: {
1672 if (caret.pos > 0) {
1673 caret.pos = 0;
1674 caret.tag = caret.line.tags;
1675 UpdateCaret();
1677 return;
1680 case CaretDirection.End: {
1681 if (caret.pos < caret.line.text.Length) {
1682 caret.pos = caret.line.text.Length;
1683 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1684 UpdateCaret();
1686 return;
1689 case CaretDirection.PgUp: {
1691 int new_y, y_offset;
1693 if (viewport_y == 0) {
1695 // This should probably be handled elsewhere
1696 if (!(owner is RichTextBox)) {
1697 // Page down doesn't do anything in a regular TextBox
1698 // if the bottom of the document
1699 // is already visible, the page and the caret stay still
1700 return;
1703 // We're just placing the caret at the end of the document, no scrolling needed
1704 owner.vscroll.Value = 0;
1705 Line line = GetLine (1);
1706 PositionCaret (line, 0);
1709 y_offset = caret.line.Y - viewport_y;
1710 new_y = caret.line.Y - viewport_height;
1712 owner.vscroll.Value = Math.Max (new_y, 0);
1713 PositionCaret ((int)caret.line.widths[caret.pos], y_offset + viewport_y);
1714 return;
1717 case CaretDirection.PgDn: {
1718 int new_y, y_offset;
1720 if ((viewport_y + viewport_height) > document_y) {
1722 // This should probably be handled elsewhere
1723 if (!(owner is RichTextBox)) {
1724 // Page up doesn't do anything in a regular TextBox
1725 // if the bottom of the document
1726 // is already visible, the page and the caret stay still
1727 return;
1730 // We're just placing the caret at the end of the document, no scrolling needed
1731 owner.vscroll.Value = owner.vscroll.Maximum - viewport_height + 1;
1732 Line line = GetLine (lines);
1733 PositionCaret (line, line.Text.Length);
1736 y_offset = caret.line.Y - viewport_y;
1737 new_y = caret.line.Y + viewport_height;
1739 owner.vscroll.Value = Math.Min (new_y, owner.vscroll.Maximum - viewport_height + 1);
1740 PositionCaret ((int)caret.line.widths[caret.pos], y_offset + viewport_y);
1742 return;
1745 case CaretDirection.CtrlPgUp: {
1746 PositionCaret(0, viewport_y);
1747 DisplayCaret ();
1748 return;
1751 case CaretDirection.CtrlPgDn: {
1752 Line line;
1753 LineTag tag;
1754 int index;
1756 tag = FindTag(0, viewport_y + viewport_height, out index, false);
1757 if (tag.line.line_no > 1) {
1758 line = GetLine(tag.line.line_no - 1);
1759 } else {
1760 line = tag.line;
1762 PositionCaret(line, line.Text.Length);
1763 DisplayCaret ();
1764 return;
1767 case CaretDirection.CtrlHome: {
1768 caret.line = GetLine(1);
1769 caret.pos = 0;
1770 caret.tag = caret.line.tags;
1772 UpdateCaret();
1773 return;
1776 case CaretDirection.CtrlEnd: {
1777 caret.line = GetLine(lines);
1778 caret.pos = caret.line.text.Length;
1779 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1781 UpdateCaret();
1782 return;
1785 case CaretDirection.SelectionStart: {
1786 caret.line = selection_start.line;
1787 caret.pos = selection_start.pos;
1788 caret.tag = selection_start.tag;
1790 UpdateCaret();
1791 return;
1794 case CaretDirection.SelectionEnd: {
1795 caret.line = selection_end.line;
1796 caret.pos = selection_end.pos;
1797 caret.tag = selection_end.tag;
1799 UpdateCaret();
1800 return;
1805 // Draw the document
1806 internal void Draw(Graphics g, Rectangle clip) {
1807 Line line; // Current line being drawn
1808 LineTag tag; // Current tag being drawn
1809 int start; // First line to draw
1810 int end; // Last line to draw
1811 StringBuilder text; // String representing the current line
1812 int line_no; //
1813 Brush disabled;
1814 Brush hilight;
1815 Brush hilight_text;
1817 // First, figure out from what line to what line we need to draw
1818 start = GetLineByPixel(clip.Top + viewport_y, false).line_no;
1819 end = GetLineByPixel(clip.Bottom + viewport_y, false).line_no;
1820 //Console.WriteLine("Starting drawing at line {0}, ending at line {1} (clip-bottom:{2})", start, end, clip.Bottom);
1822 // Now draw our elements; try to only draw those that are visible
1823 line_no = start;
1825 #if Debug
1826 DateTime n = DateTime.Now;
1827 Console.WriteLine("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
1828 #endif
1830 disabled = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorGrayText);
1831 hilight = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlight);
1832 hilight_text = ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorHighlightText);
1834 while (line_no <= end) {
1835 line = GetLine(line_no);
1836 #if not
1837 if (owner.backcolor_set || (owner.Enabled && !owner.read_only)) {
1838 g.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(owner.BackColor), new Rectangle(clip.Left, line.Y - viewport_y, clip.Width, line.Y - viewport_y + line.Height));
1839 } else {
1840 g.FillRectangle(ThemeEngine.Current.ResPool.GetSolidBrush(ThemeEngine.Current.ColorControl), new Rectangle(clip.Left, line.Y - viewport_y, clip.Width, line.Y - viewport_y + line.Height));
1842 #endif
1845 tag = line.tags;
1846 if (!calc_pass) {
1847 text = line.text;
1848 } else {
1849 // This fails if there's a password > 1024 chars...
1850 text = this.password_cache;
1852 while (tag != null) {
1853 if (tag.length == 0) {
1854 tag = tag.next;
1855 continue;
1858 if (((tag.X + tag.width) > (clip.Left - viewport_x)) || (tag.X < (clip.Right - viewport_x))) {
1859 // Check for selection
1860 if ((!selection_visible) || (!owner.ShowSelection) || (line_no < selection_start.line.line_no) || (line_no > selection_end.line.line_no)) {
1861 // regular drawing, no selection to deal with
1862 //g.DrawString(s.Substring(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
1863 if (owner.is_enabled) {
1864 g.DrawString(text.ToString(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
1865 } else {
1866 Color a;
1867 Color b;
1869 a = ((SolidBrush)tag.color).Color;
1870 b = ThemeEngine.Current.ColorWindowText;
1872 if ((a.R == b.R) && (a.G == b.G) && (a.B == b.B)) {
1873 g.DrawString(text.ToString(tag.start-1, tag.length), tag.font, disabled, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
1874 } else {
1875 g.DrawString(text.ToString(tag.start-1, tag.length), tag.font, tag.color, tag.X + line.align_shift - viewport_x, line.Y + tag.shift - viewport_y, StringFormat.GenericTypographic);
1878 } else {
1879 // we might have to draw our selection
1880 if ((line_no != selection_start.line.line_no) && (line_no != selection_end.line.line_no)) {
1881 // Special case, whole line is selected, draw this tag selected
1882 g.FillRectangle(
1883 hilight, // Brush
1884 tag.X + line.align_shift - viewport_x, // X
1885 line.Y + tag.shift - viewport_y, // Y
1886 line.widths[tag.start + tag.length - 1], // width
1887 tag.height // Height
1890 g.DrawString(
1891 //s.Substring(tag.start-1, tag.length), // String
1892 text.ToString(tag.start-1, tag.length), // String
1893 tag.font, // Font
1894 hilight_text, // Brush
1895 tag.X + line.align_shift - viewport_x, // X
1896 line.Y + tag.shift - viewport_y, // Y
1897 StringFormat.GenericTypographic);
1898 } else {
1899 bool highlight;
1900 bool partial;
1902 highlight = false;
1903 partial = false;
1905 // One or more, but not all tags on the line are selected
1906 if ((selection_start.tag == tag) && (selection_end.tag == tag)) {
1907 // Single tag selected, draw "normalSELECTEDnormal"
1908 partial = true;
1909 // First, the regular part
1910 g.DrawString(
1911 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1912 text.ToString(tag.start - 1, selection_start.pos - tag.start + 1), // String
1913 tag.font, // Font
1914 tag.color, // Brush
1915 tag.X + line.align_shift - viewport_x, // X
1916 line.Y + tag.shift - viewport_y, // Y
1917 StringFormat.GenericTypographic);
1919 // Now the highlight
1920 g.FillRectangle(
1921 hilight, // Brush
1922 line.widths[selection_start.pos] + line.align_shift - viewport_x, // X
1923 line.Y + tag.shift - viewport_y, // Y
1924 line.widths[selection_end.pos] - line.widths[selection_start.pos], // Width
1925 tag.height); // Height
1927 g.DrawString(
1928 //s.Substring(selection_start.pos, selection_end.pos - selection_start.pos), // String
1929 text.ToString(selection_start.pos, selection_end.pos - selection_start.pos), // String
1930 tag.font, // Font
1931 hilight_text, // Brush
1932 line.widths[selection_start.pos] + line.align_shift - viewport_x, // X
1933 line.Y + tag.shift - viewport_y, // Y
1934 StringFormat.GenericTypographic);
1936 // And back to the regular
1937 g.DrawString(
1938 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1939 text.ToString(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1940 tag.font, // Font
1941 tag.color, // Brush
1942 line.widths[selection_end.pos] + line.align_shift - viewport_x, // X
1943 line.Y + tag.shift - viewport_y, // Y
1944 StringFormat.GenericTypographic);
1946 } else if (selection_start.tag == tag) {
1947 partial = true;
1949 // The highlighted part
1950 g.FillRectangle(
1951 hilight,
1952 line.widths[selection_start.pos] + line.align_shift - viewport_x,
1953 line.Y + tag.shift - viewport_y,
1954 line.widths[tag.start + tag.length - 1] - line.widths[selection_start.pos],
1955 tag.height);
1957 g.DrawString(
1958 //s.Substring(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
1959 text.ToString(selection_start.pos, tag.start + tag.length - selection_start.pos - 1), // String
1960 tag.font, // Font
1961 hilight_text, // Brush
1962 line.widths[selection_start.pos] + line.align_shift - viewport_x, // X
1963 line.Y + tag.shift - viewport_y, // Y
1964 StringFormat.GenericTypographic);
1966 // The regular part
1967 g.DrawString(
1968 //s.Substring(tag.start - 1, selection_start.pos - tag.start + 1), // String
1969 text.ToString(tag.start - 1, selection_start.pos - tag.start + 1), // String
1970 tag.font, // Font
1971 tag.color, // Brush
1972 tag.X + line.align_shift - viewport_x, // X
1973 line.Y + tag.shift - viewport_y, // Y
1974 StringFormat.GenericTypographic);
1975 } else if (selection_end.tag == tag) {
1976 partial = true;
1978 // The highlighted part
1979 g.FillRectangle(
1980 hilight,
1981 tag.X + line.align_shift - viewport_x,
1982 line.Y + tag.shift - viewport_y,
1983 line.widths[selection_end.pos] - line.widths[tag.start - 1],
1984 tag.height);
1986 g.DrawString(
1987 //s.Substring(tag.start - 1, selection_end.pos - tag.start + 1), // String
1988 text.ToString(tag.start - 1, selection_end.pos - tag.start + 1), // String
1989 tag.font, // Font
1990 hilight_text, // Brush
1991 tag.X + line.align_shift - viewport_x, // X
1992 line.Y + tag.shift - viewport_y, // Y
1993 StringFormat.GenericTypographic);
1995 // The regular part
1996 g.DrawString(
1997 //s.Substring(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1998 text.ToString(selection_end.pos, tag.start + tag.length - selection_end.pos - 1), // String
1999 tag.font, // Font
2000 tag.color, // Brush
2001 line.widths[selection_end.pos] + line.align_shift - viewport_x, // X
2002 line.Y + tag.shift - viewport_y, // Y
2003 StringFormat.GenericTypographic);
2004 } else {
2005 // no partially selected tags here, simple checks...
2006 if (selection_start.line == line) {
2007 int begin;
2008 int stop;
2010 begin = tag.start - 1;
2011 stop = tag.start + tag.length - 1;
2012 if (selection_end.line == line) {
2013 if ((begin >= selection_start.pos) && (stop < selection_end.pos)) {
2014 highlight = true;
2016 } else {
2017 if (stop > selection_start.pos) {
2018 highlight = true;
2021 } else if (selection_end.line == line) {
2022 if ((tag.start - 1) < selection_end.pos) {
2023 highlight = true;
2028 if (!partial) {
2029 if (highlight) {
2030 g.FillRectangle(
2031 hilight,
2032 tag.X + line.align_shift - viewport_x,
2033 line.Y + tag.shift - viewport_y,
2034 line.widths[tag.start + tag.length - 1] - line.widths[tag.start - 1],
2035 tag.height);
2037 g.DrawString(
2038 //s.Substring(tag.start-1, tag.length), // String
2039 text.ToString(tag.start-1, tag.length), // String
2040 tag.font, // Font
2041 hilight_text, // Brush
2042 tag.X + line.align_shift - viewport_x, // X
2043 line.Y + tag.shift - viewport_y, // Y
2044 StringFormat.GenericTypographic);
2045 } else {
2046 g.DrawString(
2047 //s.Substring(tag.start-1, tag.length), // String
2048 text.ToString(tag.start-1, tag.length), // String
2049 tag.font, // Font
2050 tag.color, // Brush
2051 tag.X + line.align_shift - viewport_x, // X
2052 line.Y + tag.shift - viewport_y, // Y
2053 StringFormat.GenericTypographic);
2061 tag = tag.next;
2064 line_no++;
2066 #if Debug
2067 n = DateTime.Now;
2068 Console.WriteLine("Finished drawing: {0}s {1}ms", n.Second, n.Millisecond);
2069 #endif
2073 private void InsertLineString (Line line, int pos, string s)
2075 bool carriage_return = false;
2077 if (s.EndsWith ("\r")) {
2078 s = s.Substring (0, s.Length - 1);
2079 carriage_return = true;
2082 InsertString (line, pos, s);
2084 if (carriage_return) {
2085 Line l = GetLine (line.line_no);
2086 l.carriage_return = true;
2090 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
2091 internal void Insert(Line line, int pos, bool update_caret, string s) {
2092 int break_index;
2093 int base_line;
2094 int old_line_count;
2095 int count = 1;
2096 LineTag tag = LineTag.FindTag (line, pos);
2098 NoRecalc = true;
2099 undo.BeginCompoundAction ();
2101 base_line = line.line_no;
2102 old_line_count = lines;
2104 break_index = s.IndexOf ('\n');
2106 // Bump the text at insertion point a line down if we're inserting more than one line
2107 if (break_index > -1) {
2108 Split(line, pos);
2109 line.soft_break = false;
2110 // Remainder of start line is now in base_line + 1
2113 if (break_index == -1)
2114 break_index = s.Length;
2116 InsertLineString (line, pos, s.Substring (0, break_index));
2117 break_index++;
2119 while (break_index < s.Length) {
2120 bool soft = false;
2121 int next_break = s.IndexOf ('\n', break_index);
2122 int adjusted_next_break;
2123 bool carriage_return = false;
2125 if (next_break == -1) {
2126 next_break = s.Length;
2127 soft = true;
2130 adjusted_next_break = next_break;
2131 if (s [next_break - 1] == '\r') {
2132 adjusted_next_break--;
2133 carriage_return = true;
2136 string line_text = s.Substring (break_index, adjusted_next_break - break_index);
2137 Add (base_line + count, line_text, line.alignment, tag.font, tag.color);
2139 if (carriage_return) {
2140 Line last = GetLine (base_line + count);
2141 last.carriage_return = true;
2143 if (soft)
2144 last.soft_break = true;
2145 } else if (soft) {
2146 Line last = GetLine (base_line + count);
2147 last.soft_break = true;
2150 count++;
2151 break_index = next_break + 1;
2154 NoRecalc = false;
2156 UpdateView(line, lines - old_line_count + 1, pos);
2158 if (update_caret) {
2159 // Move caret to the end of the inserted text
2160 Line l = GetLine (line.line_no + lines - old_line_count);
2161 PositionCaret(l, l.text.Length);
2162 DisplayCaret ();
2165 undo.EndCompoundAction ();
2168 // Inserts a character at the given position
2169 internal void InsertString(Line line, int pos, string s) {
2170 InsertString(line.FindTag(pos), pos, s);
2173 // Inserts a string at the given position
2174 internal void InsertString(LineTag tag, int pos, string s) {
2175 Line line;
2176 int len;
2178 len = s.Length;
2180 CharCount += len;
2182 line = tag.line;
2183 line.text.Insert(pos, s);
2184 tag.length += len;
2186 // TODO: sometimes getting a null tag here when pasting ???
2187 tag = tag.next;
2188 while (tag != null) {
2189 tag.start += len;
2190 tag = tag.next;
2192 line.Grow(len);
2193 line.recalc = true;
2195 UpdateView(line, pos);
2198 // Inserts a string at the caret position
2199 internal void InsertStringAtCaret(string s, bool move_caret) {
2200 LineTag tag;
2201 int len;
2203 len = s.Length;
2205 CharCount += len;
2207 caret.line.text.Insert(caret.pos, s);
2208 caret.tag.length += len;
2210 if (caret.tag.next != null) {
2211 tag = caret.tag.next;
2212 while (tag != null) {
2213 tag.start += len;
2214 tag = tag.next;
2217 caret.line.Grow(len);
2218 caret.line.recalc = true;
2220 UpdateView(caret.line, caret.pos);
2221 if (move_caret) {
2222 caret.pos += len;
2223 UpdateCaret();
2229 // Inserts a character at the given position
2230 internal void InsertChar(Line line, int pos, char ch) {
2231 InsertChar(line.FindTag(pos), pos, ch);
2234 // Inserts a character at the given position
2235 internal void InsertChar(LineTag tag, int pos, char ch) {
2236 Line line;
2238 CharCount++;
2240 line = tag.line;
2241 line.text.Insert(pos, ch);
2242 tag.length++;
2244 tag = tag.next;
2245 while (tag != null) {
2246 tag.start++;
2247 tag = tag.next;
2249 line.Grow(1);
2250 line.recalc = true;
2252 UpdateView(line, pos);
2255 // Inserts a character at the current caret position
2256 internal void InsertCharAtCaret(char ch, bool move_caret) {
2257 LineTag tag;
2259 CharCount++;
2261 caret.line.text.Insert(caret.pos, ch);
2262 caret.tag.length++;
2264 if (caret.tag.next != null) {
2265 tag = caret.tag.next;
2266 while (tag != null) {
2267 tag.start++;
2268 tag = tag.next;
2271 caret.line.Grow(1);
2272 caret.line.recalc = true;
2274 UpdateView(caret.line, caret.pos);
2275 if (move_caret) {
2276 caret.pos++;
2277 UpdateCaret();
2278 SetSelectionToCaret(true);
2282 internal void DeleteMultiline (Line start_line, int pos, int length)
2284 Marker start = new Marker ();
2285 Marker end = new Marker ();
2286 int start_index = LineTagToCharIndex (start_line, pos);
2288 start.line = start_line;
2289 start.pos = pos;
2290 start.tag = LineTag.FindTag (start_line, pos);
2292 CharIndexToLineTag (start_index + length, out end.line,
2293 out end.tag, out end.pos);
2295 if (start.line == end.line) {
2296 DeleteChars (start.tag, pos, end.pos - pos);
2297 } else {
2299 // Delete first and last lines
2300 DeleteChars (start.tag, start.pos, start.line.text.Length - start.pos);
2301 DeleteChars (end.line.tags, 0, end.pos);
2303 int current = start.line.line_no + 1;
2304 if (current < end.line.line_no) {
2305 for (int i = end.line.line_no - 1; i >= current; i--) {
2306 Delete (i);
2310 // BIG FAT WARNING - selection_end.line might be stale due
2311 // to the above Delete() call. DONT USE IT before hitting the end of this method!
2313 // Join start and end
2314 Combine (start.line.line_no, current);
2319 // Deletes n characters at the given position; it will not delete past line limits
2320 // pos is 0-based
2321 internal void DeleteChars(LineTag tag, int pos, int count) {
2322 Line line;
2323 bool streamline;
2325 streamline = false;
2326 line = tag.line;
2328 CharCount -= count;
2330 if (pos == line.text.Length) {
2331 return;
2334 line.text.Remove(pos, count);
2336 // Make sure the tag points to the right spot
2337 while ((tag != null) && (tag.start + tag.length - 1) <= pos) {
2338 tag = tag.next;
2341 if (tag == null) {
2342 return;
2345 // Check if we're crossing tag boundaries
2346 if ((pos + count) > (tag.start + tag.length - 1)) {
2347 int left;
2349 // We have to delete cross tag boundaries
2350 streamline = true;
2351 left = count;
2353 left -= tag.start + tag.length - pos - 1;
2354 tag.length -= tag.start + tag.length - pos - 1;
2356 tag = tag.next;
2357 while ((tag != null) && (left > 0)) {
2358 tag.start -= count - left;
2359 if (tag.length > left) {
2360 tag.length -= left;
2361 left = 0;
2362 } else {
2363 left -= tag.length;
2364 tag.length = 0;
2366 tag = tag.next;
2369 } else {
2370 // We got off easy, same tag
2372 tag.length -= count;
2374 if (tag.length == 0) {
2375 streamline = true;
2379 // Delete empty orphaned tags at the end
2380 LineTag walk = tag;
2381 while (walk != null && walk.next != null && walk.next.length == 0) {
2382 LineTag t = walk;
2383 walk.next = walk.next.next;
2384 if (walk.next != null)
2385 walk.next.previous = t;
2386 walk = walk.next;
2389 // Adjust the start point of any tags following
2390 if (tag != null) {
2391 tag = tag.next;
2392 while (tag != null) {
2393 tag.start -= count;
2394 tag = tag.next;
2398 line.recalc = true;
2399 if (streamline) {
2400 line.Streamline(lines);
2403 UpdateView(line, pos);
2406 // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
2407 internal void DeleteChar(LineTag tag, int pos, bool forward) {
2408 Line line;
2409 bool streamline;
2411 CharCount--;
2413 streamline = false;
2414 line = tag.line;
2416 if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true)) {
2417 return;
2421 if (forward) {
2422 line.text.Remove(pos, 1);
2424 while ((tag != null) && (tag.start + tag.length - 1) <= pos) {
2425 tag = tag.next;
2428 if (tag == null) {
2429 return;
2432 tag.length--;
2434 if (tag.length == 0) {
2435 streamline = true;
2437 } else {
2438 pos--;
2439 line.text.Remove(pos, 1);
2440 if (pos >= (tag.start - 1)) {
2441 tag.length--;
2442 if (tag.length == 0) {
2443 streamline = true;
2445 } else if (tag.previous != null) {
2446 tag.previous.length--;
2447 if (tag.previous.length == 0) {
2448 streamline = true;
2453 // Delete empty orphaned tags at the end
2454 LineTag walk = tag;
2455 while (walk != null && walk.next != null && walk.next.length == 0) {
2456 LineTag t = walk;
2457 walk.next = walk.next.next;
2458 if (walk.next != null)
2459 walk.next.previous = t;
2460 walk = walk.next;
2463 tag = tag.next;
2464 while (tag != null) {
2465 tag.start--;
2466 tag = tag.next;
2468 line.recalc = true;
2469 if (streamline) {
2470 line.Streamline(lines);
2473 UpdateView(line, pos);
2476 // Combine two lines
2477 internal void Combine(int FirstLine, int SecondLine) {
2478 Combine(GetLine(FirstLine), GetLine(SecondLine));
2481 internal void Combine(Line first, Line second) {
2482 LineTag last;
2483 int shift;
2485 // Combine the two tag chains into one
2486 last = first.tags;
2488 // Maintain the line ending style
2489 first.soft_break = second.soft_break;
2491 while (last.next != null) {
2492 last = last.next;
2495 last.next = second.tags;
2496 last.next.previous = last;
2498 shift = last.start + last.length - 1;
2500 // Fix up references within the chain
2501 last = last.next;
2502 while (last != null) {
2503 last.line = first;
2504 last.start += shift;
2505 last = last.next;
2508 // Combine both lines' strings
2509 first.text.Insert(first.text.Length, second.text.ToString());
2510 first.Grow(first.text.Length);
2512 // Remove the reference to our (now combined) tags from the doomed line
2513 second.tags = null;
2515 // Renumber lines
2516 DecrementLines(first.line_no + 2); // first.line_no + 1 will be deleted, so we need to start renumbering one later
2518 // Mop up
2519 first.recalc = true;
2520 first.height = 0; // This forces RecalcDocument/UpdateView to redraw from this line on
2521 first.Streamline(lines);
2523 // Update Caret, Selection, etc
2524 if (caret.line == second) {
2525 caret.Combine(first, shift);
2527 if (selection_anchor.line == second) {
2528 selection_anchor.Combine(first, shift);
2530 if (selection_start.line == second) {
2531 selection_start.Combine(first, shift);
2533 if (selection_end.line == second) {
2534 selection_end.Combine(first, shift);
2537 #if Debug
2538 Line check_first;
2539 Line check_second;
2541 check_first = GetLine(first.line_no);
2542 check_second = GetLine(check_first.line_no + 1);
2544 Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
2545 #endif
2547 this.Delete(second);
2549 #if Debug
2550 check_first = GetLine(first.line_no);
2551 check_second = GetLine(check_first.line_no + 1);
2553 Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
2554 #endif
2557 // Split the line at the position into two
2558 internal void Split(int LineNo, int pos) {
2559 Line line;
2560 LineTag tag;
2562 line = GetLine(LineNo);
2563 tag = LineTag.FindTag(line, pos);
2564 Split(line, tag, pos, false);
2567 internal void Split(Line line, int pos) {
2568 LineTag tag;
2570 tag = LineTag.FindTag(line, pos);
2571 Split(line, tag, pos, false);
2574 ///<summary>Split line at given tag and position into two lines</summary>
2575 ///<param name="soft">True if the split should be marked as 'soft', indicating that it can be contracted
2576 ///if more space becomes available on previous line</param>
2577 internal void Split(Line line, LineTag tag, int pos, bool soft) {
2578 LineTag new_tag;
2579 Line new_line;
2580 bool move_caret;
2581 bool move_sel_start;
2582 bool move_sel_end;
2584 move_caret = false;
2585 move_sel_start = false;
2586 move_sel_end = false;
2588 // Adjust selection and cursors
2589 if (soft && (caret.line == line) && (caret.pos >= pos)) {
2590 move_caret = true;
2592 if (selection_start.line == line && selection_start.pos > pos) {
2593 move_sel_start = true;
2596 if (selection_end.line == line && selection_end.pos > pos) {
2597 move_sel_end = true;
2600 // cover the easy case first
2601 if (pos == line.text.Length) {
2602 Add(line.line_no + 1, "", line.alignment, tag.font, tag.color);
2604 new_line = GetLine(line.line_no + 1);
2606 line.carriage_return = false;
2607 new_line.carriage_return = line.carriage_return;
2609 if (soft) {
2610 if (move_caret) {
2611 caret.line = new_line;
2612 caret.line.soft_break = true;
2613 caret.tag = new_line.tags;
2614 caret.pos = 0;
2615 } else {
2616 new_line.soft_break = true;
2620 if (move_sel_start) {
2621 selection_start.line = new_line;
2622 selection_start.pos = 0;
2623 selection_start.tag = new_line.tags;
2626 if (move_sel_end) {
2627 selection_end.line = new_line;
2628 selection_end.pos = 0;
2629 selection_end.tag = new_line.tags;
2631 return;
2634 // We need to move the rest of the text into the new line
2635 Add(line.line_no + 1, line.text.ToString(pos, line.text.Length - pos), line.alignment, tag.font, tag.color);
2637 // Now transfer our tags from this line to the next
2638 new_line = GetLine(line.line_no + 1);
2640 line.carriage_return = false;
2641 new_line.carriage_return = line.carriage_return;
2643 line.recalc = true;
2644 new_line.recalc = true;
2646 if ((tag.start - 1) == pos) {
2647 int shift;
2649 // We can simply break the chain and move the tag into the next line
2650 if (tag == line.tags) {
2651 new_tag = new LineTag(line, 1, 0);
2652 new_tag.font = tag.font;
2653 new_tag.color = tag.color;
2654 line.tags = new_tag;
2657 if (tag.previous != null) {
2658 tag.previous.next = null;
2660 new_line.tags = tag;
2661 tag.previous = null;
2662 tag.line = new_line;
2664 // Walk the list and correct the start location of the tags we just bumped into the next line
2665 shift = tag.start - 1;
2667 new_tag = tag;
2668 while (new_tag != null) {
2669 new_tag.start -= shift;
2670 new_tag.line = new_line;
2671 new_tag = new_tag.next;
2673 } else {
2674 int shift;
2676 new_tag = new LineTag(new_line, 1, tag.start - 1 + tag.length - pos);
2677 new_tag.next = tag.next;
2678 new_tag.font = tag.font;
2679 new_tag.color = tag.color;
2680 new_line.tags = new_tag;
2681 if (new_tag.next != null) {
2682 new_tag.next.previous = new_tag;
2684 tag.next = null;
2685 tag.length = pos - tag.start + 1;
2687 shift = pos;
2688 new_tag = new_tag.next;
2689 while (new_tag != null) {
2690 new_tag.start -= shift;
2691 new_tag.line = new_line;
2692 new_tag = new_tag.next;
2697 if (soft) {
2698 if (move_caret) {
2699 caret.line = new_line;
2700 caret.pos = caret.pos - pos;
2701 caret.tag = caret.line.FindTag(caret.pos);
2703 new_line.soft_break = true;
2706 if (move_sel_start) {
2707 selection_start.line = new_line;
2708 selection_start.pos = selection_start.pos - pos;
2709 selection_start.tag = new_line.FindTag(selection_start.pos);
2712 if (move_sel_end) {
2713 selection_end.line = new_line;
2714 selection_end.pos = selection_end.pos - pos;
2715 selection_end.tag = new_line.FindTag(selection_end.pos);
2718 CharCount -= line.text.Length - pos;
2719 line.text.Remove(pos, line.text.Length - pos);
2722 // Adds a line of text, with given font.
2723 // Bumps any line at that line number that already exists down
2724 internal void Add(int LineNo, string Text, Font font, Brush color) {
2725 Add(LineNo, Text, HorizontalAlignment.Left, font, color);
2728 internal void Add(int LineNo, string Text, HorizontalAlignment align, Font font, Brush color) {
2729 Line add;
2730 Line line;
2731 int line_no;
2733 CharCount += Text.Length;
2735 if (LineNo<1 || Text == null) {
2736 if (LineNo<1) {
2737 throw new ArgumentNullException("LineNo", "Line numbers must be positive");
2738 } else {
2739 throw new ArgumentNullException("Text", "Cannot insert NULL line");
2743 add = new Line(LineNo, Text, align, font, color);
2745 line = document;
2746 while (line != sentinel) {
2747 add.parent = line;
2748 line_no = line.line_no;
2750 if (LineNo > line_no) {
2751 line = line.right;
2752 } else if (LineNo < line_no) {
2753 line = line.left;
2754 } else {
2755 // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
2756 IncrementLines(line.line_no);
2757 line = line.left;
2761 add.left = sentinel;
2762 add.right = sentinel;
2764 if (add.parent != null) {
2765 if (LineNo > add.parent.line_no) {
2766 add.parent.right = add;
2767 } else {
2768 add.parent.left = add;
2770 } else {
2771 // Root node
2772 document = add;
2775 RebalanceAfterAdd(add);
2777 lines++;
2780 internal virtual void Clear() {
2781 lines = 0;
2782 CharCount = 0;
2783 document = sentinel;
2786 public virtual object Clone() {
2787 Document clone;
2789 clone = new Document(null);
2791 clone.lines = this.lines;
2792 clone.document = (Line)document.Clone();
2794 return clone;
2797 internal void Delete(int LineNo) {
2798 Line line;
2800 if (LineNo>lines) {
2801 return;
2804 line = GetLine(LineNo);
2806 CharCount -= line.text.Length;
2808 DecrementLines(LineNo + 1);
2809 Delete(line);
2812 internal void Delete(Line line1) {
2813 Line line2;// = new Line();
2814 Line line3;
2816 if ((line1.left == sentinel) || (line1.right == sentinel)) {
2817 line3 = line1;
2818 } else {
2819 line3 = line1.right;
2820 while (line3.left != sentinel) {
2821 line3 = line3.left;
2825 if (line3.left != sentinel) {
2826 line2 = line3.left;
2827 } else {
2828 line2 = line3.right;
2831 line2.parent = line3.parent;
2832 if (line3.parent != null) {
2833 if(line3 == line3.parent.left) {
2834 line3.parent.left = line2;
2835 } else {
2836 line3.parent.right = line2;
2838 } else {
2839 document = line2;
2842 if (line3 != line1) {
2843 LineTag tag;
2845 if (selection_start.line == line3) {
2846 selection_start.line = line1;
2849 if (selection_end.line == line3) {
2850 selection_end.line = line1;
2853 if (selection_anchor.line == line3) {
2854 selection_anchor.line = line1;
2857 if (caret.line == line3) {
2858 caret.line = line1;
2862 line1.alignment = line3.alignment;
2863 line1.ascent = line3.ascent;
2864 line1.hanging_indent = line3.hanging_indent;
2865 line1.height = line3.height;
2866 line1.indent = line3.indent;
2867 line1.line_no = line3.line_no;
2868 line1.recalc = line3.recalc;
2869 line1.right_indent = line3.right_indent;
2870 line1.soft_break = line3.soft_break;
2871 line1.space = line3.space;
2872 line1.tags = line3.tags;
2873 line1.text = line3.text;
2874 line1.widths = line3.widths;
2875 line1.Y = line3.Y;
2877 tag = line1.tags;
2878 while (tag != null) {
2879 tag.line = line1;
2880 tag = tag.next;
2884 if (line3.color == LineColor.Black)
2885 RebalanceAfterDelete(line2);
2887 this.lines--;
2890 // Invalidate a section of the document to trigger redraw
2891 internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
2892 Line l1;
2893 Line l2;
2894 int p1;
2895 int p2;
2897 if ((start == end) && (start_pos == end_pos)) {
2898 return;
2901 if (end_pos == -1) {
2902 end_pos = end.text.Length;
2905 // figure out what's before what so the logic below is straightforward
2906 if (start.line_no < end.line_no) {
2907 l1 = start;
2908 p1 = start_pos;
2910 l2 = end;
2911 p2 = end_pos;
2912 } else if (start.line_no > end.line_no) {
2913 l1 = end;
2914 p1 = end_pos;
2916 l2 = start;
2917 p2 = start_pos;
2918 } else {
2919 if (start_pos < end_pos) {
2920 l1 = start;
2921 p1 = start_pos;
2923 l2 = end;
2924 p2 = end_pos;
2925 } else {
2926 l1 = end;
2927 p1 = end_pos;
2929 l2 = start;
2930 p2 = start_pos;
2933 #if Debug
2934 Console.WriteLine("Invaliding from {0}:{1} to {2}:{3}", l1.line_no, p1, l2.line_no, p2);
2935 #endif
2937 owner.Invalidate(
2938 new Rectangle(
2939 (int)l1.widths[p1] + l1.align_shift - viewport_x,
2940 l1.Y - viewport_y,
2941 (int)l2.widths[p2] - (int)l1.widths[p1] + 1,
2942 l1.height
2945 return;
2948 #if Debug
2949 Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Start => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l1.widths[p1] + l1.align_shift - viewport_x, l1.Y - viewport_y, viewport_width, l1.height);
2950 #endif
2952 // Three invalidates:
2953 // First line from start
2954 owner.Invalidate(new Rectangle((int)l1.widths[p1] + l1.align_shift - viewport_x, l1.Y - viewport_y, viewport_width, l1.height));
2956 // lines inbetween
2957 if ((l1.line_no + 1) < l2.line_no) {
2958 int y;
2960 y = GetLine(l1.line_no + 1).Y;
2961 owner.Invalidate(new Rectangle(0, y - viewport_y, viewport_width, GetLine(l2.line_no).Y - y - viewport_y));
2963 #if Debug
2964 Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} Middle => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, 0, y - viewport_y, viewport_width, GetLine(l2.line_no).Y - y - viewport_y);
2965 #endif
2968 // Last line to end
2969 owner.Invalidate(new Rectangle((int)l2.widths[0] + l2.align_shift - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height));
2970 #if Debug
2971 Console.WriteLine("Invaliding from {0}:{1} to {2}:{3} End => x={4}, y={5}, {6}x{7}", l1.line_no, p1, l2.line_no, p2, (int)l2.widths[0] + l2.align_shift - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height);
2972 #endif
2975 /// <summary>Select text around caret</summary>
2976 internal void ExpandSelection(CaretSelection mode, bool to_caret) {
2977 if (to_caret) {
2978 // We're expanding the selection to the caret position
2979 switch(mode) {
2980 case CaretSelection.Line: {
2981 // Invalidate the selection delta
2982 if (caret > selection_prev) {
2983 Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length);
2984 } else {
2985 Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0);
2988 if (caret.line.line_no <= selection_anchor.line.line_no) {
2989 selection_start.line = caret.line;
2990 selection_start.tag = caret.line.tags;
2991 selection_start.pos = 0;
2993 selection_end.line = selection_anchor.line;
2994 selection_end.tag = selection_anchor.tag;
2995 selection_end.pos = selection_anchor.pos;
2997 selection_end_anchor = true;
2998 } else {
2999 selection_start.line = selection_anchor.line;
3000 selection_start.pos = selection_anchor.height;
3001 selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
3003 selection_end.line = caret.line;
3004 selection_end.tag = caret.line.tags;
3005 selection_end.pos = caret.line.text.Length;
3007 selection_end_anchor = false;
3009 selection_prev.line = caret.line;
3010 selection_prev.tag = caret.tag;
3011 selection_prev.pos = caret.pos;
3013 break;
3016 case CaretSelection.Word: {
3017 int start_pos;
3018 int end_pos;
3020 start_pos = FindWordSeparator(caret.line, caret.pos, false);
3021 end_pos = FindWordSeparator(caret.line, caret.pos, true);
3024 // Invalidate the selection delta
3025 if (caret > selection_prev) {
3026 Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos);
3027 } else {
3028 Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos);
3030 if (caret < selection_anchor) {
3031 selection_start.line = caret.line;
3032 selection_start.tag = caret.line.FindTag(start_pos);
3033 selection_start.pos = start_pos;
3035 selection_end.line = selection_anchor.line;
3036 selection_end.tag = selection_anchor.tag;
3037 selection_end.pos = selection_anchor.pos;
3039 selection_prev.line = caret.line;
3040 selection_prev.tag = caret.tag;
3041 selection_prev.pos = start_pos;
3043 selection_end_anchor = true;
3044 } else {
3045 selection_start.line = selection_anchor.line;
3046 selection_start.pos = selection_anchor.height;
3047 selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height);
3049 selection_end.line = caret.line;
3050 selection_end.tag = caret.line.FindTag(end_pos);
3051 selection_end.pos = end_pos;
3053 selection_prev.line = caret.line;
3054 selection_prev.tag = caret.tag;
3055 selection_prev.pos = end_pos;
3057 selection_end_anchor = false;
3059 break;
3062 case CaretSelection.Position: {
3063 SetSelectionToCaret(false);
3064 return;
3067 } else {
3068 // We're setting the selection 'around' the caret position
3069 switch(mode) {
3070 case CaretSelection.Line: {
3071 this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length);
3073 selection_start.line = caret.line;
3074 selection_start.tag = caret.line.tags;
3075 selection_start.pos = 0;
3077 selection_end.line = caret.line;
3078 selection_end.pos = caret.line.text.Length;
3079 selection_end.tag = caret.line.FindTag(selection_end.pos);
3081 selection_anchor.line = selection_end.line;
3082 selection_anchor.tag = selection_end.tag;
3083 selection_anchor.pos = selection_end.pos;
3084 selection_anchor.height = 0;
3086 selection_prev.line = caret.line;
3087 selection_prev.tag = caret.tag;
3088 selection_prev.pos = caret.pos;
3090 this.selection_end_anchor = true;
3092 break;
3095 case CaretSelection.Word: {
3096 int start_pos;
3097 int end_pos;
3099 start_pos = FindWordSeparator(caret.line, caret.pos, false);
3100 end_pos = FindWordSeparator(caret.line, caret.pos, true);
3102 this.Invalidate(selection_start.line, start_pos, caret.line, end_pos);
3104 selection_start.line = caret.line;
3105 selection_start.tag = caret.line.FindTag(start_pos);
3106 selection_start.pos = start_pos;
3108 selection_end.line = caret.line;
3109 selection_end.tag = caret.line.FindTag(end_pos);
3110 selection_end.pos = end_pos;
3112 selection_anchor.line = selection_end.line;
3113 selection_anchor.tag = selection_end.tag;
3114 selection_anchor.pos = selection_end.pos;
3115 selection_anchor.height = start_pos;
3117 selection_prev.line = caret.line;
3118 selection_prev.tag = caret.tag;
3119 selection_prev.pos = caret.pos;
3121 this.selection_end_anchor = true;
3123 break;
3128 SetSelectionVisible (!(selection_start == selection_end));
3131 internal void SetSelectionToCaret(bool start) {
3132 if (start) {
3133 // Invalidate old selection; selection is being reset to empty
3134 this.Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3136 selection_start.line = caret.line;
3137 selection_start.tag = caret.tag;
3138 selection_start.pos = caret.pos;
3140 // start always also selects end
3141 selection_end.line = caret.line;
3142 selection_end.tag = caret.tag;
3143 selection_end.pos = caret.pos;
3145 selection_anchor.line = caret.line;
3146 selection_anchor.tag = caret.tag;
3147 selection_anchor.pos = caret.pos;
3148 } else {
3149 // Invalidate from previous end to caret (aka new end)
3150 if (selection_end_anchor) {
3151 if (selection_start != caret) {
3152 this.Invalidate(selection_start.line, selection_start.pos, caret.line, caret.pos);
3154 } else {
3155 if (selection_end != caret) {
3156 this.Invalidate(selection_end.line, selection_end.pos, caret.line, caret.pos);
3160 if (caret < selection_anchor) {
3161 selection_start.line = caret.line;
3162 selection_start.tag = caret.tag;
3163 selection_start.pos = caret.pos;
3165 selection_end.line = selection_anchor.line;
3166 selection_end.tag = selection_anchor.tag;
3167 selection_end.pos = selection_anchor.pos;
3169 selection_end_anchor = true;
3170 } else {
3171 selection_start.line = selection_anchor.line;
3172 selection_start.tag = selection_anchor.tag;
3173 selection_start.pos = selection_anchor.pos;
3175 selection_end.line = caret.line;
3176 selection_end.tag = caret.tag;
3177 selection_end.pos = caret.pos;
3179 selection_end_anchor = false;
3183 SetSelectionVisible (!(selection_start == selection_end));
3186 internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
3187 if (selection_visible) {
3188 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3191 if ((end.line_no < start.line_no) || ((end == start) && (end_pos <= start_pos))) {
3192 selection_start.line = end;
3193 selection_start.tag = LineTag.FindTag(end, end_pos);
3194 selection_start.pos = end_pos;
3196 selection_end.line = start;
3197 selection_end.tag = LineTag.FindTag(start, start_pos);
3198 selection_end.pos = start_pos;
3200 selection_end_anchor = true;
3201 } else {
3202 selection_start.line = start;
3203 selection_start.tag = LineTag.FindTag(start, start_pos);
3204 selection_start.pos = start_pos;
3206 selection_end.line = end;
3207 selection_end.tag = LineTag.FindTag(end, end_pos);
3208 selection_end.pos = end_pos;
3210 selection_end_anchor = false;
3213 selection_anchor.line = start;
3214 selection_anchor.tag = selection_start.tag;
3215 selection_anchor.pos = start_pos;
3217 if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
3218 SetSelectionVisible (false);
3219 } else {
3220 SetSelectionVisible (true);
3221 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3225 internal void SetSelectionStart(Line start, int start_pos) {
3226 // Invalidate from the previous to the new start pos
3227 Invalidate(selection_start.line, selection_start.pos, start, start_pos);
3229 selection_start.line = start;
3230 selection_start.pos = start_pos;
3231 selection_start.tag = LineTag.FindTag(start, start_pos);
3233 selection_anchor.line = start;
3234 selection_anchor.pos = start_pos;
3235 selection_anchor.tag = selection_start.tag;
3237 selection_end_anchor = false;
3240 if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
3241 SetSelectionVisible (true);
3242 } else {
3243 SetSelectionVisible (false);
3246 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3249 internal void SetSelectionStart(int character_index) {
3250 Line line;
3251 LineTag tag;
3252 int pos;
3254 if (character_index < 0) {
3255 return;
3258 CharIndexToLineTag(character_index, out line, out tag, out pos);
3259 SetSelectionStart(line, pos);
3262 internal void SetSelectionEnd(Line end, int end_pos) {
3264 if (end == selection_end.line && end_pos == selection_start.pos) {
3265 selection_anchor.line = selection_start.line;
3266 selection_anchor.tag = selection_start.tag;
3267 selection_anchor.pos = selection_start.pos;
3269 selection_end.line = selection_start.line;
3270 selection_end.tag = selection_start.tag;
3271 selection_end.pos = selection_start.pos;
3273 selection_end_anchor = false;
3274 } else if ((end.line_no < selection_anchor.line.line_no) || ((end == selection_anchor.line) && (end_pos <= selection_anchor.pos))) {
3275 selection_start.line = end;
3276 selection_start.tag = LineTag.FindTag(end, end_pos);
3277 selection_start.pos = end_pos;
3279 selection_end.line = selection_anchor.line;
3280 selection_end.tag = selection_anchor.tag;
3281 selection_end.pos = selection_anchor.pos;
3283 selection_end_anchor = true;
3284 } else {
3285 selection_start.line = selection_anchor.line;
3286 selection_start.tag = selection_anchor.tag;
3287 selection_start.pos = selection_anchor.pos;
3289 selection_end.line = end;
3290 selection_end.tag = LineTag.FindTag(end, end_pos);
3291 selection_end.pos = end_pos;
3293 selection_end_anchor = false;
3296 if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
3297 SetSelectionVisible (true);
3298 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3299 } else {
3300 SetSelectionVisible (false);
3301 // ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
3305 internal void SetSelectionEnd(int character_index) {
3306 Line line;
3307 LineTag tag;
3308 int pos;
3310 if (character_index < 0) {
3311 return;
3314 CharIndexToLineTag(character_index, out line, out tag, out pos);
3315 SetSelectionEnd(line, pos);
3318 internal void SetSelection(Line start, int start_pos) {
3319 if (selection_visible) {
3320 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3323 selection_start.line = start;
3324 selection_start.pos = start_pos;
3325 selection_start.tag = LineTag.FindTag(start, start_pos);
3327 selection_end.line = start;
3328 selection_end.tag = selection_start.tag;
3329 selection_end.pos = start_pos;
3331 selection_anchor.line = start;
3332 selection_anchor.tag = selection_start.tag;
3333 selection_anchor.pos = start_pos;
3335 selection_end_anchor = false;
3336 SetSelectionVisible (false);
3339 internal void InvalidateSelectionArea() {
3340 Invalidate (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3343 // Return the current selection, as string
3344 internal string GetSelection() {
3345 // We return String.Empty if there is no selection
3346 if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
3347 return string.Empty;
3350 if (!multiline || (selection_start.line == selection_end.line)) {
3351 return selection_start.line.text.ToString(selection_start.pos, selection_end.pos - selection_start.pos);
3352 } else {
3353 StringBuilder sb;
3354 int i;
3355 int start;
3356 int end;
3358 sb = new StringBuilder();
3359 start = selection_start.line.line_no;
3360 end = selection_end.line.line_no;
3362 sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos) + Environment.NewLine);
3364 if ((start + 1) < end) {
3365 for (i = start + 1; i < end; i++) {
3366 sb.Append(GetLine(i).text.ToString() + Environment.NewLine);
3370 sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
3372 return sb.ToString();
3376 internal void ReplaceSelection(string s, bool select_new) {
3377 int i;
3379 undo.BeginCompoundAction ();
3381 int selection_start_pos = LineTagToCharIndex (selection_start.line, selection_start.pos);
3382 // First, delete any selected text
3383 if ((selection_start.pos != selection_end.pos) || (selection_start.line != selection_end.line)) {
3384 if (!multiline || (selection_start.line == selection_end.line)) {
3385 undo.RecordDeleteChars(selection_start.line, selection_start.pos + 1, selection_end.pos - selection_start.pos);
3387 DeleteChars(selection_start.tag, selection_start.pos, selection_end.pos - selection_start.pos);
3389 // The tag might have been removed, we need to recalc it
3390 selection_start.tag = selection_start.line.FindTag(selection_start.pos);
3391 } else {
3392 int start;
3393 int end;
3395 start = selection_start.line.line_no;
3396 end = selection_end.line.line_no;
3398 undo.RecordDelete(selection_start.line, selection_start.pos + 1, selection_end.line, selection_end.pos);
3400 // Delete first line
3401 DeleteChars(selection_start.tag, selection_start.pos, selection_start.line.text.Length - selection_start.pos);
3403 // Delete last line
3404 DeleteChars(selection_end.line.tags, 0, selection_end.pos);
3406 start++;
3407 if (start < end) {
3408 for (i = end - 1; i >= start; i--) {
3409 Delete(i);
3413 // BIG FAT WARNING - selection_end.line might be stale due
3414 // to the above Delete() call. DONT USE IT before hitting the end of this method!
3416 // Join start and end
3417 Combine(selection_start.line.line_no, start);
3421 Insert(selection_start.line, selection_start.pos, true, s);
3422 undo.RecordInsertString (selection_start.line, selection_start.pos, s);
3424 if (!select_new) {
3425 CharIndexToLineTag(selection_start_pos + s.Length, out selection_start.line,
3426 out selection_start.tag, out selection_start.pos);
3428 selection_end.line = selection_start.line;
3429 selection_end.pos = selection_start.pos;
3430 selection_end.tag = selection_start.tag;
3431 selection_anchor.line = selection_start.line;
3432 selection_anchor.pos = selection_start.pos;
3433 selection_anchor.tag = selection_start.tag;
3435 SetSelectionVisible (false);
3436 } else {
3437 CharIndexToLineTag(selection_start_pos, out selection_start.line,
3438 out selection_start.tag, out selection_start.pos);
3440 CharIndexToLineTag(selection_start_pos + s.Length, out selection_end.line,
3441 out selection_end.tag, out selection_end.pos);
3443 selection_anchor.line = selection_start.line;
3444 selection_anchor.pos = selection_start.pos;
3445 selection_anchor.tag = selection_start.tag;
3447 SetSelectionVisible (true);
3450 undo.EndCompoundAction ();
3453 internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
3454 Line line;
3455 LineTag tag;
3456 int i;
3457 int chars;
3458 int start;
3460 chars = 0;
3462 for (i = 1; i <= lines; i++) {
3463 line = GetLine(i);
3465 start = chars;
3466 chars += line.text.Length + crlf_size;
3468 if (index <= chars) {
3469 // we found the line
3470 tag = line.tags;
3472 while (tag != null) {
3473 if (index < (start + tag.start + tag.length)) {
3474 line_out = line;
3475 tag_out = LineTag.GetFinalTag (tag);
3476 pos = index - start;
3477 return;
3479 if (tag.next == null) {
3480 Line next_line;
3482 next_line = GetLine(line.line_no + 1);
3484 if (next_line != null) {
3485 line_out = next_line;
3486 tag_out = LineTag.GetFinalTag (next_line.tags);
3487 pos = 0;
3488 return;
3489 } else {
3490 line_out = line;
3491 tag_out = LineTag.GetFinalTag (tag);
3492 pos = line_out.text.Length;
3493 return;
3496 tag = tag.next;
3501 line_out = GetLine(lines);
3502 tag = line_out.tags;
3503 while (tag.next != null) {
3504 tag = tag.next;
3506 tag_out = tag;
3507 pos = line_out.text.Length;
3510 internal int LineTagToCharIndex(Line line, int pos) {
3511 int i;
3512 int length;
3514 // Count first and last line
3515 length = 0;
3517 // Count the lines in the middle
3519 for (i = 1; i < line.line_no; i++) {
3520 length += GetLine(i).text.Length + crlf_size;
3523 length += pos;
3525 return length;
3528 internal int SelectionLength() {
3529 if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
3530 return 0;
3533 if (!multiline || (selection_start.line == selection_end.line)) {
3534 return selection_end.pos - selection_start.pos;
3535 } else {
3536 int i;
3537 int start;
3538 int end;
3539 int length;
3541 // Count first and last line
3542 length = selection_start.line.text.Length - selection_start.pos + selection_end.pos + crlf_size;
3544 // Count the lines in the middle
3545 start = selection_start.line.line_no + 1;
3546 end = selection_end.line.line_no;
3548 if (start < end) {
3549 for (i = start; i < end; i++) {
3550 length += GetLine(i).text.Length + crlf_size;
3554 return length;
3561 /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
3562 internal Line GetLine(int LineNo) {
3563 Line line = document;
3565 while (line != sentinel) {
3566 if (LineNo == line.line_no) {
3567 return line;
3568 } else if (LineNo < line.line_no) {
3569 line = line.left;
3570 } else {
3571 line = line.right;
3575 return null;
3578 /// <summary>Retrieve the previous tag; walks line boundaries</summary>
3579 internal LineTag PreviousTag(LineTag tag) {
3580 Line l;
3582 if (tag.previous != null) {
3583 return tag.previous;
3586 // Next line
3587 if (tag.line.line_no == 1) {
3588 return null;
3591 l = GetLine(tag.line.line_no - 1);
3592 if (l != null) {
3593 LineTag t;
3595 t = l.tags;
3596 while (t.next != null) {
3597 t = t.next;
3599 return t;
3602 return null;
3605 /// <summary>Retrieve the next tag; walks line boundaries</summary>
3606 internal LineTag NextTag(LineTag tag) {
3607 Line l;
3609 if (tag.next != null) {
3610 return tag.next;
3613 // Next line
3614 l = GetLine(tag.line.line_no + 1);
3615 if (l != null) {
3616 return l.tags;
3619 return null;
3622 internal Line ParagraphStart(Line line) {
3623 while (line.soft_break) {
3624 line = GetLine(line.line_no - 1);
3626 return line;
3629 internal Line ParagraphEnd(Line line) {
3630 Line l;
3632 while (line.soft_break) {
3633 l = GetLine(line.line_no + 1);
3634 if ((l == null) || (!l.soft_break)) {
3635 break;
3637 line = l;
3639 return line;
3642 /// <summary>Give it a Y pixel coordinate and it returns the Line covering that Y coordinate</summary>
3643 internal Line GetLineByPixel(int y, bool exact) {
3644 Line line = document;
3645 Line last = null;
3647 while (line != sentinel) {
3648 last = line;
3649 if ((y >= line.Y) && (y < (line.Y+line.height))) {
3650 return line;
3651 } else if (y < line.Y) {
3652 line = line.left;
3653 } else {
3654 line = line.right;
3658 if (exact) {
3659 return null;
3661 return last;
3664 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3665 internal LineTag FindTag(int x, int y, out int index, bool exact) {
3666 Line line;
3667 LineTag tag;
3669 line = GetLineByPixel(y, exact);
3670 if (line == null) {
3671 index = 0;
3672 return null;
3674 tag = line.tags;
3676 // Alignment adjustment
3677 x += line.align_shift;
3679 while (true) {
3680 if (x >= tag.X && x < (tag.X+tag.width)) {
3681 int end;
3683 end = tag.start + tag.length - 1;
3685 for (int pos = tag.start; pos < end; pos++) {
3686 if (x < line.widths[pos]) {
3687 index = pos;
3688 return LineTag.GetFinalTag (tag);
3691 index=end;
3692 return LineTag.GetFinalTag (tag);
3694 if (tag.next != null) {
3695 tag = tag.next;
3696 } else {
3697 if (exact) {
3698 index = 0;
3699 return null;
3702 index = line.text.Length;
3703 return LineTag.GetFinalTag (tag);
3708 // Give it x/y pixel coordinates and it returns the Tag at that position; optionally the char position is returned in index
3709 internal LineTag FindCursor(int x, int y, out int index) {
3710 Line line;
3711 LineTag tag;
3713 line = GetLineByPixel(y, false);
3714 tag = line.tags;
3716 // Adjust for alignment
3717 x -= line.align_shift;
3719 while (true) {
3720 if (x >= tag.X && x < (tag.X+tag.width)) {
3721 int end;
3723 end = tag.start + tag.length - 1;
3725 for (int pos = tag.start-1; pos < end; pos++) {
3726 // When clicking on a character, we position the cursor to whatever edge
3727 // of the character the click was closer
3728 if (x < (line.widths[pos] + ((line.widths[pos+1]-line.widths[pos])/2))) {
3729 index = pos;
3730 return tag;
3733 index=end;
3734 return tag;
3736 if (tag.next != null) {
3737 tag = tag.next;
3738 } else {
3739 index = line.text.Length;
3740 return tag;
3745 /// <summary>Format area of document in specified font and color</summary>
3746 /// <param name="start_pos">1-based start position on start_line</param>
3747 /// <param name="end_pos">1-based end position on end_line </param>
3748 internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, Font font, Brush color) {
3749 Line l;
3751 // First, format the first line
3752 if (start_line != end_line) {
3753 // First line
3754 LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, font, color);
3756 // Format last line
3757 LineTag.FormatText(end_line, 1, end_pos, font, color);
3759 // Now all the lines inbetween
3760 for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
3761 l = GetLine(i);
3762 LineTag.FormatText(l, 1, l.text.Length, font, color);
3764 } else {
3765 // Special case, single line
3766 LineTag.FormatText(start_line, start_pos, end_pos - start_pos, font, color);
3770 /// <summary>Re-format areas of the document in specified font and color</summary>
3771 /// <param name="start_pos">1-based start position on start_line</param>
3772 /// <param name="end_pos">1-based end position on end_line </param>
3773 /// <param name="font">Font specifying attributes</param>
3774 /// <param name="color">Color (or NULL) to apply</param>
3775 /// <param name="apply">Attributes from font and color to apply</param>
3776 internal void FormatText(Line start_line, int start_pos, Line end_line, int end_pos, FontDefinition attributes) {
3777 Line l;
3779 // First, format the first line
3780 if (start_line != end_line) {
3781 // First line
3782 LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, attributes);
3784 // Format last line
3785 LineTag.FormatText(end_line, 1, end_pos - 1, attributes);
3787 // Now all the lines inbetween
3788 for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
3789 l = GetLine(i);
3790 LineTag.FormatText(l, 1, l.text.Length, attributes);
3792 } else {
3793 // Special case, single line
3794 LineTag.FormatText(start_line, start_pos, end_pos - start_pos, attributes);
3798 internal void RecalculateAlignments() {
3799 Line line;
3800 int line_no;
3802 line_no = 1;
3804 while (line_no <= lines) {
3805 line = GetLine(line_no);
3807 if (line != null) {
3808 switch (line.alignment) {
3809 case HorizontalAlignment.Left:
3810 line.align_shift = 0;
3811 break;
3812 case HorizontalAlignment.Center:
3813 line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
3814 break;
3815 case HorizontalAlignment.Right:
3816 line.align_shift = viewport_width - (int)line.widths[line.text.Length];
3817 break;
3821 line_no++;
3823 return;
3826 /// <summary>Calculate formatting for the whole document</summary>
3827 internal bool RecalculateDocument(Graphics g) {
3828 return RecalculateDocument(g, 1, this.lines, false);
3831 /// <summary>Calculate formatting starting at a certain line</summary>
3832 internal bool RecalculateDocument(Graphics g, int start) {
3833 return RecalculateDocument(g, start, this.lines, false);
3836 /// <summary>Calculate formatting within two given line numbers</summary>
3837 internal bool RecalculateDocument(Graphics g, int start, int end) {
3838 return RecalculateDocument(g, start, end, false);
3841 /// <summary>With optimize on, returns true if line heights changed</summary>
3842 internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
3843 Line line;
3844 int line_no;
3845 int Y;
3846 int new_width;
3847 bool changed;
3848 int shift;
3850 if (no_recalc) {
3851 recalc_pending = true;
3852 recalc_start = start;
3853 recalc_end = end;
3854 recalc_optimize = optimize;
3855 return false;
3858 Y = GetLine(start).Y;
3859 line_no = start;
3860 new_width = 0;
3861 shift = this.lines;
3862 if (!optimize) {
3863 changed = true; // We always return true if we run non-optimized
3864 } else {
3865 changed = false;
3868 while (line_no <= (end + this.lines - shift)) {
3869 line = GetLine(line_no++);
3870 line.Y = Y;
3872 if (!calc_pass) {
3873 if (!optimize) {
3874 line.RecalculateLine(g, this);
3875 } else {
3876 if (line.recalc && line.RecalculateLine(g, this)) {
3877 changed = true;
3878 // If the height changed, all subsequent lines change
3879 end = this.lines;
3880 shift = this.lines;
3883 } else {
3884 if (!optimize) {
3885 line.RecalculatePasswordLine(g, this);
3886 } else {
3887 if (line.recalc && line.RecalculatePasswordLine(g, this)) {
3888 changed = true;
3889 // If the height changed, all subsequent lines change
3890 end = this.lines;
3891 shift = this.lines;
3896 if (line.widths[line.text.Length] > new_width) {
3897 new_width = (int)line.widths[line.text.Length];
3900 // Calculate alignment
3901 if (line.alignment != HorizontalAlignment.Left) {
3902 if (line.alignment == HorizontalAlignment.Center) {
3903 line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
3904 } else {
3905 line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
3909 Y += line.height;
3911 if (line_no > lines) {
3912 break;
3916 if (document_x != new_width) {
3917 document_x = new_width;
3918 if (WidthChanged != null) {
3919 WidthChanged(this, null);
3923 RecalculateAlignments();
3925 line = GetLine(lines);
3927 if (document_y != line.Y + line.height) {
3928 document_y = line.Y + line.height;
3929 if (HeightChanged != null) {
3930 HeightChanged(this, null);
3933 UpdateCaret();
3934 return changed;
3937 internal int Size() {
3938 return lines;
3941 private void owner_HandleCreated(object sender, EventArgs e) {
3942 RecalculateDocument(owner.CreateGraphicsInternal());
3943 AlignCaret();
3946 private void owner_VisibleChanged(object sender, EventArgs e) {
3947 if (owner.Visible) {
3948 RecalculateDocument(owner.CreateGraphicsInternal());
3952 internal static bool IsWordSeparator(char ch) {
3953 switch(ch) {
3954 case ' ':
3955 case '\t':
3956 case '(':
3957 case ')': {
3958 return true;
3961 default: {
3962 return false;
3966 internal int FindWordSeparator(Line line, int pos, bool forward) {
3967 int len;
3969 len = line.text.Length;
3971 if (forward) {
3972 for (int i = pos + 1; i < len; i++) {
3973 if (IsWordSeparator(line.Text[i])) {
3974 return i + 1;
3977 return len;
3978 } else {
3979 for (int i = pos - 1; i > 0; i--) {
3980 if (IsWordSeparator(line.Text[i - 1])) {
3981 return i;
3984 return 0;
3988 /* Search document for text */
3989 internal bool FindChars(char[] chars, Marker start, Marker end, out Marker result) {
3990 Line line;
3991 int line_no;
3992 int pos;
3993 int line_len;
3995 // Search for occurence of any char in the chars array
3996 result = new Marker();
3998 line = start.line;
3999 line_no = start.line.line_no;
4000 pos = start.pos;
4001 while (line_no <= end.line.line_no) {
4002 line_len = line.text.Length;
4003 while (pos < line_len) {
4004 for (int i = 0; i < chars.Length; i++) {
4005 if (line.text[pos] == chars[i]) {
4006 // Special case
4007 if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
4008 return false;
4011 result.line = line;
4012 result.pos = pos;
4013 return true;
4016 pos++;
4019 pos = 0;
4020 line_no++;
4021 line = GetLine(line_no);
4024 return false;
4027 // This version does not build one big string for searching, instead it handles
4028 // line-boundaries, which is faster and less memory intensive
4029 // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific
4030 // search stuff and change it to accept and return positions instead of Markers (which would match
4031 // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
4032 internal bool Find(string search, Marker start, Marker end, out Marker result, RichTextBoxFinds options) {
4033 Marker last;
4034 string search_string;
4035 Line line;
4036 int line_no;
4037 int pos;
4038 int line_len;
4039 int current;
4040 bool word;
4041 bool word_option;
4042 bool ignore_case;
4043 bool reverse;
4044 char c;
4046 result = new Marker();
4047 word_option = ((options & RichTextBoxFinds.WholeWord) != 0);
4048 ignore_case = ((options & RichTextBoxFinds.MatchCase) == 0);
4049 reverse = ((options & RichTextBoxFinds.Reverse) != 0);
4051 line = start.line;
4052 line_no = start.line.line_no;
4053 pos = start.pos;
4054 current = 0;
4056 // Prep our search string, lowercasing it if we do case-independent matching
4057 if (ignore_case) {
4058 StringBuilder sb;
4059 sb = new StringBuilder(search);
4060 for (int i = 0; i < sb.Length; i++) {
4061 sb[i] = Char.ToLower(sb[i]);
4063 search_string = sb.ToString();
4064 } else {
4065 search_string = search;
4068 // We need to check if the character before our start position is a wordbreak
4069 if (word_option) {
4070 if (line_no == 1) {
4071 if ((pos == 0) || (IsWordSeparator(line.text[pos - 1]))) {
4072 word = true;
4073 } else {
4074 word = false;
4076 } else {
4077 if (pos > 0) {
4078 if (IsWordSeparator(line.text[pos - 1])) {
4079 word = true;
4080 } else {
4081 word = false;
4083 } else {
4084 // Need to check the end of the previous line
4085 Line prev_line;
4087 prev_line = GetLine(line_no - 1);
4088 if (prev_line.soft_break) {
4089 if (IsWordSeparator(prev_line.text[prev_line.text.Length - 1])) {
4090 word = true;
4091 } else {
4092 word = false;
4094 } else {
4095 word = true;
4099 } else {
4100 word = false;
4103 // To avoid duplication of this loop with reverse logic, we search
4104 // through the document, remembering the last match and when returning
4105 // report that last remembered match
4107 last = new Marker();
4108 last.height = -1; // Abused - we use it to track change
4110 while (line_no <= end.line.line_no) {
4111 if (line_no != end.line.line_no) {
4112 line_len = line.text.Length;
4113 } else {
4114 line_len = end.pos;
4117 while (pos < line_len) {
4118 if (word_option && (current == search_string.Length)) {
4119 if (IsWordSeparator(line.text[pos])) {
4120 if (!reverse) {
4121 goto FindFound;
4122 } else {
4123 last = result;
4124 current = 0;
4126 } else {
4127 current = 0;
4131 if (ignore_case) {
4132 c = Char.ToLower(line.text[pos]);
4133 } else {
4134 c = line.text[pos];
4137 if (c == search_string[current]) {
4138 if (current == 0) {
4139 result.line = line;
4140 result.pos = pos;
4142 if (!word_option || (word_option && (word || (current > 0)))) {
4143 current++;
4146 if (!word_option && (current == search_string.Length)) {
4147 if (!reverse) {
4148 goto FindFound;
4149 } else {
4150 last = result;
4151 current = 0;
4154 } else {
4155 current = 0;
4157 pos++;
4159 if (!word_option) {
4160 continue;
4163 if (IsWordSeparator(c)) {
4164 word = true;
4165 } else {
4166 word = false;
4170 if (word_option) {
4171 // Mark that we just saw a word boundary
4172 if (!line.soft_break) {
4173 word = true;
4176 if (current == search_string.Length) {
4177 if (word) {
4178 if (!reverse) {
4179 goto FindFound;
4180 } else {
4181 last = result;
4182 current = 0;
4184 } else {
4185 current = 0;
4190 pos = 0;
4191 line_no++;
4192 line = GetLine(line_no);
4195 if (reverse) {
4196 if (last.height != -1) {
4197 result = last;
4198 return true;
4202 return false;
4204 FindFound:
4205 if (!reverse) {
4206 // if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
4207 // return false;
4208 // }
4209 return true;
4212 result = last;
4213 return true;
4217 /* Marker stuff */
4218 internal void GetMarker(out Marker mark, bool start) {
4219 mark = new Marker();
4221 if (start) {
4222 mark.line = GetLine(1);
4223 mark.tag = mark.line.tags;
4224 mark.pos = 0;
4225 } else {
4226 mark.line = GetLine(lines);
4227 mark.tag = mark.line.tags;
4228 while (mark.tag.next != null) {
4229 mark.tag = mark.tag.next;
4231 mark.pos = mark.line.text.Length;
4234 #endregion // Internal Methods
4236 #region Events
4237 internal event EventHandler CaretMoved;
4238 internal event EventHandler WidthChanged;
4239 internal event EventHandler HeightChanged;
4240 internal event EventHandler LengthChanged;
4241 #endregion // Events
4243 #region Administrative
4244 public IEnumerator GetEnumerator() {
4245 // FIXME
4246 return null;
4249 public override bool Equals(object obj) {
4250 if (obj == null) {
4251 return false;
4254 if (!(obj is Document)) {
4255 return false;
4258 if (obj == this) {
4259 return true;
4262 if (ToString().Equals(((Document)obj).ToString())) {
4263 return true;
4266 return false;
4269 public override int GetHashCode() {
4270 return document_id;
4273 public override string ToString() {
4274 return "document " + this.document_id;
4276 #endregion // Administrative
4279 internal class LineTag {
4280 #region Local Variables;
4281 // Payload; formatting
4282 internal Font font; // System.Drawing.Font object for this tag
4283 internal Brush color; // System.Drawing.Brush object
4285 // Payload; text
4286 internal int start; // start, in chars; index into Line.text
4287 internal int length; // length, in chars
4288 internal bool r_to_l; // Which way is the font
4290 // Drawing support
4291 internal int height; // Height in pixels of the text this tag describes
4292 internal int X; // X location of the text this tag describes
4293 internal float width; // Width in pixels of the text this tag describes
4294 internal int ascent; // Ascent of the font for this tag
4295 internal int shift; // Shift down for this tag, to stay on baseline
4297 // Administrative
4298 internal Line line; // The line we're on
4299 internal LineTag next; // Next tag on the same line
4300 internal LineTag previous; // Previous tag on the same line
4301 #endregion;
4303 #region Constructors
4304 internal LineTag(Line line, int start, int length) {
4305 this.line = line;
4306 this.start = start;
4307 this.length = length;
4308 this.X = 0;
4309 this.width = 0;
4311 #endregion // Constructors
4313 #region Internal Methods
4314 ///<summary>Break a tag into two with identical attributes; pos is 1-based; returns tag starting at &gt;pos&lt; or null if end-of-line</summary>
4315 internal LineTag Break(int pos) {
4316 LineTag new_tag;
4318 // Sanity
4319 if (pos == this.start) {
4320 return this;
4321 } else if (pos >= (start + length)) {
4322 return null;
4325 new_tag = new LineTag(line, pos, start + length - pos);
4326 new_tag.color = color;
4327 new_tag.font = font;
4328 this.length -= new_tag.length;
4329 new_tag.next = this.next;
4330 this.next = new_tag;
4331 new_tag.previous = this;
4332 if (new_tag.next != null) {
4333 new_tag.next.previous = new_tag;
4336 return new_tag;
4339 ///<summary>Create new font and brush from existing font and given new attributes. Returns true if fontheight changes</summary>
4340 internal static bool GenerateTextFormat(Font font_from, Brush color_from, FontDefinition attributes, out Font new_font, out Brush new_color) {
4341 float size;
4342 string face;
4343 FontStyle style;
4344 GraphicsUnit unit;
4346 if (attributes.font_obj == null) {
4347 size = font_from.SizeInPoints;
4348 unit = font_from.Unit;
4349 face = font_from.Name;
4350 style = font_from.Style;
4352 if (attributes.face != null) {
4353 face = attributes.face;
4356 if (attributes.size != 0) {
4357 size = attributes.size;
4360 style |= attributes.add_style;
4361 style &= ~attributes.remove_style;
4363 // Create new font
4364 new_font = new Font(face, size, style, unit);
4365 } else {
4366 new_font = attributes.font_obj;
4369 // Create 'new' color brush
4370 if (attributes.color != Color.Empty) {
4371 new_color = new SolidBrush(attributes.color);
4372 } else {
4373 new_color = color_from;
4376 if (new_font.Height == font_from.Height) {
4377 return false;
4379 return true;
4382 /// <summary>Applies 'font' and 'brush' to characters starting at 'start' for 'length' chars;
4383 /// Removes any previous tags overlapping the same area;
4384 /// returns true if lineheight has changed</summary>
4385 /// <param name="start">1-based character position on line</param>
4386 internal static bool FormatText(Line line, int start, int length, Font font, Brush color) {
4387 LineTag tag;
4388 LineTag start_tag;
4389 LineTag end_tag;
4390 int end;
4391 bool retval = false; // Assume line-height doesn't change
4393 // Too simple?
4394 if (font.Height != line.height) {
4395 retval = true;
4397 line.recalc = true; // This forces recalculation of the line in RecalculateDocument
4399 // A little sanity, not sure if it's needed, might be able to remove for speed
4400 if (length > line.text.Length) {
4401 length = line.text.Length;
4404 tag = line.tags;
4405 end = start + length;
4407 // Common special case
4408 if ((start == 1) && (length == tag.length)) {
4409 tag.ascent = 0;
4410 tag.font = font;
4411 tag.color = color;
4412 return retval;
4415 //Console.WriteLine("Finding tag for {0} {1}", line, start);
4416 start_tag = FindTag(line, start);
4417 end_tag = FindTag (line, end);
4419 if (start_tag == null) { // FIXME - is there a better way to handle this, or do we even need it?
4420 throw new Exception(String.Format("Could not find start_tag in document at line {0} position {1}", line.line_no, start));
4423 tag = new LineTag(line, start, length);
4424 tag.font = font;
4425 tag.color = color;
4427 if (start == 1) {
4428 line.tags = tag;
4430 //Console.WriteLine("Start tag: '{0}'", start_tag!=null ? start_tag.ToString() : "NULL");
4431 if (start_tag.start == start) {
4432 tag.next = start_tag;
4433 tag.previous = start_tag.previous;
4434 if (start_tag.previous != null) {
4435 start_tag.previous.next = tag;
4437 start_tag.previous = tag;
4438 } else {
4439 tag.next = end_tag;
4441 if (end_tag != null) {
4442 // Shorten up the end tag
4443 end_tag.previous = tag;
4444 end_tag.length = end - start_tag.start + start_tag.length;
4445 end_tag.start = end;
4449 // Elimination loop
4450 tag = tag.next;
4451 while (tag != end_tag) {
4452 if ((tag.start + tag.length) <= end) {
4453 // remove the tag
4454 tag.previous.next = tag.next;
4455 if (tag.next != null) {
4456 tag.next.previous = tag.previous;
4458 tag = tag.previous;
4460 tag = tag.next;
4463 return retval;
4466 /// <summary>Applies font attributes specified to characters starting at 'start' for 'length' chars;
4467 /// Breaks tags at start and end point, keeping middle tags with altered attributes.
4468 /// Returns true if lineheight has changed</summary>
4469 /// <param name="start">1-based character position on line</param>
4470 internal static bool FormatText(Line line, int start, int length, FontDefinition attributes) {
4471 LineTag tag;
4472 LineTag start_tag;
4473 LineTag end_tag;
4474 bool retval = false; // Assume line-height doesn't change
4476 line.recalc = true; // This forces recalculation of the line in RecalculateDocument
4478 // A little sanity, not sure if it's needed, might be able to remove for speed
4479 if (length > line.text.Length) {
4480 length = line.text.Length;
4483 tag = line.tags;
4485 // Common special case
4486 if ((start == 1) && (length == tag.length)) {
4487 tag.ascent = 0;
4488 GenerateTextFormat(tag.font, tag.color, attributes, out tag.font, out tag.color);
4489 return retval;
4492 start_tag = FindTag(line, start);
4494 if (start_tag == null) {
4495 if (length == 0) {
4496 // We are 'starting' after all valid tags; create a new tag with the right attributes
4497 start_tag = FindTag(line, line.text.Length - 1);
4498 start_tag.next = new LineTag(line, line.text.Length + 1, 0);
4499 start_tag.next.font = start_tag.font;
4500 start_tag.next.color = start_tag.color;
4501 start_tag.next.previous = start_tag;
4502 start_tag = start_tag.next;
4503 } else {
4504 throw new Exception(String.Format("Could not find start_tag in document at line {0} position {1}", line.line_no, start));
4506 } else {
4507 start_tag = start_tag.Break(start);
4510 end_tag = FindTag(line, start + length);
4511 if (end_tag != null) {
4512 end_tag = end_tag.Break(start + length);
4515 // start_tag or end_tag might be null; we're cool with that
4516 // we now walk from start_tag to end_tag, applying new attributes
4517 tag = start_tag;
4518 while ((tag != null) && tag != end_tag) {
4519 if (LineTag.GenerateTextFormat(tag.font, tag.color, attributes, out tag.font, out tag.color)) {
4520 retval = true;
4522 tag = tag.next;
4524 return retval;
4528 /// <summary>Finds the tag that describes the character at position 'pos' on 'line'</summary>
4529 internal static LineTag FindTag(Line line, int pos) {
4530 LineTag tag = line.tags;
4532 // Beginning of line is a bit special
4533 if (pos == 0) {
4534 // Not sure if we should get the final tag here
4535 return tag;
4538 while (tag != null) {
4539 if ((tag.start <= pos) && (pos < (tag.start+tag.length))) {
4540 return GetFinalTag (tag);
4543 tag = tag.next;
4546 return null;
4549 // There can be multiple tags at the same position, we want to make
4550 // sure we are using the very last tag at the given position
4551 internal static LineTag GetFinalTag (LineTag tag)
4553 LineTag res = tag;
4555 while (res.next != null && res.next.length == 0)
4556 res = res.next;
4557 return res;
4560 /// <summary>Combines 'this' tag with 'other' tag</summary>
4561 internal bool Combine(LineTag other) {
4562 if (!this.Equals(other)) {
4563 return false;
4566 this.width += other.width;
4567 this.length += other.length;
4568 this.next = other.next;
4569 if (this.next != null) {
4570 this.next.previous = this;
4573 return true;
4577 /// <summary>Remove 'this' tag ; to be called when formatting is to be removed</summary>
4578 internal bool Remove() {
4579 if ((this.start == 1) && (this.next == null)) {
4580 // We cannot remove the only tag
4581 return false;
4583 if (this.start != 1) {
4584 this.previous.length += this.length;
4585 this.previous.width = -1;
4586 this.previous.next = this.next;
4587 this.next.previous = this.previous;
4588 } else {
4589 this.next.start = 1;
4590 this.next.length += this.length;
4591 this.next.width = -1;
4592 this.line.tags = this.next;
4593 this.next.previous = null;
4595 return true;
4599 /// <summary>Checks if 'this' tag describes the same formatting options as 'obj'</summary>
4600 public override bool Equals(object obj) {
4601 LineTag other;
4603 if (obj == null) {
4604 return false;
4607 if (!(obj is LineTag)) {
4608 return false;
4611 if (obj == this) {
4612 return true;
4615 other = (LineTag)obj;
4617 if (this.font.Equals(other.font) && this.color.Equals(other.color)) { // FIXME add checking for things like link or type later
4618 return true;
4621 return false;
4624 public override int GetHashCode() {
4625 return base.GetHashCode ();
4628 public override string ToString() {
4629 if (length > 0)
4630 return "Tag starts at index " + this.start + "length " + this.length + " text: " + this.line.Text.Substring(this.start-1, this.length) + "Font " + this.font.ToString();
4631 return "Zero Lengthed tag at index " + this.start;
4634 #endregion // Internal Methods
4637 internal class UndoClass {
4638 internal enum ActionType {
4639 InsertChar,
4640 InsertString,
4641 DeleteChar,
4642 DeleteChars,
4643 CursorMove,
4644 Mark,
4645 CompoundBegin,
4646 CompoundEnd,
4649 internal class Action {
4650 internal ActionType type;
4651 internal int line_no;
4652 internal int pos;
4653 internal object data;
4656 #region Local Variables
4657 private Document document;
4658 private Stack undo_actions;
4659 private Stack redo_actions;
4660 private int caret_line;
4661 private int caret_pos;
4662 #endregion // Local Variables
4664 #region Constructors
4665 internal UndoClass(Document doc) {
4666 document = doc;
4667 undo_actions = new Stack(50);
4668 redo_actions = new Stack(50);
4670 #endregion // Constructors
4672 #region Properties
4673 [MonoTODO("Change this to be configurable")]
4674 internal int UndoLevels {
4675 get {
4676 return undo_actions.Count;
4680 [MonoTODO("Change this to be configurable")]
4681 internal int RedoLevels {
4682 get {
4683 return redo_actions.Count;
4687 [MonoTODO("Come up with good naming and localization")]
4688 internal string UndoName {
4689 get {
4690 Action action;
4692 action = (Action)undo_actions.Peek();
4693 switch(action.type) {
4694 case ActionType.InsertChar: {
4695 Locale.GetText("Insert character");
4696 break;
4699 case ActionType.DeleteChar: {
4700 Locale.GetText("Delete character");
4701 break;
4704 case ActionType.InsertString: {
4705 Locale.GetText("Insert string");
4706 break;
4709 case ActionType.DeleteChars: {
4710 Locale.GetText("Delete string");
4711 break;
4714 case ActionType.CursorMove: {
4715 Locale.GetText("Cursor move");
4716 break;
4719 return null;
4723 internal string RedoName() {
4724 return null;
4726 #endregion // Properties
4728 #region Internal Methods
4729 internal void Clear() {
4730 undo_actions.Clear();
4731 redo_actions.Clear();
4734 internal void Undo() {
4735 Action action;
4736 int compound_stack = 0;
4738 if (undo_actions.Count == 0) {
4739 return;
4744 do {
4745 action = (Action)undo_actions.Pop();
4747 // Put onto redo stack
4748 redo_actions.Push(action);
4750 // Do the thing
4751 switch(action.type) {
4752 case ActionType.CompoundEnd:
4753 compound_stack++;
4754 break;
4756 case ActionType.CompoundBegin:
4757 compound_stack--;
4758 break;
4760 case ActionType.InsertString:
4761 document.DeleteMultiline (document.GetLine (action.line_no),
4762 action.pos, ((string) action.data).Length + 1);
4763 break;
4765 case ActionType.InsertChar: {
4766 // FIXME - implement me
4767 break;
4770 case ActionType.DeleteChars: {
4771 this.Insert(document.GetLine(action.line_no), action.pos, (Line)action.data);
4772 Undo(); // Grab the cursor location
4773 break;
4776 case ActionType.CursorMove: {
4777 document.caret.line = document.GetLine(action.line_no);
4778 if (document.caret.line == null) {
4779 Undo();
4780 break;
4783 document.caret.tag = document.caret.line.FindTag(action.pos);
4784 document.caret.pos = action.pos;
4785 document.caret.height = document.caret.tag.height;
4787 if (document.owner.IsHandleCreated) {
4788 XplatUI.DestroyCaret(document.owner.Handle);
4789 XplatUI.CreateCaret(document.owner.Handle, 2, document.caret.height);
4790 XplatUI.SetCaretPos(document.owner.Handle, (int)document.caret.tag.line.widths[document.caret.pos] + document.caret.line.align_shift - document.viewport_x, document.caret.line.Y + document.caret.tag.shift - document.viewport_y + Document.caret_shift);
4792 document.DisplayCaret ();
4795 // FIXME - enable call
4796 //if (document.CaretMoved != null) document.CaretMoved(this, EventArgs.Empty);
4797 break;
4800 } while (compound_stack > 0);
4803 internal void Redo() {
4804 if (redo_actions.Count == 0) {
4805 return;
4808 #endregion // Internal Methods
4810 #region Private Methods
4812 public void BeginCompoundAction ()
4814 Action cb = new Action ();
4815 cb.type = ActionType.CompoundBegin;
4817 undo_actions.Push (cb);
4820 public void EndCompoundAction ()
4822 Action ce = new Action ();
4823 ce.type = ActionType.CompoundEnd;
4825 undo_actions.Push (ce);
4828 // pos = 1-based
4829 public void RecordDeleteChars(Line line, int pos, int length) {
4830 RecordDelete(line, pos, line, pos + length - 1);
4833 // start_pos, end_pos = 1 based
4834 public void RecordDelete(Line start_line, int start_pos, Line end_line, int end_pos) {
4835 Line l;
4836 Action a;
4838 l = Duplicate(start_line, start_pos, end_line, end_pos);
4840 a = new Action();
4841 a.type = ActionType.DeleteChars;
4842 a.data = l;
4843 a.line_no = start_line.line_no;
4844 a.pos = start_pos - 1;
4846 // Record the cursor position before, since the actions will occur in reverse order
4847 RecordCursor();
4848 undo_actions.Push(a);
4851 public void RecordInsertString (Line line, int pos, string str)
4853 Action a = new Action ();
4855 a.type = ActionType.InsertString;
4856 a.data = str;
4857 a.line_no = line.line_no;
4858 a.pos = pos;
4860 undo_actions.Push (a);
4863 public void RecordCursor() {
4864 if (document.caret.line == null) {
4865 return;
4868 RecordCursor(document.caret.line, document.caret.pos);
4871 public void RecordCursor(Line line, int pos) {
4872 Action a;
4874 if ((line.line_no == caret_line) && (pos == caret_pos)) {
4875 return;
4878 caret_line = line.line_no;
4879 caret_pos = pos;
4881 a = new Action();
4882 a.type = ActionType.CursorMove;
4883 a.line_no = line.line_no;
4884 a.pos = pos;
4886 undo_actions.Push(a);
4889 // start_pos = 1-based
4890 // end_pos = 1-based
4891 public Line Duplicate(Line start_line, int start_pos, Line end_line, int end_pos) {
4892 Line ret;
4893 Line line;
4894 Line current;
4895 LineTag tag;
4896 LineTag current_tag;
4897 int start;
4898 int end;
4899 int tag_start;
4900 int tag_length;
4902 line = new Line();
4903 ret = line;
4905 for (int i = start_line.line_no; i <= end_line.line_no; i++) {
4906 current = document.GetLine(i);
4908 if (start_line.line_no == i) {
4909 start = start_pos;
4910 } else {
4911 start = 1;
4914 if (end_line.line_no == i) {
4915 end = end_pos;
4916 } else {
4917 end = current.text.Length;
4920 // Text for the tag
4921 line.text = new StringBuilder(current.text.ToString(start - 1, end - start + 1));
4923 // Copy tags from start to start+length onto new line
4924 current_tag = current.FindTag(start - 1);
4925 while ((current_tag != null) && (current_tag.start < end)) {
4926 if ((current_tag.start <= start) && (start < (current_tag.start + current_tag.length))) {
4927 // start tag is within this tag
4928 tag_start = start;
4929 } else {
4930 tag_start = current_tag.start;
4933 if (end < (current_tag.start + current_tag.length)) {
4934 tag_length = end - tag_start + 1;
4935 } else {
4936 tag_length = current_tag.start + current_tag.length - tag_start;
4938 tag = new LineTag(line, tag_start - start + 1, tag_length);
4939 tag.color = current_tag.color;
4940 tag.font = current_tag.font;
4942 current_tag = current_tag.next;
4944 // Add the new tag to the line
4945 if (line.tags == null) {
4946 line.tags = tag;
4947 } else {
4948 LineTag tail;
4949 tail = line.tags;
4951 while (tail.next != null) {
4952 tail = tail.next;
4954 tail.next = tag;
4955 tag.previous = tail;
4959 if ((i + 1) <= end_line.line_no) {
4960 line.soft_break = current.soft_break;
4962 // Chain them (we use right/left as next/previous)
4963 line.right = new Line();
4964 line.right.left = line;
4965 line = line.right;
4969 return ret;
4972 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
4973 internal void Insert(Line line, int pos, Line insert) {
4974 Line current;
4975 LineTag tag;
4976 int offset;
4977 int lines;
4978 Line first;
4980 // Handle special case first
4981 if (insert.right == null) {
4983 // Single line insert
4984 document.Split(line, pos);
4986 if (insert.tags == null) {
4987 return; // Blank line
4990 //Insert our tags at the end
4991 tag = line.tags;
4993 while (tag.next != null) {
4994 tag = tag.next;
4997 offset = tag.start + tag.length - 1;
4999 tag.next = insert.tags;
5000 line.text.Insert(offset, insert.text.ToString());
5002 // Adjust start locations
5003 tag = tag.next;
5004 while (tag != null) {
5005 tag.start += offset;
5006 tag.line = line;
5007 tag = tag.next;
5009 // Put it back together
5010 document.Combine(line.line_no, line.line_no + 1);
5011 document.UpdateView(line, pos);
5012 return;
5015 first = line;
5016 lines = 1;
5017 current = insert;
5018 while (current != null) {
5019 if (current == insert) {
5020 // Inserting the first line we split the line (and make space)
5021 document.Split(line, pos);
5022 //Insert our tags at the end of the line
5023 tag = line.tags;
5025 if (tag != null) {
5026 while (tag.next != null) {
5027 tag = tag.next;
5029 offset = tag.start + tag.length - 1;
5030 tag.next = current.tags;
5031 tag.next.previous = tag;
5033 tag = tag.next;
5035 } else {
5036 offset = 0;
5037 line.tags = current.tags;
5038 line.tags.previous = null;
5039 tag = line.tags;
5041 } else {
5042 document.Split(line.line_no, 0);
5043 offset = 0;
5044 line.tags = current.tags;
5045 line.tags.previous = null;
5046 tag = line.tags;
5048 // Adjust start locations and line pointers
5049 while (tag != null) {
5050 tag.start += offset;
5051 tag.line = line;
5052 tag = tag.next;
5055 line.text.Insert(offset, current.text.ToString());
5056 line.Grow(line.text.Length);
5058 line.recalc = true;
5059 line = document.GetLine(line.line_no + 1);
5061 // FIXME? Test undo of line-boundaries
5062 if ((current.right == null) && (current.tags.length != 0)) {
5063 document.Combine(line.line_no - 1, line.line_no);
5065 current = current.right;
5066 lines++;
5070 // Recalculate our document
5071 document.UpdateView(first, lines, pos);
5072 return;
5074 #endregion // Private Methods