Grasshopper project system now uses csproj extension
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / RichTextBox.cs
index 5df73365faef77bc02b950b8cb5e8fcd902c5b35..d307ed5ce101178be24dd0d87ed56452ca9409bb 100644 (file)
@@ -24,7 +24,7 @@
 //
 //
 
-// NOT COMPLETE
+// #define DEBUG
 
 using System;
 using System.Collections;
@@ -32,18 +32,21 @@ using System.ComponentModel;
 using System.Drawing;
 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)]
+#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;
@@ -65,12 +68,9 @@ 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;
@@ -92,17 +92,18 @@ namespace System.Windows.Forms {
 
                #region Private & Internal Methods
                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 +115,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,13 +147,8 @@ namespace System.Windows.Forms {
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
                public override System.Drawing.Image BackgroundImage {
-                       get {
-                               return background_image;
-                       }
-
-                       set {
-                               base.BackgroundImage = value;
-                       }
+                       get { return base.BackgroundImage; }
+                       set { base.BackgroundImage = value; }
                }
 
                [DefaultValue(0)]
@@ -164,7 +167,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public bool CanRedo {
                        get {
-                               return can_redo;
+                               return document.undo.CanRedo;
                        }
                }
 
@@ -200,7 +203,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);
                                }
                        }
                }
@@ -242,7 +245,7 @@ namespace System.Windows.Forms {
                [MonoTODO]
                public string RedoActionName {
                        get {
-                               return redo_action_name;
+                               return document.undo.RedoActionName;
                        }
                }
 
@@ -260,8 +263,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;
@@ -641,6 +648,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 +672,7 @@ namespace System.Windows.Forms {
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
                public string UndoActionName {
                        get {
-                               return document.undo.UndoName;
+                               return document.undo.UndoActionName;
                        }
                }
 
@@ -864,9 +874,7 @@ namespace System.Windows.Forms {
                                try {
                                        sb = new StringBuilder((int)data.Length);
                                        buffer = new byte[1024];
-                               }
-
-                               catch {
+                               } catch {
                                        throw new IOException("Not enough memory to load document");
                                }
 
@@ -880,6 +888,10 @@ namespace System.Windows.Forms {
                        }
 
                        InsertRTFFromStream(data, 0, 1);
+
+                       document.PositionCaret (document.GetLine (1), 0);
+                       document.SetSelectionToCaret (true);
+                       ScrollToCaret ();
                }
 
                [MonoTODO("Make smarter RTF detection?")]
@@ -896,15 +908,17 @@ namespace System.Windows.Forms {
 
                        data = null;
 
+
                        try {
                                data = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 1024);
+
                                LoadFile(data, fileType);
                        }
-
+#if !DEBUG
                        catch {
                                throw new IOException("Could not open file " + path);
                        }
-
+#endif
                        finally {
                                if (data != null) {
                                        data.Close();
@@ -916,8 +930,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) {
@@ -1018,9 +1033,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 +1051,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,9 +1080,9 @@ 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);
                }
 
                protected override void OnSystemColorsChanged(EventArgs e) {
@@ -1079,9 +1094,9 @@ namespace System.Windows.Forms {
                }
 
                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) {
@@ -1090,80 +1105,110 @@ namespace System.Windows.Forms {
                #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; }
+               }
 
-               public event ContentsResizedEventHandler        ContentsResized;
+               public event ContentsResizedEventHandler ContentsResized {
+                       add { Events.AddHandler (ContentsResizedEvent, value); }
+                       remove { Events.RemoveHandler (ContentsResizedEvent, value); }
+               }
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event EventHandler                       DoubleClick;
+               public new event EventHandler DoubleClick {
+                       add { base.DoubleClick += value; }
+                       remove { base.DoubleClick -= value; }
+               }
 
                [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;
-               public event EventHandler                       ImeChange;
-               public event LinkClickedEventHandler            LinkClicked;
-               public event EventHandler                       Protected;
+               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 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
@@ -1204,6 +1249,7 @@ namespace System.Windows.Forms {
                                                        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,6 +1257,7 @@ namespace System.Windows.Forms {
                                                                } else {
                                                                        this.rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
                                                                }
+                                                               FlushText (rtf, false);
                                                        }
                                                        break;
                                                }
@@ -1420,6 +1467,7 @@ namespace System.Windows.Forms {
                                } else {
                                        rtf_color = new SolidBrush(Color.FromArgb(color.Red, color.Green, color.Blue));
                                }
+                               
                        }
 
                        rtf_chars += rtf_line.Length;
@@ -1432,7 +1480,7 @@ namespace System.Windows.Forms {
                                line = document.GetLine(rtf_cursor_y);
                                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); // FormatText is 1-based
                                }
                                if (newline) {
                                        document.Split(line, rtf_cursor_x + length);
@@ -1481,26 +1529,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) {