2008-09-09 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextControl.cs
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.
19 //
20 // Copyright (c) 2004-2006 Novell, Inc. (http://www.novell.com)
21 //
22 // Authors:
23 //      Peter Bartok    pbartok@novell.com
24 //
25 //
26
27 // NOT COMPLETE
28
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, etc.
34 // - Implement CaretPgUp/PgDown
35
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
40 //
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
44
45
46 #undef Debug
47
48 using System;
49 using System.Collections;
50 using System.Drawing;
51 using System.Drawing.Text;
52 using System.Text;
53 using RTF=System.Windows.Forms.RTF;
54
55 namespace System.Windows.Forms {
56         internal enum LineColor {
57                 Red     = 0,
58                 Black   = 1
59         }
60
61         internal enum CaretSelection {
62                 Position,       // Selection=Caret
63                 Word,           // Selection=Word under caret
64                 Line            // Selection=Line under caret
65         }
66
67         [Flags]
68         internal enum FormatSpecified {
69                 None,
70
71                 BackColor = 2,
72                 Font = 4,
73                 Color = 8,
74         }
75
76         internal enum CaretDirection {
77                 CharForward,    // Move a char to the right
78                 CharBack,       // Move a char to the left
79                 LineUp,         // Move a line up
80                 LineDown,       // Move a line down
81                 Home,           // Move to the beginning of the line
82                 End,            // Move to the end of the line
83                 PgUp,           // Move one page up
84                 PgDn,           // Move one page down
85                 CtrlPgUp,       // Move caret to the first visible char in the viewport
86                 CtrlPgDn,       // Move caret to the last visible char in the viewport
87                 CtrlHome,       // Move to the beginning of the document
88                 CtrlEnd,        // Move to the end of the document
89                 WordBack,       // Move to the beginning of the previous word (or beginning of line)
90                 WordForward,    // Move to the beginning of the next word (or end of line)
91                 SelectionStart, // Move to the beginning of the current selection
92                 SelectionEnd,   // Move to the end of the current selection
93                 CharForwardNoWrap,   // Move a char forward, but don't wrap onto the next line
94                 CharBackNoWrap      // Move a char backward, but don't wrap onto the previous line
95         }
96
97         internal enum LineEnding {
98                 Wrap = 1,    // line wraps to the next line
99                 Limp = 2,    // \r
100                 Hard = 4,    // \r\n
101                 Soft = 8,    // \r\r\n
102                 Rich = 16,    // \n
103
104                 None = 0
105         }
106         
107         internal class Document : ICloneable, IEnumerable {
108                 #region Structures
109                 // FIXME - go through code and check for places where
110                 // we do explicit comparisons instead of using the compare overloads
111                 internal struct Marker {
112                         internal Line           line;
113                         internal LineTag        tag;
114                         internal int            pos;
115                         internal int            height;
116
117                         public static bool operator<(Marker lhs, Marker rhs) {
118                                 if (lhs.line.line_no < rhs.line.line_no) {
119                                         return true;
120                                 }
121
122                                 if (lhs.line.line_no == rhs.line.line_no) {
123                                         if (lhs.pos < rhs.pos) {
124                                                 return true;
125                                         }
126                                 }
127                                 return false;
128                         }
129
130                         public static bool operator>(Marker lhs, Marker rhs) {
131                                 if (lhs.line.line_no > rhs.line.line_no) {
132                                         return true;
133                                 }
134
135                                 if (lhs.line.line_no == rhs.line.line_no) {
136                                         if (lhs.pos > rhs.pos) {
137                                                 return true;
138                                         }
139                                 }
140                                 return false;
141                         }
142
143                         public static bool operator==(Marker lhs, Marker rhs) {
144                                 if ((lhs.line.line_no == rhs.line.line_no) && (lhs.pos == rhs.pos)) {
145                                         return true;
146                                 }
147                                 return false;
148                         }
149
150                         public static bool operator!=(Marker lhs, Marker rhs) {
151                                 if ((lhs.line.line_no != rhs.line.line_no) || (lhs.pos != rhs.pos)) {
152                                         return true;
153                                 }
154                                 return false;
155                         }
156
157                         public void Combine(Line move_to_line, int move_to_line_length) {
158                                 line = move_to_line;
159                                 pos += move_to_line_length;
160                                 tag = LineTag.FindTag(line, pos);
161                         }
162
163                         // This is for future use, right now Document.Split does it by hand, with some added shortcut logic
164                         public void Split(Line move_to_line, int split_at) {
165                                 line = move_to_line;
166                                 pos -= split_at;
167                                 tag = LineTag.FindTag(line, pos);
168                         }
169
170                         public override bool Equals(object obj) {
171                                    return this==(Marker)obj;
172                         }
173
174                         public override int GetHashCode() {
175                                 return base.GetHashCode ();
176                         }
177
178                         public override string ToString() {
179                                 return "Marker Line " + line + ", Position " + pos;
180                         }
181
182                 }
183                 #endregion Structures
184
185                 #region Local Variables
186                 private Line            document;
187                 private int             lines;
188                 private Line            sentinel;
189                 private int             document_id;
190                 private Random          random = new Random();
191                 internal string         password_char;
192                 private StringBuilder   password_cache;
193                 private bool            calc_pass;
194                 private int             char_count;
195                 private bool            enable_links;
196
197                 // For calculating widths/heights
198                 public static readonly StringFormat string_format = new StringFormat (StringFormat.GenericTypographic);
199
200                 private int             recalc_suspended;
201                 private bool            recalc_pending;
202                 private int             recalc_start = 1;   // This starts at one, since lines are 1 based
203                 private int             recalc_end;
204                 private bool            recalc_optimize;
205
206                 private int             update_suspended;
207                 private bool update_pending;
208                 private int update_start = 1;
209
210                 internal bool           multiline;
211                 internal HorizontalAlignment alignment;
212                 internal bool           wrap;
213
214                 internal UndoManager    undo;
215
216                 internal Marker         caret;
217                 internal Marker         selection_start;
218                 internal Marker         selection_end;
219                 internal bool           selection_visible;
220                 internal Marker         selection_anchor;
221                 internal Marker         selection_prev;
222                 internal bool           selection_end_anchor;
223
224                 internal int            viewport_x;
225                 internal int            viewport_y;             // The visible area of the document
226                 internal int            offset_x;
227                 internal int            offset_y;
228                 internal int            viewport_width;
229                 internal int            viewport_height;
230
231                 internal int            document_x;             // Width of the document
232                 internal int            document_y;             // Height of the document
233
234                 internal Rectangle      invalid;
235
236                 internal int            crlf_size;              // 1 or 2, depending on whether we use \r\n or just \n
237
238                 internal TextBoxBase    owner;                  // Who's owning us?
239                 static internal int     caret_width = 1;
240                 static internal int     caret_shift = 1;
241
242                 internal int left_margin = 2;  // A left margin for all lines
243                 internal int top_margin = 2;
244                 internal int right_margin = 2;
245                 #endregion      // Local Variables
246
247                 #region Constructors
248                 internal Document (TextBoxBase owner)
249                 {
250                         lines = 0;
251
252                         this.owner = owner;
253
254                         multiline = true;
255                         password_char = "";
256                         calc_pass = false;
257                         recalc_pending = false;
258
259                         // Tree related stuff
260                         sentinel = new Line (this, LineEnding.None);
261                         sentinel.color = LineColor.Black;
262
263                         document = sentinel;
264
265                         // We always have a blank line
266                         owner.HandleCreated += new EventHandler(owner_HandleCreated);
267                         owner.VisibleChanged += new EventHandler(owner_VisibleChanged);
268
269                         Add (1, String.Empty, owner.Font, owner.ForeColor, LineEnding.None);
270
271                         undo = new UndoManager (this);
272
273                         selection_visible = false;
274                         selection_start.line = this.document;
275                         selection_start.pos = 0;
276                         selection_start.tag = selection_start.line.tags;
277                         selection_end.line = this.document;
278                         selection_end.pos = 0;
279                         selection_end.tag = selection_end.line.tags;
280                         selection_anchor.line = this.document;
281                         selection_anchor.pos = 0;
282                         selection_anchor.tag = selection_anchor.line.tags;
283                         caret.line = this.document;
284                         caret.pos = 0;
285                         caret.tag = caret.line.tags;
286
287                         viewport_x = 0;
288                         viewport_y = 0;
289
290                         offset_x = 0;
291                         offset_y = 0;
292
293                         crlf_size = 2;
294
295                         // Default selection is empty
296
297                         document_id = random.Next();
298
299                         string_format.Trimming = StringTrimming.None;
300                         string_format.FormatFlags = StringFormatFlags.DisplayFormatControl;
301
302                         UpdateMargins ();
303                 }
304                 #endregion
305
306                 #region Internal Properties
307                 internal Line Root {
308                         get {
309                                 return document;
310                         }
311
312                         set {
313                                 document = value;
314                         }
315                 }
316
317                 internal int Lines {
318                         get {
319                                 return lines;
320                         }
321                 }
322
323                 internal Line CaretLine {
324                         get {
325                                 return caret.line;
326                         }
327                 }
328
329                 internal int CaretPosition {
330                         get {
331                                 return caret.pos;
332                         }
333                 }
334
335                 internal Point Caret {
336                         get {
337                                 return new Point((int)caret.tag.Line.widths[caret.pos] + caret.line.X, caret.line.Y);
338                         }
339                 }
340
341                 internal LineTag CaretTag {
342                         get {
343                                 return caret.tag;
344                         }
345
346                         set {
347                                 caret.tag = value;
348                         }
349                 }
350
351                 internal int CRLFSize {
352                         get {
353                                 return crlf_size;
354                         }
355
356                         set {
357                                 crlf_size = value;
358                         }
359                 }
360
361                 /// <summary>
362                 ///  Whether text is scanned for links
363                 /// </summary>
364                 internal bool EnableLinks {
365                         get { return enable_links; }
366                         set { enable_links = value; }
367                 }
368
369                 internal string PasswordChar {
370                         get {
371                                 return password_char;
372                         }
373
374                         set {
375                                 password_char = value;
376                                 PasswordCache.Length = 0;
377                                 if ((password_char.Length != 0) && (password_char[0] != '\0')) {
378                                         calc_pass = true;
379                                 } else {
380                                         calc_pass = false;
381                                 }
382                         }
383                 }
384
385                 private StringBuilder PasswordCache {
386                         get { 
387                                 if (password_cache == null) 
388                                           password_cache = new StringBuilder(); 
389                                 return password_cache;
390                         }
391                 }
392
393                 internal int ViewPortX {
394                         get {
395                                 return viewport_x;
396                         }
397
398                         set {
399                                 viewport_x = value;
400                         }
401                 }
402
403                 internal int Length {
404                         get {
405                                 return char_count + lines - 1;  // Add \n for each line but the last
406                         }
407                 }
408
409                 private int CharCount {
410                         get {
411                                 return char_count;
412                         }
413
414                         set {
415                                 char_count = value;
416
417                                 if (LengthChanged != null) {
418                                         LengthChanged(this, EventArgs.Empty);
419                                 }
420                         }
421                 }
422
423                 internal int ViewPortY {
424                         get {
425                                 return viewport_y;
426                         }
427
428                         set {
429                                 viewport_y = value;
430                         }
431                 }
432
433                 internal int OffsetX
434                 {
435                         get
436                         {
437                                 return offset_x;
438                         }
439
440                         set
441                         {
442                                 offset_x = value;
443                         }
444                 }
445
446                 internal int OffsetY
447                 {
448                         get
449                         {
450                                 return offset_y;
451                         }
452
453                         set
454                         {
455                                 offset_y = value;
456                         }
457                 }
458
459                 internal int ViewPortWidth {
460                         get {
461                                 return viewport_width;
462                         }
463
464                         set {
465                                 viewport_width = value;
466                         }
467                 }
468
469                 internal int ViewPortHeight {
470                         get {
471                                 return viewport_height;
472                         }
473
474                         set {
475                                 viewport_height = value;
476                         }
477                 }
478
479
480                 internal int Width {
481                         get {
482                                 return this.document_x;
483                         }
484                 }
485
486                 internal int Height {
487                         get {
488                                 return this.document_y;
489                         }
490                 }
491
492                 internal bool SelectionVisible {
493                         get {
494                                 return selection_visible;
495                         }
496                 }
497
498                 internal bool Wrap {
499                         get {
500                                 return wrap;
501                         }
502
503                         set {
504                                 wrap = value;
505                         }
506                 }
507
508                 #endregion      // Internal Properties
509
510                 #region Private Methods
511
512                 internal void UpdateMargins ()
513                 {
514                         switch (owner.actual_border_style) {
515                                 case BorderStyle.None:
516                                         left_margin = 0;
517                                         top_margin = 0;
518                                         right_margin = 1;
519                                         break;
520                                 case BorderStyle.FixedSingle:
521                                         left_margin = 2;
522                                         top_margin = 2;
523                                         right_margin = 3;
524                                         break;
525                                 case BorderStyle.Fixed3D:
526                                         left_margin = 1;
527                                         top_margin = 1;
528                                         right_margin = 2;
529                                         break;
530                         }
531                 }
532
533                 internal void SuspendRecalc ()
534                 {
535                         if (recalc_suspended == 0) {
536                                 recalc_start = int.MaxValue;
537                                 recalc_end = int.MinValue;
538                         }
539                         
540                         recalc_suspended++;
541                 }
542
543                 internal void ResumeRecalc (bool immediate_update)
544                 {
545                         if (recalc_suspended > 0)
546                                 recalc_suspended--;
547
548                         if (recalc_suspended == 0 && (immediate_update || recalc_pending) && !(recalc_start == int.MaxValue && recalc_end == int.MinValue)) {
549                                 RecalculateDocument (owner.CreateGraphicsInternal (), recalc_start, recalc_end, recalc_optimize);
550                                 recalc_pending = false;
551                         }
552                 }
553
554                 internal void SuspendUpdate ()
555                 {
556                         update_suspended++;
557                 }
558
559                 internal void ResumeUpdate (bool immediate_update)
560                 {
561                         if (update_suspended > 0)
562                                 update_suspended--;
563
564                         if (immediate_update && update_suspended == 0 && update_pending) {
565                                 UpdateView (GetLine (update_start), 0);
566                                 update_pending = false;
567                         }
568                 }
569
570                 // For debugging
571                 internal int DumpTree(Line line, bool with_tags) {
572                         int     total;
573
574                         total = 1;
575
576                         Console.Write("Line {0} [# {1}], Y: {2}, ending style: {3},  Text: '{4}'",
577                                         line.line_no, line.GetHashCode(), line.Y, line.ending,
578                                         line.text != null ? line.text.ToString() : "undefined");
579
580                         if (line.left == sentinel) {
581                                 Console.Write(", left = sentinel");
582                         } else if (line.left == null) {
583                                 Console.Write(", left = NULL");
584                         }
585
586                         if (line.right == sentinel) {
587                                 Console.Write(", right = sentinel");
588                         } else if (line.right == null) {
589                                 Console.Write(", right = NULL");
590                         }
591
592                         Console.WriteLine("");
593
594                         if (with_tags) {
595                                 LineTag tag;
596                                 int     count;
597                                 int     length;
598
599                                 tag = line.tags;
600                                 count = 1;
601                                 length = 0;
602                                 Console.Write("   Tags: ");
603                                 while (tag != null) {
604                                         Console.Write("{0} <{1}>-<{2}>", count++, tag.Start, tag.End
605                                                         /*line.text.ToString (tag.start - 1, tag.length)*/);
606                                         length += tag.Length;
607
608                                         if (tag.Line != line) {
609                                                 Console.Write("BAD line link");
610                                                 throw new Exception("Bad line link in tree");
611                                         }
612                                         tag = tag.Next;
613                                         if (tag != null) {
614                                                 Console.Write(", ");
615                                         }
616                                 }
617                                 if (length > line.text.Length) {
618                                         throw new Exception(String.Format("Length of tags more than length of text on line (expected {0} calculated {1})", line.text.Length, length));
619                                 } else if (length < line.text.Length) {
620                                         throw new Exception(String.Format("Length of tags less than length of text on line (expected {0} calculated {1})", line.text.Length, length));
621                                 }
622                                 Console.WriteLine("");
623                         }
624                         if (line.left != null) {
625                                 if (line.left != sentinel) {
626                                         total += DumpTree(line.left, with_tags);
627                                 }
628                         } else {
629                                 if (line != sentinel) {
630                                         throw new Exception("Left should not be NULL");
631                                 }
632                         }
633
634                         if (line.right != null) {
635                                 if (line.right != sentinel) {
636                                         total += DumpTree(line.right, with_tags);
637                                 }
638                         } else {
639                                 if (line != sentinel) {
640                                         throw new Exception("Right should not be NULL");
641                                 }
642                         }
643
644                         for (int i = 1; i <= this.lines; i++) {
645                                 if (GetLine(i) == null) {
646                                         throw new Exception(String.Format("Hole in line order, missing {0}", i));
647                                 }
648                         }
649
650                         if (line == this.Root) {
651                                 if (total < this.lines) {
652                                         throw new Exception(String.Format("Not enough nodes in tree, found {0}, expected {1}", total, this.lines));
653                                 } else if (total > this.lines) {
654                                         throw new Exception(String.Format("Too many nodes in tree, found {0}, expected {1}", total, this.lines));
655                                 }
656                         }
657
658                         return total;
659                 }
660
661                 private void SetSelectionVisible (bool value)
662                 {
663                         selection_visible = value;
664
665                         // cursor and selection are enemies, we can't have both in the same room at the same time
666                         if (owner.IsHandleCreated && !owner.show_caret_w_selection)
667                                 XplatUI.CaretVisible (owner.Handle, !selection_visible);
668                 }
669
670                 private void DecrementLines(int line_no) {
671                         int     current;
672
673                         current = line_no;
674                         while (current <= lines) {
675                                 GetLine(current).line_no--;
676                                 current++;
677                         }
678                         return;
679                 }
680
681                 private void IncrementLines(int line_no) {
682                         int     current;
683
684                         current = this.lines;
685                         while (current >= line_no) {
686                                 GetLine(current).line_no++;
687                                 current--;
688                         }
689                         return;
690                 }
691
692                 private void RebalanceAfterAdd(Line line1) {
693                         Line    line2;
694
695                         while ((line1 != document) && (line1.parent.color == LineColor.Red)) {
696                                 if (line1.parent == line1.parent.parent.left) {
697                                         line2 = line1.parent.parent.right;
698
699                                         if ((line2 != null) && (line2.color == LineColor.Red)) {
700                                                 line1.parent.color = LineColor.Black;
701                                                 line2.color = LineColor.Black;
702                                                 line1.parent.parent.color = LineColor.Red;
703                                                 line1 = line1.parent.parent;
704                                         } else {
705                                                 if (line1 == line1.parent.right) {
706                                                         line1 = line1.parent;
707                                                         RotateLeft(line1);
708                                                 }
709
710                                                 line1.parent.color = LineColor.Black;
711                                                 line1.parent.parent.color = LineColor.Red;
712
713                                                 RotateRight(line1.parent.parent);
714                                         }
715                                 } else {
716                                         line2 = line1.parent.parent.left;
717
718                                         if ((line2 != null) && (line2.color == LineColor.Red)) {
719                                                 line1.parent.color = LineColor.Black;
720                                                 line2.color = LineColor.Black;
721                                                 line1.parent.parent.color = LineColor.Red;
722                                                 line1 = line1.parent.parent;
723                                         } else {
724                                                 if (line1 == line1.parent.left) {
725                                                         line1 = line1.parent;
726                                                         RotateRight(line1);
727                                                 }
728
729                                                 line1.parent.color = LineColor.Black;
730                                                 line1.parent.parent.color = LineColor.Red;
731                                                 RotateLeft(line1.parent.parent);
732                                         }
733                                 }
734                         }
735                         document.color = LineColor.Black;
736                 }
737
738                 private void RebalanceAfterDelete(Line line1) {
739                         Line line2;
740
741                         while ((line1 != document) && (line1.color == LineColor.Black)) {
742                                 if (line1 == line1.parent.left) {
743                                         line2 = line1.parent.right;
744                                         if (line2.color == LineColor.Red) { 
745                                                 line2.color = LineColor.Black;
746                                                 line1.parent.color = LineColor.Red;
747                                                 RotateLeft(line1.parent);
748                                                 line2 = line1.parent.right;
749                                         }
750                                         if ((line2.left.color == LineColor.Black) && (line2.right.color == LineColor.Black)) { 
751                                                 line2.color = LineColor.Red;
752                                                 line1 = line1.parent;
753                                         } else {
754                                                 if (line2.right.color == LineColor.Black) {
755                                                         line2.left.color = LineColor.Black;
756                                                         line2.color = LineColor.Red;
757                                                         RotateRight(line2);
758                                                         line2 = line1.parent.right;
759                                                 }
760                                                 line2.color = line1.parent.color;
761                                                 line1.parent.color = LineColor.Black;
762                                                 line2.right.color = LineColor.Black;
763                                                 RotateLeft(line1.parent);
764                                                 line1 = document;
765                                         }
766                                 } else { 
767                                         line2 = line1.parent.left;
768                                         if (line2.color == LineColor.Red) {
769                                                 line2.color = LineColor.Black;
770                                                 line1.parent.color = LineColor.Red;
771                                                 RotateRight(line1.parent);
772                                                 line2 = line1.parent.left;
773                                         }
774                                         if ((line2.right.color == LineColor.Black) && (line2.left.color == LineColor.Black)) {
775                                                 line2.color = LineColor.Red;
776                                                 line1 = line1.parent;
777                                         } else {
778                                                 if (line2.left.color == LineColor.Black) {
779                                                         line2.right.color = LineColor.Black;
780                                                         line2.color = LineColor.Red;
781                                                         RotateLeft(line2);
782                                                         line2 = line1.parent.left;
783                                                 }
784                                                 line2.color = line1.parent.color;
785                                                 line1.parent.color = LineColor.Black;
786                                                 line2.left.color = LineColor.Black;
787                                                 RotateRight(line1.parent);
788                                                 line1 = document;
789                                         }
790                                 }
791                         }
792                         line1.color = LineColor.Black;
793                 }
794
795                 private void RotateLeft(Line line1) {
796                         Line    line2 = line1.right;
797
798                         line1.right = line2.left;
799
800                         if (line2.left != sentinel) {
801                                 line2.left.parent = line1;
802                         }
803
804                         if (line2 != sentinel) {
805                                 line2.parent = line1.parent;
806                         }
807
808                         if (line1.parent != null) {
809                                 if (line1 == line1.parent.left) {
810                                         line1.parent.left = line2;
811                                 } else {
812                                         line1.parent.right = line2;
813                                 }
814                         } else {
815                                 document = line2;
816                         }
817
818                         line2.left = line1;
819                         if (line1 != sentinel) {
820                                 line1.parent = line2;
821                         }
822                 }
823
824                 private void RotateRight(Line line1) {
825                         Line line2 = line1.left;
826
827                         line1.left = line2.right;
828
829                         if (line2.right != sentinel) {
830                                 line2.right.parent = line1;
831                         }
832
833                         if (line2 != sentinel) {
834                                 line2.parent = line1.parent;
835                         }
836
837                         if (line1.parent != null) {
838                                 if (line1 == line1.parent.right) {
839                                         line1.parent.right = line2;
840                                 } else {
841                                         line1.parent.left = line2;
842                                 }
843                         } else {
844                                 document = line2;
845                         }
846
847                         line2.right = line1;
848                         if (line1 != sentinel) {
849                                 line1.parent = line2;
850                         }
851                 }        
852
853
854                 internal void UpdateView(Line line, int pos) {
855                         if (!owner.IsHandleCreated) {
856                                 return;
857                         }
858
859                         if (update_suspended > 0) {
860                                 update_start = Math.Min (update_start, line.line_no);
861                                 // update_end = Math.Max (update_end, line.line_no);
862                                 // recalc_optimize = true;
863                                 update_pending = true;
864                                 return;
865                         }
866
867                         // Optimize invalidation based on Line alignment
868                         if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no, true)) {
869                                 // Lineheight changed, invalidate the rest of the document
870                                 if ((line.Y - viewport_y) >=0 ) {
871                                         // We formatted something that's in view, only draw parts of the screen
872                                         owner.Invalidate(new Rectangle(
873                                                 offset_x, 
874                                                 line.Y - viewport_y + offset_y, 
875                                                 viewport_width, 
876                                                 owner.Height - line.Y - viewport_y));
877                                 } else {
878                                         // The tag was above the visible area, draw everything
879                                         owner.Invalidate();
880                                 }
881                         } else {
882                                 switch(line.alignment) {
883                                         case HorizontalAlignment.Left: {
884                                                 owner.Invalidate(new Rectangle(
885                                                         line.X + ((int)line.widths[pos] - viewport_x - 1) + offset_x, 
886                                                         line.Y - viewport_y + offset_y, 
887                                                         viewport_width, 
888                                                         line.height + 1));
889                                                 break;
890                                         }
891
892                                         case HorizontalAlignment.Center: {
893                                                 owner.Invalidate(new Rectangle(
894                                                         line.X + offset_x, 
895                                                         line.Y - viewport_y + offset_y, 
896                                                         viewport_width, 
897                                                         line.height + 1));
898                                                 break;
899                                         }
900
901                                         case HorizontalAlignment.Right: {
902                                                 owner.Invalidate(new Rectangle(
903                                                         line.X + offset_x, 
904                                                         line.Y - viewport_y + offset_y, 
905                                                         (int)line.widths[pos + 1] - viewport_x + line.X, 
906                                                         line.height + 1));
907                                                 break;
908                                         }
909                                 }
910                         }
911                 }
912
913
914                 // Update display from line, down line_count lines; pos is unused, but required for the signature
915                 internal void UpdateView(Line line, int line_count, int pos) {
916                         if (!owner.IsHandleCreated) {
917                                 return;
918                         }
919
920                         if (recalc_suspended > 0) {
921                                 recalc_start = Math.Min (recalc_start, line.line_no);
922                                 recalc_end = Math.Max (recalc_end, line.line_no + line_count);
923                                 recalc_optimize = true;
924                                 recalc_pending = true;
925                                 return;
926                         }
927
928                         int start_line_top = line.Y;                    
929
930                         int end_line_bottom;
931                         Line end_line;
932
933                         end_line = GetLine (line.line_no + line_count);
934                         if (end_line == null)
935                                 end_line = GetLine (lines);
936
937
938                         end_line_bottom = end_line.Y + end_line.height;
939                         
940                         if (RecalculateDocument(owner.CreateGraphicsInternal(), line.line_no, line.line_no + line_count, true)) {
941                                 // Lineheight changed, invalidate the rest of the document
942                                 if ((line.Y - viewport_y) >=0 ) {
943                                         // We formatted something that's in view, only draw parts of the screen
944                                         owner.Invalidate(new Rectangle(
945                                                 offset_x, 
946                                                 line.Y - viewport_y + offset_y, 
947                                                 viewport_width, 
948                                                 owner.Height - line.Y - viewport_y));
949                                 } else {
950                                         // The tag was above the visible area, draw everything
951                                         owner.Invalidate();
952                                 }
953                         } else {
954                                 int x = 0 - viewport_x + offset_x;
955                                 int w = viewport_width;
956                                 int y = Math.Min (start_line_top - viewport_y, line.Y - viewport_y) + offset_y;
957                                 int h = Math.Max (end_line_bottom - y, end_line.Y + end_line.height - y);
958
959                                 owner.Invalidate (new Rectangle (x, y, w, h));
960                         }
961                 }
962
963                 /// <summary>
964                 ///  Scans the next paragraph for http:/ ftp:/ www. https:/ etc and marks the tags
965                 ///  as links.
966                 /// </summary>
967                 /// <param name="start_line">The line to start on</param>
968                 /// <param name="link_changed">marks as true if something is changed</param>
969                 private void ScanForLinks (Line start_line, ref bool link_changed)
970                 {
971                         Line current_line = start_line;
972                         StringBuilder line_no_breaks = new StringBuilder ();
973                         StringBuilder line_link_record = new StringBuilder ();
974                         ArrayList cumulative_length_list = new ArrayList ();
975                         bool update_caret_tag = false;
976
977                         cumulative_length_list.Add (0);
978
979                         while (current_line != null) {
980                                 line_no_breaks.Append (current_line.text);
981
982                                 if (link_changed == false)
983                                         current_line.LinkRecord (line_link_record);
984
985                                 current_line.ClearLinks ();
986
987                                 cumulative_length_list.Add (line_no_breaks.Length);
988
989                                 if (current_line.ending == LineEnding.Wrap)
990                                         current_line = GetLine (current_line.LineNo + 1);
991                                 else
992                                         break;
993                         }
994
995                         // search for protocols.. make sure www. is first!
996                         string [] search_terms = new string [] { "www.", "http:/", "ftp:/", "https:/" };
997                         int search_found = 0;
998                         int index_found = 0;
999                         string line_no_breaks_string = line_no_breaks.ToString ();
1000                         int line_no_breaks_index = 0;
1001                         int link_end = 0;
1002
1003                         while (true) {
1004                                 if (line_no_breaks_index >= line_no_breaks_string.Length)
1005                                         break;
1006
1007                                 index_found = FirstIndexOfAny (line_no_breaks_string, search_terms, line_no_breaks_index, out search_found);
1008
1009                                 //no links found on this line
1010                                 if (index_found == -1)
1011                                         break;
1012
1013                                 if (search_found == 0) {
1014                                         // if we are at the end of the line to analyse and the end of the line
1015                                         // is "www." then there are no links here
1016                                         if (line_no_breaks_string.Length == index_found + search_terms [0].Length)
1017                                                 break;
1018
1019                                         // if after www. we don't have a letter a digit or a @ or - or /
1020                                         // then it is not a web address, we should continue searching
1021                                         if (char.IsLetterOrDigit (line_no_breaks_string [index_found + search_terms [0].Length]) == false &&
1022                                                 "@/~".IndexOf (line_no_breaks_string [index_found + search_terms [0].Length].ToString ()) == -1) {
1023                                                 line_no_breaks_index = index_found + search_terms [0].Length;
1024                                                 continue;
1025                                         }
1026                                 }
1027
1028                                 link_end = line_no_breaks_string.Length - 1;
1029                                 line_no_breaks_index = line_no_breaks_string.Length;
1030
1031                                 // we've found a link, we just need to find where it ends now
1032                                 for (int i = index_found + search_terms [search_found].Length; i < line_no_breaks_string.Length; i++) {
1033                                         if (line_no_breaks_string [i - 1] == '.') {
1034                                                 if (char.IsLetterOrDigit (line_no_breaks_string [i]) == false &&
1035                                                         "@/~".IndexOf (line_no_breaks_string [i].ToString ()) == -1) {
1036                                                         link_end = i - 1;
1037                                                         line_no_breaks_index = i;
1038                                                         break;
1039                                                 }
1040                                         } else {
1041                                                 if (char.IsLetterOrDigit (line_no_breaks_string [i]) == false &&
1042                                                         "@-/:~.?=_&".IndexOf (line_no_breaks_string [i].ToString ()) == -1) {
1043                                                         link_end = i - 1;
1044                                                         line_no_breaks_index = i;
1045                                                         break;
1046                                                 }
1047                                         }
1048                                 }
1049
1050                                 string link_text = line_no_breaks_string.Substring (index_found, link_end - index_found + 1);
1051                                 int current_cumulative = 0;
1052
1053                                 // we've found a link - index_found -> link_end
1054                                 // now we just make all the tags as containing link and
1055                                 // point them to the text for the whole link
1056
1057                                 current_line = start_line;
1058
1059                                 //find the line we start on
1060                                 for (current_cumulative = 1; current_cumulative < cumulative_length_list.Count; current_cumulative++)
1061                                         if ((int)cumulative_length_list [current_cumulative] > index_found)
1062                                                 break;
1063
1064                                 current_line = GetLine (start_line.LineNo + current_cumulative - 1);
1065
1066                                 // find the tag we start on
1067                                 LineTag current_tag = current_line.FindTag (index_found - (int)cumulative_length_list [current_cumulative - 1] + 1);
1068
1069                                 if (current_tag.Start != (index_found - (int)cumulative_length_list [current_cumulative - 1]) + 1) {
1070                                         if (current_tag == CaretTag)
1071                                                 update_caret_tag = true;
1072
1073                                         current_tag = current_tag.Break ((index_found - (int)cumulative_length_list [current_cumulative - 1]) + 1);
1074                                 }
1075
1076                                 // set the tag
1077                                 current_tag.IsLink = true;
1078                                 current_tag.LinkText = link_text;
1079
1080                                 //go through each character
1081                                 // find the tag we are in
1082                                 // skip the number of characters in the tag
1083                                 for (int i = 1; i < link_text.Length; i++) {
1084                                         // on to a new word-wrapped line
1085                                         if ((int)cumulative_length_list [current_cumulative] <= index_found + i) {
1086
1087                                                 current_line = GetLine (start_line.LineNo + current_cumulative++);
1088                                                 current_tag = current_line.FindTag (index_found + i - (int)cumulative_length_list [current_cumulative - 1] + 1);
1089
1090                                                 current_tag.IsLink = true;
1091                                                 current_tag.LinkText = link_text;
1092
1093                                                 continue;
1094                                         }
1095
1096                                         if (current_tag.End < index_found + 1 + i - (int)cumulative_length_list [current_cumulative - 1]) {
1097                                                 // skip empty tags in the middle of the URL
1098                                                 do {
1099                                                         current_tag = current_tag.Next;
1100                                                 } while (current_tag.Length == 0);
1101
1102                                                 current_tag.IsLink = true;
1103                                                 current_tag.LinkText = link_text;
1104                                         }
1105                                 }
1106
1107                                 //if there are characters left in the tag after the link
1108                                 // split the tag
1109                                 // make the second part a non link
1110                                 if (current_tag.End > (index_found + link_text.Length + 1) - (int)cumulative_length_list [current_cumulative - 1]) {
1111                                         if (current_tag == CaretTag)
1112                                                 update_caret_tag = true;
1113
1114                                         current_tag.Break ((index_found + link_text.Length + 1) - (int)cumulative_length_list [current_cumulative - 1]);
1115                                 }
1116                         }
1117
1118                         if (update_caret_tag) {
1119                                 CaretTag = LineTag.FindTag (CaretLine, CaretPosition);
1120                                 link_changed = true;
1121                         } else {
1122                                 if (link_changed == false) {
1123                                         current_line = start_line;
1124                                         StringBuilder new_link_record = new StringBuilder ();
1125
1126                                         while (current_line != null) {
1127                                                 current_line.LinkRecord (new_link_record);
1128
1129                                                 if (current_line.ending == LineEnding.Wrap)
1130                                                         current_line = GetLine (current_line.LineNo + 1);
1131                                                 else
1132                                                         break;
1133                                         }
1134
1135                                         if (new_link_record.Equals (line_link_record) == false)
1136                                                 link_changed = true;
1137                                 }
1138                         }
1139                 }
1140
1141                 private int FirstIndexOfAny (string haystack, string [] needles, int start_index, out int term_found)
1142                 {
1143                         term_found = -1;
1144                         int best_index = -1;
1145
1146                         for (int i = 0; i < needles.Length; i++) {
1147 #if  NET_2_0
1148                                 int index = haystack.IndexOf (needles [i], start_index, StringComparison.InvariantCultureIgnoreCase);
1149 #else
1150                                 int index = haystack.ToLower().IndexOf(needles[i], start_index);
1151 #endif
1152
1153                                 if (index > -1) {
1154                                         if (term_found > -1) {
1155                                                 if (index < best_index) {
1156                                                         best_index = index;
1157                                                         term_found = i;
1158                                                 }
1159                                         } else {
1160                                                 best_index = index;
1161                                                 term_found = i;
1162                                         }
1163                                 }
1164                         }
1165
1166                         return best_index;
1167                 }
1168
1169
1170
1171                 private void InvalidateLinks (Rectangle clip)
1172                 {
1173                         for (int i = (owner.list_links.Count - 1); i >= 0; i--) {
1174                                 TextBoxBase.LinkRectangle link = (TextBoxBase.LinkRectangle) owner.list_links [i];
1175
1176                                 if (clip.IntersectsWith (link.LinkAreaRectangle))
1177                                         owner.list_links.RemoveAt (i);
1178                         }
1179                 }
1180                 #endregion      // Private Methods
1181
1182                 #region Internal Methods
1183
1184                 internal void ScanForLinks (int start, int end, ref bool link_changed)
1185                 {
1186                         Line line = null;
1187                         LineEnding lastending = LineEnding.Rich;
1188
1189                         // make sure we start scanning at the real begining of the line
1190                         while (true) {
1191                                 if (start != 1 && GetLine (start - 1).ending == LineEnding.Wrap)
1192                                         start--;
1193                                 else
1194                                         break;
1195                         }
1196
1197                         for (int i = start; i <= end && i <= lines; i++) {
1198                                 line = GetLine (i);
1199
1200                                 if (lastending != LineEnding.Wrap)
1201                                         ScanForLinks (line, ref link_changed);
1202
1203                                 lastending = line.ending;
1204
1205                                 if (lastending == LineEnding.Wrap && (i + 1) <= end)
1206                                         end++;
1207                         }
1208                 }
1209
1210                 // Clear the document and reset state
1211                 internal void Empty() {
1212
1213                         document = sentinel;
1214                         lines = 0;
1215
1216                         // We always have a blank line
1217                         Add (1, String.Empty, owner.Font, owner.ForeColor, LineEnding.None);
1218                         
1219                         this.RecalculateDocument(owner.CreateGraphicsInternal());
1220                         PositionCaret(0, 0);
1221
1222                         SetSelectionVisible (false);
1223
1224                         selection_start.line = this.document;
1225                         selection_start.pos = 0;
1226                         selection_start.tag = selection_start.line.tags;
1227                         selection_end.line = this.document;
1228                         selection_end.pos = 0;
1229                         selection_end.tag = selection_end.line.tags;
1230                         char_count = 0;
1231
1232                         viewport_x = 0;
1233                         viewport_y = 0;
1234
1235                         document_x = 0;
1236                         document_y = 0;
1237
1238                         if (owner.IsHandleCreated)
1239                                 owner.Invalidate ();
1240                 }
1241
1242                 internal void PositionCaret(Line line, int pos) {
1243                         caret.tag = line.FindTag (pos);
1244
1245                         MoveCaretToTextTag ();
1246
1247                         caret.line = line;
1248                         caret.pos = pos;
1249
1250                         if (owner.IsHandleCreated) {
1251                                 if (owner.Focused) {
1252                                         if (caret.height != caret.tag.Height)
1253                                                 XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
1254                                         XplatUI.SetCaretPos(owner.Handle, 
1255                                                 offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x, 
1256                                                 offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
1257                                 }
1258
1259                                 if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1260                         }
1261
1262                         // We set this at the end because we use the heights to determine whether or
1263                         // not we need to recreate the caret
1264                         caret.height = caret.tag.Height;
1265
1266                 }
1267
1268                 internal void PositionCaret(int x, int y) {
1269                         if (!owner.IsHandleCreated) {
1270                                 return;
1271                         }
1272
1273                         caret.tag = FindCursor(x, y, out caret.pos);
1274                         
1275                         MoveCaretToTextTag ();
1276                         
1277                         caret.line = caret.tag.Line;
1278                         caret.height = caret.tag.Height;
1279
1280                         if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
1281                                 XplatUI.CreateCaret (owner.Handle, caret_width, caret.height);
1282                                 XplatUI.SetCaretPos(owner.Handle, 
1283                                         (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x + offset_x, 
1284                                         offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
1285                         }
1286
1287                         if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1288                 }
1289
1290                 internal void CaretHasFocus() {
1291                         if ((caret.tag != null) && owner.IsHandleCreated) {
1292                                 XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
1293                                 XplatUI.SetCaretPos(owner.Handle, 
1294                                         offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x, 
1295                                         offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
1296
1297                                 DisplayCaret ();
1298                         }
1299
1300                         if (owner.IsHandleCreated && SelectionLength () > 0) {
1301                                 InvalidateSelectionArea ();
1302                         }
1303                 }
1304
1305                 internal void CaretLostFocus() {
1306                         if (!owner.IsHandleCreated) {
1307                                 return;
1308                         }
1309                         XplatUI.DestroyCaret(owner.Handle);
1310                 }
1311
1312                 internal void AlignCaret ()
1313                 {
1314                         AlignCaret (true);
1315                 }
1316
1317                 internal void AlignCaret(bool changeCaretTag) {
1318                         if (!owner.IsHandleCreated) {
1319                                 return;
1320                         }
1321
1322                         if (changeCaretTag) {
1323                                 caret.tag = LineTag.FindTag (caret.line, caret.pos);
1324
1325                                 MoveCaretToTextTag ();
1326                         }
1327
1328                         // if the caret has had SelectionFont changed to a
1329                         // different height, we reflect changes unless the new
1330                         // font is larger than the line (line recalculations
1331                         // ignore empty tags) in which case we make it equal
1332                         // the line height and then when text is entered
1333                         if (caret.tag.Height > caret.tag.Line.Height) {
1334                                 caret.height = caret.line.height;
1335                         } else {
1336                                 caret.height = caret.tag.Height;
1337                         }
1338
1339                         if (owner.Focused) {
1340                                 XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
1341                                 XplatUI.SetCaretPos (owner.Handle, 
1342                                         offset_x + (int) caret.tag.Line.widths [caret.pos] + caret.line.X - viewport_x, 
1343                                         offset_y + caret.line.Y + viewport_y + caret_shift);
1344                                 DisplayCaret ();
1345                         }
1346
1347                         if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1348                 }
1349
1350                 internal void UpdateCaret() {
1351                         if (!owner.IsHandleCreated || caret.tag == null) {
1352                                 return;
1353                         }
1354
1355                         MoveCaretToTextTag ();
1356
1357                         if (caret.tag.Height != caret.height) {
1358                                 caret.height = caret.tag.Height;
1359                                 if (owner.Focused) {
1360                                         XplatUI.CreateCaret(owner.Handle, caret_width, caret.height);
1361                                 }
1362                         }
1363
1364                         if (owner.Focused) {
1365                                 XplatUI.SetCaretPos(owner.Handle, 
1366                                         offset_x + (int)caret.tag.Line.widths[caret.pos] + caret.line.X - viewport_x, 
1367                                         offset_y + caret.line.Y + caret.tag.Shift - viewport_y + caret_shift);
1368                                 DisplayCaret ();
1369                         }
1370                         
1371                         if (CaretMoved != null) CaretMoved(this, EventArgs.Empty);
1372                 }
1373
1374                 internal void DisplayCaret() {
1375                         if (!owner.IsHandleCreated) {
1376                                 return;
1377                         }
1378
1379                         if (owner.ShowSelection && (!selection_visible || owner.show_caret_w_selection)) {
1380                                 XplatUI.CaretVisible(owner.Handle, true);
1381                         }
1382                 }
1383
1384                 internal void HideCaret() {
1385                         if (!owner.IsHandleCreated) {
1386                                 return;
1387                         }
1388
1389                         if (owner.Focused) {
1390                                 XplatUI.CaretVisible(owner.Handle, false);
1391                         }
1392                 }
1393
1394                 
1395                 internal void MoveCaretToTextTag ()
1396                 {
1397                         if (caret.tag == null || caret.tag.IsTextTag)
1398                                 return;
1399
1400                         
1401
1402                         if (caret.pos < caret.tag.Start) {
1403                                 caret.tag = caret.tag.Previous;
1404                         } else {
1405                                 caret.tag = caret.tag.Next;
1406                         }
1407                 }
1408
1409                 internal void MoveCaret(CaretDirection direction) {
1410                         // FIXME should we use IsWordSeparator to detect whitespace, instead 
1411                         // of looking for actual spaces in the Word move cases?
1412
1413                         bool nowrap = false;
1414                         switch(direction) {
1415                                 case CaretDirection.CharForwardNoWrap:
1416                                         nowrap = true;
1417                                         goto case CaretDirection.CharForward;
1418                                 case CaretDirection.CharForward: {
1419                                         caret.pos++;
1420                                         if (caret.pos > caret.line.TextLengthWithoutEnding ()) {
1421                                                 if (!nowrap) {
1422                                                         // Go into next line
1423                                                         if (caret.line.line_no < this.lines) {
1424                                                                 caret.line = GetLine(caret.line.line_no+1);
1425                                                                 caret.pos = 0;
1426                                                                 caret.tag = caret.line.tags;
1427                                                         } else {
1428                                                                 caret.pos--;
1429                                                         }
1430                                                 } else {
1431                                                         // Single line; we stay where we are
1432                                                         caret.pos--;
1433                                                 }
1434                                         } else {
1435                                                 if ((caret.tag.Start - 1 + caret.tag.Length) < caret.pos) {
1436                                                         caret.tag = caret.tag.Next;
1437                                                 }
1438                                         }
1439                                         UpdateCaret();
1440                                         return;
1441                                 }
1442
1443                                 case CaretDirection.CharBackNoWrap:
1444                                         nowrap = true;
1445                                         goto case CaretDirection.CharBack;
1446                                 case CaretDirection.CharBack: {
1447                                         if (caret.pos > 0) {
1448                                                 // caret.pos--; // folded into the if below
1449                                                 
1450                                                 if (--caret.pos > 0) {
1451                                                         if (caret.tag.Start > caret.pos) {
1452                                                                 caret.tag = caret.tag.Previous;
1453                                                         }
1454                                                 }
1455                                         } else {
1456                                                 if (caret.line.line_no > 1 && !nowrap) {
1457                                                         caret.line = GetLine(caret.line.line_no - 1);
1458                                                         caret.pos = caret.line.TextLengthWithoutEnding ();
1459                                                         caret.tag = LineTag.FindTag(caret.line, caret.pos);
1460                                                 }
1461                                         }
1462                                         UpdateCaret();
1463                                         return;
1464                                 }
1465
1466                                 case CaretDirection.WordForward: {
1467                                         int len;
1468
1469                                         len = caret.line.text.Length;
1470                                         if (caret.pos < len) {
1471                                                 while ((caret.pos < len) && (caret.line.text[caret.pos] != ' ')) {
1472                                                         caret.pos++;
1473                                                 }
1474                                                 if (caret.pos < len) {
1475                                                         // Skip any whitespace
1476                                                         while ((caret.pos < len) && (caret.line.text[caret.pos] == ' ')) {
1477                                                                 caret.pos++;
1478                                                         }
1479                                                 }
1480                                                 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1481                                         } else {
1482                                                 if (caret.line.line_no < this.lines) {
1483                                                         caret.line = GetLine(caret.line.line_no + 1);
1484                                                         caret.pos = 0;
1485                                                         caret.tag = caret.line.tags;
1486                                                 }
1487                                         }
1488                                         UpdateCaret();
1489                                         return;
1490                                 }
1491
1492                                 case CaretDirection.WordBack: {
1493                                         if (caret.pos > 0) {
1494                                                 caret.pos--;
1495
1496                                                 while ((caret.pos > 0) && (caret.line.text[caret.pos] == ' ')) {
1497                                                         caret.pos--;
1498                                                 }
1499
1500                                                 while ((caret.pos > 0) && (caret.line.text[caret.pos] != ' ')) {
1501                                                         caret.pos--;
1502                                                 }
1503
1504                                                 if (caret.line.text.ToString(caret.pos, 1) == " ") {
1505                                                         if (caret.pos != 0) {
1506                                                                 caret.pos++;
1507                                                         } else {
1508                                                                 caret.line = GetLine(caret.line.line_no - 1);
1509                                                                 caret.pos = caret.line.text.Length;
1510                                                         }
1511                                                 }
1512                                                 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1513                                         } else {
1514                                                 if (caret.line.line_no > 1) {
1515                                                         caret.line = GetLine(caret.line.line_no - 1);
1516                                                         caret.pos = caret.line.text.Length;
1517                                                         caret.tag = LineTag.FindTag(caret.line, caret.pos);
1518                                                 }
1519                                         }
1520                                         UpdateCaret();
1521                                         return;
1522                                 }
1523
1524                                 case CaretDirection.LineUp: {
1525                                         if (caret.line.line_no > 1) {
1526                                                 int     pixel;
1527
1528                                                 pixel = (int)caret.line.widths[caret.pos];
1529                                                 PositionCaret(pixel, GetLine(caret.line.line_no - 1).Y);
1530
1531                                                 DisplayCaret ();
1532                                         }
1533                                         return;
1534                                 }
1535
1536                                 case CaretDirection.LineDown: {
1537                                         if (caret.line.line_no < lines) {
1538                                                 int     pixel;
1539
1540                                                 pixel = (int)caret.line.widths[caret.pos];
1541                                                 PositionCaret(pixel, GetLine(caret.line.line_no + 1).Y);
1542
1543                                                 DisplayCaret ();
1544                                         }
1545                                         return;
1546                                 }
1547
1548                                 case CaretDirection.Home: {
1549                                         if (caret.pos > 0) {
1550                                                 caret.pos = 0;
1551                                                 caret.tag = caret.line.tags;
1552                                                 UpdateCaret();
1553                                         }
1554                                         return;
1555                                 }
1556
1557                                 case CaretDirection.End: {
1558                                         if (caret.pos < caret.line.TextLengthWithoutEnding ()) {
1559                                                 caret.pos = caret.line.TextLengthWithoutEnding ();
1560                                                 caret.tag = LineTag.FindTag(caret.line, caret.pos);
1561                                                 UpdateCaret();
1562                                         }
1563                                         return;
1564                                 }
1565
1566                                 case CaretDirection.PgUp: {
1567
1568                                         if (caret.line.line_no == 1 && owner.richtext) {
1569                                                 owner.vscroll.Value = 0;
1570                                                 Line line = GetLine (1);
1571                                                 PositionCaret (line, 0);
1572                                         }
1573
1574                                         int y_offset = caret.line.Y + caret.line.height - 1 - viewport_y;
1575                                         int index;
1576                                         LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
1577                                                         viewport_y - viewport_height, out index);
1578
1579                                         owner.vscroll.Value = Math.Min (top.Line.Y, owner.vscroll.Maximum - viewport_height);
1580                                         PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
1581
1582                                         return;
1583                                 }
1584
1585                                 case CaretDirection.PgDn: {
1586
1587                                         if (caret.line.line_no == lines && owner.richtext) {
1588                                                 owner.vscroll.Value = owner.vscroll.Maximum - viewport_height + 1;
1589                                                 Line line = GetLine (lines);
1590                                                 PositionCaret (line, line.TextLengthWithoutEnding());
1591                                         }
1592
1593                                         int y_offset = caret.line.Y - viewport_y;
1594                                         int index;
1595                                         LineTag top = FindCursor ((int) caret.line.widths [caret.pos],
1596                                                         viewport_y + viewport_height, out index);
1597
1598                                         owner.vscroll.Value = Math.Min (top.Line.Y, owner.vscroll.Maximum - viewport_height);
1599                                         PositionCaret ((int) caret.line.widths [caret.pos], y_offset + viewport_y);
1600                                         
1601                                         return;
1602                                 }
1603
1604                                 case CaretDirection.CtrlPgUp: {
1605                                         PositionCaret(0, viewport_y);
1606                                         DisplayCaret ();
1607                                         return;
1608                                 }
1609
1610                                 case CaretDirection.CtrlPgDn: {
1611                                         Line    line;
1612                                         LineTag tag;
1613                                         int     index;
1614
1615                                         tag = FindCursor (0, viewport_y + viewport_height, out index);
1616                                         if (tag.Line.line_no > 1) {
1617                                                 line = GetLine(tag.Line.line_no - 1);
1618                                         } else {
1619                                                 line = tag.Line;
1620                                         }
1621                                         PositionCaret(line, line.Text.Length);
1622                                         DisplayCaret ();
1623                                         return;
1624                                 }
1625
1626                                 case CaretDirection.CtrlHome: {
1627                                         caret.line = GetLine(1);
1628                                         caret.pos = 0;
1629                                         caret.tag = caret.line.tags;
1630
1631                                         UpdateCaret();
1632                                         return;
1633                                 }
1634
1635                                 case CaretDirection.CtrlEnd: {
1636                                         caret.line = GetLine(lines);
1637                                         caret.pos = caret.line.TextLengthWithoutEnding ();
1638                                         caret.tag = LineTag.FindTag(caret.line, caret.pos);
1639
1640                                         UpdateCaret();
1641                                         return;
1642                                 }
1643
1644                                 case CaretDirection.SelectionStart: {
1645                                         caret.line = selection_start.line;
1646                                         caret.pos = selection_start.pos;
1647                                         caret.tag = selection_start.tag;
1648
1649                                         UpdateCaret();
1650                                         return;
1651                                 }
1652
1653                                 case CaretDirection.SelectionEnd: {
1654                                         caret.line = selection_end.line;
1655                                         caret.pos = selection_end.pos;
1656                                         caret.tag = selection_end.tag;
1657
1658                                         UpdateCaret();
1659                                         return;
1660                                 }
1661                         }
1662                 }
1663
1664                 internal void DumpDoc ()
1665                 {
1666                         Console.WriteLine ("<doc lines='{0}'>", lines);
1667                         for (int i = 1; i <= lines ; i++) {
1668                                 Line line = GetLine (i);
1669                                 Console.WriteLine ("<line no='{0}' ending='{1}'>", line.line_no, line.ending);
1670
1671                                 LineTag tag = line.tags;
1672                                 while (tag != null) {
1673                                         Console.Write ("\t<tag type='{0}' span='{1}->{2}' font='{3}' color='{4}'>",
1674                                                         tag.GetType (), tag.Start, tag.Length, tag.Font, tag.Color);
1675                                         Console.Write (tag.Text ());
1676                                         Console.WriteLine ("</tag>");
1677                                         tag = tag.Next;
1678                                 }
1679                                 Console.WriteLine ("</line>");
1680                         }
1681                         Console.WriteLine ("</doc>");
1682                 }
1683
1684                 internal void Draw (Graphics g, Rectangle clip)
1685                 {
1686                         Line line;              // Current line being drawn
1687                         LineTag tag;            // Current tag being drawn
1688                         int start;              // First line to draw
1689                         int end;                // Last line to draw
1690                         StringBuilder text;     // String representing the current line
1691                         int line_no;
1692                         Color tag_color;
1693                         Color current_color;
1694
1695                         // First, figure out from what line to what line we need to draw
1696
1697                         if (multiline) {
1698                                 start = GetLineByPixel(clip.Top + viewport_y - offset_y, false).line_no;
1699                                 end = GetLineByPixel(clip.Bottom + viewport_y - offset_y, false).line_no;
1700                         } else {
1701                                 start = GetLineByPixel(clip.Left + viewport_x - offset_x, false).line_no;
1702                                 end = GetLineByPixel(clip.Right + viewport_x - offset_x, false).line_no;
1703                         }
1704
1705                         // remove links in the list (used for mouse down events) that are within the clip area.
1706                         InvalidateLinks (clip);
1707
1708                         ///
1709                         /// We draw the single border ourself
1710                         ///
1711                         if (owner.actual_border_style == BorderStyle.FixedSingle) {
1712                                 ControlPaint.DrawBorder (g, owner.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
1713                         }
1714
1715                         /// Make sure that we aren't drawing one more line then we need to
1716                         line = GetLine (end - 1);
1717                         if (line != null && clip.Bottom == offset_y + line.Y + line.height - viewport_y)
1718                                 end--;
1719
1720                         line_no = start;
1721
1722                         #if Debug
1723                                 DateTime        n = DateTime.Now;
1724                                 Console.WriteLine ("Started drawing: {0}s {1}ms", n.Second, n.Millisecond);
1725                                 Console.WriteLine ("CLIP:  {0}", clip);
1726                                 Console.WriteLine ("S: {0}", GetLine (start).text);
1727                                 Console.WriteLine ("E: {0}", GetLine (end).text);
1728                         #endif
1729
1730                         // Non multiline selection can be handled outside of the loop
1731                         if (!multiline && selection_visible && owner.ShowSelection) {
1732                                 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
1733                                                 offset_x + selection_start.line.widths [selection_start.pos] +
1734                                                 selection_start.line.X - viewport_x, 
1735                                                 offset_y + selection_start.line.Y,
1736                                                 (selection_end.line.X + selection_end.line.widths [selection_end.pos]) -
1737                                                 (selection_start.line.X + selection_start.line.widths [selection_start.pos]), 
1738                                                 selection_start.line.height);
1739                         }
1740
1741                         while (line_no <= end) {
1742                                 line = GetLine (line_no);
1743                                 float line_y = line.Y - viewport_y + offset_y;
1744                                 
1745                                 tag = line.tags;
1746                                 if (!calc_pass) {
1747                                         text = line.text;
1748                                 } else {
1749                                         if (PasswordCache.Length < line.text.Length)
1750                                                 PasswordCache.Append(Char.Parse(password_char), line.text.Length - PasswordCache.Length);
1751                                         else if (PasswordCache.Length > line.text.Length)
1752                                                 PasswordCache.Remove(line.text.Length, PasswordCache.Length - line.text.Length);
1753                                         text = PasswordCache;
1754                                 }
1755
1756                                 int line_selection_start = text.Length + 1;
1757                                 int line_selection_end = text.Length + 1;
1758                                 if (selection_visible && owner.ShowSelection &&
1759                                                 (line_no >= selection_start.line.line_no) &&
1760                                                 (line_no <= selection_end.line.line_no)) {
1761
1762                                         if (line_no == selection_start.line.line_no)
1763                                                 line_selection_start = selection_start.pos + 1;
1764                                         else
1765                                                 line_selection_start = 1;
1766
1767                                         if (line_no == selection_end.line.line_no)
1768                                                 line_selection_end = selection_end.pos + 1;
1769                                         else
1770                                                 line_selection_end = text.Length + 1;
1771
1772                                         if (line_selection_end == line_selection_start) {
1773                                                 // There isn't really selection
1774                                                 line_selection_start = text.Length + 1;
1775                                                 line_selection_end = line_selection_start;
1776                                         } else if (multiline) {
1777                                                 // lets draw some selection baby!!  (non multiline selection is drawn outside the loop)
1778                                                 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorHighlight),
1779                                                                 offset_x + line.widths [line_selection_start - 1] + line.X - viewport_x, 
1780                                                                 line_y, line.widths [line_selection_end - 1] - line.widths [line_selection_start - 1], 
1781                                                                 line.height);
1782                                         }
1783                                 }
1784
1785                                 current_color = line.tags.ColorToDisplay;
1786                                 while (tag != null) {
1787
1788                                         // Skip empty tags
1789                                         if (tag.Length == 0) {
1790                                                 tag = tag.Next;
1791                                                 continue;
1792                                         }
1793
1794                                         if (((tag.X + tag.Width) < (clip.Left - viewport_x - offset_x)) && 
1795                                              (tag.X > (clip.Right - viewport_x - offset_x))) {
1796                                                 tag = tag.Next;
1797                                                 continue;
1798                                         }
1799
1800                                         if (tag.BackColor != Color.Empty) {
1801                                                 g.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (tag.BackColor), 
1802                                                                 offset_x + tag.X + line.X - viewport_x,
1803                                                                 line_y + tag.Shift, tag.Width, line.height);
1804                                         }
1805
1806                                         tag_color = tag.ColorToDisplay;
1807                                         current_color = tag_color;
1808
1809                                         if (!owner.Enabled) {
1810                                                 Color a = tag.Color;
1811                                                 Color b = ThemeEngine.Current.ColorWindowText;
1812
1813                                                 if ((a.R == b.R) && (a.G == b.G) && (a.B == b.B))
1814                                                         tag_color = ThemeEngine.Current.ColorGrayText;
1815
1816                                         } 
1817
1818                                         int tag_pos = tag.Start;
1819                                         current_color = tag_color;
1820                                         while (tag_pos < tag.Start + tag.Length) {
1821                                                 int old_tag_pos = tag_pos;
1822
1823                                                 if (tag_pos >= line_selection_start && tag_pos < line_selection_end) {
1824                                                         current_color = ThemeEngine.Current.ColorHighlightText;
1825                                                         tag_pos = Math.Min (tag.End, line_selection_end);
1826                                                 } else if (tag_pos < line_selection_start) {
1827                                                         current_color = tag_color;
1828                                                         tag_pos = Math.Min (tag.End, line_selection_start);
1829                                                 } else {
1830                                                         current_color = tag_color;
1831                                                         tag_pos = tag.End;
1832                                                 }
1833
1834                                                 Rectangle text_size;
1835
1836                                                 tag.Draw (g, current_color,
1837                                                                 offset_x + line.X - viewport_x,
1838                                                                 line_y + tag.Shift,
1839                                                                 old_tag_pos - 1, Math.Min (tag.Start + tag.Length, tag_pos) - 1,
1840                                                                 text.ToString (), out text_size, tag.IsLink);
1841
1842                                                 if (tag.IsLink) {
1843                                                         TextBoxBase.LinkRectangle link = new TextBoxBase.LinkRectangle (text_size);
1844                                                         link.LinkTag = tag;
1845                                                         owner.list_links.Add (link);
1846                                                 }
1847                                         }
1848                                         tag = tag.Next;
1849                                 }
1850
1851                                 line.DrawEnding (g, line_y);
1852                                 line_no++;
1853                         }
1854                 }
1855
1856                 private int GetLineEnding (string line, int start, out LineEnding ending)
1857                 {
1858                         int res;
1859                         int rich_index;
1860
1861                         if (start >= line.Length) {
1862                                 ending = LineEnding.Wrap;
1863                                 return -1;
1864                         }
1865                         
1866                         res = line.IndexOf ('\r', start);
1867                         rich_index = line.IndexOf ('\n', start);
1868                         
1869                         // Handle the case where we find both of them, and the \n is before the \r
1870                         if (res != -1 && rich_index != -1)
1871                                 if (rich_index < res) {
1872                                         ending = LineEnding.Rich;
1873                                         return rich_index;                              
1874                                 }
1875                         
1876                         if (res != -1) {
1877                                 if (res + 2 < line.Length && line [res + 1] == '\r' && line [res + 2] == '\n') {
1878                                         ending = LineEnding.Soft;
1879                                         return res;
1880                                 }
1881                                 if (res + 1 < line.Length && line [res + 1] == '\n') {
1882                                         ending = LineEnding.Hard;
1883                                         return res;
1884                                 }
1885                                 ending = LineEnding.Limp;
1886                                 return res;
1887                         }
1888
1889                         if (rich_index != -1) {
1890                                 ending = LineEnding.Rich;
1891                                 return rich_index;
1892                         }
1893
1894                         ending = LineEnding.Wrap;
1895                         return line.Length;
1896                 }
1897
1898                 // Get the line ending, but only of the types specified
1899                 private int GetLineEnding (string line, int start, out LineEnding ending, LineEnding type)
1900                 {
1901                         int index = start;
1902                         int last_length = 0;
1903
1904                         do {
1905                                 index = GetLineEnding (line, index + last_length, out ending);
1906                                 last_length = LineEndingLength (ending);
1907                         } while 
1908                                 ((ending & type) != ending && index != -1);
1909                         
1910                         return index == -1 ? line.Length : index;
1911                 }
1912                 
1913                 internal int LineEndingLength (LineEnding ending)
1914                 {
1915                         switch (ending) {
1916                                 case LineEnding.Limp:
1917                                 case LineEnding.Rich:
1918                                         return 1;
1919                                 case LineEnding.Hard:
1920                                         return 2;
1921                                 case LineEnding.Soft:
1922                                         return 3;
1923                         }
1924
1925                         return 0;
1926                 }
1927
1928                 internal string LineEndingToString (LineEnding ending)
1929                 {
1930                         switch (ending) {
1931                                 case LineEnding.Limp:
1932                                         return "\r";
1933                                 case LineEnding.Hard:
1934                                         return "\r\n";
1935                                 case LineEnding.Soft:
1936                                         return "\r\r\n";
1937                                 case LineEnding.Rich:
1938                                         return "\n";
1939                         }
1940                         
1941                         return string.Empty;
1942                 }
1943
1944                 internal void Insert (Line line, int pos, bool update_caret, string s)
1945                 {
1946                         Insert (line, pos, update_caret, s, line.FindTag (pos));
1947                 }
1948
1949                 // Insert text at the given position; use formatting at insertion point for inserted text
1950                 internal void Insert (Line line, int pos, bool update_caret, string s, LineTag tag)
1951                 {
1952                         int break_index;
1953                         int base_line;
1954                         int old_line_count;
1955                         int count = 1;
1956                         LineEnding ending;
1957                         Line split_line;
1958                         
1959                         // Don't recalculate while we mess around
1960                         SuspendRecalc ();
1961                         
1962                         base_line = line.line_no;
1963                         old_line_count = lines;
1964
1965                         break_index = GetLineEnding (s, 0, out ending, LineEnding.Hard | LineEnding.Rich);
1966
1967                         // There are no line feeds in our text to be pasted
1968                         if (break_index == s.Length) {
1969                                 line.InsertString (pos, s, tag);
1970                         } else {
1971                                 // Add up to the first line feed to our current position
1972                                 line.InsertString (pos, s.Substring (0, break_index + LineEndingLength (ending)), tag);
1973                                 
1974                                 // Split the rest of the original line to a new line
1975                                 Split (line, pos + (break_index + LineEndingLength (ending)));
1976                                 line.ending = ending;
1977                                 break_index += LineEndingLength (ending);
1978                                 split_line = GetLine (line.line_no + 1);
1979                                 
1980                                 // Insert brand new lines for any more line feeds in the inserted string
1981                                 while (true) {
1982                                         int next_break = GetLineEnding (s, break_index, out ending, LineEnding.Hard | LineEnding.Rich);
1983                                         
1984                                         if (next_break == s.Length)
1985                                                 break;
1986                                                 
1987                                         string line_text = s.Substring (break_index, next_break - break_index +
1988                                                         LineEndingLength (ending));
1989
1990                                         Add (base_line + count, line_text, line.alignment, tag.Font, tag.Color, ending);
1991
1992                                         Line last = GetLine (base_line + count);
1993                                         last.ending = ending;
1994
1995                                         count++;
1996                                         break_index = next_break + LineEndingLength (ending);
1997                                 }
1998
1999                                 // Add the remainder of the insert text to the split
2000                                 // part of the original line
2001                                 split_line.InsertString (0, s.Substring (break_index));
2002                         }
2003                         
2004                         // Allow the document to recalculate things
2005                         ResumeRecalc (false);
2006
2007                         // Update our character count
2008                         CharCount += s.Length;
2009
2010                         UpdateView (line, lines - old_line_count + 1, pos);
2011
2012                         // Move the caret to the end of the inserted text if requested
2013                         if (update_caret) {
2014                                 Line l = GetLine (line.line_no + lines - old_line_count);
2015                                 PositionCaret (l, l.text.Length);
2016                                 DisplayCaret ();
2017                         }
2018                 }
2019
2020                 // Inserts a string at the given position
2021                 internal void InsertString (Line line, int pos, string s)
2022                 {
2023                         // Update our character count
2024                         CharCount += s.Length;
2025
2026                         // Insert the text into the Line
2027                         line.InsertString (pos, s);
2028                 }
2029
2030                 // Inserts a character at the current caret position
2031                 internal void InsertCharAtCaret (char ch, bool move_caret)
2032                 {
2033                         caret.line.InsertString (caret.pos, ch.ToString(), caret.tag);
2034
2035                         // Update our character count
2036                         CharCount++;
2037                         
2038                         undo.RecordTyping (caret.line, caret.pos, ch);
2039
2040                         UpdateView (caret.line, caret.pos);
2041                         
2042                         if (move_caret) {
2043                                 caret.pos++;
2044                                 UpdateCaret ();
2045                                 SetSelectionToCaret (true);
2046                         }
2047                 }
2048                 
2049                 internal void InsertPicture (Line line, int pos, RTF.Picture picture)
2050                 {
2051                         //LineTag next_tag;
2052                         LineTag tag;
2053                         int len;
2054
2055                         len = 1;
2056
2057                         // Just a place holder basically
2058                         line.text.Insert (pos, "I");
2059
2060                         PictureTag picture_tag = new PictureTag (line, pos + 1, picture);
2061
2062                         tag = LineTag.FindTag (line, pos);
2063                         picture_tag.CopyFormattingFrom (tag);
2064                         /*next_tag = */tag.Break (pos + 1);
2065                         picture_tag.Previous = tag;
2066                         picture_tag.Next = tag.Next;
2067                         tag.Next = picture_tag;
2068
2069                         //
2070                         // Picture tags need to be surrounded by text tags
2071                         //
2072                         if (picture_tag.Next == null) {
2073                                 picture_tag.Next = new LineTag (line, pos + 1);
2074                                 picture_tag.Next.CopyFormattingFrom (tag);
2075                                 picture_tag.Next.Previous = picture_tag;
2076                         }
2077
2078                         tag = picture_tag.Next;
2079                         while (tag != null) {
2080                                 tag.Start += len;
2081                                 tag = tag.Next;
2082                         }
2083
2084                         line.Grow (len);
2085                         line.recalc = true;
2086
2087                         UpdateView (line, pos);
2088                 }
2089
2090                 internal void DeleteMultiline (Line start_line, int pos, int length)
2091                 {
2092                         Marker start = new Marker ();
2093                         Marker end = new Marker ();
2094                         int start_index = LineTagToCharIndex (start_line, pos);
2095
2096                         start.line = start_line;
2097                         start.pos = pos;
2098                         start.tag = LineTag.FindTag (start_line, pos);
2099
2100                         CharIndexToLineTag (start_index + length, out end.line,
2101                                         out end.tag, out end.pos);
2102
2103                         SuspendUpdate ();
2104
2105                         if (start.line == end.line) {
2106                                 DeleteChars (start.line, pos, end.pos - pos);
2107                         } else {
2108
2109                                 // Delete first and last lines
2110                                 DeleteChars (start.line, start.pos, start.line.text.Length - start.pos);
2111                                 DeleteChars (end.line, 0, end.pos);
2112
2113                                 int current = start.line.line_no + 1;
2114                                 if (current < end.line.line_no) {
2115                                         for (int i = end.line.line_no - 1; i >= current; i--) {
2116                                                 Delete (i);
2117                                         }
2118                                 }
2119
2120                                 // BIG FAT WARNING - selection_end.line might be stale due 
2121                                 // to the above Delete() call. DONT USE IT before hitting the end of this method!
2122
2123                                 // Join start and end
2124                                 Combine (start.line.line_no, current);
2125                         }
2126
2127                         ResumeUpdate (true);
2128                 }
2129
2130                 
2131                 // Deletes n characters at the given position; it will not delete past line limits
2132                 // pos is 0-based
2133                 public void DeleteChars (Line line, int pos, int count)
2134                 {
2135                         // Reduce our character count
2136                         CharCount -= count;
2137                         
2138                         line.DeleteCharacters (pos, count);
2139
2140                         if (pos >= line.TextLengthWithoutEnding ()) {
2141                                 LineEnding ending = line.ending;
2142                                 GetLineEnding (line.text.ToString (), 0, out ending);
2143                                 
2144                                 if (ending != line.ending) {
2145                                         line.ending = ending;
2146
2147                                         if (!multiline) {
2148                                                 UpdateView (line, lines, pos);
2149                                                 owner.Invalidate ();
2150                                                 return;
2151                                         }
2152                                 }
2153                         }
2154                         if (!multiline) {
2155                                 UpdateView (line, lines, pos);
2156                                 owner.Invalidate ();
2157                         } else 
2158                                 UpdateView (line, pos);
2159                 }
2160
2161                 // Deletes a character at or after the given position (depending on forward); it will not delete past line limits
2162                 public void DeleteChar (Line line, int pos, bool forward)
2163                 {
2164                         if ((pos == 0 && forward == false) || (pos == line.text.Length && forward == true))
2165                                 return;
2166                         
2167                         if (forward)
2168                                 DeleteChars (line, pos, 1);
2169                         else
2170                                 DeleteChars (line, pos - 1, 1);
2171                 }
2172
2173                 // Combine two lines
2174                 internal void Combine(int FirstLine, int SecondLine) {
2175                         Combine(GetLine(FirstLine), GetLine(SecondLine));
2176                 }
2177
2178                 internal void Combine(Line first, Line second) {
2179                         LineTag last;
2180                         int     shift;
2181
2182                         // strip the ending off of the first lines text
2183                         first.text.Length = first.text.Length - LineEndingLength (first.ending);
2184
2185                         // Combine the two tag chains into one
2186                         last = first.tags;
2187
2188                         // Maintain the line ending style
2189                         first.ending = second.ending;
2190
2191                         while (last.Next != null) {
2192                                 last = last.Next;
2193                         }
2194
2195                         // need to get the shift before setting the next tag since that effects length
2196                         shift = last.Start + last.Length - 1;
2197                         last.Next = second.tags;
2198                         last.Next.Previous = last;
2199
2200                         // Fix up references within the chain
2201                         last = last.Next;
2202                         while (last != null) {
2203                                 last.Line = first;
2204                                 last.Start += shift;
2205                                 last = last.Next;
2206                         }
2207
2208                         // Combine both lines' strings
2209                         first.text.Insert(first.text.Length, second.text.ToString());
2210                         first.Grow(first.text.Length);
2211
2212                         // Remove the reference to our (now combined) tags from the doomed line
2213                         second.tags = null;
2214
2215                         // Renumber lines
2216                         DecrementLines(first.line_no + 2);      // first.line_no + 1 will be deleted, so we need to start renumbering one later
2217
2218                         // Mop up
2219                         first.recalc = true;
2220                         first.height = 0;       // This forces RecalcDocument/UpdateView to redraw from this line on
2221                         first.Streamline(lines);
2222
2223                         // Update Caret, Selection, etc
2224                         if (caret.line == second) {
2225                                 caret.Combine(first, shift);
2226                         }
2227                         if (selection_anchor.line == second) {
2228                                 selection_anchor.Combine(first, shift);
2229                         }
2230                         if (selection_start.line == second) {
2231                                 selection_start.Combine(first, shift);
2232                         }
2233                         if (selection_end.line == second) {
2234                                 selection_end.Combine(first, shift);
2235                         }
2236
2237                         #if Debug
2238                                 Line    check_first;
2239                                 Line    check_second;
2240
2241                                 check_first = GetLine(first.line_no);
2242                                 check_second = GetLine(check_first.line_no + 1);
2243
2244                                 Console.WriteLine("Pre-delete: Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
2245                         #endif
2246
2247                         this.Delete(second);
2248
2249                         #if Debug
2250                                 check_first = GetLine(first.line_no);
2251                                 check_second = GetLine(check_first.line_no + 1);
2252
2253                                 Console.WriteLine("Post-delete Y of first line: {0}, second line: {1}", check_first.Y, check_second.Y);
2254                         #endif
2255                 }
2256
2257                 // Split the line at the position into two
2258                 internal void Split(int LineNo, int pos) {
2259                         Line    line;
2260                         LineTag tag;
2261
2262                         line = GetLine(LineNo);
2263                         tag = LineTag.FindTag(line, pos);
2264                         Split(line, tag, pos);
2265                 }
2266
2267                 internal void Split(Line line, int pos) {
2268                         LineTag tag;
2269
2270                         tag = LineTag.FindTag(line, pos);
2271                         Split(line, tag, pos);
2272                 }
2273
2274                 ///<summary>Split line at given tag and position into two lines</summary>
2275                 ///if more space becomes available on previous line
2276                 internal void Split(Line line, LineTag tag, int pos) {
2277                         LineTag new_tag;
2278                         Line    new_line;
2279                         bool    move_caret;
2280                         bool    move_sel_start;
2281                         bool    move_sel_end;
2282
2283                         move_caret = false;
2284                         move_sel_start = false;
2285                         move_sel_end = false;
2286
2287 #if DEBUG
2288                         SanityCheck();
2289
2290                         if (tag.End < pos)
2291                                 throw new Exception ("Split called with the wrong tag");
2292 #endif
2293
2294                         // Adjust selection and cursors
2295                         if (caret.line == line && caret.pos >= pos) {
2296                                 move_caret = true;
2297                         }
2298                         if (selection_start.line == line && selection_start.pos > pos) {
2299                                 move_sel_start = true;
2300                         }
2301
2302                         if (selection_end.line == line && selection_end.pos > pos) {
2303                                 move_sel_end = true;
2304                         }
2305
2306                         // cover the easy case first
2307                         if (pos == line.text.Length) {
2308                                 Add (line.line_no + 1, String.Empty, line.alignment, tag.Font, tag.Color, line.ending);
2309
2310                                 new_line = GetLine (line.line_no + 1);
2311                                 
2312                                 if (move_caret) {
2313                                         caret.line = new_line;
2314                                         caret.tag = new_line.tags;
2315                                         caret.pos = 0;
2316
2317                                         if (selection_visible == false) {
2318                                                 SetSelectionToCaret (true);
2319                                         }
2320                                 }
2321
2322                                 if (move_sel_start) {
2323                                         selection_start.line = new_line;
2324                                         selection_start.pos = 0;
2325                                         selection_start.tag = new_line.tags;
2326                                 }
2327
2328                                 if (move_sel_end) {
2329                                         selection_end.line = new_line;
2330                                         selection_end.pos = 0;
2331                                         selection_end.tag = new_line.tags;
2332                                 }
2333
2334 #if DEBUG
2335                                 SanityCheck ();
2336 #endif
2337                                 return;
2338                         }
2339
2340                         // We need to move the rest of the text into the new line
2341                         Add (line.line_no + 1, line.text.ToString (pos, line.text.Length - pos), line.alignment, tag.Font, tag.Color, line.ending);
2342
2343                         // Now transfer our tags from this line to the next
2344                         new_line = GetLine(line.line_no + 1);
2345
2346                         line.recalc = true;
2347                         new_line.recalc = true;
2348
2349                         //make sure that if we are at the end of a tag, we start on the begining
2350                         //of a new one, if one exists... Stops us creating an empty tag and
2351                         //make the operation easier.
2352                         if (tag.Next != null && (tag.Next.Start - 1) == pos)
2353                                 tag = tag.Next;
2354
2355                         if ((tag.Start - 1) == pos) {
2356                                 int     shift;
2357
2358                                 // We can simply break the chain and move the tag into the next line
2359
2360                                 // if the tag we are moving is the first, create an empty tag
2361                                 // for the line we are leaving behind
2362                                 if (tag == line.tags) {
2363                                         new_tag = new LineTag(line, 1);
2364                                         new_tag.CopyFormattingFrom (tag);
2365                                         line.tags = new_tag;
2366                                 }
2367
2368                                 if (tag.Previous != null) {
2369                                         tag.Previous.Next = null;
2370                                 }
2371                                 new_line.tags = tag;
2372                                 tag.Previous = null;
2373                                 tag.Line = new_line;
2374
2375                                 // Walk the list and correct the start location of the tags we just bumped into the next line
2376                                 shift = tag.Start - 1;
2377
2378                                 new_tag = tag;
2379                                 while (new_tag != null) {
2380                                         new_tag.Start -= shift;
2381                                         new_tag.Line = new_line;
2382                                         new_tag = new_tag.Next;
2383                                 }
2384                         } else {
2385                                 int     shift;
2386
2387                                 new_tag = new LineTag (new_line, 1);                    
2388                                 new_tag.Next = tag.Next;
2389                                 new_tag.CopyFormattingFrom (tag);
2390                                 new_line.tags = new_tag;
2391                                 if (new_tag.Next != null) {
2392                                         new_tag.Next.Previous = new_tag;
2393                                 }
2394                                 tag.Next = null;
2395
2396                                 shift = pos;
2397                                 new_tag = new_tag.Next;
2398                                 while (new_tag != null) {
2399                                         new_tag.Start -= shift;
2400                                         new_tag.Line = new_line;
2401                                         new_tag = new_tag.Next;
2402
2403                                 }
2404                         }
2405
2406                         if (move_caret) {
2407                                 caret.line = new_line;
2408                                 caret.pos = caret.pos - pos;
2409                                 caret.tag = caret.line.FindTag(caret.pos);
2410
2411                                 if (selection_visible == false) {
2412                                         SetSelectionToCaret (true);
2413                                 }
2414                         }
2415
2416                         if (move_sel_start) {
2417                                 selection_start.line = new_line;
2418                                 selection_start.pos = selection_start.pos - pos;
2419                                 if  (selection_start.Equals(selection_end))
2420                                         selection_start.tag = new_line.FindTag(selection_start.pos);
2421                                 else
2422                                         selection_start.tag = new_line.FindTag (selection_start.pos + 1);
2423                         }
2424
2425                         if (move_sel_end) {
2426                                 selection_end.line = new_line;
2427                                 selection_end.pos = selection_end.pos - pos;
2428                                 selection_end.tag = new_line.FindTag(selection_end.pos);
2429                         }
2430
2431                         CharCount -= line.text.Length - pos;
2432                         line.text.Remove(pos, line.text.Length - pos);
2433 #if DEBUG
2434                         SanityCheck ();
2435 #endif
2436                 }
2437
2438 #if DEBUG
2439                 private void SanityCheck () {
2440                         for (int i = 1; i < lines; i++) {
2441                                 LineTag tag = GetLine (i).tags;
2442
2443                                 if (tag.Start != 1)
2444                                         throw new Exception ("Line doesn't start at the begining");
2445
2446                                 int start = 1;
2447                                 tag = tag.Next;
2448
2449                                 while (tag != null) {
2450                                         if (tag.Start == start)
2451                                                 throw new Exception ("Empty tag!");
2452
2453                                         if (tag.Start < start)
2454                                                 throw new Exception ("Insane!!");
2455
2456                                         start = tag.Start;
2457                                         tag = tag.Next;
2458                                 }
2459                         }
2460                 }
2461 #endif
2462
2463                 // Adds a line of text, with given font.
2464                 // Bumps any line at that line number that already exists down
2465                 internal void Add (int LineNo, string Text, Font font, Color color, LineEnding ending)
2466                 {
2467                         Add (LineNo, Text, alignment, font, color, ending);
2468                 }
2469
2470                 internal void Add (int LineNo, string Text, HorizontalAlignment align, Font font, Color color, LineEnding ending)
2471                 {
2472                         Line    add;
2473                         Line    line;
2474                         int     line_no;
2475
2476                         CharCount += Text.Length;
2477
2478                         if (LineNo<1 || Text == null) {
2479                                 if (LineNo<1) {
2480                                         throw new ArgumentNullException("LineNo", "Line numbers must be positive");
2481                                 } else {
2482                                         throw new ArgumentNullException("Text", "Cannot insert NULL line");
2483                                 }
2484                         }
2485
2486                         add = new Line (this, LineNo, Text, align, font, color, ending);
2487
2488                         line = document;
2489                         while (line != sentinel) {
2490                                 add.parent = line;
2491                                 line_no = line.line_no;
2492
2493                                 if (LineNo > line_no) {
2494                                         line = line.right;
2495                                 } else if (LineNo < line_no) {
2496                                         line = line.left;
2497                                 } else {
2498                                         // Bump existing line numbers; walk all nodes to the right of this one and increment line_no
2499                                         IncrementLines(line.line_no);
2500                                         line = line.left;
2501                                 }
2502                         }
2503
2504                         add.left = sentinel;
2505                         add.right = sentinel;
2506
2507                         if (add.parent != null) {
2508                                 if (LineNo > add.parent.line_no) {
2509                                         add.parent.right = add;
2510                                 } else {
2511                                         add.parent.left = add;
2512                                 }
2513                         } else {
2514                                 // Root node
2515                                 document = add;
2516                         }
2517
2518                         RebalanceAfterAdd(add);
2519
2520                         lines++;
2521                 }
2522
2523                 internal virtual void Clear() {
2524                         lines = 0;
2525                         CharCount = 0;
2526                         document = sentinel;
2527                 }
2528
2529                 public virtual object Clone() {
2530                         Document clone;
2531
2532                         clone = new Document(null);
2533
2534                         clone.lines = this.lines;
2535                         clone.document = (Line)document.Clone();
2536
2537                         return clone;
2538                 }
2539
2540                 private void Delete (int LineNo)
2541                 {
2542                         Line    line;
2543
2544                         if (LineNo > lines)
2545                                 return;
2546
2547                         line = GetLine (LineNo);
2548
2549                         CharCount -= line.text.Length;
2550
2551                         DecrementLines (LineNo + 1);
2552                         Delete (line);
2553                 }
2554
2555                 private void Delete(Line line1) {
2556                         Line    line2;// = new Line();
2557                         Line    line3;
2558
2559                         if ((line1.left == sentinel) || (line1.right == sentinel)) {
2560                                 line3 = line1;
2561                         } else {
2562                                 line3 = line1.right;
2563                                 while (line3.left != sentinel) {
2564                                         line3 = line3.left;
2565                                 }
2566                         }
2567
2568                         if (line3.left != sentinel) {
2569                                 line2 = line3.left;
2570                         } else {
2571                                 line2 = line3.right;
2572                         }
2573
2574                         line2.parent = line3.parent;
2575                         if (line3.parent != null) {
2576                                 if(line3 == line3.parent.left) {
2577                                         line3.parent.left = line2;
2578                                 } else {
2579                                         line3.parent.right = line2;
2580                                 }
2581                         } else {
2582                                 document = line2;
2583                         }
2584
2585                         if (line3 != line1) {
2586                                 LineTag tag;
2587
2588                                 if (selection_start.line == line3) {
2589                                         selection_start.line = line1;
2590                                 }
2591
2592                                 if (selection_end.line == line3) {
2593                                         selection_end.line = line1;
2594                                 }
2595
2596                                 if (selection_anchor.line == line3) {
2597                                         selection_anchor.line = line1;
2598                                 }
2599
2600                                 if (caret.line == line3) {
2601                                         caret.line = line1;
2602                                 }
2603
2604
2605                                 line1.alignment = line3.alignment;
2606                                 line1.ascent = line3.ascent;
2607                                 line1.hanging_indent = line3.hanging_indent;
2608                                 line1.height = line3.height;
2609                                 line1.indent = line3.indent;
2610                                 line1.line_no = line3.line_no;
2611                                 line1.recalc = line3.recalc;
2612                                 line1.right_indent = line3.right_indent;
2613                                 line1.ending = line3.ending;
2614                                 line1.space = line3.space;
2615                                 line1.tags = line3.tags;
2616                                 line1.text = line3.text;
2617                                 line1.widths = line3.widths;
2618                                 line1.offset = line3.offset;
2619
2620                                 tag = line1.tags;
2621                                 while (tag != null) {
2622                                         tag.Line = line1;
2623                                         tag = tag.Next;
2624                                 }
2625                         }
2626
2627                         if (line3.color == LineColor.Black)
2628                                 RebalanceAfterDelete(line2);
2629
2630                         this.lines--;
2631                 }
2632
2633                 // Invalidates the start line until the end of the viewstate
2634                 internal void InvalidateLinesAfter (Line start) {
2635                         owner.Invalidate (new Rectangle (0, start.Y - viewport_y, viewport_width, viewport_height - start.Y));
2636                 }
2637
2638                 // Invalidate a section of the document to trigger redraw
2639                 internal void Invalidate(Line start, int start_pos, Line end, int end_pos) {
2640                         Line    l1;
2641                         Line    l2;
2642                         int     p1;
2643                         int     p2;
2644
2645                         if ((start == end) && (start_pos == end_pos)) {
2646                                 return;
2647                         }
2648
2649                         if (end_pos == -1) {
2650                                 end_pos = end.text.Length;
2651                         }
2652         
2653                         // figure out what's before what so the logic below is straightforward
2654                         if (start.line_no < end.line_no) {
2655                                 l1 = start;
2656                                 p1 = start_pos;
2657
2658                                 l2 = end;
2659                                 p2 = end_pos;
2660                         } else if (start.line_no > end.line_no) {
2661                                 l1 = end;
2662                                 p1 = end_pos;
2663
2664                                 l2 = start;
2665                                 p2 = start_pos;
2666                         } else {
2667                                 if (start_pos < end_pos) {
2668                                         l1 = start;
2669                                         p1 = start_pos;
2670
2671                                         l2 = end;
2672                                         p2 = end_pos;
2673                                 } else {
2674                                         l1 = end;
2675                                         p1 = end_pos;
2676
2677                                         l2 = start;
2678                                         p2 = start_pos;
2679                                 }
2680
2681                                 int endpoint = (int) l1.widths [p2];
2682                                 if (p2 == l1.text.Length + 1) {
2683                                         endpoint = (int) viewport_width;
2684                                 }
2685
2686                                 #if Debug
2687                                         Console.WriteLine("Invaliding backwards from {0}:{1} to {2}:{3}   {4}",
2688                                                         l1.line_no, p1, l2.line_no, p2,
2689                                                         new Rectangle(
2690                                                                 (int)l1.widths[p1] + l1.X - viewport_x, 
2691                                                                 l1.Y - viewport_y, 
2692                                                                 (int)l1.widths[p2], 
2693                                                                 l1.height
2694                                                                 )
2695                                                 );
2696                                 #endif
2697
2698                                 owner.Invalidate(new Rectangle (
2699                                         offset_x + (int)l1.widths[p1] + l1.X - viewport_x, 
2700                                         offset_y + l1.Y - viewport_y,
2701                                         endpoint - (int) l1.widths [p1] + 1, 
2702                                         l1.height));
2703                                 return;
2704                         }
2705
2706                         #if Debug
2707                                 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.X - viewport_x, l1.Y - viewport_y, viewport_width, l1.height);
2708                                 Console.WriteLine ("invalidate start line:  {0}  position:  {1}", l1.text, p1);
2709                         #endif
2710
2711                         // Three invalidates:
2712                         // First line from start
2713                         owner.Invalidate(new Rectangle(
2714                                 offset_x + (int)l1.widths[p1] + l1.X - viewport_x, 
2715                                 offset_y + l1.Y - viewport_y, 
2716                                 viewport_width, 
2717                                 l1.height));
2718
2719                         
2720                         // lines inbetween
2721                         if ((l1.line_no + 1) < l2.line_no) {
2722                                 int     y;
2723
2724                                 y = GetLine(l1.line_no + 1).Y;
2725                                 owner.Invalidate(new Rectangle(
2726                                         offset_x, 
2727                                         offset_y + y - viewport_y, 
2728                                         viewport_width, 
2729                                         l2.Y - y));
2730
2731                                 #if Debug
2732                                         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, l2.Y - y);
2733                                 #endif
2734                         }
2735                         
2736
2737                         // Last line to end
2738                         owner.Invalidate(new Rectangle(
2739                                 offset_x + (int)l2.widths[0] + l2.X - viewport_x, 
2740                                 offset_y + l2.Y - viewport_y, 
2741                                 (int)l2.widths[p2] + 1, 
2742                                 l2.height));
2743
2744                         #if Debug
2745                                 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.X - viewport_x, l2.Y - viewport_y, (int)l2.widths[p2] + 1, l2.height);
2746                         #endif
2747                 }
2748
2749                 /// <summary>Select text around caret</summary>
2750                 internal void ExpandSelection(CaretSelection mode, bool to_caret) {
2751                         if (to_caret) {
2752                                 // We're expanding the selection to the caret position
2753                                 switch(mode) {
2754                                         case CaretSelection.Line: {
2755                                                 // Invalidate the selection delta
2756                                                 if (caret > selection_prev) {
2757                                                         Invalidate(selection_prev.line, 0, caret.line, caret.line.text.Length);
2758                                                 } else {
2759                                                         Invalidate(selection_prev.line, selection_prev.line.text.Length, caret.line, 0);
2760                                                 }
2761
2762                                                 if (caret.line.line_no <= selection_anchor.line.line_no) {
2763                                                         selection_start.line = caret.line;
2764                                                         selection_start.tag = caret.line.tags;
2765                                                         selection_start.pos = 0;
2766
2767                                                         selection_end.line = selection_anchor.line;
2768                                                         selection_end.tag = selection_anchor.tag;
2769                                                         selection_end.pos = selection_anchor.pos;
2770
2771                                                         selection_end_anchor = true;
2772                                                 } else {
2773                                                         selection_start.line = selection_anchor.line;
2774                                                         selection_start.pos = selection_anchor.height;
2775                                                         selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1);
2776
2777                                                         selection_end.line = caret.line;
2778                                                         selection_end.tag = caret.line.tags;
2779                                                         selection_end.pos = caret.line.text.Length;
2780
2781                                                         selection_end_anchor = false;
2782                                                 }
2783                                                 selection_prev.line = caret.line;
2784                                                 selection_prev.tag = caret.tag;
2785                                                 selection_prev.pos = caret.pos;
2786
2787                                                 break;
2788                                         }
2789
2790                                         case CaretSelection.Word: {
2791                                                 int     start_pos;
2792                                                 int     end_pos;
2793
2794                                                 start_pos = FindWordSeparator(caret.line, caret.pos, false);
2795                                                 end_pos = FindWordSeparator(caret.line, caret.pos, true);
2796
2797                                                 
2798                                                 // Invalidate the selection delta
2799                                                 if (caret > selection_prev) {
2800                                                         Invalidate(selection_prev.line, selection_prev.pos, caret.line, end_pos);
2801                                                 } else {
2802                                                         Invalidate(selection_prev.line, selection_prev.pos, caret.line, start_pos);
2803                                                 }
2804                                                 if (caret < selection_anchor) {
2805                                                         selection_start.line = caret.line;
2806                                                         selection_start.tag = caret.line.FindTag(start_pos + 1);
2807                                                         selection_start.pos = start_pos;
2808
2809                                                         selection_end.line = selection_anchor.line;
2810                                                         selection_end.tag = selection_anchor.tag;
2811                                                         selection_end.pos = selection_anchor.pos;
2812
2813                                                         selection_prev.line = caret.line;
2814                                                         selection_prev.tag = caret.tag;
2815                                                         selection_prev.pos = start_pos;
2816
2817                                                         selection_end_anchor = true;
2818                                                 } else {
2819                                                         selection_start.line = selection_anchor.line;
2820                                                         selection_start.pos = selection_anchor.height;
2821                                                         selection_start.tag = selection_anchor.line.FindTag(selection_anchor.height + 1);
2822
2823                                                         selection_end.line = caret.line;
2824                                                         selection_end.tag = caret.line.FindTag(end_pos);
2825                                                         selection_end.pos = end_pos;
2826
2827                                                         selection_prev.line = caret.line;
2828                                                         selection_prev.tag = caret.tag;
2829                                                         selection_prev.pos = end_pos;
2830
2831                                                         selection_end_anchor = false;
2832                                                 }
2833                                                 break;
2834                                         }
2835
2836                                         case CaretSelection.Position: {
2837                                                 SetSelectionToCaret(false);
2838                                                 return;
2839                                         }
2840                                 }
2841                         } else {
2842                                 // We're setting the selection 'around' the caret position
2843                                 switch(mode) {
2844                                         case CaretSelection.Line: {
2845                                                 this.Invalidate(caret.line, 0, caret.line, caret.line.text.Length);
2846
2847                                                 selection_start.line = caret.line;
2848                                                 selection_start.tag = caret.line.tags;
2849                                                 selection_start.pos = 0;
2850
2851                                                 selection_end.line = caret.line;
2852                                                 selection_end.pos = caret.line.text.Length;
2853                                                 selection_end.tag = caret.line.FindTag(selection_end.pos);
2854
2855                                                 selection_anchor.line = selection_end.line;
2856                                                 selection_anchor.tag = selection_end.tag;
2857                                                 selection_anchor.pos = selection_end.pos;
2858                                                 selection_anchor.height = 0;
2859
2860                                                 selection_prev.line = caret.line;
2861                                                 selection_prev.tag = caret.tag;
2862                                                 selection_prev.pos = caret.pos;
2863
2864                                                 this.selection_end_anchor = true;
2865
2866                                                 break;
2867                                         }
2868
2869                                         case CaretSelection.Word: {
2870                                                 int     start_pos;
2871                                                 int     end_pos;
2872
2873                                                 start_pos = FindWordSeparator(caret.line, caret.pos, false);
2874                                                 end_pos = FindWordSeparator(caret.line, caret.pos, true);
2875
2876                                                 this.Invalidate(selection_start.line, start_pos, caret.line, end_pos);
2877
2878                                                 selection_start.line = caret.line;
2879                                                 selection_start.tag = caret.line.FindTag(start_pos + 1);
2880                                                 selection_start.pos = start_pos;
2881
2882                                                 selection_end.line = caret.line;
2883                                                 selection_end.tag = caret.line.FindTag(end_pos);
2884                                                 selection_end.pos = end_pos;
2885
2886                                                 selection_anchor.line = selection_end.line;
2887                                                 selection_anchor.tag = selection_end.tag;
2888                                                 selection_anchor.pos = selection_end.pos;
2889                                                 selection_anchor.height = start_pos;
2890
2891                                                 selection_prev.line = caret.line;
2892                                                 selection_prev.tag = caret.tag;
2893                                                 selection_prev.pos = caret.pos;
2894
2895                                                 this.selection_end_anchor = true;
2896
2897                                                 break;
2898                                         }
2899                                 }
2900                         }
2901
2902                         SetSelectionVisible (!(selection_start == selection_end));
2903                 }
2904
2905                 internal void SetSelectionToCaret(bool start) {
2906                         if (start) {
2907                                 // Invalidate old selection; selection is being reset to empty
2908                                 this.Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
2909
2910                                 selection_start.line = caret.line;
2911                                 selection_start.tag = caret.tag;
2912                                 selection_start.pos = caret.pos;
2913
2914                                 // start always also selects end
2915                                 selection_end.line = caret.line;
2916                                 selection_end.tag = caret.tag;
2917                                 selection_end.pos = caret.pos;
2918
2919                                 selection_anchor.line = caret.line;
2920                                 selection_anchor.tag = caret.tag;
2921                                 selection_anchor.pos = caret.pos;
2922                         } else {
2923                                 // Invalidate from previous end to caret (aka new end)
2924                                 if (selection_end_anchor) {
2925                                         if (selection_start != caret) {
2926                                                 this.Invalidate(selection_start.line, selection_start.pos, caret.line, caret.pos);
2927                                         }
2928                                 } else {
2929                                         if (selection_end != caret) {
2930                                                 this.Invalidate(selection_end.line, selection_end.pos, caret.line, caret.pos);
2931                                         }
2932                                 }
2933
2934                                 if (caret < selection_anchor) {
2935                                         selection_start.line = caret.line;
2936                                         selection_start.tag = caret.tag;
2937                                         selection_start.pos = caret.pos;
2938
2939                                         selection_end.line = selection_anchor.line;
2940                                         selection_end.tag = selection_anchor.tag;
2941                                         selection_end.pos = selection_anchor.pos;
2942
2943                                         selection_end_anchor = true;
2944                                 } else {
2945                                         selection_start.line = selection_anchor.line;
2946                                         selection_start.tag = selection_anchor.tag;
2947                                         selection_start.pos = selection_anchor.pos;
2948
2949                                         selection_end.line = caret.line;
2950                                         selection_end.tag = caret.tag;
2951                                         selection_end.pos = caret.pos;
2952
2953                                         selection_end_anchor = false;
2954                                 }
2955                         }
2956
2957                         SetSelectionVisible (!(selection_start == selection_end));
2958                 }
2959
2960                 internal void SetSelection(Line start, int start_pos, Line end, int end_pos) {
2961                         if (selection_visible) {
2962                                 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
2963                         }
2964
2965                         if ((end.line_no < start.line_no) || ((end == start) && (end_pos <= start_pos))) {
2966                                 selection_start.line = end;
2967                                 selection_start.tag = LineTag.FindTag(end, end_pos);
2968                                 selection_start.pos = end_pos;
2969
2970                                 selection_end.line = start;
2971                                 selection_end.tag = LineTag.FindTag(start, start_pos);
2972                                 selection_end.pos = start_pos;
2973
2974                                 selection_end_anchor = true;
2975                         } else {
2976                                 selection_start.line = start;
2977                                 selection_start.tag = LineTag.FindTag(start, start_pos);
2978                                 selection_start.pos = start_pos;
2979
2980                                 selection_end.line = end;
2981                                 selection_end.tag = LineTag.FindTag(end, end_pos);
2982                                 selection_end.pos = end_pos;
2983
2984                                 selection_end_anchor = false;
2985                         }
2986
2987                         selection_anchor.line = start;
2988                         selection_anchor.tag = selection_start.tag;
2989                         selection_anchor.pos = start_pos;
2990
2991                         if (((start == end) && (start_pos == end_pos)) || start == null || end == null) {
2992                                 SetSelectionVisible (false);
2993                         } else {
2994                                 SetSelectionVisible (true);
2995                                 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
2996                         }
2997                 }
2998
2999                 internal void SetSelectionStart(Line start, int start_pos, bool invalidate) {
3000                         // Invalidate from the previous to the new start pos
3001                         if (invalidate)
3002                                 Invalidate(selection_start.line, selection_start.pos, start, start_pos);
3003
3004                         selection_start.line = start;
3005                         selection_start.pos = start_pos;
3006                         selection_start.tag = LineTag.FindTag(start, start_pos);
3007
3008                         selection_anchor.line = start;
3009                         selection_anchor.pos = start_pos;
3010                         selection_anchor.tag = selection_start.tag;
3011
3012                         selection_end_anchor = false;
3013
3014                         
3015                         if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
3016                                 SetSelectionVisible (true);
3017                         } else {
3018                                 SetSelectionVisible (false);
3019                         }
3020
3021                         if (invalidate)
3022                                 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3023                 }
3024
3025                 internal void SetSelectionStart(int character_index, bool invalidate) {
3026                         Line    line;
3027                         LineTag tag;
3028                         int     pos;
3029
3030                         if (character_index < 0) {
3031                                 return;
3032                         }
3033
3034                         CharIndexToLineTag(character_index, out line, out tag, out pos);
3035                         SetSelectionStart(line, pos, invalidate);
3036                 }
3037
3038                 internal void SetSelectionEnd(Line end, int end_pos, bool invalidate) {
3039
3040                         if (end == selection_end.line && end_pos == selection_start.pos) {
3041                                 selection_anchor.line = selection_start.line;
3042                                 selection_anchor.tag = selection_start.tag;
3043                                 selection_anchor.pos = selection_start.pos;
3044
3045                                 selection_end.line = selection_start.line;
3046                                 selection_end.tag = selection_start.tag;
3047                                 selection_end.pos = selection_start.pos;
3048
3049                                 selection_end_anchor = false;
3050                         } else if ((end.line_no < selection_anchor.line.line_no) || ((end == selection_anchor.line) && (end_pos <= selection_anchor.pos))) {
3051                                 selection_start.line = end;
3052                                 selection_start.tag = LineTag.FindTag(end, end_pos);
3053                                 selection_start.pos = end_pos;
3054
3055                                 selection_end.line = selection_anchor.line;
3056                                 selection_end.tag = selection_anchor.tag;
3057                                 selection_end.pos = selection_anchor.pos;
3058
3059                                 selection_end_anchor = true;
3060                         } else {
3061                                 selection_start.line = selection_anchor.line;
3062                                 selection_start.tag = selection_anchor.tag;
3063                                 selection_start.pos = selection_anchor.pos;
3064
3065                                 selection_end.line = end;
3066                                 selection_end.tag = LineTag.FindTag(end, end_pos);
3067                                 selection_end.pos = end_pos;
3068
3069                                 selection_end_anchor = false;
3070                         }
3071
3072                         if ((selection_end.line != selection_start.line) || (selection_end.pos != selection_start.pos)) {
3073                                 SetSelectionVisible (true);
3074                                 if (invalidate)
3075                                         Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3076                         } else {
3077                                 SetSelectionVisible (false);
3078                                 // ?? Do I need to invalidate here, tests seem to work without it, but I don't think they should :-s
3079                         }
3080                 }
3081
3082                 internal void SetSelectionEnd(int character_index, bool invalidate) {
3083                         Line    line;
3084                         LineTag tag;
3085                         int     pos;
3086
3087                         if (character_index < 0) {
3088                                 return;
3089                         }
3090
3091                         CharIndexToLineTag(character_index, out line, out tag, out pos);
3092                         SetSelectionEnd(line, pos, invalidate);
3093                 }
3094
3095                 internal void SetSelection(Line start, int start_pos) {
3096                         if (selection_visible) {
3097                                 Invalidate(selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3098                         }
3099
3100                         selection_start.line = start;
3101                         selection_start.pos = start_pos;
3102                         selection_start.tag = LineTag.FindTag(start, start_pos);
3103
3104                         selection_end.line = start;
3105                         selection_end.tag = selection_start.tag;
3106                         selection_end.pos = start_pos;
3107
3108                         selection_anchor.line = start;
3109                         selection_anchor.tag = selection_start.tag;
3110                         selection_anchor.pos = start_pos;
3111
3112                         selection_end_anchor = false;
3113                         SetSelectionVisible (false);
3114                 }
3115
3116                 internal void InvalidateSelectionArea() {
3117                         Invalidate (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3118                 }
3119
3120                 // Return the current selection, as string
3121                 internal string GetSelection() {
3122                         // We return String.Empty if there is no selection
3123                         if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
3124                                 return string.Empty;
3125                         }
3126
3127                         if (selection_start.line == selection_end.line) {
3128                                 return selection_start.line.text.ToString (selection_start.pos, selection_end.pos - selection_start.pos);
3129                         } else {
3130                                 StringBuilder   sb;
3131                                 int             i;
3132                                 int             start;
3133                                 int             end;
3134
3135                                 sb = new StringBuilder();
3136                                 start = selection_start.line.line_no;
3137                                 end = selection_end.line.line_no;
3138
3139                                 sb.Append(selection_start.line.text.ToString(selection_start.pos, selection_start.line.text.Length - selection_start.pos) + Environment.NewLine);
3140
3141                                 if ((start + 1) < end) {
3142                                         for (i = start + 1; i < end; i++) {
3143                                                 sb.Append(GetLine(i).text.ToString() + Environment.NewLine);
3144                                         }
3145                                 }
3146
3147                                 sb.Append(selection_end.line.text.ToString(0, selection_end.pos));
3148
3149                                 return sb.ToString();
3150                         }
3151                 }
3152
3153                 internal void ReplaceSelection(string s, bool select_new) {
3154                         int             i;
3155
3156                         int selection_start_pos = LineTagToCharIndex (selection_start.line, selection_start.pos);
3157                         SuspendRecalc ();
3158
3159                         // First, delete any selected text
3160                         if ((selection_start.pos != selection_end.pos) || (selection_start.line != selection_end.line)) {
3161                                 if (selection_start.line == selection_end.line) {
3162                                         undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3163
3164                                         DeleteChars (selection_start.line, selection_start.pos, selection_end.pos - selection_start.pos);
3165
3166                                         // The tag might have been removed, we need to recalc it
3167                                         selection_start.tag = selection_start.line.FindTag(selection_start.pos + 1);
3168                                 } else {
3169                                         int             start;
3170                                         int             end;
3171
3172                                         start = selection_start.line.line_no;
3173                                         end = selection_end.line.line_no;
3174
3175                                         undo.RecordDeleteString (selection_start.line, selection_start.pos, selection_end.line, selection_end.pos);
3176
3177                                         InvalidateLinesAfter(selection_start.line);
3178
3179                                         // Delete first line
3180                                         DeleteChars (selection_start.line, selection_start.pos, selection_start.line.text.Length - selection_start.pos);
3181                                         selection_start.line.recalc = true;
3182
3183                                         // Delete last line
3184                                         DeleteChars(selection_end.line, 0, selection_end.pos);
3185
3186                                         start++;
3187                                         if (start < end) {
3188                                                 for (i = end - 1; i >= start; i--) {
3189                                                         Delete(i);
3190                                                 }
3191                                         }
3192
3193                                         // BIG FAT WARNING - selection_end.line might be stale due 
3194                                         // to the above Delete() call. DONT USE IT before hitting the end of this method!
3195
3196                                         // Join start and end
3197                                         Combine(selection_start.line.line_no, start);
3198                                 }
3199                         }
3200
3201
3202                         Insert(selection_start.line, selection_start.pos, false, s);
3203                         undo.RecordInsertString (selection_start.line, selection_start.pos, s);
3204                         ResumeRecalc (false);
3205
3206                         Line begin_update_line = selection_start.line;
3207                         int begin_update_pos = selection_start.pos;
3208                         
3209                         if (!select_new) {
3210                                 CharIndexToLineTag(selection_start_pos + s.Length, out selection_start.line,
3211                                                 out selection_start.tag, out selection_start.pos);
3212
3213                                 selection_end.line = selection_start.line;
3214                                 selection_end.pos = selection_start.pos;
3215                                 selection_end.tag = selection_start.tag;
3216                                 selection_anchor.line = selection_start.line;
3217                                 selection_anchor.pos = selection_start.pos;
3218                                 selection_anchor.tag = selection_start.tag;
3219
3220                                 SetSelectionVisible (false);
3221                         } else {
3222                                 CharIndexToLineTag(selection_start_pos, out selection_start.line,
3223                                                 out selection_start.tag, out selection_start.pos);
3224
3225                                 CharIndexToLineTag(selection_start_pos + s.Length, out selection_end.line,
3226                                                 out selection_end.tag, out selection_end.pos);
3227
3228                                 selection_anchor.line = selection_start.line;
3229                                 selection_anchor.pos = selection_start.pos;
3230                                 selection_anchor.tag = selection_start.tag;
3231
3232                                 SetSelectionVisible (true);
3233                         }
3234
3235                         PositionCaret (selection_start.line, selection_start.pos);
3236                         UpdateView (begin_update_line, selection_end.line.line_no - begin_update_line.line_no, begin_update_pos);
3237                 }
3238
3239                 internal void CharIndexToLineTag(int index, out Line line_out, out LineTag tag_out, out int pos) {
3240                         Line    line;
3241                         LineTag tag;
3242                         int     i;
3243                         int     chars;
3244                         int     start;
3245
3246                         chars = 0;
3247
3248                         for (i = 1; i <= lines; i++) {
3249                                 line = GetLine(i);
3250
3251                                 start = chars;
3252                                 chars += line.text.Length;
3253
3254                                 if (index <= chars) {
3255                                         // we found the line
3256                                         tag = line.tags;
3257
3258                                         while (tag != null) {
3259                                                 if (index < (start + tag.Start + tag.Length - 1)) {
3260                                                         line_out = line;
3261                                                         tag_out = LineTag.GetFinalTag (tag);
3262                                                         pos = index - start;
3263                                                         return;
3264                                                 }
3265                                                 if (tag.Next == null) {
3266                                                         Line    next_line;
3267
3268                                                         next_line = GetLine(line.line_no + 1);
3269
3270                                                         if (next_line != null) {
3271                                                                 line_out = next_line;
3272                                                                 tag_out = LineTag.GetFinalTag (next_line.tags);
3273                                                                 pos = 0;
3274                                                                 return;
3275                                                         } else {
3276                                                                 line_out = line;
3277                                                                 tag_out = LineTag.GetFinalTag (tag);
3278                                                                 pos = line_out.text.Length;
3279                                                                 return;
3280                                                         }
3281                                                 }
3282                                                 tag = tag.Next;
3283                                         }
3284                                 }
3285                         }
3286
3287                         line_out = GetLine(lines);
3288                         tag = line_out.tags;
3289                         while (tag.Next != null) {
3290                                 tag = tag.Next;
3291                         }
3292                         tag_out = tag;
3293                         pos = line_out.text.Length;
3294                 }
3295
3296                 internal int LineTagToCharIndex(Line line, int pos) {
3297                         int     i;
3298                         int     length;
3299
3300                         // Count first and last line
3301                         length = 0;
3302
3303                         // Count the lines in the middle
3304
3305                         for (i = 1; i < line.line_no; i++) {
3306                                 length += GetLine(i).text.Length;
3307                         }
3308
3309                         length += pos;
3310
3311                         return length;
3312                 }
3313
3314                 internal int SelectionLength() {
3315                         if ((selection_start.pos == selection_end.pos) && (selection_start.line == selection_end.line)) {
3316                                 return 0;
3317                         }
3318
3319                         if (selection_start.line == selection_end.line) {
3320                                 return selection_end.pos - selection_start.pos;
3321                         } else {
3322                                 int     i;
3323                                 int     start;
3324                                 int     end;
3325                                 int     length;
3326
3327                                 // Count first and last line
3328                                 length = selection_start.line.text.Length - selection_start.pos + selection_end.pos + crlf_size;
3329
3330                                 // Count the lines in the middle
3331                                 start = selection_start.line.line_no + 1;
3332                                 end = selection_end.line.line_no;
3333
3334                                 if (start < end) {
3335                                         for (i = start; i < end; i++) {
3336                                                 Line line = GetLine (i);
3337                                                 length += line.text.Length + LineEndingLength (line.ending);
3338                                         }
3339                                 }
3340
3341                                 return length;
3342                         }
3343
3344                         
3345                 }
3346
3347
3348                 /// <summary>Give it a Line number and it returns the Line object at with that line number</summary>
3349                 internal Line GetLine(int LineNo) {
3350                         Line    line = document;
3351
3352                         while (line != sentinel) {
3353                                 if (LineNo == line.line_no) {
3354                                         return line;
3355                                 } else if (LineNo < line.line_no) {
3356                                         line = line.left;
3357                                 } else {
3358                                         line = line.right;
3359                                 }
3360                         }
3361
3362                         return null;
3363                 }
3364
3365                 /// <summary>Retrieve the previous tag; walks line boundaries</summary>
3366                 internal LineTag PreviousTag(LineTag tag) {
3367                         Line l; 
3368
3369                         if (tag.Previous != null) {
3370                                 return tag.Previous;
3371                         }
3372
3373                         // Next line 
3374                         if (tag.Line.line_no == 1) {
3375                                 return null;
3376                         }
3377
3378                         l = GetLine(tag.Line.line_no - 1);
3379                         if (l != null) {
3380                                 LineTag t;
3381
3382                                 t = l.tags;
3383                                 while (t.Next != null) {
3384                                         t = t.Next;
3385                                 }
3386                                 return t;
3387                         }
3388
3389                         return null;
3390                 }
3391
3392                 /// <summary>Retrieve the next tag; walks line boundaries</summary>
3393                 internal LineTag NextTag(LineTag tag) {
3394                         Line l;
3395
3396                         if (tag.Next != null) {
3397                                 return tag.Next;
3398                         }
3399
3400                         // Next line
3401                         l = GetLine(tag.Line.line_no + 1);
3402                         if (l != null) {
3403                                 return l.tags;
3404                         }
3405
3406                         return null;
3407                 }
3408
3409                 internal Line ParagraphStart(Line line) {
3410                         Line lastline = line;
3411                         do {
3412                                 if (line.line_no <= 1)
3413                                         break;
3414
3415                                 line = lastline;
3416                                 lastline = GetLine (line.line_no - 1);
3417                         } while (lastline.ending == LineEnding.Wrap);
3418
3419                         return line;
3420                 }       
3421
3422                 internal Line ParagraphEnd(Line line) {
3423                         Line    l;
3424    
3425                         while (line.ending == LineEnding.Wrap) {
3426                                 l = GetLine(line.line_no + 1);
3427                                 if ((l == null) || (l.ending != LineEnding.Wrap)) {
3428                                         break;
3429                                 }
3430                                 line = l;
3431                         }
3432                         return line;
3433                 }
3434
3435                 /// <summary>Give it a pixel offset coordinate and it returns the Line covering that are (offset
3436                 /// is either X or Y depending on if we are multiline
3437                 /// </summary>
3438                 internal Line GetLineByPixel (int offset, bool exact)
3439                 {
3440                         Line    line = document;
3441                         Line    last = null;
3442
3443                         if (multiline) {
3444                                 while (line != sentinel) {
3445                                         last = line;
3446                                         if ((offset >= line.Y) && (offset < (line.Y+line.height))) {
3447                                                 return line;
3448                                         } else if (offset < line.Y) {
3449                                                 line = line.left;
3450                                         } else {
3451                                                 line = line.right;
3452                                         }
3453                                 }
3454                         } else {
3455                                 while (line != sentinel) {
3456                                         last = line;
3457                                         if ((offset >= line.X) && (offset < (line.X + line.Width)))
3458                                                 return line;
3459                                         else if (offset < line.X)
3460                                                 line = line.left;
3461                                         else
3462                                                 line = line.right;
3463                                 }
3464                         }
3465
3466                         if (exact) {
3467                                 return null;
3468                         }
3469                         return last;
3470                 }
3471
3472                 // Give it x/y pixel coordinates and it returns the Tag at that position
3473                 internal LineTag FindCursor (int x, int y, out int index)
3474                 {
3475                         Line line;
3476
3477                         x -= offset_x;
3478                         y -= offset_y;
3479
3480                         line = GetLineByPixel (multiline ? y : x, false);
3481
3482                         LineTag tag = line.GetTag (x);
3483                                 
3484                         if (tag.Length == 0 && tag.Start == 1)
3485                                 index = 0;
3486                         else
3487                                 index = tag.GetCharIndex (x - line.align_shift);
3488                         
3489                         return tag;
3490                 }
3491
3492                 /// <summary>Format area of document in specified font and color</summary>
3493                 /// <param name="start_pos">1-based start position on start_line</param>
3494                 /// <param name="end_pos">1-based end position on end_line </param>
3495                 internal void FormatText (Line start_line, int start_pos, Line end_line, int end_pos, Font font,
3496                                 Color color, Color back_color, FormatSpecified specified)
3497                 {
3498                         Line    l;
3499
3500                         // First, format the first line
3501                         if (start_line != end_line) {
3502                                 // First line
3503                                 LineTag.FormatText(start_line, start_pos, start_line.text.Length - start_pos + 1, font, color, back_color, specified);
3504
3505                                 // Format last line
3506                                 LineTag.FormatText(end_line, 1, end_pos, font, color, back_color, specified);
3507
3508                                 // Now all the lines inbetween
3509                                 for (int i = start_line.line_no + 1; i < end_line.line_no; i++) {
3510                                         l = GetLine(i);
3511                                         LineTag.FormatText(l, 1, l.text.Length, font, color, back_color, specified);
3512                                 }
3513                         } else {
3514                                 // Special case, single line
3515                                 LineTag.FormatText(start_line, start_pos, end_pos - start_pos, font, color, back_color, specified);
3516                                 
3517                                 if ((end_pos - start_pos) == 0 && CaretTag.Length != 0)
3518                                         CaretTag = CaretTag.Next;
3519                         }
3520                 }
3521
3522                 internal void RecalculateAlignments ()
3523                 {
3524                         Line    line;
3525                         int     line_no;
3526
3527                         line_no = 1;
3528
3529
3530
3531                         while (line_no <= lines) {
3532                                 line = GetLine(line_no);
3533
3534                                 if (line != null) {
3535                                         switch (line.alignment) {
3536                                         case HorizontalAlignment.Left:
3537                                                 line.align_shift = 0;
3538                                                 break;
3539                                         case HorizontalAlignment.Center:
3540                                                 line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
3541                                                 break;
3542                                         case HorizontalAlignment.Right:
3543                                                 line.align_shift = viewport_width - (int)line.widths[line.text.Length] - right_margin;
3544                                                 break;
3545                                         }
3546                                 }
3547
3548                                 line_no++;
3549                         }
3550                         return;
3551                 }
3552
3553                 /// <summary>Calculate formatting for the whole document</summary>
3554                 internal bool RecalculateDocument(Graphics g) {
3555                         return RecalculateDocument(g, 1, this.lines, false);
3556                 }
3557
3558                 /// <summary>Calculate formatting starting at a certain line</summary>
3559                 internal bool RecalculateDocument(Graphics g, int start) {
3560                         return RecalculateDocument(g, start, this.lines, false);
3561                 }
3562
3563                 /// <summary>Calculate formatting within two given line numbers</summary>
3564                 internal bool RecalculateDocument(Graphics g, int start, int end) {
3565                         return RecalculateDocument(g, start, end, false);
3566                 }
3567
3568                 /// <summary>With optimize on, returns true if line heights changed</summary>
3569                 internal bool RecalculateDocument(Graphics g, int start, int end, bool optimize) {
3570                         Line    line;
3571                         int     line_no;
3572                         int     offset;
3573                         int     new_width;
3574                         bool    changed;
3575                         int     shift;
3576
3577                         if (recalc_suspended > 0) {
3578                                 recalc_pending = true;
3579                                 recalc_start = Math.Min (recalc_start, start);
3580                                 recalc_end = Math.Max (recalc_end, end);
3581                                 recalc_optimize = optimize;
3582                                 return false;
3583                         }
3584
3585                         // Fixup the positions, they can go kinda nuts
3586                         // (this is suspend and resume recalc - they set them to 1 and max)
3587                         start = Math.Max (start, 1);
3588                         end = Math.Min (end, lines);
3589
3590                         offset = GetLine(start).offset;
3591                         line_no = start;
3592                         new_width = 0;
3593                         shift = this.lines;
3594                         if (!optimize) {
3595                                 changed = true;         // We always return true if we run non-optimized
3596                         } else {
3597                                 changed = false;
3598                         }
3599
3600                         while (line_no <= (end + this.lines - shift)) {
3601                                 line = GetLine(line_no++);
3602                                 line.offset = offset;
3603
3604                                 // if we are not calculating a password
3605                                 if (!calc_pass) {
3606                                         if (!optimize) {
3607                                                 line.RecalculateLine(g, this);
3608                                         } else {
3609                                                 if (line.recalc && line.RecalculateLine(g, this)) {
3610                                                         changed = true;
3611                                                         // If the height changed, all subsequent lines change
3612                                                         end = this.lines;
3613                                                         shift = this.lines;
3614                                                 }
3615                                         }
3616                                 } else {
3617                                         if (!optimize) {
3618                                                 line.RecalculatePasswordLine(g, this);
3619                                         } else {
3620                                                 if (line.recalc && line.RecalculatePasswordLine(g, this)) {
3621                                                         changed = true;
3622                                                         // If the height changed, all subsequent lines change
3623                                                         end = this.lines;
3624                                                         shift = this.lines;
3625                                                 }
3626                                         }
3627                                 }
3628
3629                                 if (line.widths[line.text.Length] > new_width) {
3630                                         new_width = (int)line.widths[line.text.Length];
3631                                 }
3632
3633                                 // Calculate alignment
3634                                 if (line.alignment != HorizontalAlignment.Left) {
3635                                         if (line.alignment == HorizontalAlignment.Center) {
3636                                                 line.align_shift = (viewport_width - (int)line.widths[line.text.Length]) / 2;
3637                                         } else {
3638                                                 line.align_shift = viewport_width - (int)line.widths[line.text.Length] - 1;
3639                                         }
3640                                 }
3641
3642                                 if (multiline)
3643                                         offset += line.height;
3644                                 else
3645                                         offset += (int) line.widths [line.text.Length];
3646
3647                                 if (line_no > lines) {
3648                                         break;
3649                                 }
3650                         }
3651
3652                         if (document_x != new_width) {
3653                                 document_x = new_width;
3654                                 if (WidthChanged != null) {
3655                                         WidthChanged(this, null);
3656                                 }
3657                         }
3658
3659                         RecalculateAlignments();
3660
3661                         line = GetLine(lines);
3662
3663                         if (document_y != line.Y + line.height) {
3664                                 document_y = line.Y + line.height;
3665                                 if (HeightChanged != null) {
3666                                         HeightChanged(this, null);
3667                                 }
3668                         }
3669
3670                         // scan for links and tell us if its all
3671                         // changed, so we can update everything
3672                         if (EnableLinks)
3673                                 ScanForLinks (start, end, ref changed);
3674
3675                         UpdateCaret();
3676                         return changed;
3677                 }
3678
3679                 internal int Size() {
3680                         return lines;
3681                 }
3682
3683                 private void owner_HandleCreated(object sender, EventArgs e) {
3684                         RecalculateDocument(owner.CreateGraphicsInternal());
3685                         AlignCaret();
3686                 }
3687
3688                 private void owner_VisibleChanged(object sender, EventArgs e) {
3689                         if (owner.Visible) {
3690                                 RecalculateDocument(owner.CreateGraphicsInternal());
3691                         }
3692                 }
3693
3694                 internal static bool IsWordSeparator (char ch)
3695                 {
3696                         switch (ch) {
3697                         case ' ':
3698                         case '\t':
3699                         case '(':
3700                         case ')':
3701                         case '\r':
3702                         case '\n':
3703                                 return true;
3704                         default:
3705                                 return false;
3706                         }
3707                 }
3708
3709                 internal int FindWordSeparator(Line line, int pos, bool forward) {
3710                         int len;
3711
3712                         len = line.text.Length;
3713
3714                         if (forward) {
3715                                 for (int i = pos + 1; i < len; i++) {
3716                                         if (IsWordSeparator(line.Text[i])) {
3717                                                 return i + 1;
3718                                         }
3719                                 }
3720                                 return len;
3721                         } else {
3722                                 for (int i = pos - 1; i > 0; i--) {
3723                                         if (IsWordSeparator(line.Text[i - 1])) {
3724                                                 return i;
3725                                         }
3726                                 }
3727                                 return 0;
3728                         }
3729                 }
3730
3731                 /* Search document for text */
3732                 internal bool FindChars(char[] chars, Marker start, Marker end, out Marker result) {
3733                         Line    line;
3734                         int     line_no;
3735                         int     pos;
3736                         int     line_len;
3737
3738                         // Search for occurence of any char in the chars array
3739                         result = new Marker();
3740
3741                         line = start.line;
3742                         line_no = start.line.line_no;
3743                         pos = start.pos;
3744                         while (line_no <= end.line.line_no) {
3745                                 line_len = line.text.Length;
3746                                 while (pos < line_len) {
3747                                         for (int i = 0; i < chars.Length; i++) {
3748                                                 if (line.text[pos] == chars[i]) {
3749                                                         // Special case
3750                                                         if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
3751                                                                 return false;
3752                                                         }
3753
3754                                                         result.line = line;
3755                                                         result.pos = pos;
3756                                                         return true;
3757                                                 }
3758                                         }
3759                                         pos++;
3760                                 }
3761
3762                                 pos = 0;
3763                                 line_no++;
3764                                 line = GetLine(line_no);
3765                         }
3766
3767                         return false;
3768                 }
3769
3770                 // This version does not build one big string for searching, instead it handles 
3771                 // line-boundaries, which is faster and less memory intensive
3772                 // FIXME - Depending on culture stuff we might have to create a big string and use culturespecific 
3773                 // search stuff and change it to accept and return positions instead of Markers (which would match 
3774                 // RichTextBox behaviour better but would be inconsistent with the rest of TextControl)
3775                 internal bool Find(string search, Marker start, Marker end, out Marker result, RichTextBoxFinds options) {
3776                         Marker  last;
3777                         string  search_string;
3778                         Line    line;
3779                         int     line_no;
3780                         int     pos;
3781                         int     line_len;
3782                         int     current;
3783                         bool    word;
3784                         bool    word_option;
3785                         bool    ignore_case;
3786                         bool    reverse;
3787                         char    c;
3788
3789                         result = new Marker();
3790                         word_option = ((options & RichTextBoxFinds.WholeWord) != 0);
3791                         ignore_case = ((options & RichTextBoxFinds.MatchCase) == 0);
3792                         reverse = ((options & RichTextBoxFinds.Reverse) != 0);
3793
3794                         line = start.line;
3795                         line_no = start.line.line_no;
3796                         pos = start.pos;
3797                         current = 0;
3798
3799                         // Prep our search string, lowercasing it if we do case-independent matching
3800                         if (ignore_case) {
3801                                 StringBuilder   sb;
3802                                 sb = new StringBuilder(search);
3803                                 for (int i = 0; i < sb.Length; i++) {
3804                                         sb[i] = Char.ToLower(sb[i]);
3805                                 }
3806                                 search_string = sb.ToString();
3807                         } else {
3808                                 search_string = search;
3809                         }
3810
3811                         // We need to check if the character before our start position is a wordbreak
3812                         if (word_option) {
3813                                 if (line_no == 1) {
3814                                         if ((pos == 0) || (IsWordSeparator(line.text[pos - 1]))) {
3815                                                 word = true;
3816                                         } else {
3817                                                 word = false;
3818                                         }
3819                                 } else {
3820                                         if (pos > 0) {
3821                                                 if (IsWordSeparator(line.text[pos - 1])) {
3822                                                         word = true;
3823                                                 } else {
3824                                                         word = false;
3825                                                 }
3826                                         } else {
3827                                                 // Need to check the end of the previous line
3828                                                 Line    prev_line;
3829
3830                                                 prev_line = GetLine(line_no - 1);
3831                                                 if (prev_line.ending == LineEnding.Wrap) {
3832                                                         if (IsWordSeparator(prev_line.text[prev_line.text.Length - 1])) {
3833                                                                 word = true;
3834                                                         } else {
3835                                                                 word = false;
3836                                                         }
3837                                                 } else {
3838                                                         word = true;
3839                                                 }
3840                                         }
3841                                 }
3842                         } else {
3843                                 word = false;
3844                         }
3845
3846                         // To avoid duplication of this loop with reverse logic, we search
3847                         // through the document, remembering the last match and when returning
3848                         // report that last remembered match
3849
3850                         last = new Marker();
3851                         last.height = -1;       // Abused - we use it to track change
3852
3853                         while (line_no <= end.line.line_no) {
3854                                 if (line_no != end.line.line_no) {
3855                                         line_len = line.text.Length;
3856                                 } else {
3857                                         line_len = end.pos;
3858                                 }
3859
3860                                 while (pos < line_len) {
3861
3862                                         if (word_option && (current == search_string.Length)) {
3863                                                 if (IsWordSeparator(line.text[pos])) {
3864                                                         if (!reverse) {
3865                                                                 goto FindFound;
3866                                                         } else {
3867                                                                 last = result;
3868                                                                 current = 0;
3869                                                         }
3870                                                 } else {
3871                                                         current = 0;
3872                                                 }
3873                                         }
3874
3875                                         if (ignore_case) {
3876                                                 c = Char.ToLower(line.text[pos]);
3877                                         } else {
3878                                                 c = line.text[pos];
3879                                         }
3880
3881                                         if (c == search_string[current]) {
3882                                                 
3883                                                 if (current == 0) {
3884                                                         result.line = line;
3885                                                         result.pos = pos;
3886                                                 }
3887                                                 if (!word_option || (word_option && (word || (current > 0)))) {
3888                                                         current++;
3889                                                 }
3890
3891                                                 if (!word_option && (current == search_string.Length)) {
3892                                                         if (!reverse) {
3893                                                                 goto FindFound;
3894                                                         } else {
3895                                                                 last = result;
3896                                                                 current = 0;
3897                                                         }
3898                                                 }
3899                                         } else {
3900                                                 current = 0;
3901                                         }
3902                                         pos++;
3903
3904                                         if (!word_option) {
3905                                                 continue;
3906                                         }
3907
3908                                         if (IsWordSeparator(c)) {
3909                                                 word = true;
3910                                         } else {
3911                                                 word = false;
3912                                         }
3913                                 }
3914
3915                                 if (word_option) {
3916                                         // Mark that we just saw a word boundary
3917                                         if (line.ending != LineEnding.Wrap || line.line_no == lines - 1) {
3918                                                 word = true;
3919                                         }
3920
3921                                         if (current == search_string.Length) {
3922                                                 if (word) {
3923                                                         if (!reverse) {
3924                                                                 goto FindFound;
3925                                                         } else {
3926                                                                 last = result;
3927                                                                 current = 0;
3928                                                         }
3929                                                 } else {
3930                                                         current = 0;
3931                                                 }
3932                                         }
3933                                 }
3934
3935                                 pos = 0;
3936                                 line_no++;
3937                                 line = GetLine(line_no);
3938                         }
3939
3940                         if (reverse) {
3941                                 if (last.height != -1) {
3942                                         result = last;
3943                                         return true;
3944                                 }
3945                         }
3946
3947                         return false;
3948
3949                         FindFound:
3950                         if (!reverse) {
3951 //                              if ((line.line_no == end.line.line_no) && (pos >= end.pos)) {
3952 //                                      return false;
3953 //                              }
3954                                 return true;
3955                         }
3956
3957                         result = last;
3958                         return true;
3959
3960                 }
3961
3962                 /* Marker stuff */
3963                 internal void GetMarker(out Marker mark, bool start) {
3964                         mark = new Marker();
3965
3966                         if (start) {
3967                                 mark.line = GetLine(1);
3968                                 mark.tag = mark.line.tags;
3969                                 mark.pos = 0;
3970                         } else {
3971                                 mark.line = GetLine(lines);
3972                                 mark.tag = mark.line.tags;
3973                                 while (mark.tag.Next != null) {
3974                                         mark.tag = mark.tag.Next;
3975                                 }
3976                                 mark.pos = mark.line.text.Length;
3977                         }
3978                 }
3979                 #endregion      // Internal Methods
3980
3981                 #region Events
3982                 internal event EventHandler CaretMoved;
3983                 internal event EventHandler WidthChanged;
3984                 internal event EventHandler HeightChanged;
3985                 internal event EventHandler LengthChanged;
3986                 #endregion      // Events
3987
3988                 #region Administrative
3989                 public IEnumerator GetEnumerator() {
3990                         // FIXME
3991                         return null;
3992                 }
3993
3994                 public override bool Equals(object obj) {
3995                         if (obj == null) {
3996                                 return false;
3997                         }
3998
3999                         if (!(obj is Document)) {
4000                                 return false;
4001                         }
4002
4003                         if (obj == this) {
4004                                 return true;
4005                         }
4006
4007                         if (ToString().Equals(((Document)obj).ToString())) {
4008                                 return true;
4009                         }
4010
4011                         return false;
4012                 }
4013
4014                 public override int GetHashCode() {
4015                         return document_id;
4016                 }
4017
4018                 public override string ToString() {
4019                         return "document " + this.document_id;
4020                 }
4021                 #endregion      // Administrative
4022         }
4023
4024         internal class PictureTag : LineTag {
4025
4026                 internal RTF.Picture picture;
4027
4028                 internal PictureTag (Line line, int start, RTF.Picture picture) : base (line, start)
4029                 {
4030                         this.picture = picture;
4031                 }
4032
4033                 public override bool IsTextTag {
4034                         get { return false; }
4035                 }
4036
4037                 public override SizeF SizeOfPosition (Graphics dc, int pos)
4038                 {
4039                         return picture.Size;
4040                 }
4041
4042                 internal override int MaxHeight ()
4043                 {
4044                         return (int) (picture.Height + 0.5F);
4045                 }
4046
4047                 public override void Draw (Graphics dc, Color color, float xoff, float y, int start, int end)
4048                 {
4049                         picture.DrawImage (dc, xoff + Line.widths [start], y, false);
4050                 }
4051
4052                 public override void Draw (Graphics dc, Color color, float xoff, float y, int start, int end, string text)
4053                 {
4054                         picture.DrawImage (dc, xoff + + Line.widths [start], y, false);
4055                 }
4056
4057                 public override string Text ()
4058                 {
4059                         return "I";
4060                 }
4061         }
4062
4063         internal class UndoManager {
4064
4065                 internal enum ActionType {
4066
4067                         Typing,
4068
4069                         // This is basically just cut & paste
4070                         InsertString,
4071                         DeleteString,
4072
4073                         UserActionBegin,
4074                         UserActionEnd
4075                 }
4076
4077                 internal class Action {
4078                         internal ActionType     type;
4079                         internal int            line_no;
4080                         internal int            pos;
4081                         internal object         data;
4082                 }
4083
4084                 #region Local Variables
4085                 private Document        document;
4086                 private Stack           undo_actions;
4087                 private Stack           redo_actions;
4088
4089                 //private int           caret_line;
4090                 //private int           caret_pos;
4091
4092                 // When performing an action, we lock the queue, so that the action can't be undone
4093                 private bool locked;
4094                 #endregion      // Local Variables
4095
4096                 #region Constructors
4097                 internal UndoManager (Document document)
4098                 {
4099                         this.document = document;
4100                         undo_actions = new Stack (50);
4101                         redo_actions = new Stack (50);
4102                 }
4103                 #endregion      // Constructors
4104
4105                 #region Properties
4106                 internal bool CanUndo {
4107                         get { return undo_actions.Count > 0; }
4108                 }
4109
4110                 internal bool CanRedo {
4111                         get { return redo_actions.Count > 0; }
4112                 }
4113
4114                 internal string UndoActionName {
4115                         get {
4116                                 foreach (Action action in undo_actions) {
4117                                         if (action.type == ActionType.UserActionBegin)
4118                                                 return (string) action.data;
4119                                         if (action.type == ActionType.Typing)
4120                                                 return Locale.GetText ("Typing");
4121                                 }
4122                                 return String.Empty;
4123                         }
4124                 }
4125
4126                 internal string RedoActionName {
4127                         get {
4128                                 foreach (Action action in redo_actions) {
4129                                         if (action.type == ActionType.UserActionBegin)
4130                                                 return (string) action.data;
4131                                         if (action.type == ActionType.Typing)
4132                                                 return Locale.GetText ("Typing");
4133                                 }
4134                                 return String.Empty;
4135                         }
4136                 }
4137                 #endregion      // Properties
4138
4139                 #region Internal Methods
4140                 internal void Clear ()
4141                 {
4142                         undo_actions.Clear();
4143                         redo_actions.Clear();
4144                 }
4145
4146                 internal bool Undo ()
4147                 {
4148                         Action action;
4149                         bool user_action_finished = false;
4150
4151                         if (undo_actions.Count == 0)
4152                                 return false;
4153
4154                         locked = true;
4155                         do {
4156                                 Line start;
4157                                 action = (Action) undo_actions.Pop ();
4158
4159                                 // Put onto redo stack
4160                                 redo_actions.Push(action);
4161
4162                                 // Do the thing
4163                                 switch(action.type) {
4164
4165                                 case ActionType.UserActionBegin:
4166                                         user_action_finished = true;
4167                                         break;
4168
4169                                 case ActionType.UserActionEnd:
4170                                         // noop
4171                                         break;
4172
4173                                 case ActionType.InsertString:
4174                                         start = document.GetLine (action.line_no);
4175                                         document.SuspendUpdate ();
4176                                         document.DeleteMultiline (start, action.pos, ((string) action.data).Length + 1);
4177                                         document.PositionCaret (start, action.pos);
4178                                         document.SetSelectionToCaret (true);
4179                                         document.ResumeUpdate (true);
4180                                         break;
4181
4182                                 case ActionType.Typing:
4183                                         start = document.GetLine (action.line_no);
4184                                         document.SuspendUpdate ();
4185                                         document.DeleteMultiline (start, action.pos, ((StringBuilder) action.data).Length);
4186                                         document.PositionCaret (start, action.pos);
4187                                         document.SetSelectionToCaret (true);
4188                                         document.ResumeUpdate (true);
4189
4190                                         // This is an open ended operation, so only a single typing operation can be undone at once
4191                                         user_action_finished = true;
4192                                         break;
4193
4194                                 case ActionType.DeleteString:
4195                                         start = document.GetLine (action.line_no);
4196                                         document.SuspendUpdate ();
4197                                         Insert (start, action.pos, (Line) action.data, true);
4198                                         document.ResumeUpdate (true);
4199                                         break;
4200                                 }
4201                         } while (!user_action_finished && undo_actions.Count > 0);
4202
4203                         locked = false;
4204
4205                         return true;
4206                 }
4207
4208                 internal bool Redo ()
4209                 {
4210                         Action action;
4211                         bool user_action_finished = false;
4212
4213                         if (redo_actions.Count == 0)
4214                                 return false;
4215
4216                         locked = true;
4217                         do {
4218                                 Line start;
4219                                 int start_index;
4220
4221                                 action = (Action) redo_actions.Pop ();
4222                                 undo_actions.Push (action);
4223
4224                                 switch (action.type) {
4225
4226                                 case ActionType.UserActionBegin:
4227                                         //  Noop
4228                                         break;
4229
4230                                 case ActionType.UserActionEnd:
4231                                         user_action_finished = true;
4232                                         break;
4233
4234                                 case ActionType.InsertString:
4235                                         start = document.GetLine (action.line_no);
4236                                         document.SuspendUpdate ();
4237                                         start_index = document.LineTagToCharIndex (start, action.pos);
4238                                         document.InsertString (start, action.pos, (string) action.data);
4239                                         document.CharIndexToLineTag (start_index + ((string) action.data).Length,
4240                                                         out document.caret.line, out document.caret.tag,
4241                                                         out document.caret.pos);
4242                                         document.UpdateCaret ();
4243                                         document.SetSelectionToCaret (true);
4244                                         document.ResumeUpdate (true);
4245                                         break;
4246
4247                                 case ActionType.Typing:
4248                                         start = document.GetLine (action.line_no);
4249                                         document.SuspendUpdate ();
4250                                         start_index = document.LineTagToCharIndex (start, action.pos);
4251                                         document.InsertString (start, action.pos, ((StringBuilder) action.data).ToString ());
4252                                         document.CharIndexToLineTag (start_index + ((StringBuilder) action.data).Length,
4253                                                         out document.caret.line, out document.caret.tag,
4254                                                         out document.caret.pos);
4255                                         document.UpdateCaret ();
4256                                         document.SetSelectionToCaret (true);
4257                                         document.ResumeUpdate (true);
4258
4259                                         // This is an open ended operation, so only a single typing operation can be undone at once
4260                                         user_action_finished = true;
4261                                         break;
4262
4263                                 case ActionType.DeleteString:
4264                                         start = document.GetLine (action.line_no);
4265                                         document.SuspendUpdate ();
4266                                         document.DeleteMultiline (start, action.pos, ((Line) action.data).text.Length);
4267                                         document.PositionCaret (start, action.pos);
4268                                         document.SetSelectionToCaret (true);
4269                                         document.ResumeUpdate (true);
4270
4271                                         break;
4272                                 }
4273                         } while (!user_action_finished && redo_actions.Count > 0);
4274
4275                         locked = false;
4276
4277                         return true;
4278                 }
4279                 #endregion      // Internal Methods
4280
4281                 #region Private Methods
4282
4283                 public void BeginUserAction (string name)
4284                 {
4285                         if (locked)
4286                                 return;
4287
4288                         // Nuke the redo queue
4289                         redo_actions.Clear ();
4290
4291                         Action ua = new Action ();
4292                         ua.type = ActionType.UserActionBegin;
4293                         ua.data = name;
4294
4295                         undo_actions.Push (ua);
4296                 }
4297
4298                 public void EndUserAction ()
4299                 {
4300                         if (locked)
4301                                 return;
4302
4303                         Action ua = new Action ();
4304                         ua.type = ActionType.UserActionEnd;
4305
4306                         undo_actions.Push (ua);
4307                 }
4308
4309                 // start_pos, end_pos = 1 based
4310                 public void RecordDeleteString (Line start_line, int start_pos, Line end_line, int end_pos)
4311                 {
4312                         if (locked)
4313                                 return;
4314
4315                         // Nuke the redo queue
4316                         redo_actions.Clear ();
4317
4318                         Action  a = new Action ();
4319
4320                         // We cant simply store the string, because then formatting would be lost
4321                         a.type = ActionType.DeleteString;
4322                         a.line_no = start_line.line_no;
4323                         a.pos = start_pos;
4324                         a.data = Duplicate (start_line, start_pos, end_line, end_pos);
4325
4326                         undo_actions.Push(a);
4327                 }
4328
4329                 public void RecordInsertString (Line line, int pos, string str)
4330                 {
4331                         if (locked || str.Length == 0)
4332                                 return;
4333
4334                         // Nuke the redo queue
4335                         redo_actions.Clear ();
4336
4337                         Action a = new Action ();
4338
4339                         a.type = ActionType.InsertString;
4340                         a.data = str;
4341                         a.line_no = line.line_no;
4342                         a.pos = pos;
4343
4344                         undo_actions.Push (a);
4345                 }
4346
4347                 public void RecordTyping (Line line, int pos, char ch)
4348                 {
4349                         if (locked)
4350                                 return;
4351
4352                         // Nuke the redo queue
4353                         redo_actions.Clear ();
4354
4355                         Action a = null;
4356
4357                         if (undo_actions.Count > 0)
4358                                 a = (Action) undo_actions.Peek ();
4359
4360                         if (a == null || a.type != ActionType.Typing) {
4361                                 a = new Action ();
4362                                 a.type = ActionType.Typing;
4363                                 a.data = new StringBuilder ();
4364                                 a.line_no = line.line_no;
4365                                 a.pos = pos;
4366
4367                                 undo_actions.Push (a);
4368                         }
4369
4370                         StringBuilder data = (StringBuilder) a.data;
4371                         data.Append (ch);
4372                 }
4373
4374                 // start_pos = 1-based
4375                 // end_pos = 1-based
4376                 public Line Duplicate(Line start_line, int start_pos, Line end_line, int end_pos)
4377                 {
4378                         Line    ret;
4379                         Line    line;
4380                         Line    current;
4381                         LineTag tag;
4382                         LineTag current_tag;
4383                         int     start;
4384                         int     end;
4385                         int     tag_start;
4386
4387                         line = new Line (start_line.document, start_line.ending);
4388                         ret = line;
4389
4390                         for (int i = start_line.line_no; i <= end_line.line_no; i++) {
4391                                 current = document.GetLine(i);
4392
4393                                 if (start_line.line_no == i) {
4394                                         start = start_pos;
4395                                 } else {
4396                                         start = 0;
4397                                 }
4398
4399                                 if (end_line.line_no == i) {
4400                                         end = end_pos;
4401                                 } else {
4402                                         end = current.text.Length;
4403                                 }
4404
4405                                 if (end_pos == 0)
4406                                         continue;
4407
4408                                 // Text for the tag
4409                                 line.text = new StringBuilder (current.text.ToString (start, end - start));
4410
4411                                 // Copy tags from start to start+length onto new line
4412                                 current_tag = current.FindTag (start + 1);
4413                                 while ((current_tag != null) && (current_tag.Start <= end)) {
4414                                         if ((current_tag.Start <= start) && (start < (current_tag.Start + current_tag.Length))) {
4415                                                 // start tag is within this tag
4416                                                 tag_start = start;
4417                                         } else {
4418                                                 tag_start = current_tag.Start;
4419                                         }
4420
4421                                         tag = new LineTag(line, tag_start - start + 1);
4422                                         tag.CopyFormattingFrom (current_tag);
4423
4424                                         current_tag = current_tag.Next;
4425
4426                                         // Add the new tag to the line
4427                                         if (line.tags == null) {
4428                                                 line.tags = tag;
4429                                         } else {
4430                                                 LineTag tail;
4431                                                 tail = line.tags;
4432
4433                                                 while (tail.Next != null) {
4434                                                         tail = tail.Next;
4435                                                 }
4436                                                 tail.Next = tag;
4437                                                 tag.Previous = tail;
4438                                         }
4439                                 }
4440
4441                                 if ((i + 1) <= end_line.line_no) {
4442                                         line.ending = current.ending;
4443
4444                                         // Chain them (we use right/left as next/previous)
4445                                         line.right = new Line (start_line.document, start_line.ending);
4446                                         line.right.left = line;
4447                                         line = line.right;
4448                                 }
4449                         }
4450
4451                         return ret;
4452                 }
4453
4454                 // Insert multi-line text at the given position; use formatting at insertion point for inserted text
4455                 internal void Insert(Line line, int pos, Line insert, bool select)
4456                 {
4457                         Line    current;
4458                         LineTag tag;
4459                         int     offset;
4460                         int     lines;
4461                         Line    first;
4462
4463                         // Handle special case first
4464                         if (insert.right == null) {
4465
4466                                 // Single line insert
4467                                 document.Split(line, pos);
4468
4469                                 if (insert.tags == null) {
4470                                         return; // Blank line
4471                                 }
4472
4473                                 //Insert our tags at the end
4474                                 tag = line.tags;
4475
4476                                 while (tag.Next != null) {
4477                                         tag = tag.Next;
4478                                 }
4479
4480                                 offset = tag.Start + tag.Length - 1;
4481
4482                                 tag.Next = insert.tags;
4483                                 line.text.Insert(offset, insert.text.ToString());
4484
4485                                 // Adjust start locations
4486                                 tag = tag.Next;
4487                                 while (tag != null) {
4488                                         tag.Start += offset;
4489                                         tag.Line = line;
4490                                         tag = tag.Next;
4491                                 }
4492                                 // Put it back together
4493                                 document.Combine(line.line_no, line.line_no + 1);
4494
4495                                 if (select) {
4496                                         document.SetSelectionStart (line, pos, false);
4497                                         document.SetSelectionEnd (line, pos + insert.text.Length, false);
4498                                 }
4499
4500                                 document.UpdateView(line, pos);
4501                                 return;
4502                         }
4503
4504                         first = line;
4505                         lines = 1;
4506                         current = insert;
4507
4508                         while (current != null) {
4509
4510                                 if (current == insert) {
4511                                         // Inserting the first line we split the line (and make space)
4512                                         document.Split(line.line_no, pos);
4513                                         //Insert our tags at the end of the line
4514                                         tag = line.tags;
4515
4516                                         
4517                                         if (tag != null && tag.Length != 0) {
4518                                                 while (tag.Next != null) {
4519                                                         tag = tag.Next;
4520                                                 }
4521                                                 offset = tag.Start + tag.Length - 1;
4522                                                 tag.Next = current.tags;
4523                                                 tag.Next.Previous = tag;
4524
4525                                                 tag = tag.Next;
4526
4527                                         } else {
4528                                                 offset = 0;
4529                                                 line.tags = current.tags;
4530                                                 line.tags.Previous = null;
4531                                                 tag = line.tags;
4532                                         }
4533
4534                                         line.ending = current.ending;
4535                                 } else {
4536                                         document.Split(line.line_no, 0);
4537                                         offset = 0;
4538                                         line.tags = current.tags;
4539                                         line.tags.Previous = null;
4540                                         line.ending = current.ending;
4541                                         tag = line.tags;
4542                                 }
4543
4544                                 // Adjust start locations and line pointers
4545                                 while (tag != null) {
4546                                         tag.Start += offset - 1;
4547                                         tag.Line = line;
4548                                         tag = tag.Next;
4549                                 }
4550
4551                                 line.text.Insert(offset, current.text.ToString());
4552                                 line.Grow(line.text.Length);
4553
4554                                 line.recalc = true;
4555                                 line = document.GetLine(line.line_no + 1);
4556
4557                                 // FIXME? Test undo of line-boundaries
4558                                 if ((current.right == null) && (current.tags.Length != 0)) {
4559                                         document.Combine(line.line_no - 1, line.line_no);
4560                                 }
4561                                 current = current.right;
4562                                 lines++;
4563
4564                         }
4565
4566                         // Recalculate our document
4567                         document.UpdateView(first, lines, pos);
4568                         return;
4569                 }               
4570                 #endregion      // Private Methods
4571         }
4572 }