* TextControl.cs:
authorJackson Harper <jackson@novell.com>
Tue, 20 Mar 2007 21:48:08 +0000 (21:48 -0000)
committerJackson Harper <jackson@novell.com>
Tue, 20 Mar 2007 21:48:08 +0000 (21:48 -0000)
        * TextBoxBase.cs: Allow different types of line endings. \r,
        * \r\n, \r\r\n, and \n.

svn path=/trunk/mcs/; revision=74708

mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms/TextBoxBase.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/TextControl.cs

index b04217176881e410a73879543307546597e28f28..483cbfe539ed30725234e548086bb9509f5a3973 100644 (file)
@@ -1,3 +1,9 @@
+2007-03-20  Jackson Harper  <jackson@ximian.com>
+
+       * TextControl.cs:
+       * TextBoxBase.cs: Allow different types of line endings. \r, \r\n,
+       \r\r\n, \n.
+
 2007-03-20  Rolf Bjarne Kvinge <RKvinge@novell.com> 
 
        * ComboBox.cs: PreferredHeight seems to be ItemHeight + 6, but there is
index ce3224b236f4b5c030d100178866cfc3967d78fb..088eb189db740ff88fb2f7cf25bf9b1de5f38db6 100644 (file)
@@ -375,7 +375,7 @@ namespace System.Windows.Forms {
                                        document.Add(i+1, CaseAdjust(value[i]), alignment, Font, brush);
                                        if (carriage_return) {
                                                Line line = document.GetLine (i + 1);
-                                               line.carriage_return = true;
+                                               line.ending = "\r\n";
                                        }
                                }
 
@@ -587,10 +587,8 @@ namespace System.Windows.Forms {
 
                                Line line = null;
                                for (int i = 1; i <= document.Lines; i++) {
-                                       if (line != null && line.carriage_return)
-                                               sb.Append ("\r");
-                                       if (line != null && !line.soft_break)
-                                               sb.Append (Environment.NewLine);
+                                       if (line != null)
+                                               sb.Append (line.ending);
                                        line = document.GetLine (i);
                                        sb.Append(line.text.ToString());
                                }
index 71ab14c7aeaa43fbefc0eaf496c017628d181f15..dfd3fc8a5bc31d20d20c58799abadedcfc789b9c 100644 (file)
@@ -124,7 +124,7 @@ namespace System.Windows.Forms {
                internal int                    indent;                 // Left indent for the first line
                internal int                    hanging_indent;         // Hanging indent (left indent for all but the first line)
                internal int                    right_indent;           // Right indent for all lines
-               internal bool carriage_return;
+               internal string ending;  // The ending of a line, can be "\r\n", "\r", "\n\r", "\n", or empty.  "\n" is default
 
 
                // Stuff that's important for the tree
@@ -151,6 +151,8 @@ namespace System.Windows.Forms {
                        recalc = true;
                        soft_break = false;
                        alignment = document.alignment;
+
+                       ending = "\n";
                }
 
                internal Line(Document document, int LineNo, string Text, Font font, SolidBrush color) : this (document) {
@@ -332,6 +334,12 @@ namespace System.Windows.Forms {
                        }
                }
 
+               internal void MakeSoft ()
+               {
+                       soft_break = true;
+                       ending = String.Empty;
+               }
+
                internal void Streamline(int lines) {
                        LineTag current;
                        LineTag next;
@@ -504,7 +512,7 @@ namespace System.Windows.Forms {
                                                pos = wrap_pos;
                                                len = text.Length;
                                                doc.Split(this, tag, pos, this.soft_break);
-                                               this.soft_break = true;
+                                               MakeSoft ();
                                                len = this.text.Length;
                                                
                                                retval = true;
@@ -516,7 +524,7 @@ namespace System.Windows.Forms {
                                                widths [pos + 1] = widths [pos] + w;
 
                                                doc.Split(this, tag, pos, this.soft_break);
-                                               this.soft_break = true;
+                                               MakeSoft ();
                                                len = this.text.Length;
                                                retval = true;
                                                wrapped = true;
@@ -825,7 +833,7 @@ namespace System.Windows.Forms {
 
                        Add(1, "", owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.ForeColor));
                        Line l = GetLine (1);
-                       l.soft_break = true;
+                       l.MakeSoft ();
 
                        undo = new UndoManager (this);
 
@@ -1438,7 +1446,7 @@ namespace System.Windows.Forms {
                        // We always have a blank line
                        Add(1, "", owner.Font, ThemeEngine.Current.ResPool.GetSolidBrush(owner.ForeColor));
                        Line l = GetLine (1);
-                       l.soft_break = true;
+                       l.MakeSoft ();
                        
                        this.RecalculateDocument(owner.CreateGraphicsInternal());
                        PositionCaret(0, 0);
@@ -2063,21 +2071,33 @@ namespace System.Windows.Forms {
                        }
                }
 
-               private void InsertLineString (Line line, int pos, string s)
+               // these are the patterns I am allowing:  "\r\r\n", "\r\n", "\n", "\r"
+               internal int GetLineEnding (string line, int start, out string ending)
                {
-                       bool carriage_return = false;
+                       int res;
 
-                       if (s.EndsWith ("\r")) {
-                               s = s.Substring (0, s.Length - 1);
-                               carriage_return = true;
+                       res = line.IndexOf ('\r', start);
+                       if (res != -1) {
+                               if (res + 2 < line.Length && line [res + 1] == '\r' && line [res + 2] == '\n') {
+                                       ending = "\r\r\n";
+                                       return res;
+                               }
+                               if (res + 1 < line.Length && line [res + 1] == '\n') {
+                                       ending = "\r\n";
+                                       return res;
+                               }
+                               ending = "\r";
+                               return res;
                        }
 
-                       InsertString (line, pos, s);
-
-                       if (carriage_return) {
-                               Line l = GetLine (line.line_no);
-                               l.carriage_return = true;
+                       res = line.IndexOf ('\n', start);
+                       if (res != -1) {
+                               ending = "\n";
+                               return res;
                        }
+
+                       ending = String.Empty;
+                       return line.Length;
                }
 
                // Insert multi-line text at the given position; use formatting at insertion point for inserted text
@@ -2086,6 +2106,7 @@ namespace System.Windows.Forms {
                        int base_line;
                        int old_line_count;
                        int count = 1;
+                       string ending;
                        LineTag tag = LineTag.FindTag (line, pos);
                        
                        SuspendRecalc ();
@@ -2093,54 +2114,34 @@ namespace System.Windows.Forms {
                        base_line = line.line_no;
                        old_line_count = lines;
 
-                       break_index = s.IndexOf ('\n');
+                       break_index = GetLineEnding (s, 0, out ending);
 
                        // Bump the text at insertion point a line down if we're inserting more than one line
-                       if (break_index > -1) {
-                               Split(line, pos);
+                       if (break_index != s.Length) {
+                               Split (line, pos);
                                line.soft_break = false;
+                               line.ending = ending;
                                // Remainder of start line is now in base_line + 1
                        }
 
-                       if (break_index == -1)
-                               break_index = s.Length;
-
-                       InsertLineString (line, pos, s.Substring (0, break_index));
-                       break_index++;
+                       InsertString (line, pos, s.Substring (0, break_index));
 
+                       break_index += ending.Length;
                        while (break_index < s.Length) {
                                bool soft = false;
-                               int next_break = s.IndexOf ('\n', break_index);
-                               int adjusted_next_break;
-                               bool carriage_return = false;
-
-                               if (next_break == -1) {
-                                       next_break = s.Length;
-                                       soft = true;
-                               }
-
-                               adjusted_next_break = next_break;
-                               if (s [next_break - 1] == '\r') {
-                                       adjusted_next_break--;
-                                       carriage_return = true;
-                               }
+                               int next_break = GetLineEnding (s, break_index, out ending);
+                               string line_text = s.Substring (break_index, next_break - break_index);
 
-                               string line_text = s.Substring (break_index, adjusted_next_break - break_index);
                                Add (base_line + count, line_text, line.alignment, tag.font, tag.color);
 
-                               if (carriage_return) {
-                                       Line last = GetLine (base_line + count);
-                                       last.carriage_return = true;
+                               Line last = GetLine (base_line + count);
+                               last.ending = ending;
 
-                                       if (soft)
-                                               last.soft_break = true;
-                               } else if (soft) {
-                                       Line last = GetLine (base_line + count);
-                                       last.soft_break = true;
-                               }
+                               if (soft)
+                                       last.MakeSoft ();
 
                                count++;
-                               break_index = next_break + 1;
+                               break_index = next_break + ending.Length;
                        }
 
                        ResumeRecalc (true);
@@ -2503,6 +2504,7 @@ namespace System.Windows.Forms {
 
                        // Maintain the line ending style
                        first.soft_break = second.soft_break;
+                       first.ending = second.ending;
 
                        while (last.next != null) {
                                last = last.next;
@@ -2619,9 +2621,9 @@ namespace System.Windows.Forms {
 
                                new_line = GetLine(line.line_no + 1);
 
-                               line.carriage_return = false;
-                               new_line.carriage_return = line.carriage_return;
-                               new_line.soft_break = soft;
+                               new_line.ending = line.ending;
+                               if (soft)
+                                       new_line.MakeSoft ();
                                
                                if (move_caret) {
                                        caret.line = new_line;
@@ -2649,9 +2651,10 @@ namespace System.Windows.Forms {
                        // Now transfer our tags from this line to the next
                        new_line = GetLine(line.line_no + 1);
 
-                       line.carriage_return = false;
-                       new_line.carriage_return = line.carriage_return;
+                       new_line.ending = line.ending;
                        new_line.soft_break = soft;
+                       if (soft)
+                               new_line.MakeSoft ();
 
                        line.recalc = true;
                        new_line.recalc = true;