2007-11-14 Jonathan Pobst <monkey@jpobst.com>
authorJonathan Pobst <monkey@jpobst.com>
Wed, 14 Nov 2007 22:49:20 +0000 (22:49 -0000)
committerJonathan Pobst <monkey@jpobst.com>
Wed, 14 Nov 2007 22:49:20 +0000 (22:49 -0000)
* LineTag.cs: Don't attempt to draw '\r', treat it like it doesn't exist.
When measureing CR or LF, use /u000D instead of /u0013. (Hex, not decimal.)
* TextControl.cs: Fix a case in GetLineEnding where a \n before a \r would
be ignored.  Create a new GetLineEnding that can specify which types of
line endings to look for.  On Insert, only create new lines for \n and \r\n.
[Fixes bug #324274]

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

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

index 489d35d0d2741a485564c9961a49269b43948717..23d0785a52944da71cf8a2a6771f6f011965583b 100644 (file)
@@ -1,3 +1,12 @@
+2007-11-14  Jonathan Pobst  <monkey@jpobst.com>
+
+       * LineTag.cs: Don't attempt to draw '\r', treat it like it doesn't exist.
+       When measureing CR or LF, use /u000D instead of /u0013. (Hex, not decimal.)
+       * TextControl.cs: Fix a case in GetLineEnding where a \n before a \r would
+       be ignored.  Create a new GetLineEnding that can specify which types of
+       line endings to look for.  On Insert, only create new lines for \n and \r\n.
+       [Fixes bug #324274]
+
 2007-11-14  Jonathan Pobst  <monkey@jpobst.com>
 
        * TextBoxBase.cs: As we loop through each line changing the font, tell
index cd62963bdd9dae4b08010410fa8f8dbd95ad97c5..df325bfefcba317c7abae41bd58b5ba1cfafd44c 100644 (file)
@@ -251,7 +251,7 @@ namespace System.Windows.Forms
                \r
                public virtual void Draw (Graphics dc, Color color, float x, float y, int start, int end)\r
                {\r
-                       TextBoxTextRenderer.DrawText (dc, line.text.ToString (start, end), font, color, x, y, false);\r
+                       TextBoxTextRenderer.DrawText (dc, line.text.ToString (start, end).Replace ("\r", string.Empty), font, color, x, y, false);\r
                }\r
 \r
                public virtual void Draw (Graphics dc, Color color, float xoff, float y, int start, int end, string text)\r
@@ -261,7 +261,7 @@ namespace System.Windows.Forms
                                if (tab_index == -1)\r
                                        tab_index = end;\r
 \r
-                               TextBoxTextRenderer.DrawText (dc, text.Substring (start, tab_index - start), font, color, xoff + line.widths[start], y, false);\r
+                               TextBoxTextRenderer.DrawText (dc, text.Substring (start, tab_index - start).Replace ("\r", string.Empty), font, color, xoff + line.widths[start], y, false);\r
 \r
                                // non multilines get the unknown char \r
                                if (!line.document.multiline && tab_index != end)\r
@@ -452,7 +452,7 @@ namespace System.Windows.Forms
                                return res;\r
                        case 10:\r
                        case 13:\r
-                               return TextBoxTextRenderer.MeasureText (dc, "\u0013", font);\r
+                               return TextBoxTextRenderer.MeasureText (dc, "\u000D", font);\r
                        }\r
                        \r
                        return TextBoxTextRenderer.MeasureText (dc, text, font);\r
index 97d93b162e3ed2396df9880a844e49afae97ba26..05746fffa6b019400e8434dc249ae61cb7bf572b 100644 (file)
@@ -95,13 +95,13 @@ namespace System.Windows.Forms {
        }
 
        internal enum LineEnding {
-               Wrap,    // line wraps to the next line
-               Limp,    // \r
-               Hard,    // \r\n
-               Soft,    // \r\r\n
-               Rich,    // \n
+               Wrap = 1,    // line wraps to the next line
+               Limp = 2,    // \r
+               Hard = 4,    // \r\n
+               Soft = 8,    // \r\r\n
+               Rich = 16,    // \n
 
-               None
+               None = 0
        }
        
        internal class Document : ICloneable, IEnumerable {
@@ -1511,11 +1511,26 @@ namespace System.Windows.Forms {
                        }
                }
 
-               internal int GetLineEnding (string line, int start, out LineEnding ending)
+               private int GetLineEnding (string line, int start, out LineEnding ending)
                {
                        int res;
+                       int rich_index;
 
+                       if (start >= line.Length) {
+                               ending = LineEnding.Wrap;
+                               return -1;
+                       }
+                       
                        res = line.IndexOf ('\r', start);
+                       rich_index = line.IndexOf ('\n', start);
+                       
+                       // Handle the case where we find both of them, and the \n is before the \r
+                       if (res != -1 && rich_index != -1)
+                               if (rich_index < res) {
+                                       ending = LineEnding.Rich;
+                                       return rich_index;                              
+                               }
+                       
                        if (res != -1) {
                                if (res + 2 < line.Length && line [res + 1] == '\r' && line [res + 2] == '\n') {
                                        ending = LineEnding.Soft;
@@ -1529,16 +1544,30 @@ namespace System.Windows.Forms {
                                return res;
                        }
 
-                       res = line.IndexOf ('\n', start);
-                       if (res != -1) {
+                       if (rich_index != -1) {
                                ending = LineEnding.Rich;
-                               return res;
+                               return rich_index;
                        }
 
                        ending = LineEnding.Wrap;
                        return line.Length;
                }
 
+               // Get the line ending, but only of the types specified
+               private int GetLineEnding (string line, int start, out LineEnding ending, LineEnding type)
+               {
+                       int index = start;
+                       int last_length = 0;
+
+                       do {
+                               index = GetLineEnding (line, index + last_length, out ending);
+                               last_length = LineEndingLength (ending);
+                       } while 
+                               ((ending & type) != ending && index != -1);
+                       
+                       return index == -1 ? line.Length : index;
+               }
+               
                internal int LineEndingLength (LineEnding ending)
                {
                        switch (ending) {
@@ -1589,7 +1618,7 @@ namespace System.Windows.Forms {
                        base_line = line.line_no;
                        old_line_count = lines;
 
-                       break_index = GetLineEnding (s, 0, out ending);
+                       break_index = GetLineEnding (s, 0, out ending, LineEnding.Hard | LineEnding.Rich);
 
                        // There are no line feeds in our text to be pasted
                        if (break_index == s.Length) {
@@ -1606,7 +1635,7 @@ namespace System.Windows.Forms {
                                
                                // Insert brand new lines for any more line feeds in the inserted string
                                while (true) {
-                                       int next_break = GetLineEnding (s, break_index, out ending);
+                                       int next_break = GetLineEnding (s, break_index, out ending, LineEnding.Hard | LineEnding.Rich);
                                        
                                        if (next_break == s.Length)
                                                break;