Code Coverage Statistics for Source File
c:\Tools\SD3\src\Libraries\ICSharpCode.TextEditor\Project\Src\Gui\TextAreaDragDropHandler.cs
|
Sequence Point Coverage
N/A
0 of 0
|
Branch Coverage
N/A
0 of 0
|
Lines
127
|
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: 2727 $</version> |
|
6 |
// </file> |
|
7 |
||
8 |
using System; |
|
9 |
using System.Drawing; |
|
10 |
using System.Windows.Forms; |
|
11 |
||
12 |
using ICSharpCode.TextEditor.Document; |
|
13 |
||
14 |
namespace ICSharpCode.TextEditor |
|
15 |
{ |
|
16 |
public class TextAreaDragDropHandler |
|
17 |
{ |
|
18 |
TextArea textArea; |
|
19 |
||
20 |
public void Attach(TextArea textArea) |
|
21 |
{ |
|
22 |
this.textArea = textArea; |
|
23 |
textArea.AllowDrop = true; |
|
24 |
||
25 |
textArea.DragEnter += new DragEventHandler(OnDragEnter); |
|
26 |
textArea.DragDrop += new DragEventHandler(OnDragDrop); |
|
27 |
textArea.DragOver += new DragEventHandler(OnDragOver); |
|
28 |
} |
|
29 |
||
30 |
static DragDropEffects GetDragDropEffect(DragEventArgs e) |
|
31 |
{ |
|
32 |
if ((e.AllowedEffect & DragDropEffects.Move) > 0 && |
|
33 |
(e.AllowedEffect & DragDropEffects.Copy) > 0) { |
|
34 |
return (e.KeyState & 8) > 0 ? DragDropEffects.Copy : DragDropEffects.Move; |
|
35 |
} else if ((e.AllowedEffect & DragDropEffects.Move) > 0) { |
|
36 |
return DragDropEffects.Move; |
|
37 |
} else if ((e.AllowedEffect & DragDropEffects.Copy) > 0) { |
|
38 |
return DragDropEffects.Copy; |
|
39 |
} |
|
40 |
return DragDropEffects.None; |
|
41 |
} |
|
42 |
||
43 |
protected void OnDragEnter(object sender, DragEventArgs e) |
|
44 |
{ |
|
45 |
if (e.Data.GetDataPresent(typeof(string))) { |
|
46 |
e.Effect = GetDragDropEffect(e); |
|
47 |
} |
|
48 |
} |
|
49 |
||
50 |
||
51 |
void InsertString(int offset, string str) |
|
52 |
{ |
|
53 |
textArea.Document.Insert(offset, str); |
|
54 |
||
55 |
textArea.SelectionManager.SetSelection(new DefaultSelection(textArea.Document, |
|
56 |
textArea.Document.OffsetToPosition(offset), |
|
57 |
textArea.Document.OffsetToPosition(offset + str.Length))); |
|
58 |
textArea.Caret.Position = textArea.Document.OffsetToPosition(offset + str.Length); |
|
59 |
textArea.Refresh(); |
|
60 |
} |
|
61 |
||
62 |
protected void OnDragDrop(object sender, DragEventArgs e) |
|
63 |
{ |
|
64 |
Point p = textArea.PointToClient(new Point(e.X, e.Y)); |
|
65 |
||
66 |
if (e.Data.GetDataPresent(typeof(string))) { |
|
67 |
textArea.BeginUpdate(); |
|
68 |
try { |
|
69 |
int offset = textArea.Caret.Offset; |
|
70 |
if (textArea.TextEditorProperties.UseCustomLine |
|
71 |
&& textArea.Document.CustomLineManager.IsReadOnly(textArea.Caret.Line, false)) |
|
72 |
{ |
|
73 |
// prevent dragging text into readonly section |
|
74 |
return; |
|
75 |
} |
|
76 |
textArea.Document.UndoStack.StartUndoGroup(); |
|
77 |
if (e.Data.GetDataPresent(typeof(DefaultSelection))) { |
|
78 |
ISelection sel = (ISelection)e.Data.GetData(typeof(DefaultSelection)); |
|
79 |
if (sel.ContainsPosition(textArea.Caret.Position)) { |
|
80 |
return; |
|
81 |
} |
|
82 |
if (GetDragDropEffect(e) == DragDropEffects.Move) { |
|
83 |
if (textArea.TextEditorProperties.UseCustomLine |
|
84 |
&& textArea.Document.CustomLineManager.IsReadOnly(sel, false)) |
|
85 |
{ |
|
86 |
// prevent dragging text out of readonly section |
|
87 |
return; |
|
88 |
} |
|
89 |
int len = sel.Length; |
|
90 |
textArea.Document.Remove(sel.Offset, len); |
|
91 |
if (sel.Offset < offset) { |
|
92 |
offset -= len; |
|
93 |
} |
|
94 |
} |
|
95 |
} |
|
96 |
textArea.SelectionManager.ClearSelection(); |
|
97 |
InsertString(offset, (string)e.Data.GetData(typeof(string))); |
|
98 |
textArea.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.WholeTextArea)); |
|
99 |
} finally { |
|
100 |
textArea.Document.UndoStack.EndUndoGroup(); |
|
101 |
textArea.EndUpdate(); |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
protected void OnDragOver(object sender, DragEventArgs e) |
|
107 |
{ |
|
108 |
if (!textArea.Focused) { |
|
109 |
textArea.Focus(); |
|
110 |
} |
|
111 |
||
112 |
Point p = textArea.PointToClient(new Point(e.X, e.Y)); |
|
113 |
||
114 |
if (textArea.TextView.DrawingPosition.Contains(p.X, p.Y)) { |
|
115 |
TextLocation realmousepos= textArea.TextView.GetLogicalPosition(p.X - textArea.TextView.DrawingPosition.X, |
|
116 |
p.Y - textArea.TextView.DrawingPosition.Y); |
|
117 |
int lineNr = Math.Min(textArea.Document.TotalNumberOfLines - 1, Math.Max(0, realmousepos.Y)); |
|
118 |
||
119 |
textArea.Caret.Position = new TextLocation(realmousepos.X, lineNr); |
|
120 |
textArea.SetDesiredColumn(); |
|
121 |
if (e.Data.GetDataPresent(typeof(string))) { |
|
122 |
e.Effect = GetDragDropEffect(e); |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 |
} |