New test.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RichTextBox.cs
index 1246a127b5646fb1c68ad2bd64b49f9bf7ffe31d..5df73365faef77bc02b950b8cb5e8fcd902c5b35 100644 (file)
@@ -17,7 +17,7 @@
 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
-// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
+// Copyright (c) 2005-2006 Novell, Inc. (http://www.novell.com)
 //
 // Authors:
 //     Peter Bartok    <pbartok@novell.com>
@@ -57,6 +57,7 @@ namespace System.Windows.Forms {
                private HorizontalAlignment rtf_rtfalign;
                private int             rtf_cursor_x;
                private int             rtf_cursor_y;
+               private int             rtf_chars;
                #endregion      // Local Variables
 
                #region Public Constructors
@@ -82,6 +83,10 @@ namespace System.Windows.Forms {
                        ForeColor = ThemeEngine.Current.ColorWindowText;
                        base.HScrolled += new EventHandler(RichTextBox_HScrolled);
                        base.VScrolled += new EventHandler(RichTextBox_VScrolled);
+
+#if NET_2_0
+                       SetStyle (ControlStyles.StandardDoubleClick, false);
+#endif
                }
                #endregion      // Public Constructors
 
@@ -261,8 +266,6 @@ namespace System.Windows.Forms {
                        get {
                                Line            start_line;
                                Line            end_line;
-                               int             current;
-                               int             total;
 
                                start_line = document.GetLine(1);
                                end_line = document.GetLine(document.Lines);
@@ -278,6 +281,8 @@ namespace System.Windows.Forms {
                                InsertRTFFromStream(data, 0, 1);
 
                                data.Close();
+
+                               Invalidate();
                        }
                }
 
@@ -305,21 +310,26 @@ namespace System.Windows.Forms {
                                MemoryStream    data;
                                int             x;
                                int             y;
+                               int             sel_start;
+                               int             chars;
                                Line            line;
+                               LineTag         tag;
 
                                if (document.selection_visible) {
-                                       document.ReplaceSelection("");
+                                       document.ReplaceSelection("", false);
                                }
 
+                               sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
+
                                data = new MemoryStream(Encoding.ASCII.GetBytes(value), false);
-                               InsertRTFFromStream(data, document.selection_start.pos, document.selection_start.line.line_no, out x, out y);
+                               InsertRTFFromStream(data, document.selection_start.pos, document.selection_start.line.line_no, out x, out y, out chars);
                                data.Close();
 
-                               line = document.GetLine(y);
-                               document.SetSelection(document.GetLine(y), x);
-                               document.PositionCaret(line, x);
+                               document.CharIndexToLineTag(sel_start + chars + (y - document.selection_start.line.line_no) * 2, out line, out tag, out sel_start);
+                               document.SetSelection(line, sel_start);
+                               document.PositionCaret(line, sel_start);
                                document.DisplayCaret();
-
+                               ScrollToCaret();
                                OnTextChanged(EventArgs.Empty);
                        }
                }
@@ -391,6 +401,32 @@ namespace System.Windows.Forms {
                        }
                }
 
+               [Browsable(false)]
+               [DefaultValue(false)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public bool SelectionBullet {
+                       get {
+                               return false;
+                       }
+
+                       set {
+                       }
+               }
+
+               [Browsable(false)]
+               [DefaultValue(0)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public int SelectionCharOffset {
+                       get {
+                               return 0;
+                       }
+
+                       set {
+                       }
+               }
+
                [Browsable(false)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public Color SelectionColor {
@@ -425,13 +461,17 @@ namespace System.Windows.Forms {
                        }
 
                        set {
-                               int     sel_start;
-                               int     sel_end;
+                               FontDefinition  attributes;
+                               int             sel_start;
+                               int             sel_end;
+
+                               attributes = new FontDefinition();
+                               attributes.color = value;
 
                                sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
                                sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
-Console.WriteLine("FIXME - SelectionColor should not alter font");
-                               document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos, document.selection_start.tag.font, new SolidBrush(value));
+
+                               document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos + 1, attributes);
 
                                document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
                                document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
@@ -475,20 +515,50 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                        }
 
                        set {
-                               int     sel_start;
-                               int     sel_end;
+                               FontDefinition  attributes;
+                               int             sel_start;
+                               int             sel_end;
+
+                               attributes = new FontDefinition();
+                               attributes.font_obj = value;
 
                                sel_start = document.LineTagToCharIndex(document.selection_start.line, document.selection_start.pos);
                                sel_end = document.LineTagToCharIndex(document.selection_end.line, document.selection_end.pos);
 
-                               document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos, value, document.selection_start.tag.color);
+                               document.FormatText(document.selection_start.line, document.selection_start.pos + 1, document.selection_end.line, document.selection_end.pos + 1, attributes);
 
                                document.CharIndexToLineTag(sel_start, out document.selection_start.line, out document.selection_start.tag, out document.selection_start.pos);
                                document.CharIndexToLineTag(sel_end, out document.selection_end.line, out document.selection_end.tag, out document.selection_end.pos);
 
                                document.UpdateView(document.selection_start.line, 0);
                                document.AlignCaret();
-                               
+
+                       }
+               }
+
+               [Browsable(false)]
+               [DefaultValue(0)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public int SelectionHangingIndent {
+                       get {
+                               return 0;
+                       }
+
+                       set {
+                       }
+               }
+
+               [Browsable(false)]
+               [DefaultValue(0)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public int SelectionIndent {
+                       get {
+                               return 0;
+                       }
+
+                       set {
                        }
                }
 
@@ -504,6 +574,44 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                        }
                }
 
+               [Browsable(false)]
+               [DefaultValue(false)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public bool SelectionProtected {
+                       get {
+                               return false;
+                       }
+
+                       set {
+                       }
+               }
+
+               [Browsable(false)]
+               [DefaultValue(0)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public int SelectionRightIndent {
+                       get {
+                               return 0;
+                       }
+
+                       set {
+                       }
+               }
+
+               [Browsable(false)]
+               [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+               [MonoTODO]
+               public int[] SelectionTabs {
+                       get {
+                               return new int[0];
+                       }
+
+                       set {
+                       }
+               }
+
                [Browsable(false)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public RichTextBoxSelectionTypes SelectionType {
@@ -521,6 +629,17 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                        }
                }
 
+               [DefaultValue(false)]
+               [MonoTODO]
+               public bool ShowSelectionMargin {
+                       get {
+                               return false;
+                       }
+
+                       set {
+                       }
+               }
+
                [Localizable(true)]
                public override string Text {
                        get {
@@ -734,8 +853,6 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                }
 
                public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
-                       RTF.RTF rtf;    // Not 'using SWF.RTF' to avoid ambiguities with font and color
-
                        document.Empty();
 
                        // FIXME - ignoring unicode
@@ -796,7 +913,7 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                }
 
                public void Paste(DataFormats.Format clipFormat) {
-                       base.Paste(clipFormat, false);
+                       base.Paste(Clipboard.GetDataObject(), clipFormat, false);
                }
 
                [MonoTODO()]
@@ -1050,7 +1167,14 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                #endregion      // Events
 
                #region Private Methods
+
+               internal override void SelectWord ()
+               {
+                       document.ExpandSelection(CaretSelection.Word, false);
+               }
+
                private void HandleControl(RTF.RTF rtf) {
+//                     Console.WriteLine ("HANDLING MAJOR:  {0}      MINOR:  {1}", rtf.Major, rtf.Minor);
                        switch(rtf.Major) {
                                case RTF.Major.Unicode: {
                                        switch(rtf.Minor) {
@@ -1069,7 +1193,7 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                                }
 
                                case RTF.Major.Destination: {
-                                       Console.Write("[Got Destination control {0}]", rtf.Minor);
+//                                     Console.Write("[Got Destination control {0}]", rtf.Minor);
                                        rtf.SkipGroup();
                                        break;
                                }
@@ -1149,7 +1273,7 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                                                        if (rtf.Param == RTF.RTF.NoParam) {
                                                                rtf_rtfstyle |= FontStyle.Underline;
                                                        } else {
-                                                               rtf_rtfstyle &= ~FontStyle.Underline;
+                                                               rtf_rtfstyle = rtf_rtfstyle & ~FontStyle.Underline;
                                                        }
                                                        break;
                                                }
@@ -1164,7 +1288,7 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                                }
 
                                case RTF.Major.SpecialChar: {
-                                       Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
+                                       //Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
                                        SpecialChar(rtf);
                                        break;
                                }
@@ -1193,52 +1317,59 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                                }
 
                                case Minor.Tab: {
-                                       Console.Write("\t");
+                                       rtf_line.Append ("\t");
+//                                     FlushText (rtf, false);
                                        break;
                                }
 
+                               case Minor.NoReqHyphen:
                                case Minor.NoBrkHyphen: {
-                                       Console.Write("-");
+                                       rtf_line.Append ("-");
+//                                     FlushText (rtf, false);
                                        break;
                                }
 
                                case Minor.Bullet: {
-                                       Console.Write("*");
+                                       Console.WriteLine("*");
                                        break;
                                }
 
+                       case Minor.WidowCtrl:
+                               break;
+
                                case Minor.EmDash: {
-                                       Console.Write("\97");
+                               rtf_line.Append ("\u2014");
                                        break;
                                }
 
                                case Minor.EnDash: {
-                                       Console.Write("\96");
+                                       rtf_line.Append ("\u2013");
                                        break;
                                }
-
+/*
                                case Minor.LQuote: {
-                                       Console.Write("\91");
+                                       Console.Write("\u2018");
                                        break;
                                }
 
                                case Minor.RQuote: {
-                                       Console.Write("\92");
+                                       Console.Write("\u2019");
                                        break;
                                }
 
                                case Minor.LDblQuote: {
-                                       Console.Write("\93");
+                                       Console.Write("\u201C");
                                        break;
                                }
 
                                case Minor.RDblQuote: {
-                                       Console.Write("\94");
+                                       Console.Write("\u201D");
                                        break;
                                }
-
+*/
                                default: {
-                                       rtf.SkipGroup();
+//                                     Console.WriteLine ("skipped special char:   {0}", rtf.Minor);
+//                                     rtf.SkipGroup();
                                        break;
                                }
                        }
@@ -1267,7 +1398,7 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                        Font            font;
 
                        length = rtf_line.Length;
-                       if (length == 0) {
+                       if (!newline && (length == 0)) {
                                return;
                        }
 
@@ -1291,14 +1422,21 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                                }
                        }
 
+                       rtf_chars += rtf_line.Length;
+
                        if (rtf_cursor_x == 0) {
                                document.Add(rtf_cursor_y, rtf_line.ToString(), rtf_rtfalign, font, rtf_color);
                        } else {
                                Line    line;
 
                                line = document.GetLine(rtf_cursor_y);
-                               document.InsertString(line, rtf_cursor_x, rtf_line.ToString());
-                               document.FormatText(line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length, font, rtf_color); // FormatText is 1-based
+                               if (rtf_line.Length > 0) {
+                                       document.InsertString(line, rtf_cursor_x, rtf_line.ToString());
+                                       document.FormatText(line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length, font, rtf_color); // FormatText is 1-based
+                               }
+                               if (newline) {
+                                       document.Split(line, rtf_cursor_x + length);
+                               }
                        }
 
                        if (newline) {
@@ -1313,11 +1451,12 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y) {
                        int     x;
                        int     y;
+                       int     chars;
 
-                       InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y);
+                       InsertRTFFromStream(data, cursor_x, cursor_y, out x, out y, out chars);
                }
 
-               private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y) {
+               private void InsertRTFFromStream(Stream data, int cursor_x, int cursor_y, out int to_x, out int to_y, out int chars) {
                        RTF.RTF         rtf;
 
                        rtf = new RTF.RTF(data);
@@ -1336,24 +1475,31 @@ Console.WriteLine("FIXME - SelectionColor should not alter font");
                        rtf_rtffont = null;
                        rtf_cursor_x = cursor_x;
                        rtf_cursor_y = cursor_y;
+                       rtf_chars = 0;
+                       rtf.DefaultFont(this.Font.Name);
 
                        rtf_text_map = new RTF.TextMap();
                        RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
 
+                       document.NoRecalc = true;
+
                        try {
                                rtf.Read();     // That's it
                                FlushText(rtf, false);
                        }
 
-                       catch (RTF.RTFException) {
-                               // Seems to be plain text...
-                               
+                       catch (RTF.RTFException e) {
+                               // Seems to be plain text or broken RTF
+                               Console.WriteLine("RTF Parsing failure: {0}", e.Message);
                        }
 
                        to_x = rtf_cursor_x;
                        to_y = rtf_cursor_y;
+                       chars = rtf_chars;
+
+                       document.RecalculateDocument(CreateGraphicsInternal(), cursor_y, document.Lines, false);
+                       document.NoRecalc = false;
 
-                       document.RecalculateDocument(CreateGraphics(), cursor_y, document.Lines, false);
                        document.Invalidate(document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
                }