2007-10-04 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RichTextBox.cs
index 5df73365faef77bc02b950b8cb5e8fcd902c5b35..7901a63d754d4ba0cf065a43ed363dcef76ad681 100644 (file)
 //
 //
 
-// NOT COMPLETE
+// #define DEBUG
 
 using System;
 using System.Collections;
 using System.ComponentModel;
 using System.Drawing;
+using System.Drawing.Imaging;
 using System.IO;
 using System.Text;
+using System.Runtime.InteropServices;
 using RTF=System.Windows.Forms.RTF;
 
 namespace System.Windows.Forms {
+#if NET_2_0
+       [ClassInterface (ClassInterfaceType.AutoDispatch)]
+       [Docking (DockingBehavior.Ask)]
+       [ComVisible (true)]
+       [Designer ("System.Windows.Forms.Design.RichTextBoxDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
+#endif
        public class RichTextBox : TextBoxBase {
                #region Local Variables
                internal bool           auto_word_select;
                internal int            bullet_indent;
-               internal bool           can_redo;
                internal bool           detect_urls;
-               internal string         redo_action_name;
                internal int            margin_right;
-               internal string         undo_action_name;
                internal float          zoom;
 
                private RTF.TextMap     rtf_text_map;
@@ -58,6 +63,14 @@ namespace System.Windows.Forms {
                private int             rtf_cursor_x;
                private int             rtf_cursor_y;
                private int             rtf_chars;
+               private int rtf_par_line_left_indent;
+
+#if NET_2_0
+               private bool            enable_auto_drag_drop;
+               private RichTextBoxLanguageOptions language_option;
+               private bool            rich_text_shortcuts_enabled;
+               private Color           selection_back_color;
+#endif
                #endregion      // Local Variables
 
                #region Public Constructors
@@ -65,22 +78,27 @@ namespace System.Windows.Forms {
                        accepts_return = true;
                        auto_word_select = false;
                        bullet_indent = 0;
-                       can_redo = false;
                        detect_urls = true;
                        max_length = Int32.MaxValue;
-                       redo_action_name = string.Empty;
                        margin_right = 0;
-                       undo_action_name = string.Empty;
                        zoom = 1;
                        base.Multiline = true;
                        document.CRLFSize = 1;
+                       shortcuts_enabled = true;
 
                        scrollbars = RichTextBoxScrollBars.Both;
                        alignment = HorizontalAlignment.Left;
                        LostFocus += new EventHandler(RichTextBox_LostFocus);
                        GotFocus += new EventHandler(RichTextBox_GotFocus);
                        BackColor = ThemeEngine.Current.ColorWindow;
+#if NET_2_0
+                       backcolor_set = false;
+                       language_option = RichTextBoxLanguageOptions.AutoFontSizeAdjust;
+                       rich_text_shortcuts_enabled = true;
+                       selection_back_color = DefaultBackColor;
+#endif
                        ForeColor = ThemeEngine.Current.ColorWindowText;
+
                        base.HScrolled += new EventHandler(RichTextBox_HScrolled);
                        base.VScrolled += new EventHandler(RichTextBox_VScrolled);
 
@@ -91,18 +109,34 @@ namespace System.Windows.Forms {
                #endregion      // Public Constructors
 
                #region Private & Internal Methods
+               internal override Color ChangeBackColor (Color backColor)
+               {
+                       if (backColor == Color.Empty) {
+#if NET_2_0
+                               backcolor_set = false;
+                               if (!ReadOnly) {
+                                       backColor = SystemColors.Window;
+                               }
+#else
+                               backColor = SystemColors.Window;
+#endif
+                       }
+                       return backColor;
+               }
+
                private void RichTextBox_LostFocus(object sender, EventArgs e) {
-                       has_focus = false;
                        Invalidate();
                }
 
                private void RichTextBox_GotFocus(object sender, EventArgs e) {
-                       has_focus = true;
                        Invalidate();
                }
                #endregion      // Private & Internal Methods
 
                #region Public Instance Properties
+#if NET_2_0
+               [Browsable (false)]
+#endif
                public override bool AllowDrop {
                        get {
                                return base.AllowDrop;
@@ -114,7 +148,14 @@ namespace System.Windows.Forms {
                }
 
                [DefaultValue(false)]
+#if NET_2_0
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Visible)]
+               [RefreshProperties (RefreshProperties.Repaint)]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               [Browsable (false)]
+#else
                [Localizable(true)]
+#endif
                public override bool AutoSize {
                        get {
                                return auto_size;
@@ -139,14 +180,18 @@ namespace System.Windows.Forms {
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
                public override System.Drawing.Image BackgroundImage {
-                       get {
-                               return background_image;
-                       }
+                       get { return base.BackgroundImage; }
+                       set { base.BackgroundImage = value; }
+               }
 
-                       set {
-                               base.BackgroundImage = value;
-                       }
+#if NET_2_0
+               [Browsable (false)]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public override ImageLayout BackgroundImageLayout {
+                       get { return base.BackgroundImageLayout; }
+                       set { base.BackgroundImageLayout = value; }
                }
+#endif
 
                [DefaultValue(0)]
                [Localizable(true)]
@@ -164,7 +209,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public bool CanRedo {
                        get {
-                               return can_redo;
+                               return document.undo.CanRedo;
                        }
                }
 
@@ -179,6 +224,15 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if NET_2_0
+               [MonoTODO ("Stub")]
+               [DefaultValue (false)]
+               public bool EnableAutoDragDrop {
+                       get { return enable_auto_drag_drop; }
+                       set { enable_auto_drag_drop = value; }
+               }
+#endif
+
                public override Font Font {
                        get {
                                return base.Font;
@@ -200,7 +254,7 @@ namespace System.Windows.Forms {
                                        // Font changes always set the whole doc to that font
                                        start = document.GetLine(1);
                                        end = document.GetLine(document.Lines);
-                                       document.FormatText(start, 1, end, end.text.Length + 1, base.Font, new SolidBrush(this.ForeColor));
+                                       document.FormatText(start, 1, end, end.text.Length + 1, base.Font, null, null, FormatSpecified.Font);
                                }
                        }
                }
@@ -215,6 +269,16 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if NET_2_0
+               [MonoTODO ("Stub")]
+               [Browsable (false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public RichTextBoxLanguageOptions LanguageOption {
+                       get { return language_option; }
+                       set { language_option = value; }
+               }
+#endif
+
                [DefaultValue(Int32.MaxValue)]
                public override int MaxLength {
                        get {
@@ -229,7 +293,7 @@ namespace System.Windows.Forms {
                [DefaultValue(true)]
                public override bool Multiline {
                        get {
-                               return multiline;
+                               return base.Multiline;
                        }
 
                        set {
@@ -242,10 +306,21 @@ namespace System.Windows.Forms {
                [MonoTODO]
                public string RedoActionName {
                        get {
-                               return redo_action_name;
+                               return document.undo.RedoActionName;
                        }
                }
 
+#if NET_2_0
+               [MonoTODO ("Stub")]
+               [Browsable (false)]
+               [DefaultValue (true)]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public bool RichTextShortcutsEnabled {
+                       get { return rich_text_shortcuts_enabled; }
+                       set { rich_text_shortcuts_enabled = value; }
+               }
+#endif
+
                [DefaultValue(0)]
                [Localizable(true)]
                [MonoTODO("Teach TextControl.RecalculateLine to consider the right margin as well")]
@@ -260,8 +335,12 @@ namespace System.Windows.Forms {
                }
 
                [Browsable(false)]
-               [DefaultValue("")]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
+#if NET_2_0
+               [RefreshProperties (RefreshProperties.All)]
+#else
+               [DefaultValue("")]
+#endif
                public string Rtf {
                        get {
                                Line            start_line;
@@ -294,7 +373,14 @@ namespace System.Windows.Forms {
                        }
 
                        set {
-                               scrollbars = value;
+                               if (!Enum.IsDefined (typeof (RichTextBoxScrollBars), value))
+                                       throw new InvalidEnumArgumentException ("value", (int) value,
+                                               typeof (RichTextBoxScrollBars));
+
+                               if (value != scrollbars) {
+                                       scrollbars = value;
+                                       CalculateDocument ();
+                               }
                        }
                }
 
@@ -306,7 +392,7 @@ namespace System.Windows.Forms {
                                return GenerateRTF(document.selection_start.line, document.selection_start.pos, document.selection_end.line, document.selection_end.pos).ToString();
                        }
 
-                       set {
+                       set {                           
                                MemoryStream    data;
                                int             x;
                                int             y;
@@ -401,6 +487,16 @@ namespace System.Windows.Forms {
                        }
                }
 
+#if NET_2_0
+               [MonoTODO ("Stub")]
+               [Browsable (false)]
+               [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
+               public Color SelectionBackColor {
+                       get { return selection_back_color; }
+                       set { selection_back_color = value; }
+               }
+#endif
+
                [Browsable(false)]
                [DefaultValue(false)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
@@ -436,42 +532,32 @@ namespace System.Windows.Forms {
                                LineTag end;
                                LineTag tag;
 
-                               start = document.selection_start.tag;
-                               end = document.selection_end.tag;
-                               color = ((SolidBrush)document.selection_start.tag.color).Color;
+                               start = document.selection_start.line.FindTag (document.selection_start.pos);
+                               end = document.selection_start.line.FindTag (document.selection_end.pos);
+                               color = start.color.Color;
 
                                tag = start;
-                               while (true) {
-                                       if (!color.Equals(((SolidBrush)tag.color).Color)) {
-                                               return Color.Empty;
-                                       }
+                               while (tag != null && tag != end) {
 
-                                       if (tag == end) {
-                                               break;
-                                       }
-
-                                       tag = document.NextTag(tag);
+                                       if (!color.Equals (tag.color.Color))
+                                               return Color.Empty;
 
-                                       if (tag == null) {
-                                               break;
-                                       }
+                                       tag = document.NextTag (tag);
                                }
 
                                return color;
                        }
 
                        set {
-                               FontDefinition  attributes;
-                               int             sel_start;
-                               int             sel_end;
-
-                               attributes = new FontDefinition();
-                               attributes.color = value;
+                               int sel_start;
+                               int sel_end;
 
                                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 + 1, attributes);
+                               document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
+                                               document.selection_end.line, document.selection_end.pos + 1, null,
+                                               new SolidBrush (value), null, FormatSpecified.Color);
 
                                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);
@@ -490,24 +576,18 @@ namespace System.Windows.Forms {
                                LineTag end;
                                LineTag tag;
 
-                               start = document.selection_start.tag;
-                               end = document.selection_end.tag;
-                               font = document.selection_start.tag.font;
-
-                               tag = start;
-                               while (true) {
-                                       if (!font.Equals(tag.font)) {
-                                               return null;
-                                       }
+                               start = document.selection_start.line.FindTag (document.selection_start.pos);
+                               end = document.selection_start.line.FindTag (document.selection_end.pos);
+                               font = start.font;
 
-                                       if (tag == end) {
-                                               break;
-                                       }
+                               if (selection_length > 1) {
+                                       tag = start;
+                                       while (tag != null && tag != end) {
 
-                                       tag = document.NextTag(tag);
+                                               if (!font.Equals(tag.font))
+                                                       return null;
 
-                                       if (tag == null) {
-                                               break;
+                                               tag = document.NextTag (tag);
                                        }
                                }
 
@@ -515,17 +595,15 @@ namespace System.Windows.Forms {
                        }
 
                        set {
-                               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 + 1, attributes);
+                               document.FormatText (document.selection_start.line, document.selection_start.pos + 1,
+                                               document.selection_end.line, document.selection_end.pos + 1, value,
+                                               null, null, FormatSpecified.Font);
 
                                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);
@@ -641,6 +719,9 @@ namespace System.Windows.Forms {
                }
 
                [Localizable(true)]
+#if NET_2_0
+               [RefreshProperties (RefreshProperties.All)]
+#endif
                public override string Text {
                        get {
                                return base.Text;
@@ -662,7 +743,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public string UndoActionName {
                        get {
-                               return document.undo.UndoName;
+                               return document.undo.UndoActionName;
                        }
                }
 
@@ -809,6 +890,8 @@ namespace System.Windows.Forms {
                        return Find(str, -1, -1, options);
                }
 
+               
+#if !NET_2_0
                public char GetCharFromPosition(Point pt) {
                        LineTag tag;
                        int     pos;
@@ -820,10 +903,27 @@ namespace System.Windows.Forms {
                        }
 
                        return tag.line.text[pos];
-                       
                }
+#else
+               internal override char GetCharFromPositionInternal (Point p)
+               {
+                       LineTag tag;
+                       int pos;
+
+                       PointToTagPos (p, out tag, out pos);
 
-               public int GetCharIndexFromPosition(Point pt) {
+                       if (pos >= tag.line.text.Length)
+                               return '\n';
+
+                       return tag.line.text[pos];
+               }
+#endif
+
+               public
+#if NET_2_0
+               override
+#endif 
+               int GetCharIndexFromPosition(Point pt) {
                        LineTag tag;
                        int     pos;
 
@@ -832,7 +932,11 @@ namespace System.Windows.Forms {
                        return document.LineTagToCharIndex(tag.line, pos);
                }
 
-               public int GetLineFromCharIndex(int index) {
+               public
+#if NET_2_0
+               override
+#endif
+               int GetLineFromCharIndex(int index) {
                        Line    line;
                        LineTag tag;
                        int     pos;
@@ -842,7 +946,11 @@ namespace System.Windows.Forms {
                        return line.LineNo - 1;
                }
 
-               public Point GetPositionFromCharIndex(int index) {
+               public
+#if NET_2_0
+               override
+#endif
+               Point GetPositionFromCharIndex(int index) {
                        Line    line;
                        LineTag tag;
                        int     pos;
@@ -855,31 +963,34 @@ namespace System.Windows.Forms {
                public void LoadFile(System.IO.Stream data, RichTextBoxStreamType fileType) {
                        document.Empty();
 
+                       
                        // FIXME - ignoring unicode
                        if (fileType == RichTextBoxStreamType.PlainText) {
-                               StringBuilder   sb;
-                               int             count;
-                               byte[]          buffer;
+                               StringBuilder sb;
+                               char[] buffer;
 
                                try {
-                                       sb = new StringBuilder((int)data.Length);
-                                       buffer = new byte[1024];
-                               }
-
-                               catch {
+                                       sb = new StringBuilder ((int) data.Length);
+                                       buffer = new char [1024];
+                               } catch {
                                        throw new IOException("Not enough memory to load document");
                                }
 
-                               count = 0;
-                               while (count < data.Length) {
-                                       count += data.Read(buffer, count, 1024);
-                                       sb.Append(buffer);
+                               StreamReader sr = new StreamReader (data, Encoding.Default, true);
+                               int charsRead = sr.Read (buffer, 0, buffer.Length);
+                               while (charsRead > 0) {
+                                       sb.Append (buffer, 0, charsRead);
+                                       charsRead = sr.Read (buffer, 0, buffer.Length);
                                }
                                base.Text = sb.ToString();
                                return;
                        }
 
                        InsertRTFFromStream(data, 0, 1);
+
+                       document.PositionCaret (document.GetLine (1), 0);
+                       document.SetSelectionToCaret (true);
+                       ScrollToCaret ();
                }
 
                [MonoTODO("Make smarter RTF detection?")]
@@ -896,15 +1007,17 @@ namespace System.Windows.Forms {
 
                        data = null;
 
+
                        try {
                                data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
+
                                LoadFile(data, fileType);
                        }
-
-                       catch {
-                               throw new IOException("Could not open file " + path);
+#if !DEBUG
+                       catch (Exception ex) {
+                               throw new IOException("Could not open file " + path, ex);
                        }
-
+#endif
                        finally {
                                if (data != null) {
                                        data.Close();
@@ -916,8 +1029,9 @@ namespace System.Windows.Forms {
                        base.Paste(Clipboard.GetDataObject(), clipFormat, false);
                }
 
-               [MonoTODO()]
-               public void Redo() {
+               public void Redo()
+               {
+                       document.undo.Redo ();
                }
 
                public void SaveFile(Stream data, RichTextBoxStreamType fileType) {
@@ -936,7 +1050,7 @@ namespace System.Windows.Forms {
                                case RichTextBoxStreamType.PlainText: 
                                case RichTextBoxStreamType.TextTextOleObjs: 
                                case RichTextBoxStreamType.UnicodePlainText: {
-                                       if (!multiline) {
+                                       if (!Multiline) {
                                                bytes = encoding.GetBytes(document.Root.text.ToString());
                                                data.Write(bytes, 0, bytes.Length);
                                                return;
@@ -1006,6 +1120,23 @@ namespace System.Windows.Forms {
 //                     }
                }
 
+#if NET_2_0
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public new void DrawToBitmap (Bitmap bitmap, Rectangle clip)
+               {
+                       Graphics dc = Graphics.FromImage (bitmap);
+
+                       if (backcolor_set || (Enabled && !read_only)) {
+                               dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (BackColor), clip);
+                       } else {
+                               dc.FillRectangle (ThemeEngine.Current.ResPool.GetSolidBrush (ThemeEngine.Current.ColorControl), clip);
+                       }
+                       
+                       // Draw the viewable document
+                       document.Draw (dc, clip);
+               }
+#endif
+
                #endregion      // Public Instance Methods
 
                #region Protected Instance Methods
@@ -1018,9 +1149,9 @@ namespace System.Windows.Forms {
                }
 
                protected virtual void OnContentsResized(ContentsResizedEventArgs e) {
-                       if (ContentsResized != null) {
-                               ContentsResized(this, e);
-                       }
+                       ContentsResizedEventHandler eh = (ContentsResizedEventHandler)(Events [ContentsResizedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected override void OnContextMenuChanged(EventArgs e) {
@@ -1036,28 +1167,28 @@ namespace System.Windows.Forms {
                }
 
                protected virtual void OnHScroll(EventArgs e) {
-                       if (HScroll != null) {
-                               HScroll(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [HScrollEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                [MonoTODO("Determine when to call this")]
                protected virtual void OnImeChange(EventArgs e) {
-                       if (ImeChange != null) {
-                               ImeChange(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ImeChangeEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnLinkClicked(LinkClickedEventArgs e) {
-                       if (LinkClicked != null) {
-                               LinkClicked(this, e);
-                       }
+                       LinkClickedEventHandler eh = (LinkClickedEventHandler)(Events [LinkClickedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected virtual void OnProtected(EventArgs e) {
-                       if (Protected != null) {
-                               Protected(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ProtectedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected override void OnRightToLeftChanged(EventArgs e) {
@@ -1065,11 +1196,12 @@ namespace System.Windows.Forms {
                }
 
                protected virtual void OnSelectionChanged(EventArgs e) {
-                       if (SelectionChanged != null) {
-                               SelectionChanged(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [SelectionChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
+#if !NET_2_0
                protected override void OnSystemColorsChanged(EventArgs e) {
                        base.OnSystemColorsChanged (e);
                }
@@ -1077,93 +1209,142 @@ namespace System.Windows.Forms {
                protected override void OnTextChanged(EventArgs e) {
                        base.OnTextChanged (e);
                }
+#endif
 
                protected virtual void OnVScroll(EventArgs e) {
-                       if (VScroll != null) {
-                               VScroll(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [VScrollEvent]);
+                       if (eh != null)
+                               eh (this, e);
                }
 
                protected override void WndProc(ref Message m) {
                        base.WndProc (ref m);
                }
+
+#if NET_2_0
+               protected override bool ProcessCmdKey (ref Message msg, Keys keyData)
+               {
+                       return base.ProcessCmdKey (ref msg, keyData);
+               }
+#endif
                #endregion      // Protected Instance Methods
 
                #region Events
+               static object ContentsResizedEvent = new object ();
+               static object HScrollEvent = new object ();
+               static object ImeChangeEvent = new object ();
+               static object LinkClickedEvent = new object ();
+               static object ProtectedEvent = new object ();
+               static object SelectionChangedEvent = new object ();
+               static object VScrollEvent = new object ();
+
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event EventHandler                       BackgroundImageChanged;
+               public new event EventHandler BackgroundImageChanged {
+                       add { base.BackgroundImageChanged += value; }
+                       remove { base.BackgroundImageChanged -= value; }
+               }
+
+#if NET_2_0
+               [Browsable (false)]
+               [EditorBrowsable (EditorBrowsableState.Never)]
+               public new event EventHandler BackgroundImageLayoutChanged {
+                       add { base.BackgroundImageLayoutChanged += value; }
+                       remove { base.BackgroundImageLayoutChanged -= value; }
+               }
+#endif
 
-               public event ContentsResizedEventHandler        ContentsResized;
+               public event ContentsResizedEventHandler ContentsResized {
+                       add { Events.AddHandler (ContentsResizedEvent, value); }
+                       remove { Events.RemoveHandler (ContentsResizedEvent, value); }
+               }
 
+#if !NET_2_0
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event EventHandler                       DoubleClick;
+               public new event EventHandler DoubleClick {
+                       add { base.DoubleClick += value; }
+                       remove { base.DoubleClick -= value; }
+               }
+#endif
 
                [Browsable(false)]
+#if !NET_2_0
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event DragEventHandler DragDrop {
-                       add {
-                               base.DragDrop += value;
-                       }
-
-                       remove {
-                               base.DragDrop -= value;
-                       }
+#endif
+               public new event DragEventHandler DragDrop {
+                       add { base.DragDrop += value; }
+                       remove { base.DragDrop -= value; }
                }
 
                [Browsable(false)]
+#if !NET_2_0
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event DragEventHandler DragEnter {
-                       add {
-                               base.DragEnter += value;
-                       }
-
-                       remove {
-                               base.DragEnter -= value;
-                       }
+#endif
+               public new event DragEventHandler DragEnter {
+                       add { base.DragEnter += value; }
+                       remove { base.DragEnter -= value; }
                }
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event EventHandler DragLeave {
-                       add {
-                               base.DragLeave += value;
-                       }
-
-                       remove {
-                               base.DragLeave -= value;
-                       }
+               public new event EventHandler DragLeave {
+                       add { base.DragLeave += value; }
+                       remove { base.DragLeave -= value; }
                }
 
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event DragEventHandler DragOver {
-                       add {
-                               base.DragOver += value;
-                       }
-
-                       remove {
-                               base.DragOver -= value;
-                       }
+               public new event DragEventHandler DragOver {
+                       add { base.DragOver += value; }
+                       remove { base.DragOver -= value; }
                }
 
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event GiveFeedbackEventHandler           GiveFeedback;
+               public new event GiveFeedbackEventHandler GiveFeedback {
+                       add { base.GiveFeedback += value; }
+                       remove { base.GiveFeedback -= value; }
+               }
+
+               public event EventHandler HScroll {
+                       add { Events.AddHandler (HScrollEvent, value); }
+                       remove { Events.RemoveHandler (HScrollEvent, value); }
+               }
+
+               public event EventHandler ImeChange {
+                       add { Events.AddHandler (ImeChangeEvent, value); }
+                       remove { Events.RemoveHandler (ImeChangeEvent, value); }
+               }
+
+               public event LinkClickedEventHandler LinkClicked {
+                       add { Events.AddHandler (LinkClickedEvent, value); }
+                       remove { Events.RemoveHandler (LinkClickedEvent, value); }
+               }
 
-               public event EventHandler                       HScroll;
-               public event EventHandler                       ImeChange;
-               public event LinkClickedEventHandler            LinkClicked;
-               public event EventHandler                       Protected;
+               public event EventHandler Protected {
+                       add { Events.AddHandler (ProtectedEvent, value); }
+                       remove { Events.RemoveHandler (ProtectedEvent, value); }
+               }
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event QueryContinueDragEventHandler      QueryContinueDrag;
-               public event EventHandler                       SelectionChanged;
-               public event EventHandler                       VScroll;
+               public new event QueryContinueDragEventHandler QueryContinueDrag {
+                       add { base.QueryContinueDrag += value; }
+                       remove { base.QueryContinueDrag -= value; }
+               }
+
+               public event EventHandler SelectionChanged {
+                       add { Events.AddHandler (SelectionChangedEvent, value); }
+                       remove { Events.RemoveHandler (SelectionChangedEvent, value); }
+               }
+
+               public event EventHandler VScroll {
+                       add { Events.AddHandler (VScrollEvent, value); }
+                       remove { Events.RemoveHandler (VScrollEvent, value); }
+               }
                #endregion      // Events
 
                #region Private Methods
@@ -1174,16 +1355,15 @@ namespace System.Windows.Forms {
                }
 
                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) {
-                                               case Minor.UnicodeCharBytes: {
+                                               case RTF.Minor.UnicodeCharBytes: {
                                                        rtf_skip_width = rtf.Param;
                                                        break;
                                                }
 
-                                               case Minor.UnicodeChar: {
+                                               case RTF.Minor.UnicodeChar: {
                                                        rtf_skip_count += rtf_skip_width;
                                                        rtf_line.Append((char)rtf.Param);
                                                        break;
@@ -1198,12 +1378,24 @@ namespace System.Windows.Forms {
                                        break;
                                }
 
+                               case RTF.Major.PictAttr:
+                                       if (rtf.Picture != null && rtf.Picture.IsValid ()) {
+                                               Line line = document.GetLine (rtf_cursor_y);
+                                               document.InsertPicture (line, 0, rtf.Picture);
+                                               rtf_cursor_x++;
+
+                                               FlushText (rtf, true);
+                                               rtf.Picture = null;
+                                       }
+                                       break;
+
                                case RTF.Major.CharAttr: {
                                        switch(rtf.Minor) {
-                                               case Minor.ForeColor: {
+                                               case RTF.Minor.ForeColor: {
                                                        System.Windows.Forms.RTF.Color  color;
 
                                                        color = System.Windows.Forms.RTF.Color.GetColor(rtf, rtf.Param);
+                                                       
                                                        if (color != null) {
                                                                FlushText(rtf, false);
                                                                if (color.Red == -1 && color.Green == -1 && color.Blue == -1) {
@@ -1211,17 +1403,18 @@ namespace System.Windows.Forms {
                                                                } else {
                                                                        this.rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
                                                                }
+                                                               FlushText (rtf, false);
                                                        }
                                                        break;
                                                }
 
-                                               case Minor.FontSize: {
+                                               case RTF.Minor.FontSize: {
                                                        FlushText(rtf, false);
                                                        this.rtf_rtffont_size = rtf.Param / 2;
                                                        break;
                                                }
 
-                                               case Minor.FontNum: {
+                                               case RTF.Minor.FontNum: {
                                                        System.Windows.Forms.RTF.Font   font;
 
                                                        font = System.Windows.Forms.RTF.Font.GetFont(rtf, rtf.Param);
@@ -1232,13 +1425,13 @@ namespace System.Windows.Forms {
                                                        break;
                                                }
 
-                                               case Minor.Plain: {
+                                               case RTF.Minor.Plain: {
                                                        FlushText(rtf, false);
                                                        rtf_rtfstyle = FontStyle.Regular;
                                                        break;
                                                }
 
-                                               case Minor.Bold: {
+                                               case RTF.Minor.Bold: {
                                                        FlushText(rtf, false);
                                                        if (rtf.Param == RTF.RTF.NoParam) {
                                                                rtf_rtfstyle |= FontStyle.Bold;
@@ -1248,7 +1441,7 @@ namespace System.Windows.Forms {
                                                        break;
                                                }
 
-                                               case Minor.Italic: {
+                                               case RTF.Minor.Italic: {
                                                        FlushText(rtf, false);
                                                        if (rtf.Param == RTF.RTF.NoParam) {
                                                                rtf_rtfstyle |= FontStyle.Italic;
@@ -1258,7 +1451,7 @@ namespace System.Windows.Forms {
                                                        break;
                                                }
 
-                                               case Minor.StrikeThru: {
+                                               case RTF.Minor.StrikeThru: {
                                                        FlushText(rtf, false);
                                                        if (rtf.Param == RTF.RTF.NoParam) {
                                                                rtf_rtfstyle |= FontStyle.Strikeout;
@@ -1268,7 +1461,7 @@ namespace System.Windows.Forms {
                                                        break;
                                                }
 
-                                               case Minor.Underline: {
+                                               case RTF.Minor.Underline: {
                                                        FlushText(rtf, false);
                                                        if (rtf.Param == RTF.RTF.NoParam) {
                                                                rtf_rtfstyle |= FontStyle.Underline;
@@ -1278,7 +1471,7 @@ namespace System.Windows.Forms {
                                                        break;
                                                }
 
-                                               case Minor.NoUnderline: {
+                                               case RTF.Minor.NoUnderline: {
                                                        FlushText(rtf, false);
                                                        rtf_rtfstyle &= ~FontStyle.Underline;
                                                        break;
@@ -1287,6 +1480,15 @@ namespace System.Windows.Forms {
                                        break;
                                }
 
+                       case RTF.Major.ParAttr: {
+                               switch (rtf.Minor) {
+                               case RTF.Minor.LeftIndent:
+                                       rtf_par_line_left_indent = (int) (((float) rtf.Param / 1440.0F) * CreateGraphics ().DpiX + 0.5F);
+                                       break;
+                               }
+                               break;
+                       }
+                               
                                case RTF.Major.SpecialChar: {
                                        //Console.Write("[Got SpecialChar control {0}]", rtf.Minor);
                                        SpecialChar(rtf);
@@ -1297,72 +1499,72 @@ namespace System.Windows.Forms {
 
                private void SpecialChar(RTF.RTF rtf) {
                        switch(rtf.Minor) {
-                               case Minor.Page:
-                               case Minor.Sect:
-                               case Minor.Row:
-                               case Minor.Line:
-                               case Minor.Par: {
+                               case RTF.Minor.Page:
+                               case RTF.Minor.Sect:
+                               case RTF.Minor.Row:
+                               case RTF.Minor.Line:
+                               case RTF.Minor.Par: {
                                        FlushText(rtf, true);
                                        break;
                                }
 
-                               case Minor.Cell: {
+                               case RTF.Minor.Cell: {
                                        Console.Write(" ");
                                        break;
                                }
 
-                               case Minor.NoBrkSpace: {
+                               case RTF.Minor.NoBrkSpace: {
                                        Console.Write(" ");
                                        break;
                                }
 
-                               case Minor.Tab: {
+                               case RTF.Minor.Tab: {
                                        rtf_line.Append ("\t");
 //                                     FlushText (rtf, false);
                                        break;
                                }
 
-                               case Minor.NoReqHyphen:
-                               case Minor.NoBrkHyphen: {
+                               case RTF.Minor.NoReqHyphen:
+                               case RTF.Minor.NoBrkHyphen: {
                                        rtf_line.Append ("-");
 //                                     FlushText (rtf, false);
                                        break;
                                }
 
-                               case Minor.Bullet: {
+                               case RTF.Minor.Bullet: {
                                        Console.WriteLine("*");
                                        break;
                                }
 
-                       case Minor.WidowCtrl:
+                       case RTF.Minor.WidowCtrl:
                                break;
 
-                               case Minor.EmDash: {
+                               case RTF.Minor.EmDash: {
                                rtf_line.Append ("\u2014");
                                        break;
                                }
 
-                               case Minor.EnDash: {
+                               case RTF.Minor.EnDash: {
                                        rtf_line.Append ("\u2013");
                                        break;
                                }
 /*
-                               case Minor.LQuote: {
+                               case RTF.Minor.LQuote: {
                                        Console.Write("\u2018");
                                        break;
                                }
 
-                               case Minor.RQuote: {
+                               case RTF.Minor.RQuote: {
                                        Console.Write("\u2019");
                                        break;
                                }
 
-                               case Minor.LDblQuote: {
+                               case RTF.Minor.LDblQuote: {
                                        Console.Write("\u201C");
                                        break;
                                }
 
-                               case Minor.RDblQuote: {
+                               case RTF.Minor.RDblQuote: {
                                        Console.Write("\u201D");
                                        break;
                                }
@@ -1381,6 +1583,7 @@ namespace System.Windows.Forms {
                                return;
                        }
 
+                       /*
                        if ((RTF.StandardCharCode)rtf.Minor != RTF.StandardCharCode.nothing) {
                                rtf_line.Append(rtf_text_map[(RTF.StandardCharCode)rtf.Minor]);
                        } else {
@@ -1391,6 +1594,8 @@ namespace System.Windows.Forms {
                                        Console.Write("[Literal:0x{0:X2}]", (int)rtf.Major);
                                }
                        }
+                       */
+                       rtf_line.Append (rtf.EncodedText);
                }
 
                private void FlushText(RTF.RTF rtf, bool newline) {
@@ -1420,22 +1625,35 @@ namespace System.Windows.Forms {
                                } else {
                                        rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
                                }
+                               
                        }
 
                        rtf_chars += rtf_line.Length;
 
+                       
+
                        if (rtf_cursor_x == 0) {
-                               document.Add(rtf_cursor_y, rtf_line.ToString(), rtf_rtfalign, font, rtf_color);
+                               document.Add(rtf_cursor_y, rtf_line.ToString(), rtf_rtfalign, font, rtf_color,
+                                               newline ? LineEnding.Rich : LineEnding.Wrap);
+                               if (rtf_par_line_left_indent != 0) {
+                                       Line line = document.GetLine (rtf_cursor_y);
+                                       line.indent = rtf_par_line_left_indent;
+                               }
                        } else {
                                Line    line;
 
                                line = document.GetLine(rtf_cursor_y);
+                               line.indent = rtf_par_line_left_indent;
                                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
+                                       document.FormatText (line, rtf_cursor_x + 1, line, rtf_cursor_x + 1 + length,
+                                                       font, rtf_color, null,
+                                                       FormatSpecified.Font | FormatSpecified.Color);
                                }
                                if (newline) {
                                        document.Split(line, rtf_cursor_x + length);
+                                       line = document.GetLine (rtf_cursor_y);
+                                       line.ending = LineEnding.Rich;
                                }
                        }
 
@@ -1481,26 +1699,31 @@ namespace System.Windows.Forms {
                        rtf_text_map = new RTF.TextMap();
                        RTF.TextMap.SetupStandardTable(rtf_text_map.Table);
 
-                       document.NoRecalc = true;
+                       document.SuspendRecalc ();
 
                        try {
                                rtf.Read();     // That's it
                                FlushText(rtf, false);
+
                        }
 
+
                        catch (RTF.RTFException e) {
+#if DEBUG
+                               throw e;
+#endif
                                // 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.ResumeRecalc (true);
 
-                       document.Invalidate(document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
+                       document.Invalidate (document.GetLine(cursor_y), 0, document.GetLine(document.Lines), -1);
                }
 
                private void RichTextBox_HScrolled(object sender, EventArgs e) {
@@ -1645,7 +1868,7 @@ namespace System.Windows.Forms {
                                                }
                                        }
 
-                                       pos = tag.start + tag.length - 1;
+                                       pos = tag.start + tag.Length - 1;
                                        tag = tag.next;
                                }
                                pos = 0;
@@ -1726,21 +1949,21 @@ namespace System.Windows.Forms {
 
                                        // Emit the string itself
                                        if (line_no != end_line.line_no) {
-                                               EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
+                                               EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.Length - pos - 1));
                                        } else {
-                                               if (end_pos < (tag.start + tag.length - 1)) {
+                                               if (end_pos < (tag.start + tag.Length - 1)) {
                                                        // Emit partial tag only, end_pos is inside this tag
                                                        EmitRTFText(sb, tag.line.text.ToString(pos, end_pos - pos));
                                                } else {
-                                                       EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.length - pos - 1));
+                                                       EmitRTFText(sb, tag.line.text.ToString(pos, tag.start + tag.Length - pos - 1));
                                                }
                                        }
 
-                                       pos = tag.start + tag.length - 1;
+                                       pos = tag.start + tag.Length - 1;
                                        tag = tag.next;
                                }
                                if (pos >= line.text.Length) {
-                                       if (!line.soft_break) {
+                                       if (line.ending != LineEnding.Wrap) {
                                                sb.Append("\\par\n");
                                        }
                                }