OSDN Git Service

a12dcbfca883d6a015d044cb88c02da0cc4e39ef
[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.RegularExpressions;
33 using System.Drawing;
34 using System.Windows.Forms;
35
36 namespace Radegast
37 {
38     public class RichTextBoxPrinter : ITextPrinter
39     {
40         private RRichTextBox rtb;
41         private bool mono;
42         private static readonly string urlRegexString = @"\b(?<url>(https?|secondlife)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])";
43         Regex urlRegex;
44
45         public RichTextBoxPrinter(RRichTextBox textBox)
46         {
47             rtb = textBox;
48
49             // Are we running mono?
50             mono = Type.GetType("Mono.Runtime") != null;
51             if (mono)
52             {
53                 // On Linux we cannot do manual links
54                 // so we keep using built in URL detection
55                 rtb.DetectUrls = true;
56             }
57
58             urlRegex = new Regex(urlRegexString, RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
59
60         }
61
62         public void InsertLink(string text)
63         {
64             rtb.InsertLink(text);
65         }
66
67         public void InsertLink(string text, string hyperlink)
68         {
69             rtb.InsertLink(text, hyperlink);
70         }
71
72         private void FindURLs(string text)
73         {
74             Match m = urlRegex.Match(text);
75             
76             if (!m.Success)
77             {
78                 rtb.AppendText(text);
79                 return;
80             }
81
82             int pos = 0;
83             do
84             {
85                 rtb.AppendText(text.Substring(pos, m.Index - pos));
86                 pos = m.Index + m.Length;
87                 Color c = ForeColor;
88                 rtb.InsertLink(m.Groups[1].Value);
89                 ForeColor = c;
90             }
91             while ((m = m.NextMatch()).Success);
92
93             rtb.AppendText(text.Substring(pos));
94         }
95
96         #region ITextPrinter Members
97
98         public void PrintText(string text)
99         {
100             if (rtb.InvokeRequired)
101             {
102                 rtb.Invoke(new MethodInvoker(() => rtb.AppendText(text)));
103                 return;
104             }
105
106             if (mono)
107             {
108                 rtb.AppendText(text);
109             }
110             else
111             {
112                 FindURLs(text);
113             }
114         }
115
116         public void PrintTextLine(string text)
117         {
118             PrintText(text + Environment.NewLine);
119         }
120
121         public void PrintTextLine(string text, Color color)
122         {
123             if (rtb.InvokeRequired)
124             {
125                 rtb.Invoke(new MethodInvoker(() => PrintTextLine(text, color)));
126                 return;
127             }
128
129             Color c = ForeColor;
130             ForeColor = color;
131             PrintTextLine(text);
132             ForeColor = c;
133         }
134
135         public void ClearText()
136         {
137             rtb.Clear();
138         }
139
140         public string Content
141         {
142             get
143             {
144                 return rtb.Text;
145             }
146             set
147             {
148                 rtb.Text = value;
149             }
150         }
151
152         public System.Drawing.Color ForeColor
153         {
154             get
155             {
156                 return rtb.SelectionColor;
157             }
158             set
159             {
160                 rtb.SelectionColor = value;
161             }
162         }
163
164         public System.Drawing.Color BackColor
165         {
166             get
167             {
168                 return rtb.SelectionBackColor;
169             }
170             set
171             {
172                 rtb.SelectionBackColor = value;
173             }
174         }
175
176         public System.Drawing.Font Font
177         {
178             get
179             {
180                 return rtb.SelectionFont;
181             }
182             set
183             {
184                 rtb.SelectionFont = value;
185             }
186         }
187
188         #endregion
189     }
190 }