Example #1
0
        private void SetRtfText(string value)
        {
            bool oldReadOnly = ReadOnly;

            // if moduleHandle is zero we are using legacy rtf and need this link code
            if (!detectUrls || moduleHandle != IntPtr.Zero)
            {
                ReadOnly = false;
                base.Rtf = value;
                ReadOnly = oldReadOnly;
                return;
            }
            if (value == null)
            {
                ReadOnly = false;
                Text = "";
                ReadOnly = oldReadOnly;
                return;
            }

            // Quick check to see if current text equals last text to be set
            int hashCode = value.GetHashCode();
            if (hashCode == lastSetHashCode)
                return;
            lock (this)
            {
                lastSetHashCode = hashCode;
                try
                {
                    Cursor.Hide();
                    BeginUpdate();
                    ReadOnly = false;
                    var sb = new StringBuilder(value);
                    var matches = new List<LinkMatch>();
                    int idx = 0;
                    while (true)
                    {
                        var match = linkRegex.Match(sb.ToString(), idx);
                        if (!match.Success) break;
                        var link = match.Groups["link"];
                        var name = match.Groups["name"];
                        var m2 = new LinkMatch(link.Value, name.Value, match.Index, name.Length);
                        sb.Remove(match.Index, match.Length);
                        string linkText = string.Format("#:@{0}@:#", matches.Count.ToString());
                        sb.Insert(match.Index, linkText);
                        matches.Add(m2);
                        idx = match.Index + linkText.Length;
                    }
                    base.Rtf = sb.ToString();
                    idx = 0;
                    try
                    {
                        while (true)
                        {
                            int start = base.Find("#:@", idx, RichTextBoxFinds.NoHighlight);
                            if (start == -1) break;
                            int end = base.Find("@:#", start + 3, RichTextBoxFinds.NoHighlight);
                            if (end == -1) break; // Error?

                            base.Select(start, end - start + 3);
                            string text = base.SelectedText;
                            int link = int.Parse(text.Substring(3, text.Length - 6));
                            var match = matches[link];
                            base.SelectedText = "";
                            InsertLink(match.name, match.link, start);
                        }
                    }
                    catch
                    {
                    }
                    base.SelectionStart = 0;
                    //Point factored = new Point(0,0);
                    //SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref factored);
                    //base.ScrollToCaret();
                }
                catch
                {
                    base.Rtf = value;
                }
                finally
                {
                    try
                    {
                        ReadOnly = oldReadOnly;
                        EndUpdate();
                        Invalidate();
                        Update();
                        Cursor.Show();
                    }
                    catch
                    {
                    }
                }
            }
        }
Example #2
0
        private void SetRtfText(string value)
        {
            bool oldReadOnly = ReadOnly;

            // if moduleHandle is zero we are using legacy rtf and need this link code
            if (!detectUrls || moduleHandle != IntPtr.Zero)
            {
                ReadOnly = false;
                base.Rtf = value;
                ReadOnly = oldReadOnly;
                return;
            }
            if (value == null)
            {
                ReadOnly = false;
                Text     = "";
                ReadOnly = oldReadOnly;
                return;
            }

            // Quick check to see if current text equals last text to be set
            int hashCode = value.GetHashCode();

            if (hashCode == lastSetHashCode)
            {
                return;
            }
            lock (this)
            {
                lastSetHashCode = hashCode;
                try
                {
                    Cursor.Hide();
                    BeginUpdate();
                    ReadOnly = false;
                    var sb      = new StringBuilder(value);
                    var matches = new List <LinkMatch>();
                    int idx     = 0;
                    while (true)
                    {
                        var match = linkRegex.Match(sb.ToString(), idx);
                        if (!match.Success)
                        {
                            break;
                        }
                        var link = match.Groups["link"];
                        var name = match.Groups["name"];
                        var m2   = new LinkMatch(link.Value, name.Value, match.Index, name.Length);
                        sb.Remove(match.Index, match.Length);
                        string linkText = string.Format("#:@{0}@:#", matches.Count.ToString());
                        sb.Insert(match.Index, linkText);
                        matches.Add(m2);
                        idx = match.Index + linkText.Length;
                    }
                    base.Rtf = sb.ToString();
                    idx      = 0;
                    try
                    {
                        while (true)
                        {
                            int start = base.Find("#:@", idx, RichTextBoxFinds.NoHighlight);
                            if (start == -1)
                            {
                                break;
                            }
                            int end = base.Find("@:#", start + 3, RichTextBoxFinds.NoHighlight);
                            if (end == -1)
                            {
                                break;            // Error?
                            }
                            base.Select(start, end - start + 3);
                            string text  = base.SelectedText;
                            int    link  = int.Parse(text.Substring(3, text.Length - 6));
                            var    match = matches[link];
                            base.SelectedText = "";
                            InsertLink(match.name, match.link, start);
                        }
                    }
                    catch
                    {
                    }
                    base.SelectionStart = 0;
                    //Point factored = new Point(0,0);
                    //SendMessage(this.Handle, (int)WindowsMessages.EM_SETSCROLLPOS, 0, ref factored);
                    //base.ScrollToCaret();
                }
                catch
                {
                    base.Rtf = value;
                }
                finally
                {
                    try
                    {
                        ReadOnly = oldReadOnly;
                        EndUpdate();
                        Invalidate();
                        Update();
                        Cursor.Show();
                    }
                    catch
                    {
                    }
                }
            }
        }