17
17
* Modified By Coding Seb for the purpose of this project
18
18
\***********************************************************************************/
19
19
20
+ using System . Linq ;
21
+ using System . Text . RegularExpressions ;
20
22
using System . Windows ;
21
23
using System . Windows . Controls ;
22
24
using System . Windows . Documents ;
@@ -43,36 +45,67 @@ static SearchableTextControl()
43
45
new FrameworkPropertyMetadata ( typeof ( SearchableTextControl ) ) ) ;
44
46
}
45
47
48
+ public SearchableTextControl ( )
49
+ {
50
+ CommandBindings . Add ( new CommandBinding ( ApplicationCommands . Copy , ( sender , e ) => Clipboard . SetText ( SelectedText ) , ( sender , e ) => e . CanExecute = IsSelectable && SelectedText . Length > 0 ) ) ;
51
+ }
52
+
46
53
protected override void OnMouseLeftButtonDown ( MouseButtonEventArgs e )
47
54
{
55
+ base . OnMouseLeftButtonDown ( e ) ;
56
+
48
57
if ( displayTextBlock != null && IsSelectable )
49
58
{
50
- base . OnMouseLeftButtonDown ( e ) ;
51
- displayTextBlock . Focusable = false ;
52
- displayTextBlock . IsHitTestVisible = false ;
53
- ResetSelectionTextRange ( ) ;
54
- Point mouseDownPoint = e . GetPosition ( this ) ;
55
- StartSelectPosition = displayTextBlock . GetPositionFromPoint ( mouseDownPoint , true ) ;
56
- isSelecting = true ;
59
+ if ( e . ClickCount == 3 )
60
+ {
61
+ StartSelectPosition = displayTextBlock . ContentStart ;
62
+ EndSelectPosition = displayTextBlock . ContentEnd ;
63
+
64
+ SelectFromStartToEnd ( ) ;
65
+ }
66
+ if ( e . ClickCount == 2 )
67
+ {
68
+ int index = StartSelectPosition . DocumentStart . GetOffsetToPosition ( StartSelectPosition ) ;
69
+ Match wordMatch = Regex . Matches ( displayTextBlock . Text , @"\w+" , RegexOptions . Singleline )
70
+ . Cast < Match > ( )
71
+ . First ( match => match . Index <= index - 1 && match . Index + match . Length >= index - 1 ) ;
72
+
73
+ StartSelectPosition = displayTextBlock . ContentStart . GetPositionAtOffset ( wordMatch . Index + 1 ) ;
74
+ EndSelectPosition = displayTextBlock . ContentStart . GetPositionAtOffset ( wordMatch . Index + wordMatch . Length + 1 ) ;
75
+
76
+ SelectFromStartToEnd ( ) ;
77
+ }
78
+ else
79
+ {
80
+ CaptureMouse ( ) ;
81
+ Focus ( ) ;
82
+ displayTextBlock . Focusable = false ;
83
+ displayTextBlock . IsHitTestVisible = false ;
84
+ ResetSelectionTextRange ( ) ;
85
+ Point mouseDownPoint = e . GetPosition ( this ) ;
86
+ StartSelectPosition = displayTextBlock . GetPositionFromPoint ( mouseDownPoint , true ) ;
87
+ isSelecting = true ;
88
+ }
57
89
}
58
90
}
59
91
60
92
protected override void OnMouseMove ( MouseEventArgs e )
61
93
{
94
+ base . OnMouseMove ( e ) ;
62
95
if ( isSelecting && displayTextBlock != null && IsSelectable )
63
96
{
64
- base . OnMouseMove ( e ) ;
65
97
SelectTextFromMouseAction ( e ) ;
66
98
}
67
99
}
68
100
69
101
protected override void OnMouseLeftButtonUp ( MouseButtonEventArgs e )
70
102
{
71
103
base . OnMouseLeftButtonUp ( e ) ;
104
+ ReleaseMouseCapture ( ) ;
72
105
if ( displayTextBlock != null && IsSelectable && isSelecting )
73
106
{
74
- base . OnMouseUp ( e ) ;
75
107
SelectTextFromMouseAction ( e ) ;
108
+
76
109
isSelecting = false ;
77
110
}
78
111
}
@@ -81,6 +114,11 @@ private void SelectTextFromMouseAction(MouseEventArgs e)
81
114
{
82
115
Point mouseUpPoint = e . GetPosition ( this ) ;
83
116
EndSelectPosition = displayTextBlock . GetPositionFromPoint ( mouseUpPoint , true ) ;
117
+ SelectFromStartToEnd ( ) ;
118
+ }
119
+
120
+ private void SelectFromStartToEnd ( )
121
+ {
84
122
ResetSelectionTextRange ( ) ;
85
123
TextRange textRange = new TextRange ( StartSelectPosition , EndSelectPosition ) ;
86
124
textRange . ApplyPropertyValue ( TextElement . ForegroundProperty , SystemColors . HighlightTextBrush ) ;
0 commit comments