Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Gui\TextAreaClipboardHandler.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
252
|
Highlight:
Uncovered Code
Covered Code
| L | V | Source |
|---|---|---|
1 |
// <file> |
|
2 |
// <copyright see="prj:///doc/copyright.txt"/> |
|
3 |
// <license see="prj:///doc/license.txt"/> |
|
4 |
// <owner name="Mike Krüger" email="mike@icsharpcode.net"/> |
|
5 |
// <version>$Revision: 2719 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Drawing; |
|
10 |
using System.IO; |
|
11 |
using System.Runtime.InteropServices; |
|
12 |
using System.Windows.Forms; |
|
13 |
||
14 |
using ICSharpCode.TextEditor.Document; |
|
15 |
using ICSharpCode.TextEditor.Util; |
|
16 |
||
17 |
namespace ICSharpCode.TextEditor |
|
18 |
{ |
|
19 |
public class TextAreaClipboardHandler |
|
20 |
{ |
|
21 |
TextArea textArea; |
|
22 |
||
23 |
public bool EnableCut { |
|
24 |
get { |
|
25 |
return textArea.EnableCutOrPaste; //textArea.SelectionManager.HasSomethingSelected; |
|
26 |
} |
|
27 |
} |
|
28 |
||
29 |
public bool EnableCopy { |
|
30 |
get { |
|
31 |
return true; //textArea.SelectionManager.HasSomethingSelected; |
|
32 |
} |
|
33 |
} |
|
34 |
||
35 |
public bool EnablePaste { |
|
36 |
get { |
|
37 |
try { |
|
38 |
return Clipboard.ContainsText(); |
|
39 |
} catch (ExternalException) { |
|
40 |
return false; |
|
41 |
} |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
public bool EnableDelete { |
|
46 |
get { |
|
47 |
return textArea.SelectionManager.HasSomethingSelected && textArea.EnableCutOrPaste; |
|
48 |
} |
|
49 |
} |
|
50 |
||
51 |
public bool EnableSelectAll { |
|
52 |
get { |
|
53 |
return true; |
|
54 |
} |
|
55 |
} |
|
56 |
||
57 |
public TextAreaClipboardHandler(TextArea textArea) |
|
58 |
{ |
|
59 |
this.textArea = textArea; |
|
60 |
textArea.SelectionManager.SelectionChanged += new EventHandler(DocumentSelectionChanged); |
|
61 |
} |
|
62 |
||
63 |
void DocumentSelectionChanged(object sender, EventArgs e) |
|
64 |
{ |
|
65 |
// ((DefaultWorkbench)WorkbenchSingleton.Workbench).UpdateToolbars(); |
|
66 |
} |
|
67 |
||
68 |
string LineSelectedType |
|
69 |
{ |
|
70 |
get { |
|
71 |
return "MSDEVLineSelect"; // This is the type VS 2003 and 2005 use for flagging a whole line copy |
|
72 |
} |
|
73 |
} |
|
74 |
||
75 |
bool CopyTextToClipboard(string stringToCopy, bool asLine) |
|
76 |
{ |
|
77 |
if (stringToCopy.Length > 0) { |
|
78 |
DataObject dataObject = new DataObject(); |
|
79 |
dataObject.SetData(DataFormats.UnicodeText, true, stringToCopy); |
|
80 |
if (asLine) { |
|
81 |
MemoryStream lineSelected = new MemoryStream(1); |
|
82 |
lineSelected.WriteByte(1); |
|
83 |
dataObject.SetData(LineSelectedType, false, lineSelected); |
|
84 |
} |
|
85 |
// Default has no highlighting, therefore we don't need RTF output |
|
86 |
if (textArea.Document.HighlightingStrategy.Name != "Default") { |
|
87 |
dataObject.SetData(DataFormats.Rtf, RtfWriter.GenerateRtf(textArea)); |
|
88 |
} |
|
89 |
OnCopyText(new CopyTextEventArgs(stringToCopy)); |
|
90 |
||
91 |
SafeSetClipboard(dataObject); |
|
92 |
return true; |
|
93 |
} else { |
|
94 |
return false; |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
// Code duplication: TextAreaClipboardHandler.cs also has SafeSetClipboard |
|
99 |
[ThreadStatic] static int SafeSetClipboardDataVersion; |
|
100 |
||
101 |
static void SafeSetClipboard(object dataObject) |
|
102 |
{ |
|
103 |
// Work around ExternalException bug. (SD2-426) |
|
104 |
// Best reproducable inside Virtual PC. |
|
105 |
int version = unchecked(++SafeSetClipboardDataVersion); |
|
106 |
try { |
|
107 |
Clipboard.SetDataObject(dataObject, true); |
|
108 |
} catch (ExternalException) { |
|
109 |
Timer timer = new Timer(); |
|
110 |
timer.Interval = 100; |
|
111 |
timer.Tick += delegate { |
|
112 |
timer.Stop(); |
|
113 |
timer.Dispose(); |
|
114 |
if (SafeSetClipboardDataVersion == version) { |
|
115 |
try { |
|
116 |
Clipboard.SetDataObject(dataObject, true, 10, 50); |
|
117 |
} catch (ExternalException) { } |
|
118 |
} |
|
119 |
}; |
|
120 |
timer.Start(); |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
bool CopyTextToClipboard(string stringToCopy) |
|
125 |
{ |
|
126 |
return CopyTextToClipboard(stringToCopy, false); |
|
127 |
} |
|
128 |
||
129 |
public void Cut(object sender, EventArgs e) |
|
130 |
{ |
|
131 |
if (textArea.TextEditorProperties.UseCustomLine == true) { |
|
132 |
if (textArea.SelectionManager.HasSomethingSelected) { |
|
133 |
if (textArea.SelectionManager.SelectionIsReadonly) |
|
134 |
return; |
|
135 |
} else if (textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false) == true) |
|
136 |
return; |
|
137 |
} |
|
138 |
if (CopyTextToClipboard(textArea.SelectionManager.SelectedText)) { |
|
139 |
// Remove text |
|
140 |
textArea.BeginUpdate(); |
|
141 |
textArea.Caret.Position = textArea.SelectionManager.SelectionCollection[0].StartPosition; |
|
142 |
textArea.SelectionManager.RemoveSelectedText(); |
|
143 |
textArea.EndUpdate(); |
|
144 |
} else if (textArea.Document.TextEditorProperties.CutCopyWholeLine){ |
|
145 |
// No text was selected, select and cut the entire line |
|
146 |
int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset); |
|
147 |
LineSegment lineWhereCaretIs = textArea.Document.GetLineSegment(curLineNr); |
|
148 |
string caretLineText = textArea.Document.GetText(lineWhereCaretIs.Offset, lineWhereCaretIs.TotalLength); |
|
149 |
textArea.SelectionManager.SetSelection(textArea.Document.OffsetToPosition(lineWhereCaretIs.Offset), textArea.Document.OffsetToPosition(lineWhereCaretIs.Offset + lineWhereCaretIs.TotalLength)); |
|
150 |
if (CopyTextToClipboard(caretLineText, true)) { |
|
151 |
// remove line |
|
152 |
textArea.BeginUpdate(); |
|
153 |
textArea.Caret.Position = textArea.Document.OffsetToPosition(lineWhereCaretIs.Offset); |
|
154 |
textArea.SelectionManager.RemoveSelectedText(); |
|
155 |
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.PositionToEnd, new TextLocation(0, curLineNr))); |
|
156 |
textArea.EndUpdate(); |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
public void Copy(object sender, EventArgs e) |
|
162 |
{ |
|
163 |
if (!CopyTextToClipboard(textArea.SelectionManager.SelectedText) && textArea.Document.TextEditorProperties.CutCopyWholeLine) { |
|
164 |
// No text was selected, select the entire line, copy it, and then deselect |
|
165 |
int curLineNr = textArea.Document.GetLineNumberForOffset(textArea.Caret.Offset); |
|
166 |
LineSegment lineWhereCaretIs = textArea.Document.GetLineSegment(curLineNr); |
|
167 |
string caretLineText = textArea.Document.GetText(lineWhereCaretIs.Offset, lineWhereCaretIs.TotalLength); |
|
168 |
CopyTextToClipboard(caretLineText, true); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
public void Paste(object sender, EventArgs e) |
|
173 |
{ |
|
174 |
if (textArea.TextEditorProperties.UseCustomLine == true) { |
|
175 |
if (textArea.SelectionManager.HasSomethingSelected) { |
|
176 |
if (textArea.SelectionManager.SelectionIsReadonly) |
|
177 |
return; |
|
178 |
} else if (textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false) == true) |
|
179 |
return; |
|
180 |
} |
|
181 |
// Clipboard.GetDataObject may throw an exception... |
|
182 |
for (int i = 0;; i++) { |
|
183 |
try { |
|
184 |
IDataObject data = Clipboard.GetDataObject(); |
|
185 |
bool fullLine = data.GetDataPresent(LineSelectedType); |
|
186 |
if (data.GetDataPresent(DataFormats.UnicodeText)) { |
|
187 |
string text = (string)data.GetData(DataFormats.UnicodeText); |
|
188 |
if (text.Length > 0) { |
|
189 |
textArea.Document.UndoStack.StartUndoGroup(); |
|
190 |
try { |
|
191 |
if (textArea.SelectionManager.HasSomethingSelected) { |
|
192 |
Delete(sender, e); |
|
193 |
} |
|
194 |
if (fullLine) { |
|
195 |
int col = textArea.Caret.Column; |
|
196 |
textArea.Caret.Column = 0; |
|
197 |
textArea.InsertString(text); |
|
198 |
textArea.Caret.Column = col; |
|
199 |
} |
|
200 |
else { |
|
201 |
textArea.InsertString(text); |
|
202 |
} |
|
203 |
} finally { |
|
204 |
textArea.Document.UndoStack.EndUndoGroup(); |
|
205 |
} |
|
206 |
} |
|
207 |
} |
|
208 |
return; |
|
209 |
} catch (ExternalException) { |
|
210 |
// GetDataObject does not provide RetryTimes parameter |
|
211 |
if (i > 5) throw; |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
||
216 |
public void Delete(object sender, EventArgs e) |
|
217 |
{ |
|
218 |
new ICSharpCode.TextEditor.Actions.Delete().Execute(textArea); |
|
219 |
} |
|
220 |
||
221 |
public void SelectAll(object sender, EventArgs e) |
|
222 |
{ |
|
223 |
new ICSharpCode.TextEditor.Actions.SelectWholeDocument().Execute(textArea); |
|
224 |
} |
|
225 |
||
226 |
protected virtual void OnCopyText(CopyTextEventArgs e) |
|
227 |
{ |
|
228 |
if (CopyText != null) { |
|
229 |
CopyText(this, e); |
|
230 |
} |
|
231 |
} |
|
232 |
||
233 |
public event CopyTextEventHandler CopyText; |
|
234 |
} |
|
235 |
||
236 |
public delegate void CopyTextEventHandler(object sender, CopyTextEventArgs e); |
|
237 |
public class CopyTextEventArgs : EventArgs |
|
238 |
{ |
|
239 |
string text; |
|
240 |
||
241 |
public string Text { |
|
242 |
get { |
|
243 |
return text; |
|
244 |
} |
|
245 |
} |
|
246 |
||
247 |
public CopyTextEventArgs(string text) |
|
248 |
{ |
|
249 |
this.text = text; |
|
250 |
} |
|
251 |
} |
|
252 |
} |