OSDN Git Service

Added a more robust Secondlife URI parser to all RichTextBoxPrinters.
[radegast/radegast.git] / Radegast / Core / RichTextBoxPrinter.cs
1 // 
2 // Radegast Metaverse Client
3 // Copyright (c) 2009-2014, Radegast Development Team
4 // All rights reserved.
5 // 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 
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.
17 // 
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.
28 //
29 // $Id$
30 //
31 using System;
32 using System.Text;
33 using System.Text.RegularExpressions;
34 using System.Drawing;
35 using System.Windows.Forms;
36
37 namespace Radegast
38 {
39     public class RichTextBoxPrinter : ITextPrinter
40     {
41         private RRichTextBox rtb;
42         private bool mono;
43         private static readonly string urlRegexString = @"(https?://[^ \r\n]+)|(\[secondlife://[^ \]\r\n]* ?(?:[^\]\r\n]*)])|(secondlife://[^ \r\n]*)";
44         Regex urlRegex;
45         private SlUriParser uriParser;
46
47         public RichTextBoxPrinter(RRichTextBox textBox)
48         {
49             rtb = textBox;
50
51             // Are we running mono?
52             mono = Type.GetType("Mono.Runtime") != null;
53             if (mono)
54             {
55                 // On Linux we cannot do manual links
56                 // so we keep using built in URL detection
57                 rtb.DetectUrls = true;
58             }
59
60             uriParser = new SlUriParser();
61             urlRegex = new Regex(urlRegexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
62
63         }
64
65         public void InsertLink(string text)
66         {
67             rtb.InsertLink(text);
68         }
69
70         public void InsertLink(string text, string hyperlink)
71         {
72             rtb.InsertLink(text, hyperlink);
73         }
74
75         private void FindURLs(string text)
76         {
77             StringBuilder sb = new StringBuilder();
78             string[] lineParts = urlRegex.Split(text);
79             int linePartIndex;
80
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>
94             // ...
95             for (linePartIndex = 0; linePartIndex < lineParts.Length - 1; linePartIndex += 2)
96             {
97                 rtb.AppendText(lineParts[linePartIndex]);
98                 Color c = ForeColor;
99                 rtb.InsertLink(uriParser.GetLinkName(lineParts[linePartIndex + 1]), lineParts[linePartIndex+1]);
100                 ForeColor = c;
101             }
102             if (linePartIndex != lineParts.Length)
103             {
104                 rtb.AppendText(lineParts[linePartIndex]);
105             }
106         }
107
108         #region ITextPrinter Members
109
110         public void PrintText(string text)
111         {
112             if (rtb.InvokeRequired)
113             {
114                 rtb.Invoke(new MethodInvoker(() => rtb.AppendText(text)));
115                 return;
116             }
117
118             if (mono)
119             {
120                 rtb.AppendText(text);
121             }
122             else
123             {
124                 FindURLs(text);
125             }
126         }
127
128         public void PrintTextLine(string text)
129         {
130             PrintText(text + Environment.NewLine);
131         }
132
133         public void PrintTextLine(string text, Color color)
134         {
135             if (rtb.InvokeRequired)
136             {
137                 rtb.Invoke(new MethodInvoker(() => PrintTextLine(text, color)));
138                 return;
139             }
140
141             Color c = ForeColor;
142             ForeColor = color;
143             PrintTextLine(text);
144             ForeColor = c;
145         }
146
147         public void ClearText()
148         {
149             rtb.Clear();
150         }
151
152         public string Content
153         {
154             get
155             {
156                 return rtb.Text;
157             }
158             set
159             {
160                 rtb.Text = value;
161             }
162         }
163
164         public System.Drawing.Color ForeColor
165         {
166             get
167             {
168                 return rtb.SelectionColor;
169             }
170             set
171             {
172                 rtb.SelectionColor = value;
173             }
174         }
175
176         public System.Drawing.Color BackColor
177         {
178             get
179             {
180                 return rtb.SelectionBackColor;
181             }
182             set
183             {
184                 rtb.SelectionBackColor = value;
185             }
186         }
187
188         public System.Drawing.Font Font
189         {
190             get
191             {
192                 return rtb.SelectionFont;
193             }
194             set
195             {
196                 rtb.SelectionFont = value;
197             }
198         }
199
200         #endregion
201     }
202 }