2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 // * Neither the name of the application "Radegast", nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 using System.Text.RegularExpressions;
35 using System.Windows.Forms;
39 public class RichTextBoxPrinter : ITextPrinter
41 private RRichTextBox rtb;
43 private static readonly string urlRegexString = @"(https?://[^ \r\n]+)|(\[secondlife://[^ \]\r\n]* ?(?:[^\]\r\n]*)])|(secondlife://[^ \r\n]*)";
45 private SlUriParser uriParser;
47 public RichTextBoxPrinter(RRichTextBox textBox)
51 // Are we running mono?
52 mono = Type.GetType("Mono.Runtime") != null;
55 // On Linux we cannot do manual links
56 // so we keep using built in URL detection
57 rtb.DetectUrls = true;
60 uriParser = new SlUriParser();
61 urlRegex = new Regex(urlRegexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
65 public void InsertLink(string text)
70 public void InsertLink(string text, string hyperlink)
72 rtb.InsertLink(text, hyperlink);
75 private void FindURLs(string text)
77 StringBuilder sb = new StringBuilder();
78 string[] lineParts = urlRegex.Split(text);
81 // 'text' will be split into 1 + NumLinks*2 parts...
82 // If 'text' has no links in it:
83 // lineParts[0] = text
84 // If 'text' has one link in it:
85 // lineParts[0] = <Text before first link>
86 // lineParts[1] = <first link>
87 // lineParts[2] = <text after first link>
88 // If 'text' has two links in it:
89 // lineParts[0] = <Text before first link>
90 // lineParts[1] = <first link>
91 // lineParts[2] = <text after first link>
92 // lineParts[3] = <second link>
93 // lineParts[4] = <text after second link>
95 for (linePartIndex = 0; linePartIndex < lineParts.Length - 1; linePartIndex += 2)
97 rtb.AppendText(lineParts[linePartIndex]);
99 rtb.InsertLink(uriParser.GetLinkName(lineParts[linePartIndex + 1]), lineParts[linePartIndex+1]);
102 if (linePartIndex != lineParts.Length)
104 rtb.AppendText(lineParts[linePartIndex]);
108 #region ITextPrinter Members
110 public void PrintText(string text)
112 if (rtb.InvokeRequired)
114 rtb.Invoke(new MethodInvoker(() => rtb.AppendText(text)));
120 rtb.AppendText(text);
128 public void PrintTextLine(string text)
130 PrintText(text + Environment.NewLine);
133 public void PrintTextLine(string text, Color color)
135 if (rtb.InvokeRequired)
137 rtb.Invoke(new MethodInvoker(() => PrintTextLine(text, color)));
147 public void ClearText()
152 public string Content
164 public System.Drawing.Color ForeColor
168 return rtb.SelectionColor;
172 rtb.SelectionColor = value;
176 public System.Drawing.Color BackColor
180 return rtb.SelectionBackColor;
184 rtb.SelectionBackColor = value;
188 public System.Drawing.Font Font
192 return rtb.SelectionFont;
196 rtb.SelectionFont = value;